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