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