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