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