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