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