Synch to Gnus 200306181725.
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile
32   (require 'cl)
33   (require 'static)
34   (defvar tool-bar-map))
35
36 (require 'path-util)
37 (require 'gnus)
38 (require 'gnus-sum)
39 (require 'gnus-spec)
40 (require 'gnus-int)
41 (require 'gnus-win)
42 (require 'alist)
43 (require 'mime-view)
44 (require 'wid-edit)
45
46 ;; Avoid byte-compile warnings.
47 (eval-when-compile
48   (require 'mm-bodies)
49   (require 'mail-parse)
50   (require 'mm-decode)
51   (require 'mm-view)
52   (require 'mm-uu))
53
54 (require 'message)
55
56 (autoload 'gnus-msg-mail "gnus-msg" nil t)
57 (autoload 'gnus-button-mailto "gnus-msg")
58 (autoload 'gnus-button-reply "gnus-msg" nil t)
59
60 (defgroup gnus-article nil
61   "Article display."
62   :link '(custom-manual "(gnus)The Article Buffer")
63   :group 'gnus)
64
65 (defgroup gnus-article-treat nil
66   "Treating article parts."
67   :link '(custom-manual "(gnus)Article Hiding")
68   :group 'gnus-article)
69
70 (defgroup gnus-article-hiding nil
71   "Hiding article parts."
72   :link '(custom-manual "(gnus)Article Hiding")
73   :group 'gnus-article)
74
75 (defgroup gnus-article-highlight nil
76   "Article highlighting."
77   :link '(custom-manual "(gnus)Article Highlighting")
78   :group 'gnus-article
79   :group 'gnus-visual)
80
81 (defgroup gnus-article-signature nil
82   "Article signatures."
83   :link '(custom-manual "(gnus)Article Signature")
84   :group 'gnus-article)
85
86 (defgroup gnus-article-headers nil
87   "Article headers."
88   :link '(custom-manual "(gnus)Hiding Headers")
89   :group 'gnus-article)
90
91 (defgroup gnus-article-washing nil
92   "Special commands on articles."
93   :link '(custom-manual "(gnus)Article Washing")
94   :group 'gnus-article)
95
96 (defgroup gnus-article-emphasis nil
97   "Fontisizing articles."
98   :link '(custom-manual "(gnus)Article Fontisizing")
99   :group 'gnus-article)
100
101 (defgroup gnus-article-saving nil
102   "Saving articles."
103   :link '(custom-manual "(gnus)Saving Articles")
104   :group 'gnus-article)
105
106 (defgroup gnus-article-mime nil
107   "Worshiping the MIME wonder."
108   :link '(custom-manual "(gnus)Using MIME")
109   :group 'gnus-article)
110
111 (defgroup gnus-article-buttons nil
112   "Pushable buttons in the article buffer."
113   :link '(custom-manual "(gnus)Article Buttons")
114   :group 'gnus-article)
115
116 (defgroup gnus-article-various nil
117   "Other article options."
118   :link '(custom-manual "(gnus)Misc Article")
119   :group 'gnus-article)
120
121 (defcustom gnus-ignored-headers
122   (mapcar
123    (lambda (header)
124      (concat "^" header ":"))
125    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
126      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
127      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
128      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
129      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
130      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
131      "X-Attribution" "X-Originating-IP" "Delivered-To"
132      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
133      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
134      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
135      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
136      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
137      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
138      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
139      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
140      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
141      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
142      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
143      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
144      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
145      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
146      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
147      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
148      "List-[A-Za-z]+" "X-Listprocessor-Version"
149      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
150      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
151      "X-Received" "Content-length" "X-precedence"
152      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
153      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
154      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
155      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
156      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
157      "X-Content-length" "X-Posting-Agent" "Original-Received"
158      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
159      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
160      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
161      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
162      "X-Abuse-and-DMCA-Info" "X-Postfilter"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice :custom-show nil
167                  regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(repeat :value-to-internal (lambda (widget value)
177                                       (custom-split-regexp-maybe value))
178                  :match (lambda (widget value)
179                           (or (stringp value)
180                               (widget-editable-list-match widget value)))
181                  regexp)
182   :group 'gnus-article-hiding)
183
184 (defcustom gnus-sorted-header-list
185   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
186     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
187   "*This variable is a list of regular expressions.
188 If it is non-nil, headers that match the regular expressions will
189 be placed first in the article buffer in the sequence specified by
190 this list."
191   :type '(repeat regexp)
192   :group 'gnus-article-hiding)
193
194 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
195   "Headers that are only to be displayed if they have interesting data.
196 Possible values in this list are:
197
198   'empty       Headers with no content.
199   'newsgroups  Newsgroup identical to Gnus group.
200   'to-address  To identical to To-address.
201   'to-list     To identical to To-list.
202   'cc-list     CC identical to To-list.
203   'followup-to Followup-to identical to Newsgroups.
204   'reply-to    Reply-to identical to From.
205   'date        Date less than four days old.
206   'long-to     To and/or Cc longer than 1024 characters.
207   'many-to     Multiple To and/or Cc."
208   :type '(set (const :tag "Headers with no content." empty)
209               (const :tag "Newsgroups identical to Gnus group." newsgroups)
210               (const :tag "To identical to To-address." to-address)
211               (const :tag "To identical to To-list." to-list)
212               (const :tag "CC identical to To-list." cc-list)
213               (const :tag "Followup-to identical to Newsgroups." followup-to)
214               (const :tag "Reply-to identical to From." reply-to)
215               (const :tag "Date less than four days old." date)
216               (const :tag "To and/or Cc longer than 1024 characters." long-to)
217               (const :tag "Multiple To and/or Cc headers." many-to))
218   :group 'gnus-article-hiding)
219
220 (defcustom gnus-article-skip-boring nil
221   "Skip over text that is not worth reading.
222 By default, if you set this t, then Gnus will display citations and
223 signatures, but will never scroll down to show you a page consisting
224 only of boring text.  Boring text is controlled by
225 `gnus-article-boring-faces'."
226   :type 'boolean
227   :group 'gnus-article-hiding)
228
229 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
230   "Regexp matching signature separator.
231 This can also be a list of regexps.  In that case, it will be checked
232 from head to tail looking for a separator.  Searches will be done from
233 the end of the buffer."
234   :type '(repeat string)
235   :group 'gnus-article-signature)
236
237 (defcustom gnus-signature-limit nil
238   "Provide a limit to what is considered a signature.
239 If it is a number, no signature may not be longer (in characters) than
240 that number.  If it is a floating point number, no signature may be
241 longer (in lines) than that number.  If it is a function, the function
242 will be called without any parameters, and if it returns nil, there is
243 no signature in the buffer.  If it is a string, it will be used as a
244 regexp.  If it matches, the text in question is not a signature."
245   :type '(choice (integer :value 200)
246                  (number :value 4.0)
247                  (function :value fun)
248                  (regexp :value ".*"))
249   :group 'gnus-article-signature)
250
251 (defcustom gnus-hidden-properties '(invisible t intangible t)
252   "Property list to use for hiding text."
253   :type 'sexp
254   :group 'gnus-article-hiding)
255
256 ;; Fixme: This isn't the right thing for mixed graphical and and
257 ;; non-graphical frames in a session.
258 (defcustom gnus-article-x-face-command
259   (cond
260    (noninteractive
261     'ignore)
262    ((featurep 'xemacs)
263     (if (or (gnus-image-type-available-p 'xface)
264             (gnus-image-type-available-p 'pbm))
265         'gnus-display-x-face-in-from
266       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -"))
267    ((and (fboundp 'image-type-available-p)
268          (module-installed-p 'x-face-e21))
269     'x-face-decode-message-header)
270    ((gnus-image-type-available-p 'pbm)
271     'gnus-display-x-face-in-from)
272    ((and window-system
273          (module-installed-p 'x-face-mule))
274     'x-face-mule-gnus-article-display-x-face)
275    (t
276     "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
277 display -"))
278   "*String or function to be executed to display an X-Face header.
279 If it is a string, the command will be executed in a sub-shell
280 asynchronously.  The compressed face will be piped to this command."
281   :type `(choice
282           ,@(let (x-face-e21 x-face-mule)
283               (if (featurep 'xemacs)
284                   nil
285                 (setq x-face-e21 (module-installed-p 'x-face-e21)
286                       x-face-mule (module-installed-p 'x-face-mule)))
287               (delq nil
288                     (list
289                      'string
290                      (if (or (gnus-image-type-available-p 'xface)
291                              (gnus-image-type-available-p 'pbm))
292                          '(function-item gnus-display-x-face-in-from))
293                      (if (and x-face-e21
294                               (fboundp 'image-type-available-p))
295                          '(function-item
296                            :tag "x-face-decode-message-header (x-face-e21)"
297                            x-face-decode-message-header))
298                      (if x-face-mule
299                          '(function-item
300                            x-face-mule-gnus-article-display-x-face))
301                      'function))))
302   ;;:version "21.1"
303   :group 'gnus-picon
304   :group 'gnus-article-washing)
305
306 (defcustom gnus-article-x-face-too-ugly nil
307   "Regexp matching posters whose face shouldn't be shown automatically."
308   :type '(choice regexp (const nil))
309   :group 'gnus-article-washing)
310
311 (defcustom gnus-article-banner-alist nil
312   "Banner alist for stripping.
313 For example,
314      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
315   :version "21.1"
316   :type '(repeat (cons symbol regexp))
317   :group 'gnus-article-washing)
318
319 (gnus-define-group-parameter
320  banner
321  :variable-document
322  "Alist of regexps (to match group names) and banner."
323  :variable-group gnus-article-washing
324  :parameter-type
325  '(choice :tag "Banner"
326           :value nil
327           (const :tag "Remove signature" signature)
328           (symbol :tag "Item in `gnus-article-banner-alist'" none)
329           regexp
330           (const :tag "None" nil))
331  :parameter-document
332  "If non-nil, specify how to remove `banners' from articles.
333
334 Symbol `signature' means to remove signatures delimited by
335 `gnus-signature-separator'.  Any other symbol is used to look up a
336 regular expression to match the banner in `gnus-article-banner-alist'.
337 A string is used as a regular expression to match the banner
338 directly.")
339
340 (defcustom gnus-article-address-banner-alist nil
341   "Alist of mail addresses and banners.
342 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
343 to match a mail address in the From: header, BANNER is one of a symbol
344 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
345 If ADDRESS matches author's mail address, it will remove things like
346 advertisements.  For example:
347
348 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
349 "
350   :type '(repeat
351           (cons
352            (regexp :tag "Address")
353            (choice :tag "Banner" :value nil
354                    (const :tag "Remove signature" signature)
355                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
356                    regexp
357                    (const :tag "None" nil))))
358   :group 'gnus-article-washing)
359
360 (defcustom gnus-emphasis-alist
361   (let ((format
362          "\\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)")
363         (types
364          '(("\\*" "\\*" bold)
365            ("_" "_" underline)
366            ("/" "/" italic)
367            ("_/" "/_" underline-italic)
368            ("_\\*" "\\*_" underline-bold)
369            ("\\*/" "/\\*" bold-italic)
370            ("_\\*/" "/\\*_" underline-bold-italic))))
371     `(,@(mapcar
372          (lambda (spec)
373            (list
374             (format format (car spec) (car (cdr spec)))
375             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
376          types)
377         ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
378          2 3 gnus-emphasis-strikethru)
379         ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
380          2 3 gnus-emphasis-underline)))
381   "*Alist that says how to fontify certain phrases.
382 Each item looks like this:
383
384   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
385
386 The first element is a regular expression to be matched.  The second
387 is a number that says what regular expression grouping used to find
388 the entire emphasized word.  The third is a number that says what
389 regexp grouping should be displayed and highlighted.  The fourth
390 is the face used for highlighting."
391   :type '(repeat (list :value ("" 0 0 default)
392                        regexp
393                        (integer :tag "Match group")
394                        (integer :tag "Emphasize group")
395                        face))
396   :group 'gnus-article-emphasis)
397
398 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
399   "A regexp to describe whitespace which should not be emphasized.
400 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
401 The former avoids underlining of leading and trailing whitespace,
402 and the latter avoids underlining any whitespace at all."
403   :version "21.1"
404   :group 'gnus-article-emphasis
405   :type 'regexp)
406
407 (defface gnus-emphasis-bold '((t (:bold t)))
408   "Face used for displaying strong emphasized text (*word*)."
409   :group 'gnus-article-emphasis)
410
411 (defface gnus-emphasis-italic '((t (:italic t)))
412   "Face used for displaying italic emphasized text (/word/)."
413   :group 'gnus-article-emphasis)
414
415 (defface gnus-emphasis-underline '((t (:underline t)))
416   "Face used for displaying underlined emphasized text (_word_)."
417   :group 'gnus-article-emphasis)
418
419 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
420   "Face used for displaying underlined bold emphasized text (_*word*_)."
421   :group 'gnus-article-emphasis)
422
423 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
424   "Face used for displaying underlined italic emphasized text (_/word/_)."
425   :group 'gnus-article-emphasis)
426
427 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
428   "Face used for displaying bold italic emphasized text (/*word*/)."
429   :group 'gnus-article-emphasis)
430
431 (defface gnus-emphasis-underline-bold-italic
432   '((t (:bold t :italic t :underline t)))
433   "Face used for displaying underlined bold italic emphasized text.
434 Example: (_/*word*/_)."
435   :group 'gnus-article-emphasis)
436
437 (defface gnus-emphasis-strikethru '((t (:strikethru t)))
438   "Face used for displaying strike-through text (-word-)."
439   :group 'gnus-article-emphasis)
440
441 (defface gnus-emphasis-highlight-words
442   '((t (:background "black" :foreground "yellow")))
443   "Face used for displaying highlighted words."
444   :group 'gnus-article-emphasis)
445
446 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
447   "Format for display of Date headers in article bodies.
448 See `format-time-string' for the possible values.
449
450 The variable can also be function, which should return a complete Date
451 header.  The function is called with one argument, the time, which can
452 be fed to `format-time-string'."
453   :type '(choice string symbol)
454   :link '(custom-manual "(gnus)Article Date")
455   :group 'gnus-article-washing)
456
457 (eval-and-compile
458   (autoload 'mail-extract-address-components "mail-extr"))
459
460 (defcustom gnus-save-all-headers t
461   "*If non-nil, don't remove any headers before saving."
462   :group 'gnus-article-saving
463   :type 'boolean)
464
465 (defcustom gnus-prompt-before-saving 'always
466   "*This variable says how much prompting is to be done when saving articles.
467 If it is nil, no prompting will be done, and the articles will be
468 saved to the default files.  If this variable is `always', each and
469 every article that is saved will be preceded by a prompt, even when
470 saving large batches of articles.  If this variable is neither nil not
471 `always', there the user will be prompted once for a file name for
472 each invocation of the saving commands."
473   :group 'gnus-article-saving
474   :type '(choice (item always)
475                  (item :tag "never" nil)
476                  (sexp :tag "once" :format "%t\n" :value t)))
477
478 (defcustom gnus-saved-headers gnus-visible-headers
479   "Headers to keep if `gnus-save-all-headers' is nil.
480 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
481 If that variable is nil, however, all headers that match this regexp
482 will be kept while the rest will be deleted before saving."
483   :group 'gnus-article-saving
484   :type 'regexp)
485
486 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
487   "A function to save articles in your favourite format.
488 The function must be interactively callable (in other words, it must
489 be an Emacs command).
490
491 Gnus provides the following functions:
492
493 * gnus-summary-save-in-rmail (Rmail format)
494 * gnus-summary-save-in-mail (Unix mail format)
495 * gnus-summary-save-in-folder (MH folder)
496 * gnus-summary-save-in-file (article format)
497 * gnus-summary-save-body-in-file (article body)
498 * gnus-summary-save-in-vm (use VM's folder format)
499 * gnus-summary-write-to-file (article format -- overwrite)."
500   :group 'gnus-article-saving
501   :type '(radio (function-item gnus-summary-save-in-rmail)
502                 (function-item gnus-summary-save-in-mail)
503                 (function-item gnus-summary-save-in-folder)
504                 (function-item gnus-summary-save-in-file)
505                 (function-item gnus-summary-save-body-in-file)
506                 (function-item gnus-summary-save-in-vm)
507                 (function-item gnus-summary-write-to-file)))
508
509 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
510   "A function generating a file name to save articles in Rmail format.
511 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
512   :group 'gnus-article-saving
513   :type 'function)
514
515 (defcustom gnus-mail-save-name 'gnus-plain-save-name
516   "A function generating a file name to save articles in Unix mail format.
517 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
518   :group 'gnus-article-saving
519   :type 'function)
520
521 (defcustom gnus-folder-save-name 'gnus-folder-save-name
522   "A function generating a file name to save articles in MH folder.
523 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
524   :group 'gnus-article-saving
525   :type 'function)
526
527 (defcustom gnus-file-save-name 'gnus-numeric-save-name
528   "A function generating a file name to save articles in article format.
529 The function is called with NEWSGROUP, HEADERS, and optional
530 LAST-FILE."
531   :group 'gnus-article-saving
532   :type 'function)
533
534 (defcustom gnus-split-methods
535   '((gnus-article-archive-name)
536     (gnus-article-nndoc-name))
537   "*Variable used to suggest where articles are to be saved.
538 For instance, if you would like to save articles related to Gnus in
539 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
540 you could set this variable to something like:
541
542  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
543    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
544
545 This variable is an alist where the where the key is the match and the
546 value is a list of possible files to save in if the match is non-nil.
547
548 If the match is a string, it is used as a regexp match on the
549 article.  If the match is a symbol, that symbol will be funcalled
550 from the buffer of the article to be saved with the newsgroup as the
551 parameter.  If it is a list, it will be evaled in the same buffer.
552
553 If this form or function returns a string, this string will be used as
554 a possible file name; and if it returns a non-nil list, that list will
555 be used as possible file names."
556   :group 'gnus-article-saving
557   :type '(repeat (choice (list :value (fun) function)
558                          (cons :value ("" "") regexp (repeat string))
559                          (sexp :value nil))))
560
561 (defcustom gnus-article-display-method-for-mime
562   'gnus-article-display-mime-message
563   "Function to display a MIME message.
564 The function is called from the article buffer."
565   :group 'gnus-article-mime
566   :type 'function)
567
568 (defcustom gnus-article-display-method-for-traditional
569   'gnus-article-display-traditional-message
570   "*Function to display a traditional message.
571 The function is called from the article buffer."
572   :group 'gnus-article-mime
573   :type 'function)
574
575 (defcustom gnus-page-delimiter "^\^L"
576   "*Regexp describing what to use as article page delimiters.
577 The default value is \"^\^L\", which is a form linefeed at the
578 beginning of a line."
579   :type 'regexp
580   :group 'gnus-article-various)
581
582 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
583   "*The format specification for the article mode line.
584 See `gnus-summary-mode-line-format' for a closer description.
585
586 The following additional specs are available:
587
588 %w  The article washing status.
589 %m  The number of MIME parts in the article."
590   :type 'string
591   :group 'gnus-article-various)
592
593 (defcustom gnus-article-mode-hook nil
594   "*A hook for Gnus article mode."
595   :type 'hook
596   :group 'gnus-article-various)
597
598 (when (featurep 'xemacs)
599   ;; Extracted from gnus-xmas-define in order to preserve user settings
600   (when (fboundp 'turn-off-scroll-in-place)
601     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
602   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
603   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
604
605 (defcustom gnus-article-menu-hook nil
606   "*Hook run after the creation of the article mode menu."
607   :type 'hook
608   :group 'gnus-article-various)
609
610 (defcustom gnus-article-prepare-hook nil
611   "*A hook called after an article has been prepared in the article buffer."
612   :type 'hook
613   :group 'gnus-article-various)
614
615 (make-obsolete-variable 'gnus-article-hide-pgp-hook
616                         "This variable is obsolete in Gnus 5.10.")
617
618 (defcustom gnus-article-button-face 'bold
619   "Face used for highlighting buttons in the article buffer.
620
621 An article button is a piece of text that you can activate by pressing
622 `RET' or `mouse-2' above it."
623   :type 'face
624   :group 'gnus-article-buttons)
625
626 (defcustom gnus-article-mouse-face 'highlight
627   "Face used for mouse highlighting in the article buffer.
628
629 Article buttons will be displayed in this face when the cursor is
630 above them."
631   :type 'face
632   :group 'gnus-article-buttons)
633
634 (defcustom gnus-signature-face 'gnus-signature-face
635   "Face used for highlighting a signature in the article buffer.
636 Obsolete; use the face `gnus-signature-face' for customizations instead."
637   :type 'face
638   :group 'gnus-article-highlight
639   :group 'gnus-article-signature)
640
641 (defface gnus-signature-face
642   '((t
643      (:italic t)))
644   "Face used for highlighting a signature in the article buffer."
645   :group 'gnus-article-highlight
646   :group 'gnus-article-signature)
647
648 (defface gnus-header-from-face
649   '((((class color)
650       (background dark))
651      (:foreground "spring green"))
652     (((class color)
653       (background light))
654      (:foreground "red3"))
655     (t
656      (:italic t)))
657   "Face used for displaying from headers."
658   :group 'gnus-article-headers
659   :group 'gnus-article-highlight)
660
661 (defface gnus-header-subject-face
662   '((((class color)
663       (background dark))
664      (:foreground "SeaGreen3"))
665     (((class color)
666       (background light))
667      (:foreground "red4"))
668     (t
669      (:bold t :italic t)))
670   "Face used for displaying subject headers."
671   :group 'gnus-article-headers
672   :group 'gnus-article-highlight)
673
674 (defface gnus-header-newsgroups-face
675   '((((class color)
676       (background dark))
677      (:foreground "yellow" :italic t))
678     (((class color)
679       (background light))
680      (:foreground "MidnightBlue" :italic t))
681     (t
682      (:italic t)))
683   "Face used for displaying newsgroups headers."
684   :group 'gnus-article-headers
685   :group 'gnus-article-highlight)
686
687 (defface gnus-header-name-face
688   '((((class color)
689       (background dark))
690      (:foreground "SeaGreen"))
691     (((class color)
692       (background light))
693      (:foreground "maroon"))
694     (t
695      (:bold t)))
696   "Face used for displaying header names."
697   :group 'gnus-article-headers
698   :group 'gnus-article-highlight)
699
700 (defface gnus-header-content-face
701   '((((class color)
702       (background dark))
703      (:foreground "forest green" :italic t))
704     (((class color)
705       (background light))
706      (:foreground "indianred4" :italic t))
707     (t
708      (:italic t)))
709   "Face used for displaying header content."
710   :group 'gnus-article-headers
711   :group 'gnus-article-highlight)
712
713 (defcustom gnus-header-face-alist
714   '(("From" nil gnus-header-from-face)
715     ("Subject" nil gnus-header-subject-face)
716     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
717     ("" gnus-header-name-face gnus-header-content-face))
718   "*Controls highlighting of article header.
719
720 An alist of the form (HEADER NAME CONTENT).
721
722 HEADER is a regular expression which should match the name of an
723 header header and NAME and CONTENT are either face names or nil.
724
725 The name of each header field will be displayed using the face
726 specified by the first element in the list where HEADER match the
727 header name and NAME is non-nil.  Similarly, the content will be
728 displayed by the first non-nil matching CONTENT face."
729   :group 'gnus-article-headers
730   :group 'gnus-article-highlight
731   :type '(repeat (list (regexp :tag "Header")
732                        (choice :tag "Name"
733                                (item :tag "skip" nil)
734                                (face :value default))
735                        (choice :tag "Content"
736                                (item :tag "skip" nil)
737                                (face :value default)))))
738
739 (defcustom gnus-article-decode-hook
740   '(article-decode-group-name article-decode-idna-rhs)
741   "*Hook run to decode charsets in articles."
742   :group 'gnus-article-headers
743   :type 'hook)
744
745 (defcustom gnus-display-mime-function 'gnus-display-mime
746   "Function to display MIME articles."
747   :group 'gnus-article-mime
748   :type 'function)
749
750 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
751   "Function used to decode headers.")
752
753 (defvar gnus-article-dumbquotes-map
754   '(("\200" "EUR")
755     ("\202" ",")
756     ("\203" "f")
757     ("\204" ",,")
758     ("\205" "...")
759     ("\213" "<")
760     ("\214" "OE")
761     ("\221" "`")
762     ("\222" "'")
763     ("\223" "``")
764     ("\224" "\"")
765     ("\225" "*")
766     ("\226" "-")
767     ("\227" "--")
768     ("\230" "~")
769     ("\231" "(TM)")
770     ("\233" ">")
771     ("\234" "oe")
772     ("\264" "'"))
773   "Table for MS-to-Latin1 translation.")
774
775 (defcustom gnus-ignored-mime-types nil
776   "List of MIME types that should be ignored by Gnus."
777   :version "21.1"
778   :group 'gnus-article-mime
779   :type '(repeat regexp))
780
781 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
782   "List of MIME types that should not be given buttons when rendered inline.
783 See also `gnus-buttonized-mime-types' which may override this variable.
784 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
785   :version "21.1"
786   :group 'gnus-article-mime
787   :type '(repeat regexp))
788
789 (defcustom gnus-buttonized-mime-types nil
790   "List of MIME types that should be given buttons when rendered inline.
791 If set, this variable overrides `gnus-unbuttonized-mime-types'.
792 To see e.g. security buttons you could set this to
793 `(\"multipart/signed\")'.
794 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
795   :version "21.1"
796   :group 'gnus-article-mime
797   :type '(repeat regexp))
798
799 (defcustom gnus-inhibit-mime-unbuttonizing nil
800   "If non-nil, all MIME parts get buttons.
801 When nil (the default value), then some MIME parts do not get buttons,
802 as described by the variables `gnus-buttonized-mime-types' and
803 `gnus-unbuttonized-mime-types'."
804   :version "21.3"
805   :type 'boolean)
806
807 (defcustom gnus-body-boundary-delimiter "_"
808   "String used to delimit header and body.
809 This variable is used by `gnus-article-treat-body-boundary' which can
810 be controlled by `gnus-treat-body-boundary'."
811   :group 'gnus-article-various
812   :type '(choice (item :tag "None" :value nil)
813                  string))
814
815 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces")
816   "Defines the location of the faces database.
817 For information on obtaining this database of pretty pictures, please
818 see http://www.cs.indiana.edu/picons/ftp/index.html"
819   :type '(repeat directory)
820   :link '(url-link :tag "download"
821                    "http://www.cs.indiana.edu/picons/ftp/index.html")
822   :link '(custom-manual "(gnus)Picons")
823   :group 'gnus-picon)
824
825 (defun gnus-picons-installed-p ()
826   "Say whether picons are installed on your machine."
827   (let ((installed nil))
828     (dolist (database gnus-picon-databases)
829       (when (file-exists-p database)
830         (setq installed t)))
831     installed))
832
833 (defcustom gnus-article-mime-part-function nil
834   "Function called with a MIME handle as the argument.
835 This is meant for people who want to do something automatic based
836 on parts -- for instance, adding Vcard info to a database."
837   :group 'gnus-article-mime
838   :type 'function)
839
840 (defcustom gnus-mime-multipart-functions nil
841   "An alist of MIME types to functions to display them."
842   :version "21.1"
843   :group 'gnus-article-mime
844   :type 'alist)
845
846 (defcustom gnus-article-date-lapsed-new-header nil
847   "Whether the X-Sent and Date headers can coexist.
848 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
849 either replace the old \"Date:\" header (if this variable is nil), or
850 be added below it (otherwise)."
851   :version "21.1"
852   :group 'gnus-article-headers
853   :type 'boolean)
854
855 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
856   "Function called with a MIME handle as the argument.
857 This is meant for people who want to view first matched part.
858 For `undisplayed-alternative' (default), the first undisplayed
859 part or alternative part is used.  For `undisplayed', the first
860 undisplayed part is used.  For a function, the first part which
861 the function return t is used.  For nil, the first part is
862 used."
863   :version "21.1"
864   :group 'gnus-article-mime
865   :type '(choice
866           (item :tag "first" :value nil)
867           (item :tag "undisplayed" :value undisplayed)
868           (item :tag "undisplayed or alternative"
869                 :value undisplayed-alternative)
870           (function)))
871
872 (defcustom gnus-mime-action-alist
873   '(("save to file" . gnus-mime-save-part)
874     ("save and strip" . gnus-mime-save-part-and-strip)
875     ("delete part" . gnus-mime-delete-part)
876     ("display as text" . gnus-mime-inline-part)
877     ("view the part" . gnus-mime-view-part)
878     ("pipe to command" . gnus-mime-pipe-part)
879     ("toggle display" . gnus-article-press-button)
880     ("toggle display" . gnus-article-view-part-as-charset)
881     ("view as type" . gnus-mime-view-part-as-type)
882     ("view internally" . gnus-mime-view-part-internally)
883     ("view externally" . gnus-mime-view-part-externally))
884   "An alist of actions that run on the MIME attachment."
885   :group 'gnus-article-mime
886   :type '(repeat (cons (string :tag "name")
887                        (function))))
888
889 ;;;
890 ;;; The treatment variables
891 ;;;
892
893 (defvar gnus-part-display-hook nil
894   "Hook called on parts that are to receive treatment.")
895
896 (defvar gnus-article-treat-custom
897   '(choice (const :tag "Off" nil)
898            (const :tag "On" t)
899            (const :tag "Header" head)
900            (const :tag "Last" last)
901            (const :tag "Mime" mime)
902            (integer :tag "Less")
903            (repeat :tag "Groups" regexp)
904            (sexp :tag "Predicate")))
905
906 (defvar gnus-article-treat-head-custom
907   '(choice (const :tag "Off" nil)
908            (const :tag "Header" head)))
909
910 (defvar gnus-article-treat-types '("text/plain")
911   "Parts to treat.")
912
913 (defvar gnus-inhibit-treatment nil
914   "Whether to inhibit treatment.")
915
916 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
917   "Highlight the signature.
918 Valid values are nil, t, `head', `last', an integer or a predicate.
919 See Info node `(gnus)Customizing Articles'."
920   :group 'gnus-article-treat
921   :link '(custom-manual "(gnus)Customizing Articles")
922   :type gnus-article-treat-custom)
923 (put 'gnus-treat-highlight-signature 'highlight t)
924
925 (defcustom gnus-treat-buttonize 100000
926   "Add buttons.
927 Valid values are nil, t, `head', `last', an integer or a predicate.
928 See Info node `(gnus)Customizing Articles'."
929   :group 'gnus-article-treat
930   :link '(custom-manual "(gnus)Customizing Articles")
931   :type gnus-article-treat-custom)
932 (put 'gnus-treat-buttonize 'highlight t)
933
934 (defcustom gnus-treat-buttonize-head 'head
935   "Add buttons to the head.
936 Valid values are nil, t, `head', `last', an integer or a predicate.
937 See Info node `(gnus)Customizing Articles' for details."
938   :group 'gnus-article-treat
939   :link '(custom-manual "(gnus)Customizing Articles")
940   :type gnus-article-treat-head-custom)
941 (put 'gnus-treat-buttonize-head 'highlight t)
942
943 (defcustom gnus-treat-emphasize
944   (and (or window-system
945            (featurep 'xemacs)
946            (>= (string-to-number emacs-version) 21))
947        50000)
948   "Emphasize text.
949 Valid values are nil, t, `head', `last', an integer or a predicate.
950 See Info node `(gnus)Customizing Articles' for details."
951   :group 'gnus-article-treat
952   :link '(custom-manual "(gnus)Customizing Articles")
953   :type gnus-article-treat-custom)
954 (put 'gnus-treat-emphasize 'highlight t)
955
956 (defcustom gnus-treat-strip-cr nil
957   "Remove carriage returns.
958 Valid values are nil, t, `head', `last', an integer or a predicate.
959 See Info node `(gnus)Customizing Articles' for details."
960   :group 'gnus-article-treat
961   :link '(custom-manual "(gnus)Customizing Articles")
962   :type gnus-article-treat-custom)
963
964 (defcustom gnus-treat-unsplit-urls nil
965   "Remove newlines from within URLs.
966 Valid values are nil, t, `head', `last', an integer or a predicate.
967 See Info node `(gnus)Customizing Articles' for details."
968   :group 'gnus-article-treat
969   :link '(custom-manual "(gnus)Customizing Articles")
970   :type gnus-article-treat-custom)
971
972 (defcustom gnus-treat-leading-whitespace nil
973   "Remove leading whitespace in headers.
974 Valid values are nil, t, `head', `last', an integer or a predicate.
975 See Info node `(gnus)Customizing Articles' for details."
976   :group 'gnus-article-treat
977   :link '(custom-manual "(gnus)Customizing Articles")
978   :type gnus-article-treat-custom)
979
980 (defcustom gnus-treat-hide-headers 'head
981   "Hide headers.
982 Valid values are nil, t, `head', `last', an integer or a predicate.
983 See Info node `(gnus)Customizing Articles' for details."
984   :group 'gnus-article-treat
985   :link '(custom-manual "(gnus)Customizing Articles")
986   :type gnus-article-treat-head-custom)
987
988 (defcustom gnus-treat-hide-boring-headers nil
989   "Hide boring headers.
990 Valid values are nil, t, `head', `last', an integer or a predicate.
991 See Info node `(gnus)Customizing Articles' for details."
992   :group 'gnus-article-treat
993   :link '(custom-manual "(gnus)Customizing Articles")
994   :type gnus-article-treat-head-custom)
995
996 (defcustom gnus-treat-hide-signature nil
997   "Hide the signature.
998 Valid values are nil, t, `head', `last', an integer or a predicate.
999 See Info node `(gnus)Customizing Articles' for details."
1000   :group 'gnus-article-treat
1001   :link '(custom-manual "(gnus)Customizing Articles")
1002   :type gnus-article-treat-custom)
1003
1004 (defcustom gnus-treat-fill-article nil
1005   "Fill the article.
1006 Valid values are nil, t, `head', `last', an integer or a predicate.
1007 See Info node `(gnus)Customizing Articles' for details."
1008   :group 'gnus-article-treat
1009   :link '(custom-manual "(gnus)Customizing Articles")
1010   :type gnus-article-treat-custom)
1011
1012 (defcustom gnus-treat-hide-citation nil
1013   "Hide cited text.
1014 Valid values are nil, t, `head', `last', an integer or a predicate.
1015 See Info node `(gnus)Customizing Articles' for details."
1016   :group 'gnus-article-treat
1017   :link '(custom-manual "(gnus)Customizing Articles")
1018   :type gnus-article-treat-custom)
1019
1020 (defcustom gnus-treat-hide-citation-maybe nil
1021   "Hide cited text.
1022 Valid values are nil, t, `head', `last', an integer or a predicate.
1023 See Info node `(gnus)Customizing Articles' for details."
1024   :group 'gnus-article-treat
1025   :link '(custom-manual "(gnus)Customizing Articles")
1026   :type gnus-article-treat-custom)
1027
1028 (defcustom gnus-treat-strip-list-identifiers 'head
1029   "Strip list identifiers from `gnus-list-identifiers`.
1030 Valid values are nil, t, `head', `last', an integer or a predicate.
1031 See Info node `(gnus)Customizing Articles' for details."
1032   :version "21.1"
1033   :group 'gnus-article-treat
1034   :link '(custom-manual "(gnus)Customizing Articles")
1035   :type gnus-article-treat-custom)
1036
1037 (make-obsolete-variable 'gnus-treat-strip-pgp
1038                         "This option is obsolete in Gnus 5.10.")
1039
1040 (defcustom gnus-treat-strip-pem nil
1041   "Strip PEM signatures.
1042 Valid values are nil, t, `head', `last', an integer or a predicate.
1043 See Info node `(gnus)Customizing Articles' for details."
1044   :group 'gnus-article-treat
1045   :link '(custom-manual "(gnus)Customizing Articles")
1046   :type gnus-article-treat-custom)
1047
1048 (defcustom gnus-treat-strip-banner t
1049   "Strip banners from articles.
1050 The banner to be stripped is specified in the `banner' group parameter.
1051 Valid values are nil, t, `head', `last', an integer or a predicate.
1052 See Info node `(gnus)Customizing Articles' for details."
1053   :group 'gnus-article-treat
1054   :link '(custom-manual "(gnus)Customizing Articles")
1055   :type gnus-article-treat-custom)
1056
1057 (defcustom gnus-treat-highlight-headers 'head
1058   "Highlight the headers.
1059 Valid values are nil, t, `head', `last', an integer or a predicate.
1060 See Info node `(gnus)Customizing Articles' for details."
1061   :group 'gnus-article-treat
1062   :link '(custom-manual "(gnus)Customizing Articles")
1063   :type gnus-article-treat-head-custom)
1064 (put 'gnus-treat-highlight-headers 'highlight t)
1065
1066 (defcustom gnus-treat-highlight-citation t
1067   "Highlight cited text.
1068 Valid values are nil, t, `head', `last', an integer or a predicate.
1069 See Info node `(gnus)Customizing Articles' for details."
1070   :group 'gnus-article-treat
1071   :link '(custom-manual "(gnus)Customizing Articles")
1072   :type gnus-article-treat-custom)
1073 (put 'gnus-treat-highlight-citation 'highlight t)
1074
1075 (defcustom gnus-treat-date-ut nil
1076   "Display the Date in UT (GMT).
1077 Valid values are nil, t, `head', `last', an integer or a predicate.
1078 See Info node `(gnus)Customizing Articles' for details."
1079   :group 'gnus-article-treat
1080   :link '(custom-manual "(gnus)Customizing Articles")
1081   :type gnus-article-treat-head-custom)
1082
1083 (defcustom gnus-treat-date-local nil
1084   "Display the Date in the local timezone.
1085 Valid values are nil, t, `head', `last', an integer or a predicate.
1086 See Info node `(gnus)Customizing Articles' for details."
1087   :group 'gnus-article-treat
1088   :link '(custom-manual "(gnus)Customizing Articles")
1089   :type gnus-article-treat-head-custom)
1090
1091 (defcustom gnus-treat-date-english nil
1092   "Display the Date in a format that can be read aloud in English.
1093 Valid values are nil, t, `head', `last', an integer or a predicate.
1094 See Info node `(gnus)Customizing Articles' for details."
1095   :group 'gnus-article-treat
1096   :link '(custom-manual "(gnus)Customizing Articles")
1097   :type gnus-article-treat-head-custom)
1098
1099 (defcustom gnus-treat-date-lapsed nil
1100   "Display the Date header in a way that says how much time has elapsed.
1101 Valid values are nil, t, `head', `last', an integer or a predicate.
1102 See Info node `(gnus)Customizing Articles' for details."
1103   :group 'gnus-article-treat
1104   :link '(custom-manual "(gnus)Customizing Articles")
1105   :type gnus-article-treat-head-custom)
1106
1107 (defcustom gnus-treat-date-original nil
1108   "Display the date in the original timezone.
1109 Valid values are nil, t, `head', `last', an integer or a predicate.
1110 See Info node `(gnus)Customizing Articles' for details."
1111   :group 'gnus-article-treat
1112   :link '(custom-manual "(gnus)Customizing Articles")
1113   :type gnus-article-treat-head-custom)
1114
1115 (defcustom gnus-treat-date-iso8601 nil
1116   "Display the date in the ISO8601 format.
1117 Valid values are nil, t, `head', `last', an integer or a predicate.
1118 See Info node `(gnus)Customizing Articles' for details."
1119   :version "21.1"
1120   :group 'gnus-article-treat
1121   :link '(custom-manual "(gnus)Customizing Articles")
1122   :type gnus-article-treat-head-custom)
1123
1124 (defcustom gnus-treat-date-user-defined nil
1125   "Display the date in a user-defined format.
1126 The format is defined by the `gnus-article-time-format' variable.
1127 Valid values are nil, t, `head', `last', an integer or a predicate.
1128 See Info node `(gnus)Customizing Articles' for details."
1129   :group 'gnus-article-treat
1130   :link '(custom-manual "(gnus)Customizing Articles")
1131   :type gnus-article-treat-head-custom)
1132
1133 (defcustom gnus-treat-strip-headers-in-body t
1134   "Strip the X-No-Archive header line from the beginning of the body.
1135 Valid values are nil, t, `head', `last', an integer or a predicate.
1136 See Info node `(gnus)Customizing Articles' for details."
1137   :version "21.1"
1138   :group 'gnus-article-treat
1139   :link '(custom-manual "(gnus)Customizing Articles")
1140   :type gnus-article-treat-custom)
1141
1142 (defcustom gnus-treat-strip-trailing-blank-lines nil
1143   "Strip trailing blank lines.
1144 Valid values are nil, t, `head', `last', an integer or a predicate.
1145 See Info node `(gnus)Customizing Articles' for details."
1146   :group 'gnus-article-treat
1147   :link '(custom-manual "(gnus)Customizing Articles")
1148   :type gnus-article-treat-custom)
1149
1150 (defcustom gnus-treat-strip-leading-blank-lines nil
1151   "Strip leading blank lines.
1152 Valid values are nil, t, `head', `last', an integer or a predicate.
1153 See Info node `(gnus)Customizing Articles' for details."
1154   :group 'gnus-article-treat
1155   :link '(custom-manual "(gnus)Customizing Articles")
1156   :type gnus-article-treat-custom)
1157
1158 (defcustom gnus-treat-strip-multiple-blank-lines nil
1159   "Strip multiple blank lines.
1160 Valid values are nil, t, `head', `last', an integer or a predicate.
1161 See Info node `(gnus)Customizing Articles' for details."
1162   :group 'gnus-article-treat
1163   :link '(custom-manual "(gnus)Customizing Articles")
1164   :type gnus-article-treat-custom)
1165
1166 (defcustom gnus-treat-unfold-headers 'head
1167   "Unfold folded header lines.
1168 Valid values are nil, t, `head', `last', an integer or a predicate.
1169 See Info node `(gnus)Customizing Articles' for details."
1170   :group 'gnus-article-treat
1171   :link '(custom-manual "(gnus)Customizing Articles")
1172   :type gnus-article-treat-custom)
1173
1174 (defcustom gnus-treat-fold-headers nil
1175   "Fold headers.
1176 Valid values are nil, t, `head', `last', an integer or a predicate.
1177 See Info node `(gnus)Customizing Articles' for details."
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-custom)
1181
1182 (defcustom gnus-treat-fold-newsgroups 'head
1183   "Fold the Newsgroups and Followup-To headers.
1184 Valid values are nil, t, `head', `last', an integer or a predicate.
1185 See Info node `(gnus)Customizing Articles' for details."
1186   :group 'gnus-article-treat
1187   :link '(custom-manual "(gnus)Customizing Articles")
1188   :type gnus-article-treat-custom)
1189
1190 (defcustom gnus-treat-overstrike t
1191   "Treat overstrike highlighting.
1192 Valid values are nil, t, `head', `last', an integer or a predicate.
1193 See Info node `(gnus)Customizing Articles' for details."
1194   :group 'gnus-article-treat
1195   :link '(custom-manual "(gnus)Customizing Articles")
1196   :type gnus-article-treat-custom)
1197 (put 'gnus-treat-overstrike 'highlight t)
1198
1199 (defcustom gnus-treat-display-xface
1200   (and (not noninteractive)
1201        (or (memq gnus-article-x-face-command
1202                  '(x-face-decode-message-header
1203                    x-face-mule-gnus-article-display-x-face))
1204            (and (fboundp 'image-type-available-p)
1205                 (image-type-available-p 'xbm)
1206                 (string-match "^0x" (shell-command-to-string "uncompface"))
1207                 (executable-find "icontopbm"))
1208            (and (featurep 'xemacs)
1209                 (featurep 'xface)))
1210        'head)
1211   "Display X-Face headers.
1212 Valid values are nil, t, `head', `last', an integer or a predicate.
1213 See Info node `(gnus)Customizing Articles' and Info node
1214 `(gnus)X-Face' for details."
1215   :group 'gnus-article-treat
1216   ;;:version "21.1"
1217   :link '(custom-manual "(gnus)Customizing Articles")
1218   :link '(custom-manual "(gnus)X-Face")
1219   :type gnus-article-treat-head-custom)
1220 (put 'gnus-treat-display-xface 'highlight t)
1221
1222 (defcustom gnus-article-should-use-smiley-mule
1223   (not (or (featurep 'xemacs)
1224            (gnus-image-type-available-p 'xpm)
1225            (gnus-image-type-available-p 'pbm)))
1226   "If non-nil, gnus uses `smiley-mule' for displaying smileys rather than
1227 `smiley'.  It defaults to t when Emacs 20 or earlier is running.
1228 `smiley-mule' is boundled in BITMAP-MULE package.  You can set it to t
1229 even if you are using Emacs 21+.  It has no effect on XEmacs."
1230   :group 'gnus-article-various
1231   :type 'boolean
1232   :get (lambda (symbol)
1233          (and (not noninteractive)
1234               (default-value symbol)
1235               (not (featurep 'xemacs))
1236               (module-installed-p 'smiley-mule)
1237               t))
1238   :set (lambda (symbol value)
1239          (set-default symbol (and (not noninteractive)
1240                                   value
1241                                   (not (featurep 'xemacs))
1242                                   (module-installed-p 'smiley-mule)
1243                                   t))))
1244
1245 (defvar gnus-article-smiley-mule-loaded-p nil
1246   "Internal variable used to say whether `smiley-mule' is loaded (whether
1247 smiley functions are not overridden by `smiley').")
1248
1249 (defcustom gnus-treat-display-face
1250   (and (not noninteractive)
1251        ;; x-face-e21 handles both X-Face and Face headers.
1252        (not (and (eq gnus-article-x-face-command 'x-face-decode-message-header)
1253                  (module-installed-p 'x-face-e21)))
1254        (or (and (fboundp 'image-type-available-p)
1255                 (image-type-available-p 'png))
1256            (and (featurep 'xemacs)
1257                 (featurep 'png)))
1258        'head)
1259   "Display Face headers.
1260 Valid values are nil, t, `head', `last', an integer or a predicate.
1261 See Info node `(gnus)Customizing Articles' and Info node
1262 `(gnus)X-Face' for details."
1263   :group 'gnus-article-treat
1264   :version "21.1"
1265   :link '(custom-manual "(gnus)Customizing Articles")
1266   :link '(custom-manual "(gnus)X-Face")
1267   :type gnus-article-treat-head-custom)
1268 (put 'gnus-treat-display-face 'highlight t)
1269
1270 (defcustom gnus-treat-display-smileys
1271   (if (and (not noninteractive)
1272            (or (and (featurep 'xemacs)
1273                     (featurep 'xpm))
1274                (gnus-image-type-available-p 'xpm)
1275                (gnus-image-type-available-p 'pbm)
1276                (and (not (featurep 'xemacs))
1277                     window-system
1278                     (module-installed-p 'smiley-mule))))
1279       t
1280     nil)
1281   "Display smileys.
1282 Valid values are nil, t, `head', `last', an integer or a predicate.
1283 See Info node `(gnus)Customizing Articles' and Info node
1284 `(gnus)Smileys' for details."
1285   :group 'gnus-article-treat
1286   ;;:version "21.1"
1287   :link '(custom-manual "(gnus)Customizing Articles")
1288   :link '(custom-manual "(gnus)Smileys")
1289   :type gnus-article-treat-custom)
1290 (put 'gnus-treat-display-smileys 'highlight t)
1291
1292 (defcustom gnus-treat-from-picon
1293   (if (and (gnus-image-type-available-p 'xpm)
1294            (gnus-picons-installed-p))
1295       'head nil)
1296   "Display picons in the From header.
1297 Valid values are nil, t, `head', `last', an integer or a predicate.
1298 See Info node `(gnus)Customizing Articles' and Info node
1299 `(gnus)Picons' for details."
1300   :group 'gnus-article-treat
1301   :group 'gnus-picon
1302   :link '(custom-manual "(gnus)Customizing Articles")
1303   :link '(custom-manual "(gnus)Picons")
1304   :type gnus-article-treat-head-custom)
1305 (put 'gnus-treat-from-picon 'highlight t)
1306
1307 (defcustom gnus-treat-mail-picon
1308   (if (and (gnus-image-type-available-p 'xpm)
1309            (gnus-picons-installed-p))
1310       'head nil)
1311   "Display picons in To and Cc headers.
1312 Valid values are nil, t, `head', `last', an integer or a predicate.
1313 See Info node `(gnus)Customizing Articles' and Info node
1314 `(gnus)Picons' for details."
1315   :group 'gnus-article-treat
1316   :group 'gnus-picon
1317   :link '(custom-manual "(gnus)Customizing Articles")
1318   :link '(custom-manual "(gnus)Picons")
1319   :type gnus-article-treat-head-custom)
1320 (put 'gnus-treat-mail-picon 'highlight t)
1321
1322 (defcustom gnus-treat-newsgroups-picon
1323   (if (and (gnus-image-type-available-p 'xpm)
1324            (gnus-picons-installed-p))
1325       'head nil)
1326   "Display picons in the Newsgroups and Followup-To headers.
1327 Valid values are nil, t, `head', `last', an integer or a predicate.
1328 See Info node `(gnus)Customizing Articles' and Info node
1329 `(gnus)Picons' for details."
1330   :group 'gnus-article-treat
1331   :group 'gnus-picon
1332   :link '(custom-manual "(gnus)Customizing Articles")
1333   :link '(custom-manual "(gnus)Picons")
1334   :type gnus-article-treat-head-custom)
1335 (put 'gnus-treat-newsgroups-picon 'highlight t)
1336
1337 (defcustom gnus-treat-body-boundary
1338   (if (or gnus-treat-newsgroups-picon
1339           gnus-treat-mail-picon
1340           gnus-treat-from-picon)
1341       'head nil)
1342   "Draw a boundary at the end of the headers.
1343 Valid values are nil, t, `head', `last', an integer or a predicate.
1344 See Info node `(gnus)Customizing Articles' for details."
1345   :version "21.1"
1346   :group 'gnus-article-treat
1347   :link '(custom-manual "(gnus)Customizing Articles")
1348   :type gnus-article-treat-custom)
1349
1350 (defcustom gnus-treat-capitalize-sentences nil
1351   "Capitalize sentence-starting words.
1352 Valid values are nil, t, `head', `last', an integer or a predicate.
1353 See Info node `(gnus)Customizing Articles' for details."
1354   :version "21.1"
1355   :group 'gnus-article-treat
1356   :link '(custom-manual "(gnus)Customizing Articles")
1357   :type gnus-article-treat-custom)
1358
1359 (defcustom gnus-treat-wash-html nil
1360   "Format as HTML.
1361 Valid values are nil, t, `head', `last', an integer or a predicate.
1362 See Info node `(gnus)Customizing Articles' for details."
1363   :group 'gnus-article-treat
1364   :link '(custom-manual "(gnus)Customizing Articles")
1365   :type gnus-article-treat-custom)
1366
1367 (defcustom gnus-treat-fill-long-lines nil
1368   "Fill long lines.
1369 Valid values are nil, t, `head', `last', an integer or a predicate.
1370 See Info node `(gnus)Customizing Articles' for details."
1371   :group 'gnus-article-treat
1372   :link '(custom-manual "(gnus)Customizing Articles")
1373   :type gnus-article-treat-custom)
1374
1375 (defcustom gnus-treat-play-sounds nil
1376   "Play sounds.
1377 Valid values are nil, t, `head', `last', an integer or a predicate.
1378 See Info node `(gnus)Customizing Articles' for details."
1379   :version "21.1"
1380   :group 'gnus-article-treat
1381   :link '(custom-manual "(gnus)Customizing Articles")
1382   :type gnus-article-treat-custom)
1383
1384 (defcustom gnus-treat-decode-article-as-default-mime-charset nil
1385   "Decode an article as `default-mime-charset'.  For instance, if you want to
1386 attempt to decode an article even if the value of `gnus-show-mime' is nil,
1387 you could set this variable to something like: nil for don't decode, t for
1388 decode the body, '(or header t) for the whole article, etc."
1389   :group 'gnus-article-treat
1390   :type '(radio (const :tag "Off" nil)
1391                 (const :tag "Decode body" t)
1392                 (const :tag "Decode all" (or head t))))
1393
1394 (defcustom gnus-treat-translate nil
1395   "Translate articles from one language to another.
1396 Valid values are nil, t, `head', `last', an integer or a predicate.
1397 See Info node `(gnus)Customizing Articles' for details."
1398   :version "21.1"
1399   :group 'gnus-article-treat
1400   :link '(custom-manual "(gnus)Customizing Articles")
1401   :type gnus-article-treat-custom)
1402
1403 (defcustom gnus-treat-x-pgp-sig nil
1404   "Verify X-PGP-Sig.
1405 To automatically treat X-PGP-Sig, set it to head.
1406 Valid values are nil, t, `head', `last', an integer or a predicate.
1407 See Info node `(gnus)Customizing Articles' for details."
1408   :group 'gnus-article-treat
1409   :group 'mime-security
1410   :type gnus-article-treat-custom)
1411
1412 (defcustom gnus-treat-monafy nil
1413   "Display body part with mona font.
1414 Valid values are nil, t, `head', `last', an integer or a predicate.
1415 See Info node `(gnus)Customizing Articles' for details."
1416   :group 'gnus-article-treat
1417   :group 'mime-security
1418   :link '(custom-manual "(gnus)Customizing Articles")
1419   :type gnus-article-treat-custom)
1420
1421 (defvar gnus-article-encrypt-protocol-alist
1422   '(("PGP" . mml2015-self-encrypt)))
1423
1424 ;; Set to nil if more than one protocol added to
1425 ;; gnus-article-encrypt-protocol-alist.
1426 (defcustom gnus-article-encrypt-protocol "PGP"
1427   "The protocol used for encrypt articles.
1428 It is a string, such as \"PGP\". If nil, ask user."
1429   :type 'string
1430   :group 'mime-security)
1431
1432 (defvar gnus-article-wash-function nil
1433   "Function used for converting HTML into text.")
1434
1435 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1436                               (mm-coding-system-p 'utf-8)
1437                               (executable-find idna-program))
1438   "Whether IDNA decoding of headers is used when viewing messages.
1439 This requires GNU Libidn, and by default only enabled if it is found."
1440   :group 'gnus-article-headers
1441   :type 'boolean)
1442
1443 (defcustom gnus-article-over-scroll nil
1444   "If non-nil, allow scrolling the article buffer even when there no more text."
1445   :group 'gnus-article
1446   :type 'boolean)
1447
1448 ;;; Internal variables
1449
1450 (defvar gnus-english-month-names
1451   '("January" "February" "March" "April" "May" "June" "July" "August"
1452     "September" "October" "November" "December"))
1453
1454 (defvar article-goto-body-goes-to-point-min-p nil)
1455 (defvar gnus-article-wash-types nil)
1456 (defvar gnus-article-emphasis-alist nil)
1457 (defvar gnus-article-image-alist nil)
1458
1459 (defvar gnus-article-mime-handle-alist-1 nil)
1460 (defvar gnus-treatment-function-alist
1461   '((gnus-treat-decode-article-as-default-mime-charset
1462      gnus-article-decode-article-as-default-mime-charset)
1463     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1464     (gnus-treat-monafy gnus-article-monafy)
1465     (gnus-treat-strip-banner gnus-article-strip-banner)
1466     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1467     (gnus-treat-buttonize gnus-article-add-buttons)
1468     (gnus-treat-fill-article gnus-article-fill-cited-article)
1469     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1470     (gnus-treat-strip-cr gnus-article-remove-cr)
1471     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1472     (gnus-treat-date-ut gnus-article-date-ut)
1473     (gnus-treat-date-local gnus-article-date-local)
1474     (gnus-treat-date-english gnus-article-date-english)
1475     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1476     (gnus-treat-date-original gnus-article-date-original)
1477     (gnus-treat-date-user-defined gnus-article-date-user)
1478     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1479     (gnus-treat-display-face gnus-article-display-face)
1480     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1481     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1482     (gnus-treat-hide-signature gnus-article-hide-signature)
1483     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1484     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1485     (gnus-treat-strip-pem gnus-article-hide-pem)
1486     (gnus-treat-from-picon gnus-treat-from-picon)
1487     (gnus-treat-mail-picon gnus-treat-mail-picon)
1488     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1489     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1490     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1491     (gnus-treat-strip-trailing-blank-lines
1492      gnus-article-remove-trailing-blank-lines)
1493     (gnus-treat-strip-leading-blank-lines
1494      gnus-article-strip-leading-blank-lines)
1495     (gnus-treat-strip-multiple-blank-lines
1496      gnus-article-strip-multiple-blank-lines)
1497     (gnus-treat-overstrike gnus-article-treat-overstrike)
1498     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1499     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1500     ;; Displaying X-Face should be done after unfolding headers
1501     ;; to protect bitmap lines.
1502     (gnus-treat-display-xface gnus-article-display-x-face)
1503     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1504     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1505     (gnus-treat-display-smileys gnus-treat-smiley)
1506     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1507     (gnus-treat-wash-html gnus-article-wash-html)
1508     (gnus-treat-emphasize gnus-article-emphasize)
1509     (gnus-treat-hide-citation gnus-article-hide-citation)
1510     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1511     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1512     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1513     (gnus-treat-play-sounds gnus-earcon-display)))
1514
1515 (defvar gnus-article-mime-handle-alist nil)
1516 (defvar article-lapsed-timer nil)
1517 (defvar gnus-article-current-summary nil)
1518
1519 (defvar gnus-article-mode-syntax-table
1520   (let ((table (copy-syntax-table text-mode-syntax-table)))
1521     ;; This causes the citation match run O(2^n).
1522     ;; (modify-syntax-entry ?- "w" table)
1523     (modify-syntax-entry ?> ")<" table)
1524     (modify-syntax-entry ?< "(>" table)
1525     ;; make M-. in article buffers work for `foo' strings
1526     (modify-syntax-entry ?' " " table)
1527     (modify-syntax-entry ?` " " table)
1528     table)
1529   "Syntax table used in article mode buffers.
1530 Initialized from `text-mode-syntax-table.")
1531
1532 (defvar gnus-save-article-buffer nil)
1533
1534 (defvar gnus-article-mode-line-format-alist
1535   (nconc '((?w (gnus-article-wash-status) ?s)
1536            (?m (gnus-article-mime-part-status) ?s))
1537          gnus-summary-mode-line-format-alist))
1538
1539 (defvar gnus-number-of-articles-to-be-saved nil)
1540
1541 (defvar gnus-inhibit-hiding nil)
1542
1543 ;;; Macros for dealing with the article buffer.
1544
1545 (defmacro gnus-with-article-headers (&rest forms)
1546   `(save-excursion
1547      (set-buffer gnus-article-buffer)
1548      (save-restriction
1549        (let ((buffer-read-only nil)
1550              (inhibit-point-motion-hooks t)
1551              (case-fold-search t))
1552          (article-narrow-to-head)
1553          ,@forms))))
1554
1555 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1556 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1557
1558 (defmacro gnus-with-article-buffer (&rest forms)
1559   `(save-excursion
1560      (set-buffer gnus-article-buffer)
1561      (let ((buffer-read-only nil))
1562        ,@forms)))
1563
1564 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1565 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1566
1567 (defun gnus-article-goto-header (header)
1568   "Go to HEADER, which is a regular expression."
1569   (re-search-forward (concat "^\\(" header "\\):") nil t))
1570
1571 (defsubst gnus-article-hide-text (b e props)
1572   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1573   (gnus-add-text-properties-when 'article-type nil b e props)
1574   (when (memq 'intangible props)
1575     (put-text-property
1576      (max (1- b) (point-min))
1577      b 'intangible (cddr (memq 'intangible props)))))
1578
1579 (defsubst gnus-article-unhide-text (b e)
1580   "Remove hidden text properties from region between B and E."
1581   (remove-text-properties b e gnus-hidden-properties)
1582   (when (memq 'intangible gnus-hidden-properties)
1583     (put-text-property (max (1- b) (point-min))
1584                        b 'intangible nil)))
1585
1586 (defun gnus-article-hide-text-type (b e type)
1587   "Hide text of TYPE between B and E."
1588   (gnus-add-wash-type type)
1589   (gnus-article-hide-text
1590    b e (cons 'article-type (cons type gnus-hidden-properties))))
1591
1592 (defun gnus-article-unhide-text-type (b e type)
1593   "Unhide text of TYPE between B and E."
1594   (gnus-delete-wash-type type)
1595   (remove-text-properties
1596    b e (cons 'article-type (cons type gnus-hidden-properties)))
1597   (when (memq 'intangible gnus-hidden-properties)
1598     (put-text-property (max (1- b) (point-min))
1599                        b 'intangible nil)))
1600
1601 (defun gnus-article-hide-text-of-type (type)
1602   "Hide text of TYPE in the current buffer."
1603   (save-excursion
1604     (let ((b (point-min))
1605           (e (point-max)))
1606       (while (setq b (text-property-any b e 'article-type type))
1607         (add-text-properties b (incf b) gnus-hidden-properties)))))
1608
1609 (defun gnus-article-delete-text-of-type (type)
1610   "Delete text of TYPE in the current buffer."
1611   (save-excursion
1612     (let ((b (point-min)))
1613       (while (setq b (text-property-any b (point-max) 'article-type type))
1614         (delete-region
1615          b (or (text-property-not-all b (point-max) 'article-type type)
1616                (point-max)))))))
1617
1618 (defun gnus-article-delete-invisible-text ()
1619   "Delete all invisible text in the current buffer."
1620   (save-excursion
1621     (let ((b (point-min)))
1622       (while (setq b (text-property-any b (point-max) 'invisible t))
1623         (delete-region
1624          b (or (text-property-not-all b (point-max) 'invisible t)
1625                (point-max)))))))
1626
1627 (defun gnus-article-text-type-exists-p (type)
1628   "Say whether any text of type TYPE exists in the buffer."
1629   (text-property-any (point-min) (point-max) 'article-type type))
1630
1631 (defsubst gnus-article-header-rank ()
1632   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1633   (let ((list gnus-sorted-header-list)
1634         (i 1))
1635     (while list
1636       (if (looking-at (car list))
1637           (setq list nil)
1638         (setq list (cdr list))
1639         (incf i)))
1640       i))
1641
1642 (defun article-hide-headers (&optional arg delete)
1643   "Hide unwanted headers and possibly sort them as well."
1644   (interactive (gnus-article-hidden-arg))
1645   ;; Lars said that this function might be inhibited.
1646   (if (gnus-article-check-hidden-text 'headers arg)
1647       (progn
1648         ;; Show boring headers as well.
1649         (gnus-article-show-hidden-text 'boring-headers)
1650         (when (eq 1 (point-min))
1651           (set-window-start (get-buffer-window (current-buffer)) 1)))
1652     (unless gnus-inhibit-hiding
1653       (save-excursion
1654         (save-restriction
1655           (let ((inhibit-read-only t)
1656                 (case-fold-search t)
1657                 (max (1+ (length gnus-sorted-header-list)))
1658                 (ignored (when (not gnus-visible-headers)
1659                            (cond ((stringp gnus-ignored-headers)
1660                                   gnus-ignored-headers)
1661                                  ((listp gnus-ignored-headers)
1662                                   (mapconcat 'identity gnus-ignored-headers
1663                                              "\\|")))))
1664                 (visible
1665                  (cond ((stringp gnus-visible-headers)
1666                         gnus-visible-headers)
1667                        ((and gnus-visible-headers
1668                              (listp gnus-visible-headers))
1669                         (mapconcat 'identity gnus-visible-headers "\\|"))))
1670                 (inhibit-point-motion-hooks t)
1671                 beg)
1672             ;; First we narrow to just the headers.
1673             (article-narrow-to-head)
1674             ;; Hide any "From " lines at the beginning of (mail) articles.
1675             (while (looking-at "From ")
1676               (forward-line 1))
1677             (unless (bobp)
1678               (if delete
1679                   (delete-region (point-min) (point))
1680                 (gnus-article-hide-text (point-min) (point)
1681                                         (nconc (list 'article-type 'headers)
1682                                                gnus-hidden-properties))))
1683             ;; Then treat the rest of the header lines.
1684             ;; Then we use the two regular expressions
1685             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1686             ;; select which header lines is to remain visible in the
1687             ;; article buffer.
1688             (while (re-search-forward "^[^ \t:]*:" nil t)
1689               (beginning-of-line)
1690               ;; Mark the rank of the header.
1691               (put-text-property
1692                (point) (1+ (point)) 'message-rank
1693                (if (or (and visible (looking-at visible))
1694                        (and ignored
1695                             (not (looking-at ignored))))
1696                    (gnus-article-header-rank)
1697                  (+ 2 max)))
1698               (forward-line 1))
1699             (message-sort-headers-1)
1700             (when (setq beg (text-property-any
1701                              (point-min) (point-max) 'message-rank (+ 2 max)))
1702               ;; We delete or make invisible the unwanted headers.
1703               (gnus-add-wash-type 'headers)
1704               (if delete
1705                   (progn
1706                     (add-text-properties
1707                      (point-min) (+ 5 (point-min))
1708                      '(article-type headers dummy-invisible t))
1709                     (delete-region beg (point-max)))
1710                 (gnus-article-hide-text-type beg (point-max) 'headers)))))))))
1711
1712 (defun article-hide-boring-headers (&optional arg)
1713   "Toggle hiding of headers that aren't very interesting.
1714 If given a negative prefix, always show; if given a positive prefix,
1715 always hide."
1716   (interactive (gnus-article-hidden-arg))
1717   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1718              (not gnus-show-all-headers))
1719     (save-excursion
1720       (save-restriction
1721         (let ((buffer-read-only nil)
1722               (list gnus-boring-article-headers)
1723               (inhibit-point-motion-hooks t)
1724               elem)
1725           (article-narrow-to-head)
1726           (while list
1727             (setq elem (pop list))
1728             (goto-char (point-min))
1729             (cond
1730              ;; Hide empty headers.
1731              ((eq elem 'empty)
1732               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1733                 (forward-line -1)
1734                 (gnus-article-hide-text-type
1735                  (gnus-point-at-bol)
1736                  (progn
1737                    (end-of-line)
1738                    (if (re-search-forward "^[^ \t]" nil t)
1739                        (match-beginning 0)
1740                      (point-max)))
1741                  'boring-headers)))
1742              ;; Hide boring Newsgroups header.
1743              ((eq elem 'newsgroups)
1744               (when (gnus-string-equal
1745                      (gnus-fetch-field "newsgroups")
1746                      (gnus-group-real-name
1747                       (if (boundp 'gnus-newsgroup-name)
1748                           gnus-newsgroup-name
1749                         "")))
1750                 (gnus-article-hide-header "newsgroups")))
1751              ((eq elem 'to-address)
1752               (let ((to (message-fetch-field "to"))
1753                     (to-address
1754                      (gnus-parameter-to-address
1755                       (if (boundp 'gnus-newsgroup-name)
1756                           gnus-newsgroup-name ""))))
1757                 (when (and to to-address
1758                            (ignore-errors
1759                              (gnus-string-equal
1760                               ;; only one address in To
1761                               (nth 1 (mail-extract-address-components to))
1762                               to-address)))
1763                   (gnus-article-hide-header "to"))))
1764              ((eq elem 'to-list)
1765               (let ((to (message-fetch-field "to"))
1766                     (to-list
1767                      (gnus-parameter-to-list
1768                       (if (boundp 'gnus-newsgroup-name)
1769                           gnus-newsgroup-name ""))))
1770                 (when (and to to-list
1771                            (ignore-errors
1772                              (gnus-string-equal
1773                               ;; only one address in To
1774                               (nth 1 (mail-extract-address-components to))
1775                               to-list)))
1776                   (gnus-article-hide-header "to"))))
1777              ((eq elem 'cc-list)
1778               (let ((cc (message-fetch-field "cc"))
1779                     (to-list
1780                      (gnus-parameter-to-list
1781                       (if (boundp 'gnus-newsgroup-name)
1782                           gnus-newsgroup-name ""))))
1783                 (when (and cc to-list
1784                            (ignore-errors
1785                              (gnus-string-equal
1786                               ;; only one address in CC
1787                               (nth 1 (mail-extract-address-components cc))
1788                               to-list)))
1789                   (gnus-article-hide-header "cc"))))
1790              ((eq elem 'followup-to)
1791               (when (gnus-string-equal
1792                      (message-fetch-field "followup-to")
1793                      (message-fetch-field "newsgroups"))
1794                 (gnus-article-hide-header "followup-to")))
1795              ((eq elem 'reply-to)
1796               (if (gnus-group-find-parameter
1797                    gnus-newsgroup-name 'broken-reply-to)
1798                   (gnus-article-hide-header "reply-to")
1799                 (let ((from (message-fetch-field "from"))
1800                       (reply-to (message-fetch-field "reply-to")))
1801                   (when (and
1802                          from reply-to
1803                          (ignore-errors
1804                            (gnus-string-equal
1805                             (nth 1 (mail-extract-address-components from))
1806                             (nth 1 (mail-extract-address-components reply-to)))))
1807                     (gnus-article-hide-header "reply-to")))))
1808              ((eq elem 'date)
1809               (let ((date (message-fetch-field "date")))
1810                 (when (and date
1811                            (< (days-between (current-time-string) date)
1812                               4))
1813                   (gnus-article-hide-header "date"))))
1814              ((eq elem 'long-to)
1815               (let ((to (message-fetch-field "to"))
1816                     (cc (message-fetch-field "cc")))
1817                 (when (> (length to) 1024)
1818                   (gnus-article-hide-header "to"))
1819                 (when (> (length cc) 1024)
1820                   (gnus-article-hide-header "cc"))))
1821              ((eq elem 'many-to)
1822               (let ((to-count 0)
1823                     (cc-count 0))
1824                 (goto-char (point-min))
1825                 (while (re-search-forward "^to:" nil t)
1826                   (setq to-count (1+ to-count)))
1827                 (when (> to-count 1)
1828                   (while (> to-count 0)
1829                     (goto-char (point-min))
1830                     (save-restriction
1831                       (re-search-forward "^to:" nil nil to-count)
1832                       (forward-line -1)
1833                       (narrow-to-region (point) (point-max))
1834                       (gnus-article-hide-header "to"))
1835                     (setq to-count (1- to-count))))
1836                 (goto-char (point-min))
1837                 (while (re-search-forward "^cc:" nil t)
1838                   (setq cc-count (1+ cc-count)))
1839                 (when (> cc-count 1)
1840                   (while (> cc-count 0)
1841                     (goto-char (point-min))
1842                     (save-restriction
1843                       (re-search-forward "^cc:" nil nil cc-count)
1844                       (forward-line -1)
1845                       (narrow-to-region (point) (point-max))
1846                       (gnus-article-hide-header "cc"))
1847                     (setq cc-count (1- cc-count)))))))))))))
1848
1849 (defun gnus-article-hide-header (header)
1850   (save-excursion
1851     (goto-char (point-min))
1852     (when (re-search-forward (concat "^" header ":") nil t)
1853       (gnus-article-hide-text-type
1854        (gnus-point-at-bol)
1855        (progn
1856          (end-of-line)
1857          (if (re-search-forward "^[^ \t]" nil t)
1858              (match-beginning 0)
1859            (point-max)))
1860        'boring-headers))))
1861
1862 (defvar gnus-article-normalized-header-length 40
1863   "Length of normalized headers.")
1864
1865 (defun article-normalize-headers ()
1866   "Make all header lines 40 characters long."
1867   (interactive)
1868   (let ((buffer-read-only nil)
1869         column)
1870     (save-excursion
1871       (save-restriction
1872         (article-narrow-to-head)
1873         (while (not (eobp))
1874           (cond
1875            ((< (setq column (- (gnus-point-at-eol) (point)))
1876                gnus-article-normalized-header-length)
1877             (end-of-line)
1878             (insert (make-string
1879                      (- gnus-article-normalized-header-length column)
1880                      ? )))
1881            ((> column gnus-article-normalized-header-length)
1882             (gnus-put-text-property
1883              (progn
1884                (forward-char gnus-article-normalized-header-length)
1885                (point))
1886              (gnus-point-at-eol)
1887              'invisible t))
1888            (t
1889             ;; Do nothing.
1890             ))
1891           (forward-line 1))))))
1892
1893 (defun article-treat-dumbquotes ()
1894   "Translate M****s*** sm*rtq**t*s into proper text.
1895 Note that this function guesses whether a character is a sm*rtq**t* or
1896 not, so it should only be used interactively.
1897
1898 Sm*rtq**t*s are M****s***'s unilateral extension to the character map
1899 in an attempt to provide more quoting characters.  If you see
1900 something like \\222 or \\264 where you're expecting some kind of
1901 apostrophe or quotation mark, then try this wash."
1902   (interactive)
1903   (article-translate-strings gnus-article-dumbquotes-map))
1904
1905 (defun article-translate-characters (from to)
1906   "Translate all characters in the body of the article according to FROM and TO.
1907 FROM is a string of characters to translate from; to is a string of
1908 characters to translate to."
1909   (save-excursion
1910     (when (article-goto-body)
1911       (let ((buffer-read-only nil)
1912             (x (make-string 225 ?x))
1913             (i -1))
1914         (while (< (incf i) (length x))
1915           (aset x i i))
1916         (setq i 0)
1917         (while (< i (length from))
1918           (aset x (aref from i) (aref to i))
1919           (incf i))
1920         (translate-region (point) (point-max) x)))))
1921
1922 (defun article-translate-strings (map)
1923   "Translate all string in the body of the article according to MAP.
1924 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1925   (save-excursion
1926     (when (article-goto-body)
1927       (let ((buffer-read-only nil)
1928             elem)
1929         (while (setq elem (pop map))
1930           (save-excursion
1931             (while (search-forward (car elem) nil t)
1932               (replace-match (cadr elem)))))))))
1933
1934 (defun article-treat-overstrike ()
1935   "Translate overstrikes into bold text."
1936   (interactive)
1937   (save-excursion
1938     (when (article-goto-body)
1939       (let ((buffer-read-only nil))
1940         (while (search-forward "\b" nil t)
1941           (let ((next (char-after))
1942                 start end previous)
1943             (backward-char 2)
1944             (setq start (point)
1945                   previous (char-after))
1946             (forward-char 3)
1947             (setq end (point))
1948             (backward-char)
1949             ;; We do the boldification/underlining by hiding the
1950             ;; overstrikes and putting the proper text property
1951             ;; on the letters.
1952             (cond
1953              ((eq next previous)
1954               (gnus-article-hide-text-type start (point) 'overstrike)
1955               (put-text-property (point) end 'face 'bold))
1956              ((eq next ?_)
1957               (gnus-article-hide-text-type
1958                (1- (point)) (1+ (point)) 'overstrike)
1959               (put-text-property
1960                start (1- (point)) 'face 'underline))
1961              ((eq previous ?_)
1962               (gnus-article-hide-text-type start (point) 'overstrike)
1963               (put-text-property
1964                (point) end 'face 'underline)))))))))
1965
1966 (defun gnus-article-treat-unfold-headers ()
1967   "Unfold folded message headers.
1968 Only the headers that fit into the current window width will be
1969 unfolded."
1970   (interactive)
1971   (gnus-with-article-headers
1972     (let (length)
1973       (while (not (eobp))
1974         (save-restriction
1975           (mail-header-narrow-to-field)
1976           (let ((header (buffer-string)))
1977             (with-temp-buffer
1978               (insert header)
1979               (goto-char (point-min))
1980               (while (re-search-forward "\n[\t ]" nil t)
1981                 (replace-match " " t t)))
1982             (setq length (- (point-max) (point-min) 1)))
1983           (when (< length (window-width))
1984             (while (re-search-forward "\n[\t ]" nil t)
1985               (replace-match " " t t)))
1986           (goto-char (point-max)))))))
1987
1988 (defun gnus-article-treat-fold-headers ()
1989   "Fold message headers."
1990   (interactive)
1991   (gnus-with-article-headers
1992     (while (not (eobp))
1993       (save-restriction
1994         (mail-header-narrow-to-field)
1995         (mail-header-fold-field)
1996         (goto-char (point-max))))))
1997
1998 (defun gnus-treat-smiley ()
1999   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2000   (interactive)
2001   (unless (featurep 'xemacs)
2002     (when (and (>= emacs-major-version 21)
2003                (not gnus-article-should-use-smiley-mule)
2004                gnus-article-smiley-mule-loaded-p)
2005       (load "smiley" nil t)
2006       (setq gnus-article-smiley-mule-loaded-p nil))
2007     (when (and gnus-article-should-use-smiley-mule
2008                (not gnus-article-smiley-mule-loaded-p))
2009       (load "smiley-mule" nil t)
2010       (setq gnus-article-smiley-mule-loaded-p t)))
2011   (gnus-with-article-buffer
2012     (if (memq 'smiley gnus-article-wash-types)
2013         (gnus-delete-images 'smiley)
2014       (article-goto-body)
2015       (let ((images (smiley-region (point) (point-max))))
2016         (when images
2017           (gnus-add-wash-type 'smiley)
2018           (dolist (image images)
2019             (gnus-add-image 'smiley image)))))))
2020
2021 (defun gnus-article-remove-images ()
2022   "Remove all images from the article buffer."
2023   (interactive)
2024   (gnus-with-article-buffer
2025     (dolist (elem gnus-article-image-alist)
2026       (gnus-delete-images (car elem)))))
2027
2028 (defun gnus-article-treat-fold-newsgroups ()
2029   "Unfold folded message headers.
2030 Only the headers that fit into the current window width will be
2031 unfolded."
2032   (interactive)
2033   (gnus-with-article-headers
2034     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2035       (save-restriction
2036         (mail-header-narrow-to-field)
2037         (while (re-search-forward ", *" nil t)
2038           (replace-match ", " t t))
2039         (mail-header-fold-field)
2040         (goto-char (point-max))))))
2041
2042 (defun gnus-article-treat-body-boundary ()
2043   "Place a boundary line at the end of the headers."
2044   (interactive)
2045   (when (and gnus-body-boundary-delimiter
2046              (> (length gnus-body-boundary-delimiter) 0))
2047     (gnus-with-article-headers
2048       (goto-char (point-max))
2049       (let ((start (point)))
2050         (insert "X-Boundary: ")
2051         (gnus-add-text-properties start (point) '(invisible t intangible t))
2052         (insert (let (str)
2053                   (while (>= (1- (window-width)) (length str))
2054                     (setq str (concat str gnus-body-boundary-delimiter)))
2055                   (substring str 0 (1- (window-width))))
2056                 "\n")
2057         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2058
2059 (defun article-fill-long-lines ()
2060   "Fill lines that are wider than the window width."
2061   (interactive)
2062   (save-excursion
2063     (let ((buffer-read-only nil)
2064           (width (window-width (get-buffer-window (current-buffer)))))
2065       (save-restriction
2066         (article-goto-body)
2067         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2068           (while (not (eobp))
2069             (end-of-line)
2070             (when (>= (current-column) (min fill-column width))
2071               (narrow-to-region (min (1+ (point)) (point-max))
2072                                 (gnus-point-at-bol))
2073               (let ((goback (point-marker)))
2074                 (fill-paragraph nil)
2075                 (goto-char (marker-position goback)))
2076               (widen))
2077             (forward-line 1)))))))
2078
2079 (defun article-capitalize-sentences ()
2080   "Capitalize the first word in each sentence."
2081   (interactive)
2082   (save-excursion
2083     (let ((buffer-read-only nil)
2084           (paragraph-start "^[\n\^L]"))
2085       (article-goto-body)
2086       (while (not (eobp))
2087         (capitalize-word 1)
2088         (forward-sentence)))))
2089
2090 (defun article-remove-cr ()
2091   "Remove trailing CRs and then translate remaining CRs into LFs."
2092   (interactive)
2093   (save-excursion
2094     (let ((buffer-read-only nil))
2095       (goto-char (point-min))
2096       (while (re-search-forward "\r+$" nil t)
2097         (replace-match "" t t))
2098       (goto-char (point-min))
2099       (while (search-forward "\r" nil t)
2100         (replace-match "\n" t t)))))
2101
2102 (defun article-remove-trailing-blank-lines ()
2103   "Remove all trailing blank lines from the article."
2104   (interactive)
2105   (save-excursion
2106     (let ((buffer-read-only nil))
2107       (goto-char (point-max))
2108       (delete-region
2109        (point)
2110        (progn
2111          (while (and (not (bobp))
2112                      (looking-at "^[ \t]*$")
2113                      (not (gnus-annotation-in-region-p
2114                            (point) (gnus-point-at-eol))))
2115            (forward-line -1))
2116          (forward-line 1)
2117          (point))))))
2118
2119 (defun article-display-face ()
2120   "Display any Face headers in the header."
2121   (interactive)
2122   (gnus-with-article-headers
2123     (if (memq 'face gnus-article-wash-types)
2124         (gnus-delete-images 'face)
2125       (let (face faces)
2126         (save-excursion
2127           (set-buffer gnus-original-article-buffer)
2128           (save-restriction
2129             (mail-narrow-to-head)
2130             (while (gnus-article-goto-header "Face")
2131               (push (mail-header-field-value) faces))))
2132         (while (setq face (pop faces))
2133           (let ((png (gnus-convert-face-to-png face))
2134                 image)
2135             (when png
2136               (setq image (gnus-create-image png 'png t))
2137               (gnus-article-goto-header "from")
2138               (when (bobp)
2139                 (insert "From: [no `from' set]\n")
2140                 (forward-char -17))
2141               (gnus-add-wash-type 'face)
2142               (gnus-add-image 'face image)
2143               (gnus-put-image image))))))
2144     ))
2145
2146 (defun article-display-x-face (&optional force)
2147   "Look for an X-Face header and display it if present."
2148   (interactive (list 'force))
2149   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2150     (gnus-with-article-headers
2151       ;; Delete the old process, if any.
2152       (when (process-status "article-x-face")
2153         (delete-process "article-x-face"))
2154       (if (memq 'xface gnus-article-wash-types)
2155           ;; We have already displayed X-Faces, so we remove them
2156           ;; instead.
2157           (gnus-delete-images 'xface)
2158         ;; Display X-Faces.
2159         (let (x-faces from face)
2160           (save-excursion
2161             (when (and wash-face-p
2162                        (progn
2163                          (goto-char (point-min))
2164                          (not (re-search-forward
2165                                "^X-Face\\(-[0-9]+\\)?:[\t ]*" nil t)))
2166                        (gnus-buffer-live-p gnus-original-article-buffer))
2167               ;; If type `W f', use gnus-original-article-buffer,
2168               ;; otherwise use the current buffer because displaying
2169               ;; RFC822 parts calls this function too.
2170               (set-buffer gnus-original-article-buffer))
2171             (save-restriction
2172               (mail-narrow-to-head)
2173               (while (gnus-article-goto-header "X-Face")
2174                 (push (mail-header-field-value) x-faces))
2175               (setq from (message-fetch-field "from"))))
2176           ;; Sending multiple EOFs to xv doesn't work, so we only do a
2177           ;; single external face.
2178           (when (stringp gnus-article-x-face-command)
2179             (setq x-faces (list (car x-faces))))
2180           (while (and (setq face (pop x-faces))
2181                       gnus-article-x-face-command
2182                       (or force
2183                           ;; Check whether this face is censored.
2184                           (not gnus-article-x-face-too-ugly)
2185                           (and gnus-article-x-face-too-ugly from
2186                                (not (string-match gnus-article-x-face-too-ugly
2187                                                   from)))))
2188             ;; We display the face.
2189             (if (symbolp gnus-article-x-face-command)
2190                 ;; The command is a lisp function, so we call it.
2191                 (if (functionp gnus-article-x-face-command)
2192                     (funcall gnus-article-x-face-command face)
2193                   (error "%s is not a function" gnus-article-x-face-command))
2194               ;; The command is a string, so we interpret the command
2195               ;; as a, well, command, and fork it off.
2196               (let ((process-connection-type nil))
2197                 (process-kill-without-query
2198                  (start-process
2199                   "article-x-face" nil shell-file-name shell-command-switch
2200                   gnus-article-x-face-command))
2201                 (with-temp-buffer
2202                   (insert face)
2203                   (process-send-region "article-x-face"
2204                                        (point-min) (point-max)))
2205                 (process-send-eof "article-x-face")))))))))
2206
2207 (defun article-decode-mime-words ()
2208   "Decode all MIME-encoded words in the article."
2209   (interactive)
2210   (save-excursion
2211     (set-buffer gnus-article-buffer)
2212     (let ((inhibit-point-motion-hooks t)
2213           buffer-read-only
2214           (mail-parse-charset gnus-newsgroup-charset)
2215           (mail-parse-ignored-charsets
2216            (save-excursion (set-buffer gnus-summary-buffer)
2217                            gnus-newsgroup-ignored-charsets)))
2218       (mail-decode-encoded-word-region (point-min) (point-max)))))
2219
2220 (defun article-decode-charset (&optional prompt)
2221   "Decode charset-encoded text in the article.
2222 If PROMPT (the prefix), prompt for a coding system to use."
2223   (interactive "P")
2224   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2225         buffer-read-only
2226         (mail-parse-charset gnus-newsgroup-charset)
2227         (mail-parse-ignored-charsets
2228          (save-excursion (condition-case nil
2229                              (set-buffer gnus-summary-buffer)
2230                            (error))
2231                          gnus-newsgroup-ignored-charsets))
2232         ct cte ctl charset format)
2233     (save-excursion
2234       (save-restriction
2235         (article-narrow-to-head)
2236         (setq ct (message-fetch-field "Content-Type" t)
2237               cte (message-fetch-field "Content-Transfer-Encoding" t)
2238               ctl (and ct (ignore-errors
2239                             (mail-header-parse-content-type ct)))
2240               charset (cond
2241                        (prompt
2242                         (mm-read-coding-system "Charset to decode: "))
2243                        (ctl
2244                         (mail-content-type-get ctl 'charset)))
2245               format (and ctl (mail-content-type-get ctl 'format)))
2246         (when cte
2247           (setq cte (mail-header-strip cte)))
2248         (if (and ctl (not (string-match "/" (car ctl))))
2249             (setq ctl nil))
2250         (goto-char (point-max)))
2251       (forward-line 1)
2252       (save-restriction
2253         (narrow-to-region (point) (point-max))
2254         (when (and (eq mail-parse-charset 'gnus-decoded)
2255                    (eq (mm-body-7-or-8) '8bit))
2256           ;; The text code could have been decoded.
2257           (setq charset mail-parse-charset))
2258         (when (and (or (not ctl)
2259                        (equal (car ctl) "text/plain"))
2260                    (not format)) ;; article with format will decode later.
2261           (mm-decode-body
2262            charset (and cte (intern (downcase
2263                                      (gnus-strip-whitespace cte))))
2264            (car ctl)))))))
2265
2266 (defun article-decode-encoded-words ()
2267   "Remove encoded-word encoding from headers."
2268   (let (buffer-read-only)
2269     (let ((charset (save-excursion
2270                      (set-buffer gnus-summary-buffer)
2271                      default-mime-charset)))
2272       (mime-decode-header-in-buffer charset))))
2273
2274 (defun article-decode-group-name ()
2275   "Decode group names in `Newsgroups:'."
2276   (let ((inhibit-point-motion-hooks t)
2277         buffer-read-only
2278         (method (gnus-find-method-for-group gnus-newsgroup-name)))
2279     (when (and (or gnus-group-name-charset-method-alist
2280                    gnus-group-name-charset-group-alist)
2281                (gnus-buffer-live-p gnus-original-article-buffer))
2282       (save-restriction
2283         (article-narrow-to-head)
2284         (with-current-buffer gnus-original-article-buffer
2285           (goto-char (point-min)))
2286         (while (re-search-forward
2287                 "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2288           (replace-match (save-match-data
2289                            (gnus-decode-newsgroups
2290                             ;; XXX how to use data in article buffer?
2291                             (with-current-buffer gnus-original-article-buffer
2292                               (re-search-forward
2293                                "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2294                                nil t)
2295                               (match-string 1))
2296                             gnus-newsgroup-name method))
2297                          t t nil 1))
2298         (goto-char (point-min))
2299         (with-current-buffer gnus-original-article-buffer
2300           (goto-char (point-min)))
2301         (while (re-search-forward
2302                 "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2303           (replace-match (save-match-data
2304                            (gnus-decode-newsgroups
2305                             ;; XXX how to use data in article buffer?
2306                             (with-current-buffer gnus-original-article-buffer
2307                               (re-search-forward
2308                                "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2309                                nil t)
2310                               (match-string 1))
2311                             gnus-newsgroup-name method))
2312                          t t nil 1))))))
2313
2314 (autoload 'idna-to-unicode "idna")
2315
2316 (defun article-decode-idna-rhs ()
2317   "Decode IDNA strings in RHS in From:, To: and Cc: headers in current buffer."
2318   (when gnus-use-idna
2319     (save-restriction
2320       (let ((inhibit-point-motion-hooks t)
2321             buffer-read-only)
2322         (article-narrow-to-head)
2323         (goto-char (point-min))
2324         (while (re-search-forward "\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2325           (let (ace unicode)
2326             (when (save-match-data
2327                     (and (setq ace (match-string 1))
2328                          (save-excursion
2329                            (and (re-search-backward "^[^ \t]" nil t)
2330                                 (looking-at "From\\|To\\|Cc")))
2331                          (save-excursion (backward-char)
2332                                          (message-idna-inside-rhs-p))
2333                          (setq unicode (idna-to-unicode ace))))
2334               (unless (string= ace unicode)
2335                 (replace-match unicode nil nil nil 1)))))))))
2336
2337 (defun article-de-quoted-unreadable (&optional force read-charset)
2338   "Translate a quoted-printable-encoded article.
2339 If FORCE, decode the article whether it is marked as quoted-printable
2340 or not.
2341 If READ-CHARSET, ask for a coding system."
2342   (interactive (list 'force current-prefix-arg))
2343   (save-excursion
2344     (let ((buffer-read-only nil) type charset)
2345       (if (gnus-buffer-live-p gnus-original-article-buffer)
2346           (with-current-buffer gnus-original-article-buffer
2347             (setq type
2348                   (gnus-fetch-field "content-transfer-encoding"))
2349             (let* ((ct (gnus-fetch-field "content-type"))
2350                    (ctl (and ct
2351                              (ignore-errors
2352                                (mail-header-parse-content-type ct)))))
2353               (setq charset (and ctl
2354                                  (mail-content-type-get ctl 'charset)))
2355               (if (stringp charset)
2356                   (setq charset (intern (downcase charset)))))))
2357       (if read-charset
2358           (setq charset (mm-read-coding-system "Charset: " charset)))
2359       (unless charset
2360         (setq charset gnus-newsgroup-charset))
2361       (when (or force
2362                 (and type (let ((case-fold-search t))
2363                             (string-match "quoted-printable" type))))
2364         (article-goto-body)
2365         (quoted-printable-decode-region
2366          (point) (point-max) (mm-charset-to-coding-system charset))))))
2367
2368 (defun article-de-base64-unreadable (&optional force read-charset)
2369   "Translate a base64 article.
2370 If FORCE, decode the article whether it is marked as base64 not.
2371 If READ-CHARSET, ask for a coding system."
2372   (interactive (list 'force current-prefix-arg))
2373   (save-excursion
2374     (let ((buffer-read-only nil) type charset)
2375       (if (gnus-buffer-live-p gnus-original-article-buffer)
2376           (with-current-buffer gnus-original-article-buffer
2377             (setq type
2378                   (gnus-fetch-field "content-transfer-encoding"))
2379             (let* ((ct (gnus-fetch-field "content-type"))
2380                    (ctl (and ct
2381                              (ignore-errors
2382                                (mail-header-parse-content-type ct)))))
2383               (setq charset (and ctl
2384                                  (mail-content-type-get ctl 'charset)))
2385               (if (stringp charset)
2386                   (setq charset (intern (downcase charset)))))))
2387       (if read-charset
2388           (setq charset (mm-read-coding-system "Charset: " charset)))
2389       (unless charset
2390         (setq charset gnus-newsgroup-charset))
2391       (when (or force
2392                 (and type (let ((case-fold-search t))
2393                             (string-match "base64" type))))
2394         (article-goto-body)
2395         (save-restriction
2396           (narrow-to-region (point) (point-max))
2397           (base64-decode-region (point-min) (point-max))
2398           (mm-decode-coding-region
2399            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2400
2401 (eval-when-compile
2402   (require 'rfc1843))
2403
2404 (defun article-decode-HZ ()
2405   "Translate a HZ-encoded article."
2406   (interactive)
2407   (require 'rfc1843)
2408   (save-excursion
2409     (let ((buffer-read-only nil))
2410       (rfc1843-decode-region (point-min) (point-max)))))
2411
2412 (defun article-unsplit-urls ()
2413   "Remove the newlines that some other mailers insert into URLs."
2414   (interactive)
2415   (save-excursion
2416     (let ((buffer-read-only nil))
2417       (goto-char (point-min))
2418       (while (re-search-forward
2419               "^\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2420         (replace-match "\\1\\3" t)))
2421     (when (interactive-p)
2422       (gnus-treat-article nil))))
2423
2424
2425 (defun article-wash-html (&optional read-charset)
2426   "Format an HTML article.
2427 If READ-CHARSET, ask for a coding system."
2428   (interactive "P")
2429   (save-excursion
2430     (let ((buffer-read-only nil)
2431           charset)
2432       (when (gnus-buffer-live-p gnus-original-article-buffer)
2433         (with-current-buffer gnus-original-article-buffer
2434           (let* ((ct (gnus-fetch-field "content-type"))
2435                  (ctl (and ct
2436                            (ignore-errors
2437                              (mail-header-parse-content-type ct)))))
2438             (setq charset (and ctl
2439                                (mail-content-type-get ctl 'charset)))
2440             (when (stringp charset)
2441               (setq charset (intern (downcase charset)))))))
2442       (when read-charset
2443         (setq charset (mm-read-coding-system "Charset: " charset)))
2444       (unless charset
2445         (setq charset gnus-newsgroup-charset))
2446       (article-goto-body)
2447       (save-window-excursion
2448         (save-restriction
2449           (narrow-to-region (point) (point-max))
2450           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2451                  (entry (assq func mm-text-html-washer-alist)))
2452             (when entry
2453               (setq func (cdr entry)))
2454             (cond
2455              ((functionp func)
2456               (funcall func))
2457              (t
2458               (apply (car func) (cdr func))))))))))
2459
2460 (defun gnus-article-wash-html-with-w3 ()
2461   "Wash the current buffer with w3."
2462   (mm-setup-w3)
2463   (let ((w3-strict-width (window-width))
2464         (url-standalone-mode t)
2465         (url-gateway-unplugged t)
2466         (w3-honor-stylesheets nil))
2467     (condition-case ()
2468         (w3-region (point-min) (point-max))
2469       (error))))
2470
2471 (defun gnus-article-wash-html-with-w3m ()
2472   "Wash the current buffer with emacs-w3m."
2473   (mm-setup-w3m)
2474   (save-restriction
2475     (narrow-to-region (point) (point-max))
2476     (let ((w3m-safe-url-regexp (if mm-inline-text-html-with-images
2477                                    nil
2478                                  "\\`cid:"))
2479           w3m-force-redisplay)
2480       (w3m-region (point-min) (point-max)))
2481     (when mm-inline-text-html-with-w3m-keymap
2482       (add-text-properties
2483        (point-min) (point-max)
2484        (nconc (mm-w3m-local-map-property)
2485               '(mm-inline-text-html-with-w3m t))))))
2486
2487 (defun article-hide-list-identifiers ()
2488   "Remove list identifies from the Subject header.
2489 The `gnus-list-identifiers' variable specifies what to do."
2490   (interactive)
2491   (let ((inhibit-point-motion-hooks t)
2492         (regexp (if (consp gnus-list-identifiers)
2493                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2494                   gnus-list-identifiers))
2495         buffer-read-only)
2496     (when regexp
2497       (save-excursion
2498         (save-restriction
2499           (article-narrow-to-head)
2500           (goto-char (point-min))
2501           (while (re-search-forward
2502                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2503                   nil t)
2504             (delete-region (match-beginning 2) (match-end 0))
2505             (beginning-of-line))
2506           (when (re-search-forward
2507                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2508             (delete-region (match-beginning 1) (match-end 1))))))))
2509
2510 (defun article-hide-pem (&optional arg)
2511   "Toggle hiding of any PEM headers and signatures in the current article.
2512 If given a negative prefix, always show; if given a positive prefix,
2513 always hide."
2514   (interactive (gnus-article-hidden-arg))
2515   (unless (gnus-article-check-hidden-text 'pem arg)
2516     (save-excursion
2517       (let (buffer-read-only end)
2518         (goto-char (point-min))
2519         ;; Hide the horrendously ugly "header".
2520         (when (and (search-forward
2521                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2522                     nil t)
2523                    (setq end (1+ (match-beginning 0))))
2524           (gnus-add-wash-type 'pem)
2525           (gnus-article-hide-text-type
2526            end
2527            (if (search-forward "\n\n" nil t)
2528                (match-end 0)
2529              (point-max))
2530            'pem)
2531           ;; Hide the trailer as well
2532           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2533                                 nil t)
2534             (gnus-article-hide-text-type
2535              (match-beginning 0) (match-end 0) 'pem)))))))
2536
2537 (defun article-strip-banner ()
2538   "Strip the banners specified by the `banner' group parameter and by
2539 `gnus-article-address-banner-alist'."
2540   (interactive)
2541   (save-excursion
2542     (save-restriction
2543       (let ((inhibit-point-motion-hooks t))
2544         (when (gnus-parameter-banner gnus-newsgroup-name)
2545           (article-really-strip-banner
2546            (gnus-parameter-banner gnus-newsgroup-name)))
2547         (when gnus-article-address-banner-alist
2548           (article-really-strip-banner
2549            (let ((from (save-restriction
2550                          (widen)
2551                          (article-narrow-to-head)
2552                          (mail-fetch-field "from"))))
2553              (when (and from
2554                         (setq from
2555                               (caar (mail-header-parse-addresses from))))
2556                (catch 'found
2557                  (dolist (pair gnus-article-address-banner-alist)
2558                    (when (string-match (car pair) from)
2559                      (throw 'found (cdr pair)))))))))))))
2560
2561 (defun article-really-strip-banner (banner)
2562   "Strip the banner specified by the argument."
2563   (save-excursion
2564     (save-restriction
2565       (let ((inhibit-point-motion-hooks t)
2566             (gnus-signature-limit nil)
2567             buffer-read-only)
2568         (article-goto-body)
2569         (cond
2570          ((eq banner 'signature)
2571           (when (gnus-article-narrow-to-signature)
2572             (widen)
2573             (forward-line -1)
2574             (delete-region (point) (point-max))))
2575          ((symbolp banner)
2576           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2577               (while (re-search-forward banner nil t)
2578                 (delete-region (match-beginning 0) (match-end 0)))))
2579          ((stringp banner)
2580           (while (re-search-forward banner nil t)
2581             (delete-region (match-beginning 0) (match-end 0)))))))))
2582
2583 (defun article-babel ()
2584   "Translate article using an online translation service."
2585   (interactive)
2586   (require 'babel)
2587   (save-excursion
2588     (set-buffer gnus-article-buffer)
2589     (when (article-goto-body)
2590       (let* ((buffer-read-only nil)
2591              (start (point))
2592              (end (point-max))
2593              (orig (buffer-substring start end))
2594              (trans (babel-as-string orig)))
2595         (save-restriction
2596           (narrow-to-region start end)
2597           (delete-region start end)
2598           (insert trans))))))
2599
2600 (defun article-hide-signature (&optional arg)
2601   "Hide the signature in the current article.
2602 If given a negative prefix, always show; if given a positive prefix,
2603 always hide."
2604   (interactive (gnus-article-hidden-arg))
2605   (save-excursion
2606     (save-restriction
2607       (if (interactive-p)
2608           (progn
2609             (widen)
2610             (article-goto-body))
2611         (goto-char (point-min)))
2612       (unless (gnus-article-check-hidden-text 'signature arg)
2613         (let ((buffer-read-only nil)
2614               (button (point)))
2615           (while (setq button (text-property-any button (point-max)
2616                                                  'gnus-callback
2617                                                  'gnus-signature-toggle))
2618             (setq button (text-property-not-all button (point-max)
2619                                                 'gnus-callback
2620                                                 'gnus-signature-toggle))
2621             (when (and button (not (eobp)))
2622               (gnus-article-hide-text-type
2623                (1+ button)
2624                (next-single-property-change (1+ button) 'mime-view-entity
2625                                             nil (point-max))
2626                'signature)))))))
2627   (gnus-set-mode-line 'article))
2628
2629 (defun article-strip-headers-in-body ()
2630   "Strip offensive headers from bodies."
2631   (interactive)
2632   (save-excursion
2633     (article-goto-body)
2634     (let ((case-fold-search t))
2635       (when (looking-at "x-no-archive:")
2636         (gnus-delete-line)))))
2637
2638 (defun article-strip-leading-blank-lines ()
2639   "Remove all blank lines from the beginning of the article."
2640   (interactive)
2641   (save-excursion
2642     (let ((inhibit-point-motion-hooks t)
2643           buffer-read-only)
2644       (when (article-goto-body)
2645         (while (and (not (eobp))
2646                     (looking-at "[ \t]*$"))
2647           (gnus-delete-line))))))
2648
2649 (defun article-narrow-to-head ()
2650   "Narrow the buffer to the head of the message.
2651 Point is left at the beginning of the narrowed-to region."
2652   (narrow-to-region
2653    (goto-char (point-min))
2654    (if (search-forward "\n\n" nil 1)
2655        (1- (point))
2656      (point-max)))
2657   (goto-char (point-min)))
2658
2659 (defun article-goto-body ()
2660   "Place point at the start of the body."
2661   (goto-char (point-min))
2662   (cond
2663    ;; This variable is only bound when dealing with separate
2664    ;; MIME body parts.
2665    (article-goto-body-goes-to-point-min-p
2666     t)
2667    ((search-forward "\n\n" nil t)
2668     t)
2669    (t
2670     (goto-char (point-max))
2671     nil)))
2672
2673 (defun article-strip-multiple-blank-lines ()
2674   "Replace consecutive blank lines with one empty line."
2675   (interactive)
2676   (save-excursion
2677     (let ((inhibit-point-motion-hooks t)
2678           buffer-read-only)
2679       ;; First make all blank lines empty.
2680       (article-goto-body)
2681       (while (re-search-forward "^[ \t]+$" nil t)
2682         (unless (gnus-annotation-in-region-p
2683                  (match-beginning 0) (match-end 0))
2684           (replace-match "" nil t)))
2685       ;; Then replace multiple empty lines with a single empty line.
2686       (article-goto-body)
2687       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2688         (unless (gnus-annotation-in-region-p
2689                  (match-beginning 0) (match-end 0))
2690           (delete-region (match-beginning 1) (match-end 1)))))))
2691
2692 (defun article-strip-leading-space ()
2693   "Remove all white space from the beginning of the lines in the article."
2694   (interactive)
2695   (save-excursion
2696     (let ((inhibit-point-motion-hooks t)
2697           buffer-read-only)
2698       (article-goto-body)
2699       (while (re-search-forward "^[ \t]+" nil t)
2700         (replace-match "" t t)))))
2701
2702 (defun article-strip-trailing-space ()
2703   "Remove all white space from the end of the lines in the article."
2704   (interactive)
2705   (save-excursion
2706     (let ((inhibit-point-motion-hooks t)
2707           buffer-read-only)
2708       (article-goto-body)
2709       (while (re-search-forward "[ \t]+$" nil t)
2710         (replace-match "" t t)))))
2711
2712 (defun article-strip-blank-lines ()
2713   "Strip leading, trailing and multiple blank lines."
2714   (interactive)
2715   (article-strip-leading-blank-lines)
2716   (article-remove-trailing-blank-lines)
2717   (article-strip-multiple-blank-lines))
2718
2719 (defun article-strip-all-blank-lines ()
2720   "Strip all blank lines."
2721   (interactive)
2722   (save-excursion
2723     (let ((inhibit-point-motion-hooks t)
2724           buffer-read-only)
2725       (article-goto-body)
2726       (while (re-search-forward "^[ \t]*\n" nil t)
2727         (replace-match "" t t)))))
2728
2729 (defun gnus-article-narrow-to-signature ()
2730   "Narrow to the signature; return t if a signature is found, else nil."
2731   (let ((inhibit-point-motion-hooks t))
2732     (when (gnus-article-search-signature)
2733       (forward-line 1)
2734       ;; Check whether we have some limits to what we consider
2735       ;; to be a signature.
2736       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2737                       (list gnus-signature-limit)))
2738             limit limited)
2739         (while (setq limit (pop limits))
2740           (if (or (and (integerp limit)
2741                        (< (- (point-max) (point)) limit))
2742                   (and (floatp limit)
2743                        (< (count-lines (point) (point-max)) limit))
2744                   (and (functionp limit)
2745                        (funcall limit))
2746                   (and (stringp limit)
2747                        (not (re-search-forward limit nil t))))
2748               ()                        ; This limit did not succeed.
2749             (setq limited t
2750                   limits nil)))
2751         (unless limited
2752           (narrow-to-region (point) (point-max))
2753           t)))))
2754
2755 (defun gnus-article-search-signature ()
2756   "Search the current buffer for the signature separator.
2757 Put point at the beginning of the signature separator."
2758   (let ((cur (point)))
2759     (goto-char (point-max))
2760     (if (if (stringp gnus-signature-separator)
2761             (re-search-backward gnus-signature-separator nil t)
2762           (let ((seps gnus-signature-separator))
2763             (while (and seps
2764                         (not (re-search-backward (car seps) nil t)))
2765               (pop seps))
2766             seps))
2767         t
2768       (goto-char cur)
2769       nil)))
2770
2771 (defun gnus-article-hidden-arg ()
2772   "Return the current prefix arg as a number, or 0 if no prefix."
2773   (list (if current-prefix-arg
2774             (prefix-numeric-value current-prefix-arg)
2775           0)))
2776
2777 (defun gnus-article-check-hidden-text (type arg)
2778   "Return nil if hiding is necessary.
2779 Arg can be nil or a number.  nil and positive means hide, negative
2780 means show, 0 means toggle."
2781   (save-excursion
2782     (save-restriction
2783       (let ((hide (gnus-article-hidden-text-p type)))
2784         (cond
2785          ((or (null arg)
2786               (> arg 0))
2787           nil)
2788          ((< arg 0)
2789           (gnus-article-show-hidden-text type)
2790           t)
2791          (t
2792           (if (eq hide 'hidden)
2793               (progn
2794                 (gnus-article-show-hidden-text type)
2795                 t)
2796             nil)))))))
2797
2798 (defun gnus-article-hidden-text-p (type)
2799   "Say whether the current buffer contains hidden text of type TYPE."
2800   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2801     (while (and pos
2802                 (not (get-text-property pos 'invisible))
2803                 (not (get-text-property pos 'dummy-invisible)))
2804       (setq pos
2805             (text-property-any (1+ pos) (point-max) 'article-type type)))
2806     (if pos
2807         'hidden
2808       nil)))
2809
2810 (defun gnus-article-show-hidden-text (type &optional dummy)
2811   "Show all hidden text of type TYPE.
2812 Originally it is hide instead of DUMMY."
2813   (let ((buffer-read-only nil)
2814         (inhibit-point-motion-hooks t))
2815     (gnus-remove-text-properties-when
2816      'article-type type
2817      (point-min) (point-max)
2818      (cons 'article-type (cons type
2819                                gnus-hidden-properties)))
2820     (gnus-delete-wash-type type)))
2821
2822 (defconst article-time-units
2823   `((year . ,(* 365.25 24 60 60))
2824     (week . ,(* 7 24 60 60))
2825     (day . ,(* 24 60 60))
2826     (hour . ,(* 60 60))
2827     (minute . 60)
2828     (second . 1))
2829   "Mapping from time units to seconds.")
2830
2831 (defun gnus-article-forward-header ()
2832   "Move point to the start of the next header.
2833 If the current header is a continuation header, this can be several
2834 lines forward."
2835   (let ((ended nil))
2836     (while (not ended)
2837       (forward-line 1)
2838       (if (looking-at "[ \t]+[^ \t]")
2839           (forward-line 1)
2840         (setq ended t)))))
2841
2842 (defun article-date-ut (&optional type highlight header)
2843   "Convert DATE date to universal time in the current article.
2844 If TYPE is `local', convert to local time; if it is `lapsed', output
2845 how much time has lapsed since DATE.  For `lapsed', the value of
2846 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2847 should replace the \"Date:\" one, or should be added below it."
2848   (interactive (list 'ut t))
2849   (let* ((header (or header
2850                      (and (eq 1 (point-min))
2851                           (mail-header-date (save-excursion
2852                                               (set-buffer gnus-summary-buffer)
2853                                               gnus-current-headers)))
2854                      (message-fetch-field "date")
2855                      ""))
2856          (date (if (vectorp header) (mail-header-date header)
2857                  header))
2858          (inhibit-point-motion-hooks t)
2859          bface eface date-pos)
2860     (when (and date (not (string= date "")))
2861       (save-excursion
2862         (save-restriction
2863           (article-narrow-to-head)
2864           (when (or (and (eq type 'lapsed)
2865                          gnus-article-date-lapsed-new-header
2866                          ;; Attempt to get the face of X-Sent first.
2867                          (re-search-forward "^X-Sent:[ \t]" nil t))
2868                     (re-search-forward "^Date:[ \t]" nil t)
2869                     ;; If Date is missing, try again for X-Sent.
2870                     (re-search-forward "^X-Sent:[ \t]" nil t))
2871             (setq bface (get-text-property (gnus-point-at-bol) 'face)
2872                   date (or (get-text-property (gnus-point-at-bol)
2873                                               'original-date)
2874                            date)
2875                   eface (get-text-property (1- (gnus-point-at-eol))
2876                                            'face)))
2877           (let ((buffer-read-only nil))
2878             ;; Delete any old X-Sent headers.
2879             (when (setq date-pos
2880                         (text-property-any (point-min) (point-max)
2881                                            'article-date-lapsed t))
2882               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2883               (delete-region (match-beginning 0)
2884                              (progn (forward-line 1) (point))))
2885             (goto-char (point-min))
2886             ;; Delete any old Date headers.
2887             (while (re-search-forward "^Date:[ \t]" nil t)
2888               (unless date-pos
2889                 (setq date-pos (match-beginning 0)))
2890               (unless (and (eq type 'lapsed)
2891                            gnus-article-date-lapsed-new-header)
2892                 (delete-region (match-beginning 0)
2893                                (progn (message-next-header) (point)))))
2894             (if date-pos
2895                 (progn
2896                   (goto-char date-pos)
2897                   (unless (bolp)
2898                     ;; Possibly, Date has been deleted.
2899                     (insert "\n"))
2900                   (when (and (eq type 'lapsed)
2901                              gnus-article-date-lapsed-new-header
2902                              (looking-at "Date:"))
2903                     (forward-line 1)))
2904               (goto-char (point-min)))
2905             (insert (article-make-date-line date type))
2906             (when (eq type 'lapsed)
2907               (put-text-property (gnus-point-at-bol) (point)
2908                                  'article-date-lapsed t))
2909             (insert "\n")
2910             (forward-line -1)
2911             ;; Do highlighting.
2912             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2913               (put-text-property (match-beginning 1) (1+ (match-end 1))
2914                                  'original-date date)
2915               (put-text-property (match-beginning 1) (1+ (match-end 1))
2916                                  'face bface)
2917               (put-text-property (match-beginning 2) (match-end 2)
2918                                  'face eface))))))))
2919
2920 (defun article-make-date-line (date type)
2921   "Return a DATE line of TYPE."
2922   (unless (memq type '(local ut original user iso8601 lapsed english))
2923     (error "Unknown conversion type: %s" type))
2924   (condition-case ()
2925       (let ((time (date-to-time date)))
2926         (cond
2927          ;; Convert to the local timezone.
2928          ((eq type 'local)
2929           (let ((tz (car (current-time-zone time))))
2930             (format "Date: %s %s%02d%02d" (current-time-string time)
2931                     (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2932                     (/ (% (abs tz) 3600) 60))))
2933          ;; Convert to Universal Time.
2934          ((eq type 'ut)
2935           (concat "Date: "
2936                   (current-time-string
2937                    (let* ((e (parse-time-string date))
2938                           (tm (apply 'encode-time e))
2939                           (ms (car tm))
2940                           (ls (- (cadr tm) (car (current-time-zone time)))))
2941                      (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2942                            ((> ls 65535) (list (1+ ms) (- ls 65536)))
2943                            (t (list ms ls)))))
2944                   " UT"))
2945          ;; Get the original date from the article.
2946          ((eq type 'original)
2947           (concat "Date: " (if (string-match "\n+$" date)
2948                                (substring date 0 (match-beginning 0))
2949                              date)))
2950          ;; Let the user define the format.
2951          ((eq type 'user)
2952           (let ((format (or (condition-case nil
2953                                 (with-current-buffer gnus-summary-buffer
2954                                   gnus-article-time-format)
2955                               (error nil))
2956                             gnus-article-time-format)))
2957             (if (functionp format)
2958                 (funcall format time)
2959               (concat "Date: " (format-time-string format time)))))
2960          ;; ISO 8601.
2961          ((eq type 'iso8601)
2962           (let ((tz (car (current-time-zone time))))
2963             (concat
2964              "Date: "
2965              (format-time-string "%Y%m%dT%H%M%S" time)
2966              (format "%s%02d%02d"
2967                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2968                      (/ (% (abs tz) 3600) 60)))))
2969          ;; Do an X-Sent lapsed format.
2970          ((eq type 'lapsed)
2971           ;; If the date is seriously mangled, the timezone functions are
2972           ;; liable to bug out, so we ignore all errors.
2973           (let* ((now (current-time))
2974                  (real-time (subtract-time now time))
2975                  (real-sec (and real-time
2976                                 (+ (* (float (car real-time)) 65536)
2977                                    (cadr real-time))))
2978                  (sec (and real-time (abs real-sec)))
2979                  num prev)
2980             (cond
2981              ((null real-time)
2982               "X-Sent: Unknown")
2983              ((zerop sec)
2984               "X-Sent: Now")
2985              (t
2986               (concat
2987                "X-Sent: "
2988                ;; This is a bit convoluted, but basically we go
2989                ;; through the time units for years, weeks, etc,
2990                ;; and divide things to see whether that results
2991                ;; in positive answers.
2992                (mapconcat
2993                 (lambda (unit)
2994                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2995                       ;; The (remaining) seconds are too few to
2996                       ;; be divided into this time unit.
2997                       ""
2998                     ;; It's big enough, so we output it.
2999                     (setq sec (- sec (* num (cdr unit))))
3000                     (prog1
3001                         (concat (if prev ", " "") (int-to-string
3002                                                    (floor num))
3003                                 " " (symbol-name (car unit))
3004                                 (if (> num 1) "s" ""))
3005                       (setq prev t))))
3006                 article-time-units "")
3007                ;; If dates are odd, then it might appear like the
3008                ;; article was sent in the future.
3009                (if (> real-sec 0)
3010                    " ago"
3011                  " in the future"))))))
3012          ;; Display the date in proper English
3013          ((eq type 'english)
3014           (let ((dtime (decode-time time)))
3015             (concat
3016              "Date: the "
3017              (number-to-string (nth 3 dtime))
3018              (let ((digit (% (nth 3 dtime) 10)))
3019                (cond
3020                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3021                 ((= digit 1) "st")
3022                 ((= digit 2) "nd")
3023                 ((= digit 3) "rd")
3024                 (t "th")))
3025              " of "
3026              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3027              " "
3028              (number-to-string (nth 5 dtime))
3029              " at "
3030              (format "%02d" (nth 2 dtime))
3031              ":"
3032              (format "%02d" (nth 1 dtime)))))))
3033     (error
3034      (format "Date: %s (from T-gnus)" date))))
3035
3036 (defun article-date-local (&optional highlight)
3037   "Convert the current article date to the local timezone."
3038   (interactive (list t))
3039   (article-date-ut 'local highlight))
3040
3041 (defun article-date-english (&optional highlight)
3042   "Convert the current article date to something that is proper English."
3043   (interactive (list t))
3044   (article-date-ut 'english highlight))
3045
3046 (defun article-date-original (&optional highlight)
3047   "Convert the current article date to what it was originally.
3048 This is only useful if you have used some other date conversion
3049 function and want to see what the date was before converting."
3050   (interactive (list t))
3051   (article-date-ut 'original highlight))
3052
3053 (defun article-date-lapsed (&optional highlight)
3054   "Convert the current article date to time lapsed since it was sent."
3055   (interactive (list t))
3056   (article-date-ut 'lapsed highlight))
3057
3058 (defun article-update-date-lapsed ()
3059   "Function to be run from a timer to update the lapsed time line."
3060   (let (deactivate-mark)
3061     (save-excursion
3062       (ignore-errors
3063         (walk-windows
3064          (lambda (w)
3065            (set-buffer (window-buffer w))
3066            (when (eq major-mode 'gnus-article-mode)
3067              (goto-char (point-min))
3068              (when (re-search-forward "^X-Sent:" nil t)
3069                (article-date-lapsed t))))
3070          nil 'visible)))))
3071
3072 (defun gnus-start-date-timer (&optional n)
3073   "Start a timer to update the X-Sent header in the article buffers.
3074 The numerical prefix says how frequently (in seconds) the function
3075 is to run."
3076   (interactive "p")
3077   (unless n
3078     (setq n 1))
3079   (gnus-stop-date-timer)
3080   (setq article-lapsed-timer
3081         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
3082
3083 (defun gnus-stop-date-timer ()
3084   "Stop the X-Sent timer."
3085   (interactive)
3086   (when article-lapsed-timer
3087     (nnheader-cancel-timer article-lapsed-timer)
3088     (setq article-lapsed-timer nil)))
3089
3090 (defun article-date-user (&optional highlight)
3091   "Convert the current article date to the user-defined format.
3092 This format is defined by the `gnus-article-time-format' variable."
3093   (interactive (list t))
3094   (article-date-ut 'user highlight))
3095
3096 (defun article-date-iso8601 (&optional highlight)
3097   "Convert the current article date to ISO8601."
3098   (interactive (list t))
3099   (article-date-ut 'iso8601 highlight))
3100
3101 ;; (defun article-show-all ()
3102 ;;   "Show all hidden text in the article buffer."
3103 ;;   (interactive)
3104 ;;   (save-excursion
3105 ;;     (widen)
3106 ;;     (let ((buffer-read-only nil))
3107 ;;       (gnus-article-unhide-text (point-min) (point-max))
3108 ;;       (gnus-remove-text-with-property 'gnus-prev)
3109 ;;       (gnus-remove-text-with-property 'gnus-next))))
3110
3111 (defun article-show-all-headers ()
3112   "Show all hidden headers in the article buffer."
3113   (interactive)
3114   (save-excursion
3115     (save-restriction
3116       (widen)
3117       (article-narrow-to-head)
3118       (let ((buffer-read-only nil))
3119         (gnus-article-unhide-text (point-min) (point-max))))))
3120
3121 (defun article-remove-leading-whitespace ()
3122   "Remove excessive whitespace from all headers."
3123   (interactive)
3124   (save-excursion
3125     (save-restriction
3126       (let ((buffer-read-only nil))
3127         (article-narrow-to-head)
3128         (goto-char (point-min))
3129         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3130           (delete-region (match-beginning 1) (match-end 1)))))))
3131
3132 (defun article-emphasize (&optional arg)
3133   "Emphasize text according to `gnus-emphasis-alist'."
3134   (interactive (gnus-article-hidden-arg))
3135   (unless (gnus-article-check-hidden-text 'emphasis arg)
3136     (save-excursion
3137       (let ((alist (or
3138                     (condition-case nil
3139                         (with-current-buffer gnus-summary-buffer
3140                           gnus-article-emphasis-alist)
3141                       (error))
3142                     gnus-emphasis-alist))
3143             (buffer-read-only nil)
3144             (props (append '(article-type emphasis)
3145                            gnus-hidden-properties))
3146             regexp elem beg invisible visible face)
3147         (article-goto-body)
3148         (setq beg (point))
3149         (while (setq elem (pop alist))
3150           (goto-char beg)
3151           (setq regexp (car elem)
3152                 invisible (nth 1 elem)
3153                 visible (nth 2 elem)
3154                 face (nth 3 elem))
3155           (while (re-search-forward regexp nil t)
3156             (when (and (match-beginning visible) (match-beginning invisible))
3157               (gnus-article-hide-text
3158                (match-beginning invisible) (match-end invisible) props)
3159               (gnus-article-unhide-text-type
3160                (match-beginning visible) (match-end visible) 'emphasis)
3161               (gnus-put-overlay-excluding-newlines
3162                (match-beginning visible) (match-end visible) 'face face)
3163               (gnus-add-wash-type 'emphasis)
3164               (goto-char (match-end invisible)))))))))
3165
3166 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3167   "Setup newsgroup emphasis alist."
3168   (unless gnus-article-emphasis-alist
3169     (let ((name (and gnus-newsgroup-name
3170                      (gnus-group-real-name gnus-newsgroup-name))))
3171       (make-local-variable 'gnus-article-emphasis-alist)
3172       (setq gnus-article-emphasis-alist
3173             (nconc
3174              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3175                (while (setq elem (pop alist))
3176                  (when (and name (string-match (car elem) name))
3177                    (setq alist nil
3178                          highlight (copy-sequence (cdr elem)))))
3179                highlight)
3180              (copy-sequence highlight-words)
3181              (if gnus-newsgroup-name
3182                  (copy-sequence (gnus-group-find-parameter
3183                                  gnus-newsgroup-name 'highlight-words t)))
3184              gnus-emphasis-alist)))))
3185
3186 (eval-when-compile
3187   (defvar gnus-summary-article-menu)
3188   (defvar gnus-summary-post-menu))
3189
3190 ;;; Saving functions.
3191
3192 (defun gnus-article-save (save-buffer file &optional num)
3193   "Save the currently selected article."
3194   (unless gnus-save-all-headers
3195     ;; Remove headers according to `gnus-saved-headers'.
3196     (let ((gnus-visible-headers
3197            (or gnus-saved-headers gnus-visible-headers))
3198           (gnus-article-buffer save-buffer))
3199       (save-excursion
3200         (set-buffer save-buffer)
3201         (article-hide-headers 1 t))))
3202   (save-window-excursion
3203     (if (not gnus-default-article-saver)
3204         (error "No default saver is defined")
3205       ;; !!! Magic!  The saving functions all save
3206       ;; `gnus-save-article-buffer' (or so they think), but we
3207       ;; bind that variable to our save-buffer.
3208       (set-buffer gnus-article-buffer)
3209       (let* ((gnus-save-article-buffer save-buffer)
3210              (filename
3211               (cond
3212                ((not gnus-prompt-before-saving) 'default)
3213                ((eq gnus-prompt-before-saving 'always) nil)
3214                (t file)))
3215              (gnus-number-of-articles-to-be-saved
3216               (when (eq gnus-prompt-before-saving t)
3217                 num)))                  ; Magic
3218         (set-buffer gnus-article-current-summary)
3219         (funcall gnus-default-article-saver filename)))))
3220
3221 (defun gnus-read-save-file-name (prompt &optional filename
3222                                         function group headers variable)
3223   (let ((default-name
3224           (funcall function group headers (symbol-value variable)))
3225         result)
3226     (setq result
3227           (expand-file-name
3228            (cond
3229             ((eq filename 'default)
3230              default-name)
3231             ((eq filename t)
3232              default-name)
3233             (filename filename)
3234             (t
3235              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3236                     (prompt
3237                      (format prompt
3238                              (if (and gnus-number-of-articles-to-be-saved
3239                                       (> gnus-number-of-articles-to-be-saved 1))
3240                                  (format "these %d articles"
3241                                          gnus-number-of-articles-to-be-saved)
3242                                "this article")))
3243                     (file
3244                      ;; Let the split methods have their say.
3245                      (cond
3246                       ;; No split name was found.
3247                       ((null split-name)
3248                        (read-file-name
3249                         (concat prompt " (default "
3250                                 (file-name-nondirectory default-name) ") ")
3251                         (file-name-directory default-name)
3252                         default-name))
3253                       ;; A single group name is returned.
3254                       ((stringp split-name)
3255                        (setq default-name
3256                              (funcall function split-name headers
3257                                       (symbol-value variable)))
3258                        (read-file-name
3259                         (concat prompt " (default "
3260                                 (file-name-nondirectory default-name) ") ")
3261                         (file-name-directory default-name)
3262                         default-name))
3263                       ;; A single split name was found
3264                       ((= 1 (length split-name))
3265                        (let* ((name (expand-file-name
3266                                      (car split-name)
3267                                      gnus-article-save-directory))
3268                               (dir (cond ((file-directory-p name)
3269                                           (file-name-as-directory name))
3270                                          ((file-exists-p name) name)
3271                                          (t gnus-article-save-directory))))
3272                          (read-file-name
3273                           (concat prompt " (default " name ") ")
3274                           dir name)))
3275                       ;; A list of splits was found.
3276                       (t
3277                        (setq split-name (nreverse split-name))
3278                        (let (result)
3279                          (let ((file-name-history
3280                                 (nconc split-name file-name-history)))
3281                            (setq result
3282                                  (expand-file-name
3283                                   (read-file-name
3284                                    (concat prompt " (`M-p' for defaults) ")
3285                                    gnus-article-save-directory
3286                                    (car split-name))
3287                                   gnus-article-save-directory)))
3288                          (car (push result file-name-history)))))))
3289                ;; Create the directory.
3290                (gnus-make-directory (file-name-directory file))
3291                ;; If we have read a directory, we append the default file name.
3292                (when (file-directory-p file)
3293                  (setq file (expand-file-name (file-name-nondirectory
3294                                                default-name)
3295                                               (file-name-as-directory file))))
3296                ;; Possibly translate some characters.
3297                (nnheader-translate-file-chars file))))))
3298     (gnus-make-directory (file-name-directory result))
3299     (set variable result)))
3300
3301 (defun gnus-article-archive-name (group)
3302   "Return the first instance of an \"Archive-name\" in the current buffer."
3303   (let ((case-fold-search t))
3304     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3305       (nnheader-concat gnus-article-save-directory
3306                        (match-string 1)))))
3307
3308 (defun gnus-article-nndoc-name (group)
3309   "If GROUP is an nndoc group, return the name of the parent group."
3310   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3311     (gnus-group-get-parameter group 'save-article-group)))
3312
3313 (defun gnus-summary-save-in-rmail (&optional filename)
3314   "Append this article to Rmail file.
3315 Optional argument FILENAME specifies file name.
3316 Directory to save to is default to `gnus-article-save-directory'."
3317   (setq filename (gnus-read-save-file-name
3318                   "Save %s in rmail file:" filename
3319                   gnus-rmail-save-name gnus-newsgroup-name
3320                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3321   (gnus-eval-in-buffer-window gnus-save-article-buffer
3322     (save-excursion
3323       (save-restriction
3324         (widen)
3325         (gnus-output-to-rmail filename))))
3326   filename)
3327
3328 (defun gnus-summary-save-in-mail (&optional filename)
3329   "Append this article to Unix mail file.
3330 Optional argument FILENAME specifies file name.
3331 Directory to save to is default to `gnus-article-save-directory'."
3332   (setq filename (gnus-read-save-file-name
3333                   "Save %s in Unix mail file:" filename
3334                   gnus-mail-save-name gnus-newsgroup-name
3335                   gnus-current-headers 'gnus-newsgroup-last-mail))
3336   (gnus-eval-in-buffer-window gnus-save-article-buffer
3337     (save-excursion
3338       (save-restriction
3339         (widen)
3340         (if (and (file-readable-p filename)
3341                  (file-regular-p filename)
3342                  (mail-file-babyl-p filename))
3343             (rmail-output-to-rmail-file filename t)
3344           (gnus-output-to-mail filename)))))
3345   filename)
3346
3347 (defun gnus-summary-save-in-file (&optional filename overwrite)
3348   "Append this article to file.
3349 Optional argument FILENAME specifies file name.
3350 Directory to save to is default to `gnus-article-save-directory'."
3351   (setq filename (gnus-read-save-file-name
3352                   "Save %s in file:" filename
3353                   gnus-file-save-name gnus-newsgroup-name
3354                   gnus-current-headers 'gnus-newsgroup-last-file))
3355   (gnus-eval-in-buffer-window gnus-save-article-buffer
3356     (save-excursion
3357       (save-restriction
3358         (widen)
3359         (when (and overwrite
3360                    (file-exists-p filename))
3361           (delete-file filename))
3362         (gnus-output-to-file filename))))
3363   filename)
3364
3365 (defun gnus-summary-write-to-file (&optional filename)
3366   "Write this article to a file, overwriting it if the file exists.
3367 Optional argument FILENAME specifies file name.
3368 The directory to save in defaults to `gnus-article-save-directory'."
3369   (gnus-summary-save-in-file nil t))
3370
3371 (defun gnus-summary-save-body-in-file (&optional filename)
3372   "Append this article body to a file.
3373 Optional argument FILENAME specifies file name.
3374 The directory to save in defaults to `gnus-article-save-directory'."
3375   (setq filename (gnus-read-save-file-name
3376                   "Save %s body in file:" filename
3377                   gnus-file-save-name gnus-newsgroup-name
3378                   gnus-current-headers 'gnus-newsgroup-last-file))
3379   (gnus-eval-in-buffer-window gnus-save-article-buffer
3380     (save-excursion
3381       (save-restriction
3382         (widen)
3383         (when (article-goto-body)
3384           (narrow-to-region (point) (point-max)))
3385         (gnus-output-to-file filename))))
3386   filename)
3387
3388 (defun gnus-summary-save-in-pipe (&optional command)
3389   "Pipe this article to subprocess."
3390   (setq command
3391         (cond ((and (eq command 'default)
3392                     gnus-last-shell-command)
3393                gnus-last-shell-command)
3394               ((stringp command)
3395                command)
3396               (t (read-string
3397                   (format
3398                    "Shell command on %s: "
3399                    (if (and gnus-number-of-articles-to-be-saved
3400                             (> gnus-number-of-articles-to-be-saved 1))
3401                        (format "these %d articles"
3402                                gnus-number-of-articles-to-be-saved)
3403                      "this article"))
3404                   gnus-last-shell-command))))
3405   (when (string-equal command "")
3406     (if gnus-last-shell-command
3407         (setq command gnus-last-shell-command)
3408       (error "A command is required")))
3409   (gnus-eval-in-buffer-window gnus-article-buffer
3410     (save-restriction
3411       (widen)
3412       (shell-command-on-region (point-min) (point-max) command nil)))
3413   (setq gnus-last-shell-command command))
3414
3415 (defmacro gnus-read-string (prompt &optional initial-contents history
3416                             default-value)
3417   "Like `read-string' but allow for older XEmacsen that don't have the 5th arg."
3418   (if (and (featurep 'xemacs)
3419            (< emacs-minor-version 2))
3420       `(read-string ,prompt ,initial-contents ,history)
3421     `(read-string ,prompt ,initial-contents ,history ,default-value)))
3422
3423 (defun gnus-summary-pipe-to-muttprint (&optional command)
3424   "Pipe this article to muttprint."
3425   (setq command (gnus-read-string
3426                  "Print using command: " gnus-summary-muttprint-program
3427                  nil gnus-summary-muttprint-program))
3428   (gnus-summary-save-in-pipe command))
3429
3430 ;;; Article file names when saving.
3431
3432 (defun gnus-capitalize-newsgroup (newsgroup)
3433   "Capitalize NEWSGROUP name."
3434   (when (not (zerop (length newsgroup)))
3435     (concat (char-to-string (upcase (aref newsgroup 0)))
3436             (substring newsgroup 1))))
3437
3438 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3439   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3440 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3441 Otherwise, it is like ~/News/news/group/num."
3442   (let ((default
3443           (expand-file-name
3444            (concat (if (gnus-use-long-file-name 'not-save)
3445                        (gnus-capitalize-newsgroup newsgroup)
3446                      (gnus-newsgroup-directory-form newsgroup))
3447                    "/" (int-to-string (mail-header-number headers)))
3448            gnus-article-save-directory)))
3449     (if (and last-file
3450              (string-equal (file-name-directory default)
3451                            (file-name-directory last-file))
3452              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3453         default
3454       (or last-file default))))
3455
3456 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3457   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3458 If variable `gnus-use-long-file-name' is non-nil, it is
3459 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3460   (let ((default
3461           (expand-file-name
3462            (concat (if (gnus-use-long-file-name 'not-save)
3463                        newsgroup
3464                      (gnus-newsgroup-directory-form newsgroup))
3465                    "/" (int-to-string (mail-header-number headers)))
3466            gnus-article-save-directory)))
3467     (if (and last-file
3468              (string-equal (file-name-directory default)
3469                            (file-name-directory last-file))
3470              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3471         default
3472       (or last-file default))))
3473
3474 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3475   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3476 If variable `gnus-use-long-file-name' is non-nil, it is
3477 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3478   (or last-file
3479       (expand-file-name
3480        (if (gnus-use-long-file-name 'not-save)
3481            newsgroup
3482          (file-relative-name
3483           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3484           default-directory))
3485        gnus-article-save-directory)))
3486
3487 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3488   "Generate file name from sender."
3489   (let ((from (mail-header-from headers)))
3490     (expand-file-name
3491      (if (and from (string-match "\\([^ <]+\\)@" from))
3492          (match-string 1 from)
3493        "nobody")
3494      gnus-article-save-directory)))
3495
3496 (defun article-verify-x-pgp-sig ()
3497   "Verify X-PGP-Sig."
3498   (interactive)
3499   (if (gnus-buffer-live-p gnus-original-article-buffer)
3500       (let ((sig (with-current-buffer gnus-original-article-buffer
3501                    (gnus-fetch-field "X-PGP-Sig")))
3502             items info headers)
3503         (when (and sig
3504                    mml2015-use
3505                    (mml2015-clear-verify-function))
3506           (with-temp-buffer
3507             (insert-buffer-substring gnus-original-article-buffer)
3508             (setq items (split-string sig))
3509             (message-narrow-to-head)
3510             (let ((inhibit-point-motion-hooks t)
3511                   (case-fold-search t))
3512               ;; Don't verify multiple headers.
3513               (setq headers (mapconcat (lambda (header)
3514                                          (concat header ": "
3515                                                  (mail-fetch-field header)
3516                                                  "\n"))
3517                                        (split-string (nth 1 items) ",") "")))
3518             (delete-region (point-min) (point-max))
3519             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3520             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3521             (insert headers)
3522             (widen)
3523             (forward-line)
3524             (while (not (eobp))
3525               (if (looking-at "^-")
3526                   (insert "- "))
3527               (forward-line))
3528             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3529             (insert "Version: " (car items) "\n\n")
3530             (insert (mapconcat 'identity (cddr items) "\n"))
3531             (insert "\n-----END PGP SIGNATURE-----\n")
3532             (let ((mm-security-handle (list (format "multipart/signed"))))
3533               (mml2015-clean-buffer)
3534               (let ((coding-system-for-write (or gnus-newsgroup-charset
3535                                                  'iso-8859-1)))
3536                 (funcall (mml2015-clear-verify-function)))
3537               (setq info
3538                     (or (mm-handle-multipart-ctl-parameter
3539                          mm-security-handle 'gnus-details)
3540                         (mm-handle-multipart-ctl-parameter
3541                          mm-security-handle 'gnus-info)))))
3542           (when info
3543             (let (buffer-read-only bface eface)
3544               (save-restriction
3545                 (message-narrow-to-head)
3546                 (goto-char (point-max))
3547                 (forward-line -1)
3548                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
3549                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
3550                 (message-remove-header "X-Gnus-PGP-Verify")
3551                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3552                     (forward-line)
3553                   (goto-char (point-max)))
3554                 (narrow-to-region (point) (point))
3555                 (insert "X-Gnus-PGP-Verify: " info "\n")
3556                 (goto-char (point-min))
3557                 (forward-line)
3558                 (while (not (eobp))
3559                   (if (not (looking-at "^[ \t]"))
3560                       (insert " "))
3561                   (forward-line))
3562                 ;; Do highlighting.
3563                 (goto-char (point-min))
3564                 (when (looking-at "\\([^:]+\\): *")
3565                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3566                                      'face bface)
3567                   (put-text-property (match-end 0) (point-max)
3568                                      'face eface)))))))))
3569
3570 (defun article-verify-cancel-lock ()
3571   "Verify Cancel-Lock header."
3572   (interactive)
3573   (if (gnus-buffer-live-p gnus-original-article-buffer)
3574       (canlock-verify gnus-original-article-buffer)))
3575
3576 (defun article-monafy ()
3577   "Display body part with mona font."
3578   (interactive)
3579   (unless (if (featurep 'xemacs)
3580               (find-face 'gnus-mona-face)
3581             (facep 'gnus-mona-face))
3582     (require 'navi2ch-mona)
3583     (set-face-font (make-face 'gnus-mona-face) navi2ch-mona-font))
3584   (save-excursion
3585     (let ((buffer-read-only nil))
3586       (article-goto-body)
3587       (gnus-overlay-put
3588        (gnus-make-overlay (point) (point-max))
3589        'face 'gnus-mona-face))))
3590
3591 (eval-and-compile
3592   (mapcar
3593    (lambda (func)
3594      (let (afunc gfunc)
3595        (if (consp func)
3596            (setq afunc (car func)
3597                  gfunc (cdr func))
3598          (setq afunc func
3599                gfunc (intern (format "gnus-%s" func))))
3600        (defalias gfunc
3601          (when (fboundp afunc)
3602            `(lambda (&optional interactive &rest args)
3603               ,(documentation afunc t)
3604               (interactive (list t))
3605               (save-excursion
3606                 (set-buffer gnus-article-buffer)
3607                 (if interactive
3608                     (call-interactively ',afunc)
3609                   (apply ',afunc args))))))))
3610    '(article-hide-headers
3611      article-verify-x-pgp-sig
3612      article-verify-cancel-lock
3613      article-monafy
3614      article-hide-boring-headers
3615      article-treat-overstrike
3616      article-fill-long-lines
3617      article-capitalize-sentences
3618      article-remove-cr
3619      article-remove-leading-whitespace
3620      article-display-x-face
3621      article-display-face
3622      article-de-quoted-unreadable
3623      article-de-base64-unreadable
3624      article-decode-HZ
3625      article-wash-html
3626      article-unsplit-urls
3627      article-hide-list-identifiers
3628      article-strip-banner
3629      article-babel
3630      article-hide-pem
3631      article-hide-signature
3632      article-strip-headers-in-body
3633      article-remove-trailing-blank-lines
3634      article-strip-leading-blank-lines
3635      article-strip-multiple-blank-lines
3636      article-strip-leading-space
3637      article-strip-trailing-space
3638      article-strip-blank-lines
3639      article-strip-all-blank-lines
3640      article-date-local
3641      article-date-english
3642      article-date-iso8601
3643      article-date-original
3644      article-date-ut
3645      article-decode-mime-words
3646      article-decode-charset
3647      article-decode-encoded-words
3648      article-date-user
3649      article-date-lapsed
3650      article-emphasize
3651      article-treat-dumbquotes
3652      article-normalize-headers
3653      (article-show-all-headers . gnus-article-show-all-headers)
3654 ;;      (article-show-all . gnus-article-show-all)
3655      )))
3656 \f
3657 ;;;
3658 ;;; Gnus article mode
3659 ;;;
3660
3661 (put 'gnus-article-mode 'mode-class 'special)
3662
3663 (gnus-define-keys gnus-article-mode-map
3664   " " gnus-article-goto-next-page
3665   "\177" gnus-article-goto-prev-page
3666   [delete] gnus-article-goto-prev-page
3667   [backspace] gnus-article-goto-prev-page
3668   "\C-c^" gnus-article-refer-article
3669   "h" gnus-article-show-summary
3670   "s" gnus-article-show-summary
3671   "\C-c\C-m" gnus-article-mail
3672   "?" gnus-article-describe-briefly
3673   "e" gnus-summary-edit-article
3674   "<" beginning-of-buffer
3675   ">" end-of-buffer
3676   "\C-c\C-i" gnus-info-find-node
3677   "\C-c\C-b" gnus-bug
3678   "R" gnus-article-reply-with-original
3679   "F" gnus-article-followup-with-original
3680   "\C-hk" gnus-article-describe-key
3681   "\C-hc" gnus-article-describe-key-briefly
3682
3683   "\C-d" gnus-article-read-summary-keys
3684   "\M-*" gnus-article-read-summary-keys
3685   "\M-#" gnus-article-read-summary-keys
3686   "\M-^" gnus-article-read-summary-keys
3687   "\M-g" gnus-article-read-summary-keys)
3688
3689 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
3690 (let (keys)
3691   (let ((key 32))
3692     (while (<= key 127)
3693       (push (char-to-string key) keys)
3694       (incf key))
3695     (dolist (elem (accessible-keymaps gnus-summary-mode-map))
3696       (setq key (car elem))
3697       (when (and (> (length key) 0)
3698                  (not (eq 'menu-bar (aref key 0)))
3699                  (symbolp (lookup-key gnus-summary-mode-map key)))
3700         (push key keys))))
3701   (dolist (key keys)
3702     (unless (lookup-key gnus-article-mode-map key)
3703       (define-key gnus-article-mode-map key 'gnus-article-read-summary-keys))))
3704
3705 (defun gnus-article-make-menu-bar ()
3706   (unless (boundp 'gnus-article-commands-menu)
3707     (gnus-summary-make-menu-bar))
3708   (gnus-turn-off-edit-menu 'article)
3709   (unless (boundp 'gnus-article-article-menu)
3710     (easy-menu-define
3711      gnus-article-article-menu gnus-article-mode-map ""
3712      '("Article"
3713        ["Scroll forwards" gnus-article-goto-next-page t]
3714        ["Scroll backwards" gnus-article-goto-prev-page t]
3715        ["Show summary" gnus-article-show-summary t]
3716        ["Fetch Message-ID at point" gnus-article-refer-article t]
3717        ["Mail to address at point" gnus-article-mail t]
3718        ["Send a bug report" gnus-bug t]))
3719
3720     (easy-menu-define
3721      gnus-article-treatment-menu gnus-article-mode-map ""
3722      ;; Fixme: this should use :active (and maybe :visible).
3723      '("Treatment"
3724        ["Hide headers" gnus-article-hide-headers t]
3725        ["Hide signature" gnus-article-hide-signature t]
3726        ["Hide citation" gnus-article-hide-citation t]
3727        ["Treat overstrike" gnus-article-treat-overstrike t]
3728        ["Remove carriage return" gnus-article-remove-cr t]
3729        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3730        ["Treat html" gnus-article-wash-html t]
3731        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3732        ["Decode HZ" gnus-article-decode-HZ t]))
3733
3734     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3735
3736     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3737
3738     (gnus-run-hooks 'gnus-article-menu-hook)))
3739
3740 (defun gnus-article-mode ()
3741   "Major mode for displaying an article.
3742
3743 All normal editing commands are switched off.
3744
3745 The following commands are available in addition to all summary mode
3746 commands:
3747 \\<gnus-article-mode-map>
3748 \\[gnus-article-next-page]\t Scroll the article one page forwards
3749 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3750 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3751 \\[gnus-article-show-summary]\t Display the summary buffer
3752 \\[gnus-article-mail]\t Send a reply to the address near point
3753 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3754 \\[gnus-info-find-node]\t Go to the Gnus info node"
3755   (interactive)
3756   (gnus-simplify-mode-line)
3757   (setq mode-name "Article")
3758   (setq major-mode 'gnus-article-mode)
3759   (make-local-variable 'minor-mode-alist)
3760   (unless (assq 'gnus-show-mime minor-mode-alist)
3761     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3762   (use-local-map gnus-article-mode-map)
3763   (when (gnus-visual-p 'article-menu 'menu)
3764     (gnus-article-make-menu-bar)
3765     (when gnus-summary-tool-bar-map
3766       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
3767   (gnus-update-format-specifications nil 'article-mode)
3768   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3769   (make-local-variable 'gnus-page-broken)
3770   (make-local-variable 'gnus-button-marker-list)
3771   (make-local-variable 'gnus-article-current-summary)
3772   (make-local-variable 'gnus-article-mime-handles)
3773   (make-local-variable 'gnus-article-decoded-p)
3774   (make-local-variable 'gnus-article-mime-handle-alist)
3775   (make-local-variable 'gnus-article-wash-types)
3776   (make-local-variable 'gnus-article-image-alist)
3777   (make-local-variable 'gnus-article-charset)
3778   (make-local-variable 'gnus-article-ignored-charsets)
3779   (gnus-set-default-directory)
3780   (buffer-disable-undo)
3781   (setq buffer-read-only t)
3782   (set-syntax-table gnus-article-mode-syntax-table)
3783   (gnus-run-hooks 'gnus-article-mode-hook))
3784
3785 (defun gnus-article-setup-buffer ()
3786   "Initialize the article buffer."
3787   (let* ((name (if gnus-single-article-buffer "*Article*"
3788                  (concat "*Article " gnus-newsgroup-name "*")))
3789          (original
3790           (progn (string-match "\\*Article" name)
3791                  (concat " *Original Article"
3792                          (substring name (match-end 0))))))
3793     (setq gnus-article-buffer name)
3794     (setq gnus-original-article-buffer original)
3795     (setq gnus-article-mime-handle-alist nil)
3796     ;; This might be a variable local to the summary buffer.
3797     (unless gnus-single-article-buffer
3798       (save-excursion
3799         (set-buffer gnus-summary-buffer)
3800         (setq gnus-article-buffer name)
3801         (setq gnus-original-article-buffer original)
3802         (gnus-set-global-variables)))
3803     (gnus-article-setup-highlight-words)
3804     ;; Init original article buffer.
3805     (save-excursion
3806       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3807       (set-buffer-multibyte t)
3808       (setq major-mode 'gnus-original-article-mode)
3809       (make-local-variable 'gnus-original-article))
3810     (if (get-buffer name)
3811         (save-excursion
3812           (set-buffer name)
3813           (when (and gnus-article-edit-mode
3814                      (buffer-modified-p)
3815                      (not
3816                       (y-or-n-p "Article mode edit in progress; discard? ")))
3817             (error "Action aborted"))
3818           (set (make-local-variable 'gnus-article-edit-mode) nil)
3819           (buffer-disable-undo)
3820           (setq buffer-read-only t)
3821           ;; This list just keeps growing if we don't reset it.
3822           (setq gnus-button-marker-list nil)
3823           (unless (eq major-mode 'gnus-article-mode)
3824             (gnus-article-mode))
3825           (current-buffer))
3826       (save-excursion
3827         (set-buffer (gnus-get-buffer-create name))
3828         (gnus-article-mode)
3829         (make-local-variable 'gnus-summary-buffer)
3830         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3831         (current-buffer)))))
3832
3833 ;; Set article window start at LINE, where LINE is the number of lines
3834 ;; from the head of the article.
3835 (defun gnus-article-set-window-start (&optional line)
3836   (set-window-start
3837    (gnus-get-buffer-window gnus-article-buffer t)
3838    (save-excursion
3839      (set-buffer gnus-article-buffer)
3840      (goto-char (point-min))
3841      (if (not line)
3842          (point-min)
3843        (gnus-message 6 "Moved to bookmark")
3844        (search-forward "\n\n" nil t)
3845        (forward-line line)
3846        (point)))))
3847
3848 ;;; @@ article filters
3849 ;;;
3850
3851 (defun gnus-article-display-mime-message ()
3852   "Article display method for MIME message."
3853   ;; called from `gnus-original-article-buffer'.
3854   (let (charset all-headers)
3855     (with-current-buffer gnus-summary-buffer
3856       (setq charset default-mime-charset
3857             all-headers gnus-have-all-headers))
3858     (make-local-variable 'default-mime-charset)
3859     (setq default-mime-charset charset)
3860     (with-current-buffer (get-buffer-create gnus-article-buffer)
3861       (make-local-variable 'default-mime-charset)
3862       (setq default-mime-charset charset))
3863     (mime-display-message mime-message-structure
3864                           gnus-article-buffer nil gnus-article-mode-map)
3865     (when all-headers
3866       (gnus-article-hide-headers nil -1)))
3867   (run-hooks 'gnus-mime-article-prepare-hook))
3868
3869 (defun gnus-article-display-traditional-message ()
3870   "Article display method for traditional message."
3871   (set-buffer gnus-article-buffer)
3872   (let (buffer-read-only)
3873     (erase-buffer)
3874     (insert-buffer-substring gnus-original-article-buffer)))
3875
3876 (defun gnus-article-make-full-mail-header (&optional number charset)
3877   "Create a new mail header structure in a raw article buffer."
3878   (unless (and number charset)
3879     (save-current-buffer
3880       (set-buffer gnus-summary-buffer)
3881       (unless number
3882         (setq number (or (cdr gnus-article-current) 0)))
3883       (unless charset
3884         (setq charset (or default-mime-charset 'x-ctext)))))
3885   (goto-char (point-min))
3886   (let ((header-end (if (search-forward "\n\n" nil t)
3887                         (1- (point))
3888                       (goto-char (point-max))))
3889         (chars (- (point-max) (point)))
3890         (lines (count-lines (point) (point-max)))
3891         (default-mime-charset charset)
3892         xref)
3893     (narrow-to-region (point-min) header-end)
3894     (setq xref (std11-fetch-field "xref"))
3895     (prog1
3896         (make-full-mail-header
3897          number
3898          (std11-fetch-field "subject")
3899          (std11-fetch-field "from")
3900          (std11-fetch-field "date")
3901          (std11-fetch-field "message-id")
3902          (std11-fetch-field "references")
3903          chars
3904          lines
3905          (when xref (concat "Xref: " xref)))
3906       (widen))))
3907
3908 (defun gnus-article-prepare (article &optional all-headers header)
3909   "Prepare ARTICLE in article mode buffer.
3910 ARTICLE should either be an article number or a Message-ID.
3911 If ARTICLE is an id, HEADER should be the article headers.
3912 If ALL-HEADERS is non-nil, no headers are hidden."
3913   (save-excursion
3914     ;; Make sure we start in a summary buffer.
3915     (unless (eq major-mode 'gnus-summary-mode)
3916       (set-buffer gnus-summary-buffer))
3917     (setq gnus-summary-buffer (current-buffer))
3918     (let* ((gnus-article (if header (mail-header-number header) article))
3919            (summary-buffer (current-buffer))
3920            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3921            (group gnus-newsgroup-name)
3922            result)
3923       (save-excursion
3924         (gnus-article-setup-buffer)
3925         (set-buffer gnus-article-buffer)
3926         ;; Deactivate active regions.
3927         (when (and (boundp 'transient-mark-mode)
3928                    transient-mark-mode)
3929           (setq mark-active nil))
3930         (if (not (setq result (let ((buffer-read-only nil))
3931                                 (gnus-request-article-this-buffer
3932                                  article group))))
3933             ;; There is no such article.
3934             (save-excursion
3935               (when (and (numberp article)
3936                          (not (memq article gnus-newsgroup-sparse)))
3937                 (setq gnus-article-current
3938                       (cons gnus-newsgroup-name article))
3939                 (set-buffer gnus-summary-buffer)
3940                 (setq gnus-current-article article)
3941                 (if (and (memq article gnus-newsgroup-undownloaded)
3942                          (not (gnus-online (gnus-find-method-for-group
3943                                             gnus-newsgroup-name))))
3944                     (progn
3945                       (gnus-summary-set-agent-mark article)
3946                       (message "Message marked for downloading"))
3947                   (gnus-summary-mark-article article gnus-canceled-mark)
3948                   (unless (memq article gnus-newsgroup-sparse)
3949                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3950           (if (or (eq result 'pseudo)
3951                   (eq result 'nneething))
3952               (progn
3953                 (save-excursion
3954                   (set-buffer summary-buffer)
3955                   (push article gnus-newsgroup-history)
3956                   (setq gnus-last-article gnus-current-article
3957                         gnus-current-article 0
3958                         gnus-current-headers nil
3959                         gnus-article-current nil)
3960                   (if (eq result 'nneething)
3961                       (gnus-configure-windows 'summary)
3962                     (gnus-configure-windows 'article))
3963                   (gnus-set-global-variables))
3964                 (let ((gnus-article-mime-handle-alist-1
3965                        gnus-article-mime-handle-alist))
3966                   (gnus-set-mode-line 'article)))
3967             ;; The result from the `request' was an actual article -
3968             ;; or at least some text that is now displayed in the
3969             ;; article buffer.
3970             (when (and (numberp article)
3971                        (not (eq article gnus-current-article)))
3972               ;; Seems like a new article has been selected.
3973               ;; `gnus-current-article' must be an article number.
3974               (save-excursion
3975                 (set-buffer summary-buffer)
3976                 (push article gnus-newsgroup-history)
3977                 (setq gnus-last-article gnus-current-article
3978                       gnus-current-article article
3979                       gnus-current-headers
3980                       (gnus-summary-article-header gnus-current-article)
3981                       gnus-article-current
3982                       (cons gnus-newsgroup-name gnus-current-article))
3983                 (unless (vectorp gnus-current-headers)
3984                   (setq gnus-current-headers nil))
3985                 (gnus-summary-goto-subject gnus-current-article)
3986                 (when (gnus-summary-show-thread)
3987                   ;; If the summary buffer really was folded, the
3988                   ;; previous goto may not actually have gone to
3989                   ;; the right article, but the thread root instead.
3990                   ;; So we go again.
3991                   (gnus-summary-goto-subject gnus-current-article))
3992                 (gnus-run-hooks 'gnus-mark-article-hook)
3993                 (gnus-set-mode-line 'summary)
3994                 (when (gnus-visual-p 'article-highlight 'highlight)
3995                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3996                 ;; Set the global newsgroup variables here.
3997                 (gnus-set-global-variables)
3998                 (setq gnus-have-all-headers
3999                       (or all-headers gnus-show-all-headers))))
4000             (save-excursion
4001               (gnus-configure-windows 'article))
4002             (when (or (numberp article)
4003                       (stringp article))
4004               (gnus-article-prepare-display)
4005               ;; Do page break.
4006               (goto-char (point-min))
4007               (setq gnus-page-broken
4008                     (when gnus-break-pages
4009                       (gnus-narrow-to-page)
4010                       t)))
4011             (let ((gnus-article-mime-handle-alist-1
4012                    gnus-article-mime-handle-alist))
4013               (gnus-set-mode-line 'article))
4014             (article-goto-body)
4015             (unless (bobp)
4016               (forward-line -1))
4017             (set-window-point (get-buffer-window (current-buffer)) (point))
4018             (gnus-configure-windows 'article)
4019             t))))))
4020
4021 (defun gnus-article-prepare-mime-display (&optional number)
4022   (goto-char (point-min))
4023   (when (re-search-forward "^[^\t ]+:" nil t)
4024     (goto-char (match-beginning 0)))
4025   (let ((entity (if (eq 1 (point-min))
4026                     (get-text-property 1 'mime-view-entity)
4027                   (get-text-property (point) 'mime-view-entity)))
4028         last-entity child-entity next type)
4029     (setq child-entity (mime-entity-children entity))
4030     (if child-entity
4031         (setq last-entity (nth (1- (length child-entity))
4032                                child-entity))
4033       (setq last-entity entity))
4034     (save-restriction
4035       (narrow-to-region (point)
4036                         (if (search-forward "\n\n" nil t)
4037                             (point)
4038                           (point-max)))
4039       (gnus-treat-article 'head)
4040       (put-text-property (point-min) (point-max) 'article-treated-header t)
4041       (goto-char (point-max)))
4042     (while (and (not (eobp)) entity)
4043       (setq next (set-marker
4044                   (make-marker)
4045                   (next-single-property-change (point) 'mime-view-entity
4046                                                nil (point-max))))
4047       (let ((types (mime-entity-content-type entity)))
4048         (while (eq 'multipart (mime-content-type-primary-type types))
4049           (setq entity (car (mime-entity-children entity))
4050                 types (mime-entity-content-type entity)))
4051         (when types
4052           (setq type (format "%s/%s"
4053                              (mime-content-type-primary-type types)
4054                              (mime-content-type-subtype types)))))
4055       (if (string-equal type "message/rfc822")
4056           (progn
4057             (setq next (point))
4058             (let ((children (mime-entity-children entity))
4059                   last-children)
4060               (when children
4061                 (setq last-children (nth (1- (length children)) children))
4062                 (while
4063                     (and
4064                      (not (eq last-children
4065                               (get-text-property next 'mime-view-entity)))
4066                      (setq next
4067                            (next-single-property-change next
4068                                                         'mime-view-entity
4069                                                         nil (point-max)))))))
4070             (setq next (next-single-property-change next 'mime-view-entity
4071                                                     nil (point-max)))
4072             (save-restriction
4073               (narrow-to-region (point) next)
4074               (gnus-article-prepare-mime-display)
4075               (goto-char (point-max)))
4076             (setq entity (get-text-property (point) 'mime-view-entity)))
4077         (save-restriction
4078           (narrow-to-region (point) next)
4079           ;; Kludge. We have to count true number, but for now,
4080           ;; part number is here only to achieve `last'.
4081           (gnus-treat-article nil 1
4082                               (if (eq entity last-entity)
4083                                   1 2)
4084                               type)
4085           (goto-char (point-max)))
4086         (setq entity (get-text-property next 'mime-view-entity))))))
4087
4088 ;;;###autoload
4089 (defun gnus-article-prepare-display ()
4090   "Make the current buffer look like a nice article."
4091   (let ((gnus-article-buffer (current-buffer))
4092         buffer-read-only)
4093     (unless (eq major-mode 'gnus-article-mode)
4094       (gnus-article-mode))
4095     (setq buffer-read-only nil
4096           gnus-button-marker-list nil
4097           gnus-article-wash-types nil
4098           gnus-article-image-alist nil)
4099     (save-restriction
4100       (widen)
4101       (static-if (featurep 'xemacs)
4102           (map-extents (lambda (extent maparg) (delete-extent extent)))
4103         (let ((lists (overlay-lists)))
4104           (dolist (overlay (nconc (car lists) (cdr lists)))
4105             (delete-overlay overlay)))))
4106     (gnus-run-hooks 'gnus-tmp-internal-hook))
4107   (let ((show-mime (unless (member gnus-newsgroup-name '("nndraft:delayed"
4108                                                          "nndraft:drafts"))
4109                      gnus-show-mime))
4110         (inhibit-read-only t))
4111     (set-buffer gnus-original-article-buffer)
4112     ;; Display message.
4113     (setq mime-message-structure gnus-current-headers)
4114     (mime-buffer-entity-set-buffer-internal mime-message-structure
4115                                             gnus-original-article-buffer)
4116     (mime-entity-set-representation-type-internal mime-message-structure
4117                                                   'mime-buffer-entity)
4118     (luna-send mime-message-structure 'initialize-instance
4119                mime-message-structure)
4120     (if show-mime
4121         (let (mime-display-header-hook mime-display-text/plain-hook)
4122           (funcall gnus-article-display-method-for-mime))
4123       (funcall gnus-article-display-method-for-traditional))
4124     ;; Call the treatment functions.
4125     (save-restriction
4126       (widen)
4127       (if show-mime
4128           (gnus-article-prepare-mime-display)
4129         (narrow-to-region (goto-char (point-min))
4130                           (if (search-forward "\n\n" nil t)
4131                               (point)
4132                             (point-max)))
4133         (gnus-treat-article 'head)
4134         (put-text-property (point-min) (point-max) 'article-treated-header t)
4135         (goto-char (point-max))
4136         (widen)
4137         (narrow-to-region (point) (point-max))
4138         (gnus-treat-article nil))
4139       (put-text-property (point-min) (point-max) 'read-only nil)))
4140   (gnus-run-hooks 'gnus-article-prepare-hook))
4141
4142 (defun gnus-article-decode-article-as-default-mime-charset ()
4143   "Decode an article as `default-mime-charset'.  It won't work if the
4144 value of the variable `gnus-show-mime' is non-nil."
4145   (unless (or gnus-show-mime
4146               (member gnus-newsgroup-name '("nndraft:delayed"
4147                                             "nndraft:drafts")))
4148     (set (make-local-variable 'default-mime-charset)
4149          (with-current-buffer gnus-summary-buffer
4150            default-mime-charset))
4151     (decode-mime-charset-region (point-min) (point-max)
4152                                 default-mime-charset)))
4153
4154 ;;;
4155 ;;; Gnus MIME viewing functions
4156 ;;;
4157
4158 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4159   "Format of the MIME buttons.
4160
4161 Valid specifiers include:
4162 %t  The MIME type
4163 %T  MIME type, along with additional info
4164 %n  The `name' parameter
4165 %d  The description, if any
4166 %l  The length of the encoded part
4167 %p  The part identifier number
4168 %e  Dots if the part isn't displayed
4169
4170 General format specifiers can also be used.  See Info node
4171 `(gnus)Formatting Variables'.")
4172
4173 (defvar gnus-mime-button-line-format-alist
4174   '((?t gnus-tmp-type ?s)
4175     (?T gnus-tmp-type-long ?s)
4176     (?n gnus-tmp-name ?s)
4177     (?d gnus-tmp-description ?s)
4178     (?p gnus-tmp-id ?s)
4179     (?l gnus-tmp-length ?d)
4180     (?e gnus-tmp-dots ?s)))
4181
4182 (defvar gnus-mime-button-commands
4183   '((gnus-article-press-button "\r" "Toggle Display")
4184     (gnus-mime-view-part "v" "View Interactively...")
4185     (gnus-mime-view-part-as-type "t" "View As Type...")
4186     (gnus-mime-view-part-as-charset "C" "View As charset...")
4187     (gnus-mime-save-part "o" "Save...")
4188     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4189     (gnus-mime-delete-part "d" "Delete part")
4190     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4191     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4192     (gnus-mime-view-part-internally "E" "View Internally")
4193     (gnus-mime-view-part-externally "e" "View Externally")
4194     (gnus-mime-print-part "p" "Print")
4195     (gnus-mime-pipe-part "|" "Pipe To Command...")
4196     (gnus-mime-action-on-part "." "Take action on the part...")))
4197
4198 (defun gnus-article-mime-part-status ()
4199   (with-current-buffer gnus-article-buffer
4200     (let ((entity (get-text-property (point-min) 'mime-view-entity))
4201           children)
4202       (if (and entity
4203                (setq children (mime-entity-children entity))
4204                (setq children (length children)))
4205           (if (eq 1 children)
4206               " (1 part)"
4207             (format " (%d parts)" children))
4208         ""))))
4209
4210 (defvar gnus-mime-button-map
4211   (let ((map (make-sparse-keymap)))
4212     (unless (>= (string-to-number emacs-version) 21)
4213       ;; XEmacs doesn't care.
4214       (set-keymap-parent map gnus-article-mode-map))
4215     (define-key map gnus-mouse-2 'gnus-article-push-button)
4216     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4217     (dolist (c gnus-mime-button-commands)
4218       (define-key map (cadr c) (car c)))
4219     map))
4220
4221 (easy-menu-define
4222   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4223   `("MIME Part"
4224     ,@(mapcar (lambda (c)
4225                 (vector (caddr c) (car c) :enable t))
4226               gnus-mime-button-commands)))
4227
4228 (eval-when-compile
4229   (define-compiler-macro popup-menu (&whole form
4230                                             menu &optional position prefix)
4231     (if (and (fboundp 'popup-menu)
4232              (not (memq 'popup-menu (assoc "lmenu" load-history))))
4233         form
4234       ;; Gnus is probably running under Emacs 20.
4235       `(let* ((menu (cdr ,menu))
4236               (response (x-popup-menu
4237                          t (list (car menu)
4238                                  (cons "" (mapcar (lambda (c)
4239                                                     (cons (caddr c) (car c)))
4240                                                   (cdr menu)))))))
4241          (if response
4242              (call-interactively (nth 3 (assq response menu))))))))
4243
4244 (defun gnus-mime-button-menu (event prefix)
4245  "Construct a context-sensitive menu of MIME commands."
4246  (interactive "e\nP")
4247  (save-window-excursion
4248    (let ((pos (event-start event)))
4249      (select-window (posn-window pos))
4250      (goto-char (posn-point pos))
4251      (gnus-article-check-buffer)
4252      (popup-menu gnus-mime-button-menu nil prefix))))
4253
4254 (defun gnus-mime-view-all-parts (&optional handles)
4255   "View all the MIME parts."
4256   (interactive)
4257   (save-current-buffer
4258     (set-buffer gnus-article-buffer)
4259     (let ((handles (or handles gnus-article-mime-handles))
4260           (mail-parse-charset gnus-newsgroup-charset)
4261           (mail-parse-ignored-charsets
4262            (with-current-buffer gnus-summary-buffer
4263              gnus-newsgroup-ignored-charsets)))
4264       (when handles
4265         (mm-remove-parts handles)
4266         (goto-char (point-min))
4267         (or (search-forward "\n\n") (goto-char (point-max)))
4268         (let (buffer-read-only)
4269           (delete-region (point) (point-max))
4270           (mm-display-parts handles))))))
4271
4272 (defun gnus-mime-save-part-and-strip ()
4273   "Save the MIME part under point then replace it with an external body."
4274   (interactive)
4275   (gnus-article-check-buffer)
4276   (let* ((data (get-text-property (point) 'gnus-data))
4277          file param
4278          (handles gnus-article-mime-handles))
4279     (if (mm-multiple-handles gnus-article-mime-handles)
4280         (error "This function is not implemented"))
4281     (setq file (and data (mm-save-part data)))
4282     (when file
4283       (with-current-buffer (mm-handle-buffer data)
4284         (erase-buffer)
4285         (insert "Content-Type: " (mm-handle-media-type data))
4286         (mml-insert-parameter-string (cdr (mm-handle-type data))
4287                                      '(charset))
4288         (insert "\n")
4289         (insert "Content-ID: " (message-make-message-id) "\n")
4290         (insert "Content-Transfer-Encoding: binary\n")
4291         (insert "\n"))
4292       (setcdr data
4293               (cdr (mm-make-handle nil
4294                                    `("message/external-body"
4295                                      (access-type . "LOCAL-FILE")
4296                                      (name . ,file)))))
4297       (set-buffer gnus-summary-buffer)
4298       (gnus-article-edit-article
4299        `(lambda ()
4300           (erase-buffer)
4301           (let ((mail-parse-charset (or gnus-article-charset
4302                                         ',gnus-newsgroup-charset))
4303                 (mail-parse-ignored-charsets
4304                  (or gnus-article-ignored-charsets
4305                      ',gnus-newsgroup-ignored-charsets))
4306                 (mbl mml-buffer-list))
4307             (setq mml-buffer-list nil)
4308             (insert-buffer gnus-original-article-buffer)
4309             (mime-to-mml ',handles)
4310             (setq gnus-article-mime-handles nil)
4311             (let ((mbl1 mml-buffer-list))
4312               (setq mml-buffer-list mbl)
4313               (set (make-local-variable 'mml-buffer-list) mbl1))
4314             (gnus-make-local-hook 'kill-buffer-hook)
4315             (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4316        `(lambda (no-highlight)
4317           (let ((mail-parse-charset (or gnus-article-charset
4318                                         ',gnus-newsgroup-charset))
4319                 (message-options message-options)
4320                 (message-options-set-recipient)
4321                 (mail-parse-ignored-charsets
4322                  (or gnus-article-ignored-charsets
4323                      ',gnus-newsgroup-ignored-charsets)))
4324             (mml-to-mime)
4325             (mml-destroy-buffers)
4326             (remove-hook 'kill-buffer-hook
4327                          'mml-destroy-buffers t)
4328             (kill-local-variable 'mml-buffer-list))
4329           (gnus-summary-edit-article-done
4330            ,(or (mail-header-references gnus-current-headers) "")
4331            ,(gnus-group-read-only-p)
4332            ,gnus-summary-buffer no-highlight))))))
4333
4334 (defun gnus-mime-delete-part ()
4335   "Delete the MIME part under point.
4336 Replace it with some information about the removed part."
4337   (interactive)
4338   (gnus-article-check-buffer)
4339   (unless (and gnus-novice-user
4340                (not (gnus-yes-or-no-p
4341                      "Really delete attachment forever? ")))
4342     (let* ((data (get-text-property (point) 'gnus-data))
4343            (handles gnus-article-mime-handles)
4344            (none "(none)")
4345            (description
4346             (or
4347              (mail-decode-encoded-word-string (or (mm-handle-description data)
4348                                                   none))))
4349            (filename
4350             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4351                 none))
4352            (type (mm-handle-media-type data)))
4353       (if (mm-multiple-handles gnus-article-mime-handles)
4354           (error "This function is not implemented"))
4355       (with-current-buffer (mm-handle-buffer data)
4356         (let ((bsize (format "%s" (buffer-size))))
4357           (erase-buffer)
4358           (insert
4359            (concat
4360             "<#part type=text/plain nofile=yes disposition=attachment"
4361             " description=\"Deleted attachment (" bsize " Byte)\">"
4362             ",----\n"
4363             "| The following attachment has been deleted:\n"
4364             "|\n"
4365             "| Type:           " type "\n"
4366             "| Filename:       " filename "\n"
4367             "| Size (encoded): " bsize " Byte\n"
4368             "| Description:    " description "\n"
4369             "`----\n"
4370             "<#/part>"))
4371           (setcdr data
4372                   (cdr (mm-make-handle nil `("text/plain"))))))
4373       (set-buffer gnus-summary-buffer)
4374       ;; FIXME: maybe some of the following code (borrowed from
4375       ;; `gnus-mime-save-part-and-strip') isn't necessary?
4376       (gnus-article-edit-article
4377        `(lambda ()
4378           (erase-buffer)
4379           (let ((mail-parse-charset (or gnus-article-charset
4380                                         ',gnus-newsgroup-charset))
4381                 (mail-parse-ignored-charsets
4382                  (or gnus-article-ignored-charsets
4383                      ',gnus-newsgroup-ignored-charsets))
4384                 (mbl mml-buffer-list))
4385             (setq mml-buffer-list nil)
4386             (insert-buffer gnus-original-article-buffer)
4387             (mime-to-mml ',handles)
4388             (setq gnus-article-mime-handles nil)
4389             (let ((mbl1 mml-buffer-list))
4390               (setq mml-buffer-list mbl)
4391               (set (make-local-variable 'mml-buffer-list) mbl1))
4392             (gnus-make-local-hook 'kill-buffer-hook)
4393             (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4394        `(lambda (no-highlight)
4395           (let ((mail-parse-charset (or gnus-article-charset
4396                                         ',gnus-newsgroup-charset))
4397                 (message-options message-options)
4398                 (message-options-set-recipient)
4399                 (mail-parse-ignored-charsets
4400                  (or gnus-article-ignored-charsets
4401                      ',gnus-newsgroup-ignored-charsets)))
4402             (mml-to-mime)
4403             (mml-destroy-buffers)
4404             (remove-hook 'kill-buffer-hook
4405                          'mml-destroy-buffers t)
4406             (kill-local-variable 'mml-buffer-list))
4407           (gnus-summary-edit-article-done
4408            ,(or (mail-header-references gnus-current-headers) "")
4409            ,(gnus-group-read-only-p)
4410            ,gnus-summary-buffer no-highlight)))))
4411   ;; Not in `gnus-mime-save-part-and-strip':
4412   (gnus-article-edit-done)
4413   (gnus-summary-expand-window)
4414   (gnus-summary-show-article))
4415
4416 (defun gnus-mime-save-part ()
4417   "Save the MIME part under point."
4418   (interactive)
4419   (gnus-article-check-buffer)
4420   (let ((data (get-text-property (point) 'gnus-data)))
4421     (when data
4422       (mm-save-part data))))
4423
4424 (defun gnus-mime-pipe-part ()
4425   "Pipe the MIME part under point to a process."
4426   (interactive)
4427   (gnus-article-check-buffer)
4428   (let ((data (get-text-property (point) 'gnus-data)))
4429     (when data
4430       (mm-pipe-part data))))
4431
4432 (defun gnus-mime-view-part ()
4433   "Interactively choose a viewing method for the MIME part under point."
4434   (interactive)
4435   (gnus-article-check-buffer)
4436   (let ((data (get-text-property (point) 'gnus-data)))
4437     (when data
4438       (setq gnus-article-mime-handles
4439             (mm-merge-handles
4440              gnus-article-mime-handles (setq data (copy-sequence data))))
4441       (mm-interactively-view-part data))))
4442
4443 (defun gnus-mime-view-part-as-type-internal ()
4444   (gnus-article-check-buffer)
4445   (let* ((name (mail-content-type-get
4446                 (mm-handle-type (get-text-property (point) 'gnus-data))
4447                 'name))
4448          (def-type (and name (mm-default-file-encoding name))))
4449     (and def-type (cons def-type 0))))
4450
4451 (defun gnus-mime-view-part-as-type (&optional mime-type)
4452   "Choose a MIME media type, and view the part as such."
4453   (interactive)
4454   (unless mime-type
4455     (setq mime-type (completing-read
4456                      "View as MIME type: "
4457                      (mapcar #'list (mailcap-mime-types))
4458                      nil nil
4459                      (gnus-mime-view-part-as-type-internal))))
4460   (gnus-article-check-buffer)
4461   (let ((handle (get-text-property (point) 'gnus-data)))
4462     (when handle
4463       (setq handle
4464             (mm-make-handle (mm-handle-buffer handle)
4465                             (cons mime-type (cdr (mm-handle-type handle)))
4466                             (mm-handle-encoding handle)
4467                             (mm-handle-undisplayer handle)
4468                             (mm-handle-disposition handle)
4469                             (mm-handle-description handle)
4470                             nil
4471                             (mm-handle-id handle)))
4472       (setq gnus-article-mime-handles
4473             (mm-merge-handles gnus-article-mime-handles handle))
4474       (gnus-mm-display-part handle))))
4475
4476 (eval-when-compile
4477   (require 'jka-compr))
4478
4479 ;; jka-compr.el uses a "sh -c" to direct stderr to err-file, but these days
4480 ;; emacs can do that itself.
4481 ;;
4482 (defun gnus-mime-jka-compr-maybe-uncompress ()
4483   "Uncompress the current buffer if `auto-compression-mode' is enabled.
4484 The uncompress method used is derived from `buffer-file-name'."
4485   (when (and (fboundp 'jka-compr-installed-p)
4486              (jka-compr-installed-p))
4487     (let ((info (jka-compr-get-compression-info buffer-file-name)))
4488       (when info
4489         (let ((basename (file-name-nondirectory buffer-file-name))
4490               (args     (jka-compr-info-uncompress-args    info))
4491               (prog     (jka-compr-info-uncompress-program info))
4492               (message  (jka-compr-info-uncompress-message info))
4493               (err-file (jka-compr-make-temp-name)))
4494           (if message
4495               (message "%s %s..." message basename))
4496           (unwind-protect
4497               (unless (memq (apply 'call-process-region
4498                                    (point-min) (point-max)
4499                                    prog
4500                                    t (list t err-file) nil
4501                                    args)
4502                             jka-compr-acceptable-retval-list)
4503                 (jka-compr-error prog args basename message err-file))
4504             (jka-compr-delete-temp-file err-file)))))))
4505
4506 (defun gnus-mime-copy-part (&optional handle)
4507   "Put the MIME part under point into a new buffer.
4508 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4509 are decompressed."
4510   (interactive)
4511   (gnus-article-check-buffer)
4512   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4513          (contents (and handle (mm-get-part handle)))
4514          (base (and handle
4515                     (file-name-nondirectory
4516                      (or
4517                       (mail-content-type-get (mm-handle-type handle) 'name)
4518                       (mail-content-type-get (mm-handle-disposition handle)
4519                                              'filename)
4520                       "*decoded*"))))
4521          (buffer (and base (generate-new-buffer base))))
4522     (when contents
4523       (switch-to-buffer buffer)
4524       (insert contents)
4525       ;; We do it this way to make `normal-mode' set the appropriate mode.
4526       (unwind-protect
4527           (progn
4528             (setq buffer-file-name (expand-file-name base))
4529             (gnus-mime-jka-compr-maybe-uncompress)
4530             (normal-mode))
4531         (setq buffer-file-name nil))
4532       (goto-char (point-min)))))
4533
4534 (defun gnus-mime-print-part (&optional handle filename)
4535   "Print the MIME part under point."
4536   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4537   (gnus-article-check-buffer)
4538   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4539          (contents (and handle (mm-get-part handle)))
4540          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4541          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
4542     (when contents
4543         (if printer
4544             (unwind-protect
4545                 (progn
4546                   (mm-save-part-to-file handle file)
4547                   (call-process shell-file-name nil
4548                                 (generate-new-buffer " *mm*")
4549                                 nil
4550                                 shell-command-switch
4551                                 (mm-mailcap-command
4552                                  printer file (mm-handle-type handle))))
4553               (delete-file file))
4554           (with-temp-buffer
4555             (insert contents)
4556             (gnus-print-buffer))
4557           (ps-despool filename)))))
4558
4559 (defun gnus-mime-inline-part (&optional handle arg)
4560   "Insert the MIME part under point into the current buffer."
4561   (interactive (list nil current-prefix-arg))
4562   (gnus-article-check-buffer)
4563   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4564          contents charset
4565          (b (point))
4566          buffer-read-only)
4567     (when handle
4568       (if (and (not arg) (mm-handle-undisplayer handle))
4569           (mm-remove-part handle)
4570         (setq contents (mm-get-part handle))
4571         (cond
4572          ((not arg)
4573           (setq charset (or (mail-content-type-get
4574                              (mm-handle-type handle) 'charset)
4575                             gnus-newsgroup-charset)))
4576          ((numberp arg)
4577           (if (mm-handle-undisplayer handle)
4578               (mm-remove-part handle))
4579           (setq charset
4580                 (or (cdr (assq arg
4581                                gnus-summary-show-article-charset-alist))
4582                     (mm-read-coding-system "Charset: ")))))
4583         (forward-line 2)
4584         (mm-insert-inline handle
4585                           (if (and charset
4586                                    (setq charset (mm-charset-to-coding-system
4587                                                   charset))
4588                                    (not (eq charset 'ascii)))
4589                               (mm-decode-coding-string contents charset)
4590                             contents))
4591         (goto-char b)))))
4592
4593 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4594   "Insert the MIME part under point into the current buffer using the
4595 specified charset."
4596   (interactive (list nil current-prefix-arg))
4597   (gnus-article-check-buffer)
4598   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4599          contents charset
4600          (b (point))
4601          buffer-read-only)
4602     (when handle
4603       (if (mm-handle-undisplayer handle)
4604           (mm-remove-part handle))
4605       (let ((gnus-newsgroup-charset
4606              (or (cdr (assq arg
4607                             gnus-summary-show-article-charset-alist))
4608                  (mm-read-coding-system "Charset: ")))
4609             (gnus-newsgroup-ignored-charsets 'gnus-all))
4610         (gnus-article-press-button)))))
4611
4612 (defun gnus-mime-view-part-externally (&optional handle)
4613   "View the MIME part under point with an external viewer."
4614   (interactive)
4615   (gnus-article-check-buffer)
4616   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4617          (mm-user-display-methods nil)
4618          (mm-inlined-types nil)
4619          (mail-parse-charset gnus-newsgroup-charset)
4620          (mail-parse-ignored-charsets
4621           (save-excursion (set-buffer gnus-summary-buffer)
4622                           gnus-newsgroup-ignored-charsets)))
4623     (when handle
4624       (if (mm-handle-undisplayer handle)
4625           (mm-remove-part handle)
4626         (mm-display-part handle)))))
4627
4628 (defun gnus-mime-view-part-internally (&optional handle)
4629   "View the MIME part under point with an internal viewer.
4630 If no internal viewer is available, use an external viewer."
4631   (interactive)
4632   (gnus-article-check-buffer)
4633   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4634          (mm-inlined-types '(".*"))
4635          (mm-inline-large-images t)
4636          (mail-parse-charset gnus-newsgroup-charset)
4637          (mail-parse-ignored-charsets
4638           (save-excursion (set-buffer gnus-summary-buffer)
4639                           gnus-newsgroup-ignored-charsets))
4640          buffer-read-only)
4641     (when handle
4642       (if (mm-handle-undisplayer handle)
4643           (mm-remove-part handle)
4644         (mm-display-part handle)))))
4645
4646 (defun gnus-mime-action-on-part (&optional action)
4647   "Do something with the MIME attachment at \(point\)."
4648   (interactive
4649    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
4650   (gnus-article-check-buffer)
4651   (let ((action-pair (assoc action gnus-mime-action-alist)))
4652     (if action-pair
4653         (funcall (cdr action-pair)))))
4654
4655 (defun gnus-article-part-wrapper (n function)
4656   (save-current-buffer
4657     (set-buffer gnus-article-buffer)
4658     (when (> n (length gnus-article-mime-handle-alist))
4659       (error "No such part"))
4660     (gnus-article-goto-part n)
4661     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4662       (funcall function handle))))
4663
4664 (defun gnus-article-pipe-part (n)
4665   "Pipe MIME part N, which is the numerical prefix."
4666   (interactive "p")
4667   (gnus-article-part-wrapper n 'mm-pipe-part))
4668
4669 (defun gnus-article-save-part (n)
4670   "Save MIME part N, which is the numerical prefix."
4671   (interactive "p")
4672   (gnus-article-part-wrapper n 'mm-save-part))
4673
4674 (defun gnus-article-interactively-view-part (n)
4675   "View MIME part N interactively, which is the numerical prefix."
4676   (interactive "p")
4677   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4678
4679 (defun gnus-article-copy-part (n)
4680   "Copy MIME part N, which is the numerical prefix."
4681   (interactive "p")
4682   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4683
4684 (defun gnus-article-view-part-as-charset (n)
4685   "Copy MIME part N, which is the numerical prefix."
4686   (interactive "p")
4687   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4688
4689 (defun gnus-article-view-part-externally (n)
4690   "View MIME part N externally, which is the numerical prefix."
4691   (interactive "p")
4692   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4693
4694 (defun gnus-article-inline-part (n)
4695   "Inline MIME part N, which is the numerical prefix."
4696   (interactive "p")
4697   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4698
4699 (defun gnus-article-mime-match-handle-first (condition)
4700   (if condition
4701       (let ((alist gnus-article-mime-handle-alist) ihandle n)
4702         (while (setq ihandle (pop alist))
4703           (if (and (cond
4704                     ((functionp condition)
4705                      (funcall condition (cdr ihandle)))
4706                     ((eq condition 'undisplayed)
4707                      (not (or (mm-handle-undisplayer (cdr ihandle))
4708                               (equal (mm-handle-media-type (cdr ihandle))
4709                                      "multipart/alternative"))))
4710                     ((eq condition 'undisplayed-alternative)
4711                      (not (mm-handle-undisplayer (cdr ihandle))))
4712                     (t t))
4713                    (gnus-article-goto-part (car ihandle))
4714                    (or (not n) (< (car ihandle) n)))
4715               (setq n (car ihandle))))
4716         (or n 1))
4717     1))
4718
4719 (defun gnus-article-view-part (&optional n)
4720   "View MIME part N, which is the numerical prefix."
4721   (interactive "P")
4722   (save-current-buffer
4723     (set-buffer gnus-article-buffer)
4724     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4725                              gnus-article-mime-match-handle-function)))
4726     (when (> n (length gnus-article-mime-handle-alist))
4727       (error "No such part"))
4728     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4729       (when (gnus-article-goto-part n)
4730         (if (equal (car handle) "multipart/alternative")
4731             (gnus-article-press-button)
4732           (when (eq (gnus-mm-display-part handle) 'internal)
4733             (gnus-set-window-start)))))))
4734
4735 (defsubst gnus-article-mime-total-parts ()
4736   (if (bufferp (car gnus-article-mime-handles))
4737       1 ;; single part
4738     (1- (length gnus-article-mime-handles))))
4739
4740 (defun gnus-mm-display-part (handle)
4741   "Display HANDLE and fix MIME button."
4742   (let ((id (get-text-property (point) 'gnus-part))
4743         (point (point))
4744         buffer-read-only)
4745     (forward-line 1)
4746     (prog1
4747         (let ((window (selected-window))
4748               (mail-parse-charset gnus-newsgroup-charset)
4749               (mail-parse-ignored-charsets
4750                (if (gnus-buffer-live-p gnus-summary-buffer)
4751                    (save-excursion
4752                      (set-buffer gnus-summary-buffer)
4753                      gnus-newsgroup-ignored-charsets)
4754                  nil)))
4755           (save-excursion
4756             (unwind-protect
4757                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4758                       (beg (point)))
4759                   (when win
4760                     (select-window win))
4761                   (goto-char point)
4762                   (forward-line)
4763                   (if (mm-handle-displayed-p handle)
4764                       ;; This will remove the part.
4765                       (mm-display-part handle)
4766                     (save-restriction
4767                       (narrow-to-region (point)
4768                                         (if (eobp) (point) (1+ (point))))
4769                       (mm-display-part handle)
4770                       ;; We narrow to the part itself and
4771                       ;; then call the treatment functions.
4772                       (goto-char (point-min))
4773                       (forward-line 1)
4774                       (narrow-to-region (point) (point-max))
4775                       (gnus-treat-article
4776                        nil id
4777                        (gnus-article-mime-total-parts)
4778                        (mm-handle-media-type handle)))))
4779               (if (window-live-p window)
4780                   (select-window window)))))
4781       (goto-char point)
4782       (gnus-delete-line)
4783       (gnus-insert-mime-button
4784        handle id (list (mm-handle-displayed-p handle)))
4785       (goto-char point))))
4786
4787 (defun gnus-article-goto-part (n)
4788   "Go to MIME part N."
4789   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
4790
4791 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4792   (let ((gnus-tmp-name
4793          (or (mail-content-type-get (mm-handle-type handle) 'name)
4794              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4795              (mail-content-type-get (mm-handle-type handle) 'url)
4796              ""))
4797         (gnus-tmp-type (mm-handle-media-type handle))
4798         (gnus-tmp-description
4799          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4800                                               "")))
4801         (gnus-tmp-dots
4802          (if (if displayed (car displayed)
4803                (mm-handle-displayed-p handle))
4804              "" "..."))
4805         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4806                            (buffer-size)))
4807         gnus-tmp-type-long b e)
4808     (when (string-match ".*/" gnus-tmp-name)
4809       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4810     (setq gnus-tmp-type-long (concat gnus-tmp-type
4811                                      (and (not (equal gnus-tmp-name ""))
4812                                           (concat "; " gnus-tmp-name))))
4813     (unless (equal gnus-tmp-description "")
4814       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4815     (unless (bolp)
4816       (insert "\n"))
4817     (setq b (point))
4818     (gnus-eval-format
4819      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4820      `(,@(gnus-local-map-property gnus-mime-button-map)
4821          gnus-callback gnus-mm-display-part
4822          gnus-part ,gnus-tmp-id
4823          article-type annotation
4824          gnus-data ,handle))
4825     (setq e (if (bolp)
4826                 ;; Exclude a newline.
4827                 (1- (point))
4828               (point)))
4829     (widget-convert-button
4830      'link b e
4831      :mime-handle handle
4832      :action 'gnus-widget-press-button
4833      :button-keymap gnus-mime-button-map
4834      :help-echo
4835      (lambda (widget/window &optional overlay pos)
4836        ;; Needed to properly clear the message due to a bug in
4837        ;; wid-edit (XEmacs only).
4838        (if (boundp 'help-echo-owns-message)
4839            (setq help-echo-owns-message t))
4840        (format
4841         "%S: %s the MIME part; %S: more options"
4842         (aref gnus-mouse-2 0)
4843         ;; XEmacs will get a single widget arg; Emacs 21 will get
4844         ;; window, overlay, position.
4845         (if (mm-handle-displayed-p
4846              (if overlay
4847                  (with-current-buffer (gnus-overlay-buffer overlay)
4848                    (widget-get (widget-at (gnus-overlay-start overlay))
4849                                :mime-handle))
4850                (widget-get widget/window :mime-handle)))
4851             "hide" "show")
4852         (aref gnus-down-mouse-3 0))))))
4853
4854 (defun gnus-widget-press-button (elems el)
4855   (goto-char (widget-get elems :from))
4856   (gnus-article-press-button))
4857
4858 (defvar gnus-displaying-mime nil)
4859
4860 (defun gnus-display-mime (&optional ihandles)
4861   "Display the MIME parts."
4862   (save-excursion
4863     (save-selected-window
4864       (let ((window (get-buffer-window gnus-article-buffer))
4865             (point (point)))
4866         (when window
4867           (select-window window)
4868           ;; We have to do this since selecting the window
4869           ;; may change the point.  So we set the window point.
4870           (set-window-point window point)))
4871       (let* ((handles (or ihandles
4872                           (mm-dissect-buffer nil gnus-article-loose-mime)
4873                           (and gnus-article-emulate-mime
4874                                (mm-uu-dissect))))
4875              buffer-read-only handle name type b e display)
4876         (when (and (not ihandles)
4877                    (not gnus-displaying-mime))
4878           ;; Top-level call; we clean up.
4879           (when gnus-article-mime-handles
4880             (mm-destroy-parts gnus-article-mime-handles)
4881             (setq gnus-article-mime-handle-alist nil));; A trick.
4882           (setq gnus-article-mime-handles handles)
4883           ;; We allow users to glean info from the handles.
4884           (when gnus-article-mime-part-function
4885             (gnus-mime-part-function handles)))
4886         (if (and handles
4887                  (or (not (stringp (car handles)))
4888                      (cdr handles)))
4889             (progn
4890               (when (and (not ihandles)
4891                          (not gnus-displaying-mime))
4892                 ;; Clean up for mime parts.
4893                 (article-goto-body)
4894                 (delete-region (point) (point-max)))
4895               (let ((gnus-displaying-mime t))
4896                 (gnus-mime-display-part handles)))
4897           (save-restriction
4898             (article-goto-body)
4899             (narrow-to-region (point) (point-max))
4900             (gnus-treat-article nil 1 1)
4901             (widen)))
4902         (unless ihandles
4903           ;; Highlight the headers.
4904           (save-excursion
4905             (save-restriction
4906               (article-goto-body)
4907               (narrow-to-region (point-min) (point))
4908               (gnus-treat-article 'head))))))))
4909
4910 (defcustom gnus-mime-display-multipart-as-mixed nil
4911   "Display \"multipart\" parts as  \"multipart/mixed\".
4912
4913 If t, it overrides nil values of
4914 `gnus-mime-display-multipart-alternative-as-mixed' and
4915 `gnus-mime-display-multipart-related-as-mixed'."
4916   :group 'gnus-article-mime
4917   :type 'boolean)
4918
4919 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
4920   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
4921   :group 'gnus-article-mime
4922   :type 'boolean)
4923
4924 (defcustom gnus-mime-display-multipart-related-as-mixed nil
4925   "Display \"multipart/related\" parts as  \"multipart/mixed\".
4926
4927 If displaying \"text/html\" is discouraged \(see
4928 `mm-discouraged-alternatives'\) images or other material inside a
4929 \"multipart/related\" part might be overlooked when this variable is nil."
4930   :group 'gnus-article-mime
4931   :type 'boolean)
4932
4933 (defun gnus-mime-display-part (handle)
4934   (cond
4935    ;; Single part.
4936    ((not (stringp (car handle)))
4937     (gnus-mime-display-single handle))
4938    ;; User-defined multipart
4939    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4940     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4941              handle))
4942    ;; multipart/alternative
4943    ((and (equal (car handle) "multipart/alternative")
4944          (not (or gnus-mime-display-multipart-as-mixed
4945                   gnus-mime-display-multipart-alternative-as-mixed)))
4946     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4947       (push (cons id handle) gnus-article-mime-handle-alist)
4948       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4949    ;; multipart/related
4950    ((and (equal (car handle) "multipart/related")
4951          (not (or gnus-mime-display-multipart-as-mixed
4952                   gnus-mime-display-multipart-related-as-mixed)))
4953     ;;;!!!We should find the start part, but we just default
4954     ;;;!!!to the first part.
4955     ;;(gnus-mime-display-part (cadr handle))
4956     ;;;!!! Most multipart/related is an HTML message plus images.
4957     ;;;!!! Unfortunately we are unable to let W3 display those
4958     ;;;!!! included images, so we just display it as a mixed multipart.
4959     ;;(gnus-mime-display-mixed (cdr handle))
4960     ;;;!!! No, w3 can display everything just fine.
4961     (gnus-mime-display-part (cadr handle)))
4962    ((equal (car handle) "multipart/signed")
4963     (gnus-add-wash-type 'signed)
4964     (gnus-mime-display-security handle))
4965    ((equal (car handle) "multipart/encrypted")
4966     (gnus-add-wash-type 'encrypted)
4967     (gnus-mime-display-security handle))
4968    ;; Other multiparts are handled like multipart/mixed.
4969    (t
4970     (gnus-mime-display-mixed (cdr handle)))))
4971
4972 (defun gnus-mime-part-function (handles)
4973   (if (stringp (car handles))
4974       (mapcar 'gnus-mime-part-function (cdr handles))
4975     (funcall gnus-article-mime-part-function handles)))
4976
4977 (defun gnus-mime-display-mixed (handles)
4978   (mapcar 'gnus-mime-display-part handles))
4979
4980 (defun gnus-mime-display-single (handle)
4981   (let ((type (mm-handle-media-type handle))
4982         (ignored gnus-ignored-mime-types)
4983         (not-attachment t)
4984         (move nil)
4985         display text)
4986     (catch 'ignored
4987       (progn
4988         (while ignored
4989           (when (string-match (pop ignored) type)
4990             (throw 'ignored nil)))
4991         (if (and (setq not-attachment
4992                        (and (not (mm-inline-override-p handle))
4993                             (or (not (mm-handle-disposition handle))
4994                                 (equal (car (mm-handle-disposition handle))
4995                                        "inline")
4996                                 (mm-attachment-override-p handle))))
4997                  (mm-automatic-display-p handle)
4998                  (or (and
4999                       (mm-inlinable-p handle)
5000                       (mm-inlined-p handle))
5001                      (mm-automatic-external-display-p type)))
5002             (setq display t)
5003           (when (equal (mm-handle-media-supertype handle) "text")
5004             (setq text t)))
5005         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5006               beg)
5007           (push (cons id handle) gnus-article-mime-handle-alist)
5008           (when (or (not display)
5009                     (not (gnus-unbuttonized-mime-type-p type)))
5010             ;(gnus-article-insert-newline)
5011             (gnus-insert-mime-button
5012              handle id (list (or display (and not-attachment text))))
5013             (gnus-article-insert-newline)
5014             ;(gnus-article-insert-newline)
5015             ;; Remember modify the number of forward lines.
5016             (setq move t))
5017           (setq beg (point))
5018           (cond
5019            (display
5020             (when move
5021               (forward-line -1)
5022               (setq beg (point)))
5023             (let ((mail-parse-charset gnus-newsgroup-charset)
5024                   (mail-parse-ignored-charsets
5025                    (save-excursion (condition-case ()
5026                                        (set-buffer gnus-summary-buffer)
5027                                      (error))
5028                                    gnus-newsgroup-ignored-charsets)))
5029               (mm-display-part handle t))
5030             (goto-char (point-max)))
5031            ((and text not-attachment)
5032             (when move
5033               (forward-line -1)
5034               (setq beg (point)))
5035             (gnus-article-insert-newline)
5036             (mm-insert-inline handle (mm-get-part handle))
5037             (goto-char (point-max))))
5038           ;; Do highlighting.
5039           (save-excursion
5040             (save-restriction
5041               (narrow-to-region beg (point))
5042               (gnus-treat-article
5043                nil id
5044                (gnus-article-mime-total-parts)
5045                (mm-handle-media-type handle)))))))))
5046
5047 (defun gnus-unbuttonized-mime-type-p (type)
5048   "Say whether TYPE is to be unbuttonized."
5049   (unless gnus-inhibit-mime-unbuttonizing
5050     (when (catch 'found
5051             (let ((types gnus-unbuttonized-mime-types))
5052               (while types
5053                 (when (string-match (pop types) type)
5054                   (throw 'found t)))))
5055       (not (catch 'found
5056              (let ((types gnus-buttonized-mime-types))
5057                (while types
5058                  (when (string-match (pop types) type)
5059                    (throw 'found t)))))))))
5060
5061 (defun gnus-article-insert-newline ()
5062   "Insert a newline, but mark it as undeletable."
5063   (gnus-put-text-property
5064    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5065
5066 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5067   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5068          (ihandles handles)
5069          (point (point))
5070          handle buffer-read-only from props begend not-pref)
5071     (save-window-excursion
5072       (save-restriction
5073         (when ibegend
5074           (narrow-to-region (car ibegend)
5075                             (or (cdr ibegend)
5076                                 (progn
5077                                   (goto-char (car ibegend))
5078                                   (forward-line 2)
5079                                   (point))))
5080           (delete-region (point-min) (point-max))
5081           (mm-remove-parts handles))
5082         (setq begend (list (point-marker)))
5083         ;; Do the toggle.
5084         (unless (setq not-pref (cadr (member preferred ihandles)))
5085           (setq not-pref (car ihandles)))
5086         (when (or ibegend
5087                   (not preferred)
5088                   (not (gnus-unbuttonized-mime-type-p
5089                         "multipart/alternative")))
5090           (gnus-add-text-properties
5091            (setq from (point))
5092            (progn
5093              (insert (format "%d.  " id))
5094              (point))
5095            `(gnus-callback
5096              (lambda (handles)
5097                (unless ,(not ibegend)
5098                  (setq gnus-article-mime-handle-alist
5099                        ',gnus-article-mime-handle-alist))
5100                (gnus-mime-display-alternative
5101                 ',ihandles ',not-pref ',begend ,id))
5102              ,@(gnus-local-map-property gnus-mime-button-map)
5103              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5104              face ,gnus-article-button-face
5105              gnus-part ,id
5106              gnus-data ,handle))
5107           (widget-convert-button 'link from (point)
5108                                  :action 'gnus-widget-press-button
5109                                  :button-keymap gnus-widget-button-keymap)
5110           ;; Do the handles
5111           (while (setq handle (pop handles))
5112             (gnus-add-text-properties
5113              (setq from (point))
5114              (progn
5115                (insert (format "(%c) %-18s"
5116                                (if (equal handle preferred) ?* ? )
5117                                (mm-handle-media-type handle)))
5118                (point))
5119              `(gnus-callback
5120                (lambda (handles)
5121                  (unless ,(not ibegend)
5122                    (setq gnus-article-mime-handle-alist
5123                          ',gnus-article-mime-handle-alist))
5124                  (gnus-mime-display-alternative
5125                   ',ihandles ',handle ',begend ,id))
5126                ,@(gnus-local-map-property gnus-mime-button-map)
5127                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5128                face ,gnus-article-button-face
5129                gnus-part ,id
5130                gnus-data ,handle))
5131             (widget-convert-button 'link from (point)
5132                                    :action 'gnus-widget-press-button
5133                                    :button-keymap gnus-widget-button-keymap)
5134             (insert "  "))
5135           (insert "\n\n"))
5136         (when preferred
5137           (if (stringp (car preferred))
5138               (gnus-display-mime preferred)
5139             (let ((mail-parse-charset gnus-newsgroup-charset)
5140                   (mail-parse-ignored-charsets
5141                    (save-excursion (set-buffer gnus-summary-buffer)
5142                                    gnus-newsgroup-ignored-charsets)))
5143               (mm-display-part preferred)
5144               ;; Do highlighting.
5145               (save-excursion
5146                 (save-restriction
5147                   (narrow-to-region (car begend) (point-max))
5148                   (gnus-treat-article
5149                    nil (length gnus-article-mime-handle-alist)
5150                    (gnus-article-mime-total-parts)
5151                    (mm-handle-media-type handle))))))
5152           (goto-char (point-max))
5153           (setcdr begend (point-marker)))))
5154     (when ibegend
5155       (goto-char point))))
5156
5157 (defconst gnus-article-wash-status-strings
5158   (let ((alist '((cite "c" "Possible hidden citation text"
5159                        " " "All citation text visible")
5160                  (headers "h" "Hidden headers"
5161                           " " "All headers visible.")
5162                  (pgp "p" "Encrypted or signed message status hidden"
5163                       " " "No hidden encryption nor digital signature status")
5164                  (signature "s" "Signature has been hidden"
5165                             " " "Signature is visible")
5166                  (overstrike "o" "Overstrike (^H) characters applied"
5167                              " " "No overstrike characters applied")
5168                  (gnus-show-mime "m" "Mime processing is activated"
5169                                  " " "Mime processing is not activated")
5170                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5171                            " " "No /*_emphasis_*/ characters applied")))
5172         result)
5173     (dolist (entry alist result)
5174       (let ((key (nth 0 entry))
5175             (on (copy-sequence (nth 1 entry)))
5176             (on-help (nth 2 entry))
5177             (off (copy-sequence (nth 3 entry)))
5178             (off-help (nth 4 entry)))
5179         (put-text-property 0 1 'help-echo on-help on)
5180         (put-text-property 0 1 'help-echo off-help off)
5181         (push (list key on off) result))))
5182   "Alist of strings describing wash status in the mode line.
5183 Each entry has the form (KEY ON OF), where the KEY is a symbol
5184 representing the particular washing function, ON is the string to use
5185 in the article mode line when the washing function is active, and OFF
5186 is the string to use when it is inactive.")
5187
5188 (defun gnus-article-wash-status-entry (key value)
5189   (let ((entry (assoc key gnus-article-wash-status-strings)))
5190     (if value (nth 1 entry) (nth 2 entry))))
5191
5192 (defun gnus-article-wash-status ()
5193   "Return a string which display status of article washing."
5194   (save-excursion
5195     (set-buffer gnus-article-buffer)
5196     (let ((cite (memq 'cite gnus-article-wash-types))
5197           (headers (memq 'headers gnus-article-wash-types))
5198           (boring (memq 'boring-headers gnus-article-wash-types))
5199           (pgp (memq 'pgp gnus-article-wash-types))
5200           (pem (memq 'pem gnus-article-wash-types))
5201           (signed (memq 'signed gnus-article-wash-types))
5202           (encrypted (memq 'encrypted gnus-article-wash-types))
5203           (signature (memq 'signature gnus-article-wash-types))
5204           (overstrike (memq 'overstrike gnus-article-wash-types))
5205           (emphasis (memq 'emphasis gnus-article-wash-types)))
5206       (concat
5207        (gnus-article-wash-status-entry 'cite cite)
5208        (gnus-article-wash-status-entry 'headers (or headers boring))
5209        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5210        (gnus-article-wash-status-entry 'signature signature)
5211        (gnus-article-wash-status-entry 'overstrike overstrike)
5212        (gnus-article-wash-status-entry 'gnus-show-mime gnus-show-mime)
5213        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5214
5215 (defun gnus-add-wash-type (type)
5216   "Add a washing of TYPE to the current status."
5217   (add-to-list 'gnus-article-wash-types type))
5218
5219 (defun gnus-delete-wash-type (type)
5220   "Add a washing of TYPE to the current status."
5221   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5222
5223 (defun gnus-add-image (category image)
5224   "Add IMAGE of CATEGORY to the list of displayed images."
5225   (let ((entry (assq category gnus-article-image-alist)))
5226     (unless entry
5227       (setq entry (list category))
5228       (push entry gnus-article-image-alist))
5229     (nconc entry (list image))))
5230
5231 (defun gnus-delete-images (category)
5232   "Delete all images in CATEGORY."
5233   (let ((entry (assq category gnus-article-image-alist)))
5234     (dolist (image (cdr entry))
5235       (gnus-remove-image image))
5236     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5237     (gnus-delete-wash-type category)))
5238
5239 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5240
5241 (defun gnus-article-maybe-hide-headers ()
5242   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5243 Provided for backwards compatibility."
5244   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5245                  (not (save-excursion (set-buffer gnus-summary-buffer)
5246                                       gnus-have-all-headers)))
5247              (not gnus-inhibit-hiding))
5248     (gnus-article-hide-headers)))
5249
5250 ;;; Article savers.
5251
5252 (defun gnus-output-to-file (file-name)
5253   "Append the current article to a file named FILE-NAME."
5254   (let ((artbuf (current-buffer)))
5255     (with-temp-buffer
5256       (insert-buffer-substring artbuf)
5257       ;; Append newline at end of the buffer as separator, and then
5258       ;; save it to file.
5259       (goto-char (point-max))
5260       (insert "\n")
5261       (let ((file-name-coding-system nnmail-pathname-coding-system)
5262             (pathname-coding-system nnmail-pathname-coding-system))
5263         (write-region-as-binary (point-min) (point-max) file-name 'append))
5264       t)))
5265
5266 (defun gnus-narrow-to-page (&optional arg)
5267   "Narrow the article buffer to a page.
5268 If given a numerical ARG, move forward ARG pages."
5269   (interactive "P")
5270   (setq arg (if arg (prefix-numeric-value arg) 0))
5271   (save-excursion
5272     (set-buffer gnus-article-buffer)
5273     (goto-char (point-min))
5274     (widen)
5275     ;; Remove any old next/prev buttons.
5276     (when (gnus-visual-p 'page-marker)
5277       (let ((buffer-read-only nil))
5278         (gnus-remove-text-with-property 'gnus-prev)
5279         (gnus-remove-text-with-property 'gnus-next)))
5280     (when
5281         (cond ((< arg 0)
5282                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
5283               ((> arg 0)
5284                (re-search-forward page-delimiter nil 'move arg)))
5285       (goto-char (match-end 0)))
5286     (narrow-to-region
5287      (point)
5288      (if (re-search-forward page-delimiter nil 'move)
5289          (match-beginning 0)
5290        (point)))
5291     (when (and (gnus-visual-p 'page-marker)
5292                (not (= (point-min) 1)))
5293       (save-excursion
5294         (goto-char (point-min))
5295         (gnus-insert-prev-page-button)))
5296     (when (and (gnus-visual-p 'page-marker)
5297                (< (+ (point-max) 2) (buffer-size)))
5298       (save-excursion
5299         (goto-char (point-max))
5300         (gnus-insert-next-page-button)))))
5301
5302 ;; Article mode commands
5303
5304 (defun gnus-article-goto-next-page ()
5305   "Show the next page of the article."
5306   (interactive)
5307   (when (gnus-article-next-page)
5308     (goto-char (point-min))
5309     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
5310
5311
5312 (defun gnus-article-goto-prev-page ()
5313   "Show the next page of the article."
5314   (interactive)
5315   (if (bobp)
5316       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
5317     (gnus-article-prev-page nil)))
5318
5319 ;; This is cleaner but currently breaks `gnus-pick-mode':
5320 ;;
5321 ;; (defun gnus-article-goto-next-page ()
5322 ;;   "Show the next page of the article."
5323 ;;   (interactive)
5324 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5325 ;;     (gnus-summary-next-page)))
5326 ;;
5327 ;; (defun gnus-article-goto-prev-page ()
5328 ;;   "Show the next page of the article."
5329 ;;   (interactive)
5330 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5331 ;;     (gnus-summary-prev-page)))
5332
5333 (defun gnus-article-next-page (&optional lines)
5334   "Show the next page of the current article.
5335 If end of article, return non-nil.  Otherwise return nil.
5336 Argument LINES specifies lines to be scrolled up."
5337   (interactive "p")
5338   (move-to-window-line -1)
5339   (if (save-excursion
5340         (end-of-line)
5341         (and (pos-visible-in-window-p)  ;Not continuation line.
5342              (>= (1+ (point)) (point-max)))) ;Allow for trailing newline.
5343       ;; Nothing in this page.
5344       (if (or (not gnus-page-broken)
5345               (save-excursion
5346                 (save-restriction
5347                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
5348           (progn
5349             (when gnus-article-over-scroll
5350               (gnus-article-next-page-1 lines))
5351             t)                  ;Nothing more.
5352         (gnus-narrow-to-page 1)         ;Go to next page.
5353         nil)
5354     ;; More in this page.
5355     (gnus-article-next-page-1 lines)
5356     nil))
5357
5358 (defun gnus-article-next-page-1 (lines)
5359   (let ((scroll-in-place nil))
5360     (condition-case ()
5361         (scroll-up lines)
5362       (end-of-buffer
5363        ;; Long lines may cause an end-of-buffer error.
5364        (goto-char (point-max)))))
5365   (move-to-window-line 0))
5366
5367 (defun gnus-article-prev-page (&optional lines)
5368   "Show previous page of current article.
5369 Argument LINES specifies lines to be scrolled down."
5370   (interactive "p")
5371   (move-to-window-line 0)
5372   (if (and gnus-page-broken
5373            (bobp)
5374            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
5375       (progn
5376         (gnus-narrow-to-page -1)        ;Go to previous page.
5377         (goto-char (point-max))
5378         (recenter -1))
5379     (let ((scroll-in-place nil))
5380       (prog1
5381           (condition-case ()
5382               (scroll-down lines)
5383             (beginning-of-buffer
5384              (goto-char (point-min))))
5385         (move-to-window-line 0)))))
5386
5387 (defun gnus-article-only-boring-p ()
5388   "Decide whether there is only boring text remaining in the article.
5389 Something \"interesting\" is a word of at least two letters that does
5390 not have a face in `gnus-article-boring-faces'."
5391   (when (and gnus-article-skip-boring
5392              (boundp 'gnus-article-boring-faces)
5393              (symbol-value 'gnus-article-boring-faces))
5394     (save-excursion
5395       (catch 'only-boring
5396         (while (re-search-forward "\\b\\w\\w" nil t)
5397           (forward-char -1)
5398           (when (not (gnus-intersection
5399                       (gnus-faces-at (point))
5400                       (symbol-value 'gnus-article-boring-faces)))
5401             (throw 'only-boring nil)))
5402         (throw 'only-boring t)))))
5403
5404 (defun gnus-article-refer-article ()
5405   "Read article specified by message-id around point."
5406   (interactive)
5407   (save-excursion
5408     (re-search-backward "[ \t]\\|^" (gnus-point-at-bol) t)
5409     (re-search-forward "<?news:<?\\|<" (gnus-point-at-eol) t)
5410     (if (re-search-forward "[^@ ]+@[^ \t>]+" (gnus-point-at-eol) t)
5411         (let ((msg-id (concat "<" (match-string 0) ">")))
5412           (set-buffer gnus-summary-buffer)
5413           (gnus-summary-refer-article msg-id))
5414       (error "No references around point"))))
5415
5416 (defun gnus-article-show-summary ()
5417   "Reconfigure windows to show summary buffer."
5418   (interactive)
5419   (if (not (gnus-buffer-live-p gnus-summary-buffer))
5420       (error "There is no summary buffer for this article buffer")
5421     (gnus-article-set-globals)
5422     (gnus-configure-windows 'article)
5423     (gnus-summary-goto-subject gnus-current-article)
5424     (gnus-summary-position-point)))
5425
5426 (defun gnus-article-describe-briefly ()
5427   "Describe article mode commands briefly."
5428   (interactive)
5429   (gnus-message 6 (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-goto-next-page]:Next page   \\[gnus-article-goto-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
5430
5431 (defun gnus-article-summary-command ()
5432   "Execute the last keystroke in the summary buffer."
5433   (interactive)
5434   (let ((obuf (current-buffer))
5435         (owin (current-window-configuration))
5436         func)
5437     (switch-to-buffer gnus-article-current-summary 'norecord)
5438     (setq func (lookup-key (current-local-map) (this-command-keys)))
5439     (call-interactively func)
5440     (set-buffer obuf)
5441     (set-window-configuration owin)
5442     (set-window-point (get-buffer-window (current-buffer)) (point))))
5443
5444 (defun gnus-article-summary-command-nosave ()
5445   "Execute the last keystroke in the summary buffer."
5446   (interactive)
5447   (let (func)
5448     (pop-to-buffer gnus-article-current-summary 'norecord)
5449     (setq func (lookup-key (current-local-map) (this-command-keys)))
5450     (call-interactively func)))
5451
5452 (defun gnus-article-check-buffer ()
5453   "Beep if not in an article buffer."
5454   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
5455     (error "Command invoked outside of a Gnus article buffer")))
5456
5457 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5458   "Read a summary buffer key sequence and execute it from the article buffer."
5459   (interactive "P")
5460   (gnus-article-check-buffer)
5461   (let ((nosaves
5462          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5463            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5464            "=" "^" "\M-^" "|"))
5465         (nosave-but-article
5466          '("A\r"))
5467         (nosave-in-article
5468          '("\C-d"))
5469         (up-to-top
5470          '("n" "Gn" "p" "Gp"))
5471         keys new-sum-point)
5472     (save-excursion
5473       (set-buffer gnus-article-current-summary)
5474       (let (gnus-pick-mode)
5475         (push (or key last-command-event) unread-command-events)
5476         (setq keys (static-if (featurep 'xemacs)
5477                        (events-to-keys (read-key-sequence nil))
5478                      (read-key-sequence nil)))))
5479     (message "")
5480
5481     (if (or (member keys nosaves)
5482             (member keys nosave-but-article)
5483             (member keys nosave-in-article))
5484         (let (func)
5485           (save-window-excursion
5486             (pop-to-buffer gnus-article-current-summary 'norecord)
5487             ;; We disable the pick minor mode commands.
5488             (let (gnus-pick-mode)
5489               (setq func (lookup-key (current-local-map) keys))))
5490           (if (or (not func)
5491                   (numberp func))
5492               (ding)
5493             (unless (member keys nosave-in-article)
5494               (set-buffer gnus-article-current-summary))
5495             (call-interactively func)
5496             (setq new-sum-point (point)))
5497           (when (member keys nosave-but-article)
5498             (pop-to-buffer gnus-article-buffer 'norecord)))
5499       ;; These commands should restore window configuration.
5500       (let ((obuf (current-buffer))
5501             (owin (current-window-configuration))
5502             (opoint (point))
5503             (summary gnus-article-current-summary)
5504             func in-buffer selected)
5505         (if not-restore-window
5506             (pop-to-buffer summary 'norecord)
5507           (switch-to-buffer summary 'norecord))
5508         (setq in-buffer (current-buffer))
5509         ;; We disable the pick minor mode commands.
5510         (if (and (setq func (let (gnus-pick-mode)
5511                               (lookup-key (current-local-map) keys)))
5512                  (functionp func))
5513             (progn
5514               (call-interactively func)
5515               (setq new-sum-point (point))
5516               (when (eq in-buffer (current-buffer))
5517                 (setq selected (gnus-summary-select-article))
5518                 (set-buffer obuf)
5519                 (unless not-restore-window
5520                   (set-window-configuration owin))
5521                 (when (eq selected 'old)
5522                   (article-goto-body)
5523                   (set-window-start (get-buffer-window (current-buffer))
5524                                     1)
5525                   (set-window-point (get-buffer-window (current-buffer))
5526                                     (point)))
5527                 (let ((win (get-buffer-window gnus-article-current-summary)))
5528                   (when win
5529                     (set-window-point win new-sum-point))))    )
5530           (switch-to-buffer gnus-article-buffer)
5531           (ding))))))
5532
5533 (defun gnus-article-describe-key (key)
5534   "Display documentation of the function invoked by KEY.  KEY is a string."
5535   (interactive "kDescribe key: ")
5536   (gnus-article-check-buffer)
5537   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5538       (save-excursion
5539         (set-buffer gnus-article-current-summary)
5540         (let (gnus-pick-mode)
5541           (if (featurep 'xemacs)
5542               (progn
5543                 (push (elt key 0) unread-command-events)
5544                 (setq key (events-to-keys
5545                            (read-key-sequence "Describe key: "))))
5546             (setq unread-command-events
5547                   (mapcar
5548                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5549                    key))
5550             (setq key (read-key-sequence "Describe key: "))))
5551         (describe-key key))
5552     (describe-key key)))
5553
5554 (defun gnus-article-describe-key-briefly (key &optional insert)
5555   "Display documentation of the function invoked by KEY.  KEY is a string."
5556   (interactive "kDescribe key: \nP")
5557   (gnus-article-check-buffer)
5558   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5559       (save-excursion
5560         (set-buffer gnus-article-current-summary)
5561         (let (gnus-pick-mode)
5562           (if (featurep 'xemacs)
5563               (progn
5564                 (push (elt key 0) unread-command-events)
5565                 (setq key (events-to-keys
5566                            (read-key-sequence "Describe key: "))))
5567             (setq unread-command-events
5568                   (mapcar
5569                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5570                    key))
5571             (setq key (read-key-sequence "Describe key: "))))
5572         (describe-key-briefly key insert))
5573     (describe-key-briefly key insert)))
5574
5575 (defun gnus-article-reply-with-original (&optional wide)
5576   "Start composing a reply mail to the current message.
5577 The text in the region will be yanked.  If the region isn't active,
5578 the entire article will be yanked."
5579   (interactive "P")
5580   (let ((article (cdr gnus-article-current))
5581         contents)
5582     (if (not (gnus-mark-active-p))
5583         (with-current-buffer gnus-summary-buffer
5584           (gnus-summary-reply (list (list article)) wide))
5585       (setq contents (buffer-substring (point) (mark t)))
5586       ;; Deactivate active regions.
5587       (when (and (boundp 'transient-mark-mode)
5588                  transient-mark-mode)
5589         (setq mark-active nil))
5590       (with-current-buffer gnus-summary-buffer
5591         (gnus-summary-reply
5592          (list (list article contents)) wide)))))
5593
5594 (defun gnus-article-followup-with-original ()
5595   "Compose a followup to the current article.
5596 The text in the region will be yanked.  If the region isn't active,
5597 the entire article will be yanked."
5598   (interactive)
5599   (let ((article (cdr gnus-article-current))
5600         contents)
5601       (if (not (gnus-mark-active-p))
5602           (with-current-buffer gnus-summary-buffer
5603             (gnus-summary-followup (list (list article))))
5604         (setq contents (buffer-substring (point) (mark t)))
5605         ;; Deactivate active regions.
5606         (when (and (boundp 'transient-mark-mode)
5607                    transient-mark-mode)
5608           (setq mark-active nil))
5609         (with-current-buffer gnus-summary-buffer
5610           (gnus-summary-followup
5611            (list (list article contents)))))))
5612
5613 (defun gnus-article-hide (&optional arg force)
5614   "Hide all the gruft in the current article.
5615 This means that signatures, cited text and (some) headers will be
5616 hidden.
5617 If given a prefix, show the hidden text instead."
5618   (interactive (append (gnus-article-hidden-arg) (list 'force)))
5619   (gnus-article-hide-headers arg)
5620   (gnus-article-hide-list-identifiers arg)
5621   (gnus-article-hide-citation-maybe arg force)
5622   (gnus-article-hide-signature arg))
5623
5624 (defun gnus-article-maybe-highlight ()
5625   "Do some article highlighting if article highlighting is requested."
5626   (when (gnus-visual-p 'article-highlight 'highlight)
5627     (gnus-article-highlight-some)))
5628
5629 (defun gnus-check-group-server ()
5630   ;; Make sure the connection to the server is alive.
5631   (unless (gnus-server-opened
5632            (gnus-find-method-for-group gnus-newsgroup-name))
5633     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
5634     (gnus-request-group gnus-newsgroup-name t)))
5635
5636 (eval-when-compile
5637   (autoload 'nneething-get-file-name "nneething"))
5638
5639 (defun gnus-request-article-this-buffer (article group)
5640   "Get an article and insert it into this buffer."
5641   (let (do-update-line sparse-header)
5642     (prog1
5643         (save-excursion
5644           (erase-buffer)
5645           (gnus-kill-all-overlays)
5646           (setq group (or group gnus-newsgroup-name))
5647
5648           ;; Using `gnus-request-article' directly will insert the article into
5649           ;; `nntp-server-buffer' - so we'll save some time by not having to
5650           ;; copy it from the server buffer into the article buffer.
5651
5652           ;; We only request an article by message-id when we do not have the
5653           ;; headers for it, so we'll have to get those.
5654           (when (stringp article)
5655             (gnus-read-header article))
5656
5657           ;; If the article number is negative, that means that this article
5658           ;; doesn't belong in this newsgroup (possibly), so we find its
5659           ;; message-id and request it by id instead of number.
5660           (when (and (numberp article)
5661                      gnus-summary-buffer
5662                      (get-buffer gnus-summary-buffer)
5663                      (gnus-buffer-exists-p gnus-summary-buffer))
5664             (save-excursion
5665               (set-buffer gnus-summary-buffer)
5666               (let ((header (gnus-summary-article-header article)))
5667                 (when (< article 0)
5668                   (cond
5669                    ((memq article gnus-newsgroup-sparse)
5670                     ;; This is a sparse gap article.
5671                     (setq do-update-line article)
5672                     (setq article (mail-header-id header))
5673                     (setq sparse-header (gnus-read-header article))
5674                     (setq gnus-newsgroup-sparse
5675                           (delq article gnus-newsgroup-sparse)))
5676                    ((vectorp header)
5677                     ;; It's a real article.
5678                     (setq article (mail-header-id header)))
5679                    (t
5680                     ;; It is an extracted pseudo-article.
5681                     (setq article 'pseudo)
5682                     (gnus-request-pseudo-article header))))
5683
5684                 (let ((method (gnus-find-method-for-group
5685                                gnus-newsgroup-name)))
5686                   (when (and (eq (car method) 'nneething)
5687                              (vectorp header))
5688                     (let ((dir (nneething-get-file-name
5689                                 (mail-header-id header))))
5690                       (when (and (stringp dir)
5691                                  (file-directory-p dir))
5692                         (setq article 'nneething)
5693                         (gnus-group-enter-directory dir))))))))
5694
5695           (cond
5696            ;; Refuse to select canceled articles.
5697            ((and (numberp article)
5698                  gnus-summary-buffer
5699                  (get-buffer gnus-summary-buffer)
5700                  (gnus-buffer-exists-p gnus-summary-buffer)
5701                  (eq (cdr (save-excursion
5702                             (set-buffer gnus-summary-buffer)
5703                             (assq article gnus-newsgroup-reads)))
5704                      gnus-canceled-mark))
5705             nil)
5706            ;; We first check `gnus-original-article-buffer'.
5707            ((and (get-buffer gnus-original-article-buffer)
5708                  (numberp article)
5709                  (save-excursion
5710                    (set-buffer gnus-original-article-buffer)
5711                    (and (equal (car gnus-original-article) group)
5712                         (eq (cdr gnus-original-article) article))))
5713             (insert-buffer-substring gnus-original-article-buffer)
5714             'article)
5715            ;; Check the backlog.
5716            ((and gnus-keep-backlog
5717                  (gnus-backlog-request-article group article (current-buffer)))
5718             'article)
5719            ;; Check asynchronous pre-fetch.
5720            ((gnus-async-request-fetched-article group article (current-buffer))
5721             (gnus-async-prefetch-next group article gnus-summary-buffer)
5722             (when (and (numberp article) gnus-keep-backlog)
5723               (gnus-backlog-enter-article group article (current-buffer)))
5724             'article)
5725            ;; Check the cache.
5726            ((and gnus-use-cache
5727                  (numberp article)
5728                  (gnus-cache-request-article article group))
5729             'article)
5730            ;; Check the agent cache.
5731            ((gnus-agent-request-article article group)
5732             'article)
5733            ;; Get the article and put into the article buffer.
5734            ((or (stringp article)
5735                 (numberp article))
5736             (let ((gnus-override-method gnus-override-method)
5737                   (methods (and (stringp article)
5738                                 gnus-refer-article-method))
5739                   (backend (car (gnus-find-method-for-group
5740                                  gnus-newsgroup-name)))
5741                   result
5742                   (buffer-read-only nil))
5743               (if (or (not (listp methods))
5744                       (and (symbolp (car methods))
5745                            (assq (car methods) nnoo-definition-alist)))
5746                   (setq methods (list methods)))
5747               (when (and (null gnus-override-method)
5748                          methods)
5749                 (setq gnus-override-method (pop methods)))
5750               (while (not result)
5751                 (when (eq gnus-override-method 'current)
5752                   (setq gnus-override-method
5753                         (with-current-buffer gnus-summary-buffer
5754                           gnus-current-select-method)))
5755                 (erase-buffer)
5756                 (gnus-kill-all-overlays)
5757                 (let ((gnus-newsgroup-name group))
5758                   (gnus-check-group-server))
5759                 (cond
5760                  ((gnus-request-article article group (current-buffer))
5761                   (when (numberp article)
5762                     (gnus-async-prefetch-next group article
5763                                               gnus-summary-buffer)
5764                     (when gnus-keep-backlog
5765                       (gnus-backlog-enter-article
5766                        group article (current-buffer))))
5767                   (setq result 'article))
5768                  (methods
5769                   (setq gnus-override-method (pop methods)))
5770                  ((not (string-match "^400 "
5771                                      (nnheader-get-report backend)))
5772                   ;; If we get 400 server disconnect, reconnect and
5773                   ;; retry; otherwise, assume the article has expired.
5774                   (setq result 'done))))
5775               (and (eq result 'article) 'article)))
5776            ;; It was a pseudo.
5777            (t article)))
5778
5779       ;; Associate this article with the current summary buffer.
5780       (setq gnus-article-current-summary gnus-summary-buffer)
5781
5782       ;; Take the article from the original article buffer
5783       ;; and place it in the buffer it's supposed to be in.
5784       (when (and (get-buffer gnus-article-buffer)
5785                  (equal (buffer-name (current-buffer))
5786                         (buffer-name (get-buffer gnus-article-buffer))))
5787         (save-excursion
5788           (if (get-buffer gnus-original-article-buffer)
5789               (set-buffer gnus-original-article-buffer)
5790             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5791             (buffer-disable-undo)
5792             (setq major-mode 'gnus-original-article-mode)
5793             (setq buffer-read-only t))
5794           (let (buffer-read-only)
5795             (erase-buffer)
5796             (insert-buffer-substring gnus-article-buffer))
5797           (setq gnus-original-article (cons group article)))
5798
5799         ;; Decode charsets.
5800         (run-hooks 'gnus-article-decode-hook)
5801         ;; Mark article as decoded or not.
5802         (setq gnus-article-decoded-p gnus-article-decode-hook))
5803
5804       ;; Update sparse articles.
5805       (when (and do-update-line
5806                  (or (numberp article)
5807                      (stringp article)))
5808         (let ((buf (current-buffer)))
5809           (set-buffer gnus-summary-buffer)
5810           (gnus-summary-update-article do-update-line sparse-header)
5811           (gnus-summary-goto-subject do-update-line nil t)
5812           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5813                             (point))
5814           (set-buffer buf))))))
5815
5816 ;;;
5817 ;;; Article editing
5818 ;;;
5819
5820 (defcustom gnus-article-edit-mode-hook nil
5821   "Hook run in article edit mode buffers."
5822   :group 'gnus-article-various
5823   :type 'hook)
5824
5825 (defcustom gnus-article-edit-article-setup-function
5826   'gnus-article-mime-edit-article-setup
5827   "Function called to setup an editing article buffer."
5828   :group 'gnus-article-various
5829   :type 'function)
5830
5831 (defvar gnus-article-edit-done-function nil)
5832
5833 (defvar gnus-article-edit-mode-map nil)
5834 (defvar gnus-article-edit-mode nil)
5835
5836 ;; Should we be using derived.el for this?
5837 (unless gnus-article-edit-mode-map
5838   (setq gnus-article-edit-mode-map (make-keymap))
5839   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5840
5841   (gnus-define-keys gnus-article-edit-mode-map
5842     "\C-c?"    describe-mode
5843     "\C-c\C-c" gnus-article-edit-done
5844     "\C-c\C-k" gnus-article-edit-exit
5845     "\C-c\C-f\C-t" message-goto-to
5846     "\C-c\C-f\C-o" message-goto-from
5847     "\C-c\C-f\C-b" message-goto-bcc
5848     ;;"\C-c\C-f\C-w" message-goto-fcc
5849     "\C-c\C-f\C-c" message-goto-cc
5850     "\C-c\C-f\C-s" message-goto-subject
5851     "\C-c\C-f\C-r" message-goto-reply-to
5852     "\C-c\C-f\C-n" message-goto-newsgroups
5853     "\C-c\C-f\C-d" message-goto-distribution
5854     "\C-c\C-f\C-f" message-goto-followup-to
5855     "\C-c\C-f\C-m" message-goto-mail-followup-to
5856     "\C-c\C-f\C-k" message-goto-keywords
5857     "\C-c\C-f\C-u" message-goto-summary
5858     "\C-c\C-f\C-i" message-insert-or-toggle-importance
5859     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
5860     "\C-c\C-b" message-goto-body
5861     "\C-c\C-i" message-goto-signature
5862
5863     "\C-c\C-t" message-insert-to
5864     "\C-c\C-n" message-insert-newsgroups
5865     "\C-c\C-o" message-sort-headers
5866     "\C-c\C-e" message-elide-region
5867     "\C-c\C-v" message-delete-not-region
5868     "\C-c\C-z" message-kill-to-signature
5869     "\M-\r" message-newline-and-reformat
5870     "\C-c\C-a" mml-attach-file
5871     "\C-a" message-beginning-of-line
5872     "\t" message-tab
5873     "\M-;" comment-region)
5874
5875   (gnus-define-keys (gnus-article-edit-wash-map
5876                      "\C-c\C-w" gnus-article-edit-mode-map)
5877     "f" gnus-article-edit-full-stops))
5878
5879 (easy-menu-define
5880   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
5881   '("Field"
5882     ["Fetch To" message-insert-to t]
5883     ["Fetch Newsgroups" message-insert-newsgroups t]
5884     "----"
5885     ["To" message-goto-to t]
5886     ["From" message-goto-from t]
5887     ["Subject" message-goto-subject t]
5888     ["Cc" message-goto-cc t]
5889     ["Reply-To" message-goto-reply-to t]
5890     ["Summary" message-goto-summary t]
5891     ["Keywords" message-goto-keywords t]
5892     ["Newsgroups" message-goto-newsgroups t]
5893     ["Followup-To" message-goto-followup-to t]
5894     ["Mail-Followup-To" message-goto-mail-followup-to t]
5895     ["Distribution" message-goto-distribution t]
5896     ["Body" message-goto-body t]
5897     ["Signature" message-goto-signature t]))
5898
5899 (define-derived-mode gnus-article-edit-mode text-mode "Article Edit"
5900   "Major mode for editing articles.
5901 This is an extended text-mode.
5902
5903 \\{gnus-article-edit-mode-map}"
5904   (make-local-variable 'gnus-article-edit-done-function)
5905   (make-local-variable 'gnus-prev-winconf)
5906   (set (make-local-variable 'font-lock-defaults)
5907        '(message-font-lock-keywords t))
5908   (set (make-local-variable 'mail-header-separator) "")
5909   (set (make-local-variable 'gnus-article-edit-mode) t)
5910   (easy-menu-add message-mode-field-menu message-mode-map)
5911   (setq buffer-read-only nil)
5912   (buffer-enable-undo)
5913   (widen))
5914
5915 (defun gnus-article-edit (&optional force)
5916   "Edit the current article.
5917 This will have permanent effect only in mail groups.
5918 If FORCE is non-nil, allow editing of articles even in read-only
5919 groups."
5920   (interactive "P")
5921   (when (and (not force)
5922              (gnus-group-read-only-p))
5923     (error "The current newsgroup does not support article editing"))
5924   (gnus-article-date-original)
5925   (gnus-article-edit-article
5926    'ignore
5927    `(lambda (no-highlight)
5928       'ignore
5929       (gnus-summary-edit-article-done
5930        ,(or (mail-header-references gnus-current-headers) "")
5931        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
5932
5933 (defun gnus-article-edit-article (start-func exit-func)
5934   "Start editing the contents of the current article buffer."
5935   (let ((winconf (current-window-configuration)))
5936     (set-buffer gnus-article-buffer)
5937     (gnus-article-edit-mode)
5938     (funcall start-func)
5939     (set-buffer-modified-p nil)
5940     (gnus-configure-windows 'edit-article)
5941     (setq gnus-article-edit-done-function exit-func)
5942     (setq gnus-prev-winconf winconf)
5943     (when gnus-article-edit-article-setup-function
5944       (funcall gnus-article-edit-article-setup-function))
5945     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
5946
5947 (defun gnus-article-edit-done (&optional arg)
5948   "Update the article edits and exit."
5949   (interactive "P")
5950   (let ((func gnus-article-edit-done-function)
5951         (buf (current-buffer))
5952         (start (window-start))
5953         (p (point))
5954         (winconf gnus-prev-winconf))
5955     (remove-hook 'gnus-article-mode-hook
5956                  'gnus-article-mime-edit-article-unwind)
5957     (widen) ;; Widen it in case that users narrowed the buffer.
5958     (funcall func arg)
5959     (set-buffer buf)
5960     ;; The cache and backlog have to be flushed somewhat.
5961     (when gnus-keep-backlog
5962       (gnus-backlog-remove-article
5963        (car gnus-article-current) (cdr gnus-article-current)))
5964     ;; Flush original article as well.
5965     (save-excursion
5966       (when (get-buffer gnus-original-article-buffer)
5967         (set-buffer gnus-original-article-buffer)
5968         (setq gnus-original-article nil)))
5969     (when gnus-use-cache
5970       (gnus-cache-update-article
5971        (car gnus-article-current) (cdr gnus-article-current)))
5972     ;; We remove all text props from the article buffer.
5973     (kill-all-local-variables)
5974     (gnus-set-text-properties (point-min) (point-max) nil)
5975     (gnus-article-mode)
5976     (set-window-configuration winconf)
5977     (set-buffer buf)
5978     (set-window-start (get-buffer-window buf) start)
5979     (set-window-point (get-buffer-window buf) (point))))
5980
5981 (defun gnus-article-edit-exit ()
5982   "Exit the article editing without updating."
5983   (interactive)
5984   (when (or (not (buffer-modified-p))
5985             (yes-or-no-p "Article modified; kill anyway? "))
5986     (let ((curbuf (current-buffer))
5987           (p (point))
5988           (window-start (window-start)))
5989       (erase-buffer)
5990       (if (gnus-buffer-live-p gnus-original-article-buffer)
5991           (insert-buffer-substring gnus-original-article-buffer))
5992       (let ((winconf gnus-prev-winconf))
5993         (kill-all-local-variables)
5994         (gnus-article-mode)
5995         (set-window-configuration winconf)
5996         ;; Tippy-toe some to make sure that point remains where it was.
5997         (save-current-buffer
5998           (set-buffer curbuf)
5999           (set-window-start (get-buffer-window (current-buffer)) window-start)
6000           (goto-char p))))))
6001
6002 (defun gnus-article-edit-full-stops ()
6003   "Interactively repair spacing at end of sentences."
6004   (interactive)
6005   (save-excursion
6006     (goto-char (point-min))
6007     (search-forward-regexp "^$" nil t)
6008     (let ((case-fold-search nil))
6009       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6010
6011 ;;;
6012 ;;; Article editing with MIME-Edit
6013 ;;;
6014
6015 (defcustom gnus-article-mime-edit-article-setup-hook nil
6016   "Hook run after setting up a MIME editing article buffer."
6017   :group 'gnus-article-various
6018   :type 'hook)
6019
6020 (defun gnus-article-mime-edit-article-unwind ()
6021   "Unwind `gnus-article-buffer' if article editing was given up."
6022   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
6023   (when (featurep 'font-lock)
6024     (setq font-lock-defaults nil)
6025     (font-lock-mode -1))
6026   (when mime-edit-mode-flag
6027     (mime-edit-exit 'nomime 'no-error)
6028     (message "")))
6029
6030 (defun gnus-article-mime-edit-article-setup ()
6031   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
6032 after replacing with the original article."
6033   (setq gnus-show-mime t)
6034   (setq gnus-article-edit-done-function
6035         `(lambda (&rest args)
6036            (when mime-edit-mode-flag
6037              (let (mime-edit-insert-user-agent-field)
6038                (mime-edit-exit))
6039              (message ""))
6040            (goto-char (point-min))
6041            (let (case-fold-search)
6042              (when (re-search-forward
6043                     (format "^%s$" (regexp-quote mail-header-separator))
6044                     nil t)
6045                (replace-match "")))
6046            (apply ,gnus-article-edit-done-function args)
6047            (insert
6048             (prog1
6049                 (buffer-substring-no-properties (point-min) (point-max))
6050               (set-buffer (get-buffer-create gnus-original-article-buffer))
6051               (erase-buffer)))
6052            (setq gnus-current-headers (gnus-article-make-full-mail-header))
6053            (set-buffer gnus-article-buffer)
6054            (gnus-article-prepare-display)))
6055   (substitute-key-definition 'gnus-article-edit-done
6056                              'gnus-article-mime-edit-done
6057                              gnus-article-edit-mode-map)
6058   (substitute-key-definition 'gnus-article-edit-exit
6059                              'gnus-article-mime-edit-exit
6060                              gnus-article-edit-mode-map)
6061   (erase-buffer)
6062   (insert-buffer-substring gnus-original-article-buffer)
6063   (unless (member (with-current-buffer gnus-summary-buffer
6064                     gnus-newsgroup-name)
6065                   '("nndraft:delayed" "nndraft:drafts"))
6066     (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
6067       (fset 'mime-edit-decode-single-part-in-buffer
6068             (lambda (&rest args)
6069               (if (let ((content-type (car args)))
6070                     (and (eq 'message (mime-content-type-primary-type
6071                                        content-type))
6072                          (eq 'rfc822 (mime-content-type-subtype
6073                                       content-type))))
6074                   (setcar (cdr args) 'not-decode-text))
6075               (apply ofn args)))
6076       (unwind-protect
6077           (mime-edit-again)
6078         (fset 'mime-edit-decode-single-part-in-buffer ofn))))
6079   (when (featurep 'font-lock)
6080     (set (make-local-variable 'font-lock-defaults)
6081          '(message-font-lock-keywords t))
6082     (font-lock-set-defaults)
6083     (turn-on-font-lock))
6084   (set-buffer-modified-p nil)
6085   (delete-other-windows)
6086   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
6087   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
6088
6089 (defun gnus-article-mime-edit-done (&optional arg)
6090   "Update the article MIME edits and exit."
6091   (interactive "P")
6092   (when (featurep 'font-lock)
6093     (setq font-lock-defaults nil)
6094     (font-lock-mode -1))
6095   (let ((inhibit-read-only t))
6096     (gnus-article-edit-done arg)))
6097
6098 (defun gnus-article-mime-edit-exit ()
6099   "Exit the article MIME editing without updating."
6100   (interactive)
6101   (when (or (not (buffer-modified-p))
6102             (yes-or-no-p "Article modified; kill anyway? "))
6103     (when (featurep 'font-lock)
6104       (setq font-lock-defaults nil)
6105       (font-lock-mode -1))
6106     (when mime-edit-mode-flag
6107       (let (mime-edit-insert-user-agent-field)
6108         (mime-edit-exit))
6109       (message ""))
6110     (goto-char (point-min))
6111     (let (case-fold-search)
6112       (when (re-search-forward
6113              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
6114         (replace-match "")))
6115     (let ((winconf gnus-prev-winconf))
6116       (insert (prog1
6117                   (buffer-substring-no-properties (point-min) (point-max))
6118                 (set-buffer (get-buffer-create gnus-original-article-buffer))
6119                 (erase-buffer)))
6120       (setq gnus-current-headers (gnus-article-make-full-mail-header))
6121       (set-buffer gnus-article-buffer)
6122       (gnus-article-prepare-display)
6123       (set-window-configuration winconf))))
6124
6125 ;;;
6126 ;;; Article highlights
6127 ;;;
6128
6129 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6130
6131 ;;; Internal Variables:
6132
6133 (defcustom gnus-button-url-regexp
6134   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
6135       "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?[-a-z0-9_=!?#$@~%&*+\\/:;.,[:word:]]+[-a-z0-9_=#$@~%&*+\\/[:word:]]\\)"
6136     "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)\\)")
6137   "Regular expression that matches URLs."
6138   :group 'gnus-article-buttons
6139   :type 'regexp)
6140
6141 (defcustom gnus-button-valid-fqdn-regexp
6142   message-valid-fqdn-regexp
6143   "Regular expression that matches a valid FQDN."
6144   :group 'gnus-article-buttons
6145   :type 'regexp)
6146
6147 (defcustom gnus-button-man-handler 'manual-entry
6148   "Function to use for displaying man pages.
6149 The function must take at least one argument with a string naming the
6150 man page."
6151   :type '(choice (function-item :tag "Man" manual-entry)
6152                  (function-item :tag "Woman" woman)
6153                  (function :tag "Other"))
6154   :group 'gnus-article-buttons)
6155
6156 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6157   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6158 If the default site is too slow, try to find a CTAN mirror, see
6159 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6160 the variable `gnus-button-handle-ctan'."
6161   :group 'gnus-article-buttons
6162   :link '(custom-manual "(gnus)Group Parameters")
6163   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6164                  (const "http://tug.ctan.org/tex-archive/")
6165                  (const "http://www.dante.de/CTAN/")
6166                  (string :tag "Other")))
6167
6168 (defcustom gnus-button-ctan-handler 'browse-url
6169   "Function to use for displaying CTAN links.
6170 The function must take one argument, the string naming the URL."
6171   :type '(choice (function-item :tag "Browse Url" browse-url)
6172                  (function :tag "Other"))
6173   :group 'gnus-article-buttons)
6174
6175 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6176   "Bogus strings removed from CTAN URLs."
6177   :group 'gnus-article-buttons
6178   :type '(choice (const "^/?tex-archive/\\|/")
6179                  (regexp :tag "Other")))
6180
6181 (defcustom gnus-button-ctan-directory-regexp
6182   (concat
6183    "\\("; Cannot use `\(?: ... \)' (compatibility with Emacs 20).
6184    "biblio\\|digests\\|dviware\\|fonts\\|graphics\\|help\\|"
6185    "indexing\\|info\\|language\\|macros\\|support\\|systems\\|"
6186    "tds\\|tools\\|usergrps\\|web\\|nonfree\\|obsolete"
6187    "\\)")
6188   "Regular expression for ctan directories.
6189 It should match all directories in the top level of `gnus-ctan-url'."
6190   :group 'gnus-article-buttons
6191   :type 'regexp)
6192
6193 (defcustom gnus-button-mid-or-mail-regexp
6194   (concat "\\b\\(<?[a-z0-9][^<>\")!;:,{}\n\t ]*@"
6195           gnus-button-valid-fqdn-regexp
6196           ">?\\)\\b")
6197   "Regular expression that matches a message ID or a mail address."
6198   :group 'gnus-article-buttons
6199   :type 'regexp)
6200
6201 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6202   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6203 Strings like this can be either a message ID or a mail address.  If it is one
6204 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6205 message ID or a mail address, respectively.  If this variable is set to the
6206 symbol `ask', always query the user what do do.  If it is a function, this
6207 function will be called with the string as it's only argument.  The function
6208 must return `mid', `mail', `invalid' or `ask'."
6209   :group 'gnus-article-buttons
6210   :type '(choice (function-item :tag "Heuristic function"
6211                                 gnus-button-mid-or-mail-heuristic)
6212                  (const ask)
6213                  (const mid)
6214                  (const mail)))
6215
6216 (defcustom gnus-button-mid-or-mail-heuristic-alist
6217   '((-10.0 . ".+\\$.+@")
6218     (-10.0 . "#")
6219     (-10.0 . "\\*")
6220     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
6221     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
6222     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
6223     (-1.0  . "^[^a-z]+@")
6224     ;;
6225     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
6226     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
6227     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
6228     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
6229     ;;
6230     (-2.0  . "^[0-9]")
6231     (-1.0  . "^[0-9][0-9]")
6232     ;;
6233     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
6234     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6235     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
6236     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6237     ;;
6238     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
6239     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
6240     ;;       "[0-9]{8,}.*\@"
6241     (-3.0
6242      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
6243     ;; "[0-9]{12,}.*\@"
6244     ;; compensation for TDMA dated mail addresses:
6245     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
6246     ;;
6247     (-20.0 . "\\.fsf@") ;; Gnus
6248     (-20.0 . "^slrn")
6249     (-20.0 . "^Pine")
6250     (-20.0 . "_-_") ;; Subject change in thread
6251     ;;
6252     (-20.0 . "\\.ln@") ;; leafnode
6253     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
6254     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
6255     ;;
6256     ;; (5.0 . "") ;; $local_part_len <= 7
6257     (10.0  . "^[^0-9]+@")
6258     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
6259     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
6260     (3.0   . "\@stud")
6261     ;;
6262     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
6263     ;;
6264     (0.5   . "^[A-Z][a-z]")
6265     (0.5   . "^[A-Z][a-z][a-z]")
6266     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
6267     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
6268   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
6269
6270 A negative RATE indicates a message IDs, whereas a positive indicates a mail
6271 address.  The REGEXP is processed with `case-fold-search' set to nil."
6272   :group 'gnus-article-buttons
6273   :type '(repeat (cons (number :tag "Rate")
6274                        (regexp :tag "Regexp"))))
6275
6276 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
6277   "Guess whether MID-OR-MAIL is a message ID or a mail address.
6278 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
6279 address, `ask' if unsure and `invalid' if the string is invalid."
6280   (let ((case-fold-search nil)
6281         (list gnus-button-mid-or-mail-heuristic-alist)
6282         (result 0) rate regexp lpartlen elem)
6283     (setq lpartlen
6284           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
6285     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
6286     ;; Certain special cases...
6287     (when (string-match
6288            (concat
6289             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$" "\\|"
6290             "^[0-9]+\.[0-9]+\@compuserve")
6291            mid-or-mail)
6292       (gnus-message 8 "`%s' is a known mail address.")
6293       (setq result 'mail))
6294     (when (string-match "@.*@\\| " mid-or-mail)
6295       (gnus-message 8 "`%s' is invalid.")
6296       (setq result 'invalid))
6297     ;; Nothing more to do, if result is not a number here...
6298     (when (numberp result)
6299       (while list
6300         (setq elem (car list)
6301               rate (car elem)
6302               regexp (cdr elem)
6303               list (cdr list))
6304         (when (string-match regexp mid-or-mail)
6305           (setq result (+ result rate))
6306           (gnus-message
6307            9 "`%s' matched `%s', rate `%s', result `%s'."
6308            mid-or-mail regexp rate result)))
6309       (when (<= lpartlen 7)
6310         (setq result (+ result 5.0))
6311         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
6312                       mid-or-mail result))
6313       (when (>= lpartlen 12)
6314         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
6315         (cond
6316          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
6317           ;; Long local part should contain realname if e-mail address,
6318           ;; too many digits: message-id.
6319           ;; $score -= 5.0 + 0.1 * $local_part_len;
6320           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
6321           (setq result (+ result rate))
6322           (gnus-message
6323            9 "Many digits in `%s', rate `%s', result `%s'."
6324            mid-or-mail rate result))
6325          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
6326                         mid-or-mail)
6327           ;; Too few vowels [^aeiouy]{4,}.*\@
6328           (setq result (+ result -5.0))
6329           (gnus-message
6330            9 "Few vowels in `%s', rate `%s', result `%s'."
6331            mid-or-mail -5.0 result))
6332          (t
6333           (setq result (+ result 5.0))
6334           (gnus-message
6335            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
6336     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
6337     (cond
6338      ;; Maybe we should make this a customizable alist: (condition . 'result)
6339      ((< result -10.0) 'mid)
6340      ((> result  10.0) 'mail)
6341      (t 'ask))))
6342
6343 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
6344   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
6345          (url-mid (concat "news" ":" mid-or-mail))
6346          (url-mailto (concat "mailto" ":" mid-or-mail)))
6347     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
6348     (when (fboundp pref)
6349       (setq guessed
6350             ;; get rid of surrounding angles...
6351             (funcall pref
6352                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
6353       (if (or (eq 'mid guessed) (eq 'mail guessed))
6354           (setq pref guessed)
6355         (setq pref 'ask)))
6356     (if (eq pref 'ask)
6357         (save-window-excursion
6358           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
6359               (setq pref 'mail)
6360             (setq pref 'mid))))
6361     (cond ((eq pref 'mid)
6362            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
6363            (gnus-button-handle-news url-mid))
6364           ((eq pref 'mail)
6365            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
6366            (gnus-url-mailto url-mailto))
6367           (t (gnus-message 3 "Invalid string.")))))
6368
6369 (defun gnus-button-handle-custom (url)
6370   "Follow a Custom URL."
6371   (customize-apropos (gnus-url-unhex-string url)))
6372
6373 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
6374
6375 ;; FIXME: Maybe we should merge some of the functions that do quite similar
6376 ;; stuff?
6377
6378 (defun gnus-button-handle-describe-function (url)
6379   "Call `describe-function' when pushing the corresponding URL button."
6380   (describe-function
6381    (intern
6382     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6383
6384 (defun gnus-button-handle-describe-variable (url)
6385   "Call `describe-variable' when pushing the corresponding URL button."
6386   (describe-variable
6387    (intern
6388     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6389
6390 (defun gnus-button-handle-symbol (url)
6391 "Display help on variable or function.
6392 Calls `describe-variable' or `describe-function'."
6393   (let ((sym (intern url)))
6394     (cond
6395      ((fboundp sym) (describe-function sym))
6396      ((boundp sym) (describe-variable sym))
6397      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
6398
6399 (defun gnus-button-handle-describe-key (url)
6400   "Call `describe-key' when pushing the corresponding URL button."
6401   (let* ((key-string
6402           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
6403          (keys (ignore-errors (eval `(kbd ,key-string)))))
6404     (if keys
6405         (describe-key keys)
6406       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
6407
6408 (defun gnus-button-handle-apropos (url)
6409   "Call `apropos' when pushing the corresponding URL button."
6410   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6411
6412 (defun gnus-button-handle-apropos-command (url)
6413   "Call `apropos' when pushing the corresponding URL button."
6414   (apropos-command
6415    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6416
6417 (defun gnus-button-handle-apropos-variable (url)
6418   "Call `apropos' when pushing the corresponding URL button."
6419   (funcall
6420    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
6421    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6422
6423 (defun gnus-button-handle-apropos-documentation (url)
6424   "Call `apropos' when pushing the corresponding URL button."
6425   (funcall
6426    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
6427    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6428
6429 (defun gnus-button-handle-library (url)
6430   "Call `locate-library' when pushing the corresponding URL button."
6431   (gnus-message 9 "url=`%s'" url)
6432   (let* ((lib (locate-library url))
6433          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
6434     (if (not lib)
6435         (gnus-message 1 "Cannot locale library `%s'." url)
6436       (find-file-read-only file))))
6437
6438 (defun gnus-button-handle-ctan (url)
6439   "Call `browse-url' when pushing a CTAN URL button."
6440   (funcall
6441    gnus-button-ctan-handler
6442    (concat
6443     gnus-ctan-url
6444     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
6445
6446 (defcustom gnus-button-tex-level 5
6447   "*Integer that says how many TeX-related buttons Gnus will show.
6448 The higher the number, the more buttons will appear and the more false
6449 positives are possible.  Note that you can set this variable local to
6450 specifific groups.  Setting it higher in TeX groups is probably a good idea.
6451 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6452 how to set variables in specific groups."
6453   :group 'gnus-article-buttons
6454   :link '(custom-manual "(gnus)Group Parameters")
6455   :type 'integer)
6456
6457 (defcustom gnus-button-man-level 5
6458   "*Integer that says how many man-related buttons Gnus will show.
6459 The higher the number, the more buttons will appear and the more false
6460 positives are possible.  Note that you can set this variable local to
6461 specifific groups.  Setting it higher in Unix groups is probably a good idea.
6462 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6463 how to set variables in specific groups."
6464   :group 'gnus-article-buttons
6465   :link '(custom-manual "(gnus)Group Parameters")
6466   :type 'integer)
6467
6468 (defcustom gnus-button-emacs-level 5
6469   "*Integer that says how many emacs-related buttons Gnus will show.
6470 The higher the number, the more buttons will appear and the more false
6471 positives are possible.  Note that you can set this variable local to
6472 specifific groups.  Setting it higher in Emacs or Gnus related groups is
6473 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
6474 `gnus-parameters' on how to set variables in specific groups."
6475   :group 'gnus-article-buttons
6476   :link '(custom-manual "(gnus)Group Parameters")
6477   :type 'integer)
6478
6479 (defcustom gnus-button-message-level 5
6480   "*Integer that says how many buttons for news or mail messages will appear.
6481 The higher the number, the more buttons will appear and the more false
6482 positives are possible."
6483   ;; mail addresses, MIDs, URLs for news, ...
6484   :group 'gnus-article-buttons
6485   :type 'integer)
6486
6487 (defcustom gnus-button-browse-level 5
6488   "*Integer that says how many buttons for browsing will appear.
6489 The higher the number, the more buttons will appear and the more false
6490 positives are possible."
6491   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
6492   :group 'gnus-article-buttons
6493   :type 'integer)
6494
6495 (defcustom gnus-button-alist
6496   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
6497      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
6498     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
6499      gnus-button-handle-news 2)
6500     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
6501      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
6502     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
6503      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
6504     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
6505      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
6506     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
6507      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
6508     ("mailto:\\([-a-z.@_+0-9%=?]+\\)"
6509      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6510     ("\\bmailto:\\([^ \n\t]+\\)"
6511      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6512     ;; CTAN
6513     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
6514              gnus-button-ctan-directory-regexp
6515              "[^][>)!;:,'\n\t ]+\\)")
6516      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
6517     ((concat "\\btex-archive/\\("
6518              gnus-button-ctan-directory-regexp
6519              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
6520      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
6521     ((concat
6522       "\\b\\("
6523       gnus-button-ctan-directory-regexp
6524       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
6525      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
6526     ;; This is info
6527     ("\\binfo:\\(//\\)?\\([^'\">\n\t ]+\\)"
6528      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
6529     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
6530      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
6531     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
6532      ;; Info links like `C-h i d m CC Mode RET'
6533      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 2)
6534     ;; This is custom
6535     ("\\bcustom:\\(//\\)?\\([^'\">\n\t ]+\\)"
6536      0 (>= gnus-button-emacs-level 5) gnus-button-handle-custom 2)
6537     ("M-x[ \t\n]customize-[^ ]+[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
6538      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1)
6539     ;; Emacs help commands
6540     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6541      ;; regexp doesn't match arguments containing ` '.
6542      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
6543     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6544      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
6545     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6546      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
6547     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6548      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
6549     ;; The following entries may lead to many false positives so don't enable
6550     ;; them by default (use a high button level):
6551     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>"
6552      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6553     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
6554      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6555     ("`\\([a-z][a-z0-9]+-[a-z]+-[-a-z]+\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
6556      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
6557     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
6558      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
6559     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
6560      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
6561     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6562      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
6563     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6564      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
6565     ("`\\(\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
6566      ;; Unlike the other regexps we really have to require quoting
6567      ;; here to determine where it ends.
6568      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
6569     ;; This is how URLs _should_ be embedded in text (RFC 1738)...
6570     ("<URL: *\\([^<>]*\\)>"
6571      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6572     ;; Raw URLs.
6573     (gnus-button-url-regexp
6574      0 (>= gnus-button-browse-level 0) browse-url 0)
6575     ;; man pages
6576     ("\\b\\([a-z][a-z]+\\)([1-9])\\W"
6577      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
6578      gnus-button-handle-man 1)
6579     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
6580     ("\\b\\([a-z][-_.a-z0-9]+\\)([1-9])\\W"
6581      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
6582      gnus-button-handle-man 1)
6583     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
6584     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
6585     ("\\b\\([a-z][-_.:a-z0-9]+\\)([1-9][X1a-z]*)\\W\\|\\b\\(X\\)([1-9])\\W"
6586      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
6587     ;; MID or mail: To avoid too many false positives we don't try to catch
6588     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
6589     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
6590     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
6591     ;; so that non-ambiguous entries (see above) match first.
6592     (gnus-button-mid-or-mail-regexp
6593      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
6594   "*Alist of regexps matching buttons in article bodies.
6595
6596 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
6597 REGEXP: is the string (case insensitive) matching text around the button (can
6598 also be lisp expression evaluating to a string),
6599 BUTTON: is the number of the regexp grouping actually matching the button,
6600 FORM: is a lisp expression which must eval to true for the button to
6601 be added,
6602 CALLBACK: is the function to call when the user push this button, and each
6603 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
6604
6605 CALLBACK can also be a variable, in that case the value of that
6606 variable it the real callback function."
6607   :group 'gnus-article-buttons
6608   :type '(repeat (list (choice regexp variable)
6609                        (integer :tag "Button")
6610                        (sexp :tag "Form")
6611                        (function :tag "Callback")
6612                        (repeat :tag "Par"
6613                                :inline t
6614                                (integer :tag "Regexp group")))))
6615
6616 (defcustom gnus-header-button-alist
6617   '(("^\\(References\\|Message-I[Dd]\\):" "<[^<>]+>"
6618      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
6619     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
6620      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
6621     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
6622      0 (>= gnus-button-message-level 0) gnus-button-mailto 0)
6623     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
6624      0 (>= gnus-button-browse-level 0) browse-url 0)
6625     ("^Subject:" gnus-button-url-regexp
6626      0 (>= gnus-button-browse-level 0) browse-url 0)
6627     ("^[^:]+:" gnus-button-url-regexp
6628      0 (>= gnus-button-browse-level 0) browse-url 0)
6629     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?]+\\)"
6630      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6631     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
6632      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
6633   "*Alist of headers and regexps to match buttons in article heads.
6634
6635 This alist is very similar to `gnus-button-alist', except that each
6636 alist has an additional HEADER element first in each entry:
6637
6638 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
6639
6640 HEADER is a regexp to match a header.  For a fuller explanation, see
6641 `gnus-button-alist'."
6642   :group 'gnus-article-buttons
6643   :group 'gnus-article-headers
6644   :type '(repeat (list (regexp :tag "Header")
6645                        regexp
6646                        (integer :tag "Button")
6647                        (sexp :tag "Form")
6648                        (function :tag "Callback")
6649                        (repeat :tag "Par"
6650                                :inline t
6651                                (integer :tag "Regexp group")))))
6652
6653 (defvar gnus-button-regexp nil)
6654 (defvar gnus-button-marker-list nil)
6655 ;; Regexp matching any of the regexps from `gnus-button-alist'.
6656
6657 (defvar gnus-button-last nil)
6658 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
6659
6660 ;;; Commands:
6661
6662 (defun gnus-article-push-button (event)
6663   "Check text under the mouse pointer for a callback function.
6664 If the text under the mouse pointer has a `gnus-callback' property,
6665 call it with the value of the `gnus-data' text property."
6666   (interactive "e")
6667   (set-buffer (window-buffer (posn-window (event-start event))))
6668   (let* ((pos (posn-point (event-start event)))
6669          (data (get-text-property pos 'gnus-data))
6670          (fun (get-text-property pos 'gnus-callback)))
6671     (goto-char pos)
6672     (when fun
6673       (funcall fun data))))
6674
6675 (defun gnus-article-press-button ()
6676   "Check text at point for a callback function.
6677 If the text at point has a `gnus-callback' property,
6678 call it with the value of the `gnus-data' text property."
6679   (interactive)
6680   (let ((data (get-text-property (point) 'gnus-data))
6681         (fun (get-text-property (point) 'gnus-callback)))
6682     (when fun
6683       (funcall fun data))))
6684
6685 (defun gnus-article-prev-button (n)
6686   "Move point to N buttons backward.
6687 If N is negative, move forward instead."
6688   (interactive "p")
6689   (gnus-article-next-button (- n)))
6690
6691 (defun gnus-article-next-button (n)
6692   "Move point to N buttons forward.
6693 If N is negative, move backward instead."
6694   (interactive "p")
6695   (let ((function (if (< n 0) 'previous-single-property-change
6696                     'next-single-property-change))
6697         (inhibit-point-motion-hooks t)
6698         (backward (< n 0))
6699         (limit (if (< n 0) (point-min) (point-max))))
6700     (setq n (abs n))
6701     (while (and (not (= limit (point)))
6702                 (> n 0))
6703       ;; Skip past the current button.
6704       (when (get-text-property (point) 'gnus-callback)
6705         (goto-char (funcall function (point) 'gnus-callback nil limit)))
6706       ;; Go to the next (or previous) button.
6707       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
6708       ;; Put point at the start of the button.
6709       (when (and backward (not (get-text-property (point) 'gnus-callback)))
6710         (goto-char (funcall function (point) 'gnus-callback nil limit)))
6711       ;; Skip past intangible buttons.
6712       (when (get-text-property (point) 'intangible)
6713         (incf n))
6714       (decf n))
6715     (unless (zerop n)
6716       (gnus-message 5 "No more buttons"))
6717     n))
6718
6719 (defun gnus-article-highlight (&optional force)
6720   "Highlight current article.
6721 This function calls `gnus-article-highlight-headers',
6722 `gnus-article-highlight-citation',
6723 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6724 do the highlighting.  See the documentation for those functions."
6725   (interactive (list 'force))
6726   (gnus-article-highlight-headers)
6727   (gnus-article-highlight-citation force)
6728   (gnus-article-highlight-signature)
6729   (gnus-article-add-buttons force)
6730   (gnus-article-add-buttons-to-head))
6731
6732 (defun gnus-article-highlight-some (&optional force)
6733   "Highlight current article.
6734 This function calls `gnus-article-highlight-headers',
6735 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6736 do the highlighting.  See the documentation for those functions."
6737   (interactive (list 'force))
6738   (gnus-article-highlight-headers)
6739   (gnus-article-highlight-signature)
6740   (gnus-article-add-buttons))
6741
6742 (defun gnus-article-highlight-headers ()
6743   "Highlight article headers as specified by `gnus-header-face-alist'."
6744   (interactive)
6745   (save-excursion
6746     (set-buffer gnus-article-buffer)
6747     (save-restriction
6748       (let ((alist gnus-header-face-alist)
6749             (buffer-read-only nil)
6750             (case-fold-search t)
6751             (inhibit-point-motion-hooks t)
6752             entry regexp header-face field-face from hpoints fpoints)
6753         (article-narrow-to-head)
6754         (while (setq entry (pop alist))
6755           (goto-char (point-min))
6756           (setq regexp (concat "^\\("
6757                                (if (string-equal "" (nth 0 entry))
6758                                    "[^\t ]"
6759                                  (nth 0 entry))
6760                                "\\)")
6761                 header-face (nth 1 entry)
6762                 field-face (nth 2 entry))
6763           (while (and (re-search-forward regexp nil t)
6764                       (not (eobp)))
6765             (beginning-of-line)
6766             (setq from (point))
6767             (unless (search-forward ":" nil t)
6768               (forward-char 1))
6769             (when (and header-face
6770                        (not (memq (point) hpoints)))
6771               (push (point) hpoints)
6772               (gnus-put-text-property from (point) 'face header-face))
6773             (when (and field-face
6774                        (not (memq (setq from (point)) fpoints)))
6775               (push from fpoints)
6776               (if (re-search-forward "^[^ \t]" nil t)
6777                   (forward-char -2)
6778                 (goto-char (point-max)))
6779               (gnus-put-text-property from (point) 'face field-face))))))))
6780
6781 (defun gnus-article-highlight-signature ()
6782   "Highlight the signature in an article.
6783 It does this by highlighting everything after
6784 `gnus-signature-separator' using `gnus-signature-face'."
6785   (interactive)
6786   (when gnus-signature-face
6787     (save-excursion
6788       (set-buffer gnus-article-buffer)
6789       (let ((buffer-read-only nil)
6790             (inhibit-point-motion-hooks t))
6791         (save-restriction
6792           (when (gnus-article-narrow-to-signature)
6793             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
6794                               'face gnus-signature-face)))))))
6795
6796 (defun gnus-article-buttonize-signature ()
6797   "Add button to the signature."
6798   (interactive)
6799   (save-excursion
6800     (set-buffer gnus-article-buffer)
6801     (let ((buffer-read-only nil)
6802           (inhibit-point-motion-hooks t))
6803       (when (gnus-article-search-signature)
6804         (gnus-article-add-button (match-beginning 0) (match-end 0)
6805                                  'gnus-signature-toggle
6806                                  (set-marker (make-marker)
6807                                              (1+ (match-end 0))))))))
6808
6809 (defun gnus-button-in-region-p (b e prop)
6810   "Say whether PROP exists in the region."
6811   (text-property-not-all b e prop nil))
6812
6813 (defun gnus-article-add-buttons (&optional force)
6814   "Find external references in the article and make buttons of them.
6815 \"External references\" are things like Message-IDs and URLs, as
6816 specified by `gnus-button-alist'."
6817   (interactive (list 'force))
6818   (save-excursion
6819     (set-buffer gnus-article-buffer)
6820     (let ((buffer-read-only nil)
6821           (inhibit-point-motion-hooks t)
6822           (case-fold-search t)
6823           (alist gnus-button-alist)
6824           beg entry regexp)
6825       ;; Remove all old markers.
6826       (let (marker entry new-list)
6827         (while (setq marker (pop gnus-button-marker-list))
6828           (if (or (< marker (point-min)) (>= marker (point-max)))
6829               (push marker new-list)
6830             (goto-char marker)
6831             (when (setq entry (gnus-button-entry))
6832               (put-text-property (match-beginning (nth 1 entry))
6833                                  (match-end (nth 1 entry))
6834                                  'gnus-callback nil))
6835             (set-marker marker nil)))
6836         (setq gnus-button-marker-list new-list))
6837       ;; We skip the headers.
6838       (article-goto-body)
6839       (setq beg (point))
6840       (while (setq entry (pop alist))
6841         (setq regexp (eval (car entry)))
6842         (goto-char beg)
6843         (while (re-search-forward regexp nil t)
6844           (let* ((start (and entry (match-beginning (nth 1 entry))))
6845                  (end (and entry (match-end (nth 1 entry))))
6846                  (from (match-beginning 0)))
6847             (when (and (or (eq t (nth 2 entry))
6848                            (eval (nth 2 entry)))
6849                        (not (gnus-button-in-region-p
6850                              start end 'gnus-callback)))
6851               ;; That optional form returned non-nil, so we add the
6852               ;; button.
6853               (gnus-article-add-button
6854                start end 'gnus-button-push
6855                (car (push (set-marker (make-marker) from)
6856                           gnus-button-marker-list))))))))))
6857
6858 ;; Add buttons to the head of an article.
6859 (defun gnus-article-add-buttons-to-head ()
6860   "Add buttons to the head of the article."
6861   (interactive)
6862   (save-excursion
6863     (set-buffer gnus-article-buffer)
6864     (save-restriction
6865       (let ((buffer-read-only nil)
6866             (inhibit-point-motion-hooks t)
6867             (case-fold-search t)
6868             (alist gnus-header-button-alist)
6869             entry beg end)
6870         (article-narrow-to-head)
6871         (while alist
6872           ;; Each alist entry.
6873           (setq entry (car alist)
6874                 alist (cdr alist))
6875           (goto-char (point-min))
6876           (while (re-search-forward (car entry) nil t)
6877             ;; Each header matching the entry.
6878             (setq beg (match-beginning 0))
6879             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
6880                                (match-beginning 0))
6881                           (point-max)))
6882             (goto-char beg)
6883             (while (re-search-forward (eval (nth 1 entry)) end t)
6884               ;; Each match within a header.
6885               (let* ((entry (cdr entry))
6886                      (start (match-beginning (nth 1 entry)))
6887                      (end (match-end (nth 1 entry)))
6888                      (form (nth 2 entry)))
6889                 (goto-char (match-end 0))
6890                 (when (eval form)
6891                   (gnus-article-add-button
6892                    start end (nth 3 entry)
6893                    (buffer-substring (match-beginning (nth 4 entry))
6894                                      (match-end (nth 4 entry)))))))
6895             (goto-char end)))))))
6896
6897 ;;; External functions:
6898
6899 (defun gnus-article-add-button (from to fun &optional data)
6900   "Create a button between FROM and TO with callback FUN and data DATA."
6901   (when gnus-article-button-face
6902     (gnus-overlay-put (gnus-make-overlay from to)
6903                       'face gnus-article-button-face))
6904   (gnus-add-text-properties
6905    from to
6906    (nconc (and gnus-article-mouse-face
6907                (list gnus-mouse-face-prop gnus-article-mouse-face))
6908           (list 'gnus-callback fun)
6909           (and data (list 'gnus-data data))))
6910   (widget-convert-button 'link from to :action 'gnus-widget-press-button
6911                          ;; Quote `:button-keymap' for Mule 2.3
6912                          ;; but it won't work.
6913                          ':button-keymap gnus-widget-button-keymap))
6914
6915 ;;; Internal functions:
6916
6917 (defun gnus-article-set-globals ()
6918   (save-excursion
6919     (set-buffer gnus-summary-buffer)
6920     (gnus-set-global-variables)))
6921
6922 (defun gnus-signature-toggle (end)
6923   (save-excursion
6924     (set-buffer gnus-article-buffer)
6925     (let ((buffer-read-only nil)
6926           (inhibit-point-motion-hooks t)
6927           (limit (next-single-property-change end 'mime-view-entity
6928                                               nil (point-max))))
6929       (if (text-property-any end limit 'article-type 'signature)
6930           (progn
6931             (gnus-delete-wash-type 'signature)
6932             (gnus-remove-text-properties-when
6933              'article-type 'signature end limit
6934              (cons 'article-type (cons 'signature
6935                                        gnus-hidden-properties))))
6936         (gnus-add-wash-type 'signature)
6937         (gnus-add-text-properties-when
6938          'article-type nil end limit
6939          (cons 'article-type (cons 'signature
6940                                    gnus-hidden-properties)))))
6941     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
6942       (gnus-set-mode-line 'article))))
6943
6944 (defun gnus-button-entry ()
6945   ;; Return the first entry in `gnus-button-alist' matching this place.
6946   (let ((alist gnus-button-alist)
6947         (entry nil))
6948     (while alist
6949       (setq entry (pop alist))
6950       (if (looking-at (eval (car entry)))
6951           (setq alist nil)
6952         (setq entry nil)))
6953     entry))
6954
6955 (defun gnus-button-push (marker)
6956   ;; Push button starting at MARKER.
6957   (save-excursion
6958     (goto-char marker)
6959     (let* ((entry (gnus-button-entry))
6960            (inhibit-point-motion-hooks t)
6961            (fun (nth 3 entry))
6962            (args (mapcar (lambda (group)
6963                            (let ((string (match-string group)))
6964                              (gnus-set-text-properties
6965                               0 (length string) nil string)
6966                              string))
6967                          (nthcdr 4 entry))))
6968       (cond
6969        ((fboundp fun)
6970         (apply fun args))
6971        ((and (boundp fun)
6972              (fboundp (symbol-value fun)))
6973         (apply (symbol-value fun) args))
6974        (t
6975         (gnus-message 1 "You must define `%S' to use this button"
6976                       (cons fun args)))))))
6977
6978 (defun gnus-parse-news-url (url)
6979   (let (scheme server group message-id articles)
6980     (with-temp-buffer
6981       (insert url)
6982       (goto-char (point-min))
6983       (when (looking-at "\\([A-Za-z]+\\):")
6984         (setq scheme (match-string 1))
6985         (goto-char (match-end 0)))
6986       (when (looking-at "//\\([^/]+\\)/")
6987         (setq server (match-string 1))
6988         (goto-char (match-end 0)))
6989
6990       (cond
6991        ((looking-at "\\(.*@.*\\)")
6992         (setq message-id (match-string 1)))
6993        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
6994         (setq group (match-string 1)
6995               articles (split-string (match-string 2) "-")))
6996        ((looking-at "\\([^/]+\\)/?")
6997         (setq group (match-string 1)))
6998        (t
6999         (error "Unknown news URL syntax"))))
7000     (list scheme server group message-id articles)))
7001
7002 (defun gnus-button-handle-news (url)
7003   "Fetch a news URL."
7004   (destructuring-bind (scheme server group message-id articles)
7005       (gnus-parse-news-url url)
7006     (cond
7007      (message-id
7008       (save-excursion
7009         (set-buffer gnus-summary-buffer)
7010         (if server
7011             (let ((gnus-refer-article-method (list (list 'nntp server))))
7012               (gnus-summary-refer-article message-id))
7013           (gnus-summary-refer-article message-id))))
7014      (group
7015       (gnus-button-fetch-group url)))))
7016
7017 (defun gnus-button-handle-man (url)
7018   "Fetch a man page."
7019   (funcall gnus-button-man-handler url))
7020
7021 (defun gnus-button-handle-info-url (url)
7022   "Fetch an info URL."
7023   (cond
7024    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7025     (gnus-info-find-node
7026      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7027                      "Gnus")
7028              ")" (gnus-url-unhex-string (match-string 2 url)))))
7029    ((string-match "([^)\"]+)[^\"]+" url)
7030     (setq url
7031           (gnus-replace-in-string
7032            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7033     (gnus-info-find-node url))
7034    (t (error "Can't parse %s" url))))
7035
7036 (defun gnus-button-handle-info-keystrokes (url)
7037   "Call `info' when pushing the corresponding URL button."
7038   ;; For links like `C-h i d m gnus RET', `C-h i d m CC Mode RET'.
7039   (info)
7040   (Info-directory)
7041   (Info-menu url))
7042
7043 (defun gnus-button-message-id (message-id)
7044   "Fetch MESSAGE-ID."
7045   (save-excursion
7046     (set-buffer gnus-summary-buffer)
7047     (gnus-summary-refer-article message-id)))
7048
7049 (defun gnus-button-fetch-group (address)
7050   "Fetch GROUP specified by ADDRESS."
7051   (if (not (string-match "[:/]" address))
7052       ;; This is just a simple group url.
7053       (gnus-group-read-ephemeral-group address gnus-select-method)
7054     (if (not
7055          (string-match
7056           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7057           address))
7058         (error "Can't parse %s" address)
7059       (gnus-group-read-ephemeral-group
7060        (match-string 4 address)
7061        `(nntp ,(match-string 1 address)
7062               (nntp-address ,(match-string 1 address))
7063               (nntp-port-number ,(if (match-end 3)
7064                                      (match-string 3 address)
7065                                    "nntp")))
7066        nil nil nil
7067        (and (match-end 6) (list (string-to-int (match-string 6 address))))))))
7068
7069 (defun gnus-url-parse-query-string (query &optional downcase)
7070   (let (retval pairs cur key val)
7071     (setq pairs (split-string query "&"))
7072     (while pairs
7073       (setq cur (car pairs)
7074             pairs (cdr pairs))
7075       (if (not (string-match "=" cur))
7076           nil                           ; Grace
7077         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7078               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7079         (if downcase
7080             (setq key (downcase key)))
7081         (setq cur (assoc key retval))
7082         (if cur
7083             (setcdr cur (cons val (cdr cur)))
7084           (setq retval (cons (list key val) retval)))))
7085     retval))
7086
7087 (defun gnus-url-mailto (url)
7088   ;; Send mail to someone
7089   (when (string-match "mailto:/*\\(.*\\)" url)
7090     (setq url (substring url (match-beginning 1) nil)))
7091   (let (to args subject func)
7092     (if (string-match (regexp-quote "?") url)
7093         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
7094               args (gnus-url-parse-query-string
7095                     (substring url (match-end 0) nil) t))
7096       (setq to (gnus-url-unhex-string url)))
7097     (setq args (cons (list "to" to) args)
7098           subject (cdr-safe (assoc "subject" args)))
7099     (gnus-msg-mail)
7100     (while args
7101       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
7102       (if (fboundp func)
7103           (funcall func)
7104         (message-position-on-field (caar args)))
7105       (insert (mapconcat 'identity (cdar args) ", "))
7106       (setq args (cdr args)))
7107     (if subject
7108         (message-goto-body)
7109       (message-goto-subject))))
7110
7111 (defun gnus-button-embedded-url (address)
7112   "Activate ADDRESS with `browse-url'."
7113   (browse-url (gnus-strip-whitespace address)))
7114
7115 ;;; Next/prev buttons in the article buffer.
7116
7117 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
7118 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
7119
7120 (defvar gnus-prev-page-map
7121   (let ((map (make-sparse-keymap)))
7122     (unless (>= emacs-major-version 21)
7123       ;; XEmacs doesn't care.
7124       (set-keymap-parent map gnus-article-mode-map))
7125     (define-key map gnus-mouse-2 'gnus-button-prev-page)
7126     (define-key map "\r" 'gnus-button-prev-page)
7127     map))
7128
7129 (defun gnus-insert-prev-page-button ()
7130   (let ((b (point))
7131         (buffer-read-only nil)
7132         (situation (get-text-property (point-min) 'mime-view-situation)))
7133     (gnus-eval-format
7134      gnus-prev-page-line-format nil
7135      `(,@(gnus-local-map-property gnus-prev-page-map)
7136          gnus-prev t
7137          gnus-callback gnus-article-button-prev-page
7138          article-type annotation
7139          mime-view-situation ,situation))
7140     (widget-convert-button
7141      'link b (if (bolp)
7142                  ;; Exclude a newline.
7143                  (1- (point))
7144                (point))
7145      :action 'gnus-button-prev-page
7146      :button-keymap gnus-prev-page-map)))
7147
7148 (defvar gnus-next-page-map
7149   (let ((map (make-sparse-keymap)))
7150     (unless (>= emacs-major-version 21)
7151       ;; XEmacs doesn't care.
7152       (set-keymap-parent map gnus-article-mode-map))
7153     (define-key map gnus-mouse-2 'gnus-button-next-page)
7154     (define-key map "\r" 'gnus-button-next-page)
7155     map))
7156
7157 (defun gnus-button-next-page (&optional args more-args)
7158   "Go to the next page."
7159   (interactive)
7160   (let ((win (selected-window)))
7161     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7162     (gnus-article-next-page)
7163     (select-window win)))
7164
7165 (defun gnus-button-prev-page (&optional args more-args)
7166   "Go to the prev page."
7167   (interactive)
7168   (let ((win (selected-window)))
7169     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7170     (gnus-article-prev-page)
7171     (select-window win)))
7172
7173 (defun gnus-insert-next-page-button ()
7174   (let ((b (point))
7175         (buffer-read-only nil)
7176         (situation (get-text-property (point-min) 'mime-view-situation)))
7177     (gnus-eval-format gnus-next-page-line-format nil
7178                       `(,@(gnus-local-map-property gnus-next-page-map)
7179                           gnus-next t
7180                           gnus-callback gnus-article-button-next-page
7181                           article-type annotation
7182                           mime-view-situation ,situation))
7183     (widget-convert-button
7184      'link b (if (bolp)
7185                  ;; Exclude a newline.
7186                  (1- (point))
7187                (point))
7188      :action 'gnus-button-next-page
7189      :button-keymap gnus-next-page-map)))
7190
7191 (defun gnus-article-button-next-page (arg)
7192   "Go to the next page."
7193   (interactive "P")
7194   (let ((win (selected-window)))
7195     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7196     (gnus-article-next-page)
7197     (select-window win)))
7198
7199 (defun gnus-article-button-prev-page (arg)
7200   "Go to the prev page."
7201   (interactive "P")
7202   (let ((win (selected-window)))
7203     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7204     (gnus-article-prev-page)
7205     (select-window win)))
7206
7207 (defvar gnus-decode-header-methods
7208   '(mail-decode-encoded-word-region)
7209   "List of methods used to decode headers.
7210
7211 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
7212 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
7213 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
7214 whose names match REGEXP.
7215
7216 For example:
7217 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
7218  mail-decode-encoded-word-region
7219  (\"chinese\" . rfc1843-decode-region))
7220 ")
7221
7222 (defvar gnus-decode-header-methods-cache nil)
7223
7224 (defun gnus-multi-decode-header (start end)
7225   "Apply the functions from `gnus-encoded-word-methods' that match."
7226   (unless (and gnus-decode-header-methods-cache
7227                (eq gnus-newsgroup-name
7228                    (car gnus-decode-header-methods-cache)))
7229     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
7230     (mapcar (lambda (x)
7231               (if (symbolp x)
7232                   (nconc gnus-decode-header-methods-cache (list x))
7233                 (if (and gnus-newsgroup-name
7234                          (string-match (car x) gnus-newsgroup-name))
7235                     (nconc gnus-decode-header-methods-cache
7236                            (list (cdr x))))))
7237             gnus-decode-header-methods))
7238   (let ((xlist gnus-decode-header-methods-cache))
7239     (pop xlist)
7240     (save-restriction
7241       (narrow-to-region start end)
7242       (while xlist
7243         (funcall (pop xlist) (point-min) (point-max))))))
7244
7245 ;;;
7246 ;;; Treatment top-level handling.
7247 ;;;
7248
7249 (defun gnus-treat-article (condition &optional part-number total-parts type)
7250   (let ((length (- (point-max) (point-min)))
7251         (alist gnus-treatment-function-alist)
7252         (article-goto-body-goes-to-point-min-p t)
7253         (treated-type
7254          (or (not type)
7255              (catch 'found
7256                (let ((list gnus-article-treat-types))
7257                  (while list
7258                    (when (string-match (pop list) type)
7259                      (throw 'found t)))))))
7260         (highlightp (gnus-visual-p 'article-highlight 'highlight))
7261         (entity (static-unless (featurep 'xemacs)
7262                   (when (eq 'head condition)
7263                     (get-text-property (point-min) 'mime-view-entity))))
7264         val elem buttonized)
7265     (gnus-run-hooks 'gnus-part-display-hook)
7266     (unless gnus-inhibit-treatment
7267       (dolist (elem alist)
7268         (setq val
7269               (save-excursion
7270                 (when (gnus-buffer-live-p gnus-summary-buffer)
7271                   (set-buffer gnus-summary-buffer))
7272                 (symbol-value (car elem))))
7273         (when (and (or (consp val)
7274                        treated-type)
7275                    (gnus-treat-predicate val)
7276                    (or (not (get (car elem) 'highlight))
7277                        highlightp))
7278           (when (and (not buttonized)
7279                      (memq (car elem)
7280                            '(gnus-treat-hide-signature
7281                              gnus-treat-highlight-signature)))
7282             (gnus-article-buttonize-signature)
7283             (setq buttonized t))
7284           (save-restriction
7285             (funcall (cadr elem)))))
7286       ;; FSF Emacsen does not inherit the existing text properties
7287       ;; in the new text, so we should do it for `mime-view-entity'.
7288       (static-unless (featurep 'xemacs)
7289         (when entity
7290           (put-text-property (point-min) (point-max)
7291                              'mime-view-entity entity))))))
7292
7293 ;; Dynamic variables.
7294 (eval-when-compile
7295   (defvar part-number)
7296   (defvar total-parts)
7297   (defvar type)
7298   (defvar condition)
7299   (defvar length))
7300
7301 (defun gnus-treat-predicate (val)
7302   (cond
7303    ((null val)
7304     nil)
7305    ((and (listp val)
7306          (stringp (car val)))
7307     (apply 'gnus-or (mapcar `(lambda (s)
7308                                (string-match s ,(or gnus-newsgroup-name "")))
7309                             val)))
7310    ((listp val)
7311     (let ((pred (pop val)))
7312       (cond
7313        ((eq pred 'or)
7314         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
7315        ((eq pred 'and)
7316         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
7317        ((eq pred 'not)
7318         (not (gnus-treat-predicate (car val))))
7319        ((eq pred 'typep)
7320         (equal (car val) type))
7321        (t
7322         (error "%S is not a valid predicate" pred)))))
7323    ((eq val 'mime)
7324     gnus-show-mime)
7325    (condition
7326     (eq condition val))
7327    ((eq val t)
7328     t)
7329    ((eq val 'head)
7330     nil)
7331    ((eq val 'last)
7332     (eq part-number total-parts))
7333    ((numberp val)
7334     (< length val))
7335    (t
7336     (error "%S is not a valid value" val))))
7337
7338 (defun gnus-article-encrypt-body (protocol &optional n)
7339   "Encrypt the article body."
7340   (interactive
7341    (list
7342     (or gnus-article-encrypt-protocol
7343         (completing-read "Encrypt protocol: "
7344                          gnus-article-encrypt-protocol-alist
7345                          nil t))
7346     current-prefix-arg))
7347   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
7348     (unless func
7349       (error (format "Can't find the encrypt protocol %s" protocol)))
7350     (if (member gnus-newsgroup-name '("nndraft:delayed"
7351                                       "nndraft:drafts"
7352                                       "nndraft:queue"))
7353         (error "Can't encrypt the article in group %s"
7354                gnus-newsgroup-name))
7355     (gnus-summary-iterate n
7356       (save-excursion
7357         (set-buffer gnus-summary-buffer)
7358         (let ((mail-parse-charset gnus-newsgroup-charset)
7359               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
7360               (summary-buffer gnus-summary-buffer)
7361               references point)
7362           (gnus-set-global-variables)
7363           (when (gnus-group-read-only-p)
7364             (error "The current newsgroup does not support article encrypt"))
7365           (gnus-summary-show-article t)
7366           (setq references
7367                 (or (mail-header-references gnus-current-headers) ""))
7368           (set-buffer gnus-article-buffer)
7369           (let* ((buffer-read-only nil)
7370                  (headers
7371                   (mapcar (lambda (field)
7372                             (and (save-restriction
7373                                    (message-narrow-to-head)
7374                                    (goto-char (point-min))
7375                                    (search-forward field nil t))
7376                                  (prog2
7377                                      (message-narrow-to-field)
7378                                      (buffer-string)
7379                                    (delete-region (point-min) (point-max))
7380                                    (widen))))
7381                           '("Content-Type:" "Content-Transfer-Encoding:"
7382                             "Content-Disposition:"))))
7383             (message-narrow-to-head)
7384             (message-remove-header "MIME-Version")
7385             (goto-char (point-max))
7386             (setq point (point))
7387             (insert (apply 'concat headers))
7388             (widen)
7389             (narrow-to-region point (point-max))
7390             (let ((message-options message-options))
7391               (message-options-set 'message-sender user-mail-address)
7392               (message-options-set 'message-recipients user-mail-address)
7393               (message-options-set 'message-sign-encrypt 'not)
7394               (funcall func))
7395             (goto-char (point-min))
7396             (insert "MIME-Version: 1.0\n")
7397             (widen)
7398             (gnus-summary-edit-article-done
7399              references nil summary-buffer t))
7400           (when gnus-keep-backlog
7401             (gnus-backlog-remove-article
7402              (car gnus-article-current) (cdr gnus-article-current)))
7403           (save-excursion
7404             (when (get-buffer gnus-original-article-buffer)
7405               (set-buffer gnus-original-article-buffer)
7406               (setq gnus-original-article nil)))
7407           (when gnus-use-cache
7408             (gnus-cache-update-article
7409              (car gnus-article-current) (cdr gnus-article-current))))))))
7410
7411 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
7412   "The following specs can be used:
7413 %t  The security MIME type
7414 %i  Additional info
7415 %d  Details
7416 %D  Details if button is pressed")
7417
7418 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
7419   "The following specs can be used:
7420 %t  The security MIME type
7421 %i  Additional info
7422 %d  Details
7423 %D  Details if button is pressed")
7424
7425 (defvar gnus-mime-security-button-line-format-alist
7426   '((?t gnus-tmp-type ?s)
7427     (?i gnus-tmp-info ?s)
7428     (?d gnus-tmp-details ?s)
7429     (?D gnus-tmp-pressed-details ?s)))
7430
7431 (defvar gnus-mime-security-button-map
7432   (let ((map (make-sparse-keymap)))
7433     (unless (>= (string-to-number emacs-version) 21)
7434       (set-keymap-parent map gnus-article-mode-map))
7435     (define-key map gnus-mouse-2 'gnus-article-push-button)
7436     (define-key map "\r" 'gnus-article-press-button)
7437     map))
7438
7439 (defvar gnus-mime-security-details-buffer nil)
7440
7441 (defvar gnus-mime-security-button-pressed nil)
7442
7443 (defvar gnus-mime-security-show-details-inline t
7444   "If non-nil, show details in the article buffer.")
7445
7446 (defun gnus-mime-security-verify-or-decrypt (handle)
7447   (mm-remove-parts (cdr handle))
7448   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
7449         point buffer-read-only)
7450     (if region
7451         (goto-char (car region)))
7452     (save-restriction
7453       (narrow-to-region (point) (point))
7454       (with-current-buffer (mm-handle-multipart-original-buffer handle)
7455         (let* ((mm-verify-option 'known)
7456                (mm-decrypt-option 'known)
7457                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
7458           (unless (eq nparts (cdr handle))
7459             (mm-destroy-parts (cdr handle))
7460             (setcdr handle nparts))))
7461       (setq point (point))
7462       (gnus-mime-display-security handle)
7463       (goto-char (point-max)))
7464     (when region
7465       (delete-region (point) (cdr region))
7466       (set-marker (car region) nil)
7467       (set-marker (cdr region) nil))
7468     (goto-char point)))
7469
7470 (defun gnus-mime-security-show-details (handle)
7471   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
7472     (if (not details)
7473         (gnus-message 5 "No details.")
7474       (if gnus-mime-security-show-details-inline
7475           (let ((gnus-mime-security-button-pressed
7476                  (not (get-text-property (point) 'gnus-mime-details)))
7477                 (gnus-mime-security-button-line-format
7478                  (get-text-property (point) 'gnus-line-format))
7479                 buffer-read-only)
7480             (forward-char -1)
7481             (while (eq (get-text-property (point) 'gnus-line-format)
7482                        gnus-mime-security-button-line-format)
7483               (forward-char -1))
7484             (forward-char)
7485             (save-restriction
7486               (narrow-to-region (point) (point))
7487               (gnus-insert-mime-security-button handle))
7488             (delete-region (point)
7489                            (or (text-property-not-all
7490                                 (point) (point-max)
7491                                 'gnus-line-format
7492                                 gnus-mime-security-button-line-format)
7493                                (point-max))))
7494         ;; Not inlined.
7495         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
7496             (with-current-buffer gnus-mime-security-details-buffer
7497               (erase-buffer)
7498               t)
7499           (setq gnus-mime-security-details-buffer
7500                 (gnus-get-buffer-create "*MIME Security Details*")))
7501         (with-current-buffer gnus-mime-security-details-buffer
7502           (insert details)
7503           (goto-char (point-min)))
7504         (pop-to-buffer gnus-mime-security-details-buffer)))))
7505
7506 (defun gnus-mime-security-press-button (handle)
7507   (save-excursion
7508     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7509         (gnus-mime-security-show-details handle)
7510       (gnus-mime-security-verify-or-decrypt handle))))
7511
7512 (defun gnus-insert-mime-security-button (handle &optional displayed)
7513   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
7514          (gnus-tmp-type
7515           (concat
7516            (or (nth 2 (assoc protocol mm-verify-function-alist))
7517                (nth 2 (assoc protocol mm-decrypt-function-alist))
7518                "Unknown")
7519            (if (equal (car handle) "multipart/signed")
7520                " Signed" " Encrypted")
7521            " Part"))
7522          (gnus-tmp-info
7523           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7524               "Undecided"))
7525          (gnus-tmp-details
7526           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
7527          gnus-tmp-pressed-details
7528          b e)
7529     (setq gnus-tmp-details
7530           (if gnus-tmp-details
7531               (concat "\n" gnus-tmp-details)
7532             ""))
7533     (setq gnus-tmp-pressed-details
7534           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
7535     (unless (bolp)
7536       (insert "\n"))
7537     (setq b (point))
7538     (gnus-eval-format
7539      gnus-mime-security-button-line-format
7540      gnus-mime-security-button-line-format-alist
7541      `(,@(gnus-local-map-property gnus-mime-security-button-map)
7542          gnus-callback gnus-mime-security-press-button
7543          gnus-line-format ,gnus-mime-security-button-line-format
7544          gnus-mime-details ,gnus-mime-security-button-pressed
7545          article-type annotation
7546          gnus-data ,handle))
7547     (setq e (if (bolp)
7548                 ;; Exclude a newline.
7549                 (1- (point))
7550               (point)))
7551     (widget-convert-button
7552      'link b e
7553      :mime-handle handle
7554      :action 'gnus-widget-press-button
7555      :button-keymap gnus-mime-security-button-map
7556      :help-echo
7557      (lambda (widget/window &optional overlay pos)
7558        ;; Needed to properly clear the message due to a bug in
7559        ;; wid-edit (XEmacs only).
7560        (when (boundp 'help-echo-owns-message)
7561          (setq help-echo-owns-message t))
7562        (format
7563         "%S: show detail"
7564         (aref gnus-mouse-2 0))))))
7565
7566 (defun gnus-mime-display-security (handle)
7567   (save-restriction
7568     (narrow-to-region (point) (point))
7569     (unless (gnus-unbuttonized-mime-type-p (car handle))
7570       (gnus-insert-mime-security-button handle))
7571     (gnus-mime-display-mixed (cdr handle))
7572     (unless (bolp)
7573       (insert "\n"))
7574     (unless (gnus-unbuttonized-mime-type-p (car handle))
7575       (let ((gnus-mime-security-button-line-format
7576              gnus-mime-security-button-end-line-format))
7577         (gnus-insert-mime-security-button handle)))
7578     (mm-set-handle-multipart-parameter
7579      handle 'gnus-region
7580      (cons (set-marker (make-marker) (point-min))
7581            (set-marker (make-marker) (point-max))))))
7582
7583
7584 ;;; @ for mime-view
7585 ;;;
7586
7587 (defun gnus-article-header-presentation-method (entity situation)
7588   (mime-insert-header entity)
7589   (article-decode-group-name))
7590
7591 (set-alist 'mime-header-presentation-method-alist
7592            'gnus-original-article-mode
7593            #'gnus-article-header-presentation-method)
7594
7595 (defun gnus-mime-preview-quitting-method ()
7596   (mime-preview-kill-buffer)
7597   (delete-other-windows)
7598   (gnus-article-show-summary)
7599   (gnus-summary-select-article gnus-show-all-headers t))
7600
7601 (set-alist 'mime-preview-quitting-method-alist
7602            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
7603
7604 (set-alist 'mime-preview-following-method-alist
7605            'gnus-original-article-mode #'gnus-following-method)
7606
7607 (set-alist 'mime-preview-over-to-previous-method-alist
7608            'gnus-original-article-mode
7609            (lambda ()
7610              (if (> (point-min) 1)
7611                  (gnus-article-prev-page)
7612                (gnus-article-read-summary-keys
7613                 nil (gnus-character-to-event ?P)))))
7614
7615 (set-alist 'mime-preview-over-to-next-method-alist
7616            'gnus-original-article-mode'
7617            (lambda ()
7618              (if (< (point-max) (buffer-size))
7619                  (gnus-article-next-page)
7620                (gnus-article-read-summary-keys
7621                 nil (gnus-character-to-event ?N)))))
7622
7623
7624 ;;; @ end
7625 ;;;
7626
7627 (gnus-ems-redefine)
7628
7629 (provide 'gnus-art)
7630
7631 (run-hooks 'gnus-art-load-hook)
7632
7633 ;;; gnus-art.el ends here