* gnus-art.el (article-fill-long-lines): Fill-paragraph properly.
[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 (min (1+ (point)) (point-max)) (gnus-point-at-bol))
1799               (let ((goback (point-marker)))
1800                 (fill-paragraph nil)
1801                 (goto-char (marker-position goback)))
1802               (widen))
1803             (forward-line 1)))))))
1804
1805 (defun article-capitalize-sentences ()
1806   "Capitalize the first word in each sentence."
1807   (interactive)
1808   (save-excursion
1809     (let ((buffer-read-only nil)
1810           (paragraph-start "^[\n\^L]"))
1811       (article-goto-body)
1812       (while (not (eobp))
1813         (capitalize-word 1)
1814         (forward-sentence)))))
1815
1816 (defun article-remove-cr ()
1817   "Remove trailing CRs and then translate remaining CRs into LFs."
1818   (interactive)
1819   (save-excursion
1820     (let ((buffer-read-only nil))
1821       (goto-char (point-min))
1822       (while (re-search-forward "\r+$" nil t)
1823         (replace-match "" t t))
1824       (goto-char (point-min))
1825       (while (search-forward "\r" nil t)
1826         (replace-match "\n" t t)))))
1827
1828 (defun article-remove-trailing-blank-lines ()
1829   "Remove all trailing blank lines from the article."
1830   (interactive)
1831   (save-excursion
1832     (let ((buffer-read-only nil))
1833       (goto-char (point-max))
1834       (delete-region
1835        (point)
1836        (progn
1837          (while (and (not (bobp))
1838                      (looking-at "^[ \t]*$")
1839                      (not (gnus-annotation-in-region-p
1840                            (point) (gnus-point-at-eol))))
1841            (forward-line -1))
1842          (forward-line 1)
1843          (point))))))
1844
1845 (defun article-display-x-face (&optional force)
1846   "Look for an X-Face header and display it if present."
1847   (interactive (list 'force))
1848   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
1849     (gnus-with-article-headers
1850       ;; Delete the old process, if any.
1851       (when (process-status "article-x-face")
1852         (delete-process "article-x-face"))
1853       (if (memq 'xface gnus-article-wash-types)
1854           ;; We have already displayed X-Faces, so we remove them
1855           ;; instead.
1856           (gnus-delete-images 'xface)
1857         ;; Display X-Faces.
1858         (let (x-faces from face grey)
1859           (save-excursion
1860             (when (and wash-face-p
1861                        (progn
1862                          (goto-char (point-min))
1863                          (not (re-search-forward
1864                                "^X-Face\\(-[0-9]+\\)?:[\t ]*" nil t)))
1865                        (gnus-buffer-live-p gnus-original-article-buffer))
1866               ;; If type `W f', use gnus-original-article-buffer,
1867               ;; otherwise use the current buffer because displaying
1868               ;; RFC822 parts calls this function too.
1869               (set-buffer gnus-original-article-buffer))
1870             (save-restriction
1871               (mail-narrow-to-head)
1872               (if gnus-treat-display-grey-xface
1873                   (progn
1874                     (while (gnus-article-goto-header "X-Face\\(-[0-9]+\\)?")
1875                       (if (match-beginning 2)
1876                           (progn
1877                             (setq grey t)
1878                             (push (cons (- (string-to-number (match-string 2)))
1879                                         (mail-header-field-value))
1880                                   x-faces))
1881                         (push (cons 0 (mail-header-field-value)) x-faces)))
1882                     (dolist (x-face (prog1
1883                                         (if grey
1884                                             (sort x-faces 'car-less-than-car)
1885                                           (nreverse x-faces))
1886                                       (setq x-faces nil)))
1887                       (push (cdr x-face) x-faces)))
1888                 (while (gnus-article-goto-header "X-Face")
1889                   (push (mail-header-field-value) x-faces)))
1890               (setq from (message-fetch-field "from"))))
1891           (if grey
1892               (let ((xpm (gnus-convert-gray-x-face-to-xpm x-faces))
1893                     image)
1894                 (when xpm
1895                   (setq image (gnus-create-image xpm 'xpm t))
1896                   (gnus-article-goto-header "from")
1897                   (when (bobp)
1898                     (insert "From: [no `from' set]\n")
1899                     (forward-char -17))
1900                   (gnus-add-wash-type 'xface)
1901                   (gnus-add-image 'xface image)
1902                   (gnus-put-image image)))
1903             ;; Sending multiple EOFs to xv doesn't work, so we only do a
1904             ;; single external face.
1905             (when (stringp gnus-article-x-face-command)
1906               (setq x-faces (list (car x-faces))))
1907             (while (and (setq face (pop x-faces))
1908                         gnus-article-x-face-command
1909                         (or force
1910                             ;; Check whether this face is censored.
1911                             (not gnus-article-x-face-too-ugly)
1912                             (and gnus-article-x-face-too-ugly from
1913                                  (not (string-match gnus-article-x-face-too-ugly
1914                                                     from)))))
1915               ;; We display the face.
1916               (if (symbolp gnus-article-x-face-command)
1917                   ;; The command is a lisp function, so we call it.
1918                   (if (gnus-functionp gnus-article-x-face-command)
1919                       (funcall gnus-article-x-face-command face)
1920                     (error "%s is not a function" gnus-article-x-face-command))
1921                 ;; The command is a string, so we interpret the command
1922                 ;; as a, well, command, and fork it off.
1923                 (let ((process-connection-type nil))
1924                   (process-kill-without-query
1925                    (start-process
1926                     "article-x-face" nil shell-file-name shell-command-switch
1927                     gnus-article-x-face-command))
1928                   (with-temp-buffer
1929                     (insert face)
1930                     (process-send-region "article-x-face"
1931                                          (point-min) (point-max)))
1932                   (process-send-eof "article-x-face"))))))))))
1933
1934 (defun article-decode-mime-words ()
1935   "Decode all MIME-encoded words in the article."
1936   (interactive)
1937   (save-excursion
1938     (set-buffer gnus-article-buffer)
1939     (let ((inhibit-point-motion-hooks t)
1940           buffer-read-only
1941           (mail-parse-charset gnus-newsgroup-charset)
1942           (mail-parse-ignored-charsets
1943            (save-excursion (set-buffer gnus-summary-buffer)
1944                            gnus-newsgroup-ignored-charsets)))
1945       (mail-decode-encoded-word-region (point-min) (point-max)))))
1946
1947 (defun article-decode-charset (&optional prompt)
1948   "Decode charset-encoded text in the article.
1949 If PROMPT (the prefix), prompt for a coding system to use."
1950   (interactive "P")
1951   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1952         buffer-read-only
1953         (mail-parse-charset gnus-newsgroup-charset)
1954         (mail-parse-ignored-charsets
1955          (save-excursion (condition-case nil
1956                              (set-buffer gnus-summary-buffer)
1957                            (error))
1958                          gnus-newsgroup-ignored-charsets))
1959         ct cte ctl charset format)
1960   (save-excursion
1961     (save-restriction
1962       (article-narrow-to-head)
1963       (setq ct (message-fetch-field "Content-Type" t)
1964             cte (message-fetch-field "Content-Transfer-Encoding" t)
1965             ctl (and ct (ignore-errors
1966                           (mail-header-parse-content-type ct)))
1967             charset (cond
1968                      (prompt
1969                       (mm-read-coding-system "Charset to decode: "))
1970                      (ctl
1971                       (mail-content-type-get ctl 'charset)))
1972             format (and ctl (mail-content-type-get ctl 'format)))
1973       (when cte
1974         (setq cte (mail-header-strip cte)))
1975       (if (and ctl (not (string-match "/" (car ctl))))
1976           (setq ctl nil))
1977       (goto-char (point-max)))
1978     (forward-line 1)
1979     (save-restriction
1980       (narrow-to-region (point) (point-max))
1981       (when (and (eq mail-parse-charset 'gnus-decoded)
1982                  (eq (mm-body-7-or-8) '8bit))
1983         ;; The text code could have been decoded.
1984         (setq charset mail-parse-charset))
1985       (when (and (or (not ctl)
1986                      (equal (car ctl) "text/plain"))
1987                  (not format)) ;; article with format will decode later.
1988         (mm-decode-body
1989          charset (and cte (intern (downcase
1990                                    (gnus-strip-whitespace cte))))
1991          (car ctl)))))))
1992
1993 (defun article-decode-encoded-words ()
1994   "Remove encoded-word encoding from headers."
1995   (let ((inhibit-point-motion-hooks t)
1996         (mail-parse-charset gnus-newsgroup-charset)
1997         (mail-parse-ignored-charsets
1998          (save-excursion (condition-case nil
1999                              (set-buffer gnus-summary-buffer)
2000                            (error))
2001                          gnus-newsgroup-ignored-charsets))
2002         buffer-read-only)
2003     (save-restriction
2004       (article-narrow-to-head)
2005       (funcall gnus-decode-header-function (point-min) (point-max)))))
2006
2007 (defun article-decode-group-name ()
2008   "Decode group names in `Newsgroups:'."
2009   (let ((inhibit-point-motion-hooks t)
2010         buffer-read-only
2011         (method (gnus-find-method-for-group gnus-newsgroup-name)))
2012     (when (and (or gnus-group-name-charset-method-alist
2013                    gnus-group-name-charset-group-alist)
2014                (gnus-buffer-live-p gnus-original-article-buffer))
2015       (when (nnmail-fetch-field "Newsgroups")
2016         (nnheader-replace-header "Newsgroups"
2017                                  (gnus-decode-newsgroups
2018                                   (with-current-buffer
2019                                       gnus-original-article-buffer
2020                                     (nnmail-fetch-field "Newsgroups"))
2021                                   gnus-newsgroup-name method)))
2022       (when (nnmail-fetch-field "Followup-To")
2023         (nnheader-replace-header "Followup-To"
2024                                  (gnus-decode-newsgroups
2025                                   (with-current-buffer
2026                                       gnus-original-article-buffer
2027                                     (nnmail-fetch-field "Followup-To"))
2028                                   gnus-newsgroup-name method))))))
2029
2030 (defun article-de-quoted-unreadable (&optional force read-charset)
2031   "Translate a quoted-printable-encoded article.
2032 If FORCE, decode the article whether it is marked as quoted-printable
2033 or not.
2034 If READ-CHARSET, ask for a coding system."
2035   (interactive (list 'force current-prefix-arg))
2036   (save-excursion
2037     (let ((buffer-read-only nil) type charset)
2038       (if (gnus-buffer-live-p gnus-original-article-buffer)
2039           (with-current-buffer gnus-original-article-buffer
2040             (setq type
2041                   (gnus-fetch-field "content-transfer-encoding"))
2042             (let* ((ct (gnus-fetch-field "content-type"))
2043                    (ctl (and ct
2044                              (ignore-errors
2045                                (mail-header-parse-content-type ct)))))
2046               (setq charset (and ctl
2047                                  (mail-content-type-get ctl 'charset)))
2048               (if (stringp charset)
2049                   (setq charset (intern (downcase charset)))))))
2050       (if read-charset
2051           (setq charset (mm-read-coding-system "Charset: " charset)))
2052       (unless charset
2053         (setq charset gnus-newsgroup-charset))
2054       (when (or force
2055                 (and type (let ((case-fold-search t))
2056                             (string-match "quoted-printable" type))))
2057         (article-goto-body)
2058         (quoted-printable-decode-region
2059          (point) (point-max) (mm-charset-to-coding-system charset))))))
2060
2061 (defun article-de-base64-unreadable (&optional force read-charset)
2062   "Translate a base64 article.
2063 If FORCE, decode the article whether it is marked as base64 not.
2064 If READ-CHARSET, ask for a coding system."
2065   (interactive (list 'force current-prefix-arg))
2066   (save-excursion
2067     (let ((buffer-read-only nil) type charset)
2068       (if (gnus-buffer-live-p gnus-original-article-buffer)
2069           (with-current-buffer gnus-original-article-buffer
2070             (setq type
2071                   (gnus-fetch-field "content-transfer-encoding"))
2072             (let* ((ct (gnus-fetch-field "content-type"))
2073                    (ctl (and ct
2074                              (ignore-errors
2075                                (mail-header-parse-content-type ct)))))
2076               (setq charset (and ctl
2077                                  (mail-content-type-get ctl 'charset)))
2078               (if (stringp charset)
2079                   (setq charset (intern (downcase charset)))))))
2080       (if read-charset
2081           (setq charset (mm-read-coding-system "Charset: " charset)))
2082       (unless charset
2083         (setq charset gnus-newsgroup-charset))
2084       (when (or force
2085                 (and type (let ((case-fold-search t))
2086                             (string-match "base64" type))))
2087         (article-goto-body)
2088         (save-restriction
2089           (narrow-to-region (point) (point-max))
2090           (base64-decode-region (point-min) (point-max))
2091           (mm-decode-coding-region
2092            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2093
2094 (eval-when-compile
2095   (require 'rfc1843))
2096
2097 (defun article-decode-HZ ()
2098   "Translate a HZ-encoded article."
2099   (interactive)
2100   (require 'rfc1843)
2101   (save-excursion
2102     (let ((buffer-read-only nil))
2103       (rfc1843-decode-region (point-min) (point-max)))))
2104
2105 (defun article-unsplit-urls ()
2106   "Remove the newlines that some other mailers insert into URLs."
2107   (interactive)
2108   (save-excursion
2109     (let ((buffer-read-only nil))
2110       (goto-char (point-min))
2111       (while (re-search-forward
2112               "^\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2113         (replace-match "\\1\\3" t)))
2114     (when (and gnus-display-mime-function (interactive-p))
2115       (funcall gnus-display-mime-function))))
2116
2117
2118 (defun article-wash-html (&optional read-charset)
2119   "Format an html article.
2120 If READ-CHARSET, ask for a coding system."
2121   (interactive "P")
2122   (save-excursion
2123     (let ((buffer-read-only nil)
2124           charset)
2125       (if (gnus-buffer-live-p gnus-original-article-buffer)
2126           (with-current-buffer gnus-original-article-buffer
2127             (let* ((ct (gnus-fetch-field "content-type"))
2128                    (ctl (and ct
2129                              (ignore-errors
2130                                (mail-header-parse-content-type ct)))))
2131               (setq charset (and ctl
2132                                  (mail-content-type-get ctl 'charset)))
2133               (if (stringp charset)
2134                   (setq charset (intern (downcase charset)))))))
2135       (if read-charset
2136           (setq charset (mm-read-coding-system "Charset: " charset)))
2137       (unless charset
2138         (setq charset gnus-newsgroup-charset))
2139       (article-goto-body)
2140       (save-window-excursion
2141         (save-restriction
2142           (narrow-to-region (point) (point-max))
2143           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2144                  (entry (assq func mm-text-html-washer-alist)))
2145             (if entry
2146                 (setq func (cdr entry)))
2147             (cond
2148              ((gnus-functionp func)
2149               (funcall func))
2150              (t
2151               (apply (car func) (cdr func))))))))))
2152
2153 (defun gnus-article-wash-html-with-w3 ()
2154   "Wash the current buffer with w3."
2155   (mm-setup-w3)
2156   (let ((w3-strict-width (window-width))
2157         (url-standalone-mode t)
2158         (url-gateway-unplugged t)
2159         (w3-honor-stylesheets nil))
2160     (condition-case ()
2161         (w3-region (point-min) (point-max))
2162       (error))))
2163
2164 (defun gnus-article-wash-html-with-w3m ()
2165   "Wash the current buffer with emacs-w3m."
2166   (mm-setup-w3m)
2167   (save-restriction
2168     (narrow-to-region (point) (point-max))
2169     (let ((w3m-safe-url-regexp (if mm-inline-text-html-with-images
2170                                    nil
2171                                  "\\`cid:"))
2172           (w3m-display-inline-images mm-inline-text-html-with-images)
2173           w3m-force-redisplay)
2174       (w3m-region (point-min) (point-max)))
2175     (when mm-inline-text-html-with-w3m-keymap
2176       (add-text-properties
2177        (point-min) (point-max)
2178        (append '(mm-inline-text-html-with-w3m t)
2179                (gnus-local-map-property mm-w3m-mode-map))))))
2180
2181 (defun article-hide-list-identifiers ()
2182   "Remove list identifies from the Subject header.
2183 The `gnus-list-identifiers' variable specifies what to do."
2184   (interactive)
2185   (let ((inhibit-point-motion-hooks t)
2186         (regexp (if (consp gnus-list-identifiers)
2187                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2188                   gnus-list-identifiers))
2189         buffer-read-only)
2190     (when regexp
2191       (save-excursion
2192         (save-restriction
2193           (article-narrow-to-head)
2194           (goto-char (point-min))
2195           (while (re-search-forward
2196                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2197                   nil t)
2198             (delete-region (match-beginning 2) (match-end 0))
2199             (beginning-of-line))
2200           (when (re-search-forward
2201                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2202             (delete-region (match-beginning 1) (match-end 1))))))))
2203
2204 (defun article-hide-pgp ()
2205   "Remove any PGP headers and signatures in the current article."
2206   (interactive)
2207   (save-excursion
2208     (save-restriction
2209       (let ((inhibit-point-motion-hooks t)
2210             buffer-read-only beg end)
2211         (article-goto-body)
2212         ;; Hide the "header".
2213         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
2214           (gnus-add-wash-type 'pgp)
2215           (delete-region (match-beginning 0) (match-end 0))
2216           ;; Remove armor headers (rfc2440 6.2)
2217           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
2218                                      (point)))
2219           (setq beg (point))
2220           ;; Hide the actual signature.
2221           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
2222                (setq end (1+ (match-beginning 0)))
2223                (delete-region
2224                 end
2225                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
2226                     (match-end 0)
2227                   ;; Perhaps we shouldn't hide to the end of the buffer
2228                   ;; if there is no end to the signature?
2229                   (point-max))))
2230           ;; Hide "- " PGP quotation markers.
2231           (when (and beg end)
2232             (narrow-to-region beg end)
2233             (goto-char (point-min))
2234             (while (re-search-forward "^- " nil t)
2235               (delete-region
2236                (match-beginning 0) (match-end 0)))
2237             (widen))
2238           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
2239
2240 (defun article-hide-pem (&optional arg)
2241   "Toggle hiding of any PEM headers and signatures in the current article.
2242 If given a negative prefix, always show; if given a positive prefix,
2243 always hide."
2244   (interactive (gnus-article-hidden-arg))
2245   (unless (gnus-article-check-hidden-text 'pem arg)
2246     (save-excursion
2247       (let (buffer-read-only end)
2248         (goto-char (point-min))
2249         ;; Hide the horrendously ugly "header".
2250         (when (and (search-forward
2251                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2252                     nil t)
2253                    (setq end (1+ (match-beginning 0))))
2254           (gnus-add-wash-type 'pem)
2255           (gnus-article-hide-text-type
2256            end
2257            (if (search-forward "\n\n" nil t)
2258                (match-end 0)
2259              (point-max))
2260            'pem)
2261           ;; Hide the trailer as well
2262           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2263                                 nil t)
2264             (gnus-article-hide-text-type
2265              (match-beginning 0) (match-end 0) 'pem)))))))
2266
2267 (defun article-strip-banner ()
2268   "Strip the banner specified by the `banner' group parameter."
2269   (interactive)
2270   (save-excursion
2271     (save-restriction
2272       (let ((inhibit-point-motion-hooks t)
2273             (banner (gnus-parameter-banner gnus-newsgroup-name))
2274             (gnus-signature-limit nil)
2275             buffer-read-only beg end)
2276         (when (and gnus-article-address-banner-alist
2277                    (not banner))
2278           (setq banner
2279                 (let ((from (save-restriction
2280                               (widen)
2281                               (article-narrow-to-head)
2282                               (caar (mail-header-parse-addresses
2283                                      (mail-fetch-field "from"))))))
2284                   (catch 'found
2285                     (dolist (pair gnus-article-address-banner-alist)
2286                       (when (string-match (car pair) from)
2287                         (throw 'found (cdr pair))))))))
2288         (when banner
2289           (article-goto-body)
2290           (cond
2291            ((eq banner 'signature)
2292             (when (gnus-article-narrow-to-signature)
2293               (widen)
2294               (forward-line -1)
2295               (delete-region (point) (point-max))))
2296            ((symbolp banner)
2297             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2298                 (while (re-search-forward banner nil t)
2299                   (delete-region (match-beginning 0) (match-end 0)))))
2300            ((stringp banner)
2301             (while (re-search-forward banner nil t)
2302               (delete-region (match-beginning 0) (match-end 0))))))))))
2303
2304 (defun article-babel ()
2305   "Translate article using an online translation service."
2306   (interactive)
2307   (require 'babel)
2308   (save-excursion
2309     (set-buffer gnus-article-buffer)
2310     (when (article-goto-body)
2311       (let* ((buffer-read-only nil)
2312              (start (point))
2313              (end (point-max))
2314              (orig (buffer-substring start end))
2315              (trans (babel-as-string orig)))
2316         (save-restriction
2317           (narrow-to-region start end)
2318           (delete-region start end)
2319           (insert trans))))))
2320
2321 (defun article-hide-signature (&optional arg)
2322   "Hide the signature in the current article.
2323 If given a negative prefix, always show; if given a positive prefix,
2324 always hide."
2325   (interactive (gnus-article-hidden-arg))
2326   (unless (gnus-article-check-hidden-text 'signature arg)
2327     (save-excursion
2328       (save-restriction
2329         (let ((buffer-read-only nil))
2330           (when (gnus-article-narrow-to-signature)
2331             (gnus-article-hide-text-type
2332              (point-min) (point-max) 'signature))))))
2333   (gnus-set-mode-line 'article))
2334
2335 (defun article-strip-headers-in-body ()
2336   "Strip offensive headers from bodies."
2337   (interactive)
2338   (save-excursion
2339     (article-goto-body)
2340     (let ((case-fold-search t))
2341       (when (looking-at "x-no-archive:")
2342         (gnus-delete-line)))))
2343
2344 (defun article-strip-leading-blank-lines ()
2345   "Remove all blank lines from the beginning of the article."
2346   (interactive)
2347   (save-excursion
2348     (let ((inhibit-point-motion-hooks t)
2349           buffer-read-only)
2350       (when (article-goto-body)
2351         (while (and (not (eobp))
2352                     (looking-at "[ \t]*$"))
2353           (gnus-delete-line))))))
2354
2355 (defun article-narrow-to-head ()
2356   "Narrow the buffer to the head of the message.
2357 Point is left at the beginning of the narrowed-to region."
2358   (narrow-to-region
2359    (goto-char (point-min))
2360    (if (search-forward "\n\n" nil 1)
2361        (1- (point))
2362      (point-max)))
2363   (goto-char (point-min)))
2364
2365 (defun article-goto-body ()
2366   "Place point at the start of the body."
2367   (goto-char (point-min))
2368   (cond
2369    ;; This variable is only bound when dealing with separate
2370    ;; MIME body parts.
2371    (article-goto-body-goes-to-point-min-p
2372     t)
2373    ((search-forward "\n\n" nil t)
2374     t)
2375    (t
2376     (goto-char (point-max))
2377     nil)))
2378
2379 (defun article-strip-multiple-blank-lines ()
2380   "Replace consecutive blank lines with one empty line."
2381   (interactive)
2382   (save-excursion
2383     (let ((inhibit-point-motion-hooks t)
2384           buffer-read-only)
2385       ;; First make all blank lines empty.
2386       (article-goto-body)
2387       (while (re-search-forward "^[ \t]+$" nil t)
2388         (unless (gnus-annotation-in-region-p
2389                  (match-beginning 0) (match-end 0))
2390           (replace-match "" nil t)))
2391       ;; Then replace multiple empty lines with a single empty line.
2392       (article-goto-body)
2393       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2394         (unless (gnus-annotation-in-region-p
2395                  (match-beginning 0) (match-end 0))
2396           (delete-region (match-beginning 1) (match-end 1)))))))
2397
2398 (defun article-strip-leading-space ()
2399   "Remove all white space from the beginning of the lines in the article."
2400   (interactive)
2401   (save-excursion
2402     (let ((inhibit-point-motion-hooks t)
2403           buffer-read-only)
2404       (article-goto-body)
2405       (while (re-search-forward "^[ \t]+" nil t)
2406         (replace-match "" t t)))))
2407
2408 (defun article-strip-trailing-space ()
2409   "Remove all white space from the end of the lines in the article."
2410   (interactive)
2411   (save-excursion
2412     (let ((inhibit-point-motion-hooks t)
2413           buffer-read-only)
2414       (article-goto-body)
2415       (while (re-search-forward "[ \t]+$" nil t)
2416         (replace-match "" t t)))))
2417
2418 (defun article-strip-blank-lines ()
2419   "Strip leading, trailing and multiple blank lines."
2420   (interactive)
2421   (article-strip-leading-blank-lines)
2422   (article-remove-trailing-blank-lines)
2423   (article-strip-multiple-blank-lines))
2424
2425 (defun article-strip-all-blank-lines ()
2426   "Strip all blank lines."
2427   (interactive)
2428   (save-excursion
2429     (let ((inhibit-point-motion-hooks t)
2430           buffer-read-only)
2431       (article-goto-body)
2432       (while (re-search-forward "^[ \t]*\n" nil t)
2433         (replace-match "" t t)))))
2434
2435 (defun gnus-article-narrow-to-signature ()
2436   "Narrow to the signature; return t if a signature is found, else nil."
2437   (let ((inhibit-point-motion-hooks t))
2438     (when (gnus-article-search-signature)
2439       (forward-line 1)
2440       ;; Check whether we have some limits to what we consider
2441       ;; to be a signature.
2442       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2443                       (list gnus-signature-limit)))
2444             limit limited)
2445         (while (setq limit (pop limits))
2446           (if (or (and (integerp limit)
2447                        (< (- (point-max) (point)) limit))
2448                   (and (floatp limit)
2449                        (< (count-lines (point) (point-max)) limit))
2450                   (and (gnus-functionp limit)
2451                        (funcall limit))
2452                   (and (stringp limit)
2453                        (not (re-search-forward limit nil t))))
2454               ()                        ; This limit did not succeed.
2455             (setq limited t
2456                   limits nil)))
2457         (unless limited
2458           (narrow-to-region (point) (point-max))
2459           t)))))
2460
2461 (defun gnus-article-search-signature ()
2462   "Search the current buffer for the signature separator.
2463 Put point at the beginning of the signature separator."
2464   (let ((cur (point)))
2465     (goto-char (point-max))
2466     (if (if (stringp gnus-signature-separator)
2467             (re-search-backward gnus-signature-separator nil t)
2468           (let ((seps gnus-signature-separator))
2469             (while (and seps
2470                         (not (re-search-backward (car seps) nil t)))
2471               (pop seps))
2472             seps))
2473         t
2474       (goto-char cur)
2475       nil)))
2476
2477 (defun gnus-article-hidden-arg ()
2478   "Return the current prefix arg as a number, or 0 if no prefix."
2479   (list (if current-prefix-arg
2480             (prefix-numeric-value current-prefix-arg)
2481           0)))
2482
2483 (defun gnus-article-check-hidden-text (type arg)
2484   "Return nil if hiding is necessary.
2485 Arg can be nil or a number.  nil and positive means hide, negative
2486 means show, 0 means toggle."
2487   (save-excursion
2488     (save-restriction
2489       (let ((hide (gnus-article-hidden-text-p type)))
2490         (cond
2491          ((or (null arg)
2492               (> arg 0))
2493           nil)
2494          ((< arg 0)
2495           (gnus-article-show-hidden-text type)
2496           t)
2497          (t
2498           (if (eq hide 'hidden)
2499               (progn
2500                 (gnus-article-show-hidden-text type)
2501                 t)
2502             nil)))))))
2503
2504 (defun gnus-article-hidden-text-p (type)
2505   "Say whether the current buffer contains hidden text of type TYPE."
2506   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2507     (while (and pos
2508                 (not (get-text-property pos 'invisible))
2509                 (not (get-text-property pos 'dummy-invisible)))
2510       (setq pos
2511             (text-property-any (1+ pos) (point-max) 'article-type type)))
2512     (if pos
2513         'hidden
2514       nil)))
2515
2516 (defun gnus-article-show-hidden-text (type &optional dummy)
2517   "Show all hidden text of type TYPE.
2518 Originally it is hide instead of DUMMY."
2519   (let ((buffer-read-only nil)
2520         (inhibit-point-motion-hooks t))
2521     (gnus-remove-text-properties-when
2522      'article-type type
2523      (point-min) (point-max)
2524      (cons 'article-type (cons type
2525                                gnus-hidden-properties)))
2526     (gnus-delete-wash-type type)))
2527
2528 (defconst article-time-units
2529   `((year . ,(* 365.25 24 60 60))
2530     (week . ,(* 7 24 60 60))
2531     (day . ,(* 24 60 60))
2532     (hour . ,(* 60 60))
2533     (minute . 60)
2534     (second . 1))
2535   "Mapping from time units to seconds.")
2536
2537 (defun article-date-ut (&optional type highlight header)
2538   "Convert DATE date to universal time in the current article.
2539 If TYPE is `local', convert to local time; if it is `lapsed', output
2540 how much time has lapsed since DATE.  For `lapsed', the value of
2541 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2542 should replace the \"Date:\" one, or should be added below it."
2543   (interactive (list 'ut t))
2544   (let* ((header (or header
2545                      (message-fetch-field "date")
2546                      ""))
2547          (tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
2548          (date-regexp
2549           (cond
2550            ((not gnus-article-date-lapsed-new-header)
2551             tdate-regexp)
2552            ((eq type 'lapsed)
2553             "^X-Sent:[ \t]")
2554            (t
2555             "^Date:[ \t]")))
2556          (date (if (vectorp header) (mail-header-date header)
2557                  header))
2558          (inhibit-point-motion-hooks t)
2559          pos
2560          bface eface)
2561     (save-excursion
2562       (save-restriction
2563         (article-narrow-to-head)
2564         (when (re-search-forward tdate-regexp nil t)
2565           (setq bface (get-text-property (gnus-point-at-bol) 'face)
2566                 date (or (get-text-property (gnus-point-at-bol)
2567                                             'original-date)
2568                          date)
2569                 eface (get-text-property (1- (gnus-point-at-eol)) 'face))
2570           (forward-line 1))
2571         (when (and date (not (string= date "")))
2572           (goto-char (point-min))
2573           (let ((buffer-read-only nil))
2574             ;; Delete any old Date headers.
2575             (while (re-search-forward date-regexp nil t)
2576               (if pos
2577                   (delete-region (progn (beginning-of-line) (point))
2578                                  (progn (forward-line 1) (point)))
2579                 (delete-region (progn (beginning-of-line) (point))
2580                                (progn (end-of-line) (point)))
2581                 (setq pos (point))))
2582             (when (and (not pos) (re-search-forward tdate-regexp nil t))
2583               (forward-line 1))
2584             (if pos (goto-char pos))
2585             (insert (article-make-date-line date (or type 'ut)))
2586             (when (not pos)
2587               (insert "\n")
2588               (forward-line -1))
2589             ;; Do highlighting.
2590             (beginning-of-line)
2591             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2592               (put-text-property (match-beginning 1) (1+ (match-end 1))
2593                                  'original-date date)
2594               (put-text-property (match-beginning 1) (1+ (match-end 1))
2595                                  'face bface)
2596               (put-text-property (match-beginning 2) (match-end 2)
2597                                  'face eface))))))))
2598
2599 (defun article-make-date-line (date type)
2600   "Return a DATE line of TYPE."
2601   (unless (memq type '(local ut original user iso8601 lapsed english))
2602     (error "Unknown conversion type: %s" type))
2603   (condition-case ()
2604       (let ((time (date-to-time date)))
2605         (cond
2606          ;; Convert to the local timezone.
2607          ((eq type 'local)
2608           (let ((tz (car (current-time-zone time))))
2609             (format "Date: %s %s%02d%02d" (current-time-string time)
2610                     (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2611                     (/ (% (abs tz) 3600) 60))))
2612          ;; Convert to Universal Time.
2613          ((eq type 'ut)
2614           (concat "Date: "
2615                   (current-time-string
2616                    (let* ((e (parse-time-string date))
2617                           (tm (apply 'encode-time e))
2618                           (ms (car tm))
2619                           (ls (- (cadr tm) (car (current-time-zone time)))))
2620                      (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2621                            ((> ls 65535) (list (1+ ms) (- ls 65536)))
2622                            (t (list ms ls)))))
2623                   " UT"))
2624          ;; Get the original date from the article.
2625          ((eq type 'original)
2626           (concat "Date: " (if (string-match "\n+$" date)
2627                                (substring date 0 (match-beginning 0))
2628                              date)))
2629          ;; Let the user define the format.
2630          ((eq type 'user)
2631           (let ((format (or (condition-case nil
2632                                 (with-current-buffer gnus-summary-buffer
2633                                   gnus-article-time-format)
2634                               (error nil))
2635                             gnus-article-time-format)))
2636             (if (gnus-functionp format)
2637                 (funcall format time)
2638               (concat "Date: " (format-time-string format time)))))
2639          ;; ISO 8601.
2640          ((eq type 'iso8601)
2641           (let ((tz (car (current-time-zone time))))
2642             (concat
2643              "Date: "
2644              (format-time-string "%Y%m%dT%H%M%S" time)
2645              (format "%s%02d%02d"
2646                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2647                      (/ (% (abs tz) 3600) 60)))))
2648          ;; Do an X-Sent lapsed format.
2649          ((eq type 'lapsed)
2650           ;; If the date is seriously mangled, the timezone functions are
2651           ;; liable to bug out, so we ignore all errors.
2652           (let* ((now (current-time))
2653                  (real-time (subtract-time now time))
2654                  (real-sec (and real-time
2655                                 (+ (* (float (car real-time)) 65536)
2656                                    (cadr real-time))))
2657                  (sec (and real-time (abs real-sec)))
2658                  num prev)
2659             (cond
2660              ((null real-time)
2661               "X-Sent: Unknown")
2662              ((zerop sec)
2663               "X-Sent: Now")
2664              (t
2665               (concat
2666                "X-Sent: "
2667                ;; This is a bit convoluted, but basically we go
2668                ;; through the time units for years, weeks, etc,
2669                ;; and divide things to see whether that results
2670                ;; in positive answers.
2671                (mapconcat
2672                 (lambda (unit)
2673                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2674                       ;; The (remaining) seconds are too few to
2675                       ;; be divided into this time unit.
2676                       ""
2677                     ;; It's big enough, so we output it.
2678                     (setq sec (- sec (* num (cdr unit))))
2679                     (prog1
2680                         (concat (if prev ", " "") (int-to-string
2681                                                    (floor num))
2682                                 " " (symbol-name (car unit))
2683                                 (if (> num 1) "s" ""))
2684                       (setq prev t))))
2685                 article-time-units "")
2686                ;; If dates are odd, then it might appear like the
2687                ;; article was sent in the future.
2688                (if (> real-sec 0)
2689                    " ago"
2690                  " in the future"))))))
2691          ;; Display the date in proper English
2692          ((eq type 'english)
2693           (let ((dtime (decode-time time)))
2694             (concat
2695              "Date: the "
2696              (number-to-string (nth 3 dtime))
2697              (let ((digit (% (nth 3 dtime) 10)))
2698                (cond
2699                 ((memq (nth 3 dtime) '(11 12 13)) "th")
2700                 ((= digit 1) "st")
2701                 ((= digit 2) "nd")
2702                 ((= digit 3) "rd")
2703                 (t "th")))
2704              " of "
2705              (nth (1- (nth 4 dtime)) gnus-english-month-names)
2706              " "
2707              (number-to-string (nth 5 dtime))
2708              " at "
2709              (format "%02d" (nth 2 dtime))
2710              ":"
2711              (format "%02d" (nth 1 dtime)))))))
2712         (error
2713          (format "Date: %s (from Gnus)" date))))
2714
2715 (defun article-date-local (&optional highlight)
2716   "Convert the current article date to the local timezone."
2717   (interactive (list t))
2718   (article-date-ut 'local highlight))
2719
2720 (defun article-date-english (&optional highlight)
2721   "Convert the current article date to something that is proper English."
2722   (interactive (list t))
2723   (article-date-ut 'english highlight))
2724
2725 (defun article-date-original (&optional highlight)
2726   "Convert the current article date to what it was originally.
2727 This is only useful if you have used some other date conversion
2728 function and want to see what the date was before converting."
2729   (interactive (list t))
2730   (article-date-ut 'original highlight))
2731
2732 (defun article-date-lapsed (&optional highlight)
2733   "Convert the current article date to time lapsed since it was sent."
2734   (interactive (list t))
2735   (article-date-ut 'lapsed highlight))
2736
2737 (defun article-update-date-lapsed ()
2738   "Function to be run from a timer to update the lapsed time line."
2739   (let (deactivate-mark)
2740     (save-excursion
2741       (ignore-errors
2742         (walk-windows
2743          (lambda (w)
2744            (set-buffer (window-buffer w))
2745            (when (eq major-mode 'gnus-article-mode)
2746              (goto-char (point-min))
2747              (when (re-search-forward "^X-Sent:" nil t)
2748                (article-date-lapsed t))))
2749          nil 'visible)))))
2750
2751 (defun gnus-start-date-timer (&optional n)
2752   "Start a timer to update the X-Sent header in the article buffers.
2753 The numerical prefix says how frequently (in seconds) the function
2754 is to run."
2755   (interactive "p")
2756   (unless n
2757     (setq n 1))
2758   (gnus-stop-date-timer)
2759   (setq article-lapsed-timer
2760         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2761
2762 (defun gnus-stop-date-timer ()
2763   "Stop the X-Sent timer."
2764   (interactive)
2765   (when article-lapsed-timer
2766     (nnheader-cancel-timer article-lapsed-timer)
2767     (setq article-lapsed-timer nil)))
2768
2769 (defun article-date-user (&optional highlight)
2770   "Convert the current article date to the user-defined format.
2771 This format is defined by the `gnus-article-time-format' variable."
2772   (interactive (list t))
2773   (article-date-ut 'user highlight))
2774
2775 (defun article-date-iso8601 (&optional highlight)
2776   "Convert the current article date to ISO8601."
2777   (interactive (list t))
2778   (article-date-ut 'iso8601 highlight))
2779
2780 ;; (defun article-show-all ()
2781 ;;   "Show all hidden text in the article buffer."
2782 ;;   (interactive)
2783 ;;   (save-excursion
2784 ;;     (let ((buffer-read-only nil))
2785 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
2786
2787 (defun article-remove-leading-whitespace ()
2788   "Remove excessive whitespace from all headers."
2789   (interactive)
2790   (save-excursion
2791     (save-restriction
2792       (let ((buffer-read-only nil))
2793         (article-narrow-to-head)
2794         (goto-char (point-min))
2795         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
2796           (delete-region (match-beginning 1) (match-end 1)))))))
2797
2798 (defun article-emphasize (&optional arg)
2799   "Emphasize text according to `gnus-emphasis-alist'."
2800   (interactive (gnus-article-hidden-arg))
2801   (unless (gnus-article-check-hidden-text 'emphasis arg)
2802     (save-excursion
2803       (let ((alist (or
2804                     (condition-case nil
2805                         (with-current-buffer gnus-summary-buffer
2806                           gnus-article-emphasis-alist)
2807                       (error))
2808                     gnus-emphasis-alist))
2809             (buffer-read-only nil)
2810             (props (append '(article-type emphasis)
2811                            gnus-hidden-properties))
2812             regexp elem beg invisible visible face)
2813         (article-goto-body)
2814         (setq beg (point))
2815         (while (setq elem (pop alist))
2816           (goto-char beg)
2817           (setq regexp (car elem)
2818                 invisible (nth 1 elem)
2819                 visible (nth 2 elem)
2820                 face (nth 3 elem))
2821           (while (re-search-forward regexp nil t)
2822             (when (and (match-beginning visible) (match-beginning invisible))
2823               (gnus-article-hide-text
2824                (match-beginning invisible) (match-end invisible) props)
2825               (gnus-article-unhide-text-type
2826                (match-beginning visible) (match-end visible) 'emphasis)
2827               (gnus-put-overlay-excluding-newlines
2828                (match-beginning visible) (match-end visible) 'face face)
2829               (gnus-add-wash-type 'emphasis)
2830               (goto-char (match-end invisible)))))))))
2831
2832 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2833   "Setup newsgroup emphasis alist."
2834   (unless gnus-article-emphasis-alist
2835     (let ((name (and gnus-newsgroup-name
2836                      (gnus-group-real-name gnus-newsgroup-name))))
2837       (make-local-variable 'gnus-article-emphasis-alist)
2838       (setq gnus-article-emphasis-alist
2839             (nconc
2840              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2841                (while (setq elem (pop alist))
2842                  (when (and name (string-match (car elem) name))
2843                    (setq alist nil
2844                          highlight (copy-sequence (cdr elem)))))
2845                highlight)
2846              (copy-sequence highlight-words)
2847              (if gnus-newsgroup-name
2848                  (copy-sequence (gnus-group-find-parameter
2849                                  gnus-newsgroup-name 'highlight-words t)))
2850              gnus-emphasis-alist)))))
2851
2852 (eval-when-compile
2853   (defvar gnus-summary-article-menu)
2854   (defvar gnus-summary-post-menu))
2855
2856 ;;; Saving functions.
2857
2858 (defun gnus-article-save (save-buffer file &optional num)
2859   "Save the currently selected article."
2860   (unless gnus-save-all-headers
2861     ;; Remove headers according to `gnus-saved-headers'.
2862     (let ((gnus-visible-headers
2863            (or gnus-saved-headers gnus-visible-headers))
2864           (gnus-article-buffer save-buffer))
2865       (save-excursion
2866         (set-buffer save-buffer)
2867         (article-hide-headers 1 t))))
2868   (save-window-excursion
2869     (if (not gnus-default-article-saver)
2870         (error "No default saver is defined")
2871       ;; !!! Magic!  The saving functions all save
2872       ;; `gnus-save-article-buffer' (or so they think), but we
2873       ;; bind that variable to our save-buffer.
2874       (set-buffer gnus-article-buffer)
2875       (let* ((gnus-save-article-buffer save-buffer)
2876              (filename
2877               (cond
2878                ((not gnus-prompt-before-saving) 'default)
2879                ((eq gnus-prompt-before-saving 'always) nil)
2880                (t file)))
2881              (gnus-number-of-articles-to-be-saved
2882               (when (eq gnus-prompt-before-saving t)
2883                 num)))                  ; Magic
2884         (set-buffer gnus-article-current-summary)
2885         (funcall gnus-default-article-saver filename)))))
2886
2887 (defun gnus-read-save-file-name (prompt &optional filename
2888                                         function group headers variable)
2889   (let ((default-name
2890           (funcall function group headers (symbol-value variable)))
2891         result)
2892     (setq result
2893           (expand-file-name
2894            (cond
2895             ((eq filename 'default)
2896              default-name)
2897             ((eq filename t)
2898              default-name)
2899             (filename filename)
2900             (t
2901              (let* ((split-name (gnus-get-split-value gnus-split-methods))
2902                     (prompt
2903                      (format prompt
2904                              (if (and gnus-number-of-articles-to-be-saved
2905                                       (> gnus-number-of-articles-to-be-saved 1))
2906                                  (format "these %d articles"
2907                                          gnus-number-of-articles-to-be-saved)
2908                                "this article")))
2909                     (file
2910                      ;; Let the split methods have their say.
2911                      (cond
2912                       ;; No split name was found.
2913                       ((null split-name)
2914                        (read-file-name
2915                         (concat prompt " (default "
2916                                 (file-name-nondirectory default-name) ") ")
2917                         (file-name-directory default-name)
2918                         default-name))
2919                       ;; A single group name is returned.
2920                       ((stringp split-name)
2921                        (setq default-name
2922                              (funcall function split-name headers
2923                                       (symbol-value variable)))
2924                        (read-file-name
2925                         (concat prompt " (default "
2926                                 (file-name-nondirectory default-name) ") ")
2927                         (file-name-directory default-name)
2928                         default-name))
2929                       ;; A single split name was found
2930                       ((= 1 (length split-name))
2931                        (let* ((name (expand-file-name
2932                                      (car split-name)
2933                                      gnus-article-save-directory))
2934                               (dir (cond ((file-directory-p name)
2935                                           (file-name-as-directory name))
2936                                          ((file-exists-p name) name)
2937                                          (t gnus-article-save-directory))))
2938                          (read-file-name
2939                           (concat prompt " (default " name ") ")
2940                           dir name)))
2941                       ;; A list of splits was found.
2942                       (t
2943                        (setq split-name (nreverse split-name))
2944                        (let (result)
2945                          (let ((file-name-history
2946                                 (nconc split-name file-name-history)))
2947                            (setq result
2948                                  (expand-file-name
2949                                   (read-file-name
2950                                    (concat prompt " (`M-p' for defaults) ")
2951                                    gnus-article-save-directory
2952                                    (car split-name))
2953                                   gnus-article-save-directory)))
2954                          (car (push result file-name-history)))))))
2955                ;; Create the directory.
2956                (gnus-make-directory (file-name-directory file))
2957       ;; If we have read a directory, we append the default file name.
2958                (when (file-directory-p file)
2959                  (setq file (expand-file-name (file-name-nondirectory
2960                                                default-name)
2961                                               (file-name-as-directory file))))
2962                ;; Possibly translate some characters.
2963                (nnheader-translate-file-chars file))))))
2964     (gnus-make-directory (file-name-directory result))
2965     (set variable result)))
2966
2967 (defun gnus-article-archive-name (group)
2968   "Return the first instance of an \"Archive-name\" in the current buffer."
2969   (let ((case-fold-search t))
2970     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2971       (nnheader-concat gnus-article-save-directory
2972                        (match-string 1)))))
2973
2974 (defun gnus-article-nndoc-name (group)
2975   "If GROUP is an nndoc group, return the name of the parent group."
2976   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2977     (gnus-group-get-parameter group 'save-article-group)))
2978
2979 (defun gnus-summary-save-in-rmail (&optional filename)
2980   "Append this article to Rmail file.
2981 Optional argument FILENAME specifies file name.
2982 Directory to save to is default to `gnus-article-save-directory'."
2983   (setq filename (gnus-read-save-file-name
2984                   "Save %s in rmail file:" filename
2985                   gnus-rmail-save-name gnus-newsgroup-name
2986                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2987   (gnus-eval-in-buffer-window gnus-save-article-buffer
2988     (save-excursion
2989       (save-restriction
2990         (widen)
2991         (gnus-output-to-rmail filename))))
2992   filename)
2993
2994 (defun gnus-summary-save-in-mail (&optional filename)
2995   "Append this article to Unix mail file.
2996 Optional argument FILENAME specifies file name.
2997 Directory to save to is default to `gnus-article-save-directory'."
2998   (setq filename (gnus-read-save-file-name
2999                   "Save %s in Unix mail file:" filename
3000                   gnus-mail-save-name gnus-newsgroup-name
3001                   gnus-current-headers 'gnus-newsgroup-last-mail))
3002   (gnus-eval-in-buffer-window gnus-save-article-buffer
3003     (save-excursion
3004       (save-restriction
3005         (widen)
3006         (if (and (file-readable-p filename)
3007                  (mail-file-babyl-p filename))
3008             (rmail-output-to-rmail-file filename t)
3009           (gnus-output-to-mail filename)))))
3010   filename)
3011
3012 (defun gnus-summary-save-in-file (&optional filename overwrite)
3013   "Append this article to file.
3014 Optional argument FILENAME specifies file name.
3015 Directory to save to is default to `gnus-article-save-directory'."
3016   (setq filename (gnus-read-save-file-name
3017                   "Save %s in file:" filename
3018                   gnus-file-save-name gnus-newsgroup-name
3019                   gnus-current-headers 'gnus-newsgroup-last-file))
3020   (gnus-eval-in-buffer-window gnus-save-article-buffer
3021     (save-excursion
3022       (save-restriction
3023         (widen)
3024         (when (and overwrite
3025                    (file-exists-p filename))
3026           (delete-file filename))
3027         (gnus-output-to-file filename))))
3028   filename)
3029
3030 (defun gnus-summary-write-to-file (&optional filename)
3031   "Write this article to a file, overwriting it if the file exists.
3032 Optional argument FILENAME specifies file name.
3033 The directory to save in defaults to `gnus-article-save-directory'."
3034   (gnus-summary-save-in-file nil t))
3035
3036 (defun gnus-summary-save-body-in-file (&optional filename)
3037   "Append this article body to a file.
3038 Optional argument FILENAME specifies file name.
3039 The directory to save in defaults to `gnus-article-save-directory'."
3040   (setq filename (gnus-read-save-file-name
3041                   "Save %s body in file:" filename
3042                   gnus-file-save-name gnus-newsgroup-name
3043                   gnus-current-headers 'gnus-newsgroup-last-file))
3044   (gnus-eval-in-buffer-window gnus-save-article-buffer
3045     (save-excursion
3046       (save-restriction
3047         (widen)
3048         (when (article-goto-body)
3049           (narrow-to-region (point) (point-max)))
3050         (gnus-output-to-file filename))))
3051   filename)
3052
3053 (defun gnus-summary-save-in-pipe (&optional command)
3054   "Pipe this article to subprocess."
3055   (setq command
3056         (cond ((and (eq command 'default)
3057                     gnus-last-shell-command)
3058                gnus-last-shell-command)
3059               ((stringp command)
3060                command)
3061               (t (read-string
3062                   (format
3063                    "Shell command on %s: "
3064                    (if (and gnus-number-of-articles-to-be-saved
3065                             (> gnus-number-of-articles-to-be-saved 1))
3066                        (format "these %d articles"
3067                                gnus-number-of-articles-to-be-saved)
3068                      "this article"))
3069                   gnus-last-shell-command))))
3070   (when (string-equal command "")
3071     (if gnus-last-shell-command
3072         (setq command gnus-last-shell-command)
3073       (error "A command is required")))
3074   (gnus-eval-in-buffer-window gnus-article-buffer
3075     (save-restriction
3076       (widen)
3077       (shell-command-on-region (point-min) (point-max) command nil)))
3078   (setq gnus-last-shell-command command))
3079
3080 (defun gnus-summary-pipe-to-muttprint (&optional command)
3081   "Pipe this article to muttprint."
3082   (setq command (read-string
3083                  "Print using command: " gnus-summary-muttprint-program
3084                  nil gnus-summary-muttprint-program))
3085   (gnus-summary-save-in-pipe command))
3086
3087 ;;; Article file names when saving.
3088
3089 (defun gnus-capitalize-newsgroup (newsgroup)
3090   "Capitalize NEWSGROUP name."
3091   (when (not (zerop (length newsgroup)))
3092     (concat (char-to-string (upcase (aref newsgroup 0)))
3093             (substring newsgroup 1))))
3094
3095 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3096   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3097 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3098 Otherwise, it is like ~/News/news/group/num."
3099   (let ((default
3100           (expand-file-name
3101            (concat (if (gnus-use-long-file-name 'not-save)
3102                        (gnus-capitalize-newsgroup newsgroup)
3103                      (gnus-newsgroup-directory-form newsgroup))
3104                    "/" (int-to-string (mail-header-number headers)))
3105            gnus-article-save-directory)))
3106     (if (and last-file
3107              (string-equal (file-name-directory default)
3108                            (file-name-directory last-file))
3109              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3110         default
3111       (or last-file default))))
3112
3113 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3114   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3115 If variable `gnus-use-long-file-name' is non-nil, it is
3116 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3117   (let ((default
3118           (expand-file-name
3119            (concat (if (gnus-use-long-file-name 'not-save)
3120                        newsgroup
3121                      (gnus-newsgroup-directory-form newsgroup))
3122                    "/" (int-to-string (mail-header-number headers)))
3123            gnus-article-save-directory)))
3124     (if (and last-file
3125              (string-equal (file-name-directory default)
3126                            (file-name-directory last-file))
3127              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3128         default
3129       (or last-file default))))
3130
3131 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3132   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3133 If variable `gnus-use-long-file-name' is non-nil, it is
3134 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3135   (or last-file
3136       (expand-file-name
3137        (if (gnus-use-long-file-name 'not-save)
3138            newsgroup
3139          (file-relative-name
3140           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3141           default-directory))
3142        gnus-article-save-directory)))
3143
3144 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3145   "Generate file name from sender."
3146   (let ((from (mail-header-from headers)))
3147     (expand-file-name
3148      (if (and from (string-match "\\([^ <]+\\)@" from))
3149          (match-string 1 from)
3150        "nobody")
3151      gnus-article-save-directory)))
3152
3153 (defun article-verify-x-pgp-sig ()
3154   "Verify X-PGP-Sig."
3155   (interactive)
3156   (if (gnus-buffer-live-p gnus-original-article-buffer)
3157       (let ((sig (with-current-buffer gnus-original-article-buffer
3158                    (gnus-fetch-field "X-PGP-Sig")))
3159             items info headers)
3160         (when (and sig
3161                    mml2015-use
3162                    (mml2015-clear-verify-function))
3163           (with-temp-buffer
3164             (insert-buffer gnus-original-article-buffer)
3165             (setq items (split-string sig))
3166             (message-narrow-to-head)
3167             (let ((inhibit-point-motion-hooks t)
3168                   (case-fold-search t))
3169               ;; Don't verify multiple headers.
3170               (setq headers (mapconcat (lambda (header)
3171                                          (concat header ": "
3172                                                  (mail-fetch-field header) "\n"))
3173                                        (split-string (nth 1 items) ",") "")))
3174             (delete-region (point-min) (point-max))
3175             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3176             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3177             (insert headers)
3178             (widen)
3179             (forward-line)
3180             (while (not (eobp))
3181               (if (looking-at "^-")
3182                   (insert "- "))
3183               (forward-line))
3184             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3185             (insert "Version: " (car items) "\n\n")
3186             (insert (mapconcat 'identity (cddr items) "\n"))
3187             (insert "\n-----END PGP SIGNATURE-----\n")
3188             (let ((mm-security-handle (list (format "multipart/signed"))))
3189               (mml2015-clean-buffer)
3190               (let ((coding-system-for-write (or gnus-newsgroup-charset
3191                                                  'iso-8859-1)))
3192                 (funcall (mml2015-clear-verify-function)))
3193               (setq info
3194                     (or (mm-handle-multipart-ctl-parameter
3195                          mm-security-handle 'gnus-details)
3196                         (mm-handle-multipart-ctl-parameter
3197                          mm-security-handle 'gnus-info)))))
3198           (when info
3199             (let (buffer-read-only bface eface)
3200               (save-restriction
3201                 (message-narrow-to-head)
3202                 (goto-char (point-max))
3203                 (forward-line -1)
3204                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
3205                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
3206                 (message-remove-header "X-Gnus-PGP-Verify")
3207                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3208                     (forward-line)
3209                   (goto-char (point-max)))
3210                 (narrow-to-region (point) (point))
3211                 (insert "X-Gnus-PGP-Verify: " info "\n")
3212                 (goto-char (point-min))
3213                 (forward-line)
3214                 (while (not (eobp))
3215                   (if (not (looking-at "^[ \t]"))
3216                       (insert " "))
3217                   (forward-line))
3218                 ;; Do highlighting.
3219                 (goto-char (point-min))
3220                 (when (looking-at "\\([^:]+\\): *")
3221                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3222                                      'face bface)
3223                   (put-text-property (match-end 0) (point-max)
3224                                      'face eface)))))))))
3225
3226 (defun article-verify-cancel-lock ()
3227   "Verify Cancel-Lock header."
3228   (interactive)
3229   (if (gnus-buffer-live-p gnus-original-article-buffer)
3230       (canlock-verify gnus-original-article-buffer)))
3231
3232 (eval-and-compile
3233   (mapcar
3234    (lambda (func)
3235      (let (afunc gfunc)
3236        (if (consp func)
3237            (setq afunc (car func)
3238                  gfunc (cdr func))
3239          (setq afunc func
3240                gfunc (intern (format "gnus-%s" func))))
3241        (defalias gfunc
3242          (when (fboundp afunc)
3243            `(lambda (&optional interactive &rest args)
3244               ,(documentation afunc t)
3245               (interactive (list t))
3246               (save-excursion
3247                 (set-buffer gnus-article-buffer)
3248                 (if interactive
3249                     (call-interactively ',afunc)
3250                   (apply ',afunc args))))))))
3251    '(article-hide-headers
3252      article-verify-x-pgp-sig
3253      article-verify-cancel-lock
3254      article-hide-boring-headers
3255      article-treat-overstrike
3256      article-fill-long-lines
3257      article-capitalize-sentences
3258      article-remove-cr
3259      article-remove-leading-whitespace
3260      article-display-x-face
3261      article-de-quoted-unreadable
3262      article-de-base64-unreadable
3263      article-decode-HZ
3264      article-wash-html
3265      article-unsplit-urls
3266      article-hide-list-identifiers
3267      article-hide-pgp
3268      article-strip-banner
3269      article-babel
3270      article-hide-pem
3271      article-hide-signature
3272      article-strip-headers-in-body
3273      article-remove-trailing-blank-lines
3274      article-strip-leading-blank-lines
3275      article-strip-multiple-blank-lines
3276      article-strip-leading-space
3277      article-strip-trailing-space
3278      article-strip-blank-lines
3279      article-strip-all-blank-lines
3280      article-date-local
3281      article-date-english
3282      article-date-iso8601
3283      article-date-original
3284      article-date-ut
3285      article-decode-mime-words
3286      article-decode-charset
3287      article-decode-encoded-words
3288      article-date-user
3289      article-date-lapsed
3290      article-emphasize
3291      article-treat-dumbquotes
3292      article-normalize-headers
3293 ;;     (article-show-all . gnus-article-show-all-headers)
3294      )))
3295 \f
3296 ;;;
3297 ;;; Gnus article mode
3298 ;;;
3299
3300 (put 'gnus-article-mode 'mode-class 'special)
3301
3302 (set-keymap-parent gnus-article-mode-map widget-keymap)
3303
3304 (gnus-define-keys gnus-article-mode-map
3305   " " gnus-article-goto-next-page
3306   "\177" gnus-article-goto-prev-page
3307   [delete] gnus-article-goto-prev-page
3308   [backspace] gnus-article-goto-prev-page
3309   "\C-c^" gnus-article-refer-article
3310   "h" gnus-article-show-summary
3311   "s" gnus-article-show-summary
3312   "\C-c\C-m" gnus-article-mail
3313   "?" gnus-article-describe-briefly
3314   "e" gnus-summary-edit-article
3315   "<" beginning-of-buffer
3316   ">" end-of-buffer
3317   "\C-c\C-i" gnus-info-find-node
3318   "\C-c\C-b" gnus-bug
3319   "R" gnus-article-reply-with-original
3320   "F" gnus-article-followup-with-original
3321   "\C-hk" gnus-article-describe-key
3322   "\C-hc" gnus-article-describe-key-briefly
3323
3324   "\C-d" gnus-article-read-summary-keys
3325   "\M-*" gnus-article-read-summary-keys
3326   "\M-#" gnus-article-read-summary-keys
3327   "\M-^" gnus-article-read-summary-keys
3328   "\M-g" gnus-article-read-summary-keys)
3329
3330 (substitute-key-definition
3331  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
3332
3333 (defun gnus-article-make-menu-bar ()
3334   (unless (boundp 'gnus-article-commands-menu)
3335     (gnus-summary-make-menu-bar))
3336   (gnus-turn-off-edit-menu 'article)
3337   (unless (boundp 'gnus-article-article-menu)
3338     (easy-menu-define
3339      gnus-article-article-menu gnus-article-mode-map ""
3340      '("Article"
3341        ["Scroll forwards" gnus-article-goto-next-page t]
3342        ["Scroll backwards" gnus-article-goto-prev-page t]
3343        ["Show summary" gnus-article-show-summary t]
3344        ["Fetch Message-ID at point" gnus-article-refer-article t]
3345        ["Mail to address at point" gnus-article-mail t]
3346        ["Send a bug report" gnus-bug t]))
3347
3348     (easy-menu-define
3349      gnus-article-treatment-menu gnus-article-mode-map ""
3350      ;; Fixme: this should use :active (and maybe :visible).
3351      '("Treatment"
3352        ["Hide headers" gnus-article-hide-headers t]
3353        ["Hide signature" gnus-article-hide-signature t]
3354        ["Hide citation" gnus-article-hide-citation t]
3355        ["Treat overstrike" gnus-article-treat-overstrike t]
3356        ["Remove carriage return" gnus-article-remove-cr t]
3357        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3358        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
3359        ["Remove base64" gnus-article-de-base64-unreadable t]
3360        ["Treat html" gnus-article-wash-html t]
3361        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3362        ["Decode HZ" gnus-article-decode-HZ t]))
3363
3364     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3365
3366     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3367
3368     (gnus-run-hooks 'gnus-article-menu-hook)))
3369
3370 ;; Fixme: do something for the Emacs tool bar in Article mode a la
3371 ;; Summary.
3372
3373 (defun gnus-article-mode ()
3374   "Major mode for displaying an article.
3375
3376 All normal editing commands are switched off.
3377
3378 The following commands are available in addition to all summary mode
3379 commands:
3380 \\<gnus-article-mode-map>
3381 \\[gnus-article-next-page]\t Scroll the article one page forwards
3382 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3383 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3384 \\[gnus-article-show-summary]\t Display the summary buffer
3385 \\[gnus-article-mail]\t Send a reply to the address near point
3386 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3387 \\[gnus-info-find-node]\t Go to the Gnus info node"
3388   (interactive)
3389   (gnus-simplify-mode-line)
3390   (setq mode-name "Article")
3391   (setq major-mode 'gnus-article-mode)
3392   (make-local-variable 'minor-mode-alist)
3393   (use-local-map gnus-article-mode-map)
3394   (when (gnus-visual-p 'article-menu 'menu)
3395     (gnus-article-make-menu-bar))
3396   (gnus-update-format-specifications nil 'article-mode)
3397   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3398   (make-local-variable 'gnus-page-broken)
3399   (make-local-variable 'gnus-button-marker-list)
3400   (make-local-variable 'gnus-article-current-summary)
3401   (make-local-variable 'gnus-article-mime-handles)
3402   (make-local-variable 'gnus-article-decoded-p)
3403   (make-local-variable 'gnus-article-mime-handle-alist)
3404   (make-local-variable 'gnus-article-wash-types)
3405   (make-local-variable 'gnus-article-image-alist)
3406   (make-local-variable 'gnus-article-charset)
3407   (make-local-variable 'gnus-article-ignored-charsets)
3408   (gnus-set-default-directory)
3409   (buffer-disable-undo)
3410   (setq buffer-read-only t)
3411   (set-syntax-table gnus-article-mode-syntax-table)
3412   (mm-enable-multibyte)
3413   (gnus-run-hooks 'gnus-article-mode-hook))
3414
3415 (defun gnus-article-setup-buffer ()
3416   "Initialize the article buffer."
3417   (let* ((name (if gnus-single-article-buffer "*Article*"
3418                  (concat "*Article " gnus-newsgroup-name "*")))
3419          (original
3420           (progn (string-match "\\*Article" name)
3421                  (concat " *Original Article"
3422                          (substring name (match-end 0))))))
3423     (setq gnus-article-buffer name)
3424     (setq gnus-original-article-buffer original)
3425     (setq gnus-article-mime-handle-alist nil)
3426     ;; This might be a variable local to the summary buffer.
3427     (unless gnus-single-article-buffer
3428       (save-excursion
3429         (set-buffer gnus-summary-buffer)
3430         (setq gnus-article-buffer name)
3431         (setq gnus-original-article-buffer original)
3432         (gnus-set-global-variables)))
3433     (gnus-article-setup-highlight-words)
3434     ;; Init original article buffer.
3435     (save-excursion
3436       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3437       (mm-enable-multibyte)
3438       (setq major-mode 'gnus-original-article-mode)
3439       (make-local-variable 'gnus-original-article))
3440     (if (get-buffer name)
3441         (save-excursion
3442           (set-buffer name)
3443           (when gnus-article-mime-handles
3444             (mm-destroy-parts gnus-article-mime-handles)
3445             (setq gnus-article-mime-handles nil))
3446           ;; Set it to nil in article-buffer!
3447           (setq gnus-article-mime-handle-alist nil)
3448           (buffer-disable-undo)
3449           (setq buffer-read-only t)
3450           (unless (eq major-mode 'gnus-article-mode)
3451             (gnus-article-mode))
3452           (current-buffer))
3453       (save-excursion
3454         (set-buffer (gnus-get-buffer-create name))
3455         (gnus-article-mode)
3456         (make-local-variable 'gnus-summary-buffer)
3457         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3458         (current-buffer)))))
3459
3460 ;; Set article window start at LINE, where LINE is the number of lines
3461 ;; from the head of the article.
3462 (defun gnus-article-set-window-start (&optional line)
3463   (set-window-start
3464    (gnus-get-buffer-window gnus-article-buffer t)
3465    (save-excursion
3466      (set-buffer gnus-article-buffer)
3467      (goto-char (point-min))
3468      (if (not line)
3469          (point-min)
3470        (gnus-message 6 "Moved to bookmark")
3471        (search-forward "\n\n" nil t)
3472        (forward-line line)
3473        (point)))))
3474
3475 (defun gnus-article-prepare (article &optional all-headers header)
3476   "Prepare ARTICLE in article mode buffer.
3477 ARTICLE should either be an article number or a Message-ID.
3478 If ARTICLE is an id, HEADER should be the article headers.
3479 If ALL-HEADERS is non-nil, no headers are hidden."
3480   (save-excursion
3481     ;; Make sure we start in a summary buffer.
3482     (unless (eq major-mode 'gnus-summary-mode)
3483       (set-buffer gnus-summary-buffer))
3484     (setq gnus-summary-buffer (current-buffer))
3485     (let* ((gnus-article (if header (mail-header-number header) article))
3486            (summary-buffer (current-buffer))
3487            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3488            (group gnus-newsgroup-name)
3489            result)
3490       (save-excursion
3491         (gnus-article-setup-buffer)
3492         (set-buffer gnus-article-buffer)
3493         ;; Deactivate active regions.
3494         (when (and (boundp 'transient-mark-mode)
3495                    transient-mark-mode)
3496           (setq mark-active nil))
3497         (if (not (setq result (let ((buffer-read-only nil))
3498                                 (gnus-request-article-this-buffer
3499                                  article group))))
3500             ;; There is no such article.
3501             (save-excursion
3502               (when (and (numberp article)
3503                          (not (memq article gnus-newsgroup-sparse)))
3504                 (setq gnus-article-current
3505                       (cons gnus-newsgroup-name article))
3506                 (set-buffer gnus-summary-buffer)
3507                 (setq gnus-current-article article)
3508                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3509                     (progn
3510                       (gnus-summary-set-agent-mark article)
3511                       (message "Message marked for downloading"))
3512                   (gnus-summary-mark-article article gnus-canceled-mark)
3513                   (unless (memq article gnus-newsgroup-sparse)
3514                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3515           (if (or (eq result 'pseudo)
3516                   (eq result 'nneething))
3517               (progn
3518                 (save-excursion
3519                   (set-buffer summary-buffer)
3520                   (push article gnus-newsgroup-history)
3521                   (setq gnus-last-article gnus-current-article
3522                         gnus-current-article 0
3523                         gnus-current-headers nil
3524                         gnus-article-current nil)
3525                   (if (eq result 'nneething)
3526                       (gnus-configure-windows 'summary)
3527                     (gnus-configure-windows 'article))
3528                   (gnus-set-global-variables))
3529                 (let ((gnus-article-mime-handle-alist-1
3530                        gnus-article-mime-handle-alist))
3531                   (gnus-set-mode-line 'article)))
3532             ;; The result from the `request' was an actual article -
3533             ;; or at least some text that is now displayed in the
3534             ;; article buffer.
3535             (when (and (numberp article)
3536                        (not (eq article gnus-current-article)))
3537               ;; Seems like a new article has been selected.
3538               ;; `gnus-current-article' must be an article number.
3539               (save-excursion
3540                 (set-buffer summary-buffer)
3541                 (push article gnus-newsgroup-history)
3542                 (setq gnus-last-article gnus-current-article
3543                       gnus-current-article article
3544                       gnus-current-headers
3545                       (gnus-summary-article-header gnus-current-article)
3546                       gnus-article-current
3547                       (cons gnus-newsgroup-name gnus-current-article))
3548                 (unless (vectorp gnus-current-headers)
3549                   (setq gnus-current-headers nil))
3550                 (gnus-summary-goto-subject gnus-current-article)
3551                 (when (gnus-summary-show-thread)
3552                   ;; If the summary buffer really was folded, the
3553                   ;; previous goto may not actually have gone to
3554                   ;; the right article, but the thread root instead.
3555                   ;; So we go again.
3556                   (gnus-summary-goto-subject gnus-current-article))
3557                 (gnus-run-hooks 'gnus-mark-article-hook)
3558                 (gnus-set-mode-line 'summary)
3559                 (when (gnus-visual-p 'article-highlight 'highlight)
3560                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3561                 ;; Set the global newsgroup variables here.
3562                 (gnus-set-global-variables)
3563                 (setq gnus-have-all-headers
3564                       (or all-headers gnus-show-all-headers))))
3565             (save-excursion
3566               (gnus-configure-windows 'article))
3567             (when (or (numberp article)
3568                       (stringp article))
3569               (gnus-article-prepare-display)
3570               ;; Do page break.
3571               (goto-char (point-min))
3572               (setq gnus-page-broken
3573                     (when gnus-break-pages
3574                       (gnus-narrow-to-page)
3575                       t)))
3576             (let ((gnus-article-mime-handle-alist-1
3577                    gnus-article-mime-handle-alist))
3578               (gnus-set-mode-line 'article))
3579             (article-goto-body)
3580             (unless (bobp)
3581               (forward-line -1))
3582             (set-window-point (get-buffer-window (current-buffer)) (point))
3583             (gnus-configure-windows 'article)
3584             t))))))
3585
3586 ;;;###autoload
3587 (defun gnus-article-prepare-display ()
3588   "Make the current buffer look like a nice article."
3589   ;; Hooks for getting information from the article.
3590   ;; This hook must be called before being narrowed.
3591   (let ((gnus-article-buffer (current-buffer))
3592         buffer-read-only)
3593     (unless (eq major-mode 'gnus-article-mode)
3594       (gnus-article-mode))
3595     (setq buffer-read-only nil
3596           gnus-article-wash-types nil
3597           gnus-article-image-alist nil)
3598     (gnus-run-hooks 'gnus-tmp-internal-hook)
3599     (when gnus-display-mime-function
3600       (funcall gnus-display-mime-function))
3601     (gnus-run-hooks 'gnus-article-prepare-hook)))
3602
3603 ;;;
3604 ;;; Gnus MIME viewing functions
3605 ;;;
3606
3607 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3608   "Format of the MIME buttons.
3609
3610 Valid specifiers include:
3611 %t  The MIME type
3612 %T  MIME type, along with additional info
3613 %n  The `name' parameter
3614 %d  The description, if any
3615 %l  The length of the encoded part
3616 %p  The part identifier number
3617 %e  Dots if the part isn't displayed
3618
3619 General format specifiers can also be used.  See
3620 (gnus)Formatting Variables.")
3621
3622 (defvar gnus-mime-button-line-format-alist
3623   '((?t gnus-tmp-type ?s)
3624     (?T gnus-tmp-type-long ?s)
3625     (?n gnus-tmp-name ?s)
3626     (?d gnus-tmp-description ?s)
3627     (?p gnus-tmp-id ?s)
3628     (?l gnus-tmp-length ?d)
3629     (?e gnus-tmp-dots ?s)))
3630
3631 (defvar gnus-mime-button-commands
3632   '((gnus-article-press-button "\r" "Toggle Display")
3633     (gnus-mime-view-part "v" "View Interactively...")
3634     (gnus-mime-view-part-as-type "t" "View As Type...")
3635     (gnus-mime-view-part-as-charset "C" "View As charset...")
3636     (gnus-mime-save-part "o" "Save...")
3637     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
3638     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3639     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3640     (gnus-mime-view-part-internally "E" "View Internally")
3641     (gnus-mime-view-part-externally "e" "View Externally")
3642     (gnus-mime-print-part "p" "Print")
3643     (gnus-mime-pipe-part "|" "Pipe To Command...")
3644     (gnus-mime-action-on-part "." "Take action on the part")))
3645
3646 (defun gnus-article-mime-part-status ()
3647   (if gnus-article-mime-handle-alist-1
3648       (if (eq 1 (length gnus-article-mime-handle-alist-1))
3649           " (1 part)"
3650         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
3651     ""))
3652
3653 (defvar gnus-mime-button-map
3654   (let ((map (make-sparse-keymap)))
3655     (unless (>= (string-to-number emacs-version) 21)
3656       ;; XEmacs doesn't care.
3657       (set-keymap-parent map gnus-article-mode-map))
3658     (define-key map gnus-mouse-2 'gnus-article-push-button)
3659     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3660     (dolist (c gnus-mime-button-commands)
3661       (define-key map (cadr c) (car c)))
3662     map))
3663
3664 (defun gnus-mime-button-menu (event)
3665   "Construct a context-sensitive menu of MIME commands."
3666   (interactive "e")
3667   (save-window-excursion
3668     (let ((pos (event-start event)))
3669       (select-window (posn-window pos))
3670       (goto-char (posn-point pos))
3671       (gnus-article-check-buffer)
3672       (let ((response (x-popup-menu
3673                        t `("MIME Part"
3674                            ("" ,@(mapcar (lambda (c)
3675                                            (cons (caddr c) (car c)))
3676                                          gnus-mime-button-commands))))))
3677         (if response
3678             (call-interactively response))))))
3679
3680 (defun gnus-mime-view-all-parts (&optional handles)
3681   "View all the MIME parts."
3682   (interactive)
3683   (save-current-buffer
3684     (set-buffer gnus-article-buffer)
3685     (let ((handles (or handles gnus-article-mime-handles))
3686           (mail-parse-charset gnus-newsgroup-charset)
3687           (mail-parse-ignored-charsets
3688            (with-current-buffer gnus-summary-buffer
3689              gnus-newsgroup-ignored-charsets)))
3690       (when handles
3691         (mm-remove-parts handles)
3692         (goto-char (point-min))
3693         (or (search-forward "\n\n") (goto-char (point-max)))
3694         (let (buffer-read-only)
3695           (delete-region (point) (point-max))
3696           (mm-display-parts handles))))))
3697
3698 (defun gnus-mime-save-part-and-strip ()
3699   "Save the MIME part under point then replace it with an external body."
3700   (interactive)
3701   (gnus-article-check-buffer)
3702   (let* ((data (get-text-property (point) 'gnus-data))
3703          file param
3704          (handles gnus-article-mime-handles))
3705     (if (mm-multiple-handles gnus-article-mime-handles)
3706         (error "This function is not implemented"))
3707     (setq file (and data (mm-save-part data)))
3708     (when file
3709       (with-current-buffer (mm-handle-buffer data)
3710         (erase-buffer)
3711         (insert "Content-Type: " (mm-handle-media-type data))
3712         (mml-insert-parameter-string (cdr (mm-handle-type data))
3713                                      '(charset))
3714         (insert "\n")
3715         (insert "Content-ID: " (message-make-message-id) "\n")
3716         (insert "Content-Transfer-Encoding: binary\n")
3717         (insert "\n"))
3718       (setcdr data
3719               (cdr (mm-make-handle nil
3720                                    `("message/external-body"
3721                                      (access-type . "LOCAL-FILE")
3722                                      (name . ,file)))))
3723       (set-buffer gnus-summary-buffer)
3724       (gnus-article-edit-article
3725        `(lambda ()
3726            (erase-buffer)
3727            (let ((mail-parse-charset (or gnus-article-charset
3728                                          ',gnus-newsgroup-charset))
3729                  (mail-parse-ignored-charsets
3730                   (or gnus-article-ignored-charsets
3731                       ',gnus-newsgroup-ignored-charsets))
3732                  (mbl mml-buffer-list))
3733              (setq mml-buffer-list nil)
3734              (insert-buffer gnus-original-article-buffer)
3735              (mime-to-mml ',handles)
3736              (setq gnus-article-mime-handles nil)
3737              (let ((mbl1 mml-buffer-list))
3738                (setq mml-buffer-list mbl)
3739                (set (make-local-variable 'mml-buffer-list) mbl1))
3740              ;; LOCAL argument of add-hook differs between GNU Emacs
3741              ;; and XEmacs. make-local-hook makes sure they are local.
3742              (make-local-hook 'kill-buffer-hook)
3743              (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3744        `(lambda (no-highlight)
3745           (let ((mail-parse-charset (or gnus-article-charset
3746                                         ',gnus-newsgroup-charset))
3747                 (message-options message-options)
3748                 (message-options-set-recipient)
3749                 (mail-parse-ignored-charsets
3750                  (or gnus-article-ignored-charsets
3751                      ',gnus-newsgroup-ignored-charsets)))
3752            (mml-to-mime)
3753            (mml-destroy-buffers)
3754            (remove-hook 'kill-buffer-hook
3755                         'mml-destroy-buffers t)
3756            (kill-local-variable 'mml-buffer-list))
3757           (gnus-summary-edit-article-done
3758            ,(or (mail-header-references gnus-current-headers) "")
3759            ,(gnus-group-read-only-p)
3760            ,gnus-summary-buffer no-highlight))))))
3761
3762 (defun gnus-mime-save-part ()
3763   "Save the MIME part under point."
3764   (interactive)
3765   (gnus-article-check-buffer)
3766   (let ((data (get-text-property (point) 'gnus-data)))
3767     (when data
3768       (mm-save-part data))))
3769
3770 (defun gnus-mime-pipe-part ()
3771   "Pipe the MIME part under point to a process."
3772   (interactive)
3773   (gnus-article-check-buffer)
3774   (let ((data (get-text-property (point) 'gnus-data)))
3775     (when data
3776       (mm-pipe-part data))))
3777
3778 (defun gnus-mime-view-part ()
3779   "Interactively choose a viewing method for the MIME part under point."
3780   (interactive)
3781   (gnus-article-check-buffer)
3782   (let ((data (get-text-property (point) 'gnus-data)))
3783     (when data
3784       (setq gnus-article-mime-handles
3785             (mm-merge-handles
3786              gnus-article-mime-handles (setq data (copy-sequence data))))
3787       (mm-interactively-view-part data))))
3788
3789 (defun gnus-mime-view-part-as-type-internal ()
3790   (gnus-article-check-buffer)
3791   (let* ((name (mail-content-type-get
3792                 (mm-handle-type (get-text-property (point) 'gnus-data))
3793                 'name))
3794          (def-type (and name (mm-default-file-encoding name))))
3795     (and def-type (cons def-type 0))))
3796
3797 (defun gnus-mime-view-part-as-type (&optional mime-type)
3798   "Choose a MIME media type, and view the part as such."
3799   (interactive)
3800   (unless mime-type
3801     (setq mime-type (completing-read
3802                      "View as MIME type: "
3803                      (mapcar #'list (mailcap-mime-types))
3804                      nil nil
3805                      (gnus-mime-view-part-as-type-internal))))
3806   (gnus-article-check-buffer)
3807   (let ((handle (get-text-property (point) 'gnus-data)))
3808     (when handle
3809       (setq handle
3810             (mm-make-handle (mm-handle-buffer handle)
3811                             (cons mime-type (cdr (mm-handle-type handle)))
3812                             (mm-handle-encoding handle)
3813                             (mm-handle-undisplayer handle)
3814                             (mm-handle-disposition handle)
3815                             (mm-handle-description handle)
3816                             nil
3817                             (mm-handle-id handle)))
3818       (setq gnus-article-mime-handles
3819             (mm-merge-handles gnus-article-mime-handles handle))
3820       (gnus-mm-display-part handle))))
3821
3822 (defun gnus-mime-copy-part (&optional handle)
3823   "Put the MIME part under point into a new buffer."
3824   (interactive)
3825   (gnus-article-check-buffer)
3826   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3827          (contents (and handle (mm-get-part handle)))
3828          (base (and handle
3829                     (file-name-nondirectory
3830                      (or
3831                       (mail-content-type-get (mm-handle-type handle) 'name)
3832                       (mail-content-type-get (mm-handle-type handle)
3833                                              'filename)
3834                       "*decoded*"))))
3835          (buffer (and base (generate-new-buffer base))))
3836     (when contents
3837       (switch-to-buffer buffer)
3838       (insert contents)
3839       ;; We do it this way to make `normal-mode' set the appropriate mode.
3840       (unwind-protect
3841           (progn
3842             (setq buffer-file-name (expand-file-name base))
3843             (normal-mode))
3844         (setq buffer-file-name nil))
3845       (goto-char (point-min)))))
3846
3847 (defun gnus-mime-print-part (&optional handle filename)
3848   "Print the MIME part under point."
3849   (interactive (list nil (ps-print-preprint current-prefix-arg)))
3850   (gnus-article-check-buffer)
3851   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3852          (contents (and handle (mm-get-part handle)))
3853          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
3854          (printer (mailcap-mime-info (mm-handle-type handle) "print")))
3855     (when contents
3856         (if printer
3857             (unwind-protect
3858                 (progn
3859                   (with-temp-file file
3860                     (insert contents))
3861                   (call-process shell-file-name nil
3862                                 (generate-new-buffer " *mm*")
3863                                 nil
3864                                 shell-command-switch
3865                                 (mm-mailcap-command
3866                                  printer file (mm-handle-type handle))))
3867               (delete-file file))
3868           (with-temp-buffer
3869             (insert contents)
3870             (gnus-print-buffer))
3871           (ps-despool filename)))))
3872
3873 (defun gnus-mime-inline-part (&optional handle arg)
3874   "Insert the MIME part under point into the current buffer."
3875   (interactive (list nil current-prefix-arg))
3876   (gnus-article-check-buffer)
3877   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3878          contents charset
3879          (b (point))
3880          buffer-read-only)
3881     (when handle
3882       (if (and (not arg) (mm-handle-undisplayer handle))
3883           (mm-remove-part handle)
3884         (setq contents (mm-get-part handle))
3885         (cond
3886          ((not arg)
3887           (setq charset (or (mail-content-type-get
3888                              (mm-handle-type handle) 'charset)
3889                             gnus-newsgroup-charset)))
3890          ((numberp arg)
3891           (if (mm-handle-undisplayer handle)
3892               (mm-remove-part handle))
3893           (setq charset
3894                 (or (cdr (assq arg
3895                                gnus-summary-show-article-charset-alist))
3896                     (mm-read-coding-system "Charset: ")))))
3897         (forward-line 2)
3898         (mm-insert-inline handle
3899                           (if (and charset
3900                                    (setq charset (mm-charset-to-coding-system
3901                                                   charset))
3902                                    (not (eq charset 'ascii)))
3903                               (mm-decode-coding-string contents charset)
3904                             contents))
3905         (goto-char b)))))
3906
3907 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3908   "Insert the MIME part under point into the current buffer using the
3909 specified charset."
3910   (interactive (list nil current-prefix-arg))
3911   (gnus-article-check-buffer)
3912   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3913          contents charset
3914          (b (point))
3915          buffer-read-only)
3916     (when handle
3917       (if (mm-handle-undisplayer handle)
3918           (mm-remove-part handle))
3919       (let ((gnus-newsgroup-charset
3920              (or (cdr (assq arg
3921                             gnus-summary-show-article-charset-alist))
3922                  (mm-read-coding-system "Charset: ")))
3923           (gnus-newsgroup-ignored-charsets 'gnus-all))
3924         (gnus-article-press-button)))))
3925
3926 (defun gnus-mime-view-part-externally (&optional handle)
3927   "View the MIME part under point with an external viewer."
3928   (interactive)
3929   (gnus-article-check-buffer)
3930   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3931          (mm-user-display-methods nil)
3932          (mm-inlined-types nil)
3933          (mail-parse-charset gnus-newsgroup-charset)
3934          (mail-parse-ignored-charsets
3935           (save-excursion (set-buffer gnus-summary-buffer)
3936                           gnus-newsgroup-ignored-charsets)))
3937     (when handle
3938       (if (mm-handle-undisplayer handle)
3939           (mm-remove-part handle)
3940         (mm-display-part handle)))))
3941
3942 (defun gnus-mime-view-part-internally (&optional handle)
3943   "View the MIME part under point with an internal viewer.
3944 If no internal viewer is available, use an external viewer."
3945   (interactive)
3946   (gnus-article-check-buffer)
3947   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3948          (mm-inlined-types '(".*"))
3949          (mm-inline-large-images t)
3950          (mail-parse-charset gnus-newsgroup-charset)
3951          (mail-parse-ignored-charsets
3952           (save-excursion (set-buffer gnus-summary-buffer)
3953                           gnus-newsgroup-ignored-charsets)))
3954     (when handle
3955       (if (mm-handle-undisplayer handle)
3956           (mm-remove-part handle)
3957         (mm-display-part handle)))))
3958
3959 (defun gnus-mime-action-on-part (&optional action)
3960   "Do something with the MIME attachment at \(point\)."
3961   (interactive
3962    (list (completing-read "Action: " gnus-mime-action-alist)))
3963   (gnus-article-check-buffer)
3964   (let ((action-pair (assoc action gnus-mime-action-alist)))
3965     (if action-pair
3966         (funcall (cdr action-pair)))))
3967
3968 (defun gnus-article-part-wrapper (n function)
3969   (save-current-buffer
3970     (set-buffer gnus-article-buffer)
3971     (when (> n (length gnus-article-mime-handle-alist))
3972       (error "No such part"))
3973     (gnus-article-goto-part n)
3974     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3975       (funcall function handle))))
3976
3977 (defun gnus-article-pipe-part (n)
3978   "Pipe MIME part N, which is the numerical prefix."
3979   (interactive "p")
3980   (gnus-article-part-wrapper n 'mm-pipe-part))
3981
3982 (defun gnus-article-save-part (n)
3983   "Save MIME part N, which is the numerical prefix."
3984   (interactive "p")
3985   (gnus-article-part-wrapper n 'mm-save-part))
3986
3987 (defun gnus-article-interactively-view-part (n)
3988   "View MIME part N interactively, which is the numerical prefix."
3989   (interactive "p")
3990   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3991
3992 (defun gnus-article-copy-part (n)
3993   "Copy MIME part N, which is the numerical prefix."
3994   (interactive "p")
3995   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3996
3997 (defun gnus-article-view-part-as-charset (n)
3998   "Copy MIME part N, which is the numerical prefix."
3999   (interactive "p")
4000   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4001
4002 (defun gnus-article-view-part-externally (n)
4003   "View MIME part N externally, which is the numerical prefix."
4004   (interactive "p")
4005   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4006
4007 (defun gnus-article-inline-part (n)
4008   "Inline MIME part N, which is the numerical prefix."
4009   (interactive "p")
4010   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4011
4012 (defun gnus-article-mime-match-handle-first (condition)
4013   (if condition
4014       (let ((alist gnus-article-mime-handle-alist) ihandle n)
4015         (while (setq ihandle (pop alist))
4016           (if (and (cond
4017                     ((functionp condition)
4018                      (funcall condition (cdr ihandle)))
4019                     ((eq condition 'undisplayed)
4020                      (not (or (mm-handle-undisplayer (cdr ihandle))
4021                               (equal (mm-handle-media-type (cdr ihandle))
4022                                      "multipart/alternative"))))
4023                     ((eq condition 'undisplayed-alternative)
4024                      (not (mm-handle-undisplayer (cdr ihandle))))
4025                     (t t))
4026                    (gnus-article-goto-part (car ihandle))
4027                    (or (not n) (< (car ihandle) n)))
4028               (setq n (car ihandle))))
4029         (or n 1))
4030     1))
4031
4032 (defun gnus-article-view-part (&optional n)
4033   "View MIME part N, which is the numerical prefix."
4034   (interactive "P")
4035   (save-current-buffer
4036     (set-buffer gnus-article-buffer)
4037     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4038                              gnus-article-mime-match-handle-function)))
4039     (when (> n (length gnus-article-mime-handle-alist))
4040       (error "No such part"))
4041     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4042       (when (gnus-article-goto-part n)
4043         (if (equal (car handle) "multipart/alternative")
4044             (gnus-article-press-button)
4045           (when (eq (gnus-mm-display-part handle) 'internal)
4046             (gnus-set-window-start)))))))
4047
4048 (defsubst gnus-article-mime-total-parts ()
4049   (if (bufferp (car gnus-article-mime-handles))
4050       1 ;; single part
4051     (1- (length gnus-article-mime-handles))))
4052
4053 (defun gnus-mm-display-part (handle)
4054   "Display HANDLE and fix MIME button."
4055   (let ((id (get-text-property (point) 'gnus-part))
4056         (point (point))
4057         buffer-read-only)
4058     (forward-line 1)
4059     (prog1
4060         (let ((window (selected-window))
4061               (mail-parse-charset gnus-newsgroup-charset)
4062               (mail-parse-ignored-charsets
4063                (if (gnus-buffer-live-p gnus-summary-buffer)
4064                    (save-excursion
4065                      (set-buffer gnus-summary-buffer)
4066                      gnus-newsgroup-ignored-charsets)
4067                  nil)))
4068           (save-excursion
4069             (unwind-protect
4070                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4071                       (beg (point)))
4072                   (when win
4073                     (select-window win))
4074                   (goto-char point)
4075                   (forward-line)
4076                   (if (mm-handle-displayed-p handle)
4077                       ;; This will remove the part.
4078                       (mm-display-part handle)
4079                     (save-restriction
4080                       (narrow-to-region (point)
4081                                         (if (eobp) (point) (1+ (point))))
4082                       (mm-display-part handle)
4083                       ;; We narrow to the part itself and
4084                       ;; then call the treatment functions.
4085                       (goto-char (point-min))
4086                       (forward-line 1)
4087                       (narrow-to-region (point) (point-max))
4088                       (gnus-treat-article
4089                        nil id
4090                        (gnus-article-mime-total-parts)
4091                        (mm-handle-media-type handle)))))
4092               (if (window-live-p window)
4093                   (select-window window)))))
4094       (goto-char point)
4095       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
4096       (gnus-insert-mime-button
4097        handle id (list (mm-handle-displayed-p handle)))
4098       (goto-char point))))
4099
4100 (defun gnus-article-goto-part (n)
4101   "Go to MIME part N."
4102   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
4103     (when point
4104       (goto-char point))))
4105
4106 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4107   (let ((gnus-tmp-name
4108          (or (mail-content-type-get (mm-handle-type handle) 'name)
4109              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4110              (mail-content-type-get (mm-handle-type handle) 'url)
4111              ""))
4112         (gnus-tmp-type (mm-handle-media-type handle))
4113         (gnus-tmp-description
4114          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4115                                               "")))
4116         (gnus-tmp-dots
4117          (if (if displayed (car displayed)
4118                (mm-handle-displayed-p handle))
4119              "" "..."))
4120         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4121                            (buffer-size)))
4122         gnus-tmp-type-long b e)
4123     (when (string-match ".*/" gnus-tmp-name)
4124       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4125     (setq gnus-tmp-type-long (concat gnus-tmp-type
4126                                      (and (not (equal gnus-tmp-name ""))
4127                                           (concat "; " gnus-tmp-name))))
4128     (unless (equal gnus-tmp-description "")
4129       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4130     (unless (bolp)
4131       (insert "\n"))
4132     (setq b (point))
4133     (gnus-eval-format
4134      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4135      `(,@(gnus-local-map-property gnus-mime-button-map)
4136          gnus-callback gnus-mm-display-part
4137          gnus-part ,gnus-tmp-id
4138          article-type annotation
4139          gnus-data ,handle))
4140     (setq e (point))
4141     (widget-convert-button
4142      'link b e
4143      :mime-handle handle
4144      :action 'gnus-widget-press-button
4145      :button-keymap gnus-mime-button-map
4146      :help-echo
4147      (lambda (widget/window &optional overlay pos)
4148        ;; Needed to properly clear the message due to a bug in
4149        ;; wid-edit (XEmacs only).
4150        (if (boundp 'help-echo-owns-message)
4151            (setq help-echo-owns-message t))
4152        (format
4153         "%S: %s the MIME part; %S: more options"
4154         (aref gnus-mouse-2 0)
4155         ;; XEmacs will get a single widget arg; Emacs 21 will get
4156         ;; window, overlay, position.
4157         (if (mm-handle-displayed-p
4158              (if overlay
4159                  (with-current-buffer (gnus-overlay-buffer overlay)
4160                    (widget-get (widget-at (gnus-overlay-start overlay))
4161                                :mime-handle))
4162                (widget-get widget/window :mime-handle)))
4163             "hide" "show")
4164         (aref gnus-down-mouse-3 0))))))
4165
4166 (defun gnus-widget-press-button (elems el)
4167   (goto-char (widget-get elems :from))
4168   (gnus-article-press-button))
4169
4170 (defvar gnus-displaying-mime nil)
4171
4172 (defun gnus-display-mime (&optional ihandles)
4173   "Display the MIME parts."
4174   (save-excursion
4175     (save-selected-window
4176       (let ((window (get-buffer-window gnus-article-buffer))
4177             (point (point)))
4178         (when window
4179           (select-window window)
4180           ;; We have to do this since selecting the window
4181           ;; may change the point.  So we set the window point.
4182           (set-window-point window point)))
4183       (let* ((handles (or ihandles (mm-dissect-buffer
4184                                     nil gnus-article-loose-mime)
4185                           (mm-uu-dissect)))
4186              buffer-read-only handle name type b e display)
4187         (when (and (not ihandles)
4188                    (not gnus-displaying-mime))
4189           ;; Top-level call; we clean up.
4190           (when gnus-article-mime-handles
4191             (mm-destroy-parts gnus-article-mime-handles)
4192             (setq gnus-article-mime-handle-alist nil));; A trick.
4193           (setq gnus-article-mime-handles handles)
4194           ;; We allow users to glean info from the handles.
4195           (when gnus-article-mime-part-function
4196             (gnus-mime-part-function handles)))
4197         (if (and handles
4198                  (or (not (stringp (car handles)))
4199                      (cdr handles)))
4200             (progn
4201               (when (and (not ihandles)
4202                          (not gnus-displaying-mime))
4203                 ;; Clean up for mime parts.
4204                 (article-goto-body)
4205                 (delete-region (point) (point-max)))
4206               (let ((gnus-displaying-mime t))
4207                 (gnus-mime-display-part handles)))
4208           (save-restriction
4209             (article-goto-body)
4210             (narrow-to-region (point) (point-max))
4211             (gnus-treat-article nil 1 1)
4212             (widen)))
4213         (unless ihandles
4214           ;; Highlight the headers.
4215           (save-excursion
4216             (save-restriction
4217               (article-goto-body)
4218               (narrow-to-region (point-min) (point))
4219               (gnus-treat-article 'head))))))))
4220
4221 (defvar gnus-mime-display-multipart-as-mixed nil)
4222 (defvar gnus-mime-display-multipart-alternative-as-mixed nil)
4223 (defvar gnus-mime-display-multipart-related-as-mixed nil)
4224
4225 (defun gnus-mime-display-part (handle)
4226   (cond
4227    ;; Single part.
4228    ((not (stringp (car handle)))
4229     (gnus-mime-display-single handle))
4230    ;; User-defined multipart
4231    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4232     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4233              handle))
4234    ;; multipart/alternative
4235    ((and (equal (car handle) "multipart/alternative")
4236          (not (or gnus-mime-display-multipart-as-mixed
4237                   gnus-mime-display-multipart-alternative-as-mixed)))
4238     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4239       (push (cons id handle) gnus-article-mime-handle-alist)
4240       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4241    ;; multipart/related
4242    ((and (equal (car handle) "multipart/related")
4243          (not (or gnus-mime-display-multipart-as-mixed
4244                   gnus-mime-display-multipart-related-as-mixed)))
4245     ;;;!!!We should find the start part, but we just default
4246     ;;;!!!to the first part.
4247     ;;(gnus-mime-display-part (cadr handle))
4248     ;;;!!! Most multipart/related is an HTML message plus images.
4249     ;;;!!! Unfortunately we are unable to let W3 display those
4250     ;;;!!! included images, so we just display it as a mixed multipart.
4251     ;;(gnus-mime-display-mixed (cdr handle))
4252     ;;;!!! No, w3 can display everything just fine.
4253     (gnus-mime-display-part (cadr handle)))
4254    ((equal (car handle) "multipart/signed")
4255     (gnus-add-wash-type 'signed)
4256     (gnus-mime-display-security handle))
4257    ((equal (car handle) "multipart/encrypted")
4258     (gnus-add-wash-type 'encrypted)
4259     (gnus-mime-display-security handle))
4260    ;; Other multiparts are handled like multipart/mixed.
4261    (t
4262     (gnus-mime-display-mixed (cdr handle)))))
4263
4264 (defun gnus-mime-part-function (handles)
4265   (if (stringp (car handles))
4266       (mapcar 'gnus-mime-part-function (cdr handles))
4267     (funcall gnus-article-mime-part-function handles)))
4268
4269 (defun gnus-mime-display-mixed (handles)
4270   (mapcar 'gnus-mime-display-part handles))
4271
4272 (defun gnus-mime-display-single (handle)
4273   (let ((type (mm-handle-media-type handle))
4274         (ignored gnus-ignored-mime-types)
4275         (not-attachment t)
4276         (move nil)
4277         display text)
4278     (catch 'ignored
4279       (progn
4280         (while ignored
4281           (when (string-match (pop ignored) type)
4282             (throw 'ignored nil)))
4283         (if (and (setq not-attachment
4284                        (and (not (mm-inline-override-p handle))
4285                             (or (not (mm-handle-disposition handle))
4286                                 (equal (car (mm-handle-disposition handle))
4287                                        "inline")
4288                                 (mm-attachment-override-p handle))))
4289                  (mm-automatic-display-p handle)
4290                  (or (and
4291                       (mm-inlinable-p handle)
4292                       (mm-inlined-p handle))
4293                      (mm-automatic-external-display-p type)))
4294             (setq display t)
4295           (when (equal (mm-handle-media-supertype handle) "text")
4296             (setq text t)))
4297         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4298               beg)
4299           (push (cons id handle) gnus-article-mime-handle-alist)
4300           (when (or (not display)
4301                     (not (gnus-unbuttonized-mime-type-p type)))
4302             ;(gnus-article-insert-newline)
4303             (gnus-insert-mime-button
4304              handle id (list (or display (and not-attachment text))))
4305             (gnus-article-insert-newline)
4306             ;(gnus-article-insert-newline)
4307             ;; Remember modify the number of forward lines.
4308             (setq move t))
4309           (setq beg (point))
4310           (cond
4311            (display
4312             (when move
4313               (forward-line -1)
4314               (setq beg (point)))
4315             (let ((mail-parse-charset gnus-newsgroup-charset)
4316                   (mail-parse-ignored-charsets
4317                    (save-excursion (condition-case ()
4318                                        (set-buffer gnus-summary-buffer)
4319                                      (error))
4320                                    gnus-newsgroup-ignored-charsets)))
4321               (mm-display-part handle t))
4322             (goto-char (point-max)))
4323            ((and text not-attachment)
4324             (when move
4325               (forward-line -1)
4326               (setq beg (point)))
4327             (gnus-article-insert-newline)
4328             (mm-insert-inline handle (mm-get-part handle))
4329             (goto-char (point-max))))
4330           ;; Do highlighting.
4331           (save-excursion
4332             (save-restriction
4333               (narrow-to-region beg (point))
4334               (gnus-treat-article
4335                nil id
4336                (gnus-article-mime-total-parts)
4337                (mm-handle-media-type handle)))))))))
4338
4339 (defun gnus-unbuttonized-mime-type-p (type)
4340   "Say whether TYPE is to be unbuttonized."
4341   (unless gnus-inhibit-mime-unbuttonizing
4342     (when (catch 'found
4343             (let ((types gnus-unbuttonized-mime-types))
4344               (while types
4345                 (when (string-match (pop types) type)
4346                   (throw 'found t)))))
4347       (not (catch 'found
4348              (let ((types gnus-buttonized-mime-types))
4349                (while types
4350                  (when (string-match (pop types) type)
4351                    (throw 'found t)))))))))
4352
4353 (defun gnus-article-insert-newline ()
4354   "Insert a newline, but mark it as undeletable."
4355   (gnus-put-text-property
4356    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4357
4358 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4359   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4360          (ihandles handles)
4361          (point (point))
4362          handle buffer-read-only from props begend not-pref)
4363     (save-window-excursion
4364       (save-restriction
4365         (when ibegend
4366           (narrow-to-region (car ibegend)
4367                             (or (cdr ibegend)
4368                                 (progn
4369                                   (goto-char (car ibegend))
4370                                   (forward-line 2)
4371                                   (point))))
4372           (delete-region (point-min) (point-max))
4373           (mm-remove-parts handles))
4374         (setq begend (list (point-marker)))
4375         ;; Do the toggle.
4376         (unless (setq not-pref (cadr (member preferred ihandles)))
4377           (setq not-pref (car ihandles)))
4378         (when (or ibegend
4379                   (not preferred)
4380                   (not (gnus-unbuttonized-mime-type-p
4381                         "multipart/alternative")))
4382           (gnus-add-text-properties
4383            (setq from (point))
4384            (progn
4385              (insert (format "%d.  " id))
4386              (point))
4387            `(gnus-callback
4388              (lambda (handles)
4389                (unless ,(not ibegend)
4390                  (setq gnus-article-mime-handle-alist
4391                        ',gnus-article-mime-handle-alist))
4392                (gnus-mime-display-alternative
4393                 ',ihandles ',not-pref ',begend ,id))
4394              ,@(gnus-local-map-property gnus-mime-button-map)
4395              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4396              face ,gnus-article-button-face
4397              gnus-part ,id
4398              gnus-data ,handle))
4399           (widget-convert-button 'link from (point)
4400                                  :action 'gnus-widget-press-button
4401                                  :button-keymap gnus-widget-button-keymap)
4402           ;; Do the handles
4403           (while (setq handle (pop handles))
4404             (gnus-add-text-properties
4405              (setq from (point))
4406              (progn
4407                (insert (format "(%c) %-18s"
4408                                (if (equal handle preferred) ?* ? )
4409                                (mm-handle-media-type handle)))
4410                (point))
4411              `(gnus-callback
4412                (lambda (handles)
4413                  (unless ,(not ibegend)
4414                    (setq gnus-article-mime-handle-alist
4415                          ',gnus-article-mime-handle-alist))
4416                  (gnus-mime-display-alternative
4417                   ',ihandles ',handle ',begend ,id))
4418                ,@(gnus-local-map-property gnus-mime-button-map)
4419                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4420                face ,gnus-article-button-face
4421                gnus-part ,id
4422                gnus-data ,handle))
4423             (widget-convert-button 'link from (point)
4424                                    :action 'gnus-widget-press-button
4425                                    :button-keymap gnus-widget-button-keymap)
4426             (insert "  "))
4427           (insert "\n\n"))
4428         (when preferred
4429           (if (stringp (car preferred))
4430               (gnus-display-mime preferred)
4431             (let ((mail-parse-charset gnus-newsgroup-charset)
4432                   (mail-parse-ignored-charsets
4433                    (save-excursion (set-buffer gnus-summary-buffer)
4434                                    gnus-newsgroup-ignored-charsets)))
4435               (mm-display-part preferred)
4436               ;; Do highlighting.
4437               (save-excursion
4438                 (save-restriction
4439                   (narrow-to-region (car begend) (point-max))
4440                   (gnus-treat-article
4441                    nil (length gnus-article-mime-handle-alist)
4442                    (gnus-article-mime-total-parts)
4443                    (mm-handle-media-type handle))))))
4444           (goto-char (point-max))
4445           (setcdr begend (point-marker)))))
4446     (when ibegend
4447       (goto-char point))))
4448
4449 (defconst gnus-article-wash-status-strings
4450   (let ((alist '((cite "c" "Possible hidden citation text"
4451                        " " "All citation text visible")
4452                  (headers "h" "Hidden headers"
4453                           " " "All headers visible.")
4454                  (pgp "p" "Encrypted or signed message status hidden"
4455                       " " "No hidden encryption nor digital signature status")
4456                  (signature "s" "Signature has been hidden"
4457                             " " "Signature is visible")
4458                  (overstrike "o" "Overstrike (^H) characters applied"
4459                              " " "No overstrike characters applied")
4460                  (emphasis "e" "/*_Emphasis_*/ characters applied"
4461                            " " "No /*_emphasis_*/ characters applied")))
4462         result)
4463     (dolist (entry alist result)
4464       (let ((key (nth 0 entry))
4465             (on (copy-sequence (nth 1 entry)))
4466             (on-help (nth 2 entry))
4467             (off (copy-sequence (nth 3 entry)))
4468             (off-help (nth 4 entry)))
4469         (put-text-property 0 1 'help-echo on-help on)
4470         (put-text-property 0 1 'help-echo off-help off)
4471         (push (list key on off) result))))
4472   "Alist of strings describing wash status in the mode line.
4473 Each entry has the form (KEY ON OF), where the KEY is a symbol
4474 representing the particular washing function, ON is the string to use
4475 in the article mode line when the washing function is active, and OFF
4476 is the string to use when it is inactive.")
4477
4478 (defun gnus-article-wash-status-entry (key value)
4479   (let ((entry (assoc key gnus-article-wash-status-strings)))
4480     (if value (nth 1 entry) (nth 2 entry))))
4481
4482 (defun gnus-article-wash-status ()
4483   "Return a string which display status of article washing."
4484   (save-excursion
4485     (set-buffer gnus-article-buffer)
4486     (let ((cite (memq 'cite gnus-article-wash-types))
4487           (headers (memq 'headers gnus-article-wash-types))
4488           (boring (memq 'boring-headers gnus-article-wash-types))
4489           (pgp (memq 'pgp gnus-article-wash-types))
4490           (pem (memq 'pem gnus-article-wash-types))
4491           (signed (memq 'signed gnus-article-wash-types))
4492           (encrypted (memq 'encrypted gnus-article-wash-types))
4493           (signature (memq 'signature gnus-article-wash-types))
4494           (overstrike (memq 'overstrike gnus-article-wash-types))
4495           (emphasis (memq 'emphasis gnus-article-wash-types)))
4496       (concat
4497        (gnus-article-wash-status-entry 'cite cite)
4498        (gnus-article-wash-status-entry 'headers (or headers boring))
4499        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
4500        (gnus-article-wash-status-entry 'signature signature)
4501        (gnus-article-wash-status-entry 'overstrike overstrike)
4502        (gnus-article-wash-status-entry 'emphasis emphasis)))))
4503
4504 (defun gnus-add-wash-type (type)
4505   "Add a washing of TYPE to the current status."
4506   (add-to-list 'gnus-article-wash-types type))
4507
4508 (defun gnus-delete-wash-type (type)
4509   "Add a washing of TYPE to the current status."
4510   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
4511
4512 (defun gnus-add-image (category image)
4513   "Add IMAGE of CATEGORY to the list of displayed images."
4514   (let ((entry (assq category gnus-article-image-alist)))
4515     (unless entry
4516       (setq entry (list category))
4517       (push entry gnus-article-image-alist))
4518     (nconc entry (list image))))
4519
4520 (defun gnus-delete-images (category)
4521   "Delete all images in CATEGORY."
4522   (let ((entry (assq category gnus-article-image-alist)))
4523     (dolist (image (cdr entry))
4524       (gnus-remove-image image))
4525     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
4526     (gnus-delete-wash-type category)))
4527
4528 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4529
4530 (defun gnus-article-maybe-hide-headers ()
4531   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4532 Provided for backwards compatibility."
4533   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4534                  (not (save-excursion (set-buffer gnus-summary-buffer)
4535                                       gnus-have-all-headers)))
4536              (not gnus-inhibit-hiding))
4537     (gnus-article-hide-headers)))
4538
4539 ;;; Article savers.
4540
4541 (defun gnus-output-to-file (file-name)
4542   "Append the current article to a file named FILE-NAME."
4543   (let ((artbuf (current-buffer)))
4544     (with-temp-buffer
4545       (insert-buffer-substring artbuf)
4546       ;; Append newline at end of the buffer as separator, and then
4547       ;; save it to file.
4548       (goto-char (point-max))
4549       (insert "\n")
4550       (let ((file-name-coding-system nnmail-pathname-coding-system))
4551         (mm-append-to-file (point-min) (point-max) file-name))
4552       t)))
4553
4554 (defun gnus-narrow-to-page (&optional arg)
4555   "Narrow the article buffer to a page.
4556 If given a numerical ARG, move forward ARG pages."
4557   (interactive "P")
4558   (setq arg (if arg (prefix-numeric-value arg) 0))
4559   (save-excursion
4560     (set-buffer gnus-article-buffer)
4561     (goto-char (point-min))
4562     (widen)
4563     ;; Remove any old next/prev buttons.
4564     (when (gnus-visual-p 'page-marker)
4565       (let ((buffer-read-only nil))
4566         (gnus-remove-text-with-property 'gnus-prev)
4567         (gnus-remove-text-with-property 'gnus-next)))
4568     (when
4569         (cond ((< arg 0)
4570                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4571               ((> arg 0)
4572                (re-search-forward page-delimiter nil 'move arg)))
4573       (goto-char (match-end 0)))
4574     (narrow-to-region
4575      (point)
4576      (if (re-search-forward page-delimiter nil 'move)
4577          (match-beginning 0)
4578        (point)))
4579     (when (and (gnus-visual-p 'page-marker)
4580                (not (= (point-min) 1)))
4581       (save-excursion
4582         (goto-char (point-min))
4583         (gnus-insert-prev-page-button)))
4584     (when (and (gnus-visual-p 'page-marker)
4585                (< (+ (point-max) 2) (buffer-size)))
4586       (save-excursion
4587         (goto-char (point-max))
4588         (gnus-insert-next-page-button)))))
4589
4590 ;; Article mode commands
4591
4592 (defun gnus-article-goto-next-page ()
4593   "Show the next page of the article."
4594   (interactive)
4595   (when (gnus-article-next-page)
4596     (goto-char (point-min))
4597     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4598
4599 (defun gnus-article-goto-prev-page ()
4600   "Show the next page of the article."
4601   (interactive)
4602   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4603     (gnus-article-prev-page nil)))
4604
4605 (defun gnus-article-next-page (&optional lines)
4606   "Show the next page of the current article.
4607 If end of article, return non-nil.  Otherwise return nil.
4608 Argument LINES specifies lines to be scrolled up."
4609   (interactive "p")
4610   (move-to-window-line -1)
4611   (if (save-excursion
4612         (end-of-line)
4613         (and (pos-visible-in-window-p)  ;Not continuation line.
4614              (eobp)))
4615       ;; Nothing in this page.
4616       (if (or (not gnus-page-broken)
4617               (save-excursion
4618                 (save-restriction
4619                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
4620           t                             ;Nothing more.
4621         (gnus-narrow-to-page 1)         ;Go to next page.
4622         nil)
4623     ;; More in this page.
4624     (let ((scroll-in-place nil))
4625       (condition-case ()
4626           (scroll-up lines)
4627         (end-of-buffer
4628          ;; Long lines may cause an end-of-buffer error.
4629          (goto-char (point-max)))))
4630     (move-to-window-line 0)
4631     nil))
4632
4633 (defun gnus-article-prev-page (&optional lines)
4634   "Show previous page of current article.
4635 Argument LINES specifies lines to be scrolled down."
4636   (interactive "p")
4637   (move-to-window-line 0)
4638   (if (and gnus-page-broken
4639            (bobp)
4640            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
4641       (progn
4642         (gnus-narrow-to-page -1)        ;Go to previous page.
4643         (goto-char (point-max))
4644         (recenter -1))
4645     (let ((scroll-in-place nil))
4646       (prog1
4647           (condition-case ()
4648               (scroll-down lines)
4649             (beginning-of-buffer
4650              (goto-char (point-min))))
4651         (move-to-window-line 0)))))
4652
4653 (defun gnus-article-refer-article ()
4654   "Read article specified by message-id around point."
4655   (interactive)
4656   (let ((point (point)))
4657     (search-forward ">" nil t)          ;Move point to end of "<....>".
4658     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4659         (let ((message-id (match-string 1)))
4660           (goto-char point)
4661           (set-buffer gnus-summary-buffer)
4662           (gnus-summary-refer-article message-id))
4663       (goto-char (point))
4664       (error "No references around point"))))
4665
4666 (defun gnus-article-show-summary ()
4667   "Reconfigure windows to show summary buffer."
4668   (interactive)
4669   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4670       (error "There is no summary buffer for this article buffer")
4671     (gnus-article-set-globals)
4672     (gnus-configure-windows 'article)
4673     (gnus-summary-goto-subject gnus-current-article)
4674     (gnus-summary-position-point)))
4675
4676 (defun gnus-article-describe-briefly ()
4677   "Describe article mode commands briefly."
4678   (interactive)
4679   (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")))
4680
4681 (defun gnus-article-summary-command ()
4682   "Execute the last keystroke in the summary buffer."
4683   (interactive)
4684   (let ((obuf (current-buffer))
4685         (owin (current-window-configuration))
4686         func)
4687     (switch-to-buffer gnus-article-current-summary 'norecord)
4688     (setq func (lookup-key (current-local-map) (this-command-keys)))
4689     (call-interactively func)
4690     (set-buffer obuf)
4691     (set-window-configuration owin)
4692     (set-window-point (get-buffer-window (current-buffer)) (point))))
4693
4694 (defun gnus-article-summary-command-nosave ()
4695   "Execute the last keystroke in the summary buffer."
4696   (interactive)
4697   (let (func)
4698     (pop-to-buffer gnus-article-current-summary 'norecord)
4699     (setq func (lookup-key (current-local-map) (this-command-keys)))
4700     (call-interactively func)))
4701
4702 (defun gnus-article-check-buffer ()
4703   "Beep if not in an article buffer."
4704   (unless (equal major-mode 'gnus-article-mode)
4705     (error "Command invoked outside of a Gnus article buffer")))
4706
4707 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
4708   "Read a summary buffer key sequence and execute it from the article buffer."
4709   (interactive "P")
4710   (gnus-article-check-buffer)
4711   (let ((nosaves
4712          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
4713            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
4714            "=" "^" "\M-^" "|"))
4715         (nosave-but-article
4716          '("A\r"))
4717         (nosave-in-article
4718          '("\C-d"))
4719         (up-to-top
4720          '("n" "Gn" "p" "Gp"))
4721         keys new-sum-point)
4722     (save-excursion
4723       (set-buffer gnus-article-current-summary)
4724       (let (gnus-pick-mode)
4725         (push (or key last-command-event) unread-command-events)
4726         (setq keys (if (featurep 'xemacs)
4727                        (events-to-keys (read-key-sequence nil))
4728                      (read-key-sequence nil)))))
4729
4730     (message "")
4731
4732     (if (or (member keys nosaves)
4733             (member keys nosave-but-article)
4734             (member keys nosave-in-article))
4735         (let (func)
4736           (save-window-excursion
4737             (pop-to-buffer gnus-article-current-summary 'norecord)
4738             ;; We disable the pick minor mode commands.
4739             (let (gnus-pick-mode)
4740               (setq func (lookup-key (current-local-map) keys))))
4741           (if (or (not func)
4742                   (numberp func))
4743               (ding)
4744             (unless (member keys nosave-in-article)
4745               (set-buffer gnus-article-current-summary))
4746             (call-interactively func)
4747             (setq new-sum-point (point)))
4748           (when (member keys nosave-but-article)
4749             (pop-to-buffer gnus-article-buffer 'norecord)))
4750       ;; These commands should restore window configuration.
4751       (let ((obuf (current-buffer))
4752             (owin (current-window-configuration))
4753             (opoint (point))
4754             (summary gnus-article-current-summary)
4755             func in-buffer selected)
4756         (if not-restore-window
4757             (pop-to-buffer summary 'norecord)
4758           (switch-to-buffer summary 'norecord))
4759         (setq in-buffer (current-buffer))
4760         ;; We disable the pick minor mode commands.
4761         (if (and (setq func (let (gnus-pick-mode)
4762                               (lookup-key (current-local-map) keys)))
4763                  (functionp func))
4764             (progn
4765               (call-interactively func)
4766               (setq new-sum-point (point))
4767               (when (eq in-buffer (current-buffer))
4768                 (setq selected (gnus-summary-select-article))
4769                 (set-buffer obuf)
4770                 (unless not-restore-window
4771                   (set-window-configuration owin))
4772                 (when (eq selected 'old)
4773                   (article-goto-body)
4774                   (set-window-start (get-buffer-window (current-buffer))
4775                                     1)
4776                   (set-window-point (get-buffer-window (current-buffer))
4777                                     (point)))
4778                 (let ((win (get-buffer-window gnus-article-current-summary)))
4779                   (when win
4780                     (set-window-point win new-sum-point))))    )
4781           (switch-to-buffer gnus-article-buffer)
4782           (ding))))))
4783
4784 (defun gnus-article-describe-key (key)
4785   "Display documentation of the function invoked by KEY.  KEY is a string."
4786   (interactive "kDescribe key: ")
4787   (gnus-article-check-buffer)
4788   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4789       (save-excursion
4790         (set-buffer gnus-article-current-summary)
4791         (let (gnus-pick-mode)
4792           (if (featurep 'xemacs)
4793               (progn
4794                 (push (elt key 0) unread-command-events)
4795                 (setq key (events-to-keys
4796                            (read-key-sequence "Describe key: "))))
4797             (setq unread-command-events
4798                   (mapcar
4799                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
4800                    (string-to-list key)))
4801             (setq key (read-key-sequence "Describe key: "))))
4802         (describe-key key))
4803     (describe-key key)))
4804
4805 (defun gnus-article-describe-key-briefly (key &optional insert)
4806   "Display documentation of the function invoked by KEY.  KEY is a string."
4807   (interactive "kDescribe key: \nP")
4808   (gnus-article-check-buffer)
4809   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4810       (save-excursion
4811         (set-buffer gnus-article-current-summary)
4812         (let (gnus-pick-mode)
4813           (if (featurep 'xemacs)
4814               (progn
4815                 (push (elt key 0) unread-command-events)
4816                 (setq key (events-to-keys
4817                            (read-key-sequence "Describe key: "))))
4818             (setq unread-command-events
4819                   (mapcar
4820                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
4821                    (string-to-list key)))
4822             (setq key (read-key-sequence "Describe key: "))))
4823         (describe-key-briefly key insert))
4824     (describe-key-briefly key insert)))
4825
4826 (defun gnus-article-reply-with-original (&optional wide)
4827   "Start composing a reply mail to the current message.
4828 The text in the region will be yanked.  If the region isn't active,
4829 the entire article will be yanked."
4830   (interactive "P")
4831   (let ((article (cdr gnus-article-current)) cont)
4832     (if (not (mark t))
4833         (gnus-summary-reply (list (list article)) wide)
4834       (setq cont (buffer-substring (point) (mark t)))
4835       ;; Deactivate active regions.
4836       (when (and (boundp 'transient-mark-mode)
4837                  transient-mark-mode)
4838         (setq mark-active nil))
4839       (gnus-summary-reply
4840        (list (list article cont)) wide))))
4841
4842 (defun gnus-article-followup-with-original ()
4843   "Compose a followup to the current article.
4844 The text in the region will be yanked.  If the region isn't active,
4845 the entire article will be yanked."
4846   (interactive)
4847   (let ((article (cdr gnus-article-current))
4848         cont)
4849     (if (not (mark t))
4850         (gnus-summary-followup (list (list article)))
4851       (setq cont (buffer-substring (point) (mark t)))
4852       ;; Deactivate active regions.
4853       (when (and (boundp 'transient-mark-mode)
4854                  transient-mark-mode)
4855         (setq mark-active nil))
4856       (gnus-summary-followup
4857        (list (list article cont))))))
4858
4859 (defun gnus-article-hide (&optional arg force)
4860   "Hide all the gruft in the current article.
4861 This means that PGP stuff, signatures, cited text and (some)
4862 headers will be hidden.
4863 If given a prefix, show the hidden text instead."
4864   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4865   (gnus-article-hide-headers arg)
4866   (gnus-article-hide-list-identifiers arg)
4867   (gnus-article-hide-pgp arg)
4868   (gnus-article-hide-citation-maybe arg force)
4869   (gnus-article-hide-signature arg))
4870
4871 (defun gnus-article-maybe-highlight ()
4872   "Do some article highlighting if article highlighting is requested."
4873   (when (gnus-visual-p 'article-highlight 'highlight)
4874     (gnus-article-highlight-some)))
4875
4876 (defun gnus-check-group-server ()
4877   ;; Make sure the connection to the server is alive.
4878   (unless (gnus-server-opened
4879            (gnus-find-method-for-group gnus-newsgroup-name))
4880     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4881     (gnus-request-group gnus-newsgroup-name t)))
4882
4883 (eval-when-compile
4884   (autoload 'nneething-get-file-name "nneething"))
4885
4886 (defun gnus-request-article-this-buffer (article group)
4887   "Get an article and insert it into this buffer."
4888   (let (do-update-line sparse-header)
4889     (prog1
4890         (save-excursion
4891           (erase-buffer)
4892           (gnus-kill-all-overlays)
4893           (setq group (or group gnus-newsgroup-name))
4894
4895           ;; Using `gnus-request-article' directly will insert the article into
4896           ;; `nntp-server-buffer' - so we'll save some time by not having to
4897           ;; copy it from the server buffer into the article buffer.
4898
4899           ;; We only request an article by message-id when we do not have the
4900           ;; headers for it, so we'll have to get those.
4901           (when (stringp article)
4902             (gnus-read-header article))
4903
4904           ;; If the article number is negative, that means that this article
4905           ;; doesn't belong in this newsgroup (possibly), so we find its
4906           ;; message-id and request it by id instead of number.
4907           (when (and (numberp article)
4908                      gnus-summary-buffer
4909                      (get-buffer gnus-summary-buffer)
4910                      (gnus-buffer-exists-p gnus-summary-buffer))
4911             (save-excursion
4912               (set-buffer gnus-summary-buffer)
4913               (let ((header (gnus-summary-article-header article)))
4914                 (when (< article 0)
4915                   (cond
4916                    ((memq article gnus-newsgroup-sparse)
4917                     ;; This is a sparse gap article.
4918                     (setq do-update-line article)
4919                     (setq article (mail-header-id header))
4920                     (setq sparse-header (gnus-read-header article))
4921                     (setq gnus-newsgroup-sparse
4922                           (delq article gnus-newsgroup-sparse)))
4923                    ((vectorp header)
4924                     ;; It's a real article.
4925                     (setq article (mail-header-id header)))
4926                    (t
4927                     ;; It is an extracted pseudo-article.
4928                     (setq article 'pseudo)
4929                     (gnus-request-pseudo-article header))))
4930
4931                 (let ((method (gnus-find-method-for-group
4932                                gnus-newsgroup-name)))
4933                   (when (and (eq (car method) 'nneething)
4934                              (vectorp header))
4935                     (let ((dir (nneething-get-file-name
4936                                 (mail-header-id header))))
4937                       (when (and (stringp dir)
4938                                  (file-directory-p dir))
4939                         (setq article 'nneething)
4940                         (gnus-group-enter-directory dir))))))))
4941
4942           (cond
4943            ;; Refuse to select canceled articles.
4944            ((and (numberp article)
4945                  gnus-summary-buffer
4946                  (get-buffer gnus-summary-buffer)
4947                  (gnus-buffer-exists-p gnus-summary-buffer)
4948                  (eq (cdr (save-excursion
4949                             (set-buffer gnus-summary-buffer)
4950                             (assq article gnus-newsgroup-reads)))
4951                      gnus-canceled-mark))
4952             nil)
4953            ;; We first check `gnus-original-article-buffer'.
4954            ((and (get-buffer gnus-original-article-buffer)
4955                  (numberp article)
4956                  (save-excursion
4957                    (set-buffer gnus-original-article-buffer)
4958                    (and (equal (car gnus-original-article) group)
4959                         (eq (cdr gnus-original-article) article))))
4960             (insert-buffer-substring gnus-original-article-buffer)
4961             'article)
4962            ;; Check the backlog.
4963            ((and gnus-keep-backlog
4964                  (gnus-backlog-request-article group article (current-buffer)))
4965             'article)
4966            ;; Check asynchronous pre-fetch.
4967            ((gnus-async-request-fetched-article group article (current-buffer))
4968             (gnus-async-prefetch-next group article gnus-summary-buffer)
4969             (when (and (numberp article) gnus-keep-backlog)
4970               (gnus-backlog-enter-article group article (current-buffer)))
4971             'article)
4972            ;; Check the cache.
4973            ((and gnus-use-cache
4974                  (numberp article)
4975                  (gnus-cache-request-article article group))
4976             'article)
4977            ;; Check the agent cache.
4978            ((and gnus-agent gnus-agent-cache gnus-plugged
4979                  (numberp article)
4980                  (gnus-agent-request-article article group))
4981             'article)
4982            ;; Get the article and put into the article buffer.
4983            ((or (stringp article)
4984                 (numberp article))
4985             (let ((gnus-override-method gnus-override-method)
4986                   (methods (and (stringp article)
4987                                 gnus-refer-article-method))
4988                   (backend (car (gnus-find-method-for-group
4989                                  gnus-newsgroup-name)))
4990                   result
4991                   (buffer-read-only nil))
4992               (if (or (not (listp methods))
4993                       (and (symbolp (car methods))
4994                            (assq (car methods) nnoo-definition-alist)))
4995                   (setq methods (list methods)))
4996               (when (and (null gnus-override-method)
4997                          methods)
4998                 (setq gnus-override-method (pop methods)))
4999               (while (not result)
5000                 (when (eq gnus-override-method 'current)
5001                   (setq gnus-override-method
5002                         (with-current-buffer gnus-summary-buffer
5003                           gnus-current-select-method)))
5004                 (erase-buffer)
5005                 (gnus-kill-all-overlays)
5006                 (let ((gnus-newsgroup-name group))
5007                   (gnus-check-group-server))
5008                 (cond
5009                  ((gnus-request-article article group (current-buffer))
5010                   (when (numberp article)
5011                     (gnus-async-prefetch-next group article
5012                                               gnus-summary-buffer)
5013                     (when gnus-keep-backlog
5014                       (gnus-backlog-enter-article
5015                        group article (current-buffer))))
5016                   (setq result 'article))
5017                  (methods
5018                   (setq gnus-override-method (pop methods)))
5019                  ((not (string-match "^400 "
5020                                      (nnheader-get-report backend)))
5021                   ;; If we get 400 server disconnect, reconnect and
5022                   ;; retry; otherwise, assume the article has expired.
5023                   (setq result 'done))))
5024               (and (eq result 'article) 'article)))
5025            ;; It was a pseudo.
5026            (t article)))
5027
5028       ;; Associate this article with the current summary buffer.
5029       (setq gnus-article-current-summary gnus-summary-buffer)
5030
5031       ;; Take the article from the original article buffer
5032       ;; and place it in the buffer it's supposed to be in.
5033       (when (and (get-buffer gnus-article-buffer)
5034                  (equal (buffer-name (current-buffer))
5035                         (buffer-name (get-buffer gnus-article-buffer))))
5036         (save-excursion
5037           (if (get-buffer gnus-original-article-buffer)
5038               (set-buffer gnus-original-article-buffer)
5039             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5040             (buffer-disable-undo)
5041             (setq major-mode 'gnus-original-article-mode)
5042             (setq buffer-read-only t))
5043           (let (buffer-read-only)
5044             (erase-buffer)
5045             (insert-buffer-substring gnus-article-buffer))
5046           (setq gnus-original-article (cons group article)))
5047
5048         ;; Decode charsets.
5049         (run-hooks 'gnus-article-decode-hook)
5050         ;; Mark article as decoded or not.
5051         (setq gnus-article-decoded-p gnus-article-decode-hook))
5052
5053       ;; Update sparse articles.
5054       (when (and do-update-line
5055                  (or (numberp article)
5056                      (stringp article)))
5057         (let ((buf (current-buffer)))
5058           (set-buffer gnus-summary-buffer)
5059           (gnus-summary-update-article do-update-line sparse-header)
5060           (gnus-summary-goto-subject do-update-line nil t)
5061           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5062                             (point))
5063           (set-buffer buf))))))
5064
5065 ;;;
5066 ;;; Article editing
5067 ;;;
5068
5069 (defcustom gnus-article-edit-mode-hook nil
5070   "Hook run in article edit mode buffers."
5071   :group 'gnus-article-various
5072   :type 'hook)
5073
5074 (defvar gnus-article-edit-done-function nil)
5075
5076 (defvar gnus-article-edit-mode-map nil)
5077
5078 ;; Should we be using derived.el for this?
5079 (unless gnus-article-edit-mode-map
5080   (setq gnus-article-edit-mode-map (make-keymap))
5081   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5082
5083
5084   (gnus-define-keys gnus-article-edit-mode-map
5085     "\C-c?"    describe-mode
5086     "\C-c\C-c" gnus-article-edit-done
5087     "\C-c\C-k" gnus-article-edit-exit
5088     "\C-c\C-f\C-t" message-goto-to
5089     "\C-c\C-f\C-o" message-goto-from
5090     "\C-c\C-f\C-b" message-goto-bcc
5091     ;;"\C-c\C-f\C-w" message-goto-fcc
5092     "\C-c\C-f\C-c" message-goto-cc
5093     "\C-c\C-f\C-s" message-goto-subject
5094     "\C-c\C-f\C-r" message-goto-reply-to
5095     "\C-c\C-f\C-n" message-goto-newsgroups
5096     "\C-c\C-f\C-d" message-goto-distribution
5097     "\C-c\C-f\C-f" message-goto-followup-to
5098     "\C-c\C-f\C-m" message-goto-mail-followup-to
5099     "\C-c\C-f\C-k" message-goto-keywords
5100     "\C-c\C-f\C-u" message-goto-summary
5101     "\C-c\C-f\C-i" message-insert-or-toggle-importance
5102     "\C-c\C-f\C-a" message-gen-unsubscribed-mft
5103     "\C-c\C-b" message-goto-body
5104     "\C-c\C-i" message-goto-signature
5105
5106     "\C-c\C-t" message-insert-to
5107     "\C-c\C-n" message-insert-newsgroups
5108     "\C-c\C-o" message-sort-headers
5109     "\C-c\C-e" message-elide-region
5110     "\C-c\C-v" message-delete-not-region
5111     "\C-c\C-z" message-kill-to-signature
5112     "\M-\r" message-newline-and-reformat
5113     "\C-c\C-a" mml-attach-file
5114     "\C-a" message-beginning-of-line
5115     "\t" message-tab
5116     "\M-;" comment-region)
5117
5118   (gnus-define-keys (gnus-article-edit-wash-map
5119                      "\C-c\C-w" gnus-article-edit-mode-map)
5120     "f" gnus-article-edit-full-stops))
5121
5122 (easy-menu-define
5123   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
5124   '("Field"
5125     ["Fetch To" message-insert-to t]
5126     ["Fetch Newsgroups" message-insert-newsgroups t]
5127     "----"
5128     ["To" message-goto-to t]
5129     ["From" message-goto-from t]
5130     ["Subject" message-goto-subject t]
5131     ["Cc" message-goto-cc t]
5132     ["Reply-To" message-goto-reply-to t]
5133     ["Summary" message-goto-summary t]
5134     ["Keywords" message-goto-keywords t]
5135     ["Newsgroups" message-goto-newsgroups t]
5136     ["Followup-To" message-goto-followup-to t]
5137     ["Mail-Followup-To" message-goto-mail-followup-to t]
5138     ["Distribution" message-goto-distribution t]
5139     ["Body" message-goto-body t]
5140     ["Signature" message-goto-signature t]))
5141
5142 (define-derived-mode gnus-article-edit-mode text-mode "Article Edit"
5143   "Major mode for editing articles.
5144 This is an extended text-mode.
5145
5146 \\{gnus-article-edit-mode-map}"
5147   (make-local-variable 'gnus-article-edit-done-function)
5148   (make-local-variable 'gnus-prev-winconf)
5149   (set (make-local-variable 'font-lock-defaults)
5150        '(message-font-lock-keywords t))
5151   (set (make-local-variable 'mail-header-separator) "")
5152   (easy-menu-add message-mode-field-menu message-mode-map)
5153   (mml-mode)
5154   (setq buffer-read-only nil)
5155   (buffer-enable-undo)
5156   (widen))
5157
5158 (defun gnus-article-edit (&optional force)
5159   "Edit the current article.
5160 This will have permanent effect only in mail groups.
5161 If FORCE is non-nil, allow editing of articles even in read-only
5162 groups."
5163   (interactive "P")
5164   (when (and (not force)
5165              (gnus-group-read-only-p))
5166     (error "The current newsgroup does not support article editing"))
5167   (gnus-article-date-original)
5168   (gnus-article-edit-article
5169    'ignore
5170    `(lambda (no-highlight)
5171       'ignore
5172       (gnus-summary-edit-article-done
5173        ,(or (mail-header-references gnus-current-headers) "")
5174        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
5175
5176 (defun gnus-article-edit-article (start-func exit-func)
5177   "Start editing the contents of the current article buffer."
5178   (let ((winconf (current-window-configuration)))
5179     (set-buffer gnus-article-buffer)
5180     (gnus-article-edit-mode)
5181     (funcall start-func)
5182     (set-buffer-modified-p nil)
5183     (gnus-configure-windows 'edit-article)
5184     (setq gnus-article-edit-done-function exit-func)
5185     (setq gnus-prev-winconf winconf)
5186     (gnus-message 6 "C-c C-c to end edits")))
5187
5188 (defun gnus-article-edit-done (&optional arg)
5189   "Update the article edits and exit."
5190   (interactive "P")
5191   (let ((func gnus-article-edit-done-function)
5192         (buf (current-buffer))
5193         (start (window-start))
5194         (p (point))
5195         (winconf gnus-prev-winconf))
5196     (widen) ;; Widen it in case that users narrowed the buffer.
5197     (funcall func arg)
5198     (set-buffer buf)
5199     ;; The cache and backlog have to be flushed somewhat.
5200     (when gnus-keep-backlog
5201       (gnus-backlog-remove-article
5202        (car gnus-article-current) (cdr gnus-article-current)))
5203     ;; Flush original article as well.
5204     (save-excursion
5205       (when (get-buffer gnus-original-article-buffer)
5206         (set-buffer gnus-original-article-buffer)
5207         (setq gnus-original-article nil)))
5208     (when gnus-use-cache
5209       (gnus-cache-update-article
5210        (car gnus-article-current) (cdr gnus-article-current)))
5211     ;; We remove all text props from the article buffer.
5212     (kill-all-local-variables)
5213     (gnus-set-text-properties (point-min) (point-max) nil)
5214     (gnus-article-mode)
5215     (set-window-configuration winconf)
5216     (set-buffer buf)
5217     (set-window-start (get-buffer-window buf) start)
5218     (set-window-point (get-buffer-window buf) (point))))
5219
5220 (defun gnus-article-edit-exit ()
5221   "Exit the article editing without updating."
5222   (interactive)
5223   (when (or (not (buffer-modified-p))
5224             (yes-or-no-p "Article modified; kill anyway? "))
5225     (let ((curbuf (current-buffer))
5226           (p (point))
5227           (window-start (window-start)))
5228       (erase-buffer)
5229       (if (gnus-buffer-live-p gnus-original-article-buffer)
5230           (insert-buffer gnus-original-article-buffer))
5231       (let ((winconf gnus-prev-winconf))
5232         (gnus-article-mode)
5233         (set-window-configuration winconf)
5234         ;; Tippy-toe some to make sure that point remains where it was.
5235         (save-current-buffer
5236           (set-buffer curbuf)
5237           (set-window-start (get-buffer-window (current-buffer)) window-start)
5238           (goto-char p))))))
5239
5240 (defun gnus-article-edit-full-stops ()
5241   "Interactively repair spacing at end of sentences."
5242   (interactive)
5243   (save-excursion
5244     (goto-char (point-min))
5245     (search-forward-regexp "^$" nil t)
5246     (let ((case-fold-search nil))
5247       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
5248
5249 ;;;
5250 ;;; Article highlights
5251 ;;;
5252
5253 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
5254
5255 ;;; Internal Variables:
5256
5257 (defcustom gnus-button-url-regexp 
5258   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
5259       "\\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:]]\\)"
5260     "\\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\\)\\)")
5261   "Regular expression that matches URLs."
5262   :group 'gnus-article-buttons
5263   :type 'regexp)
5264
5265 (defcustom gnus-button-man-handler 'man
5266   "Function to use for displaying man pages.
5267 The function must take at least one argument with a string naming the
5268 man page."
5269   :type '(choice (function-item :tag "Man" man)
5270                  (function-item :tag "Woman" woman)
5271                  (function :tag "Other"))
5272   :group 'gnus-article-buttons)
5273
5274 (defcustom gnus-button-alist
5275   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
5276      0 t gnus-button-handle-news 3)
5277     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
5278      gnus-button-handle-news 2)
5279     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
5280      1 t
5281      gnus-button-fetch-group 4)
5282     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
5283     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
5284      t gnus-button-message-id 3)
5285     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
5286     ("mailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5287     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
5288     ;; This is info
5289     ("\\binfo:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t
5290      gnus-button-handle-info 2)
5291     ;; This is how URLs _should_ be embedded in text...
5292     ("<URL: *\\([^<>]*\\)>" 1 t gnus-button-embedded-url 1)
5293     ;; Raw URLs.
5294     (gnus-button-url-regexp 0 t browse-url 0)
5295     ;; man pages
5296     ("\\b\\([a-z]+\\)([0-9])\\W" 0 t gnus-button-handle-man 1))
5297   "*Alist of regexps matching buttons in article bodies.
5298
5299 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
5300 REGEXP: is the string matching text around the button (can also be lisp 
5301 expression evaluating to a string),
5302 BUTTON: is the number of the regexp grouping actually matching the button,
5303 FORM: is a lisp expression which must eval to true for the button to
5304 be added,
5305 CALLBACK: is the function to call when the user push this button, and each
5306 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
5307
5308 CALLBACK can also be a variable, in that case the value of that
5309 variable it the real callback function."
5310   :group 'gnus-article-buttons
5311   :type '(repeat (list (choice regexp variable)
5312                        (integer :tag "Button")
5313                        (sexp :tag "Form")
5314                        (function :tag "Callback")
5315                        (repeat :tag "Par"
5316                                :inline t
5317                                (integer :tag "Regexp group")))))
5318
5319 (defcustom gnus-header-button-alist
5320   '(("^\\(References\\|Message-I[Dd]\\):" "<[^<>]+>"
5321      0 t gnus-button-message-id 0)
5322     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
5323     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
5324      0 t gnus-button-mailto 0)
5325     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp 0 t browse-url 0)
5326     ("^Subject:" gnus-button-url-regexp 0 t browse-url 0)
5327     ("^[^:]+:" gnus-button-url-regexp 0 t browse-url 0)
5328     ("^[^:]+:" "\\bmailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5329     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
5330      gnus-button-message-id 3))
5331   "*Alist of headers and regexps to match buttons in article heads.
5332
5333 This alist is very similar to `gnus-button-alist', except that each
5334 alist has an additional HEADER element first in each entry:
5335
5336 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
5337
5338 HEADER is a regexp to match a header.  For a fuller explanation, see
5339 `gnus-button-alist'."
5340   :group 'gnus-article-buttons
5341   :group 'gnus-article-headers
5342   :type '(repeat (list (regexp :tag "Header")
5343                        regexp
5344                        (integer :tag "Button")
5345                        (sexp :tag "Form")
5346                        (function :tag "Callback")
5347                        (repeat :tag "Par"
5348                                :inline t
5349                                (integer :tag "Regexp group")))))
5350
5351 (defvar gnus-button-regexp nil)
5352 (defvar gnus-button-marker-list nil)
5353 ;; Regexp matching any of the regexps from `gnus-button-alist'.
5354
5355 (defvar gnus-button-last nil)
5356 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
5357
5358 ;;; Commands:
5359
5360 (defun gnus-article-push-button (event)
5361   "Check text under the mouse pointer for a callback function.
5362 If the text under the mouse pointer has a `gnus-callback' property,
5363 call it with the value of the `gnus-data' text property."
5364   (interactive "e")
5365   (set-buffer (window-buffer (posn-window (event-start event))))
5366   (let* ((pos (posn-point (event-start event)))
5367          (data (get-text-property pos 'gnus-data))
5368          (fun (get-text-property pos 'gnus-callback)))
5369     (goto-char pos)
5370     (when fun
5371       (funcall fun data))))
5372
5373 (defun gnus-article-press-button ()
5374   "Check text at point for a callback function.
5375 If the text at point has a `gnus-callback' property,
5376 call it with the value of the `gnus-data' text property."
5377   (interactive)
5378   (let* ((data (get-text-property (point) 'gnus-data))
5379          (fun (get-text-property (point) 'gnus-callback)))
5380     (when fun
5381       (funcall fun data))))
5382
5383 (defun gnus-article-highlight (&optional force)
5384   "Highlight current article.
5385 This function calls `gnus-article-highlight-headers',
5386 `gnus-article-highlight-citation',
5387 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5388 do the highlighting.  See the documentation for those functions."
5389   (interactive (list 'force))
5390   (gnus-article-highlight-headers)
5391   (gnus-article-highlight-citation force)
5392   (gnus-article-highlight-signature)
5393   (gnus-article-add-buttons force)
5394   (gnus-article-add-buttons-to-head))
5395
5396 (defun gnus-article-highlight-some (&optional force)
5397   "Highlight current article.
5398 This function calls `gnus-article-highlight-headers',
5399 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5400 do the highlighting.  See the documentation for those functions."
5401   (interactive (list 'force))
5402   (gnus-article-highlight-headers)
5403   (gnus-article-highlight-signature)
5404   (gnus-article-add-buttons))
5405
5406 (defun gnus-article-highlight-headers ()
5407   "Highlight article headers as specified by `gnus-header-face-alist'."
5408   (interactive)
5409   (save-excursion
5410     (set-buffer gnus-article-buffer)
5411     (save-restriction
5412       (let ((alist gnus-header-face-alist)
5413             (buffer-read-only nil)
5414             (case-fold-search t)
5415             (inhibit-point-motion-hooks t)
5416             entry regexp header-face field-face from hpoints fpoints)
5417         (article-narrow-to-head)
5418         (while (setq entry (pop alist))
5419           (goto-char (point-min))
5420           (setq regexp (concat "^\\("
5421                                (if (string-equal "" (nth 0 entry))
5422                                    "[^\t ]"
5423                                  (nth 0 entry))
5424                                "\\)")
5425                 header-face (nth 1 entry)
5426                 field-face (nth 2 entry))
5427           (while (and (re-search-forward regexp nil t)
5428                       (not (eobp)))
5429             (beginning-of-line)
5430             (setq from (point))
5431             (unless (search-forward ":" nil t)
5432               (forward-char 1))
5433             (when (and header-face
5434                        (not (memq (point) hpoints)))
5435               (push (point) hpoints)
5436               (gnus-put-text-property from (point) 'face header-face))
5437             (when (and field-face
5438                        (not (memq (setq from (point)) fpoints)))
5439               (push from fpoints)
5440               (if (re-search-forward "^[^ \t]" nil t)
5441                   (forward-char -2)
5442                 (goto-char (point-max)))
5443               (gnus-put-text-property from (point) 'face field-face))))))))
5444
5445 (defun gnus-article-highlight-signature ()
5446   "Highlight the signature in an article.
5447 It does this by highlighting everything after
5448 `gnus-signature-separator' using `gnus-signature-face'."
5449   (interactive)
5450   (save-excursion
5451     (set-buffer gnus-article-buffer)
5452     (let ((buffer-read-only nil)
5453           (inhibit-point-motion-hooks t))
5454       (save-restriction
5455         (when (and gnus-signature-face
5456                    (gnus-article-narrow-to-signature))
5457           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
5458                             'face gnus-signature-face)
5459           (widen)
5460           (gnus-article-search-signature)
5461           (let ((start (match-beginning 0))
5462                 (end (set-marker (make-marker) (1+ (match-end 0)))))
5463             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
5464                                      end)))))))
5465
5466 (defun gnus-button-in-region-p (b e prop)
5467   "Say whether PROP exists in the region."
5468   (text-property-not-all b e prop nil))
5469
5470 (defun gnus-article-add-buttons (&optional force)
5471   "Find external references in the article and make buttons of them.
5472 \"External references\" are things like Message-IDs and URLs, as
5473 specified by `gnus-button-alist'."
5474   (interactive (list 'force))
5475   (save-excursion
5476     (set-buffer gnus-article-buffer)
5477     (let ((buffer-read-only nil)
5478           (inhibit-point-motion-hooks t)
5479           (case-fold-search t)
5480           (alist gnus-button-alist)
5481           beg entry regexp)
5482       ;; Remove all old markers.
5483       (let (marker entry new-list)
5484         (while (setq marker (pop gnus-button-marker-list))
5485           (if (or (< marker (point-min)) (>= marker (point-max)))
5486               (push marker new-list)
5487             (goto-char marker)
5488             (when (setq entry (gnus-button-entry))
5489               (put-text-property (match-beginning (nth 1 entry))
5490                                  (match-end (nth 1 entry))
5491                                  'gnus-callback nil))
5492             (set-marker marker nil)))
5493         (setq gnus-button-marker-list new-list))
5494       ;; We skip the headers.
5495       (article-goto-body)
5496       (setq beg (point))
5497       (while (setq entry (pop alist))
5498         (setq regexp (eval (car entry)))
5499         (goto-char beg)
5500         (while (re-search-forward regexp nil t)
5501           (let* ((start (and entry (match-beginning (nth 1 entry))))
5502                  (end (and entry (match-end (nth 1 entry))))
5503                  (from (match-beginning 0)))
5504             (when (and (or (eq t (nth 2 entry))
5505                            (eval (nth 2 entry)))
5506                        (not (gnus-button-in-region-p
5507                              start end 'gnus-callback)))
5508               ;; That optional form returned non-nil, so we add the
5509               ;; button.
5510               (gnus-article-add-button
5511                start end 'gnus-button-push
5512                (car (push (set-marker (make-marker) from)
5513                           gnus-button-marker-list))))))))))
5514
5515 ;; Add buttons to the head of an article.
5516 (defun gnus-article-add-buttons-to-head ()
5517   "Add buttons to the head of the article."
5518   (interactive)
5519   (save-excursion
5520     (set-buffer gnus-article-buffer)
5521     (save-restriction
5522       (let ((buffer-read-only nil)
5523             (inhibit-point-motion-hooks t)
5524             (case-fold-search t)
5525             (alist gnus-header-button-alist)
5526             entry beg end)
5527         (article-narrow-to-head)
5528         (while alist
5529           ;; Each alist entry.
5530           (setq entry (car alist)
5531                 alist (cdr alist))
5532           (goto-char (point-min))
5533           (while (re-search-forward (car entry) nil t)
5534             ;; Each header matching the entry.
5535             (setq beg (match-beginning 0))
5536             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5537                                (match-beginning 0))
5538                           (point-max)))
5539             (goto-char beg)
5540             (while (re-search-forward (eval (nth 1 entry)) end t)
5541               ;; Each match within a header.
5542               (let* ((entry (cdr entry))
5543                      (start (match-beginning (nth 1 entry)))
5544                      (end (match-end (nth 1 entry)))
5545                      (form (nth 2 entry)))
5546                 (goto-char (match-end 0))
5547                 (when (eval form)
5548                   (gnus-article-add-button
5549                    start end (nth 3 entry)
5550                    (buffer-substring (match-beginning (nth 4 entry))
5551                                      (match-end (nth 4 entry)))))))
5552             (goto-char end)))))))
5553
5554 ;;; External functions:
5555
5556 (defun gnus-article-add-button (from to fun &optional data)
5557   "Create a button between FROM and TO with callback FUN and data DATA."
5558   (when gnus-article-button-face
5559     (gnus-overlay-put (gnus-make-overlay from to)
5560                       'face gnus-article-button-face))
5561   (gnus-add-text-properties
5562    from to
5563    (nconc (and gnus-article-mouse-face
5564                (list gnus-mouse-face-prop gnus-article-mouse-face))
5565           (list 'gnus-callback fun)
5566           (and data (list 'gnus-data data))))
5567   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5568                          :button-keymap gnus-widget-button-keymap))
5569
5570 ;;; Internal functions:
5571
5572 (defun gnus-article-set-globals ()
5573   (save-excursion
5574     (set-buffer gnus-summary-buffer)
5575     (gnus-set-global-variables)))
5576
5577 (defun gnus-signature-toggle (end)
5578   (save-excursion
5579     (set-buffer gnus-article-buffer)
5580     (let ((buffer-read-only nil)
5581           (inhibit-point-motion-hooks t))
5582       (if (text-property-any end (point-max) 'article-type 'signature)
5583           (progn
5584             (gnus-delete-wash-type 'signature)
5585             (gnus-remove-text-properties-when
5586              'article-type 'signature end (point-max)
5587              (cons 'article-type (cons 'signature
5588                                        gnus-hidden-properties))))
5589         (gnus-add-wash-type 'signature)
5590         (gnus-add-text-properties-when
5591          'article-type nil end (point-max)
5592          (cons 'article-type (cons 'signature
5593                                    gnus-hidden-properties)))))
5594     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
5595       (gnus-set-mode-line 'article))))
5596
5597 (defun gnus-button-entry ()
5598   ;; Return the first entry in `gnus-button-alist' matching this place.
5599   (let ((alist gnus-button-alist)
5600         (entry nil))
5601     (while alist
5602       (setq entry (pop alist))
5603       (if (looking-at (eval (car entry)))
5604           (setq alist nil)
5605         (setq entry nil)))
5606     entry))
5607
5608 (defun gnus-button-push (marker)
5609   ;; Push button starting at MARKER.
5610   (save-excursion
5611     (goto-char marker)
5612     (let* ((entry (gnus-button-entry))
5613            (inhibit-point-motion-hooks t)
5614            (fun (nth 3 entry))
5615            (args (mapcar (lambda (group)
5616                            (let ((string (match-string group)))
5617                              (gnus-set-text-properties
5618                               0 (length string) nil string)
5619                              string))
5620                          (nthcdr 4 entry))))
5621       (cond
5622        ((fboundp fun)
5623         (apply fun args))
5624        ((and (boundp fun)
5625              (fboundp (symbol-value fun)))
5626         (apply (symbol-value fun) args))
5627        (t
5628         (gnus-message 1 "You must define `%S' to use this button"
5629                       (cons fun args)))))))
5630
5631 (defun gnus-parse-news-url (url)
5632   (let (scheme server group message-id articles)
5633     (with-temp-buffer
5634       (insert url)
5635       (goto-char (point-min))
5636       (when (looking-at "\\([A-Za-z]+\\):")
5637         (setq scheme (match-string 1))
5638         (goto-char (match-end 0)))
5639       (when (looking-at "//\\([^/]+\\)/")
5640         (setq server (match-string 1))
5641         (goto-char (match-end 0)))
5642
5643       (cond
5644        ((looking-at "\\(.*@.*\\)")
5645         (setq message-id (match-string 1)))
5646        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
5647         (setq group (match-string 1)
5648               articles (split-string (match-string 2) "-")))
5649        ((looking-at "\\([^/]+\\)/?")
5650         (setq group (match-string 1)))
5651        (t
5652         (error "Unknown news URL syntax"))))
5653     (list scheme server group message-id articles)))
5654
5655 (defun gnus-button-handle-news (url)
5656   "Fetch a news URL."
5657   (destructuring-bind (scheme server group message-id articles)
5658       (gnus-parse-news-url url)
5659     (cond
5660      (message-id
5661       (save-excursion
5662         (set-buffer gnus-summary-buffer)
5663         (if server
5664             (let ((gnus-refer-article-method (list (list 'nntp server))))
5665               (gnus-summary-refer-article message-id))
5666           (gnus-summary-refer-article message-id))))
5667      (group
5668       (gnus-button-fetch-group url)))))
5669
5670 (defun gnus-button-handle-man (url)
5671   "Fetch a man page."
5672   (funcall gnus-button-man-handler url))
5673
5674 (defun gnus-button-handle-info (url)
5675   "Fetch an info URL."
5676   (if (string-match
5677        "^\\([^:/]+\\)?/\\(.*\\)"
5678        url)
5679       (gnus-info-find-node
5680        (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
5681                        "Gnus")
5682                ")"
5683                (gnus-url-unhex-string (match-string 2 url))))
5684     (error "Can't parse %s" url)))
5685
5686 (defun gnus-button-message-id (message-id)
5687   "Fetch MESSAGE-ID."
5688   (save-excursion
5689     (set-buffer gnus-summary-buffer)
5690     (gnus-summary-refer-article message-id)))
5691
5692 (defun gnus-button-fetch-group (address)
5693   "Fetch GROUP specified by ADDRESS."
5694   (if (not (string-match "[:/]" address))
5695       ;; This is just a simple group url.
5696       (gnus-group-read-ephemeral-group address gnus-select-method)
5697     (if (not
5698          (string-match
5699           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
5700           address))
5701         (error "Can't parse %s" address)
5702       (gnus-group-read-ephemeral-group
5703        (match-string 4 address)
5704        `(nntp ,(match-string 1 address)
5705               (nntp-address ,(match-string 1 address))
5706               (nntp-port-number ,(if (match-end 3)
5707                                      (match-string 3 address)
5708                                    "nntp")))
5709        nil nil nil
5710        (and (match-end 6) (list (string-to-int (match-string 6 address))))))))
5711
5712 (defun gnus-url-parse-query-string (query &optional downcase)
5713   (let (retval pairs cur key val)
5714     (setq pairs (split-string query "&"))
5715     (while pairs
5716       (setq cur (car pairs)
5717             pairs (cdr pairs))
5718       (if (not (string-match "=" cur))
5719           nil                           ; Grace
5720         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
5721               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
5722         (if downcase
5723             (setq key (downcase key)))
5724         (setq cur (assoc key retval))
5725         (if cur
5726             (setcdr cur (cons val (cdr cur)))
5727           (setq retval (cons (list key val) retval)))))
5728     retval))
5729
5730 (defun gnus-url-mailto (url)
5731   ;; Send mail to someone
5732   (when (string-match "mailto:/*\\(.*\\)" url)
5733     (setq url (substring url (match-beginning 1) nil)))
5734   (let (to args subject func)
5735     (if (string-match (regexp-quote "?") url)
5736         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
5737               args (gnus-url-parse-query-string
5738                     (substring url (match-end 0) nil) t))
5739       (setq to (gnus-url-unhex-string url)))
5740     (setq args (cons (list "to" to) args)
5741           subject (cdr-safe (assoc "subject" args)))
5742     (gnus-msg-mail)
5743     (while args
5744       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
5745       (if (fboundp func)
5746           (funcall func)
5747         (message-position-on-field (caar args)))
5748       (insert (mapconcat 'identity (cdar args) ", "))
5749       (setq args (cdr args)))
5750     (if subject
5751         (message-goto-body)
5752       (message-goto-subject))))
5753
5754 (defun gnus-button-embedded-url (address)
5755   "Activate ADDRESS with `browse-url'."
5756   (browse-url (gnus-strip-whitespace address)))
5757
5758 ;;; Next/prev buttons in the article buffer.
5759
5760 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
5761 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
5762
5763 (defvar gnus-prev-page-map
5764   (let ((map (make-sparse-keymap)))
5765     (unless (>= emacs-major-version 21)
5766       ;; XEmacs doesn't care.
5767       (set-keymap-parent map gnus-article-mode-map))
5768     (define-key map gnus-mouse-2 'gnus-button-prev-page)
5769     (define-key map "\r" 'gnus-button-prev-page)
5770     map))
5771
5772 (defun gnus-insert-prev-page-button ()
5773   (let ((b (point))
5774         (buffer-read-only nil))
5775     (gnus-eval-format
5776      gnus-prev-page-line-format nil
5777      `(,@(gnus-local-map-property gnus-prev-page-map)
5778          gnus-prev t
5779          gnus-callback gnus-article-button-prev-page
5780          article-type annotation))
5781     (widget-convert-button
5782      'link b (point)
5783      :action 'gnus-button-prev-page
5784      :button-keymap gnus-prev-page-map)))
5785
5786 (defvar gnus-prev-page-map
5787   (let ((map (make-sparse-keymap)))
5788     (unless (>= emacs-major-version 21)
5789       ;; XEmacs doesn't care.
5790       (set-keymap-parent map gnus-article-mode-map))
5791     (define-key map gnus-mouse-2 'gnus-button-prev-page)
5792     (define-key map "\r" 'gnus-button-prev-page)
5793     map))
5794
5795 (defvar gnus-next-page-map
5796   (let ((map (make-sparse-keymap)))
5797     (unless (>= emacs-major-version 21)
5798       ;; XEmacs doesn't care.
5799       (set-keymap-parent map gnus-article-mode-map))
5800     (define-key map gnus-mouse-2 'gnus-button-next-page)
5801     (define-key map "\r" 'gnus-button-next-page)
5802     map))
5803
5804 (defun gnus-button-next-page (&optional args more-args)
5805   "Go to the next page."
5806   (interactive)
5807   (let ((win (selected-window)))
5808     (select-window (gnus-get-buffer-window gnus-article-buffer t))
5809     (gnus-article-next-page)
5810     (select-window win)))
5811
5812 (defun gnus-button-prev-page (&optional args more-args)
5813   "Go to the prev page."
5814   (interactive)
5815   (let ((win (selected-window)))
5816     (select-window (gnus-get-buffer-window gnus-article-buffer t))
5817     (gnus-article-prev-page)
5818     (select-window win)))
5819
5820 (defun gnus-insert-next-page-button ()
5821   (let ((b (point))
5822         (buffer-read-only nil))
5823     (gnus-eval-format gnus-next-page-line-format nil
5824                       `(,@(gnus-local-map-property gnus-next-page-map)
5825                           gnus-next t
5826                           gnus-callback gnus-article-button-next-page
5827                           article-type annotation))
5828     (widget-convert-button
5829      'link b (point)
5830      :action 'gnus-button-next-page
5831      :button-keymap gnus-next-page-map)))
5832
5833 (defun gnus-article-button-next-page (arg)
5834   "Go to the next page."
5835   (interactive "P")
5836   (let ((win (selected-window)))
5837     (select-window (gnus-get-buffer-window gnus-article-buffer t))
5838     (gnus-article-next-page)
5839     (select-window win)))
5840
5841 (defun gnus-article-button-prev-page (arg)
5842   "Go to the prev page."
5843   (interactive "P")
5844   (let ((win (selected-window)))
5845     (select-window (gnus-get-buffer-window gnus-article-buffer t))
5846     (gnus-article-prev-page)
5847     (select-window win)))
5848
5849 (defvar gnus-decode-header-methods
5850   '(mail-decode-encoded-word-region)
5851   "List of methods used to decode headers.
5852
5853 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5854 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5855 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5856 whose names match REGEXP.
5857
5858 For example:
5859 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5860  mail-decode-encoded-word-region
5861  (\"chinese\" . rfc1843-decode-region))
5862 ")
5863
5864 (defvar gnus-decode-header-methods-cache nil)
5865
5866 (defun gnus-multi-decode-header (start end)
5867   "Apply the functions from `gnus-encoded-word-methods' that match."
5868   (unless (and gnus-decode-header-methods-cache
5869                (eq gnus-newsgroup-name
5870                    (car gnus-decode-header-methods-cache)))
5871     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5872     (mapcar (lambda (x)
5873               (if (symbolp x)
5874                   (nconc gnus-decode-header-methods-cache (list x))
5875                 (if (and gnus-newsgroup-name
5876                          (string-match (car x) gnus-newsgroup-name))
5877                     (nconc gnus-decode-header-methods-cache
5878                            (list (cdr x))))))
5879           gnus-decode-header-methods))
5880   (let ((xlist gnus-decode-header-methods-cache))
5881     (pop xlist)
5882     (save-restriction
5883       (narrow-to-region start end)
5884       (while xlist
5885         (funcall (pop xlist) (point-min) (point-max))))))
5886
5887 ;;;
5888 ;;; Treatment top-level handling.
5889 ;;;
5890
5891 (defun gnus-treat-article (condition &optional part-number total-parts type)
5892   (let ((length (- (point-max) (point-min)))
5893         (alist gnus-treatment-function-alist)
5894         (article-goto-body-goes-to-point-min-p t)
5895         (treated-type
5896          (or (not type)
5897              (catch 'found
5898                (let ((list gnus-article-treat-types))
5899                  (while list
5900                    (when (string-match (pop list) type)
5901                      (throw 'found t)))))))
5902         (highlightp (gnus-visual-p 'article-highlight 'highlight))
5903         val elem)
5904     (gnus-run-hooks 'gnus-part-display-hook)
5905     (while (setq elem (pop alist))
5906       (setq val
5907             (save-excursion
5908               (if (gnus-buffer-live-p gnus-summary-buffer)
5909                   (set-buffer gnus-summary-buffer))
5910               (symbol-value (car elem))))
5911       (when (and (or (consp val)
5912                      treated-type)
5913                  (gnus-treat-predicate val)
5914                  (or (not (get (car elem) 'highlight))
5915                      highlightp))
5916         (save-restriction
5917           (funcall (cadr elem)))))))
5918
5919 ;; Dynamic variables.
5920 (eval-when-compile
5921   (defvar part-number)
5922   (defvar total-parts)
5923   (defvar type)
5924   (defvar condition)
5925   (defvar length))
5926
5927 (defun gnus-treat-predicate (val)
5928   (cond
5929    ((null val)
5930     nil)
5931    ((and (listp val)
5932          (stringp (car val)))
5933     (apply 'gnus-or (mapcar `(lambda (s)
5934                                (string-match s ,(or gnus-newsgroup-name "")))
5935                             val)))
5936    ((listp val)
5937     (let ((pred (pop val)))
5938       (cond
5939        ((eq pred 'or)
5940         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
5941        ((eq pred 'and)
5942         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
5943        ((eq pred 'not)
5944         (not (gnus-treat-predicate (car val))))
5945        ((eq pred 'typep)
5946         (equal (car val) type))
5947        (t
5948         (error "%S is not a valid predicate" pred)))))
5949    (condition
5950     (eq condition val))
5951    ((eq val t)
5952     t)
5953    ((eq val 'head)
5954     nil)
5955    ((eq val 'last)
5956     (eq part-number total-parts))
5957    ((numberp val)
5958     (< length val))
5959    (t
5960     (error "%S is not a valid value" val))))
5961
5962 (defun gnus-article-encrypt-body (protocol &optional n)
5963   "Encrypt the article body."
5964   (interactive
5965    (list
5966     (or gnus-article-encrypt-protocol
5967         (completing-read "Encrypt protocol: "
5968                          gnus-article-encrypt-protocol-alist
5969                          nil t))
5970     current-prefix-arg))
5971   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
5972     (unless func
5973       (error (format "Can't find the encrypt protocol %s" protocol)))
5974     (if (equal gnus-newsgroup-name "nndraft:drafts")
5975         (error "Can't encrypt the article in group nndraft:drafts"))
5976     (if (equal gnus-newsgroup-name "nndraft:queue")
5977         (error "Don't encrypt the article in group nndraft:queue"))
5978     (gnus-summary-iterate n
5979       (save-excursion
5980         (set-buffer gnus-summary-buffer)
5981         (let ((mail-parse-charset gnus-newsgroup-charset)
5982               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5983               (summary-buffer gnus-summary-buffer)
5984               references point)
5985           (gnus-set-global-variables)
5986           (when (gnus-group-read-only-p)
5987             (error "The current newsgroup does not support article encrypt"))
5988           (gnus-summary-show-article t)
5989           (setq references
5990               (or (mail-header-references gnus-current-headers) ""))
5991           (set-buffer gnus-article-buffer)
5992           (let* ((buffer-read-only nil)
5993                  (headers
5994                   (mapcar (lambda (field)
5995                             (and (save-restriction
5996                                    (message-narrow-to-head)
5997                                    (goto-char (point-min))
5998                                    (search-forward field nil t))
5999                                  (prog2
6000                                      (message-narrow-to-field)
6001                                      (buffer-substring (point-min) (point-max))
6002                                    (delete-region (point-min) (point-max))
6003                                    (widen))))
6004                           '("Content-Type:" "Content-Transfer-Encoding:"
6005                             "Content-Disposition:"))))
6006             (message-narrow-to-head)
6007             (message-remove-header "MIME-Version")
6008             (goto-char (point-max))
6009             (setq point (point))
6010             (insert (apply 'concat headers))
6011             (widen)
6012             (narrow-to-region point (point-max))
6013             (let ((message-options message-options))
6014               (message-options-set 'message-sender user-mail-address)
6015               (message-options-set 'message-recipients user-mail-address)
6016               (message-options-set 'message-sign-encrypt 'not)
6017               (funcall func))
6018             (goto-char (point-min))
6019             (insert "MIME-Version: 1.0\n")
6020             (widen)
6021             (gnus-summary-edit-article-done
6022              references nil summary-buffer t))
6023           (when gnus-keep-backlog
6024             (gnus-backlog-remove-article
6025              (car gnus-article-current) (cdr gnus-article-current)))
6026           (save-excursion
6027             (when (get-buffer gnus-original-article-buffer)
6028               (set-buffer gnus-original-article-buffer)
6029               (setq gnus-original-article nil)))
6030           (when gnus-use-cache
6031             (gnus-cache-update-article
6032              (car gnus-article-current) (cdr gnus-article-current))))))))
6033
6034 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
6035   "The following specs can be used:
6036 %t  The security MIME type
6037 %i  Additional info
6038 %d  Details
6039 %D  Details if button is pressed")
6040
6041 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
6042   "The following specs can be used:
6043 %t  The security MIME type
6044 %i  Additional info
6045 %d  Details
6046 %D  Details if button is pressed")
6047
6048 (defvar gnus-mime-security-button-line-format-alist
6049   '((?t gnus-tmp-type ?s)
6050     (?i gnus-tmp-info ?s)
6051     (?d gnus-tmp-details ?s)
6052     (?D gnus-tmp-pressed-details ?s)))
6053
6054 (defvar gnus-mime-security-button-map
6055   (let ((map (make-sparse-keymap)))
6056     (unless (>= (string-to-number emacs-version) 21)
6057       (set-keymap-parent map gnus-article-mode-map))
6058     (define-key map gnus-mouse-2 'gnus-article-push-button)
6059     (define-key map "\r" 'gnus-article-press-button)
6060     map))
6061
6062 (defvar gnus-mime-security-details-buffer nil)
6063
6064 (defvar gnus-mime-security-button-pressed nil)
6065
6066 (defvar gnus-mime-security-show-details-inline t
6067   "If non-nil, show details in the article buffer.")
6068
6069 (defun gnus-mime-security-verify-or-decrypt (handle)
6070   (mm-remove-parts (cdr handle))
6071   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
6072         point buffer-read-only)
6073     (if region
6074         (goto-char (car region)))
6075     (save-restriction
6076       (narrow-to-region (point) (point))
6077       (with-current-buffer (mm-handle-multipart-original-buffer handle)
6078         (let* ((mm-verify-option 'known)
6079                (mm-decrypt-option 'known)
6080                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
6081           (unless (eq nparts (cdr handle))
6082             (mm-destroy-parts (cdr handle))
6083             (setcdr handle nparts))))
6084       (setq point (point))
6085       (gnus-mime-display-security handle)
6086       (goto-char (point-max)))
6087     (when region
6088       (delete-region (point) (cdr region))
6089       (set-marker (car region) nil)
6090       (set-marker (cdr region) nil))
6091     (goto-char point)))
6092
6093 (defun gnus-mime-security-show-details (handle)
6094   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
6095     (if details
6096         (if gnus-mime-security-show-details-inline
6097             (let ((gnus-mime-security-button-pressed t)
6098                   (gnus-mime-security-button-line-format
6099                    (get-text-property (point) 'gnus-line-format))
6100                 buffer-read-only)
6101               (forward-char -1)
6102               (while (eq (get-text-property (point) 'gnus-line-format)
6103                          gnus-mime-security-button-line-format)
6104                 (forward-char -1))
6105               (forward-char)
6106               (save-restriction
6107                 (narrow-to-region (point) (point))
6108                 (gnus-insert-mime-security-button handle))
6109               (delete-region (point)
6110                              (or (text-property-not-all
6111                                   (point) (point-max)
6112                                   'gnus-line-format
6113                                   gnus-mime-security-button-line-format)
6114                                  (point-max))))
6115           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
6116               (with-current-buffer gnus-mime-security-details-buffer
6117                 (erase-buffer)
6118                 t)
6119             (setq gnus-mime-security-details-buffer
6120                   (gnus-get-buffer-create "*MIME Security Details*")))
6121           (with-current-buffer gnus-mime-security-details-buffer
6122             (insert details)
6123             (goto-char (point-min)))
6124           (pop-to-buffer gnus-mime-security-details-buffer))
6125       (gnus-message 5 "No details."))))
6126
6127 (defun gnus-mime-security-press-button (handle)
6128   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6129       (gnus-mime-security-show-details handle)
6130     (gnus-mime-security-verify-or-decrypt handle)))
6131
6132 (defun gnus-insert-mime-security-button (handle &optional displayed)
6133   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
6134          (gnus-tmp-type
6135           (concat
6136            (or (nth 2 (assoc protocol mm-verify-function-alist))
6137                (nth 2 (assoc protocol mm-decrypt-function-alist))
6138                "Unknown")
6139            (if (equal (car handle) "multipart/signed")
6140                " Signed" " Encrypted")
6141            " Part"))
6142          (gnus-tmp-info
6143           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6144               "Undecided"))
6145          (gnus-tmp-details
6146           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
6147          gnus-tmp-pressed-details
6148          b e)
6149     (setq gnus-tmp-details
6150           (if gnus-tmp-details
6151               (concat "\n" gnus-tmp-details) ""))
6152     (setq gnus-tmp-pressed-details
6153           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
6154     (unless (bolp)
6155       (insert "\n"))
6156     (setq b (point))
6157     (gnus-eval-format
6158      gnus-mime-security-button-line-format
6159      gnus-mime-security-button-line-format-alist
6160      `(,@(gnus-local-map-property gnus-mime-security-button-map)
6161          gnus-callback gnus-mime-security-press-button
6162          gnus-line-format ,gnus-mime-security-button-line-format
6163          article-type annotation
6164          gnus-data ,handle))
6165     (setq e (point))
6166     (widget-convert-button
6167      'link b e
6168      :mime-handle handle
6169      :action 'gnus-widget-press-button
6170      :button-keymap gnus-mime-security-button-map
6171      :help-echo
6172      (lambda (widget/window &optional overlay pos)
6173        ;; Needed to properly clear the message due to a bug in
6174        ;; wid-edit (XEmacs only).
6175        (if (boundp 'help-echo-owns-message)
6176            (setq help-echo-owns-message t))
6177        (format
6178         "%S: show detail"
6179         (aref gnus-mouse-2 0))))))
6180
6181 (defun gnus-mime-display-security (handle)
6182   (save-restriction
6183     (narrow-to-region (point) (point))
6184     (unless (gnus-unbuttonized-mime-type-p (car handle))
6185       (gnus-insert-mime-security-button handle))
6186     (gnus-mime-display-mixed (cdr handle))
6187     (unless (bolp)
6188       (insert "\n"))
6189     (unless (gnus-unbuttonized-mime-type-p (car handle))
6190       (let ((gnus-mime-security-button-line-format
6191              gnus-mime-security-button-end-line-format))
6192         (gnus-insert-mime-security-button handle)))
6193     (mm-set-handle-multipart-parameter
6194      handle 'gnus-region
6195      (cons (set-marker (make-marker) (point-min))
6196            (set-marker (make-marker) (point-max))))))
6197
6198 (gnus-ems-redefine)
6199
6200 (provide 'gnus-art)
6201
6202 (run-hooks 'gnus-art-load-hook)
6203
6204 ;;; gnus-art.el ends here