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