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