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