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