gnus-art.el (gnus-shr-put-image): Make it work as well for shr.el's that the old...
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2013 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 3 of the License, or
13 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (defvar tool-bar-map)
33 (defvar w3m-minor-mode-map)
34
35 (require 'gnus)
36 (require 'gnus-util)
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 (require 'mouse)
49
50 (autoload 'gnus-msg-mail "gnus-msg" nil t)
51 (autoload 'gnus-button-mailto "gnus-msg")
52 (autoload 'gnus-button-reply "gnus-msg" nil t)
53 (autoload 'parse-time-string "parse-time" nil nil)
54 (autoload 'ansi-color-apply-on-region "ansi-color")
55 (autoload 'mm-url-insert-file-contents-external "mm-url")
56 (autoload 'mm-extern-cache-contents "mm-extern")
57
58 (defgroup gnus-article nil
59   "Article display."
60   :link '(custom-manual "(gnus)Article Buffer")
61   :group 'gnus)
62
63 (defgroup gnus-article-treat nil
64   "Treating article parts."
65   :link '(custom-manual "(gnus)Article Hiding")
66   :group 'gnus-article)
67
68 (defgroup gnus-article-hiding nil
69   "Hiding article parts."
70   :link '(custom-manual "(gnus)Article Hiding")
71   :group 'gnus-article)
72
73 (defgroup gnus-article-highlight nil
74   "Article highlighting."
75   :link '(custom-manual "(gnus)Article Highlighting")
76   :group 'gnus-article
77   :group 'gnus-visual)
78
79 (defgroup gnus-article-signature nil
80   "Article signatures."
81   :link '(custom-manual "(gnus)Article Signature")
82   :group 'gnus-article)
83
84 (defgroup gnus-article-headers nil
85   "Article headers."
86   :link '(custom-manual "(gnus)Hiding Headers")
87   :group 'gnus-article)
88
89 (defgroup gnus-article-washing nil
90   "Special commands on articles."
91   :link '(custom-manual "(gnus)Article Washing")
92   :group 'gnus-article)
93
94 (defgroup gnus-article-emphasis nil
95   "Fontisizing articles."
96   :link '(custom-manual "(gnus)Article Fontisizing")
97   :group 'gnus-article)
98
99 (defgroup gnus-article-saving nil
100   "Saving articles."
101   :link '(custom-manual "(gnus)Saving Articles")
102   :group 'gnus-article)
103
104 (defgroup gnus-article-mime nil
105   "Worshiping the MIME wonder."
106   :link '(custom-manual "(gnus)Using MIME")
107   :group 'gnus-article)
108
109 (defgroup gnus-article-buttons nil
110   "Pushable buttons in the article buffer."
111   :link '(custom-manual "(gnus)Article Buttons")
112   :group 'gnus-article)
113
114 (defgroup gnus-article-various nil
115   "Other article options."
116   :link '(custom-manual "(gnus)Misc Article")
117   :group 'gnus-article)
118
119 (defcustom gnus-ignored-headers
120   (mapcar
121    (lambda (header)
122      (concat "^" header ":"))
123    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
124      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
125      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
126      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
127      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
128      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
129      "X-Attribution" "X-Originating-IP" "Delivered-To"
130      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
131      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
132      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
133      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
134      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
135      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
136      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
137      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
138      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
139      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
140      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
141      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
142      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
143      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
144      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
145      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
146      "List-[A-Za-z]+" "X-Listprocessor-Version"
147      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
148      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
149      "X-Received" "Content-length" "X-precedence"
150      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
151      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
152      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
153      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
154      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
155      "X-Content-length" "X-Posting-Agent" "Original-Received"
156      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
157      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
158      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
159      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
160      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
161      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
162      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
163      "Envelope-Sender" "Envelope-Recipients"))
164   "*All headers that start with this regexp will be hidden.
165 This variable can also be a list of regexps of headers to be ignored.
166 If `gnus-visible-headers' is non-nil, this variable will be ignored."
167   :type '(choice regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-hidden-properties '(invisible t intangible t)
261   "Property list to use for hiding text."
262   :type 'sexp
263   :group 'gnus-article-hiding)
264
265 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
266 ;; frames in a session.
267 (defcustom gnus-article-x-face-command
268   (if (featurep 'xemacs)
269       (if (or (gnus-image-type-available-p 'xface)
270               (gnus-image-type-available-p 'pbm))
271           'gnus-display-x-face-in-from
272         "{ echo \
273 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
274 ; uncompface; } | icontopbm | ee -")
275     (if (gnus-image-type-available-p 'pbm)
276         'gnus-display-x-face-in-from
277       "{ echo \
278 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
279 ; uncompface; } | icontopbm | display -"))
280   "*String or function to be executed to display an X-Face header.
281 If it is a string, the command will be executed in a sub-shell
282 asynchronously.  The compressed face will be piped to this command."
283   :type `(choice string
284                  (function-item gnus-display-x-face-in-from)
285                  function)
286   :version "21.1"
287   :group 'gnus-picon
288   :group 'gnus-article-washing)
289
290 (defcustom gnus-article-x-face-too-ugly nil
291   "Regexp matching posters whose face shouldn't be shown automatically."
292   :type '(choice regexp (const nil))
293   :group 'gnus-article-washing)
294
295 (defcustom gnus-article-banner-alist nil
296   "Banner alist for stripping.
297 For example,
298      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
299   :version "21.1"
300   :type '(repeat (cons symbol regexp))
301   :group 'gnus-article-washing)
302
303 (gnus-define-group-parameter
304  banner
305  :variable-document
306  "Alist of regexps (to match group names) and banner."
307  :variable-group gnus-article-washing
308  :parameter-type
309  '(choice :tag "Banner"
310           :value nil
311           (const :tag "Remove signature" signature)
312           (symbol :tag "Item in `gnus-article-banner-alist'" none)
313           regexp
314           (const :tag "None" nil))
315  :parameter-document
316  "If non-nil, specify how to remove `banners' from articles.
317
318 Symbol `signature' means to remove signatures delimited by
319 `gnus-signature-separator'.  Any other symbol is used to look up a
320 regular expression to match the banner in `gnus-article-banner-alist'.
321 A string is used as a regular expression to match the banner
322 directly.")
323
324 (defcustom gnus-article-address-banner-alist nil
325   "Alist of mail addresses and banners.
326 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
327 to match a mail address in the From: header, BANNER is one of a symbol
328 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
329 If ADDRESS matches author's mail address, it will remove things like
330 advertisements.  For example:
331
332 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
333 "
334   :type '(repeat
335           (cons
336            (regexp :tag "Address")
337            (choice :tag "Banner" :value nil
338                    (const :tag "Remove signature" signature)
339                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
340                    regexp
341                    (const :tag "None" nil))))
342   :version "22.1"
343   :group 'gnus-article-washing)
344
345 (defmacro gnus-emphasis-custom-with-format (&rest body)
346   `(let ((format "\
347 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
348 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
349      ,@body))
350
351 (defun gnus-emphasis-custom-value-to-external (value)
352   (gnus-emphasis-custom-with-format
353    (if (consp (car value))
354        (list (format format (car (car value)) (cdr (car value)))
355              2
356              (if (nth 1 value) 2 3)
357              (nth 2 value))
358      value)))
359
360 (defun gnus-emphasis-custom-value-to-internal (value)
361   (gnus-emphasis-custom-with-format
362    (let ((regexp (concat "\\`"
363                          (format (regexp-quote format)
364                                  "\\([^()]+\\)" "\\([^()]+\\)")
365                          "\\'"))
366          pattern)
367      (if (string-match regexp (setq pattern (car value)))
368          (list (cons (match-string 1 pattern) (match-string 2 pattern))
369                (= (nth 2 value) 2)
370                (nth 3 value))
371        value))))
372
373 (defcustom gnus-emphasis-alist
374   (let ((types
375          '(("\\*" "\\*" bold nil 2)
376            ("_" "_" underline)
377            ("/" "/" italic)
378            ("_/" "/_" underline-italic)
379            ("_\\*" "\\*_" underline-bold)
380            ("\\*/" "/\\*" bold-italic)
381            ("_\\*/" "/\\*_" underline-bold-italic))))
382     (nconc
383      (gnus-emphasis-custom-with-format
384       (mapcar (lambda (spec)
385                 (list (format format (car spec) (cadr spec))
386                       (or (nth 3 spec) 2)
387                       (or (nth 4 spec) 3)
388                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
389               types))
390      '(;; I've never seen anyone use this strikethru convention whereas I've
391        ;; several times seen it triggered by normal text.  --Stef
392        ;; Miles suggests that this form is sometimes used but for italics,
393        ;; so maybe we should map it to `italic'.
394        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
395        ;; 2 3 gnus-emphasis-strikethru)
396        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
397         2 3 gnus-emphasis-underline))))
398   "*Alist that says how to fontify certain phrases.
399 Each item looks like this:
400
401   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
402
403 The first element is a regular expression to be matched.  The second
404 is a number that says what regular expression grouping used to find
405 the entire emphasized word.  The third is a number that says what
406 regexp grouping should be displayed and highlighted.  The fourth
407 is the face used for highlighting."
408   :type
409   '(repeat
410     (menu-choice
411      :format "%[Customizing Style%]\n%v"
412      :indent 2
413      (group :tag "Default"
414             :value ("" 0 0 default)
415             :value-create
416             (lambda (widget)
417               (let ((value (widget-get
418                             (cadr (widget-get (widget-get widget :parent)
419                                               :args))
420                             :value)))
421                 (if (not (eq (nth 2 value) 'default))
422                     (widget-put
423                      widget
424                      :value
425                      (gnus-emphasis-custom-value-to-external value))))
426               (widget-group-value-create widget))
427             regexp
428             (integer :format "Match group: %v")
429             (integer :format "Emphasize group: %v")
430             face)
431      (group :tag "Simple"
432             :value (("_" . "_") nil default)
433             (cons :format "%v"
434                   (regexp :format "Start regexp: %v")
435                   (regexp :format "End regexp: %v"))
436             (boolean :format "Show start and end patterns: %[%v%]\n"
437                      :on " On " :off " Off ")
438             face)))
439   :get (lambda (symbol)
440          (mapcar 'gnus-emphasis-custom-value-to-internal
441                  (default-value symbol)))
442   :set (lambda (symbol value)
443          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
444                                      value)))
445   :group 'gnus-article-emphasis)
446
447 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
448   "A regexp to describe whitespace which should not be emphasized.
449 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
450 The former avoids underlining of leading and trailing whitespace,
451 and the latter avoids underlining any whitespace at all."
452   :version "21.1"
453   :group 'gnus-article-emphasis
454   :type 'regexp)
455
456 (defface gnus-emphasis-bold '((t (:bold t)))
457   "Face used for displaying strong emphasized text (*word*)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-italic '((t (:italic t)))
461   "Face used for displaying italic emphasized text (/word/)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline '((t (:underline t)))
465   "Face used for displaying underlined emphasized text (_word_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
469   "Face used for displaying underlined bold emphasized text (_*word*_)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
473   "Face used for displaying underlined italic emphasized text (_/word/_)."
474   :group 'gnus-article-emphasis)
475
476 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
477   "Face used for displaying bold italic emphasized text (/*word*/)."
478   :group 'gnus-article-emphasis)
479
480 (defface gnus-emphasis-underline-bold-italic
481   '((t (:bold t :italic t :underline t)))
482   "Face used for displaying underlined bold italic emphasized text.
483 Example: (_/*word*/_)."
484   :group 'gnus-article-emphasis)
485
486 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
487                                       '((t (:strikethru t)))
488                                     '((t (:strike-through t))))
489   "Face used for displaying strike-through text (-word-)."
490   :group 'gnus-article-emphasis)
491
492 (defface gnus-emphasis-highlight-words
493   '((t (:background "black" :foreground "yellow")))
494   "Face used for displaying highlighted words."
495   :group 'gnus-article-emphasis)
496
497 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
498   "Format for display of Date headers in article bodies.
499 See `format-time-string' for the possible values.
500
501 The variable can also be function, which should return a complete Date
502 header.  The function is called with one argument, the time, which can
503 be fed to `format-time-string'."
504   :type '(choice string function)
505   :link '(custom-manual "(gnus)Article Date")
506   :group 'gnus-article-washing)
507
508 (defcustom gnus-save-all-headers t
509   "*If non-nil, don't remove any headers before saving.
510 This will be overridden by the `:headers' property that the symbol of
511 the saver function, which is specified by `gnus-default-article-saver',
512 might have."
513   :group 'gnus-article-saving
514   :type 'boolean)
515
516 (defcustom gnus-prompt-before-saving 'always
517   "*This variable says how much prompting is to be done when saving articles.
518 If it is nil, no prompting will be done, and the articles will be
519 saved to the default files.  If this variable is `always', each and
520 every article that is saved will be preceded by a prompt, even when
521 saving large batches of articles.  If this variable is neither nil not
522 `always', there the user will be prompted once for a file name for
523 each invocation of the saving commands."
524   :group 'gnus-article-saving
525   :type '(choice (item always)
526                  (item :tag "never" nil)
527                  (sexp :tag "once" :format "%t\n" :value t)))
528
529 (defcustom gnus-saved-headers gnus-visible-headers
530   "Headers to keep if `gnus-save-all-headers' is nil.
531 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
532 If that variable is nil, however, all headers that match this regexp
533 will be kept while the rest will be deleted before saving.  This and
534 `gnus-save-all-headers' will be overridden by the `:headers' property
535 that the symbol of the saver function, which is specified by
536 `gnus-default-article-saver', might have."
537   :group 'gnus-article-saving
538   :type 'regexp)
539
540 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
541 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
542   "A function to save articles in your favorite format.
543 The function will be called by way of the `gnus-summary-save-article'
544 command, and friends such as `gnus-summary-save-article-rmail'.
545
546 Gnus provides the following functions:
547
548 * gnus-summary-save-in-rmail (Rmail format)
549 * gnus-summary-save-in-mail (Unix mail format)
550 * gnus-summary-save-in-folder (MH folder)
551 * gnus-summary-save-in-file (article format)
552 * gnus-summary-save-body-in-file (article body)
553 * gnus-summary-save-in-vm (use VM's folder format)
554 * gnus-summary-write-to-file (article format -- overwrite)
555 * gnus-summary-write-body-to-file (article body -- overwrite)
556 * gnus-summary-save-in-pipe (article format)
557
558 The symbol of each function may have the following properties:
559
560 * :decode
561 The value non-nil means save decoded articles.  This is meaningful
562 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
563 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
564 `gnus-summary-save-in-pipe'.
565
566 * :function
567 The value specifies an alternative function which appends, not
568 overwrites, articles to a file.  This implies that when saving many
569 articles at a time, `gnus-prompt-before-saving' is bound to t and all
570 articles are saved in a single file.  This is meaningful only with
571 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
572
573 * :headers
574 The value specifies the symbol of a variable of which the value
575 specifies headers to be saved.  If it is omitted,
576 `gnus-save-all-headers' and `gnus-saved-headers' control what
577 headers should be saved."
578   :group 'gnus-article-saving
579   :type '(radio (function-item gnus-summary-save-in-rmail)
580                 (function-item gnus-summary-save-in-mail)
581                 (function-item gnus-summary-save-in-folder)
582                 (function-item gnus-summary-save-in-file)
583                 (function-item gnus-summary-save-body-in-file)
584                 (function-item gnus-summary-save-in-vm)
585                 (function-item gnus-summary-write-to-file)
586                 (function-item gnus-summary-write-body-to-file)
587                 (function-item gnus-summary-save-in-pipe)
588                 (function)))
589
590 (defcustom gnus-article-save-coding-system
591   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
592       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
593       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
594       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
595   "Coding system used to save decoded articles to a file.
596
597 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
598 which can safely encode any characters in text.  This is used by the
599 commands including:
600
601 * gnus-summary-save-article-file
602 * gnus-summary-save-article-body-file
603 * gnus-summary-write-article-file
604 * gnus-summary-write-article-body-file
605
606 and the functions to which you may set `gnus-default-article-saver':
607
608 * gnus-summary-save-in-file
609 * gnus-summary-save-body-in-file
610 * gnus-summary-write-to-file
611 * gnus-summary-write-body-to-file
612
613 Those commands and functions save just text displayed in the article
614 buffer to a file if the value of this variable is non-nil.  Note that
615 buttonized MIME parts will be lost in a saved file in that case.
616 Otherwise, raw articles will be saved."
617   :group 'gnus-article-saving
618   :type `(choice
619           :format "%{%t%}:\n %[Value Menu%] %v"
620           (const :tag "Save raw articles" nil)
621           ,@(delq nil
622                   (mapcar
623                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
624                    '((const :tag "UTF-8" utf-8)
625                      (const :tag "iso-2022-7bit" iso-2022-7bit)
626                      (const :tag "Emacs internal" emacs-mule)
627                      (const :tag "escape-quoted" escape-quoted))))
628           (symbol :tag "Coding system")))
629
630 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
631   "A function generating a file name to save articles in Rmail format.
632 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
633   :group 'gnus-article-saving
634   :type 'function)
635
636 (defcustom gnus-mail-save-name 'gnus-plain-save-name
637   "A function generating a file name to save articles in Unix mail format.
638 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
639   :group 'gnus-article-saving
640   :type 'function)
641
642 (defcustom gnus-folder-save-name 'gnus-folder-save-name
643   "A function generating a file name to save articles in MH folder.
644 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
645   :group 'gnus-article-saving
646   :type 'function)
647
648 (defcustom gnus-file-save-name 'gnus-numeric-save-name
649   "A function generating a file name to save articles in article format.
650 The function is called with NEWSGROUP, HEADERS, and optional
651 LAST-FILE."
652   :group 'gnus-article-saving
653   :type 'function)
654
655 (defcustom gnus-split-methods
656   '((gnus-article-archive-name)
657     (gnus-article-nndoc-name))
658   "*Variable used to suggest where articles are to be saved.
659 For instance, if you would like to save articles related to Gnus in
660 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
661 you could set this variable to something like:
662
663  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
664    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
665
666 This variable is an alist where the key is the match and the
667 value is a list of possible files to save in if the match is
668 non-nil.
669
670 If the match is a string, it is used as a regexp match on the
671 article.  If the match is a symbol, that symbol will be funcalled
672 from the buffer of the article to be saved with the newsgroup as the
673 parameter.  If it is a list, it will be evalled in the same buffer.
674
675 If this form or function returns a string, this string will be used as a
676 possible file name; and if it returns a non-nil list, that list will be
677 used as possible file names."
678   :group 'gnus-article-saving
679   :type '(repeat (choice (list :value (fun) function)
680                          (cons :value ("" "") regexp (repeat string))
681                          (sexp :value nil))))
682
683 (defcustom gnus-page-delimiter "^\^L"
684   "*Regexp describing what to use as article page delimiters.
685 The default value is \"^\^L\", which is a form linefeed at the
686 beginning of a line."
687   :type 'regexp
688   :group 'gnus-article-various)
689
690 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
691   "*The format specification for the article mode line.
692 See `gnus-summary-mode-line-format' for a closer description.
693
694 The following additional specs are available:
695
696 %w  The article washing status.
697 %m  The number of MIME parts in the article."
698   :version "24.1"
699   :type 'string
700   :group 'gnus-article-various)
701
702 (defcustom gnus-article-mode-hook nil
703   "*A hook for Gnus article mode."
704   :type 'hook
705   :group 'gnus-article-various)
706
707 (when (featurep 'xemacs)
708   ;; Extracted from gnus-xmas-define in order to preserve user settings
709   (when (fboundp 'turn-off-scroll-in-place)
710     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
711   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
712   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
713
714 (defcustom gnus-article-menu-hook nil
715   "*Hook run after the creation of the article mode menu."
716   :type 'hook
717   :group 'gnus-article-various)
718
719 (defcustom gnus-article-prepare-hook nil
720   "*A hook called after an article has been prepared in the article buffer."
721   :type 'hook
722   :group 'gnus-article-various)
723
724 (defcustom gnus-copy-article-ignored-headers nil
725   "List of headers to be removed when copying an article.
726 Each element is a regular expression."
727   :version "23.1" ;; No Gnus
728   :type '(repeat regexp)
729   :group 'gnus-article-various)
730
731 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
732                         "Gnus 5.10 (Emacs 22.1)")
733
734 (defface gnus-button
735   '((t (:weight bold)))
736   "Face used for highlighting a button in the article buffer."
737   :group 'gnus-article-buttons)
738
739 (defcustom gnus-article-button-face 'gnus-button
740   "Face used for highlighting buttons in the article buffer.
741
742 An article button is a piece of text that you can activate by pressing
743 `RET' or `mouse-2' above it."
744   :type 'face
745   :group 'gnus-article-buttons)
746
747 (defcustom gnus-article-mouse-face 'highlight
748   "Face used for mouse highlighting in the article buffer.
749
750 Article buttons will be displayed in this face when the cursor is
751 above them."
752   :type 'face
753   :group 'gnus-article-buttons)
754
755 (defcustom gnus-signature-face 'gnus-signature
756   "Face used for highlighting a signature in the article buffer.
757 Obsolete; use the face `gnus-signature' for customizations instead."
758   :type 'face
759   :group 'gnus-article-highlight
760   :group 'gnus-article-signature)
761
762 (defface gnus-signature
763   '((t
764      (:italic t)))
765   "Face used for highlighting a signature in the article buffer."
766   :group 'gnus-article-highlight
767   :group 'gnus-article-signature)
768 ;; backward-compatibility alias
769 (put 'gnus-signature-face 'face-alias 'gnus-signature)
770 (put 'gnus-signature-face 'obsolete-face "22.1")
771
772 (defface gnus-header-from
773   '((((class color)
774       (background dark))
775      (:foreground "PaleGreen1"))
776     (((class color)
777       (background light))
778      (:foreground "red3"))
779     (t
780      (:italic t)))
781   "Face used for displaying from headers."
782   :group 'gnus-article-headers
783   :group 'gnus-article-highlight)
784 ;; backward-compatibility alias
785 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
786 (put 'gnus-header-from-face 'obsolete-face "22.1")
787
788 (defface gnus-header-subject
789   '((((class color)
790       (background dark))
791      (:foreground "SeaGreen1"))
792     (((class color)
793       (background light))
794      (:foreground "red4"))
795     (t
796      (:bold t :italic t)))
797   "Face used for displaying subject headers."
798   :group 'gnus-article-headers
799   :group 'gnus-article-highlight)
800 ;; backward-compatibility alias
801 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
802 (put 'gnus-header-subject-face 'obsolete-face "22.1")
803
804 (defface gnus-header-newsgroups
805   '((((class color)
806       (background dark))
807      (:foreground "yellow" :italic t))
808     (((class color)
809       (background light))
810      (:foreground "MidnightBlue" :italic t))
811     (t
812      (:italic t)))
813   "Face used for displaying newsgroups headers.
814 In the default setup this face is only used for crossposted
815 articles."
816   :group 'gnus-article-headers
817   :group 'gnus-article-highlight)
818 ;; backward-compatibility alias
819 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
820 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
821
822 (defface gnus-header-name
823   '((((class color)
824       (background dark))
825      (:foreground "SpringGreen2"))
826     (((class color)
827       (background light))
828      (:foreground "maroon"))
829     (t
830      (:bold t)))
831   "Face used for displaying header names."
832   :group 'gnus-article-headers
833   :group 'gnus-article-highlight)
834 ;; backward-compatibility alias
835 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
836 (put 'gnus-header-name-face 'obsolete-face "22.1")
837
838 (defface gnus-header-content
839   '((((class color)
840       (background dark))
841      (:foreground "SpringGreen1" :italic t))
842     (((class color)
843       (background light))
844      (:foreground "indianred4" :italic t))
845     (t
846      (:italic t)))  "Face used for displaying header content."
847   :group 'gnus-article-headers
848   :group 'gnus-article-highlight)
849 ;; backward-compatibility alias
850 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
851 (put 'gnus-header-content-face 'obsolete-face "22.1")
852
853 (defcustom gnus-header-face-alist
854   '(("From" nil gnus-header-from)
855     ("Subject" nil gnus-header-subject)
856     ("Newsgroups:.*," nil gnus-header-newsgroups)
857     ("" gnus-header-name gnus-header-content))
858   "*Controls highlighting of article headers.
859
860 An alist of the form (HEADER NAME CONTENT).
861
862 HEADER is a regular expression which should match the name of a
863 header and NAME and CONTENT are either face names or nil.
864
865 The name of each header field will be displayed using the face
866 specified by the first element in the list where HEADER matches
867 the header name and NAME is non-nil.  Similarly, the content will
868 be displayed by the first non-nil matching CONTENT face."
869   :group 'gnus-article-headers
870   :group 'gnus-article-highlight
871   :type '(repeat (list (regexp :tag "Header")
872                        (choice :tag "Name"
873                                (item :tag "skip" nil)
874                                (face :value default))
875                        (choice :tag "Content"
876                                (item :tag "skip" nil)
877                                (face :value default)))))
878
879 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
880                                           '((xface . (:face gnus-x-face)))
881                                         '((pbm . (:face gnus-x-face))
882                                           (png . nil)))
883   "Alist of image types and properties applied to Face and X-Face images.
884 Here are examples:
885
886 ;; Specify the altitude of Face images in the From header.
887 \(setq gnus-face-properties-alist
888       '((pbm . (:face gnus-x-face :ascent 80))
889         (png . (:ascent 80))))
890
891 ;; Show Face images as pressed buttons.
892 \(setq gnus-face-properties-alist
893       '((pbm . (:face gnus-x-face :relief -2))
894         (png . (:relief -2))))
895
896 See the manual for the valid properties for various image types.
897 Currently, `pbm' is used for X-Face images and `png' is used for Face
898 images in Emacs.  Only the `:face' property is effective on the `xface'
899 image type in XEmacs if it is built with the libcompface library."
900   :version "23.1" ;; No Gnus
901   :group 'gnus-article-headers
902   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
903
904 (defcustom gnus-article-decode-hook
905   '(article-decode-charset article-decode-encoded-words
906                            article-decode-group-name article-decode-idna-rhs)
907   "*Hook run to decode charsets in articles."
908   :group 'gnus-article-headers
909   :type 'hook)
910
911 (defcustom gnus-display-mime-function 'gnus-display-mime
912   "Function to display MIME articles."
913   :group 'gnus-article-mime
914   :type 'function)
915
916 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
917   "Function used to decode headers.")
918
919 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
920   "Function used to decode addresses.")
921
922 (defvar gnus-article-dumbquotes-map
923   '((?\200 "EUR")
924     (?\202 ",")
925     (?\203 "f")
926     (?\204 ",,")
927     (?\205 "...")
928     (?\213 "<")
929     (?\214 "OE")
930     (?\221 "`")
931     (?\222 "'")
932     (?\223 "``")
933     (?\224 "\"")
934     (?\225 "*")
935     (?\226 "-")
936     (?\227 "--")
937     (?\230 "~")
938     (?\231 "(TM)")
939     (?\233 ">")
940     (?\234 "oe")
941     (?\264 "'"))
942   "Table for MS-to-Latin1 translation.")
943
944 (defcustom gnus-ignored-mime-types nil
945   "List of MIME types that should be ignored by Gnus."
946   :version "21.1"
947   :group 'gnus-article-mime
948   :type '(repeat regexp))
949
950 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
951   "List of MIME types that should not be given buttons when rendered inline.
952 See also `gnus-buttonized-mime-types' which may override this variable.
953 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
954   :version "21.1"
955   :group 'gnus-article-mime
956   :type '(repeat regexp))
957
958 (defcustom gnus-buttonized-mime-types nil
959   "List of MIME types that should be given buttons when rendered inline.
960 If set, this variable overrides `gnus-unbuttonized-mime-types'.
961 To see e.g. security buttons you could set this to
962 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
963 this list to display radio buttons that allow you to choose one of two
964 media types those mails include.  See also `mm-discouraged-alternatives'.
965 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
966   :version "22.1"
967   :group 'gnus-article-mime
968   :type '(repeat regexp))
969
970 (defcustom gnus-inhibit-mime-unbuttonizing nil
971   "If non-nil, all MIME parts get buttons.
972 When nil (the default value), then some MIME parts do not get buttons,
973 as described by the variables `gnus-buttonized-mime-types' and
974 `gnus-unbuttonized-mime-types'."
975   :version "22.1"
976   :group 'gnus-article-mime
977   :type 'boolean)
978
979 (defcustom gnus-body-boundary-delimiter "_"
980   "String used to delimit header and body.
981 This variable is used by `gnus-article-treat-body-boundary' which can
982 be controlled by `gnus-treat-body-boundary'."
983   :version "22.1"
984   :group 'gnus-article-various
985   :type '(choice (item :tag "None" :value nil)
986                  string))
987
988 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
989                                   "/usr/share/picons")
990   "Defines the location of the faces database.
991 For information on obtaining this database of pretty pictures, please
992 see http://www.cs.indiana.edu/picons/ftp/index.html"
993   :version "22.1"
994   :type '(repeat directory)
995   :link '(url-link :tag "download"
996                    "http://www.cs.indiana.edu/picons/ftp/index.html")
997   :link '(custom-manual "(gnus)Picons")
998   :group 'gnus-picon)
999
1000 (defun gnus-picons-installed-p ()
1001   "Say whether picons are installed on your machine."
1002   (let ((installed nil))
1003     (dolist (database gnus-picon-databases)
1004       (when (file-exists-p database)
1005         (setq installed t)))
1006     installed))
1007
1008 (defcustom gnus-article-mime-part-function nil
1009   "Function called with a MIME handle as the argument.
1010 This is meant for people who want to do something automatic based
1011 on parts -- for instance, adding Vcard info to a database."
1012   :group 'gnus-article-mime
1013   :type '(choice (const nil)
1014                  function))
1015
1016 (defcustom gnus-mime-multipart-functions nil
1017   "An alist of MIME types to functions to display them."
1018   :version "21.1"
1019   :group 'gnus-article-mime
1020   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1021
1022 (defcustom gnus-article-date-headers '(combined-lapsed)
1023   "A list of Date header formats to display.
1024 Valid formats are `ut' (universal time), `local' (local time
1025 zone), `english' (readable English), `lapsed' (elapsed time),
1026 `combined-lapsed' (both the original date and the elapsed time),
1027 `original' (the original date header), `iso8601' (ISO8601
1028 format), and `user-defined' (a user-defined format defined by the
1029 `gnus-article-time-format' variable).
1030
1031 You have as many date headers as you want in the article buffer.
1032 Some of these headers are updated automatically.  See
1033 `gnus-article-update-date-headers' for details."
1034   :version "24.1"
1035   :group 'gnus-article-headers
1036   :type '(set
1037           (const :tag "Universal time (UT)" ut)
1038           (const :tag "Local time zone" local)
1039           (const :tag "Readable English" english)
1040           (const :tag "Elapsed time" lapsed)
1041           (const :tag "Original and elapsed time" combined-lapsed)
1042           (const :tag "Original date header" original)
1043           (const :tag "ISO8601 format" iso8601)
1044           (const :tag "User-defined" user-defined)))
1045
1046 (defcustom gnus-article-update-date-headers nil
1047   "A number that says how often to update the date header (in seconds).
1048 If nil, don't update it at all."
1049   :version "24.1"
1050   :group 'gnus-article-headers
1051   :type '(choice
1052           (item :tag "Don't update" :value nil)
1053           integer))
1054
1055 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1056   "Function called with a MIME handle as the argument.
1057 This is meant for people who want to view first matched part.
1058 For `undisplayed-alternative' (default), the first undisplayed
1059 part or alternative part is used.  For `undisplayed', the first
1060 undisplayed part is used.  For a function, the first part which
1061 the function return t is used.  For nil, the first part is
1062 used."
1063   :version "21.1"
1064   :group 'gnus-article-mime
1065   :type '(choice
1066           (item :tag "first" :value nil)
1067           (item :tag "undisplayed" :value undisplayed)
1068           (item :tag "undisplayed or alternative"
1069                 :value undisplayed-alternative)
1070           (function)))
1071
1072 (defcustom gnus-mime-action-alist
1073   '(("save to file" . gnus-mime-save-part)
1074     ("save and strip" . gnus-mime-save-part-and-strip)
1075     ("replace with file" . gnus-mime-replace-part)
1076     ("delete part" . gnus-mime-delete-part)
1077     ("display as text" . gnus-mime-inline-part)
1078     ("view the part" . gnus-mime-view-part)
1079     ("pipe to command" . gnus-mime-pipe-part)
1080     ("toggle display" . gnus-article-press-button)
1081     ("toggle display" . gnus-article-view-part-as-charset)
1082     ("view as type" . gnus-mime-view-part-as-type)
1083     ("view internally" . gnus-mime-view-part-internally)
1084     ("view externally" . gnus-mime-view-part-externally))
1085   "An alist of actions that run on the MIME attachment."
1086   :group 'gnus-article-mime
1087   :type '(repeat (cons (string :tag "name")
1088                        (function))))
1089
1090 (defcustom gnus-auto-select-part 1
1091   "Advance to next MIME part when deleting or stripping parts.
1092
1093 When 0, point will be placed on the same part as before.  When
1094 positive (negative), move point forward (backwards) this many
1095 parts.  When nil, redisplay article."
1096   :version "23.1" ;; No Gnus
1097   :group 'gnus-article-mime
1098   :type '(choice (const nil :tag "Redisplay article.")
1099                  (const 1 :tag "Next part.")
1100                  (const 0 :tag "Current part.")
1101                  integer))
1102
1103 ;;;
1104 ;;; The treatment variables
1105 ;;;
1106
1107 (defvar gnus-part-display-hook nil
1108   "Hook called on parts that are to receive treatment.")
1109
1110 (defvar gnus-article-treat-custom
1111   '(choice (const :tag "Off" nil)
1112            (const :tag "On" t)
1113            (const :tag "Header" head)
1114            (const :tag "First" first)
1115            (const :tag "Last" last)
1116            (integer :tag "Less")
1117            (repeat :tag "Groups" regexp)
1118            (sexp :tag "Predicate")))
1119
1120 (defvar gnus-article-treat-head-custom
1121   '(choice (const :tag "Off" nil)
1122            (const :tag "Header" head)))
1123
1124 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1125                                    "text/x-patch" "text/html")
1126   "Part types eligible for treatment.")
1127
1128 (defvar gnus-inhibit-treatment nil
1129   "Whether to inhibit treatment.")
1130
1131 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1132   "Highlight the signature.
1133 Valid values are nil, t, `head', `first', `last', an integer or a
1134 predicate.  See Info node `(gnus)Customizing Articles'."
1135   :group 'gnus-article-treat
1136   :link '(custom-manual "(gnus)Customizing Articles")
1137   :type gnus-article-treat-custom)
1138 (put 'gnus-treat-highlight-signature 'highlight t)
1139
1140 (defcustom gnus-treat-buttonize 100000
1141   "Add buttons.
1142 Valid values are nil, t, `head', `first', `last', an integer or a
1143 predicate.  See Info node `(gnus)Customizing Articles'."
1144   :group 'gnus-article-treat
1145   :link '(custom-manual "(gnus)Customizing Articles")
1146   :type gnus-article-treat-custom)
1147 (put 'gnus-treat-buttonize 'highlight t)
1148
1149 (defcustom gnus-treat-buttonize-head 'head
1150   "Add buttons to the head.
1151 Valid values are nil, t, `head', `first', `last', an integer or a
1152 predicate.  See Info node `(gnus)Customizing Articles'."
1153   :group 'gnus-article-treat
1154   :link '(custom-manual "(gnus)Customizing Articles")
1155   :type gnus-article-treat-head-custom)
1156 (put 'gnus-treat-buttonize-head 'highlight t)
1157
1158 (defcustom gnus-treat-date 'head
1159   "Display dates according to the `gnus-article-date-headers' variable.
1160 Valid values are nil, t, `head', `first', `last', an integer or a
1161 predicate.  See Info node `(gnus)Customizing Articles'."
1162   :version "24.1"
1163   :group 'gnus-article-treat
1164   :link '(custom-manual "(gnus)Customizing Articles")
1165   :type gnus-article-treat-head-custom)
1166
1167 (defcustom gnus-treat-emphasize 50000
1168   "Emphasize text.
1169 Valid values are nil, t, `head', `first', `last', an integer or a
1170 predicate.  See Info node `(gnus)Customizing Articles'."
1171   :group 'gnus-article-treat
1172   :link '(custom-manual "(gnus)Customizing Articles")
1173   :type gnus-article-treat-custom)
1174 (put 'gnus-treat-emphasize 'highlight t)
1175
1176 (defcustom gnus-treat-strip-cr nil
1177   "Remove carriage returns.
1178 Valid values are nil, t, `head', `first', `last', an integer or a
1179 predicate.  See Info node `(gnus)Customizing Articles'."
1180   :version "22.1"
1181   :group 'gnus-article-treat
1182   :link '(custom-manual "(gnus)Customizing Articles")
1183   :type gnus-article-treat-custom)
1184
1185 (defcustom gnus-treat-unsplit-urls nil
1186   "Remove newlines from within URLs.
1187 Valid values are nil, t, `head', `first', `last', an integer or a
1188 predicate.  See Info node `(gnus)Customizing Articles'."
1189   :version "22.1"
1190   :group 'gnus-article-treat
1191   :link '(custom-manual "(gnus)Customizing Articles")
1192   :type gnus-article-treat-custom)
1193
1194 (defcustom gnus-treat-leading-whitespace nil
1195   "Remove leading whitespace in headers.
1196 Valid values are nil, t, `head', `first', `last', an integer or a
1197 predicate.  See Info node `(gnus)Customizing Articles'."
1198   :version "22.1"
1199   :group 'gnus-article-treat
1200   :link '(custom-manual "(gnus)Customizing Articles")
1201   :type gnus-article-treat-custom)
1202
1203 (defcustom gnus-treat-hide-headers 'head
1204   "Hide headers.
1205 Valid values are nil, t, `head', `first', `last', an integer or a
1206 predicate.  See Info node `(gnus)Customizing Articles'."
1207   :group 'gnus-article-treat
1208   :link '(custom-manual "(gnus)Customizing Articles")
1209   :type gnus-article-treat-head-custom)
1210
1211 (defcustom gnus-treat-hide-boring-headers nil
1212   "Hide boring headers.
1213 Valid values are nil, t, `head', `first', `last', an integer or a
1214 predicate.  See Info node `(gnus)Customizing Articles'."
1215   :group 'gnus-article-treat
1216   :link '(custom-manual "(gnus)Customizing Articles")
1217   :type gnus-article-treat-head-custom)
1218
1219 (defcustom gnus-treat-hide-signature nil
1220   "Hide the signature.
1221 Valid values are nil, t, `head', `first', `last', an integer or a
1222 predicate.  See Info node `(gnus)Customizing Articles'."
1223   :group 'gnus-article-treat
1224   :link '(custom-manual "(gnus)Customizing Articles")
1225   :type gnus-article-treat-custom)
1226
1227 (defcustom gnus-treat-fill-article nil
1228   "Fill the article.
1229 Valid values are nil, t, `head', `first', `last', an integer or a
1230 predicate.  See Info node `(gnus)Customizing Articles'."
1231   :group 'gnus-article-treat
1232   :link '(custom-manual "(gnus)Customizing Articles")
1233   :type gnus-article-treat-custom)
1234
1235 (defcustom gnus-treat-hide-citation nil
1236   "Hide cited text.
1237 Valid values are nil, t, `head', `first', `last', an integer or a
1238 predicate.  See Info node `(gnus)Customizing Articles'.
1239
1240 See `gnus-article-highlight-citation' for variables used to
1241 control what it hides."
1242   :group 'gnus-article-treat
1243   :link '(custom-manual "(gnus)Customizing Articles")
1244   :type gnus-article-treat-custom)
1245
1246 (defcustom gnus-treat-hide-citation-maybe nil
1247   "Hide cited text according to certain conditions.
1248 Valid values are nil, t, `head', `first', `last', an integer or a
1249 predicate.  See Info node `(gnus)Customizing Articles'.
1250
1251 See `gnus-cite-hide-percentage' and `gnus-cite-hide-absolute' for
1252 how to control what it hides."
1253   :group 'gnus-article-treat
1254   :link '(custom-manual "(gnus)Customizing Articles")
1255   :type gnus-article-treat-custom)
1256
1257 (defcustom gnus-treat-strip-list-identifiers 'head
1258   "Strip list identifiers from `gnus-list-identifiers`.
1259 Valid values are nil, t, `head', `first', `last', an integer or a
1260 predicate.  See Info node `(gnus)Customizing Articles'."
1261   :version "21.1"
1262   :group 'gnus-article-treat
1263   :link '(custom-manual "(gnus)Customizing Articles")
1264   :type gnus-article-treat-custom)
1265
1266 (gnus-define-group-parameter
1267  list-identifier
1268  :variable-document
1269  "Alist of regexps and correspondent identifiers."
1270  :variable-group gnus-article-washing
1271  :parameter-type
1272  '(choice :tag "Identifier"
1273           :value nil
1274           (symbol :tag "Item in `gnus-list-identifiers'" none)
1275           regexp
1276           (const :tag "None" nil))
1277  :parameter-document
1278  "If non-nil, specify how to remove `identifiers' from articles' subject.
1279
1280 Any symbol is used to look up a regular expression to match the
1281 banner in `gnus-list-identifiers'.  A string is used as a regular
1282 expression to match the identifier directly.")
1283
1284 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1285                         "Gnus 5.10 (Emacs 22.1)")
1286
1287 (defcustom gnus-treat-strip-pem nil
1288   "Strip PEM signatures.
1289 Valid values are nil, t, `head', `first', `last', an integer or a
1290 predicate.  See Info node `(gnus)Customizing Articles'."
1291   :group 'gnus-article-treat
1292   :link '(custom-manual "(gnus)Customizing Articles")
1293   :type gnus-article-treat-custom)
1294
1295 (defcustom gnus-treat-strip-banner t
1296   "Strip banners from articles.
1297 The banner to be stripped is specified in the `banner' group parameter.
1298 Valid values are nil, t, `head', `first', `last', an integer or a
1299 predicate.  See Info node `(gnus)Customizing Articles'."
1300   :group 'gnus-article-treat
1301   :link '(custom-manual "(gnus)Customizing Articles")
1302   :type gnus-article-treat-custom)
1303
1304 (defcustom gnus-treat-highlight-headers 'head
1305   "Highlight the headers.
1306 Valid values are nil, t, `head', `first', `last', an integer or a
1307 predicate.  See Info node `(gnus)Customizing Articles'."
1308   :group 'gnus-article-treat
1309   :link '(custom-manual "(gnus)Customizing Articles")
1310   :type gnus-article-treat-head-custom)
1311 (put 'gnus-treat-highlight-headers 'highlight t)
1312
1313 (defcustom gnus-treat-highlight-citation t
1314   "Highlight cited text.
1315 Valid values are nil, t, `head', `first', `last', an integer or a
1316 predicate.  See Info node `(gnus)Customizing Articles'."
1317   :group 'gnus-article-treat
1318   :link '(custom-manual "(gnus)Customizing Articles")
1319   :type gnus-article-treat-custom)
1320 (put 'gnus-treat-highlight-citation 'highlight t)
1321
1322 (defcustom gnus-treat-strip-headers-in-body t
1323   "Strip the X-No-Archive header line from the beginning of the body.
1324 Valid values are nil, t, `head', `first', `last', an integer or a
1325 predicate.  See Info node `(gnus)Customizing Articles'."
1326   :version "21.1"
1327   :group 'gnus-article-treat
1328   :link '(custom-manual "(gnus)Customizing Articles")
1329   :type gnus-article-treat-custom)
1330
1331 (defcustom gnus-treat-strip-trailing-blank-lines nil
1332   "Strip trailing blank lines.
1333 Valid values are nil, t, `head', `first', `last', an integer or a
1334 predicate.  See Info node `(gnus)Customizing Articles'.
1335
1336 When set to t, it also strips trailing blanks in all MIME parts.
1337 Consider to use `last' instead."
1338   :group 'gnus-article-treat
1339   :link '(custom-manual "(gnus)Customizing Articles")
1340   :type gnus-article-treat-custom)
1341
1342 (defcustom gnus-treat-strip-leading-blank-lines nil
1343   "Strip leading blank lines.
1344 Valid values are nil, t, `head', `first', `last', an integer or a
1345 predicate.  See Info node `(gnus)Customizing Articles'.
1346
1347 When set to t, it also strips trailing blanks in all MIME parts."
1348   :group 'gnus-article-treat
1349   :link '(custom-manual "(gnus)Customizing Articles")
1350   :type gnus-article-treat-custom)
1351
1352 (defcustom gnus-treat-strip-multiple-blank-lines nil
1353   "Strip multiple blank lines.
1354 Valid values are nil, t, `head', `first', `last', an integer or a
1355 predicate.  See Info node `(gnus)Customizing Articles'."
1356   :group 'gnus-article-treat
1357   :link '(custom-manual "(gnus)Customizing Articles")
1358   :type gnus-article-treat-custom)
1359
1360 (defcustom gnus-treat-unfold-headers 'head
1361   "Unfold folded header lines.
1362 Valid values are nil, t, `head', `first', `last', an integer or a
1363 predicate.  See Info node `(gnus)Customizing Articles'."
1364   :version "22.1"
1365   :group 'gnus-article-treat
1366   :link '(custom-manual "(gnus)Customizing Articles")
1367   :type gnus-article-treat-custom)
1368
1369 (defcustom gnus-article-unfold-long-headers nil
1370   "If non-nil, allow unfolding headers even if the header is long.
1371 If it is a regexp, only long headers matching this regexp are unfolded.
1372 If it is t, all long headers are unfolded.
1373
1374 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1375   :version "23.1" ;; No Gnus
1376   :group 'gnus-article-treat
1377   :type '(choice (const nil)
1378                  (const :tag "all" t)
1379                  (regexp)))
1380
1381 (defcustom gnus-treat-fold-headers nil
1382   "Fold headers.
1383 Valid values are nil, t, `head', `first', `last', an integer or a
1384 predicate.  See Info node `(gnus)Customizing Articles'."
1385   :version "22.1"
1386   :group 'gnus-article-treat
1387   :link '(custom-manual "(gnus)Customizing Articles")
1388   :type gnus-article-treat-custom)
1389
1390 (defcustom gnus-treat-fold-newsgroups 'head
1391   "Fold the Newsgroups and Followup-To headers.
1392 Valid values are nil, t, `head', `first', `last', an integer or a
1393 predicate.  See Info node `(gnus)Customizing Articles'."
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-overstrike t
1400   "Treat overstrike highlighting.
1401 Valid values are nil, t, `head', `first', `last', an integer or a
1402 predicate.  See Info node `(gnus)Customizing Articles'."
1403   :group 'gnus-article-treat
1404   :link '(custom-manual "(gnus)Customizing Articles")
1405   :type gnus-article-treat-custom)
1406 (put 'gnus-treat-overstrike 'highlight t)
1407
1408 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1409   "Treat ANSI SGR control sequences.
1410 Valid values are nil, t, `head', `first', `last', an integer or a
1411 predicate.  See Info node `(gnus)Customizing Articles'."
1412   :group 'gnus-article-treat
1413   :link '(custom-manual "(gnus)Customizing Articles")
1414   :type gnus-article-treat-custom)
1415
1416 (make-obsolete-variable 'gnus-treat-display-xface
1417                         'gnus-treat-display-x-face "Emacs 22.1")
1418
1419 (defcustom gnus-treat-display-x-face
1420   (and (not noninteractive)
1421        (gnus-image-type-available-p 'xbm)
1422        (if (featurep 'xemacs)
1423            (featurep 'xface)
1424          (condition-case nil
1425              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1426                   (executable-find "icontopbm"))
1427            ;; shell-command-to-string may signal an error, e.g. if
1428            ;; shell-file-name is not found.
1429            (error nil)))
1430        'head)
1431   "Display X-Face headers.
1432 Valid values are nil and `head'.
1433 See Info node `(gnus)Customizing Articles' and Info node
1434 `(gnus)X-Face' for details."
1435   :group 'gnus-article-treat
1436   :version "21.1"
1437   :link '(custom-manual "(gnus)Customizing Articles")
1438   :link '(custom-manual "(gnus)X-Face")
1439   :type gnus-article-treat-head-custom
1440   :set (lambda (symbol value)
1441          (set-default
1442           symbol
1443           (cond ((or (boundp symbol) (get symbol 'saved-value))
1444                  value)
1445                 ((boundp 'gnus-treat-display-xface)
1446                  (message "\
1447 ** gnus-treat-display-xface is an obsolete variable;\
1448  use gnus-treat-display-x-face instead")
1449                  (default-value 'gnus-treat-display-xface))
1450                 ((get 'gnus-treat-display-xface 'saved-value)
1451                  (message "\
1452 ** gnus-treat-display-xface is an obsolete variable;\
1453  use gnus-treat-display-x-face instead")
1454                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1455                 (t
1456                  value)))))
1457 (put 'gnus-treat-display-x-face 'highlight t)
1458
1459 (defcustom gnus-treat-display-face
1460   (and (not noninteractive)
1461        (gnus-image-type-available-p 'png)
1462        'head)
1463   "Display Face headers.
1464 Valid values are nil, t, `head', `first', `last', an integer or a
1465 predicate.  See Info node `(gnus)Customizing Articles' and Info
1466 node `(gnus)Face' for details."
1467   :group 'gnus-article-treat
1468   :version "22.1"
1469   :link '(custom-manual "(gnus)Customizing Articles")
1470   :link '(custom-manual "(gnus)X-Face")
1471   :type gnus-article-treat-head-custom)
1472 (put 'gnus-treat-display-face 'highlight t)
1473
1474 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1475   "Display smileys.
1476 Valid values are nil, t, `head', `first', `last', an integer or a
1477 predicate.  See Info node `(gnus)Customizing Articles' and Info
1478 node `(gnus)Smileys' for details."
1479   :group 'gnus-article-treat
1480   :version "21.1"
1481   :link '(custom-manual "(gnus)Customizing Articles")
1482   :link '(custom-manual "(gnus)Smileys")
1483   :type gnus-article-treat-custom)
1484 (put 'gnus-treat-display-smileys 'highlight t)
1485
1486 (defcustom gnus-treat-from-picon
1487   (if (and (gnus-image-type-available-p 'xpm)
1488            (gnus-picons-installed-p))
1489       'head nil)
1490   "Display picons in the From header.
1491 Valid values are nil, t, `head', `first', `last', an integer or a
1492 predicate.  See Info node `(gnus)Customizing Articles' and Info
1493 node `(gnus)Picons' for details."
1494   :version "22.1"
1495   :group 'gnus-article-treat
1496   :group 'gnus-picon
1497   :link '(custom-manual "(gnus)Customizing Articles")
1498   :link '(custom-manual "(gnus)Picons")
1499   :type gnus-article-treat-head-custom)
1500 (put 'gnus-treat-from-picon 'highlight t)
1501
1502 (defcustom gnus-treat-mail-picon
1503   (if (and (gnus-image-type-available-p 'xpm)
1504            (gnus-picons-installed-p))
1505       'head nil)
1506   "Display picons in To and Cc headers.
1507 Valid values are nil, t, `head', `first', `last', an integer or a
1508 predicate.  See Info node `(gnus)Customizing Articles' and Info
1509 node `(gnus)Picons' for details."
1510   :version "22.1"
1511   :group 'gnus-article-treat
1512   :group 'gnus-picon
1513   :link '(custom-manual "(gnus)Customizing Articles")
1514   :link '(custom-manual "(gnus)Picons")
1515   :type gnus-article-treat-head-custom)
1516 (put 'gnus-treat-mail-picon 'highlight t)
1517
1518 (defcustom gnus-treat-newsgroups-picon
1519   (if (and (gnus-image-type-available-p 'xpm)
1520            (gnus-picons-installed-p))
1521       'head nil)
1522   "Display picons in the Newsgroups and Followup-To headers.
1523 Valid values are nil, t, `head', `first', `last', an integer or a
1524 predicate.  See Info node `(gnus)Customizing Articles' and Info
1525 node `(gnus)Picons' for details."
1526   :version "22.1"
1527   :group 'gnus-article-treat
1528   :group 'gnus-picon
1529   :link '(custom-manual "(gnus)Customizing Articles")
1530   :link '(custom-manual "(gnus)Picons")
1531   :type gnus-article-treat-head-custom)
1532 (put 'gnus-treat-newsgroups-picon 'highlight t)
1533
1534 (defcustom gnus-treat-from-gravatar nil
1535   "Display gravatars in the From header.
1536 Valid values are nil, t, `head', `first', `last', an integer or a
1537 predicate.  See Info node `(gnus)Customizing Articles' and Info
1538 node `(gnus)Gravatars' for details."
1539   :version "24.1"
1540   :group 'gnus-article-treat
1541   :group 'gnus-gravatar
1542   :link '(custom-manual "(gnus)Customizing Articles")
1543   :link '(custom-manual "(gnus)Gravatars")
1544   :type gnus-article-treat-head-custom)
1545 (put 'gnus-treat-from-gravatar 'highlight t)
1546
1547 (defcustom gnus-treat-mail-gravatar nil
1548   "Display gravatars in To and Cc headers.
1549 Valid values are nil, t, `head', `first', `last', an integer or a
1550 predicate.  See Info node `(gnus)Customizing Articles' and Info
1551 node `(gnus)Gravatars' for details."
1552   :version "24.1"
1553   :group 'gnus-article-treat
1554   :group 'gnus-gravatar
1555   :link '(custom-manual "(gnus)Customizing Articles")
1556   :link '(custom-manual "(gnus)Gravatars")
1557   :type gnus-article-treat-head-custom)
1558 (put 'gnus-treat-mail-gravatar 'highlight t)
1559
1560 (defcustom gnus-treat-body-boundary
1561   (if (or gnus-treat-newsgroups-picon
1562           gnus-treat-mail-picon
1563           gnus-treat-from-picon
1564           gnus-treat-from-gravatar
1565           gnus-treat-mail-gravatar)
1566       ;; If there's much decoration, the user might prefer a boundary.
1567       'head
1568     nil)
1569   "Draw a boundary at the end of the headers.
1570 Valid values are nil and `head'.
1571 See Info node `(gnus)Customizing Articles' for details."
1572   :version "22.1"
1573   :group 'gnus-article-treat
1574   :link '(custom-manual "(gnus)Customizing Articles")
1575   :type gnus-article-treat-head-custom)
1576
1577 (defcustom gnus-treat-capitalize-sentences nil
1578   "Capitalize sentence-starting words.
1579 Valid values are nil, t, `head', `first', `last', an integer or a
1580 predicate.  See Info node `(gnus)Customizing Articles'."
1581   :version "21.1"
1582   :group 'gnus-article-treat
1583   :link '(custom-manual "(gnus)Customizing Articles")
1584   :type gnus-article-treat-custom)
1585
1586 (defcustom gnus-treat-wash-html nil
1587   "Format as HTML.
1588 Valid values are nil, t, `head', `first', `last', an integer or a
1589 predicate.  See Info node `(gnus)Customizing Articles'."
1590   :version "22.1"
1591   :group 'gnus-article-treat
1592   :link '(custom-manual "(gnus)Customizing Articles")
1593   :type gnus-article-treat-custom)
1594
1595 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1596   "Fill long lines.
1597 Valid values are nil, t, `head', `first', `last', an integer or a
1598 predicate.  See Info node `(gnus)Customizing Articles'."
1599   :version "24.1"
1600   :group 'gnus-article-treat
1601   :link '(custom-manual "(gnus)Customizing Articles")
1602   :type gnus-article-treat-custom)
1603
1604 (defcustom gnus-treat-x-pgp-sig nil
1605   "Verify X-PGP-Sig.
1606 To automatically treat X-PGP-Sig, set it to head.
1607 Valid values are nil, t, `head', `first', `last', an integer or a
1608 predicate.  See Info node `(gnus)Customizing Articles'."
1609   :version "22.1"
1610   :group 'gnus-article-treat
1611   :group 'mime-security
1612   :link '(custom-manual "(gnus)Customizing Articles")
1613   :type gnus-article-treat-custom)
1614
1615 (defvar gnus-article-encrypt-protocol-alist
1616   '(("PGP" . mml2015-self-encrypt)))
1617
1618 ;; Set to nil if more than one protocol added to
1619 ;; gnus-article-encrypt-protocol-alist.
1620 (defcustom gnus-article-encrypt-protocol "PGP"
1621   "The protocol used for encrypt articles.
1622 It is a string, such as \"PGP\". If nil, ask user."
1623   :version "22.1"
1624   :type 'string
1625   :group 'mime-security)
1626
1627 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1628                               (mm-coding-system-p 'utf-8)
1629                               (executable-find idna-program))
1630   "Whether IDNA decoding of headers is used when viewing messages.
1631 This requires GNU Libidn, and by default only enabled if it is found."
1632   :version "22.1"
1633   :group 'gnus-article-headers
1634   :type 'boolean)
1635
1636 (defcustom gnus-article-over-scroll nil
1637   "If non-nil, allow scrolling the article buffer even when there no more text."
1638   :version "22.1"
1639   :group 'gnus-article
1640   :type 'boolean)
1641
1642 (defcustom gnus-inhibit-images nil
1643   "Non-nil means inhibit displaying of images inline in the article body."
1644   :version "24.1"
1645   :group 'gnus-article
1646   :type 'boolean)
1647
1648 (defcustom gnus-blocked-images 'gnus-block-private-groups
1649   "Images that have URLs matching this regexp will be blocked.
1650 This can also be a function to be evaluated.  If so, it will be
1651 called with the group name as the parameter, and should return a
1652 regexp."
1653   :version "24.1"
1654   :group 'gnus-art
1655   :type '(choice regexp function))
1656
1657 ;;; Internal variables
1658
1659 (defvar gnus-english-month-names
1660   '("January" "February" "March" "April" "May" "June" "July" "August"
1661     "September" "October" "November" "December"))
1662
1663 (defvar article-goto-body-goes-to-point-min-p nil)
1664 (defvar gnus-article-wash-types nil)
1665 (defvar gnus-article-emphasis-alist nil)
1666 (defvar gnus-article-image-alist nil)
1667
1668 (defvar gnus-article-mime-handle-alist-1 nil)
1669 (defvar gnus-treatment-function-alist
1670   '((gnus-treat-strip-cr gnus-article-remove-cr)
1671     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1672     (gnus-treat-strip-banner gnus-article-strip-banner)
1673     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1674     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1675     (gnus-treat-buttonize gnus-article-add-buttons)
1676     (gnus-treat-fill-article gnus-article-fill-cited-article)
1677     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1678     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1679     (gnus-treat-display-x-face gnus-article-display-x-face)
1680     (gnus-treat-display-face gnus-article-display-face)
1681     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1682     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1683     (gnus-treat-hide-signature gnus-article-hide-signature)
1684     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1685     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1686     (gnus-treat-from-picon gnus-treat-from-picon)
1687     (gnus-treat-mail-picon gnus-treat-mail-picon)
1688     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1689     (gnus-treat-strip-pem gnus-article-hide-pem)
1690     (gnus-treat-date gnus-article-treat-date)
1691     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1692     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1693     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1694     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1695     (gnus-treat-strip-trailing-blank-lines
1696      gnus-article-remove-trailing-blank-lines)
1697     (gnus-treat-strip-leading-blank-lines
1698      gnus-article-strip-leading-blank-lines)
1699     (gnus-treat-strip-multiple-blank-lines
1700      gnus-article-strip-multiple-blank-lines)
1701     (gnus-treat-overstrike gnus-article-treat-overstrike)
1702     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1703     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1704     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1705     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1706     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1707     (gnus-treat-display-smileys gnus-treat-smiley)
1708     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1709     (gnus-treat-wash-html gnus-article-wash-html)
1710     (gnus-treat-emphasize gnus-article-emphasize)
1711     (gnus-treat-hide-citation gnus-article-hide-citation)
1712     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1713     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1714     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1715
1716 (defvar gnus-article-mime-handle-alist nil)
1717 (defvar article-lapsed-timer nil)
1718 (defvar gnus-article-current-summary nil)
1719
1720 (defvar gnus-article-mode-syntax-table
1721   (let ((table (copy-syntax-table text-mode-syntax-table)))
1722     ;; This causes the citation match run O(2^n).
1723     ;; (modify-syntax-entry ?- "w" table)
1724     (modify-syntax-entry ?> ")<" table)
1725     (modify-syntax-entry ?< "(>" table)
1726     ;; make M-. in article buffers work for `foo' strings
1727     (modify-syntax-entry ?' " " table)
1728     (modify-syntax-entry ?` " " table)
1729     table)
1730   "Syntax table used in article mode buffers.
1731 Initialized from `text-mode-syntax-table.")
1732
1733 (defvar gnus-save-article-buffer nil)
1734
1735 (defvar gnus-number-of-articles-to-be-saved nil)
1736
1737 (defvar gnus-inhibit-hiding nil)
1738
1739 (defvar gnus-article-edit-mode nil)
1740
1741 ;;; Macros for dealing with the article buffer.
1742
1743 (defmacro gnus-with-article-headers (&rest forms)
1744   `(with-current-buffer gnus-article-buffer
1745      (save-restriction
1746        (let ((inhibit-read-only t)
1747              (inhibit-point-motion-hooks t)
1748              (case-fold-search t))
1749          (article-narrow-to-head)
1750          ,@forms))))
1751
1752 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1753 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1754
1755 (defmacro gnus-with-article-buffer (&rest forms)
1756   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1757      (with-current-buffer gnus-article-buffer
1758        (let ((inhibit-read-only t))
1759          ,@forms))))
1760
1761 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1762 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1763
1764 (defun gnus-article-goto-header (header)
1765   "Go to HEADER, which is a regular expression."
1766   (re-search-forward (concat "^\\(" header "\\):") nil t))
1767
1768 (defsubst gnus-article-hide-text (b e props)
1769   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1770   (gnus-add-text-properties-when 'article-type nil b e props)
1771   (when (memq 'intangible props)
1772     (put-text-property
1773      (max (1- b) (point-min))
1774      b 'intangible (cddr (memq 'intangible props)))))
1775
1776 (defsubst gnus-article-unhide-text (b e)
1777   "Remove hidden text properties from region between B and E."
1778   (remove-text-properties b e gnus-hidden-properties)
1779   (when (memq 'intangible gnus-hidden-properties)
1780     (put-text-property (max (1- b) (point-min))
1781                        b 'intangible nil)))
1782
1783 (defun gnus-article-hide-text-type (b e type)
1784   "Hide text of TYPE between B and E."
1785   (gnus-add-wash-type type)
1786   (gnus-article-hide-text
1787    b e (cons 'article-type (cons type gnus-hidden-properties))))
1788
1789 (defun gnus-article-unhide-text-type (b e type)
1790   "Unhide text of TYPE between B and E."
1791   (gnus-delete-wash-type type)
1792   (remove-text-properties
1793    b e (cons 'article-type (cons type gnus-hidden-properties)))
1794   (when (memq 'intangible gnus-hidden-properties)
1795     (put-text-property (max (1- b) (point-min))
1796                        b 'intangible nil)))
1797
1798 (defun gnus-article-delete-text-of-type (type)
1799   "Delete text of TYPE in the current buffer."
1800   (save-excursion
1801     (let ((b (point-min)))
1802       (if (eq type 'multipart)
1803           ;; Remove MIME buttons associated with multipart/alternative parts.
1804           (progn
1805             (goto-char b)
1806             (while (if (get-text-property (point) 'gnus-part)
1807                        (setq b (point))
1808                      (when (setq b (next-single-property-change (point)
1809                                                                 'gnus-part))
1810                        (goto-char b)
1811                        t))
1812               (end-of-line)
1813               (skip-chars-forward "\n")
1814               (when (eq (get-text-property b 'article-type) 'multipart)
1815                 (delete-region b (point)))))
1816         (while (setq b (text-property-any b (point-max) 'article-type type))
1817           (delete-region
1818            b (or (text-property-not-all b (point-max) 'article-type type)
1819                  (point-max))))))))
1820
1821 (defun gnus-article-delete-invisible-text ()
1822   "Delete all invisible text in the current buffer."
1823   (save-excursion
1824     (let ((b (point-min)))
1825       (while (setq b (text-property-any b (point-max) 'invisible t))
1826         (delete-region
1827          b (or (text-property-not-all b (point-max) 'invisible t)
1828                (point-max)))))))
1829
1830 (defsubst gnus-article-header-rank ()
1831   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1832   (let ((list gnus-sorted-header-list)
1833         (i 1))
1834     (while list
1835       (if (looking-at (car list))
1836           (setq list nil)
1837         (setq list (cdr list))
1838         (incf i)))
1839       i))
1840
1841 (defun article-hide-headers (&optional arg delete)
1842   "Hide unwanted headers and possibly sort them as well."
1843   (interactive)
1844   ;; This function might be inhibited.
1845   (unless gnus-inhibit-hiding
1846     (let ((inhibit-read-only t)
1847           (case-fold-search t)
1848           (max (1+ (length gnus-sorted-header-list)))
1849           (inhibit-point-motion-hooks t)
1850           (cur (current-buffer))
1851           ignored visible beg)
1852       (save-excursion
1853         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1854         ;; group parameters, so we should go to the summary buffer.
1855         (when (prog1
1856                   (condition-case nil
1857                       (progn (set-buffer gnus-summary-buffer) t)
1858                     (error nil))
1859                 (setq ignored (when (not gnus-visible-headers)
1860                                 (cond ((stringp gnus-ignored-headers)
1861                                        gnus-ignored-headers)
1862                                       ((listp gnus-ignored-headers)
1863                                        (mapconcat 'identity
1864                                                   gnus-ignored-headers
1865                                                   "\\|"))))
1866                       visible (cond ((stringp gnus-visible-headers)
1867                                      gnus-visible-headers)
1868                                     ((and gnus-visible-headers
1869                                           (listp gnus-visible-headers))
1870                                      (mapconcat 'identity
1871                                                 gnus-visible-headers
1872                                                 "\\|")))))
1873           (set-buffer cur))
1874         (save-restriction
1875           ;; First we narrow to just the headers.
1876           (article-narrow-to-head)
1877           ;; Hide any "From " lines at the beginning of (mail) articles.
1878           (while (looking-at "From ")
1879             (forward-line 1))
1880           (unless (bobp)
1881             (delete-region (point-min) (point)))
1882           ;; Then treat the rest of the header lines.
1883           ;; Then we use the two regular expressions
1884           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1885           ;; select which header lines is to remain visible in the
1886           ;; article buffer.
1887           (while (re-search-forward "^[^ \t:]*:" nil t)
1888             (beginning-of-line)
1889             ;; Mark the rank of the header.
1890             (put-text-property
1891              (point) (1+ (point)) 'message-rank
1892              (if (or (and visible (looking-at visible))
1893                      (and ignored
1894                           (not (looking-at ignored))))
1895                  (gnus-article-header-rank)
1896                (+ 2 max)))
1897             (forward-line 1))
1898           (message-sort-headers-1)
1899           (when (setq beg (text-property-any
1900                            (point-min) (point-max) 'message-rank (+ 2 max)))
1901             ;; We delete the unwanted headers.
1902             (gnus-add-wash-type 'headers)
1903             (add-text-properties (point-min) (+ 5 (point-min))
1904                                  '(article-type headers dummy-invisible t))
1905             (delete-region beg (point-max))))))))
1906
1907 (defun article-hide-boring-headers (&optional arg)
1908   "Toggle hiding of headers that aren't very interesting.
1909 If given a negative prefix, always show; if given a positive prefix,
1910 always hide."
1911   (interactive (gnus-article-hidden-arg))
1912   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1913              (not gnus-show-all-headers))
1914     (save-excursion
1915       (save-restriction
1916         (let ((inhibit-read-only t)
1917               (inhibit-point-motion-hooks t))
1918           (article-narrow-to-head)
1919           (dolist (elem gnus-boring-article-headers)
1920             (goto-char (point-min))
1921             (cond
1922              ;; Hide empty headers.
1923              ((eq elem 'empty)
1924               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1925                 (forward-line -1)
1926                 (gnus-article-hide-text-type
1927                  (point-at-bol)
1928                  (progn
1929                    (end-of-line)
1930                    (if (re-search-forward "^[^ \t]" nil t)
1931                        (match-beginning 0)
1932                      (point-max)))
1933                  'boring-headers)))
1934              ;; Hide boring Newsgroups header.
1935              ((eq elem 'newsgroups)
1936               (when (gnus-string-equal
1937                      (gnus-fetch-field "newsgroups")
1938                      (gnus-group-real-name
1939                       (if (boundp 'gnus-newsgroup-name)
1940                           gnus-newsgroup-name
1941                         "")))
1942                 (gnus-article-hide-header "newsgroups")))
1943              ((eq elem 'to-address)
1944               (let ((to (message-fetch-field "to"))
1945                     (to-address
1946                      (gnus-parameter-to-address
1947                       (if (boundp 'gnus-newsgroup-name)
1948                           gnus-newsgroup-name ""))))
1949                 (when (and to to-address
1950                            (ignore-errors
1951                              (gnus-string-equal
1952                               ;; only one address in To
1953                               (nth 1 (mail-extract-address-components to))
1954                               to-address)))
1955                   (gnus-article-hide-header "to"))))
1956              ((eq elem 'to-list)
1957               (let ((to (message-fetch-field "to"))
1958                     (to-list
1959                      (gnus-parameter-to-list
1960                       (if (boundp 'gnus-newsgroup-name)
1961                           gnus-newsgroup-name ""))))
1962                 (when (and to to-list
1963                            (ignore-errors
1964                              (gnus-string-equal
1965                               ;; only one address in To
1966                               (nth 1 (mail-extract-address-components to))
1967                               to-list)))
1968                   (gnus-article-hide-header "to"))))
1969              ((eq elem 'cc-list)
1970               (let ((cc (message-fetch-field "cc"))
1971                     (to-list
1972                      (gnus-parameter-to-list
1973                       (if (boundp 'gnus-newsgroup-name)
1974                           gnus-newsgroup-name ""))))
1975                 (when (and cc to-list
1976                            (ignore-errors
1977                              (gnus-string-equal
1978                               ;; only one address in CC
1979                               (nth 1 (mail-extract-address-components cc))
1980                               to-list)))
1981                   (gnus-article-hide-header "cc"))))
1982              ((eq elem 'followup-to)
1983               (when (gnus-string-equal
1984                      (message-fetch-field "followup-to")
1985                      (message-fetch-field "newsgroups"))
1986                 (gnus-article-hide-header "followup-to")))
1987              ((eq elem 'reply-to)
1988               (if (gnus-group-find-parameter
1989                    gnus-newsgroup-name 'broken-reply-to)
1990                   (gnus-article-hide-header "reply-to")
1991                 (let ((from (message-fetch-field "from"))
1992                       (reply-to (message-fetch-field "reply-to")))
1993                   (when
1994                       (and
1995                        from reply-to
1996                        (ignore-errors
1997                          (equal
1998                           (sort (mapcar
1999                                  (lambda (x) (downcase (cadr x)))
2000                                  (mail-extract-address-components from t))
2001                                 'string<)
2002                           (sort (mapcar
2003                                  (lambda (x) (downcase (cadr x)))
2004                                  (mail-extract-address-components reply-to t))
2005                                 'string<))))
2006                     (gnus-article-hide-header "reply-to")))))
2007              ((eq elem 'date)
2008               (let ((date (with-current-buffer gnus-original-article-buffer
2009                             ;; If date in `gnus-article-buffer' is localized
2010                             ;; (`gnus-treat-date-user-defined'),
2011                             ;; `days-between' might fail.
2012                             (message-fetch-field "date"))))
2013                 (when (and date
2014                            (< (days-between (current-time-string) date)
2015                               4))
2016                   (gnus-article-hide-header "date"))))
2017              ((eq elem 'long-to)
2018               (let ((to (message-fetch-field "to"))
2019                     (cc (message-fetch-field "cc")))
2020                 (when (> (length to) 1024)
2021                   (gnus-article-hide-header "to"))
2022                 (when (> (length cc) 1024)
2023                   (gnus-article-hide-header "cc"))))
2024              ((eq elem 'many-to)
2025               (let ((to-count 0)
2026                     (cc-count 0))
2027                 (goto-char (point-min))
2028                 (while (re-search-forward "^to:" nil t)
2029                   (setq to-count (1+ to-count)))
2030                 (when (> to-count 1)
2031                   (while (> to-count 0)
2032                     (goto-char (point-min))
2033                     (save-restriction
2034                       (re-search-forward "^to:" nil nil to-count)
2035                       (forward-line -1)
2036                       (narrow-to-region (point) (point-max))
2037                       (gnus-article-hide-header "to"))
2038                     (setq to-count (1- to-count))))
2039                 (goto-char (point-min))
2040                 (while (re-search-forward "^cc:" nil t)
2041                   (setq cc-count (1+ cc-count)))
2042                 (when (> cc-count 1)
2043                   (while (> cc-count 0)
2044                     (goto-char (point-min))
2045                     (save-restriction
2046                       (re-search-forward "^cc:" nil nil cc-count)
2047                       (forward-line -1)
2048                       (narrow-to-region (point) (point-max))
2049                       (gnus-article-hide-header "cc"))
2050                     (setq cc-count (1- cc-count)))))))))))))
2051
2052 (defun gnus-article-hide-header (header)
2053   (save-excursion
2054     (goto-char (point-min))
2055     (when (re-search-forward (concat "^" header ":") nil t)
2056       (gnus-article-hide-text-type
2057        (point-at-bol)
2058        (progn
2059          (end-of-line)
2060          (if (re-search-forward "^[^ \t]" nil t)
2061              (match-beginning 0)
2062            (point-max)))
2063        'boring-headers))))
2064
2065 (defvar gnus-article-normalized-header-length 40
2066   "Length of normalized headers.")
2067
2068 (defun article-normalize-headers ()
2069   "Make all header lines 40 characters long."
2070   (interactive)
2071   (let ((inhibit-read-only t)
2072         column)
2073     (save-excursion
2074       (save-restriction
2075         (article-narrow-to-head)
2076         (while (not (eobp))
2077           (cond
2078            ((< (setq column (- (point-at-eol) (point)))
2079                gnus-article-normalized-header-length)
2080             (end-of-line)
2081             (insert (make-string
2082                      (- gnus-article-normalized-header-length column)
2083                      ? )))
2084            ((> column gnus-article-normalized-header-length)
2085             (gnus-put-text-property
2086              (progn
2087                (forward-char gnus-article-normalized-header-length)
2088                (point))
2089              (point-at-eol)
2090              'invisible t))
2091            (t
2092             ;; Do nothing.
2093             ))
2094           (forward-line 1))))))
2095
2096 (defun article-treat-dumbquotes ()
2097   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2098 Note that this function guesses whether a character is a sm*rtq**t* or
2099 not, so it should only be used interactively.
2100
2101 Sm*rtq**t*s are M****s***'s unilateral extension to the
2102 iso-8859-1 character map in an attempt to provide more quoting
2103 characters.  If you see something like \\222 or \\264 where
2104 you're expecting some kind of apostrophe or quotation mark, then
2105 try this wash."
2106   (interactive)
2107   (article-translate-strings gnus-article-dumbquotes-map))
2108
2109 (defvar org-entities)
2110
2111 (defun article-treat-non-ascii ()
2112   "Translate many Unicode characters into their ASCII equivalents."
2113   (interactive)
2114   (require 'org-entities)
2115   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2116     (dolist (elem org-entities)
2117       (when (and (listp elem)
2118                  (= (length (nth 6 elem)) 1))
2119         (if (featurep 'xemacs)
2120             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2121           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2122     (save-excursion
2123       (when (article-goto-body)
2124         (let ((inhibit-read-only t)
2125               replace props)
2126           (while (not (eobp))
2127             (if (not (setq replace (if (featurep 'xemacs)
2128                                        (get-char-table (following-char) table)
2129                                      (aref table (following-char)))))
2130                 (forward-char 1)
2131               (if (prog1
2132                       (setq props (text-properties-at (point)))
2133                     (delete-char 1))
2134                   (add-text-properties (point) (progn (insert replace) (point))
2135                                        props)
2136                 (insert replace)))))))))
2137
2138 (defun article-translate-strings (map)
2139   "Translate all string in the body of the article according to MAP.
2140 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2141   (save-excursion
2142     (when (article-goto-body)
2143       (let ((inhibit-read-only t))
2144         (dolist (elem map)
2145           (let ((from (car elem))
2146                 (to (cadr elem)))
2147             (save-excursion
2148               (if (stringp from)
2149                   (while (search-forward from nil t)
2150                     (replace-match to))
2151                 (while (not (eobp))
2152                   (if (eq (following-char) from)
2153                       (progn
2154                         (delete-char 1)
2155                         (insert to))
2156                     (forward-char 1)))))))))))
2157
2158 (defun article-treat-overstrike ()
2159   "Translate overstrikes into bold text."
2160   (interactive)
2161   (save-excursion
2162     (when (article-goto-body)
2163       (let ((inhibit-read-only t))
2164         (while (search-forward "\b" nil t)
2165           (let ((next (char-after))
2166                 (previous (char-after (- (point) 2))))
2167             ;; We do the boldification/underlining by hiding the
2168             ;; overstrikes and putting the proper text property
2169             ;; on the letters.
2170             (cond
2171              ((eq next previous)
2172               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2173               (put-text-property (point) (1+ (point)) 'face 'bold))
2174              ((eq next ?_)
2175               (gnus-article-hide-text-type
2176                (1- (point)) (1+ (point)) 'overstrike)
2177               (put-text-property
2178                (- (point) 2) (1- (point)) 'face 'underline))
2179              ((eq previous ?_)
2180               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2181               (put-text-property
2182                (point) (1+ (point)) 'face 'underline)))))))))
2183
2184 (defun article-treat-ansi-sequences ()
2185   "Translate ANSI SGR control sequences into overlays or extents."
2186   (interactive)
2187   (save-excursion
2188     (when (article-goto-body)
2189       (let ((inhibit-read-only t))
2190         (ansi-color-apply-on-region (point) (point-max))))))
2191
2192 (defun gnus-article-treat-unfold-headers ()
2193   "Unfold folded message headers.
2194 Only the headers that fit into the current window width will be
2195 unfolded."
2196   (interactive)
2197   (gnus-with-article-headers
2198     (let (length)
2199       (while (not (eobp))
2200         (save-restriction
2201           (mail-header-narrow-to-field)
2202           (let* ((header (buffer-string))
2203                  (unfoldable
2204                   (or (equal gnus-article-unfold-long-headers t)
2205                       (and (stringp gnus-article-unfold-long-headers)
2206                            (string-match gnus-article-unfold-long-headers
2207                                          header)))))
2208             (with-temp-buffer
2209               (insert header)
2210               (goto-char (point-min))
2211               (while (re-search-forward "\n[\t ]" nil t)
2212                 (replace-match " " t t)))
2213             (setq length (- (point-max) (point-min) 1))
2214             (when (or unfoldable
2215                       (< length (window-width)))
2216               (while (re-search-forward "\n[\t ]" nil t)
2217                 (replace-match " " t t))))
2218           (goto-char (point-max)))))))
2219
2220 (defun gnus-article-treat-fold-headers ()
2221   "Fold message headers."
2222   (interactive)
2223   (gnus-with-article-headers
2224     (while (not (eobp))
2225       (save-restriction
2226         (mail-header-narrow-to-field)
2227         (mail-header-fold-field)
2228         (goto-char (point-max))))))
2229
2230 (defun gnus-treat-smiley ()
2231   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2232   (interactive)
2233   (gnus-with-article-buffer
2234     (if (memq 'smiley gnus-article-wash-types)
2235         (gnus-delete-images 'smiley)
2236       (article-goto-body)
2237       (let ((images (smiley-region (point) (point-max))))
2238         (when images
2239           (gnus-add-wash-type 'smiley)
2240           (dolist (image images)
2241             (gnus-add-image 'smiley image)))))))
2242
2243 (defun gnus-article-remove-images ()
2244   "Remove all images from the article buffer."
2245   (interactive)
2246   (gnus-with-article-buffer
2247     (save-restriction
2248       (widen)
2249       (dolist (elem gnus-article-image-alist)
2250         (gnus-delete-images (car elem))))))
2251
2252 (autoload 'w3m-toggle-inline-images "w3m")
2253
2254 (defun gnus-article-show-images ()
2255   "Show any images that are in the HTML-rendered article buffer.
2256 This only works if the article in question is HTML."
2257   (interactive)
2258   (gnus-with-article-buffer
2259     (save-restriction
2260       (widen)
2261       (if (eq mm-text-html-renderer 'w3m)
2262           (let ((mm-inline-text-html-with-images nil))
2263             (w3m-toggle-inline-images))
2264         (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2265                                                         'image-displayer))
2266           (destructuring-bind (start end function) region
2267             (funcall function (get-text-property start 'image-url)
2268                      start end)))))))
2269
2270 (defun gnus-article-treat-fold-newsgroups ()
2271   "Unfold folded message headers.
2272 Only the headers that fit into the current window width will be
2273 unfolded."
2274   (interactive)
2275   (gnus-with-article-headers
2276     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2277       (save-restriction
2278         (mail-header-narrow-to-field)
2279         (while (re-search-forward ", *" nil t)
2280           (replace-match ", " t t))
2281         (mail-header-fold-field)
2282         (goto-char (point-max))))))
2283
2284 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2285   "Value of `truncate-lines' in Gnus Article buffer.
2286 Valid values are nil, t, `head', `first', `last', an integer or a
2287 predicate.  See Info node `(gnus)Customizing Articles'."
2288   :version "23.1" ;; No Gnus
2289   :group 'gnus-article
2290   ;; :link '(custom-manual "(gnus)Customizing Articles")
2291   :type 'boolean)
2292
2293 (defun gnus-article-toggle-truncate-lines (&optional arg)
2294   "Toggle whether to fold or truncate long lines in article the buffer.
2295 If ARG is non-nil and not a number, toggle
2296 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2297 long lines if and only if arg is positive."
2298   (interactive "P")
2299   (cond
2300    ((and (numberp arg) (> arg 0))
2301     (setq gnus-article-truncate-lines t))
2302    ((numberp arg)
2303     (setq gnus-article-truncate-lines nil))
2304    (arg
2305     (setq gnus-article-truncate-lines
2306           (not gnus-article-truncate-lines))))
2307   (gnus-with-article-buffer
2308     (cond
2309      ((and (numberp arg) (> arg 0))
2310       (setq truncate-lines nil))
2311      ((numberp arg)
2312       (setq truncate-lines t)))
2313     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2314     ;; `toggle-truncate-lines' needs an argument.
2315     (toggle-truncate-lines)))
2316
2317 (defun gnus-article-treat-body-boundary ()
2318   "Place a boundary line at the end of the headers."
2319   (interactive)
2320   (when (and gnus-body-boundary-delimiter
2321              (> (length gnus-body-boundary-delimiter) 0))
2322     (gnus-with-article-headers
2323       (goto-char (point-max))
2324       (let ((start (point)))
2325         (insert "X-Boundary: ")
2326         (gnus-add-text-properties start (point) '(invisible t intangible t))
2327        (insert (let (str (max (window-width)))
2328                  (if (featurep 'xemacs)
2329                      (setq max (1- max)))
2330                  (while (>= max (length str))
2331                     (setq str (concat str gnus-body-boundary-delimiter)))
2332                  (substring str 0 max))
2333                 "\n")
2334         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2335
2336 (defun article-fill-long-lines ()
2337   "Fill lines that are wider than the window width."
2338   (interactive)
2339   (save-excursion
2340     (let ((inhibit-read-only t)
2341           (width (window-width (get-buffer-window (current-buffer)))))
2342       (save-restriction
2343         (article-goto-body)
2344         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2345           (while (not (eobp))
2346             (end-of-line)
2347             (when (>= (current-column) (min fill-column width))
2348               (narrow-to-region (min (1+ (point)) (point-max))
2349                                 (point-at-bol))
2350               (let ((goback (point-marker)))
2351                 (fill-paragraph nil)
2352                 (goto-char (marker-position goback)))
2353               (widen))
2354             (forward-line 1)))))))
2355
2356 (defun article-capitalize-sentences ()
2357   "Capitalize the first word in each sentence."
2358   (interactive)
2359   (save-excursion
2360     (let ((inhibit-read-only t)
2361           (paragraph-start "^[\n\^L]"))
2362       (article-goto-body)
2363       (while (not (eobp))
2364         (capitalize-word 1)
2365         (forward-sentence)))))
2366
2367 (defun article-remove-cr ()
2368   "Remove trailing CRs and then translate remaining CRs into LFs."
2369   (interactive)
2370   (save-excursion
2371     (let ((inhibit-read-only t))
2372       (goto-char (point-min))
2373       (while (re-search-forward "\r+$" nil t)
2374         (replace-match "" t t))
2375       (goto-char (point-min))
2376       (while (search-forward "\r" nil t)
2377         (replace-match "\n" t t)))))
2378
2379 (defun article-remove-trailing-blank-lines ()
2380   "Remove all trailing blank lines from the article."
2381   (interactive)
2382   (save-excursion
2383     (let ((inhibit-read-only t))
2384       (goto-char (point-max))
2385       (delete-region
2386        (point)
2387        (progn
2388          (while (and (not (bobp))
2389                      (looking-at "^[ \t]*$")
2390                      (not (gnus-annotation-in-region-p
2391                            (point) (point-at-eol))))
2392            (forward-line -1))
2393          (forward-line 1)
2394          (point))))))
2395
2396 (defvar gnus-face-properties-alist)
2397
2398 (defun article-display-face (&optional force)
2399   "Display any Face headers in the header."
2400   (interactive (list 'force))
2401   (let ((wash-face-p buffer-read-only))
2402     (gnus-with-article-headers
2403       ;; When displaying parts, this function can be called several times on
2404       ;; the same article, without any intended toggle semantic (as typing `W
2405       ;; D d' would have). So face deletion must occur only when we come from
2406       ;; an interactive command, that is when the *Article* buffer is
2407       ;; read-only.
2408       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2409           (gnus-delete-images 'face)
2410         (let ((from (message-fetch-field "from"))
2411               face faces)
2412           (save-current-buffer
2413             (when (and wash-face-p
2414                        (gnus-buffer-live-p gnus-original-article-buffer)
2415                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2416               (set-buffer gnus-original-article-buffer))
2417             (save-restriction
2418               (mail-narrow-to-head)
2419               (when (or force
2420                         ;; Check whether this face is censored.
2421                         (not (and gnus-article-x-face-too-ugly
2422                                   (or from
2423                                       (setq from (message-fetch-field "from")))
2424                                   (string-match gnus-article-x-face-too-ugly
2425                                                 from))))
2426                 (while (gnus-article-goto-header "Face")
2427                   (push (mail-header-field-value) faces)))))
2428           (when faces
2429             (goto-char (point-min))
2430             (let (png image)
2431               (unless (setq from (gnus-article-goto-header "from"))
2432                 (insert "From:")
2433                 (setq from (point))
2434                 (insert " [no `from' set]\n"))
2435               (while faces
2436                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2437                   (setq image
2438                         (apply 'gnus-create-image png 'png t
2439                                (cdr (assq 'png gnus-face-properties-alist))))
2440                   (goto-char from)
2441                   (when image
2442                     (gnus-add-wash-type 'face)
2443                     (gnus-add-image 'face image)
2444                     (gnus-put-image image nil 'face)))))))))))
2445
2446 (defun article-display-x-face (&optional force)
2447   "Look for an X-Face header and display it if present."
2448   (interactive (list 'force))
2449   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2450     (gnus-with-article-headers
2451       ;; Delete the old process, if any.
2452       (when (process-status "article-x-face")
2453         (delete-process "article-x-face"))
2454       ;; See the comment in `article-display-face'.
2455       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2456           ;; We have already displayed X-Faces, so we remove them
2457           ;; instead.
2458           (gnus-delete-images 'xface)
2459         ;; Display X-Faces.
2460         (let ((from (message-fetch-field "from"))
2461               x-faces face)
2462           (save-current-buffer
2463             (when (and wash-face-p
2464                        (gnus-buffer-live-p gnus-original-article-buffer)
2465                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2466               ;; If type `W f', use gnus-original-article-buffer,
2467               ;; otherwise use the current buffer because displaying
2468               ;; RFC822 parts calls this function too.
2469               (set-buffer gnus-original-article-buffer))
2470             (save-restriction
2471               (mail-narrow-to-head)
2472               (and gnus-article-x-face-command
2473                    (or force
2474                        ;; Check whether this face is censored.
2475                        (not (and gnus-article-x-face-too-ugly
2476                                  (or from
2477                                      (setq from (message-fetch-field "from")))
2478                                  (string-match gnus-article-x-face-too-ugly
2479                                                from))))
2480                    (while (gnus-article-goto-header "X-Face")
2481                      (push (mail-header-field-value) x-faces)))))
2482           (when x-faces
2483             ;; We display the face.
2484             (cond ((functionp gnus-article-x-face-command)
2485                    ;; The command is a lisp function, so we call it.
2486                    (mapc gnus-article-x-face-command x-faces))
2487                   ((stringp gnus-article-x-face-command)
2488                    ;; The command is a string, so we interpret the command
2489                    ;; as a, well, command, and fork it off.
2490                    (let ((process-connection-type nil))
2491                      (gnus-set-process-query-on-exit-flag
2492                       (start-process
2493                        "article-x-face" nil shell-file-name
2494                        shell-command-switch gnus-article-x-face-command)
2495                       nil)
2496                      ;; Sending multiple EOFs to xv doesn't work,
2497                      ;; so we only do a single external face.
2498                      (with-temp-buffer
2499                        (insert (car x-faces))
2500                        (process-send-region "article-x-face"
2501                                             (point-min) (point-max)))
2502                      (process-send-eof "article-x-face")))
2503                   (t
2504                    (error "`%s' set to `%s' is not a function"
2505                           gnus-article-x-face-command
2506                           'gnus-article-x-face-command)))))))))
2507
2508 (defun article-decode-mime-words ()
2509   "Decode all MIME-encoded words in the article."
2510   (interactive)
2511   (gnus-with-article-buffer
2512     (let ((inhibit-point-motion-hooks t)
2513           (mail-parse-charset gnus-newsgroup-charset)
2514           (mail-parse-ignored-charsets
2515            (with-current-buffer gnus-summary-buffer
2516              gnus-newsgroup-ignored-charsets)))
2517       (mail-decode-encoded-word-region (point-min) (point-max)))))
2518
2519 (defun article-decode-charset (&optional prompt)
2520   "Decode charset-encoded text in the article.
2521 If PROMPT (the prefix), prompt for a coding system to use."
2522   (interactive "P")
2523   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2524         (inhibit-read-only t)
2525         (mail-parse-charset gnus-newsgroup-charset)
2526         (mail-parse-ignored-charsets
2527          (save-excursion (condition-case nil
2528                              (set-buffer gnus-summary-buffer)
2529                            (error))
2530                          gnus-newsgroup-ignored-charsets))
2531         ct cte ctl charset format)
2532     (save-excursion
2533       (save-restriction
2534         (article-narrow-to-head)
2535         (setq ct (message-fetch-field "Content-Type" t)
2536               cte (message-fetch-field "Content-Transfer-Encoding" t)
2537               ctl (and ct (mail-header-parse-content-type ct))
2538               charset (cond
2539                        (prompt
2540                         (mm-read-coding-system "Charset to decode: "))
2541                        (ctl
2542                         (mail-content-type-get ctl 'charset)))
2543               format (and ctl (mail-content-type-get ctl 'format)))
2544         (when cte
2545           (setq cte (mail-header-strip cte)))
2546         (if (and ctl (not (string-match "/" (car ctl))))
2547             (setq ctl nil))
2548         (goto-char (point-max)))
2549       (forward-line 1)
2550       (save-restriction
2551         (narrow-to-region (point) (point-max))
2552         (when (and (eq mail-parse-charset 'gnus-decoded)
2553                    (eq (mm-body-7-or-8) '8bit))
2554           ;; The text code could have been decoded.
2555           (setq charset mail-parse-charset))
2556         (when (and (or (not ctl)
2557                        (equal (car ctl) "text/plain"))
2558                    (not format)) ;; article with format will decode later.
2559           (mm-decode-body
2560            charset (and cte (intern (downcase
2561                                      (gnus-strip-whitespace cte))))
2562            (car ctl)))))))
2563
2564 (defun article-decode-encoded-words ()
2565   "Remove encoded-word encoding from headers."
2566   (let ((inhibit-point-motion-hooks t)
2567         (mail-parse-charset gnus-newsgroup-charset)
2568         (mail-parse-ignored-charsets
2569          (save-excursion (condition-case nil
2570                              (set-buffer gnus-summary-buffer)
2571                            (error))
2572                          gnus-newsgroup-ignored-charsets))
2573         (inhibit-read-only t)
2574         end start)
2575     (goto-char (point-min))
2576     (when (search-forward "\n\n" nil 'move)
2577       (forward-line -1))
2578     (setq end (point))
2579     (while (not (bobp))
2580       (while (progn
2581                (forward-line -1)
2582                (and (not (bobp))
2583                     (memq (char-after) '(?\t ? )))))
2584       (setq start (point))
2585       (if (looking-at "\
2586 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2587 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2588           (funcall gnus-decode-address-function start end)
2589         (funcall gnus-decode-header-function start end))
2590       (goto-char (setq end start)))))
2591
2592 (defun article-decode-group-name ()
2593   "Decode group names in Newsgroups, Followup-To and Xref headers."
2594   (let ((inhibit-point-motion-hooks t)
2595         (inhibit-read-only t)
2596         (method (gnus-find-method-for-group gnus-newsgroup-name))
2597         regexp)
2598     (when (and (or gnus-group-name-charset-method-alist
2599                    gnus-group-name-charset-group-alist)
2600                (gnus-buffer-live-p gnus-original-article-buffer))
2601       (save-restriction
2602         (article-narrow-to-head)
2603         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2604           (with-current-buffer gnus-original-article-buffer
2605             (goto-char (point-min)))
2606           (setq regexp (concat "^" header
2607                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2608           (while (re-search-forward regexp nil t)
2609             (replace-match (save-match-data
2610                              (gnus-decode-newsgroups
2611                               ;; XXX how to use data in article buffer?
2612                               (with-current-buffer gnus-original-article-buffer
2613                                 (re-search-forward regexp nil t)
2614                                 (match-string 1))
2615                               gnus-newsgroup-name method))
2616                            t t nil 1))
2617           (goto-char (point-min)))))))
2618
2619 (autoload 'idna-to-unicode "idna")
2620
2621 (defun article-decode-idna-rhs ()
2622   "Decode IDNA strings in RHS in various headers in current buffer.
2623 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2624 Mail-Reply-To: and Mail-Followup-To:."
2625   (when gnus-use-idna
2626     (save-restriction
2627       (let ((inhibit-point-motion-hooks t)
2628             (inhibit-read-only t))
2629         (article-narrow-to-head)
2630         (goto-char (point-min))
2631         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2632           (let (ace unicode)
2633             (when (save-match-data
2634                     (and (setq ace (match-string 1))
2635                          (save-excursion
2636                            (and (re-search-backward "^[^ \t]" nil t)
2637                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2638                          (setq unicode (idna-to-unicode ace))))
2639               (unless (string= ace unicode)
2640                 (replace-match unicode nil nil nil 1)))))))))
2641
2642 (defun article-de-quoted-unreadable (&optional force read-charset)
2643   "Translate a quoted-printable-encoded article.
2644 If FORCE, decode the article whether it is marked as quoted-printable
2645 or not.
2646 If READ-CHARSET, ask for a coding system."
2647   (interactive (list 'force current-prefix-arg))
2648   (save-excursion
2649     (let ((inhibit-read-only t) type charset)
2650       (if (gnus-buffer-live-p gnus-original-article-buffer)
2651           (with-current-buffer gnus-original-article-buffer
2652             (setq type
2653                   (gnus-fetch-field "content-transfer-encoding"))
2654             (let* ((ct (gnus-fetch-field "content-type"))
2655                    (ctl (and ct (mail-header-parse-content-type ct))))
2656               (setq charset (and ctl
2657                                  (mail-content-type-get ctl 'charset)))
2658               (if (stringp charset)
2659                   (setq charset (intern (downcase charset)))))))
2660       (if read-charset
2661           (setq charset (mm-read-coding-system "Charset: " charset)))
2662       (unless charset
2663         (setq charset gnus-newsgroup-charset))
2664       (when (or force
2665                 (and type (let ((case-fold-search t))
2666                             (string-match "quoted-printable" type))))
2667         (article-goto-body)
2668         (quoted-printable-decode-region
2669          (point) (point-max) (mm-charset-to-coding-system charset))))))
2670
2671 (defun article-de-base64-unreadable (&optional force read-charset)
2672   "Translate a base64 article.
2673 If FORCE, decode the article whether it is marked as base64 not.
2674 If READ-CHARSET, ask for a coding system."
2675   (interactive (list 'force current-prefix-arg))
2676   (save-excursion
2677     (let ((inhibit-read-only t) type charset)
2678       (if (gnus-buffer-live-p gnus-original-article-buffer)
2679           (with-current-buffer gnus-original-article-buffer
2680             (setq type
2681                   (gnus-fetch-field "content-transfer-encoding"))
2682             (let* ((ct (gnus-fetch-field "content-type"))
2683                    (ctl (and ct (mail-header-parse-content-type ct))))
2684               (setq charset (and ctl
2685                                  (mail-content-type-get ctl 'charset)))
2686               (if (stringp charset)
2687                   (setq charset (intern (downcase charset)))))))
2688       (if read-charset
2689           (setq charset (mm-read-coding-system "Charset: " charset)))
2690       (unless charset
2691         (setq charset gnus-newsgroup-charset))
2692       (when (or force
2693                 (and type (let ((case-fold-search t))
2694                             (string-match "base64" type))))
2695         (article-goto-body)
2696         (save-restriction
2697           (narrow-to-region (point) (point-max))
2698           (base64-decode-region (point-min) (point-max))
2699           (mm-decode-coding-region
2700            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2701
2702 (eval-when-compile
2703   (require 'rfc1843))
2704
2705 (defun article-decode-HZ ()
2706   "Translate a HZ-encoded article."
2707   (interactive)
2708   (require 'rfc1843)
2709   (save-excursion
2710     (let ((inhibit-read-only t))
2711       (rfc1843-decode-region (point-min) (point-max)))))
2712
2713 (defun article-unsplit-urls ()
2714   "Remove the newlines that some other mailers insert into URLs."
2715   (interactive)
2716   (save-excursion
2717     (let ((inhibit-read-only t))
2718       (goto-char (point-min))
2719       (while (re-search-forward
2720               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2721         (replace-match "\\1\\3" t)))
2722     (when (gmm-called-interactively-p 'any)
2723       (gnus-treat-article nil))))
2724
2725 (defun article-wash-html ()
2726   "Format an HTML article."
2727   (interactive)
2728   (let ((handles nil)
2729         (buffer-read-only nil))
2730     (when (gnus-buffer-live-p gnus-original-article-buffer)
2731       (with-current-buffer gnus-original-article-buffer
2732         (setq handles (mm-dissect-buffer t t))))
2733     (article-goto-body)
2734     (delete-region (point) (point-max))
2735     (mm-enable-multibyte)
2736     (mm-inline-text-html handles)))
2737
2738 (defvar gnus-article-browse-html-temp-list nil
2739   "List of temporary files created by `gnus-article-browse-html-parts'.
2740 Internal variable.")
2741
2742 (defcustom gnus-article-browse-delete-temp 'ask
2743   "What to do with temporary files from `gnus-article-browse-html-parts'.
2744 If nil, don't delete temporary files.  If it is t, delete them on
2745 exit from the summary buffer.  If it is the symbol `file', query
2746 on each file, if it is `ask' ask once when exiting from the
2747 summary buffer."
2748   :group 'gnus-article
2749   :version "23.1" ;; No Gnus
2750   :type '(choice (const :tag "Don't delete" nil)
2751                  (const :tag "Don't ask" t)
2752                  (const :tag "Ask" ask)
2753                  (const :tag "Ask for each file" file)))
2754
2755 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2756
2757 (defun gnus-article-browse-delete-temp-files (&optional how)
2758   "Delete temp-files created by `gnus-article-browse-html-parts'."
2759   (when (and gnus-article-browse-html-temp-list
2760              (progn
2761                (or how (setq how gnus-article-browse-delete-temp))
2762                (if (eq how 'ask)
2763                    (let ((files (length gnus-article-browse-html-temp-list)))
2764                      (or (gnus-y-or-n-p
2765                           (if (= files 1)
2766                               "Delete the temporary HTML file? "
2767                             (format "Delete all %s temporary HTML files? "
2768                                     files)))
2769                          (setq gnus-article-browse-html-temp-list nil)))
2770                  how)))
2771     (dolist (file gnus-article-browse-html-temp-list)
2772       (cond ((file-directory-p file)
2773              (when (or (not (eq how 'file))
2774                        (gnus-y-or-n-p
2775                         (format
2776                          "Delete temporary HTML file(s) in directory `%s'? "
2777                          (file-name-as-directory file))))
2778                (gnus-delete-directory file)))
2779             ((file-exists-p file)
2780              (when (or (not (eq how 'file))
2781                        (gnus-y-or-n-p
2782                         (format "Delete temporary HTML file `%s'? " file)))
2783                (delete-file file)))))
2784     ;; Also remove file from the list when not deleted or if file doesn't
2785     ;; exist anymore.
2786     (setq gnus-article-browse-html-temp-list nil))
2787   gnus-article-browse-html-temp-list)
2788
2789 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2790   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2791 Return file name."
2792   (save-match-data
2793     (let (file type)
2794       (catch 'found
2795         (dolist (handle handles)
2796           (cond
2797            ((not (listp handle)))
2798            ((equal (mm-handle-media-supertype handle) "multipart")
2799             (when (setq file (gnus-article-browse-html-save-cid-content
2800                               cid handle directory))
2801               (throw 'found file)))
2802            ((equal (concat "<" cid ">") (mm-handle-id handle))
2803             (setq file
2804                   (expand-file-name
2805                    (or (mm-handle-filename handle)
2806                        (concat
2807                         (make-temp-name "cid")
2808                         (car (rassoc (car (mm-handle-type handle)) mailcap-mime-extensions))))
2809                    directory))
2810             (mm-save-part-to-file handle file)
2811             (throw 'found file))))))))
2812
2813 (defun gnus-article-browse-html-parts (list &optional header)
2814   "View all \"text/html\" parts from LIST.
2815 Recurse into multiparts.  The optional HEADER that should be a decoded
2816 message header will be added to the bodies of the \"text/html\" parts."
2817   ;; Internal function used by `gnus-article-browse-html-article'.
2818   (let (type file charset content cid-dir tmp-file showed)
2819     ;; Find and show the html-parts.
2820     (dolist (handle list)
2821       ;; If HTML, show it:
2822       (cond ((not (listp handle)))
2823             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2824                  (and (equal (car type) "message/external-body")
2825                       (or header
2826                           (setq file (mm-handle-filename handle)))
2827                       (or (mm-handle-cache handle)
2828                           (condition-case code
2829                               (progn (mm-extern-cache-contents handle) t)
2830                             (error
2831                              (gnus-message 3 "%s" (error-message-string code))
2832                              (when (>= gnus-verbose 3) (sit-for 2))
2833                              nil)))
2834                       (progn
2835                         (setq handle (mm-handle-cache handle)
2836                               type (mm-handle-type handle))
2837                         (equal (car type) "text/html"))))
2838              (setq charset (mail-content-type-get type 'charset)
2839                    content (mm-get-part handle))
2840              (with-temp-buffer
2841                (if (eq charset 'gnus-decoded)
2842                    (mm-enable-multibyte)
2843                  (mm-disable-multibyte))
2844                (insert content)
2845                ;; resolve cid contents
2846                (let ((case-fold-search t)
2847                      cid-file)
2848                  (goto-char (point-min))
2849                  (while (re-search-forward "\
2850 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2851                                            nil t)
2852                    (unless cid-dir
2853                      (setq cid-dir (mm-make-temp-file "cid" t))
2854                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2855                    (setq file nil
2856                          content nil)
2857                    (when (setq cid-file
2858                                (gnus-article-browse-html-save-cid-content
2859                                 (match-string 2)
2860                                 (with-current-buffer gnus-article-buffer
2861                                   gnus-article-mime-handles)
2862                                 cid-dir))
2863                      (when (eq system-type 'cygwin)
2864                        (setq cid-file
2865                              (concat "/" (substring
2866                                           (with-output-to-string
2867                                             (call-process "cygpath" nil
2868                                                           standard-output
2869                                                           nil "-m" cid-file))
2870                                           0 -1))))
2871                      (replace-match (concat "file://" cid-file)
2872                                     nil nil nil 1))))
2873                (unless content (setq content (buffer-string))))
2874              (when (or charset header (not file))
2875                (setq tmp-file (mm-make-temp-file
2876                                ;; Do we need to care for 8.3 filenames?
2877                                "mm-" nil ".html")))
2878              ;; Add a meta html tag to specify charset and a header.
2879              (cond
2880               (header
2881                (let (title eheader body hcharset coding)
2882                  (with-temp-buffer
2883                    (mm-enable-multibyte)
2884                    (setq case-fold-search t)
2885                    (insert header "\n")
2886                    (setq title (message-fetch-field "subject"))
2887                    (goto-char (point-min))
2888                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|\\(&\\)\\|\n"
2889                                              nil t)
2890                      (replace-match (cond ((match-beginning 1) "&lt;")
2891                                           ((match-beginning 2) "&gt;")
2892                                           ((match-beginning 3) "&amp;")
2893                                           (t "<br>\n"))))
2894                    (goto-char (point-min))
2895                    (insert "<div align=\"left\">\n")
2896                    (goto-char (point-max))
2897                    (insert "</div>\n<hr>\n")
2898                    ;; We have to examine charset one by one since
2899                    ;; charset specified in parts might be different.
2900                    (if (eq charset 'gnus-decoded)
2901                        (setq charset 'utf-8
2902                              eheader (mm-encode-coding-string (buffer-string)
2903                                                               charset)
2904                              title (when title
2905                                      (mm-encode-coding-string title charset))
2906                              body (mm-encode-coding-string content charset))
2907                      (setq hcharset (mm-find-mime-charset-region (point-min)
2908                                                                  (point-max)))
2909                      (cond ((= (length hcharset) 1)
2910                             (setq hcharset (car hcharset)
2911                                   coding (mm-charset-to-coding-system
2912                                           hcharset)))
2913                            ((> (length hcharset) 1)
2914                             (setq hcharset 'utf-8
2915                                   coding hcharset)))
2916                      (if coding
2917                          (if charset
2918                              (progn
2919                                (setq body
2920                                      (mm-charset-to-coding-system charset))
2921                                (if (eq coding body)
2922                                    (setq eheader (mm-encode-coding-string
2923                                                   (buffer-string) coding)
2924                                          title (when title
2925                                                  (mm-encode-coding-string
2926                                                   title coding))
2927                                          body content)
2928                                  (setq charset 'utf-8
2929                                        eheader (mm-encode-coding-string
2930                                                 (buffer-string) charset)
2931                                        title (when title
2932                                                (mm-encode-coding-string
2933                                                 title charset))
2934                                        body (mm-encode-coding-string
2935                                              (mm-decode-coding-string
2936                                               content body)
2937                                              charset))))
2938                            (setq charset hcharset
2939                                  eheader (mm-encode-coding-string
2940                                           (buffer-string) coding)
2941                                  title (when title
2942                                          (mm-encode-coding-string
2943                                           title coding))
2944                                  body content))
2945                        (setq eheader (mm-string-as-unibyte (buffer-string))
2946                              body content)))
2947                    (erase-buffer)
2948                    (mm-disable-multibyte)
2949                    (insert body)
2950                    (when charset
2951                      (mm-add-meta-html-tag handle charset t))
2952                    (when title
2953                      (goto-char (point-min))
2954                      (unless (search-forward "<title>" nil t)
2955                        (re-search-forward "<head>\\s-*" nil t)
2956                        (insert "<title>" title "</title>\n")))
2957                    (goto-char (point-min))
2958                    (or (re-search-forward
2959                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2960                        (re-search-forward
2961                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2962                    (insert eheader)
2963                    (mm-write-region (point-min) (point-max)
2964                                     tmp-file nil nil nil 'binary t))))
2965               (charset
2966                (mm-with-unibyte-buffer
2967                  (insert (if (eq charset 'gnus-decoded)
2968                              (mm-encode-coding-string content
2969                                                       (setq charset 'utf-8))
2970                            content))
2971                  (if (or (mm-add-meta-html-tag handle charset)
2972                          (not file))
2973                      (mm-write-region (point-min) (point-max)
2974                                       tmp-file nil nil nil 'binary t)
2975                    (setq tmp-file nil))))
2976               (tmp-file
2977                (mm-save-part-to-file handle tmp-file)))
2978              (when tmp-file
2979                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2980              (add-hook 'gnus-summary-prepare-exit-hook
2981                        'gnus-article-browse-delete-temp-files)
2982              (add-hook 'gnus-exit-gnus-hook
2983                        (lambda  ()
2984                          (gnus-article-browse-delete-temp-files t)))
2985              ;; FIXME: Warn if there's an <img> tag?
2986              (browse-url-of-file (or tmp-file (expand-file-name file)))
2987              (setq showed t))
2988             ;; If multipart, recurse
2989             ((equal (mm-handle-media-supertype handle) "multipart")
2990              (when (gnus-article-browse-html-parts handle header)
2991                (setq showed t)))
2992             ((equal (mm-handle-media-type handle) "message/rfc822")
2993              (mm-with-multibyte-buffer
2994                (mm-insert-part handle)
2995                (setq handle (mm-dissect-buffer t t))
2996                (when (and (bufferp (car handle))
2997                           (stringp (car (mm-handle-type handle))))
2998                  (setq handle (list handle)))
2999                (when header
3000                  (article-decode-encoded-words)
3001                  (let ((gnus-visible-headers
3002                         (or (get 'gnus-visible-headers 'standard-value)
3003                             gnus-visible-headers)))
3004                    (article-hide-headers))
3005                  (goto-char (point-min))
3006                  (search-forward "\n\n" nil 'move)
3007                  (skip-chars-backward "\t\n ")
3008                  (setq header (buffer-substring (point-min) (point)))))
3009              (when (prog1
3010                        (gnus-article-browse-html-parts handle header)
3011                      (mm-destroy-parts handle))
3012                (setq showed t)))))
3013     showed))
3014
3015 (defun gnus-article-browse-html-article (&optional arg)
3016   "View \"text/html\" parts of the current article with a WWW browser.
3017 Inline images embedded in a message using the cid scheme, as they are
3018 generally considered to be safe, will be processed properly.
3019 The message header is added to the beginning of every html part unless
3020 the prefix argument ARG is given.
3021
3022 Warning: Spammers use links to images (using the http scheme) in HTML
3023 articles to verify whether you have read the message.  As
3024 `gnus-article-browse-html-article' passes the HTML content to the
3025 browser without eliminating these \"web bugs\" you should only
3026 use it for mails from trusted senders.
3027
3028 If you always want to display HTML parts in the browser, set
3029 `mm-text-html-renderer' to nil.
3030
3031 This command creates temporary files to pass HTML contents including
3032 images if any to the browser, and deletes them when exiting the group
3033 \(if you want)."
3034   ;; Cf. `mm-w3m-safe-url-regexp'
3035   (interactive "P")
3036   (if arg
3037       (gnus-summary-show-article)
3038     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3039                                     gnus-visible-headers))
3040           ;; As we insert a <hr>, there's no need for the body boundary.
3041           (gnus-treat-body-boundary nil))
3042       (gnus-summary-show-article)))
3043   (with-current-buffer gnus-article-buffer
3044     (let ((header (unless arg
3045                     (save-restriction
3046                       (widen)
3047                       (buffer-substring-no-properties
3048                        (goto-char (point-min))
3049                        (if (search-forward "\n\n" nil t)
3050                            (match-beginning 0)
3051                          (goto-char (point-max))
3052                          (skip-chars-backward "\t\n ")
3053                          (point))))))
3054           parts)
3055       (set-buffer gnus-original-article-buffer)
3056       (setq parts (mm-dissect-buffer t t))
3057       ;; If singlepart, enforce a list.
3058       (when (and (bufferp (car parts))
3059                  (stringp (car (mm-handle-type parts))))
3060         (setq parts (list parts)))
3061       ;; Process the list
3062       (unless (gnus-article-browse-html-parts parts header)
3063         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3064       (mm-destroy-parts parts)
3065       (unless arg
3066         (gnus-summary-show-article)))))
3067
3068 (defun article-hide-list-identifiers ()
3069   "Remove list identifies from the Subject header.
3070 The `gnus-list-identifiers' variable specifies what to do."
3071   (interactive)
3072   (let ((inhibit-point-motion-hooks t)
3073         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3074         (inhibit-read-only t))
3075     (when regexp
3076       (save-excursion
3077         (save-restriction
3078           (article-narrow-to-head)
3079           (goto-char (point-min))
3080           (while (re-search-forward
3081                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3082                   nil t)
3083             (delete-region (match-beginning 2) (match-end 0))
3084             (beginning-of-line))
3085           (when (re-search-forward
3086                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3087             (delete-region (match-beginning 1) (match-end 1))))))))
3088
3089 (defun article-hide-pem (&optional arg)
3090   "Toggle hiding of any PEM headers and signatures in the current article.
3091 If given a negative prefix, always show; if given a positive prefix,
3092 always hide."
3093   (interactive (gnus-article-hidden-arg))
3094   (unless (gnus-article-check-hidden-text 'pem arg)
3095     (save-excursion
3096       (let ((inhibit-read-only t) end)
3097         (goto-char (point-min))
3098         ;; Hide the horrendously ugly "header".
3099         (when (and (search-forward
3100                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3101                     nil t)
3102                    (setq end (1+ (match-beginning 0))))
3103           (gnus-add-wash-type 'pem)
3104           (gnus-article-hide-text-type
3105            end
3106            (if (search-forward "\n\n" nil t)
3107                (match-end 0)
3108              (point-max))
3109            'pem)
3110           ;; Hide the trailer as well
3111           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3112                                 nil t)
3113             (gnus-article-hide-text-type
3114              (match-beginning 0) (match-end 0) 'pem)))))))
3115
3116 (defun article-strip-banner ()
3117   "Strip the banners specified by the `banner' group parameter and by
3118 `gnus-article-address-banner-alist'."
3119   (interactive)
3120   (save-excursion
3121     (save-restriction
3122       (let ((inhibit-point-motion-hooks t))
3123         (when (gnus-parameter-banner gnus-newsgroup-name)
3124           (article-really-strip-banner
3125            (gnus-parameter-banner gnus-newsgroup-name)))
3126         (when gnus-article-address-banner-alist
3127           ;; Note that the From header is decoded here, so it is
3128           ;; required that the *-extract-address-components function
3129           ;; supports non-ASCII text.
3130           (let ((from (save-restriction
3131                         (widen)
3132                         (article-narrow-to-head)
3133                         (mail-fetch-field "from"))))
3134             (when (and from
3135                        (setq from
3136                              (cadr (funcall gnus-extract-address-components
3137                                             from))))
3138               (catch 'found
3139                 (dolist (pair gnus-article-address-banner-alist)
3140                   (when (string-match (car pair) from)
3141                     (throw 'found
3142                            (article-really-strip-banner (cdr pair)))))))))))))
3143
3144 (defun article-really-strip-banner (banner)
3145   "Strip the banner specified by the argument."
3146   (save-excursion
3147     (save-restriction
3148       (let ((inhibit-point-motion-hooks t)
3149             (gnus-signature-limit nil)
3150             (inhibit-read-only t))
3151         (article-goto-body)
3152         (cond
3153          ((eq banner 'signature)
3154           (when (gnus-article-narrow-to-signature)
3155             (widen)
3156             (forward-line -1)
3157             (delete-region (point) (point-max))))
3158          ((symbolp banner)
3159           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3160               (while (re-search-forward banner nil t)
3161                 (delete-region (match-beginning 0) (match-end 0)))))
3162          ((stringp banner)
3163           (while (re-search-forward banner nil t)
3164             (delete-region (match-beginning 0) (match-end 0)))))))))
3165
3166 (defun article-babel ()
3167   "Translate article using an online translation service."
3168   (interactive)
3169   (require 'babel)
3170   (gnus-with-article-buffer
3171     (when (article-goto-body)
3172       (let* ((start (point))
3173              (end (point-max))
3174              (orig (buffer-substring start end))
3175              (trans (babel-as-string orig)))
3176         (save-restriction
3177           (narrow-to-region start end)
3178           (delete-region start end)
3179           (insert trans))))))
3180
3181 (defun article-hide-signature (&optional arg)
3182   "Hide the signature in the current article.
3183 If given a negative prefix, always show; if given a positive prefix,
3184 always hide."
3185   (interactive (gnus-article-hidden-arg))
3186   (unless (gnus-article-check-hidden-text 'signature arg)
3187     (save-excursion
3188       (save-restriction
3189         (let ((inhibit-read-only t))
3190           (when (gnus-article-narrow-to-signature)
3191             (gnus-article-hide-text-type
3192              (point-min) (point-max) 'signature))))))
3193   (gnus-set-mode-line 'article))
3194
3195 (defun article-strip-headers-in-body ()
3196   "Strip offensive headers from bodies."
3197   (interactive)
3198   (save-excursion
3199     (article-goto-body)
3200     (let ((case-fold-search t))
3201       (when (looking-at "x-no-archive:")
3202         (gnus-delete-line)))))
3203
3204 (defun article-strip-leading-blank-lines ()
3205   "Remove all blank lines from the beginning of the article."
3206   (interactive)
3207   (save-excursion
3208     (let ((inhibit-point-motion-hooks t)
3209           (inhibit-read-only t))
3210       (when (article-goto-body)
3211         (while (and (not (eobp))
3212                     (looking-at "[ \t]*$"))
3213           (gnus-delete-line))))))
3214
3215 (defun article-narrow-to-head ()
3216   "Narrow the buffer to the head of the message.
3217 Point is left at the beginning of the narrowed-to region."
3218   (narrow-to-region
3219    (goto-char (point-min))
3220    (cond
3221     ;; Absolutely no headers displayed.
3222     ((looking-at "\n")
3223      (point))
3224     ;; Normal headers.
3225     ((search-forward "\n\n" nil 1)
3226      (1- (point)))
3227     ;; Nothing but headers.
3228     (t
3229      (point-max))))
3230   (goto-char (point-min)))
3231
3232 (defun article-goto-body ()
3233   "Place point at the start of the body."
3234   (goto-char (point-min))
3235   (cond
3236    ;; This variable is only bound when dealing with separate
3237    ;; MIME body parts.
3238    (article-goto-body-goes-to-point-min-p
3239     t)
3240    ((search-forward "\n\n" nil t)
3241     t)
3242    (t
3243     (goto-char (point-max))
3244     nil)))
3245
3246 (defun article-strip-multiple-blank-lines ()
3247   "Replace consecutive blank lines with one empty line."
3248   (interactive)
3249   (save-excursion
3250     (let ((inhibit-point-motion-hooks t)
3251           (inhibit-read-only t))
3252       ;; First make all blank lines empty.
3253       (article-goto-body)
3254       (while (re-search-forward "^[ \t]+$" nil t)
3255         (unless (gnus-annotation-in-region-p
3256                  (match-beginning 0) (match-end 0))
3257           (replace-match "" nil t)))
3258       ;; Then replace multiple empty lines with a single empty line.
3259       (article-goto-body)
3260       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3261         (unless (gnus-annotation-in-region-p
3262                  (match-beginning 0) (match-end 0))
3263           (delete-region (match-beginning 1) (match-end 1)))))))
3264
3265 (defun article-strip-leading-space ()
3266   "Remove all white space from the beginning of the lines in the article."
3267   (interactive)
3268   (save-excursion
3269     (let ((inhibit-point-motion-hooks t)
3270           (inhibit-read-only t))
3271       (article-goto-body)
3272       (while (re-search-forward "^[ \t]+" nil t)
3273         (replace-match "" t t)))))
3274
3275 (defun article-strip-trailing-space ()
3276   "Remove all white space from the end of the lines in the article."
3277   (interactive)
3278   (save-excursion
3279     (let ((inhibit-point-motion-hooks t)
3280           (inhibit-read-only t))
3281       (article-goto-body)
3282       (while (re-search-forward "[ \t]+$" nil t)
3283         (replace-match "" t t)))))
3284
3285 (defun article-strip-blank-lines ()
3286   "Strip leading, trailing and multiple blank lines."
3287   (interactive)
3288   (article-strip-leading-blank-lines)
3289   (article-remove-trailing-blank-lines)
3290   (article-strip-multiple-blank-lines))
3291
3292 (defun article-strip-all-blank-lines ()
3293   "Strip all blank lines."
3294   (interactive)
3295   (save-excursion
3296     (let ((inhibit-point-motion-hooks t)
3297           (inhibit-read-only t))
3298       (article-goto-body)
3299       (while (re-search-forward "^[ \t]*\n" nil t)
3300         (replace-match "" t t)))))
3301
3302 (defun gnus-article-narrow-to-signature ()
3303   "Narrow to the signature; return t if a signature is found, else nil."
3304   (let ((inhibit-point-motion-hooks t))
3305     (when (gnus-article-search-signature)
3306       (forward-line 1)
3307       ;; Check whether we have some limits to what we consider
3308       ;; to be a signature.
3309       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3310                       (list gnus-signature-limit)))
3311             limit limited)
3312         (while (setq limit (pop limits))
3313           (if (or (and (integerp limit)
3314                        (< (- (point-max) (point)) limit))
3315                   (and (floatp limit)
3316                        (< (count-lines (point) (point-max)) limit))
3317                   (and (functionp limit)
3318                        (funcall limit))
3319                   (and (stringp limit)
3320                        (not (re-search-forward limit nil t))))
3321               ()                        ; This limit did not succeed.
3322             (setq limited t
3323                   limits nil)))
3324         (unless limited
3325           (narrow-to-region (point) (point-max))
3326           t)))))
3327
3328 (defun gnus-article-search-signature ()
3329   "Search the current buffer for the signature separator.
3330 Put point at the beginning of the signature separator."
3331   (let ((cur (point)))
3332     (goto-char (point-max))
3333     (if (if (stringp gnus-signature-separator)
3334             (re-search-backward gnus-signature-separator nil t)
3335           (let ((seps gnus-signature-separator))
3336             (while (and seps
3337                         (not (re-search-backward (car seps) nil t)))
3338               (pop seps))
3339             seps))
3340         t
3341       (goto-char cur)
3342       nil)))
3343
3344 (defun gnus-article-hidden-arg ()
3345   "Return the current prefix arg as a number, or 0 if no prefix."
3346   (list (if current-prefix-arg
3347             (prefix-numeric-value current-prefix-arg)
3348           0)))
3349
3350 (defun gnus-article-check-hidden-text (type arg)
3351   "Return nil if hiding is necessary.
3352 Arg can be nil or a number.  nil and positive means hide, negative
3353 means show, 0 means toggle."
3354   (save-excursion
3355     (save-restriction
3356       (let ((hide (gnus-article-hidden-text-p type)))
3357         (cond
3358          ((or (null arg)
3359               (> arg 0))
3360           nil)
3361          ((< arg 0)
3362           (gnus-article-show-hidden-text type)
3363           t)
3364          (t
3365           (if (eq hide 'hidden)
3366               (progn
3367                 (gnus-article-show-hidden-text type)
3368                 t)
3369             nil)))))))
3370
3371 (defun gnus-article-hidden-text-p (type)
3372   "Say whether the current buffer contains hidden text of type TYPE."
3373   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3374     (while (and pos
3375                 (not (get-text-property pos 'invisible))
3376                 (not (get-text-property pos 'dummy-invisible)))
3377       (setq pos
3378             (text-property-any (1+ pos) (point-max) 'article-type type)))
3379     (if pos
3380         'hidden
3381       nil)))
3382
3383 (defun gnus-article-show-hidden-text (type &optional dummy)
3384   "Show all hidden text of type TYPE.
3385 Originally it is hide instead of DUMMY."
3386   (let ((inhibit-read-only t)
3387         (inhibit-point-motion-hooks t))
3388     (gnus-remove-text-properties-when
3389      'article-type type
3390      (point-min) (point-max)
3391      (cons 'article-type (cons type
3392                                gnus-hidden-properties)))
3393     (gnus-delete-wash-type type)))
3394
3395 (defconst article-time-units
3396   `((year . ,(* 365.25 24 60 60))
3397     (week . ,(* 7 24 60 60))
3398     (day . ,(* 24 60 60))
3399     (hour . ,(* 60 60))
3400     (minute . 60)
3401     (second . 1))
3402   "Mapping from time units to seconds.")
3403
3404 (defun gnus-article-forward-header ()
3405   "Move point to the start of the next header.
3406 If the current header is a continuation header, this can be several
3407 lines forward."
3408   (let ((ended nil))
3409     (while (not ended)
3410       (forward-line 1)
3411       (if (looking-at "[ \t]+[^ \t]")
3412           (forward-line 1)
3413         (setq ended t)))))
3414
3415 (defun article-treat-date ()
3416   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3417                        (with-current-buffer gnus-summary-buffer
3418                          gnus-article-date-headers)
3419                      gnus-article-date-headers)
3420                    t))
3421
3422 (defun article-date-ut (&optional type highlight date-position)
3423   "Convert DATE date to TYPE in the current article.
3424 The default type is `ut'.  See `gnus-article-date-headers' for
3425 possible values."
3426   (interactive (list 'ut t))
3427   (let* ((case-fold-search t)
3428          (inhibit-read-only t)
3429          (inhibit-point-motion-hooks t)
3430          (first t)
3431          (visible-date (mail-fetch-field "Date"))
3432          pos date bface eface)
3433     (save-excursion
3434       (if date-position
3435           (progn
3436             (goto-char date-position)
3437             (setq date (get-text-property (point) 'original-date))
3438             (when (looking-at "[^:]+:[\t ]*")
3439               (setq bface (get-text-property (match-beginning 0) 'face)
3440                     eface (get-text-property (match-end 0) 'face)))
3441             (delete-region (point)
3442                            (progn
3443                              (gnus-article-forward-header)
3444                              (point)))
3445             (article-transform-date date type bface eface))
3446         (save-restriction
3447           (widen)
3448           (goto-char (point-min))
3449           (while (or (get-text-property (setq pos (point)) 'original-date)
3450                      (and (setq pos (next-single-property-change
3451                                      (point) 'original-date))
3452                           (goto-char pos)))
3453             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3454                                       (1+ (match-beginning 0))
3455                                     (point-max)))
3456             (while (setq pos (text-property-not-all pos (point-max)
3457                                                     'gnus-date-type nil))
3458               (setq date (get-text-property pos 'original-date))
3459               (goto-char pos)
3460               (when (looking-at "[^:]+:[\t ]*")
3461                 (setq bface (get-text-property (match-beginning 0) 'face)
3462                       eface (get-text-property (match-end 0) 'face)))
3463               (delete-region pos (or (text-property-any pos (point-max)
3464                                                         'gnus-date-type nil)
3465                                      (point-max))))
3466             (unless date ;; the 1st time
3467               (goto-char (point-min))
3468               (while (re-search-forward "^Date:[\t ]*" nil t)
3469                 (setq date (get-text-property (match-beginning 0)
3470                                               'original-date)
3471                       bface (get-text-property (match-beginning 0) 'face)
3472                       eface (get-text-property (match-end 0) 'face))
3473                 (delete-region (point-at-bol) (progn
3474                                                 (gnus-article-forward-header)
3475                                                 (point)))))
3476             (when (and (not date)
3477                        visible-date)
3478               (setq date visible-date))
3479             (when date
3480               (article-transform-date date type bface eface))
3481             (goto-char (point-max))
3482             (widen)))))))
3483
3484 (defun article-transform-date (date type bface eface)
3485   (dolist (this-type (cond
3486                       ((null type)
3487                        (list 'ut))
3488                       ((atom type)
3489                        (list type))
3490                       (t
3491                        type)))
3492     (goto-char
3493      (prog1
3494          (point)
3495        (add-text-properties
3496         (point)
3497         (progn
3498           (insert (article-make-date-line date (or this-type 'ut)) "\n")
3499           (point))
3500         (list 'original-date date 'gnus-date-type this-type))))
3501     ;; Do highlighting.
3502     (when (looking-at
3503            "\\([^:]+:\\)[\t ]*\\(\\(?:[^\t\n ]+[\t ]+\\)*[^\t\n ]+\\)?")
3504       (put-text-property (match-beginning 1) (match-end 1) 'face bface)
3505       (when (match-beginning 2)
3506         (put-text-property (match-beginning 2) (match-end 2) 'face eface))
3507       (while (and (zerop (forward-line 1))
3508                   (looking-at "[\t ]+\\(\\(?:[^\t\n ]+[\t ]+\\)*[^\t\n ]+\\)?"))
3509         (when (match-beginning 1)
3510           (put-text-property (match-beginning 1) (match-end 1) 'face eface))))))
3511
3512 (defun article-make-date-line (date type)
3513   "Return a DATE line of TYPE."
3514   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3515                              combined-lapsed))
3516     (error "Unknown conversion type: %s" type))
3517   (condition-case ()
3518       (let ((time (ignore-errors (date-to-time date))))
3519         (cond
3520          ;; Convert to the local timezone.
3521          ((eq type 'local)
3522           (concat "Date: " (message-make-date time)))
3523          ;; Convert to Universal Time.
3524          ((eq type 'ut)
3525           (concat "Date: "
3526                   (substring
3527                    (message-make-date
3528                     (let* ((e (parse-time-string date))
3529                            (tm (apply 'encode-time e))
3530                            (ms (car tm))
3531                            (ls (- (cadr tm) (car (current-time-zone time)))))
3532                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3533                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3534                             (t (list ms ls)))))
3535                    0 -5)
3536                   "UT"))
3537          ;; Get the original date from the article.
3538          ((eq type 'original)
3539           (concat "Date: " (if (string-match "\n+$" date)
3540                                (substring date 0 (match-beginning 0))
3541                              date)))
3542          ;; Let the user define the format.
3543          ((eq type 'user-defined)
3544           (let ((format (or (condition-case nil
3545                                 (with-current-buffer gnus-summary-buffer
3546                                   gnus-article-time-format)
3547                               (error nil))
3548                             gnus-article-time-format)))
3549             (if (functionp format)
3550                 (funcall format time)
3551               (concat "Date: " (format-time-string format time)))))
3552          ;; ISO 8601.
3553          ((eq type 'iso8601)
3554           (let ((tz (car (current-time-zone time))))
3555             (concat
3556              "Date: "
3557              (format-time-string "%Y%m%dT%H%M%S" time)
3558              (format "%s%02d%02d"
3559                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3560                      (/ (% (abs tz) 3600) 60)))))
3561          ;; Do a lapsed format.
3562          ((eq type 'lapsed)
3563           (concat "Date: " (article-lapsed-string time)))
3564          ;; A combined date/lapsed format.
3565          ((eq type 'combined-lapsed)
3566           (let ((date-string (article-make-date-line date 'original))
3567                 (segments 3)
3568                 lapsed-string)
3569             (while (and
3570                     time
3571                     (setq lapsed-string
3572                           (concat " (" (article-lapsed-string time segments) ")"))
3573                     (> (+ (length date-string)
3574                           (length lapsed-string))
3575                        (+ fill-column 6))
3576                     (> segments 0))
3577               (setq segments (1- segments)))
3578             (if (> segments 0)
3579                 (concat date-string lapsed-string)
3580               date-string)))
3581          ;; Display the date in proper English
3582          ((eq type 'english)
3583           (let ((dtime (decode-time time)))
3584             (concat
3585              "Date: the "
3586              (number-to-string (nth 3 dtime))
3587              (let ((digit (% (nth 3 dtime) 10)))
3588                (cond
3589                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3590                 ((= digit 1) "st")
3591                 ((= digit 2) "nd")
3592                 ((= digit 3) "rd")
3593                 (t "th")))
3594              " of "
3595              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3596              " "
3597              (number-to-string (nth 5 dtime))
3598              " at "
3599              (format "%02d" (nth 2 dtime))
3600              ":"
3601              (format "%02d" (nth 1 dtime)))))))
3602     (foo
3603      (format "Date: %s (from Gnus)" date))))
3604
3605 (defun article-lapsed-string (time &optional max-segments)
3606   ;; If the date is seriously mangled, the timezone functions are
3607   ;; liable to bug out, so we ignore all errors.
3608   (let* ((now (current-time))
3609          (real-time (subtract-time now time))
3610          (real-sec (and real-time
3611                         (+ (* (float (car real-time)) 65536)
3612                            (cadr real-time))))
3613          (sec (and real-time (abs real-sec)))
3614          (segments 0)
3615          num prev)
3616     (unless max-segments
3617       (setq max-segments (length article-time-units)))
3618     (cond
3619      ((null real-time)
3620       "Unknown")
3621      ((zerop sec)
3622       "Now")
3623      (t
3624       (concat
3625        ;; This is a bit convoluted, but basically we go
3626        ;; through the time units for years, weeks, etc,
3627        ;; and divide things to see whether that results
3628        ;; in positive answers.
3629        (mapconcat
3630         (lambda (unit)
3631           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3632                   (>= segments max-segments))
3633               ;; The (remaining) seconds are too few to
3634               ;; be divided into this time unit.
3635               ""
3636             ;; It's big enough, so we output it.
3637             (setq sec (- sec (* num (cdr unit))))
3638             (prog1
3639                 (concat (if prev ", " "") (int-to-string
3640                                            (floor num))
3641                         " " (symbol-name (car unit))
3642                         (if (> num 1) "s" ""))
3643               (setq prev t
3644                     segments (1+ segments)))))
3645         article-time-units "")
3646        ;; If dates are odd, then it might appear like the
3647        ;; article was sent in the future.
3648        (if (> real-sec 0)
3649            " ago"
3650          " in the future"))))))
3651
3652 (defun article-date-local (&optional highlight)
3653   "Convert the current article date to the local timezone."
3654   (interactive (list t))
3655   (article-date-ut 'local highlight))
3656
3657 (defun article-date-english (&optional highlight)
3658   "Convert the current article date to something that is proper English."
3659   (interactive (list t))
3660   (article-date-ut 'english highlight))
3661
3662 (defun article-date-original (&optional highlight)
3663   "Convert the current article date to what it was originally.
3664 This is only useful if you have used some other date conversion
3665 function and want to see what the date was before converting."
3666   (interactive (list t))
3667   (article-date-ut 'original highlight))
3668
3669 (defun article-date-lapsed (&optional highlight)
3670   "Convert the current article date to time lapsed since it was sent."
3671   (interactive (list t))
3672   (article-date-ut 'lapsed highlight))
3673
3674 (defun article-date-combined-lapsed (&optional highlight)
3675   "Convert the current article date to time lapsed since it was sent."
3676   (interactive (list t))
3677   (article-date-ut 'combined-lapsed highlight))
3678
3679 (defun article-update-date-lapsed ()
3680   "Function to be run from a timer to update the lapsed time line."
3681   (save-match-data
3682     (let ((buffer (current-buffer)))
3683       (ignore-errors
3684         (walk-windows
3685          (lambda (w)
3686            (set-buffer (window-buffer w))
3687            (when (eq major-mode 'gnus-article-mode)
3688              (let ((old-line (count-lines (point-min) (point)))
3689                    (old-column (- (point) (line-beginning-position)))
3690                    (window-start (window-start w))
3691                    (pos (point-min))
3692                    type next end)
3693                (while (setq pos (text-property-not-all pos (point-max)
3694                                                        'gnus-date-type nil))
3695                  (setq next (or (next-single-property-change pos
3696                                                              'gnus-date-type)
3697                                 (point-max)))
3698                  (setq type (get-text-property pos 'gnus-date-type))
3699                  (when (memq type '(lapsed combined-lapsed user-defined))
3700                    (article-date-ut type t pos)
3701                    (setq end (or (next-single-property-change pos
3702                                                               'gnus-date-type)
3703                                  (point-max)))
3704                    (when window-start
3705                      (if (/= window-start next)
3706                          (setq window-start nil)
3707                        (set-window-start w end)))
3708                    (setq next end))
3709                  (setq pos next))
3710                (goto-char (point-min))
3711                (when (> old-column 0)
3712                  (setq old-line (1- old-line)))
3713                (forward-line old-line)
3714                (end-of-line)
3715                (when (> (current-column) old-column)
3716                  (beginning-of-line)
3717                  (forward-char old-column)))))
3718          nil 'visible))
3719       (set-buffer buffer))))
3720
3721 (defun gnus-start-date-timer (&optional n)
3722   "Start a timer to update the Date headers in the article buffers.
3723 The numerical prefix says how frequently (in seconds) the function
3724 is to run."
3725   (interactive "p")
3726   (unless n
3727     (setq n 1))
3728   (gnus-stop-date-timer)
3729   (setq article-lapsed-timer
3730         (run-at-time 1 n 'article-update-date-lapsed)))
3731
3732 (defun gnus-stop-date-timer ()
3733   "Stop the Date timer."
3734   (interactive)
3735   (when article-lapsed-timer
3736     (nnheader-cancel-timer article-lapsed-timer)
3737     (setq article-lapsed-timer nil)))
3738
3739 (defun article-date-user (&optional highlight)
3740   "Convert the current article date to the user-defined format.
3741 This format is defined by the `gnus-article-time-format' variable."
3742   (interactive (list t))
3743   (article-date-ut 'user highlight))
3744
3745 (defun article-date-iso8601 (&optional highlight)
3746   "Convert the current article date to ISO8601."
3747   (interactive (list t))
3748   (article-date-ut 'iso8601 highlight))
3749
3750 (defmacro gnus-article-save-original-date (&rest forms)
3751   "Save the original date as a text property and evaluate FORMS."
3752   `(let* ((case-fold-search t)
3753           (start (progn
3754                    (goto-char (point-min))
3755                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3756                               (not (bolp)))
3757                      (match-end 0))))
3758           (date (when (and start
3759                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3760                                               nil t))
3761                   (buffer-substring-no-properties start
3762                                                   (match-beginning 0)))))
3763      (goto-char (point-max))
3764      (skip-chars-backward "\n")
3765      (put-text-property (point-min) (point) 'original-date date)
3766      ,@forms
3767      (goto-char (point-max))
3768      (skip-chars-backward "\n")
3769      (put-text-property (point-min) (point) 'original-date date)))
3770
3771 ;; (defun article-show-all ()
3772 ;;   "Show all hidden text in the article buffer."
3773 ;;   (interactive)
3774 ;;   (save-excursion
3775 ;;     (let ((inhibit-read-only t))
3776 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3777
3778 (defun article-remove-leading-whitespace ()
3779   "Remove excessive whitespace from all headers."
3780   (interactive)
3781   (save-excursion
3782     (save-restriction
3783       (let ((inhibit-read-only t))
3784         (article-narrow-to-head)
3785         (goto-char (point-min))
3786         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3787           (delete-region (match-beginning 1) (match-end 1)))))))
3788
3789 (defun article-emphasize (&optional arg)
3790   "Emphasize text according to `gnus-emphasis-alist'."
3791   (interactive (gnus-article-hidden-arg))
3792   (unless (gnus-article-check-hidden-text 'emphasis arg)
3793     (save-excursion
3794       (let ((alist (or
3795                     (condition-case nil
3796                         (with-current-buffer gnus-summary-buffer
3797                           gnus-article-emphasis-alist)
3798                       (error))
3799                     gnus-emphasis-alist))
3800             (inhibit-read-only t)
3801             (props (append '(article-type emphasis)
3802                            gnus-hidden-properties))
3803             regexp elem beg invisible visible face)
3804         (article-goto-body)
3805         (setq beg (point))
3806         (while (setq elem (pop alist))
3807           (goto-char beg)
3808           (setq regexp (car elem)
3809                 invisible (nth 1 elem)
3810                 visible (nth 2 elem)
3811                 face (nth 3 elem))
3812           (while (re-search-forward regexp nil t)
3813             (when (and (match-beginning visible) (match-beginning invisible))
3814               (gnus-article-hide-text
3815                (match-beginning invisible) (match-end invisible) props)
3816               (gnus-article-unhide-text-type
3817                (match-beginning visible) (match-end visible) 'emphasis)
3818               (gnus-put-overlay-excluding-newlines
3819                (match-beginning visible) (match-end visible) 'face face)
3820               (gnus-add-wash-type 'emphasis)
3821               (goto-char (match-end invisible)))))))))
3822
3823 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3824   "Setup newsgroup emphasis alist."
3825   (unless gnus-article-emphasis-alist
3826     (let ((name (and gnus-newsgroup-name
3827                      (gnus-group-real-name gnus-newsgroup-name))))
3828       (make-local-variable 'gnus-article-emphasis-alist)
3829       (setq gnus-article-emphasis-alist
3830             (nconc
3831              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3832                (while (setq elem (pop alist))
3833                  (when (and name (string-match (car elem) name))
3834                    (setq alist nil
3835                          highlight (copy-sequence (cdr elem)))))
3836                highlight)
3837              (copy-sequence highlight-words)
3838              (if gnus-newsgroup-name
3839                  (copy-sequence (gnus-group-find-parameter
3840                                  gnus-newsgroup-name 'highlight-words t)))
3841              gnus-emphasis-alist)))))
3842
3843 (defvar gnus-summary-article-menu)
3844 (defvar gnus-summary-post-menu)
3845
3846 ;;; Saving functions.
3847
3848 (defun gnus-article-save (save-buffer file &optional num)
3849   "Save the currently selected article."
3850   (when (or (get gnus-default-article-saver :headers)
3851             (not gnus-save-all-headers))
3852     ;; Remove headers according to `gnus-saved-headers' or the value
3853     ;; of the `:headers' property that the saver function might have.
3854     (let ((gnus-visible-headers
3855            (or (symbol-value (get gnus-default-article-saver :headers))
3856                gnus-saved-headers gnus-visible-headers))
3857           ;; Ignore group parameter.  See `article-hide-headers'.
3858           (gnus-summary-buffer nil))
3859       (with-current-buffer save-buffer
3860         (article-hide-headers 1 t))))
3861   (save-window-excursion
3862     (if (not gnus-default-article-saver)
3863         (error "No default saver is defined")
3864       ;; !!! Magic!  The saving functions all save
3865       ;; `gnus-save-article-buffer' (or so they think), but we
3866       ;; bind that variable to our save-buffer.
3867       (set-buffer gnus-article-buffer)
3868       (let* ((gnus-save-article-buffer save-buffer)
3869              (filename
3870               (cond
3871                ((not gnus-prompt-before-saving) 'default)
3872                ((eq gnus-prompt-before-saving 'always) nil)
3873                (t file)))
3874              (gnus-number-of-articles-to-be-saved
3875               (when (eq gnus-prompt-before-saving t)
3876                 num)))                  ; Magic
3877         (set-buffer gnus-article-current-summary)
3878         (funcall gnus-default-article-saver filename)))))
3879
3880 (defun gnus-read-save-file-name (prompt &optional filename
3881                                         function group headers variable
3882                                         dir-var)
3883   (let ((default-name
3884           (funcall function group headers (symbol-value variable)))
3885         result)
3886     (setq result
3887           (expand-file-name
3888            (cond
3889             ((eq filename 'default)
3890              default-name)
3891             ((eq filename t)
3892              default-name)
3893             (filename filename)
3894             (t
3895              (when (symbol-value dir-var)
3896                (setq default-name (expand-file-name
3897                                    (file-name-nondirectory default-name)
3898                                    (symbol-value dir-var))))
3899              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3900                     (prompt
3901                      (format prompt
3902                              (if (and gnus-number-of-articles-to-be-saved
3903                                       (> gnus-number-of-articles-to-be-saved 1))
3904                                  (format "these %d articles"
3905                                          gnus-number-of-articles-to-be-saved)
3906                                "this article")))
3907                     (file
3908                      ;; Let the split methods have their say.
3909                      (cond
3910                       ;; No split name was found.
3911                       ((null split-name)
3912                        (read-file-name
3913                         (concat prompt " (default "
3914                                 (file-name-nondirectory default-name) "): ")
3915                         (file-name-directory default-name)
3916                         default-name))
3917                       ;; A single group name is returned.
3918                       ((stringp split-name)
3919                        (setq default-name
3920                              (funcall function split-name headers
3921                                       (symbol-value variable)))
3922                        (read-file-name
3923                         (concat prompt " (default "
3924                                 (file-name-nondirectory default-name) "): ")
3925                         (file-name-directory default-name)
3926                         default-name))
3927                       ;; A single split name was found
3928                       ((= 1 (length split-name))
3929                        (let* ((name (expand-file-name
3930                                      (car split-name)
3931                                      gnus-article-save-directory))
3932                               (dir (cond ((file-directory-p name)
3933                                           (file-name-as-directory name))
3934                                          ((file-exists-p name) name)
3935                                          (t gnus-article-save-directory))))
3936                          (read-file-name
3937                           (concat prompt " (default " name "): ")
3938                           dir name)))
3939                       ;; A list of splits was found.
3940                       (t
3941                        (setq split-name (nreverse split-name))
3942                        (let (result)
3943                          (let ((file-name-history
3944                                 (nconc split-name file-name-history)))
3945                            (setq result
3946                                  (expand-file-name
3947                                   (read-file-name
3948                                    (concat prompt " (`M-p' for defaults): ")
3949                                    gnus-article-save-directory
3950                                    (car split-name))
3951                                   gnus-article-save-directory)))
3952                          (car (push result file-name-history)))))))
3953                ;; Create the directory.
3954                (gnus-make-directory (file-name-directory file))
3955                ;; If we have read a directory, we append the default file name.
3956                (when (file-directory-p file)
3957                  (setq file (expand-file-name (file-name-nondirectory
3958                                                default-name)
3959                                               (file-name-as-directory file))))
3960                ;; Possibly translate some characters.
3961                (nnheader-translate-file-chars file))))))
3962     (gnus-make-directory (file-name-directory result))
3963     (when variable
3964       (set variable result))
3965     (when dir-var
3966       (set dir-var (file-name-directory result)))
3967     result))
3968
3969 (defun gnus-article-archive-name (group)
3970   "Return the first instance of an \"Archive-name\" in the current buffer."
3971   (let ((case-fold-search t))
3972     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3973       (nnheader-concat gnus-article-save-directory
3974                        (match-string 1)))))
3975
3976 (defun gnus-article-nndoc-name (group)
3977   "If GROUP is an nndoc group, return the name of the parent group."
3978   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3979     (gnus-group-get-parameter group 'save-article-group)))
3980
3981 (defun gnus-summary-save-in-rmail (&optional filename)
3982   "Append this article to Rmail file.
3983 Optional argument FILENAME specifies file name.
3984 Directory to save to is default to `gnus-article-save-directory'."
3985   (setq filename (gnus-read-save-file-name
3986                   "Save %s in rmail file" filename
3987                   gnus-rmail-save-name gnus-newsgroup-name
3988                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3989   (with-current-buffer gnus-save-article-buffer
3990     (save-excursion
3991       (save-restriction
3992         (widen)
3993         ;; Note that unlike gnus-summary-save-in-mail, there is no
3994         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3995         ;; not use Babyl.
3996         (gnus-output-to-rmail filename))))
3997   filename)
3998
3999 (defun gnus-summary-save-in-mail (&optional filename)
4000   "Append this article to Unix mail file.
4001 Optional argument FILENAME specifies file name.
4002 Directory to save to is default to `gnus-article-save-directory'."
4003   (setq filename (gnus-read-save-file-name
4004                   "Save %s in Unix mail file" filename
4005                   gnus-mail-save-name gnus-newsgroup-name
4006                   gnus-current-headers 'gnus-newsgroup-last-mail))
4007   (with-current-buffer gnus-save-article-buffer
4008     (save-excursion
4009       (save-restriction
4010         (widen)
4011         (if (and (file-readable-p filename)
4012                  (file-regular-p filename)
4013                  (mail-file-babyl-p filename))
4014             (gnus-output-to-rmail filename)
4015           (gnus-output-to-mail filename)))))
4016   filename)
4017
4018 (put 'gnus-summary-save-in-file :decode t)
4019 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4020 (defun gnus-summary-save-in-file (&optional filename overwrite)
4021   "Append this article to file.
4022 Optional argument FILENAME specifies file name.
4023 Directory to save to is default to `gnus-article-save-directory'."
4024   (setq filename (gnus-read-save-file-name
4025                   "Save %s in file" filename
4026                   gnus-file-save-name gnus-newsgroup-name
4027                   gnus-current-headers 'gnus-newsgroup-last-file))
4028   (with-current-buffer gnus-save-article-buffer
4029     (save-excursion
4030       (save-restriction
4031         (widen)
4032         (when (and overwrite
4033                    (file-exists-p filename))
4034           (delete-file filename))
4035         (gnus-output-to-file filename))))
4036   filename)
4037
4038 (put 'gnus-summary-write-to-file :decode t)
4039 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4040 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4041 (defun gnus-summary-write-to-file (&optional filename)
4042   "Write this article to a file, overwriting it if the file exists.
4043 Optional argument FILENAME specifies file name.
4044 The directory to save in defaults to `gnus-article-save-directory'."
4045   (setq filename (gnus-read-save-file-name
4046                   "Save %s in file" filename
4047                   gnus-file-save-name gnus-newsgroup-name
4048                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4049   (gnus-summary-save-in-file filename t))
4050
4051 (put 'gnus-summary-save-body-in-file :decode t)
4052 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4053   "Append this article body to a file.
4054 Optional argument FILENAME specifies file name.
4055 The directory to save in defaults to `gnus-article-save-directory'."
4056   (setq filename (gnus-read-save-file-name
4057                   "Save %s body in file" filename
4058                   gnus-file-save-name gnus-newsgroup-name
4059                   gnus-current-headers 'gnus-newsgroup-last-file))
4060   (with-current-buffer gnus-save-article-buffer
4061     (save-excursion
4062       (save-restriction
4063         (widen)
4064         (when (article-goto-body)
4065           (narrow-to-region (point) (point-max)))
4066         (when (and overwrite
4067                    (file-exists-p filename))
4068           (delete-file filename))
4069         (gnus-output-to-file filename))))
4070   filename)
4071
4072 (put 'gnus-summary-write-body-to-file :decode t)
4073 (put 'gnus-summary-write-body-to-file
4074      :function 'gnus-summary-save-body-in-file)
4075 (defun gnus-summary-write-body-to-file (&optional filename)
4076   "Write this article body to a file, overwriting it if the file exists.
4077 Optional argument FILENAME specifies file name.
4078 The directory to save in defaults to `gnus-article-save-directory'."
4079   (setq filename (gnus-read-save-file-name
4080                   "Save %s body in file" filename
4081                   gnus-file-save-name gnus-newsgroup-name
4082                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4083   (gnus-summary-save-body-in-file filename t))
4084
4085 (put 'gnus-summary-save-in-pipe :decode t)
4086 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4087 (defun gnus-summary-save-in-pipe (&optional command raw)
4088   "Pipe this article to subprocess COMMAND.
4089 Valid values for COMMAND include:
4090   a string
4091     The executable command name and possibly arguments.
4092   nil
4093     You will be prompted for the command in the minibuffer.
4094   the symbol `default'
4095     It will be replaced with the command which the variable
4096     `gnus-summary-pipe-output-default-command' holds or the command
4097     last used for saving.
4098 Non-nil value for RAW overrides `:decode' and `:headers' properties
4099 and the raw article including all headers will be piped."
4100   (let ((article (gnus-summary-article-number))
4101         (decode (unless raw
4102                   (get 'gnus-summary-save-in-pipe :decode)))
4103         save-buffer default)
4104     (if article
4105         (if (vectorp (gnus-summary-article-header article))
4106             (save-current-buffer
4107               (gnus-summary-select-article decode decode nil article)
4108               (insert-buffer-substring
4109                (prog1
4110                    (if decode
4111                        gnus-article-buffer
4112                      gnus-original-article-buffer)
4113                  (setq save-buffer
4114                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4115               ;; Remove unwanted headers.
4116               (when (and (not raw)
4117                          (or (get 'gnus-summary-save-in-pipe :headers)
4118                              (not gnus-save-all-headers)))
4119                 (let ((gnus-visible-headers
4120                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4121                                               :headers))
4122                            gnus-saved-headers gnus-visible-headers))
4123                       (gnus-summary-buffer nil))
4124                   (article-hide-headers 1 t))))
4125           (error "%d is not a real article" article))
4126       (error "No article to pipe"))
4127     (setq default (or gnus-summary-pipe-output-default-command
4128                       gnus-last-shell-command))
4129     (unless (stringp command)
4130       (setq command
4131             (if (and (eq command 'default) default)
4132                 default
4133               (gnus-read-shell-command "Shell command on this article: "
4134                                        default))))
4135     (when (string-equal command "")
4136       (if default
4137           (setq command default)
4138         (error "A command is required")))
4139     (with-current-buffer save-buffer
4140       (save-restriction
4141         (widen)
4142         (shell-command-on-region (point-min) (point-max) command nil)))
4143     (gnus-kill-buffer save-buffer))
4144   (setq gnus-summary-pipe-output-default-command command))
4145
4146 (defun gnus-summary-pipe-to-muttprint (&optional command)
4147   "Pipe this article to muttprint."
4148   (unless (stringp command)
4149     (setq command (read-string
4150                    "Print using command: " gnus-summary-muttprint-program
4151                    nil gnus-summary-muttprint-program)))
4152   (let ((gnus-summary-pipe-output-default-command
4153          gnus-summary-pipe-output-default-command))
4154     (gnus-summary-save-in-pipe command))
4155   (setq gnus-summary-muttprint-program command))
4156
4157 ;;; Article file names when saving.
4158
4159 (defun gnus-capitalize-newsgroup (newsgroup)
4160   "Capitalize NEWSGROUP name."
4161   (when (not (zerop (length newsgroup)))
4162     (concat (char-to-string (upcase (aref newsgroup 0)))
4163             (substring newsgroup 1))))
4164
4165 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4166   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4167 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4168 Otherwise, it is like ~/News/news/group/num."
4169   (let ((default
4170           (expand-file-name
4171            (concat (if (gnus-use-long-file-name 'not-save)
4172                        (gnus-capitalize-newsgroup newsgroup)
4173                      (gnus-newsgroup-directory-form newsgroup))
4174                    "/" (int-to-string (mail-header-number headers)))
4175            gnus-article-save-directory)))
4176     (if (and last-file
4177              (string-equal (file-name-directory default)
4178                            (file-name-directory last-file))
4179              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4180         default
4181       (or last-file default))))
4182
4183 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4184   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4185 If variable `gnus-use-long-file-name' is non-nil, it is
4186 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4187   (let ((default
4188           (expand-file-name
4189            (concat (if (gnus-use-long-file-name 'not-save)
4190                        newsgroup
4191                      (gnus-newsgroup-directory-form newsgroup))
4192                    "/" (int-to-string (mail-header-number headers)))
4193            gnus-article-save-directory)))
4194     (if (and last-file
4195              (string-equal (file-name-directory default)
4196                            (file-name-directory last-file))
4197              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4198         default
4199       (or last-file default))))
4200
4201 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4202   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4203 If variable `gnus-use-long-file-name' is non-nil, it is
4204 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4205   (or last-file
4206       (expand-file-name
4207        (if (gnus-use-long-file-name 'not-save)
4208            newsgroup
4209          (file-relative-name
4210           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4211           default-directory))
4212        gnus-article-save-directory)))
4213
4214 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4215   "Generate file name from sender."
4216   (let ((from (mail-header-from headers)))
4217     (expand-file-name
4218      (if (and from (string-match "\\([^ <]+\\)@" from))
4219          (match-string 1 from)
4220        "nobody")
4221      gnus-article-save-directory)))
4222
4223 (defun article-verify-x-pgp-sig ()
4224   "Verify X-PGP-Sig."
4225   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4226   (interactive)
4227   (if (gnus-buffer-live-p gnus-original-article-buffer)
4228       (let ((sig (with-current-buffer gnus-original-article-buffer
4229                    (gnus-fetch-field "X-PGP-Sig")))
4230             items info headers)
4231         (when (and sig
4232                    mml2015-use
4233                    (mml2015-clear-verify-function))
4234           (with-temp-buffer
4235             (insert-buffer-substring gnus-original-article-buffer)
4236             (setq items (split-string sig))
4237             (message-narrow-to-head)
4238             (let ((inhibit-point-motion-hooks t)
4239                   (case-fold-search t))
4240               ;; Don't verify multiple headers.
4241               (setq headers (mapconcat (lambda (header)
4242                                          (concat header ": "
4243                                                  (mail-fetch-field header)
4244                                                  "\n"))
4245                                        (split-string (nth 1 items) ",") "")))
4246             (delete-region (point-min) (point-max))
4247             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4248             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4249             (insert headers)
4250             (widen)
4251             (forward-line)
4252             (while (not (eobp))
4253               (if (looking-at "^-")
4254                   (insert "- "))
4255               (forward-line))
4256             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4257             (insert "Version: " (car items) "\n\n")
4258             (insert (mapconcat 'identity (cddr items) "\n"))
4259             (insert "\n-----END PGP SIGNATURE-----\n")
4260             (let ((mm-security-handle (list (format "multipart/signed"))))
4261               (mml2015-clean-buffer)
4262               (let ((coding-system-for-write (or gnus-newsgroup-charset
4263                                                  'iso-8859-1)))
4264                 (funcall (mml2015-clear-verify-function)))
4265               (setq info
4266                     (or (mm-handle-multipart-ctl-parameter
4267                          mm-security-handle 'gnus-details)
4268                         (mm-handle-multipart-ctl-parameter
4269                          mm-security-handle 'gnus-info)))))
4270           (when info
4271             (let ((inhibit-read-only t) bface eface)
4272               (save-restriction
4273                 (message-narrow-to-head)
4274                 (goto-char (point-max))
4275                 (forward-line -1)
4276                 (setq bface (get-text-property (point-at-bol) 'face)
4277                       eface (get-text-property (1- (point-at-eol)) 'face))
4278                 (message-remove-header "X-Gnus-PGP-Verify")
4279                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4280                     (forward-line)
4281                   (goto-char (point-max)))
4282                 (narrow-to-region (point) (point))
4283                 (insert "X-Gnus-PGP-Verify: " info "\n")
4284                 (goto-char (point-min))
4285                 (forward-line)
4286                 (while (not (eobp))
4287                   (if (not (looking-at "^[ \t]"))
4288                       (insert " "))
4289                   (forward-line))
4290                 ;; Do highlighting.
4291                 (goto-char (point-min))
4292                 (when (looking-at "\\([^:]+\\): *")
4293                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4294                                      'face bface)
4295                   (put-text-property (match-end 0) (point-max)
4296                                      'face eface)))))))))
4297
4298 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4299
4300 (defun article-verify-cancel-lock ()
4301   "Verify Cancel-Lock header."
4302   (interactive)
4303   (if (gnus-buffer-live-p gnus-original-article-buffer)
4304       (canlock-verify gnus-original-article-buffer)))
4305
4306 (eval-and-compile
4307   (mapc
4308    (lambda (func)
4309      (let (afunc gfunc)
4310        (if (consp func)
4311            (setq afunc (car func)
4312                  gfunc (cdr func))
4313          (setq afunc func
4314                gfunc (intern (format "gnus-%s" func))))
4315        (defalias gfunc
4316          (when (fboundp afunc)
4317            `(lambda (&optional interactive &rest args)
4318               ,(documentation afunc t)
4319               (interactive (list t))
4320               (with-current-buffer gnus-article-buffer
4321                 (if interactive
4322                     (call-interactively ',afunc)
4323                   (apply ',afunc args))))))))
4324    '(article-hide-headers
4325      article-verify-x-pgp-sig
4326      article-verify-cancel-lock
4327      article-hide-boring-headers
4328      article-treat-overstrike
4329      article-treat-ansi-sequences
4330      article-fill-long-lines
4331      article-capitalize-sentences
4332      article-remove-cr
4333      article-remove-leading-whitespace
4334      article-display-x-face
4335      article-display-face
4336      article-de-quoted-unreadable
4337      article-de-base64-unreadable
4338      article-decode-HZ
4339      article-wash-html
4340      article-unsplit-urls
4341      article-hide-list-identifiers
4342      article-strip-banner
4343      article-babel
4344      article-hide-pem
4345      article-hide-signature
4346      article-strip-headers-in-body
4347      article-remove-trailing-blank-lines
4348      article-strip-leading-blank-lines
4349      article-strip-multiple-blank-lines
4350      article-strip-leading-space
4351      article-strip-trailing-space
4352      article-strip-blank-lines
4353      article-strip-all-blank-lines
4354      article-date-local
4355      article-date-english
4356      article-date-iso8601
4357      article-date-original
4358      article-treat-date
4359      article-date-ut
4360      article-decode-mime-words
4361      article-decode-charset
4362      article-decode-encoded-words
4363      article-date-user
4364      article-date-lapsed
4365      article-date-combined-lapsed
4366      article-emphasize
4367      article-treat-dumbquotes
4368      article-treat-non-ascii
4369      article-normalize-headers
4370      ;;(article-show-all . gnus-article-show-all-headers)
4371      )))
4372 \f
4373 ;;;
4374 ;;; Gnus article mode
4375 ;;;
4376
4377 (put 'gnus-article-mode 'mode-class 'special)
4378
4379 (set-keymap-parent gnus-article-mode-map widget-keymap)
4380
4381 (gnus-define-keys gnus-article-mode-map
4382   " " gnus-article-goto-next-page
4383   [?\S-\ ] gnus-article-goto-prev-page
4384   "\177" gnus-article-goto-prev-page
4385   [delete] gnus-article-goto-prev-page
4386   [backspace] gnus-article-goto-prev-page
4387   "\C-c^" gnus-article-refer-article
4388   "h" gnus-article-show-summary
4389   "s" gnus-article-show-summary
4390   "\C-c\C-m" gnus-article-mail
4391   "?" gnus-article-describe-briefly
4392   "<" beginning-of-buffer
4393   ">" end-of-buffer
4394   "\C-c\C-i" gnus-info-find-node
4395   "\C-c\C-b" gnus-bug
4396   "R" gnus-article-reply-with-original
4397   "F" gnus-article-followup-with-original
4398   "\C-hk" gnus-article-describe-key
4399   "\C-hc" gnus-article-describe-key-briefly
4400   "\C-hb" gnus-article-describe-bindings
4401
4402   "e" gnus-article-read-summary-keys
4403   "\C-d" gnus-article-read-summary-keys
4404   "\M-*" gnus-article-read-summary-keys
4405   "\M-#" gnus-article-read-summary-keys
4406   "\M-^" gnus-article-read-summary-keys
4407   "\M-g" gnus-article-read-summary-keys)
4408
4409 (substitute-key-definition
4410  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4411
4412 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4413   "W" gnus-article-wide-reply-with-original)
4414 (if (featurep 'xemacs)
4415     (set-keymap-default-binding gnus-article-send-map
4416                                 'gnus-article-read-summary-send-keys)
4417   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4418
4419 (defun gnus-article-make-menu-bar ()
4420   (unless (boundp 'gnus-article-commands-menu)
4421     (gnus-summary-make-menu-bar))
4422   (unless (boundp 'gnus-article-article-menu)
4423     (easy-menu-define
4424      gnus-article-article-menu gnus-article-mode-map ""
4425      '("Article"
4426        ["Scroll forwards" gnus-article-goto-next-page t]
4427        ["Scroll backwards" gnus-article-goto-prev-page t]
4428        ["Show summary" gnus-article-show-summary t]
4429        ["Fetch Message-ID at point" gnus-article-refer-article t]
4430        ["Mail to address at point" gnus-article-mail t]
4431        ["Send a bug report" gnus-bug t]))
4432
4433     (easy-menu-define
4434      gnus-article-treatment-menu gnus-article-mode-map ""
4435      ;; Fixme: this should use :active (and maybe :visible).
4436      '("Treatment"
4437        ["Hide headers" gnus-article-hide-headers t]
4438        ["Hide signature" gnus-article-hide-signature t]
4439        ["Hide citation" gnus-article-hide-citation t]
4440        ["Treat overstrike" gnus-article-treat-overstrike t]
4441        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4442        ["Remove carriage return" gnus-article-remove-cr t]
4443        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4444        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4445        ["Remove base64" gnus-article-de-base64-unreadable t]
4446        ["Treat html" gnus-article-wash-html t]
4447        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4448        ["Decode HZ" gnus-article-decode-HZ t]))
4449
4450     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4451
4452     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4453
4454     (gnus-run-hooks 'gnus-article-menu-hook)))
4455
4456 (defvar bookmark-make-record-function)
4457 (defvar shr-put-image-function)
4458
4459 (defun gnus-article-mode ()
4460   "Major mode for displaying an article.
4461
4462 All normal editing commands are switched off.
4463
4464 The following commands are available in addition to all summary mode
4465 commands:
4466 \\<gnus-article-mode-map>
4467 \\[gnus-article-next-page]\t Scroll the article one page forwards
4468 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4469 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4470 \\[gnus-article-show-summary]\t Display the summary buffer
4471 \\[gnus-article-mail]\t Send a reply to the address near point
4472 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4473 \\[gnus-info-find-node]\t Go to the Gnus info node"
4474   (interactive)
4475   (kill-all-local-variables)
4476   (gnus-simplify-mode-line)
4477   (setq mode-name "Article")
4478   (setq major-mode 'gnus-article-mode)
4479   (make-local-variable 'minor-mode-alist)
4480   (use-local-map gnus-article-mode-map)
4481   (when (gnus-visual-p 'article-menu 'menu)
4482     (gnus-article-make-menu-bar)
4483     (when gnus-summary-tool-bar-map
4484       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4485   (gnus-update-format-specifications nil 'article-mode)
4486   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4487   (set (make-local-variable 'gnus-page-broken) nil)
4488   (make-local-variable 'gnus-article-current-summary)
4489   (make-local-variable 'gnus-article-mime-handles)
4490   (make-local-variable 'gnus-article-decoded-p)
4491   (make-local-variable 'gnus-article-mime-handle-alist)
4492   (make-local-variable 'gnus-article-wash-types)
4493   (make-local-variable 'gnus-article-image-alist)
4494   (make-local-variable 'gnus-article-charset)
4495   (make-local-variable 'gnus-article-ignored-charsets)
4496   (set (make-local-variable 'bookmark-make-record-function)
4497        'gnus-summary-bookmark-make-record)
4498   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4499   ;; face.
4500   (set (make-local-variable 'nobreak-char-display) nil)
4501   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4502   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4503   (setq cursor-in-non-selected-windows nil)
4504   (gnus-set-default-directory)
4505   (buffer-disable-undo)
4506   (setq buffer-read-only t
4507         show-trailing-whitespace nil)
4508   (set-syntax-table gnus-article-mode-syntax-table)
4509   (mm-enable-multibyte)
4510   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4511
4512 (defun gnus-article-setup-buffer ()
4513   "Initialize the article buffer."
4514   (let* ((name (if gnus-single-article-buffer "*Article*"
4515                  (concat "*Article "
4516                          (gnus-group-decoded-name gnus-newsgroup-name)
4517                          "*")))
4518          (original
4519           (progn (string-match "\\*Article" name)
4520                  (concat " *Original Article"
4521                          (substring name (match-end 0))))))
4522     (setq gnus-article-buffer name)
4523     (setq gnus-original-article-buffer original)
4524     (setq gnus-article-mime-handle-alist nil)
4525     (with-current-buffer gnus-summary-buffer
4526       ;; This might be a variable local to the summary buffer.
4527       (unless gnus-single-article-buffer
4528         (setq gnus-article-buffer name)
4529         (setq gnus-original-article-buffer original)
4530         (gnus-set-global-variables)))
4531     (gnus-article-setup-highlight-words)
4532     ;; Init original article buffer.
4533     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4534       (mm-enable-multibyte)
4535       (setq major-mode 'gnus-original-article-mode)
4536       (make-local-variable 'gnus-original-article))
4537     (if (and (get-buffer name)
4538              (with-current-buffer name
4539                (if gnus-article-edit-mode
4540                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4541                        (progn
4542                          (set-buffer-modified-p nil)
4543                          (gnus-kill-buffer name)
4544                          (message "")
4545                          nil)
4546                      (error "Action aborted"))
4547                  t)))
4548         (with-current-buffer name
4549           (set (make-local-variable 'gnus-article-edit-mode) nil)
4550           (gnus-article-stop-animations)
4551           (when gnus-article-mime-handles
4552             (mm-destroy-parts gnus-article-mime-handles)
4553             (setq gnus-article-mime-handles nil))
4554           ;; Set it to nil in article-buffer!
4555           (setq gnus-article-mime-handle-alist nil)
4556           (buffer-disable-undo)
4557           (setq buffer-read-only t)
4558           (unless (eq major-mode 'gnus-article-mode)
4559             (gnus-article-mode))
4560           (setq truncate-lines gnus-article-truncate-lines)
4561           (current-buffer))
4562       (let ((summary gnus-summary-buffer))
4563         (with-current-buffer (gnus-get-buffer-create name)
4564           (gnus-article-mode)
4565           (setq truncate-lines gnus-article-truncate-lines)
4566           (set (make-local-variable 'gnus-summary-buffer) summary)
4567           (gnus-summary-set-local-parameters gnus-newsgroup-name)
4568           (when article-lapsed-timer
4569             (gnus-stop-date-timer))
4570           (when gnus-article-update-date-headers
4571             (gnus-start-date-timer gnus-article-update-date-headers))
4572           (current-buffer))))))
4573
4574 (defun gnus-article-stop-animations ()
4575   (dolist (timer (and (boundp 'timer-list)
4576                       timer-list))
4577     (when (eq (gnus-timer--function timer) 'image-animate-timeout)
4578       (cancel-timer timer))))
4579
4580 (defun gnus-stop-downloads ()
4581   (when (boundp 'url-queue)
4582     (set (intern "url-queue" obarray) nil)))
4583
4584 ;; Set article window start at LINE, where LINE is the number of lines
4585 ;; from the head of the article.
4586 (defun gnus-article-set-window-start (&optional line)
4587   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4588     (when article-window
4589       (set-window-start
4590        article-window
4591        (with-current-buffer gnus-article-buffer
4592          (goto-char (point-min))
4593          (if (not line)
4594              (point-min)
4595            (gnus-message 6 "Moved to bookmark")
4596            (search-forward "\n\n" nil t)
4597            (forward-line line)
4598            (point)))))))
4599
4600 (defun gnus-article-prepare (article &optional all-headers header)
4601   "Prepare ARTICLE in article mode buffer.
4602 ARTICLE should either be an article number or a Message-ID.
4603 If ARTICLE is an id, HEADER should be the article headers.
4604 If ALL-HEADERS is non-nil, no headers are hidden."
4605   (save-excursion
4606     ;; Make sure we start in a summary buffer.
4607     (unless (eq major-mode 'gnus-summary-mode)
4608       (set-buffer gnus-summary-buffer))
4609     (setq gnus-summary-buffer (current-buffer))
4610     (let* ((gnus-article (if header (mail-header-number header) article))
4611            (summary-buffer (current-buffer))
4612            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4613            (group gnus-newsgroup-name)
4614            result)
4615       (save-excursion
4616         (gnus-article-setup-buffer)
4617         (set-buffer gnus-article-buffer)
4618         ;; Deactivate active regions.
4619         (when (and (boundp 'transient-mark-mode)
4620                    transient-mark-mode)
4621           (setq mark-active nil))
4622         (if (not (setq result (let ((inhibit-read-only t))
4623                                 (gnus-request-article-this-buffer
4624                                  article group))))
4625             ;; There is no such article.
4626             (save-excursion
4627               (when (and (numberp article)
4628                          (not (memq article gnus-newsgroup-sparse)))
4629                 (setq gnus-article-current
4630                       (cons gnus-newsgroup-name article))
4631                 (set-buffer gnus-summary-buffer)
4632                 (setq gnus-current-article article)
4633                 (if (and (memq article gnus-newsgroup-undownloaded)
4634                          (not (gnus-online (gnus-find-method-for-group
4635                                             gnus-newsgroup-name))))
4636                     (progn
4637                       (gnus-summary-set-agent-mark article)
4638                       (message "Message marked for downloading"))
4639                   (gnus-summary-mark-article article gnus-canceled-mark)
4640                   (unless (memq article gnus-newsgroup-sparse)
4641                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4642           (if (or (eq result 'pseudo)
4643                   (eq result 'nneething))
4644               (progn
4645                 (with-current-buffer summary-buffer
4646                   (push article gnus-newsgroup-history)
4647                   (setq gnus-last-article gnus-current-article
4648                         gnus-current-article 0
4649                         gnus-current-headers nil
4650                         gnus-article-current nil)
4651                   (if (eq result 'nneething)
4652                       (gnus-configure-windows 'summary)
4653                     (gnus-configure-windows 'article))
4654                   (gnus-set-global-variables))
4655                 (let ((gnus-article-mime-handle-alist-1
4656                        gnus-article-mime-handle-alist))
4657                   (gnus-set-mode-line 'article)))
4658             ;; The result from the `request' was an actual article -
4659             ;; or at least some text that is now displayed in the
4660             ;; article buffer.
4661             (when (and (numberp article)
4662                        (not (eq article gnus-current-article)))
4663               ;; Seems like a new article has been selected.
4664               ;; `gnus-current-article' must be an article number.
4665               (with-current-buffer summary-buffer
4666                 (push article gnus-newsgroup-history)
4667                 (setq gnus-last-article gnus-current-article
4668                       gnus-current-article article
4669                       gnus-current-headers
4670                       (gnus-summary-article-header gnus-current-article)
4671                       gnus-article-current
4672                       (cons gnus-newsgroup-name gnus-current-article))
4673                 (unless (vectorp gnus-current-headers)
4674                   (setq gnus-current-headers nil))
4675                 (gnus-summary-goto-subject gnus-current-article)
4676                 (when (gnus-summary-show-thread)
4677                   ;; If the summary buffer really was folded, the
4678                   ;; previous goto may not actually have gone to
4679                   ;; the right article, but the thread root instead.
4680                   ;; So we go again.
4681                   (gnus-summary-goto-subject gnus-current-article))
4682                 (gnus-run-hooks 'gnus-mark-article-hook)
4683                 (gnus-set-mode-line 'summary)
4684                 (when (gnus-visual-p 'article-highlight 'highlight)
4685                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4686                 ;; Set the global newsgroup variables here.
4687                 (gnus-set-global-variables)
4688                 (setq gnus-have-all-headers
4689                       (or all-headers gnus-show-all-headers))))
4690             (save-excursion
4691               (gnus-configure-windows 'article))
4692             (when (or (numberp article)
4693                       (stringp article))
4694               (gnus-article-prepare-display)
4695               ;; Do page break.
4696               (goto-char (point-min))
4697               (when gnus-break-pages
4698                 (gnus-narrow-to-page)))
4699             (let ((gnus-article-mime-handle-alist-1
4700                    gnus-article-mime-handle-alist))
4701               (gnus-set-mode-line 'article))
4702             (article-goto-body)
4703             (unless (bobp)
4704               (forward-line -1))
4705             (set-window-point (get-buffer-window (current-buffer)) (point))
4706             (gnus-configure-windows 'article)
4707             (gnus-run-hooks 'gnus-article-prepare-hook)
4708             t))))))
4709
4710 ;;;###autoload
4711 (defun gnus-article-prepare-display ()
4712   "Make the current buffer look like a nice article."
4713   ;; Hooks for getting information from the article.
4714   ;; This hook must be called before being narrowed.
4715   (let ((gnus-article-buffer (current-buffer))
4716         buffer-read-only
4717         (inhibit-read-only t))
4718     (unless (eq major-mode 'gnus-article-mode)
4719       (gnus-article-mode))
4720     (setq buffer-read-only nil
4721           gnus-article-wash-types nil
4722           gnus-article-image-alist nil)
4723     (gnus-run-hooks 'gnus-tmp-internal-hook)
4724     (when gnus-display-mime-function
4725       (funcall gnus-display-mime-function))))
4726
4727 ;;;
4728 ;;; Gnus Sticky Article Mode
4729 ;;;
4730
4731 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4732   "Mode for sticky articles."
4733   ;; Release bindings that won't work.
4734   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4735                              gnus-sticky-article-mode-map)
4736   (substitute-key-definition 'gnus-article-refer-article 'undefined
4737                              gnus-sticky-article-mode-map)
4738   (dolist (k '("e" "h" "s" "F" "R"))
4739     (define-key gnus-sticky-article-mode-map k nil))
4740   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4741   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4742   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4743   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4744
4745 (defun gnus-sticky-article (arg)
4746   "Make the current article sticky.
4747 If a prefix ARG is given, ask for a name for this sticky article buffer."
4748   (interactive "P")
4749   (gnus-summary-show-thread)
4750   (gnus-summary-select-article nil nil 'pseudo)
4751   (let (new-art-buf-name)
4752     (gnus-eval-in-buffer-window gnus-article-buffer
4753       (setq new-art-buf-name
4754             (concat
4755              "*Sticky Article: "
4756              (if arg
4757                  (read-from-minibuffer "Sticky article buffer name: ")
4758                (gnus-with-article-headers
4759                  (gnus-article-goto-header "subject")
4760                  (setq new-art-buf-name
4761                        (buffer-substring-no-properties
4762                         (line-beginning-position) (line-end-position)))
4763                  (goto-char (point-min))
4764                  (gnus-article-goto-header "from")
4765                  (setq new-art-buf-name
4766                        (concat
4767                         new-art-buf-name ", "
4768                         (buffer-substring-no-properties
4769                          (line-beginning-position) (line-end-position))))
4770                  (goto-char (point-min))
4771                  (gnus-article-goto-header "date")
4772                  (setq new-art-buf-name
4773                        (concat
4774                         new-art-buf-name ", "
4775                         (buffer-substring-no-properties
4776                          (line-beginning-position) (line-end-position))))))
4777              "*"))
4778       (if (and (gnus-buffer-live-p new-art-buf-name)
4779                (with-current-buffer new-art-buf-name
4780                  (eq major-mode 'gnus-sticky-article-mode)))
4781           (switch-to-buffer new-art-buf-name)
4782         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4783       (gnus-sticky-article-mode))
4784     (setq gnus-article-buffer new-art-buf-name))
4785   (gnus-summary-recenter)
4786   (gnus-summary-position-point))
4787
4788 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4789   "Kill the given sticky article BUFFER.
4790 If none is given, assume the current buffer and kill it if it has
4791 `gnus-sticky-article-mode'."
4792   (interactive)
4793   (unless buffer
4794     (setq buffer (current-buffer)))
4795   (with-current-buffer buffer
4796     (when (eq major-mode 'gnus-sticky-article-mode)
4797       (gnus-kill-buffer buffer))))
4798
4799 (defun gnus-kill-sticky-article-buffers (arg)
4800   "Kill all sticky article buffers.
4801 If a prefix ARG is given, ask for confirmation."
4802   (interactive "P")
4803   (dolist (buf (gnus-buffers))
4804     (with-current-buffer buf
4805       (when (eq major-mode 'gnus-sticky-article-mode)
4806        (if (not arg)
4807            (gnus-kill-buffer buf)
4808          (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4809            (gnus-kill-buffer buf)))))))
4810
4811 ;;;
4812 ;;; Gnus MIME viewing functions
4813 ;;;
4814
4815 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4816   "Format of the MIME buttons.
4817
4818 Valid specifiers include:
4819 %t  The MIME type
4820 %T  MIME type, along with additional info
4821 %n  The `name' parameter
4822 %d  The description, if any
4823 %l  The length of the encoded part
4824 %p  The part identifier number
4825 %e  Dots if the part isn't displayed
4826
4827 General format specifiers can also be used.  See Info node
4828 `(gnus)Formatting Variables'.")
4829
4830 (defvar gnus-mime-button-line-format-alist
4831   '((?t gnus-tmp-type ?s)
4832     (?T gnus-tmp-type-long ?s)
4833     (?n gnus-tmp-name ?s)
4834     (?d gnus-tmp-description ?s)
4835     (?p gnus-tmp-id ?s)
4836     (?l gnus-tmp-length ?d)
4837     (?e gnus-tmp-dots ?s)))
4838
4839 (defvar gnus-mime-button-commands
4840   '((gnus-article-press-button "\r" "Toggle Display")
4841     (gnus-mime-view-part "v" "View Interactively...")
4842     (gnus-mime-view-part-as-type "t" "View As Type...")
4843     (gnus-mime-view-part-as-charset "C" "View As charset...")
4844     (gnus-mime-save-part "o" "Save...")
4845     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4846     (gnus-mime-replace-part "r" "Replace part")
4847     (gnus-mime-delete-part "d" "Delete part")
4848     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4849     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4850     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4851     (gnus-mime-view-part-externally "e" "View Externally")
4852     (gnus-mime-print-part "p" "Print")
4853     (gnus-mime-pipe-part "|" "Pipe To Command...")
4854     (gnus-mime-action-on-part "." "Take action on the part...")))
4855
4856 (defun gnus-article-mime-part-status ()
4857   (if gnus-article-mime-handle-alist-1
4858       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4859           " (1 part)"
4860         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4861     ""))
4862
4863 (defvar gnus-mime-button-map
4864   (let ((map (make-sparse-keymap)))
4865     (define-key map gnus-mouse-2 'gnus-article-push-button)
4866     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4867     (dolist (c gnus-mime-button-commands)
4868       (define-key map (cadr c) (car c)))
4869     map))
4870
4871 (easy-menu-define
4872   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4873   `("MIME Part"
4874     ,@(mapcar (lambda (c)
4875                 (vector (caddr c) (car c) :active t))
4876               gnus-mime-button-commands)))
4877
4878 (defvar gnus-url-button-commands
4879   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4880
4881 (defvar gnus-url-button-map
4882   (let ((map (make-sparse-keymap)))
4883     (dolist (c gnus-url-button-commands)
4884       (define-key map (cadr c) (car c)))
4885     map))
4886
4887 (easy-menu-define
4888   gnus-url-button-menu gnus-url-button-map "URL button menu."
4889   `("Url Button"
4890     ,@(mapcar (lambda (c)
4891                 (vector (caddr c) (car c) :active t))
4892               gnus-url-button-commands)))
4893
4894 (defmacro gnus-bind-safe-url-regexp (&rest body)
4895   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4896   `(let ((mm-w3m-safe-url-regexp
4897           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4898                                 (gnus-buffer-live-p
4899                                  gnus-article-current-summary))
4900                            (with-current-buffer gnus-article-current-summary
4901                              gnus-newsgroup-name)
4902                          gnus-newsgroup-name)))
4903             (if (cond ((not group)
4904                        ;; Maybe we're in a mml-preview buffer
4905                        ;; and no group is selected.
4906                        t)
4907                       ((stringp gnus-safe-html-newsgroups)
4908                        (string-match gnus-safe-html-newsgroups group))
4909                       ((consp gnus-safe-html-newsgroups)
4910                        (member group gnus-safe-html-newsgroups)))
4911                 nil
4912               mm-w3m-safe-url-regexp))))
4913      ,@body))
4914
4915 (defun gnus-mime-button-menu (event prefix)
4916  "Construct a context-sensitive menu of MIME commands."
4917  (interactive "e\nP")
4918  (save-window-excursion
4919    (let ((pos (event-start event)))
4920      (select-window (posn-window pos))
4921      (goto-char (posn-point pos))
4922      (gnus-article-check-buffer)
4923      (popup-menu gnus-mime-button-menu nil prefix))))
4924
4925 (defun gnus-mime-view-all-parts (&optional handles)
4926   "View all the MIME parts."
4927   (interactive)
4928   (with-current-buffer gnus-article-buffer
4929     (let ((handles (or handles gnus-article-mime-handles))
4930           (mail-parse-charset gnus-newsgroup-charset)
4931           (mail-parse-ignored-charsets
4932            (with-current-buffer gnus-summary-buffer
4933              gnus-newsgroup-ignored-charsets)))
4934       (when handles
4935         (mm-remove-parts handles)
4936         (goto-char (point-min))
4937         (or (search-forward "\n\n") (goto-char (point-max)))
4938         (let ((inhibit-read-only t))
4939           (delete-region (point) (point-max))
4940           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4941
4942 (defun gnus-article-jump-to-part (n)
4943   "Jump to MIME part N."
4944   (interactive "P")
4945   (let ((parts (with-current-buffer gnus-article-buffer
4946                  (length gnus-article-mime-handle-alist))))
4947     (when (zerop parts)
4948       (error "No such part"))
4949     (pop-to-buffer gnus-article-buffer)
4950     (or n
4951         (setq n (if (= parts 1)
4952                     1
4953                   (read-number (format "Jump to part (1..%s): " parts)))))
4954     (unless (and (integerp n) (<= n parts) (>= n 1))
4955       (setq n
4956             (progn
4957               (gnus-message 7 "Invalid part `%s', using %s instead."
4958                             n parts)
4959               parts)))
4960     (gnus-message 9 "Jumping to part %s." n)
4961     (cond ((>= gnus-auto-select-part 1)
4962            (while (and (<= n parts)
4963                        (not (gnus-article-goto-part n)))
4964              (setq n (1+ n))))
4965           ((< gnus-auto-select-part 0)
4966            (while (and (>= n 1)
4967                        (not (gnus-article-goto-part n)))
4968              (setq n (1- n))))
4969           (t
4970            (gnus-article-goto-part n)))))
4971
4972 (defvar gnus-mime-buttonized-part-id nil
4973   "ID of a mime part that should be buttonized.
4974 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4975
4976 (eval-when-compile
4977   (defsubst gnus-article-edit-part (handles &optional current-id)
4978     "Edit an article in order to delete a mime part.
4979 This function is exclusively used by `gnus-mime-save-part-and-strip'
4980 and `gnus-mime-delete-part', and not provided at run-time normally."
4981     (gnus-article-edit-article
4982      `(lambda ()
4983         (buffer-disable-undo)
4984         (erase-buffer)
4985         (let ((mail-parse-charset (or gnus-article-charset
4986                                       ',gnus-newsgroup-charset))
4987               (mail-parse-ignored-charsets
4988                (or gnus-article-ignored-charsets
4989                    ',gnus-newsgroup-ignored-charsets))
4990               (mbl mml-buffer-list))
4991           (setq mml-buffer-list nil)
4992           (insert-buffer-substring gnus-original-article-buffer)
4993           (mime-to-mml ',handles)
4994           (setq gnus-article-mime-handles nil)
4995           (let ((mbl1 mml-buffer-list))
4996             (setq mml-buffer-list mbl)
4997             (set (make-local-variable 'mml-buffer-list) mbl1))
4998           (gnus-make-local-hook 'kill-buffer-hook)
4999           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
5000      `(lambda (no-highlight)
5001         (let ((mail-parse-charset (or gnus-article-charset
5002                                       ',gnus-newsgroup-charset))
5003               (message-options message-options)
5004               (message-options-set-recipient)
5005               (mail-parse-ignored-charsets
5006                (or gnus-article-ignored-charsets
5007                    ',gnus-newsgroup-ignored-charsets)))
5008           (mml-to-mime)
5009           (mml-destroy-buffers)
5010           (remove-hook 'kill-buffer-hook
5011                        'mml-destroy-buffers t)
5012           (kill-local-variable 'mml-buffer-list))
5013         (gnus-summary-edit-article-done
5014          ,(or (mail-header-references gnus-current-headers) "")
5015          ,(gnus-group-read-only-p)
5016          ,gnus-summary-buffer no-highlight))
5017      t)
5018     ;; Force buttonizing this part.
5019     (let ((gnus-mime-buttonized-part-id current-id))
5020       (gnus-article-edit-done))
5021     (gnus-configure-windows 'article)
5022     (when (and current-id (integerp gnus-auto-select-part))
5023       (gnus-article-jump-to-part
5024        (min (max (+ current-id gnus-auto-select-part) 1)
5025             (with-current-buffer gnus-article-buffer
5026               (length gnus-article-mime-handle-alist)))))))
5027
5028 (defun gnus-mime-replace-part (file)
5029   "Replace MIME part under point with an external body."
5030   ;; Useful if file has already been saved to disk
5031   (interactive
5032    (list
5033     (read-file-name "Replace MIME part with file: "
5034                     (or mm-default-directory default-directory)
5035                     nil nil)))
5036   (gnus-mime-save-part-and-strip file))
5037
5038 (defun gnus-mime-save-part-and-strip (&optional file)
5039   "Save the MIME part under point then replace it with an external body.
5040 If FILE is given, use it for the external part."
5041   (interactive)
5042   (gnus-article-check-buffer)
5043   (when (gnus-group-read-only-p)
5044     (error "The current group does not support deleting of parts"))
5045   (when (mm-complicated-handles gnus-article-mime-handles)
5046     (error "\
5047 The current article has a complicated MIME structure, giving up..."))
5048   (let* ((data (get-text-property (point) 'gnus-data))
5049          (id (get-text-property (point) 'gnus-part))
5050          param
5051          (handles gnus-article-mime-handles))
5052     (unless file
5053       (setq file
5054             (and data (mm-save-part data "Delete MIME part and save to: "))))
5055     (when file
5056       (with-current-buffer (mm-handle-buffer data)
5057         (erase-buffer)
5058         (insert "Content-Type: " (mm-handle-media-type data))
5059         (mml-insert-parameter-string (cdr (mm-handle-type data))
5060                                      '(charset))
5061         ;; Add a filename for the sake of saving the part again.
5062         (mml-insert-parameter
5063          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5064         (insert "\n")
5065         (insert "Content-ID: " (message-make-message-id) "\n")
5066         (insert "Content-Transfer-Encoding: binary\n")
5067         (insert "\n"))
5068       (setcdr data
5069               (cdr (mm-make-handle nil
5070                                    `("message/external-body"
5071                                      (access-type . "LOCAL-FILE")
5072                                      (name . ,file)))))
5073       ;; (set-buffer gnus-summary-buffer)
5074       (gnus-article-edit-part handles id))))
5075
5076 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5077 ;; parts...>') but with stripping would be nice.
5078
5079 (defun gnus-mime-delete-part ()
5080   "Delete the MIME part under point.
5081 Replace it with some information about the removed part."
5082   (interactive)
5083   (gnus-article-check-buffer)
5084   (when (gnus-group-read-only-p)
5085     (error "The current group does not support deleting of parts"))
5086   (when (mm-complicated-handles gnus-article-mime-handles)
5087     (error "\
5088 The current article has a complicated MIME structure, giving up..."))
5089   (when (or gnus-expert-user
5090             (gnus-yes-or-no-p "\
5091 Deleting parts may malfunction or destroy the article; continue? "))
5092     (let* ((data (get-text-property (point) 'gnus-data))
5093            (id (get-text-property (point) 'gnus-part))
5094            (handles gnus-article-mime-handles)
5095            (description
5096             (let ((desc (mm-handle-description data)))
5097               (when desc
5098                 (mail-decode-encoded-word-string desc))))
5099            (filename (or (mm-handle-filename data) "(none)"))
5100            (type (mm-handle-media-type data)))
5101       (unless data
5102         (error "No MIME part under point"))
5103       (with-current-buffer (mm-handle-buffer data)
5104         (let ((bsize (buffer-size)))
5105           (erase-buffer)
5106           (insert
5107            (concat
5108             ",----\n"
5109             "| The following attachment has been deleted:\n"
5110             "|\n"
5111             "| Type:           " type "\n"
5112             "| Filename:       " filename "\n"
5113             "| Size (encoded): " (format "%s byte%s\n"
5114                                          bsize (if (= bsize 1)
5115                                                    ""
5116                                                  "s"))
5117             (when description
5118               (concat    "| Description:    " description "\n"))
5119             "`----\n"))
5120           (setcdr data
5121                   (cdr (mm-make-handle
5122                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5123                         (list "attachment")
5124                         (format "Deleted attachment (%s bytes)" bsize))))))
5125       ;; (set-buffer gnus-summary-buffer)
5126       (gnus-article-edit-part handles id))))
5127
5128 (defun gnus-mime-save-part ()
5129   "Save the MIME part under point."
5130   (interactive)
5131   (gnus-article-check-buffer)
5132   (let ((data (get-text-property (point) 'gnus-data)))
5133     (when data
5134       (mm-save-part data))))
5135
5136 (defun gnus-mime-pipe-part (&optional cmd)
5137   "Pipe the MIME part under point to a process.
5138 Use CMD as the process."
5139   (interactive)
5140   (gnus-article-check-buffer)
5141   (let ((data (get-text-property (point) 'gnus-data)))
5142     (when data
5143       (mm-pipe-part data cmd))))
5144
5145 (defun gnus-mime-view-part ()
5146   "Interactively choose a viewing method for the MIME part under point."
5147   (interactive)
5148   (gnus-article-check-buffer)
5149   (let ((data (get-text-property (point) 'gnus-data)))
5150     (when data
5151       (setq gnus-article-mime-handles
5152             (mm-merge-handles
5153              gnus-article-mime-handles (setq data (copy-sequence data))))
5154       (mm-interactively-view-part data))))
5155
5156 (defun gnus-mime-view-part-as-type-internal ()
5157   (gnus-article-check-buffer)
5158   (let* ((handle (get-text-property (point) 'gnus-data))
5159          (name (or
5160                 ;; Content-Type: foo/bar; name=...
5161                 (mail-content-type-get (mm-handle-type handle) 'name)
5162                 ;; Content-Disposition: attachment; filename=...
5163                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5164          (def-type (and name (mm-default-file-encoding name))))
5165     (or (and def-type (cons def-type 0))
5166         (and handle
5167              (equal (mm-handle-media-supertype handle) "text")
5168              '("text/plain" . 0))
5169         '("application/octet-stream" . 0))))
5170
5171 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5172   "Choose a MIME media type, and view the part as such.
5173 If non-nil, PRED is a predicate to use during completion to limit the
5174 available media-types."
5175   (interactive)
5176   (unless mime-type
5177     (setq mime-type
5178           (let ((default (gnus-mime-view-part-as-type-internal)))
5179             (gnus-completing-read
5180              "View as MIME type"
5181              (if pred
5182                  (gnus-remove-if-not pred (mailcap-mime-types))
5183                (mailcap-mime-types))
5184              nil nil nil
5185              (car default)))))
5186   (gnus-article-check-buffer)
5187   (let ((handle (get-text-property (point) 'gnus-data)))
5188     (when handle
5189       (when (equal (mm-handle-media-type handle) "message/external-body")
5190         (unless (mm-handle-cache handle)
5191           (mm-extern-cache-contents handle))
5192         (setq handle (mm-handle-cache handle)))
5193       (setq handle
5194             (mm-make-handle (mm-handle-buffer handle)
5195                             (cons mime-type (cdr (mm-handle-type handle)))
5196                             (mm-handle-encoding handle)
5197                             (mm-handle-undisplayer handle)
5198                             (mm-handle-disposition handle)
5199                             (mm-handle-description handle)
5200                             nil
5201                             (mm-handle-id handle)))
5202       (setq gnus-article-mime-handles
5203             (mm-merge-handles gnus-article-mime-handles handle))
5204       (when (mm-handle-displayed-p handle)
5205         (mm-remove-part handle))
5206       (gnus-mm-display-part handle))))
5207
5208 (defun gnus-mime-copy-part (&optional handle arg)
5209   "Put the MIME part under point into a new buffer.
5210 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5211 are decompressed."
5212   (interactive (list nil current-prefix-arg))
5213   (gnus-article-check-buffer)
5214   (unless handle
5215     (setq handle (get-text-property (point) 'gnus-data)))
5216   (when handle
5217     (let ((filename (mm-handle-filename handle))
5218           contents dont-decode charset coding-system)
5219       (mm-with-unibyte-buffer
5220         (mm-insert-part handle)
5221         (setq contents (or (condition-case nil
5222                                (mm-decompress-buffer filename nil 'sig)
5223                              (error
5224                               (setq dont-decode t)
5225                               nil))
5226                            (buffer-string))))
5227       (setq filename (cond (filename (file-name-nondirectory filename))
5228                            (dont-decode "*raw data*")
5229                            (t "*decoded*")))
5230       (cond
5231        (dont-decode)
5232        ((not arg)
5233         (unless (setq charset (mail-content-type-get
5234                                (mm-handle-type handle) 'charset))
5235           (unless (setq coding-system (mm-with-unibyte-buffer
5236                                         (insert contents)
5237                                         (mm-find-buffer-file-coding-system)))
5238             (setq charset gnus-newsgroup-charset))))
5239        ((numberp arg)
5240         (setq charset (or (cdr (assq arg
5241                                      gnus-summary-show-article-charset-alist))
5242                           (mm-read-coding-system "Charset: ")))))
5243       (switch-to-buffer (generate-new-buffer filename))
5244       (if (or coding-system
5245               (and charset
5246                    (setq coding-system (mm-charset-to-coding-system charset))
5247                    (not (eq coding-system 'ascii))))
5248           (progn
5249             (mm-enable-multibyte)
5250             (insert (mm-decode-coding-string contents coding-system))
5251             (setq buffer-file-coding-system
5252                   (if (boundp 'last-coding-system-used)
5253                       (symbol-value 'last-coding-system-used)
5254                     coding-system)))
5255         (mm-disable-multibyte)
5256         (insert contents)
5257         (setq buffer-file-coding-system mm-binary-coding-system))
5258       ;; We do it this way to make `normal-mode' set the appropriate mode.
5259       (unwind-protect
5260           (progn
5261             (setq buffer-file-name (expand-file-name filename))
5262             (normal-mode))
5263         (setq buffer-file-name nil))
5264       (goto-char (point-min)))))
5265
5266 (defun gnus-mime-print-part (&optional handle filename)
5267   "Print the MIME part under point."
5268   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5269   (gnus-article-check-buffer)
5270   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5271          (contents (and handle (mm-get-part handle)))
5272          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5273          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5274     (when contents
5275         (if printer
5276             (unwind-protect
5277                 (progn
5278                   (mm-save-part-to-file handle file)
5279                   (call-process shell-file-name nil
5280                                 (generate-new-buffer " *mm*")
5281                                 nil
5282                                 shell-command-switch
5283                                 (mm-mailcap-command
5284                                  printer file (mm-handle-type handle))))
5285               (delete-file file))
5286           (with-temp-buffer
5287             (insert contents)
5288             (gnus-print-buffer))
5289           (ps-despool filename)))))
5290
5291 (defun gnus-mime-inline-part (&optional handle arg)
5292   "Insert the MIME part under point into the current buffer.
5293 Compressed files like .gz and .bz2 are decompressed."
5294   (interactive (list nil current-prefix-arg))
5295   (gnus-article-check-buffer)
5296   (unless handle
5297     (setq handle (get-text-property (point) 'gnus-data)))
5298   (when handle
5299     (let ((b (point))
5300           (inhibit-read-only t)
5301           contents charset coding-system)
5302       (if (and (not arg) (mm-handle-undisplayer handle))
5303           (mm-remove-part handle)
5304         (mm-with-unibyte-buffer
5305           (mm-insert-part handle)
5306           (setq contents
5307                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5308                     (buffer-string))))
5309         (cond
5310          ((not arg)
5311           (unless (setq charset (mail-content-type-get
5312                                  (mm-handle-type handle) 'charset))
5313             (unless (setq coding-system
5314                           (mm-with-unibyte-buffer
5315                             (insert contents)
5316                             (mm-find-buffer-file-coding-system)))
5317               (setq charset gnus-newsgroup-charset))))
5318          ((numberp arg)
5319           (if (mm-handle-undisplayer handle)
5320               (mm-remove-part handle))
5321           (setq charset
5322                 (or (cdr (assq arg
5323                                gnus-summary-show-article-charset-alist))
5324                     (mm-read-coding-system "Charset: "))))
5325          ((mm-handle-undisplayer handle)
5326           (mm-remove-part handle)))
5327         (forward-line 2)
5328         (mm-display-inline handle)
5329         (goto-char b)))))
5330
5331 (defun gnus-mime-set-charset-parameters (handle charset)
5332   "Set CHARSET to parameters in HANDLE.
5333 CHARSET may either be a string or a symbol."
5334   (unless (stringp charset)
5335     (setq charset (symbol-name charset)))
5336   (if (stringp (car handle))
5337       (dolist (h (cdr handle))
5338         (gnus-mime-set-charset-parameters h charset))
5339     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5340                                             "message/external-body")
5341                                      (progn
5342                                        (unless (mm-handle-cache handle)
5343                                          (mm-extern-cache-contents handle))
5344                                        (mm-handle-cache handle))
5345                                    handle)))
5346            (param (assq 'charset (cdr type))))
5347       (if param
5348           (setcdr param charset)
5349         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5350
5351 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5352   "Insert the MIME part under point into the current buffer using the
5353 specified charset."
5354   (interactive (list nil current-prefix-arg))
5355   (gnus-article-check-buffer)
5356   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5357         (fun (get-text-property (point) 'gnus-callback))
5358         (gnus-newsgroup-ignored-charsets 'gnus-all)
5359         charset form preferred parts)
5360     (when handle
5361       (when (prog1
5362                 (and fun
5363                      (setq charset
5364                            (or (cdr (assq
5365                                      arg
5366                                      gnus-summary-show-article-charset-alist))
5367                                (mm-read-coding-system "Charset: "))))
5368               (if (mm-handle-undisplayer handle)
5369                   (mm-remove-part handle)))
5370         (gnus-mime-set-charset-parameters handle charset)
5371         (when (and (consp (setq form (cdr-safe fun)))
5372                    (setq form (ignore-errors
5373                                 (assq 'gnus-mime-display-alternative form)))
5374                    (setq preferred (caddr form))
5375                    (progn
5376                      (when (eq (car preferred) 'quote)
5377                        (setq preferred (cadr preferred)))
5378                      (not (equal preferred
5379                                  (get-text-property (point) 'gnus-data))))
5380                    (setq parts (get-text-property (point) 'gnus-part))
5381                    (setq parts (cdr (assq parts
5382                                           gnus-article-mime-handle-alist)))
5383                    (equal (mm-handle-media-type parts) "multipart/alternative")
5384                    (setq parts (reverse (cdr parts))))
5385           (setcar (cddr form)
5386                   (list 'quote (or (cadr (member preferred parts))
5387                                    (car parts)))))
5388         (funcall fun handle)))))
5389
5390 (defun gnus-mime-view-part-externally (&optional handle)
5391   "View the MIME part under point with an external viewer."
5392   (interactive)
5393   (gnus-article-check-buffer)
5394   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5395          (mm-user-display-methods nil)
5396          (mm-inlined-types nil)
5397          (mail-parse-charset gnus-newsgroup-charset)
5398          (mail-parse-ignored-charsets
5399           (with-current-buffer gnus-summary-buffer
5400             gnus-newsgroup-ignored-charsets))
5401          (type (mm-handle-media-type handle))
5402          (method (mailcap-mime-info type))
5403          (mm-enable-external t))
5404     (if (not (stringp method))
5405         (gnus-mime-view-part-as-type
5406          nil (lambda (type) (stringp (mailcap-mime-info type))))
5407       (when handle
5408         (mm-display-part handle nil t)))))
5409
5410 (defun gnus-mime-view-part-internally (&optional handle)
5411   "View the MIME part under point with an internal viewer.
5412 If no internal viewer is available, use an external viewer."
5413   (interactive)
5414   (gnus-article-check-buffer)
5415   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5416          (mm-inlined-types '(".*"))
5417          (mm-inline-large-images t)
5418          (mail-parse-charset gnus-newsgroup-charset)
5419          (mail-parse-ignored-charsets
5420           (with-current-buffer gnus-summary-buffer
5421             gnus-newsgroup-ignored-charsets))
5422          (inhibit-read-only t))
5423     (if (not (mm-inlinable-p handle))
5424         (gnus-mime-view-part-as-type
5425          nil (lambda (type) (mm-inlinable-p handle type)))
5426       (when handle
5427         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5428
5429 (defun gnus-mime-action-on-part (&optional action)
5430   "Do something with the MIME attachment at \(point\)."
5431   (interactive
5432    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5433   (gnus-article-check-buffer)
5434   (let ((action-pair (assoc action gnus-mime-action-alist)))
5435     (if action-pair
5436         (funcall (cdr action-pair)))))
5437
5438 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5439   "Call FUNCTION on MIME part N.
5440 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5441 If INTERACTIVE, call FUNCTION interactively."
5442   (let (window frame)
5443     ;; Check whether the article is displayed.
5444     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5445                  (setq window (get-buffer-window gnus-article-buffer t))
5446                  (frame-visible-p (setq frame (window-frame window))))
5447       (error "No article is displayed"))
5448     (with-current-buffer gnus-article-buffer
5449       ;; Check whether the article displays the right contents.
5450       (unless (with-current-buffer gnus-summary-buffer
5451                 (eq gnus-current-article (gnus-summary-article-number)))
5452         (error "You should select the right article first"))
5453       (if n
5454           (setq n (prefix-numeric-value n))
5455         (let ((pt (point)))
5456           (setq n (or (get-text-property pt 'gnus-part)
5457                       (and (not (bobp))
5458                            (get-text-property (1- pt) 'gnus-part))
5459                       (get-text-property (prog2
5460                                              (forward-line 1)
5461                                              (point)
5462                                            (goto-char pt))
5463                                          'gnus-part)
5464                       (get-text-property
5465                        (or (and (setq pt (previous-single-property-change
5466                                           pt 'gnus-part))
5467                                 (1- pt))
5468                            (next-single-property-change (point) 'gnus-part)
5469                            (point))
5470                        'gnus-part)
5471                       1))))
5472       ;; Check whether the specified part exists.
5473       (when (> n (length gnus-article-mime-handle-alist))
5474         (error "No such part")))
5475     (unless
5476         (progn
5477           ;; To select the window is needed so that the cursor
5478           ;; might be visible on the MIME button.
5479           (select-window (prog1
5480                              window
5481                            (setq window (selected-window))
5482                            ;; Article may be displayed in the other frame.
5483                            (gnus-select-frame-set-input-focus
5484                             (prog1
5485                                 frame
5486                               (setq frame (selected-frame))))))
5487           (when (gnus-article-goto-part n)
5488             ;; We point the cursor and the arrow at the MIME button
5489             ;; when the `function' prompt the user for something.
5490             (unless (and (pos-visible-in-window-p)
5491                          (> (count-lines (point) (window-end))
5492                             (/ (1- (window-height)) 3)))
5493               (recenter (/ (1- (window-height)) 3)))
5494             (let ((cursor-in-non-selected-windows t)
5495                   (overlay-arrow-string "=>")
5496                   (overlay-arrow-position (point-marker)))
5497               (unwind-protect
5498                   (cond
5499                    ((and no-handle interactive)
5500                     (call-interactively function))
5501                    (no-handle
5502                     (funcall function))
5503                    (interactive
5504                     (call-interactively
5505                      function (get-text-property (point) 'gnus-data)))
5506                    (t
5507                     (funcall function
5508                              (get-text-property (point) 'gnus-data))))
5509                 (set-marker overlay-arrow-position nil)
5510                 (unless gnus-auto-select-part
5511                   (gnus-select-frame-set-input-focus frame)
5512                   (select-window window))))
5513             t))
5514       (if gnus-inhibit-mime-unbuttonizing
5515           ;; This is the default though the program shouldn't reach here.
5516           (error "No such part")
5517         ;; The part which doesn't have the MIME button is selected.
5518         ;; So, we display all the buttons and redo it.
5519         (let ((gnus-inhibit-mime-unbuttonizing t))
5520           (gnus-summary-show-article)
5521           (gnus-article-part-wrapper n function no-handle))))))
5522
5523 (defun gnus-article-pipe-part (n)
5524   "Pipe MIME part N, which is the numerical prefix."
5525   (interactive "P")
5526   (gnus-article-part-wrapper n 'mm-pipe-part))
5527
5528 (defun gnus-article-save-part (n)
5529   "Save MIME part N, which is the numerical prefix."
5530   (interactive "P")
5531   (gnus-article-part-wrapper n 'mm-save-part))
5532
5533 (defun gnus-article-interactively-view-part (n)
5534   "View MIME part N interactively, which is the numerical prefix."
5535   (interactive "P")
5536   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5537
5538 (defun gnus-article-copy-part (n)
5539   "Copy MIME part N, which is the numerical prefix."
5540   (interactive "P")
5541   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5542
5543 (defun gnus-article-view-part-as-charset (n)
5544   "View MIME part N using a specified charset.
5545 N is the numerical prefix."
5546   (interactive "P")
5547   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5548
5549 (defun gnus-article-view-part-externally (n)
5550   "View MIME part N externally, which is the numerical prefix."
5551   (interactive "P")
5552   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5553
5554 (defun gnus-article-inline-part (n)
5555   "Inline MIME part N, which is the numerical prefix."
5556   (interactive "P")
5557   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5558
5559 (defun gnus-article-save-part-and-strip (n)
5560   "Save MIME part N and replace it with an external body.
5561 N is the numerical prefix."
5562   (interactive "P")
5563   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5564
5565 (defun gnus-article-replace-part (n)
5566   "Replace MIME part N with an external body.
5567 N is the numerical prefix."
5568   (interactive "P")
5569   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5570
5571 (defun gnus-article-delete-part (n)
5572   "Delete MIME part N and add some information about the removed part.
5573 N is the numerical prefix."
5574   (interactive "P")
5575   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5576
5577 (defun gnus-article-view-part-as-type (n)
5578   "Choose a MIME media type, and view part N as such.
5579 N is the numerical prefix."
5580   (interactive "P")
5581   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5582
5583 (defun gnus-article-mime-match-handle-first (condition)
5584   (if condition
5585       (let (n)
5586         (dolist (ihandle gnus-article-mime-handle-alist)
5587           (if (and (cond
5588                     ((functionp condition)
5589                      (funcall condition (cdr ihandle)))
5590                     ((eq condition 'undisplayed)
5591                      (not (or (mm-handle-undisplayer (cdr ihandle))
5592                               (equal (mm-handle-media-type (cdr ihandle))
5593                                      "multipart/alternative"))))
5594                     ((eq condition 'undisplayed-alternative)
5595                      (not (mm-handle-undisplayer (cdr ihandle))))
5596                     (t t))
5597                    (gnus-article-goto-part (car ihandle))
5598                    (or (not n) (< (car ihandle) n)))
5599               (setq n (car ihandle))))
5600         (or n 1))
5601     1))
5602
5603 (defun gnus-article-view-part (&optional n)
5604   "View MIME part N, which is the numerical prefix.
5605 If the part is already shown, hide the part.  If N is nil, view
5606 all parts."
5607   (interactive "P")
5608   (with-current-buffer gnus-article-buffer
5609     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5610                              gnus-article-mime-match-handle-function)))
5611     (when (> n (length gnus-article-mime-handle-alist))
5612       (error "No such part"))
5613     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5614       (when (gnus-article-goto-part n)
5615         (if (equal (car handle) "multipart/alternative")
5616             (progn
5617               (beginning-of-line) ;; Make it toggle subparts
5618               (gnus-article-press-button))
5619           (when (eq (gnus-mm-display-part handle) 'internal)
5620             (gnus-set-window-start)))))))
5621
5622 (defsubst gnus-article-mime-total-parts ()
5623   (if (bufferp (car gnus-article-mime-handles))
5624       1 ;; single part
5625     (1- (length gnus-article-mime-handles))))
5626
5627 (defun gnus-mm-display-part (handle)
5628   "Display HANDLE and fix MIME button."
5629   (let ((id (get-text-property (point) 'gnus-part))
5630         (point (point))
5631         (inhibit-read-only t))
5632     (forward-line 1)
5633     (prog1
5634         (let ((window (selected-window))
5635               (mail-parse-charset gnus-newsgroup-charset)
5636               (mail-parse-ignored-charsets
5637                (if (gnus-buffer-live-p gnus-summary-buffer)
5638                    (with-current-buffer gnus-summary-buffer
5639                      gnus-newsgroup-ignored-charsets)
5640                  nil)))
5641           (save-excursion
5642             (unwind-protect
5643                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5644                       (beg (point)))
5645                   (when win
5646                     (select-window win))
5647                   (goto-char point)
5648                   (forward-line)
5649                   (if (mm-handle-displayed-p handle)
5650                       ;; This will remove the part.
5651                       (mm-display-part handle)
5652                     (save-restriction
5653                       (narrow-to-region (point)
5654                                         (if (eobp) (point) (1+ (point))))
5655                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5656                       ;; We narrow to the part itself and
5657                       ;; then call the treatment functions.
5658                       (goto-char (point-min))
5659                       (forward-line 1)
5660                       (narrow-to-region (point) (point-max))
5661                       (gnus-treat-article
5662                        nil id
5663                        (gnus-article-mime-total-parts)
5664                        (mm-handle-media-type handle)))))
5665               (if (window-live-p window)
5666                   (select-window window)))))
5667       (goto-char point)
5668       (gnus-delete-line)
5669       (gnus-insert-mime-button
5670        handle id (list (mm-handle-displayed-p handle)))
5671       (goto-char point))))
5672
5673 (defun gnus-article-goto-part (n)
5674   "Go to MIME part N."
5675   (when gnus-break-pages
5676     (widen))
5677   (prog1
5678       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5679             part handle end next handles)
5680         (when start
5681           (goto-char start)
5682           (if (setq handle (get-text-property start 'gnus-data))
5683               start
5684             ;; Go to the displayed subpart, assuming this is
5685             ;; multipart/alternative.
5686             (setq part start
5687                   end (point-at-eol))
5688             (while (and (not handle)
5689                         part
5690                         (< part end)
5691                         (setq next (text-property-not-all part end
5692                                                           'gnus-data nil)))
5693               (setq part next
5694                     handle (get-text-property part 'gnus-data))
5695               (push (cons handle part) handles)
5696               (unless (mm-handle-displayed-p handle)
5697                 (setq handle nil
5698                       part (text-property-any part end 'gnus-data nil))))
5699             (unless handle
5700               ;; No subpart is displayed, so we find preferred one.
5701               (setq part
5702                     (cdr (assq (mm-preferred-alternative
5703                                 (nreverse (mapcar 'car handles)))
5704                                handles))))
5705             (if part
5706                 (goto-char (1+ part))
5707               start))))
5708     (when gnus-break-pages
5709       (gnus-narrow-to-page))))
5710
5711 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5712   (let ((gnus-tmp-name
5713          (or (mm-handle-filename handle)
5714              (mail-content-type-get (mm-handle-type handle) 'url)
5715              ""))
5716         (gnus-tmp-type (mm-handle-media-type handle))
5717         (gnus-tmp-description (or (mm-handle-description handle) ""))
5718         (gnus-tmp-dots
5719          (if (if displayed (car displayed)
5720                (mm-handle-displayed-p handle))
5721              "" "..."))
5722         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5723                            (buffer-size)))
5724         gnus-tmp-type-long b e)
5725     (when (string-match ".*/" gnus-tmp-name)
5726       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5727     (setq gnus-tmp-type-long (concat gnus-tmp-type
5728                                      (and (not (equal gnus-tmp-name ""))
5729                                           (concat "; " gnus-tmp-name))))
5730     (unless (equal gnus-tmp-description "")
5731       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5732     (unless (bolp)
5733       (insert "\n"))
5734     (setq b (point))
5735     (gnus-eval-format
5736      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5737      `(keymap ,gnus-mime-button-map
5738               gnus-callback gnus-mm-display-part
5739               gnus-part ,gnus-tmp-id
5740               article-type annotation
5741               gnus-data ,handle
5742               rear-nonsticky t))
5743     (setq e (if (bolp)
5744                 ;; Exclude a newline.
5745                 (1- (point))
5746               (point)))
5747     (when gnus-article-button-face
5748       (gnus-overlay-put (gnus-make-overlay b e nil t)
5749                         'face gnus-article-button-face))
5750     (widget-convert-button
5751      'link b e
5752      :mime-handle handle
5753      :action 'gnus-widget-press-button
5754      :button-keymap gnus-mime-button-map
5755      :help-echo
5756      (lambda (widget)
5757        ;; Needed to properly clear the message due to a bug in
5758        ;; wid-edit (XEmacs only).
5759        (if (boundp 'help-echo-owns-message)
5760            (setq help-echo-owns-message t))
5761        (format
5762         "%S: %s the MIME part; %S: more options"
5763         (aref gnus-mouse-2 0)
5764         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5765             "hide" "show")
5766         (aref gnus-down-mouse-3 0))))))
5767
5768 (defun gnus-widget-press-button (elems el)
5769   (goto-char (widget-get elems :from))
5770   (gnus-article-press-button))
5771
5772 (defvar gnus-displaying-mime nil)
5773
5774 (defun gnus-display-mime (&optional ihandles)
5775   "Display the MIME parts."
5776   (save-excursion
5777     (save-selected-window
5778       (let ((window (get-buffer-window gnus-article-buffer))
5779             (point (point)))
5780         (when window
5781           (select-window window)
5782           ;; We have to do this since selecting the window
5783           ;; may change the point.  So we set the window point.
5784           (set-window-point window point)))
5785       (let ((handles ihandles)
5786             (inhibit-read-only t)
5787             handle)
5788         (cond (handles)
5789               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5790                (when gnus-article-emulate-mime
5791                  (mm-uu-dissect-text-parts handles)))
5792               (gnus-article-emulate-mime
5793                (setq handles (mm-uu-dissect))))
5794         (when (and (not ihandles)
5795                    (not gnus-displaying-mime))
5796           ;; Top-level call; we clean up.
5797           (when gnus-article-mime-handles
5798             (mm-destroy-parts gnus-article-mime-handles)
5799             (setq gnus-article-mime-handle-alist nil));; A trick.
5800           (setq gnus-article-mime-handles handles)
5801           ;; We allow users to glean info from the handles.
5802           (when gnus-article-mime-part-function
5803             (gnus-mime-part-function handles)))
5804         (if (and handles
5805                  (or (not (stringp (car handles)))
5806                      (cdr handles)))
5807             (progn
5808               (when (and (not ihandles)
5809                          (not gnus-displaying-mime))
5810                 ;; Clean up for mime parts.
5811                 (article-goto-body)
5812                 (delete-region (point) (point-max)))
5813               (let ((gnus-displaying-mime t))
5814                 (gnus-mime-display-part handles)))
5815           (save-restriction
5816             (article-goto-body)
5817             (narrow-to-region (point) (point-max))
5818             (gnus-treat-article nil 1 1 "text/plain")
5819             (widen)))
5820         (unless ihandles
5821           ;; Highlight the headers.
5822           (save-excursion
5823             (save-restriction
5824               (article-goto-body)
5825               (narrow-to-region (point-min) (point))
5826               (gnus-article-save-original-date
5827                (gnus-treat-article 'head)))))))
5828     ;; Cope with broken MIME messages.
5829     (goto-char (point-max))
5830     (unless (bolp)
5831       (insert "\n"))))
5832
5833 (defcustom gnus-mime-display-multipart-as-mixed nil
5834   "Display \"multipart\" parts as  \"multipart/mixed\".
5835
5836 If t, it overrides nil values of
5837 `gnus-mime-display-multipart-alternative-as-mixed' and
5838 `gnus-mime-display-multipart-related-as-mixed'."
5839   :group 'gnus-article-mime
5840   :type 'boolean)
5841
5842 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5843   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5844   :version "22.1"
5845   :group 'gnus-article-mime
5846   :type 'boolean)
5847
5848 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5849   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5850
5851 If displaying \"text/html\" is discouraged \(see
5852 `mm-discouraged-alternatives'\) images or other material inside a
5853 \"multipart/related\" part might be overlooked when this variable is nil."
5854   :version "22.1"
5855   :group 'gnus-article-mime
5856   :type 'boolean)
5857
5858 (defun gnus-mime-display-part (handle)
5859   (cond
5860    ;; Maybe a broken MIME message.
5861    ((null handle))
5862    ;; Single part.
5863    ((not (stringp (car handle)))
5864     (gnus-mime-display-single handle))
5865    ;; User-defined multipart
5866    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5867     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5868              handle))
5869    ;; multipart/alternative
5870    ((and (equal (car handle) "multipart/alternative")
5871          (not (or gnus-mime-display-multipart-as-mixed
5872                   gnus-mime-display-multipart-alternative-as-mixed)))
5873     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5874       (push (cons id handle) gnus-article-mime-handle-alist)
5875       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5876    ;; multipart/related
5877    ((and (equal (car handle) "multipart/related")
5878          (not (or gnus-mime-display-multipart-as-mixed
5879                   gnus-mime-display-multipart-related-as-mixed)))
5880     ;;;!!!We should find the start part, but we just default
5881     ;;;!!!to the first part.
5882     ;;(gnus-mime-display-part (cadr handle))
5883     ;;;!!! Most multipart/related is an HTML message plus images.
5884     ;;;!!! Unfortunately we are unable to let W3 display those
5885     ;;;!!! included images, so we just display it as a mixed multipart.
5886     ;;(gnus-mime-display-mixed (cdr handle))
5887     ;;;!!! No, w3 can display everything just fine.
5888     (gnus-mime-display-part (cadr handle)))
5889    ((equal (car handle) "multipart/signed")
5890     (gnus-add-wash-type 'signed)
5891     (gnus-mime-display-security handle))
5892    ((equal (car handle) "multipart/encrypted")
5893     (gnus-add-wash-type 'encrypted)
5894     (gnus-mime-display-security handle))
5895    ;; Other multiparts are handled like multipart/mixed.
5896    (t
5897     (gnus-mime-display-mixed (cdr handle)))))
5898
5899 (defun gnus-mime-part-function (handles)
5900   (if (stringp (car handles))
5901       (mapcar 'gnus-mime-part-function (cdr handles))
5902     (funcall gnus-article-mime-part-function handles)))
5903
5904 (defun gnus-mime-display-mixed (handles)
5905   (mapcar 'gnus-mime-display-part handles))
5906
5907 (defun gnus-mime-display-single (handle)
5908   (let ((type (mm-handle-media-type handle))
5909         (ignored gnus-ignored-mime-types)
5910         (not-attachment t)
5911         (move nil)
5912         display text)
5913     (catch 'ignored
5914       (progn
5915         (while ignored
5916           (when (string-match (pop ignored) type)
5917             (throw 'ignored nil)))
5918         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5919                                (with-current-buffer gnus-summary-buffer
5920                                  gnus-inhibit-images)
5921                              gnus-inhibit-images)
5922                            (string-match "\\`image/" type)))
5923                  (setq not-attachment
5924                        (and (not (mm-inline-override-p handle))
5925                             (or (not (mm-handle-disposition handle))
5926                                 (equal (car (mm-handle-disposition handle))
5927                                        "inline")
5928                                 (mm-attachment-override-p handle))))
5929                  (mm-automatic-display-p handle)
5930                  (or (and
5931                       (mm-inlinable-p handle)
5932                       (mm-inlined-p handle))
5933                      (mm-automatic-external-display-p type)))
5934             (setq display t)
5935           (when (equal (mm-handle-media-supertype handle) "text")
5936             (setq text t)))
5937         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5938               beg)
5939           (push (cons id handle) gnus-article-mime-handle-alist)
5940           (when (and display
5941                      (equal (mm-handle-media-supertype handle) "message"))
5942             (insert-char
5943              ?\n
5944              (cond ((not (bolp)) 2)
5945                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5946                    (t 1))))
5947           (when (or (not display)
5948                     (not (gnus-unbuttonized-mime-type-p type))
5949                     (eq id gnus-mime-buttonized-part-id))
5950             (gnus-insert-mime-button
5951              handle id (list (or display (and not-attachment text))))
5952             (gnus-article-insert-newline)
5953             ;; Remember modify the number of forward lines.
5954             (setq move t))
5955           (setq beg (point))
5956           (cond
5957            (display
5958             (when move
5959               (forward-line -1)
5960               (setq beg (point)))
5961             (let ((mail-parse-charset gnus-newsgroup-charset)
5962                   (mail-parse-ignored-charsets
5963                    (save-excursion (condition-case ()
5964                                        (set-buffer gnus-summary-buffer)
5965                                      (error))
5966                                    gnus-newsgroup-ignored-charsets)))
5967               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5968             (goto-char (point-max)))
5969            ((and text not-attachment)
5970             (when move
5971               (forward-line -1)
5972               (setq beg (point)))
5973             (gnus-article-insert-newline)
5974             (mm-display-inline handle)
5975             (goto-char (point-max))))
5976           ;; Do highlighting.
5977           (save-excursion
5978             (save-restriction
5979               (narrow-to-region beg (point))
5980               (if (eq handle gnus-article-mime-handles)
5981                   ;; The format=flowed case.
5982                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5983                 ;; Don't count signature parts that are never displayed.
5984                 ;; The part number should be re-calculated supposing this
5985                 ;; might be a message/rfc822 part.
5986                 (let (handles)
5987                   (dolist (part gnus-article-mime-handles)
5988                     (unless (or (stringp part)
5989                                 (equal (car (mm-handle-type part))
5990                                        "application/pgp-signature"))
5991                       (push part handles)))
5992                   (gnus-treat-article
5993                    nil (length (memq handle handles)) (length handles)
5994                    (mm-handle-media-type handle)))))))))))
5995
5996 (defun gnus-unbuttonized-mime-type-p (type)
5997   "Say whether TYPE is to be unbuttonized."
5998   (unless gnus-inhibit-mime-unbuttonizing
5999     (when (catch 'found
6000             (let ((types gnus-unbuttonized-mime-types))
6001               (while types
6002                 (when (string-match (pop types) type)
6003                   (throw 'found t)))))
6004       (not (catch 'found
6005              (let ((types gnus-buttonized-mime-types))
6006                (while types
6007                  (when (string-match (pop types) type)
6008                    (throw 'found t)))))))))
6009
6010 (defun gnus-article-insert-newline ()
6011   "Insert a newline, but mark it as undeletable."
6012   (gnus-put-text-property
6013    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
6014
6015 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
6016   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
6017          (ihandles handles)
6018          (point (point))
6019          handle (inhibit-read-only t) from props begend not-pref)
6020     (save-window-excursion
6021       (save-restriction
6022         (when ibegend
6023           (narrow-to-region (car ibegend)
6024                             (or (cdr ibegend)
6025                                 (progn
6026                                   (goto-char (car ibegend))
6027                                   (forward-line 2)
6028                                   (point))))
6029           (delete-region (point-min) (point-max))
6030           (mm-remove-parts handles))
6031         (setq begend (list (point-marker)))
6032         ;; Do the toggle.
6033         (unless (setq not-pref (cadr (member preferred ihandles)))
6034           (setq not-pref (car ihandles)))
6035         (when (or ibegend
6036                   (not preferred)
6037                   (not (gnus-unbuttonized-mime-type-p
6038                         "multipart/alternative")))
6039           (gnus-add-text-properties
6040            (setq from (point))
6041            (progn
6042              (insert (format "%d.  " id))
6043              (point))
6044            `(gnus-callback
6045              (lambda (handles)
6046                (unless ,(not ibegend)
6047                  (setq gnus-article-mime-handle-alist
6048                        ',gnus-article-mime-handle-alist))
6049                (gnus-mime-display-alternative
6050                 ',ihandles ',not-pref ',begend ,id))
6051              keymap ,gnus-mime-button-map
6052              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6053              face ,gnus-article-button-face
6054              gnus-part ,id
6055              article-type multipart
6056              rear-nonsticky t))
6057           (widget-convert-button 'link from (point)
6058                                  :action 'gnus-widget-press-button
6059                                  :button-keymap gnus-widget-button-keymap)
6060           ;; Do the handles
6061           (while (setq handle (pop handles))
6062             (gnus-add-text-properties
6063              (setq from (point))
6064              (progn
6065                (insert (format "(%c) %-18s"
6066                                (if (equal handle preferred) ?* ? )
6067                                (mm-handle-media-type handle)))
6068                (point))
6069              `(gnus-callback
6070                (lambda (handles)
6071                  (unless ,(not ibegend)
6072                    (setq gnus-article-mime-handle-alist
6073                          ',gnus-article-mime-handle-alist))
6074                  (gnus-mime-display-alternative
6075                   ',ihandles ',handle ',begend ,id))
6076                keymap ,gnus-mime-button-map
6077                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6078                face ,gnus-article-button-face
6079                gnus-part ,id
6080                gnus-data ,handle
6081                rear-nonsticky t))
6082             (widget-convert-button 'link from (point)
6083                                    :action 'gnus-widget-press-button
6084                                    :button-keymap gnus-widget-button-keymap)
6085             (insert "  "))
6086           (insert "\n\n"))
6087         (when preferred
6088           (if (stringp (car preferred))
6089               (gnus-display-mime preferred)
6090             (let ((mail-parse-charset gnus-newsgroup-charset)
6091                   (mail-parse-ignored-charsets
6092                    (with-current-buffer gnus-summary-buffer
6093                      gnus-newsgroup-ignored-charsets)))
6094               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6095               ;; Do highlighting.
6096               (save-excursion
6097                 (save-restriction
6098                   (narrow-to-region (car begend) (point-max))
6099                   (gnus-treat-article
6100                    nil (length gnus-article-mime-handle-alist)
6101                    (gnus-article-mime-total-parts)
6102                    (mm-handle-media-type preferred))))))
6103           (goto-char (point-max))
6104           (setcdr begend (point-marker)))))
6105     (when ibegend
6106       (goto-char point))))
6107
6108 (defconst gnus-article-wash-status-strings
6109   (let ((alist '((cite "c" "Possible hidden citation text"
6110                        " " "All citation text visible")
6111                  (headers "h" "Hidden headers"
6112                           " " "All headers visible.")
6113                  (pgp "p" "Encrypted or signed message status hidden"
6114                       " " "No hidden encryption nor digital signature status")
6115                  (signature "s" "Signature has been hidden"
6116                             " " "Signature is visible")
6117                  (overstrike "o" "Overstrike (^H) characters applied"
6118                              " " "No overstrike characters applied")
6119                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6120                            " " "No /*_emphasis_*/ characters applied")))
6121         result)
6122     (dolist (entry alist result)
6123       (let ((key (nth 0 entry))
6124             (on (copy-sequence (nth 1 entry)))
6125             (on-help (nth 2 entry))
6126             (off (copy-sequence (nth 3 entry)))
6127             (off-help (nth 4 entry)))
6128         (put-text-property 0 1 'help-echo on-help on)
6129         (put-text-property 0 1 'help-echo off-help off)
6130         (push (list key on off) result))))
6131   "Alist of strings describing wash status in the mode line.
6132 Each entry has the form (KEY ON OF), where the KEY is a symbol
6133 representing the particular washing function, ON is the string to use
6134 in the article mode line when the washing function is active, and OFF
6135 is the string to use when it is inactive.")
6136
6137 (defun gnus-article-wash-status-entry (key value)
6138   (let ((entry (assoc key gnus-article-wash-status-strings)))
6139     (if value (nth 1 entry) (nth 2 entry))))
6140
6141 (defun gnus-article-wash-status ()
6142   "Return a string which display status of article washing."
6143   (with-current-buffer gnus-article-buffer
6144     (let ((cite (memq 'cite gnus-article-wash-types))
6145           (headers (memq 'headers gnus-article-wash-types))
6146           (boring (memq 'boring-headers gnus-article-wash-types))
6147           (pgp (memq 'pgp gnus-article-wash-types))
6148           (pem (memq 'pem gnus-article-wash-types))
6149           (signed (memq 'signed gnus-article-wash-types))
6150           (encrypted (memq 'encrypted gnus-article-wash-types))
6151           (signature (memq 'signature gnus-article-wash-types))
6152           (overstrike (memq 'overstrike gnus-article-wash-types))
6153           (emphasis (memq 'emphasis gnus-article-wash-types)))
6154       (concat
6155        (gnus-article-wash-status-entry 'cite cite)
6156        (gnus-article-wash-status-entry 'headers (or headers boring))
6157        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6158        (gnus-article-wash-status-entry 'signature signature)
6159        (gnus-article-wash-status-entry 'overstrike overstrike)
6160        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6161
6162 (defun gnus-add-wash-type (type)
6163   "Add a washing of TYPE to the current status."
6164   (add-to-list 'gnus-article-wash-types type))
6165
6166 (defun gnus-delete-wash-type (type)
6167   "Add a washing of TYPE to the current status."
6168   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6169
6170 (defun gnus-add-image (category image)
6171   "Add IMAGE of CATEGORY to the list of displayed images."
6172   (let ((entry (assq category gnus-article-image-alist)))
6173     (unless entry
6174       (setq entry (list category))
6175       (push entry gnus-article-image-alist))
6176     (nconc entry (list image))))
6177
6178 (defun gnus-delete-images (category)
6179   "Delete all images in CATEGORY."
6180   (let ((entry (assq category gnus-article-image-alist)))
6181     (dolist (image (cdr entry))
6182       (gnus-remove-image image category))
6183     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6184     (gnus-delete-wash-type category)))
6185
6186 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6187
6188 (defun gnus-article-maybe-hide-headers ()
6189   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6190 Provided for backwards compatibility."
6191   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6192                  (not (with-current-buffer gnus-summary-buffer
6193                         gnus-have-all-headers)))
6194              (not gnus-inhibit-hiding))
6195     (gnus-article-hide-headers)))
6196
6197 (declare-function shr-put-image "shr" (data alt &optional flags))
6198
6199 (defun gnus-shr-put-image (data alt &optional flags)
6200   "Put image DATA with a string ALT.  Enable image to be deleted."
6201   (let ((image (if flags
6202                    (shr-put-image data (propertize (or alt "*")
6203                                                    'gnus-image-category 'shr)
6204                                   flags)
6205                  ;; Old `shr-put-image' doesn't take the optional `flags'
6206                  ;; argument.
6207                  (shr-put-image data (propertize (or alt "*")
6208                                                  'gnus-image-category 'shr)))))
6209     (when image
6210       (gnus-add-image 'shr image))))
6211
6212 ;;; Article savers.
6213
6214 (defun gnus-output-to-file (file-name)
6215   "Append the current article to a file named FILE-NAME.
6216 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6217 text and used as the value of the coding cookie which is added to the
6218 top of a file.  Otherwise, this function saves a raw article without
6219 the coding cookie."
6220   (let* ((artbuf (current-buffer))
6221          (file-name-coding-system nnmail-pathname-coding-system)
6222          (coding gnus-article-save-coding-system)
6223          (coding-system-for-read (if coding
6224                                      nil ;; Rely on the coding cookie.
6225                                    mm-text-coding-system))
6226          (coding-system-for-write (or coding
6227                                       mm-text-coding-system-for-write
6228                                       mm-text-coding-system))
6229          (exists (file-exists-p file-name)))
6230     (with-temp-buffer
6231       (when exists
6232         (insert-file-contents file-name)
6233         (goto-char (point-min))
6234         ;; Remove the existing coding cookie.
6235         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6236           (delete-region (match-beginning 0) (match-end 0))))
6237       (goto-char (point-max))
6238       (insert-buffer-substring artbuf)
6239       ;; Append newline at end of the buffer as separator, and then
6240       ;; save it to file.
6241       (goto-char (point-max))
6242       (insert "\n")
6243       (when coding
6244         ;; If the coding system is not suitable to encode the text,
6245         ;; ask a user for a proper one.
6246         (when (fboundp 'select-safe-coding-system)
6247           (setq coding (coding-system-base
6248                         (save-window-excursion
6249                           (select-safe-coding-system (point-min) (point-max)
6250                                                      coding))))
6251           (setq coding-system-for-write
6252                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6253                     coding)))
6254         (goto-char (point-min))
6255         ;; Add the coding cookie.
6256         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6257                         coding-system-for-write)))
6258       (if exists
6259           (progn
6260             (write-region (point-min) (point-max) file-name nil 'no-message)
6261             (message "Appended to %s" file-name))
6262         (write-region (point-min) (point-max) file-name))))
6263   t)
6264
6265 (defun gnus-narrow-to-page (&optional arg)
6266   "Narrow the article buffer to a page.
6267 If given a numerical ARG, move forward ARG pages."
6268   (interactive "P")
6269   (setq arg (if arg (prefix-numeric-value arg) 0))
6270   (with-current-buffer gnus-article-buffer
6271     (widen)
6272     ;; Remove any old next/prev buttons.
6273     (when (gnus-visual-p 'page-marker)
6274       (let ((inhibit-read-only t))
6275         (gnus-remove-text-with-property 'gnus-prev)
6276         (gnus-remove-text-with-property 'gnus-next)))
6277     (let (st nd pt)
6278       (when (save-excursion
6279               (cond ((< arg 0)
6280                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6281                          (prog1
6282                              (setq nd (match-beginning 0)
6283                                    pt nd)
6284                            (when (re-search-backward page-delimiter nil t)
6285                              (setq st (match-end 0))))
6286                        (when (re-search-forward page-delimiter nil t)
6287                          (setq nd (match-beginning 0)
6288                                pt (point-min)))))
6289                     ((> arg 0)
6290                      (if (re-search-forward page-delimiter nil 'move arg)
6291                          (prog1
6292                              (setq st (match-end 0)
6293                                    pt st)
6294                            (when (re-search-forward page-delimiter nil t)
6295                              (setq nd (match-beginning 0))))
6296                        (when (re-search-backward page-delimiter nil t)
6297                          (setq st (match-end 0)
6298                                pt (point-max)))))
6299                     (t
6300                      (when (re-search-backward page-delimiter nil t)
6301                        (goto-char (setq st (match-end 0))))
6302                      (when (re-search-forward page-delimiter nil t)
6303                        (setq nd (match-beginning 0)))
6304                      (or st nd))))
6305         (setq gnus-page-broken t)
6306         (when pt (goto-char pt))
6307         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6308         (when (gnus-visual-p 'page-marker)
6309           (save-excursion
6310             (when nd
6311               (goto-char nd)
6312               (gnus-insert-next-page-button))
6313             (when st
6314               (goto-char st)
6315               (gnus-insert-prev-page-button))))))))
6316
6317 ;; Article mode commands
6318
6319 (defun gnus-article-goto-next-page ()
6320   "Show the next page of the article."
6321   (interactive)
6322   (when (gnus-article-next-page)
6323     (goto-char (point-min))
6324     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6325
6326
6327 (defun gnus-article-goto-prev-page ()
6328   "Show the previous page of the article."
6329   (interactive)
6330   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6331       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6332     (gnus-article-prev-page nil)))
6333
6334 ;; This is cleaner but currently breaks `gnus-pick-mode':
6335 ;;
6336 ;; (defun gnus-article-goto-next-page ()
6337 ;;   "Show the next page of the article."
6338 ;;   (interactive)
6339 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6340 ;;     (gnus-summary-next-page)))
6341 ;;
6342 ;; (defun gnus-article-goto-prev-page ()
6343 ;;   "Show the next page of the article."
6344 ;;   (interactive)
6345 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6346 ;;     (gnus-summary-prev-page)))
6347
6348 (defun gnus-article-next-page (&optional lines)
6349   "Show the next page of the current article.
6350 If end of article, return non-nil.  Otherwise return nil.
6351 Argument LINES specifies lines to be scrolled up."
6352   (interactive "p")
6353   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6354   (if (and (not (and gnus-article-over-scroll
6355                      (> (count-lines (window-start) (point-max))
6356                         (if (featurep 'xemacs)
6357                             (or lines (1- (window-height)))
6358                           (+ (or lines (1- (window-height))) scroll-margin)))))
6359            (save-excursion
6360              (end-of-line)
6361              (and (pos-visible-in-window-p)     ;Not continuation line.
6362                   (>= (point) (point-max)))))
6363       ;; Nothing in this page.
6364       (if (or (not gnus-page-broken)
6365               (save-excursion
6366                 (save-restriction
6367                   (widen)
6368                   (forward-line)
6369                   (eobp)))) ;Real end-of-buffer?
6370           (progn
6371             (when gnus-article-over-scroll
6372               (gnus-article-next-page-1 lines))
6373             t)                  ;Nothing more.
6374         (gnus-narrow-to-page 1)         ;Go to next page.
6375         nil)
6376     ;; More in this page.
6377     (gnus-article-next-page-1 lines)
6378     nil))
6379
6380 (defun gnus-article-beginning-of-window ()
6381   "Move point to the beginning of the window.
6382 In Emacs, the point is placed at the line number which `scroll-margin'
6383 specifies."
6384   (if (featurep 'xemacs)
6385       (move-to-window-line 0)
6386     ;; There is an obscure bug in Emacs that makes it impossible to
6387     ;; scroll past big pictures in the article buffer.  Try to fix
6388     ;; this by adding a sanity check by counting the lines visible.
6389     (when (> (count-lines (window-start) (window-end)) 30)
6390       (move-to-window-line
6391        (min (max 0 scroll-margin)
6392             (max 1 (- (window-height)
6393                       (if mode-line-format 1 0)
6394                       (if header-line-format 1 0)
6395                       2)))))))
6396
6397 (defun gnus-article-next-page-1 (lines)
6398   (condition-case ()
6399       (let ((scroll-in-place nil)
6400             (auto-window-vscroll nil))
6401         (scroll-up lines))
6402     (end-of-buffer
6403      ;; Long lines may cause an end-of-buffer error.
6404      (goto-char (point-max))))
6405   (gnus-article-beginning-of-window))
6406
6407 (defun gnus-article-prev-page (&optional lines)
6408   "Show previous page of current article.
6409 Argument LINES specifies lines to be scrolled down."
6410   (interactive "p")
6411   (move-to-window-line 0)
6412   (if (and gnus-page-broken
6413            (bobp)
6414            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6415       (progn
6416         (gnus-narrow-to-page -1)        ;Go to previous page.
6417         (goto-char (point-max))
6418         (recenter (if gnus-article-over-scroll
6419                       (if lines
6420                           (max (if (featurep 'xemacs)
6421                                    lines
6422                                  (+ lines scroll-margin))
6423                                3)
6424                         (- (window-height) 2))
6425                     -1)))
6426     (prog1
6427         (condition-case ()
6428             (let ((scroll-in-place nil))
6429               (scroll-down lines))
6430           (beginning-of-buffer
6431            (goto-char (point-min))))
6432       (gnus-article-beginning-of-window))))
6433
6434 (defun gnus-article-only-boring-p ()
6435   "Decide whether there is only boring text remaining in the article.
6436 Something \"interesting\" is a word of at least two letters that does
6437 not have a face in `gnus-article-boring-faces'."
6438   (when (and gnus-article-skip-boring
6439              (boundp 'gnus-article-boring-faces)
6440              (symbol-value 'gnus-article-boring-faces))
6441     (save-excursion
6442       (let ((inhibit-point-motion-hooks t))
6443         (catch 'only-boring
6444           (while (re-search-forward "\\b\\w\\w" nil t)
6445             (forward-char -1)
6446             (when (not (gnus-intersection
6447                         (gnus-faces-at (point))
6448                         (symbol-value 'gnus-article-boring-faces)))
6449               (throw 'only-boring nil)))
6450           (throw 'only-boring t))))))
6451
6452 (defun gnus-article-refer-article ()
6453   "Read article specified by message-id around point."
6454   (interactive)
6455   (save-excursion
6456     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6457     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6458     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6459         (let ((msg-id (concat "<" (match-string 0) ">")))
6460           (set-buffer gnus-summary-buffer)
6461           (gnus-summary-refer-article msg-id))
6462       (error "No references around point"))))
6463
6464 (defun gnus-article-show-summary ()
6465   "Reconfigure windows to show summary buffer."
6466   (interactive)
6467   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6468       (error "There is no summary buffer for this article buffer")
6469     (gnus-article-set-globals)
6470     (gnus-configure-windows 'article)
6471     (gnus-summary-goto-subject gnus-current-article)
6472     (gnus-summary-position-point)))
6473
6474 (defun gnus-article-describe-briefly ()
6475   "Describe article mode commands briefly."
6476   (interactive)
6477   (gnus-message 6 "%s" (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")))
6478
6479 (defun gnus-article-check-buffer ()
6480   "Beep if not in an article buffer."
6481   (unless (equal major-mode 'gnus-article-mode)
6482     (error "Command invoked outside of a Gnus article buffer")))
6483
6484 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6485   "Read a summary buffer key sequence and execute it from the article buffer."
6486   (interactive "P")
6487   (gnus-article-check-buffer)
6488   (let ((nosaves
6489          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6490            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6491            "=" "^" "\M-^" "|"))
6492         (nosave-but-article
6493          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6494            "An" "Ap" [?A (meta return)] [?A delete]))
6495         (nosave-in-article
6496          '("AS" "\C-d"))
6497         (up-to-top
6498          '("n" "Gn" "p" "Gp"))
6499         keys new-sum-point)
6500     (with-current-buffer gnus-article-current-summary
6501       (let (gnus-pick-mode)
6502         (setq unread-command-events (nconc unread-command-events
6503                                            (list (or key last-command-event)))
6504               keys (if (featurep 'xemacs)
6505                        (events-to-keys (read-key-sequence nil t))
6506                      (read-key-sequence nil t)))))
6507
6508     (message "")
6509
6510     (cond
6511      ((eq (aref keys (1- (length keys))) ?\C-h)
6512       (gnus-article-describe-bindings (substring keys 0 -1)))
6513      ((or (member keys nosaves)
6514           (member keys nosave-but-article)
6515           (member keys nosave-in-article))
6516       (let (func)
6517         (save-window-excursion
6518           (pop-to-buffer gnus-article-current-summary)
6519           ;; We disable the pick minor mode commands.
6520           (let (gnus-pick-mode)
6521             (setq func (key-binding keys t))))
6522         (if (or (not func)
6523                 (numberp func))
6524             (ding)
6525           (unless (member keys nosave-in-article)
6526             (set-buffer gnus-article-current-summary))
6527           (when (and (symbolp func)
6528                      (get func 'disabled))
6529             (error "Function %s disabled" func))
6530           (call-interactively func)
6531           (setq new-sum-point (point)))
6532         (when (member keys nosave-but-article)
6533           (pop-to-buffer gnus-article-buffer))))
6534      (t
6535       ;; These commands should restore window configuration.
6536       (let ((obuf (current-buffer))
6537             (owin (current-window-configuration))
6538             win func in-buffer selected new-sum-start new-sum-hscroll err)
6539         (cond (not-restore-window
6540                (pop-to-buffer gnus-article-current-summary)
6541                (setq win (selected-window)))
6542               ((setq win (get-buffer-window gnus-article-current-summary))
6543                (select-window win))
6544               (t
6545                (let ((summary-buffer gnus-article-current-summary))
6546                  (gnus-configure-windows 'article)
6547                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6548                    (let ((gnus-buffer-configuration
6549                           '((article ((vertical 1.0
6550                                                 (summary 0.25 point)
6551                                                 (article 1.0)))))))
6552                      (gnus-configure-windows 'article))
6553                    (setq win (get-buffer-window summary-buffer 'visible)))
6554                  (gnus-select-frame-set-input-focus (window-frame win))
6555                  (select-window win))))
6556         (setq in-buffer (current-buffer))
6557         ;; We disable the pick minor mode commands.
6558         (setq func (let (gnus-pick-mode)
6559                      (key-binding keys t)))
6560         (when (get func 'disabled)
6561           (error "Function %s disabled" func))
6562         (if (and func
6563                  (functionp func)
6564                  (condition-case code
6565                      (progn
6566                        (call-interactively func)
6567                        t)
6568                    (error
6569                     (setq err code)
6570                     nil)))
6571             (progn
6572               (when (eq win (selected-window))
6573                 (setq new-sum-point (point)
6574                       new-sum-start (window-start win)
6575                       new-sum-hscroll (window-hscroll win)))
6576               (when (or (eq in-buffer (current-buffer))
6577                         (when (eq obuf (current-buffer))
6578                           (set-buffer in-buffer)
6579                           t))
6580                 (setq selected (gnus-summary-select-article))
6581                 (set-buffer obuf)
6582                 (unless not-restore-window
6583                   (set-window-configuration owin))
6584                 (when (and (eq selected 'old)
6585                            new-sum-point)
6586                   (set-window-start (get-buffer-window (current-buffer))
6587                                     1)
6588                   (set-window-point (get-buffer-window (current-buffer))
6589                                     (if (article-goto-body)
6590                                         (1- (point))
6591                                       (point))))
6592                 (when (and (not not-restore-window)
6593                            new-sum-point
6594                            (window-live-p win)
6595                            (with-current-buffer (window-buffer win)
6596                              (eq major-mode 'gnus-summary-mode)))
6597                   (set-window-point win new-sum-point)
6598                   (set-window-start win new-sum-start)
6599                   (set-window-hscroll win new-sum-hscroll))))
6600           (set-window-configuration owin)
6601           (if err
6602               (signal (car err) (cdr err))
6603             (ding))))))))
6604
6605 (defun gnus-article-read-summary-send-keys ()
6606   (interactive)
6607   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6608     (gnus-article-read-summary-keys)))
6609
6610 (defun gnus-article-describe-key (key)
6611   "Display documentation of the function invoked by KEY.
6612 KEY is a string or a vector."
6613   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6614                        (read-key-sequence "Describe key: "))))
6615   (gnus-article-check-buffer)
6616   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6617                                   gnus-article-read-summary-send-keys))
6618       (with-current-buffer gnus-article-current-summary
6619         (setq unread-command-events
6620               (if (featurep 'xemacs)
6621                   (append key nil)
6622                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6623                                         (list 'meta (- x 128))
6624                                       x))
6625                         key)))
6626         (let ((cursor-in-echo-area t)
6627               gnus-pick-mode)
6628           (describe-key (read-key-sequence nil t))))
6629     (describe-key key)))
6630
6631 (defun gnus-article-describe-key-briefly (key &optional insert)
6632   "Display documentation of the function invoked by KEY.
6633 KEY is a string or a vector."
6634   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6635                        (read-key-sequence "Describe key: "))
6636                      current-prefix-arg))
6637   (gnus-article-check-buffer)
6638   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6639                                   gnus-article-read-summary-send-keys))
6640       (with-current-buffer gnus-article-current-summary
6641         (setq unread-command-events
6642               (if (featurep 'xemacs)
6643                   (append key nil)
6644                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6645                                         (list 'meta (- x 128))
6646                                       x))
6647                         key)))
6648         (let ((cursor-in-echo-area t)
6649               gnus-pick-mode)
6650           (describe-key-briefly (read-key-sequence nil t) insert)))
6651     (describe-key-briefly key insert)))
6652
6653 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6654 (defvar gnus-agent-summary-mode)
6655 (defvar gnus-draft-mode)
6656 (defvar help-xref-stack-item)
6657
6658 (defun gnus-article-describe-bindings (&optional prefix)
6659   "Show a list of all defined keys, and their definitions.
6660 The optional argument PREFIX, if non-nil, should be a key sequence;
6661 then we display only bindings that start with that prefix."
6662   (interactive)
6663   (gnus-article-check-buffer)
6664   (let ((keymap (copy-keymap gnus-article-mode-map))
6665         (map (copy-keymap gnus-article-send-map))
6666         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6667         parent agent draft)
6668     (define-key keymap "S" map)
6669     (define-key map [t] nil)
6670     (with-current-buffer gnus-article-current-summary
6671       (set-keymap-parent
6672        keymap
6673        (if (setq parent (keymap-parent gnus-article-mode-map))
6674            (prog1
6675                (setq parent (copy-keymap parent))
6676              (set-keymap-parent parent (current-local-map)))
6677          (current-local-map)))
6678       (set-keymap-parent map (key-binding "S"))
6679       (let (key def gnus-pick-mode)
6680         (while sumkeys
6681           (setq key (pop sumkeys))
6682           (cond ((and (vectorp key) (= (length key) 1)
6683                       (consp (setq def (aref key 0)))
6684                       (numberp (car def)) (numberp (cdr def)))
6685                  (when (< (max (car def) (cdr def)) 128)
6686                    (setq sumkeys
6687                          (append (mapcar
6688                                   #'vector
6689                                   (nreverse (gnus-uncompress-range def)))
6690                                  sumkeys))))
6691                 ((setq def (key-binding key))
6692                  (unless (eq def 'undefined)
6693                    (define-key keymap key def))))))
6694       (when (boundp 'gnus-agent-summary-mode)
6695         (setq agent gnus-agent-summary-mode))
6696       (when (boundp 'gnus-draft-mode)
6697         (setq draft gnus-draft-mode)))
6698     (with-temp-buffer
6699       (use-local-map keymap)
6700       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6701       (set (make-local-variable 'gnus-draft-mode) draft)
6702       (describe-bindings prefix))
6703     (let ((item `((lambda (prefix)
6704                     (with-current-buffer ,(current-buffer)
6705                       (gnus-article-describe-bindings prefix)))
6706                   ,prefix)))
6707       ;; Loading `help-mode' here is necessary if `describe-bindings'
6708       ;; is replaced with something, e.g. `helm-descbinds'.
6709       (require 'help-mode)
6710       (with-current-buffer (let (help-xref-following) (help-buffer))
6711         (setq help-xref-stack-item item)))))
6712
6713 (defun gnus-article-reply-with-original (&optional wide)
6714   "Start composing a reply mail to the current message.
6715 The text in the region will be yanked.  If the region isn't active,
6716 the entire article will be yanked."
6717   (interactive)
6718   (let ((article (cdr gnus-article-current))
6719         contents)
6720     (if (not (gnus-region-active-p))
6721         (with-current-buffer gnus-summary-buffer
6722           (gnus-summary-reply (list (list article)) wide))
6723       (setq contents (buffer-substring (point) (mark t)))
6724       ;; Deactivate active regions.
6725       (when (and (boundp 'transient-mark-mode)
6726                  transient-mark-mode)
6727         (setq mark-active nil))
6728       (with-current-buffer gnus-summary-buffer
6729         (gnus-summary-reply
6730          (list (list article contents)) wide)))))
6731
6732 (defun gnus-article-wide-reply-with-original ()
6733   "Start composing a wide reply mail to the current message.
6734 The text in the region will be yanked.  If the region isn't active,
6735 the entire article will be yanked."
6736   (interactive)
6737   (gnus-article-reply-with-original t))
6738
6739 (defun gnus-article-followup-with-original ()
6740   "Compose a followup to the current article.
6741 The text in the region will be yanked.  If the region isn't active,
6742 the entire article will be yanked."
6743   (interactive)
6744   (let ((article (cdr gnus-article-current))
6745         contents)
6746       (if (not (gnus-region-active-p))
6747           (with-current-buffer gnus-summary-buffer
6748             (gnus-summary-followup (list (list article))))
6749         (setq contents (buffer-substring (point) (mark t)))
6750         ;; Deactivate active regions.
6751         (when (and (boundp 'transient-mark-mode)
6752                    transient-mark-mode)
6753           (setq mark-active nil))
6754         (with-current-buffer gnus-summary-buffer
6755           (gnus-summary-followup
6756            (list (list article contents)))))))
6757
6758 (defun gnus-article-hide (&optional arg force)
6759   "Hide all the gruft in the current article.
6760 This means that signatures, cited text and (some) headers will be
6761 hidden.
6762 If given a prefix, show the hidden text instead."
6763   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6764   (gnus-article-hide-headers arg)
6765   (gnus-article-hide-list-identifiers arg)
6766   (gnus-article-hide-citation-maybe arg force)
6767   (gnus-article-hide-signature arg))
6768
6769 (defun gnus-check-group-server ()
6770   ;; Make sure the connection to the server is alive.
6771   (unless (gnus-server-opened
6772            (gnus-find-method-for-group gnus-newsgroup-name))
6773     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6774     (gnus-request-group gnus-newsgroup-name t)))
6775
6776 (eval-when-compile
6777   (autoload 'nneething-get-file-name "nneething"))
6778
6779 (defun gnus-request-article-this-buffer (article group)
6780   "Get an article and insert it into this buffer."
6781   (let (do-update-line sparse-header)
6782     (prog1
6783         (save-excursion
6784           (erase-buffer)
6785           (gnus-kill-all-overlays)
6786           (setq group (or group gnus-newsgroup-name))
6787
6788           ;; Using `gnus-request-article' directly will insert the article into
6789           ;; `nntp-server-buffer' - so we'll save some time by not having to
6790           ;; copy it from the server buffer into the article buffer.
6791
6792           ;; We only request an article by message-id when we do not have the
6793           ;; headers for it, so we'll have to get those.
6794           (when (stringp article)
6795             (gnus-read-header article))
6796
6797           ;; If the article number is negative, that means that this article
6798           ;; doesn't belong in this newsgroup (possibly), so we find its
6799           ;; message-id and request it by id instead of number.
6800           (when (and (numberp article)
6801                      gnus-summary-buffer
6802                      (get-buffer gnus-summary-buffer)
6803                      (gnus-buffer-exists-p gnus-summary-buffer))
6804             (with-current-buffer gnus-summary-buffer
6805               (let ((header (gnus-summary-article-header article)))
6806                 (when (< article 0)
6807                   (cond
6808                    ((memq article gnus-newsgroup-sparse)
6809                     ;; This is a sparse gap article.
6810                     (setq do-update-line article)
6811                     (setq article (mail-header-id header))
6812                     (setq sparse-header (gnus-read-header article))
6813                     (setq gnus-newsgroup-sparse
6814                           (delq article gnus-newsgroup-sparse)))
6815                    ((vectorp header)
6816                     ;; It's a real article.
6817                     (setq article (mail-header-id header)))
6818                    (t
6819                     ;; It is an extracted pseudo-article.
6820                     (setq article 'pseudo)
6821                     (gnus-request-pseudo-article header))))
6822
6823                 (let ((method (gnus-find-method-for-group
6824                                gnus-newsgroup-name)))
6825                   (when (and (eq (car method) 'nneething)
6826                              (vectorp header))
6827                     (let ((dir (nneething-get-file-name
6828                                 (mail-header-id header))))
6829                       (when (and (stringp dir)
6830                                  (file-directory-p dir))
6831                         (setq article 'nneething)
6832                         (gnus-group-enter-directory dir))))))))
6833
6834           (cond
6835            ;; Refuse to select canceled articles.
6836            ((and (numberp article)
6837                  gnus-summary-buffer
6838                  (get-buffer gnus-summary-buffer)
6839                  (gnus-buffer-exists-p gnus-summary-buffer)
6840                  (eq (cdr (with-current-buffer gnus-summary-buffer
6841                             (assq article gnus-newsgroup-reads)))
6842                      gnus-canceled-mark))
6843             nil)
6844            ;; We first check `gnus-original-article-buffer'.
6845            ((and (get-buffer gnus-original-article-buffer)
6846                  (numberp article)
6847                  (with-current-buffer gnus-original-article-buffer
6848                    (and (equal (car gnus-original-article) group)
6849                         (eq (cdr gnus-original-article) article))))
6850             ;; `insert-buffer-substring' would incorrectly use the
6851             ;; equivalent of string-make-multibyte which amount to decoding
6852             ;; with locale-coding-system, causing failure of
6853             ;; subsequent decoding.
6854             (insert (mm-string-to-multibyte
6855                      (with-current-buffer gnus-original-article-buffer
6856                        (buffer-substring (point-min) (point-max)))))
6857             'article)
6858            ;; Check the backlog.
6859            ((and gnus-keep-backlog
6860                  (gnus-backlog-request-article group article (current-buffer)))
6861             'article)
6862            ;; Check asynchronous pre-fetch.
6863            ((gnus-async-request-fetched-article group article (current-buffer))
6864             (gnus-async-prefetch-next group article gnus-summary-buffer)
6865             (when (and (numberp article) gnus-keep-backlog)
6866               (gnus-backlog-enter-article group article (current-buffer)))
6867             'article)
6868            ;; Check the cache.
6869            ((and gnus-use-cache
6870                  (numberp article)
6871                  (gnus-cache-request-article article group))
6872             'article)
6873            ;; Check the agent cache.
6874            ((gnus-agent-request-article article group)
6875             'article)
6876            ;; Get the article and put into the article buffer.
6877            ((or (stringp article)
6878                 (numberp article))
6879             (let ((gnus-override-method gnus-override-method)
6880                   (methods (and (stringp article)
6881                                 (with-current-buffer gnus-summary-buffer
6882                                   (gnus-refer-article-methods))))
6883                   (backend (car (gnus-find-method-for-group
6884                                  gnus-newsgroup-name)))
6885                   result
6886                   (inhibit-read-only t))
6887               (when (and (null gnus-override-method)
6888                          methods)
6889                 (setq gnus-override-method (pop methods)))
6890               (while (not result)
6891                 (erase-buffer)
6892                 (gnus-kill-all-overlays)
6893                 (let ((gnus-newsgroup-name group))
6894                   (gnus-check-group-server))
6895                 (cond
6896                  ((gnus-request-article article group (current-buffer))
6897                   (when (numberp article)
6898                     (gnus-async-prefetch-next group article
6899                                               gnus-summary-buffer)
6900                     (when gnus-keep-backlog
6901                       (gnus-backlog-enter-article
6902                        group article (current-buffer)))
6903                     (when (and gnus-agent
6904                                (gnus-agent-group-covered-p group))
6905                       (gnus-agent-store-article article group)))
6906                   (setq result 'article))
6907                  (methods
6908                   (setq gnus-override-method (pop methods)))
6909                  ((not (string-match "^400 "
6910                                      (nnheader-get-report backend)))
6911                   ;; If we get 400 server disconnect, reconnect and
6912                   ;; retry; otherwise, assume the article has expired.
6913                   (setq result 'done))))
6914               (and (eq result 'article) 'article)))
6915            ;; It was a pseudo.
6916            (t article)))
6917
6918       ;; Associate this article with the current summary buffer.
6919       (setq gnus-article-current-summary gnus-summary-buffer)
6920
6921       ;; Take the article from the original article buffer
6922       ;; and place it in the buffer it's supposed to be in.
6923       (when (and (get-buffer gnus-article-buffer)
6924                  (equal (buffer-name (current-buffer))
6925                         (buffer-name (get-buffer gnus-article-buffer))))
6926         (save-excursion
6927           (if (get-buffer gnus-original-article-buffer)
6928               (set-buffer gnus-original-article-buffer)
6929             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6930             (buffer-disable-undo)
6931             (setq major-mode 'gnus-original-article-mode)
6932             (setq buffer-read-only t))
6933           (let ((inhibit-read-only t))
6934             (erase-buffer)
6935             (insert-buffer-substring gnus-article-buffer))
6936           (setq gnus-original-article (cons group article)))
6937
6938         ;; Decode charsets.
6939         (run-hooks 'gnus-article-decode-hook)
6940         ;; Mark article as decoded or not.
6941         (setq gnus-article-decoded-p gnus-article-decode-hook))
6942
6943       ;; Update sparse articles.
6944       (when (and do-update-line
6945                  (or (numberp article)
6946                      (stringp article)))
6947         (let ((buf (current-buffer)))
6948           (set-buffer gnus-summary-buffer)
6949           (gnus-summary-update-article do-update-line sparse-header)
6950           (gnus-summary-goto-subject do-update-line nil t)
6951           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6952                             (point))
6953           (set-buffer buf))))))
6954
6955 (defun gnus-block-private-groups (group)
6956   (if (or (gnus-news-group-p group)
6957           (gnus-member-of-valid 'global group))
6958       ;; Block nothing in news groups.
6959       nil
6960     ;; Block everything anywhere else.
6961     "."))
6962
6963 (defun gnus-blocked-images ()
6964   (if (functionp gnus-blocked-images)
6965       (funcall gnus-blocked-images gnus-newsgroup-name)
6966     gnus-blocked-images))
6967
6968 ;;;
6969 ;;; Article editing
6970 ;;;
6971
6972 (defcustom gnus-article-edit-mode-hook nil
6973   "Hook run in article edit mode buffers."
6974   :group 'gnus-article-various
6975   :type 'hook)
6976
6977 (defvar gnus-article-edit-done-function nil)
6978
6979 (defvar gnus-article-edit-mode-map nil)
6980
6981 ;; Should we be using derived.el for this?
6982 (unless gnus-article-edit-mode-map
6983   (setq gnus-article-edit-mode-map (make-keymap))
6984   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6985
6986   (gnus-define-keys gnus-article-edit-mode-map
6987     "\C-c?"    describe-mode
6988     "\C-c\C-c" gnus-article-edit-done
6989     "\C-c\C-k" gnus-article-edit-exit
6990     "\C-c\C-f\C-t" message-goto-to
6991     "\C-c\C-f\C-o" message-goto-from
6992     "\C-c\C-f\C-b" message-goto-bcc
6993     ;;"\C-c\C-f\C-w" message-goto-fcc
6994     "\C-c\C-f\C-c" message-goto-cc
6995     "\C-c\C-f\C-s" message-goto-subject
6996     "\C-c\C-f\C-r" message-goto-reply-to
6997     "\C-c\C-f\C-n" message-goto-newsgroups
6998     "\C-c\C-f\C-d" message-goto-distribution
6999     "\C-c\C-f\C-f" message-goto-followup-to
7000     "\C-c\C-f\C-m" message-goto-mail-followup-to
7001     "\C-c\C-f\C-k" message-goto-keywords
7002     "\C-c\C-f\C-u" message-goto-summary
7003     "\C-c\C-f\C-i" message-insert-or-toggle-importance
7004     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
7005     "\C-c\C-b" message-goto-body
7006     "\C-c\C-i" message-goto-signature
7007
7008     "\C-c\C-t" message-insert-to
7009     "\C-c\C-n" message-insert-newsgroups
7010     "\C-c\C-o" message-sort-headers
7011     "\C-c\C-e" message-elide-region
7012     "\C-c\C-v" message-delete-not-region
7013     "\C-c\C-z" message-kill-to-signature
7014     "\M-\r" message-newline-and-reformat
7015     "\C-c\C-a" mml-attach-file
7016     "\C-a" message-beginning-of-line
7017     "\t" message-tab
7018     "\M-;" comment-region)
7019
7020   (gnus-define-keys (gnus-article-edit-wash-map
7021                      "\C-c\C-w" gnus-article-edit-mode-map)
7022     "f" gnus-article-edit-full-stops))
7023
7024 (easy-menu-define
7025   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7026   '("Field"
7027     ["Fetch To" message-insert-to t]
7028     ["Fetch Newsgroups" message-insert-newsgroups t]
7029     "----"
7030     ["To" message-goto-to t]
7031     ["From" message-goto-from t]
7032     ["Subject" message-goto-subject t]
7033     ["Cc" message-goto-cc t]
7034     ["Reply-To" message-goto-reply-to t]
7035     ["Summary" message-goto-summary t]
7036     ["Keywords" message-goto-keywords t]
7037     ["Newsgroups" message-goto-newsgroups t]
7038     ["Followup-To" message-goto-followup-to t]
7039     ["Mail-Followup-To" message-goto-mail-followup-to t]
7040     ["Distribution" message-goto-distribution t]
7041     ["Body" message-goto-body t]
7042     ["Signature" message-goto-signature t]))
7043
7044 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7045   "Major mode for editing articles.
7046 This is an extended text-mode.
7047
7048 \\{gnus-article-edit-mode-map}"
7049   (make-local-variable 'gnus-article-edit-done-function)
7050   (make-local-variable 'gnus-prev-winconf)
7051   (set (make-local-variable 'font-lock-defaults)
7052        '(message-font-lock-keywords t))
7053   (set (make-local-variable 'mail-header-separator) "")
7054   (set (make-local-variable 'gnus-article-edit-mode) t)
7055   (easy-menu-add message-mode-field-menu message-mode-map)
7056   (mml-mode)
7057   (setq buffer-read-only nil)
7058   (buffer-enable-undo)
7059   (widen))
7060
7061 (defun gnus-article-edit (&optional force)
7062   "Edit the current article.
7063 This will have permanent effect only in mail groups.
7064 If FORCE is non-nil, allow editing of articles even in read-only
7065 groups."
7066   (interactive "P")
7067   (when (and (not force)
7068              (gnus-group-read-only-p))
7069     (error "The current newsgroup does not support article editing"))
7070   (gnus-article-date-original)
7071   (gnus-article-edit-article
7072    'ignore
7073    `(lambda (no-highlight)
7074       'ignore
7075       (gnus-summary-edit-article-done
7076        ,(or (mail-header-references gnus-current-headers) "")
7077        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7078
7079 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7080   "Start editing the contents of the current article buffer."
7081   (let ((winconf (current-window-configuration)))
7082     (set-buffer gnus-article-buffer)
7083     (let ((message-auto-save-directory
7084            ;; Don't associate the article buffer with a draft file.
7085            nil))
7086       (gnus-article-edit-mode))
7087     (funcall start-func)
7088     (set-buffer-modified-p nil)
7089     (gnus-configure-windows 'edit-article)
7090     (setq gnus-article-edit-done-function exit-func)
7091     (setq gnus-prev-winconf winconf)
7092     (unless quiet
7093       (gnus-message 6 "C-c C-c to end edits"))))
7094
7095 (defun gnus-article-edit-done (&optional arg)
7096   "Update the article edits and exit."
7097   (interactive "P")
7098   (let ((func gnus-article-edit-done-function)
7099         (buf (current-buffer))
7100         (start (window-start))
7101         (p (point))
7102         (winconf gnus-prev-winconf))
7103     (widen) ;; Widen it in case that users narrowed the buffer.
7104     (funcall func arg)
7105     (set-buffer buf)
7106     ;; The cache and backlog have to be flushed somewhat.
7107     (when gnus-keep-backlog
7108       (gnus-backlog-remove-article
7109        (car gnus-article-current) (cdr gnus-article-current)))
7110     ;; Flush original article as well.
7111     (gnus-flush-original-article-buffer)
7112     (when gnus-use-cache
7113       (gnus-cache-update-article
7114        (car gnus-article-current) (cdr gnus-article-current)))
7115     ;; We remove all text props from the article buffer.
7116     (kill-all-local-variables)
7117     (set-text-properties (point-min) (point-max) nil)
7118     (gnus-article-mode)
7119     (set-window-configuration winconf)
7120     (set-buffer buf)
7121     (set-window-start (get-buffer-window buf) start)
7122     (set-window-point (get-buffer-window buf) (point)))
7123   (gnus-summary-show-article))
7124
7125 (defun gnus-flush-original-article-buffer ()
7126   (when (get-buffer gnus-original-article-buffer)
7127     (with-current-buffer gnus-original-article-buffer
7128       (setq gnus-original-article nil))))
7129
7130 (defun gnus-article-edit-exit ()
7131   "Exit the article editing without updating."
7132   (interactive)
7133   (when (or (not (buffer-modified-p))
7134             (yes-or-no-p "Article modified; kill anyway? "))
7135     (let ((curbuf (current-buffer))
7136           (p (point))
7137           (window-start (window-start)))
7138       (erase-buffer)
7139       (if (gnus-buffer-live-p gnus-original-article-buffer)
7140           (insert-buffer-substring gnus-original-article-buffer))
7141       (let ((winconf gnus-prev-winconf))
7142         (kill-all-local-variables)
7143         (gnus-article-mode)
7144         (set-window-configuration winconf)
7145         ;; Tippy-toe some to make sure that point remains where it was.
7146         (save-current-buffer
7147           (set-buffer curbuf)
7148           (set-window-start (get-buffer-window (current-buffer)) window-start)
7149           (goto-char p))))
7150     (gnus-summary-show-article)))
7151
7152 (defun gnus-article-edit-full-stops ()
7153   "Interactively repair spacing at end of sentences."
7154   (interactive)
7155   (save-excursion
7156     (goto-char (point-min))
7157     (search-forward-regexp "^$" nil t)
7158     (let ((case-fold-search nil))
7159       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7160
7161 ;;;
7162 ;;; Article highlights
7163 ;;;
7164
7165 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7166
7167 ;;; Internal Variables:
7168
7169 (defcustom gnus-button-url-regexp
7170   (concat
7171    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7172    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7173    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7174    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7175        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7176              (punct "!?:;.,"))
7177          (concat
7178           "\\(?:"
7179           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7180           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7181           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7182           "\\|"
7183           "[" chars punct     "]+" "[" chars "]"
7184           "\\)"))
7185      (concat ;; XEmacs 21.4 doesn't support POSIX.
7186       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7187       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7188    "\\)")
7189   "Regular expression that matches URLs."
7190   :group 'gnus-article-buttons
7191   :type 'regexp)
7192
7193 (defcustom gnus-button-valid-fqdn-regexp
7194   message-valid-fqdn-regexp
7195   "Regular expression that matches a valid FQDN."
7196   :version "22.1"
7197   :group 'gnus-article-buttons
7198   :type 'regexp)
7199
7200 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7201 (defcustom gnus-button-valid-localpart-regexp
7202   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7203   "Regular expression that matches a localpart of mail addresses or MIDs."
7204   :version "22.1"
7205   :group 'gnus-article-buttons
7206   :type 'regexp)
7207
7208 (defcustom gnus-button-man-handler 'manual-entry
7209   "Function to use for displaying man pages.
7210 The function must take at least one argument with a string naming the
7211 man page."
7212   :version "22.1"
7213   :type '(choice (function-item :tag "Man" manual-entry)
7214                  (function-item :tag "Woman" woman)
7215                  (function :tag "Other"))
7216   :group 'gnus-article-buttons)
7217
7218 (defcustom gnus-button-mid-or-mail-regexp
7219   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7220           gnus-button-valid-fqdn-regexp
7221           ">?\\)\\b")
7222   "Regular expression that matches a message ID or a mail address."
7223   :version "22.1"
7224   :group 'gnus-article-buttons
7225   :type 'regexp)
7226
7227 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7228   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7229 Strings like this can be either a message ID or a mail address.  If it is one
7230 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7231 message ID or a mail address, respectively.  If this variable is set to the
7232 symbol `ask', always query the user what do do.  If it is a function, this
7233 function will be called with the string as its only argument.  The function
7234 must return `mid', `mail', `invalid' or `ask'."
7235   :version "22.1"
7236   :group 'gnus-article-buttons
7237   :type '(choice (function-item :tag "Heuristic function"
7238                                 gnus-button-mid-or-mail-heuristic)
7239                  (const ask)
7240                  (const mid)
7241                  (const mail)))
7242
7243 (defcustom gnus-button-mid-or-mail-heuristic-alist
7244   '((-10.0 . ".+\\$.+@")
7245     (-10.0 . "#")
7246     (-10.0 . "\\*")
7247     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7248     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7249     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7250     (-1.0  . "^[^a-z]+@")
7251     ;;
7252     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7253     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7254     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7255     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7256     ;;
7257     (-2.0  . "^[0-9]")
7258     (-1.0  . "^[0-9][0-9]")
7259     ;;
7260     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7261     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7262     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7263     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7264     ;;
7265     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7266     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7267     ;;       "[0-9]{8,}.*\@"
7268     (-3.0
7269      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7270     ;; "[0-9]{12,}.*\@"
7271     ;; compensation for TDMA dated mail addresses:
7272     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7273     ;;
7274     (-20.0 . "\\.fsf@") ;; Gnus
7275     (-20.0 . "^slrn")
7276     (-20.0 . "^Pine")
7277     (-20.0 . "^alpine\\.")
7278     (-20.0 . "_-_") ;; Subject change in thread
7279     ;;
7280     (-20.0 . "\\.ln@") ;; leafnode
7281     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7282     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7283     ;;
7284     ;; (5.0 . "") ;; $local_part_len <= 7
7285     (10.0  . "^[^0-9]+@")
7286     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7287     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7288     (3.0   . "\@stud")
7289     ;;
7290     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7291     ;;
7292     (0.5   . "^[A-Z][a-z]")
7293     (0.5   . "^[A-Z][a-z][a-z]")
7294     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7295     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7296   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7297
7298 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7299 address.  The REGEXP is processed with `case-fold-search' set to nil."
7300   :version "22.1"
7301   :group 'gnus-article-buttons
7302   :type '(repeat (cons (number :tag "Rate")
7303                        (regexp :tag "Regexp"))))
7304
7305 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7306   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7307 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7308 address, `ask' if unsure and `invalid' if the string is invalid."
7309   (let ((case-fold-search nil)
7310         (list gnus-button-mid-or-mail-heuristic-alist)
7311         (result 0) rate regexp lpartlen elem)
7312     (setq lpartlen
7313           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7314     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7315     ;; Certain special cases...
7316     (when (string-match
7317            (concat
7318             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7319             "^[0-9]+\\.[0-9]+@compuserve\\|"
7320             "@public\\.gmane\\.org")
7321            mid-or-mail)
7322       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7323       (setq result 'mail))
7324     (when (string-match "@.*@\\| " mid-or-mail)
7325       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7326       (setq result 'invalid))
7327     ;; Nothing more to do, if result is not a number here...
7328     (when (numberp result)
7329       (while list
7330         (setq elem (car list)
7331               rate (car elem)
7332               regexp (cdr elem)
7333               list (cdr list))
7334         (when (string-match regexp mid-or-mail)
7335           (setq result (+ result rate))
7336           (gnus-message
7337            9 "`%s' matched `%s', rate `%s', result `%s'."
7338            mid-or-mail regexp rate result)))
7339       (when (<= lpartlen 7)
7340         (setq result (+ result 5.0))
7341         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7342                       mid-or-mail result))
7343       (when (>= lpartlen 12)
7344         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7345         (cond
7346          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7347           ;; Long local part should contain realname if e-mail address,
7348           ;; too many digits: message-id.
7349           ;; $score -= 5.0 + 0.1 * $local_part_len;
7350           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7351           (setq result (+ result rate))
7352           (gnus-message
7353            9 "Many digits in `%s', rate `%s', result `%s'."
7354            mid-or-mail rate result))
7355          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7356                         mid-or-mail)
7357           ;; Too few vowels [^aeiouy]{4,}.*\@
7358           (setq result (+ result -5.0))
7359           (gnus-message
7360            9 "Few vowels in `%s', rate `%s', result `%s'."
7361            mid-or-mail -5.0 result))
7362          (t
7363           (setq result (+ result 5.0))
7364           (gnus-message
7365            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7366     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7367     ;; Maybe we should make this a customizable alist: (condition . 'result)
7368     (cond
7369      ((symbolp result) result)
7370      ;; Now convert number into proper results:
7371      ((< result -10.0) 'mid)
7372      ((> result  10.0) 'mail)
7373      (t 'ask))))
7374
7375 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7376   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7377          (url-mid (concat "news" ":" mid-or-mail))
7378          (url-mailto (concat "mailto" ":" mid-or-mail)))
7379     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7380     (when (fboundp pref)
7381       (setq guessed
7382             ;; get rid of surrounding angles...
7383             (funcall pref
7384                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7385       (if (or (eq 'mid guessed) (eq 'mail guessed))
7386           (setq pref guessed)
7387         (setq pref 'ask)))
7388     (if (eq pref 'ask)
7389         (save-window-excursion
7390           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7391               (setq pref 'mail)
7392             (setq pref 'mid))))
7393     (cond ((eq pref 'mid)
7394            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7395            (gnus-button-handle-news url-mid))
7396           ((eq pref 'mail)
7397            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7398            (gnus-url-mailto url-mailto))
7399           (t (gnus-message 3 "Invalid string.")))))
7400
7401 (defun gnus-button-handle-custom (fun arg)
7402   "Call function FUN on argument ARG.
7403 Both FUN and ARG are supposed to be strings.  ARG will be passed
7404 as a symbol to FUN."
7405   (funcall (intern fun)
7406            (if (string-match "^customize-apropos" fun)
7407                arg
7408              (intern arg))))
7409
7410 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7411
7412 (defun gnus-button-handle-describe-function (url)
7413   "Call `describe-function' when pushing the corresponding URL button."
7414   (describe-function
7415    (intern
7416     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7417
7418 (defun gnus-button-handle-describe-variable (url)
7419   "Call `describe-variable' when pushing the corresponding URL button."
7420   (describe-variable
7421    (intern
7422     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7423
7424 (defun gnus-button-handle-symbol (url)
7425 "Display help on variable or function.
7426 Calls `describe-variable' or `describe-function'."
7427   (let ((sym (intern url)))
7428     (cond
7429      ((fboundp sym) (describe-function sym))
7430      ((boundp sym) (describe-variable sym))
7431      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7432
7433 (defun gnus-button-handle-describe-key (url)
7434   "Call `describe-key' when pushing the corresponding URL button."
7435   (let* ((key-string
7436           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7437          (keys (ignore-errors (eval `(kbd ,key-string)))))
7438     (if keys
7439         (describe-key keys)
7440       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7441
7442 (defun gnus-button-handle-apropos (url)
7443   "Call `apropos' when pushing the corresponding URL button."
7444   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7445
7446 (defun gnus-button-handle-apropos-command (url)
7447   "Call `apropos' when pushing the corresponding URL button."
7448   (apropos-command
7449    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7450
7451 (defun gnus-button-handle-apropos-variable (url)
7452   "Call `apropos' when pushing the corresponding URL button."
7453   (funcall
7454    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7455    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7456
7457 (defun gnus-button-handle-apropos-documentation (url)
7458   "Call `apropos' when pushing the corresponding URL button."
7459   (funcall
7460    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7461    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7462
7463 (defun gnus-button-handle-library (url)
7464   "Call `locate-library' when pushing the corresponding URL button."
7465   (gnus-message 9 "url=`%s'" url)
7466   (let* ((lib (locate-library url))
7467          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7468     (if (not lib)
7469         (gnus-message 1 "Cannot locale library `%s'." url)
7470       (find-file-read-only file))))
7471
7472 (defcustom gnus-button-man-level 5
7473   "*Integer that says how many man-related buttons Gnus will show.
7474 The higher the number, the more buttons will appear and the more false
7475 positives are possible.  Note that you can set this variable local to
7476 specific groups.  Setting it higher in Unix groups is probably a good idea.
7477 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7478 how to set variables in specific groups."
7479   :version "22.1"
7480   :group 'gnus-article-buttons
7481   :link '(custom-manual "(gnus)Group Parameters")
7482   :type 'integer)
7483
7484 (defcustom gnus-button-emacs-level 5
7485   "*Integer that says how many emacs-related buttons Gnus will show.
7486 The higher the number, the more buttons will appear and the more false
7487 positives are possible.  Note that you can set this variable local to
7488 specific groups.  Setting it higher in Emacs or Gnus related groups is
7489 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7490 `gnus-parameters' on how to set variables in specific groups."
7491   :version "22.1"
7492   :group 'gnus-article-buttons
7493   :link '(custom-manual "(gnus)Group Parameters")
7494   :type 'integer)
7495
7496 (defcustom gnus-button-message-level 5
7497   "*Integer that says how many buttons for news or mail messages will appear.
7498 The higher the number, the more buttons will appear and the more false
7499 positives are possible."
7500   ;; mail addresses, MIDs, URLs for news, ...
7501   :version "22.1"
7502   :group 'gnus-article-buttons
7503   :type 'integer)
7504
7505 (defcustom gnus-button-browse-level 5
7506   "*Integer that says how many buttons for browsing will appear.
7507 The higher the number, the more buttons will appear and the more false
7508 positives are possible."
7509   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7510   :version "22.1"
7511   :group 'gnus-article-buttons
7512   :type 'integer)
7513
7514 (defcustom gnus-button-alist
7515   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7516      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7517     ((concat "\\b\\(nntp\\|news\\):\\("
7518              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7519      0 t gnus-button-handle-news 2)
7520     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7521      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7522     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7523      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7524     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7525     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7526      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7527     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7528      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7529     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7530      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7531     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7532      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7533     ;; RFC 2368 (The mailto URL scheme)
7534     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7535      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7536     ("\\bmailto:\\([^ \n\t]+\\)"
7537      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7538     ;; Info Konqueror style <info:/foo/bar baz>.
7539     ;; Must come before " Gnus home-grown style".
7540     ("\\binfo://?\\([^'\">\n\t]+\\)"
7541      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7542    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7543     ("\\binfo://\\([^'\">\n\t ]+\\)"
7544      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7545     ;; Info GNOME style <info:foo#bar_baz>
7546     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7547      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7548     ;; Info KDE style <info:(foo)bar baz>
7549     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7550      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7551     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7552      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7553     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n,]*\\)\\)?"
7554      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7555      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7556     ;; This is custom
7557     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7558      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7559     ;; Emacs help commands
7560     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7561      ;; regexp doesn't match arguments containing ` '.
7562      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7563     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7564      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7565     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7566      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7567     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7568      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7569     ;; The following entries may lead to many false positives so don't enable
7570     ;; them by default (use a high button level).
7571     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7572      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7573      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7574     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7575      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7576     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7577      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7578     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7579      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7580     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7581      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7582     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7583      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7584     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7585      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7586     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7587      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7588     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7589      ;; Unlike the other regexps we really have to require quoting
7590      ;; here to determine where it ends.
7591      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7592     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7593     ("<URL: *\\([^\n<>]*\\)>"
7594      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7595     ;; RFC 2396 (2.4.3., delims) ...
7596     ("\"URL: *\\([^\n\"]*\\)\""
7597      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7598     ;; Raw URLs.
7599     (gnus-button-url-regexp
7600      0 (>= gnus-button-browse-level 0) browse-url 0)
7601     ;; man pages
7602     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7603      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7604      gnus-button-handle-man 1)
7605     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7606     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7607      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7608      gnus-button-handle-man 1)
7609     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7610     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7611     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7612      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7613     ;; Recognizing patches to .el files.  This is somewhat obscure,
7614     ;; but considering the percentage of Gnus users who hack Emacs
7615     ;; Lisp files...
7616     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7617      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7618     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7619      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7620     ;; MID or mail: To avoid too many false positives we don't try to catch
7621     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7622     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7623     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7624     ;; so that non-ambiguous entries (see above) match first.
7625     (gnus-button-mid-or-mail-regexp
7626      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7627   "*Alist of regexps matching buttons in article bodies.
7628
7629 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7630 REGEXP: is the string (case insensitive) matching text around the button (can
7631 also be Lisp expression evaluating to a string),
7632 BUTTON: is the number of the regexp grouping actually matching the button,
7633 FORM: is a Lisp expression which must eval to true for the button to
7634 be added,
7635 CALLBACK: is the function to call when the user push this button, and each
7636 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7637
7638 CALLBACK can also be a variable, in that case the value of that
7639 variable it the real callback function."
7640   :group 'gnus-article-buttons
7641   :type '(repeat (list (choice regexp variable sexp)
7642                        (integer :tag "Button")
7643                        (sexp :tag "Form")
7644                        (function :tag "Callback")
7645                        (repeat :tag "Par"
7646                                :inline t
7647                                (integer :tag "Regexp group")))))
7648 (put 'gnus-button-alist 'risky-local-variable t)
7649
7650 (defcustom gnus-header-button-alist
7651   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7652      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7653     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7654      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7655     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7656      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7657     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7658      0 (>= gnus-button-browse-level 0) browse-url 0)
7659     ("^Subject:" gnus-button-url-regexp
7660      0 (>= gnus-button-browse-level 0) browse-url 0)
7661     ("^[^:]+:" gnus-button-url-regexp
7662      0 (>= gnus-button-browse-level 0) browse-url 0)
7663     ("^OpenPGP:.*url=" gnus-button-url-regexp
7664      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7665     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7666      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7667     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7668      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7669   "*Alist of headers and regexps to match buttons in article heads.
7670
7671 This alist is very similar to `gnus-button-alist', except that each
7672 alist has an additional HEADER element first in each entry:
7673
7674 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7675
7676 HEADER is a regexp to match a header.  For a fuller explanation, see
7677 `gnus-button-alist'."
7678   :group 'gnus-article-buttons
7679   :group 'gnus-article-headers
7680   :type '(repeat (list (regexp :tag "Header")
7681                        (choice regexp variable)
7682                        (integer :tag "Button")
7683                        (sexp :tag "Form")
7684                        (function :tag "Callback")
7685                        (repeat :tag "Par"
7686                                :inline t
7687                                (integer :tag "Regexp group")))))
7688 (put 'gnus-header-button-alist 'risky-local-variable t)
7689
7690 ;;; Commands:
7691
7692 (defun gnus-article-push-button (event)
7693   "Check text under the mouse pointer for a callback function.
7694 If the text under the mouse pointer has a `gnus-callback' property,
7695 call it with the value of the `gnus-data' text property."
7696   (interactive "e")
7697   (set-buffer (window-buffer (posn-window (event-start event))))
7698   (let* ((pos (posn-point (event-start event)))
7699          (data (get-text-property pos 'gnus-data))
7700          (fun (get-text-property pos 'gnus-callback)))
7701     (goto-char pos)
7702     (when fun
7703       (funcall fun data))))
7704
7705 (defun gnus-article-press-button ()
7706   "Check text at point for a callback function.
7707 If the text at point has a `gnus-callback' property,
7708 call it with the value of the `gnus-data' text property."
7709   (interactive)
7710   (let ((data (get-text-property (point) 'gnus-data))
7711         (fun (get-text-property (point) 'gnus-callback)))
7712     (when fun
7713       (funcall fun data))))
7714
7715 (defun gnus-article-highlight (&optional force)
7716   "Highlight current article.
7717 This function calls `gnus-article-highlight-headers',
7718 `gnus-article-highlight-citation',
7719 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7720 do the highlighting.  See the documentation for those functions."
7721   (interactive (list 'force))
7722   (gnus-article-highlight-headers)
7723   (gnus-article-highlight-citation force)
7724   (gnus-article-highlight-signature)
7725   (gnus-article-add-buttons)
7726   (gnus-article-add-buttons-to-head))
7727
7728 (defun gnus-article-highlight-some (&optional force)
7729   "Highlight current article.
7730 This function calls `gnus-article-highlight-headers',
7731 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7732 do the highlighting.  See the documentation for those functions."
7733   (interactive (list 'force))
7734   (gnus-article-highlight-headers)
7735   (gnus-article-highlight-signature)
7736   (gnus-article-add-buttons))
7737
7738 (defun gnus-article-highlight-headers ()
7739   "Highlight article headers as specified by `gnus-header-face-alist'."
7740   (interactive)
7741   (gnus-with-article-headers
7742     (let (regexp header-face field-face from hpoints fpoints)
7743       (dolist (entry gnus-header-face-alist)
7744         (goto-char (point-min))
7745         (setq regexp (concat "^\\("
7746                              (if (string-equal "" (nth 0 entry))
7747                                  "[^\t ]"
7748                                (nth 0 entry))
7749                              "\\)")
7750               header-face (nth 1 entry)
7751               field-face (nth 2 entry))
7752         (while (and (re-search-forward regexp nil t)
7753                     (not (eobp)))
7754           (beginning-of-line)
7755           (setq from (point))
7756           (unless (search-forward ":" nil t)
7757             (forward-char 1))
7758           (when (and header-face
7759                      (not (memq (point) hpoints)))
7760             (push (point) hpoints)
7761             (gnus-put-text-property from (point) 'face header-face))
7762           (when (and field-face
7763                      (not (memq (setq from (point)) fpoints)))
7764             (push from fpoints)
7765             (if (re-search-forward "^[^ \t]" nil t)
7766                 (forward-char -2)
7767               (goto-char (point-max)))
7768             (gnus-put-text-property from (point) 'face field-face)))))))
7769
7770 (defun gnus-article-highlight-signature ()
7771   "Highlight the signature in an article.
7772 It does this by highlighting everything after
7773 `gnus-signature-separator' using the face `gnus-signature'."
7774   (interactive)
7775   (gnus-with-article-buffer
7776     (let ((inhibit-point-motion-hooks t))
7777       (save-restriction
7778         (when (and gnus-signature-face
7779                    (gnus-article-narrow-to-signature))
7780           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7781                             'face gnus-signature-face)
7782           (widen)
7783           (gnus-article-search-signature)
7784           (let ((start (match-beginning 0))
7785                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7786             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7787                                      end)))))))
7788
7789 (defun gnus-button-in-region-p (b e prop)
7790   "Say whether PROP exists in the region."
7791   (text-property-not-all b e prop nil))
7792
7793 (defun gnus-article-add-buttons ()
7794   "Find external references in the article and make buttons of them.
7795 \"External references\" are things like Message-IDs and URLs, as
7796 specified by `gnus-button-alist'."
7797   (interactive)
7798   (gnus-with-article-buffer
7799     (let ((inhibit-point-motion-hooks t)
7800           (case-fold-search t)
7801           (alist gnus-button-alist)
7802           beg entry regexp)
7803       ;; We skip the headers.
7804       (article-goto-body)
7805       (setq beg (point))
7806       (while (setq entry (pop alist))
7807         (setq regexp (eval (car entry)))
7808         (goto-char beg)
7809         (while (re-search-forward regexp nil t)
7810           (let ((start (match-beginning (nth 1 entry)))
7811                 (end (match-end (nth 1 entry)))
7812                 (from (match-beginning 0)))
7813             (when (and (eval (nth 2 entry))
7814                        (not (gnus-button-in-region-p
7815                              start end 'gnus-callback)))
7816               ;; That optional form returned non-nil, so we add the
7817               ;; button.
7818               (setq from (set-marker (make-marker) from))
7819               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7820                            (gnus-article-extend-url-button from start end))
7821                 (gnus-article-add-button start end
7822                                          'gnus-button-push (list from entry))
7823                 (gnus-put-text-property
7824                  start end
7825                  'gnus-string (buffer-substring-no-properties
7826                                start end))))))))))
7827
7828 (defun gnus-article-extend-url-button (beg start end)
7829   "Extend url button if url is folded into two or more lines.
7830 Return non-nil if button is extended.  BEG is a marker that points to
7831 the beginning position of a text containing url.  START and END are
7832 the endpoints of a url button before it is extended.  The concatenated
7833 url is put as the `gnus-button-url' overlay property on the button."
7834   (let ((opoint (point))
7835         (points (list start end))
7836         url delim regexp)
7837     (prog1
7838         (when (and (progn
7839                      (goto-char end)
7840                      (not (looking-at "[\t ]*[\">]")))
7841                    (progn
7842                      (goto-char start)
7843                      (string-match
7844                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7845                       (buffer-substring (point-at-bol) start)))
7846                    (progn
7847                      (setq url (list (buffer-substring start end))
7848                            delim (if (match-beginning 1) ">" "\""))
7849                      (beginning-of-line)
7850                      (setq regexp (concat
7851                                    (when (and (looking-at
7852                                                message-cite-prefix-regexp)
7853                                               (< (match-end 0) start))
7854                                      (regexp-quote (match-string 0)))
7855                                    "\
7856 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7857                                    delim "\\)"))
7858                      (while (progn
7859                               (forward-line 1)
7860                               (and (looking-at regexp)
7861                                    (prog1
7862                                        (match-beginning 1)
7863                                      (push (or (match-string 2)
7864                                                (match-string 1))
7865                                            url)
7866                                      (push (setq end (or (match-end 2)
7867                                                          (match-end 1)))
7868                                            points)
7869                                      (push (or (match-beginning 2)
7870                                                (match-beginning 1))
7871                                            points)))))
7872                      (match-beginning 2)))
7873           (let (gnus-article-mouse-face widget-mouse-face)
7874             (while points
7875               (gnus-article-add-button (pop points) (pop points)
7876                                        'gnus-button-push
7877                                        (list beg (assq 'gnus-button-url-regexp
7878                                                        gnus-button-alist)))))
7879           (let ((overlay (gnus-make-overlay start end)))
7880             (gnus-overlay-put overlay 'evaporate t)
7881             (gnus-overlay-put overlay 'gnus-button-url
7882                               (list (mapconcat 'identity (nreverse url) "")))
7883             (when gnus-article-mouse-face
7884               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7885           t)
7886       (goto-char opoint))))
7887
7888 ;; Add buttons to the head of an article.
7889 (defun gnus-article-add-buttons-to-head ()
7890   "Add buttons to the head of the article."
7891   (interactive)
7892   (gnus-with-article-headers
7893     (let (beg end)
7894       (dolist (entry gnus-header-button-alist)
7895         ;; Each alist entry.
7896         (goto-char (point-min))
7897         (while (re-search-forward (car entry) nil t)
7898           ;; Each header matching the entry.
7899           (setq beg (match-beginning 0))
7900           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7901                              (match-beginning 0))
7902                         (point-max)))
7903           (goto-char beg)
7904           (while (re-search-forward (eval (nth 1 entry)) end t)
7905             ;; Each match within a header.
7906             (let* ((entry (cdr entry))
7907                    (start (match-beginning (nth 1 entry)))
7908                    (end (match-end (nth 1 entry)))
7909                    (form (nth 2 entry)))
7910               (goto-char (match-end 0))
7911               (when (eval form)
7912                 (gnus-article-add-button
7913                  start end (nth 3 entry)
7914                  (buffer-substring (match-beginning (nth 4 entry))
7915                                    (match-end (nth 4 entry)))))))
7916           (goto-char end))))))
7917
7918 ;;; External functions:
7919
7920 (defun gnus-article-add-button (from to fun &optional data text)
7921   "Create a button between FROM and TO with callback FUN and data DATA."
7922   (when gnus-article-button-face
7923     (gnus-overlay-put (gnus-make-overlay from to nil t)
7924                       'face gnus-article-button-face))
7925   (gnus-add-text-properties
7926    from to
7927    (nconc (and gnus-article-mouse-face
7928                (list gnus-mouse-face-prop gnus-article-mouse-face))
7929           (list 'gnus-callback fun)
7930           (and data (list 'gnus-data data))))
7931   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7932                          :help-echo (or text "Follow the link")
7933                          :keymap gnus-url-button-map
7934                          :button-keymap gnus-widget-button-keymap))
7935
7936 (defun gnus-article-copy-string ()
7937   "Copy the string in the button to the kill ring."
7938   (interactive)
7939   (gnus-article-check-buffer)
7940   (let ((data (get-text-property (point) 'gnus-string)))
7941     (when data
7942       (with-temp-buffer
7943         (insert data)
7944         (copy-region-as-kill (point-min) (point-max))
7945         (message "Copied %s" data)))))
7946
7947 ;;; Internal functions:
7948
7949 (defun gnus-article-set-globals ()
7950   (with-current-buffer gnus-summary-buffer
7951     (gnus-set-global-variables)))
7952
7953 (defun gnus-signature-toggle (end)
7954   (gnus-with-article-buffer
7955     (let ((inhibit-point-motion-hooks t))
7956       (if (text-property-any end (point-max) 'article-type 'signature)
7957           (progn
7958             (gnus-delete-wash-type 'signature)
7959             (gnus-remove-text-properties-when
7960              'article-type 'signature end (point-max)
7961              (cons 'article-type (cons 'signature
7962                                        gnus-hidden-properties))))
7963         (gnus-add-wash-type 'signature)
7964         (gnus-add-text-properties-when
7965          'article-type nil end (point-max)
7966          (cons 'article-type (cons 'signature
7967                                    gnus-hidden-properties)))))
7968     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7969       (gnus-set-mode-line 'article))))
7970
7971 (defun gnus-button-push (marker-and-entry)
7972   ;; Push button starting at MARKER.
7973   (save-excursion
7974     (let* ((marker (car marker-and-entry))
7975            (entry (cadr marker-and-entry))
7976            (regexp (car entry))
7977            (inhibit-point-motion-hooks t))
7978       (goto-char marker)
7979       ;; This is obviously true, or something bad is happening :)
7980       ;; But we need it to have the match-data
7981       (when (looking-at (or (if (symbolp regexp)
7982                                 (symbol-value regexp)
7983                               regexp)))
7984         (let ((fun (nth 3 entry))
7985               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7986                              (get-char-property marker 'gnus-button-url))
7987                         (mapcar (lambda (group)
7988                                   (let ((string (match-string group)))
7989                                     (set-text-properties
7990                                      0 (length string) nil string)
7991                                     string))
7992                                 (nthcdr 4 entry)))))
7993
7994           (cond
7995            ((fboundp fun)
7996             (apply fun args))
7997            ((and (boundp fun)
7998                  (fboundp (symbol-value fun)))
7999             (apply (symbol-value fun) args))
8000            (t
8001             (gnus-message 1 "You must define `%S' to use this button"
8002                           (cons fun args)))))))))
8003
8004 (defun gnus-parse-news-url (url)
8005   (let (scheme server port group message-id articles)
8006     (with-temp-buffer
8007       (insert url)
8008       (goto-char (point-min))
8009       (when (looking-at "\\([A-Za-z]+\\):")
8010         (setq scheme (match-string 1))
8011         (goto-char (match-end 0)))
8012       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8013         (setq server (match-string 1))
8014         (setq port (if (stringp (match-string 3))
8015                        (string-to-number (match-string 3))
8016                      (match-string 3)))
8017         (goto-char (match-end 0)))
8018
8019       (cond
8020        ((looking-at "\\(.*@.*\\)")
8021         (setq message-id (match-string 1)))
8022        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8023         (setq group (match-string 1)
8024               articles (split-string (match-string 2) "-")))
8025        ((looking-at "\\([^/]+\\)/?")
8026         (setq group (match-string 1)))
8027        (t
8028         (error "Unknown news URL syntax"))))
8029     (list scheme server port group message-id articles)))
8030
8031 (defun gnus-button-handle-news (url)
8032   "Fetch a news URL."
8033   (destructuring-bind (scheme server port group message-id articles)
8034       (gnus-parse-news-url url)
8035     (cond
8036      (message-id
8037       (with-current-buffer gnus-summary-buffer
8038         (if server
8039             (let ((gnus-refer-article-method
8040                    (nconc (list (list 'nntp server))
8041                           gnus-refer-article-method))
8042                   (nntp-port-number (or port "nntp")))
8043               (gnus-message 7 "Fetching %s with %s"
8044                             message-id gnus-refer-article-method)
8045               (gnus-summary-refer-article message-id))
8046           (gnus-summary-refer-article message-id))))
8047      (group
8048       (gnus-button-fetch-group url)))))
8049
8050 (defun gnus-button-patch (library line)
8051   "Visit an Emacs Lisp library LIBRARY on line LINE."
8052   (interactive)
8053   (let ((file (locate-library (file-name-nondirectory library))))
8054     (unless file
8055       (error "Couldn't find library %s" library))
8056     (find-file file)
8057     (goto-char (point-min))
8058     (forward-line (1- (string-to-number line)))))
8059
8060 (defun gnus-button-handle-man (url)
8061   "Fetch a man page."
8062   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8063   (when (eq gnus-button-man-handler 'woman)
8064     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8065   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8066   (funcall gnus-button-man-handler url))
8067
8068 (defun gnus-button-handle-info-url (url)
8069   "Fetch an info URL."
8070   (setq url (mm-subst-char-in-string ?+ ?\  url))
8071   (cond
8072    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8073     (gnus-info-find-node
8074      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8075                      "Gnus")
8076              ")" (gnus-url-unhex-string (match-string 2 url)))))
8077    ((string-match "([^)\"]+)[^\"]+" url)
8078     (setq url
8079           (gnus-replace-in-string
8080            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8081     (gnus-info-find-node url))
8082    (t (error "Can't parse %s" url))))
8083
8084 (defun gnus-button-handle-info-url-gnome (url)
8085   "Fetch GNOME style info URL."
8086   (setq url (mm-subst-char-in-string ?_ ?\  url))
8087   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8088       (gnus-info-find-node
8089        (concat "("
8090                (gnus-url-unhex-string
8091                  (match-string 1 url))
8092                ")"
8093                (or (gnus-url-unhex-string
8094                     (match-string 2 url))
8095                    "Top")))
8096     (error "Can't parse %s" url)))
8097
8098 (defun gnus-button-handle-info-url-kde (url)
8099   "Fetch KDE style info URL."
8100   (gnus-info-find-node (gnus-url-unhex-string url)))
8101
8102 ;; (info) will autoload info.el
8103 (declare-function Info-menu "info" (menu-item &optional fork))
8104 (declare-function Info-index-next "info" (num))
8105
8106 (defun gnus-button-handle-info-keystrokes (url)
8107   "Call `info' when pushing the corresponding URL button."
8108   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8109   (let (node indx comma)
8110     (if (string-match
8111          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8112                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8113                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8114                  "\\(?:[ \t\n,]*\\)\\)?")
8115          url)
8116         (setq node (match-string 2 url)
8117               indx (match-string 3 url))
8118       (error "Can't parse %s" url))
8119     (info)
8120     (Info-directory)
8121     (Info-menu node)
8122     (when (> (length indx) 0)
8123       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8124                             "\\([ \t\n,]*\\)")
8125                     indx)
8126       (setq comma (match-string 2 indx))
8127       (setq indx  (match-string 1 indx))
8128       (Info-index indx)
8129       (when comma
8130         (dotimes (i (with-temp-buffer
8131                       (insert comma)
8132                       ;; Note: the XEmacs version of `how-many' takes
8133                       ;; no optional argument.
8134                       (goto-char (point-min))
8135                       (how-many ",")))
8136           (Info-index-next 1)))
8137       nil)))
8138
8139 (autoload 'pgg-snarf-keys-region "pgg")
8140 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8141 (declare-function pgg-display-output-buffer "pgg" (start end status))
8142
8143 (defun gnus-button-openpgp (url)
8144   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8145   (with-temp-buffer
8146     (mm-url-insert-file-contents-external url)
8147     (pgg-snarf-keys-region (point-min) (point-max))
8148     (pgg-display-output-buffer nil nil nil)))
8149
8150 (defun gnus-button-message-id (message-id)
8151   "Fetch MESSAGE-ID."
8152   (with-current-buffer gnus-summary-buffer
8153     (gnus-summary-refer-article message-id)))
8154
8155 (defun gnus-button-fetch-group (address &rest ignore)
8156   "Fetch GROUP specified by ADDRESS."
8157   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8158                       address)
8159     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8160     ;; for nntp:// and news://
8161     (setq address (match-string 3 address)))
8162   (if (not (string-match "[:/]" address))
8163       ;; This is just a simple group url.
8164       (gnus-group-read-ephemeral-group address gnus-select-method)
8165     (if (not
8166          (string-match
8167           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8168           address))
8169         (error "Can't parse %s" address)
8170       (gnus-group-read-ephemeral-group
8171        (match-string 4 address)
8172        `(nntp ,(match-string 1 address)
8173               (nntp-address ,(match-string 1 address))
8174               (nntp-port-number ,(if (match-end 3)
8175                                      (match-string 3 address)
8176                                    "nntp")))
8177        nil nil nil
8178        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8179
8180 (defun gnus-url-parse-query-string (query &optional downcase)
8181   (let (retval pairs cur key val)
8182     (setq pairs (split-string query "&"))
8183     (while pairs
8184       (setq cur (car pairs)
8185             pairs (cdr pairs))
8186       (if (not (string-match "=" cur))
8187           nil                           ; Grace
8188         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8189               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8190         (if downcase
8191             (setq key (downcase key)))
8192         (setq cur (assoc key retval))
8193         (if cur
8194             (setcdr cur (cons val (cdr cur)))
8195           (setq retval (cons (list key val) retval)))))
8196     retval))
8197
8198 (defun gnus-url-mailto (url)
8199   ;; Send mail to someone
8200   (setq url (replace-regexp-in-string "\n" " " url))
8201   (when (string-match "mailto:/*\\(.*\\)" url)
8202     (setq url (substring url (match-beginning 1) nil)))
8203   (let (to args subject func)
8204     (setq args (gnus-url-parse-query-string
8205                 (if (string-match "^\\?" url)
8206                     (substring url 1)
8207                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8208                       (concat "to=" (match-string 1 url) "&"
8209                               (match-string 2 url))
8210                     (concat "to=" url))))
8211           subject (cdr-safe (assoc "subject" args)))
8212     (gnus-msg-mail)
8213     (while args
8214       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8215       (if (fboundp func)
8216           (funcall func)
8217         (message-position-on-field (caar args)))
8218       (insert (gnus-replace-in-string
8219                (mapconcat 'identity (reverse (cdar args)) ", ")
8220                "\r\n" "\n" t))
8221       (setq args (cdr args)))
8222     (if subject
8223         (message-goto-body)
8224       (message-goto-subject))))
8225
8226 (defun gnus-button-embedded-url (address)
8227   "Activate ADDRESS with `browse-url'."
8228   (browse-url (gnus-strip-whitespace address)))
8229
8230 ;;; Next/prev buttons in the article buffer.
8231
8232 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8233 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8234
8235 (defvar gnus-prev-page-map
8236   (let ((map (make-sparse-keymap)))
8237     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8238     (define-key map "\r" 'gnus-button-prev-page)
8239     map))
8240
8241 (defvar gnus-next-page-map
8242   (let ((map (make-sparse-keymap)))
8243     (define-key map gnus-mouse-2 'gnus-button-next-page)
8244     (define-key map "\r" 'gnus-button-next-page)
8245     map))
8246
8247 (defun gnus-insert-prev-page-button ()
8248   (let ((b (point)) e
8249         (inhibit-read-only t))
8250     (gnus-eval-format
8251      gnus-prev-page-line-format nil
8252      `(keymap ,gnus-prev-page-map
8253               gnus-prev t
8254               gnus-callback gnus-article-button-prev-page
8255               article-type annotation))
8256     (setq e (if (bolp)
8257                 ;; Exclude a newline.
8258                 (1- (point))
8259               (point)))
8260     (when gnus-article-button-face
8261       (gnus-overlay-put (gnus-make-overlay b e nil t)
8262                         'face gnus-article-button-face))
8263     (widget-convert-button
8264      'link b e
8265      :action 'gnus-button-prev-page
8266      :button-keymap gnus-prev-page-map)))
8267
8268 (defun gnus-button-next-page (&optional args more-args)
8269   "Go to the next page."
8270   (interactive)
8271   (let ((win (selected-window)))
8272     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8273     (gnus-article-next-page)
8274     (select-window win)))
8275
8276 (defun gnus-button-prev-page (&optional args more-args)
8277   "Go to the prev page."
8278   (interactive)
8279   (let ((win (selected-window)))
8280     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8281     (gnus-article-prev-page)
8282     (select-window win)))
8283
8284 (defun gnus-insert-next-page-button ()
8285   (let ((b (point)) e
8286         (inhibit-read-only t))
8287     (gnus-eval-format gnus-next-page-line-format nil
8288                       `(keymap ,gnus-next-page-map
8289                                gnus-next t
8290                                gnus-callback gnus-article-button-next-page
8291                                article-type annotation))
8292     (setq e (if (bolp)
8293                 ;; Exclude a newline.
8294                 (1- (point))
8295               (point)))
8296     (when gnus-article-button-face
8297       (gnus-overlay-put (gnus-make-overlay b e nil t)
8298                         'face gnus-article-button-face))
8299     (widget-convert-button
8300      'link b e
8301      :action 'gnus-button-next-page
8302      :button-keymap gnus-next-page-map)))
8303
8304 (defun gnus-article-button-next-page (arg)
8305   "Go to the next page."
8306   (interactive "P")
8307   (let ((win (selected-window)))
8308     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8309     (gnus-article-next-page)
8310     (select-window win)))
8311
8312 (defun gnus-article-button-prev-page (arg)
8313   "Go to the prev page."
8314   (interactive "P")
8315   (let ((win (selected-window)))
8316     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8317     (gnus-article-prev-page)
8318     (select-window win)))
8319
8320 (defvar gnus-decode-header-methods
8321   '(mail-decode-encoded-word-region)
8322   "List of methods used to decode headers.
8323
8324 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8325 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8326 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8327 whose names match REGEXP.
8328
8329 For example:
8330 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8331  mail-decode-encoded-word-region
8332  (\"chinese\" . rfc1843-decode-region))
8333 ")
8334
8335 (defvar gnus-decode-header-methods-cache nil)
8336
8337 (defun gnus-multi-decode-header (start end)
8338   "Apply the functions from `gnus-encoded-word-methods' that match."
8339   (unless (and gnus-decode-header-methods-cache
8340                (eq gnus-newsgroup-name
8341                    (car gnus-decode-header-methods-cache)))
8342     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8343     (dolist (x gnus-decode-header-methods)
8344       (if (symbolp x)
8345           (nconc gnus-decode-header-methods-cache (list x))
8346         (if (and gnus-newsgroup-name
8347                  (string-match (car x) gnus-newsgroup-name))
8348             (nconc gnus-decode-header-methods-cache
8349                    (list (cdr x)))))))
8350   (let ((xlist gnus-decode-header-methods-cache))
8351     (pop xlist)
8352     (save-restriction
8353       (narrow-to-region start end)
8354       (while xlist
8355         (funcall (pop xlist) (point-min) (point-max))))))
8356
8357 ;;;
8358 ;;; Treatment top-level handling.
8359 ;;;
8360
8361 (defvar gnus-inhibit-article-treatments nil)
8362
8363 (defun gnus-treat-article (gnus-treat-condition
8364                            &optional part-number total-parts gnus-treat-type)
8365   (let ((gnus-treat-length (- (point-max) (point-min)))
8366         (alist gnus-treatment-function-alist)
8367         (article-goto-body-goes-to-point-min-p t)
8368         (treated-type
8369          (or (not gnus-treat-type)
8370              (catch 'found
8371                (let ((list gnus-article-treat-types))
8372                  (while list
8373                    (when (string-match (pop list) gnus-treat-type)
8374                      (throw 'found t)))))))
8375         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8376         val elem)
8377     (gnus-run-hooks 'gnus-part-display-hook)
8378     (dolist (elem alist)
8379       (setq val
8380             (save-excursion
8381               (when (gnus-buffer-live-p gnus-summary-buffer)
8382                 (set-buffer gnus-summary-buffer))
8383               (symbol-value (car elem))))
8384       (when (and (or (consp val)
8385                      treated-type)
8386                  (or (not gnus-inhibit-article-treatments)
8387                      (eq gnus-treat-condition 'head))
8388                  (gnus-treat-predicate val)
8389                  (or (not (get (car elem) 'highlight))
8390                      highlightp))
8391         (save-restriction
8392           (funcall (cadr elem)))))))
8393
8394 ;; Dynamic variables.
8395 (defvar part-number)
8396 (defvar total-parts)
8397 (defvar gnus-treat-type)
8398 (defvar gnus-treat-condition)
8399 (defvar gnus-treat-length)
8400
8401 (defun gnus-treat-predicate (val)
8402   (cond
8403    ((null val)
8404     nil)
8405    (gnus-treat-condition
8406     (eq gnus-treat-condition val))
8407    ((and (listp val)
8408          (stringp (car val)))
8409     (apply 'gnus-or (mapcar `(lambda (s)
8410                                (string-match s ,(or gnus-newsgroup-name "")))
8411                             val)))
8412    ((listp val)
8413     (let ((pred (pop val)))
8414       (cond
8415        ((eq pred 'or)
8416         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8417        ((eq pred 'and)
8418         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8419        ((eq pred 'not)
8420         (not (gnus-treat-predicate (car val))))
8421        ((eq pred 'typep)
8422         (equal (car val) gnus-treat-type))
8423        (t
8424         (error "%S is not a valid predicate" pred)))))
8425    ((eq val t)
8426     t)
8427    ((eq val 'head)
8428     nil)
8429    ((eq val 'first)
8430     (eq part-number 1))
8431    ((eq val 'last)
8432     (eq part-number total-parts))
8433    ((numberp val)
8434     (< gnus-treat-length val))
8435    (t
8436     (error "%S is not a valid value" val))))
8437
8438 (defun gnus-article-encrypt-body (protocol &optional n)
8439   "Encrypt the article body."
8440   (interactive
8441    (list
8442     (or gnus-article-encrypt-protocol
8443         (gnus-completing-read "Encrypt protocol"
8444                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8445                               t))
8446     current-prefix-arg))
8447   ;; User might hit `K E' instead of `K e', so prompt once.
8448   (when (and gnus-article-encrypt-protocol
8449              gnus-novice-user)
8450     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8451       (error "Encrypt aborted")))
8452   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8453     (unless func
8454       (error "Can't find the encrypt protocol %s" protocol))
8455     (if (member gnus-newsgroup-name '("nndraft:delayed"
8456                                       "nndraft:drafts"
8457                                       "nndraft:queue"))
8458         (error "Can't encrypt the article in group %s"
8459                gnus-newsgroup-name))
8460     (gnus-summary-iterate n
8461       (with-current-buffer gnus-summary-buffer
8462         (let ((mail-parse-charset gnus-newsgroup-charset)
8463               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8464               (summary-buffer gnus-summary-buffer)
8465               references point)
8466           (gnus-set-global-variables)
8467           (when (gnus-group-read-only-p)
8468             (error "The current newsgroup does not support article encrypt"))
8469           (gnus-summary-show-article t)
8470           (setq references
8471               (or (mail-header-references gnus-current-headers) ""))
8472           (set-buffer gnus-article-buffer)
8473           (let* ((inhibit-read-only t)
8474                  (headers
8475                   (mapcar (lambda (field)
8476                             (and (save-restriction
8477                                    (message-narrow-to-head)
8478                                    (goto-char (point-min))
8479                                    (search-forward field nil t))
8480                                  (prog2
8481                                      (message-narrow-to-field)
8482                                      (buffer-string)
8483                                    (delete-region (point-min) (point-max))
8484                                    (widen))))
8485                           '("Content-Type:" "Content-Transfer-Encoding:"
8486                             "Content-Disposition:"))))
8487             (message-narrow-to-head)
8488             (message-remove-header "MIME-Version")
8489             (goto-char (point-max))
8490             (setq point (point))
8491             (insert (apply 'concat headers))
8492             (widen)
8493             (narrow-to-region point (point-max))
8494             (let ((message-options message-options))
8495               (message-options-set 'message-sender user-mail-address)
8496               (message-options-set 'message-recipients user-mail-address)
8497               (message-options-set 'message-sign-encrypt 'not)
8498               (funcall func))
8499             (goto-char (point-min))
8500             (insert "MIME-Version: 1.0\n")
8501             (widen)
8502             (gnus-summary-edit-article-done
8503              references nil summary-buffer t))
8504           (when gnus-keep-backlog
8505             (gnus-backlog-remove-article
8506              (car gnus-article-current) (cdr gnus-article-current)))
8507           (gnus-flush-original-article-buffer)
8508           (when gnus-use-cache
8509             (gnus-cache-update-article
8510              (car gnus-article-current) (cdr gnus-article-current))))))))
8511
8512 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8513   "The following specs can be used:
8514 %t  The security MIME type
8515 %i  Additional info
8516 %d  Details
8517 %D  Details if button is pressed")
8518
8519 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8520   "The following specs can be used:
8521 %t  The security MIME type
8522 %i  Additional info
8523 %d  Details
8524 %D  Details if button is pressed")
8525
8526 (defvar gnus-mime-security-button-line-format-alist
8527   '((?t gnus-tmp-type ?s)
8528     (?i gnus-tmp-info ?s)
8529     (?d gnus-tmp-details ?s)
8530     (?D gnus-tmp-pressed-details ?s)))
8531
8532 (defvar gnus-mime-security-button-commands
8533   '((gnus-article-press-button "\r" "Show Detail")
8534     (undefined "v")
8535     (undefined "t")
8536     (undefined "C")
8537     (gnus-mime-security-save-part "o" "Save...")
8538     (undefined "\C-o")
8539     (undefined "r")
8540     (undefined "d")
8541     (undefined "c")
8542     (undefined "i")
8543     (undefined "E")
8544     (undefined "e")
8545     (undefined "p")
8546     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8547     (undefined ".")))
8548
8549 (defvar gnus-mime-security-button-map
8550   (let ((map (make-sparse-keymap)))
8551     (define-key map gnus-mouse-2 'gnus-article-push-button)
8552     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8553     (dolist (c gnus-mime-security-button-commands)
8554       (define-key map (cadr c) (car c)))
8555     map))
8556
8557 (easy-menu-define
8558   gnus-mime-security-button-menu gnus-mime-security-button-map
8559   "Security button menu."
8560   `("Security Part"
8561     ,@(delq nil
8562             (mapcar (lambda (c)
8563                       (unless (eq (car c) 'undefined)
8564                         (vector (caddr c) (car c) :active t)))
8565                     gnus-mime-security-button-commands))))
8566
8567 (defun gnus-mime-security-button-menu (event prefix)
8568   "Construct a context-sensitive menu of security commands."
8569   (interactive "e\nP")
8570   (save-window-excursion
8571     (let ((pos (event-start event)))
8572       (select-window (posn-window pos))
8573       (goto-char (posn-point pos))
8574       (gnus-article-check-buffer)
8575       (popup-menu gnus-mime-security-button-menu nil prefix))))
8576
8577 (defvar gnus-mime-security-details-buffer nil)
8578
8579 (defvar gnus-mime-security-button-pressed nil)
8580
8581 (defvar gnus-mime-security-show-details-inline t
8582   "If non-nil, show details in the article buffer.")
8583
8584 (defun gnus-mime-security-verify-or-decrypt (handle)
8585   (mm-remove-parts (cdr handle))
8586   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8587         point (inhibit-read-only t))
8588     (if region
8589         (goto-char (car region)))
8590     (setq point (point))
8591     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8592       (let* ((mm-verify-option 'known)
8593              (mm-decrypt-option 'known)
8594              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8595         (unless (eq nparts (cdr handle))
8596           (mm-destroy-parts (cdr handle))
8597           (setcdr handle nparts))))
8598     (gnus-mime-display-security handle)
8599     (when region
8600       (delete-region (point) (cdr region))
8601       (set-marker (car region) nil)
8602       (set-marker (cdr region) nil))
8603     (goto-char point)))
8604
8605 (defun gnus-mime-security-show-details (handle)
8606   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8607     (if (not details)
8608         (gnus-message 5 "No details.")
8609       (if gnus-mime-security-show-details-inline
8610           (let ((gnus-mime-security-button-pressed
8611                  (not (get-text-property (point) 'gnus-mime-details)))
8612                 (gnus-mime-security-button-line-format
8613                  (get-text-property (point) 'gnus-line-format))
8614                 (inhibit-read-only t))
8615             (forward-char -1)
8616             (while (eq (get-text-property (point) 'gnus-line-format)
8617                        gnus-mime-security-button-line-format)
8618               (forward-char -1))
8619             (forward-char)
8620             (save-restriction
8621               (narrow-to-region (point) (point))
8622               (gnus-insert-mime-security-button handle))
8623             (delete-region (point)
8624                            (or (text-property-not-all
8625                                 (point) (point-max)
8626                                 'gnus-line-format
8627                                 gnus-mime-security-button-line-format)
8628                                (point-max))))
8629         ;; Not inlined.
8630         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8631             (with-current-buffer gnus-mime-security-details-buffer
8632               (erase-buffer)
8633               t)
8634           (setq gnus-mime-security-details-buffer
8635                 (gnus-get-buffer-create "*MIME Security Details*")))
8636         (with-current-buffer gnus-mime-security-details-buffer
8637           (insert details)
8638           (goto-char (point-min)))
8639         (pop-to-buffer gnus-mime-security-details-buffer)))))
8640
8641 (defun gnus-mime-security-press-button (handle)
8642   (save-excursion
8643     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8644         (gnus-mime-security-show-details handle)
8645       (gnus-mime-security-verify-or-decrypt handle))))
8646
8647 (defun gnus-insert-mime-security-button (handle &optional displayed)
8648   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8649          (gnus-tmp-type
8650           (concat
8651            (or (nth 2 (assoc protocol mm-verify-function-alist))
8652                (nth 2 (assoc protocol mm-decrypt-function-alist))
8653                "Unknown")
8654            (if (equal (car handle) "multipart/signed")
8655                " Signed" " Encrypted")
8656            " Part"))
8657          (gnus-tmp-info
8658           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8659               "Undecided"))
8660          (gnus-tmp-details
8661           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8662          gnus-tmp-pressed-details
8663          b e)
8664     (setq gnus-tmp-details
8665           (if gnus-tmp-details
8666               (concat "\n" gnus-tmp-details)
8667             ""))
8668     (setq gnus-tmp-pressed-details
8669           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8670     (unless (bolp)
8671       (insert "\n"))
8672     (setq b (point))
8673     (gnus-eval-format
8674      gnus-mime-security-button-line-format
8675      gnus-mime-security-button-line-format-alist
8676      `(keymap ,gnus-mime-security-button-map
8677          gnus-callback gnus-mime-security-press-button
8678          gnus-line-format ,gnus-mime-security-button-line-format
8679          gnus-mime-details ,gnus-mime-security-button-pressed
8680          article-type annotation
8681          gnus-data ,handle))
8682     (setq e (if (bolp)
8683                 ;; Exclude a newline.
8684                 (1- (point))
8685               (point)))
8686     (when gnus-article-button-face
8687       (gnus-overlay-put (gnus-make-overlay b e nil t)
8688                         'face gnus-article-button-face))
8689     (widget-convert-button
8690      'link b e
8691      :mime-handle handle
8692      :action 'gnus-widget-press-button
8693      :button-keymap gnus-mime-security-button-map
8694      :help-echo
8695      (lambda (widget)
8696        ;; Needed to properly clear the message due to a bug in
8697        ;; wid-edit (XEmacs only).
8698        (when (boundp 'help-echo-owns-message)
8699          (setq help-echo-owns-message t))
8700        (format
8701         "%S: show detail; %S: more options"
8702         (aref gnus-mouse-2 0)
8703         (aref gnus-down-mouse-3 0))))))
8704
8705 (defun gnus-mime-display-security (handle)
8706   (save-restriction
8707     (narrow-to-region (point) (point))
8708     (unless (gnus-unbuttonized-mime-type-p (car handle))
8709       (gnus-insert-mime-security-button handle))
8710     (gnus-mime-display-part (cadr handle))
8711     (unless (bolp)
8712       (insert "\n"))
8713     (unless (gnus-unbuttonized-mime-type-p (car handle))
8714       (let ((gnus-mime-security-button-line-format
8715              gnus-mime-security-button-end-line-format))
8716         (gnus-insert-mime-security-button handle)))
8717     (mm-set-handle-multipart-parameter
8718      handle 'gnus-region (cons (point-min-marker) (point-max-marker)))
8719     (goto-char (point-max))))
8720
8721 (defun gnus-mime-security-run-function (function)
8722   "Run FUNCTION with the security part under point."
8723   (gnus-article-check-buffer)
8724   (let ((data (get-text-property (point) 'gnus-data))
8725         buffer handle)
8726     (when (and (stringp (car-safe data))
8727                (setq buffer (mm-handle-multipart-original-buffer data))
8728                (setq handle (cadr data)))
8729       (if (bufferp (mm-handle-buffer handle))
8730           (progn
8731             (setq handle (cons buffer (copy-sequence (cdr handle))))
8732             (mm-handle-set-undisplayer handle nil))
8733         (setq handle (mm-make-handle
8734                       buffer
8735                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8736                       nil nil nil nil nil nil)))
8737       (funcall function handle))))
8738
8739 (defun gnus-mime-security-save-part ()
8740   "Save the security part under point."
8741   (interactive)
8742   (gnus-mime-security-run-function 'mm-save-part))
8743
8744 (defun gnus-mime-security-pipe-part ()
8745   "Pipe the security part under point to a process."
8746   (interactive)
8747   (gnus-mime-security-run-function 'mm-pipe-part))
8748
8749 (gnus-ems-redefine)
8750
8751 (provide 'gnus-art)
8752
8753 (run-hooks 'gnus-art-load-hook)
8754
8755 ;;; gnus-art.el ends here