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