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