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