Synch to Gnus 200305231439.
[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     (let ((face (message-fetch-field "face")))
2124       (when face
2125         (let ((png (gnus-convert-face-to-png face))
2126               image)
2127           (when png
2128             (setq image (gnus-create-image png 'png t))
2129             (gnus-article-goto-header "from")
2130             (when (bobp)
2131               (insert "From: [no `from' set]\n")
2132               (forward-char -17))
2133             (gnus-add-wash-type 'face)
2134             (gnus-add-image 'face image)
2135             (gnus-put-image image)))))))
2136
2137 (defun article-display-x-face (&optional force)
2138   "Look for an X-Face header and display it if present."
2139   (interactive (list 'force))
2140   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2141     (gnus-with-article-headers
2142       ;; Delete the old process, if any.
2143       (when (process-status "article-x-face")
2144         (delete-process "article-x-face"))
2145       (if (memq 'xface gnus-article-wash-types)
2146           ;; We have already displayed X-Faces, so we remove them
2147           ;; instead.
2148           (gnus-delete-images 'xface)
2149         ;; Display X-Faces.
2150         (let (x-faces from face)
2151           (save-excursion
2152             (when (and wash-face-p
2153                        (progn
2154                          (goto-char (point-min))
2155                          (not (re-search-forward
2156                                "^X-Face\\(-[0-9]+\\)?:[\t ]*" nil t)))
2157                        (gnus-buffer-live-p gnus-original-article-buffer))
2158               ;; If type `W f', use gnus-original-article-buffer,
2159               ;; otherwise use the current buffer because displaying
2160               ;; RFC822 parts calls this function too.
2161               (set-buffer gnus-original-article-buffer))
2162             (save-restriction
2163               (mail-narrow-to-head)
2164               (while (gnus-article-goto-header "X-Face")
2165                 (push (mail-header-field-value) x-faces))
2166               (setq from (message-fetch-field "from"))))
2167           ;; Sending multiple EOFs to xv doesn't work, so we only do a
2168           ;; single external face.
2169           (when (stringp gnus-article-x-face-command)
2170             (setq x-faces (list (car x-faces))))
2171           (while (and (setq face (pop x-faces))
2172                       gnus-article-x-face-command
2173                       (or force
2174                           ;; Check whether this face is censored.
2175                           (not gnus-article-x-face-too-ugly)
2176                           (and gnus-article-x-face-too-ugly from
2177                                (not (string-match gnus-article-x-face-too-ugly
2178                                                   from)))))
2179             ;; We display the face.
2180             (if (symbolp gnus-article-x-face-command)
2181                 ;; The command is a lisp function, so we call it.
2182                 (if (functionp gnus-article-x-face-command)
2183                     (funcall gnus-article-x-face-command face)
2184                   (error "%s is not a function" gnus-article-x-face-command))
2185               ;; The command is a string, so we interpret the command
2186               ;; as a, well, command, and fork it off.
2187               (let ((process-connection-type nil))
2188                 (process-kill-without-query
2189                  (start-process
2190                   "article-x-face" nil shell-file-name shell-command-switch
2191                   gnus-article-x-face-command))
2192                 (with-temp-buffer
2193                   (insert face)
2194                   (process-send-region "article-x-face"
2195                                        (point-min) (point-max)))
2196                 (process-send-eof "article-x-face")))))))))
2197
2198 (defun article-decode-mime-words ()
2199   "Decode all MIME-encoded words in the article."
2200   (interactive)
2201   (save-excursion
2202     (set-buffer gnus-article-buffer)
2203     (let ((inhibit-point-motion-hooks t)
2204           buffer-read-only
2205           (mail-parse-charset gnus-newsgroup-charset)
2206           (mail-parse-ignored-charsets
2207            (save-excursion (set-buffer gnus-summary-buffer)
2208                            gnus-newsgroup-ignored-charsets)))
2209       (mail-decode-encoded-word-region (point-min) (point-max)))))
2210
2211 (defun article-decode-charset (&optional prompt)
2212   "Decode charset-encoded text in the article.
2213 If PROMPT (the prefix), prompt for a coding system to use."
2214   (interactive "P")
2215   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2216         buffer-read-only
2217         (mail-parse-charset gnus-newsgroup-charset)
2218         (mail-parse-ignored-charsets
2219          (save-excursion (condition-case nil
2220                              (set-buffer gnus-summary-buffer)
2221                            (error))
2222                          gnus-newsgroup-ignored-charsets))
2223         ct cte ctl charset format)
2224     (save-excursion
2225       (save-restriction
2226         (article-narrow-to-head)
2227         (setq ct (message-fetch-field "Content-Type" t)
2228               cte (message-fetch-field "Content-Transfer-Encoding" t)
2229               ctl (and ct (ignore-errors
2230                             (mail-header-parse-content-type ct)))
2231               charset (cond
2232                        (prompt
2233                         (mm-read-coding-system "Charset to decode: "))
2234                        (ctl
2235                         (mail-content-type-get ctl 'charset)))
2236               format (and ctl (mail-content-type-get ctl 'format)))
2237         (when cte
2238           (setq cte (mail-header-strip cte)))
2239         (if (and ctl (not (string-match "/" (car ctl))))
2240             (setq ctl nil))
2241         (goto-char (point-max)))
2242       (forward-line 1)
2243       (save-restriction
2244         (narrow-to-region (point) (point-max))
2245         (when (and (eq mail-parse-charset 'gnus-decoded)
2246                    (eq (mm-body-7-or-8) '8bit))
2247           ;; The text code could have been decoded.
2248           (setq charset mail-parse-charset))
2249         (when (and (or (not ctl)
2250                        (equal (car ctl) "text/plain"))
2251                    (not format)) ;; article with format will decode later.
2252           (mm-decode-body
2253            charset (and cte (intern (downcase
2254                                      (gnus-strip-whitespace cte))))
2255            (car ctl)))))))
2256
2257 (defun article-decode-encoded-words ()
2258   "Remove encoded-word encoding from headers."
2259   (let (buffer-read-only)
2260     (let ((charset (save-excursion
2261                      (set-buffer gnus-summary-buffer)
2262                      default-mime-charset)))
2263       (mime-decode-header-in-buffer charset))))
2264
2265 (defun article-decode-group-name ()
2266   "Decode group names in `Newsgroups:'."
2267   (let ((inhibit-point-motion-hooks t)
2268         buffer-read-only
2269         (method (gnus-find-method-for-group gnus-newsgroup-name)))
2270     (when (and (or gnus-group-name-charset-method-alist
2271                    gnus-group-name-charset-group-alist)
2272                (gnus-buffer-live-p gnus-original-article-buffer))
2273       (save-restriction
2274         (article-narrow-to-head)
2275         (with-current-buffer gnus-original-article-buffer
2276           (goto-char (point-min)))
2277         (while (re-search-forward
2278                 "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2279           (replace-match (save-match-data
2280                            (gnus-decode-newsgroups
2281                             ;; XXX how to use data in article buffer?
2282                             (with-current-buffer gnus-original-article-buffer
2283                               (re-search-forward
2284                                "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2285                                nil t)
2286                               (match-string 1))
2287                             gnus-newsgroup-name method))
2288                          t t nil 1))
2289         (goto-char (point-min))
2290         (with-current-buffer gnus-original-article-buffer
2291           (goto-char (point-min)))
2292         (while (re-search-forward
2293                 "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2294           (replace-match (save-match-data
2295                            (gnus-decode-newsgroups
2296                             ;; XXX how to use data in article buffer?
2297                             (with-current-buffer gnus-original-article-buffer
2298                               (re-search-forward
2299                                "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2300                                nil t)
2301                               (match-string 1))
2302                             gnus-newsgroup-name method))
2303                          t t nil 1))))))
2304
2305 (autoload 'idna-to-unicode "idna")
2306
2307 (defun article-decode-idna-rhs ()
2308   "Decode IDNA strings in RHS in From:, To: and Cc: headers in current buffer."
2309   (when gnus-use-idna
2310     (save-restriction
2311       (let ((inhibit-point-motion-hooks t)
2312             buffer-read-only)
2313         (article-narrow-to-head)
2314         (goto-char (point-min))
2315         (while (re-search-forward "\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2316           (let (ace unicode)
2317             (when (save-match-data
2318                     (and (setq ace (match-string 1))
2319                          (save-excursion
2320                            (and (re-search-backward "^[^ \t]" nil t)
2321                                 (looking-at "From\\|To\\|Cc")))
2322                          (save-excursion (backward-char)
2323                                          (message-idna-inside-rhs-p))
2324                          (setq unicode (idna-to-unicode ace))))
2325               (unless (string= ace unicode)
2326                 (replace-match unicode nil nil nil 1)))))))))
2327
2328 (defun article-de-quoted-unreadable (&optional force read-charset)
2329   "Translate a quoted-printable-encoded article.
2330 If FORCE, decode the article whether it is marked as quoted-printable
2331 or not.
2332 If READ-CHARSET, ask for a coding system."
2333   (interactive (list 'force current-prefix-arg))
2334   (save-excursion
2335     (let ((buffer-read-only nil) type charset)
2336       (if (gnus-buffer-live-p gnus-original-article-buffer)
2337           (with-current-buffer gnus-original-article-buffer
2338             (setq type
2339                   (gnus-fetch-field "content-transfer-encoding"))
2340             (let* ((ct (gnus-fetch-field "content-type"))
2341                    (ctl (and ct
2342                              (ignore-errors
2343                                (mail-header-parse-content-type ct)))))
2344               (setq charset (and ctl
2345                                  (mail-content-type-get ctl 'charset)))
2346               (if (stringp charset)
2347                   (setq charset (intern (downcase charset)))))))
2348       (if read-charset
2349           (setq charset (mm-read-coding-system "Charset: " charset)))
2350       (unless charset
2351         (setq charset gnus-newsgroup-charset))
2352       (when (or force
2353                 (and type (let ((case-fold-search t))
2354                             (string-match "quoted-printable" type))))
2355         (article-goto-body)
2356         (quoted-printable-decode-region
2357          (point) (point-max) (mm-charset-to-coding-system charset))))))
2358
2359 (defun article-de-base64-unreadable (&optional force read-charset)
2360   "Translate a base64 article.
2361 If FORCE, decode the article whether it is marked as base64 not.
2362 If READ-CHARSET, ask for a coding system."
2363   (interactive (list 'force current-prefix-arg))
2364   (save-excursion
2365     (let ((buffer-read-only nil) type charset)
2366       (if (gnus-buffer-live-p gnus-original-article-buffer)
2367           (with-current-buffer gnus-original-article-buffer
2368             (setq type
2369                   (gnus-fetch-field "content-transfer-encoding"))
2370             (let* ((ct (gnus-fetch-field "content-type"))
2371                    (ctl (and ct
2372                              (ignore-errors
2373                                (mail-header-parse-content-type ct)))))
2374               (setq charset (and ctl
2375                                  (mail-content-type-get ctl 'charset)))
2376               (if (stringp charset)
2377                   (setq charset (intern (downcase charset)))))))
2378       (if read-charset
2379           (setq charset (mm-read-coding-system "Charset: " charset)))
2380       (unless charset
2381         (setq charset gnus-newsgroup-charset))
2382       (when (or force
2383                 (and type (let ((case-fold-search t))
2384                             (string-match "base64" type))))
2385         (article-goto-body)
2386         (save-restriction
2387           (narrow-to-region (point) (point-max))
2388           (base64-decode-region (point-min) (point-max))
2389           (mm-decode-coding-region
2390            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2391
2392 (eval-when-compile
2393   (require 'rfc1843))
2394
2395 (defun article-decode-HZ ()
2396   "Translate a HZ-encoded article."
2397   (interactive)
2398   (require 'rfc1843)
2399   (save-excursion
2400     (let ((buffer-read-only nil))
2401       (rfc1843-decode-region (point-min) (point-max)))))
2402
2403 (defun article-unsplit-urls ()
2404   "Remove the newlines that some other mailers insert into URLs."
2405   (interactive)
2406   (save-excursion
2407     (let ((buffer-read-only nil))
2408       (goto-char (point-min))
2409       (while (re-search-forward
2410               "^\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2411         (replace-match "\\1\\3" t)))
2412     (when (and gnus-display-mime-function (interactive-p))
2413       (funcall gnus-display-mime-function))))
2414
2415
2416 (defun article-wash-html (&optional read-charset)
2417   "Format an HTML article.
2418 If READ-CHARSET, ask for a coding system."
2419   (interactive "P")
2420   (save-excursion
2421     (let ((buffer-read-only nil)
2422           charset)
2423       (when (gnus-buffer-live-p gnus-original-article-buffer)
2424         (with-current-buffer gnus-original-article-buffer
2425           (let* ((ct (gnus-fetch-field "content-type"))
2426                  (ctl (and ct
2427                            (ignore-errors
2428                              (mail-header-parse-content-type ct)))))
2429             (setq charset (and ctl
2430                                (mail-content-type-get ctl 'charset)))
2431             (when (stringp charset)
2432               (setq charset (intern (downcase charset)))))))
2433       (when read-charset
2434         (setq charset (mm-read-coding-system "Charset: " charset)))
2435       (unless charset
2436         (setq charset gnus-newsgroup-charset))
2437       (article-goto-body)
2438       (save-window-excursion
2439         (save-restriction
2440           (narrow-to-region (point) (point-max))
2441           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2442                  (entry (assq func mm-text-html-washer-alist)))
2443             (when entry
2444               (setq func (cdr entry)))
2445             (cond
2446              ((functionp func)
2447               (funcall func))
2448              (t
2449               (apply (car func) (cdr func))))))))))
2450
2451 (defun gnus-article-wash-html-with-w3 ()
2452   "Wash the current buffer with w3."
2453   (mm-setup-w3)
2454   (let ((w3-strict-width (window-width))
2455         (url-standalone-mode t)
2456         (url-gateway-unplugged t)
2457         (w3-honor-stylesheets nil))
2458     (condition-case ()
2459         (w3-region (point-min) (point-max))
2460       (error))))
2461
2462 (defun gnus-article-wash-html-with-w3m ()
2463   "Wash the current buffer with emacs-w3m."
2464   (mm-setup-w3m)
2465   (save-restriction
2466     (narrow-to-region (point) (point-max))
2467     (let ((w3m-safe-url-regexp (if mm-inline-text-html-with-images
2468                                    nil
2469                                  "\\`cid:"))
2470           w3m-force-redisplay)
2471       (w3m-region (point-min) (point-max)))
2472     (when mm-inline-text-html-with-w3m-keymap
2473       (add-text-properties
2474        (point-min) (point-max)
2475        (nconc (mm-w3m-local-map-property)
2476               '(mm-inline-text-html-with-w3m t))))))
2477
2478 (defun article-hide-list-identifiers ()
2479   "Remove list identifies from the Subject header.
2480 The `gnus-list-identifiers' variable specifies what to do."
2481   (interactive)
2482   (let ((inhibit-point-motion-hooks t)
2483         (regexp (if (consp gnus-list-identifiers)
2484                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2485                   gnus-list-identifiers))
2486         buffer-read-only)
2487     (when regexp
2488       (save-excursion
2489         (save-restriction
2490           (article-narrow-to-head)
2491           (goto-char (point-min))
2492           (while (re-search-forward
2493                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2494                   nil t)
2495             (delete-region (match-beginning 2) (match-end 0))
2496             (beginning-of-line))
2497           (when (re-search-forward
2498                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2499             (delete-region (match-beginning 1) (match-end 1))))))))
2500
2501 (defun article-hide-pem (&optional arg)
2502   "Toggle hiding of any PEM headers and signatures in the current article.
2503 If given a negative prefix, always show; if given a positive prefix,
2504 always hide."
2505   (interactive (gnus-article-hidden-arg))
2506   (unless (gnus-article-check-hidden-text 'pem arg)
2507     (save-excursion
2508       (let (buffer-read-only end)
2509         (goto-char (point-min))
2510         ;; Hide the horrendously ugly "header".
2511         (when (and (search-forward
2512                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2513                     nil t)
2514                    (setq end (1+ (match-beginning 0))))
2515           (gnus-add-wash-type 'pem)
2516           (gnus-article-hide-text-type
2517            end
2518            (if (search-forward "\n\n" nil t)
2519                (match-end 0)
2520              (point-max))
2521            'pem)
2522           ;; Hide the trailer as well
2523           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2524                                 nil t)
2525             (gnus-article-hide-text-type
2526              (match-beginning 0) (match-end 0) 'pem)))))))
2527
2528 (defun article-strip-banner ()
2529   "Strip the banners specified by the `banner' group parameter and by
2530 `gnus-article-address-banner-alist'."
2531   (interactive)
2532   (save-excursion
2533     (save-restriction
2534       (let ((inhibit-point-motion-hooks t))
2535         (when (gnus-parameter-banner gnus-newsgroup-name)
2536           (article-really-strip-banner
2537            (gnus-parameter-banner gnus-newsgroup-name)))
2538         (when gnus-article-address-banner-alist
2539           (article-really-strip-banner
2540            (let ((from (save-restriction
2541                          (widen)
2542                          (article-narrow-to-head)
2543                          (mail-fetch-field "from"))))
2544              (when (and from
2545                         (setq from
2546                               (caar (mail-header-parse-addresses from))))
2547                (catch 'found
2548                  (dolist (pair gnus-article-address-banner-alist)
2549                    (when (string-match (car pair) from)
2550                      (throw 'found (cdr pair)))))))))))))
2551
2552 (defun article-really-strip-banner (banner)
2553   "Strip the banner specified by the argument."
2554   (save-excursion
2555     (save-restriction
2556       (let ((inhibit-point-motion-hooks t)
2557             (gnus-signature-limit nil)
2558             buffer-read-only)
2559         (article-goto-body)
2560         (cond
2561          ((eq banner 'signature)
2562           (when (gnus-article-narrow-to-signature)
2563             (widen)
2564             (forward-line -1)
2565             (delete-region (point) (point-max))))
2566          ((symbolp banner)
2567           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2568               (while (re-search-forward banner nil t)
2569                 (delete-region (match-beginning 0) (match-end 0)))))
2570          ((stringp banner)
2571           (while (re-search-forward banner nil t)
2572             (delete-region (match-beginning 0) (match-end 0)))))))))
2573
2574 (defun article-babel ()
2575   "Translate article using an online translation service."
2576   (interactive)
2577   (require 'babel)
2578   (save-excursion
2579     (set-buffer gnus-article-buffer)
2580     (when (article-goto-body)
2581       (let* ((buffer-read-only nil)
2582              (start (point))
2583              (end (point-max))
2584              (orig (buffer-substring start end))
2585              (trans (babel-as-string orig)))
2586         (save-restriction
2587           (narrow-to-region start end)
2588           (delete-region start end)
2589           (insert trans))))))
2590
2591 (defun article-hide-signature (&optional arg)
2592   "Hide the signature in the current article.
2593 If given a negative prefix, always show; if given a positive prefix,
2594 always hide."
2595   (interactive (gnus-article-hidden-arg))
2596   (save-excursion
2597     (save-restriction
2598       (if (interactive-p)
2599           (progn
2600             (widen)
2601             (article-goto-body))
2602         (goto-char (point-min)))
2603       (unless (gnus-article-check-hidden-text 'signature arg)
2604         (let ((buffer-read-only nil)
2605               (button (point)))
2606           (while (setq button (text-property-any button (point-max)
2607                                                  'gnus-callback
2608                                                  'gnus-signature-toggle))
2609             (setq button (text-property-not-all button (point-max)
2610                                                 'gnus-callback
2611                                                 'gnus-signature-toggle))
2612             (when (and button (not (eobp)))
2613               (gnus-article-hide-text-type
2614                (1+ button)
2615                (next-single-property-change (1+ button) 'mime-view-entity
2616                                             nil (point-max))
2617                'signature)))))))
2618   (gnus-set-mode-line 'article))
2619
2620 (defun article-strip-headers-in-body ()
2621   "Strip offensive headers from bodies."
2622   (interactive)
2623   (save-excursion
2624     (article-goto-body)
2625     (let ((case-fold-search t))
2626       (when (looking-at "x-no-archive:")
2627         (gnus-delete-line)))))
2628
2629 (defun article-strip-leading-blank-lines ()
2630   "Remove all blank lines from the beginning of the article."
2631   (interactive)
2632   (save-excursion
2633     (let ((inhibit-point-motion-hooks t)
2634           buffer-read-only)
2635       (when (article-goto-body)
2636         (while (and (not (eobp))
2637                     (looking-at "[ \t]*$"))
2638           (gnus-delete-line))))))
2639
2640 (defun article-narrow-to-head ()
2641   "Narrow the buffer to the head of the message.
2642 Point is left at the beginning of the narrowed-to region."
2643   (narrow-to-region
2644    (goto-char (point-min))
2645    (if (search-forward "\n\n" nil 1)
2646        (1- (point))
2647      (point-max)))
2648   (goto-char (point-min)))
2649
2650 (defun article-goto-body ()
2651   "Place point at the start of the body."
2652   (goto-char (point-min))
2653   (cond
2654    ;; This variable is only bound when dealing with separate
2655    ;; MIME body parts.
2656    (article-goto-body-goes-to-point-min-p
2657     t)
2658    ((search-forward "\n\n" nil t)
2659     t)
2660    (t
2661     (goto-char (point-max))
2662     nil)))
2663
2664 (defun article-strip-multiple-blank-lines ()
2665   "Replace consecutive blank lines with one empty line."
2666   (interactive)
2667   (save-excursion
2668     (let ((inhibit-point-motion-hooks t)
2669           buffer-read-only)
2670       ;; First make all blank lines empty.
2671       (article-goto-body)
2672       (while (re-search-forward "^[ \t]+$" nil t)
2673         (unless (gnus-annotation-in-region-p
2674                  (match-beginning 0) (match-end 0))
2675           (replace-match "" nil t)))
2676       ;; Then replace multiple empty lines with a single empty line.
2677       (article-goto-body)
2678       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2679         (unless (gnus-annotation-in-region-p
2680                  (match-beginning 0) (match-end 0))
2681           (delete-region (match-beginning 1) (match-end 1)))))))
2682
2683 (defun article-strip-leading-space ()
2684   "Remove all white space from the beginning of the lines in the article."
2685   (interactive)
2686   (save-excursion
2687     (let ((inhibit-point-motion-hooks t)
2688           buffer-read-only)
2689       (article-goto-body)
2690       (while (re-search-forward "^[ \t]+" nil t)
2691         (replace-match "" t t)))))
2692
2693 (defun article-strip-trailing-space ()
2694   "Remove all white space from the end of the lines in the article."
2695   (interactive)
2696   (save-excursion
2697     (let ((inhibit-point-motion-hooks t)
2698           buffer-read-only)
2699       (article-goto-body)
2700       (while (re-search-forward "[ \t]+$" nil t)
2701         (replace-match "" t t)))))
2702
2703 (defun article-strip-blank-lines ()
2704   "Strip leading, trailing and multiple blank lines."
2705   (interactive)
2706   (article-strip-leading-blank-lines)
2707   (article-remove-trailing-blank-lines)
2708   (article-strip-multiple-blank-lines))
2709
2710 (defun article-strip-all-blank-lines ()
2711   "Strip all blank lines."
2712   (interactive)
2713   (save-excursion
2714     (let ((inhibit-point-motion-hooks t)
2715           buffer-read-only)
2716       (article-goto-body)
2717       (while (re-search-forward "^[ \t]*\n" nil t)
2718         (replace-match "" t t)))))
2719
2720 (defun gnus-article-narrow-to-signature ()
2721   "Narrow to the signature; return t if a signature is found, else nil."
2722   (let ((inhibit-point-motion-hooks t))
2723     (when (gnus-article-search-signature)
2724       (forward-line 1)
2725       ;; Check whether we have some limits to what we consider
2726       ;; to be a signature.
2727       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2728                       (list gnus-signature-limit)))
2729             limit limited)
2730         (while (setq limit (pop limits))
2731           (if (or (and (integerp limit)
2732                        (< (- (point-max) (point)) limit))
2733                   (and (floatp limit)
2734                        (< (count-lines (point) (point-max)) limit))
2735                   (and (functionp limit)
2736                        (funcall limit))
2737                   (and (stringp limit)
2738                        (not (re-search-forward limit nil t))))
2739               ()                        ; This limit did not succeed.
2740             (setq limited t
2741                   limits nil)))
2742         (unless limited
2743           (narrow-to-region (point) (point-max))
2744           t)))))
2745
2746 (defun gnus-article-search-signature ()
2747   "Search the current buffer for the signature separator.
2748 Put point at the beginning of the signature separator."
2749   (let ((cur (point)))
2750     (goto-char (point-max))
2751     (if (if (stringp gnus-signature-separator)
2752             (re-search-backward gnus-signature-separator nil t)
2753           (let ((seps gnus-signature-separator))
2754             (while (and seps
2755                         (not (re-search-backward (car seps) nil t)))
2756               (pop seps))
2757             seps))
2758         t
2759       (goto-char cur)
2760       nil)))
2761
2762 (defun gnus-article-hidden-arg ()
2763   "Return the current prefix arg as a number, or 0 if no prefix."
2764   (list (if current-prefix-arg
2765             (prefix-numeric-value current-prefix-arg)
2766           0)))
2767
2768 (defun gnus-article-check-hidden-text (type arg)
2769   "Return nil if hiding is necessary.
2770 Arg can be nil or a number.  nil and positive means hide, negative
2771 means show, 0 means toggle."
2772   (save-excursion
2773     (save-restriction
2774       (let ((hide (gnus-article-hidden-text-p type)))
2775         (cond
2776          ((or (null arg)
2777               (> arg 0))
2778           nil)
2779          ((< arg 0)
2780           (gnus-article-show-hidden-text type)
2781           t)
2782          (t
2783           (if (eq hide 'hidden)
2784               (progn
2785                 (gnus-article-show-hidden-text type)
2786                 t)
2787             nil)))))))
2788
2789 (defun gnus-article-hidden-text-p (type)
2790   "Say whether the current buffer contains hidden text of type TYPE."
2791   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2792     (while (and pos
2793                 (not (get-text-property pos 'invisible))
2794                 (not (get-text-property pos 'dummy-invisible)))
2795       (setq pos
2796             (text-property-any (1+ pos) (point-max) 'article-type type)))
2797     (if pos
2798         'hidden
2799       nil)))
2800
2801 (defun gnus-article-show-hidden-text (type &optional dummy)
2802   "Show all hidden text of type TYPE.
2803 Originally it is hide instead of DUMMY."
2804   (let ((buffer-read-only nil)
2805         (inhibit-point-motion-hooks t))
2806     (gnus-remove-text-properties-when
2807      'article-type type
2808      (point-min) (point-max)
2809      (cons 'article-type (cons type
2810                                gnus-hidden-properties)))
2811     (gnus-delete-wash-type type)))
2812
2813 (defconst article-time-units
2814   `((year . ,(* 365.25 24 60 60))
2815     (week . ,(* 7 24 60 60))
2816     (day . ,(* 24 60 60))
2817     (hour . ,(* 60 60))
2818     (minute . 60)
2819     (second . 1))
2820   "Mapping from time units to seconds.")
2821
2822 (defun gnus-article-forward-header ()
2823   "Move point to the start of the next header.
2824 If the current header is a continuation header, this can be several
2825 lines forward."
2826   (let ((ended nil))
2827     (while (not ended)
2828       (forward-line 1)
2829       (if (looking-at "[ \t]+[^ \t]")
2830           (forward-line 1)
2831         (setq ended t)))))
2832
2833 (defun article-date-ut (&optional type highlight header)
2834   "Convert DATE date to universal time in the current article.
2835 If TYPE is `local', convert to local time; if it is `lapsed', output
2836 how much time has lapsed since DATE.  For `lapsed', the value of
2837 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2838 should replace the \"Date:\" one, or should be added below it."
2839   (interactive (list 'ut t))
2840   (let* ((header (or header
2841                      (and (eq 1 (point-min))
2842                           (mail-header-date (save-excursion
2843                                               (set-buffer gnus-summary-buffer)
2844                                               gnus-current-headers)))
2845                      (message-fetch-field "date")
2846                      ""))
2847          (date (if (vectorp header) (mail-header-date header)
2848                  header))
2849          (inhibit-point-motion-hooks t)
2850          bface eface date-pos)
2851     (when (and date (not (string= date "")))
2852       (save-excursion
2853         (save-restriction
2854           (article-narrow-to-head)
2855           (when (or (and (eq type 'lapsed)
2856                          gnus-article-date-lapsed-new-header
2857                          ;; Attempt to get the face of X-Sent first.
2858                          (re-search-forward "^X-Sent:[ \t]" nil t))
2859                     (re-search-forward "^Date:[ \t]" nil t)
2860                     ;; If Date is missing, try again for X-Sent.
2861                     (re-search-forward "^X-Sent:[ \t]" nil t))
2862             (setq bface (get-text-property (gnus-point-at-bol) 'face)
2863                   date (or (get-text-property (gnus-point-at-bol)
2864                                               'original-date)
2865                            date)
2866                   eface (get-text-property (1- (gnus-point-at-eol))
2867                                            'face)))
2868           (let ((buffer-read-only nil))
2869             ;; Delete any old X-Sent headers.
2870             (when (setq date-pos
2871                         (text-property-any (point-min) (point-max)
2872                                            'article-date-lapsed t))
2873               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2874               (delete-region (match-beginning 0)
2875                              (progn (forward-line 1) (point))))
2876             (goto-char (point-min))
2877             ;; Delete any old Date headers.
2878             (while (re-search-forward "^Date:[ \t]" nil t)
2879               (unless date-pos
2880                 (setq date-pos (match-beginning 0)))
2881               (unless (and (eq type 'lapsed)
2882                            gnus-article-date-lapsed-new-header)
2883                 (delete-region (match-beginning 0)
2884                                (progn (message-next-header) (point)))))
2885             (if date-pos
2886                 (progn
2887                   (goto-char date-pos)
2888                   (unless (bolp)
2889                     ;; Possibly, Date has been deleted.
2890                     (insert "\n"))
2891                   (when (and (eq type 'lapsed)
2892                              gnus-article-date-lapsed-new-header
2893                              (looking-at "Date:"))
2894                     (forward-line 1)))
2895               (goto-char (point-min)))
2896             (insert (article-make-date-line date type))
2897             (when (eq type 'lapsed)
2898               (put-text-property (gnus-point-at-bol) (point)
2899                                  'article-date-lapsed t))
2900             (insert "\n")
2901             (forward-line -1)
2902             ;; Do highlighting.
2903             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2904               (put-text-property (match-beginning 1) (1+ (match-end 1))
2905                                  'original-date date)
2906               (put-text-property (match-beginning 1) (1+ (match-end 1))
2907                                  'face bface)
2908               (put-text-property (match-beginning 2) (match-end 2)
2909                                  'face eface))))))))
2910
2911 (defun article-make-date-line (date type)
2912   "Return a DATE line of TYPE."
2913   (unless (memq type '(local ut original user iso8601 lapsed english))
2914     (error "Unknown conversion type: %s" type))
2915   (condition-case ()
2916       (let ((time (date-to-time date)))
2917         (cond
2918          ;; Convert to the local timezone.
2919          ((eq type 'local)
2920           (let ((tz (car (current-time-zone time))))
2921             (format "Date: %s %s%02d%02d" (current-time-string time)
2922                     (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2923                     (/ (% (abs tz) 3600) 60))))
2924          ;; Convert to Universal Time.
2925          ((eq type 'ut)
2926           (concat "Date: "
2927                   (current-time-string
2928                    (let* ((e (parse-time-string date))
2929                           (tm (apply 'encode-time e))
2930                           (ms (car tm))
2931                           (ls (- (cadr tm) (car (current-time-zone time)))))
2932                      (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2933                            ((> ls 65535) (list (1+ ms) (- ls 65536)))
2934                            (t (list ms ls)))))
2935                   " UT"))
2936          ;; Get the original date from the article.
2937          ((eq type 'original)
2938           (concat "Date: " (if (string-match "\n+$" date)
2939                                (substring date 0 (match-beginning 0))
2940                              date)))
2941          ;; Let the user define the format.
2942          ((eq type 'user)
2943           (let ((format (or (condition-case nil
2944                                 (with-current-buffer gnus-summary-buffer
2945                                   gnus-article-time-format)
2946                               (error nil))
2947                             gnus-article-time-format)))
2948             (if (functionp format)
2949                 (funcall format time)
2950               (concat "Date: " (format-time-string format time)))))
2951          ;; ISO 8601.
2952          ((eq type 'iso8601)
2953           (let ((tz (car (current-time-zone time))))
2954             (concat
2955              "Date: "
2956              (format-time-string "%Y%m%dT%H%M%S" time)
2957              (format "%s%02d%02d"
2958                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2959                      (/ (% (abs tz) 3600) 60)))))
2960          ;; Do an X-Sent lapsed format.
2961          ((eq type 'lapsed)
2962           ;; If the date is seriously mangled, the timezone functions are
2963           ;; liable to bug out, so we ignore all errors.
2964           (let* ((now (current-time))
2965                  (real-time (subtract-time now time))
2966                  (real-sec (and real-time
2967                                 (+ (* (float (car real-time)) 65536)
2968                                    (cadr real-time))))
2969                  (sec (and real-time (abs real-sec)))
2970                  num prev)
2971             (cond
2972              ((null real-time)
2973               "X-Sent: Unknown")
2974              ((zerop sec)
2975               "X-Sent: Now")
2976              (t
2977               (concat
2978                "X-Sent: "
2979                ;; This is a bit convoluted, but basically we go
2980                ;; through the time units for years, weeks, etc,
2981                ;; and divide things to see whether that results
2982                ;; in positive answers.
2983                (mapconcat
2984                 (lambda (unit)
2985                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2986                       ;; The (remaining) seconds are too few to
2987                       ;; be divided into this time unit.
2988                       ""
2989                     ;; It's big enough, so we output it.
2990                     (setq sec (- sec (* num (cdr unit))))
2991                     (prog1
2992                         (concat (if prev ", " "") (int-to-string
2993                                                    (floor num))
2994                                 " " (symbol-name (car unit))
2995                                 (if (> num 1) "s" ""))
2996                       (setq prev t))))
2997                 article-time-units "")
2998                ;; If dates are odd, then it might appear like the
2999                ;; article was sent in the future.
3000                (if (> real-sec 0)
3001                    " ago"
3002                  " in the future"))))))
3003          ;; Display the date in proper English
3004          ((eq type 'english)
3005           (let ((dtime (decode-time time)))
3006             (concat
3007              "Date: the "
3008              (number-to-string (nth 3 dtime))
3009              (let ((digit (% (nth 3 dtime) 10)))
3010                (cond
3011                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3012                 ((= digit 1) "st")
3013                 ((= digit 2) "nd")
3014                 ((= digit 3) "rd")
3015                 (t "th")))
3016              " of "
3017              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3018              " "
3019              (number-to-string (nth 5 dtime))
3020              " at "
3021              (format "%02d" (nth 2 dtime))
3022              ":"
3023              (format "%02d" (nth 1 dtime)))))))
3024     (error
3025      (format "Date: %s (from T-gnus)" date))))
3026
3027 (defun article-date-local (&optional highlight)
3028   "Convert the current article date to the local timezone."
3029   (interactive (list t))
3030   (article-date-ut 'local highlight))
3031
3032 (defun article-date-english (&optional highlight)
3033   "Convert the current article date to something that is proper English."
3034   (interactive (list t))
3035   (article-date-ut 'english highlight))
3036
3037 (defun article-date-original (&optional highlight)
3038   "Convert the current article date to what it was originally.
3039 This is only useful if you have used some other date conversion
3040 function and want to see what the date was before converting."
3041   (interactive (list t))
3042   (article-date-ut 'original highlight))
3043
3044 (defun article-date-lapsed (&optional highlight)
3045   "Convert the current article date to time lapsed since it was sent."
3046   (interactive (list t))
3047   (article-date-ut 'lapsed highlight))
3048
3049 (defun article-update-date-lapsed ()
3050   "Function to be run from a timer to update the lapsed time line."
3051   (let (deactivate-mark)
3052     (save-excursion
3053       (ignore-errors
3054         (walk-windows
3055          (lambda (w)
3056            (set-buffer (window-buffer w))
3057            (when (eq major-mode 'gnus-article-mode)
3058              (goto-char (point-min))
3059              (when (re-search-forward "^X-Sent:" nil t)
3060                (article-date-lapsed t))))
3061          nil 'visible)))))
3062
3063 (defun gnus-start-date-timer (&optional n)
3064   "Start a timer to update the X-Sent header in the article buffers.
3065 The numerical prefix says how frequently (in seconds) the function
3066 is to run."
3067   (interactive "p")
3068   (unless n
3069     (setq n 1))
3070   (gnus-stop-date-timer)
3071   (setq article-lapsed-timer
3072         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
3073
3074 (defun gnus-stop-date-timer ()
3075   "Stop the X-Sent timer."
3076   (interactive)
3077   (when article-lapsed-timer
3078     (nnheader-cancel-timer article-lapsed-timer)
3079     (setq article-lapsed-timer nil)))
3080
3081 (defun article-date-user (&optional highlight)
3082   "Convert the current article date to the user-defined format.
3083 This format is defined by the `gnus-article-time-format' variable."
3084   (interactive (list t))
3085   (article-date-ut 'user highlight))
3086
3087 (defun article-date-iso8601 (&optional highlight)
3088   "Convert the current article date to ISO8601."
3089   (interactive (list t))
3090   (article-date-ut 'iso8601 highlight))
3091
3092 ;; (defun article-show-all ()
3093 ;;   "Show all hidden text in the article buffer."
3094 ;;   (interactive)
3095 ;;   (save-excursion
3096 ;;     (widen)
3097 ;;     (let ((buffer-read-only nil))
3098 ;;       (gnus-article-unhide-text (point-min) (point-max))
3099 ;;       (gnus-remove-text-with-property 'gnus-prev)
3100 ;;       (gnus-remove-text-with-property 'gnus-next))))
3101
3102 (defun article-show-all-headers ()
3103   "Show all hidden headers in the article buffer."
3104   (interactive)
3105   (save-excursion
3106     (save-restriction
3107       (widen)
3108       (article-narrow-to-head)
3109       (let ((buffer-read-only nil))
3110         (gnus-article-unhide-text (point-min) (point-max))))))
3111
3112 (defun article-remove-leading-whitespace ()
3113   "Remove excessive whitespace from all headers."
3114   (interactive)
3115   (save-excursion
3116     (save-restriction
3117       (let ((buffer-read-only nil))
3118         (article-narrow-to-head)
3119         (goto-char (point-min))
3120         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3121           (delete-region (match-beginning 1) (match-end 1)))))))
3122
3123 (defun article-emphasize (&optional arg)
3124   "Emphasize text according to `gnus-emphasis-alist'."
3125   (interactive (gnus-article-hidden-arg))
3126   (unless (gnus-article-check-hidden-text 'emphasis arg)
3127     (save-excursion
3128       (let ((alist (or
3129                     (condition-case nil
3130                         (with-current-buffer gnus-summary-buffer
3131                           gnus-article-emphasis-alist)
3132                       (error))
3133                     gnus-emphasis-alist))
3134             (buffer-read-only nil)
3135             (props (append '(article-type emphasis)
3136                            gnus-hidden-properties))
3137             regexp elem beg invisible visible face)
3138         (article-goto-body)
3139         (setq beg (point))
3140         (while (setq elem (pop alist))
3141           (goto-char beg)
3142           (setq regexp (car elem)
3143                 invisible (nth 1 elem)
3144                 visible (nth 2 elem)
3145                 face (nth 3 elem))
3146           (while (re-search-forward regexp nil t)
3147             (when (and (match-beginning visible) (match-beginning invisible))
3148               (gnus-article-hide-text
3149                (match-beginning invisible) (match-end invisible) props)
3150               (gnus-article-unhide-text-type
3151                (match-beginning visible) (match-end visible) 'emphasis)
3152               (gnus-put-overlay-excluding-newlines
3153                (match-beginning visible) (match-end visible) 'face face)
3154               (gnus-add-wash-type 'emphasis)
3155               (goto-char (match-end invisible)))))))))
3156
3157 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3158   "Setup newsgroup emphasis alist."
3159   (unless gnus-article-emphasis-alist
3160     (let ((name (and gnus-newsgroup-name
3161                      (gnus-group-real-name gnus-newsgroup-name))))
3162       (make-local-variable 'gnus-article-emphasis-alist)
3163       (setq gnus-article-emphasis-alist
3164             (nconc
3165              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3166                (while (setq elem (pop alist))
3167                  (when (and name (string-match (car elem) name))
3168                    (setq alist nil
3169                          highlight (copy-sequence (cdr elem)))))
3170                highlight)
3171              (copy-sequence highlight-words)
3172              (if gnus-newsgroup-name
3173                  (copy-sequence (gnus-group-find-parameter
3174                                  gnus-newsgroup-name 'highlight-words t)))
3175              gnus-emphasis-alist)))))
3176
3177 (eval-when-compile
3178   (defvar gnus-summary-article-menu)
3179   (defvar gnus-summary-post-menu))
3180
3181 ;;; Saving functions.
3182
3183 (defun gnus-article-save (save-buffer file &optional num)
3184   "Save the currently selected article."
3185   (unless gnus-save-all-headers
3186     ;; Remove headers according to `gnus-saved-headers'.
3187     (let ((gnus-visible-headers
3188            (or gnus-saved-headers gnus-visible-headers))
3189           (gnus-article-buffer save-buffer))
3190       (save-excursion
3191         (set-buffer save-buffer)
3192         (article-hide-headers 1 t))))
3193   (save-window-excursion
3194     (if (not gnus-default-article-saver)
3195         (error "No default saver is defined")
3196       ;; !!! Magic!  The saving functions all save
3197       ;; `gnus-save-article-buffer' (or so they think), but we
3198       ;; bind that variable to our save-buffer.
3199       (set-buffer gnus-article-buffer)
3200       (let* ((gnus-save-article-buffer save-buffer)
3201              (filename
3202               (cond
3203                ((not gnus-prompt-before-saving) 'default)
3204                ((eq gnus-prompt-before-saving 'always) nil)
3205                (t file)))
3206              (gnus-number-of-articles-to-be-saved
3207               (when (eq gnus-prompt-before-saving t)
3208                 num)))                  ; Magic
3209         (set-buffer gnus-article-current-summary)
3210         (funcall gnus-default-article-saver filename)))))
3211
3212 (defun gnus-read-save-file-name (prompt &optional filename
3213                                         function group headers variable)
3214   (let ((default-name
3215           (funcall function group headers (symbol-value variable)))
3216         result)
3217     (setq result
3218           (expand-file-name
3219            (cond
3220             ((eq filename 'default)
3221              default-name)
3222             ((eq filename t)
3223              default-name)
3224             (filename filename)
3225             (t
3226              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3227                     (prompt
3228                      (format prompt
3229                              (if (and gnus-number-of-articles-to-be-saved
3230                                       (> gnus-number-of-articles-to-be-saved 1))
3231                                  (format "these %d articles"
3232                                          gnus-number-of-articles-to-be-saved)
3233                                "this article")))
3234                     (file
3235                      ;; Let the split methods have their say.
3236                      (cond
3237                       ;; No split name was found.
3238                       ((null split-name)
3239                        (read-file-name
3240                         (concat prompt " (default "
3241                                 (file-name-nondirectory default-name) ") ")
3242                         (file-name-directory default-name)
3243                         default-name))
3244                       ;; A single group name is returned.
3245                       ((stringp split-name)
3246                        (setq default-name
3247                              (funcall function split-name headers
3248                                       (symbol-value variable)))
3249                        (read-file-name
3250                         (concat prompt " (default "
3251                                 (file-name-nondirectory default-name) ") ")
3252                         (file-name-directory default-name)
3253                         default-name))
3254                       ;; A single split name was found
3255                       ((= 1 (length split-name))
3256                        (let* ((name (expand-file-name
3257                                      (car split-name)
3258                                      gnus-article-save-directory))
3259                               (dir (cond ((file-directory-p name)
3260                                           (file-name-as-directory name))
3261                                          ((file-exists-p name) name)
3262                                          (t gnus-article-save-directory))))
3263                          (read-file-name
3264                           (concat prompt " (default " name ") ")
3265                           dir name)))
3266                       ;; A list of splits was found.
3267                       (t
3268                        (setq split-name (nreverse split-name))
3269                        (let (result)
3270                          (let ((file-name-history
3271                                 (nconc split-name file-name-history)))
3272                            (setq result
3273                                  (expand-file-name
3274                                   (read-file-name
3275                                    (concat prompt " (`M-p' for defaults) ")
3276                                    gnus-article-save-directory
3277                                    (car split-name))
3278                                   gnus-article-save-directory)))
3279                          (car (push result file-name-history)))))))
3280                ;; Create the directory.
3281                (gnus-make-directory (file-name-directory file))
3282                ;; If we have read a directory, we append the default file name.
3283                (when (file-directory-p file)
3284                  (setq file (expand-file-name (file-name-nondirectory
3285                                                default-name)
3286                                               (file-name-as-directory file))))
3287                ;; Possibly translate some characters.
3288                (nnheader-translate-file-chars file))))))
3289     (gnus-make-directory (file-name-directory result))
3290     (set variable result)))
3291
3292 (defun gnus-article-archive-name (group)
3293   "Return the first instance of an \"Archive-name\" in the current buffer."
3294   (let ((case-fold-search t))
3295     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3296       (nnheader-concat gnus-article-save-directory
3297                        (match-string 1)))))
3298
3299 (defun gnus-article-nndoc-name (group)
3300   "If GROUP is an nndoc group, return the name of the parent group."
3301   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3302     (gnus-group-get-parameter group 'save-article-group)))
3303
3304 (defun gnus-summary-save-in-rmail (&optional filename)
3305   "Append this article to Rmail file.
3306 Optional argument FILENAME specifies file name.
3307 Directory to save to is default to `gnus-article-save-directory'."
3308   (setq filename (gnus-read-save-file-name
3309                   "Save %s in rmail file:" filename
3310                   gnus-rmail-save-name gnus-newsgroup-name
3311                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3312   (gnus-eval-in-buffer-window gnus-save-article-buffer
3313     (save-excursion
3314       (save-restriction
3315         (widen)
3316         (gnus-output-to-rmail filename))))
3317   filename)
3318
3319 (defun gnus-summary-save-in-mail (&optional filename)
3320   "Append this article to Unix mail file.
3321 Optional argument FILENAME specifies file name.
3322 Directory to save to is default to `gnus-article-save-directory'."
3323   (setq filename (gnus-read-save-file-name
3324                   "Save %s in Unix mail file:" filename
3325                   gnus-mail-save-name gnus-newsgroup-name
3326                   gnus-current-headers 'gnus-newsgroup-last-mail))
3327   (gnus-eval-in-buffer-window gnus-save-article-buffer
3328     (save-excursion
3329       (save-restriction
3330         (widen)
3331         (if (and (file-readable-p filename)
3332                  (file-regular-p filename)
3333                  (mail-file-babyl-p filename))
3334             (rmail-output-to-rmail-file filename t)
3335           (gnus-output-to-mail filename)))))
3336   filename)
3337
3338 (defun gnus-summary-save-in-file (&optional filename overwrite)
3339   "Append this article to file.
3340 Optional argument FILENAME specifies file name.
3341 Directory to save to is default to `gnus-article-save-directory'."
3342   (setq filename (gnus-read-save-file-name
3343                   "Save %s in file:" filename
3344                   gnus-file-save-name gnus-newsgroup-name
3345                   gnus-current-headers 'gnus-newsgroup-last-file))
3346   (gnus-eval-in-buffer-window gnus-save-article-buffer
3347     (save-excursion
3348       (save-restriction
3349         (widen)
3350         (when (and overwrite
3351                    (file-exists-p filename))
3352           (delete-file filename))
3353         (gnus-output-to-file filename))))
3354   filename)
3355
3356 (defun gnus-summary-write-to-file (&optional filename)
3357   "Write this article to a file, overwriting it if the file exists.
3358 Optional argument FILENAME specifies file name.
3359 The directory to save in defaults to `gnus-article-save-directory'."
3360   (gnus-summary-save-in-file nil t))
3361
3362 (defun gnus-summary-save-body-in-file (&optional filename)
3363   "Append this article body to a file.
3364 Optional argument FILENAME specifies file name.
3365 The directory to save in defaults to `gnus-article-save-directory'."
3366   (setq filename (gnus-read-save-file-name
3367                   "Save %s body in file:" filename
3368                   gnus-file-save-name gnus-newsgroup-name
3369                   gnus-current-headers 'gnus-newsgroup-last-file))
3370   (gnus-eval-in-buffer-window gnus-save-article-buffer
3371     (save-excursion
3372       (save-restriction
3373         (widen)
3374         (when (article-goto-body)
3375           (narrow-to-region (point) (point-max)))
3376         (gnus-output-to-file filename))))
3377   filename)
3378
3379 (defun gnus-summary-save-in-pipe (&optional command)
3380   "Pipe this article to subprocess."
3381   (setq command
3382         (cond ((and (eq command 'default)
3383                     gnus-last-shell-command)
3384                gnus-last-shell-command)
3385               ((stringp command)
3386                command)
3387               (t (read-string
3388                   (format
3389                    "Shell command on %s: "
3390                    (if (and gnus-number-of-articles-to-be-saved
3391                             (> gnus-number-of-articles-to-be-saved 1))
3392                        (format "these %d articles"
3393                                gnus-number-of-articles-to-be-saved)
3394                      "this article"))
3395                   gnus-last-shell-command))))
3396   (when (string-equal command "")
3397     (if gnus-last-shell-command
3398         (setq command gnus-last-shell-command)
3399       (error "A command is required")))
3400   (gnus-eval-in-buffer-window gnus-article-buffer
3401     (save-restriction
3402       (widen)
3403       (shell-command-on-region (point-min) (point-max) command nil)))
3404   (setq gnus-last-shell-command command))
3405
3406 (defmacro gnus-read-string (prompt &optional initial-contents history 
3407                             default-value)
3408   "Like `read-string' but allow for older XEmacsen that don't have the 5th arg."
3409   (if (and (featurep 'xemacs)
3410            (< emacs-minor-version 2))
3411       `(read-string ,prompt ,initial-contents ,history)
3412     `(read-string ,prompt ,initial-contents ,history ,default-value)))
3413
3414 (defun gnus-summary-pipe-to-muttprint (&optional command)
3415   "Pipe this article to muttprint."
3416   (setq command (gnus-read-string
3417                  "Print using command: " gnus-summary-muttprint-program
3418                  nil gnus-summary-muttprint-program))
3419   (gnus-summary-save-in-pipe command))
3420
3421 ;;; Article file names when saving.
3422
3423 (defun gnus-capitalize-newsgroup (newsgroup)
3424   "Capitalize NEWSGROUP name."
3425   (when (not (zerop (length newsgroup)))
3426     (concat (char-to-string (upcase (aref newsgroup 0)))
3427             (substring newsgroup 1))))
3428
3429 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3430   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3431 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3432 Otherwise, it is like ~/News/news/group/num."
3433   (let ((default
3434           (expand-file-name
3435            (concat (if (gnus-use-long-file-name 'not-save)
3436                        (gnus-capitalize-newsgroup newsgroup)
3437                      (gnus-newsgroup-directory-form newsgroup))
3438                    "/" (int-to-string (mail-header-number headers)))
3439            gnus-article-save-directory)))
3440     (if (and last-file
3441              (string-equal (file-name-directory default)
3442                            (file-name-directory last-file))
3443              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3444         default
3445       (or last-file default))))
3446
3447 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3448   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3449 If variable `gnus-use-long-file-name' is non-nil, it is
3450 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3451   (let ((default
3452           (expand-file-name
3453            (concat (if (gnus-use-long-file-name 'not-save)
3454                        newsgroup
3455                      (gnus-newsgroup-directory-form newsgroup))
3456                    "/" (int-to-string (mail-header-number headers)))
3457            gnus-article-save-directory)))
3458     (if (and last-file
3459              (string-equal (file-name-directory default)
3460                            (file-name-directory last-file))
3461              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3462         default
3463       (or last-file default))))
3464
3465 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3466   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3467 If variable `gnus-use-long-file-name' is non-nil, it is
3468 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3469   (or last-file
3470       (expand-file-name
3471        (if (gnus-use-long-file-name 'not-save)
3472            newsgroup
3473          (file-relative-name
3474           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3475           default-directory))
3476        gnus-article-save-directory)))
3477
3478 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3479   "Generate file name from sender."
3480   (let ((from (mail-header-from headers)))
3481     (expand-file-name
3482      (if (and from (string-match "\\([^ <]+\\)@" from))
3483          (match-string 1 from)
3484        "nobody")
3485      gnus-article-save-directory)))
3486
3487 (defun article-verify-x-pgp-sig ()
3488   "Verify X-PGP-Sig."
3489   (interactive)
3490   (if (gnus-buffer-live-p gnus-original-article-buffer)
3491       (let ((sig (with-current-buffer gnus-original-article-buffer
3492                    (gnus-fetch-field "X-PGP-Sig")))
3493             items info headers)
3494         (when (and sig
3495                    mml2015-use
3496                    (mml2015-clear-verify-function))
3497           (with-temp-buffer
3498             (insert-buffer-substring gnus-original-article-buffer)
3499             (setq items (split-string sig))
3500             (message-narrow-to-head)
3501             (let ((inhibit-point-motion-hooks t)
3502                   (case-fold-search t))
3503               ;; Don't verify multiple headers.
3504               (setq headers (mapconcat (lambda (header)
3505                                          (concat header ": "
3506                                                  (mail-fetch-field header)
3507                                                  "\n"))
3508                                        (split-string (nth 1 items) ",") "")))
3509             (delete-region (point-min) (point-max))
3510             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3511             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3512             (insert headers)
3513             (widen)
3514             (forward-line)
3515             (while (not (eobp))
3516               (if (looking-at "^-")
3517                   (insert "- "))
3518               (forward-line))
3519             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3520             (insert "Version: " (car items) "\n\n")
3521             (insert (mapconcat 'identity (cddr items) "\n"))
3522             (insert "\n-----END PGP SIGNATURE-----\n")
3523             (let ((mm-security-handle (list (format "multipart/signed"))))
3524               (mml2015-clean-buffer)
3525               (let ((coding-system-for-write (or gnus-newsgroup-charset
3526                                                  'iso-8859-1)))
3527                 (funcall (mml2015-clear-verify-function)))
3528               (setq info
3529                     (or (mm-handle-multipart-ctl-parameter
3530                          mm-security-handle 'gnus-details)
3531                         (mm-handle-multipart-ctl-parameter
3532                          mm-security-handle 'gnus-info)))))
3533           (when info
3534             (let (buffer-read-only bface eface)
3535               (save-restriction
3536                 (message-narrow-to-head)
3537                 (goto-char (point-max))
3538                 (forward-line -1)
3539                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
3540                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
3541                 (message-remove-header "X-Gnus-PGP-Verify")
3542                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3543                     (forward-line)
3544                   (goto-char (point-max)))
3545                 (narrow-to-region (point) (point))
3546                 (insert "X-Gnus-PGP-Verify: " info "\n")
3547                 (goto-char (point-min))
3548                 (forward-line)
3549                 (while (not (eobp))
3550                   (if (not (looking-at "^[ \t]"))
3551                       (insert " "))
3552                   (forward-line))
3553                 ;; Do highlighting.
3554                 (goto-char (point-min))
3555                 (when (looking-at "\\([^:]+\\): *")
3556                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3557                                      'face bface)
3558                   (put-text-property (match-end 0) (point-max)
3559                                      'face eface)))))))))
3560
3561 (defun article-verify-cancel-lock ()
3562   "Verify Cancel-Lock header."
3563   (interactive)
3564   (if (gnus-buffer-live-p gnus-original-article-buffer)
3565       (canlock-verify gnus-original-article-buffer)))
3566
3567 (defun article-monafy ()
3568   "Display body part with mona font."
3569   (interactive)
3570   (unless (if (featurep 'xemacs)
3571               (find-face 'gnus-mona-face)
3572             (facep 'gnus-mona-face))
3573     (require 'navi2ch-mona)
3574     (set-face-font (make-face 'gnus-mona-face) navi2ch-mona-font))
3575   (save-excursion
3576     (let ((buffer-read-only nil))
3577       (article-goto-body)
3578       (gnus-overlay-put
3579        (gnus-make-overlay (point) (point-max))
3580        'face 'gnus-mona-face))))
3581
3582 (eval-and-compile
3583   (mapcar
3584    (lambda (func)
3585      (let (afunc gfunc)
3586        (if (consp func)
3587            (setq afunc (car func)
3588                  gfunc (cdr func))
3589          (setq afunc func
3590                gfunc (intern (format "gnus-%s" func))))
3591        (defalias gfunc
3592          (when (fboundp afunc)
3593            `(lambda (&optional interactive &rest args)
3594               ,(documentation afunc t)
3595               (interactive (list t))
3596               (save-excursion
3597                 (set-buffer gnus-article-buffer)
3598                 (if interactive
3599                     (call-interactively ',afunc)
3600                   (apply ',afunc args))))))))
3601    '(article-hide-headers
3602      article-verify-x-pgp-sig
3603      article-verify-cancel-lock
3604      article-monafy
3605      article-hide-boring-headers
3606      article-treat-overstrike
3607      article-fill-long-lines
3608      article-capitalize-sentences
3609      article-remove-cr
3610      article-remove-leading-whitespace
3611      article-display-x-face
3612      article-display-face
3613      article-de-quoted-unreadable
3614      article-de-base64-unreadable
3615      article-decode-HZ
3616      article-wash-html
3617      article-unsplit-urls
3618      article-hide-list-identifiers
3619      article-strip-banner
3620      article-babel
3621      article-hide-pem
3622      article-hide-signature
3623      article-strip-headers-in-body
3624      article-remove-trailing-blank-lines
3625      article-strip-leading-blank-lines
3626      article-strip-multiple-blank-lines
3627      article-strip-leading-space
3628      article-strip-trailing-space
3629      article-strip-blank-lines
3630      article-strip-all-blank-lines
3631      article-date-local
3632      article-date-english
3633      article-date-iso8601
3634      article-date-original
3635      article-date-ut
3636      article-decode-mime-words
3637      article-decode-charset
3638      article-decode-encoded-words
3639      article-date-user
3640      article-date-lapsed
3641      article-emphasize
3642      article-treat-dumbquotes
3643      article-normalize-headers
3644      (article-show-all-headers . gnus-article-show-all-headers)
3645 ;;      (article-show-all . gnus-article-show-all)
3646      )))
3647 \f
3648 ;;;
3649 ;;; Gnus article mode
3650 ;;;
3651
3652 (put 'gnus-article-mode 'mode-class 'special)
3653
3654 (gnus-define-keys gnus-article-mode-map
3655   " " gnus-article-goto-next-page
3656   "\177" gnus-article-goto-prev-page
3657   [delete] gnus-article-goto-prev-page
3658   [backspace] gnus-article-goto-prev-page
3659   "\C-c^" gnus-article-refer-article
3660   "h" gnus-article-show-summary
3661   "s" gnus-article-show-summary
3662   "\C-c\C-m" gnus-article-mail
3663   "?" gnus-article-describe-briefly
3664   "e" gnus-summary-edit-article
3665   "<" beginning-of-buffer
3666   ">" end-of-buffer
3667   "\C-c\C-i" gnus-info-find-node
3668   "\C-c\C-b" gnus-bug
3669   "R" gnus-article-reply-with-original
3670   "F" gnus-article-followup-with-original
3671   "\C-hk" gnus-article-describe-key
3672   "\C-hc" gnus-article-describe-key-briefly
3673
3674   "\C-d" gnus-article-read-summary-keys
3675   "\M-*" gnus-article-read-summary-keys
3676   "\M-#" gnus-article-read-summary-keys
3677   "\M-^" gnus-article-read-summary-keys
3678   "\M-g" gnus-article-read-summary-keys)
3679
3680 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
3681 (let (keys)
3682   (let ((key 32))
3683     (while (<= key 127)
3684       (push (char-to-string key) keys)
3685       (incf key))
3686     (dolist (elem (accessible-keymaps gnus-summary-mode-map))
3687       (setq key (car elem))
3688       (when (and (> (length key) 0)
3689                  (not (eq 'menu-bar (aref key 0)))
3690                  (symbolp (lookup-key gnus-summary-mode-map key)))
3691         (push key keys))))
3692   (dolist (key keys)
3693     (unless (lookup-key gnus-article-mode-map key)
3694       (define-key gnus-article-mode-map key 'gnus-article-read-summary-keys))))
3695
3696 (defun gnus-article-make-menu-bar ()
3697   (unless (boundp 'gnus-article-commands-menu)
3698     (gnus-summary-make-menu-bar))
3699   (gnus-turn-off-edit-menu 'article)
3700   (unless (boundp 'gnus-article-article-menu)
3701     (easy-menu-define
3702      gnus-article-article-menu gnus-article-mode-map ""
3703      '("Article"
3704        ["Scroll forwards" gnus-article-goto-next-page t]
3705        ["Scroll backwards" gnus-article-goto-prev-page t]
3706        ["Show summary" gnus-article-show-summary t]
3707        ["Fetch Message-ID at point" gnus-article-refer-article t]
3708        ["Mail to address at point" gnus-article-mail t]
3709        ["Send a bug report" gnus-bug t]))
3710
3711     (easy-menu-define
3712      gnus-article-treatment-menu gnus-article-mode-map ""
3713      ;; Fixme: this should use :active (and maybe :visible).
3714      '("Treatment"
3715        ["Hide headers" gnus-article-hide-headers t]
3716        ["Hide signature" gnus-article-hide-signature t]
3717        ["Hide citation" gnus-article-hide-citation t]
3718        ["Treat overstrike" gnus-article-treat-overstrike t]
3719        ["Remove carriage return" gnus-article-remove-cr t]
3720        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3721        ["Treat html" gnus-article-wash-html t]
3722        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3723        ["Decode HZ" gnus-article-decode-HZ t]))
3724
3725     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3726
3727     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3728
3729     (gnus-run-hooks 'gnus-article-menu-hook)))
3730
3731 (defun gnus-article-mode ()
3732   "Major mode for displaying an article.
3733
3734 All normal editing commands are switched off.
3735
3736 The following commands are available in addition to all summary mode
3737 commands:
3738 \\<gnus-article-mode-map>
3739 \\[gnus-article-next-page]\t Scroll the article one page forwards
3740 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3741 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3742 \\[gnus-article-show-summary]\t Display the summary buffer
3743 \\[gnus-article-mail]\t Send a reply to the address near point
3744 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3745 \\[gnus-info-find-node]\t Go to the Gnus info node"
3746   (interactive)
3747   (gnus-simplify-mode-line)
3748   (setq mode-name "Article")
3749   (setq major-mode 'gnus-article-mode)
3750   (make-local-variable 'minor-mode-alist)
3751   (unless (assq 'gnus-show-mime minor-mode-alist)
3752     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3753   (use-local-map gnus-article-mode-map)
3754   (when (gnus-visual-p 'article-menu 'menu)
3755     (gnus-article-make-menu-bar)
3756     (when gnus-summary-tool-bar-map
3757       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
3758   (gnus-update-format-specifications nil 'article-mode)
3759   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3760   (make-local-variable 'gnus-page-broken)
3761   (make-local-variable 'gnus-button-marker-list)
3762   (make-local-variable 'gnus-article-current-summary)
3763   (make-local-variable 'gnus-article-mime-handles)
3764   (make-local-variable 'gnus-article-decoded-p)
3765   (make-local-variable 'gnus-article-mime-handle-alist)
3766   (make-local-variable 'gnus-article-wash-types)
3767   (make-local-variable 'gnus-article-image-alist)
3768   (make-local-variable 'gnus-article-charset)
3769   (make-local-variable 'gnus-article-ignored-charsets)
3770   (gnus-set-default-directory)
3771   (buffer-disable-undo)
3772   (setq buffer-read-only t)
3773   (set-syntax-table gnus-article-mode-syntax-table)
3774   (gnus-run-hooks 'gnus-article-mode-hook))
3775
3776 (defun gnus-article-setup-buffer ()
3777   "Initialize the article buffer."
3778   (let* ((name (if gnus-single-article-buffer "*Article*"
3779                  (concat "*Article " gnus-newsgroup-name "*")))
3780          (original
3781           (progn (string-match "\\*Article" name)
3782                  (concat " *Original Article"
3783                          (substring name (match-end 0))))))
3784     (setq gnus-article-buffer name)
3785     (setq gnus-original-article-buffer original)
3786     (setq gnus-article-mime-handle-alist nil)
3787     ;; This might be a variable local to the summary buffer.
3788     (unless gnus-single-article-buffer
3789       (save-excursion
3790         (set-buffer gnus-summary-buffer)
3791         (setq gnus-article-buffer name)
3792         (setq gnus-original-article-buffer original)
3793         (gnus-set-global-variables)))
3794     (gnus-article-setup-highlight-words)
3795     ;; Init original article buffer.
3796     (save-excursion
3797       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3798       (set-buffer-multibyte t)
3799       (setq major-mode 'gnus-original-article-mode)
3800       (make-local-variable 'gnus-original-article))
3801     (if (get-buffer name)
3802         (save-excursion
3803           (set-buffer name)
3804           (when (and gnus-article-edit-mode
3805                      (buffer-modified-p)
3806                      (not
3807                       (y-or-n-p "Article mode edit in progress; discard? ")))
3808             (error "Action aborted"))
3809           (set (make-local-variable 'gnus-article-edit-mode) nil)
3810           (buffer-disable-undo)
3811           (setq buffer-read-only t)
3812           ;; This list just keeps growing if we don't reset it.
3813           (setq gnus-button-marker-list nil)
3814           (unless (eq major-mode 'gnus-article-mode)
3815             (gnus-article-mode))
3816           (current-buffer))
3817       (save-excursion
3818         (set-buffer (gnus-get-buffer-create name))
3819         (gnus-article-mode)
3820         (make-local-variable 'gnus-summary-buffer)
3821         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3822         (current-buffer)))))
3823
3824 ;; Set article window start at LINE, where LINE is the number of lines
3825 ;; from the head of the article.
3826 (defun gnus-article-set-window-start (&optional line)
3827   (set-window-start
3828    (gnus-get-buffer-window gnus-article-buffer t)
3829    (save-excursion
3830      (set-buffer gnus-article-buffer)
3831      (goto-char (point-min))
3832      (if (not line)
3833          (point-min)
3834        (gnus-message 6 "Moved to bookmark")
3835        (search-forward "\n\n" nil t)
3836        (forward-line line)
3837        (point)))))
3838
3839 ;;; @@ article filters
3840 ;;;
3841
3842 (defun gnus-article-display-mime-message ()
3843   "Article display method for MIME message."
3844   ;; called from `gnus-original-article-buffer'.
3845   (let (charset all-headers)
3846     (with-current-buffer gnus-summary-buffer
3847       (setq charset default-mime-charset
3848             all-headers gnus-have-all-headers))
3849     (make-local-variable 'default-mime-charset)
3850     (setq default-mime-charset charset)
3851     (with-current-buffer (get-buffer-create gnus-article-buffer)
3852       (make-local-variable 'default-mime-charset)
3853       (setq default-mime-charset charset))
3854     (mime-display-message mime-message-structure
3855                           gnus-article-buffer nil gnus-article-mode-map)
3856     (when all-headers
3857       (gnus-article-hide-headers nil -1)))
3858   (run-hooks 'gnus-mime-article-prepare-hook))
3859
3860 (defun gnus-article-display-traditional-message ()
3861   "Article display method for traditional message."
3862   (set-buffer gnus-article-buffer)
3863   (let (buffer-read-only)
3864     (erase-buffer)
3865     (insert-buffer-substring gnus-original-article-buffer)))
3866
3867 (defun gnus-article-make-full-mail-header (&optional number charset)
3868   "Create a new mail header structure in a raw article buffer."
3869   (unless (and number charset)
3870     (save-current-buffer
3871       (set-buffer gnus-summary-buffer)
3872       (unless number
3873         (setq number (or (cdr gnus-article-current) 0)))
3874       (unless charset
3875         (setq charset (or default-mime-charset 'x-ctext)))))
3876   (goto-char (point-min))
3877   (let ((header-end (if (search-forward "\n\n" nil t)
3878                         (1- (point))
3879                       (goto-char (point-max))))
3880         (chars (- (point-max) (point)))
3881         (lines (count-lines (point) (point-max)))
3882         (default-mime-charset charset)
3883         xref)
3884     (narrow-to-region (point-min) header-end)
3885     (setq xref (std11-fetch-field "xref"))
3886     (prog1
3887         (make-full-mail-header
3888          number
3889          (std11-fetch-field "subject")
3890          (std11-fetch-field "from")
3891          (std11-fetch-field "date")
3892          (std11-fetch-field "message-id")
3893          (std11-fetch-field "references")
3894          chars
3895          lines
3896          (when xref (concat "Xref: " xref)))
3897       (widen))))
3898
3899 (defun gnus-article-prepare (article &optional all-headers header)
3900   "Prepare ARTICLE in article mode buffer.
3901 ARTICLE should either be an article number or a Message-ID.
3902 If ARTICLE is an id, HEADER should be the article headers.
3903 If ALL-HEADERS is non-nil, no headers are hidden."
3904   (save-excursion
3905     ;; Make sure we start in a summary buffer.
3906     (unless (eq major-mode 'gnus-summary-mode)
3907       (set-buffer gnus-summary-buffer))
3908     (setq gnus-summary-buffer (current-buffer))
3909     (let* ((gnus-article (if header (mail-header-number header) article))
3910            (summary-buffer (current-buffer))
3911            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3912            (group gnus-newsgroup-name)
3913            result)
3914       (save-excursion
3915         (gnus-article-setup-buffer)
3916         (set-buffer gnus-article-buffer)
3917         ;; Deactivate active regions.
3918         (when (and (boundp 'transient-mark-mode)
3919                    transient-mark-mode)
3920           (setq mark-active nil))
3921         (if (not (setq result (let ((buffer-read-only nil))
3922                                 (gnus-request-article-this-buffer
3923                                  article group))))
3924             ;; There is no such article.
3925             (save-excursion
3926               (when (and (numberp article)
3927                          (not (memq article gnus-newsgroup-sparse)))
3928                 (setq gnus-article-current
3929                       (cons gnus-newsgroup-name article))
3930                 (set-buffer gnus-summary-buffer)
3931                 (setq gnus-current-article article)
3932                 (if (and (memq article gnus-newsgroup-undownloaded)
3933                          (not (gnus-online (gnus-find-method-for-group
3934                                             gnus-newsgroup-name))))
3935                     (progn
3936                       (gnus-summary-set-agent-mark article)
3937                       (message "Message marked for downloading"))
3938                   (gnus-summary-mark-article article gnus-canceled-mark)
3939                   (unless (memq article gnus-newsgroup-sparse)
3940                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3941           (if (or (eq result 'pseudo)
3942                   (eq result 'nneething))
3943               (progn
3944                 (save-excursion
3945                   (set-buffer summary-buffer)
3946                   (push article gnus-newsgroup-history)
3947                   (setq gnus-last-article gnus-current-article
3948                         gnus-current-article 0
3949                         gnus-current-headers nil
3950                         gnus-article-current nil)
3951                   (if (eq result 'nneething)
3952                       (gnus-configure-windows 'summary)
3953                     (gnus-configure-windows 'article))
3954                   (gnus-set-global-variables))
3955                 (let ((gnus-article-mime-handle-alist-1
3956                        gnus-article-mime-handle-alist))
3957                   (gnus-set-mode-line 'article)))
3958             ;; The result from the `request' was an actual article -
3959             ;; or at least some text that is now displayed in the
3960             ;; article buffer.
3961             (when (and (numberp article)
3962                        (not (eq article gnus-current-article)))
3963               ;; Seems like a new article has been selected.
3964               ;; `gnus-current-article' must be an article number.
3965               (save-excursion
3966                 (set-buffer summary-buffer)
3967                 (push article gnus-newsgroup-history)
3968                 (setq gnus-last-article gnus-current-article
3969                       gnus-current-article article
3970                       gnus-current-headers
3971                       (gnus-summary-article-header gnus-current-article)
3972                       gnus-article-current
3973                       (cons gnus-newsgroup-name gnus-current-article))
3974                 (unless (vectorp gnus-current-headers)
3975                   (setq gnus-current-headers nil))
3976                 (gnus-summary-goto-subject gnus-current-article)
3977                 (when (gnus-summary-show-thread)
3978                   ;; If the summary buffer really was folded, the
3979                   ;; previous goto may not actually have gone to
3980                   ;; the right article, but the thread root instead.
3981                   ;; So we go again.
3982                   (gnus-summary-goto-subject gnus-current-article))
3983                 (gnus-run-hooks 'gnus-mark-article-hook)
3984                 (gnus-set-mode-line 'summary)
3985                 (when (gnus-visual-p 'article-highlight 'highlight)
3986                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3987                 ;; Set the global newsgroup variables here.
3988                 (gnus-set-global-variables)
3989                 (setq gnus-have-all-headers
3990                       (or all-headers gnus-show-all-headers))))
3991             (save-excursion
3992               (gnus-configure-windows 'article))
3993             (when (or (numberp article)
3994                       (stringp article))
3995               (gnus-article-prepare-display)
3996               ;; Do page break.
3997               (goto-char (point-min))
3998               (setq gnus-page-broken
3999                     (when gnus-break-pages
4000                       (gnus-narrow-to-page)
4001                       t)))
4002             (let ((gnus-article-mime-handle-alist-1
4003                    gnus-article-mime-handle-alist))
4004               (gnus-set-mode-line 'article))
4005             (article-goto-body)
4006             (unless (bobp)
4007               (forward-line -1))
4008             (set-window-point (get-buffer-window (current-buffer)) (point))
4009             (gnus-configure-windows 'article)
4010             t))))))
4011
4012 (defun gnus-article-prepare-mime-display (&optional number)
4013   (goto-char (point-min))
4014   (when (re-search-forward "^[^\t ]+:" nil t)
4015     (goto-char (match-beginning 0)))
4016   (let ((entity (if (eq 1 (point-min))
4017                     (get-text-property 1 'mime-view-entity)
4018                   (get-text-property (point) 'mime-view-entity)))
4019         last-entity child-entity next type)
4020     (setq child-entity (mime-entity-children entity))
4021     (if child-entity
4022         (setq last-entity (nth (1- (length child-entity))
4023                                child-entity))
4024       (setq last-entity entity))
4025     (save-restriction
4026       (narrow-to-region (point)
4027                         (if (search-forward "\n\n" nil t)
4028                             (point)
4029                           (point-max)))
4030       (gnus-treat-article 'head)
4031       (put-text-property (point-min) (point-max) 'article-treated-header t)
4032       (goto-char (point-max)))
4033     (while (and (not (eobp)) entity)
4034       (setq next (set-marker
4035                   (make-marker)
4036                   (next-single-property-change (point) 'mime-view-entity
4037                                                nil (point-max))))
4038       (let ((types (mime-entity-content-type entity)))
4039         (while (eq 'multipart (mime-content-type-primary-type types))
4040           (setq entity (car (mime-entity-children entity))
4041                 types (mime-entity-content-type entity)))
4042         (when types
4043           (setq type (format "%s/%s"
4044                              (mime-content-type-primary-type types)
4045                              (mime-content-type-subtype types)))))
4046       (if (string-equal type "message/rfc822")
4047           (progn
4048             (setq next (point))
4049             (let ((children (mime-entity-children entity))
4050                   last-children)
4051               (when children
4052                 (setq last-children (nth (1- (length children)) children))
4053                 (while
4054                     (and
4055                      (not (eq last-children
4056                               (get-text-property next 'mime-view-entity)))
4057                      (setq next
4058                            (next-single-property-change next
4059                                                         'mime-view-entity
4060                                                         nil (point-max)))))))
4061             (setq next (next-single-property-change next 'mime-view-entity
4062                                                     nil (point-max)))
4063             (save-restriction
4064               (narrow-to-region (point) next)
4065               (gnus-article-prepare-mime-display)
4066               (goto-char (point-max)))
4067             (setq entity (get-text-property (point) 'mime-view-entity)))
4068         (save-restriction
4069           (narrow-to-region (point) next)
4070           ;; Kludge. We have to count true number, but for now,
4071           ;; part number is here only to achieve `last'.
4072           (gnus-treat-article nil 1
4073                               (if (eq entity last-entity)
4074                                   1 2)
4075                               type)
4076           (goto-char (point-max)))
4077         (setq entity (get-text-property next 'mime-view-entity))))))
4078
4079 ;;;###autoload
4080 (defun gnus-article-prepare-display ()
4081   "Make the current buffer look like a nice article."
4082   (let ((gnus-article-buffer (current-buffer))
4083         buffer-read-only)
4084     (unless (eq major-mode 'gnus-article-mode)
4085       (gnus-article-mode))
4086     (setq buffer-read-only nil
4087           gnus-button-marker-list nil
4088           gnus-article-wash-types nil
4089           gnus-article-image-alist nil)
4090     (save-restriction
4091       (widen)
4092       (static-if (featurep 'xemacs)
4093           (map-extents (lambda (extent maparg) (delete-extent extent)))
4094         (let ((lists (overlay-lists)))
4095           (dolist (overlay (nconc (car lists) (cdr lists)))
4096             (delete-overlay overlay)))))
4097     (gnus-run-hooks 'gnus-tmp-internal-hook))
4098   (let ((show-mime (unless (member gnus-newsgroup-name '("nndraft:delayed"
4099                                                          "nndraft:drafts"))
4100                      gnus-show-mime))
4101         (inhibit-read-only t))
4102     (set-buffer gnus-original-article-buffer)
4103     ;; Display message.
4104     (setq mime-message-structure gnus-current-headers)
4105     (mime-buffer-entity-set-buffer-internal mime-message-structure
4106                                             gnus-original-article-buffer)
4107     (mime-entity-set-representation-type-internal mime-message-structure
4108                                                   'mime-buffer-entity)
4109     (luna-send mime-message-structure 'initialize-instance
4110                mime-message-structure)
4111     (if show-mime
4112         (let (mime-display-header-hook mime-display-text/plain-hook)
4113           (funcall gnus-article-display-method-for-mime))
4114       (funcall gnus-article-display-method-for-traditional))
4115     ;; Call the treatment functions.
4116     (save-restriction
4117       (widen)
4118       (if show-mime
4119           (gnus-article-prepare-mime-display)
4120         (narrow-to-region (goto-char (point-min))
4121                           (if (search-forward "\n\n" nil t)
4122                               (point)
4123                             (point-max)))
4124         (gnus-treat-article 'head)
4125         (put-text-property (point-min) (point-max) 'article-treated-header t)
4126         (goto-char (point-max))
4127         (widen)
4128         (narrow-to-region (point) (point-max))
4129         (gnus-treat-article nil))
4130       (put-text-property (point-min) (point-max) 'read-only nil)))
4131   (gnus-run-hooks 'gnus-article-prepare-hook))
4132
4133 (defun gnus-article-decode-article-as-default-mime-charset ()
4134   "Decode an article as `default-mime-charset'.  It won't work if the
4135 value of the variable `gnus-show-mime' is non-nil."
4136   (unless (or gnus-show-mime
4137               (member gnus-newsgroup-name '("nndraft:delayed"
4138                                             "nndraft:drafts")))
4139     (set (make-local-variable 'default-mime-charset)
4140          (with-current-buffer gnus-summary-buffer
4141            default-mime-charset))
4142     (decode-mime-charset-region (point-min) (point-max)
4143                                 default-mime-charset)))
4144
4145 ;;;
4146 ;;; Gnus MIME viewing functions
4147 ;;;
4148
4149 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4150   "Format of the MIME buttons.
4151
4152 Valid specifiers include:
4153 %t  The MIME type
4154 %T  MIME type, along with additional info
4155 %n  The `name' parameter
4156 %d  The description, if any
4157 %l  The length of the encoded part
4158 %p  The part identifier number
4159 %e  Dots if the part isn't displayed
4160
4161 General format specifiers can also be used.  See Info node
4162 `(gnus)Formatting Variables'.")
4163
4164 (defvar gnus-mime-button-line-format-alist
4165   '((?t gnus-tmp-type ?s)
4166     (?T gnus-tmp-type-long ?s)
4167     (?n gnus-tmp-name ?s)
4168     (?d gnus-tmp-description ?s)
4169     (?p gnus-tmp-id ?s)
4170     (?l gnus-tmp-length ?d)
4171     (?e gnus-tmp-dots ?s)))
4172
4173 (defvar gnus-mime-button-commands
4174   '((gnus-article-press-button "\r" "Toggle Display")
4175     (gnus-mime-view-part "v" "View Interactively...")
4176     (gnus-mime-view-part-as-type "t" "View As Type...")
4177     (gnus-mime-view-part-as-charset "C" "View As charset...")
4178     (gnus-mime-save-part "o" "Save...")
4179     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4180     (gnus-mime-delete-part "d" "Delete part")
4181     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4182     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4183     (gnus-mime-view-part-internally "E" "View Internally")
4184     (gnus-mime-view-part-externally "e" "View Externally")
4185     (gnus-mime-print-part "p" "Print")
4186     (gnus-mime-pipe-part "|" "Pipe To Command...")
4187     (gnus-mime-action-on-part "." "Take action on the part...")))
4188
4189 (defun gnus-article-mime-part-status ()
4190   (with-current-buffer gnus-article-buffer
4191     (let ((entity (get-text-property (point-min) 'mime-view-entity))
4192           children)
4193       (if (and entity
4194                (setq children (mime-entity-children entity))
4195                (setq children (length children)))
4196           (if (eq 1 children)
4197               " (1 part)"
4198             (format " (%d parts)" children))
4199         ""))))
4200
4201 (defvar gnus-mime-button-map
4202   (let ((map (make-sparse-keymap)))
4203     (unless (>= (string-to-number emacs-version) 21)
4204       ;; XEmacs doesn't care.
4205       (set-keymap-parent map gnus-article-mode-map))
4206     (define-key map gnus-mouse-2 'gnus-article-push-button)
4207     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4208     (dolist (c gnus-mime-button-commands)
4209       (define-key map (cadr c) (car c)))
4210     map))
4211
4212 (easy-menu-define
4213   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4214   `("MIME Part"
4215     ,@(mapcar (lambda (c)
4216                 (vector (caddr c) (car c) :enable t))
4217               gnus-mime-button-commands)))
4218
4219 (eval-when-compile
4220   (define-compiler-macro popup-menu (&whole form
4221                                             menu &optional position prefix)
4222     (if (and (fboundp 'popup-menu)
4223              (not (memq 'popup-menu (assoc "lmenu" load-history))))
4224         form
4225       ;; Gnus is probably running under Emacs 20.
4226       `(let* ((menu (cdr ,menu))
4227               (response (x-popup-menu
4228                          t (list (car menu)
4229                                  (cons "" (mapcar (lambda (c)
4230                                                     (cons (caddr c) (car c)))
4231                                                   (cdr menu)))))))
4232          (if response
4233              (call-interactively (nth 3 (assq response menu))))))))
4234
4235 (defun gnus-mime-button-menu (event prefix)
4236  "Construct a context-sensitive menu of MIME commands."
4237  (interactive "e\nP")
4238  (save-window-excursion
4239    (let ((pos (event-start event)))
4240      (select-window (posn-window pos))
4241      (goto-char (posn-point pos))
4242      (gnus-article-check-buffer)
4243      (popup-menu gnus-mime-button-menu nil prefix))))
4244
4245 (defun gnus-mime-view-all-parts (&optional handles)
4246   "View all the MIME parts."
4247   (interactive)
4248   (save-current-buffer
4249     (set-buffer gnus-article-buffer)
4250     (let ((handles (or handles gnus-article-mime-handles))
4251           (mail-parse-charset gnus-newsgroup-charset)
4252           (mail-parse-ignored-charsets
4253            (with-current-buffer gnus-summary-buffer
4254              gnus-newsgroup-ignored-charsets)))
4255       (when handles
4256         (mm-remove-parts handles)
4257         (goto-char (point-min))
4258         (or (search-forward "\n\n") (goto-char (point-max)))
4259         (let (buffer-read-only)
4260           (delete-region (point) (point-max))
4261           (mm-display-parts handles))))))
4262
4263 (defun gnus-mime-save-part-and-strip ()
4264   "Save the MIME part under point then replace it with an external body."
4265   (interactive)
4266   (gnus-article-check-buffer)
4267   (let* ((data (get-text-property (point) 'gnus-data))
4268          file param
4269          (handles gnus-article-mime-handles))
4270     (if (mm-multiple-handles gnus-article-mime-handles)
4271         (error "This function is not implemented"))
4272     (setq file (and data (mm-save-part data)))
4273     (when file
4274       (with-current-buffer (mm-handle-buffer data)
4275         (erase-buffer)
4276         (insert "Content-Type: " (mm-handle-media-type data))
4277         (mml-insert-parameter-string (cdr (mm-handle-type data))
4278                                      '(charset))
4279         (insert "\n")
4280         (insert "Content-ID: " (message-make-message-id) "\n")
4281         (insert "Content-Transfer-Encoding: binary\n")
4282         (insert "\n"))
4283       (setcdr data
4284               (cdr (mm-make-handle nil
4285                                    `("message/external-body"
4286                                      (access-type . "LOCAL-FILE")
4287                                      (name . ,file)))))
4288       (set-buffer gnus-summary-buffer)
4289       (gnus-article-edit-article
4290        `(lambda ()
4291           (erase-buffer)
4292           (let ((mail-parse-charset (or gnus-article-charset
4293                                         ',gnus-newsgroup-charset))
4294                 (mail-parse-ignored-charsets
4295                  (or gnus-article-ignored-charsets
4296                      ',gnus-newsgroup-ignored-charsets))
4297                 (mbl mml-buffer-list))
4298             (setq mml-buffer-list nil)
4299             (insert-buffer gnus-original-article-buffer)
4300             (mime-to-mml ',handles)
4301             (setq gnus-article-mime-handles nil)
4302             (let ((mbl1 mml-buffer-list))
4303               (setq mml-buffer-list mbl)
4304               (set (make-local-variable 'mml-buffer-list) mbl1))
4305             (gnus-make-local-hook 'kill-buffer-hook)
4306             (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4307        `(lambda (no-highlight)
4308           (let ((mail-parse-charset (or gnus-article-charset
4309                                         ',gnus-newsgroup-charset))
4310                 (message-options message-options)
4311                 (message-options-set-recipient)
4312                 (mail-parse-ignored-charsets
4313                  (or gnus-article-ignored-charsets
4314                      ',gnus-newsgroup-ignored-charsets)))
4315             (mml-to-mime)
4316             (mml-destroy-buffers)
4317             (remove-hook 'kill-buffer-hook
4318                          'mml-destroy-buffers t)
4319             (kill-local-variable 'mml-buffer-list))
4320           (gnus-summary-edit-article-done
4321            ,(or (mail-header-references gnus-current-headers) "")
4322            ,(gnus-group-read-only-p)
4323            ,gnus-summary-buffer no-highlight))))))
4324
4325 (defun gnus-mime-delete-part ()
4326   "Delete the MIME part under point.
4327 Replace it with some information about the removed part."
4328   (interactive)
4329   (gnus-article-check-buffer)
4330   (unless (and gnus-novice-user
4331                (not (gnus-yes-or-no-p
4332                      "Really delete attachment forever? ")))
4333     (let* ((data (get-text-property (point) 'gnus-data))
4334            (handles gnus-article-mime-handles)
4335            (none "(none)")
4336            (description
4337             (or
4338              (mail-decode-encoded-word-string (or (mm-handle-description data)
4339                                                   none))))
4340            (filename
4341             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4342                 none))
4343            (type (mm-handle-media-type data)))
4344       (if (mm-multiple-handles gnus-article-mime-handles)
4345           (error "This function is not implemented"))
4346       (with-current-buffer (mm-handle-buffer data)
4347         (let ((bsize (format "%s" (buffer-size))))
4348           (erase-buffer)
4349           (insert
4350            (concat
4351             "<#part type=text/plain nofile=yes disposition=attachment"
4352             " description=\"Deleted attachment (" bsize " Byte)\">"
4353             ",----\n"
4354             "| The following attachment has been deleted:\n"
4355             "|\n"
4356             "| Type:           " type "\n"
4357             "| Filename:       " filename "\n"
4358             "| Size (encoded): " bsize " Byte\n"
4359             "| Description:    " description "\n"
4360             "`----\n"
4361             "<#/part>"))
4362           (setcdr data
4363                   (cdr (mm-make-handle nil `("text/plain"))))))
4364       (set-buffer gnus-summary-buffer)
4365       ;; FIXME: maybe some of the following code (borrowed from
4366       ;; `gnus-mime-save-part-and-strip') isn't necessary?
4367       (gnus-article-edit-article
4368        `(lambda ()
4369           (erase-buffer)
4370           (let ((mail-parse-charset (or gnus-article-charset
4371                                         ',gnus-newsgroup-charset))
4372                 (mail-parse-ignored-charsets
4373                  (or gnus-article-ignored-charsets
4374                      ',gnus-newsgroup-ignored-charsets))
4375                 (mbl mml-buffer-list))
4376             (setq mml-buffer-list nil)
4377             (insert-buffer gnus-original-article-buffer)
4378             (mime-to-mml ',handles)
4379             (setq gnus-article-mime-handles nil)
4380             (let ((mbl1 mml-buffer-list))
4381               (setq mml-buffer-list mbl)
4382               (set (make-local-variable 'mml-buffer-list) mbl1))
4383             (gnus-make-local-hook 'kill-buffer-hook)
4384             (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4385        `(lambda (no-highlight)
4386           (let ((mail-parse-charset (or gnus-article-charset
4387                                         ',gnus-newsgroup-charset))
4388                 (message-options message-options)
4389                 (message-options-set-recipient)
4390                 (mail-parse-ignored-charsets
4391                  (or gnus-article-ignored-charsets
4392                      ',gnus-newsgroup-ignored-charsets)))
4393             (mml-to-mime)
4394             (mml-destroy-buffers)
4395             (remove-hook 'kill-buffer-hook
4396                          'mml-destroy-buffers t)
4397             (kill-local-variable 'mml-buffer-list))
4398           (gnus-summary-edit-article-done
4399            ,(or (mail-header-references gnus-current-headers) "")
4400            ,(gnus-group-read-only-p)
4401            ,gnus-summary-buffer no-highlight)))))
4402   ;; Not in `gnus-mime-save-part-and-strip':
4403   (gnus-article-edit-done)
4404   (gnus-summary-expand-window)
4405   (gnus-summary-show-article))
4406
4407 (defun gnus-mime-save-part ()
4408   "Save the MIME part under point."
4409   (interactive)
4410   (gnus-article-check-buffer)
4411   (let ((data (get-text-property (point) 'gnus-data)))
4412     (when data
4413       (mm-save-part data))))
4414
4415 (defun gnus-mime-pipe-part ()
4416   "Pipe the MIME part under point to a process."
4417   (interactive)
4418   (gnus-article-check-buffer)
4419   (let ((data (get-text-property (point) 'gnus-data)))
4420     (when data
4421       (mm-pipe-part data))))
4422
4423 (defun gnus-mime-view-part ()
4424   "Interactively choose a viewing method for the MIME part under point."
4425   (interactive)
4426   (gnus-article-check-buffer)
4427   (let ((data (get-text-property (point) 'gnus-data)))
4428     (when data
4429       (setq gnus-article-mime-handles
4430             (mm-merge-handles
4431              gnus-article-mime-handles (setq data (copy-sequence data))))
4432       (mm-interactively-view-part data))))
4433
4434 (defun gnus-mime-view-part-as-type-internal ()
4435   (gnus-article-check-buffer)
4436   (let* ((name (mail-content-type-get
4437                 (mm-handle-type (get-text-property (point) 'gnus-data))
4438                 'name))
4439          (def-type (and name (mm-default-file-encoding name))))
4440     (and def-type (cons def-type 0))))
4441
4442 (defun gnus-mime-view-part-as-type (&optional mime-type)
4443   "Choose a MIME media type, and view the part as such."
4444   (interactive)
4445   (unless mime-type
4446     (setq mime-type (completing-read
4447                      "View as MIME type: "
4448                      (mapcar #'list (mailcap-mime-types))
4449                      nil nil
4450                      (gnus-mime-view-part-as-type-internal))))
4451   (gnus-article-check-buffer)
4452   (let ((handle (get-text-property (point) 'gnus-data)))
4453     (when handle
4454       (setq handle
4455             (mm-make-handle (mm-handle-buffer handle)
4456                             (cons mime-type (cdr (mm-handle-type handle)))
4457                             (mm-handle-encoding handle)
4458                             (mm-handle-undisplayer handle)
4459                             (mm-handle-disposition handle)
4460                             (mm-handle-description handle)
4461                             nil
4462                             (mm-handle-id handle)))
4463       (setq gnus-article-mime-handles
4464             (mm-merge-handles gnus-article-mime-handles handle))
4465       (gnus-mm-display-part handle))))
4466
4467 (eval-when-compile
4468   (require 'jka-compr))
4469
4470 ;; jka-compr.el uses a "sh -c" to direct stderr to err-file, but these days
4471 ;; emacs can do that itself.
4472 ;;
4473 (defun gnus-mime-jka-compr-maybe-uncompress ()
4474   "Uncompress the current buffer if `auto-compression-mode' is enabled.
4475 The uncompress method used is derived from `buffer-file-name'."
4476   (when (and (fboundp 'jka-compr-installed-p)
4477              (jka-compr-installed-p))
4478     (let ((info (jka-compr-get-compression-info buffer-file-name)))
4479       (when info
4480         (let ((basename (file-name-nondirectory buffer-file-name))
4481               (args     (jka-compr-info-uncompress-args    info))
4482               (prog     (jka-compr-info-uncompress-program info))
4483               (message  (jka-compr-info-uncompress-message info))
4484               (err-file (jka-compr-make-temp-name)))
4485           (if message
4486               (message "%s %s..." message basename))
4487           (unwind-protect
4488               (unless (memq (apply 'call-process-region
4489                                    (point-min) (point-max) 
4490                                    prog
4491                                    t (list t err-file) nil
4492                                    args)
4493                             jka-compr-acceptable-retval-list)
4494                 (jka-compr-error prog args basename message err-file))
4495             (jka-compr-delete-temp-file err-file)))))))
4496
4497 (defun gnus-mime-copy-part (&optional handle)
4498   "Put the MIME part under point into a new buffer.
4499 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4500 are decompressed."
4501   (interactive)
4502   (gnus-article-check-buffer)
4503   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4504          (contents (and handle (mm-get-part handle)))
4505          (base (and handle
4506                     (file-name-nondirectory
4507                      (or
4508                       (mail-content-type-get (mm-handle-type handle) 'name)
4509                       (mail-content-type-get (mm-handle-disposition handle)
4510                                              'filename)
4511                       "*decoded*"))))
4512          (buffer (and base (generate-new-buffer base))))
4513     (when contents
4514       (switch-to-buffer buffer)
4515       (insert contents)
4516       ;; We do it this way to make `normal-mode' set the appropriate mode.
4517       (unwind-protect
4518           (progn
4519             (setq buffer-file-name (expand-file-name base))
4520             (gnus-mime-jka-compr-maybe-uncompress)
4521             (normal-mode))
4522         (setq buffer-file-name nil))
4523       (goto-char (point-min)))))
4524
4525 (defun gnus-mime-print-part (&optional handle filename)
4526   "Print the MIME part under point."
4527   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4528   (gnus-article-check-buffer)
4529   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4530          (contents (and handle (mm-get-part handle)))
4531          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4532          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
4533     (when contents
4534         (if printer
4535             (unwind-protect
4536                 (progn
4537                   (mm-save-part-to-file handle file)
4538                   (call-process shell-file-name nil
4539                                 (generate-new-buffer " *mm*")
4540                                 nil
4541                                 shell-command-switch
4542                                 (mm-mailcap-command
4543                                  printer file (mm-handle-type handle))))
4544               (delete-file file))
4545           (with-temp-buffer
4546             (insert contents)
4547             (gnus-print-buffer))
4548           (ps-despool filename)))))
4549
4550 (defun gnus-mime-inline-part (&optional handle arg)
4551   "Insert the MIME part under point into the current buffer."
4552   (interactive (list nil current-prefix-arg))
4553   (gnus-article-check-buffer)
4554   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4555          contents charset
4556          (b (point))
4557          buffer-read-only)
4558     (when handle
4559       (if (and (not arg) (mm-handle-undisplayer handle))
4560           (mm-remove-part handle)
4561         (setq contents (mm-get-part handle))
4562         (cond
4563          ((not arg)
4564           (setq charset (or (mail-content-type-get
4565                              (mm-handle-type handle) 'charset)
4566                             gnus-newsgroup-charset)))
4567          ((numberp arg)
4568           (if (mm-handle-undisplayer handle)
4569               (mm-remove-part handle))
4570           (setq charset
4571                 (or (cdr (assq arg
4572                                gnus-summary-show-article-charset-alist))
4573                     (mm-read-coding-system "Charset: ")))))
4574         (forward-line 2)
4575         (mm-insert-inline handle
4576                           (if (and charset
4577                                    (setq charset (mm-charset-to-coding-system
4578                                                   charset))
4579                                    (not (eq charset 'ascii)))
4580                               (mm-decode-coding-string contents charset)
4581                             contents))
4582         (goto-char b)))))
4583
4584 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4585   "Insert the MIME part under point into the current buffer using the
4586 specified charset."
4587   (interactive (list nil current-prefix-arg))
4588   (gnus-article-check-buffer)
4589   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4590          contents charset
4591          (b (point))
4592          buffer-read-only)
4593     (when handle
4594       (if (mm-handle-undisplayer handle)
4595           (mm-remove-part handle))
4596       (let ((gnus-newsgroup-charset
4597              (or (cdr (assq arg
4598                             gnus-summary-show-article-charset-alist))
4599                  (mm-read-coding-system "Charset: ")))
4600             (gnus-newsgroup-ignored-charsets 'gnus-all))
4601         (gnus-article-press-button)))))
4602
4603 (defun gnus-mime-view-part-externally (&optional handle)
4604   "View the MIME part under point with an external viewer."
4605   (interactive)
4606   (gnus-article-check-buffer)
4607   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4608          (mm-user-display-methods nil)
4609          (mm-inlined-types nil)
4610          (mail-parse-charset gnus-newsgroup-charset)
4611          (mail-parse-ignored-charsets
4612           (save-excursion (set-buffer gnus-summary-buffer)
4613                           gnus-newsgroup-ignored-charsets)))
4614     (when handle
4615       (if (mm-handle-undisplayer handle)
4616           (mm-remove-part handle)
4617         (mm-display-part handle)))))
4618
4619 (defun gnus-mime-view-part-internally (&optional handle)
4620   "View the MIME part under point with an internal viewer.
4621 If no internal viewer is available, use an external viewer."
4622   (interactive)
4623   (gnus-article-check-buffer)
4624   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4625          (mm-inlined-types '(".*"))
4626          (mm-inline-large-images t)
4627          (mail-parse-charset gnus-newsgroup-charset)
4628          (mail-parse-ignored-charsets
4629           (save-excursion (set-buffer gnus-summary-buffer)
4630                           gnus-newsgroup-ignored-charsets))
4631          buffer-read-only)
4632     (when handle
4633       (if (mm-handle-undisplayer handle)
4634           (mm-remove-part handle)
4635         (mm-display-part handle)))))
4636
4637 (defun gnus-mime-action-on-part (&optional action)
4638   "Do something with the MIME attachment at \(point\)."
4639   (interactive
4640    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
4641   (gnus-article-check-buffer)
4642   (let ((action-pair (assoc action gnus-mime-action-alist)))
4643     (if action-pair
4644         (funcall (cdr action-pair)))))
4645
4646 (defun gnus-article-part-wrapper (n function)
4647   (save-current-buffer
4648     (set-buffer gnus-article-buffer)
4649     (when (> n (length gnus-article-mime-handle-alist))
4650       (error "No such part"))
4651     (gnus-article-goto-part n)
4652     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4653       (funcall function handle))))
4654
4655 (defun gnus-article-pipe-part (n)
4656   "Pipe MIME part N, which is the numerical prefix."
4657   (interactive "p")
4658   (gnus-article-part-wrapper n 'mm-pipe-part))
4659
4660 (defun gnus-article-save-part (n)
4661   "Save MIME part N, which is the numerical prefix."
4662   (interactive "p")
4663   (gnus-article-part-wrapper n 'mm-save-part))
4664
4665 (defun gnus-article-interactively-view-part (n)
4666   "View MIME part N interactively, which is the numerical prefix."
4667   (interactive "p")
4668   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4669
4670 (defun gnus-article-copy-part (n)
4671   "Copy MIME part N, which is the numerical prefix."
4672   (interactive "p")
4673   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4674
4675 (defun gnus-article-view-part-as-charset (n)
4676   "Copy MIME part N, which is the numerical prefix."
4677   (interactive "p")
4678   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4679
4680 (defun gnus-article-view-part-externally (n)
4681   "View MIME part N externally, which is the numerical prefix."
4682   (interactive "p")
4683   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4684
4685 (defun gnus-article-inline-part (n)
4686   "Inline MIME part N, which is the numerical prefix."
4687   (interactive "p")
4688   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4689
4690 (defun gnus-article-mime-match-handle-first (condition)
4691   (if condition
4692       (let ((alist gnus-article-mime-handle-alist) ihandle n)
4693         (while (setq ihandle (pop alist))
4694           (if (and (cond
4695                     ((functionp condition)
4696                      (funcall condition (cdr ihandle)))
4697                     ((eq condition 'undisplayed)
4698                      (not (or (mm-handle-undisplayer (cdr ihandle))
4699                               (equal (mm-handle-media-type (cdr ihandle))
4700                                      "multipart/alternative"))))
4701                     ((eq condition 'undisplayed-alternative)
4702                      (not (mm-handle-undisplayer (cdr ihandle))))
4703                     (t t))
4704                    (gnus-article-goto-part (car ihandle))
4705                    (or (not n) (< (car ihandle) n)))
4706               (setq n (car ihandle))))
4707         (or n 1))
4708     1))
4709
4710 (defun gnus-article-view-part (&optional n)
4711   "View MIME part N, which is the numerical prefix."
4712   (interactive "P")
4713   (save-current-buffer
4714     (set-buffer gnus-article-buffer)
4715     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4716                              gnus-article-mime-match-handle-function)))
4717     (when (> n (length gnus-article-mime-handle-alist))
4718       (error "No such part"))
4719     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4720       (when (gnus-article-goto-part n)
4721         (if (equal (car handle) "multipart/alternative")
4722             (gnus-article-press-button)
4723           (when (eq (gnus-mm-display-part handle) 'internal)
4724             (gnus-set-window-start)))))))
4725
4726 (defsubst gnus-article-mime-total-parts ()
4727   (if (bufferp (car gnus-article-mime-handles))
4728       1 ;; single part
4729     (1- (length gnus-article-mime-handles))))
4730
4731 (defun gnus-mm-display-part (handle)
4732   "Display HANDLE and fix MIME button."
4733   (let ((id (get-text-property (point) 'gnus-part))
4734         (point (point))
4735         buffer-read-only)
4736     (forward-line 1)
4737     (prog1
4738         (let ((window (selected-window))
4739               (mail-parse-charset gnus-newsgroup-charset)
4740               (mail-parse-ignored-charsets
4741                (if (gnus-buffer-live-p gnus-summary-buffer)
4742                    (save-excursion
4743                      (set-buffer gnus-summary-buffer)
4744                      gnus-newsgroup-ignored-charsets)
4745                  nil)))
4746           (save-excursion
4747             (unwind-protect
4748                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4749                       (beg (point)))
4750                   (when win
4751                     (select-window win))
4752                   (goto-char point)
4753                   (forward-line)
4754                   (if (mm-handle-displayed-p handle)
4755                       ;; This will remove the part.
4756                       (mm-display-part handle)
4757                     (save-restriction
4758                       (narrow-to-region (point)
4759                                         (if (eobp) (point) (1+ (point))))
4760                       (mm-display-part handle)
4761                       ;; We narrow to the part itself and
4762                       ;; then call the treatment functions.
4763                       (goto-char (point-min))
4764                       (forward-line 1)
4765                       (narrow-to-region (point) (point-max))
4766                       (gnus-treat-article
4767                        nil id
4768                        (gnus-article-mime-total-parts)
4769                        (mm-handle-media-type handle)))))
4770               (if (window-live-p window)
4771                   (select-window window)))))
4772       (goto-char point)
4773       (gnus-delete-line)
4774       (gnus-insert-mime-button
4775        handle id (list (mm-handle-displayed-p handle)))
4776       (goto-char point))))
4777
4778 (defun gnus-article-goto-part (n)
4779   "Go to MIME part N."
4780   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
4781
4782 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4783   (let ((gnus-tmp-name
4784          (or (mail-content-type-get (mm-handle-type handle) 'name)
4785              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4786              (mail-content-type-get (mm-handle-type handle) 'url)
4787              ""))
4788         (gnus-tmp-type (mm-handle-media-type handle))
4789         (gnus-tmp-description
4790          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4791                                               "")))
4792         (gnus-tmp-dots
4793          (if (if displayed (car displayed)
4794                (mm-handle-displayed-p handle))
4795              "" "..."))
4796         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4797                            (buffer-size)))
4798         gnus-tmp-type-long b e)
4799     (when (string-match ".*/" gnus-tmp-name)
4800       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4801     (setq gnus-tmp-type-long (concat gnus-tmp-type
4802                                      (and (not (equal gnus-tmp-name ""))
4803                                           (concat "; " gnus-tmp-name))))
4804     (unless (equal gnus-tmp-description "")
4805       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4806     (unless (bolp)
4807       (insert "\n"))
4808     (setq b (point))
4809     (gnus-eval-format
4810      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4811      `(,@(gnus-local-map-property gnus-mime-button-map)
4812          gnus-callback gnus-mm-display-part
4813          gnus-part ,gnus-tmp-id
4814          article-type annotation
4815          gnus-data ,handle))
4816     (setq e (if (bolp)
4817                 ;; Exclude a newline.
4818                 (1- (point))
4819               (point)))
4820     (widget-convert-button
4821      'link b e
4822      :mime-handle handle
4823      :action 'gnus-widget-press-button
4824      :button-keymap gnus-mime-button-map
4825      :help-echo
4826      (lambda (widget/window &optional overlay pos)
4827        ;; Needed to properly clear the message due to a bug in
4828        ;; wid-edit (XEmacs only).
4829        (if (boundp 'help-echo-owns-message)
4830            (setq help-echo-owns-message t))
4831        (format
4832         "%S: %s the MIME part; %S: more options"
4833         (aref gnus-mouse-2 0)
4834         ;; XEmacs will get a single widget arg; Emacs 21 will get
4835         ;; window, overlay, position.
4836         (if (mm-handle-displayed-p
4837              (if overlay
4838                  (with-current-buffer (gnus-overlay-buffer overlay)
4839                    (widget-get (widget-at (gnus-overlay-start overlay))
4840                                :mime-handle))
4841                (widget-get widget/window :mime-handle)))
4842             "hide" "show")
4843         (aref gnus-down-mouse-3 0))))))
4844
4845 (defun gnus-widget-press-button (elems el)
4846   (goto-char (widget-get elems :from))
4847   (gnus-article-press-button))
4848
4849 (defvar gnus-displaying-mime nil)
4850
4851 (defun gnus-display-mime (&optional ihandles)
4852   "Display the MIME parts."
4853   (save-excursion
4854     (save-selected-window
4855       (let ((window (get-buffer-window gnus-article-buffer))
4856             (point (point)))
4857         (when window
4858           (select-window window)
4859           ;; We have to do this since selecting the window
4860           ;; may change the point.  So we set the window point.
4861           (set-window-point window point)))
4862       (let* ((handles (or ihandles
4863                           (mm-dissect-buffer nil gnus-article-loose-mime)
4864                           (and gnus-article-emulate-mime
4865                                (mm-uu-dissect))))
4866              buffer-read-only handle name type b e display)
4867         (when (and (not ihandles)
4868                    (not gnus-displaying-mime))
4869           ;; Top-level call; we clean up.
4870           (when gnus-article-mime-handles
4871             (mm-destroy-parts gnus-article-mime-handles)
4872             (setq gnus-article-mime-handle-alist nil));; A trick.
4873           (setq gnus-article-mime-handles handles)
4874           ;; We allow users to glean info from the handles.
4875           (when gnus-article-mime-part-function
4876             (gnus-mime-part-function handles)))
4877         (if (and handles
4878                  (or (not (stringp (car handles)))
4879                      (cdr handles)))
4880             (progn
4881               (when (and (not ihandles)
4882                          (not gnus-displaying-mime))
4883                 ;; Clean up for mime parts.
4884                 (article-goto-body)
4885                 (delete-region (point) (point-max)))
4886               (let ((gnus-displaying-mime t))
4887                 (gnus-mime-display-part handles)))
4888           (save-restriction
4889             (article-goto-body)
4890             (narrow-to-region (point) (point-max))
4891             (gnus-treat-article nil 1 1)
4892             (widen)))
4893         (unless ihandles
4894           ;; Highlight the headers.
4895           (save-excursion
4896             (save-restriction
4897               (article-goto-body)
4898               (narrow-to-region (point-min) (point))
4899               (gnus-treat-article 'head))))))))
4900
4901 (defcustom gnus-mime-display-multipart-as-mixed nil
4902   "Display \"multipart\" parts as  \"multipart/mixed\".
4903
4904 If t, it overrides nil values of
4905 `gnus-mime-display-multipart-alternative-as-mixed' and
4906 `gnus-mime-display-multipart-related-as-mixed'."
4907   :group 'gnus-article-mime
4908   :type 'boolean)
4909
4910 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
4911   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
4912   :group 'gnus-article-mime
4913   :type 'boolean)
4914
4915 (defcustom gnus-mime-display-multipart-related-as-mixed nil
4916   "Display \"multipart/related\" parts as  \"multipart/mixed\".
4917
4918 If displaying \"text/html\" is discouraged \(see
4919 `mm-discouraged-alternatives'\) images or other material inside a
4920 \"multipart/related\" part might be overlooked when this variable is nil."
4921   :group 'gnus-article-mime
4922   :type 'boolean)
4923
4924 (defun gnus-mime-display-part (handle)
4925   (cond
4926    ;; Single part.
4927    ((not (stringp (car handle)))
4928     (gnus-mime-display-single handle))
4929    ;; User-defined multipart
4930    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4931     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4932              handle))
4933    ;; multipart/alternative
4934    ((and (equal (car handle) "multipart/alternative")
4935          (not (or gnus-mime-display-multipart-as-mixed
4936                   gnus-mime-display-multipart-alternative-as-mixed)))
4937     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4938       (push (cons id handle) gnus-article-mime-handle-alist)
4939       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4940    ;; multipart/related
4941    ((and (equal (car handle) "multipart/related")
4942          (not (or gnus-mime-display-multipart-as-mixed
4943                   gnus-mime-display-multipart-related-as-mixed)))
4944     ;;;!!!We should find the start part, but we just default
4945     ;;;!!!to the first part.
4946     ;;(gnus-mime-display-part (cadr handle))
4947     ;;;!!! Most multipart/related is an HTML message plus images.
4948     ;;;!!! Unfortunately we are unable to let W3 display those
4949     ;;;!!! included images, so we just display it as a mixed multipart.
4950     ;;(gnus-mime-display-mixed (cdr handle))
4951     ;;;!!! No, w3 can display everything just fine.
4952     (gnus-mime-display-part (cadr handle)))
4953    ((equal (car handle) "multipart/signed")
4954     (gnus-add-wash-type 'signed)
4955     (gnus-mime-display-security handle))
4956    ((equal (car handle) "multipart/encrypted")
4957     (gnus-add-wash-type 'encrypted)
4958     (gnus-mime-display-security handle))
4959    ;; Other multiparts are handled like multipart/mixed.
4960    (t
4961     (gnus-mime-display-mixed (cdr handle)))))
4962
4963 (defun gnus-mime-part-function (handles)
4964   (if (stringp (car handles))
4965       (mapcar 'gnus-mime-part-function (cdr handles))
4966     (funcall gnus-article-mime-part-function handles)))
4967
4968 (defun gnus-mime-display-mixed (handles)
4969   (mapcar 'gnus-mime-display-part handles))
4970
4971 (defun gnus-mime-display-single (handle)
4972   (let ((type (mm-handle-media-type handle))
4973         (ignored gnus-ignored-mime-types)
4974         (not-attachment t)
4975         (move nil)
4976         display text)
4977     (catch 'ignored
4978       (progn
4979         (while ignored
4980           (when (string-match (pop ignored) type)
4981             (throw 'ignored nil)))
4982         (if (and (setq not-attachment
4983                        (and (not (mm-inline-override-p handle))
4984                             (or (not (mm-handle-disposition handle))
4985                                 (equal (car (mm-handle-disposition handle))
4986                                        "inline")
4987                                 (mm-attachment-override-p handle))))
4988                  (mm-automatic-display-p handle)
4989                  (or (and
4990                       (mm-inlinable-p handle)
4991                       (mm-inlined-p handle))
4992                      (mm-automatic-external-display-p type)))
4993             (setq display t)
4994           (when (equal (mm-handle-media-supertype handle) "text")
4995             (setq text t)))
4996         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4997               beg)
4998           (push (cons id handle) gnus-article-mime-handle-alist)
4999           (when (or (not display)
5000                     (not (gnus-unbuttonized-mime-type-p type)))
5001             ;(gnus-article-insert-newline)
5002             (gnus-insert-mime-button
5003              handle id (list (or display (and not-attachment text))))
5004             (gnus-article-insert-newline)
5005             ;(gnus-article-insert-newline)
5006             ;; Remember modify the number of forward lines.
5007             (setq move t))
5008           (setq beg (point))
5009           (cond
5010            (display
5011             (when move
5012               (forward-line -1)
5013               (setq beg (point)))
5014             (let ((mail-parse-charset gnus-newsgroup-charset)
5015                   (mail-parse-ignored-charsets
5016                    (save-excursion (condition-case ()
5017                                        (set-buffer gnus-summary-buffer)
5018                                      (error))
5019                                    gnus-newsgroup-ignored-charsets)))
5020               (mm-display-part handle t))
5021             (goto-char (point-max)))
5022            ((and text not-attachment)
5023             (when move
5024               (forward-line -1)
5025               (setq beg (point)))
5026             (gnus-article-insert-newline)
5027             (mm-insert-inline handle (mm-get-part handle))
5028             (goto-char (point-max))))
5029           ;; Do highlighting.
5030           (save-excursion
5031             (save-restriction
5032               (narrow-to-region beg (point))
5033               (gnus-treat-article
5034                nil id
5035                (gnus-article-mime-total-parts)
5036                (mm-handle-media-type handle)))))))))
5037
5038 (defun gnus-unbuttonized-mime-type-p (type)
5039   "Say whether TYPE is to be unbuttonized."
5040   (unless gnus-inhibit-mime-unbuttonizing
5041     (when (catch 'found
5042             (let ((types gnus-unbuttonized-mime-types))
5043               (while types
5044                 (when (string-match (pop types) type)
5045                   (throw 'found t)))))
5046       (not (catch 'found
5047              (let ((types gnus-buttonized-mime-types))
5048                (while types
5049                  (when (string-match (pop types) type)
5050                    (throw 'found t)))))))))
5051
5052 (defun gnus-article-insert-newline ()
5053   "Insert a newline, but mark it as undeletable."
5054   (gnus-put-text-property
5055    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5056
5057 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5058   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5059          (ihandles handles)
5060          (point (point))
5061          handle buffer-read-only from props begend not-pref)
5062     (save-window-excursion
5063       (save-restriction
5064         (when ibegend
5065           (narrow-to-region (car ibegend)
5066                             (or (cdr ibegend)
5067                                 (progn
5068                                   (goto-char (car ibegend))
5069                                   (forward-line 2)
5070                                   (point))))
5071           (delete-region (point-min) (point-max))
5072           (mm-remove-parts handles))
5073         (setq begend (list (point-marker)))
5074         ;; Do the toggle.
5075         (unless (setq not-pref (cadr (member preferred ihandles)))
5076           (setq not-pref (car ihandles)))
5077         (when (or ibegend
5078                   (not preferred)
5079                   (not (gnus-unbuttonized-mime-type-p
5080                         "multipart/alternative")))
5081           (gnus-add-text-properties
5082            (setq from (point))
5083            (progn
5084              (insert (format "%d.  " id))
5085              (point))
5086            `(gnus-callback
5087              (lambda (handles)
5088                (unless ,(not ibegend)
5089                  (setq gnus-article-mime-handle-alist
5090                        ',gnus-article-mime-handle-alist))
5091                (gnus-mime-display-alternative
5092                 ',ihandles ',not-pref ',begend ,id))
5093              ,@(gnus-local-map-property gnus-mime-button-map)
5094              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5095              face ,gnus-article-button-face
5096              gnus-part ,id
5097              gnus-data ,handle))
5098           (widget-convert-button 'link from (point)
5099                                  :action 'gnus-widget-press-button
5100                                  :button-keymap gnus-widget-button-keymap)
5101           ;; Do the handles
5102           (while (setq handle (pop handles))
5103             (gnus-add-text-properties
5104              (setq from (point))
5105              (progn
5106                (insert (format "(%c) %-18s"
5107                                (if (equal handle preferred) ?* ? )
5108                                (mm-handle-media-type handle)))
5109                (point))
5110              `(gnus-callback
5111                (lambda (handles)
5112                  (unless ,(not ibegend)
5113                    (setq gnus-article-mime-handle-alist
5114                          ',gnus-article-mime-handle-alist))
5115                  (gnus-mime-display-alternative
5116                   ',ihandles ',handle ',begend ,id))
5117                ,@(gnus-local-map-property gnus-mime-button-map)
5118                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5119                face ,gnus-article-button-face
5120                gnus-part ,id
5121                gnus-data ,handle))
5122             (widget-convert-button 'link from (point)
5123                                    :action 'gnus-widget-press-button
5124                                    :button-keymap gnus-widget-button-keymap)
5125             (insert "  "))
5126           (insert "\n\n"))
5127         (when preferred
5128           (if (stringp (car preferred))
5129               (gnus-display-mime preferred)
5130             (let ((mail-parse-charset gnus-newsgroup-charset)
5131                   (mail-parse-ignored-charsets
5132                    (save-excursion (set-buffer gnus-summary-buffer)
5133                                    gnus-newsgroup-ignored-charsets)))
5134               (mm-display-part preferred)
5135               ;; Do highlighting.
5136               (save-excursion
5137                 (save-restriction
5138                   (narrow-to-region (car begend) (point-max))
5139                   (gnus-treat-article
5140                    nil (length gnus-article-mime-handle-alist)
5141                    (gnus-article-mime-total-parts)
5142                    (mm-handle-media-type handle))))))
5143           (goto-char (point-max))
5144           (setcdr begend (point-marker)))))
5145     (when ibegend
5146       (goto-char point))))
5147
5148 (defconst gnus-article-wash-status-strings
5149   (let ((alist '((cite "c" "Possible hidden citation text"
5150                        " " "All citation text visible")
5151                  (headers "h" "Hidden headers"
5152                           " " "All headers visible.")
5153                  (pgp "p" "Encrypted or signed message status hidden"
5154                       " " "No hidden encryption nor digital signature status")
5155                  (signature "s" "Signature has been hidden"
5156                             " " "Signature is visible")
5157                  (overstrike "o" "Overstrike (^H) characters applied"
5158                              " " "No overstrike characters applied")
5159                  (gnus-show-mime "m" "Mime processing is activated"
5160                                  " " "Mime processing is not activated")
5161                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5162                            " " "No /*_emphasis_*/ characters applied")))
5163         result)
5164     (dolist (entry alist result)
5165       (let ((key (nth 0 entry))
5166             (on (copy-sequence (nth 1 entry)))
5167             (on-help (nth 2 entry))
5168             (off (copy-sequence (nth 3 entry)))
5169             (off-help (nth 4 entry)))
5170         (put-text-property 0 1 'help-echo on-help on)
5171         (put-text-property 0 1 'help-echo off-help off)
5172         (push (list key on off) result))))
5173   "Alist of strings describing wash status in the mode line.
5174 Each entry has the form (KEY ON OF), where the KEY is a symbol
5175 representing the particular washing function, ON is the string to use
5176 in the article mode line when the washing function is active, and OFF
5177 is the string to use when it is inactive.")
5178
5179 (defun gnus-article-wash-status-entry (key value)
5180   (let ((entry (assoc key gnus-article-wash-status-strings)))
5181     (if value (nth 1 entry) (nth 2 entry))))
5182
5183 (defun gnus-article-wash-status ()
5184   "Return a string which display status of article washing."
5185   (save-excursion
5186     (set-buffer gnus-article-buffer)
5187     (let ((cite (memq 'cite gnus-article-wash-types))
5188           (headers (memq 'headers gnus-article-wash-types))
5189           (boring (memq 'boring-headers gnus-article-wash-types))
5190           (pgp (memq 'pgp gnus-article-wash-types))
5191           (pem (memq 'pem gnus-article-wash-types))
5192           (signed (memq 'signed gnus-article-wash-types))
5193           (encrypted (memq 'encrypted gnus-article-wash-types))
5194           (signature (memq 'signature gnus-article-wash-types))
5195           (overstrike (memq 'overstrike gnus-article-wash-types))
5196           (emphasis (memq 'emphasis gnus-article-wash-types)))
5197       (concat
5198        (gnus-article-wash-status-entry 'cite cite)
5199        (gnus-article-wash-status-entry 'headers (or headers boring))
5200        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5201        (gnus-article-wash-status-entry 'signature signature)
5202        (gnus-article-wash-status-entry 'overstrike overstrike)
5203        (gnus-article-wash-status-entry 'gnus-show-mime gnus-show-mime)
5204        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5205
5206 (defun gnus-add-wash-type (type)
5207   "Add a washing of TYPE to the current status."
5208   (add-to-list 'gnus-article-wash-types type))
5209
5210 (defun gnus-delete-wash-type (type)
5211   "Add a washing of TYPE to the current status."
5212   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5213
5214 (defun gnus-add-image (category image)
5215   "Add IMAGE of CATEGORY to the list of displayed images."
5216   (let ((entry (assq category gnus-article-image-alist)))
5217     (unless entry
5218       (setq entry (list category))
5219       (push entry gnus-article-image-alist))
5220     (nconc entry (list image))))
5221
5222 (defun gnus-delete-images (category)
5223   "Delete all images in CATEGORY."
5224   (let ((entry (assq category gnus-article-image-alist)))
5225     (dolist (image (cdr entry))
5226       (gnus-remove-image image))
5227     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5228     (gnus-delete-wash-type category)))
5229
5230 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5231
5232 (defun gnus-article-maybe-hide-headers ()
5233   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5234 Provided for backwards compatibility."
5235   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5236                  (not (save-excursion (set-buffer gnus-summary-buffer)
5237                                       gnus-have-all-headers)))
5238              (not gnus-inhibit-hiding))
5239     (gnus-article-hide-headers)))
5240
5241 ;;; Article savers.
5242
5243 (defun gnus-output-to-file (file-name)
5244   "Append the current article to a file named FILE-NAME."
5245   (let ((artbuf (current-buffer)))
5246     (with-temp-buffer
5247       (insert-buffer-substring artbuf)
5248       ;; Append newline at end of the buffer as separator, and then
5249       ;; save it to file.
5250       (goto-char (point-max))
5251       (insert "\n")
5252       (let ((file-name-coding-system nnmail-pathname-coding-system)
5253             (pathname-coding-system nnmail-pathname-coding-system))
5254         (write-region-as-binary (point-min) (point-max) file-name 'append))
5255       t)))
5256
5257 (defun gnus-narrow-to-page (&optional arg)
5258   "Narrow the article buffer to a page.
5259 If given a numerical ARG, move forward ARG pages."
5260   (interactive "P")
5261   (setq arg (if arg (prefix-numeric-value arg) 0))
5262   (save-excursion
5263     (set-buffer gnus-article-buffer)
5264     (goto-char (point-min))
5265     (widen)
5266     ;; Remove any old next/prev buttons.
5267     (when (gnus-visual-p 'page-marker)
5268       (let ((buffer-read-only nil))
5269         (gnus-remove-text-with-property 'gnus-prev)
5270         (gnus-remove-text-with-property 'gnus-next)))
5271     (when
5272         (cond ((< arg 0)
5273                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
5274               ((> arg 0)
5275                (re-search-forward page-delimiter nil 'move arg)))
5276       (goto-char (match-end 0)))
5277     (narrow-to-region
5278      (point)
5279      (if (re-search-forward page-delimiter nil 'move)
5280          (match-beginning 0)
5281        (point)))
5282     (when (and (gnus-visual-p 'page-marker)
5283                (not (= (point-min) 1)))
5284       (save-excursion
5285         (goto-char (point-min))
5286         (gnus-insert-prev-page-button)))
5287     (when (and (gnus-visual-p 'page-marker)
5288                (< (+ (point-max) 2) (buffer-size)))
5289       (save-excursion
5290         (goto-char (point-max))
5291         (gnus-insert-next-page-button)))))
5292
5293 ;; Article mode commands
5294
5295 (defun gnus-article-goto-next-page ()
5296   "Show the next page of the article."
5297   (interactive)
5298   (when (gnus-article-next-page)
5299     (goto-char (point-min))
5300     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
5301
5302
5303 (defun gnus-article-goto-prev-page ()
5304   "Show the next page of the article."
5305   (interactive)
5306   (if (bobp)
5307       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
5308     (gnus-article-prev-page nil)))
5309
5310 ;; This is cleaner but currently breaks `gnus-pick-mode':
5311 ;;
5312 ;; (defun gnus-article-goto-next-page ()
5313 ;;   "Show the next page of the article."
5314 ;;   (interactive)
5315 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5316 ;;     (gnus-summary-next-page)))
5317 ;;
5318 ;; (defun gnus-article-goto-prev-page ()
5319 ;;   "Show the next page of the article."
5320 ;;   (interactive)
5321 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5322 ;;     (gnus-summary-prev-page)))
5323
5324 (defun gnus-article-next-page (&optional lines)
5325   "Show the next page of the current article.
5326 If end of article, return non-nil.  Otherwise return nil.
5327 Argument LINES specifies lines to be scrolled up."
5328   (interactive "p")
5329   (move-to-window-line -1)
5330   (if (save-excursion
5331         (end-of-line)
5332         (and (pos-visible-in-window-p)  ;Not continuation line.
5333              (>= (1+ (point)) (point-max)))) ;Allow for trailing newline.
5334       ;; Nothing in this page.
5335       (if (or (not gnus-page-broken)
5336               (save-excursion
5337                 (save-restriction
5338                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
5339           (progn
5340             (when gnus-article-over-scroll
5341               (gnus-article-next-page-1 lines))
5342             t)                  ;Nothing more.
5343         (gnus-narrow-to-page 1)         ;Go to next page.
5344         nil)
5345     ;; More in this page.
5346     (gnus-article-next-page-1 lines)
5347     nil))
5348
5349 (defun gnus-article-next-page-1 (lines)
5350   (let ((scroll-in-place nil))
5351     (condition-case ()
5352         (scroll-up lines)
5353       (end-of-buffer
5354        ;; Long lines may cause an end-of-buffer error.
5355        (goto-char (point-max)))))
5356   (move-to-window-line 0))
5357
5358 (defun gnus-article-prev-page (&optional lines)
5359   "Show previous page of current article.
5360 Argument LINES specifies lines to be scrolled down."
5361   (interactive "p")
5362   (move-to-window-line 0)
5363   (if (and gnus-page-broken
5364            (bobp)
5365            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
5366       (progn
5367         (gnus-narrow-to-page -1)        ;Go to previous page.
5368         (goto-char (point-max))
5369         (recenter -1))
5370     (let ((scroll-in-place nil))
5371       (prog1
5372           (condition-case ()
5373               (scroll-down lines)
5374             (beginning-of-buffer
5375              (goto-char (point-min))))
5376         (move-to-window-line 0)))))
5377
5378 (defun gnus-article-only-boring-p ()
5379   "Decide whether there is only boring text remaining in the article.
5380 Something \"interesting\" is a word of at least two letters that does
5381 not have a face in `gnus-article-boring-faces'."
5382   (when (and gnus-article-skip-boring
5383              (boundp 'gnus-article-boring-faces)
5384              (symbol-value 'gnus-article-boring-faces))
5385     (save-excursion
5386       (catch 'only-boring
5387         (while (re-search-forward "\\b\\w\\w" nil t)
5388           (forward-char -1)
5389           (when (not (gnus-intersection
5390                       (gnus-faces-at (point))
5391                       (symbol-value 'gnus-article-boring-faces)))
5392             (throw 'only-boring nil)))
5393         (throw 'only-boring t)))))
5394
5395 (defun gnus-article-refer-article ()
5396   "Read article specified by message-id around point."
5397   (interactive)
5398   (save-excursion
5399     (re-search-backward "[ \t]\\|^" (gnus-point-at-bol) t)
5400     (re-search-forward "<?news:<?\\|<" (gnus-point-at-eol) t)
5401     (if (re-search-forward "[^@ ]+@[^ \t>]+" (gnus-point-at-eol) t)
5402         (let ((msg-id (concat "<" (match-string 0) ">")))
5403           (set-buffer gnus-summary-buffer)
5404           (gnus-summary-refer-article msg-id))
5405       (error "No references around point"))))
5406
5407 (defun gnus-article-show-summary ()
5408   "Reconfigure windows to show summary buffer."
5409   (interactive)
5410   (if (not (gnus-buffer-live-p gnus-summary-buffer))
5411       (error "There is no summary buffer for this article buffer")
5412     (gnus-article-set-globals)
5413     (gnus-configure-windows 'article)
5414     (gnus-summary-goto-subject gnus-current-article)
5415     (gnus-summary-position-point)))
5416
5417 (defun gnus-article-describe-briefly ()
5418   "Describe article mode commands briefly."
5419   (interactive)
5420   (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")))
5421
5422 (defun gnus-article-summary-command ()
5423   "Execute the last keystroke in the summary buffer."
5424   (interactive)
5425   (let ((obuf (current-buffer))
5426         (owin (current-window-configuration))
5427         func)
5428     (switch-to-buffer gnus-article-current-summary 'norecord)
5429     (setq func (lookup-key (current-local-map) (this-command-keys)))
5430     (call-interactively func)
5431     (set-buffer obuf)
5432     (set-window-configuration owin)
5433     (set-window-point (get-buffer-window (current-buffer)) (point))))
5434
5435 (defun gnus-article-summary-command-nosave ()
5436   "Execute the last keystroke in the summary buffer."
5437   (interactive)
5438   (let (func)
5439     (pop-to-buffer gnus-article-current-summary 'norecord)
5440     (setq func (lookup-key (current-local-map) (this-command-keys)))
5441     (call-interactively func)))
5442
5443 (defun gnus-article-check-buffer ()
5444   "Beep if not in an article buffer."
5445   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
5446     (error "Command invoked outside of a Gnus article buffer")))
5447
5448 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5449   "Read a summary buffer key sequence and execute it from the article buffer."
5450   (interactive "P")
5451   (gnus-article-check-buffer)
5452   (let ((nosaves
5453          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5454            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5455            "=" "^" "\M-^" "|"))
5456         (nosave-but-article
5457          '("A\r"))
5458         (nosave-in-article
5459          '("\C-d"))
5460         (up-to-top
5461          '("n" "Gn" "p" "Gp"))
5462         keys new-sum-point)
5463     (save-excursion
5464       (set-buffer gnus-article-current-summary)
5465       (let (gnus-pick-mode)
5466         (push (or key last-command-event) unread-command-events)
5467         (setq keys (static-if (featurep 'xemacs)
5468                        (events-to-keys (read-key-sequence nil))
5469                      (read-key-sequence nil)))))
5470     (message "")
5471
5472     (if (or (member keys nosaves)
5473             (member keys nosave-but-article)
5474             (member keys nosave-in-article))
5475         (let (func)
5476           (save-window-excursion
5477             (pop-to-buffer gnus-article-current-summary 'norecord)
5478             ;; We disable the pick minor mode commands.
5479             (let (gnus-pick-mode)
5480               (setq func (lookup-key (current-local-map) keys))))
5481           (if (or (not func)
5482                   (numberp func))
5483               (ding)
5484             (unless (member keys nosave-in-article)
5485               (set-buffer gnus-article-current-summary))
5486             (call-interactively func)
5487             (setq new-sum-point (point)))
5488           (when (member keys nosave-but-article)
5489             (pop-to-buffer gnus-article-buffer 'norecord)))
5490       ;; These commands should restore window configuration.
5491       (let ((obuf (current-buffer))
5492             (owin (current-window-configuration))
5493             (opoint (point))
5494             (summary gnus-article-current-summary)
5495             func in-buffer selected)
5496         (if not-restore-window
5497             (pop-to-buffer summary 'norecord)
5498           (switch-to-buffer summary 'norecord))
5499         (setq in-buffer (current-buffer))
5500         ;; We disable the pick minor mode commands.
5501         (if (and (setq func (let (gnus-pick-mode)
5502                               (lookup-key (current-local-map) keys)))
5503                  (functionp func))
5504             (progn
5505               (call-interactively func)
5506               (setq new-sum-point (point))
5507               (when (eq in-buffer (current-buffer))
5508                 (setq selected (gnus-summary-select-article))
5509                 (set-buffer obuf)
5510                 (unless not-restore-window
5511                   (set-window-configuration owin))
5512                 (when (eq selected 'old)
5513                   (article-goto-body)
5514                   (set-window-start (get-buffer-window (current-buffer))
5515                                     1)
5516                   (set-window-point (get-buffer-window (current-buffer))
5517                                     (point)))
5518                 (let ((win (get-buffer-window gnus-article-current-summary)))
5519                   (when win
5520                     (set-window-point win new-sum-point))))    )
5521           (switch-to-buffer gnus-article-buffer)
5522           (ding))))))
5523
5524 (defun gnus-article-describe-key (key)
5525   "Display documentation of the function invoked by KEY.  KEY is a string."
5526   (interactive "kDescribe key: ")
5527   (gnus-article-check-buffer)
5528   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5529       (save-excursion
5530         (set-buffer gnus-article-current-summary)
5531         (let (gnus-pick-mode)
5532           (if (featurep 'xemacs)
5533               (progn
5534                 (push (elt key 0) unread-command-events)
5535                 (setq key (events-to-keys
5536                            (read-key-sequence "Describe key: "))))
5537             (setq unread-command-events
5538                   (mapcar
5539                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5540                    key))
5541             (setq key (read-key-sequence "Describe key: "))))
5542         (describe-key key))
5543     (describe-key key)))
5544
5545 (defun gnus-article-describe-key-briefly (key &optional insert)
5546   "Display documentation of the function invoked by KEY.  KEY is a string."
5547   (interactive "kDescribe key: \nP")
5548   (gnus-article-check-buffer)
5549   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5550       (save-excursion
5551         (set-buffer gnus-article-current-summary)
5552         (let (gnus-pick-mode)
5553           (if (featurep 'xemacs)
5554               (progn
5555                 (push (elt key 0) unread-command-events)
5556                 (setq key (events-to-keys
5557                            (read-key-sequence "Describe key: "))))
5558             (setq unread-command-events
5559                   (mapcar
5560                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5561                    key))
5562             (setq key (read-key-sequence "Describe key: "))))
5563         (describe-key-briefly key insert))
5564     (describe-key-briefly key insert)))
5565
5566 (defun gnus-article-reply-with-original (&optional wide)
5567   "Start composing a reply mail to the current message.
5568 The text in the region will be yanked.  If the region isn't active,
5569 the entire article will be yanked."
5570   (interactive "P")
5571   (let ((article (cdr gnus-article-current))
5572         contents)
5573     (if (not (gnus-mark-active-p))
5574         (with-current-buffer gnus-summary-buffer
5575           (gnus-summary-reply (list (list article)) wide))
5576       (setq contents (buffer-substring (point) (mark t)))
5577       ;; Deactivate active regions.
5578       (when (and (boundp 'transient-mark-mode)
5579                  transient-mark-mode)
5580         (setq mark-active nil))
5581       (with-current-buffer gnus-summary-buffer
5582         (gnus-summary-reply
5583          (list (list article contents)) wide)))))
5584
5585 (defun gnus-article-followup-with-original ()
5586   "Compose a followup to the current article.
5587 The text in the region will be yanked.  If the region isn't active,
5588 the entire article will be yanked."
5589   (interactive)
5590   (let ((article (cdr gnus-article-current))
5591         contents)
5592       (if (not (gnus-mark-active-p))
5593           (with-current-buffer gnus-summary-buffer
5594             (gnus-summary-followup (list (list article))))
5595         (setq contents (buffer-substring (point) (mark t)))
5596         ;; Deactivate active regions.
5597         (when (and (boundp 'transient-mark-mode)
5598                    transient-mark-mode)
5599           (setq mark-active nil))
5600         (with-current-buffer gnus-summary-buffer
5601           (gnus-summary-followup
5602            (list (list article contents)))))))
5603
5604 (defun gnus-article-hide (&optional arg force)
5605   "Hide all the gruft in the current article.
5606 This means that signatures, cited text and (some) headers will be
5607 hidden.
5608 If given a prefix, show the hidden text instead."
5609   (interactive (append (gnus-article-hidden-arg) (list 'force)))
5610   (gnus-article-hide-headers arg)
5611   (gnus-article-hide-list-identifiers arg)
5612   (gnus-article-hide-citation-maybe arg force)
5613   (gnus-article-hide-signature arg))
5614
5615 (defun gnus-article-maybe-highlight ()
5616   "Do some article highlighting if article highlighting is requested."
5617   (when (gnus-visual-p 'article-highlight 'highlight)
5618     (gnus-article-highlight-some)))
5619
5620 (defun gnus-check-group-server ()
5621   ;; Make sure the connection to the server is alive.
5622   (unless (gnus-server-opened
5623            (gnus-find-method-for-group gnus-newsgroup-name))
5624     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
5625     (gnus-request-group gnus-newsgroup-name t)))
5626
5627 (eval-when-compile
5628   (autoload 'nneething-get-file-name "nneething"))
5629
5630 (defun gnus-request-article-this-buffer (article group)
5631   "Get an article and insert it into this buffer."
5632   (let (do-update-line sparse-header)
5633     (prog1
5634         (save-excursion
5635           (erase-buffer)
5636           (gnus-kill-all-overlays)
5637           (setq group (or group gnus-newsgroup-name))
5638
5639           ;; Using `gnus-request-article' directly will insert the article into
5640           ;; `nntp-server-buffer' - so we'll save some time by not having to
5641           ;; copy it from the server buffer into the article buffer.
5642
5643           ;; We only request an article by message-id when we do not have the
5644           ;; headers for it, so we'll have to get those.
5645           (when (stringp article)
5646             (gnus-read-header article))
5647
5648           ;; If the article number is negative, that means that this article
5649           ;; doesn't belong in this newsgroup (possibly), so we find its
5650           ;; message-id and request it by id instead of number.
5651           (when (and (numberp article)
5652                      gnus-summary-buffer
5653                      (get-buffer gnus-summary-buffer)
5654                      (gnus-buffer-exists-p gnus-summary-buffer))
5655             (save-excursion
5656               (set-buffer gnus-summary-buffer)
5657               (let ((header (gnus-summary-article-header article)))
5658                 (when (< article 0)
5659                   (cond
5660                    ((memq article gnus-newsgroup-sparse)
5661                     ;; This is a sparse gap article.
5662                     (setq do-update-line article)
5663                     (setq article (mail-header-id header))
5664                     (setq sparse-header (gnus-read-header article))
5665                     (setq gnus-newsgroup-sparse
5666                           (delq article gnus-newsgroup-sparse)))
5667                    ((vectorp header)
5668                     ;; It's a real article.
5669                     (setq article (mail-header-id header)))
5670                    (t
5671                     ;; It is an extracted pseudo-article.
5672                     (setq article 'pseudo)
5673                     (gnus-request-pseudo-article header))))
5674
5675                 (let ((method (gnus-find-method-for-group
5676                                gnus-newsgroup-name)))
5677                   (when (and (eq (car method) 'nneething)
5678                              (vectorp header))
5679                     (let ((dir (nneething-get-file-name
5680                                 (mail-header-id header))))
5681                       (when (and (stringp dir)
5682                                  (file-directory-p dir))
5683                         (setq article 'nneething)
5684                         (gnus-group-enter-directory dir))))))))
5685
5686           (cond
5687            ;; Refuse to select canceled articles.
5688            ((and (numberp article)
5689                  gnus-summary-buffer
5690                  (get-buffer gnus-summary-buffer)
5691                  (gnus-buffer-exists-p gnus-summary-buffer)
5692                  (eq (cdr (save-excursion
5693                             (set-buffer gnus-summary-buffer)
5694                             (assq article gnus-newsgroup-reads)))
5695                      gnus-canceled-mark))
5696             nil)
5697            ;; We first check `gnus-original-article-buffer'.
5698            ((and (get-buffer gnus-original-article-buffer)
5699                  (numberp article)
5700                  (save-excursion
5701                    (set-buffer gnus-original-article-buffer)
5702                    (and (equal (car gnus-original-article) group)
5703                         (eq (cdr gnus-original-article) article))))
5704             (insert-buffer-substring gnus-original-article-buffer)
5705             'article)
5706            ;; Check the backlog.
5707            ((and gnus-keep-backlog
5708                  (gnus-backlog-request-article group article (current-buffer)))
5709             'article)
5710            ;; Check asynchronous pre-fetch.
5711            ((gnus-async-request-fetched-article group article (current-buffer))
5712             (gnus-async-prefetch-next group article gnus-summary-buffer)
5713             (when (and (numberp article) gnus-keep-backlog)
5714               (gnus-backlog-enter-article group article (current-buffer)))
5715             'article)
5716            ;; Check the cache.
5717            ((and gnus-use-cache
5718                  (numberp article)
5719                  (gnus-cache-request-article article group))
5720             'article)
5721            ;; Check the agent cache.
5722            ((gnus-agent-request-article article group)
5723             'article)
5724            ;; Get the article and put into the article buffer.
5725            ((or (stringp article)
5726                 (numberp article))
5727             (let ((gnus-override-method gnus-override-method)
5728                   (methods (and (stringp article)
5729                                 gnus-refer-article-method))
5730                   (backend (car (gnus-find-method-for-group
5731                                  gnus-newsgroup-name)))
5732                   result
5733                   (buffer-read-only nil))
5734               (if (or (not (listp methods))
5735                       (and (symbolp (car methods))
5736                            (assq (car methods) nnoo-definition-alist)))
5737                   (setq methods (list methods)))
5738               (when (and (null gnus-override-method)
5739                          methods)
5740                 (setq gnus-override-method (pop methods)))
5741               (while (not result)
5742                 (when (eq gnus-override-method 'current)
5743                   (setq gnus-override-method
5744                         (with-current-buffer gnus-summary-buffer
5745                           gnus-current-select-method)))
5746                 (erase-buffer)
5747                 (gnus-kill-all-overlays)
5748                 (let ((gnus-newsgroup-name group))
5749                   (gnus-check-group-server))
5750                 (cond
5751                  ((gnus-request-article article group (current-buffer))
5752                   (when (numberp article)
5753                     (gnus-async-prefetch-next group article
5754                                               gnus-summary-buffer)
5755                     (when gnus-keep-backlog
5756                       (gnus-backlog-enter-article
5757                        group article (current-buffer))))
5758                   (setq result 'article))
5759                  (methods
5760                   (setq gnus-override-method (pop methods)))
5761                  ((not (string-match "^400 "
5762                                      (nnheader-get-report backend)))
5763                   ;; If we get 400 server disconnect, reconnect and
5764                   ;; retry; otherwise, assume the article has expired.
5765                   (setq result 'done))))
5766               (and (eq result 'article) 'article)))
5767            ;; It was a pseudo.
5768            (t article)))
5769
5770       ;; Associate this article with the current summary buffer.
5771       (setq gnus-article-current-summary gnus-summary-buffer)
5772
5773       ;; Take the article from the original article buffer
5774       ;; and place it in the buffer it's supposed to be in.
5775       (when (and (get-buffer gnus-article-buffer)
5776                  (equal (buffer-name (current-buffer))
5777                         (buffer-name (get-buffer gnus-article-buffer))))
5778         (save-excursion
5779           (if (get-buffer gnus-original-article-buffer)
5780               (set-buffer gnus-original-article-buffer)
5781             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5782             (buffer-disable-undo)
5783             (setq major-mode 'gnus-original-article-mode)
5784             (setq buffer-read-only t))
5785           (let (buffer-read-only)
5786             (erase-buffer)
5787             (insert-buffer-substring gnus-article-buffer))
5788           (setq gnus-original-article (cons group article)))
5789
5790         ;; Decode charsets.
5791         (run-hooks 'gnus-article-decode-hook)
5792         ;; Mark article as decoded or not.
5793         (setq gnus-article-decoded-p gnus-article-decode-hook))
5794
5795       ;; Update sparse articles.
5796       (when (and do-update-line
5797                  (or (numberp article)
5798                      (stringp article)))
5799         (let ((buf (current-buffer)))
5800           (set-buffer gnus-summary-buffer)
5801           (gnus-summary-update-article do-update-line sparse-header)
5802           (gnus-summary-goto-subject do-update-line nil t)
5803           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5804                             (point))
5805           (set-buffer buf))))))
5806
5807 ;;;
5808 ;;; Article editing
5809 ;;;
5810
5811 (defcustom gnus-article-edit-mode-hook nil
5812   "Hook run in article edit mode buffers."
5813   :group 'gnus-article-various
5814   :type 'hook)
5815
5816 (defcustom gnus-article-edit-article-setup-function
5817   'gnus-article-mime-edit-article-setup
5818   "Function called to setup an editing article buffer."
5819   :group 'gnus-article-various
5820   :type 'function)
5821
5822 (defvar gnus-article-edit-done-function nil)
5823
5824 (defvar gnus-article-edit-mode-map nil)
5825 (defvar gnus-article-edit-mode nil)
5826
5827 ;; Should we be using derived.el for this?
5828 (unless gnus-article-edit-mode-map
5829   (setq gnus-article-edit-mode-map (make-keymap))
5830   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5831
5832   (gnus-define-keys gnus-article-edit-mode-map
5833     "\C-c?"    describe-mode
5834     "\C-c\C-c" gnus-article-edit-done
5835     "\C-c\C-k" gnus-article-edit-exit
5836     "\C-c\C-f\C-t" message-goto-to
5837     "\C-c\C-f\C-o" message-goto-from
5838     "\C-c\C-f\C-b" message-goto-bcc
5839     ;;"\C-c\C-f\C-w" message-goto-fcc
5840     "\C-c\C-f\C-c" message-goto-cc
5841     "\C-c\C-f\C-s" message-goto-subject
5842     "\C-c\C-f\C-r" message-goto-reply-to
5843     "\C-c\C-f\C-n" message-goto-newsgroups
5844     "\C-c\C-f\C-d" message-goto-distribution
5845     "\C-c\C-f\C-f" message-goto-followup-to
5846     "\C-c\C-f\C-m" message-goto-mail-followup-to
5847     "\C-c\C-f\C-k" message-goto-keywords
5848     "\C-c\C-f\C-u" message-goto-summary
5849     "\C-c\C-f\C-i" message-insert-or-toggle-importance
5850     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
5851     "\C-c\C-b" message-goto-body
5852     "\C-c\C-i" message-goto-signature
5853
5854     "\C-c\C-t" message-insert-to
5855     "\C-c\C-n" message-insert-newsgroups
5856     "\C-c\C-o" message-sort-headers
5857     "\C-c\C-e" message-elide-region
5858     "\C-c\C-v" message-delete-not-region
5859     "\C-c\C-z" message-kill-to-signature
5860     "\M-\r" message-newline-and-reformat
5861     "\C-c\C-a" mml-attach-file
5862     "\C-a" message-beginning-of-line
5863     "\t" message-tab
5864     "\M-;" comment-region)
5865
5866   (gnus-define-keys (gnus-article-edit-wash-map
5867                      "\C-c\C-w" gnus-article-edit-mode-map)
5868     "f" gnus-article-edit-full-stops))
5869
5870 (easy-menu-define
5871   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
5872   '("Field"
5873     ["Fetch To" message-insert-to t]
5874     ["Fetch Newsgroups" message-insert-newsgroups t]
5875     "----"
5876     ["To" message-goto-to t]
5877     ["From" message-goto-from t]
5878     ["Subject" message-goto-subject t]
5879     ["Cc" message-goto-cc t]
5880     ["Reply-To" message-goto-reply-to t]
5881     ["Summary" message-goto-summary t]
5882     ["Keywords" message-goto-keywords t]
5883     ["Newsgroups" message-goto-newsgroups t]
5884     ["Followup-To" message-goto-followup-to t]
5885     ["Mail-Followup-To" message-goto-mail-followup-to t]
5886     ["Distribution" message-goto-distribution t]
5887     ["Body" message-goto-body t]
5888     ["Signature" message-goto-signature t]))
5889
5890 (define-derived-mode gnus-article-edit-mode text-mode "Article Edit"
5891   "Major mode for editing articles.
5892 This is an extended text-mode.
5893
5894 \\{gnus-article-edit-mode-map}"
5895   (make-local-variable 'gnus-article-edit-done-function)
5896   (make-local-variable 'gnus-prev-winconf)
5897   (set (make-local-variable 'font-lock-defaults)
5898        '(message-font-lock-keywords t))
5899   (set (make-local-variable 'mail-header-separator) "")
5900   (set (make-local-variable 'gnus-article-edit-mode) t)
5901   (easy-menu-add message-mode-field-menu message-mode-map)
5902   (setq buffer-read-only nil)
5903   (buffer-enable-undo)
5904   (widen))
5905
5906 (defun gnus-article-edit (&optional force)
5907   "Edit the current article.
5908 This will have permanent effect only in mail groups.
5909 If FORCE is non-nil, allow editing of articles even in read-only
5910 groups."
5911   (interactive "P")
5912   (when (and (not force)
5913              (gnus-group-read-only-p))
5914     (error "The current newsgroup does not support article editing"))
5915   (gnus-article-date-original)
5916   (gnus-article-edit-article
5917    'ignore
5918    `(lambda (no-highlight)
5919       'ignore
5920       (gnus-summary-edit-article-done
5921        ,(or (mail-header-references gnus-current-headers) "")
5922        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
5923
5924 (defun gnus-article-edit-article (start-func exit-func)
5925   "Start editing the contents of the current article buffer."
5926   (let ((winconf (current-window-configuration)))
5927     (set-buffer gnus-article-buffer)
5928     (gnus-article-edit-mode)
5929     (funcall start-func)
5930     (set-buffer-modified-p nil)
5931     (gnus-configure-windows 'edit-article)
5932     (setq gnus-article-edit-done-function exit-func)
5933     (setq gnus-prev-winconf winconf)
5934     (when gnus-article-edit-article-setup-function
5935       (funcall gnus-article-edit-article-setup-function))
5936     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
5937
5938 (defun gnus-article-edit-done (&optional arg)
5939   "Update the article edits and exit."
5940   (interactive "P")
5941   (let ((func gnus-article-edit-done-function)
5942         (buf (current-buffer))
5943         (start (window-start))
5944         (p (point))
5945         (winconf gnus-prev-winconf))
5946     (remove-hook 'gnus-article-mode-hook
5947                  'gnus-article-mime-edit-article-unwind)
5948     (widen) ;; Widen it in case that users narrowed the buffer.
5949     (funcall func arg)
5950     (set-buffer buf)
5951     ;; The cache and backlog have to be flushed somewhat.
5952     (when gnus-keep-backlog
5953       (gnus-backlog-remove-article
5954        (car gnus-article-current) (cdr gnus-article-current)))
5955     ;; Flush original article as well.
5956     (save-excursion
5957       (when (get-buffer gnus-original-article-buffer)
5958         (set-buffer gnus-original-article-buffer)
5959         (setq gnus-original-article nil)))
5960     (when gnus-use-cache
5961       (gnus-cache-update-article
5962        (car gnus-article-current) (cdr gnus-article-current)))
5963     ;; We remove all text props from the article buffer.
5964     (kill-all-local-variables)
5965     (gnus-set-text-properties (point-min) (point-max) nil)
5966     (gnus-article-mode)
5967     (set-window-configuration winconf)
5968     (set-buffer buf)
5969     (set-window-start (get-buffer-window buf) start)
5970     (set-window-point (get-buffer-window buf) (point))))
5971
5972 (defun gnus-article-edit-exit ()
5973   "Exit the article editing without updating."
5974   (interactive)
5975   (when (or (not (buffer-modified-p))
5976             (yes-or-no-p "Article modified; kill anyway? "))
5977     (let ((curbuf (current-buffer))
5978           (p (point))
5979           (window-start (window-start)))
5980       (erase-buffer)
5981       (if (gnus-buffer-live-p gnus-original-article-buffer)
5982           (insert-buffer-substring gnus-original-article-buffer))
5983       (let ((winconf gnus-prev-winconf))
5984         (kill-all-local-variables)
5985         (gnus-article-mode)
5986         (set-window-configuration winconf)
5987         ;; Tippy-toe some to make sure that point remains where it was.
5988         (save-current-buffer
5989           (set-buffer curbuf)
5990           (set-window-start (get-buffer-window (current-buffer)) window-start)
5991           (goto-char p))))))
5992
5993 (defun gnus-article-edit-full-stops ()
5994   "Interactively repair spacing at end of sentences."
5995   (interactive)
5996   (save-excursion
5997     (goto-char (point-min))
5998     (search-forward-regexp "^$" nil t)
5999     (let ((case-fold-search nil))
6000       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6001
6002 ;;;
6003 ;;; Article editing with MIME-Edit
6004 ;;;
6005
6006 (defcustom gnus-article-mime-edit-article-setup-hook nil
6007   "Hook run after setting up a MIME editing article buffer."
6008   :group 'gnus-article-various
6009   :type 'hook)
6010
6011 (defun gnus-article-mime-edit-article-unwind ()
6012   "Unwind `gnus-article-buffer' if article editing was given up."
6013   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
6014   (when (featurep 'font-lock)
6015     (setq font-lock-defaults nil)
6016     (font-lock-mode -1))
6017   (when mime-edit-mode-flag
6018     (mime-edit-exit 'nomime 'no-error)
6019     (message "")))
6020
6021 (defun gnus-article-mime-edit-article-setup ()
6022   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
6023 after replacing with the original article."
6024   (setq gnus-show-mime t)
6025   (setq gnus-article-edit-done-function
6026         `(lambda (&rest args)
6027            (when mime-edit-mode-flag
6028              (let (mime-edit-insert-user-agent-field)
6029                (mime-edit-exit))
6030              (message ""))
6031            (goto-char (point-min))
6032            (let (case-fold-search)
6033              (when (re-search-forward
6034                     (format "^%s$" (regexp-quote mail-header-separator))
6035                     nil t)
6036                (replace-match "")))
6037            (apply ,gnus-article-edit-done-function args)
6038            (insert
6039             (prog1
6040                 (buffer-substring-no-properties (point-min) (point-max))
6041               (set-buffer (get-buffer-create gnus-original-article-buffer))
6042               (erase-buffer)))
6043            (setq gnus-current-headers (gnus-article-make-full-mail-header))
6044            (set-buffer gnus-article-buffer)
6045            (gnus-article-prepare-display)))
6046   (substitute-key-definition 'gnus-article-edit-done
6047                              'gnus-article-mime-edit-done
6048                              gnus-article-edit-mode-map)
6049   (substitute-key-definition 'gnus-article-edit-exit
6050                              'gnus-article-mime-edit-exit
6051                              gnus-article-edit-mode-map)
6052   (erase-buffer)
6053   (insert-buffer-substring gnus-original-article-buffer)
6054   (unless (member (with-current-buffer gnus-summary-buffer
6055                     gnus-newsgroup-name)
6056                   '("nndraft:delayed" "nndraft:drafts"))
6057     (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
6058       (fset 'mime-edit-decode-single-part-in-buffer
6059             (lambda (&rest args)
6060               (if (let ((content-type (car args)))
6061                     (and (eq 'message (mime-content-type-primary-type
6062                                        content-type))
6063                          (eq 'rfc822 (mime-content-type-subtype
6064                                       content-type))))
6065                   (setcar (cdr args) 'not-decode-text))
6066               (apply ofn args)))
6067       (unwind-protect
6068           (mime-edit-again)
6069         (fset 'mime-edit-decode-single-part-in-buffer ofn))))
6070   (when (featurep 'font-lock)
6071     (set (make-local-variable 'font-lock-defaults)
6072          '(message-font-lock-keywords t))
6073     (font-lock-set-defaults)
6074     (turn-on-font-lock))
6075   (set-buffer-modified-p nil)
6076   (delete-other-windows)
6077   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
6078   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
6079
6080 (defun gnus-article-mime-edit-done (&optional arg)
6081   "Update the article MIME edits and exit."
6082   (interactive "P")
6083   (when (featurep 'font-lock)
6084     (setq font-lock-defaults nil)
6085     (font-lock-mode -1))
6086   (let ((inhibit-read-only t))
6087     (gnus-article-edit-done arg)))
6088
6089 (defun gnus-article-mime-edit-exit ()
6090   "Exit the article MIME editing without updating."
6091   (interactive)
6092   (when (or (not (buffer-modified-p))
6093             (yes-or-no-p "Article modified; kill anyway? "))
6094     (when (featurep 'font-lock)
6095       (setq font-lock-defaults nil)
6096       (font-lock-mode -1))
6097     (when mime-edit-mode-flag
6098       (let (mime-edit-insert-user-agent-field)
6099         (mime-edit-exit))
6100       (message ""))
6101     (goto-char (point-min))
6102     (let (case-fold-search)
6103       (when (re-search-forward
6104              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
6105         (replace-match "")))
6106     (let ((winconf gnus-prev-winconf))
6107       (insert (prog1
6108                   (buffer-substring-no-properties (point-min) (point-max))
6109                 (set-buffer (get-buffer-create gnus-original-article-buffer))
6110                 (erase-buffer)))
6111       (setq gnus-current-headers (gnus-article-make-full-mail-header))
6112       (set-buffer gnus-article-buffer)
6113       (gnus-article-prepare-display)
6114       (set-window-configuration winconf))))
6115
6116 ;;;
6117 ;;; Article highlights
6118 ;;;
6119
6120 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6121
6122 ;;; Internal Variables:
6123
6124 (defcustom gnus-button-url-regexp
6125   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
6126       "\\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:]]\\)"
6127     "\\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\\)\\)")
6128   "Regular expression that matches URLs."
6129   :group 'gnus-article-buttons
6130   :type 'regexp)
6131
6132 (defcustom gnus-button-valid-fqdn-regexp
6133   message-valid-fqdn-regexp
6134   "Regular expression that matches a valid FQDN."
6135   :group 'gnus-article-buttons
6136   :type 'regexp)
6137
6138 (defcustom gnus-button-man-handler 'manual-entry
6139   "Function to use for displaying man pages.
6140 The function must take at least one argument with a string naming the
6141 man page."
6142   :type '(choice (function-item :tag "Man" manual-entry)
6143                  (function-item :tag "Woman" woman)
6144                  (function :tag "Other"))
6145   :group 'gnus-article-buttons)
6146
6147 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6148   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6149 If the default site is too slow, try to find a CTAN mirror, see
6150 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6151 the variable `gnus-button-handle-ctan'."
6152   :group 'gnus-article-buttons
6153   :link '(custom-manual "(gnus)Group Parameters")
6154   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6155                  (const "http://tug.ctan.org/tex-archive/")
6156                  (const "http://www.dante.de/CTAN/")
6157                  (string :tag "Other")))
6158
6159 (defcustom gnus-button-ctan-handler 'browse-url
6160   "Function to use for displaying CTAN links.
6161 The function must take one argument, the string naming the URL."
6162   :type '(choice (function-item :tag "Browse Url" browse-url)
6163                  (function :tag "Other"))
6164   :group 'gnus-article-buttons)
6165
6166 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6167   "Bogus strings removed from CTAN URLs."
6168   :group 'gnus-article-buttons
6169   :type '(choice (const "^/?tex-archive/\\|/")
6170                  (regexp :tag "Other")))
6171
6172 (defcustom gnus-button-ctan-directory-regexp
6173   (concat
6174    "\\("; Cannot use `\(?: ... \)' (compatibility with Emacs 20).
6175    "biblio\\|digests\\|dviware\\|fonts\\|graphics\\|help\\|"
6176    "indexing\\|info\\|language\\|macros\\|support\\|systems\\|"
6177    "tds\\|tools\\|usergrps\\|web\\|nonfree\\|obsolete"
6178    "\\)")
6179   "Regular expression for ctan directories.
6180 It should match all directories in the top level of `gnus-ctan-url'."
6181   :group 'gnus-article-buttons
6182   :type 'regexp)
6183
6184 (defcustom gnus-button-mid-or-mail-regexp
6185   (concat "\\b\\(<?[a-z0-9][^<>\")!;:,{}\n\t ]*@"
6186           gnus-button-valid-fqdn-regexp
6187           ">?\\)\\b")
6188   "Regular expression that matches a message ID or a mail address."
6189   :group 'gnus-article-buttons
6190   :type 'regexp)
6191
6192 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6193   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6194 Strings like this can be either a message ID or a mail address.  If it is one
6195 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6196 message ID or a mail address, respectively.  If this variable is set to the
6197 symbol `ask', always query the user what do do.  If it is a function, this
6198 function will be called with the string as it's only argument.  The function
6199 must return `mid', `mail', `invalid' or `ask'."
6200   :group 'gnus-article-buttons
6201   :type '(choice (function-item :tag "Heuristic function"
6202                                 gnus-button-mid-or-mail-heuristic)
6203                  (const ask)
6204                  (const mid)
6205                  (const mail)))
6206
6207 (defcustom gnus-button-mid-or-mail-heuristic-alist
6208   '((-10.0 . ".+\\$.+@")
6209     (-10.0 . "#")
6210     (-10.0 . "\\*")
6211     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
6212     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
6213     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
6214     (-1.0  . "^[^a-z]+@")
6215     ;;
6216     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
6217     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
6218     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
6219     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
6220     ;;
6221     (-2.0  . "^[0-9]")
6222     (-1.0  . "^[0-9][0-9]")
6223     ;;
6224     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
6225     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6226     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
6227     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6228     ;;
6229     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
6230     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
6231     ;;       "[0-9]{8,}.*\@"
6232     (-3.0
6233      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
6234     ;; "[0-9]{12,}.*\@"
6235     ;; compensation for TDMA dated mail addresses:
6236     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
6237     ;;
6238     (-20.0 . "\\.fsf@") ;; Gnus
6239     (-20.0 . "^slrn")
6240     (-20.0 . "^Pine")
6241     (-20.0 . "_-_") ;; Subject change in thread
6242     ;;
6243     (-20.0 . "\\.ln@") ;; leafnode
6244     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
6245     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
6246     ;;
6247     ;; (5.0 . "") ;; $local_part_len <= 7
6248     (10.0  . "^[^0-9]+@")
6249     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
6250     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
6251     (3.0   . "\@stud")
6252     ;;
6253     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
6254     ;;
6255     (0.5   . "^[A-Z][a-z]")
6256     (0.5   . "^[A-Z][a-z][a-z]")
6257     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
6258     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
6259   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
6260
6261 A negative RATE indicates a message IDs, whereas a positive indicates a mail
6262 address.  The REGEXP is processed with `case-fold-search' set to nil."
6263   :group 'gnus-article-buttons
6264   :type '(repeat (cons (number :tag "Rate")
6265                        (regexp :tag "Regexp"))))
6266
6267 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
6268   "Guess whether MID-OR-MAIL is a message ID or a mail address.
6269 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
6270 address, `ask' if unsure and `invalid' if the string is invalid."
6271   (let ((case-fold-search nil)
6272         (list gnus-button-mid-or-mail-heuristic-alist)
6273         (result 0) rate regexp lpartlen elem)
6274     (setq lpartlen
6275           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
6276     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
6277     ;; Certain special cases...
6278     (when (string-match
6279            (concat
6280             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$" "\\|"
6281             "^[0-9]+\.[0-9]+\@compuserve")
6282            mid-or-mail)
6283       (gnus-message 8 "`%s' is a known mail address.")
6284       (setq result 'mail))
6285     (when (string-match "@.*@\\| " mid-or-mail)
6286       (gnus-message 8 "`%s' is invalid.")
6287       (setq result 'invalid))
6288     ;; Nothing more to do, if result is not a number here...
6289     (when (numberp result)
6290       (while list
6291         (setq elem (car list)
6292               rate (car elem)
6293               regexp (cdr elem)
6294               list (cdr list))
6295         (when (string-match regexp mid-or-mail)
6296           (setq result (+ result rate))
6297           (gnus-message
6298            9 "`%s' matched `%s', rate `%s', result `%s'."
6299            mid-or-mail regexp rate result)))
6300       (when (<= lpartlen 7)
6301         (setq result (+ result 5.0))
6302         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
6303                       mid-or-mail result))
6304       (when (>= lpartlen 12)
6305         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
6306         (cond
6307          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
6308           ;; Long local part should contain realname if e-mail address,
6309           ;; too many digits: message-id.
6310           ;; $score -= 5.0 + 0.1 * $local_part_len;
6311           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
6312           (setq result (+ result rate))
6313           (gnus-message
6314            9 "Many digits in `%s', rate `%s', result `%s'."
6315            mid-or-mail rate result))
6316          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
6317                         mid-or-mail)
6318           ;; Too few vowels [^aeiouy]{4,}.*\@
6319           (setq result (+ result -5.0))
6320           (gnus-message
6321            9 "Few vowels in `%s', rate `%s', result `%s'."
6322            mid-or-mail -5.0 result))
6323          (t
6324           (setq result (+ result 5.0))
6325           (gnus-message
6326            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
6327     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
6328     (cond
6329      ;; Maybe we should make this a customizable alist: (condition . 'result)
6330      ((< result -10.0) 'mid)
6331      ((> result  10.0) 'mail)
6332      (t 'ask))))
6333
6334 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
6335   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
6336          (url-mid (concat "news" ":" mid-or-mail))
6337          (url-mailto (concat "mailto" ":" mid-or-mail)))
6338     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
6339     (when (fboundp pref)
6340       (setq guessed
6341             ;; get rid of surrounding angles...
6342             (funcall pref
6343                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
6344       (if (or (eq 'mid guessed) (eq 'mail guessed))
6345           (setq pref guessed)
6346         (setq pref 'ask)))
6347     (if (eq pref 'ask)
6348         (save-window-excursion
6349           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
6350               (setq pref 'mail)
6351             (setq pref 'mid))))
6352     (cond ((eq pref 'mid)
6353            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
6354            (gnus-button-handle-news url-mid))
6355           ((eq pref 'mail)
6356            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
6357            (gnus-url-mailto url-mailto))
6358           (t (gnus-message 3 "Invalid string.")))))
6359
6360 (defun gnus-button-handle-custom (url)
6361   "Follow a Custom URL."
6362   (customize-apropos (gnus-url-unhex-string url)))
6363
6364 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
6365
6366 ;; FIXME: Maybe we should merge some of the functions that do quite similar
6367 ;; stuff?
6368
6369 (defun gnus-button-handle-describe-function (url)
6370   "Call `describe-function' when pushing the corresponding URL button."
6371   (describe-function
6372    (intern
6373     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6374
6375 (defun gnus-button-handle-describe-variable (url)
6376   "Call `describe-variable' when pushing the corresponding URL button."
6377   (describe-variable
6378    (intern
6379     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6380
6381 (defun gnus-button-handle-symbol (url)
6382 "Display help on variable or function.
6383 Calls `describe-variable' or `describe-function'."
6384   (let ((sym (intern url)))
6385     (cond
6386      ((fboundp sym) (describe-function sym))
6387      ((boundp sym) (describe-variable sym))
6388      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
6389
6390 (defun gnus-button-handle-describe-key (url)
6391   "Call `describe-key' when pushing the corresponding URL button."
6392   (let* ((key-string
6393           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
6394          (keys (ignore-errors (eval `(kbd ,key-string)))))
6395     (if keys
6396         (describe-key keys)
6397       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
6398
6399 (defun gnus-button-handle-apropos (url)
6400   "Call `apropos' when pushing the corresponding URL button."
6401   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6402
6403 (defun gnus-button-handle-apropos-command (url)
6404   "Call `apropos' when pushing the corresponding URL button."
6405   (apropos-command
6406    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6407
6408 (defun gnus-button-handle-apropos-variable (url)
6409   "Call `apropos' when pushing the corresponding URL button."
6410   (funcall
6411    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
6412    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6413
6414 (defun gnus-button-handle-apropos-documentation (url)
6415   "Call `apropos' when pushing the corresponding URL button."
6416   (funcall
6417    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
6418    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6419
6420 (defun gnus-button-handle-library (url)
6421   "Call `locate-library' when pushing the corresponding URL button."
6422   (gnus-message 9 "url=`%s'" url)
6423   (let* ((lib (locate-library url))
6424          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
6425     (if (not lib)
6426         (gnus-message 1 "Cannot locale library `%s'." url)
6427       (find-file-read-only file))))
6428
6429 (defun gnus-button-handle-ctan (url)
6430   "Call `browse-url' when pushing a CTAN URL button."
6431   (funcall
6432    gnus-button-ctan-handler
6433    (concat
6434     gnus-ctan-url
6435     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
6436
6437 (defcustom gnus-button-tex-level 5
6438   "*Integer that says how many TeX-related buttons Gnus will show.
6439 The higher the number, the more buttons will appear and the more false
6440 positives are possible.  Note that you can set this variable local to
6441 specifific groups.  Setting it higher in TeX groups is probably a good idea.
6442 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6443 how to set variables in specific groups."
6444   :group 'gnus-article-buttons
6445   :link '(custom-manual "(gnus)Group Parameters")
6446   :type 'integer)
6447
6448 (defcustom gnus-button-man-level 5
6449   "*Integer that says how many man-related buttons Gnus will show.
6450 The higher the number, the more buttons will appear and the more false
6451 positives are possible.  Note that you can set this variable local to
6452 specifific groups.  Setting it higher in Unix groups is probably a good idea.
6453 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6454 how to set variables in specific groups."
6455   :group 'gnus-article-buttons
6456   :link '(custom-manual "(gnus)Group Parameters")
6457   :type 'integer)
6458
6459 (defcustom gnus-button-emacs-level 5
6460   "*Integer that says how many emacs-related buttons Gnus will show.
6461 The higher the number, the more buttons will appear and the more false
6462 positives are possible.  Note that you can set this variable local to
6463 specifific groups.  Setting it higher in Emacs or Gnus related groups is
6464 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
6465 `gnus-parameters' on how to set variables in specific groups."
6466   :group 'gnus-article-buttons
6467   :link '(custom-manual "(gnus)Group Parameters")
6468   :type 'integer)
6469
6470 (defcustom gnus-button-message-level 5
6471   "*Integer that says how many buttons for news or mail messages will appear.
6472 The higher the number, the more buttons will appear and the more false
6473 positives are possible."
6474   ;; mail addresses, MIDs, URLs for news, ...
6475   :group 'gnus-article-buttons
6476   :type 'integer)
6477
6478 (defcustom gnus-button-browse-level 5
6479   "*Integer that says how many buttons for browsing will appear.
6480 The higher the number, the more buttons will appear and the more false
6481 positives are possible."
6482   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
6483   :group 'gnus-article-buttons
6484   :type 'integer)
6485
6486 (defcustom gnus-button-alist
6487   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
6488      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
6489     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
6490      gnus-button-handle-news 2)
6491     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
6492      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
6493     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
6494      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
6495     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
6496      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
6497     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
6498      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
6499     ("mailto:\\([-a-z.@_+0-9%=?]+\\)"
6500      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6501     ("\\bmailto:\\([^ \n\t]+\\)"
6502      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6503     ;; CTAN
6504     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
6505              gnus-button-ctan-directory-regexp
6506              "[^][>)!;:,'\n\t ]+\\)")
6507      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
6508     ((concat "\\btex-archive/\\("
6509              gnus-button-ctan-directory-regexp
6510              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
6511      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
6512     ((concat
6513       "\\b\\("
6514       gnus-button-ctan-directory-regexp
6515       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
6516      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
6517     ;; This is info
6518     ("\\binfo:\\(//\\)?\\([^'\">\n\t ]+\\)"
6519      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
6520     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
6521      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
6522     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
6523      ;; Info links like `C-h i d m CC Mode RET'
6524      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 2)
6525     ;; This is custom
6526     ("\\bcustom:\\(//\\)?\\([^'\">\n\t ]+\\)"
6527      0 (>= gnus-button-emacs-level 5) gnus-button-handle-custom 2)
6528     ("M-x[ \t\n]customize-[^ ]+[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
6529      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1)
6530     ;; Emacs help commands
6531     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6532      ;; regexp doesn't match arguments containing ` '.
6533      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
6534     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6535      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
6536     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6537      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
6538     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6539      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
6540     ;; The following entries may lead to many false positives so don't enable
6541     ;; them by default (use a high button level):
6542     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>"
6543      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6544     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
6545      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6546     ("`\\([a-z][a-z0-9]+-[a-z]+-[-a-z]+\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
6547      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
6548     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
6549      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
6550     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
6551      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
6552     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6553      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
6554     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6555      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
6556     ("`\\(\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
6557      ;; Unlike the other regexps we really have to require quoting
6558      ;; here to determine where it ends.
6559      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
6560     ;; This is how URLs _should_ be embedded in text (RFC 1738)...
6561     ("<URL: *\\([^<>]*\\)>"
6562      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6563     ;; Raw URLs.
6564     (gnus-button-url-regexp
6565      0 (>= gnus-button-browse-level 0) browse-url 0)
6566     ;; man pages
6567     ("\\b\\([a-z][a-z]+\\)([1-9])\\W"
6568      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
6569      gnus-button-handle-man 1)
6570     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
6571     ("\\b\\([a-z][-_.a-z0-9]+\\)([1-9])\\W"
6572      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
6573      gnus-button-handle-man 1)
6574     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
6575     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
6576     ("\\b\\([a-z][-_.:a-z0-9]+\\)([1-9][X1a-z]*)\\W\\|\\b\\(X\\)([1-9])\\W"
6577      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
6578     ;; MID or mail: To avoid too many false positives we don't try to catch
6579     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
6580     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
6581     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
6582     ;; so that non-ambiguous entries (see above) match first.
6583     (gnus-button-mid-or-mail-regexp
6584      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
6585   "*Alist of regexps matching buttons in article bodies.
6586
6587 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
6588 REGEXP: is the string (case insensitive) matching text around the button (can
6589 also be lisp expression evaluating to a string),
6590 BUTTON: is the number of the regexp grouping actually matching the button,
6591 FORM: is a lisp expression which must eval to true for the button to
6592 be added,
6593 CALLBACK: is the function to call when the user push this button, and each
6594 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
6595
6596 CALLBACK can also be a variable, in that case the value of that
6597 variable it the real callback function."
6598   :group 'gnus-article-buttons
6599   :type '(repeat (list (choice regexp variable)
6600                        (integer :tag "Button")
6601                        (sexp :tag "Form")
6602                        (function :tag "Callback")
6603                        (repeat :tag "Par"
6604                                :inline t
6605                                (integer :tag "Regexp group")))))
6606
6607 (defcustom gnus-header-button-alist
6608   '(("^\\(References\\|Message-I[Dd]\\):" "<[^<>]+>"
6609      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
6610     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
6611      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
6612     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
6613      0 (>= gnus-button-message-level 0) gnus-button-mailto 0)
6614     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
6615      0 (>= gnus-button-browse-level 0) browse-url 0)
6616     ("^Subject:" gnus-button-url-regexp
6617      0 (>= gnus-button-browse-level 0) browse-url 0)
6618     ("^[^:]+:" gnus-button-url-regexp
6619      0 (>= gnus-button-browse-level 0) browse-url 0)
6620     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?]+\\)"
6621      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6622     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
6623      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
6624   "*Alist of headers and regexps to match buttons in article heads.
6625
6626 This alist is very similar to `gnus-button-alist', except that each
6627 alist has an additional HEADER element first in each entry:
6628
6629 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
6630
6631 HEADER is a regexp to match a header.  For a fuller explanation, see
6632 `gnus-button-alist'."
6633   :group 'gnus-article-buttons
6634   :group 'gnus-article-headers
6635   :type '(repeat (list (regexp :tag "Header")
6636                        regexp
6637                        (integer :tag "Button")
6638                        (sexp :tag "Form")
6639                        (function :tag "Callback")
6640                        (repeat :tag "Par"
6641                                :inline t
6642                                (integer :tag "Regexp group")))))
6643
6644 (defvar gnus-button-regexp nil)
6645 (defvar gnus-button-marker-list nil)
6646 ;; Regexp matching any of the regexps from `gnus-button-alist'.
6647
6648 (defvar gnus-button-last nil)
6649 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
6650
6651 ;;; Commands:
6652
6653 (defun gnus-article-push-button (event)
6654   "Check text under the mouse pointer for a callback function.
6655 If the text under the mouse pointer has a `gnus-callback' property,
6656 call it with the value of the `gnus-data' text property."
6657   (interactive "e")
6658   (set-buffer (window-buffer (posn-window (event-start event))))
6659   (let* ((pos (posn-point (event-start event)))
6660          (data (get-text-property pos 'gnus-data))
6661          (fun (get-text-property pos 'gnus-callback)))
6662     (goto-char pos)
6663     (when fun
6664       (funcall fun data))))
6665
6666 (defun gnus-article-press-button ()
6667   "Check text at point for a callback function.
6668 If the text at point has a `gnus-callback' property,
6669 call it with the value of the `gnus-data' text property."
6670   (interactive)
6671   (let ((data (get-text-property (point) 'gnus-data))
6672         (fun (get-text-property (point) 'gnus-callback)))
6673     (when fun
6674       (funcall fun data))))
6675
6676 (defun gnus-article-prev-button (n)
6677   "Move point to N buttons backward.
6678 If N is negative, move forward instead."
6679   (interactive "p")
6680   (gnus-article-next-button (- n)))
6681
6682 (defun gnus-article-next-button (n)
6683   "Move point to N buttons forward.
6684 If N is negative, move backward instead."
6685   (interactive "p")
6686   (let ((function (if (< n 0) 'previous-single-property-change
6687                     'next-single-property-change))
6688         (inhibit-point-motion-hooks t)
6689         (backward (< n 0))
6690         (limit (if (< n 0) (point-min) (point-max))))
6691     (setq n (abs n))
6692     (while (and (not (= limit (point)))
6693                 (> n 0))
6694       ;; Skip past the current button.
6695       (when (get-text-property (point) 'gnus-callback)
6696         (goto-char (funcall function (point) 'gnus-callback nil limit)))
6697       ;; Go to the next (or previous) button.
6698       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
6699       ;; Put point at the start of the button.
6700       (when (and backward (not (get-text-property (point) 'gnus-callback)))
6701         (goto-char (funcall function (point) 'gnus-callback nil limit)))
6702       ;; Skip past intangible buttons.
6703       (when (get-text-property (point) 'intangible)
6704         (incf n))
6705       (decf n))
6706     (unless (zerop n)
6707       (gnus-message 5 "No more buttons"))
6708     n))
6709
6710 (defun gnus-article-highlight (&optional force)
6711   "Highlight current article.
6712 This function calls `gnus-article-highlight-headers',
6713 `gnus-article-highlight-citation',
6714 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6715 do the highlighting.  See the documentation for those functions."
6716   (interactive (list 'force))
6717   (gnus-article-highlight-headers)
6718   (gnus-article-highlight-citation force)
6719   (gnus-article-highlight-signature)
6720   (gnus-article-add-buttons force)
6721   (gnus-article-add-buttons-to-head))
6722
6723 (defun gnus-article-highlight-some (&optional force)
6724   "Highlight current article.
6725 This function calls `gnus-article-highlight-headers',
6726 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6727 do the highlighting.  See the documentation for those functions."
6728   (interactive (list 'force))
6729   (gnus-article-highlight-headers)
6730   (gnus-article-highlight-signature)
6731   (gnus-article-add-buttons))
6732
6733 (defun gnus-article-highlight-headers ()
6734   "Highlight article headers as specified by `gnus-header-face-alist'."
6735   (interactive)
6736   (save-excursion
6737     (set-buffer gnus-article-buffer)
6738     (save-restriction
6739       (let ((alist gnus-header-face-alist)
6740             (buffer-read-only nil)
6741             (case-fold-search t)
6742             (inhibit-point-motion-hooks t)
6743             entry regexp header-face field-face from hpoints fpoints)
6744         (article-narrow-to-head)
6745         (while (setq entry (pop alist))
6746           (goto-char (point-min))
6747           (setq regexp (concat "^\\("
6748                                (if (string-equal "" (nth 0 entry))
6749                                    "[^\t ]"
6750                                  (nth 0 entry))
6751                                "\\)")
6752                 header-face (nth 1 entry)
6753                 field-face (nth 2 entry))
6754           (while (and (re-search-forward regexp nil t)
6755                       (not (eobp)))
6756             (beginning-of-line)
6757             (setq from (point))
6758             (unless (search-forward ":" nil t)
6759               (forward-char 1))
6760             (when (and header-face
6761                        (not (memq (point) hpoints)))
6762               (push (point) hpoints)
6763               (gnus-put-text-property from (point) 'face header-face))
6764             (when (and field-face
6765                        (not (memq (setq from (point)) fpoints)))
6766               (push from fpoints)
6767               (if (re-search-forward "^[^ \t]" nil t)
6768                   (forward-char -2)
6769                 (goto-char (point-max)))
6770               (gnus-put-text-property from (point) 'face field-face))))))))
6771
6772 (defun gnus-article-highlight-signature ()
6773   "Highlight the signature in an article.
6774 It does this by highlighting everything after
6775 `gnus-signature-separator' using `gnus-signature-face'."
6776   (interactive)
6777   (when gnus-signature-face
6778     (save-excursion
6779       (set-buffer gnus-article-buffer)
6780       (let ((buffer-read-only nil)
6781             (inhibit-point-motion-hooks t))
6782         (save-restriction
6783           (when (gnus-article-narrow-to-signature)
6784             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
6785                               'face gnus-signature-face)))))))
6786
6787 (defun gnus-article-buttonize-signature ()
6788   "Add button to the signature."
6789   (interactive)
6790   (save-excursion
6791     (set-buffer gnus-article-buffer)
6792     (let ((buffer-read-only nil)
6793           (inhibit-point-motion-hooks t))
6794       (when (gnus-article-search-signature)
6795         (gnus-article-add-button (match-beginning 0) (match-end 0)
6796                                  'gnus-signature-toggle
6797                                  (set-marker (make-marker)
6798                                              (1+ (match-end 0))))))))
6799
6800 (defun gnus-button-in-region-p (b e prop)
6801   "Say whether PROP exists in the region."
6802   (text-property-not-all b e prop nil))
6803
6804 (defun gnus-article-add-buttons (&optional force)
6805   "Find external references in the article and make buttons of them.
6806 \"External references\" are things like Message-IDs and URLs, as
6807 specified by `gnus-button-alist'."
6808   (interactive (list 'force))
6809   (save-excursion
6810     (set-buffer gnus-article-buffer)
6811     (let ((buffer-read-only nil)
6812           (inhibit-point-motion-hooks t)
6813           (case-fold-search t)
6814           (alist gnus-button-alist)
6815           beg entry regexp)
6816       ;; Remove all old markers.
6817       (let (marker entry new-list)
6818         (while (setq marker (pop gnus-button-marker-list))
6819           (if (or (< marker (point-min)) (>= marker (point-max)))
6820               (push marker new-list)
6821             (goto-char marker)
6822             (when (setq entry (gnus-button-entry))
6823               (put-text-property (match-beginning (nth 1 entry))
6824                                  (match-end (nth 1 entry))
6825                                  'gnus-callback nil))
6826             (set-marker marker nil)))
6827         (setq gnus-button-marker-list new-list))
6828       ;; We skip the headers.
6829       (article-goto-body)
6830       (setq beg (point))
6831       (while (setq entry (pop alist))
6832         (setq regexp (eval (car entry)))
6833         (goto-char beg)
6834         (while (re-search-forward regexp nil t)
6835           (let* ((start (and entry (match-beginning (nth 1 entry))))
6836                  (end (and entry (match-end (nth 1 entry))))
6837                  (from (match-beginning 0)))
6838             (when (and (or (eq t (nth 2 entry))
6839                            (eval (nth 2 entry)))
6840                        (not (gnus-button-in-region-p
6841                              start end 'gnus-callback)))
6842               ;; That optional form returned non-nil, so we add the
6843               ;; button.
6844               (gnus-article-add-button
6845                start end 'gnus-button-push
6846                (car (push (set-marker (make-marker) from)
6847                           gnus-button-marker-list))))))))))
6848
6849 ;; Add buttons to the head of an article.
6850 (defun gnus-article-add-buttons-to-head ()
6851   "Add buttons to the head of the article."
6852   (interactive)
6853   (save-excursion
6854     (set-buffer gnus-article-buffer)
6855     (save-restriction
6856       (let ((buffer-read-only nil)
6857             (inhibit-point-motion-hooks t)
6858             (case-fold-search t)
6859             (alist gnus-header-button-alist)
6860             entry beg end)
6861         (article-narrow-to-head)
6862         (while alist
6863           ;; Each alist entry.
6864           (setq entry (car alist)
6865                 alist (cdr alist))
6866           (goto-char (point-min))
6867           (while (re-search-forward (car entry) nil t)
6868             ;; Each header matching the entry.
6869             (setq beg (match-beginning 0))
6870             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
6871                                (match-beginning 0))
6872                           (point-max)))
6873             (goto-char beg)
6874             (while (re-search-forward (eval (nth 1 entry)) end t)
6875               ;; Each match within a header.
6876               (let* ((entry (cdr entry))
6877                      (start (match-beginning (nth 1 entry)))
6878                      (end (match-end (nth 1 entry)))
6879                      (form (nth 2 entry)))
6880                 (goto-char (match-end 0))
6881                 (when (eval form)
6882                   (gnus-article-add-button
6883                    start end (nth 3 entry)
6884                    (buffer-substring (match-beginning (nth 4 entry))
6885                                      (match-end (nth 4 entry)))))))
6886             (goto-char end)))))))
6887
6888 ;;; External functions:
6889
6890 (defun gnus-article-add-button (from to fun &optional data)
6891   "Create a button between FROM and TO with callback FUN and data DATA."
6892   (when gnus-article-button-face
6893     (gnus-overlay-put (gnus-make-overlay from to)
6894                       'face gnus-article-button-face))
6895   (gnus-add-text-properties
6896    from to
6897    (nconc (and gnus-article-mouse-face
6898                (list gnus-mouse-face-prop gnus-article-mouse-face))
6899           (list 'gnus-callback fun)
6900           (and data (list 'gnus-data data))))
6901   (widget-convert-button 'link from to :action 'gnus-widget-press-button
6902                          ;; Quote `:button-keymap' for Mule 2.3
6903                          ;; but it won't work.
6904                          ':button-keymap gnus-widget-button-keymap))
6905
6906 ;;; Internal functions:
6907
6908 (defun gnus-article-set-globals ()
6909   (save-excursion
6910     (set-buffer gnus-summary-buffer)
6911     (gnus-set-global-variables)))
6912
6913 (defun gnus-signature-toggle (end)
6914   (save-excursion
6915     (set-buffer gnus-article-buffer)
6916     (let ((buffer-read-only nil)
6917           (inhibit-point-motion-hooks t)
6918           (limit (next-single-property-change end 'mime-view-entity
6919                                               nil (point-max))))
6920       (if (text-property-any end limit 'article-type 'signature)
6921           (progn
6922             (gnus-delete-wash-type 'signature)
6923             (gnus-remove-text-properties-when
6924              'article-type 'signature end limit
6925              (cons 'article-type (cons 'signature
6926                                        gnus-hidden-properties))))
6927         (gnus-add-wash-type 'signature)
6928         (gnus-add-text-properties-when
6929          'article-type nil end limit
6930          (cons 'article-type (cons 'signature
6931                                    gnus-hidden-properties)))))
6932     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
6933       (gnus-set-mode-line 'article))))
6934
6935 (defun gnus-button-entry ()
6936   ;; Return the first entry in `gnus-button-alist' matching this place.
6937   (let ((alist gnus-button-alist)
6938         (entry nil))
6939     (while alist
6940       (setq entry (pop alist))
6941       (if (looking-at (eval (car entry)))
6942           (setq alist nil)
6943         (setq entry nil)))
6944     entry))
6945
6946 (defun gnus-button-push (marker)
6947   ;; Push button starting at MARKER.
6948   (save-excursion
6949     (goto-char marker)
6950     (let* ((entry (gnus-button-entry))
6951            (inhibit-point-motion-hooks t)
6952            (fun (nth 3 entry))
6953            (args (mapcar (lambda (group)
6954                            (let ((string (match-string group)))
6955                              (gnus-set-text-properties
6956                               0 (length string) nil string)
6957                              string))
6958                          (nthcdr 4 entry))))
6959       (cond
6960        ((fboundp fun)
6961         (apply fun args))
6962        ((and (boundp fun)
6963              (fboundp (symbol-value fun)))
6964         (apply (symbol-value fun) args))
6965        (t
6966         (gnus-message 1 "You must define `%S' to use this button"
6967                       (cons fun args)))))))
6968
6969 (defun gnus-parse-news-url (url)
6970   (let (scheme server group message-id articles)
6971     (with-temp-buffer
6972       (insert url)
6973       (goto-char (point-min))
6974       (when (looking-at "\\([A-Za-z]+\\):")
6975         (setq scheme (match-string 1))
6976         (goto-char (match-end 0)))
6977       (when (looking-at "//\\([^/]+\\)/")
6978         (setq server (match-string 1))
6979         (goto-char (match-end 0)))
6980
6981       (cond
6982        ((looking-at "\\(.*@.*\\)")
6983         (setq message-id (match-string 1)))
6984        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
6985         (setq group (match-string 1)
6986               articles (split-string (match-string 2) "-")))
6987        ((looking-at "\\([^/]+\\)/?")
6988         (setq group (match-string 1)))
6989        (t
6990         (error "Unknown news URL syntax"))))
6991     (list scheme server group message-id articles)))
6992
6993 (defun gnus-button-handle-news (url)
6994   "Fetch a news URL."
6995   (destructuring-bind (scheme server group message-id articles)
6996       (gnus-parse-news-url url)
6997     (cond
6998      (message-id
6999       (save-excursion
7000         (set-buffer gnus-summary-buffer)
7001         (if server
7002             (let ((gnus-refer-article-method (list (list 'nntp server))))
7003               (gnus-summary-refer-article message-id))
7004           (gnus-summary-refer-article message-id))))
7005      (group
7006       (gnus-button-fetch-group url)))))
7007
7008 (defun gnus-button-handle-man (url)
7009   "Fetch a man page."
7010   (funcall gnus-button-man-handler url))
7011
7012 (defun gnus-button-handle-info-url (url)
7013   "Fetch an info URL."
7014   (cond
7015    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7016     (gnus-info-find-node
7017      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7018                      "Gnus")
7019              ")" (gnus-url-unhex-string (match-string 2 url)))))
7020    ((string-match "([^)\"]+)[^\"]+" url)
7021     (setq url
7022           (gnus-replace-in-string
7023            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7024     (gnus-info-find-node url))
7025    (t (error "Can't parse %s" url))))
7026
7027 (defun gnus-button-handle-info-keystrokes (url)
7028   "Call `info' when pushing the corresponding URL button."
7029   ;; For links like `C-h i d m gnus RET', `C-h i d m CC Mode RET'.
7030   (info)
7031   (Info-directory)
7032   (Info-menu url))
7033
7034 (defun gnus-button-message-id (message-id)
7035   "Fetch MESSAGE-ID."
7036   (save-excursion
7037     (set-buffer gnus-summary-buffer)
7038     (gnus-summary-refer-article message-id)))
7039
7040 (defun gnus-button-fetch-group (address)
7041   "Fetch GROUP specified by ADDRESS."
7042   (if (not (string-match "[:/]" address))
7043       ;; This is just a simple group url.
7044       (gnus-group-read-ephemeral-group address gnus-select-method)
7045     (if (not
7046          (string-match
7047           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7048           address))
7049         (error "Can't parse %s" address)
7050       (gnus-group-read-ephemeral-group
7051        (match-string 4 address)
7052        `(nntp ,(match-string 1 address)
7053               (nntp-address ,(match-string 1 address))
7054               (nntp-port-number ,(if (match-end 3)
7055                                      (match-string 3 address)
7056                                    "nntp")))
7057        nil nil nil
7058        (and (match-end 6) (list (string-to-int (match-string 6 address))))))))
7059
7060 (defun gnus-url-parse-query-string (query &optional downcase)
7061   (let (retval pairs cur key val)
7062     (setq pairs (split-string query "&"))
7063     (while pairs
7064       (setq cur (car pairs)
7065             pairs (cdr pairs))
7066       (if (not (string-match "=" cur))
7067           nil                           ; Grace
7068         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7069               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7070         (if downcase
7071             (setq key (downcase key)))
7072         (setq cur (assoc key retval))
7073         (if cur
7074             (setcdr cur (cons val (cdr cur)))
7075           (setq retval (cons (list key val) retval)))))
7076     retval))
7077
7078 (defun gnus-url-mailto (url)
7079   ;; Send mail to someone
7080   (when (string-match "mailto:/*\\(.*\\)" url)
7081     (setq url (substring url (match-beginning 1) nil)))
7082   (let (to args subject func)
7083     (if (string-match (regexp-quote "?") url)
7084         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
7085               args (gnus-url-parse-query-string
7086                     (substring url (match-end 0) nil) t))
7087       (setq to (gnus-url-unhex-string url)))
7088     (setq args (cons (list "to" to) args)
7089           subject (cdr-safe (assoc "subject" args)))
7090     (gnus-msg-mail)
7091     (while args
7092       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
7093       (if (fboundp func)
7094           (funcall func)
7095         (message-position-on-field (caar args)))
7096       (insert (mapconcat 'identity (cdar args) ", "))
7097       (setq args (cdr args)))
7098     (if subject
7099         (message-goto-body)
7100       (message-goto-subject))))
7101
7102 (defun gnus-button-embedded-url (address)
7103   "Activate ADDRESS with `browse-url'."
7104   (browse-url (gnus-strip-whitespace address)))
7105
7106 ;;; Next/prev buttons in the article buffer.
7107
7108 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
7109 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
7110
7111 (defvar gnus-prev-page-map
7112   (let ((map (make-sparse-keymap)))
7113     (unless (>= emacs-major-version 21)
7114       ;; XEmacs doesn't care.
7115       (set-keymap-parent map gnus-article-mode-map))
7116     (define-key map gnus-mouse-2 'gnus-button-prev-page)
7117     (define-key map "\r" 'gnus-button-prev-page)
7118     map))
7119
7120 (defun gnus-insert-prev-page-button ()
7121   (let ((b (point))
7122         (buffer-read-only nil)
7123         (situation (get-text-property (point-min) 'mime-view-situation)))
7124     (gnus-eval-format
7125      gnus-prev-page-line-format nil
7126      `(,@(gnus-local-map-property gnus-prev-page-map)
7127          gnus-prev t
7128          gnus-callback gnus-article-button-prev-page
7129          article-type annotation
7130          mime-view-situation ,situation))
7131     (widget-convert-button
7132      'link b (if (bolp)
7133                  ;; Exclude a newline.
7134                  (1- (point))
7135                (point))
7136      :action 'gnus-button-prev-page
7137      :button-keymap gnus-prev-page-map)))
7138
7139 (defvar gnus-next-page-map
7140   (let ((map (make-sparse-keymap)))
7141     (unless (>= emacs-major-version 21)
7142       ;; XEmacs doesn't care.
7143       (set-keymap-parent map gnus-article-mode-map))
7144     (define-key map gnus-mouse-2 'gnus-button-next-page)
7145     (define-key map "\r" 'gnus-button-next-page)
7146     map))
7147
7148 (defun gnus-button-next-page (&optional args more-args)
7149   "Go to the next page."
7150   (interactive)
7151   (let ((win (selected-window)))
7152     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7153     (gnus-article-next-page)
7154     (select-window win)))
7155
7156 (defun gnus-button-prev-page (&optional args more-args)
7157   "Go to the prev page."
7158   (interactive)
7159   (let ((win (selected-window)))
7160     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7161     (gnus-article-prev-page)
7162     (select-window win)))
7163
7164 (defun gnus-insert-next-page-button ()
7165   (let ((b (point))
7166         (buffer-read-only nil)
7167         (situation (get-text-property (point-min) 'mime-view-situation)))
7168     (gnus-eval-format gnus-next-page-line-format nil
7169                       `(,@(gnus-local-map-property gnus-next-page-map)
7170                           gnus-next t
7171                           gnus-callback gnus-article-button-next-page
7172                           article-type annotation
7173                           mime-view-situation ,situation))
7174     (widget-convert-button
7175      'link b (if (bolp)
7176                  ;; Exclude a newline.
7177                  (1- (point))
7178                (point))
7179      :action 'gnus-button-next-page
7180      :button-keymap gnus-next-page-map)))
7181
7182 (defun gnus-article-button-next-page (arg)
7183   "Go to the next page."
7184   (interactive "P")
7185   (let ((win (selected-window)))
7186     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7187     (gnus-article-next-page)
7188     (select-window win)))
7189
7190 (defun gnus-article-button-prev-page (arg)
7191   "Go to the prev page."
7192   (interactive "P")
7193   (let ((win (selected-window)))
7194     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7195     (gnus-article-prev-page)
7196     (select-window win)))
7197
7198 (defvar gnus-decode-header-methods
7199   '(mail-decode-encoded-word-region)
7200   "List of methods used to decode headers.
7201
7202 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
7203 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
7204 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
7205 whose names match REGEXP.
7206
7207 For example:
7208 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
7209  mail-decode-encoded-word-region
7210  (\"chinese\" . rfc1843-decode-region))
7211 ")
7212
7213 (defvar gnus-decode-header-methods-cache nil)
7214
7215 (defun gnus-multi-decode-header (start end)
7216   "Apply the functions from `gnus-encoded-word-methods' that match."
7217   (unless (and gnus-decode-header-methods-cache
7218                (eq gnus-newsgroup-name
7219                    (car gnus-decode-header-methods-cache)))
7220     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
7221     (mapcar (lambda (x)
7222               (if (symbolp x)
7223                   (nconc gnus-decode-header-methods-cache (list x))
7224                 (if (and gnus-newsgroup-name
7225                          (string-match (car x) gnus-newsgroup-name))
7226                     (nconc gnus-decode-header-methods-cache
7227                            (list (cdr x))))))
7228             gnus-decode-header-methods))
7229   (let ((xlist gnus-decode-header-methods-cache))
7230     (pop xlist)
7231     (save-restriction
7232       (narrow-to-region start end)
7233       (while xlist
7234         (funcall (pop xlist) (point-min) (point-max))))))
7235
7236 ;;;
7237 ;;; Treatment top-level handling.
7238 ;;;
7239
7240 (defun gnus-treat-article (condition &optional part-number total-parts type)
7241   (let ((length (- (point-max) (point-min)))
7242         (alist gnus-treatment-function-alist)
7243         (article-goto-body-goes-to-point-min-p t)
7244         (treated-type
7245          (or (not type)
7246              (catch 'found
7247                (let ((list gnus-article-treat-types))
7248                  (while list
7249                    (when (string-match (pop list) type)
7250                      (throw 'found t)))))))
7251         (highlightp (gnus-visual-p 'article-highlight 'highlight))
7252         (entity (static-unless (featurep 'xemacs)
7253                   (when (eq 'head condition)
7254                     (get-text-property (point-min) 'mime-view-entity))))
7255         val elem buttonized)
7256     (gnus-run-hooks 'gnus-part-display-hook)
7257     (unless gnus-inhibit-treatment
7258       (dolist (elem alist)
7259         (setq val
7260               (save-excursion
7261                 (when (gnus-buffer-live-p gnus-summary-buffer)
7262                   (set-buffer gnus-summary-buffer))
7263                 (symbol-value (car elem))))
7264         (when (and (or (consp val)
7265                        treated-type)
7266                    (gnus-treat-predicate val)
7267                    (or (not (get (car elem) 'highlight))
7268                        highlightp))
7269           (when (and (not buttonized)
7270                      (memq (car elem)
7271                            '(gnus-treat-hide-signature
7272                              gnus-treat-highlight-signature)))
7273             (gnus-article-buttonize-signature)
7274             (setq buttonized t))
7275           (save-restriction
7276             (funcall (cadr elem)))))
7277       ;; FSF Emacsen does not inherit the existing text properties
7278       ;; in the new text, so we should do it for `mime-view-entity'.
7279       (static-unless (featurep 'xemacs)
7280         (when entity
7281           (put-text-property (point-min) (point-max)
7282                              'mime-view-entity entity))))))
7283
7284 ;; Dynamic variables.
7285 (eval-when-compile
7286   (defvar part-number)
7287   (defvar total-parts)
7288   (defvar type)
7289   (defvar condition)
7290   (defvar length))
7291
7292 (defun gnus-treat-predicate (val)
7293   (cond
7294    ((null val)
7295     nil)
7296    ((and (listp val)
7297          (stringp (car val)))
7298     (apply 'gnus-or (mapcar `(lambda (s)
7299                                (string-match s ,(or gnus-newsgroup-name "")))
7300                             val)))
7301    ((listp val)
7302     (let ((pred (pop val)))
7303       (cond
7304        ((eq pred 'or)
7305         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
7306        ((eq pred 'and)
7307         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
7308        ((eq pred 'not)
7309         (not (gnus-treat-predicate (car val))))
7310        ((eq pred 'typep)
7311         (equal (car val) type))
7312        (t
7313         (error "%S is not a valid predicate" pred)))))
7314    ((eq val 'mime)
7315     gnus-show-mime)
7316    (condition
7317     (eq condition val))
7318    ((eq val t)
7319     t)
7320    ((eq val 'head)
7321     nil)
7322    ((eq val 'last)
7323     (eq part-number total-parts))
7324    ((numberp val)
7325     (< length val))
7326    (t
7327     (error "%S is not a valid value" val))))
7328
7329 (defun gnus-article-encrypt-body (protocol &optional n)
7330   "Encrypt the article body."
7331   (interactive
7332    (list
7333     (or gnus-article-encrypt-protocol
7334         (completing-read "Encrypt protocol: "
7335                          gnus-article-encrypt-protocol-alist
7336                          nil t))
7337     current-prefix-arg))
7338   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
7339     (unless func
7340       (error (format "Can't find the encrypt protocol %s" protocol)))
7341     (if (member gnus-newsgroup-name '("nndraft:delayed"
7342                                       "nndraft:drafts"
7343                                       "nndraft:queue"))
7344         (error "Can't encrypt the article in group %s"
7345                gnus-newsgroup-name))
7346     (gnus-summary-iterate n
7347       (save-excursion
7348         (set-buffer gnus-summary-buffer)
7349         (let ((mail-parse-charset gnus-newsgroup-charset)
7350               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
7351               (summary-buffer gnus-summary-buffer)
7352               references point)
7353           (gnus-set-global-variables)
7354           (when (gnus-group-read-only-p)
7355             (error "The current newsgroup does not support article encrypt"))
7356           (gnus-summary-show-article t)
7357           (setq references
7358                 (or (mail-header-references gnus-current-headers) ""))
7359           (set-buffer gnus-article-buffer)
7360           (let* ((buffer-read-only nil)
7361                  (headers
7362                   (mapcar (lambda (field)
7363                             (and (save-restriction
7364                                    (message-narrow-to-head)
7365                                    (goto-char (point-min))
7366                                    (search-forward field nil t))
7367                                  (prog2
7368                                      (message-narrow-to-field)
7369                                      (buffer-string)
7370                                    (delete-region (point-min) (point-max))
7371                                    (widen))))
7372                           '("Content-Type:" "Content-Transfer-Encoding:"
7373                             "Content-Disposition:"))))
7374             (message-narrow-to-head)
7375             (message-remove-header "MIME-Version")
7376             (goto-char (point-max))
7377             (setq point (point))
7378             (insert (apply 'concat headers))
7379             (widen)
7380             (narrow-to-region point (point-max))
7381             (let ((message-options message-options))
7382               (message-options-set 'message-sender user-mail-address)
7383               (message-options-set 'message-recipients user-mail-address)
7384               (message-options-set 'message-sign-encrypt 'not)
7385               (funcall func))
7386             (goto-char (point-min))
7387             (insert "MIME-Version: 1.0\n")
7388             (widen)
7389             (gnus-summary-edit-article-done
7390              references nil summary-buffer t))
7391           (when gnus-keep-backlog
7392             (gnus-backlog-remove-article
7393              (car gnus-article-current) (cdr gnus-article-current)))
7394           (save-excursion
7395             (when (get-buffer gnus-original-article-buffer)
7396               (set-buffer gnus-original-article-buffer)
7397               (setq gnus-original-article nil)))
7398           (when gnus-use-cache
7399             (gnus-cache-update-article
7400              (car gnus-article-current) (cdr gnus-article-current))))))))
7401
7402 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
7403   "The following specs can be used:
7404 %t  The security MIME type
7405 %i  Additional info
7406 %d  Details
7407 %D  Details if button is pressed")
7408
7409 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
7410   "The following specs can be used:
7411 %t  The security MIME type
7412 %i  Additional info
7413 %d  Details
7414 %D  Details if button is pressed")
7415
7416 (defvar gnus-mime-security-button-line-format-alist
7417   '((?t gnus-tmp-type ?s)
7418     (?i gnus-tmp-info ?s)
7419     (?d gnus-tmp-details ?s)
7420     (?D gnus-tmp-pressed-details ?s)))
7421
7422 (defvar gnus-mime-security-button-map
7423   (let ((map (make-sparse-keymap)))
7424     (unless (>= (string-to-number emacs-version) 21)
7425       (set-keymap-parent map gnus-article-mode-map))
7426     (define-key map gnus-mouse-2 'gnus-article-push-button)
7427     (define-key map "\r" 'gnus-article-press-button)
7428     map))
7429
7430 (defvar gnus-mime-security-details-buffer nil)
7431
7432 (defvar gnus-mime-security-button-pressed nil)
7433
7434 (defvar gnus-mime-security-show-details-inline t
7435   "If non-nil, show details in the article buffer.")
7436
7437 (defun gnus-mime-security-verify-or-decrypt (handle)
7438   (mm-remove-parts (cdr handle))
7439   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
7440         point buffer-read-only)
7441     (if region
7442         (goto-char (car region)))
7443     (save-restriction
7444       (narrow-to-region (point) (point))
7445       (with-current-buffer (mm-handle-multipart-original-buffer handle)
7446         (let* ((mm-verify-option 'known)
7447                (mm-decrypt-option 'known)
7448                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
7449           (unless (eq nparts (cdr handle))
7450             (mm-destroy-parts (cdr handle))
7451             (setcdr handle nparts))))
7452       (setq point (point))
7453       (gnus-mime-display-security handle)
7454       (goto-char (point-max)))
7455     (when region
7456       (delete-region (point) (cdr region))
7457       (set-marker (car region) nil)
7458       (set-marker (cdr region) nil))
7459     (goto-char point)))
7460
7461 (defun gnus-mime-security-show-details (handle)
7462   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
7463     (if (not details)
7464         (gnus-message 5 "No details.")
7465       (if gnus-mime-security-show-details-inline
7466           (let ((gnus-mime-security-button-pressed
7467                  (not (get-text-property (point) 'gnus-mime-details)))
7468                 (gnus-mime-security-button-line-format
7469                  (get-text-property (point) 'gnus-line-format))
7470                 buffer-read-only)
7471             (forward-char -1)
7472             (while (eq (get-text-property (point) 'gnus-line-format)
7473                        gnus-mime-security-button-line-format)
7474               (forward-char -1))
7475             (forward-char)
7476             (save-restriction
7477               (narrow-to-region (point) (point))
7478               (gnus-insert-mime-security-button handle))
7479             (delete-region (point)
7480                            (or (text-property-not-all
7481                                 (point) (point-max)
7482                                 'gnus-line-format
7483                                 gnus-mime-security-button-line-format)
7484                                (point-max))))
7485         ;; Not inlined.
7486         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
7487             (with-current-buffer gnus-mime-security-details-buffer
7488               (erase-buffer)
7489               t)
7490           (setq gnus-mime-security-details-buffer
7491                 (gnus-get-buffer-create "*MIME Security Details*")))
7492         (with-current-buffer gnus-mime-security-details-buffer
7493           (insert details)
7494           (goto-char (point-min)))
7495         (pop-to-buffer gnus-mime-security-details-buffer)))))
7496
7497 (defun gnus-mime-security-press-button (handle)
7498   (save-excursion
7499     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7500         (gnus-mime-security-show-details handle)
7501       (gnus-mime-security-verify-or-decrypt handle))))
7502
7503 (defun gnus-insert-mime-security-button (handle &optional displayed)
7504   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
7505          (gnus-tmp-type
7506           (concat
7507            (or (nth 2 (assoc protocol mm-verify-function-alist))
7508                (nth 2 (assoc protocol mm-decrypt-function-alist))
7509                "Unknown")
7510            (if (equal (car handle) "multipart/signed")
7511                " Signed" " Encrypted")
7512            " Part"))
7513          (gnus-tmp-info
7514           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7515               "Undecided"))
7516          (gnus-tmp-details
7517           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
7518          gnus-tmp-pressed-details
7519          b e)
7520     (setq gnus-tmp-details
7521           (if gnus-tmp-details
7522               (concat "\n" gnus-tmp-details)
7523             ""))
7524     (setq gnus-tmp-pressed-details
7525           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
7526     (unless (bolp)
7527       (insert "\n"))
7528     (setq b (point))
7529     (gnus-eval-format
7530      gnus-mime-security-button-line-format
7531      gnus-mime-security-button-line-format-alist
7532      `(,@(gnus-local-map-property gnus-mime-security-button-map)
7533          gnus-callback gnus-mime-security-press-button
7534          gnus-line-format ,gnus-mime-security-button-line-format
7535          gnus-mime-details ,gnus-mime-security-button-pressed
7536          article-type annotation
7537          gnus-data ,handle))
7538     (setq e (if (bolp)
7539                 ;; Exclude a newline.
7540                 (1- (point))
7541               (point)))
7542     (widget-convert-button
7543      'link b e
7544      :mime-handle handle
7545      :action 'gnus-widget-press-button
7546      :button-keymap gnus-mime-security-button-map
7547      :help-echo
7548      (lambda (widget/window &optional overlay pos)
7549        ;; Needed to properly clear the message due to a bug in
7550        ;; wid-edit (XEmacs only).
7551        (when (boundp 'help-echo-owns-message)
7552          (setq help-echo-owns-message t))
7553        (format
7554         "%S: show detail"
7555         (aref gnus-mouse-2 0))))))
7556
7557 (defun gnus-mime-display-security (handle)
7558   (save-restriction
7559     (narrow-to-region (point) (point))
7560     (unless (gnus-unbuttonized-mime-type-p (car handle))
7561       (gnus-insert-mime-security-button handle))
7562     (gnus-mime-display-mixed (cdr handle))
7563     (unless (bolp)
7564       (insert "\n"))
7565     (unless (gnus-unbuttonized-mime-type-p (car handle))
7566       (let ((gnus-mime-security-button-line-format
7567              gnus-mime-security-button-end-line-format))
7568         (gnus-insert-mime-security-button handle)))
7569     (mm-set-handle-multipart-parameter
7570      handle 'gnus-region
7571      (cons (set-marker (make-marker) (point-min))
7572            (set-marker (make-marker) (point-max))))))
7573
7574
7575 ;;; @ for mime-view
7576 ;;;
7577
7578 (defun gnus-article-header-presentation-method (entity situation)
7579   (mime-insert-header entity)
7580   (article-decode-group-name))
7581
7582 (set-alist 'mime-header-presentation-method-alist
7583            'gnus-original-article-mode
7584            #'gnus-article-header-presentation-method)
7585
7586 (defun gnus-mime-preview-quitting-method ()
7587   (mime-preview-kill-buffer)
7588   (delete-other-windows)
7589   (gnus-article-show-summary)
7590   (gnus-summary-select-article gnus-show-all-headers t))
7591
7592 (set-alist 'mime-preview-quitting-method-alist
7593            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
7594
7595 (set-alist 'mime-preview-following-method-alist
7596            'gnus-original-article-mode #'gnus-following-method)
7597
7598 (set-alist 'mime-preview-over-to-previous-method-alist
7599            'gnus-original-article-mode
7600            (lambda ()
7601              (if (> (point-min) 1)
7602                  (gnus-article-prev-page)
7603                (gnus-article-read-summary-keys
7604                 nil (gnus-character-to-event ?P)))))
7605
7606 (set-alist 'mime-preview-over-to-next-method-alist
7607            'gnus-original-article-mode'
7608            (lambda ()
7609              (if (< (point-max) (buffer-size))
7610                  (gnus-article-next-page)
7611                (gnus-article-read-summary-keys
7612                 nil (gnus-character-to-event ?N)))))
7613
7614
7615 ;;; @ end
7616 ;;;
7617
7618 (gnus-ems-redefine)
7619
7620 (provide 'gnus-art)
7621
7622 (run-hooks 'gnus-art-load-hook)
7623
7624 ;;; gnus-art.el ends here