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