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