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