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