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