parse-time.el: Use cl-lib as much as possible following the 2014-09-26 change in...
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2014 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 (eval-when-compile
28   (require 'cl))
29 (defvar tool-bar-map)
30 (defvar w3m-minor-mode-map)
31
32 (require 'gnus)
33 (require 'gnus-util)
34 (require 'gnus-sum)
35 (require 'gnus-spec)
36 (require 'gnus-int)
37 (require 'gnus-win)
38 (require 'mm-bodies)
39 (require 'mail-parse)
40 (require 'mm-decode)
41 (require 'mm-view)
42 (require 'wid-edit)
43 (require 'mm-uu)
44 (require 'message)
45 (require 'mouse)
46
47 (autoload 'gnus-msg-mail "gnus-msg" nil t)
48 (autoload 'gnus-button-mailto "gnus-msg")
49 (autoload 'gnus-button-reply "gnus-msg" nil t)
50 (autoload 'parse-time-string "parse-time" nil nil)
51 (autoload 'ansi-color-apply-on-region "ansi-color")
52 (autoload 'mm-url-insert-file-contents-external "mm-url")
53 (autoload 'mm-extern-cache-contents "mm-extern")
54
55 (defgroup gnus-article nil
56   "Article display."
57   :link '(custom-manual "(gnus)Article Buffer")
58   :group 'gnus)
59
60 (defgroup gnus-article-treat nil
61   "Treating article parts."
62   :link '(custom-manual "(gnus)Article Hiding")
63   :group 'gnus-article)
64
65 (defgroup gnus-article-hiding nil
66   "Hiding article parts."
67   :link '(custom-manual "(gnus)Article Hiding")
68   :group 'gnus-article)
69
70 (defgroup gnus-article-highlight nil
71   "Article highlighting."
72   :link '(custom-manual "(gnus)Article Highlighting")
73   :group 'gnus-article
74   :group 'gnus-visual)
75
76 (defgroup gnus-article-signature nil
77   "Article signatures."
78   :link '(custom-manual "(gnus)Article Signature")
79   :group 'gnus-article)
80
81 (defgroup gnus-article-headers nil
82   "Article headers."
83   :link '(custom-manual "(gnus)Hiding Headers")
84   :group 'gnus-article)
85
86 (defgroup gnus-article-washing nil
87   "Special commands on articles."
88   :link '(custom-manual "(gnus)Article Washing")
89   :group 'gnus-article)
90
91 (defgroup gnus-article-emphasis nil
92   "Fontisizing articles."
93   :link '(custom-manual "(gnus)Article Fontisizing")
94   :group 'gnus-article)
95
96 (defgroup gnus-article-saving nil
97   "Saving articles."
98   :link '(custom-manual "(gnus)Saving Articles")
99   :group 'gnus-article)
100
101 (defgroup gnus-article-mime nil
102   "Worshiping the MIME wonder."
103   :link '(custom-manual "(gnus)Using MIME")
104   :group 'gnus-article)
105
106 (defgroup gnus-article-buttons nil
107   "Pushable buttons in the article buffer."
108   :link '(custom-manual "(gnus)Article Buttons")
109   :group 'gnus-article)
110
111 (defgroup gnus-article-various nil
112   "Other article options."
113   :link '(custom-manual "(gnus)Misc Article")
114   :group 'gnus-article)
115
116 (defcustom gnus-ignored-headers
117   (mapcar
118    (lambda (header)
119      (concat "^" header ":"))
120    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
121      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
122      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
123      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
124      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
125      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
126      "X-Attribution" "X-Originating-IP" "Delivered-To"
127      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
128      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
129      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
130      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
131      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
132      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
133      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
134      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
135      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
136      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
137      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
138      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
139      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
140      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
141      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
142      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
143      "List-[A-Za-z]+" "X-Listprocessor-Version"
144      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
145      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
146      "X-Received" "Content-length" "X-precedence"
147      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
148      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
149      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
150      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
151      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
152      "X-Content-length" "X-Posting-Agent" "Original-Received"
153      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
154      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
155      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
156      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
157      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
158      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
159      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
160      "Envelope-Sender" "Envelope-Recipients"))
161   "*All headers that start with this regexp will be hidden.
162 This variable can also be a list of regexps of headers to be ignored.
163 If `gnus-visible-headers' is non-nil, this variable will be ignored."
164   :type '(choice regexp
165                  (repeat regexp))
166   :group 'gnus-article-hiding)
167
168 (defcustom gnus-visible-headers
169   "^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:"
170   "*All headers that do not match this regexp will be hidden.
171 This variable can also be a list of regexp of headers to remain visible.
172 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
173   :type '(choice
174           (repeat :value-to-internal (lambda (widget value)
175                                        (custom-split-regexp-maybe value))
176                   :match (lambda (widget value)
177                            (or (stringp value)
178                                (widget-editable-list-match widget value)))
179                   regexp)
180           (const :tag "Use gnus-ignored-headers" nil)
181           regexp)
182   :group 'gnus-article-hiding)
183
184 (defcustom gnus-sorted-header-list
185   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
186     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
187   "*This variable is a list of regular expressions.
188 If it is non-nil, headers that match the regular expressions will
189 be placed first in the article buffer in the sequence specified by
190 this list."
191   :type '(repeat regexp)
192   :group 'gnus-article-hiding)
193
194 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
195   "Headers that are only to be displayed if they have interesting data.
196 Possible values in this list are:
197
198   'empty       Headers with no content.
199   'newsgroups  Newsgroup identical to Gnus group.
200   'to-address  To identical to To-address.
201   'to-list     To identical to To-list.
202   'cc-list     CC identical to To-list.
203   'followup-to Followup-to identical to Newsgroups.
204   'reply-to    Reply-to identical to From.
205   'date        Date less than four days old.
206   'long-to     To and/or Cc longer than 1024 characters.
207   'many-to     Multiple To and/or Cc."
208   :type '(set (const :tag "Headers with no content." empty)
209               (const :tag "Newsgroups identical to Gnus group." newsgroups)
210               (const :tag "To identical to To-address." to-address)
211               (const :tag "To identical to To-list." to-list)
212               (const :tag "CC identical to To-list." cc-list)
213               (const :tag "Followup-to identical to Newsgroups." followup-to)
214               (const :tag "Reply-to identical to From." reply-to)
215               (const :tag "Date less than four days old." date)
216               (const :tag "To and/or Cc longer than 1024 characters." long-to)
217               (const :tag "Multiple To and/or Cc headers." many-to))
218   :group 'gnus-article-hiding)
219
220 (defcustom gnus-article-skip-boring nil
221   "Skip over text that is not worth reading.
222 By default, if you set this t, then Gnus will display citations and
223 signatures, but will never scroll down to show you a page consisting
224 only of boring text.  Boring text is controlled by
225 `gnus-article-boring-faces'."
226   :version "22.1"
227   :type 'boolean
228   :group 'gnus-article-hiding)
229
230 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
231   "Regexp matching signature separator.
232 This can also be a list of regexps.  In that case, it will be checked
233 from head to tail looking for a separator.  Searches will be done from
234 the end of the buffer."
235   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
236                  (regexp)
237                  (repeat :tag "List of regexp" regexp))
238   :group 'gnus-article-signature)
239
240 (defcustom gnus-signature-limit nil
241   "Provide a limit to what is considered a signature.
242 If it is a number, no signature may not be longer (in characters) than
243 that number.  If it is a floating point number, no signature may be
244 longer (in lines) than that number.  If it is a function, the function
245 will be called without any parameters, and if it returns nil, there is
246 no signature in the buffer.  If it is a string, it will be used as a
247 regexp.  If it matches, the text in question is not a signature.
248
249 This can also be a list of the above values."
250   :type '(choice (const nil)
251                  (integer :value 200)
252                  (number :value 4.0)
253                  function
254                  (regexp :value ".*"))
255   :group 'gnus-article-signature)
256
257 (defcustom gnus-hidden-properties
258   (if (featurep 'xemacs)
259       ;; `intangible' is evil, but I keep it here in case it's useful.
260       '(invisible t intangible t)
261     ;; Emacs's command loop moves point out of invisible text anyway, so
262     ;; `intangible' is clearly not needed there.
263     '(invisible t))
264   "Property list to use for hiding text."
265   :type 'sexp
266   :group 'gnus-article-hiding)
267
268 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
269 ;; frames in a session.
270 (defcustom gnus-article-x-face-command
271   (if (featurep 'xemacs)
272       (if (or (gnus-image-type-available-p 'xface)
273               (gnus-image-type-available-p 'pbm))
274           'gnus-display-x-face-in-from
275         "{ echo \
276 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
277 ; uncompface; } | icontopbm | ee -")
278     (if (gnus-image-type-available-p 'pbm)
279         'gnus-display-x-face-in-from
280       "{ echo \
281 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
282 ; uncompface; } | icontopbm | display -"))
283   "*String or function to be executed to display an X-Face header.
284 If it is a string, the command will be executed in a sub-shell
285 asynchronously.  The compressed face will be piped to this command."
286   :type `(choice string
287                  (function-item gnus-display-x-face-in-from)
288                  function)
289   :version "21.1"
290   :group 'gnus-picon
291   :group 'gnus-article-washing)
292
293 (defcustom gnus-article-x-face-too-ugly nil
294   "Regexp matching posters whose face shouldn't be shown automatically."
295   :type '(choice regexp (const nil))
296   :group 'gnus-article-washing)
297
298 (defcustom gnus-article-banner-alist nil
299   "Banner alist for stripping.
300 For example,
301      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
302   :version "21.1"
303   :type '(repeat (cons symbol regexp))
304   :group 'gnus-article-washing)
305
306 (gnus-define-group-parameter
307  banner
308  :variable-document
309  "Alist of regexps (to match group names) and banner."
310  :variable-group gnus-article-washing
311  :parameter-type
312  '(choice :tag "Banner"
313           :value nil
314           (const :tag "Remove signature" signature)
315           (symbol :tag "Item in `gnus-article-banner-alist'" none)
316           regexp
317           (const :tag "None" nil))
318  :parameter-document
319  "If non-nil, specify how to remove `banners' from articles.
320
321 Symbol `signature' means to remove signatures delimited by
322 `gnus-signature-separator'.  Any other symbol is used to look up a
323 regular expression to match the banner in `gnus-article-banner-alist'.
324 A string is used as a regular expression to match the banner
325 directly.")
326
327 (defcustom gnus-article-address-banner-alist nil
328   "Alist of mail addresses and banners.
329 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
330 to match a mail address in the From: header, BANNER is one of a symbol
331 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
332 If ADDRESS matches author's mail address, it will remove things like
333 advertisements.  For example:
334
335 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
336 "
337   :type '(repeat
338           (cons
339            (regexp :tag "Address")
340            (choice :tag "Banner" :value nil
341                    (const :tag "Remove signature" signature)
342                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
343                    regexp
344                    (const :tag "None" nil))))
345   :version "22.1"
346   :group 'gnus-article-washing)
347
348 (defmacro gnus-emphasis-custom-with-format (&rest body)
349   `(let ((format "\
350 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
351 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
352      ,@body))
353
354 (defun gnus-emphasis-custom-value-to-external (value)
355   (gnus-emphasis-custom-with-format
356    (if (consp (car value))
357        (list (format format (car (car value)) (cdr (car value)))
358              2
359              (if (nth 1 value) 2 3)
360              (nth 2 value))
361      value)))
362
363 (defun gnus-emphasis-custom-value-to-internal (value)
364   (gnus-emphasis-custom-with-format
365    (let ((regexp (concat "\\`"
366                          (format (regexp-quote format)
367                                  "\\([^()]+\\)" "\\([^()]+\\)")
368                          "\\'"))
369          pattern)
370      (if (string-match regexp (setq pattern (car value)))
371          (list (cons (match-string 1 pattern) (match-string 2 pattern))
372                (= (nth 2 value) 2)
373                (nth 3 value))
374        value))))
375
376 (defcustom gnus-emphasis-alist
377   (let ((types
378          '(("\\*" "\\*" bold nil 2)
379            ("_" "_" underline)
380            ("/" "/" italic)
381            ("_/" "/_" underline-italic)
382            ("_\\*" "\\*_" underline-bold)
383            ("\\*/" "/\\*" bold-italic)
384            ("_\\*/" "/\\*_" underline-bold-italic))))
385     (nconc
386      (gnus-emphasis-custom-with-format
387       (mapcar (lambda (spec)
388                 (list (format format (car spec) (cadr spec))
389                       (or (nth 3 spec) 2)
390                       (or (nth 4 spec) 3)
391                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
392               types))
393      '(;; I've never seen anyone use this strikethru convention whereas I've
394        ;; several times seen it triggered by normal text.  --Stef
395        ;; Miles suggests that this form is sometimes used but for italics,
396        ;; so maybe we should map it to `italic'.
397        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
398        ;; 2 3 gnus-emphasis-strikethru)
399        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
400         2 3 gnus-emphasis-underline))))
401   "*Alist that says how to fontify certain phrases.
402 Each item looks like this:
403
404   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
405
406 The first element is a regular expression to be matched.  The second
407 is a number that says what regular expression grouping used to find
408 the entire emphasized word.  The third is a number that says what
409 regexp grouping should be displayed and highlighted.  The fourth
410 is the face used for highlighting."
411   :type
412   '(repeat
413     (menu-choice
414      :format "%[Customizing Style%]\n%v"
415      :indent 2
416      (group :tag "Default"
417             :value ("" 0 0 default)
418             :value-create
419             (lambda (widget)
420               (let ((value (widget-get
421                             (cadr (widget-get (widget-get widget :parent)
422                                               :args))
423                             :value)))
424                 (if (not (eq (nth 2 value) 'default))
425                     (widget-put
426                      widget
427                      :value
428                      (gnus-emphasis-custom-value-to-external value))))
429               (widget-group-value-create widget))
430             regexp
431             (integer :format "Match group: %v")
432             (integer :format "Emphasize group: %v")
433             face)
434      (group :tag "Simple"
435             :value (("_" . "_") nil default)
436             (cons :format "%v"
437                   (regexp :format "Start regexp: %v")
438                   (regexp :format "End regexp: %v"))
439             (boolean :format "Show start and end patterns: %[%v%]\n"
440                      :on " On " :off " Off ")
441             face)))
442   :get (lambda (symbol)
443          (mapcar 'gnus-emphasis-custom-value-to-internal
444                  (default-value symbol)))
445   :set (lambda (symbol value)
446          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
447                                      value)))
448   :group 'gnus-article-emphasis)
449
450 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
451   "A regexp to describe whitespace which should not be emphasized.
452 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
453 The former avoids underlining of leading and trailing whitespace,
454 and the latter avoids underlining any whitespace at all."
455   :version "21.1"
456   :group 'gnus-article-emphasis
457   :type 'regexp)
458
459 (defface gnus-emphasis-bold '((t (:bold t)))
460   "Face used for displaying strong emphasized text (*word*)."
461   :group 'gnus-article-emphasis)
462
463 (defface gnus-emphasis-italic '((t (:italic t)))
464   "Face used for displaying italic emphasized text (/word/)."
465   :group 'gnus-article-emphasis)
466
467 (defface gnus-emphasis-underline '((t (:underline t)))
468   "Face used for displaying underlined emphasized text (_word_)."
469   :group 'gnus-article-emphasis)
470
471 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
472   "Face used for displaying underlined bold emphasized text (_*word*_)."
473   :group 'gnus-article-emphasis)
474
475 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
476   "Face used for displaying underlined italic emphasized text (_/word/_)."
477   :group 'gnus-article-emphasis)
478
479 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
480   "Face used for displaying bold italic emphasized text (/*word*/)."
481   :group 'gnus-article-emphasis)
482
483 (defface gnus-emphasis-underline-bold-italic
484   '((t (:bold t :italic t :underline t)))
485   "Face used for displaying underlined bold italic emphasized text.
486 Example: (_/*word*/_)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
490                                       '((t (:strikethru t)))
491                                     '((t (:strike-through t))))
492   "Face used for displaying strike-through text (-word-)."
493   :group 'gnus-article-emphasis)
494
495 (defface gnus-emphasis-highlight-words
496   '((t (:background "black" :foreground "yellow")))
497   "Face used for displaying highlighted words."
498   :group 'gnus-article-emphasis)
499
500 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
501   "Format for display of Date headers in article bodies.
502 See `format-time-string' for the possible values.
503
504 The variable can also be function, which should return a complete Date
505 header.  The function is called with one argument, the time, which can
506 be fed to `format-time-string'."
507   :type '(choice string function)
508   :link '(custom-manual "(gnus)Article Date")
509   :group 'gnus-article-washing)
510
511 (defcustom gnus-save-all-headers t
512   "*If non-nil, don't remove any headers before saving.
513 This will be overridden by the `:headers' property that the symbol of
514 the saver function, which is specified by `gnus-default-article-saver',
515 might have."
516   :group 'gnus-article-saving
517   :type 'boolean)
518
519 (defcustom gnus-prompt-before-saving 'always
520   "*This variable says how much prompting is to be done when saving articles.
521 If it is nil, no prompting will be done, and the articles will be
522 saved to the default files.  If this variable is `always', each and
523 every article that is saved will be preceded by a prompt, even when
524 saving large batches of articles.  If this variable is neither nil not
525 `always', there the user will be prompted once for a file name for
526 each invocation of the saving commands."
527   :group 'gnus-article-saving
528   :type '(choice (item always)
529                  (item :tag "never" nil)
530                  (sexp :tag "once" :format "%t\n" :value t)))
531
532 (defcustom gnus-saved-headers gnus-visible-headers
533   "Headers to keep if `gnus-save-all-headers' is nil.
534 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
535 If that variable is nil, however, all headers that match this regexp
536 will be kept while the rest will be deleted before saving.  This and
537 `gnus-save-all-headers' will be overridden by the `:headers' property
538 that the symbol of the saver function, which is specified by
539 `gnus-default-article-saver', might have."
540   :group 'gnus-article-saving
541   :type 'regexp)
542
543 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
544 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
545   "A function to save articles in your favorite format.
546 The function will be called by way of the `gnus-summary-save-article'
547 command, and friends such as `gnus-summary-save-article-rmail'.
548
549 Gnus provides the following functions:
550
551 * gnus-summary-save-in-rmail (Rmail format)
552 * gnus-summary-save-in-mail (Unix mail format)
553 * gnus-summary-save-in-folder (MH folder)
554 * gnus-summary-save-in-file (article format)
555 * gnus-summary-save-body-in-file (article body)
556 * gnus-summary-save-in-vm (use VM's folder format)
557 * gnus-summary-write-to-file (article format -- overwrite)
558 * gnus-summary-write-body-to-file (article body -- overwrite)
559 * gnus-summary-save-in-pipe (article format)
560
561 The symbol of each function may have the following properties:
562
563 * :decode
564 The value non-nil means save decoded articles.  This is meaningful
565 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
566 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
567 `gnus-summary-save-in-pipe'.
568
569 * :function
570 The value specifies an alternative function which appends, not
571 overwrites, articles to a file.  This implies that when saving many
572 articles at a time, `gnus-prompt-before-saving' is bound to t and all
573 articles are saved in a single file.  This is meaningful only with
574 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
575
576 * :headers
577 The value specifies the symbol of a variable of which the value
578 specifies headers to be saved.  If it is omitted,
579 `gnus-save-all-headers' and `gnus-saved-headers' control what
580 headers should be saved."
581   :group 'gnus-article-saving
582   :type '(radio (function-item gnus-summary-save-in-rmail)
583                 (function-item gnus-summary-save-in-mail)
584                 (function-item gnus-summary-save-in-folder)
585                 (function-item gnus-summary-save-in-file)
586                 (function-item gnus-summary-save-body-in-file)
587                 (function-item gnus-summary-save-in-vm)
588                 (function-item gnus-summary-write-to-file)
589                 (function-item gnus-summary-write-body-to-file)
590                 (function-item gnus-summary-save-in-pipe)
591                 (function)))
592
593 (defcustom gnus-article-save-coding-system
594   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
595       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
596       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
597       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
598   "Coding system used to save decoded articles to a file.
599
600 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
601 which can safely encode any characters in text.  This is used by the
602 commands including:
603
604 * gnus-summary-save-article-file
605 * gnus-summary-save-article-body-file
606 * gnus-summary-write-article-file
607 * gnus-summary-write-article-body-file
608
609 and the functions to which you may set `gnus-default-article-saver':
610
611 * gnus-summary-save-in-file
612 * gnus-summary-save-body-in-file
613 * gnus-summary-write-to-file
614 * gnus-summary-write-body-to-file
615
616 Those commands and functions save just text displayed in the article
617 buffer to a file if the value of this variable is non-nil.  Note that
618 buttonized MIME parts will be lost in a saved file in that case.
619 Otherwise, raw articles will be saved."
620   :group 'gnus-article-saving
621   :type `(choice
622           :format "%{%t%}:\n %[Value Menu%] %v"
623           (const :tag "Save raw articles" nil)
624           ,@(delq nil
625                   (mapcar
626                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
627                    '((const :tag "UTF-8" utf-8)
628                      (const :tag "iso-2022-7bit" iso-2022-7bit)
629                      (const :tag "Emacs internal" emacs-mule)
630                      (const :tag "escape-quoted" escape-quoted))))
631           (symbol :tag "Coding system")))
632
633 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
634   "A function generating a file name to save articles in Rmail format.
635 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
636   :group 'gnus-article-saving
637   :type 'function)
638
639 (defcustom gnus-mail-save-name 'gnus-plain-save-name
640   "A function generating a file name to save articles in Unix mail format.
641 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
642   :group 'gnus-article-saving
643   :type 'function)
644
645 (defcustom gnus-folder-save-name 'gnus-folder-save-name
646   "A function generating a file name to save articles in MH folder.
647 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
648   :group 'gnus-article-saving
649   :type 'function)
650
651 (defcustom gnus-file-save-name 'gnus-numeric-save-name
652   "A function generating a file name to save articles in article format.
653 The function is called with NEWSGROUP, HEADERS, and optional
654 LAST-FILE."
655   :group 'gnus-article-saving
656   :type 'function)
657
658 (defcustom gnus-split-methods
659   '((gnus-article-archive-name)
660     (gnus-article-nndoc-name))
661   "*Variable used to suggest where articles are to be saved.
662 For instance, if you would like to save articles related to Gnus in
663 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
664 you could set this variable to something like:
665
666  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
667    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
668
669 This variable is an alist where the key is the match and the
670 value is a list of possible files to save in if the match is
671 non-nil.
672
673 If the match is a string, it is used as a regexp match on the
674 article.  If the match is a symbol, that symbol will be funcalled
675 from the buffer of the article to be saved with the newsgroup as the
676 parameter.  If it is a list, it will be evalled in the same buffer.
677
678 If this form or function returns a string, this string will be used as a
679 possible file name; and if it returns a non-nil list, that list will be
680 used as possible file names."
681   :group 'gnus-article-saving
682   :type '(repeat (choice (list :value (fun) function)
683                          (cons :value ("" "") regexp (repeat string))
684                          (sexp :value nil))))
685
686 (defcustom gnus-page-delimiter "^\^L"
687   "*Regexp describing what to use as article page delimiters.
688 The default value is \"^\^L\", which is a form linefeed at the
689 beginning of a line."
690   :type 'regexp
691   :group 'gnus-article-various)
692
693 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
694   "*The format specification for the article mode line.
695 See `gnus-summary-mode-line-format' for a closer description.
696
697 The following additional specs are available:
698
699 %w  The article washing status.
700 %m  The number of MIME parts in the article."
701   :version "24.1"
702   :type 'string
703   :group 'gnus-article-various)
704
705 (defcustom gnus-article-mode-hook nil
706   "*A hook for Gnus article mode."
707   :type 'hook
708   :group 'gnus-article-various)
709
710 (when (featurep 'xemacs)
711   ;; Extracted from gnus-xmas-define in order to preserve user settings
712   (when (fboundp 'turn-off-scroll-in-place)
713     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
714   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
715   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
716
717 (defcustom gnus-article-menu-hook nil
718   "*Hook run after the creation of the article mode menu."
719   :type 'hook
720   :group 'gnus-article-various)
721
722 (defcustom gnus-article-prepare-hook nil
723   "*A hook called after an article has been prepared in the article buffer."
724   :type 'hook
725   :group 'gnus-article-various)
726
727 (defcustom gnus-copy-article-ignored-headers nil
728   "List of headers to be removed when copying an article.
729 Each element is a regular expression."
730   :version "23.1" ;; No Gnus
731   :type '(repeat regexp)
732   :group 'gnus-article-various)
733
734 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
735                         "Gnus 5.10 (Emacs 22.1)")
736
737 (defface gnus-button
738   '((t (:weight bold)))
739   "Face used for highlighting a button in the article buffer."
740   :group 'gnus-article-buttons)
741
742 (defcustom gnus-article-button-face 'gnus-button
743   "Face used for highlighting buttons in the article buffer.
744
745 An article button is a piece of text that you can activate by pressing
746 `RET' or `mouse-2' above it."
747   :type 'face
748   :group 'gnus-article-buttons)
749
750 (defcustom gnus-article-mouse-face 'highlight
751   "Face used for mouse highlighting in the article buffer.
752
753 Article buttons will be displayed in this face when the cursor is
754 above them."
755   :type 'face
756   :group 'gnus-article-buttons)
757
758 (defcustom gnus-signature-face 'gnus-signature
759   "Face used for highlighting a signature in the article buffer.
760 Obsolete; use the face `gnus-signature' for customizations instead."
761   :type 'face
762   :group 'gnus-article-highlight
763   :group 'gnus-article-signature)
764
765 (defface gnus-signature
766   '((t
767      (:italic t)))
768   "Face used for highlighting a signature in the article buffer."
769   :group 'gnus-article-highlight
770   :group 'gnus-article-signature)
771 ;; backward-compatibility alias
772 (put 'gnus-signature-face 'face-alias 'gnus-signature)
773 (put 'gnus-signature-face 'obsolete-face "22.1")
774
775 (defface gnus-header-from
776   '((((class color)
777       (background dark))
778      (:foreground "PaleGreen1"))
779     (((class color)
780       (background light))
781      (:foreground "red3"))
782     (t
783      (:italic t)))
784   "Face used for displaying from headers."
785   :group 'gnus-article-headers
786   :group 'gnus-article-highlight)
787 ;; backward-compatibility alias
788 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
789 (put 'gnus-header-from-face 'obsolete-face "22.1")
790
791 (defface gnus-header-subject
792   '((((class color)
793       (background dark))
794      (:foreground "SeaGreen1"))
795     (((class color)
796       (background light))
797      (:foreground "red4"))
798     (t
799      (:bold t :italic t)))
800   "Face used for displaying subject headers."
801   :group 'gnus-article-headers
802   :group 'gnus-article-highlight)
803 ;; backward-compatibility alias
804 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
805 (put 'gnus-header-subject-face 'obsolete-face "22.1")
806
807 (defface gnus-header-newsgroups
808   '((((class color)
809       (background dark))
810      (:foreground "yellow" :italic t))
811     (((class color)
812       (background light))
813      (:foreground "MidnightBlue" :italic t))
814     (t
815      (:italic t)))
816   "Face used for displaying newsgroups headers.
817 In the default setup this face is only used for crossposted
818 articles."
819   :group 'gnus-article-headers
820   :group 'gnus-article-highlight)
821 ;; backward-compatibility alias
822 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
823 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
824
825 (defface gnus-header-name
826   '((((class color)
827       (background dark))
828      (:foreground "SpringGreen2"))
829     (((class color)
830       (background light))
831      (:foreground "maroon"))
832     (t
833      (:bold t)))
834   "Face used for displaying header names."
835   :group 'gnus-article-headers
836   :group 'gnus-article-highlight)
837 ;; backward-compatibility alias
838 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
839 (put 'gnus-header-name-face 'obsolete-face "22.1")
840
841 (defface gnus-header-content
842   '((((class color)
843       (background dark))
844      (:foreground "SpringGreen1" :italic t))
845     (((class color)
846       (background light))
847      (:foreground "indianred4" :italic t))
848     (t
849      (:italic t)))  "Face used for displaying header content."
850   :group 'gnus-article-headers
851   :group 'gnus-article-highlight)
852 ;; backward-compatibility alias
853 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
854 (put 'gnus-header-content-face 'obsolete-face "22.1")
855
856 (defcustom gnus-header-face-alist
857   '(("From" nil gnus-header-from)
858     ("Subject" nil gnus-header-subject)
859     ("Newsgroups:.*," nil gnus-header-newsgroups)
860     ("" gnus-header-name gnus-header-content))
861   "*Controls highlighting of article headers.
862
863 An alist of the form (HEADER NAME CONTENT).
864
865 HEADER is a regular expression which should match the name of a
866 header and NAME and CONTENT are either face names or nil.
867
868 The name of each header field will be displayed using the face
869 specified by the first element in the list where HEADER matches
870 the header name and NAME is non-nil.  Similarly, the content will
871 be displayed by the first non-nil matching CONTENT face."
872   :group 'gnus-article-headers
873   :group 'gnus-article-highlight
874   :type '(repeat (list (regexp :tag "Header")
875                        (choice :tag "Name"
876                                (item :tag "skip" nil)
877                                (face :value default))
878                        (choice :tag "Content"
879                                (item :tag "skip" nil)
880                                (face :value default)))))
881
882 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
883                                           '((xface . (:face gnus-x-face)))
884                                         '((pbm . (:face gnus-x-face))
885                                           (png . nil)))
886   "Alist of image types and properties applied to Face and X-Face images.
887 Here are examples:
888
889 ;; Specify the altitude of Face images in the From header.
890 \(setq gnus-face-properties-alist
891       '((pbm . (:face gnus-x-face :ascent 80))
892         (png . (:ascent 80))))
893
894 ;; Show Face images as pressed buttons.
895 \(setq gnus-face-properties-alist
896       '((pbm . (:face gnus-x-face :relief -2))
897         (png . (:relief -2))))
898
899 See the manual for the valid properties for various image types.
900 Currently, `pbm' is used for X-Face images and `png' is used for Face
901 images in Emacs.  Only the `:face' property is effective on the `xface'
902 image type in XEmacs if it is built with the libcompface library."
903   :version "23.1" ;; No Gnus
904   :group 'gnus-article-headers
905   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
906
907 (defcustom gnus-article-decode-hook
908   '(article-decode-charset article-decode-encoded-words
909                            article-decode-group-name article-decode-idna-rhs)
910   "*Hook run to decode charsets in articles."
911   :group 'gnus-article-headers
912   :type 'hook)
913
914 (defcustom gnus-display-mime-function 'gnus-display-mime
915   "Function to display MIME articles."
916   :group 'gnus-article-mime
917   :type 'function)
918
919 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
920   "Function used to decode headers.")
921
922 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
923   "Function used to decode addresses.")
924
925 (defvar gnus-article-dumbquotes-map
926   '((?\200 "EUR")
927     (?\202 ",")
928     (?\203 "f")
929     (?\204 ",,")
930     (?\205 "...")
931     (?\213 "<")
932     (?\214 "OE")
933     (?\221 "`")
934     (?\222 "'")
935     (?\223 "``")
936     (?\224 "\"")
937     (?\225 "*")
938     (?\226 "-")
939     (?\227 "--")
940     (?\230 "~")
941     (?\231 "(TM)")
942     (?\233 ">")
943     (?\234 "oe")
944     (?\264 "'"))
945   "Table for MS-to-Latin1 translation.")
946
947 (defcustom gnus-ignored-mime-types nil
948   "List of MIME types that should be ignored by Gnus."
949   :version "21.1"
950   :group 'gnus-article-mime
951   :type '(repeat regexp))
952
953 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
954   "List of MIME types that should not be given buttons when rendered inline.
955 See also `gnus-buttonized-mime-types' which may override this variable.
956 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
957   :version "21.1"
958   :group 'gnus-article-mime
959   :type '(repeat regexp))
960
961 (defcustom gnus-buttonized-mime-types nil
962   "List of MIME types that should be given buttons when rendered inline.
963 If set, this variable overrides `gnus-unbuttonized-mime-types'.
964 To see e.g. security buttons you could set this to
965 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
966 this list to display radio buttons that allow you to choose one of two
967 media types those mails include.  See also `mm-discouraged-alternatives'.
968 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
969   :version "22.1"
970   :group 'gnus-article-mime
971   :type '(repeat regexp))
972
973 (defcustom gnus-inhibit-mime-unbuttonizing nil
974   "If non-nil, all MIME parts get buttons.
975 When nil (the default value), then some MIME parts do not get buttons,
976 as described by the variables `gnus-buttonized-mime-types' and
977 `gnus-unbuttonized-mime-types'."
978   :version "22.1"
979   :group 'gnus-article-mime
980   :type 'boolean)
981
982 (defcustom gnus-body-boundary-delimiter "_"
983   "String used to delimit header and body.
984 This variable is used by `gnus-article-treat-body-boundary' which can
985 be controlled by `gnus-treat-body-boundary'."
986   :version "22.1"
987   :group 'gnus-article-various
988   :type '(choice (item :tag "None" :value nil)
989                  string))
990
991 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
992                                   "/usr/share/picons")
993   "Defines the location of the faces database.
994 For information on obtaining this database of pretty pictures, please
995 see http://www.cs.indiana.edu/picons/ftp/index.html"
996   :version "22.1"
997   :type '(repeat directory)
998   :link '(url-link :tag "download"
999                    "http://www.cs.indiana.edu/picons/ftp/index.html")
1000   :link '(custom-manual "(gnus)Picons")
1001   :group 'gnus-picon)
1002
1003 (defun gnus-picons-installed-p ()
1004   "Say whether picons are installed on your machine."
1005   (let ((installed nil))
1006     (dolist (database gnus-picon-databases)
1007       (when (file-exists-p database)
1008         (setq installed t)))
1009     installed))
1010
1011 (defcustom gnus-article-mime-part-function nil
1012   "Function called with a MIME handle as the argument.
1013 This is meant for people who want to do something automatic based
1014 on parts -- for instance, adding Vcard info to a database."
1015   :group 'gnus-article-mime
1016   :type '(choice (const nil)
1017                  function))
1018
1019 (defcustom gnus-mime-multipart-functions nil
1020   "An alist of MIME types to functions to display them."
1021   :version "21.1"
1022   :group 'gnus-article-mime
1023   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1024
1025 (defcustom gnus-article-date-headers '(combined-lapsed)
1026   "A list of Date header formats to display.
1027 Valid formats are `ut' (universal time), `local' (local time
1028 zone), `english' (readable English), `lapsed' (elapsed time),
1029 `combined-lapsed' (both the original date and the elapsed time),
1030 `original' (the original date header), `iso8601' (ISO8601
1031 format), and `user-defined' (a user-defined format defined by the
1032 `gnus-article-time-format' variable).
1033
1034 You have as many date headers as you want in the article buffer.
1035 Some of these headers are updated automatically.  See
1036 `gnus-article-update-date-headers' for details."
1037   :version "24.1"
1038   :group 'gnus-article-headers
1039   :type '(set
1040           (const :tag "Universal time (UT)" ut)
1041           (const :tag "Local time zone" local)
1042           (const :tag "Readable English" english)
1043           (const :tag "Elapsed time" lapsed)
1044           (const :tag "Original and elapsed time" combined-lapsed)
1045           (const :tag "Original date header" original)
1046           (const :tag "ISO8601 format" iso8601)
1047           (const :tag "User-defined" user-defined)))
1048
1049 (defcustom gnus-article-update-date-headers nil
1050   "A number that says how often to update the date header (in seconds).
1051 If nil, don't update it at all."
1052   :version "24.1"
1053   :group 'gnus-article-headers
1054   :type '(choice
1055           (item :tag "Don't update" :value nil)
1056           integer))
1057
1058 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1059   "Function called with a MIME handle as the argument.
1060 This is meant for people who want to view first matched part.
1061 For `undisplayed-alternative' (default), the first undisplayed
1062 part or alternative part is used.  For `undisplayed', the first
1063 undisplayed part is used.  For a function, the first part which
1064 the function return t is used.  For nil, the first part is
1065 used."
1066   :version "21.1"
1067   :group 'gnus-article-mime
1068   :type '(choice
1069           (item :tag "first" :value nil)
1070           (item :tag "undisplayed" :value undisplayed)
1071           (item :tag "undisplayed or alternative"
1072                 :value undisplayed-alternative)
1073           (function)))
1074
1075 (defcustom gnus-mime-action-alist
1076   '(("save to file" . gnus-mime-save-part)
1077     ("save and strip" . gnus-mime-save-part-and-strip)
1078     ("replace with file" . gnus-mime-replace-part)
1079     ("delete part" . gnus-mime-delete-part)
1080     ("display as text" . gnus-mime-inline-part)
1081     ("view the part" . gnus-mime-view-part)
1082     ("pipe to command" . gnus-mime-pipe-part)
1083     ("toggle display" . gnus-article-press-button)
1084     ("toggle display" . gnus-article-view-part-as-charset)
1085     ("view as type" . gnus-mime-view-part-as-type)
1086     ("view internally" . gnus-mime-view-part-internally)
1087     ("view externally" . gnus-mime-view-part-externally))
1088   "An alist of actions that run on the MIME attachment."
1089   :group 'gnus-article-mime
1090   :type '(repeat (cons (string :tag "name")
1091                        (function))))
1092
1093 (defcustom gnus-auto-select-part 1
1094   "Advance to next MIME part when deleting or stripping parts.
1095
1096 When 0, point will be placed on the same part as before.  When
1097 positive (negative), move point forward (backwards) this many
1098 parts.  When nil, redisplay article."
1099   :version "23.1" ;; No Gnus
1100   :group 'gnus-article-mime
1101   :type '(choice (const nil :tag "Redisplay article.")
1102                  (const 1 :tag "Next part.")
1103                  (const 0 :tag "Current part.")
1104                  integer))
1105
1106 ;;;
1107 ;;; The treatment variables
1108 ;;;
1109
1110 (defvar gnus-part-display-hook nil
1111   "Hook called on parts that are to receive treatment.")
1112
1113 (defvar gnus-article-treat-custom
1114   '(choice (const :tag "Off" nil)
1115            (const :tag "On" t)
1116            (const :tag "Header" head)
1117            (const :tag "First" first)
1118            (const :tag "Last" last)
1119            (integer :tag "Less")
1120            (repeat :tag "Groups" regexp)
1121            (sexp :tag "Predicate")))
1122
1123 (defvar gnus-article-treat-head-custom
1124   '(choice (const :tag "Off" nil)
1125            (const :tag "Header" head)))
1126
1127 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1128                                    "text/x-patch" "text/html")
1129   "Part types eligible for treatment.")
1130
1131 (defvar gnus-inhibit-treatment nil
1132   "Whether to inhibit treatment.")
1133
1134 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1135   "Highlight the signature.
1136 Valid values are nil, t, `head', `first', `last', an integer or a
1137 predicate.  See Info node `(gnus)Customizing Articles'."
1138   :group 'gnus-article-treat
1139   :link '(custom-manual "(gnus)Customizing Articles")
1140   :type gnus-article-treat-custom)
1141 (put 'gnus-treat-highlight-signature 'highlight t)
1142
1143 (defcustom gnus-treat-buttonize 100000
1144   "Add buttons.
1145 Valid values are nil, t, `head', `first', `last', an integer or a
1146 predicate.  See Info node `(gnus)Customizing Articles'."
1147   :group 'gnus-article-treat
1148   :link '(custom-manual "(gnus)Customizing Articles")
1149   :type gnus-article-treat-custom)
1150 (put 'gnus-treat-buttonize 'highlight t)
1151
1152 (defcustom gnus-treat-buttonize-head 'head
1153   "Add buttons to the head.
1154 Valid values are nil, t, `head', `first', `last', an integer or a
1155 predicate.  See Info node `(gnus)Customizing Articles'."
1156   :group 'gnus-article-treat
1157   :link '(custom-manual "(gnus)Customizing Articles")
1158   :type gnus-article-treat-head-custom)
1159 (put 'gnus-treat-buttonize-head 'highlight t)
1160
1161 (defcustom gnus-treat-date 'head
1162   "Display dates according to the `gnus-article-date-headers' variable.
1163 Valid values are nil, t, `head', `first', `last', an integer or a
1164 predicate.  See Info node `(gnus)Customizing Articles'."
1165   :version "24.1"
1166   :group 'gnus-article-treat
1167   :link '(custom-manual "(gnus)Customizing Articles")
1168   :type gnus-article-treat-head-custom)
1169
1170 (defcustom gnus-treat-emphasize 50000
1171   "Emphasize text.
1172 Valid values are nil, t, `head', `first', `last', an integer or a
1173 predicate.  See Info node `(gnus)Customizing Articles'."
1174   :group 'gnus-article-treat
1175   :link '(custom-manual "(gnus)Customizing Articles")
1176   :type gnus-article-treat-custom)
1177 (put 'gnus-treat-emphasize 'highlight t)
1178
1179 (defcustom gnus-treat-strip-cr nil
1180   "Remove carriage returns.
1181 Valid values are nil, t, `head', `first', `last', an integer or a
1182 predicate.  See Info node `(gnus)Customizing Articles'."
1183   :version "22.1"
1184   :group 'gnus-article-treat
1185   :link '(custom-manual "(gnus)Customizing Articles")
1186   :type gnus-article-treat-custom)
1187
1188 (defcustom gnus-treat-unsplit-urls nil
1189   "Remove newlines from within URLs.
1190 Valid values are nil, t, `head', `first', `last', an integer or a
1191 predicate.  See Info node `(gnus)Customizing Articles'."
1192   :version "22.1"
1193   :group 'gnus-article-treat
1194   :link '(custom-manual "(gnus)Customizing Articles")
1195   :type gnus-article-treat-custom)
1196
1197 (defcustom gnus-treat-leading-whitespace nil
1198   "Remove leading whitespace in headers.
1199 Valid values are nil, t, `head', `first', `last', an integer or a
1200 predicate.  See Info node `(gnus)Customizing Articles'."
1201   :version "22.1"
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-custom)
1205
1206 (defcustom gnus-treat-hide-headers 'head
1207   "Hide headers.
1208 Valid values are nil, t, `head', `first', `last', an integer or a
1209 predicate.  See Info node `(gnus)Customizing Articles'."
1210   :group 'gnus-article-treat
1211   :link '(custom-manual "(gnus)Customizing Articles")
1212   :type gnus-article-treat-head-custom)
1213
1214 (defcustom gnus-treat-hide-boring-headers nil
1215   "Hide boring headers.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :group 'gnus-article-treat
1219   :link '(custom-manual "(gnus)Customizing Articles")
1220   :type gnus-article-treat-head-custom)
1221
1222 (defcustom gnus-treat-hide-signature nil
1223   "Hide the signature.
1224 Valid values are nil, t, `head', `first', `last', an integer or a
1225 predicate.  See Info node `(gnus)Customizing Articles'."
1226   :group 'gnus-article-treat
1227   :link '(custom-manual "(gnus)Customizing Articles")
1228   :type gnus-article-treat-custom)
1229
1230 (defcustom gnus-treat-fill-article nil
1231   "Fill the article.
1232 Valid values are nil, t, `head', `first', `last', an integer or a
1233 predicate.  See Info node `(gnus)Customizing Articles'."
1234   :group 'gnus-article-treat
1235   :link '(custom-manual "(gnus)Customizing Articles")
1236   :type gnus-article-treat-custom)
1237
1238 (defcustom gnus-treat-hide-citation nil
1239   "Hide cited text.
1240 Valid values are nil, t, `head', `first', `last', an integer or a
1241 predicate.  See Info node `(gnus)Customizing Articles'.
1242
1243 See `gnus-article-highlight-citation' for variables used to
1244 control what it hides."
1245   :group 'gnus-article-treat
1246   :link '(custom-manual "(gnus)Customizing Articles")
1247   :type gnus-article-treat-custom)
1248
1249 (defcustom gnus-treat-hide-citation-maybe nil
1250   "Hide cited text according to certain conditions.
1251 Valid values are nil, t, `head', `first', `last', an integer or a
1252 predicate.  See Info node `(gnus)Customizing Articles'.
1253
1254 See `gnus-cite-hide-percentage' and `gnus-cite-hide-absolute' for
1255 how to control what it hides."
1256   :group 'gnus-article-treat
1257   :link '(custom-manual "(gnus)Customizing Articles")
1258   :type gnus-article-treat-custom)
1259
1260 (defcustom gnus-treat-strip-list-identifiers 'head
1261   "Strip list identifiers from `gnus-list-identifiers`.
1262 Valid values are nil, t, `head', `first', `last', an integer or a
1263 predicate.  See Info node `(gnus)Customizing Articles'."
1264   :version "21.1"
1265   :group 'gnus-article-treat
1266   :link '(custom-manual "(gnus)Customizing Articles")
1267   :type gnus-article-treat-custom)
1268
1269 (gnus-define-group-parameter
1270  list-identifier
1271  :variable-document
1272  "Alist of regexps and correspondent identifiers."
1273  :variable-group gnus-article-washing
1274  :parameter-type
1275  '(choice :tag "Identifier"
1276           :value nil
1277           (symbol :tag "Item in `gnus-list-identifiers'" none)
1278           regexp
1279           (const :tag "None" nil))
1280  :parameter-document
1281  "If non-nil, specify how to remove `identifiers' from articles' subject.
1282
1283 Any symbol is used to look up a regular expression to match the
1284 banner in `gnus-list-identifiers'.  A string is used as a regular
1285 expression to match the identifier directly.")
1286
1287 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1288                         "Gnus 5.10 (Emacs 22.1)")
1289
1290 (defcustom gnus-treat-strip-pem nil
1291   "Strip PEM signatures.
1292 Valid values are nil, t, `head', `first', `last', an integer or a
1293 predicate.  See Info node `(gnus)Customizing Articles'."
1294   :group 'gnus-article-treat
1295   :link '(custom-manual "(gnus)Customizing Articles")
1296   :type gnus-article-treat-custom)
1297
1298 (defcustom gnus-treat-strip-banner t
1299   "Strip banners from articles.
1300 The banner to be stripped is specified in the `banner' group parameter.
1301 Valid values are nil, t, `head', `first', `last', an integer or a
1302 predicate.  See Info node `(gnus)Customizing Articles'."
1303   :group 'gnus-article-treat
1304   :link '(custom-manual "(gnus)Customizing Articles")
1305   :type gnus-article-treat-custom)
1306
1307 (defcustom gnus-treat-highlight-headers 'head
1308   "Highlight the headers.
1309 Valid values are nil, t, `head', `first', `last', an integer or a
1310 predicate.  See Info node `(gnus)Customizing Articles'."
1311   :group 'gnus-article-treat
1312   :link '(custom-manual "(gnus)Customizing Articles")
1313   :type gnus-article-treat-head-custom)
1314 (put 'gnus-treat-highlight-headers 'highlight t)
1315
1316 (defcustom gnus-treat-highlight-citation t
1317   "Highlight cited text.
1318 Valid values are nil, t, `head', `first', `last', an integer or a
1319 predicate.  See Info node `(gnus)Customizing Articles'."
1320   :group 'gnus-article-treat
1321   :link '(custom-manual "(gnus)Customizing Articles")
1322   :type gnus-article-treat-custom)
1323 (put 'gnus-treat-highlight-citation 'highlight t)
1324
1325 (defcustom gnus-treat-strip-headers-in-body t
1326   "Strip the X-No-Archive header line from the beginning of the body.
1327 Valid values are nil, t, `head', `first', `last', an integer or a
1328 predicate.  See Info node `(gnus)Customizing Articles'."
1329   :version "21.1"
1330   :group 'gnus-article-treat
1331   :link '(custom-manual "(gnus)Customizing Articles")
1332   :type gnus-article-treat-custom)
1333
1334 (defcustom gnus-treat-strip-trailing-blank-lines nil
1335   "Strip trailing blank lines.
1336 Valid values are nil, t, `head', `first', `last', an integer or a
1337 predicate.  See Info node `(gnus)Customizing Articles'.
1338
1339 When set to t, it also strips trailing blanks in all MIME parts.
1340 Consider to use `last' instead."
1341   :group 'gnus-article-treat
1342   :link '(custom-manual "(gnus)Customizing Articles")
1343   :type gnus-article-treat-custom)
1344
1345 (defcustom gnus-treat-strip-leading-blank-lines nil
1346   "Strip leading blank lines.
1347 Valid values are nil, t, `head', `first', `last', an integer or a
1348 predicate.  See Info node `(gnus)Customizing Articles'.
1349
1350 When set to t, it also strips trailing blanks in all MIME parts."
1351   :group 'gnus-article-treat
1352   :link '(custom-manual "(gnus)Customizing Articles")
1353   :type gnus-article-treat-custom)
1354
1355 (defcustom gnus-treat-strip-multiple-blank-lines nil
1356   "Strip multiple blank lines.
1357 Valid values are nil, t, `head', `first', `last', an integer or a
1358 predicate.  See Info node `(gnus)Customizing Articles'."
1359   :group 'gnus-article-treat
1360   :link '(custom-manual "(gnus)Customizing Articles")
1361   :type gnus-article-treat-custom)
1362
1363 (defcustom gnus-treat-unfold-headers 'head
1364   "Unfold folded header lines.
1365 Valid values are nil, t, `head', `first', `last', an integer or a
1366 predicate.  See Info node `(gnus)Customizing Articles'."
1367   :version "22.1"
1368   :group 'gnus-article-treat
1369   :link '(custom-manual "(gnus)Customizing Articles")
1370   :type gnus-article-treat-custom)
1371
1372 (defcustom gnus-article-unfold-long-headers nil
1373   "If non-nil, allow unfolding headers even if the header is long.
1374 If it is a regexp, only long headers matching this regexp are unfolded.
1375 If it is t, all long headers are unfolded.
1376
1377 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1378   :version "23.1" ;; No Gnus
1379   :group 'gnus-article-treat
1380   :type '(choice (const nil)
1381                  (const :tag "all" t)
1382                  (regexp)))
1383
1384 (defcustom gnus-treat-fold-headers nil
1385   "Fold headers.
1386 Valid values are nil, t, `head', `first', `last', an integer or a
1387 predicate.  See Info node `(gnus)Customizing Articles'."
1388   :version "22.1"
1389   :group 'gnus-article-treat
1390   :link '(custom-manual "(gnus)Customizing Articles")
1391   :type gnus-article-treat-custom)
1392
1393 (defcustom gnus-treat-fold-newsgroups 'head
1394   "Fold the Newsgroups and Followup-To headers.
1395 Valid values are nil, t, `head', `first', `last', an integer or a
1396 predicate.  See Info node `(gnus)Customizing Articles'."
1397   :version "22.1"
1398   :group 'gnus-article-treat
1399   :link '(custom-manual "(gnus)Customizing Articles")
1400   :type gnus-article-treat-custom)
1401
1402 (defcustom gnus-treat-overstrike t
1403   "Treat overstrike highlighting.
1404 Valid values are nil, t, `head', `first', `last', an integer or a
1405 predicate.  See Info node `(gnus)Customizing Articles'."
1406   :group 'gnus-article-treat
1407   :link '(custom-manual "(gnus)Customizing Articles")
1408   :type gnus-article-treat-custom)
1409 (put 'gnus-treat-overstrike 'highlight t)
1410
1411 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1412   "Treat ANSI SGR control sequences.
1413 Valid values are nil, t, `head', `first', `last', an integer or a
1414 predicate.  See Info node `(gnus)Customizing Articles'."
1415   :group 'gnus-article-treat
1416   :link '(custom-manual "(gnus)Customizing Articles")
1417   :type gnus-article-treat-custom)
1418
1419 (make-obsolete-variable 'gnus-treat-display-xface
1420                         'gnus-treat-display-x-face "Emacs 22.1")
1421
1422 (defcustom gnus-treat-display-x-face
1423   (and (not noninteractive)
1424        (gnus-image-type-available-p 'xbm)
1425        (if (featurep 'xemacs)
1426            (featurep 'xface)
1427          (condition-case nil
1428              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1429                   (executable-find "icontopbm"))
1430            ;; shell-command-to-string may signal an error, e.g. if
1431            ;; shell-file-name is not found.
1432            (error nil)))
1433        'head)
1434   "Display X-Face headers.
1435 Valid values are nil and `head'.
1436 See Info node `(gnus)Customizing Articles' and Info node
1437 `(gnus)X-Face' for details."
1438   :group 'gnus-article-treat
1439   :version "21.1"
1440   :link '(custom-manual "(gnus)Customizing Articles")
1441   :link '(custom-manual "(gnus)X-Face")
1442   :type gnus-article-treat-head-custom
1443   :set (lambda (symbol value)
1444          (set-default
1445           symbol
1446           (cond ((or (boundp symbol) (get symbol 'saved-value))
1447                  value)
1448                 ((boundp 'gnus-treat-display-xface)
1449                  (message "\
1450 ** gnus-treat-display-xface is an obsolete variable;\
1451  use gnus-treat-display-x-face instead")
1452                  (default-value 'gnus-treat-display-xface))
1453                 ((get 'gnus-treat-display-xface 'saved-value)
1454                  (message "\
1455 ** gnus-treat-display-xface is an obsolete variable;\
1456  use gnus-treat-display-x-face instead")
1457                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1458                 (t
1459                  value)))))
1460 (put 'gnus-treat-display-x-face 'highlight t)
1461
1462 (defcustom gnus-treat-display-face
1463   (and (not noninteractive)
1464        (gnus-image-type-available-p 'png)
1465        'head)
1466   "Display Face headers.
1467 Valid values are nil, t, `head', `first', `last', an integer or a
1468 predicate.  See Info node `(gnus)Customizing Articles' and Info
1469 node `(gnus)Face' for details."
1470   :group 'gnus-article-treat
1471   :version "22.1"
1472   :link '(custom-manual "(gnus)Customizing Articles")
1473   :link '(custom-manual "(gnus)X-Face")
1474   :type gnus-article-treat-head-custom)
1475 (put 'gnus-treat-display-face 'highlight t)
1476
1477 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1478   "Display smileys.
1479 Valid values are nil, t, `head', `first', `last', an integer or a
1480 predicate.  See Info node `(gnus)Customizing Articles' and Info
1481 node `(gnus)Smileys' for details."
1482   :group 'gnus-article-treat
1483   :version "21.1"
1484   :link '(custom-manual "(gnus)Customizing Articles")
1485   :link '(custom-manual "(gnus)Smileys")
1486   :type gnus-article-treat-custom)
1487 (put 'gnus-treat-display-smileys 'highlight t)
1488
1489 (defcustom gnus-treat-from-picon
1490   (if (and (gnus-image-type-available-p 'xpm)
1491            (gnus-picons-installed-p))
1492       'head nil)
1493   "Display picons in the From header.
1494 Valid values are nil, t, `head', `first', `last', an integer or a
1495 predicate.  See Info node `(gnus)Customizing Articles' and Info
1496 node `(gnus)Picons' for details."
1497   :version "22.1"
1498   :group 'gnus-article-treat
1499   :group 'gnus-picon
1500   :link '(custom-manual "(gnus)Customizing Articles")
1501   :link '(custom-manual "(gnus)Picons")
1502   :type gnus-article-treat-head-custom)
1503 (put 'gnus-treat-from-picon 'highlight t)
1504
1505 (defcustom gnus-treat-mail-picon
1506   (if (and (gnus-image-type-available-p 'xpm)
1507            (gnus-picons-installed-p))
1508       'head nil)
1509   "Display picons in To and Cc headers.
1510 Valid values are nil, t, `head', `first', `last', an integer or a
1511 predicate.  See Info node `(gnus)Customizing Articles' and Info
1512 node `(gnus)Picons' for details."
1513   :version "22.1"
1514   :group 'gnus-article-treat
1515   :group 'gnus-picon
1516   :link '(custom-manual "(gnus)Customizing Articles")
1517   :link '(custom-manual "(gnus)Picons")
1518   :type gnus-article-treat-head-custom)
1519 (put 'gnus-treat-mail-picon 'highlight t)
1520
1521 (defcustom gnus-treat-newsgroups-picon
1522   (if (and (gnus-image-type-available-p 'xpm)
1523            (gnus-picons-installed-p))
1524       'head nil)
1525   "Display picons in the Newsgroups and Followup-To headers.
1526 Valid values are nil, t, `head', `first', `last', an integer or a
1527 predicate.  See Info node `(gnus)Customizing Articles' and Info
1528 node `(gnus)Picons' for details."
1529   :version "22.1"
1530   :group 'gnus-article-treat
1531   :group 'gnus-picon
1532   :link '(custom-manual "(gnus)Customizing Articles")
1533   :link '(custom-manual "(gnus)Picons")
1534   :type gnus-article-treat-head-custom)
1535 (put 'gnus-treat-newsgroups-picon 'highlight t)
1536
1537 (defcustom gnus-treat-from-gravatar nil
1538   "Display gravatars in the From header.
1539 Valid values are nil, t, `head', `first', `last', an integer or a
1540 predicate.  See Info node `(gnus)Customizing Articles' and Info
1541 node `(gnus)Gravatars' for details."
1542   :version "24.1"
1543   :group 'gnus-article-treat
1544   :group 'gnus-gravatar
1545   :link '(custom-manual "(gnus)Customizing Articles")
1546   :link '(custom-manual "(gnus)Gravatars")
1547   :type gnus-article-treat-head-custom)
1548 (put 'gnus-treat-from-gravatar 'highlight t)
1549
1550 (defcustom gnus-treat-mail-gravatar nil
1551   "Display gravatars in To and Cc headers.
1552 Valid values are nil, t, `head', `first', `last', an integer or a
1553 predicate.  See Info node `(gnus)Customizing Articles' and Info
1554 node `(gnus)Gravatars' for details."
1555   :version "24.1"
1556   :group 'gnus-article-treat
1557   :group 'gnus-gravatar
1558   :link '(custom-manual "(gnus)Customizing Articles")
1559   :link '(custom-manual "(gnus)Gravatars")
1560   :type gnus-article-treat-head-custom)
1561 (put 'gnus-treat-mail-gravatar 'highlight t)
1562
1563 (defcustom gnus-treat-body-boundary
1564   (if (or gnus-treat-newsgroups-picon
1565           gnus-treat-mail-picon
1566           gnus-treat-from-picon
1567           gnus-treat-from-gravatar
1568           gnus-treat-mail-gravatar)
1569       ;; If there's much decoration, the user might prefer a boundary.
1570       'head
1571     nil)
1572   "Draw a boundary at the end of the headers.
1573 Valid values are nil and `head'.
1574 See Info node `(gnus)Customizing Articles' for details."
1575   :version "22.1"
1576   :group 'gnus-article-treat
1577   :link '(custom-manual "(gnus)Customizing Articles")
1578   :type gnus-article-treat-head-custom)
1579
1580 (defcustom gnus-treat-capitalize-sentences nil
1581   "Capitalize sentence-starting words.
1582 Valid values are nil, t, `head', `first', `last', an integer or a
1583 predicate.  See Info node `(gnus)Customizing Articles'."
1584   :version "21.1"
1585   :group 'gnus-article-treat
1586   :link '(custom-manual "(gnus)Customizing Articles")
1587   :type gnus-article-treat-custom)
1588
1589 (defcustom gnus-treat-wash-html nil
1590   "Format as HTML.
1591 Valid values are nil, t, `head', `first', `last', an integer or a
1592 predicate.  See Info node `(gnus)Customizing Articles'."
1593   :version "22.1"
1594   :group 'gnus-article-treat
1595   :link '(custom-manual "(gnus)Customizing Articles")
1596   :type gnus-article-treat-custom)
1597
1598 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1599   "Fill long lines.
1600 Valid values are nil, t, `head', `first', `last', an integer or a
1601 predicate.  See Info node `(gnus)Customizing Articles'."
1602   :version "24.1"
1603   :group 'gnus-article-treat
1604   :link '(custom-manual "(gnus)Customizing Articles")
1605   :type gnus-article-treat-custom)
1606
1607 (defcustom gnus-treat-x-pgp-sig nil
1608   "Verify X-PGP-Sig.
1609 To automatically treat X-PGP-Sig, set it to head.
1610 Valid values are nil, t, `head', `first', `last', an integer or a
1611 predicate.  See Info node `(gnus)Customizing Articles'."
1612   :version "22.1"
1613   :group 'gnus-article-treat
1614   :group 'mime-security
1615   :link '(custom-manual "(gnus)Customizing Articles")
1616   :type gnus-article-treat-custom)
1617
1618 (defvar gnus-article-encrypt-protocol-alist
1619   '(("PGP" . mml2015-self-encrypt)))
1620
1621 ;; Set to nil if more than one protocol added to
1622 ;; gnus-article-encrypt-protocol-alist.
1623 (defcustom gnus-article-encrypt-protocol "PGP"
1624   "The protocol used for encrypt articles.
1625 It is a string, such as \"PGP\". If nil, ask user."
1626   :version "22.1"
1627   :type 'string
1628   :group 'mime-security)
1629
1630 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1631                               (mm-coding-system-p 'utf-8)
1632                               (executable-find idna-program))
1633   "Whether IDNA decoding of headers is used when viewing messages.
1634 This requires GNU Libidn, and by default only enabled if it is found."
1635   :version "22.1"
1636   :group 'gnus-article-headers
1637   :type 'boolean)
1638
1639 (defcustom gnus-article-over-scroll nil
1640   "If non-nil, allow scrolling the article buffer even when there no more text."
1641   :version "22.1"
1642   :group 'gnus-article
1643   :type 'boolean)
1644
1645 (defcustom gnus-inhibit-images nil
1646   "Non-nil means inhibit displaying of images inline in the article body."
1647   :version "24.1"
1648   :group 'gnus-article
1649   :type 'boolean)
1650
1651 (defcustom gnus-blocked-images 'gnus-block-private-groups
1652   "Images that have URLs matching this regexp will be blocked.
1653 This can also be a function to be evaluated.  If so, it will be
1654 called with the group name as the parameter, and should return a
1655 regexp."
1656   :version "24.1"
1657   :group 'gnus-art
1658   :type '(choice regexp function))
1659
1660 ;;; Internal variables
1661
1662 (defvar gnus-english-month-names
1663   '("January" "February" "March" "April" "May" "June" "July" "August"
1664     "September" "October" "November" "December"))
1665
1666 (defvar article-goto-body-goes-to-point-min-p nil)
1667 (defvar gnus-article-wash-types nil)
1668 (defvar gnus-article-emphasis-alist nil)
1669 (defvar gnus-article-image-alist nil)
1670
1671 (defvar gnus-article-mime-handle-alist-1 nil)
1672 (defvar gnus-treatment-function-alist
1673   '((gnus-treat-strip-cr gnus-article-remove-cr)
1674     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1675     (gnus-treat-strip-banner gnus-article-strip-banner)
1676     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1677     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1678     (gnus-treat-buttonize gnus-article-add-buttons)
1679     (gnus-treat-fill-article gnus-article-fill-cited-article)
1680     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1681     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1682     (gnus-treat-display-x-face gnus-article-display-x-face)
1683     (gnus-treat-display-face gnus-article-display-face)
1684     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1685     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1686     (gnus-treat-hide-signature gnus-article-hide-signature)
1687     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1688     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1689     (gnus-treat-from-picon gnus-treat-from-picon)
1690     (gnus-treat-mail-picon gnus-treat-mail-picon)
1691     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1692     (gnus-treat-strip-pem gnus-article-hide-pem)
1693     (gnus-treat-date gnus-article-treat-date)
1694     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1695     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1696     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1697     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1698     (gnus-treat-strip-trailing-blank-lines
1699      gnus-article-remove-trailing-blank-lines)
1700     (gnus-treat-strip-leading-blank-lines
1701      gnus-article-strip-leading-blank-lines)
1702     (gnus-treat-strip-multiple-blank-lines
1703      gnus-article-strip-multiple-blank-lines)
1704     (gnus-treat-overstrike gnus-article-treat-overstrike)
1705     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1706     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1707     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1708     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1709     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1710     (gnus-treat-display-smileys gnus-treat-smiley)
1711     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1712     (gnus-treat-wash-html gnus-article-wash-html)
1713     (gnus-treat-emphasize gnus-article-emphasize)
1714     (gnus-treat-hide-citation gnus-article-hide-citation)
1715     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1716     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1717     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1718
1719 (defvar gnus-article-mime-handle-alist nil)
1720 (defvar article-lapsed-timer nil)
1721 (defvar gnus-article-current-summary nil)
1722
1723 (defvar gnus-article-mode-syntax-table
1724   (let ((table (copy-syntax-table text-mode-syntax-table)))
1725     ;; This causes the citation match run O(2^n).
1726     ;; (modify-syntax-entry ?- "w" table)
1727     (modify-syntax-entry ?> ")<" table)
1728     (modify-syntax-entry ?< "(>" table)
1729     ;; make M-. in article buffers work for `foo' strings
1730     (modify-syntax-entry ?' " " table)
1731     (modify-syntax-entry ?` " " table)
1732     table)
1733   "Syntax table used in article mode buffers.
1734 Initialized from `text-mode-syntax-table.")
1735
1736 (defvar gnus-save-article-buffer nil)
1737
1738 (defvar gnus-number-of-articles-to-be-saved nil)
1739
1740 (defvar gnus-inhibit-hiding nil)
1741
1742 (defvar gnus-article-edit-mode nil)
1743
1744 ;;; Macros for dealing with the article buffer.
1745
1746 (defmacro gnus-with-article-headers (&rest forms)
1747   `(with-current-buffer gnus-article-buffer
1748      (save-restriction
1749        (let ((inhibit-read-only t)
1750              (inhibit-point-motion-hooks t)
1751              (case-fold-search t))
1752          (article-narrow-to-head)
1753          ,@forms))))
1754
1755 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1756 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1757
1758 (defmacro gnus-with-article-buffer (&rest forms)
1759   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1760      (with-current-buffer gnus-article-buffer
1761        (let ((inhibit-read-only t))
1762          ,@forms))))
1763
1764 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1765 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1766
1767 (defun gnus-article-goto-header (header)
1768   "Go to HEADER, which is a regular expression."
1769   (re-search-forward (concat "^\\(" header "\\):") nil t))
1770
1771 (defsubst gnus-article-hide-text (b e props)
1772   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1773   (gnus-add-text-properties-when 'article-type nil b e props)
1774   (when (memq 'intangible props)
1775     (put-text-property
1776      (max (1- b) (point-min))
1777      b 'intangible (cddr (memq 'intangible props)))))
1778
1779 (defsubst gnus-article-unhide-text (b e)
1780   "Remove hidden text properties from region between B and E."
1781   (remove-text-properties b e gnus-hidden-properties)
1782   (when (memq 'intangible gnus-hidden-properties)
1783     (put-text-property (max (1- b) (point-min))
1784                        b 'intangible nil)))
1785
1786 (defun gnus-article-hide-text-type (b e type)
1787   "Hide text of TYPE between B and E."
1788   (gnus-add-wash-type type)
1789   (gnus-article-hide-text
1790    b e (cons 'article-type (cons type gnus-hidden-properties))))
1791
1792 (defun gnus-article-unhide-text-type (b e type)
1793   "Unhide text of TYPE between B and E."
1794   (gnus-delete-wash-type type)
1795   (remove-text-properties
1796    b e (cons 'article-type (cons type gnus-hidden-properties)))
1797   (when (memq 'intangible gnus-hidden-properties)
1798     (put-text-property (max (1- b) (point-min))
1799                        b 'intangible nil)))
1800
1801 (defun gnus-article-delete-text-of-type (type)
1802   "Delete text of TYPE in the current buffer."
1803   (save-excursion
1804     (let ((b (point-min)))
1805       (if (eq type 'multipart)
1806           ;; Remove MIME buttons associated with multipart/alternative parts.
1807           (progn
1808             (goto-char b)
1809             (while (if (get-text-property (point) 'gnus-part)
1810                        (setq b (point))
1811                      (when (setq b (next-single-property-change (point)
1812                                                                 'gnus-part))
1813                        (goto-char b)
1814                        t))
1815               (end-of-line)
1816               (skip-chars-forward "\n")
1817               (when (eq (get-text-property b 'article-type) 'multipart)
1818                 (delete-region b (point)))))
1819         (while (setq b (text-property-any b (point-max) 'article-type type))
1820           (delete-region
1821            b (or (text-property-not-all b (point-max) 'article-type type)
1822                  (point-max))))))))
1823
1824 (defun gnus-article-delete-invisible-text ()
1825   "Delete all invisible text in the current buffer."
1826   (save-excursion
1827     (let ((b (point-min)))
1828       (while (setq b (text-property-any b (point-max) 'invisible t))
1829         (delete-region
1830          b (or (text-property-not-all b (point-max) 'invisible t)
1831                (point-max)))))))
1832
1833 (defsubst gnus-article-header-rank ()
1834   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1835   (let ((list gnus-sorted-header-list)
1836         (i 1))
1837     (while list
1838       (if (looking-at (car list))
1839           (setq list nil)
1840         (setq list (cdr list))
1841         (incf i)))
1842       i))
1843
1844 (defun article-hide-headers (&optional arg delete)
1845   "Hide unwanted headers and possibly sort them as well."
1846   (interactive)
1847   ;; This function might be inhibited.
1848   (unless gnus-inhibit-hiding
1849     (let ((inhibit-read-only t)
1850           (case-fold-search t)
1851           (max (1+ (length gnus-sorted-header-list)))
1852           (inhibit-point-motion-hooks t)
1853           (cur (current-buffer))
1854           ignored visible beg)
1855       (save-excursion
1856         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1857         ;; group parameters, so we should go to the summary buffer.
1858         (when (prog1
1859                   (condition-case nil
1860                       (progn (set-buffer gnus-summary-buffer) t)
1861                     (error nil))
1862                 (setq ignored (when (not gnus-visible-headers)
1863                                 (cond ((stringp gnus-ignored-headers)
1864                                        gnus-ignored-headers)
1865                                       ((listp gnus-ignored-headers)
1866                                        (mapconcat 'identity
1867                                                   gnus-ignored-headers
1868                                                   "\\|"))))
1869                       visible (cond ((stringp gnus-visible-headers)
1870                                      gnus-visible-headers)
1871                                     ((and gnus-visible-headers
1872                                           (listp gnus-visible-headers))
1873                                      (mapconcat 'identity
1874                                                 gnus-visible-headers
1875                                                 "\\|")))))
1876           (set-buffer cur))
1877         (save-restriction
1878           ;; First we narrow to just the headers.
1879           (article-narrow-to-head)
1880           ;; Hide any "From " lines at the beginning of (mail) articles.
1881           (while (looking-at "From ")
1882             (forward-line 1))
1883           (unless (bobp)
1884             (delete-region (point-min) (point)))
1885           ;; Then treat the rest of the header lines.
1886           ;; Then we use the two regular expressions
1887           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1888           ;; select which header lines is to remain visible in the
1889           ;; article buffer.
1890           (while (re-search-forward "^[^ \t:]*:" nil t)
1891             (beginning-of-line)
1892             ;; Mark the rank of the header.
1893             (put-text-property
1894              (point) (1+ (point)) 'message-rank
1895              (if (or (and visible (looking-at visible))
1896                      (and ignored
1897                           (not (looking-at ignored))))
1898                  (gnus-article-header-rank)
1899                (+ 2 max)))
1900             (forward-line 1))
1901           (message-sort-headers-1)
1902           (when (setq beg (text-property-any
1903                            (point-min) (point-max) 'message-rank (+ 2 max)))
1904             ;; We delete the unwanted headers.
1905             (gnus-add-wash-type 'headers)
1906             (add-text-properties (point-min) (+ 5 (point-min))
1907                                  '(article-type headers dummy-invisible t))
1908             (delete-region beg (point-max))))))))
1909
1910 (defun article-hide-boring-headers (&optional arg)
1911   "Toggle hiding of headers that aren't very interesting.
1912 If given a negative prefix, always show; if given a positive prefix,
1913 always hide."
1914   (interactive (gnus-article-hidden-arg))
1915   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1916              (not gnus-show-all-headers))
1917     (save-excursion
1918       (save-restriction
1919         (let ((inhibit-read-only t)
1920               (inhibit-point-motion-hooks t))
1921           (article-narrow-to-head)
1922           (dolist (elem gnus-boring-article-headers)
1923             (goto-char (point-min))
1924             (cond
1925              ;; Hide empty headers.
1926              ((eq elem 'empty)
1927               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1928                 (forward-line -1)
1929                 (gnus-article-hide-text-type
1930                  (point-at-bol)
1931                  (progn
1932                    (end-of-line)
1933                    (if (re-search-forward "^[^ \t]" nil t)
1934                        (match-beginning 0)
1935                      (point-max)))
1936                  'boring-headers)))
1937              ;; Hide boring Newsgroups header.
1938              ((eq elem 'newsgroups)
1939               (when (gnus-string-equal
1940                      (gnus-fetch-field "newsgroups")
1941                      (gnus-group-real-name
1942                       (if (boundp 'gnus-newsgroup-name)
1943                           gnus-newsgroup-name
1944                         "")))
1945                 (gnus-article-hide-header "newsgroups")))
1946              ((eq elem 'to-address)
1947               (let ((to (message-fetch-field "to"))
1948                     (to-address
1949                      (gnus-parameter-to-address
1950                       (if (boundp 'gnus-newsgroup-name)
1951                           gnus-newsgroup-name ""))))
1952                 (when (and to to-address
1953                            (ignore-errors
1954                              (gnus-string-equal
1955                               ;; only one address in To
1956                               (nth 1 (mail-extract-address-components to))
1957                               to-address)))
1958                   (gnus-article-hide-header "to"))))
1959              ((eq elem 'to-list)
1960               (let ((to (message-fetch-field "to"))
1961                     (to-list
1962                      (gnus-parameter-to-list
1963                       (if (boundp 'gnus-newsgroup-name)
1964                           gnus-newsgroup-name ""))))
1965                 (when (and to to-list
1966                            (ignore-errors
1967                              (gnus-string-equal
1968                               ;; only one address in To
1969                               (nth 1 (mail-extract-address-components to))
1970                               to-list)))
1971                   (gnus-article-hide-header "to"))))
1972              ((eq elem 'cc-list)
1973               (let ((cc (message-fetch-field "cc"))
1974                     (to-list
1975                      (gnus-parameter-to-list
1976                       (if (boundp 'gnus-newsgroup-name)
1977                           gnus-newsgroup-name ""))))
1978                 (when (and cc to-list
1979                            (ignore-errors
1980                              (gnus-string-equal
1981                               ;; only one address in CC
1982                               (nth 1 (mail-extract-address-components cc))
1983                               to-list)))
1984                   (gnus-article-hide-header "cc"))))
1985              ((eq elem 'followup-to)
1986               (when (gnus-string-equal
1987                      (message-fetch-field "followup-to")
1988                      (message-fetch-field "newsgroups"))
1989                 (gnus-article-hide-header "followup-to")))
1990              ((eq elem 'reply-to)
1991               (if (gnus-group-find-parameter
1992                    gnus-newsgroup-name 'broken-reply-to)
1993                   (gnus-article-hide-header "reply-to")
1994                 (let ((from (message-fetch-field "from"))
1995                       (reply-to (message-fetch-field "reply-to")))
1996                   (when
1997                       (and
1998                        from reply-to
1999                        (ignore-errors
2000                          (equal
2001                           (sort (mapcar
2002                                  (lambda (x) (downcase (cadr x)))
2003                                  (mail-extract-address-components from t))
2004                                 'string<)
2005                           (sort (mapcar
2006                                  (lambda (x) (downcase (cadr x)))
2007                                  (mail-extract-address-components reply-to t))
2008                                 'string<))))
2009                     (gnus-article-hide-header "reply-to")))))
2010              ((eq elem 'date)
2011               (let ((date (with-current-buffer gnus-original-article-buffer
2012                             ;; If date in `gnus-article-buffer' is localized
2013                             ;; (`gnus-treat-date-user-defined'),
2014                             ;; `days-between' might fail.
2015                             (message-fetch-field "date"))))
2016                 (when (and date
2017                            (< (days-between (current-time-string) date)
2018                               4))
2019                   (gnus-article-hide-header "date"))))
2020              ((eq elem 'long-to)
2021               (let ((to (message-fetch-field "to"))
2022                     (cc (message-fetch-field "cc")))
2023                 (when (> (length to) 1024)
2024                   (gnus-article-hide-header "to"))
2025                 (when (> (length cc) 1024)
2026                   (gnus-article-hide-header "cc"))))
2027              ((eq elem 'many-to)
2028               (let ((to-count 0)
2029                     (cc-count 0))
2030                 (goto-char (point-min))
2031                 (while (re-search-forward "^to:" nil t)
2032                   (setq to-count (1+ to-count)))
2033                 (when (> to-count 1)
2034                   (while (> to-count 0)
2035                     (goto-char (point-min))
2036                     (save-restriction
2037                       (re-search-forward "^to:" nil nil to-count)
2038                       (forward-line -1)
2039                       (narrow-to-region (point) (point-max))
2040                       (gnus-article-hide-header "to"))
2041                     (setq to-count (1- to-count))))
2042                 (goto-char (point-min))
2043                 (while (re-search-forward "^cc:" nil t)
2044                   (setq cc-count (1+ cc-count)))
2045                 (when (> cc-count 1)
2046                   (while (> cc-count 0)
2047                     (goto-char (point-min))
2048                     (save-restriction
2049                       (re-search-forward "^cc:" nil nil cc-count)
2050                       (forward-line -1)
2051                       (narrow-to-region (point) (point-max))
2052                       (gnus-article-hide-header "cc"))
2053                     (setq cc-count (1- cc-count)))))))))))))
2054
2055 (defun gnus-article-hide-header (header)
2056   (save-excursion
2057     (goto-char (point-min))
2058     (when (re-search-forward (concat "^" header ":") nil t)
2059       (gnus-article-hide-text-type
2060        (point-at-bol)
2061        (progn
2062          (end-of-line)
2063          (if (re-search-forward "^[^ \t]" nil t)
2064              (match-beginning 0)
2065            (point-max)))
2066        'boring-headers))))
2067
2068 (defvar gnus-article-normalized-header-length 40
2069   "Length of normalized headers.")
2070
2071 (defun article-normalize-headers ()
2072   "Make all header lines 40 characters long."
2073   (interactive)
2074   (let ((inhibit-read-only t)
2075         column)
2076     (save-excursion
2077       (save-restriction
2078         (article-narrow-to-head)
2079         (while (not (eobp))
2080           (cond
2081            ((< (setq column (- (point-at-eol) (point)))
2082                gnus-article-normalized-header-length)
2083             (end-of-line)
2084             (insert (make-string
2085                      (- gnus-article-normalized-header-length column)
2086                      ? )))
2087            ((> column gnus-article-normalized-header-length)
2088             (gnus-put-text-property
2089              (progn
2090                (forward-char gnus-article-normalized-header-length)
2091                (point))
2092              (point-at-eol)
2093              'invisible t))
2094            (t
2095             ;; Do nothing.
2096             ))
2097           (forward-line 1))))))
2098
2099 (defun article-treat-dumbquotes ()
2100   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2101 Note that this function guesses whether a character is a sm*rtq**t* or
2102 not, so it should only be used interactively.
2103
2104 Sm*rtq**t*s are M****s***'s unilateral extension to the
2105 iso-8859-1 character map in an attempt to provide more quoting
2106 characters.  If you see something like \\222 or \\264 where
2107 you're expecting some kind of apostrophe or quotation mark, then
2108 try this wash."
2109   (interactive)
2110   (article-translate-strings gnus-article-dumbquotes-map))
2111
2112 (defvar org-entities)
2113
2114 (defun article-treat-non-ascii ()
2115   "Translate many Unicode characters into their ASCII equivalents."
2116   (interactive)
2117   (require 'org-entities)
2118   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2119     (dolist (elem org-entities)
2120       (when (and (listp elem)
2121                  (= (length (nth 6 elem)) 1))
2122         (if (featurep 'xemacs)
2123             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2124           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2125     (save-excursion
2126       (when (article-goto-body)
2127         (let ((inhibit-read-only t)
2128               replace props)
2129           (while (not (eobp))
2130             (if (not (setq replace (if (featurep 'xemacs)
2131                                        (get-char-table (following-char) table)
2132                                      (aref table (following-char)))))
2133                 (forward-char 1)
2134               (if (prog1
2135                       (setq props (text-properties-at (point)))
2136                     (delete-char 1))
2137                   (add-text-properties (point) (progn (insert replace) (point))
2138                                        props)
2139                 (insert replace)))))))))
2140
2141 (defun article-translate-strings (map)
2142   "Translate all string in the body of the article according to MAP.
2143 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2144   (save-excursion
2145     (when (article-goto-body)
2146       (let ((inhibit-read-only t))
2147         (dolist (elem map)
2148           (let ((from (car elem))
2149                 (to (cadr elem)))
2150             (save-excursion
2151               (if (stringp from)
2152                   (while (search-forward from nil t)
2153                     (replace-match to))
2154                 (while (not (eobp))
2155                   (if (eq (following-char) from)
2156                       (progn
2157                         (delete-char 1)
2158                         (insert to))
2159                     (forward-char 1)))))))))))
2160
2161 (defun article-treat-overstrike ()
2162   "Translate overstrikes into bold text."
2163   (interactive)
2164   (save-excursion
2165     (when (article-goto-body)
2166       (let ((inhibit-read-only t))
2167         (while (search-forward "\b" nil t)
2168           (let ((next (char-after))
2169                 (previous (char-after (- (point) 2))))
2170             ;; We do the boldification/underlining by hiding the
2171             ;; overstrikes and putting the proper text property
2172             ;; on the letters.
2173             (cond
2174              ((eq next previous)
2175               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2176               (put-text-property (point) (1+ (point)) 'face 'bold))
2177              ((eq next ?_)
2178               (gnus-article-hide-text-type
2179                (1- (point)) (1+ (point)) 'overstrike)
2180               (put-text-property
2181                (- (point) 2) (1- (point)) 'face 'underline))
2182              ((eq previous ?_)
2183               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2184               (put-text-property
2185                (point) (1+ (point)) 'face 'underline)))))))))
2186
2187 (defun article-treat-ansi-sequences ()
2188   "Translate ANSI SGR control sequences into overlays or extents."
2189   (interactive)
2190   (save-excursion
2191     (when (article-goto-body)
2192       (let ((inhibit-read-only t))
2193         (ansi-color-apply-on-region (point) (point-max))))))
2194
2195 (defun gnus-article-treat-unfold-headers ()
2196   "Unfold folded message headers.
2197 Only the headers that fit into the current window width will be
2198 unfolded."
2199   (interactive)
2200   (gnus-with-article-headers
2201     (let (length)
2202       (while (not (eobp))
2203         (save-restriction
2204           (mail-header-narrow-to-field)
2205           (let* ((header (buffer-string))
2206                  (unfoldable
2207                   (or (equal gnus-article-unfold-long-headers t)
2208                       (and (stringp gnus-article-unfold-long-headers)
2209                            (string-match gnus-article-unfold-long-headers
2210                                          header)))))
2211             (with-temp-buffer
2212               (insert header)
2213               (goto-char (point-min))
2214               (while (re-search-forward "\n[\t ]" nil t)
2215                 (replace-match " " t t)))
2216             (setq length (- (point-max) (point-min) 1))
2217             (when (or unfoldable
2218                       (< length (window-width)))
2219               (while (re-search-forward "\n[\t ]" nil t)
2220                 (replace-match " " t t))))
2221           (goto-char (point-max)))))))
2222
2223 (defun gnus-article-treat-fold-headers ()
2224   "Fold message headers."
2225   (interactive)
2226   (gnus-with-article-headers
2227     (while (not (eobp))
2228       (save-restriction
2229         (mail-header-narrow-to-field)
2230         (mail-header-fold-field)
2231         (goto-char (point-max))))))
2232
2233 (defun gnus-treat-smiley ()
2234   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2235   (interactive)
2236   (gnus-with-article-buffer
2237     (if (memq 'smiley gnus-article-wash-types)
2238         (gnus-delete-images 'smiley)
2239       (article-goto-body)
2240       (let ((images (smiley-region (point) (point-max))))
2241         (when images
2242           (gnus-add-wash-type 'smiley)
2243           (dolist (image images)
2244             (gnus-add-image 'smiley image)))))))
2245
2246 (defun gnus-article-remove-images ()
2247   "Remove all images from the article buffer."
2248   (interactive)
2249   (gnus-with-article-buffer
2250     (save-restriction
2251       (widen)
2252       (dolist (elem gnus-article-image-alist)
2253         (gnus-delete-images (car elem))))))
2254
2255 (autoload 'w3m-toggle-inline-images "w3m")
2256
2257 (defun gnus-article-show-images ()
2258   "Show any images that are in the HTML-rendered article buffer.
2259 This only works if the article in question is HTML."
2260   (interactive)
2261   (gnus-with-article-buffer
2262     (save-restriction
2263       (widen)
2264       (if (eq mm-text-html-renderer 'w3m)
2265           (let ((mm-inline-text-html-with-images nil))
2266             (w3m-toggle-inline-images))
2267         (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2268                                                         'image-displayer))
2269           (destructuring-bind (start end function) region
2270             (funcall function (get-text-property start 'image-url)
2271                      start end)))))))
2272
2273 (defun gnus-article-treat-fold-newsgroups ()
2274   "Unfold folded message headers.
2275 Only the headers that fit into the current window width will be
2276 unfolded."
2277   (interactive)
2278   (gnus-with-article-headers
2279     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2280       (save-restriction
2281         (mail-header-narrow-to-field)
2282         (while (re-search-forward ", *" nil t)
2283           (replace-match ", " t t))
2284         (mail-header-fold-field)
2285         (goto-char (point-max))))))
2286
2287 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2288   "Value of `truncate-lines' in Gnus Article buffer.
2289 Valid values are nil, t, `head', `first', `last', an integer or a
2290 predicate.  See Info node `(gnus)Customizing Articles'."
2291   :version "23.1" ;; No Gnus
2292   :group 'gnus-article
2293   ;; :link '(custom-manual "(gnus)Customizing Articles")
2294   :type 'boolean)
2295
2296 (defun gnus-article-toggle-truncate-lines (&optional arg)
2297   "Toggle whether to fold or truncate long lines in article the buffer.
2298 If ARG is non-nil and not a number, toggle
2299 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2300 long lines if and only if arg is positive."
2301   (interactive "P")
2302   (cond
2303    ((and (numberp arg) (> arg 0))
2304     (setq gnus-article-truncate-lines t))
2305    ((numberp arg)
2306     (setq gnus-article-truncate-lines nil))
2307    (arg
2308     (setq gnus-article-truncate-lines
2309           (not gnus-article-truncate-lines))))
2310   (gnus-with-article-buffer
2311     (cond
2312      ((and (numberp arg) (> arg 0))
2313       (setq truncate-lines nil))
2314      ((numberp arg)
2315       (setq truncate-lines t)))
2316     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2317     ;; `toggle-truncate-lines' needs an argument.
2318     (toggle-truncate-lines)))
2319
2320 (defun gnus-article-treat-body-boundary ()
2321   "Place a boundary line at the end of the headers."
2322   (interactive)
2323   (when (and gnus-body-boundary-delimiter
2324              (> (length gnus-body-boundary-delimiter) 0))
2325     (gnus-with-article-headers
2326       (goto-char (point-max))
2327       (let ((start (point)))
2328         (insert "X-Boundary: ")
2329         (gnus-add-text-properties start (point) '(invisible t intangible t))
2330        (insert (let (str (max (window-width)))
2331                  (if (featurep 'xemacs)
2332                      (setq max (1- max)))
2333                  (while (>= max (length str))
2334                     (setq str (concat str gnus-body-boundary-delimiter)))
2335                  (substring str 0 max))
2336                 "\n")
2337         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2338
2339 (defun article-fill-long-lines ()
2340   "Fill lines that are wider than the window width."
2341   (interactive)
2342   (save-excursion
2343     (let ((inhibit-read-only t)
2344           (width (window-width (get-buffer-window (current-buffer)))))
2345       (save-restriction
2346         (article-goto-body)
2347         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2348           (while (not (eobp))
2349             (end-of-line)
2350             (when (>= (current-column) (min fill-column width))
2351               (narrow-to-region (min (1+ (point)) (point-max))
2352                                 (point-at-bol))
2353               (let ((goback (point-marker)))
2354                 (fill-paragraph nil)
2355                 (goto-char (marker-position goback)))
2356               (widen))
2357             (forward-line 1)))))))
2358
2359 (defun article-capitalize-sentences ()
2360   "Capitalize the first word in each sentence."
2361   (interactive)
2362   (save-excursion
2363     (let ((inhibit-read-only t)
2364           (paragraph-start "^[\n\^L]"))
2365       (article-goto-body)
2366       (while (not (eobp))
2367         (capitalize-word 1)
2368         (forward-sentence)))))
2369
2370 (defun article-remove-cr ()
2371   "Remove trailing CRs and then translate remaining CRs into LFs."
2372   (interactive)
2373   (save-excursion
2374     (let ((inhibit-read-only t))
2375       (goto-char (point-min))
2376       (while (re-search-forward "\r+$" nil t)
2377         (replace-match "" t t))
2378       (goto-char (point-min))
2379       (while (search-forward "\r" nil t)
2380         (replace-match "\n" t t)))))
2381
2382 (defun article-remove-trailing-blank-lines ()
2383   "Remove all trailing blank lines from the article."
2384   (interactive)
2385   (save-excursion
2386     (let ((inhibit-read-only t))
2387       (goto-char (point-max))
2388       (delete-region
2389        (point)
2390        (progn
2391          (while (and (not (bobp))
2392                      (looking-at "^[ \t]*$")
2393                      (not (gnus-annotation-in-region-p
2394                            (point) (point-at-eol))))
2395            (forward-line -1))
2396          (forward-line 1)
2397          (point))))))
2398
2399 (defvar gnus-face-properties-alist)
2400
2401 (defun article-display-face (&optional force)
2402   "Display any Face headers in the header."
2403   (interactive (list 'force))
2404   (let ((wash-face-p buffer-read-only))
2405     (gnus-with-article-headers
2406       ;; When displaying parts, this function can be called several times on
2407       ;; the same article, without any intended toggle semantic (as typing `W
2408       ;; D d' would have). So face deletion must occur only when we come from
2409       ;; an interactive command, that is when the *Article* buffer is
2410       ;; read-only.
2411       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2412           (gnus-delete-images 'face)
2413         (let ((from (message-fetch-field "from"))
2414               face faces)
2415           (save-current-buffer
2416             (when (and wash-face-p
2417                        (gnus-buffer-live-p gnus-original-article-buffer)
2418                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2419               (set-buffer gnus-original-article-buffer))
2420             (save-restriction
2421               (mail-narrow-to-head)
2422               (when (or force
2423                         ;; Check whether this face is censored.
2424                         (not (and gnus-article-x-face-too-ugly
2425                                   (or from
2426                                       (setq from (message-fetch-field "from")))
2427                                   (string-match gnus-article-x-face-too-ugly
2428                                                 from))))
2429                 (while (gnus-article-goto-header "Face")
2430                   (push (mail-header-field-value) faces)))))
2431           (when faces
2432             (goto-char (point-min))
2433             (let (png image)
2434               (unless (setq from (gnus-article-goto-header "from"))
2435                 (insert "From:")
2436                 (setq from (point))
2437                 (insert " [no `from' set]\n"))
2438               (while faces
2439                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2440                   (setq image
2441                         (apply 'gnus-create-image png 'png t
2442                                (cdr (assq 'png gnus-face-properties-alist))))
2443                   (goto-char from)
2444                   (when image
2445                     (gnus-add-wash-type 'face)
2446                     (gnus-add-image 'face image)
2447                     (gnus-put-image image nil 'face)))))))))))
2448
2449 (defun article-display-x-face (&optional force)
2450   "Look for an X-Face header and display it if present."
2451   (interactive (list 'force))
2452   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2453     (gnus-with-article-headers
2454       ;; Delete the old process, if any.
2455       (when (process-status "article-x-face")
2456         (delete-process "article-x-face"))
2457       ;; See the comment in `article-display-face'.
2458       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2459           ;; We have already displayed X-Faces, so we remove them
2460           ;; instead.
2461           (gnus-delete-images 'xface)
2462         ;; Display X-Faces.
2463         (let ((from (message-fetch-field "from"))
2464               x-faces face)
2465           (save-current-buffer
2466             (when (and wash-face-p
2467                        (gnus-buffer-live-p gnus-original-article-buffer)
2468                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2469               ;; If type `W f', use gnus-original-article-buffer,
2470               ;; otherwise use the current buffer because displaying
2471               ;; RFC822 parts calls this function too.
2472               (set-buffer gnus-original-article-buffer))
2473             (save-restriction
2474               (mail-narrow-to-head)
2475               (and gnus-article-x-face-command
2476                    (or force
2477                        ;; Check whether this face is censored.
2478                        (not (and gnus-article-x-face-too-ugly
2479                                  (or from
2480                                      (setq from (message-fetch-field "from")))
2481                                  (string-match gnus-article-x-face-too-ugly
2482                                                from))))
2483                    (while (gnus-article-goto-header "X-Face")
2484                      (push (mail-header-field-value) x-faces)))))
2485           (when x-faces
2486             ;; We display the face.
2487             (cond ((functionp gnus-article-x-face-command)
2488                    ;; The command is a lisp function, so we call it.
2489                    (mapc gnus-article-x-face-command x-faces))
2490                   ((stringp gnus-article-x-face-command)
2491                    ;; The command is a string, so we interpret the command
2492                    ;; as a, well, command, and fork it off.
2493                    (let ((process-connection-type nil))
2494                      (gnus-set-process-query-on-exit-flag
2495                       (start-process
2496                        "article-x-face" nil shell-file-name
2497                        shell-command-switch gnus-article-x-face-command)
2498                       nil)
2499                      ;; Sending multiple EOFs to xv doesn't work,
2500                      ;; so we only do a single external face.
2501                      (with-temp-buffer
2502                        (insert (car x-faces))
2503                        (process-send-region "article-x-face"
2504                                             (point-min) (point-max)))
2505                      (process-send-eof "article-x-face")))
2506                   (t
2507                    (error "`%s' set to `%s' is not a function"
2508                           gnus-article-x-face-command
2509                           'gnus-article-x-face-command)))))))))
2510
2511 (defun article-decode-mime-words ()
2512   "Decode all MIME-encoded words in the article."
2513   (interactive)
2514   (gnus-with-article-buffer
2515     (let ((inhibit-point-motion-hooks t)
2516           (mail-parse-charset gnus-newsgroup-charset)
2517           (mail-parse-ignored-charsets
2518            (with-current-buffer gnus-summary-buffer
2519              gnus-newsgroup-ignored-charsets)))
2520       (mail-decode-encoded-word-region (point-min) (point-max)))))
2521
2522 (defun article-decode-charset (&optional prompt)
2523   "Decode charset-encoded text in the article.
2524 If PROMPT (the prefix), prompt for a coding system to use."
2525   (interactive "P")
2526   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2527         (inhibit-read-only t)
2528         (mail-parse-charset gnus-newsgroup-charset)
2529         (mail-parse-ignored-charsets
2530          (save-excursion (condition-case nil
2531                              (set-buffer gnus-summary-buffer)
2532                            (error))
2533                          gnus-newsgroup-ignored-charsets))
2534         ct cte ctl charset format)
2535     (save-excursion
2536       (save-restriction
2537         (article-narrow-to-head)
2538         (setq ct (message-fetch-field "Content-Type" t)
2539               cte (message-fetch-field "Content-Transfer-Encoding" t)
2540               ctl (and ct (mail-header-parse-content-type ct))
2541               charset (cond
2542                        (prompt
2543                         (mm-read-coding-system "Charset to decode: "))
2544                        (ctl
2545                         (mail-content-type-get ctl 'charset)))
2546               format (and ctl (mail-content-type-get ctl 'format)))
2547         (when cte
2548           (setq cte (mail-header-strip cte)))
2549         (if (and ctl (not (string-match "/" (car ctl))))
2550             (setq ctl nil))
2551         (goto-char (point-max)))
2552       (forward-line 1)
2553       (save-restriction
2554         (narrow-to-region (point) (point-max))
2555         (when (and (eq mail-parse-charset 'gnus-decoded)
2556                    (eq (mm-body-7-or-8) '8bit))
2557           ;; The text code could have been decoded.
2558           (setq charset mail-parse-charset))
2559         (when (and (or (not ctl)
2560                        (equal (car ctl) "text/plain"))
2561                    (not format)) ;; article with format will decode later.
2562           (mm-decode-body
2563            charset (and cte (intern (downcase
2564                                      (gnus-strip-whitespace cte))))
2565            (car ctl)))))))
2566
2567 (defun article-decode-encoded-words ()
2568   "Remove encoded-word encoding from headers."
2569   (let ((inhibit-point-motion-hooks t)
2570         (mail-parse-charset gnus-newsgroup-charset)
2571         (mail-parse-ignored-charsets
2572          (save-excursion (condition-case nil
2573                              (set-buffer gnus-summary-buffer)
2574                            (error))
2575                          gnus-newsgroup-ignored-charsets))
2576         (inhibit-read-only t)
2577         end start)
2578     (goto-char (point-min))
2579     (when (search-forward "\n\n" nil 'move)
2580       (forward-line -1))
2581     (setq end (point))
2582     (while (not (bobp))
2583       (while (progn
2584                (forward-line -1)
2585                (and (not (bobp))
2586                     (memq (char-after) '(?\t ? )))))
2587       (setq start (point))
2588       (if (looking-at "\
2589 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2590 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2591           (funcall gnus-decode-address-function start end)
2592         (funcall gnus-decode-header-function start end))
2593       (goto-char (setq end start)))))
2594
2595 (defun article-decode-group-name ()
2596   "Decode group names in Newsgroups, Followup-To and Xref headers."
2597   (let ((inhibit-point-motion-hooks t)
2598         (inhibit-read-only t)
2599         (method (gnus-find-method-for-group gnus-newsgroup-name))
2600         regexp)
2601     (when (and (or gnus-group-name-charset-method-alist
2602                    gnus-group-name-charset-group-alist)
2603                (gnus-buffer-live-p gnus-original-article-buffer))
2604       (save-restriction
2605         (article-narrow-to-head)
2606         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2607           (with-current-buffer gnus-original-article-buffer
2608             (goto-char (point-min)))
2609           (setq regexp (concat "^" header
2610                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2611           (while (re-search-forward regexp nil t)
2612             (replace-match (save-match-data
2613                              (gnus-decode-newsgroups
2614                               ;; XXX how to use data in article buffer?
2615                               (with-current-buffer gnus-original-article-buffer
2616                                 (re-search-forward regexp nil t)
2617                                 (match-string 1))
2618                               gnus-newsgroup-name method))
2619                            t t nil 1))
2620           (goto-char (point-min)))))))
2621
2622 (autoload 'idna-to-unicode "idna")
2623
2624 (defun article-decode-idna-rhs ()
2625   "Decode IDNA strings in RHS in various headers in current buffer.
2626 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2627 Mail-Reply-To: and Mail-Followup-To:."
2628   (when gnus-use-idna
2629     (save-restriction
2630       (let ((inhibit-point-motion-hooks t)
2631             (inhibit-read-only t))
2632         (article-narrow-to-head)
2633         (goto-char (point-min))
2634         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2635           (let (ace unicode)
2636             (when (save-match-data
2637                     (and (setq ace (match-string 1))
2638                          (save-excursion
2639                            (and (re-search-backward "^[^ \t]" nil t)
2640                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2641                          (setq unicode (idna-to-unicode ace))))
2642               (unless (string= ace unicode)
2643                 (replace-match unicode nil nil nil 1)))))))))
2644
2645 (defun article-de-quoted-unreadable (&optional force read-charset)
2646   "Translate a quoted-printable-encoded article.
2647 If FORCE, decode the article whether it is marked as quoted-printable
2648 or not.
2649 If READ-CHARSET, ask for a coding system."
2650   (interactive (list 'force current-prefix-arg))
2651   (save-excursion
2652     (let ((inhibit-read-only t) type charset)
2653       (if (gnus-buffer-live-p gnus-original-article-buffer)
2654           (with-current-buffer gnus-original-article-buffer
2655             (setq type
2656                   (gnus-fetch-field "content-transfer-encoding"))
2657             (let* ((ct (gnus-fetch-field "content-type"))
2658                    (ctl (and ct (mail-header-parse-content-type ct))))
2659               (setq charset (and ctl
2660                                  (mail-content-type-get ctl 'charset)))
2661               (if (stringp charset)
2662                   (setq charset (intern (downcase charset)))))))
2663       (if read-charset
2664           (setq charset (mm-read-coding-system "Charset: " charset)))
2665       (unless charset
2666         (setq charset gnus-newsgroup-charset))
2667       (when (or force
2668                 (and type (let ((case-fold-search t))
2669                             (string-match "quoted-printable" type))))
2670         (article-goto-body)
2671         (quoted-printable-decode-region
2672          (point) (point-max) (mm-charset-to-coding-system charset nil t))))))
2673
2674 (defun article-de-base64-unreadable (&optional force read-charset)
2675   "Translate a base64 article.
2676 If FORCE, decode the article whether it is marked as base64 not.
2677 If READ-CHARSET, ask for a coding system."
2678   (interactive (list 'force current-prefix-arg))
2679   (save-excursion
2680     (let ((inhibit-read-only t) type charset)
2681       (if (gnus-buffer-live-p gnus-original-article-buffer)
2682           (with-current-buffer gnus-original-article-buffer
2683             (setq type
2684                   (gnus-fetch-field "content-transfer-encoding"))
2685             (let* ((ct (gnus-fetch-field "content-type"))
2686                    (ctl (and ct (mail-header-parse-content-type ct))))
2687               (setq charset (and ctl
2688                                  (mail-content-type-get ctl 'charset)))
2689               (if (stringp charset)
2690                   (setq charset (intern (downcase charset)))))))
2691       (if read-charset
2692           (setq charset (mm-read-coding-system "Charset: " charset)))
2693       (unless charset
2694         (setq charset gnus-newsgroup-charset))
2695       (when (or force
2696                 (and type (let ((case-fold-search t))
2697                             (string-match "base64" type))))
2698         (article-goto-body)
2699         (save-restriction
2700           (narrow-to-region (point) (point-max))
2701           (base64-decode-region (point-min) (point-max))
2702           (mm-decode-coding-region
2703            (point-min) (point-max)
2704            (mm-charset-to-coding-system charset nil t)))))))
2705
2706 (eval-when-compile
2707   (require 'rfc1843))
2708
2709 (defun article-decode-HZ ()
2710   "Translate a HZ-encoded article."
2711   (interactive)
2712   (require 'rfc1843)
2713   (save-excursion
2714     (let ((inhibit-read-only t))
2715       (rfc1843-decode-region (point-min) (point-max)))))
2716
2717 (defun article-unsplit-urls ()
2718   "Remove the newlines that some other mailers insert into URLs."
2719   (interactive)
2720   (save-excursion
2721     (let ((inhibit-read-only t))
2722       (goto-char (point-min))
2723       (while (re-search-forward
2724               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2725         (replace-match "\\1\\3" t)))
2726     (when (gmm-called-interactively-p 'any)
2727       (gnus-treat-article nil))))
2728
2729 (defun article-wash-html ()
2730   "Format an HTML article."
2731   (interactive)
2732   (let ((handles nil)
2733         (buffer-read-only nil))
2734     (when (gnus-buffer-live-p gnus-original-article-buffer)
2735       (with-current-buffer gnus-original-article-buffer
2736         (setq handles (mm-dissect-buffer t t))))
2737     (article-goto-body)
2738     (delete-region (point) (point-max))
2739     (mm-enable-multibyte)
2740     (mm-inline-text-html handles)))
2741
2742 (defvar gnus-article-browse-html-temp-list nil
2743   "List of temporary files created by `gnus-article-browse-html-parts'.
2744 Internal variable.")
2745
2746 (defcustom gnus-article-browse-delete-temp 'ask
2747   "What to do with temporary files from `gnus-article-browse-html-parts'.
2748 If nil, don't delete temporary files.  If it is t, delete them on
2749 exit from the summary buffer.  If it is the symbol `file', query
2750 on each file, if it is `ask' ask once when exiting from the
2751 summary buffer."
2752   :group 'gnus-article
2753   :version "23.1" ;; No Gnus
2754   :type '(choice (const :tag "Don't delete" nil)
2755                  (const :tag "Don't ask" t)
2756                  (const :tag "Ask" ask)
2757                  (const :tag "Ask for each file" file)))
2758
2759 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2760
2761 (defun gnus-article-browse-delete-temp-files (&optional how)
2762   "Delete temp-files created by `gnus-article-browse-html-parts'."
2763   (when (and gnus-article-browse-html-temp-list
2764              (progn
2765                (or how (setq how gnus-article-browse-delete-temp))
2766                (if (eq how 'ask)
2767                    (let ((files (length gnus-article-browse-html-temp-list)))
2768                      (or (gnus-y-or-n-p
2769                           (if (= files 1)
2770                               "Delete the temporary HTML file? "
2771                             (format "Delete all %s temporary HTML files? "
2772                                     files)))
2773                          (setq gnus-article-browse-html-temp-list nil)))
2774                  how)))
2775     (dolist (file gnus-article-browse-html-temp-list)
2776       (cond ((file-directory-p file)
2777              (when (or (not (eq how 'file))
2778                        (gnus-y-or-n-p
2779                         (format
2780                          "Delete temporary HTML file(s) in directory `%s'? "
2781                          (file-name-as-directory file))))
2782                (gnus-delete-directory file)))
2783             ((file-exists-p file)
2784              (when (or (not (eq how 'file))
2785                        (gnus-y-or-n-p
2786                         (format "Delete temporary HTML file `%s'? " file)))
2787                (delete-file file)))))
2788     ;; Also remove file from the list when not deleted or if file doesn't
2789     ;; exist anymore.
2790     (setq gnus-article-browse-html-temp-list nil))
2791   gnus-article-browse-html-temp-list)
2792
2793 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2794   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2795 Return file name."
2796   (save-match-data
2797     (let (file type)
2798       (catch 'found
2799         (dolist (handle handles)
2800           (cond
2801            ((not (listp handle)))
2802            ;; Exclude broken handles that `gnus-summary-enter-digest-group'
2803            ;; may create.
2804            ((not (or (bufferp (car handle)) (stringp (car handle)))))
2805            ((equal (mm-handle-media-supertype handle) "multipart")
2806             (when (setq file (gnus-article-browse-html-save-cid-content
2807                               cid handle directory))
2808               (throw 'found file)))
2809            ((equal (concat "<" cid ">") (mm-handle-id handle))
2810             (setq file
2811                   (expand-file-name
2812                    (or (mm-handle-filename handle)
2813                        (concat
2814                         (make-temp-name "cid")
2815                         (car (rassoc (car (mm-handle-type handle))
2816                                      mailcap-mime-extensions))))
2817                    directory))
2818             (mm-save-part-to-file handle file)
2819             (throw 'found file))))))))
2820
2821 (defun gnus-article-browse-html-parts (list &optional header)
2822   "View all \"text/html\" parts from LIST.
2823 Recurse into multiparts.  The optional HEADER that should be a decoded
2824 message header will be added to the bodies of the \"text/html\" parts."
2825   ;; Internal function used by `gnus-article-browse-html-article'.
2826   (let (type file charset content cid-dir tmp-file showed)
2827     ;; Find and show the html-parts.
2828     (dolist (handle list)
2829       ;; If HTML, show it:
2830       (cond ((not (listp handle)))
2831             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2832                  (and (equal (car type) "message/external-body")
2833                       (or header
2834                           (setq file (mm-handle-filename handle)))
2835                       (or (mm-handle-cache handle)
2836                           (condition-case code
2837                               (progn (mm-extern-cache-contents handle) t)
2838                             (error
2839                              (gnus-message 3 "%s" (error-message-string code))
2840                              (when (>= gnus-verbose 3) (sit-for 2))
2841                              nil)))
2842                       (progn
2843                         (setq handle (mm-handle-cache handle)
2844                               type (mm-handle-type handle))
2845                         (equal (car type) "text/html"))))
2846              (setq charset (mail-content-type-get type 'charset)
2847                    content (mm-get-part handle))
2848              (with-temp-buffer
2849                (if (eq charset 'gnus-decoded)
2850                    (mm-enable-multibyte)
2851                  (mm-disable-multibyte))
2852                (insert content)
2853                ;; resolve cid contents
2854                (let ((case-fold-search t)
2855                      cid-file)
2856                  (goto-char (point-min))
2857                  (while (re-search-forward "\
2858 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2859                                            nil t)
2860                    (unless cid-dir
2861                      (setq cid-dir (mm-make-temp-file "cid" t))
2862                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2863                    (setq file nil
2864                          content nil)
2865                    (when (setq cid-file
2866                                (gnus-article-browse-html-save-cid-content
2867                                 (match-string 2)
2868                                 (with-current-buffer gnus-article-buffer
2869                                   gnus-article-mime-handles)
2870                                 cid-dir))
2871                      (when (eq system-type 'cygwin)
2872                        (setq cid-file
2873                              (concat "/" (substring
2874                                           (with-output-to-string
2875                                             (call-process "cygpath" nil
2876                                                           standard-output
2877                                                           nil "-m" cid-file))
2878                                           0 -1))))
2879                      (replace-match (concat "file://" cid-file)
2880                                     nil nil nil 1))))
2881                (unless content (setq content (buffer-string))))
2882              (when (or charset header (not file))
2883                (setq tmp-file (mm-make-temp-file
2884                                ;; Do we need to care for 8.3 filenames?
2885                                "mm-" nil ".html")))
2886              ;; Add a meta html tag to specify charset and a header.
2887              (cond
2888               (header
2889                (let (title eheader body hcharset coding)
2890                  (with-temp-buffer
2891                    (mm-enable-multibyte)
2892                    (setq case-fold-search t)
2893                    (insert header "\n")
2894                    (setq title (message-fetch-field "subject"))
2895                    (goto-char (point-min))
2896                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|\\(&\\)\\|\n"
2897                                              nil t)
2898                      (replace-match (cond ((match-beginning 1) "&lt;")
2899                                           ((match-beginning 2) "&gt;")
2900                                           ((match-beginning 3) "&amp;")
2901                                           (t "<br>\n"))))
2902                    (goto-char (point-min))
2903                    (while (re-search-forward "^[\t ]+" nil t)
2904                      (dotimes (i (prog1
2905                                      (current-column)
2906                                    (delete-region (match-beginning 0)
2907                                                   (match-end 0))))
2908                        (insert "&nbsp;")))
2909                    (goto-char (point-min))
2910                    (insert "<div align=\"left\">\n")
2911                    (goto-char (point-max))
2912                    (insert "</div>\n<hr>\n")
2913                    ;; We have to examine charset one by one since
2914                    ;; charset specified in parts might be different.
2915                    (if (eq charset 'gnus-decoded)
2916                        (setq charset 'utf-8
2917                              eheader (mm-encode-coding-string (buffer-string)
2918                                                               charset)
2919                              title (when title
2920                                      (mm-encode-coding-string title charset))
2921                              body (mm-encode-coding-string content charset))
2922                      (setq hcharset (mm-find-mime-charset-region (point-min)
2923                                                                  (point-max)))
2924                      (cond ((= (length hcharset) 1)
2925                             (setq hcharset (car hcharset)
2926                                   coding (mm-charset-to-coding-system
2927                                           hcharset nil t)))
2928                            ((> (length hcharset) 1)
2929                             (setq hcharset 'utf-8
2930                                   coding hcharset)))
2931                      (if coding
2932                          (if charset
2933                              (progn
2934                                (setq body
2935                                      (mm-charset-to-coding-system charset
2936                                                                   nil t))
2937                                (if (eq coding body)
2938                                    (setq eheader (mm-encode-coding-string
2939                                                   (buffer-string) coding)
2940                                          title (when title
2941                                                  (mm-encode-coding-string
2942                                                   title coding))
2943                                          body content)
2944                                  (setq charset 'utf-8
2945                                        eheader (mm-encode-coding-string
2946                                                 (buffer-string) charset)
2947                                        title (when title
2948                                                (mm-encode-coding-string
2949                                                 title charset))
2950                                        body (mm-encode-coding-string
2951                                              (mm-decode-coding-string
2952                                               content body)
2953                                              charset))))
2954                            (setq charset hcharset
2955                                  eheader (mm-encode-coding-string
2956                                           (buffer-string) coding)
2957                                  title (when title
2958                                          (mm-encode-coding-string
2959                                           title coding))
2960                                  body content))
2961                        (setq eheader (mm-string-as-unibyte (buffer-string))
2962                              body content)))
2963                    (erase-buffer)
2964                    (mm-disable-multibyte)
2965                    (insert body)
2966                    (when charset
2967                      (mm-add-meta-html-tag handle charset t))
2968                    (when title
2969                      (goto-char (point-min))
2970                      (unless (search-forward "<title>" nil t)
2971                        (re-search-forward "<head>\\s-*" nil t)
2972                        (insert "<title>" title "</title>\n")))
2973                    (goto-char (point-min))
2974                    (or (re-search-forward
2975                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2976                        (re-search-forward
2977                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2978                    (insert eheader)
2979                    (mm-write-region (point-min) (point-max)
2980                                     tmp-file nil nil nil 'binary t))))
2981               (charset
2982                (mm-with-unibyte-buffer
2983                  (insert (if (eq charset 'gnus-decoded)
2984                              (mm-encode-coding-string content
2985                                                       (setq charset 'utf-8))
2986                            content))
2987                  (if (or (mm-add-meta-html-tag handle charset)
2988                          (not file))
2989                      (mm-write-region (point-min) (point-max)
2990                                       tmp-file nil nil nil 'binary t)
2991                    (setq tmp-file nil))))
2992               (tmp-file
2993                (mm-save-part-to-file handle tmp-file)))
2994              (when tmp-file
2995                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2996              (add-hook 'gnus-summary-prepare-exit-hook
2997                        'gnus-article-browse-delete-temp-files)
2998              (add-hook 'gnus-exit-gnus-hook
2999                        (lambda  ()
3000                          (gnus-article-browse-delete-temp-files t)))
3001              ;; FIXME: Warn if there's an <img> tag?
3002              (browse-url-of-file (or tmp-file (expand-file-name file)))
3003              (setq showed t))
3004             ;; If multipart, recurse
3005             ((equal (mm-handle-media-supertype handle) "multipart")
3006              (when (gnus-article-browse-html-parts handle header)
3007                (setq showed t)))
3008             ((equal (mm-handle-media-type handle) "message/rfc822")
3009              (mm-with-multibyte-buffer
3010                (mm-insert-part handle)
3011                (setq handle (mm-dissect-buffer t t))
3012                (when (and (bufferp (car handle))
3013                           (stringp (car (mm-handle-type handle))))
3014                  (setq handle (list handle)))
3015                (when header
3016                  (article-decode-encoded-words)
3017                  (let ((gnus-visible-headers
3018                         (or (get 'gnus-visible-headers 'standard-value)
3019                             gnus-visible-headers)))
3020                    (article-hide-headers))
3021                  (goto-char (point-min))
3022                  (search-forward "\n\n" nil 'move)
3023                  (skip-chars-backward "\t\n ")
3024                  (setq header (buffer-substring (point-min) (point)))))
3025              (when (prog1
3026                        (gnus-article-browse-html-parts handle header)
3027                      (mm-destroy-parts handle))
3028                (setq showed t)))))
3029     showed))
3030
3031 (defun gnus-article-browse-html-article (&optional arg)
3032   "View \"text/html\" parts of the current article with a WWW browser.
3033 Inline images embedded in a message using the cid scheme, as they are
3034 generally considered to be safe, will be processed properly.
3035 The message header is added to the beginning of every html part unless
3036 the prefix argument ARG is given.
3037
3038 Warning: Spammers use links to images (using the http scheme) in HTML
3039 articles to verify whether you have read the message.  As
3040 `gnus-article-browse-html-article' passes the HTML content to the
3041 browser without eliminating these \"web bugs\" you should only
3042 use it for mails from trusted senders.
3043
3044 If you always want to display HTML parts in the browser, set
3045 `mm-text-html-renderer' to nil.
3046
3047 This command creates temporary files to pass HTML contents including
3048 images if any to the browser, and deletes them when exiting the group
3049 \(if you want)."
3050   ;; Cf. `mm-w3m-safe-url-regexp'
3051   (interactive "P")
3052   (if arg
3053       (gnus-summary-show-article)
3054     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3055                                     gnus-visible-headers))
3056           ;; As we insert a <hr>, there's no need for the body boundary.
3057           (gnus-treat-body-boundary nil))
3058       (gnus-summary-show-article)))
3059   (with-current-buffer gnus-article-buffer
3060     (let ((header (unless arg
3061                     (save-restriction
3062                       (widen)
3063                       (buffer-substring-no-properties
3064                        (goto-char (point-min))
3065                        (if (search-forward "\n\n" nil t)
3066                            (match-beginning 0)
3067                          (goto-char (point-max))
3068                          (skip-chars-backward "\t\n ")
3069                          (point))))))
3070           parts)
3071       (set-buffer gnus-original-article-buffer)
3072       (setq parts (mm-dissect-buffer t t))
3073       ;; If singlepart, enforce a list.
3074       (when (and (bufferp (car parts))
3075                  (stringp (car (mm-handle-type parts))))
3076         (setq parts (list parts)))
3077       ;; Process the list
3078       (unless (gnus-article-browse-html-parts parts header)
3079         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3080       (mm-destroy-parts parts)
3081       (unless arg
3082         (gnus-summary-show-article)))))
3083
3084 (defun article-hide-list-identifiers ()
3085   "Remove list identifies from the Subject header.
3086 The `gnus-list-identifiers' variable specifies what to do."
3087   (interactive)
3088   (let ((inhibit-point-motion-hooks t)
3089         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3090         (inhibit-read-only t))
3091     (when regexp
3092       (save-excursion
3093         (save-restriction
3094           (article-narrow-to-head)
3095           (goto-char (point-min))
3096           (while (re-search-forward
3097                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3098                   nil t)
3099             (delete-region (match-beginning 2) (match-end 0))
3100             (beginning-of-line))
3101           (when (re-search-forward
3102                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3103             (delete-region (match-beginning 1) (match-end 1))))))))
3104
3105 (defun article-hide-pem (&optional arg)
3106   "Toggle hiding of any PEM headers and signatures in the current article.
3107 If given a negative prefix, always show; if given a positive prefix,
3108 always hide."
3109   (interactive (gnus-article-hidden-arg))
3110   (unless (gnus-article-check-hidden-text 'pem arg)
3111     (save-excursion
3112       (let ((inhibit-read-only t) end)
3113         (goto-char (point-min))
3114         ;; Hide the horrendously ugly "header".
3115         (when (and (search-forward
3116                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3117                     nil t)
3118                    (setq end (1+ (match-beginning 0))))
3119           (gnus-add-wash-type 'pem)
3120           (gnus-article-hide-text-type
3121            end
3122            (if (search-forward "\n\n" nil t)
3123                (match-end 0)
3124              (point-max))
3125            'pem)
3126           ;; Hide the trailer as well
3127           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3128                                 nil t)
3129             (gnus-article-hide-text-type
3130              (match-beginning 0) (match-end 0) 'pem)))))))
3131
3132 (defun article-strip-banner ()
3133   "Strip the banners specified by the `banner' group parameter and by
3134 `gnus-article-address-banner-alist'."
3135   (interactive)
3136   (save-excursion
3137     (save-restriction
3138       (let ((inhibit-point-motion-hooks t))
3139         (when (gnus-parameter-banner gnus-newsgroup-name)
3140           (article-really-strip-banner
3141            (gnus-parameter-banner gnus-newsgroup-name)))
3142         (when gnus-article-address-banner-alist
3143           ;; Note that the From header is decoded here, so it is
3144           ;; required that the *-extract-address-components function
3145           ;; supports non-ASCII text.
3146           (let ((from (save-restriction
3147                         (widen)
3148                         (article-narrow-to-head)
3149                         (mail-fetch-field "from"))))
3150             (when (and from
3151                        (setq from
3152                              (cadr (funcall gnus-extract-address-components
3153                                             from))))
3154               (catch 'found
3155                 (dolist (pair gnus-article-address-banner-alist)
3156                   (when (string-match (car pair) from)
3157                     (throw 'found
3158                            (article-really-strip-banner (cdr pair)))))))))))))
3159
3160 (defun article-really-strip-banner (banner)
3161   "Strip the banner specified by the argument."
3162   (save-excursion
3163     (save-restriction
3164       (let ((inhibit-point-motion-hooks t)
3165             (gnus-signature-limit nil)
3166             (inhibit-read-only t))
3167         (article-goto-body)
3168         (cond
3169          ((eq banner 'signature)
3170           (when (gnus-article-narrow-to-signature)
3171             (widen)
3172             (forward-line -1)
3173             (delete-region (point) (point-max))))
3174          ((symbolp banner)
3175           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3176               (while (re-search-forward banner nil t)
3177                 (delete-region (match-beginning 0) (match-end 0)))))
3178          ((stringp banner)
3179           (while (re-search-forward banner nil t)
3180             (delete-region (match-beginning 0) (match-end 0)))))))))
3181
3182 (defun article-babel ()
3183   "Translate article using an online translation service."
3184   (interactive)
3185   (require 'babel)
3186   (gnus-with-article-buffer
3187     (when (article-goto-body)
3188       (let* ((start (point))
3189              (end (point-max))
3190              (orig (buffer-substring start end))
3191              (trans (babel-as-string orig)))
3192         (save-restriction
3193           (narrow-to-region start end)
3194           (delete-region start end)
3195           (insert trans))))))
3196
3197 (defun article-hide-signature (&optional arg)
3198   "Hide the signature in the current article.
3199 If given a negative prefix, always show; if given a positive prefix,
3200 always hide."
3201   (interactive (gnus-article-hidden-arg))
3202   (unless (gnus-article-check-hidden-text 'signature arg)
3203     (save-excursion
3204       (save-restriction
3205         (let ((inhibit-read-only t))
3206           (when (gnus-article-narrow-to-signature)
3207             (gnus-article-hide-text-type
3208              (point-min) (point-max) 'signature))))))
3209   (gnus-set-mode-line 'article))
3210
3211 (defun article-strip-headers-in-body ()
3212   "Strip offensive headers from bodies."
3213   (interactive)
3214   (save-excursion
3215     (article-goto-body)
3216     (let ((case-fold-search t))
3217       (when (looking-at "x-no-archive:")
3218         (gnus-delete-line)))))
3219
3220 (defun article-strip-leading-blank-lines ()
3221   "Remove all blank lines from the beginning of the article."
3222   (interactive)
3223   (save-excursion
3224     (let ((inhibit-point-motion-hooks t)
3225           (inhibit-read-only t))
3226       (when (article-goto-body)
3227         (while (and (not (eobp))
3228                     (looking-at "[ \t]*$"))
3229           (gnus-delete-line))))))
3230
3231 (defun article-narrow-to-head ()
3232   "Narrow the buffer to the head of the message.
3233 Point is left at the beginning of the narrowed-to region."
3234   (narrow-to-region
3235    (goto-char (point-min))
3236    (cond
3237     ;; Absolutely no headers displayed.
3238     ((looking-at "\n")
3239      (point))
3240     ;; Normal headers.
3241     ((search-forward "\n\n" nil 1)
3242      (1- (point)))
3243     ;; Nothing but headers.
3244     (t
3245      (point-max))))
3246   (goto-char (point-min)))
3247
3248 (defun article-goto-body ()
3249   "Place point at the start of the body."
3250   (goto-char (point-min))
3251   (cond
3252    ;; This variable is only bound when dealing with separate
3253    ;; MIME body parts.
3254    (article-goto-body-goes-to-point-min-p
3255     t)
3256    ((search-forward "\n\n" nil t)
3257     t)
3258    (t
3259     (goto-char (point-max))
3260     nil)))
3261
3262 (defun article-strip-multiple-blank-lines ()
3263   "Replace consecutive blank lines with one empty line."
3264   (interactive)
3265   (save-excursion
3266     (let ((inhibit-point-motion-hooks t)
3267           (inhibit-read-only t))
3268       ;; First make all blank lines empty.
3269       (article-goto-body)
3270       (while (re-search-forward "^[ \t]+$" nil t)
3271         (unless (gnus-annotation-in-region-p
3272                  (match-beginning 0) (match-end 0))
3273           (replace-match "" nil t)))
3274       ;; Then replace multiple empty lines with a single empty line.
3275       (article-goto-body)
3276       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3277         (unless (gnus-annotation-in-region-p
3278                  (match-beginning 0) (match-end 0))
3279           (delete-region (match-beginning 1) (match-end 1)))))))
3280
3281 (defun article-strip-leading-space ()
3282   "Remove all white space from the beginning of the lines in the article."
3283   (interactive)
3284   (save-excursion
3285     (let ((inhibit-point-motion-hooks t)
3286           (inhibit-read-only t))
3287       (article-goto-body)
3288       (while (re-search-forward "^[ \t]+" nil t)
3289         (replace-match "" t t)))))
3290
3291 (defun article-strip-trailing-space ()
3292   "Remove all white space from the end of the lines in the article."
3293   (interactive)
3294   (save-excursion
3295     (let ((inhibit-point-motion-hooks t)
3296           (inhibit-read-only t))
3297       (article-goto-body)
3298       (while (re-search-forward "[ \t]+$" nil t)
3299         (replace-match "" t t)))))
3300
3301 (defun article-strip-blank-lines ()
3302   "Strip leading, trailing and multiple blank lines."
3303   (interactive)
3304   (article-strip-leading-blank-lines)
3305   (article-remove-trailing-blank-lines)
3306   (article-strip-multiple-blank-lines))
3307
3308 (defun article-strip-all-blank-lines ()
3309   "Strip all blank lines."
3310   (interactive)
3311   (save-excursion
3312     (let ((inhibit-point-motion-hooks t)
3313           (inhibit-read-only t))
3314       (article-goto-body)
3315       (while (re-search-forward "^[ \t]*\n" nil t)
3316         (replace-match "" t t)))))
3317
3318 (defun gnus-article-narrow-to-signature ()
3319   "Narrow to the signature; return t if a signature is found, else nil."
3320   (let ((inhibit-point-motion-hooks t))
3321     (when (gnus-article-search-signature)
3322       (forward-line 1)
3323       ;; Check whether we have some limits to what we consider
3324       ;; to be a signature.
3325       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3326                       (list gnus-signature-limit)))
3327             limit limited)
3328         (while (setq limit (pop limits))
3329           (if (or (and (integerp limit)
3330                        (< (- (point-max) (point)) limit))
3331                   (and (floatp limit)
3332                        (< (count-lines (point) (point-max)) limit))
3333                   (and (functionp limit)
3334                        (funcall limit))
3335                   (and (stringp limit)
3336                        (not (re-search-forward limit nil t))))
3337               ()                        ; This limit did not succeed.
3338             (setq limited t
3339                   limits nil)))
3340         (unless limited
3341           (narrow-to-region (point) (point-max))
3342           t)))))
3343
3344 (defun gnus-article-search-signature ()
3345   "Search the current buffer for the signature separator.
3346 Put point at the beginning of the signature separator."
3347   (let ((cur (point)))
3348     (goto-char (point-max))
3349     (if (if (stringp gnus-signature-separator)
3350             (re-search-backward gnus-signature-separator nil t)
3351           (let ((seps gnus-signature-separator))
3352             (while (and seps
3353                         (not (re-search-backward (car seps) nil t)))
3354               (pop seps))
3355             seps))
3356         t
3357       (goto-char cur)
3358       nil)))
3359
3360 (defun gnus-article-hidden-arg ()
3361   "Return the current prefix arg as a number, or 0 if no prefix."
3362   (list (if current-prefix-arg
3363             (prefix-numeric-value current-prefix-arg)
3364           0)))
3365
3366 (defun gnus-article-check-hidden-text (type arg)
3367   "Return nil if hiding is necessary.
3368 Arg can be nil or a number.  nil and positive means hide, negative
3369 means show, 0 means toggle."
3370   (save-excursion
3371     (save-restriction
3372       (let ((hide (gnus-article-hidden-text-p type)))
3373         (cond
3374          ((or (null arg)
3375               (> arg 0))
3376           nil)
3377          ((< arg 0)
3378           (gnus-article-show-hidden-text type)
3379           t)
3380          (t
3381           (if (eq hide 'hidden)
3382               (progn
3383                 (gnus-article-show-hidden-text type)
3384                 t)
3385             nil)))))))
3386
3387 (defun gnus-article-hidden-text-p (type)
3388   "Say whether the current buffer contains hidden text of type TYPE."
3389   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3390     (while (and pos
3391                 (not (get-text-property pos 'invisible))
3392                 (not (get-text-property pos 'dummy-invisible)))
3393       (setq pos
3394             (text-property-any (1+ pos) (point-max) 'article-type type)))
3395     (if pos
3396         'hidden
3397       nil)))
3398
3399 (defun gnus-article-show-hidden-text (type &optional dummy)
3400   "Show all hidden text of type TYPE.
3401 Originally it is hide instead of DUMMY."
3402   (let ((inhibit-read-only t)
3403         (inhibit-point-motion-hooks t))
3404     (gnus-remove-text-properties-when
3405      'article-type type
3406      (point-min) (point-max)
3407      (cons 'article-type (cons type
3408                                gnus-hidden-properties)))
3409     (gnus-delete-wash-type type)))
3410
3411 (defconst article-time-units
3412   `((year . ,(* 365.25 24 60 60))
3413     (week . ,(* 7 24 60 60))
3414     (day . ,(* 24 60 60))
3415     (hour . ,(* 60 60))
3416     (minute . 60)
3417     (second . 1))
3418   "Mapping from time units to seconds.")
3419
3420 (defun gnus-article-forward-header ()
3421   "Move point to the start of the next header.
3422 If the current header is a continuation header, this can be several
3423 lines forward."
3424   (let ((ended nil))
3425     (while (not ended)
3426       (forward-line 1)
3427       (if (looking-at "[ \t]+[^ \t]")
3428           (forward-line 1)
3429         (setq ended t)))))
3430
3431 (defun article-treat-date ()
3432   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3433                        (with-current-buffer gnus-summary-buffer
3434                          gnus-article-date-headers)
3435                      gnus-article-date-headers)
3436                    t))
3437
3438 (defun article-date-ut (&optional type highlight date-position)
3439   "Convert DATE date to TYPE in the current article.
3440 The default type is `ut'.  See `gnus-article-date-headers' for
3441 possible values."
3442   (interactive (list 'ut t))
3443   (let* ((case-fold-search t)
3444          (inhibit-read-only t)
3445          (inhibit-point-motion-hooks t)
3446          (first t)
3447          (visible-date (mail-fetch-field "Date"))
3448          pos date bface eface)
3449     (save-excursion
3450       (if date-position
3451           (progn
3452             (goto-char date-position)
3453             (setq date (get-text-property (point) 'original-date))
3454             (when (looking-at "[^:]+:[\t ]*")
3455               (setq bface (get-text-property (match-beginning 0) 'face)
3456                     eface (get-text-property (match-end 0) 'face)))
3457             (delete-region (point)
3458                            (progn
3459                              (gnus-article-forward-header)
3460                              (point)))
3461             (article-transform-date date type bface eface))
3462         (save-restriction
3463           (widen)
3464           (goto-char (point-min))
3465           (while (or (get-text-property (setq pos (point)) 'original-date)
3466                      (and (setq pos (next-single-property-change
3467                                      (point) 'original-date))
3468                           (goto-char pos)))
3469             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3470                                       (1+ (match-beginning 0))
3471                                     (point-max)))
3472             (while (setq pos (text-property-not-all pos (point-max)
3473                                                     'gnus-date-type nil))
3474               (setq date (get-text-property pos 'original-date))
3475               (goto-char pos)
3476               (when (looking-at "[^:]+:[\t ]*")
3477                 (setq bface (get-text-property (match-beginning 0) 'face)
3478                       eface (get-text-property (match-end 0) 'face)))
3479               (delete-region pos (or (text-property-any pos (point-max)
3480                                                         'gnus-date-type nil)
3481                                      (point-max))))
3482             (unless date ;; the 1st time
3483               (goto-char (point-min))
3484               (while (re-search-forward "^Date:[\t ]*" nil t)
3485                 (setq date (get-text-property (match-beginning 0)
3486                                               'original-date)
3487                       bface (get-text-property (match-beginning 0) 'face)
3488                       eface (get-text-property (match-end 0) 'face))
3489                 (delete-region (point-at-bol) (progn
3490                                                 (gnus-article-forward-header)
3491                                                 (point)))))
3492             (when (and (not date)
3493                        visible-date)
3494               (setq date visible-date))
3495             (when date
3496               (article-transform-date date type bface eface))
3497             (goto-char (point-max))
3498             (widen)))))))
3499
3500 (defun article-transform-date (date type bface eface)
3501   (dolist (this-type (cond
3502                       ((null type)
3503                        (list 'ut))
3504                       ((atom type)
3505                        (list type))
3506                       (t
3507                        type)))
3508     (goto-char
3509      (prog1
3510          (point)
3511        (add-text-properties
3512         (point)
3513         (progn
3514           (insert (article-make-date-line date (or this-type 'ut)) "\n")
3515           (point))
3516         (list 'original-date date 'gnus-date-type this-type))))
3517     ;; Do highlighting.
3518     (when (looking-at
3519            "\\([^:]+:\\)[\t ]*\\(\\(?:[^\t\n ]+[\t ]+\\)*[^\t\n ]+\\)?")
3520       (put-text-property (match-beginning 1) (match-end 1) 'face bface)
3521       (when (match-beginning 2)
3522         (put-text-property (match-beginning 2) (match-end 2) 'face eface))
3523       (while (and (zerop (forward-line 1))
3524                   (looking-at "[\t ]+\\(\\(?:[^\t\n ]+[\t ]+\\)*[^\t\n ]+\\)?"))
3525         (when (match-beginning 1)
3526           (put-text-property (match-beginning 1) (match-end 1) 'face eface))))))
3527
3528 (defun article-make-date-line (date type)
3529   "Return a DATE line of TYPE."
3530   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3531                              combined-lapsed))
3532     (error "Unknown conversion type: %s" type))
3533   (condition-case ()
3534       (let ((time (ignore-errors (date-to-time date))))
3535         (cond
3536          ;; Convert to the local timezone.
3537          ((eq type 'local)
3538           (concat "Date: " (message-make-date time)))
3539          ;; Convert to Universal Time.
3540          ((eq type 'ut)
3541           (concat "Date: "
3542                   (substring
3543                    (message-make-date
3544                     (let* ((e (parse-time-string date))
3545                            (tm (apply 'encode-time e))
3546                            (ms (car tm))
3547                            (ls (- (cadr tm) (car (current-time-zone time)))))
3548                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3549                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3550                             (t (list ms ls)))))
3551                    0 -5)
3552                   "UT"))
3553          ;; Get the original date from the article.
3554          ((eq type 'original)
3555           (concat "Date: " (if (string-match "\n+$" date)
3556                                (substring date 0 (match-beginning 0))
3557                              date)))
3558          ;; Let the user define the format.
3559          ((eq type 'user-defined)
3560           (let ((format (or (condition-case nil
3561                                 (with-current-buffer gnus-summary-buffer
3562                                   gnus-article-time-format)
3563                               (error nil))
3564                             gnus-article-time-format)))
3565             (if (functionp format)
3566                 (funcall format time)
3567               (concat "Date: " (format-time-string format time)))))
3568          ;; ISO 8601.
3569          ((eq type 'iso8601)
3570           (let ((tz (car (current-time-zone time))))
3571             (concat
3572              "Date: "
3573              (format-time-string "%Y%m%dT%H%M%S" time)
3574              (format "%s%02d%02d"
3575                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3576                      (/ (% (abs tz) 3600) 60)))))
3577          ;; Do a lapsed format.
3578          ((eq type 'lapsed)
3579           (concat "Date: " (article-lapsed-string time)))
3580          ;; A combined date/lapsed format.
3581          ((eq type 'combined-lapsed)
3582           (let ((date-string (article-make-date-line date 'original))
3583                 (segments 3)
3584                 lapsed-string)
3585             (while (and
3586                     time
3587                     (setq lapsed-string
3588                           (concat " (" (article-lapsed-string time segments) ")"))
3589                     (> (+ (length date-string)
3590                           (length lapsed-string))
3591                        (+ fill-column 6))
3592                     (> segments 0))
3593               (setq segments (1- segments)))
3594             (if (> segments 0)
3595                 (concat date-string lapsed-string)
3596               date-string)))
3597          ;; Display the date in proper English
3598          ((eq type 'english)
3599           (let ((dtime (decode-time time)))
3600             (concat
3601              "Date: the "
3602              (number-to-string (nth 3 dtime))
3603              (let ((digit (% (nth 3 dtime) 10)))
3604                (cond
3605                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3606                 ((= digit 1) "st")
3607                 ((= digit 2) "nd")
3608                 ((= digit 3) "rd")
3609                 (t "th")))
3610              " of "
3611              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3612              " "
3613              (number-to-string (nth 5 dtime))
3614              " at "
3615              (format "%02d" (nth 2 dtime))
3616              ":"
3617              (format "%02d" (nth 1 dtime)))))))
3618     (foo
3619      (format "Date: %s (from Gnus)" date))))
3620
3621 (defun article-lapsed-string (time &optional max-segments)
3622   ;; If the date is seriously mangled, the timezone functions are
3623   ;; liable to bug out, so we ignore all errors.
3624   (let* ((now (current-time))
3625          (real-time (subtract-time now time))
3626          (real-sec (and real-time
3627                         (+ (* (float (car real-time)) 65536)
3628                            (cadr real-time))))
3629          (sec (and real-time (abs real-sec)))
3630          (segments 0)
3631          num prev)
3632     (unless max-segments
3633       (setq max-segments (length article-time-units)))
3634     (cond
3635      ((null real-time)
3636       "Unknown")
3637      ((zerop sec)
3638       "Now")
3639      (t
3640       (concat
3641        ;; This is a bit convoluted, but basically we go
3642        ;; through the time units for years, weeks, etc,
3643        ;; and divide things to see whether that results
3644        ;; in positive answers.
3645        (mapconcat
3646         (lambda (unit)
3647           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3648                   (>= segments max-segments))
3649               ;; The (remaining) seconds are too few to
3650               ;; be divided into this time unit.
3651               ""
3652             ;; It's big enough, so we output it.
3653             (setq sec (- sec (* num (cdr unit))))
3654             (prog1
3655                 (concat (if prev ", " "") (int-to-string
3656                                            (floor num))
3657                         " " (symbol-name (car unit))
3658                         (if (> num 1) "s" ""))
3659               (setq prev t
3660                     segments (1+ segments)))))
3661         article-time-units "")
3662        ;; If dates are odd, then it might appear like the
3663        ;; article was sent in the future.
3664        (if (> real-sec 0)
3665            " ago"
3666          " in the future"))))))
3667
3668 (defun article-date-local (&optional highlight)
3669   "Convert the current article date to the local timezone."
3670   (interactive (list t))
3671   (article-date-ut 'local highlight))
3672
3673 (defun article-date-english (&optional highlight)
3674   "Convert the current article date to something that is proper English."
3675   (interactive (list t))
3676   (article-date-ut 'english highlight))
3677
3678 (defun article-date-original (&optional highlight)
3679   "Convert the current article date to what it was originally.
3680 This is only useful if you have used some other date conversion
3681 function and want to see what the date was before converting."
3682   (interactive (list t))
3683   (article-date-ut 'original highlight))
3684
3685 (defun article-date-lapsed (&optional highlight)
3686   "Convert the current article date to time lapsed since it was sent."
3687   (interactive (list t))
3688   (article-date-ut 'lapsed highlight))
3689
3690 (defun article-date-combined-lapsed (&optional highlight)
3691   "Convert the current article date to time lapsed since it was sent."
3692   (interactive (list t))
3693   (article-date-ut 'combined-lapsed highlight))
3694
3695 (defun article-update-date-lapsed ()
3696   "Function to be run from a timer to update the lapsed time line."
3697   (save-match-data
3698     (let ((buffer (current-buffer)))
3699       (ignore-errors
3700         (walk-windows
3701          (lambda (w)
3702            (set-buffer (window-buffer w))
3703            (when (derived-mode-p 'gnus-article-mode)
3704              (let ((old-line (count-lines (point-min) (point)))
3705                    (old-column (- (point) (line-beginning-position)))
3706                    (window-start (window-start w))
3707                    (pos (point-min))
3708                    type next end)
3709                (while (setq pos (text-property-not-all pos (point-max)
3710                                                        'gnus-date-type nil))
3711                  (setq next (or (next-single-property-change pos
3712                                                              'gnus-date-type)
3713                                 (point-max)))
3714                  (setq type (get-text-property pos 'gnus-date-type))
3715                  (when (memq type '(lapsed combined-lapsed user-defined))
3716                    (article-date-ut type t pos)
3717                    (setq end (or (next-single-property-change pos
3718                                                               'gnus-date-type)
3719                                  (point-max)))
3720                    (when window-start
3721                      (if (/= window-start next)
3722                          (setq window-start nil)
3723                        (set-window-start w end)))
3724                    (setq next end))
3725                  (setq pos next))
3726                (goto-char (point-min))
3727                (when (> old-column 0)
3728                  (setq old-line (1- old-line)))
3729                (forward-line old-line)
3730                (end-of-line)
3731                (when (> (current-column) old-column)
3732                  (beginning-of-line)
3733                  (forward-char old-column)))))
3734          nil 'visible))
3735       (set-buffer buffer))))
3736
3737 (defun gnus-start-date-timer (&optional n)
3738   "Start a timer to update the Date headers in the article buffers.
3739 The numerical prefix says how frequently (in seconds) the function
3740 is to run."
3741   (interactive "p")
3742   (unless n
3743     (setq n 1))
3744   (gnus-stop-date-timer)
3745   (setq article-lapsed-timer
3746         (run-at-time 1 n 'article-update-date-lapsed)))
3747
3748 (defun gnus-stop-date-timer ()
3749   "Stop the Date timer."
3750   (interactive)
3751   (when article-lapsed-timer
3752     (nnheader-cancel-timer article-lapsed-timer)
3753     (setq article-lapsed-timer nil)))
3754
3755 (defun article-date-user (&optional highlight)
3756   "Convert the current article date to the user-defined format.
3757 This format is defined by the `gnus-article-time-format' variable."
3758   (interactive (list t))
3759   (article-date-ut 'user highlight))
3760
3761 (defun article-date-iso8601 (&optional highlight)
3762   "Convert the current article date to ISO8601."
3763   (interactive (list t))
3764   (article-date-ut 'iso8601 highlight))
3765
3766 (defmacro gnus-article-save-original-date (&rest forms)
3767   "Save the original date as a text property and evaluate FORMS."
3768   `(let* ((case-fold-search t)
3769           (start (progn
3770                    (goto-char (point-min))
3771                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3772                               (not (bolp)))
3773                      (match-end 0))))
3774           (date (when (and start
3775                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3776                                               nil t))
3777                   (buffer-substring-no-properties start
3778                                                   (match-beginning 0)))))
3779      (goto-char (point-max))
3780      (skip-chars-backward "\n")
3781      (put-text-property (point-min) (point) 'original-date date)
3782      ,@forms
3783      (goto-char (point-max))
3784      (skip-chars-backward "\n")
3785      (put-text-property (point-min) (point) 'original-date date)))
3786
3787 ;; (defun article-show-all ()
3788 ;;   "Show all hidden text in the article buffer."
3789 ;;   (interactive)
3790 ;;   (save-excursion
3791 ;;     (let ((inhibit-read-only t))
3792 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3793
3794 (defun article-remove-leading-whitespace ()
3795   "Remove excessive whitespace from all headers."
3796   (interactive)
3797   (save-excursion
3798     (save-restriction
3799       (let ((inhibit-read-only t))
3800         (article-narrow-to-head)
3801         (goto-char (point-min))
3802         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3803           (delete-region (match-beginning 1) (match-end 1)))))))
3804
3805 (defun article-emphasize (&optional arg)
3806   "Emphasize text according to `gnus-emphasis-alist'."
3807   (interactive (gnus-article-hidden-arg))
3808   (unless (gnus-article-check-hidden-text 'emphasis arg)
3809     (save-excursion
3810       (let ((alist (or
3811                     (condition-case nil
3812                         (with-current-buffer gnus-summary-buffer
3813                           gnus-article-emphasis-alist)
3814                       (error))
3815                     gnus-emphasis-alist))
3816             (inhibit-read-only t)
3817             (props (append '(article-type emphasis)
3818                            gnus-hidden-properties))
3819             regexp elem beg invisible visible face)
3820         (article-goto-body)
3821         (setq beg (point))
3822         (while (setq elem (pop alist))
3823           (goto-char beg)
3824           (setq regexp (car elem)
3825                 invisible (nth 1 elem)
3826                 visible (nth 2 elem)
3827                 face (nth 3 elem))
3828           (while (re-search-forward regexp nil t)
3829             (when (and (match-beginning visible) (match-beginning invisible))
3830               (gnus-article-hide-text
3831                (match-beginning invisible) (match-end invisible) props)
3832               (gnus-article-unhide-text-type
3833                (match-beginning visible) (match-end visible) 'emphasis)
3834               (gnus-put-overlay-excluding-newlines
3835                (match-beginning visible) (match-end visible) 'face face)
3836               (gnus-add-wash-type 'emphasis)
3837               (goto-char (match-end invisible)))))))))
3838
3839 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3840   "Setup newsgroup emphasis alist."
3841   (unless gnus-article-emphasis-alist
3842     (let ((name (and gnus-newsgroup-name
3843                      (gnus-group-real-name gnus-newsgroup-name))))
3844       (make-local-variable 'gnus-article-emphasis-alist)
3845       (setq gnus-article-emphasis-alist
3846             (nconc
3847              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3848                (while (setq elem (pop alist))
3849                  (when (and name (string-match (car elem) name))
3850                    (setq alist nil
3851                          highlight (copy-sequence (cdr elem)))))
3852                highlight)
3853              (copy-sequence highlight-words)
3854              (if gnus-newsgroup-name
3855                  (copy-sequence (gnus-group-find-parameter
3856                                  gnus-newsgroup-name 'highlight-words t)))
3857              gnus-emphasis-alist)))))
3858
3859 (defvar gnus-summary-article-menu)
3860 (defvar gnus-summary-post-menu)
3861
3862 ;;; Saving functions.
3863
3864 (defun gnus-article-save (save-buffer file &optional num)
3865   "Save the currently selected article."
3866   (when (or (get gnus-default-article-saver :headers)
3867             (not gnus-save-all-headers))
3868     ;; Remove headers according to `gnus-saved-headers' or the value
3869     ;; of the `:headers' property that the saver function might have.
3870     (let ((gnus-visible-headers
3871            (or (symbol-value (get gnus-default-article-saver :headers))
3872                gnus-saved-headers gnus-visible-headers))
3873           ;; Ignore group parameter.  See `article-hide-headers'.
3874           (gnus-summary-buffer nil))
3875       (with-current-buffer save-buffer
3876         (article-hide-headers 1 t))))
3877   (save-window-excursion
3878     (if (not gnus-default-article-saver)
3879         (error "No default saver is defined")
3880       ;; !!! Magic!  The saving functions all save
3881       ;; `gnus-save-article-buffer' (or so they think), but we
3882       ;; bind that variable to our save-buffer.
3883       (set-buffer gnus-article-buffer)
3884       (let* ((gnus-save-article-buffer save-buffer)
3885              (filename
3886               (cond
3887                ((not gnus-prompt-before-saving) 'default)
3888                ((eq gnus-prompt-before-saving 'always) nil)
3889                (t file)))
3890              (gnus-number-of-articles-to-be-saved
3891               (when (eq gnus-prompt-before-saving t)
3892                 num)))                  ; Magic
3893         (set-buffer gnus-article-current-summary)
3894         (funcall gnus-default-article-saver filename)))))
3895
3896 (defun gnus-read-save-file-name (prompt &optional filename
3897                                         function group headers variable
3898                                         dir-var)
3899   (let ((default-name
3900           (funcall function group headers (symbol-value variable)))
3901         result)
3902     (setq result
3903           (expand-file-name
3904            (cond
3905             ((eq filename 'default)
3906              default-name)
3907             ((eq filename t)
3908              default-name)
3909             (filename filename)
3910             (t
3911              (when (symbol-value dir-var)
3912                (setq default-name (expand-file-name
3913                                    (file-name-nondirectory default-name)
3914                                    (symbol-value dir-var))))
3915              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3916                     (prompt
3917                      (format prompt
3918                              (if (and gnus-number-of-articles-to-be-saved
3919                                       (> gnus-number-of-articles-to-be-saved 1))
3920                                  (format "these %d articles"
3921                                          gnus-number-of-articles-to-be-saved)
3922                                "this article")))
3923                     (file
3924                      ;; Let the split methods have their say.
3925                      (cond
3926                       ;; No split name was found.
3927                       ((null split-name)
3928                        (read-file-name
3929                         (concat prompt " (default "
3930                                 (file-name-nondirectory default-name) "): ")
3931                         (file-name-directory default-name)
3932                         default-name))
3933                       ;; A single group name is returned.
3934                       ((stringp split-name)
3935                        (setq default-name
3936                              (funcall function split-name headers
3937                                       (symbol-value variable)))
3938                        (read-file-name
3939                         (concat prompt " (default "
3940                                 (file-name-nondirectory default-name) "): ")
3941                         (file-name-directory default-name)
3942                         default-name))
3943                       ;; A single split name was found
3944                       ((= 1 (length split-name))
3945                        (let* ((name (expand-file-name
3946                                      (car split-name)
3947                                      gnus-article-save-directory))
3948                               (dir (cond ((file-directory-p name)
3949                                           (file-name-as-directory name))
3950                                          ((file-exists-p name) name)
3951                                          (t gnus-article-save-directory))))
3952                          (read-file-name
3953                           (concat prompt " (default " name "): ")
3954                           dir name)))
3955                       ;; A list of splits was found.
3956                       (t
3957                        (setq split-name (nreverse split-name))
3958                        (let (result)
3959                          (let ((file-name-history
3960                                 (nconc split-name file-name-history)))
3961                            (setq result
3962                                  (expand-file-name
3963                                   (read-file-name
3964                                    (concat prompt " (`M-p' for defaults): ")
3965                                    gnus-article-save-directory
3966                                    (car split-name))
3967                                   gnus-article-save-directory)))
3968                          (car (push result file-name-history)))))))
3969                ;; Create the directory.
3970                (gnus-make-directory (file-name-directory file))
3971                ;; If we have read a directory, we append the default file name.
3972                (when (file-directory-p file)
3973                  (setq file (expand-file-name (file-name-nondirectory
3974                                                default-name)
3975                                               (file-name-as-directory file))))
3976                ;; Possibly translate some characters.
3977                (nnheader-translate-file-chars file))))))
3978     (gnus-make-directory (file-name-directory result))
3979     (when variable
3980       (set variable result))
3981     (when dir-var
3982       (set dir-var (file-name-directory result)))
3983     result))
3984
3985 (defun gnus-article-archive-name (group)
3986   "Return the first instance of an \"Archive-name\" in the current buffer."
3987   (let ((case-fold-search t))
3988     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3989       (nnheader-concat gnus-article-save-directory
3990                        (match-string 1)))))
3991
3992 (defun gnus-article-nndoc-name (group)
3993   "If GROUP is an nndoc group, return the name of the parent group."
3994   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3995     (gnus-group-get-parameter group 'save-article-group)))
3996
3997 (defun gnus-summary-save-in-rmail (&optional filename)
3998   "Append this article to Rmail file.
3999 Optional argument FILENAME specifies file name.
4000 Directory to save to is default to `gnus-article-save-directory'."
4001   (setq filename (gnus-read-save-file-name
4002                   "Save %s in rmail file" filename
4003                   gnus-rmail-save-name gnus-newsgroup-name
4004                   gnus-current-headers 'gnus-newsgroup-last-rmail))
4005   (with-current-buffer gnus-save-article-buffer
4006     (save-excursion
4007       (save-restriction
4008         (widen)
4009         ;; Note that unlike gnus-summary-save-in-mail, there is no
4010         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
4011         ;; not use Babyl.
4012         (gnus-output-to-rmail filename))))
4013   filename)
4014
4015 (defun gnus-summary-save-in-mail (&optional filename)
4016   "Append this article to Unix mail file.
4017 Optional argument FILENAME specifies file name.
4018 Directory to save to is default to `gnus-article-save-directory'."
4019   (setq filename (gnus-read-save-file-name
4020                   "Save %s in Unix mail file" filename
4021                   gnus-mail-save-name gnus-newsgroup-name
4022                   gnus-current-headers 'gnus-newsgroup-last-mail))
4023   (with-current-buffer gnus-save-article-buffer
4024     (save-excursion
4025       (save-restriction
4026         (widen)
4027         (if (and (file-readable-p filename)
4028                  (file-regular-p filename)
4029                  (mail-file-babyl-p filename))
4030             (gnus-output-to-rmail filename)
4031           (gnus-output-to-mail filename)))))
4032   filename)
4033
4034 (put 'gnus-summary-save-in-file :decode t)
4035 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4036 (defun gnus-summary-save-in-file (&optional filename overwrite)
4037   "Append this article to file.
4038 Optional argument FILENAME specifies file name.
4039 Directory to save to is default to `gnus-article-save-directory'."
4040   (setq filename (gnus-read-save-file-name
4041                   "Save %s in file" filename
4042                   gnus-file-save-name gnus-newsgroup-name
4043                   gnus-current-headers 'gnus-newsgroup-last-file))
4044   (with-current-buffer gnus-save-article-buffer
4045     (save-excursion
4046       (save-restriction
4047         (widen)
4048         (when (and overwrite
4049                    (file-exists-p filename))
4050           (delete-file filename))
4051         (gnus-output-to-file filename))))
4052   filename)
4053
4054 (put 'gnus-summary-write-to-file :decode t)
4055 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4056 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4057 (defun gnus-summary-write-to-file (&optional filename)
4058   "Write this article to a file, overwriting it if the file exists.
4059 Optional argument FILENAME specifies file name.
4060 The directory to save in defaults to `gnus-article-save-directory'."
4061   (setq filename (gnus-read-save-file-name
4062                   "Save %s in file" filename
4063                   gnus-file-save-name gnus-newsgroup-name
4064                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4065   (gnus-summary-save-in-file filename t))
4066
4067 (put 'gnus-summary-save-body-in-file :decode t)
4068 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4069   "Append this article body to a file.
4070 Optional argument FILENAME specifies file name.
4071 The directory to save in defaults to `gnus-article-save-directory'."
4072   (setq filename (gnus-read-save-file-name
4073                   "Save %s body in file" filename
4074                   gnus-file-save-name gnus-newsgroup-name
4075                   gnus-current-headers 'gnus-newsgroup-last-file))
4076   (with-current-buffer gnus-save-article-buffer
4077     (save-excursion
4078       (save-restriction
4079         (widen)
4080         (when (article-goto-body)
4081           (narrow-to-region (point) (point-max)))
4082         (when (and overwrite
4083                    (file-exists-p filename))
4084           (delete-file filename))
4085         (gnus-output-to-file filename))))
4086   filename)
4087
4088 (put 'gnus-summary-write-body-to-file :decode t)
4089 (put 'gnus-summary-write-body-to-file
4090      :function 'gnus-summary-save-body-in-file)
4091 (defun gnus-summary-write-body-to-file (&optional filename)
4092   "Write this article body to a file, overwriting it if the file exists.
4093 Optional argument FILENAME specifies file name.
4094 The directory to save in defaults to `gnus-article-save-directory'."
4095   (setq filename (gnus-read-save-file-name
4096                   "Save %s body in file" filename
4097                   gnus-file-save-name gnus-newsgroup-name
4098                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4099   (gnus-summary-save-body-in-file filename t))
4100
4101 (put 'gnus-summary-save-in-pipe :decode t)
4102 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4103 (defun gnus-summary-save-in-pipe (&optional command raw)
4104   "Pipe this article to subprocess COMMAND.
4105 Valid values for COMMAND include:
4106   a string
4107     The executable command name and possibly arguments.
4108   nil
4109     You will be prompted for the command in the minibuffer.
4110   the symbol `default'
4111     It will be replaced with the command which the variable
4112     `gnus-summary-pipe-output-default-command' holds or the command
4113     last used for saving.
4114 Non-nil value for RAW overrides `:decode' and `:headers' properties
4115 and the raw article including all headers will be piped."
4116   (let ((article (gnus-summary-article-number))
4117         (decode (unless raw
4118                   (get 'gnus-summary-save-in-pipe :decode)))
4119         save-buffer default)
4120     (if article
4121         (if (vectorp (gnus-summary-article-header article))
4122             (save-current-buffer
4123               (gnus-summary-select-article decode decode nil article)
4124               (insert-buffer-substring
4125                (prog1
4126                    (if decode
4127                        gnus-article-buffer
4128                      gnus-original-article-buffer)
4129                  (setq save-buffer
4130                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4131               ;; Remove unwanted headers.
4132               (when (and (not raw)
4133                          (or (get 'gnus-summary-save-in-pipe :headers)
4134                              (not gnus-save-all-headers)))
4135                 (let ((gnus-visible-headers
4136                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4137                                               :headers))
4138                            gnus-saved-headers gnus-visible-headers))
4139                       (gnus-summary-buffer nil))
4140                   (article-hide-headers 1 t))))
4141           (error "%d is not a real article" article))
4142       (error "No article to pipe"))
4143     (setq default (or gnus-summary-pipe-output-default-command
4144                       gnus-last-shell-command))
4145     (unless (stringp command)
4146       (setq command
4147             (if (and (eq command 'default) default)
4148                 default
4149               (gnus-read-shell-command "Shell command on this article: "
4150                                        default))))
4151     (when (string-equal command "")
4152       (if default
4153           (setq command default)
4154         (error "A command is required")))
4155     (with-current-buffer save-buffer
4156       (save-restriction
4157         (widen)
4158         (shell-command-on-region (point-min) (point-max) command nil)))
4159     (gnus-kill-buffer save-buffer))
4160   (setq gnus-summary-pipe-output-default-command command))
4161
4162 (defun gnus-summary-pipe-to-muttprint (&optional command)
4163   "Pipe this article to muttprint."
4164   (unless (stringp command)
4165     (setq command (read-string
4166                    "Print using command: " gnus-summary-muttprint-program
4167                    nil gnus-summary-muttprint-program)))
4168   (let ((gnus-summary-pipe-output-default-command
4169          gnus-summary-pipe-output-default-command))
4170     (gnus-summary-save-in-pipe command))
4171   (setq gnus-summary-muttprint-program command))
4172
4173 ;;; Article file names when saving.
4174
4175 (defun gnus-capitalize-newsgroup (newsgroup)
4176   "Capitalize NEWSGROUP name."
4177   (when (not (zerop (length newsgroup)))
4178     (concat (char-to-string (upcase (aref newsgroup 0)))
4179             (substring newsgroup 1))))
4180
4181 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4182   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4183 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4184 Otherwise, it is like ~/News/news/group/num."
4185   (let ((default
4186           (expand-file-name
4187            (concat (if (gnus-use-long-file-name 'not-save)
4188                        (gnus-capitalize-newsgroup newsgroup)
4189                      (gnus-newsgroup-directory-form newsgroup))
4190                    "/" (int-to-string (mail-header-number headers)))
4191            gnus-article-save-directory)))
4192     (if (and last-file
4193              (string-equal (file-name-directory default)
4194                            (file-name-directory last-file))
4195              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4196         default
4197       (or last-file default))))
4198
4199 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4200   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4201 If variable `gnus-use-long-file-name' is non-nil, it is
4202 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4203   (let ((default
4204           (expand-file-name
4205            (concat (if (gnus-use-long-file-name 'not-save)
4206                        newsgroup
4207                      (gnus-newsgroup-directory-form newsgroup))
4208                    "/" (int-to-string (mail-header-number headers)))
4209            gnus-article-save-directory)))
4210     (if (and last-file
4211              (string-equal (file-name-directory default)
4212                            (file-name-directory last-file))
4213              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4214         default
4215       (or last-file default))))
4216
4217 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4218   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4219 If variable `gnus-use-long-file-name' is non-nil, it is
4220 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4221   (or last-file
4222       (expand-file-name
4223        (if (gnus-use-long-file-name 'not-save)
4224            newsgroup
4225          (file-relative-name
4226           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4227           default-directory))
4228        gnus-article-save-directory)))
4229
4230 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4231   "Generate file name from sender."
4232   (let ((from (mail-header-from headers)))
4233     (expand-file-name
4234      (if (and from (string-match "\\([^ <]+\\)@" from))
4235          (match-string 1 from)
4236        "nobody")
4237      gnus-article-save-directory)))
4238
4239 (defun article-verify-x-pgp-sig ()
4240   "Verify X-PGP-Sig."
4241   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4242   (interactive)
4243   (if (gnus-buffer-live-p gnus-original-article-buffer)
4244       (let ((sig (with-current-buffer gnus-original-article-buffer
4245                    (gnus-fetch-field "X-PGP-Sig")))
4246             items info headers)
4247         (when (and sig
4248                    mml2015-use
4249                    (mml2015-clear-verify-function))
4250           (with-temp-buffer
4251             (insert-buffer-substring gnus-original-article-buffer)
4252             (setq items (split-string sig))
4253             (message-narrow-to-head)
4254             (let ((inhibit-point-motion-hooks t)
4255                   (case-fold-search t))
4256               ;; Don't verify multiple headers.
4257               (setq headers (mapconcat (lambda (header)
4258                                          (concat header ": "
4259                                                  (mail-fetch-field header)
4260                                                  "\n"))
4261                                        (split-string (nth 1 items) ",") "")))
4262             (delete-region (point-min) (point-max))
4263             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4264             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4265             (insert headers)
4266             (widen)
4267             (forward-line)
4268             (while (not (eobp))
4269               (if (looking-at "^-")
4270                   (insert "- "))
4271               (forward-line))
4272             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4273             (insert "Version: " (car items) "\n\n")
4274             (insert (mapconcat 'identity (cddr items) "\n"))
4275             (insert "\n-----END PGP SIGNATURE-----\n")
4276             (let ((mm-security-handle (list (format "multipart/signed"))))
4277               (mml2015-clean-buffer)
4278               (let ((coding-system-for-write (or gnus-newsgroup-charset
4279                                                  'iso-8859-1)))
4280                 (funcall (mml2015-clear-verify-function)))
4281               (setq info
4282                     (or (mm-handle-multipart-ctl-parameter
4283                          mm-security-handle 'gnus-details)
4284                         (mm-handle-multipart-ctl-parameter
4285                          mm-security-handle 'gnus-info)))))
4286           (when info
4287             (let ((inhibit-read-only t) bface eface)
4288               (save-restriction
4289                 (message-narrow-to-head)
4290                 (goto-char (point-max))
4291                 (forward-line -1)
4292                 (setq bface (get-text-property (point-at-bol) 'face)
4293                       eface (get-text-property (1- (point-at-eol)) 'face))
4294                 (message-remove-header "X-Gnus-PGP-Verify")
4295                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4296                     (forward-line)
4297                   (goto-char (point-max)))
4298                 (narrow-to-region (point) (point))
4299                 (insert "X-Gnus-PGP-Verify: " info "\n")
4300                 (goto-char (point-min))
4301                 (forward-line)
4302                 (while (not (eobp))
4303                   (if (not (looking-at "^[ \t]"))
4304                       (insert " "))
4305                   (forward-line))
4306                 ;; Do highlighting.
4307                 (goto-char (point-min))
4308                 (when (looking-at "\\([^:]+\\): *")
4309                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4310                                      'face bface)
4311                   (put-text-property (match-end 0) (point-max)
4312                                      'face eface)))))))))
4313
4314 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4315
4316 (defun article-verify-cancel-lock ()
4317   "Verify Cancel-Lock header."
4318   (interactive)
4319   (if (gnus-buffer-live-p gnus-original-article-buffer)
4320       (canlock-verify gnus-original-article-buffer)))
4321
4322 (eval-and-compile
4323   (mapc
4324    (lambda (func)
4325      (let (afunc gfunc)
4326        (if (consp func)
4327            (setq afunc (car func)
4328                  gfunc (cdr func))
4329          (setq afunc func
4330                gfunc (intern (format "gnus-%s" func))))
4331        (defalias gfunc
4332          (when (fboundp afunc)
4333            `(lambda (&optional interactive &rest args)
4334               ,(documentation afunc t)
4335               (interactive (list t))
4336               (with-current-buffer gnus-article-buffer
4337                 (if interactive
4338                     (call-interactively ',afunc)
4339                   (apply ',afunc args))))))))
4340    '(article-hide-headers
4341      article-verify-x-pgp-sig
4342      article-verify-cancel-lock
4343      article-hide-boring-headers
4344      article-treat-overstrike
4345      article-treat-ansi-sequences
4346      article-fill-long-lines
4347      article-capitalize-sentences
4348      article-remove-cr
4349      article-remove-leading-whitespace
4350      article-display-x-face
4351      article-display-face
4352      article-de-quoted-unreadable
4353      article-de-base64-unreadable
4354      article-decode-HZ
4355      article-wash-html
4356      article-unsplit-urls
4357      article-hide-list-identifiers
4358      article-strip-banner
4359      article-babel
4360      article-hide-pem
4361      article-hide-signature
4362      article-strip-headers-in-body
4363      article-remove-trailing-blank-lines
4364      article-strip-leading-blank-lines
4365      article-strip-multiple-blank-lines
4366      article-strip-leading-space
4367      article-strip-trailing-space
4368      article-strip-blank-lines
4369      article-strip-all-blank-lines
4370      article-date-local
4371      article-date-english
4372      article-date-iso8601
4373      article-date-original
4374      article-treat-date
4375      article-date-ut
4376      article-decode-mime-words
4377      article-decode-charset
4378      article-decode-encoded-words
4379      article-date-user
4380      article-date-lapsed
4381      article-date-combined-lapsed
4382      article-emphasize
4383      article-treat-dumbquotes
4384      article-treat-non-ascii
4385      article-normalize-headers
4386      ;;(article-show-all . gnus-article-show-all-headers)
4387      )))
4388 \f
4389 ;;;
4390 ;;; Gnus article mode
4391 ;;;
4392
4393 (put 'gnus-article-mode 'mode-class 'special)
4394
4395 (set-keymap-parent gnus-article-mode-map widget-keymap)
4396
4397 (gnus-define-keys gnus-article-mode-map
4398   " " gnus-article-goto-next-page
4399   [?\S-\ ] gnus-article-goto-prev-page
4400   "\177" gnus-article-goto-prev-page
4401   [delete] gnus-article-goto-prev-page
4402   "\C-c^" gnus-article-refer-article
4403   "h" gnus-article-show-summary
4404   "s" gnus-article-show-summary
4405   "\C-c\C-m" gnus-article-mail
4406   "?" gnus-article-describe-briefly
4407   "<" beginning-of-buffer
4408   ">" end-of-buffer
4409   "\C-c\C-i" gnus-info-find-node
4410   "\C-c\C-b" gnus-bug
4411   "R" gnus-article-reply-with-original
4412   "F" gnus-article-followup-with-original
4413   "\C-hk" gnus-article-describe-key
4414   "\C-hc" gnus-article-describe-key-briefly
4415   "\C-hb" gnus-article-describe-bindings
4416
4417   "e" gnus-article-read-summary-keys
4418   "\C-d" gnus-article-read-summary-keys
4419   "\M-*" gnus-article-read-summary-keys
4420   "\M-#" gnus-article-read-summary-keys
4421   "\M-^" gnus-article-read-summary-keys
4422   "\M-g" gnus-article-read-summary-keys)
4423
4424 (substitute-key-definition
4425  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4426
4427 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4428   "W" gnus-article-wide-reply-with-original)
4429 (if (featurep 'xemacs)
4430     (set-keymap-default-binding gnus-article-send-map
4431                                 'gnus-article-read-summary-send-keys)
4432   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4433
4434 (defun gnus-article-make-menu-bar ()
4435   (unless (boundp 'gnus-article-commands-menu)
4436     (gnus-summary-make-menu-bar))
4437   (unless (boundp 'gnus-article-article-menu)
4438     (easy-menu-define
4439      gnus-article-article-menu gnus-article-mode-map ""
4440      '("Article"
4441        ["Scroll forwards" gnus-article-goto-next-page t]
4442        ["Scroll backwards" gnus-article-goto-prev-page t]
4443        ["Show summary" gnus-article-show-summary t]
4444        ["Fetch Message-ID at point" gnus-article-refer-article t]
4445        ["Mail to address at point" gnus-article-mail t]
4446        ["Send a bug report" gnus-bug t]))
4447
4448     (easy-menu-define
4449      gnus-article-treatment-menu gnus-article-mode-map ""
4450      ;; Fixme: this should use :active (and maybe :visible).
4451      '("Treatment"
4452        ["Hide headers" gnus-article-hide-headers t]
4453        ["Hide signature" gnus-article-hide-signature t]
4454        ["Hide citation" gnus-article-hide-citation t]
4455        ["Treat overstrike" gnus-article-treat-overstrike t]
4456        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4457        ["Remove carriage return" gnus-article-remove-cr t]
4458        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4459        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4460        ["Remove base64" gnus-article-de-base64-unreadable t]
4461        ["Treat html" gnus-article-wash-html t]
4462        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4463        ["Decode HZ" gnus-article-decode-HZ t]))
4464
4465     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4466
4467     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4468
4469     (gnus-run-hooks 'gnus-article-menu-hook)))
4470
4471 (defvar bookmark-make-record-function)
4472 (defvar shr-put-image-function)
4473
4474 (define-derived-mode gnus-article-mode fundamental-mode "Article"
4475   "Major mode for displaying an article.
4476
4477 All normal editing commands are switched off.
4478
4479 The following commands are available in addition to all summary mode
4480 commands:
4481 \\<gnus-article-mode-map>
4482 \\[gnus-article-next-page]\t Scroll the article one page forwards
4483 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4484 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4485 \\[gnus-article-show-summary]\t Display the summary buffer
4486 \\[gnus-article-mail]\t Send a reply to the address near point
4487 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4488 \\[gnus-info-find-node]\t Go to the Gnus info node"
4489   (gnus-simplify-mode-line)
4490   (make-local-variable 'minor-mode-alist)
4491   (when (gnus-visual-p 'article-menu 'menu)
4492     (gnus-article-make-menu-bar)
4493     (when gnus-summary-tool-bar-map
4494       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4495   (gnus-update-format-specifications nil 'article-mode)
4496   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4497   (set (make-local-variable 'gnus-page-broken) nil)
4498   (make-local-variable 'gnus-article-current-summary)
4499   (make-local-variable 'gnus-article-mime-handles)
4500   (make-local-variable 'gnus-article-decoded-p)
4501   (make-local-variable 'gnus-article-mime-handle-alist)
4502   (make-local-variable 'gnus-article-wash-types)
4503   (make-local-variable 'gnus-article-image-alist)
4504   (make-local-variable 'gnus-article-charset)
4505   (make-local-variable 'gnus-article-ignored-charsets)
4506   (set (make-local-variable 'bookmark-make-record-function)
4507        'gnus-summary-bookmark-make-record)
4508   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4509   ;; face.
4510   (set (make-local-variable 'nobreak-char-display) nil)
4511   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4512   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4513   (setq cursor-in-non-selected-windows nil)
4514   (gnus-set-default-directory)
4515   (buffer-disable-undo)
4516   (setq buffer-read-only t
4517         show-trailing-whitespace nil)
4518   (mm-enable-multibyte))
4519
4520 (defun gnus-article-setup-buffer ()
4521   "Initialize the article buffer."
4522   (let* ((name (if gnus-single-article-buffer "*Article*"
4523                  (concat "*Article "
4524                          (gnus-group-decoded-name gnus-newsgroup-name)
4525                          "*")))
4526          (original
4527           (progn (string-match "\\*Article" name)
4528                  (concat " *Original Article"
4529                          (substring name (match-end 0))))))
4530     (setq gnus-article-buffer name)
4531     (setq gnus-original-article-buffer original)
4532     (setq gnus-article-mime-handle-alist nil)
4533     (with-current-buffer gnus-summary-buffer
4534       ;; This might be a variable local to the summary buffer.
4535       (unless gnus-single-article-buffer
4536         (setq gnus-article-buffer name)
4537         (setq gnus-original-article-buffer original)
4538         (gnus-set-global-variables)))
4539     (gnus-article-setup-highlight-words)
4540     ;; Init original article buffer.
4541     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4542       (mm-enable-multibyte)
4543       (setq major-mode 'gnus-original-article-mode)
4544       (make-local-variable 'gnus-original-article))
4545     (if (and (get-buffer name)
4546              (with-current-buffer name
4547                (if gnus-article-edit-mode
4548                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4549                        (progn
4550                          (set-buffer-modified-p nil)
4551                          (gnus-kill-buffer name)
4552                          (message "")
4553                          nil)
4554                      (error "Action aborted"))
4555                  t)))
4556         (let ((summary gnus-summary-buffer))
4557           (with-current-buffer name
4558             (set (make-local-variable 'gnus-article-edit-mode) nil)
4559             (gnus-article-stop-animations)
4560             (when gnus-article-mime-handles
4561               (mm-destroy-parts gnus-article-mime-handles)
4562               (setq gnus-article-mime-handles nil))
4563             ;; Set it to nil in article-buffer!
4564             (setq gnus-article-mime-handle-alist nil)
4565             (buffer-disable-undo)
4566             (setq buffer-read-only t)
4567             (unless (derived-mode-p 'gnus-article-mode)
4568               (gnus-article-mode))
4569             (set (make-local-variable 'gnus-summary-buffer) summary)
4570             (setq truncate-lines gnus-article-truncate-lines)
4571             (current-buffer)))
4572       (let ((summary gnus-summary-buffer))
4573         (with-current-buffer (gnus-get-buffer-create name)
4574           (gnus-article-mode)
4575           (setq truncate-lines gnus-article-truncate-lines)
4576           (set (make-local-variable 'gnus-summary-buffer) summary)
4577           (gnus-summary-set-local-parameters gnus-newsgroup-name)
4578           (when article-lapsed-timer
4579             (gnus-stop-date-timer))
4580           (when gnus-article-update-date-headers
4581             (gnus-start-date-timer gnus-article-update-date-headers))
4582           (current-buffer))))))
4583
4584 (defun gnus-article-stop-animations ()
4585   (dolist (timer (and (boundp 'timer-list)
4586                       timer-list))
4587     (when (eq (gnus-timer--function timer) 'image-animate-timeout)
4588       (cancel-timer timer))))
4589
4590 (defun gnus-stop-downloads ()
4591   (when (boundp 'url-queue)
4592     (set (intern "url-queue" obarray) nil)))
4593
4594 ;; Set article window start at LINE, where LINE is the number of lines
4595 ;; from the head of the article.
4596 (defun gnus-article-set-window-start (&optional line)
4597   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4598     (when article-window
4599       (set-window-start
4600        article-window
4601        (with-current-buffer gnus-article-buffer
4602          (goto-char (point-min))
4603          (if (not line)
4604              (point-min)
4605            (gnus-message 6 "Moved to bookmark")
4606            (search-forward "\n\n" nil t)
4607            (forward-line line)
4608            (point)))))))
4609
4610 (defun gnus-article-prepare (article &optional all-headers header)
4611   "Prepare ARTICLE in article mode buffer.
4612 ARTICLE should either be an article number or a Message-ID.
4613 If ARTICLE is an id, HEADER should be the article headers.
4614 If ALL-HEADERS is non-nil, no headers are hidden."
4615   (save-excursion
4616     ;; Make sure we start in a summary buffer.
4617     (unless (derived-mode-p 'gnus-summary-mode)
4618       (set-buffer gnus-summary-buffer))
4619     (setq gnus-summary-buffer (current-buffer))
4620     (let* ((gnus-article (if header (mail-header-number header) article))
4621            (summary-buffer (current-buffer))
4622            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4623            (group gnus-newsgroup-name)
4624            result)
4625       (save-excursion
4626         (gnus-article-setup-buffer)
4627         (set-buffer gnus-article-buffer)
4628         ;; Deactivate active regions.
4629         (when (and (boundp 'transient-mark-mode)
4630                    transient-mark-mode)
4631           (setq mark-active nil))
4632         (if (not (setq result (let ((inhibit-read-only t))
4633                                 (gnus-request-article-this-buffer
4634                                  article group))))
4635             ;; There is no such article.
4636             (save-excursion
4637               (when (and (numberp article)
4638                          (not (memq article gnus-newsgroup-sparse)))
4639                 (setq gnus-article-current
4640                       (cons gnus-newsgroup-name article))
4641                 (set-buffer gnus-summary-buffer)
4642                 (setq gnus-current-article article)
4643                 (if (and (memq article gnus-newsgroup-undownloaded)
4644                          (not (gnus-online (gnus-find-method-for-group
4645                                             gnus-newsgroup-name))))
4646                     (progn
4647                       (gnus-summary-set-agent-mark article)
4648                       (message "Message marked for downloading"))
4649                   (gnus-summary-mark-article article gnus-canceled-mark)
4650                   (unless (memq article gnus-newsgroup-sparse)
4651                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4652           (if (or (eq result 'pseudo)
4653                   (eq result 'nneething))
4654               (progn
4655                 (with-current-buffer summary-buffer
4656                   (push article gnus-newsgroup-history)
4657                   (setq gnus-last-article gnus-current-article
4658                         gnus-current-article 0
4659                         gnus-current-headers nil
4660                         gnus-article-current nil)
4661                   (if (eq result 'nneething)
4662                       (gnus-configure-windows 'summary)
4663                     (gnus-configure-windows 'article))
4664                   (gnus-set-global-variables))
4665                 (let ((gnus-article-mime-handle-alist-1
4666                        gnus-article-mime-handle-alist))
4667                   (gnus-set-mode-line 'article)))
4668             ;; The result from the `request' was an actual article -
4669             ;; or at least some text that is now displayed in the
4670             ;; article buffer.
4671             (when (and (numberp article)
4672                        (not (eq article gnus-current-article)))
4673               ;; Seems like a new article has been selected.
4674               ;; `gnus-current-article' must be an article number.
4675               (with-current-buffer summary-buffer
4676                 (push article gnus-newsgroup-history)
4677                 (setq gnus-last-article gnus-current-article
4678                       gnus-current-article article
4679                       gnus-current-headers
4680                       (gnus-summary-article-header gnus-current-article)
4681                       gnus-article-current
4682                       (cons gnus-newsgroup-name gnus-current-article))
4683                 (unless (vectorp gnus-current-headers)
4684                   (setq gnus-current-headers nil))
4685                 (gnus-summary-goto-subject gnus-current-article)
4686                 (when (gnus-summary-show-thread)
4687                   ;; If the summary buffer really was folded, the
4688                   ;; previous goto may not actually have gone to
4689                   ;; the right article, but the thread root instead.
4690                   ;; So we go again.
4691                   (gnus-summary-goto-subject gnus-current-article))
4692                 (gnus-run-hooks 'gnus-mark-article-hook)
4693                 (gnus-set-mode-line 'summary)
4694                 (when (gnus-visual-p 'article-highlight 'highlight)
4695                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4696                 ;; Set the global newsgroup variables here.
4697                 (gnus-set-global-variables)
4698                 (setq gnus-have-all-headers
4699                       (or all-headers gnus-show-all-headers))))
4700             (save-excursion
4701               (gnus-configure-windows 'article))
4702             (when (or (numberp article)
4703                       (stringp article))
4704               (gnus-article-prepare-display)
4705               ;; Do page break.
4706               (goto-char (point-min))
4707               (when gnus-break-pages
4708                 (gnus-narrow-to-page)))
4709             (let ((gnus-article-mime-handle-alist-1
4710                    gnus-article-mime-handle-alist))
4711               (gnus-set-mode-line 'article))
4712             (article-goto-body)
4713             (unless (bobp)
4714               (forward-line -1))
4715             (set-window-point (get-buffer-window (current-buffer)) (point))
4716             (gnus-configure-windows 'article)
4717             (gnus-run-hooks 'gnus-article-prepare-hook)
4718             t))))))
4719
4720 ;;;###autoload
4721 (defun gnus-article-prepare-display ()
4722   "Make the current buffer look like a nice article."
4723   ;; Hooks for getting information from the article.
4724   ;; This hook must be called before being narrowed.
4725   (let ((gnus-article-buffer (current-buffer))
4726         buffer-read-only
4727         (inhibit-read-only t))
4728     (unless (derived-mode-p 'gnus-article-mode)
4729       (gnus-article-mode))
4730     (setq buffer-read-only nil
4731           gnus-article-wash-types nil
4732           gnus-article-image-alist nil)
4733     (gnus-run-hooks 'gnus-tmp-internal-hook)
4734     (when gnus-display-mime-function
4735       (funcall gnus-display-mime-function))
4736     ;; Add attachment buttons to the header.
4737     (when gnus-mime-display-attachment-buttons-in-header
4738       (gnus-mime-buttonize-attachments-in-header))))
4739
4740 ;;;
4741 ;;; Gnus Sticky Article Mode
4742 ;;;
4743
4744 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4745   "Mode for sticky articles."
4746   ;; Release bindings that won't work.
4747   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4748                              gnus-sticky-article-mode-map)
4749   (substitute-key-definition 'gnus-article-refer-article 'undefined
4750                              gnus-sticky-article-mode-map)
4751   (dolist (k '("e" "h" "s" "F" "R"))
4752     (define-key gnus-sticky-article-mode-map k nil))
4753   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4754   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4755   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4756   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4757
4758 (defun gnus-sticky-article (arg)
4759   "Make the current article sticky.
4760 If a prefix ARG is given, ask for a name for this sticky article buffer."
4761   (interactive "P")
4762   (gnus-summary-show-thread)
4763   (gnus-summary-select-article nil nil 'pseudo)
4764   (let (new-art-buf-name)
4765     (gnus-eval-in-buffer-window gnus-article-buffer
4766       (setq new-art-buf-name
4767             (concat
4768              "*Sticky Article: "
4769              (if arg
4770                  (read-from-minibuffer "Sticky article buffer name: ")
4771                (gnus-with-article-headers
4772                  (gnus-article-goto-header "subject")
4773                  (setq new-art-buf-name
4774                        (buffer-substring-no-properties
4775                         (line-beginning-position) (line-end-position)))
4776                  (goto-char (point-min))
4777                  (gnus-article-goto-header "from")
4778                  (setq new-art-buf-name
4779                        (concat
4780                         new-art-buf-name ", "
4781                         (buffer-substring-no-properties
4782                          (line-beginning-position) (line-end-position))))
4783                  (goto-char (point-min))
4784                  (gnus-article-goto-header "date")
4785                  (setq new-art-buf-name
4786                        (concat
4787                         new-art-buf-name ", "
4788                         (buffer-substring-no-properties
4789                          (line-beginning-position) (line-end-position))))))
4790              "*"))
4791       (if (and (gnus-buffer-live-p new-art-buf-name)
4792                (with-current-buffer new-art-buf-name
4793                  (derived-mode-p 'gnus-sticky-article-mode)))
4794           (switch-to-buffer new-art-buf-name)
4795         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4796       (gnus-sticky-article-mode))
4797     (setq gnus-article-buffer new-art-buf-name))
4798   (gnus-summary-recenter)
4799   (gnus-summary-position-point))
4800
4801 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4802   "Kill the given sticky article BUFFER.
4803 If none is given, assume the current buffer and kill it if it has
4804 `gnus-sticky-article-mode'."
4805   (interactive)
4806   (unless buffer
4807     (setq buffer (current-buffer)))
4808   (with-current-buffer buffer
4809     (when (derived-mode-p 'gnus-sticky-article-mode)
4810       (gnus-kill-buffer buffer))))
4811
4812 (defun gnus-kill-sticky-article-buffers (arg)
4813   "Kill all sticky article buffers.
4814 If a prefix ARG is given, ask for confirmation."
4815   (interactive "P")
4816   (dolist (buf (gnus-buffers))
4817     (with-current-buffer buf
4818       (when (derived-mode-p 'gnus-sticky-article-mode)
4819         (if (not arg)
4820             (gnus-kill-buffer buf)
4821           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4822             (gnus-kill-buffer buf)))))))
4823
4824 ;;;
4825 ;;; Gnus MIME viewing functions
4826 ;;;
4827
4828 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4829   "Format of the MIME buttons.
4830
4831 Valid specifiers include:
4832 %t  The MIME type
4833 %T  MIME type, along with additional info
4834 %n  The `name' parameter
4835 %d  The description, if any
4836 %l  The length of the encoded part
4837 %p  The part identifier number
4838 %e  Dots if the part isn't displayed
4839
4840 General format specifiers can also be used.  See Info node
4841 `(gnus)Formatting Variables'.")
4842
4843 (defvar gnus-mime-button-line-format-alist
4844   '((?t gnus-tmp-type ?s)
4845     (?T gnus-tmp-type-long ?s)
4846     (?n gnus-tmp-name ?s)
4847     (?d gnus-tmp-description ?s)
4848     (?p gnus-tmp-id ?s)
4849     (?l gnus-tmp-length ?d)
4850     (?e gnus-tmp-dots ?s)))
4851
4852 (defvar gnus-mime-button-commands
4853   '((gnus-article-press-button "\r" "Toggle Display")
4854     (gnus-mime-view-part "v" "View Interactively...")
4855     (gnus-mime-view-part-as-type "t" "View As Type...")
4856     (gnus-mime-view-part-as-charset "C" "View As charset...")
4857     (gnus-mime-save-part "o" "Save...")
4858     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4859     (gnus-mime-replace-part "r" "Replace part")
4860     (gnus-mime-delete-part "d" "Delete part")
4861     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4862     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4863     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4864     (gnus-mime-view-part-externally "e" "View Externally")
4865     (gnus-mime-print-part "p" "Print")
4866     (gnus-mime-pipe-part "|" "Pipe To Command...")
4867     (gnus-mime-action-on-part "." "Take action on the part...")))
4868
4869 (defun gnus-article-mime-part-status ()
4870   (if gnus-article-mime-handle-alist-1
4871       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4872           " (1 part)"
4873         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4874     ""))
4875
4876 (defvar gnus-mime-button-map
4877   (let ((map (make-sparse-keymap)))
4878     (define-key map gnus-mouse-2 'gnus-article-push-button)
4879     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4880     (dolist (c gnus-mime-button-commands)
4881       (define-key map (cadr c) (car c)))
4882     map))
4883
4884 (easy-menu-define
4885   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4886   `("MIME Part"
4887     ,@(mapcar (lambda (c)
4888                 (vector (caddr c) (car c) :active t))
4889               gnus-mime-button-commands)))
4890
4891 (defvar gnus-url-button-commands
4892   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4893
4894 (defvar gnus-url-button-map
4895   (let ((map (make-sparse-keymap)))
4896     (dolist (c gnus-url-button-commands)
4897       (define-key map (cadr c) (car c)))
4898     map))
4899
4900 (easy-menu-define
4901   gnus-url-button-menu gnus-url-button-map "URL button menu."
4902   `("Url Button"
4903     ,@(mapcar (lambda (c)
4904                 (vector (caddr c) (car c) :active t))
4905               gnus-url-button-commands)))
4906
4907 (defmacro gnus-bind-safe-url-regexp (&rest body)
4908   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4909   `(let ((mm-w3m-safe-url-regexp
4910           (let ((group (if (and (derived-mode-p 'gnus-article-mode)
4911                                 (gnus-buffer-live-p
4912                                  gnus-article-current-summary))
4913                            (with-current-buffer gnus-article-current-summary
4914                              gnus-newsgroup-name)
4915                          gnus-newsgroup-name)))
4916             (if (cond ((not group)
4917                        ;; Maybe we're in a mml-preview buffer
4918                        ;; and no group is selected.
4919                        t)
4920                       ((stringp gnus-safe-html-newsgroups)
4921                        (string-match gnus-safe-html-newsgroups group))
4922                       ((consp gnus-safe-html-newsgroups)
4923                        (member group gnus-safe-html-newsgroups)))
4924                 nil
4925               mm-w3m-safe-url-regexp))))
4926      ,@body))
4927
4928 (defun gnus-mime-button-menu (event prefix)
4929  "Construct a context-sensitive menu of MIME commands."
4930  (interactive "e\nP")
4931  (save-window-excursion
4932    (let ((pos (event-start event)))
4933      (select-window (posn-window pos))
4934      (goto-char (posn-point pos))
4935      (gnus-article-check-buffer)
4936      (popup-menu gnus-mime-button-menu nil prefix))))
4937
4938 (defun gnus-mime-view-all-parts (&optional handles)
4939   "View all the MIME parts."
4940   (interactive)
4941   (with-current-buffer gnus-article-buffer
4942     (let ((handles (or handles gnus-article-mime-handles))
4943           (mail-parse-charset gnus-newsgroup-charset)
4944           (mail-parse-ignored-charsets
4945            (with-current-buffer gnus-summary-buffer
4946              gnus-newsgroup-ignored-charsets)))
4947       (when handles
4948         (mm-remove-parts handles)
4949         (goto-char (point-min))
4950         (or (search-forward "\n\n") (goto-char (point-max)))
4951         (let ((inhibit-read-only t))
4952           (delete-region (point) (point-max))
4953           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4954
4955 (defun gnus-article-jump-to-part (n)
4956   "Jump to MIME part N."
4957   (interactive "P")
4958   (let ((parts (with-current-buffer gnus-article-buffer
4959                  (length gnus-article-mime-handle-alist))))
4960     (when (zerop parts)
4961       (error "No such part"))
4962     (pop-to-buffer gnus-article-buffer)
4963     (or n
4964         (setq n (if (= parts 1)
4965                     1
4966                   (read-number (format "Jump to part (1..%s): " parts)))))
4967     (unless (and (integerp n) (<= n parts) (>= n 1))
4968       (setq n
4969             (progn
4970               (gnus-message 7 "Invalid part `%s', using %s instead."
4971                             n parts)
4972               parts)))
4973     (gnus-message 9 "Jumping to part %s." n)
4974     (cond ((>= gnus-auto-select-part 1)
4975            (while (and (<= n parts)
4976                        (not (gnus-article-goto-part n)))
4977              (setq n (1+ n))))
4978           ((< gnus-auto-select-part 0)
4979            (while (and (>= n 1)
4980                        (not (gnus-article-goto-part n)))
4981              (setq n (1- n))))
4982           (t
4983            (gnus-article-goto-part n)))))
4984
4985 (defvar gnus-mime-buttonized-part-id nil
4986   "ID of a mime part that should be buttonized.
4987 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4988
4989 (eval-when-compile
4990   (defsubst gnus-article-edit-part (handles &optional current-id)
4991     "Edit an article in order to delete a mime part.
4992 This function is exclusively used by `gnus-mime-save-part-and-strip'
4993 and `gnus-mime-delete-part', and not provided at run-time normally."
4994     (gnus-article-edit-article
4995      `(lambda ()
4996         (buffer-disable-undo)
4997         (let ((mail-parse-charset (or gnus-article-charset
4998                                       ',gnus-newsgroup-charset))
4999               (mail-parse-ignored-charsets
5000                (or gnus-article-ignored-charsets
5001                    ',gnus-newsgroup-ignored-charsets))
5002               (mbl mml-buffer-list))
5003           (setq mml-buffer-list nil)
5004           ;; A new text must be inserted before deleting existing ones
5005           ;; at the end so as not to move existing markers of which
5006           ;; the insertion type is t.
5007           (delete-region
5008            (point-min)
5009            (prog1
5010                (goto-char (point-max))
5011              (insert-buffer-substring gnus-original-article-buffer)))
5012           (mime-to-mml ',handles)
5013           (setq gnus-article-mime-handles nil)
5014           (let ((mbl1 mml-buffer-list))
5015             (setq mml-buffer-list mbl)
5016             (set (make-local-variable 'mml-buffer-list) mbl1))
5017           (gnus-make-local-hook 'kill-buffer-hook)
5018           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
5019      `(lambda (no-highlight)
5020         (let ((mail-parse-charset (or gnus-article-charset
5021                                       ',gnus-newsgroup-charset))
5022               (message-options message-options)
5023               (message-options-set-recipient)
5024               (mail-parse-ignored-charsets
5025                (or gnus-article-ignored-charsets
5026                    ',gnus-newsgroup-ignored-charsets)))
5027           (mml-to-mime)
5028           (mml-destroy-buffers)
5029           (remove-hook 'kill-buffer-hook
5030                        'mml-destroy-buffers t)
5031           (kill-local-variable 'mml-buffer-list))
5032         (gnus-summary-edit-article-done
5033          ,(or (mail-header-references gnus-current-headers) "")
5034          ,(gnus-group-read-only-p)
5035          ,gnus-summary-buffer no-highlight))
5036      t)
5037     ;; Force buttonizing this part.
5038     (let ((gnus-mime-buttonized-part-id current-id))
5039       (gnus-article-edit-done))
5040     (gnus-configure-windows 'article)
5041     (when (and current-id (integerp gnus-auto-select-part))
5042       (gnus-article-jump-to-part
5043        (min (max (+ current-id gnus-auto-select-part) 1)
5044             (with-current-buffer gnus-article-buffer
5045               (length gnus-article-mime-handle-alist)))))))
5046
5047 (defun gnus-mime-replace-part (file)
5048   "Replace MIME part under point with an external body."
5049   ;; Useful if file has already been saved to disk
5050   (interactive
5051    (list
5052     (read-file-name "Replace MIME part with file: "
5053                     (or mm-default-directory default-directory)
5054                     nil nil)))
5055   (gnus-mime-save-part-and-strip file))
5056
5057 (defun gnus-mime-save-part-and-strip (&optional file)
5058   "Save the MIME part under point then replace it with an external body.
5059 If FILE is given, use it for the external part."
5060   (interactive)
5061   (gnus-article-check-buffer)
5062   (when (gnus-group-read-only-p)
5063     (error "The current group does not support deleting of parts"))
5064   (when (mm-complicated-handles gnus-article-mime-handles)
5065     (error "\
5066 The current article has a complicated MIME structure, giving up..."))
5067   (let* ((data (get-text-property (point) 'gnus-data))
5068          (id (get-text-property (point) 'gnus-part))
5069          param
5070          (handles gnus-article-mime-handles))
5071     (unless file
5072       (setq file
5073             (and data (mm-save-part data "Delete MIME part and save to: "))))
5074     (when file
5075       (with-current-buffer (mm-handle-buffer data)
5076         (erase-buffer)
5077         (insert "Content-Type: " (mm-handle-media-type data))
5078         (mml-insert-parameter-string (cdr (mm-handle-type data))
5079                                      '(charset))
5080         ;; Add a filename for the sake of saving the part again.
5081         (mml-insert-parameter
5082          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5083         (insert "\n")
5084         (insert "Content-ID: " (message-make-message-id) "\n")
5085         (insert "Content-Transfer-Encoding: binary\n")
5086         (insert "\n"))
5087       (setcdr data
5088               (cdr (mm-make-handle nil
5089                                    `("message/external-body"
5090                                      (access-type . "LOCAL-FILE")
5091                                      (name . ,file)))))
5092       ;; (set-buffer gnus-summary-buffer)
5093       (gnus-article-edit-part handles id))))
5094
5095 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5096 ;; parts...>') but with stripping would be nice.
5097
5098 (defun gnus-mime-delete-part ()
5099   "Delete the MIME part under point.
5100 Replace it with some information about the removed part."
5101   (interactive)
5102   (gnus-article-check-buffer)
5103   (when (gnus-group-read-only-p)
5104     (error "The current group does not support deleting of parts"))
5105   (when (mm-complicated-handles gnus-article-mime-handles)
5106     (error "\
5107 The current article has a complicated MIME structure, giving up..."))
5108   (when (or gnus-expert-user
5109             (gnus-yes-or-no-p "\
5110 Deleting parts may malfunction or destroy the article; continue? "))
5111     (let* ((data (get-text-property (point) 'gnus-data))
5112            (id (get-text-property (point) 'gnus-part))
5113            (handles gnus-article-mime-handles)
5114            (description
5115             (let ((desc (mm-handle-description data)))
5116               (when desc
5117                 (mail-decode-encoded-word-string desc))))
5118            (filename (or (mm-handle-filename data) "(none)"))
5119            (type (mm-handle-media-type data)))
5120       (unless data
5121         (error "No MIME part under point"))
5122       (with-current-buffer (mm-handle-buffer data)
5123         (let ((bsize (buffer-size)))
5124           (erase-buffer)
5125           (insert
5126            (concat
5127             ",----\n"
5128             "| The following attachment has been deleted:\n"
5129             "|\n"
5130             "| Type:           " type "\n"
5131             "| Filename:       " filename "\n"
5132             "| Size (encoded): " (format "%s byte%s\n"
5133                                          bsize (if (= bsize 1)
5134                                                    ""
5135                                                  "s"))
5136             (when description
5137               (concat    "| Description:    " description "\n"))
5138             "`----\n"))
5139           (setcdr data
5140                   (cdr (mm-make-handle
5141                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5142                         (list "attachment")
5143                         (format "Deleted attachment (%s bytes)" bsize))))))
5144       ;; (set-buffer gnus-summary-buffer)
5145       (gnus-article-edit-part handles id))))
5146
5147 (defun gnus-mime-save-part ()
5148   "Save the MIME part under point."
5149   (interactive)
5150   (gnus-article-check-buffer)
5151   (let ((data (get-text-property (point) 'gnus-data)))
5152     (when data
5153       (mm-save-part data))))
5154
5155 (defun gnus-mime-pipe-part (&optional cmd)
5156   "Pipe the MIME part under point to a process.
5157 Use CMD as the process."
5158   (interactive)
5159   (gnus-article-check-buffer)
5160   (let ((data (get-text-property (point) 'gnus-data)))
5161     (when data
5162       (mm-pipe-part data cmd))))
5163
5164 (defun gnus-mime-view-part ()
5165   "Interactively choose a viewing method for the MIME part under point."
5166   (interactive)
5167   (gnus-article-check-buffer)
5168   (let ((data (get-text-property (point) 'gnus-data)))
5169     (when data
5170       (setq gnus-article-mime-handles
5171             (mm-merge-handles
5172              gnus-article-mime-handles (setq data (copy-sequence data))))
5173       (mm-interactively-view-part data))))
5174
5175 (defun gnus-mime-view-part-as-type-internal ()
5176   (gnus-article-check-buffer)
5177   (let* ((handle (get-text-property (point) 'gnus-data))
5178          (name (or
5179                 ;; Content-Type: foo/bar; name=...
5180                 (mail-content-type-get (mm-handle-type handle) 'name)
5181                 ;; Content-Disposition: attachment; filename=...
5182                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5183          (def-type (and name (mm-default-file-encoding name))))
5184     (or (and def-type (cons def-type 0))
5185         (and handle
5186              (equal (mm-handle-media-supertype handle) "text")
5187              '("text/plain" . 0))
5188         '("application/octet-stream" . 0))))
5189
5190 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5191   "Choose a MIME media type, and view the part as such.
5192 If non-nil, PRED is a predicate to use during completion to limit the
5193 available media-types."
5194   (interactive)
5195   (unless mime-type
5196     (setq mime-type
5197           (let ((default (gnus-mime-view-part-as-type-internal)))
5198             (gnus-completing-read
5199              "View as MIME type"
5200              (if pred
5201                  (gnus-remove-if-not pred (mailcap-mime-types))
5202                (mailcap-mime-types))
5203              nil nil nil
5204              (car default)))))
5205   (gnus-article-check-buffer)
5206   (let ((handle (get-text-property (point) 'gnus-data)))
5207     (when handle
5208       (when (equal (mm-handle-media-type handle) "message/external-body")
5209         (unless (mm-handle-cache handle)
5210           (mm-extern-cache-contents handle))
5211         (setq handle (mm-handle-cache handle)))
5212       (setq handle
5213             (mm-make-handle (mm-handle-buffer handle)
5214                             (cons mime-type (cdr (mm-handle-type handle)))
5215                             (mm-handle-encoding handle)
5216                             (mm-handle-undisplayer handle)
5217                             (mm-handle-disposition handle)
5218                             (mm-handle-description handle)
5219                             nil
5220                             (mm-handle-id handle)))
5221       (setq gnus-article-mime-handles
5222             (mm-merge-handles gnus-article-mime-handles handle))
5223       (when (mm-handle-displayed-p handle)
5224         (mm-remove-part handle))
5225       (gnus-mm-display-part handle))))
5226
5227 (defun gnus-mime-copy-part (&optional handle arg)
5228   "Put the MIME part under point into a new buffer.
5229 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5230 are decompressed."
5231   (interactive (list nil current-prefix-arg))
5232   (gnus-article-check-buffer)
5233   (unless handle
5234     (setq handle (get-text-property (point) 'gnus-data)))
5235   (when handle
5236     (let ((filename (mm-handle-filename handle))
5237           contents dont-decode charset coding-system)
5238       (mm-with-unibyte-buffer
5239         (mm-insert-part handle)
5240         (setq contents (or (condition-case nil
5241                                (mm-decompress-buffer filename nil 'sig)
5242                              (error
5243                               (setq dont-decode t)
5244                               nil))
5245                            (buffer-string))))
5246       (setq filename (cond (filename (file-name-nondirectory filename))
5247                            (dont-decode "*raw data*")
5248                            (t "*decoded*")))
5249       (cond
5250        (dont-decode)
5251        ((not arg)
5252         (unless (setq charset (mail-content-type-get
5253                                (mm-handle-type handle) 'charset))
5254           (unless (setq coding-system (mm-with-unibyte-buffer
5255                                         (insert contents)
5256                                         (mm-find-buffer-file-coding-system)))
5257             (setq charset gnus-newsgroup-charset))))
5258        ((numberp arg)
5259         (setq charset (or (cdr (assq arg
5260                                      gnus-summary-show-article-charset-alist))
5261                           (mm-read-coding-system "Charset: ")))))
5262       (switch-to-buffer (generate-new-buffer filename))
5263       (if (or coding-system
5264               (and charset
5265                    (setq coding-system (mm-charset-to-coding-system
5266                                         charset nil t))
5267                    (not (eq coding-system 'ascii))))
5268           (progn
5269             (mm-enable-multibyte)
5270             (insert (mm-decode-coding-string contents coding-system))
5271             (setq buffer-file-coding-system
5272                   (if (boundp 'last-coding-system-used)
5273                       (symbol-value 'last-coding-system-used)
5274                     coding-system)))
5275         (mm-disable-multibyte)
5276         (insert contents)
5277         (setq buffer-file-coding-system mm-binary-coding-system))
5278       ;; We do it this way to make `normal-mode' set the appropriate mode.
5279       (unwind-protect
5280           (progn
5281             (setq buffer-file-name (expand-file-name filename))
5282             (normal-mode))
5283         (setq buffer-file-name nil))
5284       (goto-char (point-min)))))
5285
5286 (defun gnus-mime-print-part (&optional handle filename)
5287   "Print the MIME part under point."
5288   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5289   (gnus-article-check-buffer)
5290   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5291          (contents (and handle (mm-get-part handle)))
5292          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5293          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5294     (when contents
5295         (if printer
5296             (unwind-protect
5297                 (progn
5298                   (mm-save-part-to-file handle file)
5299                   (call-process shell-file-name nil
5300                                 (generate-new-buffer " *mm*")
5301                                 nil
5302                                 shell-command-switch
5303                                 (mm-mailcap-command
5304                                  printer file (mm-handle-type handle))))
5305               (delete-file file))
5306           (with-temp-buffer
5307             (insert contents)
5308             (gnus-print-buffer))
5309           (ps-despool filename)))))
5310
5311 (defun gnus-mime-inline-part (&optional handle arg)
5312   "Insert the MIME part under point into the current buffer.
5313 Compressed files like .gz and .bz2 are decompressed."
5314   (interactive (list nil current-prefix-arg))
5315   (gnus-article-check-buffer)
5316   (let* ((inhibit-read-only t)
5317          (b (point))
5318          (btn ;; position where the MIME button exists
5319           (if handle
5320               (if (eq handle (get-text-property b 'gnus-data))
5321                   b
5322                 (article-goto-body)
5323                 (or (text-property-any (point) (point-max) 'gnus-data handle)
5324                     (text-property-any (point-min) (point) 'gnus-data handle)))
5325             (setq handle (get-text-property b 'gnus-data))
5326             b))
5327          start contents charset coding-system)
5328     (when handle
5329       (when (= b (prog1
5330                      btn
5331                    (setq start (next-single-property-change btn 'gnus-data
5332                                                             nil (point-max))
5333                          btn (previous-single-property-change start
5334                                                               'gnus-data))))
5335         (setq b btn))
5336       (if (and (not arg) (mm-handle-undisplayer handle))
5337           (mm-remove-part handle)
5338         (mm-with-unibyte-buffer
5339           (mm-insert-part handle)
5340           (setq contents
5341                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5342                     (buffer-string))))
5343         (cond
5344          ((not arg)
5345           (unless (setq charset (mail-content-type-get
5346                                  (mm-handle-type handle) 'charset))
5347             (unless (setq coding-system
5348                           (mm-with-unibyte-buffer
5349                             (insert contents)
5350                             (mm-find-buffer-file-coding-system)))
5351               (setq charset gnus-newsgroup-charset))))
5352          ((numberp arg)
5353           (if (mm-handle-undisplayer handle)
5354               (mm-remove-part handle))
5355           (setq charset
5356                 (or (cdr (assq arg
5357                                gnus-summary-show-article-charset-alist))
5358                     (mm-read-coding-system "Charset: "))))
5359          ((mm-handle-undisplayer handle)
5360           (mm-remove-part handle)))
5361         (goto-char start)
5362         (unless (bolp)
5363           ;; This is a header button.
5364           (forward-line 1))
5365         (mm-display-inline handle))
5366       ;; Toggle the button appearance between `[button]...' and `[button]'.
5367       (goto-char btn)
5368       (let ((displayed-p (mm-handle-displayed-p handle)))
5369         (gnus-insert-mime-button handle (get-text-property btn 'gnus-part)
5370                                  (list displayed-p))
5371         (if (featurep 'emacs)
5372             (delete-region
5373              (point)
5374              (next-single-property-change (point) 'gnus-data nil (point-max)))
5375           (let* ((end (next-single-property-change (point) 'gnus-data))
5376                  (annots (annotations-at (or end (point-max)))))
5377             (delete-region (point)
5378                            (if end
5379                                (if annots (1+ end) end)
5380                              (point-max)))
5381             (dolist (annot annots)
5382               (set-extent-endpoints annot (point) (point)))))
5383         (setq start (point))
5384         (if (search-backward "\n\n" nil t)
5385             (progn
5386               (goto-char start)
5387               (unless (or displayed-p (eolp))
5388                 ;; Add extra newline.
5389                 (insert (propertize (buffer-substring (1- start) start)
5390                                     'gnus-undeletable t))))
5391           ;; We're in the article header.
5392           (delete-char -1)
5393           (dolist (ovl (gnus-overlays-in btn (point)))
5394             (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
5395             (gnus-overlay-put ovl 'face nil))
5396           (save-restriction
5397             (message-narrow-to-field)
5398             (let ((gnus-treatment-function-alist
5399                    '((gnus-treat-highlight-headers
5400                       gnus-article-highlight-headers))))
5401               (gnus-treat-article 'head)))))
5402       (goto-char b))))
5403
5404 (defun gnus-mime-set-charset-parameters (handle charset)
5405   "Set CHARSET to parameters in HANDLE.
5406 CHARSET may either be a string or a symbol."
5407   (unless (stringp charset)
5408     (setq charset (symbol-name charset)))
5409   (if (stringp (car handle))
5410       (dolist (h (cdr handle))
5411         (gnus-mime-set-charset-parameters h charset))
5412     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5413                                             "message/external-body")
5414                                      (progn
5415                                        (unless (mm-handle-cache handle)
5416                                          (mm-extern-cache-contents handle))
5417                                        (mm-handle-cache handle))
5418                                    handle)))
5419            (param (assq 'charset (cdr type))))
5420       (if param
5421           (setcdr param charset)
5422         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5423
5424 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5425   "Insert the MIME part under point into the current buffer using the
5426 specified charset."
5427   (interactive (list nil current-prefix-arg))
5428   (gnus-article-check-buffer)
5429   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5430         (fun (get-text-property (point) 'gnus-callback))
5431         (gnus-newsgroup-ignored-charsets 'gnus-all)
5432         charset form preferred parts)
5433     (when handle
5434       (when (prog1
5435                 (and fun
5436                      (setq charset
5437                            (or (cdr (assq
5438                                      arg
5439                                      gnus-summary-show-article-charset-alist))
5440                                (mm-read-coding-system "Charset: "))))
5441               (if (mm-handle-undisplayer handle)
5442                   (mm-remove-part handle)))
5443         (gnus-mime-set-charset-parameters handle charset)
5444         (when (and (consp (setq form (cdr-safe fun)))
5445                    (setq form (ignore-errors
5446                                 (assq 'gnus-mime-display-alternative form)))
5447                    (setq preferred (caddr form))
5448                    (progn
5449                      (when (eq (car preferred) 'quote)
5450                        (setq preferred (cadr preferred)))
5451                      (not (equal preferred
5452                                  (get-text-property (point) 'gnus-data))))
5453                    (setq parts (get-text-property (point) 'gnus-part))
5454                    (setq parts (cdr (assq parts
5455                                           gnus-article-mime-handle-alist)))
5456                    (equal (mm-handle-media-type parts) "multipart/alternative")
5457                    (setq parts (reverse (cdr parts))))
5458           (setcar (cddr form)
5459                   (list 'quote (or (cadr (member preferred parts))
5460                                    (car parts)))))
5461         (funcall fun handle)))))
5462
5463 (defun gnus-mime-view-part-externally (&optional handle)
5464   "View the MIME part under point with an external viewer."
5465   (interactive)
5466   (gnus-article-check-buffer)
5467   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5468          (mm-user-display-methods nil)
5469          (mm-inlined-types nil)
5470          (mail-parse-charset gnus-newsgroup-charset)
5471          (mail-parse-ignored-charsets
5472           (with-current-buffer gnus-summary-buffer
5473             gnus-newsgroup-ignored-charsets))
5474          (type (mm-handle-media-type handle))
5475          (method (mailcap-mime-info type))
5476          (mm-enable-external t))
5477     (if (not (stringp method))
5478         (gnus-mime-view-part-as-type
5479          nil (lambda (type) (stringp (mailcap-mime-info type))))
5480       (when handle
5481         (mm-display-part handle nil t)))))
5482
5483 (defun gnus-mime-view-part-internally (&optional handle)
5484   "View the MIME part under point with an internal viewer.
5485 If no internal viewer is available, use an external viewer."
5486   (interactive)
5487   (gnus-article-check-buffer)
5488   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5489          (mm-inlined-types '(".*"))
5490          (mm-inline-large-images t)
5491          (mail-parse-charset gnus-newsgroup-charset)
5492          (mail-parse-ignored-charsets
5493           (with-current-buffer gnus-summary-buffer
5494             gnus-newsgroup-ignored-charsets))
5495          (inhibit-read-only t))
5496     (if (not (mm-inlinable-p handle))
5497         (gnus-mime-view-part-as-type
5498          nil (lambda (type) (mm-inlinable-p handle type)))
5499       (when handle
5500         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5501
5502 (defun gnus-mime-action-on-part (&optional action)
5503   "Do something with the MIME attachment at \(point\)."
5504   (interactive
5505    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5506   (gnus-article-check-buffer)
5507   (let ((action-pair (assoc action gnus-mime-action-alist)))
5508     (if action-pair
5509         (funcall (cdr action-pair)))))
5510
5511 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5512   "Call FUNCTION on MIME part N.
5513 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5514 If INTERACTIVE, call FUNCTION interactively."
5515   (let (window frame)
5516     ;; Check whether the article is displayed.
5517     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5518                  (setq window (get-buffer-window gnus-article-buffer t))
5519                  (frame-visible-p (setq frame (window-frame window))))
5520       (error "No article is displayed"))
5521     (with-current-buffer gnus-article-buffer
5522       ;; Check whether the article displays the right contents.
5523       (unless (with-current-buffer gnus-summary-buffer
5524                 (eq gnus-current-article (gnus-summary-article-number)))
5525         (error "You should select the right article first"))
5526       (if n
5527           (setq n (prefix-numeric-value n))
5528         (let ((pt (point)))
5529           (setq n (or (get-text-property pt 'gnus-part)
5530                       (and (not (bobp))
5531                            (get-text-property (1- pt) 'gnus-part))
5532                       (get-text-property (prog2
5533                                              (forward-line 1)
5534                                              (point)
5535                                            (goto-char pt))
5536                                          'gnus-part)
5537                       (get-text-property
5538                        (or (and (setq pt (previous-single-property-change
5539                                           pt 'gnus-part))
5540                                 (1- pt))
5541                            (next-single-property-change (point) 'gnus-part)
5542                            (point))
5543                        'gnus-part)
5544                       1))))
5545       ;; Check whether the specified part exists.
5546       (when (> n (length gnus-article-mime-handle-alist))
5547         (error "No such part")))
5548     (unless
5549         (progn
5550           ;; To select the window is needed so that the cursor
5551           ;; might be visible on the MIME button.
5552           (select-window (prog1
5553                              window
5554                            (setq window (selected-window))
5555                            ;; Article may be displayed in the other frame.
5556                            (gnus-select-frame-set-input-focus
5557                             (prog1
5558                                 frame
5559                               (setq frame (selected-frame))))))
5560           (when (gnus-article-goto-part n)
5561             ;; We point the cursor and the arrow at the MIME button
5562             ;; when the `function' prompt the user for something.
5563             (unless (and (pos-visible-in-window-p)
5564                          (> (count-lines (point) (window-end))
5565                             (/ (1- (window-height)) 3)))
5566               (recenter (/ (1- (window-height)) 3)))
5567             (let ((cursor-in-non-selected-windows t)
5568                   (overlay-arrow-string "=>")
5569                   (overlay-arrow-position (point-marker)))
5570               (unwind-protect
5571                   (cond
5572                    ((and no-handle interactive)
5573                     (call-interactively function))
5574                    (no-handle
5575                     (funcall function))
5576                    (interactive
5577                     (call-interactively
5578                      function (get-text-property (point) 'gnus-data)))
5579                    (t
5580                     (funcall function
5581                              (get-text-property (point) 'gnus-data))))
5582                 (set-marker overlay-arrow-position nil)
5583                 (unless gnus-auto-select-part
5584                   (gnus-select-frame-set-input-focus frame)
5585                   (select-window window))))
5586             t))
5587       (if gnus-inhibit-mime-unbuttonizing
5588           ;; This is the default though the program shouldn't reach here.
5589           (error "No such part")
5590         ;; The part which doesn't have the MIME button is selected.
5591         ;; So, we display all the buttons and redo it.
5592         (let ((gnus-inhibit-mime-unbuttonizing t))
5593           (gnus-summary-show-article)
5594           (gnus-article-part-wrapper n function no-handle))))))
5595
5596 (defun gnus-article-pipe-part (n)
5597   "Pipe MIME part N, which is the numerical prefix."
5598   (interactive "P")
5599   (gnus-article-part-wrapper n 'mm-pipe-part))
5600
5601 (defun gnus-article-save-part (n)
5602   "Save MIME part N, which is the numerical prefix."
5603   (interactive "P")
5604   (gnus-article-part-wrapper n 'mm-save-part))
5605
5606 (defun gnus-article-interactively-view-part (n)
5607   "View MIME part N interactively, which is the numerical prefix."
5608   (interactive "P")
5609   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5610
5611 (defun gnus-article-copy-part (n)
5612   "Copy MIME part N, which is the numerical prefix."
5613   (interactive "P")
5614   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5615
5616 (defun gnus-article-view-part-as-charset (n)
5617   "View MIME part N using a specified charset.
5618 N is the numerical prefix."
5619   (interactive "P")
5620   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5621
5622 (defun gnus-article-view-part-externally (n)
5623   "View MIME part N externally, which is the numerical prefix."
5624   (interactive "P")
5625   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5626
5627 (defun gnus-article-inline-part (n)
5628   "Inline MIME part N, which is the numerical prefix."
5629   (interactive "P")
5630   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5631
5632 (defun gnus-article-save-part-and-strip (n)
5633   "Save MIME part N and replace it with an external body.
5634 N is the numerical prefix."
5635   (interactive "P")
5636   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5637
5638 (defun gnus-article-replace-part (n)
5639   "Replace MIME part N with an external body.
5640 N is the numerical prefix."
5641   (interactive "P")
5642   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5643
5644 (defun gnus-article-delete-part (n)
5645   "Delete MIME part N and add some information about the removed part.
5646 N is the numerical prefix."
5647   (interactive "P")
5648   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5649
5650 (defun gnus-article-view-part-as-type (n)
5651   "Choose a MIME media type, and view part N as such.
5652 N is the numerical prefix."
5653   (interactive "P")
5654   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5655
5656 (defun gnus-article-mime-match-handle-first (condition)
5657   (if condition
5658       (let (n)
5659         (dolist (ihandle gnus-article-mime-handle-alist)
5660           (if (and (cond
5661                     ((functionp condition)
5662                      (funcall condition (cdr ihandle)))
5663                     ((eq condition 'undisplayed)
5664                      (not (or (mm-handle-undisplayer (cdr ihandle))
5665                               (equal (mm-handle-media-type (cdr ihandle))
5666                                      "multipart/alternative"))))
5667                     ((eq condition 'undisplayed-alternative)
5668                      (not (mm-handle-undisplayer (cdr ihandle))))
5669                     (t t))
5670                    (gnus-article-goto-part (car ihandle))
5671                    (or (not n) (< (car ihandle) n)))
5672               (setq n (car ihandle))))
5673         (or n 1))
5674     1))
5675
5676 (defun gnus-article-view-part (&optional n)
5677   "View MIME part N, which is the numerical prefix.
5678 If the part is already shown, hide the part.  If N is nil, view
5679 all parts."
5680   (interactive "P")
5681   (with-current-buffer gnus-article-buffer
5682     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5683                              gnus-article-mime-match-handle-function)))
5684     (when (> n (length gnus-article-mime-handle-alist))
5685       (error "No such part"))
5686     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5687       (when (gnus-article-goto-part n)
5688         (if (equal (car handle) "multipart/alternative")
5689             (progn
5690               (beginning-of-line) ;; Make it toggle subparts
5691               (gnus-article-press-button))
5692           (when (eq (gnus-mm-display-part handle) 'internal)
5693             (gnus-set-window-start)))))))
5694
5695 (defsubst gnus-article-mime-total-parts ()
5696   (if (bufferp (car gnus-article-mime-handles))
5697       1 ;; single part
5698     (1- (length gnus-article-mime-handles))))
5699
5700 (defun gnus-mm-display-part (handle)
5701   "Display HANDLE and fix MIME button."
5702   (let ((id (get-text-property (point) 'gnus-part))
5703         (point (point))
5704         (inhibit-read-only t)
5705         (window (selected-window))
5706         (mail-parse-charset gnus-newsgroup-charset)
5707         (mail-parse-ignored-charsets
5708          (if (gnus-buffer-live-p gnus-summary-buffer)
5709              (with-current-buffer gnus-summary-buffer
5710                gnus-newsgroup-ignored-charsets)
5711            nil))
5712         start retval)
5713     (unwind-protect
5714         (progn
5715           (let ((win (gnus-get-buffer-window (current-buffer) t)))
5716             (when win
5717               (select-window win)
5718               (goto-char point)))
5719           (setq start (next-single-property-change point 'gnus-data
5720                                                    nil (point-max))
5721                 point (previous-single-property-change start 'gnus-data))
5722           (if (mm-handle-displayed-p handle)
5723               ;; This will remove the part.
5724               (setq retval (mm-display-part handle))
5725             (let ((part (or (and (mm-inlinable-p handle)
5726                                  (mm-inlined-p handle)
5727                                  t)
5728                             (with-temp-buffer
5729                               (gnus-bind-safe-url-regexp
5730                                (setq retval (mm-display-part handle)))
5731                               (unless (zerop (buffer-size))
5732                                 (buffer-string))))))
5733               (goto-char start)
5734               (unless (bolp)
5735                 ;; This is a header button.
5736                 (forward-line 1))
5737               (cond ((stringp part)
5738                      (save-restriction
5739                        (narrow-to-region (point)
5740                                          (progn
5741                                            (insert part)
5742                                            (unless (bolp) (insert "\n"))
5743                                            (point)))
5744                        (gnus-treat-article nil id
5745                                            (gnus-article-mime-total-parts)
5746                                            (mm-handle-media-type handle))
5747                        (mm-handle-set-undisplayer
5748                         handle
5749                         `(lambda ()
5750                            (let ((inhibit-read-only t))
5751                              (delete-region ,(copy-marker (point-min) t)
5752                                             ,(point-max-marker)))))))
5753                     (part
5754                      (mm-display-inline handle))))))
5755       (goto-char point)
5756       ;; Toggle the button appearance between `[button]...' and `[button]'.
5757       (let ((displayed-p (mm-handle-displayed-p handle)))
5758         (gnus-insert-mime-button handle id (list displayed-p))
5759         (if (featurep 'emacs)
5760             (delete-region
5761              (point)
5762              (next-single-property-change (point) 'gnus-data nil (point-max)))
5763           (let* ((end (next-single-property-change (point) 'gnus-data))
5764                  (annots (annotations-at (or end (point-max)))))
5765             (delete-region (point)
5766                            (if end
5767                                (if annots (1+ end) end)
5768                              (point-max)))
5769             (dolist (annot annots)
5770               (set-extent-endpoints annot (point) (point)))))
5771         (setq start (point))
5772         (if (search-backward "\n\n" nil t)
5773             (progn
5774               (goto-char start)
5775               (unless (or displayed-p (eolp))
5776                 ;; Add extra newline.
5777                 (insert (propertize (buffer-substring (1- start) start)
5778                                     'gnus-undeletable t))))
5779           ;; We're in the article header.
5780           (delete-char -1)
5781           (dolist (ovl (gnus-overlays-in point (point)))
5782             (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
5783             (gnus-overlay-put ovl 'face nil))
5784           (save-restriction
5785             (message-narrow-to-field)
5786             (let ((gnus-treatment-function-alist
5787                    '((gnus-treat-highlight-headers
5788                       gnus-article-highlight-headers))))
5789               (gnus-treat-article 'head)))))
5790       (goto-char point)
5791       (if (window-live-p window)
5792           (select-window window)))
5793     retval))
5794
5795 (defun gnus-article-goto-part (n)
5796   "Go to MIME part N."
5797   (when gnus-break-pages
5798     (widen))
5799   (article-goto-body)
5800   (prog1
5801       (let ((start (or (text-property-any (point) (point-max) 'gnus-part n)
5802                        ;; There may be header buttons.
5803                        (text-property-any (point-min) (point) 'gnus-part n)))
5804             part handle end next handles)
5805         (when start
5806           (goto-char start)
5807           (if (setq handle (get-text-property start 'gnus-data))
5808               start
5809             ;; Go to the displayed subpart, assuming this is
5810             ;; multipart/alternative.
5811             (setq part start
5812                   end (point-at-eol))
5813             (while (and (not handle)
5814                         part
5815                         (< part end)
5816                         (setq next (text-property-not-all part end
5817                                                           'gnus-data nil)))
5818               (setq part next
5819                     handle (get-text-property part 'gnus-data))
5820               (push (cons handle part) handles)
5821               (unless (mm-handle-displayed-p handle)
5822                 (setq handle nil
5823                       part (text-property-any part end 'gnus-data nil))))
5824             (unless handle
5825               ;; No subpart is displayed, so we find preferred one.
5826               (setq part
5827                     (cdr (assq (mm-preferred-alternative
5828                                 (nreverse (mapcar 'car handles)))
5829                                handles))))
5830             (if part
5831                 (goto-char (1+ part))
5832               start))))
5833     (when gnus-break-pages
5834       (gnus-narrow-to-page))))
5835
5836 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5837   (let ((gnus-tmp-name
5838          (or (mm-handle-filename handle)
5839              (mail-content-type-get (mm-handle-type handle) 'url)
5840              ""))
5841         (gnus-tmp-type (mm-handle-media-type handle))
5842         (gnus-tmp-description (or (mm-handle-description handle) ""))
5843         (gnus-tmp-dots
5844          (if (if displayed (car displayed)
5845                (mm-handle-displayed-p handle))
5846              "" "..."))
5847         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5848                            (buffer-size)))
5849         gnus-tmp-type-long b e)
5850     (when (string-match ".*/" gnus-tmp-name)
5851       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5852     (setq gnus-tmp-type-long (concat gnus-tmp-type
5853                                      (and (not (equal gnus-tmp-name ""))
5854                                           (concat "; " gnus-tmp-name))))
5855     (unless (equal gnus-tmp-description "")
5856       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5857     (setq b (point))
5858     (gnus-eval-format
5859      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5860      `(keymap ,gnus-mime-button-map
5861               gnus-callback gnus-mm-display-part
5862               gnus-part ,gnus-tmp-id
5863               article-type annotation
5864               gnus-data ,handle
5865               rear-nonsticky t))
5866     (setq e (if (bolp)
5867                 ;; Exclude a newline.
5868                 (1- (point))
5869               (point)))
5870     (when gnus-article-button-face
5871       (gnus-overlay-put (gnus-make-overlay b e nil t)
5872                         'face gnus-article-button-face))
5873     (widget-convert-button
5874      'link b e
5875      :mime-handle handle
5876      :action 'gnus-widget-press-button
5877      :button-keymap gnus-mime-button-map
5878      :help-echo
5879      (lambda (widget)
5880        ;; Needed to properly clear the message due to a bug in
5881        ;; wid-edit (XEmacs only).
5882        (if (boundp 'help-echo-owns-message)
5883            (setq help-echo-owns-message t))
5884        (format
5885         "%S: %s the MIME part; %S: more options"
5886         (aref gnus-mouse-2 0)
5887         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5888             "hide" "show")
5889         (aref gnus-down-mouse-3 0))))))
5890
5891 (defun gnus-widget-press-button (elems el)
5892   (goto-char (widget-get elems :from))
5893   (gnus-article-press-button))
5894
5895 (defvar gnus-displaying-mime nil)
5896
5897 (defun gnus-display-mime (&optional ihandles)
5898   "Display the MIME parts."
5899   (save-excursion
5900     (save-selected-window
5901       (let ((window (get-buffer-window gnus-article-buffer))
5902             (point (point)))
5903         (when window
5904           (select-window window)
5905           ;; We have to do this since selecting the window
5906           ;; may change the point.  So we set the window point.
5907           (set-window-point window point)))
5908       (let ((handles ihandles)
5909             (inhibit-read-only t)
5910             handle)
5911         (cond (handles)
5912               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5913                (when gnus-article-emulate-mime
5914                  (mm-uu-dissect-text-parts handles)))
5915               (gnus-article-emulate-mime
5916                (setq handles (mm-uu-dissect))))
5917         (when (and (not ihandles)
5918                    (not gnus-displaying-mime))
5919           ;; Top-level call; we clean up.
5920           (when gnus-article-mime-handles
5921             (mm-destroy-parts gnus-article-mime-handles)
5922             (setq gnus-article-mime-handle-alist nil));; A trick.
5923           (setq gnus-article-mime-handles handles)
5924           ;; We allow users to glean info from the handles.
5925           (when gnus-article-mime-part-function
5926             (gnus-mime-part-function handles)))
5927         (if (and handles
5928                  (or (not (stringp (car handles)))
5929                      (cdr handles)))
5930             (progn
5931               (when (and (not ihandles)
5932                          (not gnus-displaying-mime))
5933                 ;; Clean up for mime parts.
5934                 (article-goto-body)
5935                 (delete-region (point) (point-max)))
5936               (let ((gnus-displaying-mime t))
5937                 (gnus-mime-display-part handles)))
5938           (save-restriction
5939             (article-goto-body)
5940             (narrow-to-region (point) (point-max))
5941             (gnus-treat-article nil 1 1 "text/plain")
5942             (widen)))
5943         (unless ihandles
5944           ;; Highlight the headers.
5945           (save-excursion
5946             (save-restriction
5947               (article-goto-body)
5948               (narrow-to-region (point-min) (point))
5949               (gnus-article-save-original-date
5950                (gnus-treat-article 'head)))))))
5951     ;; Cope with broken MIME messages.
5952     (goto-char (point-max))
5953     (unless (bolp)
5954       (insert "\n"))))
5955
5956 (defcustom gnus-mime-display-multipart-as-mixed nil
5957   "Display \"multipart\" parts as  \"multipart/mixed\".
5958
5959 If t, it overrides nil values of
5960 `gnus-mime-display-multipart-alternative-as-mixed' and
5961 `gnus-mime-display-multipart-related-as-mixed'."
5962   :group 'gnus-article-mime
5963   :type 'boolean)
5964
5965 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5966   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5967   :version "22.1"
5968   :group 'gnus-article-mime
5969   :type 'boolean)
5970
5971 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5972   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5973
5974 If displaying \"text/html\" is discouraged \(see
5975 `mm-discouraged-alternatives'\) images or other material inside a
5976 \"multipart/related\" part might be overlooked when this variable is nil."
5977   :version "22.1"
5978   :group 'gnus-article-mime
5979   :type 'boolean)
5980
5981 (defcustom gnus-mime-display-attachment-buttons-in-header t
5982   "Add attachment buttons in the end of the header of an article.
5983 Since MIME attachments tend to be put at the end of an article, we may
5984 overlook them if there is a huge body.  This option offers you a copy
5985 of all non-inlinable MIME parts as buttons shown in front of an article.
5986 If nil, don't show those extra buttons."
5987   :version "24.5"
5988   :group 'gnus-article-mime
5989   :type 'boolean)
5990
5991 (defun gnus-mime-display-part (handle)
5992   (cond
5993    ;; Maybe a broken MIME message.
5994    ((null handle))
5995    ;; Single part.
5996    ((not (stringp (car handle)))
5997     (gnus-mime-display-single handle))
5998    ;; User-defined multipart
5999    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
6000     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
6001              handle))
6002    ;; multipart/alternative
6003    ((and (equal (car handle) "multipart/alternative")
6004          (not (or gnus-mime-display-multipart-as-mixed
6005                   gnus-mime-display-multipart-alternative-as-mixed)))
6006     (let ((id (1+ (length gnus-article-mime-handle-alist))))
6007       (push (cons id handle) gnus-article-mime-handle-alist)
6008       (gnus-mime-display-alternative (cdr handle) nil nil id)))
6009    ;; multipart/related
6010    ((and (equal (car handle) "multipart/related")
6011          (not (or gnus-mime-display-multipart-as-mixed
6012                   gnus-mime-display-multipart-related-as-mixed)))
6013     (gnus-mime-display-part (cadr handle)))
6014    ((equal (car handle) "multipart/signed")
6015     (gnus-add-wash-type 'signed)
6016     (gnus-mime-display-security handle))
6017    ((equal (car handle) "multipart/encrypted")
6018     (gnus-add-wash-type 'encrypted)
6019     (gnus-mime-display-security handle))
6020    ;; Other multiparts are handled like multipart/mixed.
6021    (t
6022     (gnus-mime-display-mixed (cdr handle)))))
6023
6024 (defun gnus-mime-part-function (handles)
6025   (if (stringp (car handles))
6026       (mapcar 'gnus-mime-part-function (cdr handles))
6027     (funcall gnus-article-mime-part-function handles)))
6028
6029 (defun gnus-mime-display-mixed (handles)
6030   (mapcar 'gnus-mime-display-part handles))
6031
6032 (defun gnus-mime-display-single (handle)
6033   (let ((type (mm-handle-media-type handle))
6034         (ignored gnus-ignored-mime-types)
6035         (not-attachment t)
6036         display text)
6037     (catch 'ignored
6038       (progn
6039         (while ignored
6040           (when (string-match (pop ignored) type)
6041             (throw 'ignored nil)))
6042         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
6043                                (with-current-buffer gnus-summary-buffer
6044                                  gnus-inhibit-images)
6045                              gnus-inhibit-images)
6046                            (string-match "\\`image/" type)))
6047                  (setq not-attachment
6048                        (and (not (mm-inline-override-p handle))
6049                             (or (not (mm-handle-disposition handle))
6050                                 (equal (car (mm-handle-disposition handle))
6051                                        "inline")
6052                                 (mm-attachment-override-p handle))))
6053                  (mm-automatic-display-p handle)
6054                  (or (and
6055                       (mm-inlinable-p handle)
6056                       (mm-inlined-p handle))
6057                      (mm-automatic-external-display-p type)))
6058             (setq display t)
6059           (when (equal (mm-handle-media-supertype handle) "text")
6060             (setq text t)))
6061         (let ((id (car (rassq handle gnus-article-mime-handle-alist)))
6062               beg)
6063           (unless id
6064             (setq id (1+ (length gnus-article-mime-handle-alist)))
6065             (push (cons id handle) gnus-article-mime-handle-alist))
6066           (when (and display
6067                      (equal (mm-handle-media-supertype handle) "message"))
6068             (insert-char
6069              ?\n
6070              (cond ((not (bolp)) 2)
6071                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
6072                    (t 1))))
6073           (when (or (not display)
6074                     (not (gnus-unbuttonized-mime-type-p type))
6075                     (eq id gnus-mime-buttonized-part-id))
6076             (gnus-insert-mime-button
6077              handle id (list (or display (and not-attachment text)))))
6078           (setq beg (point))
6079           (cond
6080            (display
6081             (let ((mail-parse-charset gnus-newsgroup-charset)
6082                   (mail-parse-ignored-charsets
6083                    (save-excursion (condition-case ()
6084                                        (set-buffer gnus-summary-buffer)
6085                                      (error))
6086                                    gnus-newsgroup-ignored-charsets)))
6087               (gnus-bind-safe-url-regexp (mm-display-part handle t))))
6088            ((and text not-attachment)
6089             (mm-display-inline handle)))
6090           (goto-char (point-max))
6091           (if (string-match "\\`image/" type)
6092               (gnus-article-insert-newline)
6093             (if (prog1
6094                     (= (skip-chars-backward "\n") -1)
6095                   (forward-char 1))
6096                 (gnus-article-insert-newline)
6097               (put-text-property (point) (point-max) 'gnus-undeletable t))
6098             (goto-char (point-max)))
6099           ;; Do highlighting.
6100           (save-excursion
6101             (save-restriction
6102               (narrow-to-region beg (point))
6103               (if (eq handle gnus-article-mime-handles)
6104                   ;; The format=flowed case.
6105                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
6106                 ;; Don't count signature parts that are never displayed.
6107                 ;; The part number should be re-calculated supposing this
6108                 ;; might be a message/rfc822 part.
6109                 (let (handles)
6110                   (dolist (part gnus-article-mime-handles)
6111                     (unless (or (stringp part)
6112                                 (equal (car (mm-handle-type part))
6113                                        "application/pgp-signature"))
6114                       (push part handles)))
6115                   (gnus-treat-article
6116                    nil (length (memq handle handles)) (length handles)
6117                    (mm-handle-media-type handle)))))))))))
6118
6119 (defun gnus-unbuttonized-mime-type-p (type)
6120   "Say whether TYPE is to be unbuttonized."
6121   (unless gnus-inhibit-mime-unbuttonizing
6122     (when (catch 'found
6123             (let ((types gnus-unbuttonized-mime-types))
6124               (while types
6125                 (when (string-match (pop types) type)
6126                   (throw 'found t)))))
6127       (not (catch 'found
6128              (let ((types gnus-buttonized-mime-types))
6129                (while types
6130                  (when (string-match (pop types) type)
6131                    (throw 'found t)))))))))
6132
6133 (defun gnus-article-insert-newline ()
6134   "Insert a newline, but mark it as undeletable."
6135   (gnus-put-text-property
6136    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
6137
6138 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
6139   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
6140          (ihandles handles)
6141          (point (point))
6142          handle (inhibit-read-only t) from props begend not-pref)
6143     (save-window-excursion
6144       (save-restriction
6145         (when ibegend
6146           (narrow-to-region (car ibegend)
6147                             (or (cdr ibegend)
6148                                 (progn
6149                                   (goto-char (car ibegend))
6150                                   (forward-line 2)
6151                                   (point))))
6152           (delete-region (point-min) (point-max))
6153           (mm-remove-parts handles))
6154         (setq begend (list (point-marker)))
6155         ;; Do the toggle.
6156         (unless (setq not-pref (cadr (member preferred ihandles)))
6157           (setq not-pref (car ihandles)))
6158         (when (or ibegend
6159                   (not preferred)
6160                   (not (gnus-unbuttonized-mime-type-p
6161                         "multipart/alternative")))
6162           (gnus-add-text-properties
6163            (setq from (point))
6164            (progn
6165              (insert (format "%d.  " id))
6166              (point))
6167            `(gnus-callback
6168              (lambda (handles)
6169                (unless ,(not ibegend)
6170                  (setq gnus-article-mime-handle-alist
6171                        ',gnus-article-mime-handle-alist))
6172                (gnus-mime-display-alternative
6173                 ',ihandles ',not-pref ',begend ,id))
6174              keymap ,gnus-mime-button-map
6175              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6176              face ,gnus-article-button-face
6177              gnus-part ,id
6178              article-type multipart
6179              rear-nonsticky t))
6180           (widget-convert-button 'link from (point)
6181                                  :action 'gnus-widget-press-button
6182                                  :button-keymap gnus-widget-button-keymap)
6183           ;; Do the handles
6184           (while (setq handle (pop handles))
6185             (gnus-add-text-properties
6186              (setq from (point))
6187              (progn
6188                (insert (format "(%c) %-18s"
6189                                (if (equal handle preferred) ?* ? )
6190                                (mm-handle-media-type handle)))
6191                (point))
6192              `(gnus-callback
6193                (lambda (handles)
6194                  (unless ,(not ibegend)
6195                    (setq gnus-article-mime-handle-alist
6196                          ',gnus-article-mime-handle-alist))
6197                  (gnus-mime-display-alternative
6198                   ',ihandles ',handle ',begend ,id))
6199                keymap ,gnus-mime-button-map
6200                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6201                face ,gnus-article-button-face
6202                gnus-part ,id
6203                gnus-data ,handle
6204                rear-nonsticky t))
6205             (widget-convert-button 'link from (point)
6206                                    :action 'gnus-widget-press-button
6207                                    :button-keymap gnus-widget-button-keymap)
6208             (insert "  "))
6209           (insert "\n\n"))
6210         (when preferred
6211           (if (stringp (car preferred))
6212               (gnus-display-mime preferred)
6213             (let ((mail-parse-charset gnus-newsgroup-charset)
6214                   (mail-parse-ignored-charsets
6215                    (with-current-buffer gnus-summary-buffer
6216                      gnus-newsgroup-ignored-charsets)))
6217               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6218               ;; Do highlighting.
6219               (save-excursion
6220                 (save-restriction
6221                   (narrow-to-region (car begend) (point-max))
6222                   (gnus-treat-article
6223                    nil (length gnus-article-mime-handle-alist)
6224                    (gnus-article-mime-total-parts)
6225                    (mm-handle-media-type preferred))))))
6226           (goto-char (point-max))
6227           (setcdr begend (point-marker)))))
6228     (when ibegend
6229       (goto-char point)))
6230   ;; Redraw attachment buttons in the header.
6231   (when gnus-mime-display-attachment-buttons-in-header
6232     (gnus-mime-buttonize-attachments-in-header)))
6233
6234 (defconst gnus-article-wash-status-strings
6235   (let ((alist '((cite "c" "Possible hidden citation text"
6236                        " " "All citation text visible")
6237                  (headers "h" "Hidden headers"
6238                           " " "All headers visible.")
6239                  (pgp "p" "Encrypted or signed message status hidden"
6240                       " " "No hidden encryption nor digital signature status")
6241                  (signature "s" "Signature has been hidden"
6242                             " " "Signature is visible")
6243                  (overstrike "o" "Overstrike (^H) characters applied"
6244                              " " "No overstrike characters applied")
6245                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6246                            " " "No /*_emphasis_*/ characters applied")))
6247         result)
6248     (dolist (entry alist result)
6249       (let ((key (nth 0 entry))
6250             (on (copy-sequence (nth 1 entry)))
6251             (on-help (nth 2 entry))
6252             (off (copy-sequence (nth 3 entry)))
6253             (off-help (nth 4 entry)))
6254         (put-text-property 0 1 'help-echo on-help on)
6255         (put-text-property 0 1 'help-echo off-help off)
6256         (push (list key on off) result))))
6257   "Alist of strings describing wash status in the mode line.
6258 Each entry has the form (KEY ON OF), where the KEY is a symbol
6259 representing the particular washing function, ON is the string to use
6260 in the article mode line when the washing function is active, and OFF
6261 is the string to use when it is inactive.")
6262
6263 (defun gnus-article-wash-status-entry (key value)
6264   (let ((entry (assoc key gnus-article-wash-status-strings)))
6265     (if value (nth 1 entry) (nth 2 entry))))
6266
6267 (defun gnus-article-wash-status ()
6268   "Return a string which display status of article washing."
6269   (with-current-buffer gnus-article-buffer
6270     (let ((cite (memq 'cite gnus-article-wash-types))
6271           (headers (memq 'headers gnus-article-wash-types))
6272           (boring (memq 'boring-headers gnus-article-wash-types))
6273           (pgp (memq 'pgp gnus-article-wash-types))
6274           (pem (memq 'pem gnus-article-wash-types))
6275           (signed (memq 'signed gnus-article-wash-types))
6276           (encrypted (memq 'encrypted gnus-article-wash-types))
6277           (signature (memq 'signature gnus-article-wash-types))
6278           (overstrike (memq 'overstrike gnus-article-wash-types))
6279           (emphasis (memq 'emphasis gnus-article-wash-types)))
6280       (concat
6281        (gnus-article-wash-status-entry 'cite cite)
6282        (gnus-article-wash-status-entry 'headers (or headers boring))
6283        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6284        (gnus-article-wash-status-entry 'signature signature)
6285        (gnus-article-wash-status-entry 'overstrike overstrike)
6286        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6287
6288 (defun gnus-add-wash-type (type)
6289   "Add a washing of TYPE to the current status."
6290   (add-to-list 'gnus-article-wash-types type))
6291
6292 (defun gnus-delete-wash-type (type)
6293   "Add a washing of TYPE to the current status."
6294   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6295
6296 (defun gnus-add-image (category image)
6297   "Add IMAGE of CATEGORY to the list of displayed images."
6298   (let ((entry (assq category gnus-article-image-alist)))
6299     (unless entry
6300       (setq entry (list category))
6301       (push entry gnus-article-image-alist))
6302     (nconc entry (list image))))
6303
6304 (defun gnus-delete-images (category)
6305   "Delete all images in CATEGORY."
6306   (let ((entry (assq category gnus-article-image-alist)))
6307     (dolist (image (cdr entry))
6308       (gnus-remove-image image category))
6309     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6310     (gnus-delete-wash-type category)))
6311
6312 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6313
6314 (defun gnus-article-maybe-hide-headers ()
6315   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6316 Provided for backwards compatibility."
6317   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6318                  (not (with-current-buffer gnus-summary-buffer
6319                         gnus-have-all-headers)))
6320              (not gnus-inhibit-hiding))
6321     (gnus-article-hide-headers)))
6322
6323 (declare-function shr-put-image "shr" (data alt &optional flags))
6324
6325 (defun gnus-shr-put-image (data alt &optional flags)
6326   "Put image DATA with a string ALT.  Enable image to be deleted."
6327   (let ((image (if flags
6328                    (shr-put-image data (propertize (or alt "*")
6329                                                    'gnus-image-category 'shr)
6330                                   flags)
6331                  ;; Old `shr-put-image' doesn't take the optional `flags'
6332                  ;; argument.
6333                  (shr-put-image data (propertize (or alt "*")
6334                                                  'gnus-image-category 'shr)))))
6335     (when image
6336       (gnus-add-image 'shr image))))
6337
6338 (defun gnus-mime-buttonize-attachments-in-header (&optional interactive)
6339   "Show attachments as buttons in the end of the header of an article.
6340 This function toggles the display when called interactively.  Note that
6341 buttons to be added to the header are only the ones that aren't inlined
6342 in the body.  Use `gnus-header-face-alist' to highlight buttons."
6343   (interactive (list t))
6344   (gnus-with-article-buffer
6345     (gmm-labels
6346         ;; Function that returns a flattened version of
6347         ;; `gnus-article-mime-handle-alist'.
6348         ((flattened-alist
6349           (&optional alist id all)
6350           (if alist
6351               (let ((i 1) newid flat)
6352                 (dolist (handle alist flat)
6353                   (setq newid (append id (list i))
6354                         i (1+ i))
6355                   (if (stringp (car handle))
6356                       (setq flat (nconc flat (flattened-alist (cdr handle)
6357                                                               newid all)))
6358                     (delq (rassq handle all) all)
6359                     (setq flat (nconc flat (list (cons newid handle)))))))
6360             (let ((flat (list nil)))
6361               ;; Assume that elements of `gnus-article-mime-handle-alist'
6362               ;; are in the decreasing order, but unnumbered subsidiaries
6363               ;; in each element are in the increasing order.
6364               (dolist (handle (reverse gnus-article-mime-handle-alist))
6365                 (if (stringp (cadr handle))
6366                     (setq flat (nconc flat (flattened-alist (cddr handle)
6367                                                             (list (car handle))
6368                                                             flat)))
6369                   (delq (rassq (cdr handle) flat) flat)
6370                   (setq flat (nconc flat (list (cons (list (car handle))
6371                                                      (cdr handle)))))))
6372               (setq flat (cdr flat))
6373               (mapc (lambda (handle)
6374                       (if (cdar handle)
6375                           ;; This is a hidden (i.e. unnumbered) handle.
6376                           (progn
6377                             (setcar handle
6378                                     (1+ (caar gnus-article-mime-handle-alist)))
6379                             (push handle gnus-article-mime-handle-alist))
6380                         (setcar handle (caar handle))))
6381                     flat)
6382               flat))))
6383       (let ((case-fold-search t) buttons handle type st)
6384         (save-excursion
6385           (save-restriction
6386             (widen)
6387             (article-narrow-to-head)
6388             ;; Header buttons exist?
6389             (while (and (not buttons)
6390                         (re-search-forward "^attachments?:[\n ]+" nil t))
6391               (when (get-char-property (match-end 0)
6392                                        'gnus-button-attachment-extra)
6393                 (setq buttons (match-beginning 0))))
6394             (widen)
6395             (when buttons
6396               ;; Delete header buttons.
6397               (delete-region buttons (if (re-search-forward "^[^ ]" nil t)
6398                                          (match-beginning 0)
6399                                        (point-max))))
6400             (unless (and interactive buttons)
6401               ;; Find buttons.
6402               (setq buttons nil)
6403               (dolist (button (flattened-alist))
6404                 (setq handle (cdr button)
6405                       type (mm-handle-media-type handle))
6406                 (when (or (and (if (gnus-buffer-live-p gnus-summary-buffer)
6407                                    (with-current-buffer gnus-summary-buffer
6408                                      gnus-inhibit-images)
6409                                  gnus-inhibit-images)
6410                                (string-match "\\`image/" type))
6411                           (mm-inline-override-p handle)
6412                           (and (mm-handle-disposition handle)
6413                                (not (equal (car (mm-handle-disposition handle))
6414                                            "inline"))
6415                                (not (mm-attachment-override-p handle)))
6416                           (not (mm-automatic-display-p handle))
6417                           (not (or (and (mm-inlinable-p handle)
6418                                         (mm-inlined-p handle))
6419                                    (mm-automatic-external-display-p type))))
6420                   (push button buttons)))
6421               (when buttons
6422                 ;; Add header buttons.
6423                 (article-goto-body)
6424                 (forward-line -1)
6425                 (narrow-to-region (point) (point))
6426                 (insert "Attachment" (if (cdr buttons) "s" "") ":")
6427                 (dolist (button (nreverse buttons))
6428                   (setq st (point))
6429                   (insert " ")
6430                   (mm-handle-set-undisplayer
6431                    (setq handle (copy-sequence (cdr button))) nil)
6432                   (gnus-insert-mime-button handle (car button))
6433                   (skip-chars-backward "\t\n ")
6434                   (delete-region (point) (point-max))
6435                   (when (> (current-column) (window-width))
6436                     (goto-char st)
6437                     (insert "\n")
6438                     (end-of-line)))
6439                 (insert "\n")
6440                 (dolist (ovl (gnus-overlays-in (point-min) (point)))
6441                   (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
6442                   (gnus-overlay-put ovl 'face nil))
6443                 (let ((gnus-treatment-function-alist
6444                        '((gnus-treat-highlight-headers
6445                           gnus-article-highlight-headers))))
6446                   (gnus-treat-article 'head))))))))))
6447
6448 ;;; Article savers.
6449
6450 (defun gnus-output-to-file (file-name)
6451   "Append the current article to a file named FILE-NAME.
6452 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6453 text and used as the value of the coding cookie which is added to the
6454 top of a file.  Otherwise, this function saves a raw article without
6455 the coding cookie."
6456   (let* ((artbuf (current-buffer))
6457          (file-name-coding-system nnmail-pathname-coding-system)
6458          (coding gnus-article-save-coding-system)
6459          (coding-system-for-read (if coding
6460                                      nil ;; Rely on the coding cookie.
6461                                    mm-text-coding-system))
6462          (coding-system-for-write (or coding
6463                                       mm-text-coding-system-for-write
6464                                       mm-text-coding-system))
6465          (exists (file-exists-p file-name)))
6466     (with-temp-buffer
6467       (when exists
6468         (insert-file-contents file-name)
6469         (goto-char (point-min))
6470         ;; Remove the existing coding cookie.
6471         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6472           (delete-region (match-beginning 0) (match-end 0))))
6473       (goto-char (point-max))
6474       (insert-buffer-substring artbuf)
6475       ;; Append newline at end of the buffer as separator, and then
6476       ;; save it to file.
6477       (goto-char (point-max))
6478       (insert "\n")
6479       (when coding
6480         ;; If the coding system is not suitable to encode the text,
6481         ;; ask a user for a proper one.
6482         (when (fboundp 'select-safe-coding-system)
6483           (setq coding (coding-system-base
6484                         (save-window-excursion
6485                           (select-safe-coding-system (point-min) (point-max)
6486                                                      coding))))
6487           (setq coding-system-for-write
6488                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6489                     coding)))
6490         (goto-char (point-min))
6491         ;; Add the coding cookie.
6492         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6493                         coding-system-for-write)))
6494       (if exists
6495           (progn
6496             (write-region (point-min) (point-max) file-name nil 'no-message)
6497             (message "Appended to %s" file-name))
6498         (write-region (point-min) (point-max) file-name))))
6499   t)
6500
6501 (defun gnus-narrow-to-page (&optional arg)
6502   "Narrow the article buffer to a page.
6503 If given a numerical ARG, move forward ARG pages."
6504   (interactive "P")
6505   (setq arg (if arg (prefix-numeric-value arg) 0))
6506   (with-current-buffer gnus-article-buffer
6507     (widen)
6508     ;; Remove any old next/prev buttons.
6509     (when (gnus-visual-p 'page-marker)
6510       (let ((inhibit-read-only t))
6511         (gnus-remove-text-with-property 'gnus-prev)
6512         (gnus-remove-text-with-property 'gnus-next)))
6513     (let (st nd pt)
6514       (when (save-excursion
6515               (cond ((< arg 0)
6516                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6517                          (prog1
6518                              (setq nd (match-beginning 0)
6519                                    pt nd)
6520                            (when (re-search-backward page-delimiter nil t)
6521                              (setq st (match-end 0))))
6522                        (when (re-search-forward page-delimiter nil t)
6523                          (setq nd (match-beginning 0)
6524                                pt (point-min)))))
6525                     ((> arg 0)
6526                      (if (re-search-forward page-delimiter nil 'move arg)
6527                          (prog1
6528                              (setq st (match-end 0)
6529                                    pt st)
6530                            (when (re-search-forward page-delimiter nil t)
6531                              (setq nd (match-beginning 0))))
6532                        (when (re-search-backward page-delimiter nil t)
6533                          (setq st (match-end 0)
6534                                pt (point-max)))))
6535                     (t
6536                      (when (re-search-backward page-delimiter nil t)
6537                        (goto-char (setq st (match-end 0))))
6538                      (when (re-search-forward page-delimiter nil t)
6539                        (setq nd (match-beginning 0)))
6540                      (or st nd))))
6541         (setq gnus-page-broken t)
6542         (when pt (goto-char pt))
6543         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6544         (when (gnus-visual-p 'page-marker)
6545           (save-excursion
6546             (when nd
6547               (goto-char nd)
6548               (gnus-insert-next-page-button))
6549             (when st
6550               (goto-char st)
6551               (gnus-insert-prev-page-button))))))))
6552
6553 ;; Article mode commands
6554
6555 (defun gnus-article-goto-next-page ()
6556   "Show the next page of the article."
6557   (interactive)
6558   (when (gnus-article-next-page)
6559     (goto-char (point-min))
6560     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6561
6562
6563 (defun gnus-article-goto-prev-page ()
6564   "Show the previous page of the article."
6565   (interactive)
6566   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6567       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6568     (gnus-article-prev-page nil)))
6569
6570 ;; This is cleaner but currently breaks `gnus-pick-mode':
6571 ;;
6572 ;; (defun gnus-article-goto-next-page ()
6573 ;;   "Show the next page of the article."
6574 ;;   (interactive)
6575 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6576 ;;     (gnus-summary-next-page)))
6577 ;;
6578 ;; (defun gnus-article-goto-prev-page ()
6579 ;;   "Show the next page of the article."
6580 ;;   (interactive)
6581 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6582 ;;     (gnus-summary-prev-page)))
6583
6584 (defun gnus-article-next-page (&optional lines)
6585   "Show the next page of the current article.
6586 If end of article, return non-nil.  Otherwise return nil.
6587 Argument LINES specifies lines to be scrolled up."
6588   (interactive "p")
6589   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6590   (if (and (not (and gnus-article-over-scroll
6591                      (> (count-lines (window-start) (point-max))
6592                         (if (featurep 'xemacs)
6593                             (or lines (1- (window-height)))
6594                           (+ (or lines (1- (window-height))) scroll-margin)))))
6595            (save-excursion
6596              (end-of-line)
6597              (and (pos-visible-in-window-p)     ;Not continuation line.
6598                   (>= (point) (point-max)))))
6599       ;; Nothing in this page.
6600       (if (or (not gnus-page-broken)
6601               (save-excursion
6602                 (save-restriction
6603                   (widen)
6604                   (forward-line)
6605                   (eobp)))) ;Real end-of-buffer?
6606           (progn
6607             (when gnus-article-over-scroll
6608               (gnus-article-next-page-1 lines))
6609             t)                  ;Nothing more.
6610         (gnus-narrow-to-page 1)         ;Go to next page.
6611         nil)
6612     ;; More in this page.
6613     (gnus-article-next-page-1 lines)
6614     nil))
6615
6616 (defun gnus-article-beginning-of-window ()
6617   "Move point to the beginning of the window.
6618 In Emacs, the point is placed at the line number which `scroll-margin'
6619 specifies."
6620   (if (featurep 'xemacs)
6621       (move-to-window-line 0)
6622     ;; There is an obscure bug in Emacs that makes it impossible to
6623     ;; scroll past big pictures in the article buffer.  Try to fix
6624     ;; this by adding a sanity check by counting the lines visible.
6625     (when (> (count-lines (window-start) (window-end)) 30)
6626       (move-to-window-line
6627        (min (max 0 scroll-margin)
6628             (max 1 (- (window-height)
6629                       (if mode-line-format 1 0)
6630                       (if header-line-format 1 0)
6631                       2)))))))
6632
6633 (defun gnus-article-next-page-1 (lines)
6634   (condition-case ()
6635       (let ((scroll-in-place nil)
6636             (auto-window-vscroll nil))
6637         (scroll-up lines))
6638     (end-of-buffer
6639      ;; Long lines may cause an end-of-buffer error.
6640      (goto-char (point-max))))
6641   (gnus-article-beginning-of-window))
6642
6643 (defun gnus-article-prev-page (&optional lines)
6644   "Show previous page of current article.
6645 Argument LINES specifies lines to be scrolled down."
6646   (interactive "p")
6647   (move-to-window-line 0)
6648   (if (and gnus-page-broken
6649            (bobp)
6650            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6651       (progn
6652         (gnus-narrow-to-page -1)        ;Go to previous page.
6653         (goto-char (point-max))
6654         (recenter (if gnus-article-over-scroll
6655                       (if lines
6656                           (max (if (featurep 'xemacs)
6657                                    lines
6658                                  (+ lines scroll-margin))
6659                                3)
6660                         (- (window-height) 2))
6661                     -1)))
6662     (prog1
6663         (condition-case ()
6664             (let ((scroll-in-place nil))
6665               (scroll-down lines))
6666           (beginning-of-buffer
6667            (goto-char (point-min))))
6668       (gnus-article-beginning-of-window))))
6669
6670 (defun gnus-article-only-boring-p ()
6671   "Decide whether there is only boring text remaining in the article.
6672 Something \"interesting\" is a word of at least two letters that does
6673 not have a face in `gnus-article-boring-faces'."
6674   (when (and gnus-article-skip-boring
6675              (boundp 'gnus-article-boring-faces)
6676              (symbol-value 'gnus-article-boring-faces))
6677     (save-excursion
6678       (let ((inhibit-point-motion-hooks t))
6679         (catch 'only-boring
6680           (while (re-search-forward "\\b\\w\\w" nil t)
6681             (forward-char -1)
6682             (when (not (gnus-intersection
6683                         (gnus-faces-at (point))
6684                         (symbol-value 'gnus-article-boring-faces)))
6685               (throw 'only-boring nil)))
6686           (throw 'only-boring t))))))
6687
6688 (defun gnus-article-refer-article ()
6689   "Read article specified by message-id around point."
6690   (interactive)
6691   (save-excursion
6692     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6693     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6694     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6695         (let ((msg-id (concat "<" (match-string 0) ">")))
6696           (set-buffer gnus-summary-buffer)
6697           (gnus-summary-refer-article msg-id))
6698       (error "No references around point"))))
6699
6700 (defun gnus-article-show-summary ()
6701   "Reconfigure windows to show summary buffer."
6702   (interactive)
6703   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6704       (error "There is no summary buffer for this article buffer")
6705     (gnus-article-set-globals)
6706     (gnus-configure-windows 'article)
6707     (gnus-summary-goto-subject gnus-current-article)
6708     (gnus-summary-position-point)))
6709
6710 (defun gnus-article-describe-briefly ()
6711   "Describe article mode commands briefly."
6712   (interactive)
6713   (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")))
6714
6715 (defun gnus-article-check-buffer ()
6716   "Beep if not in an article buffer."
6717   (unless (derived-mode-p 'gnus-article-mode)
6718     (error "Command invoked outside of a Gnus article buffer")))
6719
6720 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6721   "Read a summary buffer key sequence and execute it from the article buffer."
6722   (interactive "P")
6723   (gnus-article-check-buffer)
6724   (let ((nosaves
6725          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6726            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6727            "=" "^" "\M-^" "|"))
6728         (nosave-but-article
6729          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6730            "An" "Ap" [?A (meta return)] [?A delete]))
6731         (nosave-in-article
6732          '("AS" "\C-d"))
6733         (up-to-top
6734          '("n" "Gn" "p" "Gp"))
6735         keys new-sum-point)
6736     (with-current-buffer gnus-article-current-summary
6737       (let (gnus-pick-mode)
6738         (setq unread-command-events (nconc unread-command-events
6739                                            (list (or key last-command-event)))
6740               keys (if (featurep 'xemacs)
6741                        (events-to-keys (read-key-sequence nil t))
6742                      (read-key-sequence nil t)))))
6743
6744     (message "")
6745
6746     (cond
6747      ((eq (aref keys (1- (length keys))) ?\C-h)
6748       (gnus-article-describe-bindings (substring keys 0 -1)))
6749      ((or (member keys nosaves)
6750           (member keys nosave-but-article)
6751           (member keys nosave-in-article))
6752       (let (func)
6753         (save-window-excursion
6754           (pop-to-buffer gnus-article-current-summary)
6755           ;; We disable the pick minor mode commands.
6756           (let (gnus-pick-mode)
6757             (setq func (key-binding keys t))))
6758         (if (or (not func)
6759                 (numberp func))
6760             (ding)
6761           (unless (member keys nosave-in-article)
6762             (set-buffer gnus-article-current-summary))
6763           (when (and (symbolp func)
6764                      (get func 'disabled))
6765             (error "Function %s disabled" func))
6766           (call-interactively func)
6767           (setq new-sum-point (point)))
6768         (when (member keys nosave-but-article)
6769           (pop-to-buffer gnus-article-buffer))))
6770      (t
6771       ;; These commands should restore window configuration.
6772       (let ((obuf (current-buffer))
6773             (owin (current-window-configuration))
6774             win func in-buffer selected new-sum-start new-sum-hscroll err)
6775         (cond (not-restore-window
6776                (pop-to-buffer gnus-article-current-summary)
6777                (setq win (selected-window)))
6778               ((setq win (get-buffer-window gnus-article-current-summary))
6779                (select-window win))
6780               (t
6781                (let ((summary-buffer gnus-article-current-summary))
6782                  (gnus-configure-windows 'article)
6783                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6784                    (let ((gnus-buffer-configuration
6785                           '((article ((vertical 1.0
6786                                                 (summary 0.25 point)
6787                                                 (article 1.0)))))))
6788                      (gnus-configure-windows 'article))
6789                    (setq win (get-buffer-window summary-buffer 'visible)))
6790                  (gnus-select-frame-set-input-focus (window-frame win))
6791                  (select-window win))))
6792         (setq in-buffer (current-buffer))
6793         ;; We disable the pick minor mode commands.
6794         (setq func (let (gnus-pick-mode)
6795                      (key-binding keys t)))
6796         (when (get func 'disabled)
6797           (error "Function %s disabled" func))
6798         (if (and func
6799                  (functionp func)
6800                  (condition-case code
6801                      (progn
6802                        (call-interactively func)
6803                        t)
6804                    (error
6805                     (setq err code)
6806                     nil)))
6807             (progn
6808               (when (eq win (selected-window))
6809                 (setq new-sum-point (point)
6810                       new-sum-start (window-start win)
6811                       new-sum-hscroll (window-hscroll win)))
6812               (when (or (eq in-buffer (current-buffer))
6813                         (when (eq obuf (current-buffer))
6814                           (set-buffer in-buffer)
6815                           t))
6816                 (setq selected (ignore-errors (gnus-summary-select-article)))
6817                 (set-buffer obuf)
6818                 (unless not-restore-window
6819                   (set-window-configuration owin))
6820                 (when (and (eq selected 'old)
6821                            new-sum-point)
6822                   (set-window-start (get-buffer-window (current-buffer))
6823                                     1)
6824                   (set-window-point (get-buffer-window (current-buffer))
6825                                     (if (article-goto-body)
6826                                         (1- (point))
6827                                       (point))))
6828                 (when (and (not not-restore-window)
6829                            new-sum-point
6830                            (window-live-p win)
6831                            (with-current-buffer (window-buffer win)
6832                              (derived-mode-p 'gnus-summary-mode)))
6833                   (set-window-point win new-sum-point)
6834                   (set-window-start win new-sum-start)
6835                   (set-window-hscroll win new-sum-hscroll))))
6836           (set-window-configuration owin)
6837           (if err
6838               (signal (car err) (cdr err))
6839             (ding))))))))
6840
6841 (defun gnus-article-read-summary-send-keys ()
6842   (interactive)
6843   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6844     (gnus-article-read-summary-keys)))
6845
6846 (defun gnus-article-describe-key (key)
6847   "Display documentation of the function invoked by KEY.
6848 KEY is a string or a vector."
6849   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6850                        (read-key-sequence "Describe key: "))))
6851   (gnus-article-check-buffer)
6852   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6853                                   gnus-article-read-summary-send-keys))
6854       (with-current-buffer gnus-article-current-summary
6855         (setq unread-command-events
6856               (if (featurep 'xemacs)
6857                   (append key nil)
6858                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6859                                         (list 'meta (- x 128))
6860                                       x))
6861                         key)))
6862         (let ((cursor-in-echo-area t)
6863               gnus-pick-mode)
6864           (describe-key (read-key-sequence nil t))))
6865     (describe-key key)))
6866
6867 (defun gnus-article-describe-key-briefly (key &optional insert)
6868   "Display documentation of the function invoked by KEY.
6869 KEY is a string or a vector."
6870   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6871                        (read-key-sequence "Describe key: "))
6872                      current-prefix-arg))
6873   (gnus-article-check-buffer)
6874   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6875                                   gnus-article-read-summary-send-keys))
6876       (with-current-buffer gnus-article-current-summary
6877         (setq unread-command-events
6878               (if (featurep 'xemacs)
6879                   (append key nil)
6880                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6881                                         (list 'meta (- x 128))
6882                                       x))
6883                         key)))
6884         (let ((cursor-in-echo-area t)
6885               gnus-pick-mode)
6886           (describe-key-briefly (read-key-sequence nil t) insert)))
6887     (describe-key-briefly key insert)))
6888
6889 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6890 (defvar gnus-agent-summary-mode)
6891 (defvar gnus-draft-mode)
6892 (defvar help-xref-stack-item)
6893
6894 (defun gnus-article-describe-bindings (&optional prefix)
6895   "Show a list of all defined keys, and their definitions.
6896 The optional argument PREFIX, if non-nil, should be a key sequence;
6897 then we display only bindings that start with that prefix."
6898   (interactive)
6899   (gnus-article-check-buffer)
6900   (let ((keymap (copy-keymap gnus-article-mode-map))
6901         (map (copy-keymap gnus-article-send-map))
6902         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6903         parent agent draft)
6904     (define-key keymap "S" map)
6905     (define-key map [t] nil)
6906     (with-current-buffer gnus-article-current-summary
6907       (set-keymap-parent
6908        keymap
6909        (if (setq parent (keymap-parent gnus-article-mode-map))
6910            (prog1
6911                (setq parent (copy-keymap parent))
6912              (set-keymap-parent parent (current-local-map)))
6913          (current-local-map)))
6914       (set-keymap-parent map (key-binding "S"))
6915       (let (key def gnus-pick-mode)
6916         (while sumkeys
6917           (setq key (pop sumkeys))
6918           (cond ((and (vectorp key) (= (length key) 1)
6919                       (consp (setq def (aref key 0)))
6920                       (numberp (car def)) (numberp (cdr def)))
6921                  (when (< (max (car def) (cdr def)) 128)
6922                    (setq sumkeys
6923                          (append (mapcar
6924                                   #'vector
6925                                   (nreverse (gnus-uncompress-range def)))
6926                                  sumkeys))))
6927                 ((setq def (key-binding key))
6928                  (unless (eq def 'undefined)
6929                    (define-key keymap key def))))))
6930       (when (boundp 'gnus-agent-summary-mode)
6931         (setq agent gnus-agent-summary-mode))
6932       (when (boundp 'gnus-draft-mode)
6933         (setq draft gnus-draft-mode)))
6934     (with-temp-buffer
6935       (use-local-map keymap)
6936       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6937       (set (make-local-variable 'gnus-draft-mode) draft)
6938       (describe-bindings prefix))
6939     (let ((item `((lambda (prefix)
6940                     (with-current-buffer ,(current-buffer)
6941                       (gnus-article-describe-bindings prefix)))
6942                   ,prefix)))
6943       ;; Loading `help-mode' here is necessary if `describe-bindings'
6944       ;; is replaced with something, e.g. `helm-descbinds'.
6945       (require 'help-mode)
6946       (with-current-buffer (let (help-xref-following) (help-buffer))
6947         (setq help-xref-stack-item item)))))
6948
6949 (defun gnus-article-reply-with-original (&optional wide)
6950   "Start composing a reply mail to the current message.
6951 The text in the region will be yanked.  If the region isn't active,
6952 the entire article will be yanked."
6953   (interactive)
6954   (let ((article (cdr gnus-article-current))
6955         contents)
6956     (if (not (gnus-region-active-p))
6957         (with-current-buffer gnus-summary-buffer
6958           (gnus-summary-reply (list (list article)) wide))
6959       (setq contents (buffer-substring (point) (mark t)))
6960       ;; Deactivate active regions.
6961       (when (and (boundp 'transient-mark-mode)
6962                  transient-mark-mode)
6963         (setq mark-active nil))
6964       (with-current-buffer gnus-summary-buffer
6965         (gnus-summary-reply
6966          (list (list article contents)) wide)))))
6967
6968 (defun gnus-article-wide-reply-with-original ()
6969   "Start composing a wide reply mail to the current message.
6970 The text in the region will be yanked.  If the region isn't active,
6971 the entire article will be yanked."
6972   (interactive)
6973   (gnus-article-reply-with-original t))
6974
6975 (defun gnus-article-followup-with-original ()
6976   "Compose a followup to the current article.
6977 The text in the region will be yanked.  If the region isn't active,
6978 the entire article will be yanked."
6979   (interactive)
6980   (let ((article (cdr gnus-article-current))
6981         contents)
6982       (if (not (gnus-region-active-p))
6983           (with-current-buffer gnus-summary-buffer
6984             (gnus-summary-followup (list (list article))))
6985         (setq contents (buffer-substring (point) (mark t)))
6986         ;; Deactivate active regions.
6987         (when (and (boundp 'transient-mark-mode)
6988                    transient-mark-mode)
6989           (setq mark-active nil))
6990         (with-current-buffer gnus-summary-buffer
6991           (gnus-summary-followup
6992            (list (list article contents)))))))
6993
6994 (defun gnus-article-hide (&optional arg force)
6995   "Hide all the gruft in the current article.
6996 This means that signatures, cited text and (some) headers will be
6997 hidden.
6998 If given a prefix, show the hidden text instead."
6999   (interactive (append (gnus-article-hidden-arg) (list 'force)))
7000   (gnus-article-hide-headers arg)
7001   (gnus-article-hide-list-identifiers arg)
7002   (gnus-article-hide-citation-maybe arg force)
7003   (gnus-article-hide-signature arg))
7004
7005 (defun gnus-check-group-server ()
7006   ;; Make sure the connection to the server is alive.
7007   (unless (gnus-server-opened
7008            (gnus-find-method-for-group gnus-newsgroup-name))
7009     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7010     (gnus-request-group gnus-newsgroup-name t)))
7011
7012 (eval-when-compile
7013   (autoload 'nneething-get-file-name "nneething"))
7014
7015 (defun gnus-request-article-this-buffer (article group)
7016   "Get an article and insert it into this buffer."
7017   (let (do-update-line sparse-header)
7018     (prog1
7019         (save-excursion
7020           (erase-buffer)
7021           (gnus-kill-all-overlays)
7022           (setq group (or group gnus-newsgroup-name))
7023
7024           ;; Using `gnus-request-article' directly will insert the article into
7025           ;; `nntp-server-buffer' - so we'll save some time by not having to
7026           ;; copy it from the server buffer into the article buffer.
7027
7028           ;; We only request an article by message-id when we do not have the
7029           ;; headers for it, so we'll have to get those.
7030           (when (stringp article)
7031             (gnus-read-header article))
7032
7033           ;; If the article number is negative, that means that this article
7034           ;; doesn't belong in this newsgroup (possibly), so we find its
7035           ;; message-id and request it by id instead of number.
7036           (when (and (numberp article)
7037                      gnus-summary-buffer
7038                      (get-buffer gnus-summary-buffer)
7039                      (gnus-buffer-exists-p gnus-summary-buffer))
7040             (with-current-buffer gnus-summary-buffer
7041               (let ((header (gnus-summary-article-header article)))
7042                 (when (< article 0)
7043                   (cond
7044                    ((memq article gnus-newsgroup-sparse)
7045                     ;; This is a sparse gap article.
7046                     (setq do-update-line article)
7047                     (setq article (mail-header-id header))
7048                     (setq sparse-header (gnus-read-header article))
7049                     (setq gnus-newsgroup-sparse
7050                           (delq article gnus-newsgroup-sparse)))
7051                    ((vectorp header)
7052                     ;; It's a real article.
7053                     (setq article (mail-header-id header)))
7054                    (t
7055                     ;; It is an extracted pseudo-article.
7056                     (setq article 'pseudo)
7057                     (gnus-request-pseudo-article header))))
7058
7059                 (let ((method (gnus-find-method-for-group
7060                                gnus-newsgroup-name)))
7061                   (when (and (eq (car method) 'nneething)
7062                              (vectorp header))
7063                     (let ((dir (nneething-get-file-name
7064                                 (mail-header-id header))))
7065                       (when (and (stringp dir)
7066                                  (file-directory-p dir))
7067                         (setq article 'nneething)
7068                         (gnus-group-enter-directory dir))))))))
7069
7070           (cond
7071            ;; Refuse to select canceled articles.
7072            ((and (numberp article)
7073                  gnus-summary-buffer
7074                  (get-buffer gnus-summary-buffer)
7075                  (gnus-buffer-exists-p gnus-summary-buffer)
7076                  (eq (cdr (with-current-buffer gnus-summary-buffer
7077                             (assq article gnus-newsgroup-reads)))
7078                      gnus-canceled-mark))
7079             nil)
7080            ;; We first check `gnus-original-article-buffer'.
7081            ((and (get-buffer gnus-original-article-buffer)
7082                  (numberp article)
7083                  (with-current-buffer gnus-original-article-buffer
7084                    (and (equal (car gnus-original-article) group)
7085                         (eq (cdr gnus-original-article) article))))
7086             ;; `insert-buffer-substring' would incorrectly use the
7087             ;; equivalent of string-make-multibyte which amount to decoding
7088             ;; with locale-coding-system, causing failure of
7089             ;; subsequent decoding.
7090             (insert (mm-string-to-multibyte
7091                      (with-current-buffer gnus-original-article-buffer
7092                        (buffer-substring (point-min) (point-max)))))
7093             'article)
7094            ;; Check the backlog.
7095            ((and gnus-keep-backlog
7096                  (gnus-backlog-request-article group article (current-buffer)))
7097             'article)
7098            ;; Check asynchronous pre-fetch.
7099            ((gnus-async-request-fetched-article group article (current-buffer))
7100             (gnus-async-prefetch-next group article gnus-summary-buffer)
7101             (when (and (numberp article) gnus-keep-backlog)
7102               (gnus-backlog-enter-article group article (current-buffer)))
7103             'article)
7104            ;; Check the cache.
7105            ((and gnus-use-cache
7106                  (numberp article)
7107                  (gnus-cache-request-article article group))
7108             'article)
7109            ;; Check the agent cache.
7110            ((gnus-agent-request-article article group)
7111             'article)
7112            ;; Get the article and put into the article buffer.
7113            ((or (stringp article)
7114                 (numberp article))
7115             (let ((gnus-override-method gnus-override-method)
7116                   (methods (and (stringp article)
7117                                 (with-current-buffer gnus-summary-buffer
7118                                   (gnus-refer-article-methods))))
7119                   (backend (car (gnus-find-method-for-group
7120                                  gnus-newsgroup-name)))
7121                   result
7122                   (inhibit-read-only t))
7123               (when (and (null gnus-override-method)
7124                          methods)
7125                 (setq gnus-override-method (pop methods)))
7126               (while (not result)
7127                 (erase-buffer)
7128                 (gnus-kill-all-overlays)
7129                 (let ((gnus-newsgroup-name group))
7130                   (gnus-check-group-server))
7131                 (cond
7132                  ((gnus-request-article article group (current-buffer))
7133                   (when (numberp article)
7134                     (gnus-async-prefetch-next group article
7135                                               gnus-summary-buffer)
7136                     (when gnus-keep-backlog
7137                       (gnus-backlog-enter-article
7138                        group article (current-buffer)))
7139                     (when (and gnus-agent
7140                                (gnus-agent-group-covered-p group))
7141                       (gnus-agent-store-article article group)))
7142                   (setq result 'article))
7143                  (methods
7144                   (setq gnus-override-method (pop methods)))
7145                  ((not (string-match "^400 "
7146                                      (nnheader-get-report backend)))
7147                   ;; If we get 400 server disconnect, reconnect and
7148                   ;; retry; otherwise, assume the article has expired.
7149                   (setq result 'done))))
7150               (and (eq result 'article) 'article)))
7151            ;; It was a pseudo.
7152            (t article)))
7153
7154       ;; Associate this article with the current summary buffer.
7155       (setq gnus-article-current-summary gnus-summary-buffer)
7156
7157       ;; Take the article from the original article buffer
7158       ;; and place it in the buffer it's supposed to be in.
7159       (when (and (get-buffer gnus-article-buffer)
7160                  (equal (buffer-name (current-buffer))
7161                         (buffer-name (get-buffer gnus-article-buffer))))
7162         (save-excursion
7163           (if (get-buffer gnus-original-article-buffer)
7164               (set-buffer gnus-original-article-buffer)
7165             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
7166             (buffer-disable-undo)
7167             (setq major-mode 'gnus-original-article-mode)
7168             (setq buffer-read-only t))
7169           (let ((inhibit-read-only t))
7170             (erase-buffer)
7171             (insert-buffer-substring gnus-article-buffer))
7172           (setq gnus-original-article (cons group article)))
7173
7174         ;; Decode charsets.
7175         (run-hooks 'gnus-article-decode-hook)
7176         ;; Mark article as decoded or not.
7177         (setq gnus-article-decoded-p gnus-article-decode-hook))
7178
7179       ;; Update sparse articles.
7180       (when (and do-update-line
7181                  (or (numberp article)
7182                      (stringp article)))
7183         (let ((buf (current-buffer)))
7184           (set-buffer gnus-summary-buffer)
7185           (gnus-summary-update-article do-update-line sparse-header)
7186           (gnus-summary-goto-subject do-update-line nil t)
7187           (set-window-point (gnus-get-buffer-window (current-buffer) t)
7188                             (point))
7189           (set-buffer buf))))))
7190
7191 (defun gnus-block-private-groups (group)
7192   (if (or (gnus-news-group-p group)
7193           (gnus-member-of-valid 'global group))
7194       ;; Block nothing in news groups.
7195       nil
7196     ;; Block everything anywhere else.
7197     "."))
7198
7199 (defun gnus-blocked-images ()
7200   (if (functionp gnus-blocked-images)
7201       (funcall gnus-blocked-images gnus-newsgroup-name)
7202     gnus-blocked-images))
7203
7204 ;;;
7205 ;;; Article editing
7206 ;;;
7207
7208 (defcustom gnus-article-edit-mode-hook nil
7209   "Hook run in article edit mode buffers."
7210   :group 'gnus-article-various
7211   :type 'hook)
7212
7213 (defvar gnus-article-edit-done-function nil)
7214
7215 (defvar gnus-article-edit-mode-map nil)
7216
7217 ;; Should we be using derived.el for this?
7218 (unless gnus-article-edit-mode-map
7219   (setq gnus-article-edit-mode-map (make-keymap))
7220   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
7221
7222   (gnus-define-keys gnus-article-edit-mode-map
7223     "\C-c?"    describe-mode
7224     "\C-c\C-c" gnus-article-edit-done
7225     "\C-c\C-k" gnus-article-edit-exit
7226     "\C-c\C-f\C-t" message-goto-to
7227     "\C-c\C-f\C-o" message-goto-from
7228     "\C-c\C-f\C-b" message-goto-bcc
7229     ;;"\C-c\C-f\C-w" message-goto-fcc
7230     "\C-c\C-f\C-c" message-goto-cc
7231     "\C-c\C-f\C-s" message-goto-subject
7232     "\C-c\C-f\C-r" message-goto-reply-to
7233     "\C-c\C-f\C-n" message-goto-newsgroups
7234     "\C-c\C-f\C-d" message-goto-distribution
7235     "\C-c\C-f\C-f" message-goto-followup-to
7236     "\C-c\C-f\C-m" message-goto-mail-followup-to
7237     "\C-c\C-f\C-k" message-goto-keywords
7238     "\C-c\C-f\C-u" message-goto-summary
7239     "\C-c\C-f\C-i" message-insert-or-toggle-importance
7240     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
7241     "\C-c\C-b" message-goto-body
7242     "\C-c\C-i" message-goto-signature
7243
7244     "\C-c\C-t" message-insert-to
7245     "\C-c\C-n" message-insert-newsgroups
7246     "\C-c\C-o" message-sort-headers
7247     "\C-c\C-e" message-elide-region
7248     "\C-c\C-v" message-delete-not-region
7249     "\C-c\C-z" message-kill-to-signature
7250     "\M-\r" message-newline-and-reformat
7251     "\C-c\C-a" mml-attach-file
7252     "\C-a" message-beginning-of-line
7253     "\t" message-tab
7254     "\M-;" comment-region)
7255
7256   (gnus-define-keys (gnus-article-edit-wash-map
7257                      "\C-c\C-w" gnus-article-edit-mode-map)
7258     "f" gnus-article-edit-full-stops))
7259
7260 (easy-menu-define
7261   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7262   '("Field"
7263     ["Fetch To" message-insert-to t]
7264     ["Fetch Newsgroups" message-insert-newsgroups t]
7265     "----"
7266     ["To" message-goto-to t]
7267     ["From" message-goto-from t]
7268     ["Subject" message-goto-subject t]
7269     ["Cc" message-goto-cc t]
7270     ["Reply-To" message-goto-reply-to t]
7271     ["Summary" message-goto-summary t]
7272     ["Keywords" message-goto-keywords t]
7273     ["Newsgroups" message-goto-newsgroups t]
7274     ["Followup-To" message-goto-followup-to t]
7275     ["Mail-Followup-To" message-goto-mail-followup-to t]
7276     ["Distribution" message-goto-distribution t]
7277     ["Body" message-goto-body t]
7278     ["Signature" message-goto-signature t]))
7279
7280 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7281   "Major mode for editing articles.
7282 This is an extended text-mode.
7283
7284 \\{gnus-article-edit-mode-map}"
7285   (make-local-variable 'gnus-article-edit-done-function)
7286   (make-local-variable 'gnus-prev-winconf)
7287   (set (make-local-variable 'font-lock-defaults)
7288        '(message-font-lock-keywords t))
7289   (set (make-local-variable 'mail-header-separator) "")
7290   (set (make-local-variable 'gnus-article-edit-mode) t)
7291   (easy-menu-add message-mode-field-menu message-mode-map)
7292   (mml-mode)
7293   (setq buffer-read-only nil)
7294   (buffer-enable-undo)
7295   (widen))
7296
7297 (defun gnus-article-edit (&optional force)
7298   "Edit the current article.
7299 This will have permanent effect only in mail groups.
7300 If FORCE is non-nil, allow editing of articles even in read-only
7301 groups."
7302   (interactive "P")
7303   (when (and (not force)
7304              (gnus-group-read-only-p))
7305     (error "The current newsgroup does not support article editing"))
7306   (gnus-article-date-original)
7307   (gnus-article-edit-article
7308    'ignore
7309    `(lambda (no-highlight)
7310       'ignore
7311       (gnus-summary-edit-article-done
7312        ,(or (mail-header-references gnus-current-headers) "")
7313        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7314
7315 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7316   "Start editing the contents of the current article buffer."
7317   (let ((winconf (current-window-configuration)))
7318     (set-buffer gnus-article-buffer)
7319     (let ((message-auto-save-directory
7320            ;; Don't associate the article buffer with a draft file.
7321            nil))
7322       (gnus-article-edit-mode))
7323     (funcall start-func)
7324     (set-buffer-modified-p nil)
7325     (gnus-configure-windows 'edit-article)
7326     (setq gnus-article-edit-done-function exit-func)
7327     (setq gnus-prev-winconf winconf)
7328     (unless quiet
7329       (gnus-message 6 "C-c C-c to end edits"))))
7330
7331 (defun gnus-article-edit-done (&optional arg)
7332   "Update the article edits and exit."
7333   (interactive "P")
7334   (let ((func gnus-article-edit-done-function)
7335         (buf (current-buffer))
7336         (start (window-start))
7337         (p (point))
7338         (winconf gnus-prev-winconf))
7339     (widen) ;; Widen it in case that users narrowed the buffer.
7340     (funcall func arg)
7341     (set-buffer buf)
7342     ;; The cache and backlog have to be flushed somewhat.
7343     (when gnus-keep-backlog
7344       (gnus-backlog-remove-article
7345        (car gnus-article-current) (cdr gnus-article-current)))
7346     ;; Flush original article as well.
7347     (gnus-flush-original-article-buffer)
7348     (when gnus-use-cache
7349       (gnus-cache-update-article
7350        (car gnus-article-current) (cdr gnus-article-current)))
7351     ;; We remove all text props from the article buffer.
7352     (kill-all-local-variables)
7353     (set-text-properties (point-min) (point-max) nil)
7354     (gnus-article-mode)
7355     (set-window-configuration winconf)
7356     (set-buffer buf)
7357     (set-window-start (get-buffer-window buf) start)
7358     (set-window-point (get-buffer-window buf) (point)))
7359   (gnus-summary-show-article))
7360
7361 (defun gnus-flush-original-article-buffer ()
7362   (when (get-buffer gnus-original-article-buffer)
7363     (with-current-buffer gnus-original-article-buffer
7364       (setq gnus-original-article nil))))
7365
7366 (defun gnus-article-edit-exit ()
7367   "Exit the article editing without updating."
7368   (interactive)
7369   (when (or (not (buffer-modified-p))
7370             (yes-or-no-p "Article modified; kill anyway? "))
7371     (let ((curbuf (current-buffer))
7372           (p (point))
7373           (window-start (window-start)))
7374       (erase-buffer)
7375       (if (gnus-buffer-live-p gnus-original-article-buffer)
7376           (insert-buffer-substring gnus-original-article-buffer))
7377       (let ((winconf gnus-prev-winconf))
7378         (kill-all-local-variables)
7379         (gnus-article-mode)
7380         (set-window-configuration winconf)
7381         ;; Tippy-toe some to make sure that point remains where it was.
7382         (save-current-buffer
7383           (set-buffer curbuf)
7384           (set-window-start (get-buffer-window (current-buffer)) window-start)
7385           (goto-char p))))
7386     (gnus-summary-show-article)))
7387
7388 (defun gnus-article-edit-full-stops ()
7389   "Interactively repair spacing at end of sentences."
7390   (interactive)
7391   (save-excursion
7392     (goto-char (point-min))
7393     (search-forward-regexp "^$" nil t)
7394     (let ((case-fold-search nil))
7395       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7396
7397 ;;;
7398 ;;; Article highlights
7399 ;;;
7400
7401 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7402
7403 ;;; Internal Variables:
7404
7405 (defcustom gnus-button-url-regexp
7406   (concat
7407    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7408    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7409    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7410    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7411        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7412              (punct "!?:;.,"))
7413          (concat
7414           "\\(?:"
7415           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7416           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7417           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)"
7418           "\\(?:" "[" chars punct "]+" "[" chars "]" "\\)?"
7419           "\\|"
7420           "[" chars punct "]+" "[" chars "]"
7421           "\\)"))
7422      (concat ;; XEmacs 21.4 doesn't support POSIX.
7423       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7424       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7425    "\\)")
7426   "Regular expression that matches URLs."
7427   :version "24.4"
7428   :group 'gnus-article-buttons
7429   :type 'regexp)
7430
7431 (defcustom gnus-button-valid-fqdn-regexp
7432   message-valid-fqdn-regexp
7433   "Regular expression that matches a valid FQDN."
7434   :version "22.1"
7435   :group 'gnus-article-buttons
7436   :type 'regexp)
7437
7438 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7439 (defcustom gnus-button-valid-localpart-regexp
7440   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7441   "Regular expression that matches a localpart of mail addresses or MIDs."
7442   :version "22.1"
7443   :group 'gnus-article-buttons
7444   :type 'regexp)
7445
7446 (defcustom gnus-button-man-handler 'manual-entry
7447   "Function to use for displaying man pages.
7448 The function must take at least one argument with a string naming the
7449 man page."
7450   :version "22.1"
7451   :type '(choice (function-item :tag "Man" manual-entry)
7452                  (function-item :tag "Woman" woman)
7453                  (function :tag "Other"))
7454   :group 'gnus-article-buttons)
7455
7456 (defcustom gnus-button-mid-or-mail-regexp
7457   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7458           gnus-button-valid-fqdn-regexp
7459           ">?\\)\\b")
7460   "Regular expression that matches a message ID or a mail address."
7461   :version "22.1"
7462   :group 'gnus-article-buttons
7463   :type 'regexp)
7464
7465 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7466   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7467 Strings like this can be either a message ID or a mail address.  If it is one
7468 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7469 message ID or a mail address, respectively.  If this variable is set to the
7470 symbol `ask', always query the user what do do.  If it is a function, this
7471 function will be called with the string as its only argument.  The function
7472 must return `mid', `mail', `invalid' or `ask'."
7473   :version "22.1"
7474   :group 'gnus-article-buttons
7475   :type '(choice (function-item :tag "Heuristic function"
7476                                 gnus-button-mid-or-mail-heuristic)
7477                  (const ask)
7478                  (const mid)
7479                  (const mail)))
7480
7481 (defcustom gnus-button-mid-or-mail-heuristic-alist
7482   '((-10.0 . ".+\\$.+@")
7483     (-10.0 . "#")
7484     (-10.0 . "\\*")
7485     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7486     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7487     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7488     (-1.0  . "^[^a-z]+@")
7489     ;;
7490     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7491     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7492     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7493     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7494     ;;
7495     (-2.0  . "^[0-9]")
7496     (-1.0  . "^[0-9][0-9]")
7497     ;;
7498     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7499     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7500     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7501     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7502     ;;
7503     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7504     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7505     ;;       "[0-9]{8,}.*\@"
7506     (-3.0
7507      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7508     ;; "[0-9]{12,}.*\@"
7509     ;; compensation for TDMA dated mail addresses:
7510     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7511     ;;
7512     (-20.0 . "\\.fsf@") ;; Gnus
7513     (-20.0 . "^slrn")
7514     (-20.0 . "^Pine")
7515     (-20.0 . "^alpine\\.")
7516     (-20.0 . "_-_") ;; Subject change in thread
7517     ;;
7518     (-20.0 . "\\.ln@") ;; leafnode
7519     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7520     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7521     ;;
7522     ;; (5.0 . "") ;; $local_part_len <= 7
7523     (10.0  . "^[^0-9]+@")
7524     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7525     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7526     (3.0   . "\@stud")
7527     ;;
7528     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7529     ;;
7530     (0.5   . "^[A-Z][a-z]")
7531     (0.5   . "^[A-Z][a-z][a-z]")
7532     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7533     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7534   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7535
7536 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7537 address.  The REGEXP is processed with `case-fold-search' set to nil."
7538   :version "22.1"
7539   :group 'gnus-article-buttons
7540   :type '(repeat (cons (number :tag "Rate")
7541                        (regexp :tag "Regexp"))))
7542
7543 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7544   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7545 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7546 address, `ask' if unsure and `invalid' if the string is invalid."
7547   (let ((case-fold-search nil)
7548         (list gnus-button-mid-or-mail-heuristic-alist)
7549         (result 0) rate regexp lpartlen elem)
7550     (setq lpartlen
7551           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7552     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7553     ;; Certain special cases...
7554     (when (string-match
7555            (concat
7556             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7557             "^[0-9]+\\.[0-9]+@compuserve\\|"
7558             "@public\\.gmane\\.org")
7559            mid-or-mail)
7560       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7561       (setq result 'mail))
7562     (when (string-match "@.*@\\| " mid-or-mail)
7563       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7564       (setq result 'invalid))
7565     ;; Nothing more to do, if result is not a number here...
7566     (when (numberp result)
7567       (while list
7568         (setq elem (car list)
7569               rate (car elem)
7570               regexp (cdr elem)
7571               list (cdr list))
7572         (when (string-match regexp mid-or-mail)
7573           (setq result (+ result rate))
7574           (gnus-message
7575            9 "`%s' matched `%s', rate `%s', result `%s'."
7576            mid-or-mail regexp rate result)))
7577       (when (<= lpartlen 7)
7578         (setq result (+ result 5.0))
7579         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7580                       mid-or-mail result))
7581       (when (>= lpartlen 12)
7582         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7583         (cond
7584          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7585           ;; Long local part should contain realname if e-mail address,
7586           ;; too many digits: message-id.
7587           ;; $score -= 5.0 + 0.1 * $local_part_len;
7588           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7589           (setq result (+ result rate))
7590           (gnus-message
7591            9 "Many digits in `%s', rate `%s', result `%s'."
7592            mid-or-mail rate result))
7593          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7594                         mid-or-mail)
7595           ;; Too few vowels [^aeiouy]{4,}.*\@
7596           (setq result (+ result -5.0))
7597           (gnus-message
7598            9 "Few vowels in `%s', rate `%s', result `%s'."
7599            mid-or-mail -5.0 result))
7600          (t
7601           (setq result (+ result 5.0))
7602           (gnus-message
7603            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7604     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7605     ;; Maybe we should make this a customizable alist: (condition . 'result)
7606     (cond
7607      ((symbolp result) result)
7608      ;; Now convert number into proper results:
7609      ((< result -10.0) 'mid)
7610      ((> result  10.0) 'mail)
7611      (t 'ask))))
7612
7613 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7614   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7615          (url-mid (concat "news" ":" mid-or-mail))
7616          (url-mailto (concat "mailto" ":" mid-or-mail)))
7617     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7618     (when (fboundp pref)
7619       (setq guessed
7620             ;; get rid of surrounding angles...
7621             (funcall pref
7622                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7623       (if (or (eq 'mid guessed) (eq 'mail guessed))
7624           (setq pref guessed)
7625         (setq pref 'ask)))
7626     (if (eq pref 'ask)
7627         (save-window-excursion
7628           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7629               (setq pref 'mail)
7630             (setq pref 'mid))))
7631     (cond ((eq pref 'mid)
7632            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7633            (gnus-button-handle-news url-mid))
7634           ((eq pref 'mail)
7635            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7636            (gnus-url-mailto url-mailto))
7637           (t (gnus-message 3 "Invalid string.")))))
7638
7639 (defun gnus-button-handle-custom (fun arg)
7640   "Call function FUN on argument ARG.
7641 Both FUN and ARG are supposed to be strings.  ARG will be passed
7642 as a symbol to FUN."
7643   (funcall (intern fun)
7644            (if (string-match "^customize-apropos" fun)
7645                arg
7646              (intern arg))))
7647
7648 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7649
7650 (defun gnus-button-handle-describe-function (url)
7651   "Call `describe-function' when pushing the corresponding URL button."
7652   (describe-function
7653    (intern
7654     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7655
7656 (defun gnus-button-handle-describe-variable (url)
7657   "Call `describe-variable' when pushing the corresponding URL button."
7658   (describe-variable
7659    (intern
7660     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7661
7662 (defun gnus-button-handle-symbol (url)
7663 "Display help on variable or function.
7664 Calls `describe-variable' or `describe-function'."
7665   (let ((sym (intern url)))
7666     (cond
7667      ((fboundp sym) (describe-function sym))
7668      ((boundp sym) (describe-variable sym))
7669      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7670
7671 (defun gnus-button-handle-describe-key (url)
7672   "Call `describe-key' when pushing the corresponding URL button."
7673   (let* ((key-string
7674           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7675          (keys (ignore-errors (eval `(kbd ,key-string)))))
7676     (if keys
7677         (describe-key keys)
7678       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7679
7680 (defun gnus-button-handle-apropos (url)
7681   "Call `apropos' when pushing the corresponding URL button."
7682   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7683
7684 (defun gnus-button-handle-apropos-command (url)
7685   "Call `apropos' when pushing the corresponding URL button."
7686   (apropos-command
7687    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7688
7689 (defun gnus-button-handle-apropos-variable (url)
7690   "Call `apropos' when pushing the corresponding URL button."
7691   (funcall
7692    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7693    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7694
7695 (defun gnus-button-handle-apropos-documentation (url)
7696   "Call `apropos' when pushing the corresponding URL button."
7697   (funcall
7698    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7699    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7700
7701 (defun gnus-button-handle-library (url)
7702   "Call `locate-library' when pushing the corresponding URL button."
7703   (gnus-message 9 "url=`%s'" url)
7704   (let* ((lib (locate-library url))
7705          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7706     (if (not lib)
7707         (gnus-message 1 "Cannot locale library `%s'." url)
7708       (find-file-read-only file))))
7709
7710 (defcustom gnus-button-man-level 5
7711   "*Integer that says how many man-related buttons Gnus will show.
7712 The higher the number, the more buttons will appear and the more false
7713 positives are possible.  Note that you can set this variable local to
7714 specific groups.  Setting it higher in Unix groups is probably a good idea.
7715 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7716 how to set variables in specific groups."
7717   :version "22.1"
7718   :group 'gnus-article-buttons
7719   :link '(custom-manual "(gnus)Group Parameters")
7720   :type 'integer)
7721
7722 (defcustom gnus-button-emacs-level 5
7723   "*Integer that says how many emacs-related buttons Gnus will show.
7724 The higher the number, the more buttons will appear and the more false
7725 positives are possible.  Note that you can set this variable local to
7726 specific groups.  Setting it higher in Emacs or Gnus related groups is
7727 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7728 `gnus-parameters' on how to set variables in specific groups."
7729   :version "22.1"
7730   :group 'gnus-article-buttons
7731   :link '(custom-manual "(gnus)Group Parameters")
7732   :type 'integer)
7733
7734 (defcustom gnus-button-message-level 5
7735   "*Integer that says how many buttons for news or mail messages will appear.
7736 The higher the number, the more buttons will appear and the more false
7737 positives are possible."
7738   ;; mail addresses, MIDs, URLs for news, ...
7739   :version "22.1"
7740   :group 'gnus-article-buttons
7741   :type 'integer)
7742
7743 (defcustom gnus-button-browse-level 5
7744   "*Integer that says how many buttons for browsing will appear.
7745 The higher the number, the more buttons will appear and the more false
7746 positives are possible."
7747   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7748   :version "22.1"
7749   :group 'gnus-article-buttons
7750   :type 'integer)
7751
7752 (defcustom gnus-button-alist
7753   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7754      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7755     ((concat "\\b\\(nntp\\|news\\):\\("
7756              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7757      0 t gnus-button-handle-news 2)
7758     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7759      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7760     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7761      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7762     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7763     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7764      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7765     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7766      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7767     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7768      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7769     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7770      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7771     ;; RFC 2368 (The mailto URL scheme)
7772     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7773      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7774     ("\\bmailto:\\([^ \n\t]+\\)"
7775      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7776     ;; Info Konqueror style <info:/foo/bar baz>.
7777     ;; Must come before " Gnus home-grown style".
7778     ("\\binfo://?\\([^'\">\n\t]+\\)"
7779      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7780    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7781     ("\\binfo://\\([^'\">\n\t ]+\\)"
7782      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7783     ;; Info GNOME style <info:foo#bar_baz>
7784     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7785      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7786     ;; Info KDE style <info:(foo)bar baz>
7787     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7788      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7789     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7790      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7791     ("\\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,]*\\)\\)?"
7792      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7793      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7794     ;; This is custom
7795     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7796      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7797     ;; Emacs help commands
7798     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7799      ;; regexp doesn't match arguments containing ` '.
7800      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7801     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7802      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7803     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7804      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7805     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7806      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7807     ;; The following entries may lead to many false positives so don't enable
7808     ;; them by default (use a high button level).
7809     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7810      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7811      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7812     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7813      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7814     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7815      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7816     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7817      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7818     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7819      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7820     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7821      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7822     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7823      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7824     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7825      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7826     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7827      ;; Unlike the other regexps we really have to require quoting
7828      ;; here to determine where it ends.
7829      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7830     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7831     ("<URL: *\\([^\n<>]*\\)>"
7832      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7833     ;; RFC 2396 (2.4.3., delims) ...
7834     ("\"URL: *\\([^\n\"]*\\)\""
7835      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7836     ;; Raw URLs.
7837     (gnus-button-url-regexp
7838      0 (>= gnus-button-browse-level 0) browse-url 0)
7839     ;; man pages
7840     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7841      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7842      gnus-button-handle-man 1)
7843     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7844     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7845      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7846      gnus-button-handle-man 1)
7847     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7848     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7849     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7850      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7851     ;; Recognizing patches to .el files.  This is somewhat obscure,
7852     ;; but considering the percentage of Gnus users who hack Emacs
7853     ;; Lisp files...
7854     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7855      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7856     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7857      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7858     ;; MID or mail: To avoid too many false positives we don't try to catch
7859     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7860     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7861     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7862     ;; so that non-ambiguous entries (see above) match first.
7863     (gnus-button-mid-or-mail-regexp
7864      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7865   "*Alist of regexps matching buttons in article bodies.
7866
7867 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7868 REGEXP: is the string (case insensitive) matching text around the button (can
7869 also be Lisp expression evaluating to a string),
7870 BUTTON: is the number of the regexp grouping actually matching the button,
7871 FORM: is a Lisp expression which must eval to true for the button to
7872 be added,
7873 CALLBACK: is the function to call when the user push this button, and each
7874 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7875
7876 CALLBACK can also be a variable, in that case the value of that
7877 variable it the real callback function."
7878   :group 'gnus-article-buttons
7879   :type '(repeat (list (choice regexp variable sexp)
7880                        (integer :tag "Button")
7881                        (sexp :tag "Form")
7882                        (function :tag "Callback")
7883                        (repeat :tag "Par"
7884                                :inline t
7885                                (integer :tag "Regexp group")))))
7886 (put 'gnus-button-alist 'risky-local-variable t)
7887
7888 (defcustom gnus-header-button-alist
7889   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7890      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7891     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7892      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7893     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7894      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7895     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7896      0 (>= gnus-button-browse-level 0) browse-url 0)
7897     ("^Subject:" gnus-button-url-regexp
7898      0 (>= gnus-button-browse-level 0) browse-url 0)
7899     ("^[^:]+:" gnus-button-url-regexp
7900      0 (>= gnus-button-browse-level 0) browse-url 0)
7901     ("^OpenPGP:.*url=" gnus-button-url-regexp
7902      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7903     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7904      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7905     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7906      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7907   "*Alist of headers and regexps to match buttons in article heads.
7908
7909 This alist is very similar to `gnus-button-alist', except that each
7910 alist has an additional HEADER element first in each entry:
7911
7912 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7913
7914 HEADER is a regexp to match a header.  For a fuller explanation, see
7915 `gnus-button-alist'."
7916   :group 'gnus-article-buttons
7917   :group 'gnus-article-headers
7918   :type '(repeat (list (regexp :tag "Header")
7919                        (choice regexp variable)
7920                        (integer :tag "Button")
7921                        (sexp :tag "Form")
7922                        (function :tag "Callback")
7923                        (repeat :tag "Par"
7924                                :inline t
7925                                (integer :tag "Regexp group")))))
7926 (put 'gnus-header-button-alist 'risky-local-variable t)
7927
7928 ;;; Commands:
7929
7930 (defun gnus-article-push-button (event)
7931   "Check text under the mouse pointer for a callback function.
7932 If the text under the mouse pointer has a `gnus-callback' property,
7933 call it with the value of the `gnus-data' text property."
7934   (interactive "e")
7935   (set-buffer (window-buffer (posn-window (event-start event))))
7936   (let* ((pos (posn-point (event-start event)))
7937          (data (get-text-property pos 'gnus-data))
7938          (fun (get-text-property pos 'gnus-callback)))
7939     (goto-char pos)
7940     (when fun
7941       (funcall fun data))))
7942
7943 (defun gnus-article-press-button ()
7944   "Check text at point for a callback function.
7945 If the text at point has a `gnus-callback' property,
7946 call it with the value of the `gnus-data' text property."
7947   (interactive)
7948   (let ((data (get-text-property (point) 'gnus-data))
7949         (fun (get-text-property (point) 'gnus-callback)))
7950     (when fun
7951       (funcall fun data))))
7952
7953 (defun gnus-article-highlight (&optional force)
7954   "Highlight current article.
7955 This function calls `gnus-article-highlight-headers',
7956 `gnus-article-highlight-citation',
7957 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7958 do the highlighting.  See the documentation for those functions."
7959   (interactive (list 'force))
7960   (gnus-article-highlight-headers)
7961   (gnus-article-highlight-citation force)
7962   (gnus-article-highlight-signature)
7963   (gnus-article-add-buttons)
7964   (gnus-article-add-buttons-to-head))
7965
7966 (defun gnus-article-highlight-some (&optional force)
7967   "Highlight current article.
7968 This function calls `gnus-article-highlight-headers',
7969 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7970 do the highlighting.  See the documentation for those functions."
7971   (interactive (list 'force))
7972   (gnus-article-highlight-headers)
7973   (gnus-article-highlight-signature)
7974   (gnus-article-add-buttons))
7975
7976 (defun gnus-article-highlight-headers ()
7977   "Highlight article headers as specified by `gnus-header-face-alist'."
7978   (interactive)
7979   (gnus-with-article-headers
7980     (let (regexp header-face field-face from hpoints fpoints)
7981       (dolist (entry gnus-header-face-alist)
7982         (goto-char (point-min))
7983         (setq regexp (concat "^\\("
7984                              (if (string-equal "" (nth 0 entry))
7985                                  "[^\t ]"
7986                                (nth 0 entry))
7987                              "\\)")
7988               header-face (nth 1 entry)
7989               field-face (nth 2 entry))
7990         (while (and (re-search-forward regexp nil t)
7991                     (not (eobp)))
7992           (beginning-of-line)
7993           (setq from (point))
7994           (unless (search-forward ":" nil t)
7995             (forward-char 1))
7996           (when (and header-face
7997                      (not (memq (point) hpoints)))
7998             (push (point) hpoints)
7999             (gnus-put-text-property from (point) 'face header-face))
8000           (when (and field-face
8001                      (not (memq (setq from (point)) fpoints)))
8002             (push from fpoints)
8003             (if (re-search-forward "^[^ \t]" nil t)
8004                 (forward-char -2)
8005               (goto-char (point-max)))
8006             (gnus-put-text-property from (point) 'face field-face)))))))
8007
8008 (defun gnus-article-highlight-signature ()
8009   "Highlight the signature in an article.
8010 It does this by highlighting everything after
8011 `gnus-signature-separator' using the face `gnus-signature'."
8012   (interactive)
8013   (gnus-with-article-buffer
8014     (let ((inhibit-point-motion-hooks t))
8015       (save-restriction
8016         (when (and gnus-signature-face
8017                    (gnus-article-narrow-to-signature))
8018           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
8019                             'face gnus-signature-face)
8020           (widen)
8021           (gnus-article-search-signature)
8022           (let ((start (match-beginning 0))
8023                 (end (set-marker (make-marker) (1+ (match-end 0)))))
8024             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
8025                                      end)))))))
8026
8027 (defun gnus-button-in-region-p (b e prop)
8028   "Say whether PROP exists in the region."
8029   (text-property-not-all b e prop nil))
8030
8031 (defun gnus-article-add-buttons ()
8032   "Find external references in the article and make buttons of them.
8033 \"External references\" are things like Message-IDs and URLs, as
8034 specified by `gnus-button-alist'."
8035   (interactive)
8036   (gnus-with-article-buffer
8037     (let ((inhibit-point-motion-hooks t)
8038           (case-fold-search t)
8039           (alist gnus-button-alist)
8040           beg entry regexp)
8041       ;; We skip the headers.
8042       (article-goto-body)
8043       (setq beg (point))
8044       (while (setq entry (pop alist))
8045         (setq regexp (eval (car entry)))
8046         (goto-char beg)
8047         (while (re-search-forward regexp nil t)
8048           (let ((start (match-beginning (nth 1 entry)))
8049                 (end (match-end (nth 1 entry)))
8050                 (from (match-beginning 0)))
8051             (when (and (eval (nth 2 entry))
8052                        (not (gnus-button-in-region-p
8053                              start end 'gnus-callback)))
8054               ;; That optional form returned non-nil, so we add the
8055               ;; button.
8056               (setq from (set-marker (make-marker) from))
8057               (unless (and (eq (car entry) 'gnus-button-url-regexp)
8058                            (gnus-article-extend-url-button from start end))
8059                 (gnus-article-add-button start end
8060                                          'gnus-button-push (list from entry))
8061                 (gnus-put-text-property
8062                  start end
8063                  'gnus-string (buffer-substring-no-properties
8064                                start end))))))))))
8065
8066 (defun gnus-article-extend-url-button (beg start end)
8067   "Extend url button if url is folded into two or more lines.
8068 Return non-nil if button is extended.  BEG is a marker that points to
8069 the beginning position of a text containing url.  START and END are
8070 the endpoints of a url button before it is extended.  The concatenated
8071 url is put as the `gnus-button-url' overlay property on the button."
8072   (let ((opoint (point))
8073         (points (list start end))
8074         url delim regexp)
8075     (prog1
8076         (when (and (progn
8077                      (goto-char end)
8078                      (not (looking-at "[\t ]*[\">]")))
8079                    (progn
8080                      (goto-char start)
8081                      (string-match
8082                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
8083                       (buffer-substring (point-at-bol) start)))
8084                    (progn
8085                      (setq url (list (buffer-substring start end))
8086                            delim (if (match-beginning 1) ">" "\""))
8087                      (beginning-of-line)
8088                      (setq regexp (concat
8089                                    (when (and (looking-at
8090                                                message-cite-prefix-regexp)
8091                                               (< (match-end 0) start))
8092                                      (regexp-quote (match-string 0)))
8093                                    "\
8094 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
8095                                    delim "\\)"))
8096                      (while (progn
8097                               (forward-line 1)
8098                               (and (looking-at regexp)
8099                                    (prog1
8100                                        (match-beginning 1)
8101                                      (push (or (match-string 2)
8102                                                (match-string 1))
8103                                            url)
8104                                      (push (setq end (or (match-end 2)
8105                                                          (match-end 1)))
8106                                            points)
8107                                      (push (or (match-beginning 2)
8108                                                (match-beginning 1))
8109                                            points)))))
8110                      (match-beginning 2)))
8111           (let (gnus-article-mouse-face widget-mouse-face)
8112             (while points
8113               (gnus-article-add-button (pop points) (pop points)
8114                                        'gnus-button-push
8115                                        (list beg (assq 'gnus-button-url-regexp
8116                                                        gnus-button-alist)))))
8117           (let ((overlay (gnus-make-overlay start end)))
8118             (gnus-overlay-put overlay 'evaporate t)
8119             (gnus-overlay-put overlay 'gnus-button-url
8120                               (list (mapconcat 'identity (nreverse url) "")))
8121             (when gnus-article-mouse-face
8122               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
8123           t)
8124       (goto-char opoint))))
8125
8126 ;; Add buttons to the head of an article.
8127 (defun gnus-article-add-buttons-to-head ()
8128   "Add buttons to the head of the article."
8129   (interactive)
8130   (gnus-with-article-headers
8131     (let (beg end)
8132       (dolist (entry gnus-header-button-alist)
8133         ;; Each alist entry.
8134         (goto-char (point-min))
8135         (while (re-search-forward (car entry) nil t)
8136           ;; Each header matching the entry.
8137           (setq beg (match-beginning 0))
8138           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
8139                              (match-beginning 0))
8140                         (point-max)))
8141           (goto-char beg)
8142           (while (re-search-forward (eval (nth 1 entry)) end t)
8143             ;; Each match within a header.
8144             (let* ((entry (cdr entry))
8145                    (start (match-beginning (nth 1 entry)))
8146                    (end (match-end (nth 1 entry)))
8147                    (form (nth 2 entry)))
8148               (goto-char (match-end 0))
8149               (when (eval form)
8150                 (gnus-article-add-button
8151                  start end (nth 3 entry)
8152                  (buffer-substring (match-beginning (nth 4 entry))
8153                                    (match-end (nth 4 entry)))))))
8154           (goto-char end))))))
8155
8156 ;;; External functions:
8157
8158 (defun gnus-article-add-button (from to fun &optional data text)
8159   "Create a button between FROM and TO with callback FUN and data DATA."
8160   (when gnus-article-button-face
8161     (gnus-overlay-put (gnus-make-overlay from to nil t)
8162                       'face gnus-article-button-face))
8163   (gnus-add-text-properties
8164    from to
8165    (nconc (and gnus-article-mouse-face
8166                (list gnus-mouse-face-prop gnus-article-mouse-face))
8167           (list 'gnus-callback fun)
8168           (and data (list 'gnus-data data))))
8169   (widget-convert-button 'link from to :action 'gnus-widget-press-button
8170                          :help-echo (or text "Follow the link")
8171                          :keymap gnus-url-button-map
8172                          :button-keymap gnus-widget-button-keymap))
8173
8174 (defun gnus-article-copy-string ()
8175   "Copy the string in the button to the kill ring."
8176   (interactive)
8177   (gnus-article-check-buffer)
8178   (let ((data (get-text-property (point) 'gnus-string)))
8179     (when data
8180       (with-temp-buffer
8181         (insert data)
8182         (copy-region-as-kill (point-min) (point-max))
8183         (message "Copied %s" data)))))
8184
8185 ;;; Internal functions:
8186
8187 (defun gnus-article-set-globals ()
8188   (with-current-buffer gnus-summary-buffer
8189     (gnus-set-global-variables)))
8190
8191 (defun gnus-signature-toggle (end)
8192   (gnus-with-article-buffer
8193     (let ((inhibit-point-motion-hooks t))
8194       (if (text-property-any end (point-max) 'article-type 'signature)
8195           (progn
8196             (gnus-delete-wash-type 'signature)
8197             (gnus-remove-text-properties-when
8198              'article-type 'signature end (point-max)
8199              (cons 'article-type (cons 'signature
8200                                        gnus-hidden-properties))))
8201         (gnus-add-wash-type 'signature)
8202         (gnus-add-text-properties-when
8203          'article-type nil end (point-max)
8204          (cons 'article-type (cons 'signature
8205                                    gnus-hidden-properties)))))
8206     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
8207       (gnus-set-mode-line 'article))))
8208
8209 (defun gnus-button-push (marker-and-entry)
8210   ;; Push button starting at MARKER.
8211   (save-excursion
8212     (let* ((marker (car marker-and-entry))
8213            (entry (cadr marker-and-entry))
8214            (regexp (car entry))
8215            (inhibit-point-motion-hooks t))
8216       (goto-char marker)
8217       ;; This is obviously true, or something bad is happening :)
8218       ;; But we need it to have the match-data
8219       (when (looking-at (or (if (symbolp regexp)
8220                                 (symbol-value regexp)
8221                               regexp)))
8222         (let ((fun (nth 3 entry))
8223               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
8224                              (get-char-property marker 'gnus-button-url))
8225                         (mapcar (lambda (group)
8226                                   (let ((string (match-string group)))
8227                                     (set-text-properties
8228                                      0 (length string) nil string)
8229                                     string))
8230                                 (nthcdr 4 entry)))))
8231
8232           (cond
8233            ((fboundp fun)
8234             (apply fun args))
8235            ((and (boundp fun)
8236                  (fboundp (symbol-value fun)))
8237             (apply (symbol-value fun) args))
8238            (t
8239             (gnus-message 1 "You must define `%S' to use this button"
8240                           (cons fun args)))))))))
8241
8242 (defun gnus-parse-news-url (url)
8243   (let (scheme server port group message-id articles)
8244     (with-temp-buffer
8245       (insert url)
8246       (goto-char (point-min))
8247       (when (looking-at "\\([A-Za-z]+\\):")
8248         (setq scheme (match-string 1))
8249         (goto-char (match-end 0)))
8250       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8251         (setq server (match-string 1))
8252         (setq port (if (stringp (match-string 3))
8253                        (string-to-number (match-string 3))
8254                      (match-string 3)))
8255         (goto-char (match-end 0)))
8256
8257       (cond
8258        ((looking-at "\\(.*@.*\\)")
8259         (setq message-id (match-string 1)))
8260        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8261         (setq group (match-string 1)
8262               articles (split-string (match-string 2) "-")))
8263        ((looking-at "\\([^/]+\\)/?")
8264         (setq group (match-string 1)))
8265        (t
8266         (error "Unknown news URL syntax"))))
8267     (list scheme server port group message-id articles)))
8268
8269 (defun gnus-button-handle-news (url)
8270   "Fetch a news URL."
8271   (destructuring-bind (scheme server port group message-id articles)
8272       (gnus-parse-news-url url)
8273     (cond
8274      (message-id
8275       (with-current-buffer gnus-summary-buffer
8276         (if server
8277             (let ((gnus-refer-article-method
8278                    (nconc (list (list 'nntp server))
8279                           gnus-refer-article-method))
8280                   (nntp-port-number (or port "nntp")))
8281               (gnus-message 7 "Fetching %s with %s"
8282                             message-id gnus-refer-article-method)
8283               (gnus-summary-refer-article message-id))
8284           (gnus-summary-refer-article message-id))))
8285      (group
8286       (gnus-button-fetch-group url)))))
8287
8288 (defun gnus-button-patch (library line)
8289   "Visit an Emacs Lisp library LIBRARY on line LINE."
8290   (interactive)
8291   (let ((file (locate-library (file-name-nondirectory library))))
8292     (unless file
8293       (error "Couldn't find library %s" library))
8294     (find-file file)
8295     (goto-char (point-min))
8296     (forward-line (1- (string-to-number line)))))
8297
8298 (defun gnus-button-handle-man (url)
8299   "Fetch a man page."
8300   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8301   (when (eq gnus-button-man-handler 'woman)
8302     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8303   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8304   (funcall gnus-button-man-handler url))
8305
8306 (defun gnus-button-handle-info-url (url)
8307   "Fetch an info URL."
8308   (setq url (mm-subst-char-in-string ?+ ?\  url))
8309   (cond
8310    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8311     (gnus-info-find-node
8312      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8313                      "Gnus")
8314              ")" (gnus-url-unhex-string (match-string 2 url)))))
8315    ((string-match "([^)\"]+)[^\"]+" url)
8316     (setq url
8317           (gnus-replace-in-string
8318            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8319     (gnus-info-find-node url))
8320    (t (error "Can't parse %s" url))))
8321
8322 (defun gnus-button-handle-info-url-gnome (url)
8323   "Fetch GNOME style info URL."
8324   (setq url (mm-subst-char-in-string ?_ ?\  url))
8325   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8326       (gnus-info-find-node
8327        (concat "("
8328                (gnus-url-unhex-string
8329                  (match-string 1 url))
8330                ")"
8331                (or (gnus-url-unhex-string
8332                     (match-string 2 url))
8333                    "Top")))
8334     (error "Can't parse %s" url)))
8335
8336 (defun gnus-button-handle-info-url-kde (url)
8337   "Fetch KDE style info URL."
8338   (gnus-info-find-node (gnus-url-unhex-string url)))
8339
8340 ;; (info) will autoload info.el
8341 (declare-function Info-menu "info" (menu-item &optional fork))
8342 (declare-function Info-index-next "info" (num))
8343
8344 (defun gnus-button-handle-info-keystrokes (url)
8345   "Call `info' when pushing the corresponding URL button."
8346   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8347   (let (node indx comma)
8348     (if (string-match
8349          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8350                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8351                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8352                  "\\(?:[ \t\n,]*\\)\\)?")
8353          url)
8354         (setq node (match-string 2 url)
8355               indx (match-string 3 url))
8356       (error "Can't parse %s" url))
8357     (info)
8358     (Info-directory)
8359     (Info-menu node)
8360     (when (> (length indx) 0)
8361       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8362                             "\\([ \t\n,]*\\)")
8363                     indx)
8364       (setq comma (match-string 2 indx))
8365       (setq indx  (match-string 1 indx))
8366       (Info-index indx)
8367       (when comma
8368         (dotimes (i (with-temp-buffer
8369                       (insert comma)
8370                       ;; Note: the XEmacs version of `how-many' takes
8371                       ;; no optional argument.
8372                       (goto-char (point-min))
8373                       (how-many ",")))
8374           (Info-index-next 1)))
8375       nil)))
8376
8377 (autoload 'pgg-snarf-keys-region "pgg")
8378 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8379 (declare-function pgg-display-output-buffer "pgg" (start end status))
8380
8381 (defun gnus-button-openpgp (url)
8382   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8383   (with-temp-buffer
8384     (mm-url-insert-file-contents-external url)
8385     (pgg-snarf-keys-region (point-min) (point-max))
8386     (pgg-display-output-buffer nil nil nil)))
8387
8388 (defun gnus-button-message-id (message-id)
8389   "Fetch MESSAGE-ID."
8390   (with-current-buffer gnus-summary-buffer
8391     (gnus-summary-refer-article message-id)))
8392
8393 (defun gnus-button-fetch-group (address &rest ignore)
8394   "Fetch GROUP specified by ADDRESS."
8395   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8396                       address)
8397     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8398     ;; for nntp:// and news://
8399     (setq address (match-string 3 address)))
8400   (if (not (string-match "[:/]" address))
8401       ;; This is just a simple group url.
8402       (gnus-group-read-ephemeral-group address gnus-select-method)
8403     (if (not
8404          (string-match
8405           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8406           address))
8407         (error "Can't parse %s" address)
8408       (gnus-group-read-ephemeral-group
8409        (match-string 4 address)
8410        `(nntp ,(match-string 1 address)
8411               (nntp-address ,(match-string 1 address))
8412               (nntp-port-number ,(if (match-end 3)
8413                                      (match-string 3 address)
8414                                    "nntp")))
8415        nil nil nil
8416        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8417
8418 (defun gnus-url-parse-query-string (query &optional downcase)
8419   (let (retval pairs cur key val)
8420     (setq pairs (split-string query "&"))
8421     (while pairs
8422       (setq cur (car pairs)
8423             pairs (cdr pairs))
8424       (if (not (string-match "=" cur))
8425           nil                           ; Grace
8426         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8427               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8428         (if downcase
8429             (setq key (downcase key)))
8430         (setq cur (assoc key retval))
8431         (if cur
8432             (setcdr cur (cons val (cdr cur)))
8433           (setq retval (cons (list key val) retval)))))
8434     retval))
8435
8436 (defun gnus-url-mailto (url)
8437   ;; Send mail to someone
8438   (setq url (replace-regexp-in-string "\n" " " url))
8439   (when (string-match "mailto:/*\\(.*\\)" url)
8440     (setq url (substring url (match-beginning 1) nil)))
8441   (let (to args subject func)
8442     (setq args (gnus-url-parse-query-string
8443                 (if (string-match "^\\?" url)
8444                     (substring url 1)
8445                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8446                       (concat "to=" (match-string 1 url) "&"
8447                               (match-string 2 url))
8448                     (concat "to=" url))))
8449           subject (cdr-safe (assoc "subject" args)))
8450     (gnus-msg-mail)
8451     (while args
8452       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8453       (if (fboundp func)
8454           (funcall func)
8455         (message-position-on-field (caar args)))
8456       (insert (gnus-replace-in-string
8457                (mapconcat 'identity (reverse (cdar args)) ", ")
8458                "\r\n" "\n" t))
8459       (setq args (cdr args)))
8460     (if subject
8461         (message-goto-body)
8462       (message-goto-subject))))
8463
8464 (defun gnus-button-embedded-url (address)
8465   "Activate ADDRESS with `browse-url'."
8466   (browse-url (gnus-strip-whitespace address)))
8467
8468 ;;; Next/prev buttons in the article buffer.
8469
8470 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8471 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8472
8473 (defvar gnus-prev-page-map
8474   (let ((map (make-sparse-keymap)))
8475     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8476     (define-key map "\r" 'gnus-button-prev-page)
8477     map))
8478
8479 (defvar gnus-next-page-map
8480   (let ((map (make-sparse-keymap)))
8481     (define-key map gnus-mouse-2 'gnus-button-next-page)
8482     (define-key map "\r" 'gnus-button-next-page)
8483     map))
8484
8485 (defun gnus-insert-prev-page-button ()
8486   (let ((b (point)) e
8487         (inhibit-read-only t))
8488     (gnus-eval-format
8489      gnus-prev-page-line-format nil
8490      `(keymap ,gnus-prev-page-map
8491               gnus-prev t
8492               gnus-callback gnus-article-button-prev-page
8493               article-type annotation))
8494     (setq e (if (bolp)
8495                 ;; Exclude a newline.
8496                 (1- (point))
8497               (point)))
8498     (when gnus-article-button-face
8499       (gnus-overlay-put (gnus-make-overlay b e nil t)
8500                         'face gnus-article-button-face))
8501     (widget-convert-button
8502      'link b e
8503      :action 'gnus-button-prev-page
8504      :button-keymap gnus-prev-page-map)))
8505
8506 (defun gnus-button-next-page (&optional args more-args)
8507   "Go to the next page."
8508   (interactive)
8509   (let ((win (selected-window)))
8510     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8511     (gnus-article-next-page)
8512     (select-window win)))
8513
8514 (defun gnus-button-prev-page (&optional args more-args)
8515   "Go to the prev page."
8516   (interactive)
8517   (let ((win (selected-window)))
8518     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8519     (gnus-article-prev-page)
8520     (select-window win)))
8521
8522 (defun gnus-insert-next-page-button ()
8523   (let ((b (point)) e
8524         (inhibit-read-only t))
8525     (gnus-eval-format gnus-next-page-line-format nil
8526                       `(keymap ,gnus-next-page-map
8527                                gnus-next t
8528                                gnus-callback gnus-article-button-next-page
8529                                article-type annotation))
8530     (setq e (if (bolp)
8531                 ;; Exclude a newline.
8532                 (1- (point))
8533               (point)))
8534     (when gnus-article-button-face
8535       (gnus-overlay-put (gnus-make-overlay b e nil t)
8536                         'face gnus-article-button-face))
8537     (widget-convert-button
8538      'link b e
8539      :action 'gnus-button-next-page
8540      :button-keymap gnus-next-page-map)))
8541
8542 (defun gnus-article-button-next-page (arg)
8543   "Go to the next page."
8544   (interactive "P")
8545   (let ((win (selected-window)))
8546     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8547     (gnus-article-next-page)
8548     (select-window win)))
8549
8550 (defun gnus-article-button-prev-page (arg)
8551   "Go to the prev page."
8552   (interactive "P")
8553   (let ((win (selected-window)))
8554     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8555     (gnus-article-prev-page)
8556     (select-window win)))
8557
8558 (defvar gnus-decode-header-methods
8559   '(mail-decode-encoded-word-region)
8560   "List of methods used to decode headers.
8561
8562 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8563 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8564 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8565 whose names match REGEXP.
8566
8567 For example:
8568 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8569  mail-decode-encoded-word-region
8570  (\"chinese\" . rfc1843-decode-region))
8571 ")
8572
8573 (defvar gnus-decode-header-methods-cache nil)
8574
8575 (defun gnus-multi-decode-header (start end)
8576   "Apply the functions from `gnus-encoded-word-methods' that match."
8577   (unless (and gnus-decode-header-methods-cache
8578                (eq gnus-newsgroup-name
8579                    (car gnus-decode-header-methods-cache)))
8580     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8581     (dolist (x gnus-decode-header-methods)
8582       (if (symbolp x)
8583           (nconc gnus-decode-header-methods-cache (list x))
8584         (if (and gnus-newsgroup-name
8585                  (string-match (car x) gnus-newsgroup-name))
8586             (nconc gnus-decode-header-methods-cache
8587                    (list (cdr x)))))))
8588   (let ((xlist gnus-decode-header-methods-cache))
8589     (pop xlist)
8590     (save-restriction
8591       (narrow-to-region start end)
8592       (while xlist
8593         (funcall (pop xlist) (point-min) (point-max))))))
8594
8595 ;;;
8596 ;;; Treatment top-level handling.
8597 ;;;
8598
8599 (defvar gnus-inhibit-article-treatments nil)
8600
8601 (defun gnus-treat-article (gnus-treat-condition
8602                            &optional part-number total-parts gnus-treat-type)
8603   (let ((gnus-treat-length (- (point-max) (point-min)))
8604         (alist gnus-treatment-function-alist)
8605         (article-goto-body-goes-to-point-min-p t)
8606         (treated-type
8607          (or (not gnus-treat-type)
8608              (catch 'found
8609                (let ((list gnus-article-treat-types))
8610                  (while list
8611                    (when (string-match (pop list) gnus-treat-type)
8612                      (throw 'found t)))))))
8613         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8614         val elem)
8615     (gnus-run-hooks 'gnus-part-display-hook)
8616     (dolist (elem alist)
8617       (setq val
8618             (save-excursion
8619               (when (gnus-buffer-live-p gnus-summary-buffer)
8620                 (set-buffer gnus-summary-buffer))
8621               (symbol-value (car elem))))
8622       (when (and (or (consp val)
8623                      treated-type)
8624                  (or (not gnus-inhibit-article-treatments)
8625                      (eq gnus-treat-condition 'head))
8626                  (gnus-treat-predicate val)
8627                  (or (not (get (car elem) 'highlight))
8628                      highlightp))
8629         (save-restriction
8630           (funcall (cadr elem)))))))
8631
8632 ;; Dynamic variables.
8633 (defvar part-number)
8634 (defvar total-parts)
8635 (defvar gnus-treat-type)
8636 (defvar gnus-treat-condition)
8637 (defvar gnus-treat-length)
8638
8639 (defun gnus-treat-predicate (val)
8640   (cond
8641    ((null val)
8642     nil)
8643    (gnus-treat-condition
8644     (eq gnus-treat-condition val))
8645    ((and (listp val)
8646          (stringp (car val)))
8647     (apply 'gnus-or (mapcar `(lambda (s)
8648                                (string-match s ,(or gnus-newsgroup-name "")))
8649                             val)))
8650    ((listp val)
8651     (let ((pred (pop val)))
8652       (cond
8653        ((eq pred 'or)
8654         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8655        ((eq pred 'and)
8656         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8657        ((eq pred 'not)
8658         (not (gnus-treat-predicate (car val))))
8659        ((eq pred 'typep)
8660         (equal (car val) gnus-treat-type))
8661        ((functionp pred)
8662         (funcall pred))
8663        (t
8664         (error "%S is not a valid predicate" pred)))))
8665    ((eq val t)
8666     t)
8667    ((eq val 'head)
8668     nil)
8669    ((eq val 'first)
8670     (eq part-number 1))
8671    ((eq val 'last)
8672     (eq part-number total-parts))
8673    ((numberp val)
8674     (< gnus-treat-length val))
8675    (t
8676     (error "%S is not a valid value" val))))
8677
8678 (defun gnus-article-encrypt-body (protocol &optional n)
8679   "Encrypt the article body."
8680   (interactive
8681    (list
8682     (or gnus-article-encrypt-protocol
8683         (gnus-completing-read "Encrypt protocol"
8684                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8685                               t))
8686     current-prefix-arg))
8687   ;; User might hit `K E' instead of `K e', so prompt once.
8688   (when (and gnus-article-encrypt-protocol
8689              gnus-novice-user)
8690     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8691       (error "Encrypt aborted")))
8692   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8693     (unless func
8694       (error "Can't find the encrypt protocol %s" protocol))
8695     (if (member gnus-newsgroup-name '("nndraft:delayed"
8696                                       "nndraft:drafts"
8697                                       "nndraft:queue"))
8698         (error "Can't encrypt the article in group %s"
8699                gnus-newsgroup-name))
8700     (gnus-summary-iterate n
8701       (with-current-buffer gnus-summary-buffer
8702         (let ((mail-parse-charset gnus-newsgroup-charset)
8703               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8704               (summary-buffer gnus-summary-buffer)
8705               references point)
8706           (gnus-set-global-variables)
8707           (when (gnus-group-read-only-p)
8708             (error "The current newsgroup does not support article encrypt"))
8709           (gnus-summary-show-article t)
8710           (setq references
8711               (or (mail-header-references gnus-current-headers) ""))
8712           (set-buffer gnus-article-buffer)
8713           (let* ((inhibit-read-only t)
8714                  (headers
8715                   (mapcar (lambda (field)
8716                             (and (save-restriction
8717                                    (message-narrow-to-head)
8718                                    (goto-char (point-min))
8719                                    (search-forward field nil t))
8720                                  (prog2
8721                                      (message-narrow-to-field)
8722                                      (buffer-string)
8723                                    (delete-region (point-min) (point-max))
8724                                    (widen))))
8725                           '("Content-Type:" "Content-Transfer-Encoding:"
8726                             "Content-Disposition:"))))
8727             (message-narrow-to-head)
8728             (message-remove-header "MIME-Version")
8729             (goto-char (point-max))
8730             (setq point (point))
8731             (insert (apply 'concat headers))
8732             (widen)
8733             (narrow-to-region point (point-max))
8734             (let ((message-options message-options))
8735               (message-options-set 'message-sender user-mail-address)
8736               (message-options-set 'message-recipients user-mail-address)
8737               (message-options-set 'message-sign-encrypt 'not)
8738               (funcall func))
8739             (goto-char (point-min))
8740             (insert "MIME-Version: 1.0\n")
8741             (widen)
8742             (gnus-summary-edit-article-done
8743              references nil summary-buffer t))
8744           (when gnus-keep-backlog
8745             (gnus-backlog-remove-article
8746              (car gnus-article-current) (cdr gnus-article-current)))
8747           (gnus-flush-original-article-buffer)
8748           (when gnus-use-cache
8749             (gnus-cache-update-article
8750              (car gnus-article-current) (cdr gnus-article-current))))))))
8751
8752 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8753   "The following specs can be used:
8754 %t  The security MIME type
8755 %i  Additional info
8756 %d  Details
8757 %D  Details if button is pressed")
8758
8759 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8760   "The following specs can be used:
8761 %t  The security MIME type
8762 %i  Additional info
8763 %d  Details
8764 %D  Details if button is pressed")
8765
8766 (defvar gnus-mime-security-button-line-format-alist
8767   '((?t gnus-tmp-type ?s)
8768     (?i gnus-tmp-info ?s)
8769     (?d gnus-tmp-details ?s)
8770     (?D gnus-tmp-pressed-details ?s)))
8771
8772 (defvar gnus-mime-security-button-commands
8773   '((gnus-article-press-button "\r" "Show Detail")
8774     (undefined "v")
8775     (undefined "t")
8776     (undefined "C")
8777     (gnus-mime-security-save-part "o" "Save...")
8778     (undefined "\C-o")
8779     (undefined "r")
8780     (undefined "d")
8781     (undefined "c")
8782     (undefined "i")
8783     (undefined "E")
8784     (undefined "e")
8785     (undefined "p")
8786     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8787     (undefined ".")))
8788
8789 (defvar gnus-mime-security-button-map
8790   (let ((map (make-sparse-keymap)))
8791     (define-key map gnus-mouse-2 'gnus-article-push-button)
8792     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8793     (dolist (c gnus-mime-security-button-commands)
8794       (define-key map (cadr c) (car c)))
8795     map))
8796
8797 (easy-menu-define
8798   gnus-mime-security-button-menu gnus-mime-security-button-map
8799   "Security button menu."
8800   `("Security Part"
8801     ,@(delq nil
8802             (mapcar (lambda (c)
8803                       (unless (eq (car c) 'undefined)
8804                         (vector (caddr c) (car c) :active t)))
8805                     gnus-mime-security-button-commands))))
8806
8807 (defun gnus-mime-security-button-menu (event prefix)
8808   "Construct a context-sensitive menu of security commands."
8809   (interactive "e\nP")
8810   (save-window-excursion
8811     (let ((pos (event-start event)))
8812       (select-window (posn-window pos))
8813       (goto-char (posn-point pos))
8814       (gnus-article-check-buffer)
8815       (popup-menu gnus-mime-security-button-menu nil prefix))))
8816
8817 (defvar gnus-mime-security-details-buffer nil)
8818
8819 (defvar gnus-mime-security-button-pressed nil)
8820
8821 (defvar gnus-mime-security-show-details-inline t
8822   "If non-nil, show details in the article buffer.")
8823
8824 (defun gnus-mime-security-verify-or-decrypt (handle)
8825   (mm-remove-parts (cdr handle))
8826   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8827         point (inhibit-read-only t))
8828     (if region
8829         (goto-char (car region)))
8830     (setq point (point))
8831     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8832       (let* ((mm-verify-option 'known)
8833              (mm-decrypt-option 'known)
8834              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8835         (unless (eq nparts (cdr handle))
8836           (mm-destroy-parts (cdr handle))
8837           (setcdr handle nparts))))
8838     (gnus-mime-display-security handle)
8839     (when region
8840       (delete-region (point) (cdr region))
8841       (set-marker (car region) nil)
8842       (set-marker (cdr region) nil))
8843     (goto-char point)))
8844
8845 (defun gnus-mime-security-show-details (handle)
8846   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8847     (if (not details)
8848         (gnus-message 5 "No details.")
8849       (if gnus-mime-security-show-details-inline
8850           (let ((gnus-mime-security-button-pressed
8851                  (not (get-text-property (point) 'gnus-mime-details)))
8852                 (gnus-mime-security-button-line-format
8853                  (get-text-property (point) 'gnus-line-format))
8854                 (inhibit-read-only t))
8855             (forward-char -1)
8856             (while (eq (get-text-property (point) 'gnus-line-format)
8857                        gnus-mime-security-button-line-format)
8858               (forward-char -1))
8859             (forward-char)
8860             (save-restriction
8861               (narrow-to-region (point) (point))
8862               (gnus-insert-mime-security-button handle))
8863             (delete-region (point)
8864                            (or (text-property-not-all
8865                                 (point) (point-max)
8866                                 'gnus-line-format
8867                                 gnus-mime-security-button-line-format)
8868                                (point-max))))
8869         ;; Not inlined.
8870         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8871             (with-current-buffer gnus-mime-security-details-buffer
8872               (erase-buffer)
8873               t)
8874           (setq gnus-mime-security-details-buffer
8875                 (gnus-get-buffer-create "*MIME Security Details*")))
8876         (with-current-buffer gnus-mime-security-details-buffer
8877           (insert details)
8878           (goto-char (point-min)))
8879         (pop-to-buffer gnus-mime-security-details-buffer)))))
8880
8881 (defun gnus-mime-security-press-button (handle)
8882   (save-excursion
8883     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8884         (gnus-mime-security-show-details handle)
8885       (gnus-mime-security-verify-or-decrypt handle))))
8886
8887 (defun gnus-insert-mime-security-button (handle &optional displayed)
8888   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8889          (gnus-tmp-type
8890           (concat
8891            (or (nth 2 (assoc protocol mm-verify-function-alist))
8892                (nth 2 (assoc protocol mm-decrypt-function-alist))
8893                "Unknown")
8894            (if (equal (car handle) "multipart/signed")
8895                " Signed" " Encrypted")
8896            " Part"))
8897          (gnus-tmp-info
8898           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8899               "Undecided"))
8900          (gnus-tmp-details
8901           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8902          gnus-tmp-pressed-details
8903          b e)
8904     (setq gnus-tmp-details
8905           (if gnus-tmp-details
8906               (concat "\n" gnus-tmp-details)
8907             ""))
8908     (setq gnus-tmp-pressed-details
8909           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8910     (unless (bolp)
8911       (insert "\n"))
8912     (setq b (point))
8913     (gnus-eval-format
8914      gnus-mime-security-button-line-format
8915      gnus-mime-security-button-line-format-alist
8916      `(keymap ,gnus-mime-security-button-map
8917          gnus-callback gnus-mime-security-press-button
8918          gnus-line-format ,gnus-mime-security-button-line-format
8919          gnus-mime-details ,gnus-mime-security-button-pressed
8920          article-type annotation
8921          gnus-data ,handle))
8922     (setq e (if (bolp)
8923                 ;; Exclude a newline.
8924                 (1- (point))
8925               (point)))
8926     (when gnus-article-button-face
8927       (gnus-overlay-put (gnus-make-overlay b e nil t)
8928                         'face gnus-article-button-face))
8929     (widget-convert-button
8930      'link b e
8931      :mime-handle handle
8932      :action 'gnus-widget-press-button
8933      :button-keymap gnus-mime-security-button-map
8934      :help-echo
8935      (lambda (widget)
8936        ;; Needed to properly clear the message due to a bug in
8937        ;; wid-edit (XEmacs only).
8938        (when (boundp 'help-echo-owns-message)
8939          (setq help-echo-owns-message t))
8940        (format
8941         "%S: show detail; %S: more options"
8942         (aref gnus-mouse-2 0)
8943         (aref gnus-down-mouse-3 0))))))
8944
8945 (defun gnus-mime-display-security (handle)
8946   (save-restriction
8947     (narrow-to-region (point) (point))
8948     (unless (gnus-unbuttonized-mime-type-p (car handle))
8949       (gnus-insert-mime-security-button handle))
8950     (gnus-mime-display-part (cadr handle))
8951     (unless (bolp)
8952       (insert "\n"))
8953     (unless (gnus-unbuttonized-mime-type-p (car handle))
8954       (let ((gnus-mime-security-button-line-format
8955              gnus-mime-security-button-end-line-format))
8956         (gnus-insert-mime-security-button handle)))
8957     (mm-set-handle-multipart-parameter
8958      handle 'gnus-region (cons (point-min-marker) (point-max-marker)))
8959     (goto-char (point-max))))
8960
8961 (defun gnus-mime-security-run-function (function)
8962   "Run FUNCTION with the security part under point."
8963   (gnus-article-check-buffer)
8964   (let ((data (get-text-property (point) 'gnus-data))
8965         buffer handle)
8966     (when (and (stringp (car-safe data))
8967                (setq buffer (mm-handle-multipart-original-buffer data))
8968                (setq handle (cadr data)))
8969       (if (bufferp (mm-handle-buffer handle))
8970           (progn
8971             (setq handle (cons buffer (copy-sequence (cdr handle))))
8972             (mm-handle-set-undisplayer handle nil))
8973         (setq handle (mm-make-handle
8974                       buffer
8975                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8976                       nil nil nil nil nil nil)))
8977       (funcall function handle))))
8978
8979 (defun gnus-mime-security-save-part ()
8980   "Save the security part under point."
8981   (interactive)
8982   (gnus-mime-security-run-function 'mm-save-part))
8983
8984 (defun gnus-mime-security-pipe-part ()
8985   "Pipe the security part under point to a process."
8986   (interactive)
8987   (gnus-mime-security-run-function 'mm-pipe-part))
8988
8989 (gnus-ems-redefine)
8990
8991 (provide 'gnus-art)
8992
8993 (run-hooks 'gnus-art-load-hook)
8994
8995 ;;; gnus-art.el ends here