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