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