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