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