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