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