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