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