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