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