T-gnus 6.15.10 revision 00.
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'static))
33
34 (require 'path-util)
35 (require 'gnus)
36 (require 'gnus-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'gnus-win)
40 (require 'alist)
41 (require 'mime-view)
42 (require 'wid-edit)
43
44 ;; Avoid byte-compile warnings.
45 (eval-when-compile
46   (require 'mm-bodies)
47   (require 'mail-parse)
48   (require 'mm-decode)
49   (require 'mm-view)
50   (require 'mm-uu))
51
52 (autoload 'gnus-msg-mail "gnus-msg" nil t)
53 (autoload 'gnus-button-mailto "gnus-msg")
54 (autoload 'gnus-button-reply "gnus-msg" nil t)
55
56 (defgroup gnus-article nil
57   "Article display."
58   :link '(custom-manual "(gnus)The Article Buffer")
59   :group 'gnus)
60
61 (defgroup gnus-article-treat nil
62   "Treating article parts."
63   :link '(custom-manual "(gnus)Article Hiding")
64   :group 'gnus-article)
65
66 (defgroup gnus-article-hiding nil
67   "Hiding article parts."
68   :link '(custom-manual "(gnus)Article Hiding")
69   :group 'gnus-article)
70
71 (defgroup gnus-article-highlight nil
72   "Article highlighting."
73   :link '(custom-manual "(gnus)Article Highlighting")
74   :group 'gnus-article
75   :group 'gnus-visual)
76
77 (defgroup gnus-article-signature nil
78   "Article signatures."
79   :link '(custom-manual "(gnus)Article Signature")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-headers nil
83   "Article headers."
84   :link '(custom-manual "(gnus)Hiding Headers")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-washing nil
88   "Special commands on articles."
89   :link '(custom-manual "(gnus)Article Washing")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-emphasis nil
93   "Fontisizing articles."
94   :link '(custom-manual "(gnus)Article Fontisizing")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-saving nil
98   "Saving articles."
99   :link '(custom-manual "(gnus)Saving Articles")
100   :group 'gnus-article)
101
102 (defgroup gnus-article-mime nil
103   "Worshiping the MIME wonder."
104   :link '(custom-manual "(gnus)Using MIME")
105   :group 'gnus-article)
106
107 (defgroup gnus-article-buttons nil
108   "Pushable buttons in the article buffer."
109   :link '(custom-manual "(gnus)Article Buttons")
110   :group 'gnus-article)
111
112 (defgroup gnus-article-various nil
113   "Other article options."
114   :link '(custom-manual "(gnus)Misc Article")
115   :group 'gnus-article)
116
117 (defcustom gnus-ignored-headers
118   '("^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 (eval-when-compile
4283   (require 'jka-compr))
4284
4285 ;; jka-compr.el uses a "sh -c" to direct stderr to err-file, but these days
4286 ;; emacs can do that itself.
4287 ;;
4288 (defun gnus-mime-jka-compr-maybe-uncompress ()
4289   "Uncompress the current buffer if `auto-compression-mode' is enabled.
4290 The uncompress method used is derived from `buffer-file-name'."
4291   (when (and (fboundp 'jka-compr-installed-p)
4292              (jka-compr-installed-p))
4293     (let ((info (jka-compr-get-compression-info buffer-file-name)))
4294       (when info
4295         (let ((basename (file-name-nondirectory buffer-file-name))
4296               (args     (jka-compr-info-uncompress-args    info))
4297               (prog     (jka-compr-info-uncompress-program info))
4298               (message  (jka-compr-info-uncompress-message info))
4299               (err-file (jka-compr-make-temp-name)))
4300           (if message
4301               (message "%s %s..." message basename))
4302           (unwind-protect
4303               (unless (memq (apply 'call-process-region
4304                                    (point-min) (point-max) 
4305                                    prog
4306                                    t (list t err-file) nil
4307                                    args)
4308                             jka-compr-acceptable-retval-list)
4309                 (jka-compr-error prog args basename message err-file))
4310             (jka-compr-delete-temp-file err-file)))))))
4311
4312 (defun gnus-mime-copy-part (&optional handle)
4313   "Put the MIME part under point into a new buffer."
4314   (interactive)
4315   (gnus-article-check-buffer)
4316   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4317          (contents (and handle (mm-get-part handle)))
4318          (base (and handle
4319                     (file-name-nondirectory
4320                      (or
4321                       (mail-content-type-get (mm-handle-type handle) 'name)
4322                       (mail-content-type-get (mm-handle-disposition handle)
4323                                              'filename)
4324                       "*decoded*"))))
4325          (buffer (and base (generate-new-buffer base))))
4326     (when contents
4327       (switch-to-buffer buffer)
4328       (insert contents)
4329       ;; We do it this way to make `normal-mode' set the appropriate mode.
4330       (unwind-protect
4331           (progn
4332             (setq buffer-file-name (expand-file-name base))
4333             (gnus-mime-jka-compr-maybe-uncompress)
4334             (normal-mode))
4335         (setq buffer-file-name nil))
4336       (goto-char (point-min)))))
4337
4338 (defun gnus-mime-print-part (&optional handle filename)
4339   "Print the MIME part under point."
4340   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4341   (gnus-article-check-buffer)
4342   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4343          (contents (and handle (mm-get-part handle)))
4344          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4345          (printer (mailcap-mime-info (mm-handle-type handle) "print")))
4346     (when contents
4347         (if printer
4348             (unwind-protect
4349                 (progn
4350                   (with-temp-file file
4351                     (insert contents))
4352                   (call-process shell-file-name nil
4353                                 (generate-new-buffer " *mm*")
4354                                 nil
4355                                 shell-command-switch
4356                                 (mm-mailcap-command
4357                                  printer file (mm-handle-type handle))))
4358               (delete-file file))
4359           (with-temp-buffer
4360             (insert contents)
4361             (gnus-print-buffer))
4362           (ps-despool filename)))))
4363
4364 (defun gnus-mime-inline-part (&optional handle arg)
4365   "Insert the MIME part under point into the current buffer."
4366   (interactive (list nil current-prefix-arg))
4367   (gnus-article-check-buffer)
4368   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4369          contents charset
4370          (b (point))
4371          buffer-read-only)
4372     (when handle
4373       (if (and (not arg) (mm-handle-undisplayer handle))
4374           (mm-remove-part handle)
4375         (setq contents (mm-get-part handle))
4376         (cond
4377          ((not arg)
4378           (setq charset (or (mail-content-type-get
4379                              (mm-handle-type handle) 'charset)
4380                             gnus-newsgroup-charset)))
4381          ((numberp arg)
4382           (if (mm-handle-undisplayer handle)
4383               (mm-remove-part handle))
4384           (setq charset
4385                 (or (cdr (assq arg
4386                                gnus-summary-show-article-charset-alist))
4387                     (mm-read-coding-system "Charset: ")))))
4388         (forward-line 2)
4389         (mm-insert-inline handle
4390                           (if (and charset
4391                                    (setq charset (mm-charset-to-coding-system
4392                                                   charset))
4393                                    (not (eq charset 'ascii)))
4394                               (mm-decode-coding-string contents charset)
4395                             contents))
4396         (goto-char b)))))
4397
4398 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4399   "Insert the MIME part under point into the current buffer using the
4400 specified charset."
4401   (interactive (list nil current-prefix-arg))
4402   (gnus-article-check-buffer)
4403   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4404          contents charset
4405          (b (point))
4406          buffer-read-only)
4407     (when handle
4408       (if (mm-handle-undisplayer handle)
4409           (mm-remove-part handle))
4410       (let ((gnus-newsgroup-charset
4411              (or (cdr (assq arg
4412                             gnus-summary-show-article-charset-alist))
4413                  (mm-read-coding-system "Charset: ")))
4414             (gnus-newsgroup-ignored-charsets 'gnus-all))
4415         (gnus-article-press-button)))))
4416
4417 (defun gnus-mime-view-part-externally (&optional handle)
4418   "View the MIME part under point with an external viewer."
4419   (interactive)
4420   (gnus-article-check-buffer)
4421   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4422          (mm-user-display-methods nil)
4423          (mm-inlined-types nil)
4424          (mail-parse-charset gnus-newsgroup-charset)
4425          (mail-parse-ignored-charsets
4426           (save-excursion (set-buffer gnus-summary-buffer)
4427                           gnus-newsgroup-ignored-charsets)))
4428     (when handle
4429       (if (mm-handle-undisplayer handle)
4430           (mm-remove-part handle)
4431         (mm-display-part handle)))))
4432
4433 (defun gnus-mime-view-part-internally (&optional handle)
4434   "View the MIME part under point with an internal viewer.
4435 If no internal viewer is available, use an external viewer."
4436   (interactive)
4437   (gnus-article-check-buffer)
4438   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4439          (mm-inlined-types '(".*"))
4440          (mm-inline-large-images t)
4441          (mail-parse-charset gnus-newsgroup-charset)
4442          (mail-parse-ignored-charsets
4443           (save-excursion (set-buffer gnus-summary-buffer)
4444                           gnus-newsgroup-ignored-charsets)))
4445     (when handle
4446       (if (mm-handle-undisplayer handle)
4447           (mm-remove-part handle)
4448         (mm-display-part handle)))))
4449
4450 (defun gnus-mime-action-on-part (&optional action)
4451   "Do something with the MIME attachment at \(point\)."
4452   (interactive
4453    (list (completing-read "Action: " gnus-mime-action-alist)))
4454   (gnus-article-check-buffer)
4455   (let ((action-pair (assoc action gnus-mime-action-alist)))
4456     (if action-pair
4457         (funcall (cdr action-pair)))))
4458
4459 (defun gnus-article-part-wrapper (n function)
4460   (save-current-buffer
4461     (set-buffer gnus-article-buffer)
4462     (when (> n (length gnus-article-mime-handle-alist))
4463       (error "No such part"))
4464     (gnus-article-goto-part n)
4465     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4466       (funcall function handle))))
4467
4468 (defun gnus-article-pipe-part (n)
4469   "Pipe MIME part N, which is the numerical prefix."
4470   (interactive "p")
4471   (gnus-article-part-wrapper n 'mm-pipe-part))
4472
4473 (defun gnus-article-save-part (n)
4474   "Save MIME part N, which is the numerical prefix."
4475   (interactive "p")
4476   (gnus-article-part-wrapper n 'mm-save-part))
4477
4478 (defun gnus-article-interactively-view-part (n)
4479   "View MIME part N interactively, which is the numerical prefix."
4480   (interactive "p")
4481   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4482
4483 (defun gnus-article-copy-part (n)
4484   "Copy MIME part N, which is the numerical prefix."
4485   (interactive "p")
4486   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4487
4488 (defun gnus-article-view-part-as-charset (n)
4489   "Copy MIME part N, which is the numerical prefix."
4490   (interactive "p")
4491   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4492
4493 (defun gnus-article-view-part-externally (n)
4494   "View MIME part N externally, which is the numerical prefix."
4495   (interactive "p")
4496   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4497
4498 (defun gnus-article-inline-part (n)
4499   "Inline MIME part N, which is the numerical prefix."
4500   (interactive "p")
4501   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4502
4503 (defun gnus-article-mime-match-handle-first (condition)
4504   (if condition
4505       (let ((alist gnus-article-mime-handle-alist) ihandle n)
4506         (while (setq ihandle (pop alist))
4507           (if (and (cond
4508                     ((functionp condition)
4509                      (funcall condition (cdr ihandle)))
4510                     ((eq condition 'undisplayed)
4511                      (not (or (mm-handle-undisplayer (cdr ihandle))
4512                               (equal (mm-handle-media-type (cdr ihandle))
4513                                      "multipart/alternative"))))
4514                     ((eq condition 'undisplayed-alternative)
4515                      (not (mm-handle-undisplayer (cdr ihandle))))
4516                     (t t))
4517                    (gnus-article-goto-part (car ihandle))
4518                    (or (not n) (< (car ihandle) n)))
4519               (setq n (car ihandle))))
4520         (or n 1))
4521     1))
4522
4523 (defun gnus-article-view-part (&optional n)
4524   "View MIME part N, which is the numerical prefix."
4525   (interactive "P")
4526   (save-current-buffer
4527     (set-buffer gnus-article-buffer)
4528     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4529                              gnus-article-mime-match-handle-function)))
4530     (when (> n (length gnus-article-mime-handle-alist))
4531       (error "No such part"))
4532     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4533       (when (gnus-article-goto-part n)
4534         (if (equal (car handle) "multipart/alternative")
4535             (gnus-article-press-button)
4536           (when (eq (gnus-mm-display-part handle) 'internal)
4537             (gnus-set-window-start)))))))
4538
4539 (defsubst gnus-article-mime-total-parts ()
4540   (if (bufferp (car gnus-article-mime-handles))
4541       1 ;; single part
4542     (1- (length gnus-article-mime-handles))))
4543
4544 (defun gnus-mm-display-part (handle)
4545   "Display HANDLE and fix MIME button."
4546   (let ((id (get-text-property (point) 'gnus-part))
4547         (point (point))
4548         buffer-read-only)
4549     (forward-line 1)
4550     (prog1
4551         (let ((window (selected-window))
4552               (mail-parse-charset gnus-newsgroup-charset)
4553               (mail-parse-ignored-charsets
4554                (if (gnus-buffer-live-p gnus-summary-buffer)
4555                    (save-excursion
4556                      (set-buffer gnus-summary-buffer)
4557                      gnus-newsgroup-ignored-charsets)
4558                  nil)))
4559           (save-excursion
4560             (unwind-protect
4561                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4562                       (beg (point)))
4563                   (when win
4564                     (select-window win))
4565                   (goto-char point)
4566                   (forward-line)
4567                   (if (mm-handle-displayed-p handle)
4568                       ;; This will remove the part.
4569                       (mm-display-part handle)
4570                     (save-restriction
4571                       (narrow-to-region (point)
4572                                         (if (eobp) (point) (1+ (point))))
4573                       (mm-display-part handle)
4574                       ;; We narrow to the part itself and
4575                       ;; then call the treatment functions.
4576                       (goto-char (point-min))
4577                       (forward-line 1)
4578                       (narrow-to-region (point) (point-max))
4579                       (gnus-treat-article
4580                        nil id
4581                        (gnus-article-mime-total-parts)
4582                        (mm-handle-media-type handle)))))
4583               (if (window-live-p window)
4584                   (select-window window)))))
4585       (goto-char point)
4586       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
4587       (gnus-insert-mime-button
4588        handle id (list (mm-handle-displayed-p handle)))
4589       (goto-char point))))
4590
4591 (defun gnus-article-goto-part (n)
4592   "Go to MIME part N."
4593   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
4594     (when point
4595       (goto-char point))))
4596
4597 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4598   (let ((gnus-tmp-name
4599          (or (mail-content-type-get (mm-handle-type handle) 'name)
4600              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4601              (mail-content-type-get (mm-handle-type handle) 'url)
4602              ""))
4603         (gnus-tmp-type (mm-handle-media-type handle))
4604         (gnus-tmp-description
4605          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4606                                               "")))
4607         (gnus-tmp-dots
4608          (if (if displayed (car displayed)
4609                (mm-handle-displayed-p handle))
4610              "" "..."))
4611         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4612                            (buffer-size)))
4613         gnus-tmp-type-long b e)
4614     (when (string-match ".*/" gnus-tmp-name)
4615       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4616     (setq gnus-tmp-type-long (concat gnus-tmp-type
4617                                      (and (not (equal gnus-tmp-name ""))
4618                                           (concat "; " gnus-tmp-name))))
4619     (unless (equal gnus-tmp-description "")
4620       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4621     (unless (bolp)
4622       (insert "\n"))
4623     (setq b (point))
4624     (gnus-eval-format
4625      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4626      `(,@(gnus-local-map-property gnus-mime-button-map)
4627          gnus-callback gnus-mm-display-part
4628          gnus-part ,gnus-tmp-id
4629          article-type annotation
4630          gnus-data ,handle))
4631     (setq e (point))
4632     (widget-convert-button
4633      'link b e
4634      :mime-handle handle
4635      :action 'gnus-widget-press-button
4636      :button-keymap gnus-mime-button-map
4637      :help-echo
4638      (lambda (widget/window &optional overlay pos)
4639        ;; Needed to properly clear the message due to a bug in
4640        ;; wid-edit (XEmacs only).
4641        (if (boundp 'help-echo-owns-message)
4642            (setq help-echo-owns-message t))
4643        (format
4644         "%S: %s the MIME part; %S: more options"
4645         (aref gnus-mouse-2 0)
4646         ;; XEmacs will get a single widget arg; Emacs 21 will get
4647         ;; window, overlay, position.
4648         (if (mm-handle-displayed-p
4649              (if overlay
4650                  (with-current-buffer (gnus-overlay-buffer overlay)
4651                    (widget-get (widget-at (gnus-overlay-start overlay))
4652                                :mime-handle))
4653                (widget-get widget/window :mime-handle)))
4654             "hide" "show")
4655         (aref gnus-down-mouse-3 0))))))
4656
4657 (defun gnus-widget-press-button (elems el)
4658   (goto-char (widget-get elems :from))
4659   (gnus-article-press-button))
4660
4661 (defvar gnus-displaying-mime nil)
4662
4663 (defun gnus-display-mime (&optional ihandles)
4664   "Display the MIME parts."
4665   (save-excursion
4666     (save-selected-window
4667       (let ((window (get-buffer-window gnus-article-buffer))
4668             (point (point)))
4669         (when window
4670           (select-window window)
4671           ;; We have to do this since selecting the window
4672           ;; may change the point.  So we set the window point.
4673           (set-window-point window point)))
4674       (let* ((handles (or ihandles (mm-dissect-buffer
4675                                     nil gnus-article-loose-mime)
4676                           (mm-uu-dissect)))
4677              buffer-read-only handle name type b e display)
4678         (when (and (not ihandles)
4679                    (not gnus-displaying-mime))
4680           ;; Top-level call; we clean up.
4681           (when gnus-article-mime-handles
4682             (mm-destroy-parts gnus-article-mime-handles)
4683             (setq gnus-article-mime-handle-alist nil));; A trick.
4684           (setq gnus-article-mime-handles handles)
4685           ;; We allow users to glean info from the handles.
4686           (when gnus-article-mime-part-function
4687             (gnus-mime-part-function handles)))
4688         (if (and handles
4689                  (or (not (stringp (car handles)))
4690                      (cdr handles)))
4691             (progn
4692               (when (and (not ihandles)
4693                          (not gnus-displaying-mime))
4694                 ;; Clean up for mime parts.
4695                 (article-goto-body)
4696                 (delete-region (point) (point-max)))
4697               (let ((gnus-displaying-mime t))
4698                 (gnus-mime-display-part handles)))
4699           (save-restriction
4700             (article-goto-body)
4701             (narrow-to-region (point) (point-max))
4702             (gnus-treat-article nil 1 1)
4703             (widen)))
4704         (unless ihandles
4705           ;; Highlight the headers.
4706           (save-excursion
4707             (save-restriction
4708               (article-goto-body)
4709               (narrow-to-region (point-min) (point))
4710               (gnus-treat-article 'head))))))))
4711
4712 (defvar gnus-mime-display-multipart-as-mixed nil)
4713 (defvar gnus-mime-display-multipart-alternative-as-mixed nil)
4714 (defvar gnus-mime-display-multipart-related-as-mixed nil)
4715
4716 (defun gnus-mime-display-part (handle)
4717   (cond
4718    ;; Single part.
4719    ((not (stringp (car handle)))
4720     (gnus-mime-display-single handle))
4721    ;; User-defined multipart
4722    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4723     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4724              handle))
4725    ;; multipart/alternative
4726    ((and (equal (car handle) "multipart/alternative")
4727          (not (or gnus-mime-display-multipart-as-mixed
4728                   gnus-mime-display-multipart-alternative-as-mixed)))
4729     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4730       (push (cons id handle) gnus-article-mime-handle-alist)
4731       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4732    ;; multipart/related
4733    ((and (equal (car handle) "multipart/related")
4734          (not (or gnus-mime-display-multipart-as-mixed
4735                   gnus-mime-display-multipart-related-as-mixed)))
4736     ;;;!!!We should find the start part, but we just default
4737     ;;;!!!to the first part.
4738     ;;(gnus-mime-display-part (cadr handle))
4739     ;;;!!! Most multipart/related is an HTML message plus images.
4740     ;;;!!! Unfortunately we are unable to let W3 display those
4741     ;;;!!! included images, so we just display it as a mixed multipart.
4742     ;;(gnus-mime-display-mixed (cdr handle))
4743     ;;;!!! No, w3 can display everything just fine.
4744     (gnus-mime-display-part (cadr handle)))
4745    ((equal (car handle) "multipart/signed")
4746     (gnus-add-wash-type 'signed)
4747     (gnus-mime-display-security handle))
4748    ((equal (car handle) "multipart/encrypted")
4749     (gnus-add-wash-type 'encrypted)
4750     (gnus-mime-display-security handle))
4751    ;; Other multiparts are handled like multipart/mixed.
4752    (t
4753     (gnus-mime-display-mixed (cdr handle)))))
4754
4755 (defun gnus-mime-part-function (handles)
4756   (if (stringp (car handles))
4757       (mapcar 'gnus-mime-part-function (cdr handles))
4758     (funcall gnus-article-mime-part-function handles)))
4759
4760 (defun gnus-mime-display-mixed (handles)
4761   (mapcar 'gnus-mime-display-part handles))
4762
4763 (defun gnus-mime-display-single (handle)
4764   (let ((type (mm-handle-media-type handle))
4765         (ignored gnus-ignored-mime-types)
4766         (not-attachment t)
4767         (move nil)
4768         display text)
4769     (catch 'ignored
4770       (progn
4771         (while ignored
4772           (when (string-match (pop ignored) type)
4773             (throw 'ignored nil)))
4774         (if (and (setq not-attachment
4775                        (and (not (mm-inline-override-p handle))
4776                             (or (not (mm-handle-disposition handle))
4777                                 (equal (car (mm-handle-disposition handle))
4778                                        "inline")
4779                                 (mm-attachment-override-p handle))))
4780                  (mm-automatic-display-p handle)
4781                  (or (and
4782                       (mm-inlinable-p handle)
4783                       (mm-inlined-p handle))
4784                      (mm-automatic-external-display-p type)))
4785             (setq display t)
4786           (when (equal (mm-handle-media-supertype handle) "text")
4787             (setq text t)))
4788         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4789               beg)
4790           (push (cons id handle) gnus-article-mime-handle-alist)
4791           (when (or (not display)
4792                     (not (gnus-unbuttonized-mime-type-p type)))
4793             ;(gnus-article-insert-newline)
4794             (gnus-insert-mime-button
4795              handle id (list (or display (and not-attachment text))))
4796             (gnus-article-insert-newline)
4797             ;(gnus-article-insert-newline)
4798             ;; Remember modify the number of forward lines.
4799             (setq move t))
4800           (setq beg (point))
4801           (cond
4802            (display
4803             (when move
4804               (forward-line -1)
4805               (setq beg (point)))
4806             (let ((mail-parse-charset gnus-newsgroup-charset)
4807                   (mail-parse-ignored-charsets
4808                    (save-excursion (condition-case ()
4809                                        (set-buffer gnus-summary-buffer)
4810                                      (error))
4811                                    gnus-newsgroup-ignored-charsets)))
4812               (mm-display-part handle t))
4813             (goto-char (point-max)))
4814            ((and text not-attachment)
4815             (when move
4816               (forward-line -1)
4817               (setq beg (point)))
4818             (gnus-article-insert-newline)
4819             (mm-insert-inline handle (mm-get-part handle))
4820             (goto-char (point-max))))
4821           ;; Do highlighting.
4822           (save-excursion
4823             (save-restriction
4824               (narrow-to-region beg (point))
4825               (gnus-treat-article
4826                nil id
4827                (gnus-article-mime-total-parts)
4828                (mm-handle-media-type handle)))))))))
4829
4830 (defun gnus-unbuttonized-mime-type-p (type)
4831   "Say whether TYPE is to be unbuttonized."
4832   (unless gnus-inhibit-mime-unbuttonizing
4833     (when (catch 'found
4834             (let ((types gnus-unbuttonized-mime-types))
4835               (while types
4836                 (when (string-match (pop types) type)
4837                   (throw 'found t)))))
4838       (not (catch 'found
4839              (let ((types gnus-buttonized-mime-types))
4840                (while types
4841                  (when (string-match (pop types) type)
4842                    (throw 'found t)))))))))
4843
4844 (defun gnus-article-insert-newline ()
4845   "Insert a newline, but mark it as undeletable."
4846   (gnus-put-text-property
4847    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4848
4849 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4850   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4851          (ihandles handles)
4852          (point (point))
4853          handle buffer-read-only from props begend not-pref)
4854     (save-window-excursion
4855       (save-restriction
4856         (when ibegend
4857           (narrow-to-region (car ibegend)
4858                             (or (cdr ibegend)
4859                                 (progn
4860                                   (goto-char (car ibegend))
4861                                   (forward-line 2)
4862                                   (point))))
4863           (delete-region (point-min) (point-max))
4864           (mm-remove-parts handles))
4865         (setq begend (list (point-marker)))
4866         ;; Do the toggle.
4867         (unless (setq not-pref (cadr (member preferred ihandles)))
4868           (setq not-pref (car ihandles)))
4869         (when (or ibegend
4870                   (not preferred)
4871                   (not (gnus-unbuttonized-mime-type-p
4872                         "multipart/alternative")))
4873           (gnus-add-text-properties
4874            (setq from (point))
4875            (progn
4876              (insert (format "%d.  " id))
4877              (point))
4878            `(gnus-callback
4879              (lambda (handles)
4880                (unless ,(not ibegend)
4881                  (setq gnus-article-mime-handle-alist
4882                        ',gnus-article-mime-handle-alist))
4883                (gnus-mime-display-alternative
4884                 ',ihandles ',not-pref ',begend ,id))
4885              ,@(gnus-local-map-property gnus-mime-button-map)
4886              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4887              face ,gnus-article-button-face
4888              gnus-part ,id
4889              gnus-data ,handle))
4890           (widget-convert-button 'link from (point)
4891                                  :action 'gnus-widget-press-button
4892                                  :button-keymap gnus-widget-button-keymap)
4893           ;; Do the handles
4894           (while (setq handle (pop handles))
4895             (gnus-add-text-properties
4896              (setq from (point))
4897              (progn
4898                (insert (format "(%c) %-18s"
4899                                (if (equal handle preferred) ?* ? )
4900                                (mm-handle-media-type handle)))
4901                (point))
4902              `(gnus-callback
4903                (lambda (handles)
4904                  (unless ,(not ibegend)
4905                    (setq gnus-article-mime-handle-alist
4906                          ',gnus-article-mime-handle-alist))
4907                  (gnus-mime-display-alternative
4908                   ',ihandles ',handle ',begend ,id))
4909                ,@(gnus-local-map-property gnus-mime-button-map)
4910                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4911                face ,gnus-article-button-face
4912                gnus-part ,id
4913                gnus-data ,handle))
4914             (widget-convert-button 'link from (point)
4915                                    :action 'gnus-widget-press-button
4916                                    :button-keymap gnus-widget-button-keymap)
4917             (insert "  "))
4918           (insert "\n\n"))
4919         (when preferred
4920           (if (stringp (car preferred))
4921               (gnus-display-mime preferred)
4922             (let ((mail-parse-charset gnus-newsgroup-charset)
4923                   (mail-parse-ignored-charsets
4924                    (save-excursion (set-buffer gnus-summary-buffer)
4925                                    gnus-newsgroup-ignored-charsets)))
4926               (mm-display-part preferred)
4927               ;; Do highlighting.
4928               (save-excursion
4929                 (save-restriction
4930                   (narrow-to-region (car begend) (point-max))
4931                   (gnus-treat-article
4932                    nil (length gnus-article-mime-handle-alist)
4933                    (gnus-article-mime-total-parts)
4934                    (mm-handle-media-type handle))))))
4935           (goto-char (point-max))
4936           (setcdr begend (point-marker)))))
4937     (when ibegend
4938       (goto-char point))))
4939
4940 (defconst gnus-article-wash-status-strings
4941   (let ((alist '((cite "c" "Possible hidden citation text"
4942                        " " "All citation text visible")
4943                  (headers "h" "Hidden headers"
4944                           " " "All headers visible.")
4945                  (pgp "p" "Encrypted or signed message status hidden"
4946                       " " "No hidden encryption nor digital signature status")
4947                  (signature "s" "Signature has been hidden"
4948                             " " "Signature is visible")
4949                  (overstrike "o" "Overstrike (^H) characters applied"
4950                              " " "No overstrike characters applied")
4951                  (gnus-show-mime "m" "Mime processing is activated"
4952                                  " " "Mime processing is not activated")
4953                  (emphasis "e" "/*_Emphasis_*/ characters applied"
4954                            " " "No /*_emphasis_*/ characters applied")))
4955         result)
4956     (dolist (entry alist result)
4957       (let ((key (nth 0 entry))
4958             (on (copy-sequence (nth 1 entry)))
4959             (on-help (nth 2 entry))
4960             (off (copy-sequence (nth 3 entry)))
4961             (off-help (nth 4 entry)))
4962         (put-text-property 0 1 'help-echo on-help on)
4963         (put-text-property 0 1 'help-echo off-help off)
4964         (push (list key on off) result))))
4965   "Alist of strings describing wash status in the mode line.
4966 Each entry has the form (KEY ON OF), where the KEY is a symbol
4967 representing the particular washing function, ON is the string to use
4968 in the article mode line when the washing function is active, and OFF
4969 is the string to use when it is inactive.")
4970
4971 (defun gnus-article-wash-status-entry (key value)
4972   (let ((entry (assoc key gnus-article-wash-status-strings)))
4973     (if value (nth 1 entry) (nth 2 entry))))
4974
4975 (defun gnus-article-wash-status ()
4976   "Return a string which display status of article washing."
4977   (save-excursion
4978     (set-buffer gnus-article-buffer)
4979     (let ((cite (memq 'cite gnus-article-wash-types))
4980           (headers (memq 'headers gnus-article-wash-types))
4981           (boring (memq 'boring-headers gnus-article-wash-types))
4982           (pgp (memq 'pgp gnus-article-wash-types))
4983           (pem (memq 'pem gnus-article-wash-types))
4984           (signed (memq 'signed gnus-article-wash-types))
4985           (encrypted (memq 'encrypted gnus-article-wash-types))
4986           (signature (memq 'signature gnus-article-wash-types))
4987           (overstrike (memq 'overstrike gnus-article-wash-types))
4988           (emphasis (memq 'emphasis gnus-article-wash-types)))
4989       (concat
4990        (gnus-article-wash-status-entry 'cite cite)
4991        (gnus-article-wash-status-entry 'headers (or headers boring))
4992        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
4993        (gnus-article-wash-status-entry 'signature signature)
4994        (gnus-article-wash-status-entry 'overstrike overstrike)
4995        (gnus-article-wash-status-entry 'gnus-show-mime gnus-show-mime)
4996        (gnus-article-wash-status-entry 'emphasis emphasis)))))
4997
4998 (defun gnus-add-wash-type (type)
4999   "Add a washing of TYPE to the current status."
5000   (add-to-list 'gnus-article-wash-types type))
5001
5002 (defun gnus-delete-wash-type (type)
5003   "Add a washing of TYPE to the current status."
5004   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5005
5006 (defun gnus-add-image (category image)
5007   "Add IMAGE of CATEGORY to the list of displayed images."
5008   (let ((entry (assq category gnus-article-image-alist)))
5009     (unless entry
5010       (setq entry (list category))
5011       (push entry gnus-article-image-alist))
5012     (nconc entry (list image))))
5013
5014 (defun gnus-delete-images (category)
5015   "Delete all images in CATEGORY."
5016   (let ((entry (assq category gnus-article-image-alist)))
5017     (dolist (image (cdr entry))
5018       (gnus-remove-image image))
5019     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5020     (gnus-delete-wash-type category)))
5021
5022 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5023
5024 (defun gnus-article-maybe-hide-headers ()
5025   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5026 Provided for backwards compatibility."
5027   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5028                  (not (save-excursion (set-buffer gnus-summary-buffer)
5029                                       gnus-have-all-headers)))
5030              (not gnus-inhibit-hiding))
5031     (gnus-article-hide-headers)))
5032
5033 ;;; Article savers.
5034
5035 (defun gnus-output-to-file (file-name)
5036   "Append the current article to a file named FILE-NAME."
5037   (let ((artbuf (current-buffer)))
5038     (with-temp-buffer
5039       (insert-buffer-substring artbuf)
5040       ;; Append newline at end of the buffer as separator, and then
5041       ;; save it to file.
5042       (goto-char (point-max))
5043       (insert "\n")
5044       (let ((file-name-coding-system nnmail-pathname-coding-system)
5045             (pathname-coding-system nnmail-pathname-coding-system))
5046         (write-region-as-binary (point-min) (point-max) file-name 'append))
5047       t)))
5048
5049 (defun gnus-narrow-to-page (&optional arg)
5050   "Narrow the article buffer to a page.
5051 If given a numerical ARG, move forward ARG pages."
5052   (interactive "P")
5053   (setq arg (if arg (prefix-numeric-value arg) 0))
5054   (save-excursion
5055     (set-buffer gnus-article-buffer)
5056     (goto-char (point-min))
5057     (widen)
5058     ;; Remove any old next/prev buttons.
5059     (when (gnus-visual-p 'page-marker)
5060       (let ((buffer-read-only nil))
5061         (gnus-remove-text-with-property 'gnus-prev)
5062         (gnus-remove-text-with-property 'gnus-next)))
5063     (when
5064         (cond ((< arg 0)
5065                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
5066               ((> arg 0)
5067                (re-search-forward page-delimiter nil 'move arg)))
5068       (goto-char (match-end 0)))
5069     (narrow-to-region
5070      (point)
5071      (if (re-search-forward page-delimiter nil 'move)
5072          (match-beginning 0)
5073        (point)))
5074     (when (and (gnus-visual-p 'page-marker)
5075                (not (= (point-min) 1)))
5076       (save-excursion
5077         (goto-char (point-min))
5078         (gnus-insert-prev-page-button)))
5079     (when (and (gnus-visual-p 'page-marker)
5080                (< (+ (point-max) 2) (buffer-size)))
5081       (save-excursion
5082         (goto-char (point-max))
5083         (gnus-insert-next-page-button)))))
5084
5085 ;; Article mode commands
5086
5087 (defun gnus-article-goto-next-page ()
5088   "Show the next page of the article."
5089   (interactive)
5090   (when (gnus-article-next-page)
5091     (goto-char (point-min))
5092     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
5093
5094 (defun gnus-article-goto-prev-page ()
5095   "Show the next page of the article."
5096   (interactive)
5097   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
5098     (gnus-article-prev-page nil)))
5099
5100 (defun gnus-article-next-page (&optional lines)
5101   "Show the next page of the current article.
5102 If end of article, return non-nil.  Otherwise return nil.
5103 Argument LINES specifies lines to be scrolled up."
5104   (interactive "p")
5105   (let ((start (window-start))
5106         end-of-buffer end-of-page)
5107     (save-excursion
5108       (move-to-window-line -1)
5109       (if (<= (point) start)
5110           (progn
5111             (forward-line 2)
5112             (setq start (point)))
5113         (forward-line 1)
5114         (setq start nil))
5115       (unless (or (cond ((eq (1+ (buffer-size)) (point))
5116                          (and (pos-visible-in-window-p)
5117                               (setq end-of-buffer t)))
5118                         ((eobp)
5119                          (setq end-of-page t)))
5120                   (not lines))
5121         (move-to-window-line lines)
5122         (unless (search-backward "\n\n" nil t)
5123           (setq start (point)))))
5124     (cond (end-of-buffer t)
5125           (end-of-page
5126            (gnus-narrow-to-page 1)
5127            nil)
5128           (t
5129            (if start
5130                (set-window-start (selected-window) start)
5131              (let (window-pixel-scroll-increment)
5132                (scroll-up lines)))
5133            nil))))
5134
5135 (defun gnus-article-prev-page (&optional lines)
5136   "Show previous page of current article.
5137 Argument LINES specifies lines to be scrolled down."
5138   (interactive "p")
5139   (let (beginning-of-buffer beginning-of-page)
5140     (save-excursion
5141       (move-to-window-line 0)
5142       (cond ((eq 1 (point))
5143              (setq beginning-of-buffer t))
5144             ((bobp)
5145              (setq beginning-of-page t))))
5146     (cond (beginning-of-buffer)
5147           (beginning-of-page
5148            (gnus-narrow-to-page -1))
5149           (t
5150            (condition-case nil
5151                (let (window-pixel-scroll-increment)
5152                  (scroll-down lines))
5153              (beginning-of-buffer
5154               (goto-char (point-min))))))))
5155
5156 (defun gnus-article-refer-article ()
5157   "Read article specified by message-id around point."
5158   (interactive)
5159   (let ((point (point)))
5160     (search-forward ">" nil t)          ;Move point to end of "<....>".
5161     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
5162         (let ((message-id (match-string 1)))
5163           (goto-char point)
5164           (set-buffer gnus-summary-buffer)
5165           (gnus-summary-refer-article message-id))
5166       (goto-char (point))
5167       (error "No references around point"))))
5168
5169 (defun gnus-article-show-summary ()
5170   "Reconfigure windows to show summary buffer."
5171   (interactive)
5172   (if (not (gnus-buffer-live-p gnus-summary-buffer))
5173       (error "There is no summary buffer for this article buffer")
5174     (gnus-article-set-globals)
5175     (gnus-configure-windows 'article)
5176     (gnus-summary-goto-subject gnus-current-article)
5177     (gnus-summary-position-point)))
5178
5179 (defun gnus-article-describe-briefly ()
5180   "Describe article mode commands briefly."
5181   (interactive)
5182   (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")))
5183
5184 (defun gnus-article-summary-command ()
5185   "Execute the last keystroke in the summary buffer."
5186   (interactive)
5187   (let ((obuf (current-buffer))
5188         (owin (current-window-configuration))
5189         func)
5190     (switch-to-buffer gnus-article-current-summary 'norecord)
5191     (setq func (lookup-key (current-local-map) (this-command-keys)))
5192     (call-interactively func)
5193     (set-buffer obuf)
5194     (set-window-configuration owin)
5195     (set-window-point (get-buffer-window (current-buffer)) (point))))
5196
5197 (defun gnus-article-summary-command-nosave ()
5198   "Execute the last keystroke in the summary buffer."
5199   (interactive)
5200   (let (func)
5201     (pop-to-buffer gnus-article-current-summary 'norecord)
5202     (setq func (lookup-key (current-local-map) (this-command-keys)))
5203     (call-interactively func)))
5204
5205 (defun gnus-article-check-buffer ()
5206   "Beep if not in an article buffer."
5207   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
5208     (error "Command invoked outside of a Gnus article buffer")))
5209
5210 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5211   "Read a summary buffer key sequence and execute it from the article buffer."
5212   (interactive "P")
5213   (gnus-article-check-buffer)
5214   (let ((nosaves
5215          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5216            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5217            "=" "^" "\M-^" "|"))
5218         (nosave-but-article
5219          '("A\r"))
5220         (nosave-in-article
5221          '("\C-d"))
5222         (up-to-top
5223          '("n" "Gn" "p" "Gp"))
5224         keys new-sum-point)
5225     (save-excursion
5226       (set-buffer gnus-article-current-summary)
5227       (let (gnus-pick-mode)
5228         (push (or key last-command-event) unread-command-events)
5229         (setq keys (static-if (featurep 'xemacs)
5230                        (events-to-keys (read-key-sequence nil))
5231                      (read-key-sequence nil)))))
5232     (message "")
5233
5234     (if (or (member keys nosaves)
5235             (member keys nosave-but-article)
5236             (member keys nosave-in-article))
5237         (let (func)
5238           (save-window-excursion
5239             (pop-to-buffer gnus-article-current-summary 'norecord)
5240             ;; We disable the pick minor mode commands.
5241             (let (gnus-pick-mode)
5242               (setq func (lookup-key (current-local-map) keys))))
5243           (if (or (not func)
5244                   (numberp func))
5245               (ding)
5246             (unless (member keys nosave-in-article)
5247               (set-buffer gnus-article-current-summary))
5248             (call-interactively func)
5249             (setq new-sum-point (point)))
5250           (when (member keys nosave-but-article)
5251             (pop-to-buffer gnus-article-buffer 'norecord)))
5252       ;; These commands should restore window configuration.
5253       (let ((obuf (current-buffer))
5254             (owin (current-window-configuration))
5255             (opoint (point))
5256             (summary gnus-article-current-summary)
5257             func in-buffer selected)
5258         (if not-restore-window
5259             (pop-to-buffer summary 'norecord)
5260           (switch-to-buffer summary 'norecord))
5261         (setq in-buffer (current-buffer))
5262         ;; We disable the pick minor mode commands.
5263         (if (and (setq func (let (gnus-pick-mode)
5264                               (lookup-key (current-local-map) keys)))
5265                  (functionp func))
5266             (progn
5267               (call-interactively func)
5268               (setq new-sum-point (point))
5269               (when (eq in-buffer (current-buffer))
5270                 (setq selected (gnus-summary-select-article))
5271                 (set-buffer obuf)
5272                 (unless not-restore-window
5273                   (set-window-configuration owin))
5274                 (when (eq selected 'old)
5275                   (article-goto-body)
5276                   (set-window-start (get-buffer-window (current-buffer))
5277                                     1)
5278                   (set-window-point (get-buffer-window (current-buffer))
5279                                     (point)))
5280                 (let ((win (get-buffer-window gnus-article-current-summary)))
5281                   (when win
5282                     (set-window-point win new-sum-point))))    )
5283           (switch-to-buffer gnus-article-buffer)
5284           (ding))))))
5285
5286 (defun gnus-article-describe-key (key)
5287   "Display documentation of the function invoked by KEY.  KEY is a string."
5288   (interactive "kDescribe key: ")
5289   (gnus-article-check-buffer)
5290   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5291       (save-excursion
5292         (set-buffer gnus-article-current-summary)
5293         (let (gnus-pick-mode)
5294           (if (featurep 'xemacs)
5295               (progn
5296                 (push (elt key 0) unread-command-events)
5297                 (setq key (events-to-keys
5298                            (read-key-sequence "Describe key: "))))
5299             (setq unread-command-events
5300                   (mapcar
5301                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5302                    key))
5303             (setq key (read-key-sequence "Describe key: "))))
5304         (describe-key key))
5305     (describe-key key)))
5306
5307 (defun gnus-article-describe-key-briefly (key &optional insert)
5308   "Display documentation of the function invoked by KEY.  KEY is a string."
5309   (interactive "kDescribe key: \nP")
5310   (gnus-article-check-buffer)
5311   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5312       (save-excursion
5313         (set-buffer gnus-article-current-summary)
5314         (let (gnus-pick-mode)
5315           (if (featurep 'xemacs)
5316               (progn
5317                 (push (elt key 0) unread-command-events)
5318                 (setq key (events-to-keys
5319                            (read-key-sequence "Describe key: "))))
5320             (setq unread-command-events
5321                   (mapcar
5322                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5323                    key))
5324             (setq key (read-key-sequence "Describe key: "))))
5325         (describe-key-briefly key insert))
5326     (describe-key-briefly key insert)))
5327
5328 (defun gnus-article-reply-with-original (&optional wide)
5329   "Start composing a reply mail to the current message.
5330 The text in the region will be yanked.  If the region isn't active,
5331 the entire article will be yanked."
5332   (interactive "P")
5333   (let ((article (cdr gnus-article-current)) cont)
5334     (if (not (mark t))
5335         (with-current-buffer gnus-summary-buffer
5336           (gnus-summary-reply (list (list article)) wide))
5337       (setq cont (buffer-substring (point) (mark t)))
5338       ;; Deactivate active regions.
5339       (when (and (boundp 'transient-mark-mode)
5340                  transient-mark-mode)
5341         (setq mark-active nil))
5342       (with-current-buffer gnus-summary-buffer
5343         (gnus-summary-reply
5344          (list (list article cont)) wide)))))
5345
5346 (defun gnus-article-followup-with-original ()
5347   "Compose a followup to the current article.
5348 The text in the region will be yanked.  If the region isn't active,
5349 the entire article will be yanked."
5350   (interactive)
5351   (let ((article (cdr gnus-article-current)) cont)
5352       (if (not (mark t))
5353           (with-current-buffer gnus-summary-buffer
5354             (gnus-summary-followup (list (list article))))
5355         (setq cont (buffer-substring (point) (mark t)))
5356         ;; Deactivate active regions.
5357         (when (and (boundp 'transient-mark-mode)
5358                    transient-mark-mode)
5359           (setq mark-active nil))
5360         (with-current-buffer gnus-summary-buffer
5361           (gnus-summary-followup
5362            (list (list article cont)))))))
5363
5364 (defun gnus-article-hide (&optional arg force)
5365   "Hide all the gruft in the current article.
5366 This means that PGP stuff, signatures, cited text and (some)
5367 headers will be hidden.
5368 If given a prefix, show the hidden text instead."
5369   (interactive (append (gnus-article-hidden-arg) (list 'force)))
5370   (gnus-article-hide-headers arg)
5371   (gnus-article-hide-list-identifiers arg)
5372   (gnus-article-hide-pgp arg)
5373   (gnus-article-hide-citation-maybe arg force)
5374   (gnus-article-hide-signature arg))
5375
5376 (defun gnus-article-maybe-highlight ()
5377   "Do some article highlighting if article highlighting is requested."
5378   (when (gnus-visual-p 'article-highlight 'highlight)
5379     (gnus-article-highlight-some)))
5380
5381 (defun gnus-check-group-server ()
5382   ;; Make sure the connection to the server is alive.
5383   (unless (gnus-server-opened
5384            (gnus-find-method-for-group gnus-newsgroup-name))
5385     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
5386     (gnus-request-group gnus-newsgroup-name t)))
5387
5388 (eval-when-compile
5389   (autoload 'nneething-get-file-name "nneething"))
5390
5391 (defun gnus-request-article-this-buffer (article group)
5392   "Get an article and insert it into this buffer."
5393   (let (do-update-line sparse-header)
5394     (prog1
5395         (save-excursion
5396           (erase-buffer)
5397           (gnus-kill-all-overlays)
5398           (setq group (or group gnus-newsgroup-name))
5399
5400           ;; Using `gnus-request-article' directly will insert the article into
5401           ;; `nntp-server-buffer' - so we'll save some time by not having to
5402           ;; copy it from the server buffer into the article buffer.
5403
5404           ;; We only request an article by message-id when we do not have the
5405           ;; headers for it, so we'll have to get those.
5406           (when (stringp article)
5407             (gnus-read-header article))
5408
5409           ;; If the article number is negative, that means that this article
5410           ;; doesn't belong in this newsgroup (possibly), so we find its
5411           ;; message-id and request it by id instead of number.
5412           (when (and (numberp article)
5413                      gnus-summary-buffer
5414                      (get-buffer gnus-summary-buffer)
5415                      (gnus-buffer-exists-p gnus-summary-buffer))
5416             (save-excursion
5417               (set-buffer gnus-summary-buffer)
5418               (let ((header (gnus-summary-article-header article)))
5419                 (when (< article 0)
5420                   (cond
5421                    ((memq article gnus-newsgroup-sparse)
5422                     ;; This is a sparse gap article.
5423                     (setq do-update-line article)
5424                     (setq article (mail-header-id header))
5425                     (setq sparse-header (gnus-read-header article))
5426                     (setq gnus-newsgroup-sparse
5427                           (delq article gnus-newsgroup-sparse)))
5428                    ((vectorp header)
5429                     ;; It's a real article.
5430                     (setq article (mail-header-id header)))
5431                    (t
5432                     ;; It is an extracted pseudo-article.
5433                     (setq article 'pseudo)
5434                     (gnus-request-pseudo-article header))))
5435
5436                 (let ((method (gnus-find-method-for-group
5437                                gnus-newsgroup-name)))
5438                   (when (and (eq (car method) 'nneething)
5439                              (vectorp header))
5440                     (let ((dir (nneething-get-file-name
5441                                 (mail-header-id header))))
5442                       (when (and (stringp dir)
5443                                  (file-directory-p dir))
5444                         (setq article 'nneething)
5445                         (gnus-group-enter-directory dir))))))))
5446
5447           (cond
5448            ;; Refuse to select canceled articles.
5449            ((and (numberp article)
5450                  gnus-summary-buffer
5451                  (get-buffer gnus-summary-buffer)
5452                  (gnus-buffer-exists-p gnus-summary-buffer)
5453                  (eq (cdr (save-excursion
5454                             (set-buffer gnus-summary-buffer)
5455                             (assq article gnus-newsgroup-reads)))
5456                      gnus-canceled-mark))
5457             nil)
5458            ;; We first check `gnus-original-article-buffer'.
5459            ((and (get-buffer gnus-original-article-buffer)
5460                  (numberp article)
5461                  (save-excursion
5462                    (set-buffer gnus-original-article-buffer)
5463                    (and (equal (car gnus-original-article) group)
5464                         (eq (cdr gnus-original-article) article))))
5465             (insert-buffer-substring gnus-original-article-buffer)
5466             'article)
5467            ;; Check the backlog.
5468            ((and gnus-keep-backlog
5469                  (gnus-backlog-request-article group article (current-buffer)))
5470             'article)
5471            ;; Check asynchronous pre-fetch.
5472            ((gnus-async-request-fetched-article group article (current-buffer))
5473             (gnus-async-prefetch-next group article gnus-summary-buffer)
5474             (when (and (numberp article) gnus-keep-backlog)
5475               (gnus-backlog-enter-article group article (current-buffer)))
5476             'article)
5477            ;; Check the cache.
5478            ((and gnus-use-cache
5479                  (numberp article)
5480                  (gnus-cache-request-article article group))
5481             'article)
5482            ;; Check the agent cache.
5483            ((and gnus-agent gnus-agent-cache gnus-plugged
5484                  (numberp article)
5485                  (gnus-agent-request-article article group))
5486             'article)
5487            ;; Get the article and put into the article buffer.
5488            ((or (stringp article)
5489                 (numberp article))
5490             (let ((gnus-override-method gnus-override-method)
5491                   (methods (and (stringp article)
5492                                 gnus-refer-article-method))
5493                   (backend (car (gnus-find-method-for-group
5494                                  gnus-newsgroup-name)))
5495                   result
5496                   (buffer-read-only nil))
5497               (if (or (not (listp methods))
5498                       (and (symbolp (car methods))
5499                            (assq (car methods) nnoo-definition-alist)))
5500                   (setq methods (list methods)))
5501               (when (and (null gnus-override-method)
5502                          methods)
5503                 (setq gnus-override-method (pop methods)))
5504               (while (not result)
5505                 (when (eq gnus-override-method 'current)
5506                   (setq gnus-override-method
5507                         (with-current-buffer gnus-summary-buffer
5508                           gnus-current-select-method)))
5509                 (erase-buffer)
5510                 (gnus-kill-all-overlays)
5511                 (let ((gnus-newsgroup-name group))
5512                   (gnus-check-group-server))
5513                 (cond
5514                  ((gnus-request-article article group (current-buffer))
5515                   (when (numberp article)
5516                     (gnus-async-prefetch-next group article
5517                                               gnus-summary-buffer)
5518                     (when gnus-keep-backlog
5519                       (gnus-backlog-enter-article
5520                        group article (current-buffer))))
5521                   (setq result 'article))
5522                  (methods
5523                   (setq gnus-override-method (pop methods)))
5524                  ((not (string-match "^400 "
5525                                      (nnheader-get-report backend)))
5526                   ;; If we get 400 server disconnect, reconnect and
5527                   ;; retry; otherwise, assume the article has expired.
5528                   (setq result 'done))))
5529               (and (eq result 'article) 'article)))
5530            ;; It was a pseudo.
5531            (t article)))
5532
5533       ;; Associate this article with the current summary buffer.
5534       (setq gnus-article-current-summary gnus-summary-buffer)
5535
5536       ;; Take the article from the original article buffer
5537       ;; and place it in the buffer it's supposed to be in.
5538       (when (and (get-buffer gnus-article-buffer)
5539                  (equal (buffer-name (current-buffer))
5540                         (buffer-name (get-buffer gnus-article-buffer))))
5541         (save-excursion
5542           (if (get-buffer gnus-original-article-buffer)
5543               (set-buffer gnus-original-article-buffer)
5544             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5545             (buffer-disable-undo)
5546             (setq major-mode 'gnus-original-article-mode)
5547             (setq buffer-read-only t))
5548           (let (buffer-read-only)
5549             (erase-buffer)
5550             (insert-buffer-substring gnus-article-buffer))
5551           (setq gnus-original-article (cons group article)))
5552
5553         ;; Decode charsets.
5554         (run-hooks 'gnus-article-decode-hook)
5555         ;; Mark article as decoded or not.
5556         (setq gnus-article-decoded-p gnus-article-decode-hook))
5557
5558       ;; Update sparse articles.
5559       (when (and do-update-line
5560                  (or (numberp article)
5561                      (stringp article)))
5562         (let ((buf (current-buffer)))
5563           (set-buffer gnus-summary-buffer)
5564           (gnus-summary-update-article do-update-line sparse-header)
5565           (gnus-summary-goto-subject do-update-line nil t)
5566           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5567                             (point))
5568           (set-buffer buf))))))
5569
5570 ;;;
5571 ;;; Article editing
5572 ;;;
5573
5574 (defcustom gnus-article-edit-mode-hook nil
5575   "Hook run in article edit mode buffers."
5576   :group 'gnus-article-various
5577   :type 'hook)
5578
5579 (defcustom gnus-article-edit-article-setup-function
5580   'gnus-article-mime-edit-article-setup
5581   "Function called to setup an editing article buffer."
5582   :group 'gnus-article-various
5583   :type 'function)
5584
5585 (defvar gnus-article-edit-done-function nil)
5586
5587 (defvar gnus-article-edit-mode-map nil)
5588 (defvar gnus-article-edit-mode nil)
5589
5590 ;; Should we be using derived.el for this?
5591 (unless gnus-article-edit-mode-map
5592   (setq gnus-article-edit-mode-map (make-keymap))
5593   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5594
5595   (gnus-define-keys gnus-article-edit-mode-map
5596     "\C-c?"    describe-mode
5597     "\C-c\C-c" gnus-article-edit-done
5598     "\C-c\C-k" gnus-article-edit-exit
5599     "\C-c\C-f\C-t" message-goto-to
5600     "\C-c\C-f\C-o" message-goto-from
5601     "\C-c\C-f\C-b" message-goto-bcc
5602     ;;"\C-c\C-f\C-w" message-goto-fcc
5603     "\C-c\C-f\C-c" message-goto-cc
5604     "\C-c\C-f\C-s" message-goto-subject
5605     "\C-c\C-f\C-r" message-goto-reply-to
5606     "\C-c\C-f\C-n" message-goto-newsgroups
5607     "\C-c\C-f\C-d" message-goto-distribution
5608     "\C-c\C-f\C-f" message-goto-followup-to
5609     "\C-c\C-f\C-m" message-goto-mail-followup-to
5610     "\C-c\C-f\C-k" message-goto-keywords
5611     "\C-c\C-f\C-u" message-goto-summary
5612     "\C-c\C-f\C-i" message-insert-or-toggle-importance
5613     "\C-c\C-f\C-a" message-gen-unsubscribed-mft
5614     "\C-c\C-b" message-goto-body
5615     "\C-c\C-i" message-goto-signature
5616
5617     "\C-c\C-t" message-insert-to
5618     "\C-c\C-n" message-insert-newsgroups
5619     "\C-c\C-o" message-sort-headers
5620     "\C-c\C-e" message-elide-region
5621     "\C-c\C-v" message-delete-not-region
5622     "\C-c\C-z" message-kill-to-signature
5623     "\M-\r" message-newline-and-reformat
5624     "\C-c\C-a" mml-attach-file
5625     "\C-a" message-beginning-of-line
5626     "\t" message-tab
5627     "\M-;" comment-region)
5628
5629   (gnus-define-keys (gnus-article-edit-wash-map
5630                      "\C-c\C-w" gnus-article-edit-mode-map)
5631     "f" gnus-article-edit-full-stops))
5632
5633 (easy-menu-define
5634   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
5635   '("Field"
5636     ["Fetch To" message-insert-to t]
5637     ["Fetch Newsgroups" message-insert-newsgroups t]
5638     "----"
5639     ["To" message-goto-to t]
5640     ["From" message-goto-from t]
5641     ["Subject" message-goto-subject t]
5642     ["Cc" message-goto-cc t]
5643     ["Reply-To" message-goto-reply-to t]
5644     ["Summary" message-goto-summary t]
5645     ["Keywords" message-goto-keywords t]
5646     ["Newsgroups" message-goto-newsgroups t]
5647     ["Followup-To" message-goto-followup-to t]
5648     ["Mail-Followup-To" message-goto-mail-followup-to t]
5649     ["Distribution" message-goto-distribution t]
5650     ["Body" message-goto-body t]
5651     ["Signature" message-goto-signature t]))
5652
5653 (define-derived-mode gnus-article-edit-mode text-mode "Article Edit"
5654   "Major mode for editing articles.
5655 This is an extended text-mode.
5656
5657 \\{gnus-article-edit-mode-map}"
5658   (make-local-variable 'gnus-article-edit-done-function)
5659   (make-local-variable 'gnus-prev-winconf)
5660   (set (make-local-variable 'font-lock-defaults)
5661        '(message-font-lock-keywords t))
5662   (set (make-local-variable 'mail-header-separator) "")
5663   (set (make-local-variable 'gnus-article-edit-mode) t)
5664   (easy-menu-add message-mode-field-menu message-mode-map)
5665   (setq buffer-read-only nil)
5666   (buffer-enable-undo)
5667   (widen))
5668
5669 (defun gnus-article-edit (&optional force)
5670   "Edit the current article.
5671 This will have permanent effect only in mail groups.
5672 If FORCE is non-nil, allow editing of articles even in read-only
5673 groups."
5674   (interactive "P")
5675   (when (and (not force)
5676              (gnus-group-read-only-p))
5677     (error "The current newsgroup does not support article editing"))
5678   (gnus-article-date-original)
5679   (gnus-article-edit-article
5680    'ignore
5681    `(lambda (no-highlight)
5682       'ignore
5683       (gnus-summary-edit-article-done
5684        ,(or (mail-header-references gnus-current-headers) "")
5685        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
5686
5687 (defun gnus-article-edit-article (start-func exit-func)
5688   "Start editing the contents of the current article buffer."
5689   (let ((winconf (current-window-configuration)))
5690     (set-buffer gnus-article-buffer)
5691     (gnus-article-edit-mode)
5692     (funcall start-func)
5693     (set-buffer-modified-p nil)
5694     (gnus-configure-windows 'edit-article)
5695     (setq gnus-article-edit-done-function exit-func)
5696     (setq gnus-prev-winconf winconf)
5697     (when gnus-article-edit-article-setup-function
5698       (funcall gnus-article-edit-article-setup-function))
5699     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
5700
5701 (defun gnus-article-edit-done (&optional arg)
5702   "Update the article edits and exit."
5703   (interactive "P")
5704   (let ((func gnus-article-edit-done-function)
5705         (buf (current-buffer))
5706         (start (window-start))
5707         (p (point))
5708         (winconf gnus-prev-winconf))
5709     (remove-hook 'gnus-article-mode-hook
5710                  'gnus-article-mime-edit-article-unwind)
5711     (widen) ;; Widen it in case that users narrowed the buffer.
5712     (funcall func arg)
5713     (set-buffer buf)
5714     ;; The cache and backlog have to be flushed somewhat.
5715     (when gnus-keep-backlog
5716       (gnus-backlog-remove-article
5717        (car gnus-article-current) (cdr gnus-article-current)))
5718     ;; Flush original article as well.
5719     (save-excursion
5720       (when (get-buffer gnus-original-article-buffer)
5721         (set-buffer gnus-original-article-buffer)
5722         (setq gnus-original-article nil)))
5723     (when gnus-use-cache
5724       (gnus-cache-update-article
5725        (car gnus-article-current) (cdr gnus-article-current)))
5726     ;; We remove all text props from the article buffer.
5727     (kill-all-local-variables)
5728     (gnus-set-text-properties (point-min) (point-max) nil)
5729     (gnus-article-mode)
5730     (set-window-configuration winconf)
5731     (set-buffer buf)
5732     (set-window-start (get-buffer-window buf) start)
5733     (set-window-point (get-buffer-window buf) (point))))
5734
5735 (defun gnus-article-edit-exit ()
5736   "Exit the article editing without updating."
5737   (interactive)
5738   (when (or (not (buffer-modified-p))
5739             (yes-or-no-p "Article modified; kill anyway? "))
5740     (let ((curbuf (current-buffer))
5741           (p (point))
5742           (window-start (window-start)))
5743       (erase-buffer)
5744       (if (gnus-buffer-live-p gnus-original-article-buffer)
5745           (insert-buffer-substring gnus-original-article-buffer))
5746       (let ((winconf gnus-prev-winconf))
5747         (kill-all-local-variables)
5748         (gnus-article-mode)
5749         (set-window-configuration winconf)
5750         ;; Tippy-toe some to make sure that point remains where it was.
5751         (save-current-buffer
5752           (set-buffer curbuf)
5753           (set-window-start (get-buffer-window (current-buffer)) window-start)
5754           (goto-char p))))))
5755
5756 (defun gnus-article-edit-full-stops ()
5757   "Interactively repair spacing at end of sentences."
5758   (interactive)
5759   (save-excursion
5760     (goto-char (point-min))
5761     (search-forward-regexp "^$" nil t)
5762     (let ((case-fold-search nil))
5763       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
5764
5765 ;;;
5766 ;;; Article editing with MIME-Edit
5767 ;;;
5768
5769 (defcustom gnus-article-mime-edit-article-setup-hook nil
5770   "Hook run after setting up a MIME editing article buffer."
5771   :group 'gnus-article-various
5772   :type 'hook)
5773
5774 (defun gnus-article-mime-edit-article-unwind ()
5775   "Unwind `gnus-article-buffer' if article editing was given up."
5776   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5777   (when (featurep 'font-lock)
5778     (setq font-lock-defaults nil)
5779     (font-lock-mode -1))
5780   (when mime-edit-mode-flag
5781     (mime-edit-exit 'nomime 'no-error)
5782     (message "")))
5783
5784 (defun gnus-article-mime-edit-article-setup ()
5785   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
5786 after replacing with the original article."
5787   (setq gnus-show-mime t)
5788   (setq gnus-article-edit-done-function
5789         `(lambda (&rest args)
5790            (when mime-edit-mode-flag
5791              (let (mime-edit-insert-user-agent-field)
5792                (mime-edit-exit))
5793              (message ""))
5794            (goto-char (point-min))
5795            (let (case-fold-search)
5796              (when (re-search-forward
5797                     (format "^%s$" (regexp-quote mail-header-separator))
5798                     nil t)
5799                (replace-match "")))
5800            (apply ,gnus-article-edit-done-function args)
5801            (insert
5802             (prog1
5803                 (buffer-substring-no-properties (point-min) (point-max))
5804               (set-buffer (get-buffer-create gnus-original-article-buffer))
5805               (erase-buffer)))
5806            (setq gnus-current-headers (gnus-article-make-full-mail-header))
5807            (set-buffer gnus-article-buffer)
5808            (gnus-article-prepare-display)))
5809   (substitute-key-definition 'gnus-article-edit-done
5810                              'gnus-article-mime-edit-done
5811                              gnus-article-edit-mode-map)
5812   (substitute-key-definition 'gnus-article-edit-exit
5813                              'gnus-article-mime-edit-exit
5814                              gnus-article-edit-mode-map)
5815   (erase-buffer)
5816   (insert-buffer-substring gnus-original-article-buffer)
5817   (unless (member (with-current-buffer gnus-summary-buffer
5818                     gnus-newsgroup-name)
5819                   '("nndraft:delayed" "nndraft:drafts"))
5820     (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
5821       (fset 'mime-edit-decode-single-part-in-buffer
5822             (lambda (&rest args)
5823               (if (let ((content-type (car args)))
5824                     (and (eq 'message (mime-content-type-primary-type
5825                                        content-type))
5826                          (eq 'rfc822 (mime-content-type-subtype
5827                                       content-type))))
5828                   (setcar (cdr args) 'not-decode-text))
5829               (apply ofn args)))
5830       (unwind-protect
5831           (mime-edit-again)
5832         (fset 'mime-edit-decode-single-part-in-buffer ofn))))
5833   (when (featurep 'font-lock)
5834     (set (make-local-variable 'font-lock-defaults)
5835          '(message-font-lock-keywords t))
5836     (font-lock-set-defaults)
5837     (turn-on-font-lock))
5838   (set-buffer-modified-p nil)
5839   (delete-other-windows)
5840   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5841   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
5842
5843 (defun gnus-article-mime-edit-done (&optional arg)
5844   "Update the article MIME edits and exit."
5845   (interactive "P")
5846   (when (featurep 'font-lock)
5847     (setq font-lock-defaults nil)
5848     (font-lock-mode -1))
5849   (let ((inhibit-read-only t))
5850     (gnus-article-edit-done arg)))
5851
5852 (defun gnus-article-mime-edit-exit ()
5853   "Exit the article MIME editing without updating."
5854   (interactive)
5855   (when (or (not (buffer-modified-p))
5856             (yes-or-no-p "Article modified; kill anyway? "))
5857     (when (featurep 'font-lock)
5858       (setq font-lock-defaults nil)
5859       (font-lock-mode -1))
5860     (when mime-edit-mode-flag
5861       (let (mime-edit-insert-user-agent-field)
5862         (mime-edit-exit))
5863       (message ""))
5864     (goto-char (point-min))
5865     (let (case-fold-search)
5866       (when (re-search-forward
5867              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
5868         (replace-match "")))
5869     (let ((winconf gnus-prev-winconf))
5870       (insert (prog1
5871                   (buffer-substring-no-properties (point-min) (point-max))
5872                 (set-buffer (get-buffer-create gnus-original-article-buffer))
5873                 (erase-buffer)))
5874       (setq gnus-current-headers (gnus-article-make-full-mail-header))
5875       (set-buffer gnus-article-buffer)
5876       (gnus-article-prepare-display)
5877       (set-window-configuration winconf))))
5878
5879 ;;;
5880 ;;; Article highlights
5881 ;;;
5882
5883 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
5884
5885 ;;; Internal Variables:
5886
5887 (defcustom gnus-button-url-regexp
5888   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
5889       "\\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:]]\\)"
5890     "\\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\\)\\)")
5891   "Regular expression that matches URLs."
5892   :group 'gnus-article-buttons
5893   :type 'regexp)
5894
5895 (defcustom gnus-button-valid-fqdn-regexp
5896   (concat "[a-z0-9][-.a-z0-9]+\\." ;; [hostname.subdomain.]domain.
5897           ;; valid TLDs:
5898           "\\([a-z][a-z]" ;; two letter country TDLs
5899           "\\|biz\\|com\\|edu\\|gov\\|int\\|mil\\|net\\|org"
5900           "\\|aero\\|coop\\|info\\|name\\|museum"
5901           "\\|arpa\\|pro\\|uucp\\|bitnet\\|bofh" ;; old style?
5902           "\\)")
5903   "Regular expression that matches a valid FQDN."
5904   :group 'gnus-article-buttons
5905   :type 'regexp)
5906
5907 (defcustom gnus-button-man-handler 'manual-entry
5908   "Function to use for displaying man pages.
5909 The function must take at least one argument with a string naming the
5910 man page."
5911   :type '(choice (function-item :tag "Man" manual-entry)
5912                  (function-item :tag "Woman" woman)
5913                  (function :tag "Other"))
5914   :group 'gnus-article-buttons)
5915
5916 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
5917   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
5918 If the default site is too slow, try to find a CTAN mirror, see
5919 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
5920 the variable `gnus-button-handle-ctan'."
5921   :group 'gnus-article-buttons
5922   :link '(custom-manual "(gnus)Group Parameters")
5923   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
5924                  (const "http://tug.ctan.org/tex-archive/")
5925                  (const "http://www.dante.de/CTAN/")
5926                  (string :tag "Other")))
5927
5928 (defcustom gnus-button-ctan-handler 'browse-url
5929   "Function to use for displaying CTAN links.
5930 The function must take one argument, the string naming the URL."
5931   :type '(choice (function-item :tag "Browse Url" browse-url)
5932                  (function :tag "Other"))
5933   :group 'gnus-article-buttons)
5934
5935 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
5936   "Bogus strings removed from CTAN URLs."
5937   :group 'gnus-article-buttons
5938   :type '(choice (const "^/?tex-archive/\\|/")
5939                  (regexp :tag "Other")))
5940
5941 (defcustom gnus-button-mid-or-mail-regexp
5942   (concat "\\b\\(<?[a-z0-9][^<>\")!;:,{}\n\t ]*@"
5943           gnus-button-valid-fqdn-regexp
5944           ">?\\)\\b")
5945   "Regular expression that matches a message ID or a mail address."
5946   :group 'gnus-article-buttons
5947   :type 'regexp)
5948
5949 (defcustom gnus-button-prefer-mid-or-mail 'guess
5950   "What to do when the button on a string as \"foo123@bar.com\" is pushed.
5951 Strings like this can be either a message ID or a mail address.  If the
5952 variable is set to the symbol `ask', query the user what do do.  If it is the
5953 symbol `guess', Gnus will do a guess and query the user what do do if it is
5954 ambiguous.  See the variable `gnus-button-guessed-mid-regexp' for details
5955 concerning the guessing.  If it is one of the sybols `mid' or `mail', Gnus
5956 will always assume that the string is a message ID or a mail address,
5957 respectivly."
5958   ;; FIXME: doc-string could/should be improved.
5959   :group 'gnus-article-buttons
5960   :type '(choice (const ask)
5961                  (const guess)
5962                  (const mid)
5963                  (const mail)))
5964
5965 (defcustom gnus-button-guessed-mid-regexp
5966   (concat
5967    "^<?\\(slrn\\|Pine\\.\\)"
5968    "\\|\\.fsf@\\|\\.fsf_-_@\\|\\.ln@"
5969    "\\|@4ax\\.com\\|@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de"
5970           "\\|^<?.*[0-9].*[0-9].*[0-9].*[0-9].*[0-9].*[0-9].*@")
5971   "Regular expression that matches message IDs and not mail addresses."
5972   ;; TODO: Incorporate more matches from
5973   ;; <URL:http://piology.org/perl/id-or-mail.pl.html>. I.e. translate the
5974   ;; Perl-REs to Elisp-REs.
5975   :group 'gnus-article-buttons
5976   :type 'regexp)
5977
5978 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
5979   (let* ((pref gnus-button-prefer-mid-or-mail)
5980          (url-mid (concat "news" ":" mid-or-mail))
5981          (url-mailto (concat "mailto" ":" mid-or-mail)))
5982     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
5983     ;; If it looks like a MID (well known readers or servers) use 'mid,
5984     ;; otherwise 'ask the user.
5985     (if (eq pref 'guess)
5986         (if (string-match gnus-button-guessed-mid-regexp mid-or-mail)
5987             (setq pref 'mid)
5988           (setq pref 'ask)))
5989     (if (eq pref 'ask)
5990         (save-window-excursion
5991           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
5992               (setq pref 'mail)
5993             (setq pref 'mid))))
5994     (cond ((eq pref 'mid)
5995            (gnus-message 9 "calling `gnus-button-handle-news' %s" url-mid)
5996            (gnus-button-handle-news url-mid))
5997           ((eq pref 'mail)
5998            (gnus-message 9 "calling `gnus-url-mailto'  %s" url-mailto)
5999            (gnus-url-mailto url-mailto)))))
6000
6001 (defun gnus-button-handle-custom (url)
6002   "Follow a Custom URL."
6003   (customize-apropos (gnus-url-unhex-string url)))
6004
6005 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
6006
6007 (defun gnus-button-handle-describe-function (url)
6008   "Call describe-function when pushing the corresponding URL button."
6009   (describe-function
6010    (intern
6011     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6012
6013 (defun gnus-button-handle-describe-variable (url)
6014   "Call describe-variable when pushing the corresponding URL button."
6015   (describe-variable
6016    (intern
6017     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6018
6019 ;; FIXME: Is is possible to implement this?  Else it should be removed here
6020 ;; and in `gnus-button-alist'.
6021 (defun gnus-button-handle-describe-key (url)
6022   "Call describe-key when pushing the corresponding URL button."
6023   (error "not implemented"))
6024
6025 (defun gnus-button-handle-apropos (url)
6026   "Call apropos when pushing the corresponding URL button."
6027   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6028
6029 (defun gnus-button-handle-apropos-command (url)
6030   "Call apropos when pushing the corresponding URL button."
6031   (apropos-command
6032    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6033
6034 (defun gnus-button-handle-apropos-variable (url)
6035   "Call apropos when pushing the corresponding URL button."
6036   (funcall
6037    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
6038    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6039
6040 (defun gnus-button-handle-apropos-documentation (url)
6041   "Call apropos when pushing the corresponding URL button."
6042   (funcall
6043    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
6044    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6045
6046 (defun gnus-button-handle-ctan (url)
6047   "Call `browse-url' when pushing a CTAN URL button."
6048   (funcall
6049    gnus-button-ctan-handler
6050    (concat
6051     gnus-ctan-url
6052     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
6053
6054 (defcustom gnus-button-tex-level 5
6055   "*Integer that says how many TeX-related buttons Gnus will show.
6056 The higher the number, the more buttons will appear and the more false
6057 positives are possible.  Note that you can set this variable local to
6058 specifific groups.  Setting it higher in TeX groups is probably a good idea.
6059 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6060 how to set variables in specific groups."
6061   :group 'gnus-article-buttons
6062   :link '(custom-manual "(gnus)Group Parameters")
6063   :type 'integer)
6064
6065 (defcustom gnus-button-man-level 5
6066   "*Integer that says how many man-related buttons Gnus will show.
6067 The higher the number, the more buttons will appear and the more false
6068 positives are possible.  Note that you can set this variable local to
6069 specifific groups.  Setting it higher in Unix groups is probably a good idea.
6070 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6071 how to set variables in specific groups."
6072   :group 'gnus-article-buttons
6073   :link '(custom-manual "(gnus)Group Parameters")
6074   :type 'integer)
6075
6076 (defcustom gnus-button-emacs-level 5
6077   "*Integer that says how many emacs-related buttons Gnus will show.
6078 The higher the number, the more buttons will appear and the more false
6079 positives are possible.  Note that you can set this variable local to
6080 specifific groups.  Setting it higher in Emacs or Gnus related groups is
6081 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
6082 `gnus-parameters' on how to set variables in specific groups."
6083   :group 'gnus-article-buttons
6084   :link '(custom-manual "(gnus)Group Parameters")
6085   :type 'integer)
6086
6087 (defcustom gnus-button-mail-level 5
6088   "*Integer that says how many buttons for message IDs or mail addresses will appear.
6089 The higher the number, the more buttons will appear and the more false
6090 positives are possible."
6091   :group 'gnus-article-buttons
6092   :type 'integer)
6093
6094 (defcustom gnus-button-alist
6095   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
6096      0 t gnus-button-handle-news 3)
6097     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
6098      gnus-button-handle-news 2)
6099     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
6100      1 t
6101      gnus-button-fetch-group 4)
6102     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
6103     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
6104      t gnus-button-message-id 3)
6105     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
6106     ("mailto:\\([-a-z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
6107     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
6108     ;; CTAN
6109     ("\\bCTAN:[ \t\n]*\\([^>)!;:,\n\t ]*\\)" 0 (>= gnus-button-tex-level 1)
6110      gnus-button-handle-ctan 1)
6111     ;; This is info
6112     ("\\binfo:\\(//\\)?\\([^'\">\n\t ]+\\)" 0
6113      (>= gnus-button-emacs-level 1) gnus-button-handle-info 2)
6114     ;; This is custom
6115     ("\\bcustom:\\(//\\)?\\([^'\">\n\t ]+\\)" 0
6116      (>= gnus-button-emacs-level 5) gnus-button-handle-custom 2)
6117     ("M-x[ \t\n]customize-[^ ]+[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
6118      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1)
6119     ;; Emacs help commands
6120     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6121      ;; regexp doesn't match arguments containing ` '.
6122      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
6123     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6124      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
6125     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6126      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
6127     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6128      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
6129     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6130      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
6131     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6132      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
6133     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+" 0
6134      ;; this regexp needs to be fixed!
6135      (>= gnus-button-emacs-level 9) gnus-button-handle-describe-key 2)
6136     ;; This is how URLs _should_ be embedded in text...
6137     ("<URL: *\\([^<>]*\\)>" 1 t gnus-button-embedded-url 1)
6138     ;; Raw URLs.
6139     (gnus-button-url-regexp 0 t browse-url 0)
6140     ;; man pages
6141     ("\\b\\([a-z][a-z]+\\)([1-9])\\W" 0
6142      (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
6143      gnus-button-handle-man 1)
6144     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
6145     ("\\b\\([a-z][-_.a-z0-9]+\\)([1-9])\\W" 0
6146      (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
6147      gnus-button-handle-man 1)
6148     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
6149     ;; SoWWWAnchor(3iv), XSelectInput(3X11)
6150     ("\\b\\([a-z][-_.:a-z0-9]+\\)([1-9][X1a-z]*)\\W" 0
6151      (>= gnus-button-man-level 5) gnus-button-handle-man 1)
6152     ;; MID or mail: To avoid too many false positives we don't try to catch
6153     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
6154     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
6155     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
6156     ;; so that non-ambiguous entries (see above) match first.
6157     (gnus-button-mid-or-mail-regexp
6158      0 (>= gnus-button-mail-level 5) gnus-button-handle-mid-or-mail 1))
6159   "*Alist of regexps matching buttons in article bodies.
6160
6161 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
6162 REGEXP: is the string (case insensitive) matching text around the button (can
6163 also be lisp expression evaluating to a string),
6164 BUTTON: is the number of the regexp grouping actually matching the button,
6165 FORM: is a lisp expression which must eval to true for the button to
6166 be added,
6167 CALLBACK: is the function to call when the user push this button, and each
6168 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
6169
6170 CALLBACK can also be a variable, in that case the value of that
6171 variable it the real callback function."
6172   :group 'gnus-article-buttons
6173   :type '(repeat (list (choice regexp variable)
6174                        (integer :tag "Button")
6175                        (sexp :tag "Form")
6176                        (function :tag "Callback")
6177                        (repeat :tag "Par"
6178                                :inline t
6179                                (integer :tag "Regexp group")))))
6180
6181 (defcustom gnus-header-button-alist
6182   '(("^\\(References\\|Message-I[Dd]\\):" "<[^<>]+>"
6183      0 t gnus-button-message-id 0)
6184     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
6185     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
6186      0 t gnus-button-mailto 0)
6187     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp 0 t browse-url 0)
6188     ("^Subject:" gnus-button-url-regexp 0 t browse-url 0)
6189     ("^[^:]+:" gnus-button-url-regexp 0 t browse-url 0)
6190     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
6191     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
6192      gnus-button-message-id 3))
6193   "*Alist of headers and regexps to match buttons in article heads.
6194
6195 This alist is very similar to `gnus-button-alist', except that each
6196 alist has an additional HEADER element first in each entry:
6197
6198 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
6199
6200 HEADER is a regexp to match a header.  For a fuller explanation, see
6201 `gnus-button-alist'."
6202   :group 'gnus-article-buttons
6203   :group 'gnus-article-headers
6204   :type '(repeat (list (regexp :tag "Header")
6205                        regexp
6206                        (integer :tag "Button")
6207                        (sexp :tag "Form")
6208                        (function :tag "Callback")
6209                        (repeat :tag "Par"
6210                                :inline t
6211                                (integer :tag "Regexp group")))))
6212
6213 (defvar gnus-button-regexp nil)
6214 (defvar gnus-button-marker-list nil)
6215 ;; Regexp matching any of the regexps from `gnus-button-alist'.
6216
6217 (defvar gnus-button-last nil)
6218 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
6219
6220 ;;; Commands:
6221
6222 (defun gnus-article-push-button (event)
6223   "Check text under the mouse pointer for a callback function.
6224 If the text under the mouse pointer has a `gnus-callback' property,
6225 call it with the value of the `gnus-data' text property."
6226   (interactive "e")
6227   (set-buffer (window-buffer (posn-window (event-start event))))
6228   (let* ((pos (posn-point (event-start event)))
6229          (data (get-text-property pos 'gnus-data))
6230          (fun (get-text-property pos 'gnus-callback)))
6231     (goto-char pos)
6232     (when fun
6233       (funcall fun data))))
6234
6235 (defun gnus-article-press-button ()
6236   "Check text at point for a callback function.
6237 If the text at point has a `gnus-callback' property,
6238 call it with the value of the `gnus-data' text property."
6239   (interactive)
6240   (let* ((data (get-text-property (point) 'gnus-data))
6241          (fun (get-text-property (point) 'gnus-callback)))
6242     (when fun
6243       (funcall fun data))))
6244
6245 (defun gnus-article-prev-button (n)
6246   "Move point to N buttons backward.
6247 If N is negative, move forward instead."
6248   (interactive "p")
6249   (gnus-article-next-button (- n)))
6250
6251 (defun gnus-article-next-button (n)
6252   "Move point to N buttons forward.
6253 If N is negative, move backward instead."
6254   (interactive "p")
6255   (let ((function (if (< n 0) 'previous-single-property-change
6256                     'next-single-property-change))
6257         (inhibit-point-motion-hooks t)
6258         (backward (< n 0))
6259         (limit (if (< n 0) (point-min) (point-max))))
6260     (setq n (abs n))
6261     (while (and (not (= limit (point)))
6262                 (> n 0))
6263       ;; Skip past the current button.
6264       (when (get-text-property (point) 'gnus-callback)
6265         (goto-char (funcall function (point) 'gnus-callback nil limit)))
6266       ;; Go to the next (or previous) button.
6267       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
6268       ;; Put point at the start of the button.
6269       (when (and backward (not (get-text-property (point) 'gnus-callback)))
6270         (goto-char (funcall function (point) 'gnus-callback nil limit)))
6271       ;; Skip past intangible buttons.
6272       (when (get-text-property (point) 'intangible)
6273         (incf n))
6274       (decf n))
6275     (unless (zerop n)
6276       (gnus-message 5 "No more buttons"))
6277     n))
6278
6279 (defun gnus-article-highlight (&optional force)
6280   "Highlight current article.
6281 This function calls `gnus-article-highlight-headers',
6282 `gnus-article-highlight-citation',
6283 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6284 do the highlighting.  See the documentation for those functions."
6285   (interactive (list 'force))
6286   (gnus-article-highlight-headers)
6287   (gnus-article-highlight-citation force)
6288   (gnus-article-highlight-signature)
6289   (gnus-article-add-buttons force)
6290   (gnus-article-add-buttons-to-head))
6291
6292 (defun gnus-article-highlight-some (&optional force)
6293   "Highlight current article.
6294 This function calls `gnus-article-highlight-headers',
6295 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6296 do the highlighting.  See the documentation for those functions."
6297   (interactive (list 'force))
6298   (gnus-article-highlight-headers)
6299   (gnus-article-highlight-signature)
6300   (gnus-article-add-buttons))
6301
6302 (defun gnus-article-highlight-headers ()
6303   "Highlight article headers as specified by `gnus-header-face-alist'."
6304   (interactive)
6305   (save-excursion
6306     (set-buffer gnus-article-buffer)
6307     (save-restriction
6308       (let ((alist gnus-header-face-alist)
6309             (buffer-read-only nil)
6310             (case-fold-search t)
6311             (inhibit-point-motion-hooks t)
6312             entry regexp header-face field-face from hpoints fpoints)
6313         (article-narrow-to-head)
6314         (while (setq entry (pop alist))
6315           (goto-char (point-min))
6316           (setq regexp (concat "^\\("
6317                                (if (string-equal "" (nth 0 entry))
6318                                    "[^\t ]"
6319                                  (nth 0 entry))
6320                                "\\)")
6321                 header-face (nth 1 entry)
6322                 field-face (nth 2 entry))
6323           (while (and (re-search-forward regexp nil t)
6324                       (not (eobp)))
6325             (beginning-of-line)
6326             (setq from (point))
6327             (unless (search-forward ":" nil t)
6328               (forward-char 1))
6329             (when (and header-face
6330                        (not (memq (point) hpoints)))
6331               (push (point) hpoints)
6332               (gnus-put-text-property from (point) 'face header-face))
6333             (when (and field-face
6334                        (not (memq (setq from (point)) fpoints)))
6335               (push from fpoints)
6336               (if (re-search-forward "^[^ \t]" nil t)
6337                   (forward-char -2)
6338                 (goto-char (point-max)))
6339               (gnus-put-text-property from (point) 'face field-face))))))))
6340
6341 (defun gnus-article-highlight-signature ()
6342   "Highlight the signature in an article.
6343 It does this by highlighting everything after
6344 `gnus-signature-separator' using `gnus-signature-face'."
6345   (interactive)
6346   (when gnus-signature-face
6347     (save-excursion
6348       (set-buffer gnus-article-buffer)
6349       (let ((buffer-read-only nil)
6350             (inhibit-point-motion-hooks t))
6351         (save-restriction
6352           (when (gnus-article-narrow-to-signature)
6353             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
6354                               'face gnus-signature-face)))))))
6355
6356 (defun gnus-article-buttonize-signature ()
6357   "Add button to the signature."
6358   (interactive)
6359   (save-excursion
6360     (set-buffer gnus-article-buffer)
6361     (let ((buffer-read-only nil)
6362           (inhibit-point-motion-hooks t))
6363       (when (gnus-article-search-signature)
6364         (gnus-article-add-button (match-beginning 0) (match-end 0)
6365                                  'gnus-signature-toggle
6366                                  (set-marker (make-marker)
6367                                              (1+ (match-end 0))))))))
6368
6369 (defun gnus-button-in-region-p (b e prop)
6370   "Say whether PROP exists in the region."
6371   (text-property-not-all b e prop nil))
6372
6373 (defun gnus-article-add-buttons (&optional force)
6374   "Find external references in the article and make buttons of them.
6375 \"External references\" are things like Message-IDs and URLs, as
6376 specified by `gnus-button-alist'."
6377   (interactive (list 'force))
6378   (save-excursion
6379     (set-buffer gnus-article-buffer)
6380     (let ((buffer-read-only nil)
6381           (inhibit-point-motion-hooks t)
6382           (case-fold-search t)
6383           (alist gnus-button-alist)
6384           beg entry regexp)
6385       ;; Remove all old markers.
6386       (let (marker entry new-list)
6387         (while (setq marker (pop gnus-button-marker-list))
6388           (if (or (< marker (point-min)) (>= marker (point-max)))
6389               (push marker new-list)
6390             (goto-char marker)
6391             (when (setq entry (gnus-button-entry))
6392               (put-text-property (match-beginning (nth 1 entry))
6393                                  (match-end (nth 1 entry))
6394                                  'gnus-callback nil))
6395             (set-marker marker nil)))
6396         (setq gnus-button-marker-list new-list))
6397       ;; We skip the headers.
6398       (article-goto-body)
6399       (setq beg (point))
6400       (while (setq entry (pop alist))
6401         (setq regexp (eval (car entry)))
6402         (goto-char beg)
6403         (while (re-search-forward regexp nil t)
6404           (let* ((start (and entry (match-beginning (nth 1 entry))))
6405                  (end (and entry (match-end (nth 1 entry))))
6406                  (from (match-beginning 0)))
6407             (when (and (or (eq t (nth 2 entry))
6408                            (eval (nth 2 entry)))
6409                        (not (gnus-button-in-region-p
6410                              start end 'gnus-callback)))
6411               ;; That optional form returned non-nil, so we add the
6412               ;; button.
6413               (gnus-article-add-button
6414                start end 'gnus-button-push
6415                (car (push (set-marker (make-marker) from)
6416                           gnus-button-marker-list))))))))))
6417
6418 ;; Add buttons to the head of an article.
6419 (defun gnus-article-add-buttons-to-head ()
6420   "Add buttons to the head of the article."
6421   (interactive)
6422   (save-excursion
6423     (set-buffer gnus-article-buffer)
6424     (save-restriction
6425       (let ((buffer-read-only nil)
6426             (inhibit-point-motion-hooks t)
6427             (case-fold-search t)
6428             (alist gnus-header-button-alist)
6429             entry beg end)
6430         (article-narrow-to-head)
6431         (while alist
6432           ;; Each alist entry.
6433           (setq entry (car alist)
6434                 alist (cdr alist))
6435           (goto-char (point-min))
6436           (while (re-search-forward (car entry) nil t)
6437             ;; Each header matching the entry.
6438             (setq beg (match-beginning 0))
6439             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
6440                                (match-beginning 0))
6441                           (point-max)))
6442             (goto-char beg)
6443             (while (re-search-forward (eval (nth 1 entry)) end t)
6444               ;; Each match within a header.
6445               (let* ((entry (cdr entry))
6446                      (start (match-beginning (nth 1 entry)))
6447                      (end (match-end (nth 1 entry)))
6448                      (form (nth 2 entry)))
6449                 (goto-char (match-end 0))
6450                 (when (eval form)
6451                   (gnus-article-add-button
6452                    start end (nth 3 entry)
6453                    (buffer-substring (match-beginning (nth 4 entry))
6454                                      (match-end (nth 4 entry)))))))
6455             (goto-char end)))))))
6456
6457 ;;; External functions:
6458
6459 (defun gnus-article-add-button (from to fun &optional data)
6460   "Create a button between FROM and TO with callback FUN and data DATA."
6461   (when gnus-article-button-face
6462     (gnus-overlay-put (gnus-make-overlay from to)
6463                       'face gnus-article-button-face))
6464   (gnus-add-text-properties
6465    from to
6466    (nconc (and gnus-article-mouse-face
6467                (list gnus-mouse-face-prop gnus-article-mouse-face))
6468           (list 'gnus-callback fun)
6469           (and data (list 'gnus-data data))))
6470   (widget-convert-button 'link from to :action 'gnus-widget-press-button
6471                          ;; Quote `:button-keymap' for Mule 2.3
6472                          ;; but it won't work.
6473                          ':button-keymap gnus-widget-button-keymap))
6474
6475 ;;; Internal functions:
6476
6477 (defun gnus-article-set-globals ()
6478   (save-excursion
6479     (set-buffer gnus-summary-buffer)
6480     (gnus-set-global-variables)))
6481
6482 (defun gnus-signature-toggle (end)
6483   (save-excursion
6484     (set-buffer gnus-article-buffer)
6485     (let ((buffer-read-only nil)
6486           (inhibit-point-motion-hooks t)
6487           (limit (next-single-property-change end 'mime-view-entity
6488                                               nil (point-max))))
6489       (if (text-property-any end limit 'article-type 'signature)
6490           (progn
6491             (gnus-delete-wash-type 'signature)
6492             (gnus-remove-text-properties-when
6493              'article-type 'signature end limit
6494              (cons 'article-type (cons 'signature
6495                                        gnus-hidden-properties))))
6496         (gnus-add-wash-type 'signature)
6497         (gnus-add-text-properties-when
6498          'article-type nil end limit
6499          (cons 'article-type (cons 'signature
6500                                    gnus-hidden-properties)))))
6501     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
6502       (gnus-set-mode-line 'article))))
6503
6504 (defun gnus-button-entry ()
6505   ;; Return the first entry in `gnus-button-alist' matching this place.
6506   (let ((alist gnus-button-alist)
6507         (entry nil))
6508     (while alist
6509       (setq entry (pop alist))
6510       (if (looking-at (eval (car entry)))
6511           (setq alist nil)
6512         (setq entry nil)))
6513     entry))
6514
6515 (defun gnus-button-push (marker)
6516   ;; Push button starting at MARKER.
6517   (save-excursion
6518     (goto-char marker)
6519     (let* ((entry (gnus-button-entry))
6520            (inhibit-point-motion-hooks t)
6521            (fun (nth 3 entry))
6522            (args (mapcar (lambda (group)
6523                            (let ((string (match-string group)))
6524                              (gnus-set-text-properties
6525                               0 (length string) nil string)
6526                              string))
6527                          (nthcdr 4 entry))))
6528       (cond
6529        ((fboundp fun)
6530         (apply fun args))
6531        ((and (boundp fun)
6532              (fboundp (symbol-value fun)))
6533         (apply (symbol-value fun) args))
6534        (t
6535         (gnus-message 1 "You must define `%S' to use this button"
6536                       (cons fun args)))))))
6537
6538 (defun gnus-parse-news-url (url)
6539   (let (scheme server group message-id articles)
6540     (with-temp-buffer
6541       (insert url)
6542       (goto-char (point-min))
6543       (when (looking-at "\\([A-Za-z]+\\):")
6544         (setq scheme (match-string 1))
6545         (goto-char (match-end 0)))
6546       (when (looking-at "//\\([^/]+\\)/")
6547         (setq server (match-string 1))
6548         (goto-char (match-end 0)))
6549
6550       (cond
6551        ((looking-at "\\(.*@.*\\)")
6552         (setq message-id (match-string 1)))
6553        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
6554         (setq group (match-string 1)
6555               articles (split-string (match-string 2) "-")))
6556        ((looking-at "\\([^/]+\\)/?")
6557         (setq group (match-string 1)))
6558        (t
6559         (error "Unknown news URL syntax"))))
6560     (list scheme server group message-id articles)))
6561
6562 (defun gnus-button-handle-news (url)
6563   "Fetch a news URL."
6564   (destructuring-bind (scheme server group message-id articles)
6565       (gnus-parse-news-url url)
6566     (cond
6567      (message-id
6568       (save-excursion
6569         (set-buffer gnus-summary-buffer)
6570         (if server
6571             (let ((gnus-refer-article-method (list (list 'nntp server))))
6572               (gnus-summary-refer-article message-id))
6573           (gnus-summary-refer-article message-id))))
6574      (group
6575       (gnus-button-fetch-group url)))))
6576
6577 (defun gnus-button-handle-man (url)
6578   "Fetch a man page."
6579   (funcall gnus-button-man-handler url))
6580
6581 (defun gnus-button-handle-info (url)
6582   "Fetch an info URL."
6583   (if (string-match
6584        "^\\([^:/]+\\)?/\\(.*\\)"
6585        url)
6586       (gnus-info-find-node
6587        (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
6588                        "Gnus")
6589                ")"
6590                (gnus-url-unhex-string (match-string 2 url))))
6591     (error "Can't parse %s" url)))
6592
6593 (defun gnus-button-message-id (message-id)
6594   "Fetch MESSAGE-ID."
6595   (save-excursion
6596     (set-buffer gnus-summary-buffer)
6597     (gnus-summary-refer-article message-id)))
6598
6599 (defun gnus-button-fetch-group (address)
6600   "Fetch GROUP specified by ADDRESS."
6601   (if (not (string-match "[:/]" address))
6602       ;; This is just a simple group url.
6603       (gnus-group-read-ephemeral-group address gnus-select-method)
6604     (if (not
6605          (string-match
6606           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
6607           address))
6608         (error "Can't parse %s" address)
6609       (gnus-group-read-ephemeral-group
6610        (match-string 4 address)
6611        `(nntp ,(match-string 1 address)
6612               (nntp-address ,(match-string 1 address))
6613               (nntp-port-number ,(if (match-end 3)
6614                                      (match-string 3 address)
6615                                    "nntp")))
6616        nil nil nil
6617        (and (match-end 6) (list (string-to-int (match-string 6 address))))))))
6618
6619 (defun gnus-url-parse-query-string (query &optional downcase)
6620   (let (retval pairs cur key val)
6621     (setq pairs (split-string query "&"))
6622     (while pairs
6623       (setq cur (car pairs)
6624             pairs (cdr pairs))
6625       (if (not (string-match "=" cur))
6626           nil                           ; Grace
6627         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
6628               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
6629         (if downcase
6630             (setq key (downcase key)))
6631         (setq cur (assoc key retval))
6632         (if cur
6633             (setcdr cur (cons val (cdr cur)))
6634           (setq retval (cons (list key val) retval)))))
6635     retval))
6636
6637 (defun gnus-url-mailto (url)
6638   ;; Send mail to someone
6639   (when (string-match "mailto:/*\\(.*\\)" url)
6640     (setq url (substring url (match-beginning 1) nil)))
6641   (let (to args subject func)
6642     (if (string-match (regexp-quote "?") url)
6643         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
6644               args (gnus-url-parse-query-string
6645                     (substring url (match-end 0) nil) t))
6646       (setq to (gnus-url-unhex-string url)))
6647     (setq args (cons (list "to" to) args)
6648           subject (cdr-safe (assoc "subject" args)))
6649     (gnus-msg-mail)
6650     (while args
6651       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
6652       (if (fboundp func)
6653           (funcall func)
6654         (message-position-on-field (caar args)))
6655       (insert (mapconcat 'identity (cdar args) ", "))
6656       (setq args (cdr args)))
6657     (if subject
6658         (message-goto-body)
6659       (message-goto-subject))))
6660
6661 (defun gnus-button-embedded-url (address)
6662   "Activate ADDRESS with `browse-url'."
6663   (browse-url (gnus-strip-whitespace address)))
6664
6665 ;;; Next/prev buttons in the article buffer.
6666
6667 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
6668 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
6669
6670 (defvar gnus-prev-page-map
6671   (let ((map (make-sparse-keymap)))
6672     (unless (>= emacs-major-version 21)
6673       ;; XEmacs doesn't care.
6674       (set-keymap-parent map gnus-article-mode-map))
6675     (define-key map gnus-mouse-2 'gnus-button-prev-page)
6676     (define-key map "\r" 'gnus-button-prev-page)
6677     map))
6678
6679 (defun gnus-insert-prev-page-button ()
6680   (let ((b (point))
6681         (buffer-read-only nil)
6682         (situation (get-text-property (point-min) 'mime-view-situation)))
6683     (gnus-eval-format
6684      gnus-prev-page-line-format nil
6685      `(,@(gnus-local-map-property gnus-prev-page-map)
6686          gnus-prev t
6687          gnus-callback gnus-article-button-prev-page
6688          article-type annotation
6689          mime-view-situation ,situation))
6690     (widget-convert-button
6691      'link b (point)
6692      :action 'gnus-button-prev-page
6693      :button-keymap gnus-prev-page-map)))
6694
6695 (defvar gnus-next-page-map
6696   (let ((map (make-sparse-keymap)))
6697     (unless (>= emacs-major-version 21)
6698       ;; XEmacs doesn't care.
6699       (set-keymap-parent map gnus-article-mode-map))
6700     (define-key map gnus-mouse-2 'gnus-button-next-page)
6701     (define-key map "\r" 'gnus-button-next-page)
6702     map))
6703
6704 (defun gnus-button-next-page (&optional args more-args)
6705   "Go to the next page."
6706   (interactive)
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-button-prev-page (&optional args more-args)
6713   "Go to the prev page."
6714   (interactive)
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 (defun gnus-insert-next-page-button ()
6721   (let ((b (point))
6722         (buffer-read-only nil)
6723         (situation (get-text-property (point-min) 'mime-view-situation)))
6724     (gnus-eval-format gnus-next-page-line-format nil
6725                       `(,@(gnus-local-map-property gnus-next-page-map)
6726                           gnus-next t
6727                           gnus-callback gnus-article-button-next-page
6728                           article-type annotation
6729                           mime-view-situation ,situation))
6730     (widget-convert-button
6731      'link b (point)
6732      :action 'gnus-button-next-page
6733      :button-keymap gnus-next-page-map)))
6734
6735 (defun gnus-article-button-next-page (arg)
6736   "Go to the next page."
6737   (interactive "P")
6738   (let ((win (selected-window)))
6739     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6740     (gnus-article-next-page)
6741     (select-window win)))
6742
6743 (defun gnus-article-button-prev-page (arg)
6744   "Go to the prev page."
6745   (interactive "P")
6746   (let ((win (selected-window)))
6747     (select-window (gnus-get-buffer-window gnus-article-buffer t))
6748     (gnus-article-prev-page)
6749     (select-window win)))
6750
6751 (defvar gnus-decode-header-methods
6752   '(mail-decode-encoded-word-region)
6753   "List of methods used to decode headers.
6754
6755 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
6756 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
6757 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
6758 whose names match REGEXP.
6759
6760 For example:
6761 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
6762  mail-decode-encoded-word-region
6763  (\"chinese\" . rfc1843-decode-region))
6764 ")
6765
6766 (defvar gnus-decode-header-methods-cache nil)
6767
6768 (defun gnus-multi-decode-header (start end)
6769   "Apply the functions from `gnus-encoded-word-methods' that match."
6770   (unless (and gnus-decode-header-methods-cache
6771                (eq gnus-newsgroup-name
6772                    (car gnus-decode-header-methods-cache)))
6773     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
6774     (mapcar (lambda (x)
6775               (if (symbolp x)
6776                   (nconc gnus-decode-header-methods-cache (list x))
6777                 (if (and gnus-newsgroup-name
6778                          (string-match (car x) gnus-newsgroup-name))
6779                     (nconc gnus-decode-header-methods-cache
6780                            (list (cdr x))))))
6781             gnus-decode-header-methods))
6782   (let ((xlist gnus-decode-header-methods-cache))
6783     (pop xlist)
6784     (save-restriction
6785       (narrow-to-region start end)
6786       (while xlist
6787         (funcall (pop xlist) (point-min) (point-max))))))
6788
6789 ;;;
6790 ;;; Treatment top-level handling.
6791 ;;;
6792
6793 (defun gnus-treat-article (condition &optional part-number total-parts type)
6794   (let ((length (- (point-max) (point-min)))
6795         (alist gnus-treatment-function-alist)
6796         (article-goto-body-goes-to-point-min-p t)
6797         (treated-type
6798          (or (not type)
6799              (catch 'found
6800                (let ((list gnus-article-treat-types))
6801                  (while list
6802                    (when (string-match (pop list) type)
6803                      (throw 'found t)))))))
6804         (highlightp (gnus-visual-p 'article-highlight 'highlight))
6805         (entity (static-unless (featurep 'xemacs)
6806                   (when (eq 'head condition)
6807                     (get-text-property (point-min) 'mime-view-entity))))
6808         val elem buttonized)
6809     (gnus-run-hooks 'gnus-part-display-hook)
6810     (unless gnus-inhibit-treatment
6811       (dolist (elem alist)
6812         (setq val
6813               (save-excursion
6814                 (when (gnus-buffer-live-p gnus-summary-buffer)
6815                   (set-buffer gnus-summary-buffer))
6816                 (symbol-value (car elem))))
6817         (when (and (or (consp val)
6818                        treated-type)
6819                    (gnus-treat-predicate val)
6820                    (or (not (get (car elem) 'highlight))
6821                        highlightp))
6822           (when (and (not buttonized)
6823                      (memq (car elem)
6824                            '(gnus-treat-hide-signature
6825                              gnus-treat-highlight-signature)))
6826             (gnus-article-buttonize-signature)
6827             (setq buttonized t))
6828           (save-restriction
6829             (funcall (cadr elem)))))
6830       ;; FSF Emacsen does not inherit the existing text properties
6831       ;; in the new text, so we should do it for `mime-view-entity'.
6832       (static-unless (featurep 'xemacs)
6833         (when entity
6834           (put-text-property (point-min) (point-max)
6835                              'mime-view-entity entity))))))
6836
6837 ;; Dynamic variables.
6838 (eval-when-compile
6839   (defvar part-number)
6840   (defvar total-parts)
6841   (defvar type)
6842   (defvar condition)
6843   (defvar length))
6844
6845 (defun gnus-treat-predicate (val)
6846   (cond
6847    ((null val)
6848     nil)
6849    (condition
6850     (eq condition val))
6851    ((and (listp val)
6852          (stringp (car val)))
6853     (apply 'gnus-or (mapcar `(lambda (s)
6854                                (string-match s ,(or gnus-newsgroup-name "")))
6855                             val)))
6856    ((listp val)
6857     (let ((pred (pop val)))
6858       (cond
6859        ((eq pred 'or)
6860         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
6861        ((eq pred 'and)
6862         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
6863        ((eq pred 'not)
6864         (not (gnus-treat-predicate (car val))))
6865        ((eq pred 'typep)
6866         (equal (car val) type))
6867        (t
6868         (error "%S is not a valid predicate" pred)))))
6869    ((eq val 'mime)
6870     gnus-show-mime)
6871    ((eq val t)
6872     t)
6873    ((eq val 'head)
6874     nil)
6875    ((eq val 'last)
6876     (eq part-number total-parts))
6877    ((numberp val)
6878     (< length val))
6879    (t
6880     (error "%S is not a valid value" val))))
6881
6882 (defun gnus-article-encrypt-body (protocol &optional n)
6883   "Encrypt the article body."
6884   (interactive
6885    (list
6886     (or gnus-article-encrypt-protocol
6887         (completing-read "Encrypt protocol: "
6888                          gnus-article-encrypt-protocol-alist
6889                          nil t))
6890     current-prefix-arg))
6891   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
6892     (unless func
6893       (error (format "Can't find the encrypt protocol %s" protocol)))
6894     (if (member gnus-newsgroup-name '("nndraft:delayed"
6895                                       "nndraft:drafts"
6896                                       "nndraft:queue"))
6897         (error "Can't encrypt the article in group %s"
6898                gnus-newsgroup-name))
6899     (gnus-summary-iterate n
6900       (save-excursion
6901         (set-buffer gnus-summary-buffer)
6902         (let ((mail-parse-charset gnus-newsgroup-charset)
6903               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6904               (summary-buffer gnus-summary-buffer)
6905               references point)
6906           (gnus-set-global-variables)
6907           (when (gnus-group-read-only-p)
6908             (error "The current newsgroup does not support article encrypt"))
6909           (gnus-summary-show-article t)
6910           (setq references
6911                 (or (mail-header-references gnus-current-headers) ""))
6912           (set-buffer gnus-article-buffer)
6913           (let* ((buffer-read-only nil)
6914                  (headers
6915                   (mapcar (lambda (field)
6916                             (and (save-restriction
6917                                    (message-narrow-to-head)
6918                                    (goto-char (point-min))
6919                                    (search-forward field nil t))
6920                                  (prog2
6921                                      (message-narrow-to-field)
6922                                      (buffer-substring (point-min) (point-max))
6923                                    (delete-region (point-min) (point-max))
6924                                    (widen))))
6925                           '("Content-Type:" "Content-Transfer-Encoding:"
6926                             "Content-Disposition:"))))
6927             (message-narrow-to-head)
6928             (message-remove-header "MIME-Version")
6929             (goto-char (point-max))
6930             (setq point (point))
6931             (insert (apply 'concat headers))
6932             (widen)
6933             (narrow-to-region point (point-max))
6934             (let ((message-options message-options))
6935               (message-options-set 'message-sender user-mail-address)
6936               (message-options-set 'message-recipients user-mail-address)
6937               (message-options-set 'message-sign-encrypt 'not)
6938               (funcall func))
6939             (goto-char (point-min))
6940             (insert "MIME-Version: 1.0\n")
6941             (widen)
6942             (gnus-summary-edit-article-done
6943              references nil summary-buffer t))
6944           (when gnus-keep-backlog
6945             (gnus-backlog-remove-article
6946              (car gnus-article-current) (cdr gnus-article-current)))
6947           (save-excursion
6948             (when (get-buffer gnus-original-article-buffer)
6949               (set-buffer gnus-original-article-buffer)
6950               (setq gnus-original-article nil)))
6951           (when gnus-use-cache
6952             (gnus-cache-update-article
6953              (car gnus-article-current) (cdr gnus-article-current))))))))
6954
6955 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
6956   "The following specs can be used:
6957 %t  The security MIME type
6958 %i  Additional info
6959 %d  Details
6960 %D  Details if button is pressed")
6961
6962 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
6963   "The following specs can be used:
6964 %t  The security MIME type
6965 %i  Additional info
6966 %d  Details
6967 %D  Details if button is pressed")
6968
6969 (defvar gnus-mime-security-button-line-format-alist
6970   '((?t gnus-tmp-type ?s)
6971     (?i gnus-tmp-info ?s)
6972     (?d gnus-tmp-details ?s)
6973     (?D gnus-tmp-pressed-details ?s)))
6974
6975 (defvar gnus-mime-security-button-map
6976   (let ((map (make-sparse-keymap)))
6977     (unless (>= (string-to-number emacs-version) 21)
6978       (set-keymap-parent map gnus-article-mode-map))
6979     (define-key map gnus-mouse-2 'gnus-article-push-button)
6980     (define-key map "\r" 'gnus-article-press-button)
6981     map))
6982
6983 (defvar gnus-mime-security-details-buffer nil)
6984
6985 (defvar gnus-mime-security-button-pressed nil)
6986
6987 (defvar gnus-mime-security-show-details-inline t
6988   "If non-nil, show details in the article buffer.")
6989
6990 (defun gnus-mime-security-verify-or-decrypt (handle)
6991   (mm-remove-parts (cdr handle))
6992   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
6993         point buffer-read-only)
6994     (if region
6995         (goto-char (car region)))
6996     (save-restriction
6997       (narrow-to-region (point) (point))
6998       (with-current-buffer (mm-handle-multipart-original-buffer handle)
6999         (let* ((mm-verify-option 'known)
7000                (mm-decrypt-option 'known)
7001                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
7002           (unless (eq nparts (cdr handle))
7003             (mm-destroy-parts (cdr handle))
7004             (setcdr handle nparts))))
7005       (setq point (point))
7006       (gnus-mime-display-security handle)
7007       (goto-char (point-max)))
7008     (when region
7009       (delete-region (point) (cdr region))
7010       (set-marker (car region) nil)
7011       (set-marker (cdr region) nil))
7012     (goto-char point)))
7013
7014 (defun gnus-mime-security-show-details (handle)
7015   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
7016     (if details
7017         (if gnus-mime-security-show-details-inline
7018             (let ((gnus-mime-security-button-pressed t)
7019                   (gnus-mime-security-button-line-format
7020                    (get-text-property (point) 'gnus-line-format))
7021                   buffer-read-only)
7022               (forward-char -1)
7023               (while (eq (get-text-property (point) 'gnus-line-format)
7024                          gnus-mime-security-button-line-format)
7025                 (forward-char -1))
7026               (forward-char)
7027               (save-restriction
7028                 (narrow-to-region (point) (point))
7029                 (gnus-insert-mime-security-button handle))
7030               (delete-region (point)
7031                              (or (text-property-not-all
7032                                   (point) (point-max)
7033                                   'gnus-line-format
7034                                   gnus-mime-security-button-line-format)
7035                                  (point-max))))
7036           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
7037               (with-current-buffer gnus-mime-security-details-buffer
7038                 (erase-buffer)
7039                 t)
7040             (setq gnus-mime-security-details-buffer
7041                   (gnus-get-buffer-create "*MIME Security Details*")))
7042           (with-current-buffer gnus-mime-security-details-buffer
7043             (insert details)
7044             (goto-char (point-min)))
7045           (pop-to-buffer gnus-mime-security-details-buffer))
7046       (gnus-message 5 "No details."))))
7047
7048 (defun gnus-mime-security-press-button (handle)
7049   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7050       (gnus-mime-security-show-details handle)
7051     (gnus-mime-security-verify-or-decrypt handle)))
7052
7053 (defun gnus-insert-mime-security-button (handle &optional displayed)
7054   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
7055          (gnus-tmp-type
7056           (concat
7057            (or (nth 2 (assoc protocol mm-verify-function-alist))
7058                (nth 2 (assoc protocol mm-decrypt-function-alist))
7059                "Unknown")
7060            (if (equal (car handle) "multipart/signed")
7061                " Signed" " Encrypted")
7062            " Part"))
7063          (gnus-tmp-info
7064           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7065               "Undecided"))
7066          (gnus-tmp-details
7067           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
7068          gnus-tmp-pressed-details
7069          b e)
7070     (setq gnus-tmp-details
7071           (if gnus-tmp-details
7072               (concat "\n" gnus-tmp-details) ""))
7073     (setq gnus-tmp-pressed-details
7074           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
7075     (unless (bolp)
7076       (insert "\n"))
7077     (setq b (point))
7078     (gnus-eval-format
7079      gnus-mime-security-button-line-format
7080      gnus-mime-security-button-line-format-alist
7081      `(,@(gnus-local-map-property gnus-mime-security-button-map)
7082          gnus-callback gnus-mime-security-press-button
7083          gnus-line-format ,gnus-mime-security-button-line-format
7084          article-type annotation
7085          gnus-data ,handle))
7086     (setq e (point))
7087     (widget-convert-button
7088      'link b e
7089      :mime-handle handle
7090      :action 'gnus-widget-press-button
7091      :button-keymap gnus-mime-security-button-map
7092      :help-echo
7093      (lambda (widget/window &optional overlay pos)
7094        ;; Needed to properly clear the message due to a bug in
7095        ;; wid-edit (XEmacs only).
7096        (if (boundp 'help-echo-owns-message)
7097            (setq help-echo-owns-message t))
7098        (format
7099         "%S: show detail"
7100         (aref gnus-mouse-2 0))))))
7101
7102 (defun gnus-mime-display-security (handle)
7103   (save-restriction
7104     (narrow-to-region (point) (point))
7105     (unless (gnus-unbuttonized-mime-type-p (car handle))
7106       (gnus-insert-mime-security-button handle))
7107     (gnus-mime-display-mixed (cdr handle))
7108     (unless (bolp)
7109       (insert "\n"))
7110     (unless (gnus-unbuttonized-mime-type-p (car handle))
7111       (let ((gnus-mime-security-button-line-format
7112              gnus-mime-security-button-end-line-format))
7113         (gnus-insert-mime-security-button handle)))
7114     (mm-set-handle-multipart-parameter
7115      handle 'gnus-region
7116      (cons (set-marker (make-marker) (point-min))
7117            (set-marker (make-marker) (point-max))))))
7118
7119
7120 ;;; @ for mime-view
7121 ;;;
7122
7123 (defun gnus-article-header-presentation-method (entity situation)
7124   (mime-insert-header entity)
7125   (article-decode-group-name))
7126
7127 (set-alist 'mime-header-presentation-method-alist
7128            'gnus-original-article-mode
7129            #'gnus-article-header-presentation-method)
7130
7131 (defun gnus-mime-preview-quitting-method ()
7132   (mime-preview-kill-buffer)
7133   (delete-other-windows)
7134   (gnus-article-show-summary)
7135   (gnus-summary-select-article gnus-show-all-headers t))
7136
7137 (set-alist 'mime-preview-quitting-method-alist
7138            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
7139
7140 (set-alist 'mime-preview-following-method-alist
7141            'gnus-original-article-mode #'gnus-following-method)
7142
7143 (set-alist 'mime-preview-over-to-previous-method-alist
7144            'gnus-original-article-mode
7145            (lambda ()
7146              (if (> (point-min) 1)
7147                  (gnus-article-prev-page)
7148                (gnus-article-read-summary-keys
7149                 nil (gnus-character-to-event ?P)))))
7150
7151 (set-alist 'mime-preview-over-to-next-method-alist
7152            'gnus-original-article-mode'
7153            (lambda ()
7154              (if (< (point-max) (buffer-size))
7155                  (gnus-article-next-page)
7156                (gnus-article-read-summary-keys
7157                 nil (gnus-character-to-event ?N)))))
7158
7159
7160 ;;; @ end
7161 ;;;
7162
7163 (gnus-ems-redefine)
7164
7165 (provide 'gnus-art)
7166
7167 (run-hooks 'gnus-art-load-hook)
7168
7169 ;;; gnus-art.el ends here