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