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