Fix XEmacs compilation
[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                               idna-program
1633                               (executable-find idna-program))
1634   "Whether IDNA decoding of headers is used when viewing messages.
1635 This requires GNU Libidn, and by default only enabled if it is found."
1636   :version "22.1"
1637   :group 'gnus-article-headers
1638   :type 'boolean)
1639
1640 (defcustom gnus-article-over-scroll nil
1641   "If non-nil, allow scrolling the article buffer even when there no more text."
1642   :version "22.1"
1643   :group 'gnus-article
1644   :type 'boolean)
1645
1646 (defcustom gnus-inhibit-images nil
1647   "Non-nil means inhibit displaying of images inline in the article body."
1648   :version "24.1"
1649   :group 'gnus-article
1650   :type 'boolean)
1651
1652 (defcustom gnus-blocked-images 'gnus-block-private-groups
1653   "Images that have URLs matching this regexp will be blocked.
1654 This can also be a function to be evaluated.  If so, it will be
1655 called with the group name as the parameter, and should return a
1656 regexp."
1657   :version "24.1"
1658   :group 'gnus-art
1659   :type '(choice regexp function))
1660
1661 ;;; Internal variables
1662
1663 (defvar gnus-english-month-names
1664   '("January" "February" "March" "April" "May" "June" "July" "August"
1665     "September" "October" "November" "December"))
1666
1667 (defvar article-goto-body-goes-to-point-min-p nil)
1668 (defvar gnus-article-wash-types nil)
1669 (defvar gnus-article-emphasis-alist nil)
1670 (defvar gnus-article-image-alist nil)
1671
1672 (defvar gnus-article-mime-handle-alist-1 nil)
1673 (defvar gnus-treatment-function-alist
1674   '((gnus-treat-strip-cr gnus-article-remove-cr)
1675     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1676     (gnus-treat-strip-banner gnus-article-strip-banner)
1677     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1678     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1679     (gnus-treat-buttonize gnus-article-add-buttons)
1680     (gnus-treat-fill-article gnus-article-fill-cited-article)
1681     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1682     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1683     (gnus-treat-display-x-face gnus-article-display-x-face)
1684     (gnus-treat-display-face gnus-article-display-face)
1685     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1686     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1687     (gnus-treat-hide-signature gnus-article-hide-signature)
1688     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1689     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1690     (gnus-treat-from-picon gnus-treat-from-picon)
1691     (gnus-treat-mail-picon gnus-treat-mail-picon)
1692     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1693     (gnus-treat-strip-pem gnus-article-hide-pem)
1694     (gnus-treat-date gnus-article-treat-date)
1695     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1696     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1697     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1698     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1699     (gnus-treat-strip-trailing-blank-lines
1700      gnus-article-remove-trailing-blank-lines)
1701     (gnus-treat-strip-leading-blank-lines
1702      gnus-article-strip-leading-blank-lines)
1703     (gnus-treat-strip-multiple-blank-lines
1704      gnus-article-strip-multiple-blank-lines)
1705     (gnus-treat-overstrike gnus-article-treat-overstrike)
1706     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1707     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1708     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1709     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1710     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1711     (gnus-treat-display-smileys gnus-treat-smiley)
1712     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1713     (gnus-treat-wash-html gnus-article-wash-html)
1714     (gnus-treat-emphasize gnus-article-emphasize)
1715     (gnus-treat-hide-citation gnus-article-hide-citation)
1716     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1717     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1718     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1719
1720 (defvar gnus-article-mime-handle-alist nil)
1721 (defvar article-lapsed-timer nil)
1722 (defvar gnus-article-current-summary nil)
1723
1724 (defvar gnus-article-mode-syntax-table
1725   (let ((table (copy-syntax-table text-mode-syntax-table)))
1726     ;; This causes the citation match run O(2^n).
1727     ;; (modify-syntax-entry ?- "w" table)
1728     (modify-syntax-entry ?> ")<" table)
1729     (modify-syntax-entry ?< "(>" table)
1730     ;; make M-. in article buffers work for `foo' strings
1731     (modify-syntax-entry ?' " " table)
1732     (modify-syntax-entry ?` " " table)
1733     table)
1734   "Syntax table used in article mode buffers.
1735 Initialized from `text-mode-syntax-table.")
1736
1737 (defvar gnus-save-article-buffer nil)
1738
1739 (defvar gnus-number-of-articles-to-be-saved nil)
1740
1741 (defvar gnus-inhibit-hiding nil)
1742
1743 (defvar gnus-article-edit-mode nil)
1744
1745 ;;; Macros for dealing with the article buffer.
1746
1747 (defmacro gnus-with-article-headers (&rest forms)
1748   `(with-current-buffer gnus-article-buffer
1749      (save-restriction
1750        (let ((inhibit-read-only t)
1751              (inhibit-point-motion-hooks t)
1752              (case-fold-search t))
1753          (article-narrow-to-head)
1754          ,@forms))))
1755
1756 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1757 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1758
1759 (defmacro gnus-with-article-buffer (&rest forms)
1760   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1761      (with-current-buffer gnus-article-buffer
1762        (let ((inhibit-read-only t))
1763          ,@forms))))
1764
1765 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1766 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1767
1768 (defun gnus-article-goto-header (header)
1769   "Go to HEADER, which is a regular expression."
1770   (re-search-forward (concat "^\\(" header "\\):") nil t))
1771
1772 (defsubst gnus-article-hide-text (b e props)
1773   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1774   (gnus-add-text-properties-when 'article-type nil b e props)
1775   (when (memq 'intangible props)
1776     (put-text-property
1777      (max (1- b) (point-min))
1778      b 'intangible (cddr (memq 'intangible props)))))
1779
1780 (defsubst gnus-article-unhide-text (b e)
1781   "Remove hidden text properties from region between B and E."
1782   (remove-text-properties b e gnus-hidden-properties)
1783   (when (memq 'intangible gnus-hidden-properties)
1784     (put-text-property (max (1- b) (point-min))
1785                        b 'intangible nil)))
1786
1787 (defun gnus-article-hide-text-type (b e type)
1788   "Hide text of TYPE between B and E."
1789   (gnus-add-wash-type type)
1790   (gnus-article-hide-text
1791    b e (cons 'article-type (cons type gnus-hidden-properties))))
1792
1793 (defun gnus-article-unhide-text-type (b e type)
1794   "Unhide text of TYPE between B and E."
1795   (gnus-delete-wash-type type)
1796   (remove-text-properties
1797    b e (cons 'article-type (cons type gnus-hidden-properties)))
1798   (when (memq 'intangible gnus-hidden-properties)
1799     (put-text-property (max (1- b) (point-min))
1800                        b 'intangible nil)))
1801
1802 (defun gnus-article-delete-text-of-type (type)
1803   "Delete text of TYPE in the current buffer."
1804   (save-excursion
1805     (let ((b (point-min)))
1806       (if (eq type 'multipart)
1807           ;; Remove MIME buttons associated with multipart/alternative parts.
1808           (progn
1809             (goto-char b)
1810             (while (if (get-text-property (point) 'gnus-part)
1811                        (setq b (point))
1812                      (when (setq b (next-single-property-change (point)
1813                                                                 'gnus-part))
1814                        (goto-char b)
1815                        t))
1816               (end-of-line)
1817               (skip-chars-forward "\n")
1818               (when (eq (get-text-property b 'article-type) 'multipart)
1819                 (delete-region b (point)))))
1820         (while (setq b (text-property-any b (point-max) 'article-type type))
1821           (delete-region
1822            b (or (text-property-not-all b (point-max) 'article-type type)
1823                  (point-max))))))))
1824
1825 (defun gnus-article-delete-invisible-text ()
1826   "Delete all invisible text in the current buffer."
1827   (save-excursion
1828     (let ((b (point-min)))
1829       (while (setq b (text-property-any b (point-max) 'invisible t))
1830         (delete-region
1831          b (or (text-property-not-all b (point-max) 'invisible t)
1832                (point-max)))))))
1833
1834 (defsubst gnus-article-header-rank ()
1835   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1836   (let ((list gnus-sorted-header-list)
1837         (i 1))
1838     (while list
1839       (if (looking-at (car list))
1840           (setq list nil)
1841         (setq list (cdr list))
1842         (incf i)))
1843       i))
1844
1845 (defun article-hide-headers (&optional arg delete)
1846   "Hide unwanted headers and possibly sort them as well."
1847   (interactive)
1848   ;; This function might be inhibited.
1849   (unless gnus-inhibit-hiding
1850     (let ((inhibit-read-only t)
1851           (case-fold-search t)
1852           (max (1+ (length gnus-sorted-header-list)))
1853           (inhibit-point-motion-hooks t)
1854           (cur (current-buffer))
1855           ignored visible beg)
1856       (save-excursion
1857         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1858         ;; group parameters, so we should go to the summary buffer.
1859         (when (prog1
1860                   (condition-case nil
1861                       (progn (set-buffer gnus-summary-buffer) t)
1862                     (error nil))
1863                 (setq ignored (when (not gnus-visible-headers)
1864                                 (cond ((stringp gnus-ignored-headers)
1865                                        gnus-ignored-headers)
1866                                       ((listp gnus-ignored-headers)
1867                                        (mapconcat 'identity
1868                                                   gnus-ignored-headers
1869                                                   "\\|"))))
1870                       visible (cond ((stringp gnus-visible-headers)
1871                                      gnus-visible-headers)
1872                                     ((and gnus-visible-headers
1873                                           (listp gnus-visible-headers))
1874                                      (mapconcat 'identity
1875                                                 gnus-visible-headers
1876                                                 "\\|")))))
1877           (set-buffer cur))
1878         (save-restriction
1879           ;; First we narrow to just the headers.
1880           (article-narrow-to-head)
1881           ;; Hide any "From " lines at the beginning of (mail) articles.
1882           (while (looking-at "From ")
1883             (forward-line 1))
1884           (unless (bobp)
1885             (delete-region (point-min) (point)))
1886           ;; Then treat the rest of the header lines.
1887           ;; Then we use the two regular expressions
1888           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1889           ;; select which header lines is to remain visible in the
1890           ;; article buffer.
1891           (while (re-search-forward "^[^ \t:]*:" nil t)
1892             (beginning-of-line)
1893             ;; Mark the rank of the header.
1894             (put-text-property
1895              (point) (1+ (point)) 'message-rank
1896              (if (or (and visible (looking-at visible))
1897                      (and ignored
1898                           (not (looking-at ignored))))
1899                  (gnus-article-header-rank)
1900                (+ 2 max)))
1901             (forward-line 1))
1902           (message-sort-headers-1)
1903           (when (setq beg (text-property-any
1904                            (point-min) (point-max) 'message-rank (+ 2 max)))
1905             ;; We delete the unwanted headers.
1906             (gnus-add-wash-type 'headers)
1907             (add-text-properties (point-min) (+ 5 (point-min))
1908                                  '(article-type headers dummy-invisible t))
1909             (delete-region beg (point-max))))))))
1910
1911 (defun article-hide-boring-headers (&optional arg)
1912   "Toggle hiding of headers that aren't very interesting.
1913 If given a negative prefix, always show; if given a positive prefix,
1914 always hide."
1915   (interactive (gnus-article-hidden-arg))
1916   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1917              (not gnus-show-all-headers))
1918     (save-excursion
1919       (save-restriction
1920         (let ((inhibit-read-only t)
1921               (inhibit-point-motion-hooks t))
1922           (article-narrow-to-head)
1923           (dolist (elem gnus-boring-article-headers)
1924             (goto-char (point-min))
1925             (cond
1926              ;; Hide empty headers.
1927              ((eq elem 'empty)
1928               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1929                 (forward-line -1)
1930                 (gnus-article-hide-text-type
1931                  (point-at-bol)
1932                  (progn
1933                    (end-of-line)
1934                    (if (re-search-forward "^[^ \t]" nil t)
1935                        (match-beginning 0)
1936                      (point-max)))
1937                  'boring-headers)))
1938              ;; Hide boring Newsgroups header.
1939              ((eq elem 'newsgroups)
1940               (when (gnus-string-equal
1941                      (gnus-fetch-field "newsgroups")
1942                      (gnus-group-real-name
1943                       (if (boundp 'gnus-newsgroup-name)
1944                           gnus-newsgroup-name
1945                         "")))
1946                 (gnus-article-hide-header "newsgroups")))
1947              ((eq elem 'to-address)
1948               (let ((to (message-fetch-field "to"))
1949                     (to-address
1950                      (gnus-parameter-to-address
1951                       (if (boundp 'gnus-newsgroup-name)
1952                           gnus-newsgroup-name ""))))
1953                 (when (and to to-address
1954                            (ignore-errors
1955                              (gnus-string-equal
1956                               ;; only one address in To
1957                               (nth 1 (mail-extract-address-components to))
1958                               to-address)))
1959                   (gnus-article-hide-header "to"))))
1960              ((eq elem 'to-list)
1961               (let ((to (message-fetch-field "to"))
1962                     (to-list
1963                      (gnus-parameter-to-list
1964                       (if (boundp 'gnus-newsgroup-name)
1965                           gnus-newsgroup-name ""))))
1966                 (when (and to to-list
1967                            (ignore-errors
1968                              (gnus-string-equal
1969                               ;; only one address in To
1970                               (nth 1 (mail-extract-address-components to))
1971                               to-list)))
1972                   (gnus-article-hide-header "to"))))
1973              ((eq elem 'cc-list)
1974               (let ((cc (message-fetch-field "cc"))
1975                     (to-list
1976                      (gnus-parameter-to-list
1977                       (if (boundp 'gnus-newsgroup-name)
1978                           gnus-newsgroup-name ""))))
1979                 (when (and cc to-list
1980                            (ignore-errors
1981                              (gnus-string-equal
1982                               ;; only one address in CC
1983                               (nth 1 (mail-extract-address-components cc))
1984                               to-list)))
1985                   (gnus-article-hide-header "cc"))))
1986              ((eq elem 'followup-to)
1987               (when (gnus-string-equal
1988                      (message-fetch-field "followup-to")
1989                      (message-fetch-field "newsgroups"))
1990                 (gnus-article-hide-header "followup-to")))
1991              ((eq elem 'reply-to)
1992               (if (gnus-group-find-parameter
1993                    gnus-newsgroup-name 'broken-reply-to)
1994                   (gnus-article-hide-header "reply-to")
1995                 (let ((from (message-fetch-field "from"))
1996                       (reply-to (message-fetch-field "reply-to")))
1997                   (when
1998                       (and
1999                        from reply-to
2000                        (ignore-errors
2001                          (equal
2002                           (sort (mapcar
2003                                  (lambda (x) (downcase (cadr x)))
2004                                  (mail-extract-address-components from t))
2005                                 'string<)
2006                           (sort (mapcar
2007                                  (lambda (x) (downcase (cadr x)))
2008                                  (mail-extract-address-components reply-to t))
2009                                 'string<))))
2010                     (gnus-article-hide-header "reply-to")))))
2011              ((eq elem 'date)
2012               (let ((date (with-current-buffer gnus-original-article-buffer
2013                             ;; If date in `gnus-article-buffer' is localized
2014                             ;; (`gnus-treat-date-user-defined'),
2015                             ;; `days-between' might fail.
2016                             (message-fetch-field "date"))))
2017                 (when (and date
2018                            (< (days-between (current-time-string) date)
2019                               4))
2020                   (gnus-article-hide-header "date"))))
2021              ((eq elem 'long-to)
2022               (let ((to (message-fetch-field "to"))
2023                     (cc (message-fetch-field "cc")))
2024                 (when (> (length to) 1024)
2025                   (gnus-article-hide-header "to"))
2026                 (when (> (length cc) 1024)
2027                   (gnus-article-hide-header "cc"))))
2028              ((eq elem 'many-to)
2029               (let ((to-count 0)
2030                     (cc-count 0))
2031                 (goto-char (point-min))
2032                 (while (re-search-forward "^to:" nil t)
2033                   (setq to-count (1+ to-count)))
2034                 (when (> to-count 1)
2035                   (while (> to-count 0)
2036                     (goto-char (point-min))
2037                     (save-restriction
2038                       (re-search-forward "^to:" nil nil to-count)
2039                       (forward-line -1)
2040                       (narrow-to-region (point) (point-max))
2041                       (gnus-article-hide-header "to"))
2042                     (setq to-count (1- to-count))))
2043                 (goto-char (point-min))
2044                 (while (re-search-forward "^cc:" nil t)
2045                   (setq cc-count (1+ cc-count)))
2046                 (when (> cc-count 1)
2047                   (while (> cc-count 0)
2048                     (goto-char (point-min))
2049                     (save-restriction
2050                       (re-search-forward "^cc:" nil nil cc-count)
2051                       (forward-line -1)
2052                       (narrow-to-region (point) (point-max))
2053                       (gnus-article-hide-header "cc"))
2054                     (setq cc-count (1- cc-count)))))))))))))
2055
2056 (defun gnus-article-hide-header (header)
2057   (save-excursion
2058     (goto-char (point-min))
2059     (when (re-search-forward (concat "^" header ":") nil t)
2060       (gnus-article-hide-text-type
2061        (point-at-bol)
2062        (progn
2063          (end-of-line)
2064          (if (re-search-forward "^[^ \t]" nil t)
2065              (match-beginning 0)
2066            (point-max)))
2067        'boring-headers))))
2068
2069 (defvar gnus-article-normalized-header-length 40
2070   "Length of normalized headers.")
2071
2072 (defun article-normalize-headers ()
2073   "Make all header lines 40 characters long."
2074   (interactive)
2075   (let ((inhibit-read-only t)
2076         column)
2077     (save-excursion
2078       (save-restriction
2079         (article-narrow-to-head)
2080         (while (not (eobp))
2081           (cond
2082            ((< (setq column (- (point-at-eol) (point)))
2083                gnus-article-normalized-header-length)
2084             (end-of-line)
2085             (insert (make-string
2086                      (- gnus-article-normalized-header-length column)
2087                      ? )))
2088            ((> column gnus-article-normalized-header-length)
2089             (gnus-put-text-property
2090              (progn
2091                (forward-char gnus-article-normalized-header-length)
2092                (point))
2093              (point-at-eol)
2094              'invisible t))
2095            (t
2096             ;; Do nothing.
2097             ))
2098           (forward-line 1))))))
2099
2100 (defun article-treat-dumbquotes ()
2101   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2102 Note that this function guesses whether a character is a sm*rtq**t* or
2103 not, so it should only be used interactively.
2104
2105 Sm*rtq**t*s are M****s***'s unilateral extension to the
2106 iso-8859-1 character map in an attempt to provide more quoting
2107 characters.  If you see something like \\222 or \\264 where
2108 you're expecting some kind of apostrophe or quotation mark, then
2109 try this wash."
2110   (interactive)
2111   (article-translate-strings gnus-article-dumbquotes-map))
2112
2113 (defvar org-entities)
2114
2115 (defun article-treat-non-ascii ()
2116   "Translate many Unicode characters into their ASCII equivalents."
2117   (interactive)
2118   (require 'org-entities)
2119   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2120     (dolist (elem org-entities)
2121       (when (and (listp elem)
2122                  (= (length (nth 6 elem)) 1))
2123         (if (featurep 'xemacs)
2124             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2125           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2126     (save-excursion
2127       (when (article-goto-body)
2128         (let ((inhibit-read-only t)
2129               replace props)
2130           (while (not (eobp))
2131             (if (not (setq replace (if (featurep 'xemacs)
2132                                        (get-char-table (following-char) table)
2133                                      (aref table (following-char)))))
2134                 (forward-char 1)
2135               (if (prog1
2136                       (setq props (text-properties-at (point)))
2137                     (delete-char 1))
2138                   (add-text-properties (point) (progn (insert replace) (point))
2139                                        props)
2140                 (insert replace)))))))))
2141
2142 (defun article-translate-strings (map)
2143   "Translate all string in the body of the article according to MAP.
2144 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2145   (save-excursion
2146     (when (article-goto-body)
2147       (let ((inhibit-read-only t))
2148         (dolist (elem map)
2149           (let ((from (car elem))
2150                 (to (cadr elem)))
2151             (save-excursion
2152               (if (stringp from)
2153                   (while (search-forward from nil t)
2154                     (replace-match to))
2155                 (while (not (eobp))
2156                   (if (eq (following-char) from)
2157                       (progn
2158                         (delete-char 1)
2159                         (insert to))
2160                     (forward-char 1)))))))))))
2161
2162 (defun article-treat-overstrike ()
2163   "Translate overstrikes into bold text."
2164   (interactive)
2165   (save-excursion
2166     (when (article-goto-body)
2167       (let ((inhibit-read-only t))
2168         (while (search-forward "\b" nil t)
2169           (let ((next (char-after))
2170                 (previous (char-after (- (point) 2))))
2171             ;; We do the boldification/underlining by hiding the
2172             ;; overstrikes and putting the proper text property
2173             ;; on the letters.
2174             (cond
2175              ((eq next previous)
2176               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2177               (put-text-property (point) (1+ (point)) 'face 'bold))
2178              ((eq next ?_)
2179               (gnus-article-hide-text-type
2180                (1- (point)) (1+ (point)) 'overstrike)
2181               (put-text-property
2182                (- (point) 2) (1- (point)) 'face 'underline))
2183              ((eq previous ?_)
2184               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2185               (put-text-property
2186                (point) (1+ (point)) 'face 'underline)))))))))
2187
2188 (defun article-treat-ansi-sequences ()
2189   "Translate ANSI SGR control sequences into overlays or extents."
2190   (interactive)
2191   (save-excursion
2192     (when (article-goto-body)
2193       (let ((inhibit-read-only t))
2194         (ansi-color-apply-on-region (point) (point-max))))))
2195
2196 (defun gnus-article-treat-unfold-headers ()
2197   "Unfold folded message headers.
2198 Only the headers that fit into the current window width will be
2199 unfolded."
2200   (interactive)
2201   (gnus-with-article-headers
2202     (let (length)
2203       (while (not (eobp))
2204         (save-restriction
2205           (mail-header-narrow-to-field)
2206           (let* ((header (buffer-string))
2207                  (unfoldable
2208                   (or (equal gnus-article-unfold-long-headers t)
2209                       (and (stringp gnus-article-unfold-long-headers)
2210                            (string-match gnus-article-unfold-long-headers
2211                                          header)))))
2212             (with-temp-buffer
2213               (insert header)
2214               (goto-char (point-min))
2215               (while (re-search-forward "\n[\t ]" nil t)
2216                 (replace-match " " t t)))
2217             (setq length (- (point-max) (point-min) 1))
2218             (when (or unfoldable
2219                       (< length (window-width)))
2220               (while (re-search-forward "\n[\t ]" nil t)
2221                 (replace-match " " t t))))
2222           (goto-char (point-max)))))))
2223
2224 (defun gnus-article-treat-fold-headers ()
2225   "Fold message headers."
2226   (interactive)
2227   (gnus-with-article-headers
2228     (while (not (eobp))
2229       (save-restriction
2230         (mail-header-narrow-to-field)
2231         (mail-header-fold-field)
2232         (goto-char (point-max))))))
2233
2234 (defun gnus-treat-smiley ()
2235   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2236   (interactive)
2237   (gnus-with-article-buffer
2238     (if (memq 'smiley gnus-article-wash-types)
2239         (gnus-delete-images 'smiley)
2240       (article-goto-body)
2241       (let ((images (smiley-region (point) (point-max))))
2242         (when images
2243           (gnus-add-wash-type 'smiley)
2244           (dolist (image images)
2245             (gnus-add-image 'smiley image)))))))
2246
2247 (defun gnus-article-remove-images ()
2248   "Remove all images from the article buffer."
2249   (interactive)
2250   (gnus-with-article-buffer
2251     (save-restriction
2252       (widen)
2253       (dolist (elem gnus-article-image-alist)
2254         (gnus-delete-images (car elem))))))
2255
2256 (autoload 'w3m-toggle-inline-images "w3m")
2257
2258 (defun gnus-article-show-images ()
2259   "Show any images that are in the HTML-rendered article buffer.
2260 This only works if the article in question is HTML."
2261   (interactive)
2262   (gnus-with-article-buffer
2263     (save-restriction
2264       (widen)
2265       (if (eq mm-text-html-renderer 'w3m)
2266           (let ((mm-inline-text-html-with-images nil))
2267             (w3m-toggle-inline-images))
2268         (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2269                                                         'image-displayer))
2270           (destructuring-bind (start end function) region
2271             (funcall function (get-text-property start 'image-url)
2272                      start end)))))))
2273
2274 (defun gnus-article-treat-fold-newsgroups ()
2275   "Unfold folded message headers.
2276 Only the headers that fit into the current window width will be
2277 unfolded."
2278   (interactive)
2279   (gnus-with-article-headers
2280     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2281       (save-restriction
2282         (mail-header-narrow-to-field)
2283         (while (re-search-forward ", *" nil t)
2284           (replace-match ", " t t))
2285         (mail-header-fold-field)
2286         (goto-char (point-max))))))
2287
2288 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2289   "Value of `truncate-lines' in Gnus Article buffer.
2290 Valid values are nil, t, `head', `first', `last', an integer or a
2291 predicate.  See Info node `(gnus)Customizing Articles'."
2292   :version "23.1" ;; No Gnus
2293   :group 'gnus-article
2294   ;; :link '(custom-manual "(gnus)Customizing Articles")
2295   :type 'boolean)
2296
2297 (defun gnus-article-toggle-truncate-lines (&optional arg)
2298   "Toggle whether to fold or truncate long lines in article the buffer.
2299 If ARG is non-nil and not a number, toggle
2300 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2301 long lines if and only if arg is positive."
2302   (interactive "P")
2303   (cond
2304    ((and (numberp arg) (> arg 0))
2305     (setq gnus-article-truncate-lines t))
2306    ((numberp arg)
2307     (setq gnus-article-truncate-lines nil))
2308    (arg
2309     (setq gnus-article-truncate-lines
2310           (not gnus-article-truncate-lines))))
2311   (gnus-with-article-buffer
2312     (cond
2313      ((and (numberp arg) (> arg 0))
2314       (setq truncate-lines nil))
2315      ((numberp arg)
2316       (setq truncate-lines t)))
2317     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2318     ;; `toggle-truncate-lines' needs an argument.
2319     (toggle-truncate-lines)))
2320
2321 (defun gnus-article-treat-body-boundary ()
2322   "Place a boundary line at the end of the headers."
2323   (interactive)
2324   (when (and gnus-body-boundary-delimiter
2325              (> (length gnus-body-boundary-delimiter) 0))
2326     (gnus-with-article-headers
2327       (goto-char (point-max))
2328       (let ((start (point)))
2329         (insert "X-Boundary: ")
2330         (gnus-add-text-properties start (point) '(invisible t intangible t))
2331        (insert (let (str (max (window-width)))
2332                  (if (featurep 'xemacs)
2333                      (setq max (1- max)))
2334                  (while (>= max (length str))
2335                     (setq str (concat str gnus-body-boundary-delimiter)))
2336                  (substring str 0 max))
2337                 "\n")
2338         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2339
2340 (defun article-fill-long-lines ()
2341   "Fill lines that are wider than the window width."
2342   (interactive)
2343   (save-excursion
2344     (let ((inhibit-read-only t)
2345           (width (window-width (get-buffer-window (current-buffer)))))
2346       (save-restriction
2347         (article-goto-body)
2348         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2349           (while (not (eobp))
2350             (end-of-line)
2351             (when (>= (current-column) (min fill-column width))
2352               (narrow-to-region (min (1+ (point)) (point-max))
2353                                 (point-at-bol))
2354               (let ((goback (point-marker)))
2355                 (fill-paragraph nil)
2356                 (goto-char (marker-position goback)))
2357               (widen))
2358             (forward-line 1)))))))
2359
2360 (defun article-capitalize-sentences ()
2361   "Capitalize the first word in each sentence."
2362   (interactive)
2363   (save-excursion
2364     (let ((inhibit-read-only t)
2365           (paragraph-start "^[\n\^L]"))
2366       (article-goto-body)
2367       (while (not (eobp))
2368         (capitalize-word 1)
2369         (forward-sentence)))))
2370
2371 (defun article-remove-cr ()
2372   "Remove trailing CRs and then translate remaining CRs into LFs."
2373   (interactive)
2374   (save-excursion
2375     (let ((inhibit-read-only t))
2376       (goto-char (point-min))
2377       (while (re-search-forward "\r+$" nil t)
2378         (replace-match "" t t))
2379       (goto-char (point-min))
2380       (while (search-forward "\r" nil t)
2381         (replace-match "\n" t t)))))
2382
2383 (defun article-remove-trailing-blank-lines ()
2384   "Remove all trailing blank lines from the article."
2385   (interactive)
2386   (save-excursion
2387     (let ((inhibit-read-only t))
2388       (goto-char (point-max))
2389       (delete-region
2390        (point)
2391        (progn
2392          (while (and (not (bobp))
2393                      (looking-at "^[ \t]*$")
2394                      (not (gnus-annotation-in-region-p
2395                            (point) (point-at-eol))))
2396            (forward-line -1))
2397          (forward-line 1)
2398          (point))))))
2399
2400 (defvar gnus-face-properties-alist)
2401
2402 (defun article-display-face (&optional force)
2403   "Display any Face headers in the header."
2404   (interactive (list 'force))
2405   (let ((wash-face-p buffer-read-only))
2406     (gnus-with-article-headers
2407       ;; When displaying parts, this function can be called several times on
2408       ;; the same article, without any intended toggle semantic (as typing `W
2409       ;; D d' would have). So face deletion must occur only when we come from
2410       ;; an interactive command, that is when the *Article* buffer is
2411       ;; read-only.
2412       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2413           (gnus-delete-images 'face)
2414         (let ((from (message-fetch-field "from"))
2415               face faces)
2416           (save-current-buffer
2417             (when (and wash-face-p
2418                        (gnus-buffer-live-p gnus-original-article-buffer)
2419                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2420               (set-buffer gnus-original-article-buffer))
2421             (save-restriction
2422               (mail-narrow-to-head)
2423               (when (or force
2424                         ;; Check whether this face is censored.
2425                         (not (and gnus-article-x-face-too-ugly
2426                                   (or from
2427                                       (setq from (message-fetch-field "from")))
2428                                   (string-match gnus-article-x-face-too-ugly
2429                                                 from))))
2430                 (while (gnus-article-goto-header "Face")
2431                   (push (mail-header-field-value) faces)))))
2432           (when faces
2433             (goto-char (point-min))
2434             (let (png image)
2435               (unless (setq from (gnus-article-goto-header "from"))
2436                 (insert "From:")
2437                 (setq from (point))
2438                 (insert " [no `from' set]\n"))
2439               (while faces
2440                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2441                   (setq image
2442                         (apply 'gnus-create-image png 'png t
2443                                (cdr (assq 'png gnus-face-properties-alist))))
2444                   (goto-char from)
2445                   (when image
2446                     (gnus-add-wash-type 'face)
2447                     (gnus-add-image 'face image)
2448                     (gnus-put-image image nil 'face)))))))))))
2449
2450 (defun article-display-x-face (&optional force)
2451   "Look for an X-Face header and display it if present."
2452   (interactive (list 'force))
2453   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2454     (gnus-with-article-headers
2455       ;; Delete the old process, if any.
2456       (when (process-status "article-x-face")
2457         (delete-process "article-x-face"))
2458       ;; See the comment in `article-display-face'.
2459       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2460           ;; We have already displayed X-Faces, so we remove them
2461           ;; instead.
2462           (gnus-delete-images 'xface)
2463         ;; Display X-Faces.
2464         (let ((from (message-fetch-field "from"))
2465               x-faces face)
2466           (save-current-buffer
2467             (when (and wash-face-p
2468                        (gnus-buffer-live-p gnus-original-article-buffer)
2469                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2470               ;; If type `W f', use gnus-original-article-buffer,
2471               ;; otherwise use the current buffer because displaying
2472               ;; RFC822 parts calls this function too.
2473               (set-buffer gnus-original-article-buffer))
2474             (save-restriction
2475               (mail-narrow-to-head)
2476               (and gnus-article-x-face-command
2477                    (or force
2478                        ;; Check whether this face is censored.
2479                        (not (and gnus-article-x-face-too-ugly
2480                                  (or from
2481                                      (setq from (message-fetch-field "from")))
2482                                  (string-match gnus-article-x-face-too-ugly
2483                                                from))))
2484                    (while (gnus-article-goto-header "X-Face")
2485                      (push (mail-header-field-value) x-faces)))))
2486           (when x-faces
2487             ;; We display the face.
2488             (cond ((functionp gnus-article-x-face-command)
2489                    ;; The command is a lisp function, so we call it.
2490                    (mapc gnus-article-x-face-command x-faces))
2491                   ((stringp gnus-article-x-face-command)
2492                    ;; The command is a string, so we interpret the command
2493                    ;; as a, well, command, and fork it off.
2494                    (let ((process-connection-type nil))
2495                      (gnus-set-process-query-on-exit-flag
2496                       (start-process
2497                        "article-x-face" nil shell-file-name
2498                        shell-command-switch gnus-article-x-face-command)
2499                       nil)
2500                      ;; Sending multiple EOFs to xv doesn't work,
2501                      ;; so we only do a single external face.
2502                      (with-temp-buffer
2503                        (insert (car x-faces))
2504                        (process-send-region "article-x-face"
2505                                             (point-min) (point-max)))
2506                      (process-send-eof "article-x-face")))
2507                   (t
2508                    (error "`%s' set to `%s' is not a function"
2509                           gnus-article-x-face-command
2510                           'gnus-article-x-face-command)))))))))
2511
2512 (defun article-decode-mime-words ()
2513   "Decode all MIME-encoded words in the article."
2514   (interactive)
2515   (gnus-with-article-buffer
2516     (let ((inhibit-point-motion-hooks t)
2517           (mail-parse-charset gnus-newsgroup-charset)
2518           (mail-parse-ignored-charsets
2519            (with-current-buffer gnus-summary-buffer
2520              gnus-newsgroup-ignored-charsets)))
2521       (mail-decode-encoded-word-region (point-min) (point-max)))))
2522
2523 (defun article-decode-charset (&optional prompt)
2524   "Decode charset-encoded text in the article.
2525 If PROMPT (the prefix), prompt for a coding system to use."
2526   (interactive "P")
2527   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2528         (inhibit-read-only t)
2529         (mail-parse-charset gnus-newsgroup-charset)
2530         (mail-parse-ignored-charsets
2531          (save-excursion (condition-case nil
2532                              (set-buffer gnus-summary-buffer)
2533                            (error))
2534                          gnus-newsgroup-ignored-charsets))
2535         ct cte ctl charset format)
2536     (save-excursion
2537       (save-restriction
2538         (article-narrow-to-head)
2539         (setq ct (message-fetch-field "Content-Type" t)
2540               cte (message-fetch-field "Content-Transfer-Encoding" t)
2541               ctl (and ct (mail-header-parse-content-type ct))
2542               charset (cond
2543                        (prompt
2544                         (mm-read-coding-system "Charset to decode: "))
2545                        (ctl
2546                         (mail-content-type-get ctl 'charset)))
2547               format (and ctl (mail-content-type-get ctl 'format)))
2548         (when cte
2549           (setq cte (mail-header-strip cte)))
2550         (if (and ctl (not (string-match "/" (car ctl))))
2551             (setq ctl nil))
2552         (goto-char (point-max)))
2553       (forward-line 1)
2554       (save-restriction
2555         (narrow-to-region (point) (point-max))
2556         (when (and (eq mail-parse-charset 'gnus-decoded)
2557                    (eq (mm-body-7-or-8) '8bit))
2558           ;; The text code could have been decoded.
2559           (setq charset mail-parse-charset))
2560         (when (and (or (not ctl)
2561                        (equal (car ctl) "text/plain"))
2562                    (not format)) ;; article with format will decode later.
2563           (mm-decode-body
2564            charset (and cte (intern (downcase
2565                                      (gnus-strip-whitespace cte))))
2566            (car ctl)))))))
2567
2568 (defun article-decode-encoded-words ()
2569   "Remove encoded-word encoding from headers."
2570   (let ((inhibit-point-motion-hooks t)
2571         (mail-parse-charset gnus-newsgroup-charset)
2572         (mail-parse-ignored-charsets
2573          (save-excursion (condition-case nil
2574                              (set-buffer gnus-summary-buffer)
2575                            (error))
2576                          gnus-newsgroup-ignored-charsets))
2577         (inhibit-read-only t)
2578         end start)
2579     (goto-char (point-min))
2580     (when (search-forward "\n\n" nil 'move)
2581       (forward-line -1))
2582     (setq end (point))
2583     (while (not (bobp))
2584       (while (progn
2585                (forward-line -1)
2586                (and (not (bobp))
2587                     (memq (char-after) '(?\t ? )))))
2588       (setq start (point))
2589       (if (looking-at "\
2590 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2591 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2592           (funcall gnus-decode-address-function start end)
2593         (funcall gnus-decode-header-function start end))
2594       (goto-char (setq end start)))))
2595
2596 (defun article-decode-group-name ()
2597   "Decode group names in Newsgroups, Followup-To and Xref headers."
2598   (let ((inhibit-point-motion-hooks t)
2599         (inhibit-read-only t)
2600         (method (gnus-find-method-for-group gnus-newsgroup-name))
2601         regexp)
2602     (when (and (or gnus-group-name-charset-method-alist
2603                    gnus-group-name-charset-group-alist)
2604                (gnus-buffer-live-p gnus-original-article-buffer))
2605       (save-restriction
2606         (article-narrow-to-head)
2607         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2608           (with-current-buffer gnus-original-article-buffer
2609             (goto-char (point-min)))
2610           (setq regexp (concat "^" header
2611                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2612           (while (re-search-forward regexp nil t)
2613             (replace-match (save-match-data
2614                              (gnus-decode-newsgroups
2615                               ;; XXX how to use data in article buffer?
2616                               (with-current-buffer gnus-original-article-buffer
2617                                 (re-search-forward regexp nil t)
2618                                 (match-string 1))
2619                               gnus-newsgroup-name method))
2620                            t t nil 1))
2621           (goto-char (point-min)))))))
2622
2623 (autoload 'idna-to-unicode "idna")
2624
2625 (defun article-decode-idna-rhs ()
2626   "Decode IDNA strings in RHS in various headers in current buffer.
2627 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2628 Mail-Reply-To: and Mail-Followup-To:."
2629   (when gnus-use-idna
2630     (save-restriction
2631       (let ((inhibit-point-motion-hooks t)
2632             (inhibit-read-only t))
2633         (article-narrow-to-head)
2634         (goto-char (point-min))
2635         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2636           (let (ace unicode)
2637             (when (save-match-data
2638                     (and (setq ace (match-string 1))
2639                          (save-excursion
2640                            (and (re-search-backward "^[^ \t]" nil t)
2641                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2642                          (setq unicode (idna-to-unicode ace))))
2643               (unless (string= ace unicode)
2644                 (replace-match unicode nil nil nil 1)))))))))
2645
2646 (defun article-de-quoted-unreadable (&optional force read-charset)
2647   "Translate a quoted-printable-encoded article.
2648 If FORCE, decode the article whether it is marked as quoted-printable
2649 or not.
2650 If READ-CHARSET, ask for a coding system."
2651   (interactive (list 'force current-prefix-arg))
2652   (save-excursion
2653     (let ((inhibit-read-only t) type charset)
2654       (if (gnus-buffer-live-p gnus-original-article-buffer)
2655           (with-current-buffer gnus-original-article-buffer
2656             (setq type
2657                   (gnus-fetch-field "content-transfer-encoding"))
2658             (let* ((ct (gnus-fetch-field "content-type"))
2659                    (ctl (and ct (mail-header-parse-content-type ct))))
2660               (setq charset (and ctl
2661                                  (mail-content-type-get ctl 'charset)))
2662               (if (stringp charset)
2663                   (setq charset (intern (downcase charset)))))))
2664       (if read-charset
2665           (setq charset (mm-read-coding-system "Charset: " charset)))
2666       (unless charset
2667         (setq charset gnus-newsgroup-charset))
2668       (when (or force
2669                 (and type (let ((case-fold-search t))
2670                             (string-match "quoted-printable" type))))
2671         (article-goto-body)
2672         (quoted-printable-decode-region
2673          (point) (point-max) (mm-charset-to-coding-system charset nil t))))))
2674
2675 (defun article-de-base64-unreadable (&optional force read-charset)
2676   "Translate a base64 article.
2677 If FORCE, decode the article whether it is marked as base64 not.
2678 If READ-CHARSET, ask for a coding system."
2679   (interactive (list 'force current-prefix-arg))
2680   (save-excursion
2681     (let ((inhibit-read-only t) type charset)
2682       (if (gnus-buffer-live-p gnus-original-article-buffer)
2683           (with-current-buffer gnus-original-article-buffer
2684             (setq type
2685                   (gnus-fetch-field "content-transfer-encoding"))
2686             (let* ((ct (gnus-fetch-field "content-type"))
2687                    (ctl (and ct (mail-header-parse-content-type ct))))
2688               (setq charset (and ctl
2689                                  (mail-content-type-get ctl 'charset)))
2690               (if (stringp charset)
2691                   (setq charset (intern (downcase charset)))))))
2692       (if read-charset
2693           (setq charset (mm-read-coding-system "Charset: " charset)))
2694       (unless charset
2695         (setq charset gnus-newsgroup-charset))
2696       (when (or force
2697                 (and type (let ((case-fold-search t))
2698                             (string-match "base64" type))))
2699         (article-goto-body)
2700         (save-restriction
2701           (narrow-to-region (point) (point-max))
2702           (base64-decode-region (point-min) (point-max))
2703           (mm-decode-coding-region
2704            (point-min) (point-max)
2705            (mm-charset-to-coding-system charset nil t)))))))
2706
2707 (eval-when-compile
2708   (require 'rfc1843))
2709
2710 (defun article-decode-HZ ()
2711   "Translate a HZ-encoded article."
2712   (interactive)
2713   (require 'rfc1843)
2714   (save-excursion
2715     (let ((inhibit-read-only t))
2716       (rfc1843-decode-region (point-min) (point-max)))))
2717
2718 (defun article-unsplit-urls ()
2719   "Remove the newlines that some other mailers insert into URLs."
2720   (interactive)
2721   (save-excursion
2722     (let ((inhibit-read-only t))
2723       (goto-char (point-min))
2724       (while (re-search-forward
2725               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2726         (replace-match "\\1\\3" t)))
2727     (when (gmm-called-interactively-p 'any)
2728       (gnus-treat-article nil))))
2729
2730 (defun article-wash-html ()
2731   "Format an HTML article."
2732   (interactive)
2733   (let ((handles nil)
2734         (buffer-read-only nil))
2735     (when (gnus-buffer-live-p gnus-original-article-buffer)
2736       (with-current-buffer gnus-original-article-buffer
2737         (setq handles (mm-dissect-buffer t t))))
2738     (article-goto-body)
2739     (delete-region (point) (point-max))
2740     (mm-enable-multibyte)
2741     (mm-inline-text-html handles)))
2742
2743 (defvar gnus-article-browse-html-temp-list nil
2744   "List of temporary files created by `gnus-article-browse-html-parts'.
2745 Internal variable.")
2746
2747 (defcustom gnus-article-browse-delete-temp 'ask
2748   "What to do with temporary files from `gnus-article-browse-html-parts'.
2749 If nil, don't delete temporary files.  If it is t, delete them on
2750 exit from the summary buffer.  If it is the symbol `file', query
2751 on each file, if it is `ask' ask once when exiting from the
2752 summary buffer."
2753   :group 'gnus-article
2754   :version "23.1" ;; No Gnus
2755   :type '(choice (const :tag "Don't delete" nil)
2756                  (const :tag "Don't ask" t)
2757                  (const :tag "Ask" ask)
2758                  (const :tag "Ask for each file" file)))
2759
2760 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2761
2762 (defun gnus-article-browse-delete-temp-files (&optional how)
2763   "Delete temp-files created by `gnus-article-browse-html-parts'."
2764   (when (and gnus-article-browse-html-temp-list
2765              (progn
2766                (or how (setq how gnus-article-browse-delete-temp))
2767                (if (eq how 'ask)
2768                    (let ((files (length gnus-article-browse-html-temp-list)))
2769                      (or (gnus-y-or-n-p
2770                           (if (= files 1)
2771                               "Delete the temporary HTML file? "
2772                             (format "Delete all %s temporary HTML files? "
2773                                     files)))
2774                          (setq gnus-article-browse-html-temp-list nil)))
2775                  how)))
2776     (dolist (file gnus-article-browse-html-temp-list)
2777       (cond ((file-directory-p file)
2778              (when (or (not (eq how 'file))
2779                        (gnus-y-or-n-p
2780                         (format
2781                          "Delete temporary HTML file(s) in directory `%s'? "
2782                          (file-name-as-directory file))))
2783                (gnus-delete-directory file)))
2784             ((file-exists-p file)
2785              (when (or (not (eq how 'file))
2786                        (gnus-y-or-n-p
2787                         (format "Delete temporary HTML file `%s'? " file)))
2788                (delete-file file)))))
2789     ;; Also remove file from the list when not deleted or if file doesn't
2790     ;; exist anymore.
2791     (setq gnus-article-browse-html-temp-list nil))
2792   gnus-article-browse-html-temp-list)
2793
2794 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2795   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2796 Return file name."
2797   (save-match-data
2798     (let (file type)
2799       (catch 'found
2800         (dolist (handle handles)
2801           (cond
2802            ((not (listp handle)))
2803            ;; Exclude broken handles that `gnus-summary-enter-digest-group'
2804            ;; may create.
2805            ((not (or (bufferp (car handle)) (stringp (car handle)))))
2806            ((equal (mm-handle-media-supertype handle) "multipart")
2807             (when (setq file (gnus-article-browse-html-save-cid-content
2808                               cid handle directory))
2809               (throw 'found file)))
2810            ((equal (concat "<" cid ">") (mm-handle-id handle))
2811             (setq file
2812                   (expand-file-name
2813                    (or (mm-handle-filename handle)
2814                        (concat
2815                         (make-temp-name "cid")
2816                         (car (rassoc (car (mm-handle-type handle))
2817                                      mailcap-mime-extensions))))
2818                    directory))
2819             (mm-save-part-to-file handle file)
2820             (throw 'found file))))))))
2821
2822 (defun gnus-article-browse-html-parts (list &optional header)
2823   "View all \"text/html\" parts from LIST.
2824 Recurse into multiparts.  The optional HEADER that should be a decoded
2825 message header will be added to the bodies of the \"text/html\" parts."
2826   ;; Internal function used by `gnus-article-browse-html-article'.
2827   (let (type file charset content cid-dir tmp-file showed)
2828     ;; Find and show the html-parts.
2829     (dolist (handle list)
2830       ;; If HTML, show it:
2831       (cond ((not (listp handle)))
2832             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2833                  (and (equal (car type) "message/external-body")
2834                       (or header
2835                           (setq file (mm-handle-filename handle)))
2836                       (or (mm-handle-cache handle)
2837                           (condition-case code
2838                               (progn (mm-extern-cache-contents handle) t)
2839                             (error
2840                              (gnus-message 3 "%s" (error-message-string code))
2841                              (when (>= gnus-verbose 3) (sit-for 2))
2842                              nil)))
2843                       (progn
2844                         (setq handle (mm-handle-cache handle)
2845                               type (mm-handle-type handle))
2846                         (equal (car type) "text/html"))))
2847              (setq charset (mail-content-type-get type 'charset)
2848                    content (mm-get-part handle))
2849              (with-temp-buffer
2850                (if (eq charset 'gnus-decoded)
2851                    (mm-enable-multibyte)
2852                  (mm-disable-multibyte))
2853                (insert content)
2854                ;; resolve cid contents
2855                (let ((case-fold-search t)
2856                      cid-file)
2857                  (goto-char (point-min))
2858                  (while (re-search-forward "\
2859 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2860                                            nil t)
2861                    (unless cid-dir
2862                      (setq cid-dir (mm-make-temp-file "cid" t))
2863                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2864                    (setq file nil
2865                          content nil)
2866                    (when (setq cid-file
2867                                (gnus-article-browse-html-save-cid-content
2868                                 (match-string 2)
2869                                 (with-current-buffer gnus-article-buffer
2870                                   gnus-article-mime-handles)
2871                                 cid-dir))
2872                      (when (eq system-type 'cygwin)
2873                        (setq cid-file
2874                              (concat "/" (substring
2875                                           (with-output-to-string
2876                                             (call-process "cygpath" nil
2877                                                           standard-output
2878                                                           nil "-m" cid-file))
2879                                           0 -1))))
2880                      (replace-match (concat "file://" cid-file)
2881                                     nil nil nil 1))))
2882                (unless content (setq content (buffer-string))))
2883              (when (or charset header (not file))
2884                (setq tmp-file (mm-make-temp-file
2885                                ;; Do we need to care for 8.3 filenames?
2886                                "mm-" nil ".html")))
2887              ;; Add a meta html tag to specify charset and a header.
2888              (cond
2889               (header
2890                (let (title eheader body hcharset coding)
2891                  (with-temp-buffer
2892                    (mm-enable-multibyte)
2893                    (setq case-fold-search t)
2894                    (insert header "\n")
2895                    (setq title (message-fetch-field "subject"))
2896                    (goto-char (point-min))
2897                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|\\(&\\)\\|\n"
2898                                              nil t)
2899                      (replace-match (cond ((match-beginning 1) "&lt;")
2900                                           ((match-beginning 2) "&gt;")
2901                                           ((match-beginning 3) "&amp;")
2902                                           (t "<br>\n"))))
2903                    (goto-char (point-min))
2904                    (while (re-search-forward "^[\t ]+" nil t)
2905                      (dotimes (i (prog1
2906                                      (current-column)
2907                                    (delete-region (match-beginning 0)
2908                                                   (match-end 0))))
2909                        (insert "&nbsp;")))
2910                    (goto-char (point-min))
2911                    (insert "<div align=\"left\">\n")
2912                    (goto-char (point-max))
2913                    (insert "</div>\n<hr>\n")
2914                    ;; We have to examine charset one by one since
2915                    ;; charset specified in parts might be different.
2916                    (if (eq charset 'gnus-decoded)
2917                        (setq charset 'utf-8
2918                              eheader (mm-encode-coding-string (buffer-string)
2919                                                               charset)
2920                              title (when title
2921                                      (mm-encode-coding-string title charset))
2922                              body (mm-encode-coding-string content charset))
2923                      (setq hcharset (mm-find-mime-charset-region (point-min)
2924                                                                  (point-max)))
2925                      (cond ((= (length hcharset) 1)
2926                             (setq hcharset (car hcharset)
2927                                   coding (mm-charset-to-coding-system
2928                                           hcharset nil t)))
2929                            ((> (length hcharset) 1)
2930                             (setq hcharset 'utf-8
2931                                   coding hcharset)))
2932                      (if coding
2933                          (if charset
2934                              (progn
2935                                (setq body
2936                                      (mm-charset-to-coding-system charset
2937                                                                   nil t))
2938                                (if (eq coding body)
2939                                    (setq eheader (mm-encode-coding-string
2940                                                   (buffer-string) coding)
2941                                          title (when title
2942                                                  (mm-encode-coding-string
2943                                                   title coding))
2944                                          body content)
2945                                  (setq charset 'utf-8
2946                                        eheader (mm-encode-coding-string
2947                                                 (buffer-string) charset)
2948                                        title (when title
2949                                                (mm-encode-coding-string
2950                                                 title charset))
2951                                        body (mm-encode-coding-string
2952                                              (mm-decode-coding-string
2953                                               content body)
2954                                              charset))))
2955                            (setq charset hcharset
2956                                  eheader (mm-encode-coding-string
2957                                           (buffer-string) coding)
2958                                  title (when title
2959                                          (mm-encode-coding-string
2960                                           title coding))
2961                                  body content))
2962                        (setq eheader (mm-string-as-unibyte (buffer-string))
2963                              body content)))
2964                    (erase-buffer)
2965                    (mm-disable-multibyte)
2966                    (insert body)
2967                    (when charset
2968                      (mm-add-meta-html-tag handle charset t))
2969                    (when title
2970                      (goto-char (point-min))
2971                      (unless (search-forward "<title>" nil t)
2972                        (re-search-forward "<head>\\s-*" nil t)
2973                        (insert "<title>" title "</title>\n")))
2974                    (goto-char (point-min))
2975                    (or (re-search-forward
2976                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2977                        (re-search-forward
2978                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2979                    (insert eheader)
2980                    (mm-write-region (point-min) (point-max)
2981                                     tmp-file nil nil nil 'binary t))))
2982               (charset
2983                (mm-with-unibyte-buffer
2984                  (insert (if (eq charset 'gnus-decoded)
2985                              (mm-encode-coding-string content
2986                                                       (setq charset 'utf-8))
2987                            content))
2988                  (if (or (mm-add-meta-html-tag handle charset)
2989                          (not file))
2990                      (mm-write-region (point-min) (point-max)
2991                                       tmp-file nil nil nil 'binary t)
2992                    (setq tmp-file nil))))
2993               (tmp-file
2994                (mm-save-part-to-file handle tmp-file)))
2995              (when tmp-file
2996                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2997              (add-hook 'gnus-summary-prepare-exit-hook
2998                        'gnus-article-browse-delete-temp-files)
2999              (add-hook 'gnus-exit-gnus-hook
3000                        (lambda  ()
3001                          (gnus-article-browse-delete-temp-files t)))
3002              ;; FIXME: Warn if there's an <img> tag?
3003              (browse-url-of-file (or tmp-file (expand-file-name file)))
3004              (setq showed t))
3005             ;; If multipart, recurse
3006             ((equal (mm-handle-media-supertype handle) "multipart")
3007              (when (gnus-article-browse-html-parts handle header)
3008                (setq showed t)))
3009             ((equal (mm-handle-media-type handle) "message/rfc822")
3010              (mm-with-multibyte-buffer
3011                (mm-insert-part handle)
3012                (setq handle (mm-dissect-buffer t t))
3013                (when (and (bufferp (car handle))
3014                           (stringp (car (mm-handle-type handle))))
3015                  (setq handle (list handle)))
3016                (when header
3017                  (article-decode-encoded-words)
3018                  (let ((gnus-visible-headers
3019                         (or (get 'gnus-visible-headers 'standard-value)
3020                             gnus-visible-headers)))
3021                    (article-hide-headers))
3022                  (goto-char (point-min))
3023                  (search-forward "\n\n" nil 'move)
3024                  (skip-chars-backward "\t\n ")
3025                  (setq header (buffer-substring (point-min) (point)))))
3026              (when (prog1
3027                        (gnus-article-browse-html-parts handle header)
3028                      (mm-destroy-parts handle))
3029                (setq showed t)))))
3030     showed))
3031
3032 (defun gnus-article-browse-html-article (&optional arg)
3033   "View \"text/html\" parts of the current article with a WWW browser.
3034 Inline images embedded in a message using the cid scheme, as they are
3035 generally considered to be safe, will be processed properly.
3036 The message header is added to the beginning of every html part unless
3037 the prefix argument ARG is given.
3038
3039 Warning: Spammers use links to images (using the http scheme) in HTML
3040 articles to verify whether you have read the message.  As
3041 `gnus-article-browse-html-article' passes the HTML content to the
3042 browser without eliminating these \"web bugs\" you should only
3043 use it for mails from trusted senders.
3044
3045 If you always want to display HTML parts in the browser, set
3046 `mm-text-html-renderer' to nil.
3047
3048 This command creates temporary files to pass HTML contents including
3049 images if any to the browser, and deletes them when exiting the group
3050 \(if you want)."
3051   ;; Cf. `mm-w3m-safe-url-regexp'
3052   (interactive "P")
3053   (if arg
3054       (gnus-summary-show-article)
3055     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3056                                     gnus-visible-headers))
3057           ;; As we insert a <hr>, there's no need for the body boundary.
3058           (gnus-treat-body-boundary nil))
3059       (gnus-summary-show-article)))
3060   (with-current-buffer gnus-article-buffer
3061     (let ((header (unless arg
3062                     (save-restriction
3063                       (widen)
3064                       (buffer-substring-no-properties
3065                        (goto-char (point-min))
3066                        (if (search-forward "\n\n" nil t)
3067                            (match-beginning 0)
3068                          (goto-char (point-max))
3069                          (skip-chars-backward "\t\n ")
3070                          (point))))))
3071           parts)
3072       (set-buffer gnus-original-article-buffer)
3073       (setq parts (mm-dissect-buffer t t))
3074       ;; If singlepart, enforce a list.
3075       (when (and (bufferp (car parts))
3076                  (stringp (car (mm-handle-type parts))))
3077         (setq parts (list parts)))
3078       ;; Process the list
3079       (unless (gnus-article-browse-html-parts parts header)
3080         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3081       (mm-destroy-parts parts)
3082       (unless arg
3083         (gnus-summary-show-article)))))
3084
3085 (defun article-hide-list-identifiers ()
3086   "Remove list identifies from the Subject header.
3087 The `gnus-list-identifiers' variable specifies what to do."
3088   (interactive)
3089   (let ((inhibit-point-motion-hooks t)
3090         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3091         (inhibit-read-only t))
3092     (when regexp
3093       (save-excursion
3094         (save-restriction
3095           (article-narrow-to-head)
3096           (goto-char (point-min))
3097           (while (re-search-forward
3098                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3099                   nil t)
3100             (delete-region (match-beginning 2) (match-end 0))
3101             (beginning-of-line))
3102           (when (re-search-forward
3103                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3104             (delete-region (match-beginning 1) (match-end 1))))))))
3105
3106 (defun article-hide-pem (&optional arg)
3107   "Toggle hiding of any PEM headers and signatures in the current article.
3108 If given a negative prefix, always show; if given a positive prefix,
3109 always hide."
3110   (interactive (gnus-article-hidden-arg))
3111   (unless (gnus-article-check-hidden-text 'pem arg)
3112     (save-excursion
3113       (let ((inhibit-read-only t) end)
3114         (goto-char (point-min))
3115         ;; Hide the horrendously ugly "header".
3116         (when (and (search-forward
3117                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3118                     nil t)
3119                    (setq end (1+ (match-beginning 0))))
3120           (gnus-add-wash-type 'pem)
3121           (gnus-article-hide-text-type
3122            end
3123            (if (search-forward "\n\n" nil t)
3124                (match-end 0)
3125              (point-max))
3126            'pem)
3127           ;; Hide the trailer as well
3128           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3129                                 nil t)
3130             (gnus-article-hide-text-type
3131              (match-beginning 0) (match-end 0) 'pem)))))))
3132
3133 (defun article-strip-banner ()
3134   "Strip the banners specified by the `banner' group parameter and by
3135 `gnus-article-address-banner-alist'."
3136   (interactive)
3137   (save-excursion
3138     (save-restriction
3139       (let ((inhibit-point-motion-hooks t))
3140         (when (gnus-parameter-banner gnus-newsgroup-name)
3141           (article-really-strip-banner
3142            (gnus-parameter-banner gnus-newsgroup-name)))
3143         (when gnus-article-address-banner-alist
3144           ;; Note that the From header is decoded here, so it is
3145           ;; required that the *-extract-address-components function
3146           ;; supports non-ASCII text.
3147           (let ((from (save-restriction
3148                         (widen)
3149                         (article-narrow-to-head)
3150                         (mail-fetch-field "from"))))
3151             (when (and from
3152                        (setq from
3153                              (cadr (funcall gnus-extract-address-components
3154                                             from))))
3155               (catch 'found
3156                 (dolist (pair gnus-article-address-banner-alist)
3157                   (when (string-match (car pair) from)
3158                     (throw 'found
3159                            (article-really-strip-banner (cdr pair)))))))))))))
3160
3161 (defun article-really-strip-banner (banner)
3162   "Strip the banner specified by the argument."
3163   (save-excursion
3164     (save-restriction
3165       (let ((inhibit-point-motion-hooks t)
3166             (gnus-signature-limit nil)
3167             (inhibit-read-only t))
3168         (article-goto-body)
3169         (cond
3170          ((eq banner 'signature)
3171           (when (gnus-article-narrow-to-signature)
3172             (widen)
3173             (forward-line -1)
3174             (delete-region (point) (point-max))))
3175          ((symbolp banner)
3176           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3177               (while (re-search-forward banner nil t)
3178                 (delete-region (match-beginning 0) (match-end 0)))))
3179          ((stringp banner)
3180           (while (re-search-forward banner nil t)
3181             (delete-region (match-beginning 0) (match-end 0)))))))))
3182
3183 (defun article-babel ()
3184   "Translate article using an online translation service."
3185   (interactive)
3186   (require 'babel)
3187   (gnus-with-article-buffer
3188     (when (article-goto-body)
3189       (let* ((start (point))
3190              (end (point-max))
3191              (orig (buffer-substring start end))
3192              (trans (babel-as-string orig)))
3193         (save-restriction
3194           (narrow-to-region start end)
3195           (delete-region start end)
3196           (insert trans))))))
3197
3198 (defun article-hide-signature (&optional arg)
3199   "Hide the signature in the current article.
3200 If given a negative prefix, always show; if given a positive prefix,
3201 always hide."
3202   (interactive (gnus-article-hidden-arg))
3203   (unless (gnus-article-check-hidden-text 'signature arg)
3204     (save-excursion
3205       (save-restriction
3206         (let ((inhibit-read-only t))
3207           (when (gnus-article-narrow-to-signature)
3208             (gnus-article-hide-text-type
3209              (point-min) (point-max) 'signature))))))
3210   (gnus-set-mode-line 'article))
3211
3212 (defun article-strip-headers-in-body ()
3213   "Strip offensive headers from bodies."
3214   (interactive)
3215   (save-excursion
3216     (article-goto-body)
3217     (let ((case-fold-search t))
3218       (when (looking-at "x-no-archive:")
3219         (gnus-delete-line)))))
3220
3221 (defun article-strip-leading-blank-lines ()
3222   "Remove all blank lines from the beginning of the article."
3223   (interactive)
3224   (save-excursion
3225     (let ((inhibit-point-motion-hooks t)
3226           (inhibit-read-only t))
3227       (when (article-goto-body)
3228         (while (and (not (eobp))
3229                     (looking-at "[ \t]*$"))
3230           (gnus-delete-line))))))
3231
3232 (defun article-narrow-to-head ()
3233   "Narrow the buffer to the head of the message.
3234 Point is left at the beginning of the narrowed-to region."
3235   (narrow-to-region
3236    (goto-char (point-min))
3237    (cond
3238     ;; Absolutely no headers displayed.
3239     ((looking-at "\n")
3240      (point))
3241     ;; Normal headers.
3242     ((search-forward "\n\n" nil 1)
3243      (1- (point)))
3244     ;; Nothing but headers.
3245     (t
3246      (point-max))))
3247   (goto-char (point-min)))
3248
3249 (defun article-goto-body ()
3250   "Place point at the start of the body."
3251   (goto-char (point-min))
3252   (cond
3253    ;; This variable is only bound when dealing with separate
3254    ;; MIME body parts.
3255    (article-goto-body-goes-to-point-min-p
3256     t)
3257    ((search-forward "\n\n" nil t)
3258     t)
3259    (t
3260     (goto-char (point-max))
3261     nil)))
3262
3263 (defun article-strip-multiple-blank-lines ()
3264   "Replace consecutive blank lines with one empty line."
3265   (interactive)
3266   (save-excursion
3267     (let ((inhibit-point-motion-hooks t)
3268           (inhibit-read-only t))
3269       ;; First make all blank lines empty.
3270       (article-goto-body)
3271       (while (re-search-forward "^[ \t]+$" nil t)
3272         (unless (gnus-annotation-in-region-p
3273                  (match-beginning 0) (match-end 0))
3274           (replace-match "" nil t)))
3275       ;; Then replace multiple empty lines with a single empty line.
3276       (article-goto-body)
3277       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3278         (unless (gnus-annotation-in-region-p
3279                  (match-beginning 0) (match-end 0))
3280           (delete-region (match-beginning 1) (match-end 1)))))))
3281
3282 (defun article-strip-leading-space ()
3283   "Remove all white space from the beginning of the lines in the article."
3284   (interactive)
3285   (save-excursion
3286     (let ((inhibit-point-motion-hooks t)
3287           (inhibit-read-only t))
3288       (article-goto-body)
3289       (while (re-search-forward "^[ \t]+" nil t)
3290         (replace-match "" t t)))))
3291
3292 (defun article-strip-trailing-space ()
3293   "Remove all white space from the end of the lines in the article."
3294   (interactive)
3295   (save-excursion
3296     (let ((inhibit-point-motion-hooks t)
3297           (inhibit-read-only t))
3298       (article-goto-body)
3299       (while (re-search-forward "[ \t]+$" nil t)
3300         (replace-match "" t t)))))
3301
3302 (defun article-strip-blank-lines ()
3303   "Strip leading, trailing and multiple blank lines."
3304   (interactive)
3305   (article-strip-leading-blank-lines)
3306   (article-remove-trailing-blank-lines)
3307   (article-strip-multiple-blank-lines))
3308
3309 (defun article-strip-all-blank-lines ()
3310   "Strip all blank lines."
3311   (interactive)
3312   (save-excursion
3313     (let ((inhibit-point-motion-hooks t)
3314           (inhibit-read-only t))
3315       (article-goto-body)
3316       (while (re-search-forward "^[ \t]*\n" nil t)
3317         (replace-match "" t t)))))
3318
3319 (defun gnus-article-narrow-to-signature ()
3320   "Narrow to the signature; return t if a signature is found, else nil."
3321   (let ((inhibit-point-motion-hooks t))
3322     (when (gnus-article-search-signature)
3323       (forward-line 1)
3324       ;; Check whether we have some limits to what we consider
3325       ;; to be a signature.
3326       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3327                       (list gnus-signature-limit)))
3328             limit limited)
3329         (while (setq limit (pop limits))
3330           (if (or (and (integerp limit)
3331                        (< (- (point-max) (point)) limit))
3332                   (and (floatp limit)
3333                        (< (count-lines (point) (point-max)) limit))
3334                   (and (functionp limit)
3335                        (funcall limit))
3336                   (and (stringp limit)
3337                        (not (re-search-forward limit nil t))))
3338               ()                        ; This limit did not succeed.
3339             (setq limited t
3340                   limits nil)))
3341         (unless limited
3342           (narrow-to-region (point) (point-max))
3343           t)))))
3344
3345 (defun gnus-article-search-signature ()
3346   "Search the current buffer for the signature separator.
3347 Put point at the beginning of the signature separator."
3348   (let ((cur (point)))
3349     (goto-char (point-max))
3350     (if (if (stringp gnus-signature-separator)
3351             (re-search-backward gnus-signature-separator nil t)
3352           (let ((seps gnus-signature-separator))
3353             (while (and seps
3354                         (not (re-search-backward (car seps) nil t)))
3355               (pop seps))
3356             seps))
3357         t
3358       (goto-char cur)
3359       nil)))
3360
3361 (defun gnus-article-hidden-arg ()
3362   "Return the current prefix arg as a number, or 0 if no prefix."
3363   (list (if current-prefix-arg
3364             (prefix-numeric-value current-prefix-arg)
3365           0)))
3366
3367 (defun gnus-article-check-hidden-text (type arg)
3368   "Return nil if hiding is necessary.
3369 Arg can be nil or a number.  nil and positive means hide, negative
3370 means show, 0 means toggle."
3371   (save-excursion
3372     (save-restriction
3373       (let ((hide (gnus-article-hidden-text-p type)))
3374         (cond
3375          ((or (null arg)
3376               (> arg 0))
3377           nil)
3378          ((< arg 0)
3379           (gnus-article-show-hidden-text type)
3380           t)
3381          (t
3382           (if (eq hide 'hidden)
3383               (progn
3384                 (gnus-article-show-hidden-text type)
3385                 t)
3386             nil)))))))
3387
3388 (defun gnus-article-hidden-text-p (type)
3389   "Say whether the current buffer contains hidden text of type TYPE."
3390   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3391     (while (and pos
3392                 (not (get-text-property pos 'invisible))
3393                 (not (get-text-property pos 'dummy-invisible)))
3394       (setq pos
3395             (text-property-any (1+ pos) (point-max) 'article-type type)))
3396     (if pos
3397         'hidden
3398       nil)))
3399
3400 (defun gnus-article-show-hidden-text (type &optional dummy)
3401   "Show all hidden text of type TYPE.
3402 Originally it is hide instead of DUMMY."
3403   (let ((inhibit-read-only t)
3404         (inhibit-point-motion-hooks t))
3405     (gnus-remove-text-properties-when
3406      'article-type type
3407      (point-min) (point-max)
3408      (cons 'article-type (cons type
3409                                gnus-hidden-properties)))
3410     (gnus-delete-wash-type type)))
3411
3412 (defconst article-time-units
3413   `((year . ,(* 365.25 24 60 60))
3414     (week . ,(* 7 24 60 60))
3415     (day . ,(* 24 60 60))
3416     (hour . ,(* 60 60))
3417     (minute . 60)
3418     (second . 1))
3419   "Mapping from time units to seconds.")
3420
3421 (defun gnus-article-forward-header ()
3422   "Move point to the start of the next header.
3423 If the current header is a continuation header, this can be several
3424 lines forward."
3425   (let ((ended nil))
3426     (while (not ended)
3427       (forward-line 1)
3428       (if (looking-at "[ \t]+[^ \t]")
3429           (forward-line 1)
3430         (setq ended t)))))
3431
3432 (defun article-treat-date ()
3433   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3434                        (with-current-buffer gnus-summary-buffer
3435                          gnus-article-date-headers)
3436                      gnus-article-date-headers)
3437                    t))
3438
3439 (defun article-date-ut (&optional type highlight date-position)
3440   "Convert DATE date to TYPE in the current article.
3441 The default type is `ut'.  See `gnus-article-date-headers' for
3442 possible values."
3443   (interactive (list 'ut t))
3444   (let* ((case-fold-search t)
3445          (inhibit-read-only t)
3446          (inhibit-point-motion-hooks t)
3447          (first t)
3448          (visible-date (mail-fetch-field "Date"))
3449          pos date bface eface)
3450     (save-excursion
3451       (if date-position
3452           (progn
3453             (goto-char date-position)
3454             (setq date (get-text-property (point) 'original-date))
3455             (when (looking-at "[^:]+:[\t ]*")
3456               (setq bface (get-text-property (match-beginning 0) 'face)
3457                     eface (get-text-property (match-end 0) 'face)))
3458             (delete-region (point)
3459                            (progn
3460                              (gnus-article-forward-header)
3461                              (point)))
3462             (article-transform-date date type bface eface))
3463         (save-restriction
3464           (widen)
3465           (goto-char (point-min))
3466           (while (or (get-text-property (setq pos (point)) 'original-date)
3467                      (and (setq pos (next-single-property-change
3468                                      (point) 'original-date))
3469                           (goto-char pos)))
3470             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3471                                       (1+ (match-beginning 0))
3472                                     (point-max)))
3473             (while (setq pos (text-property-not-all pos (point-max)
3474                                                     'gnus-date-type nil))
3475               (setq date (get-text-property pos 'original-date))
3476               (goto-char pos)
3477               (when (looking-at "[^:]+:[\t ]*")
3478                 (setq bface (get-text-property (match-beginning 0) 'face)
3479                       eface (get-text-property (match-end 0) 'face)))
3480               (delete-region pos (or (text-property-any pos (point-max)
3481                                                         'gnus-date-type nil)
3482                                      (point-max))))
3483             (unless date ;; the 1st time
3484               (goto-char (point-min))
3485               (while (re-search-forward "^Date:[\t ]*" nil t)
3486                 (setq date (get-text-property (match-beginning 0)
3487                                               'original-date)
3488                       bface (get-text-property (match-beginning 0) 'face)
3489                       eface (get-text-property (match-end 0) 'face))
3490                 (delete-region (point-at-bol) (progn
3491                                                 (gnus-article-forward-header)
3492                                                 (point)))))
3493             (when (and (not date)
3494                        visible-date)
3495               (setq date visible-date))
3496             (when date
3497               (article-transform-date date type bface eface))
3498             (goto-char (point-max))
3499             (widen)))))))
3500
3501 (defun article-transform-date (date type bface eface)
3502   (dolist (this-type (cond
3503                       ((null type)
3504                        (list 'ut))
3505                       ((atom type)
3506                        (list type))
3507                       (t
3508                        type)))
3509     (goto-char
3510      (prog1
3511          (point)
3512        (add-text-properties
3513         (point)
3514         (progn
3515           (insert (article-make-date-line date (or this-type 'ut)) "\n")
3516           (point))
3517         (list 'original-date date 'gnus-date-type this-type))))
3518     ;; Do highlighting.
3519     (when (looking-at
3520            "\\([^:]+:\\)[\t ]*\\(\\(?:[^\t\n ]+[\t ]+\\)*[^\t\n ]+\\)?")
3521       (put-text-property (match-beginning 1) (match-end 1) 'face bface)
3522       (when (match-beginning 2)
3523         (put-text-property (match-beginning 2) (match-end 2) 'face eface))
3524       (while (and (zerop (forward-line 1))
3525                   (looking-at "[\t ]+\\(\\(?:[^\t\n ]+[\t ]+\\)*[^\t\n ]+\\)?"))
3526         (when (match-beginning 1)
3527           (put-text-property (match-beginning 1) (match-end 1) 'face eface))))))
3528
3529 (defun article-make-date-line (date type)
3530   "Return a DATE line of TYPE."
3531   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3532                              combined-lapsed))
3533     (error "Unknown conversion type: %s" type))
3534   (condition-case ()
3535       (let ((time (ignore-errors (date-to-time date))))
3536         (cond
3537          ;; Convert to the local timezone.
3538          ((eq type 'local)
3539           (concat "Date: " (message-make-date time)))
3540          ;; Convert to Universal Time.
3541          ((eq type 'ut)
3542           (concat "Date: "
3543                   (substring
3544                    (message-make-date
3545                     (let* ((e (parse-time-string date))
3546                            (tm (apply 'encode-time e))
3547                            (ms (car tm))
3548                            (ls (- (cadr tm) (car (current-time-zone time)))))
3549                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3550                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3551                             (t (list ms ls)))))
3552                    0 -5)
3553                   "UT"))
3554          ;; Get the original date from the article.
3555          ((eq type 'original)
3556           (concat "Date: " (if (string-match "\n+$" date)
3557                                (substring date 0 (match-beginning 0))
3558                              date)))
3559          ;; Let the user define the format.
3560          ((eq type 'user-defined)
3561           (let ((format (or (condition-case nil
3562                                 (with-current-buffer gnus-summary-buffer
3563                                   gnus-article-time-format)
3564                               (error nil))
3565                             gnus-article-time-format)))
3566             (if (functionp format)
3567                 (funcall format time)
3568               (concat "Date: " (format-time-string format time)))))
3569          ;; ISO 8601.
3570          ((eq type 'iso8601)
3571           (let ((tz (car (current-time-zone time))))
3572             (concat
3573              "Date: "
3574              (format-time-string "%Y%m%dT%H%M%S" time)
3575              (format "%s%02d%02d"
3576                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3577                      (/ (% (abs tz) 3600) 60)))))
3578          ;; Do a lapsed format.
3579          ((eq type 'lapsed)
3580           (concat "Date: " (article-lapsed-string time)))
3581          ;; A combined date/lapsed format.
3582          ((eq type 'combined-lapsed)
3583           (let ((date-string (article-make-date-line date 'original))
3584                 (segments 3)
3585                 lapsed-string)
3586             (while (and
3587                     time
3588                     (setq lapsed-string
3589                           (concat " (" (article-lapsed-string time segments) ")"))
3590                     (> (+ (length date-string)
3591                           (length lapsed-string))
3592                        (+ fill-column 6))
3593                     (> segments 0))
3594               (setq segments (1- segments)))
3595             (if (> segments 0)
3596                 (concat date-string lapsed-string)
3597               date-string)))
3598          ;; Display the date in proper English
3599          ((eq type 'english)
3600           (let ((dtime (decode-time time)))
3601             (concat
3602              "Date: the "
3603              (number-to-string (nth 3 dtime))
3604              (let ((digit (% (nth 3 dtime) 10)))
3605                (cond
3606                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3607                 ((= digit 1) "st")
3608                 ((= digit 2) "nd")
3609                 ((= digit 3) "rd")
3610                 (t "th")))
3611              " of "
3612              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3613              " "
3614              (number-to-string (nth 5 dtime))
3615              " at "
3616              (format "%02d" (nth 2 dtime))
3617              ":"
3618              (format "%02d" (nth 1 dtime)))))))
3619     (foo
3620      (format "Date: %s (from Gnus)" date))))
3621
3622 (defun article-lapsed-string (time &optional max-segments)
3623   ;; If the date is seriously mangled, the timezone functions are
3624   ;; liable to bug out, so we ignore all errors.
3625   (let* ((now (current-time))
3626          (real-time (subtract-time now time))
3627          (real-sec (and real-time
3628                         (+ (* (float (car real-time)) 65536)
3629                            (cadr real-time))))
3630          (sec (and real-time (abs real-sec)))
3631          (segments 0)
3632          num prev)
3633     (unless max-segments
3634       (setq max-segments (length article-time-units)))
3635     (cond
3636      ((null real-time)
3637       "Unknown")
3638      ((zerop sec)
3639       "Now")
3640      (t
3641       (concat
3642        ;; This is a bit convoluted, but basically we go
3643        ;; through the time units for years, weeks, etc,
3644        ;; and divide things to see whether that results
3645        ;; in positive answers.
3646        (mapconcat
3647         (lambda (unit)
3648           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3649                   (>= segments max-segments))
3650               ;; The (remaining) seconds are too few to
3651               ;; be divided into this time unit.
3652               ""
3653             ;; It's big enough, so we output it.
3654             (setq sec (- sec (* num (cdr unit))))
3655             (prog1
3656                 (concat (if prev ", " "") (int-to-string
3657                                            (floor num))
3658                         " " (symbol-name (car unit))
3659                         (if (> num 1) "s" ""))
3660               (setq prev t
3661                     segments (1+ segments)))))
3662         article-time-units "")
3663        ;; If dates are odd, then it might appear like the
3664        ;; article was sent in the future.
3665        (if (> real-sec 0)
3666            " ago"
3667          " in the future"))))))
3668
3669 (defun article-date-local (&optional highlight)
3670   "Convert the current article date to the local timezone."
3671   (interactive (list t))
3672   (article-date-ut 'local highlight))
3673
3674 (defun article-date-english (&optional highlight)
3675   "Convert the current article date to something that is proper English."
3676   (interactive (list t))
3677   (article-date-ut 'english highlight))
3678
3679 (defun article-date-original (&optional highlight)
3680   "Convert the current article date to what it was originally.
3681 This is only useful if you have used some other date conversion
3682 function and want to see what the date was before converting."
3683   (interactive (list t))
3684   (article-date-ut 'original highlight))
3685
3686 (defun article-date-lapsed (&optional highlight)
3687   "Convert the current article date to time lapsed since it was sent."
3688   (interactive (list t))
3689   (article-date-ut 'lapsed highlight))
3690
3691 (defun article-date-combined-lapsed (&optional highlight)
3692   "Convert the current article date to time lapsed since it was sent."
3693   (interactive (list t))
3694   (article-date-ut 'combined-lapsed highlight))
3695
3696 (defun article-update-date-lapsed ()
3697   "Function to be run from a timer to update the lapsed time line."
3698   (save-match-data
3699     (let ((buffer (current-buffer)))
3700       (ignore-errors
3701         (walk-windows
3702          (lambda (w)
3703            (set-buffer (window-buffer w))
3704            (when (derived-mode-p 'gnus-article-mode)
3705              (let ((old-line (count-lines (point-min) (point)))
3706                    (old-column (- (point) (line-beginning-position)))
3707                    (window-start (window-start w))
3708                    (pos (point-min))
3709                    type next end)
3710                (while (setq pos (text-property-not-all pos (point-max)
3711                                                        'gnus-date-type nil))
3712                  (setq next (or (next-single-property-change pos
3713                                                              'gnus-date-type)
3714                                 (point-max)))
3715                  (setq type (get-text-property pos 'gnus-date-type))
3716                  (when (memq type '(lapsed combined-lapsed user-defined))
3717                    (article-date-ut type t pos)
3718                    (setq end (or (next-single-property-change pos
3719                                                               'gnus-date-type)
3720                                  (point-max)))
3721                    (when window-start
3722                      (if (/= window-start next)
3723                          (setq window-start nil)
3724                        (set-window-start w end)))
3725                    (setq next end))
3726                  (setq pos next))
3727                (goto-char (point-min))
3728                (when (> old-column 0)
3729                  (setq old-line (1- old-line)))
3730                (forward-line old-line)
3731                (end-of-line)
3732                (when (> (current-column) old-column)
3733                  (beginning-of-line)
3734                  (forward-char old-column)))))
3735          nil 'visible))
3736       (set-buffer buffer))))
3737
3738 (defun gnus-start-date-timer (&optional n)
3739   "Start a timer to update the Date headers in the article buffers.
3740 The numerical prefix says how frequently (in seconds) the function
3741 is to run."
3742   (interactive "p")
3743   (unless n
3744     (setq n 1))
3745   (gnus-stop-date-timer)
3746   (setq article-lapsed-timer
3747         (run-at-time 1 n 'article-update-date-lapsed)))
3748
3749 (defun gnus-stop-date-timer ()
3750   "Stop the Date timer."
3751   (interactive)
3752   (when article-lapsed-timer
3753     (nnheader-cancel-timer article-lapsed-timer)
3754     (setq article-lapsed-timer nil)))
3755
3756 (defun article-date-user (&optional highlight)
3757   "Convert the current article date to the user-defined format.
3758 This format is defined by the `gnus-article-time-format' variable."
3759   (interactive (list t))
3760   (article-date-ut 'user highlight))
3761
3762 (defun article-date-iso8601 (&optional highlight)
3763   "Convert the current article date to ISO8601."
3764   (interactive (list t))
3765   (article-date-ut 'iso8601 highlight))
3766
3767 (defmacro gnus-article-save-original-date (&rest forms)
3768   "Save the original date as a text property and evaluate FORMS."
3769   `(let* ((case-fold-search t)
3770           (start (progn
3771                    (goto-char (point-min))
3772                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3773                               (not (bolp)))
3774                      (match-end 0))))
3775           (date (when (and start
3776                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3777                                               nil t))
3778                   (buffer-substring-no-properties start
3779                                                   (match-beginning 0)))))
3780      (goto-char (point-max))
3781      (skip-chars-backward "\n")
3782      (put-text-property (point-min) (point) 'original-date date)
3783      ,@forms
3784      (goto-char (point-max))
3785      (skip-chars-backward "\n")
3786      (put-text-property (point-min) (point) 'original-date date)))
3787
3788 ;; (defun article-show-all ()
3789 ;;   "Show all hidden text in the article buffer."
3790 ;;   (interactive)
3791 ;;   (save-excursion
3792 ;;     (let ((inhibit-read-only t))
3793 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3794
3795 (defun article-remove-leading-whitespace ()
3796   "Remove excessive whitespace from all headers."
3797   (interactive)
3798   (save-excursion
3799     (save-restriction
3800       (let ((inhibit-read-only t))
3801         (article-narrow-to-head)
3802         (goto-char (point-min))
3803         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3804           (delete-region (match-beginning 1) (match-end 1)))))))
3805
3806 (defun article-emphasize (&optional arg)
3807   "Emphasize text according to `gnus-emphasis-alist'."
3808   (interactive (gnus-article-hidden-arg))
3809   (unless (gnus-article-check-hidden-text 'emphasis arg)
3810     (save-excursion
3811       (let ((alist (or
3812                     (condition-case nil
3813                         (with-current-buffer gnus-summary-buffer
3814                           gnus-article-emphasis-alist)
3815                       (error))
3816                     gnus-emphasis-alist))
3817             (inhibit-read-only t)
3818             (props (append '(article-type emphasis)
3819                            gnus-hidden-properties))
3820             regexp elem beg invisible visible face)
3821         (article-goto-body)
3822         (setq beg (point))
3823         (while (setq elem (pop alist))
3824           (goto-char beg)
3825           (setq regexp (car elem)
3826                 invisible (nth 1 elem)
3827                 visible (nth 2 elem)
3828                 face (nth 3 elem))
3829           (while (re-search-forward regexp nil t)
3830             (when (and (match-beginning visible) (match-beginning invisible))
3831               (gnus-article-hide-text
3832                (match-beginning invisible) (match-end invisible) props)
3833               (gnus-article-unhide-text-type
3834                (match-beginning visible) (match-end visible) 'emphasis)
3835               (gnus-put-overlay-excluding-newlines
3836                (match-beginning visible) (match-end visible) 'face face)
3837               (gnus-add-wash-type 'emphasis)
3838               (goto-char (match-end invisible)))))))))
3839
3840 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3841   "Setup newsgroup emphasis alist."
3842   (unless gnus-article-emphasis-alist
3843     (let ((name (and gnus-newsgroup-name
3844                      (gnus-group-real-name gnus-newsgroup-name))))
3845       (make-local-variable 'gnus-article-emphasis-alist)
3846       (setq gnus-article-emphasis-alist
3847             (nconc
3848              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3849                (while (setq elem (pop alist))
3850                  (when (and name (string-match (car elem) name))
3851                    (setq alist nil
3852                          highlight (copy-sequence (cdr elem)))))
3853                highlight)
3854              (copy-sequence highlight-words)
3855              (if gnus-newsgroup-name
3856                  (copy-sequence (gnus-group-find-parameter
3857                                  gnus-newsgroup-name 'highlight-words t)))
3858              gnus-emphasis-alist)))))
3859
3860 (defvar gnus-summary-article-menu)
3861 (defvar gnus-summary-post-menu)
3862
3863 ;;; Saving functions.
3864
3865 (defun gnus-article-save (save-buffer file &optional num)
3866   "Save the currently selected article."
3867   (when (or (get gnus-default-article-saver :headers)
3868             (not gnus-save-all-headers))
3869     ;; Remove headers according to `gnus-saved-headers' or the value
3870     ;; of the `:headers' property that the saver function might have.
3871     (let ((gnus-visible-headers
3872            (or (symbol-value (get gnus-default-article-saver :headers))
3873                gnus-saved-headers gnus-visible-headers))
3874           ;; Ignore group parameter.  See `article-hide-headers'.
3875           (gnus-summary-buffer nil))
3876       (with-current-buffer save-buffer
3877         (article-hide-headers 1 t))))
3878   (save-window-excursion
3879     (if (not gnus-default-article-saver)
3880         (error "No default saver is defined")
3881       ;; !!! Magic!  The saving functions all save
3882       ;; `gnus-save-article-buffer' (or so they think), but we
3883       ;; bind that variable to our save-buffer.
3884       (set-buffer gnus-article-buffer)
3885       (let* ((gnus-save-article-buffer save-buffer)
3886              (filename
3887               (cond
3888                ((not gnus-prompt-before-saving) 'default)
3889                ((eq gnus-prompt-before-saving 'always) nil)
3890                (t file)))
3891              (gnus-number-of-articles-to-be-saved
3892               (when (eq gnus-prompt-before-saving t)
3893                 num)))                  ; Magic
3894         (set-buffer gnus-article-current-summary)
3895         (funcall gnus-default-article-saver filename)))))
3896
3897 (defun gnus-read-save-file-name (prompt &optional filename
3898                                         function group headers variable
3899                                         dir-var)
3900   (let ((default-name
3901           (funcall function group headers (symbol-value variable)))
3902         result)
3903     (setq result
3904           (expand-file-name
3905            (cond
3906             ((eq filename 'default)
3907              default-name)
3908             ((eq filename t)
3909              default-name)
3910             (filename filename)
3911             (t
3912              (when (symbol-value dir-var)
3913                (setq default-name (expand-file-name
3914                                    (file-name-nondirectory default-name)
3915                                    (symbol-value dir-var))))
3916              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3917                     (prompt
3918                      (format prompt
3919                              (if (and gnus-number-of-articles-to-be-saved
3920                                       (> gnus-number-of-articles-to-be-saved 1))
3921                                  (format "these %d articles"
3922                                          gnus-number-of-articles-to-be-saved)
3923                                "this article")))
3924                     (file
3925                      ;; Let the split methods have their say.
3926                      (cond
3927                       ;; No split name was found.
3928                       ((null split-name)
3929                        (read-file-name
3930                         (concat prompt " (default "
3931                                 (file-name-nondirectory default-name) "): ")
3932                         (file-name-directory default-name)
3933                         default-name))
3934                       ;; A single group name is returned.
3935                       ((stringp split-name)
3936                        (setq default-name
3937                              (funcall function split-name headers
3938                                       (symbol-value variable)))
3939                        (read-file-name
3940                         (concat prompt " (default "
3941                                 (file-name-nondirectory default-name) "): ")
3942                         (file-name-directory default-name)
3943                         default-name))
3944                       ;; A single split name was found
3945                       ((= 1 (length split-name))
3946                        (let* ((name (expand-file-name
3947                                      (car split-name)
3948                                      gnus-article-save-directory))
3949                               (dir (cond ((file-directory-p name)
3950                                           (file-name-as-directory name))
3951                                          ((file-exists-p name) name)
3952                                          (t gnus-article-save-directory))))
3953                          (read-file-name
3954                           (concat prompt " (default " name "): ")
3955                           dir name)))
3956                       ;; A list of splits was found.
3957                       (t
3958                        (setq split-name (nreverse split-name))
3959                        (let (result)
3960                          (let ((file-name-history
3961                                 (nconc split-name file-name-history)))
3962                            (setq result
3963                                  (expand-file-name
3964                                   (read-file-name
3965                                    (concat prompt " (`M-p' for defaults): ")
3966                                    gnus-article-save-directory
3967                                    (car split-name))
3968                                   gnus-article-save-directory)))
3969                          (car (push result file-name-history)))))))
3970                ;; Create the directory.
3971                (gnus-make-directory (file-name-directory file))
3972                ;; If we have read a directory, we append the default file name.
3973                (when (file-directory-p file)
3974                  (setq file (expand-file-name (file-name-nondirectory
3975                                                default-name)
3976                                               (file-name-as-directory file))))
3977                ;; Possibly translate some characters.
3978                (nnheader-translate-file-chars file))))))
3979     (gnus-make-directory (file-name-directory result))
3980     (when variable
3981       (set variable result))
3982     (when dir-var
3983       (set dir-var (file-name-directory result)))
3984     result))
3985
3986 (defun gnus-article-archive-name (group)
3987   "Return the first instance of an \"Archive-name\" in the current buffer."
3988   (let ((case-fold-search t))
3989     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3990       (nnheader-concat gnus-article-save-directory
3991                        (match-string 1)))))
3992
3993 (defun gnus-article-nndoc-name (group)
3994   "If GROUP is an nndoc group, return the name of the parent group."
3995   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3996     (gnus-group-get-parameter group 'save-article-group)))
3997
3998 (defun gnus-summary-save-in-rmail (&optional filename)
3999   "Append this article to Rmail file.
4000 Optional argument FILENAME specifies file name.
4001 Directory to save to is default to `gnus-article-save-directory'."
4002   (setq filename (gnus-read-save-file-name
4003                   "Save %s in rmail file" filename
4004                   gnus-rmail-save-name gnus-newsgroup-name
4005                   gnus-current-headers 'gnus-newsgroup-last-rmail))
4006   (with-current-buffer gnus-save-article-buffer
4007     (save-excursion
4008       (save-restriction
4009         (widen)
4010         ;; Note that unlike gnus-summary-save-in-mail, there is no
4011         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
4012         ;; not use Babyl.
4013         (gnus-output-to-rmail filename))))
4014   filename)
4015
4016 (defun gnus-summary-save-in-mail (&optional filename)
4017   "Append this article to Unix mail file.
4018 Optional argument FILENAME specifies file name.
4019 Directory to save to is default to `gnus-article-save-directory'."
4020   (setq filename (gnus-read-save-file-name
4021                   "Save %s in Unix mail file" filename
4022                   gnus-mail-save-name gnus-newsgroup-name
4023                   gnus-current-headers 'gnus-newsgroup-last-mail))
4024   (with-current-buffer gnus-save-article-buffer
4025     (save-excursion
4026       (save-restriction
4027         (widen)
4028         (if (and (file-readable-p filename)
4029                  (file-regular-p filename)
4030                  (mail-file-babyl-p filename))
4031             (gnus-output-to-rmail filename)
4032           (gnus-output-to-mail filename)))))
4033   filename)
4034
4035 (put 'gnus-summary-save-in-file :decode t)
4036 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4037 (defun gnus-summary-save-in-file (&optional filename overwrite)
4038   "Append this article to file.
4039 Optional argument FILENAME specifies file name.
4040 Directory to save to is default to `gnus-article-save-directory'."
4041   (setq filename (gnus-read-save-file-name
4042                   "Save %s in file" filename
4043                   gnus-file-save-name gnus-newsgroup-name
4044                   gnus-current-headers 'gnus-newsgroup-last-file))
4045   (with-current-buffer gnus-save-article-buffer
4046     (save-excursion
4047       (save-restriction
4048         (widen)
4049         (when (and overwrite
4050                    (file-exists-p filename))
4051           (delete-file filename))
4052         (gnus-output-to-file filename))))
4053   filename)
4054
4055 (put 'gnus-summary-write-to-file :decode t)
4056 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4057 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4058 (defun gnus-summary-write-to-file (&optional filename)
4059   "Write this article to a file, overwriting it if the file exists.
4060 Optional argument FILENAME specifies file name.
4061 The directory to save in defaults to `gnus-article-save-directory'."
4062   (setq filename (gnus-read-save-file-name
4063                   "Save %s in file" filename
4064                   gnus-file-save-name gnus-newsgroup-name
4065                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4066   (gnus-summary-save-in-file filename t))
4067
4068 (put 'gnus-summary-save-body-in-file :decode t)
4069 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4070   "Append this article body to a file.
4071 Optional argument FILENAME specifies file name.
4072 The directory to save in defaults to `gnus-article-save-directory'."
4073   (setq filename (gnus-read-save-file-name
4074                   "Save %s body in file" filename
4075                   gnus-file-save-name gnus-newsgroup-name
4076                   gnus-current-headers 'gnus-newsgroup-last-file))
4077   (with-current-buffer gnus-save-article-buffer
4078     (save-excursion
4079       (save-restriction
4080         (widen)
4081         (when (article-goto-body)
4082           (narrow-to-region (point) (point-max)))
4083         (when (and overwrite
4084                    (file-exists-p filename))
4085           (delete-file filename))
4086         (gnus-output-to-file filename))))
4087   filename)
4088
4089 (put 'gnus-summary-write-body-to-file :decode t)
4090 (put 'gnus-summary-write-body-to-file
4091      :function 'gnus-summary-save-body-in-file)
4092 (defun gnus-summary-write-body-to-file (&optional filename)
4093   "Write this article body to a file, overwriting it if the file exists.
4094 Optional argument FILENAME specifies file name.
4095 The directory to save in defaults to `gnus-article-save-directory'."
4096   (setq filename (gnus-read-save-file-name
4097                   "Save %s body in file" filename
4098                   gnus-file-save-name gnus-newsgroup-name
4099                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4100   (gnus-summary-save-body-in-file filename t))
4101
4102 (put 'gnus-summary-save-in-pipe :decode t)
4103 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4104 (defun gnus-summary-save-in-pipe (&optional command raw)
4105   "Pipe this article to subprocess COMMAND.
4106 Valid values for COMMAND include:
4107   a string
4108     The executable command name and possibly arguments.
4109   nil
4110     You will be prompted for the command in the minibuffer.
4111   the symbol `default'
4112     It will be replaced with the command which the variable
4113     `gnus-summary-pipe-output-default-command' holds or the command
4114     last used for saving.
4115 Non-nil value for RAW overrides `:decode' and `:headers' properties
4116 and the raw article including all headers will be piped."
4117   (let ((article (gnus-summary-article-number))
4118         (decode (unless raw
4119                   (get 'gnus-summary-save-in-pipe :decode)))
4120         save-buffer default)
4121     (if article
4122         (if (vectorp (gnus-summary-article-header article))
4123             (save-current-buffer
4124               (gnus-summary-select-article decode decode nil article)
4125               (insert-buffer-substring
4126                (prog1
4127                    (if decode
4128                        gnus-article-buffer
4129                      gnus-original-article-buffer)
4130                  (setq save-buffer
4131                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4132               ;; Remove unwanted headers.
4133               (when (and (not raw)
4134                          (or (get 'gnus-summary-save-in-pipe :headers)
4135                              (not gnus-save-all-headers)))
4136                 (let ((gnus-visible-headers
4137                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4138                                               :headers))
4139                            gnus-saved-headers gnus-visible-headers))
4140                       (gnus-summary-buffer nil))
4141                   (article-hide-headers 1 t))))
4142           (error "%d is not a real article" article))
4143       (error "No article to pipe"))
4144     (setq default (or gnus-summary-pipe-output-default-command
4145                       gnus-last-shell-command))
4146     (unless (stringp command)
4147       (setq command
4148             (if (and (eq command 'default) default)
4149                 default
4150               (gnus-read-shell-command "Shell command on this article: "
4151                                        default))))
4152     (when (string-equal command "")
4153       (if default
4154           (setq command default)
4155         (error "A command is required")))
4156     (with-current-buffer save-buffer
4157       (save-restriction
4158         (widen)
4159         (shell-command-on-region (point-min) (point-max) command nil)))
4160     (gnus-kill-buffer save-buffer))
4161   (setq gnus-summary-pipe-output-default-command command))
4162
4163 (defun gnus-summary-pipe-to-muttprint (&optional command)
4164   "Pipe this article to muttprint."
4165   (unless (stringp command)
4166     (setq command (read-string
4167                    "Print using command: " gnus-summary-muttprint-program
4168                    nil gnus-summary-muttprint-program)))
4169   (let ((gnus-summary-pipe-output-default-command
4170          gnus-summary-pipe-output-default-command))
4171     (gnus-summary-save-in-pipe command))
4172   (setq gnus-summary-muttprint-program command))
4173
4174 ;;; Article file names when saving.
4175
4176 (defun gnus-capitalize-newsgroup (newsgroup)
4177   "Capitalize NEWSGROUP name."
4178   (when (not (zerop (length newsgroup)))
4179     (concat (char-to-string (upcase (aref newsgroup 0)))
4180             (substring newsgroup 1))))
4181
4182 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4183   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4184 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4185 Otherwise, it is like ~/News/news/group/num."
4186   (let ((default
4187           (expand-file-name
4188            (concat (if (gnus-use-long-file-name 'not-save)
4189                        (gnus-capitalize-newsgroup newsgroup)
4190                      (gnus-newsgroup-directory-form newsgroup))
4191                    "/" (int-to-string (mail-header-number headers)))
4192            gnus-article-save-directory)))
4193     (if (and last-file
4194              (string-equal (file-name-directory default)
4195                            (file-name-directory last-file))
4196              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4197         default
4198       (or last-file default))))
4199
4200 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4201   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4202 If variable `gnus-use-long-file-name' is non-nil, it is
4203 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4204   (let ((default
4205           (expand-file-name
4206            (concat (if (gnus-use-long-file-name 'not-save)
4207                        newsgroup
4208                      (gnus-newsgroup-directory-form newsgroup))
4209                    "/" (int-to-string (mail-header-number headers)))
4210            gnus-article-save-directory)))
4211     (if (and last-file
4212              (string-equal (file-name-directory default)
4213                            (file-name-directory last-file))
4214              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4215         default
4216       (or last-file default))))
4217
4218 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4219   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4220 If variable `gnus-use-long-file-name' is non-nil, it is
4221 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4222   (or last-file
4223       (expand-file-name
4224        (if (gnus-use-long-file-name 'not-save)
4225            newsgroup
4226          (file-relative-name
4227           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4228           default-directory))
4229        gnus-article-save-directory)))
4230
4231 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4232   "Generate file name from sender."
4233   (let ((from (mail-header-from headers)))
4234     (expand-file-name
4235      (if (and from (string-match "\\([^ <]+\\)@" from))
4236          (match-string 1 from)
4237        "nobody")
4238      gnus-article-save-directory)))
4239
4240 (defun article-verify-x-pgp-sig ()
4241   "Verify X-PGP-Sig."
4242   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4243   (interactive)
4244   (if (gnus-buffer-live-p gnus-original-article-buffer)
4245       (let ((sig (with-current-buffer gnus-original-article-buffer
4246                    (gnus-fetch-field "X-PGP-Sig")))
4247             items info headers)
4248         (when (and sig
4249                    mml2015-use
4250                    (mml2015-clear-verify-function))
4251           (with-temp-buffer
4252             (insert-buffer-substring gnus-original-article-buffer)
4253             (setq items (split-string sig))
4254             (message-narrow-to-head)
4255             (let ((inhibit-point-motion-hooks t)
4256                   (case-fold-search t))
4257               ;; Don't verify multiple headers.
4258               (setq headers (mapconcat (lambda (header)
4259                                          (concat header ": "
4260                                                  (mail-fetch-field header)
4261                                                  "\n"))
4262                                        (split-string (nth 1 items) ",") "")))
4263             (delete-region (point-min) (point-max))
4264             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4265             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4266             (insert headers)
4267             (widen)
4268             (forward-line)
4269             (while (not (eobp))
4270               (if (looking-at "^-")
4271                   (insert "- "))
4272               (forward-line))
4273             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4274             (insert "Version: " (car items) "\n\n")
4275             (insert (mapconcat 'identity (cddr items) "\n"))
4276             (insert "\n-----END PGP SIGNATURE-----\n")
4277             (let ((mm-security-handle (list (format "multipart/signed"))))
4278               (mml2015-clean-buffer)
4279               (let ((coding-system-for-write (or gnus-newsgroup-charset
4280                                                  'iso-8859-1)))
4281                 (funcall (mml2015-clear-verify-function)))
4282               (setq info
4283                     (or (mm-handle-multipart-ctl-parameter
4284                          mm-security-handle 'gnus-details)
4285                         (mm-handle-multipart-ctl-parameter
4286                          mm-security-handle 'gnus-info)))))
4287           (when info
4288             (let ((inhibit-read-only t) bface eface)
4289               (save-restriction
4290                 (message-narrow-to-head)
4291                 (goto-char (point-max))
4292                 (forward-line -1)
4293                 (setq bface (get-text-property (point-at-bol) 'face)
4294                       eface (get-text-property (1- (point-at-eol)) 'face))
4295                 (message-remove-header "X-Gnus-PGP-Verify")
4296                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4297                     (forward-line)
4298                   (goto-char (point-max)))
4299                 (narrow-to-region (point) (point))
4300                 (insert "X-Gnus-PGP-Verify: " info "\n")
4301                 (goto-char (point-min))
4302                 (forward-line)
4303                 (while (not (eobp))
4304                   (if (not (looking-at "^[ \t]"))
4305                       (insert " "))
4306                   (forward-line))
4307                 ;; Do highlighting.
4308                 (goto-char (point-min))
4309                 (when (looking-at "\\([^:]+\\): *")
4310                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4311                                      'face bface)
4312                   (put-text-property (match-end 0) (point-max)
4313                                      'face eface)))))))))
4314
4315 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4316
4317 (defun article-verify-cancel-lock ()
4318   "Verify Cancel-Lock header."
4319   (interactive)
4320   (if (gnus-buffer-live-p gnus-original-article-buffer)
4321       (canlock-verify gnus-original-article-buffer)))
4322
4323 (eval-and-compile
4324   (mapc
4325    (lambda (func)
4326      (let (afunc gfunc)
4327        (if (consp func)
4328            (setq afunc (car func)
4329                  gfunc (cdr func))
4330          (setq afunc func
4331                gfunc (intern (format "gnus-%s" func))))
4332        (defalias gfunc
4333          (when (fboundp afunc)
4334            `(lambda (&optional interactive &rest args)
4335               ,(documentation afunc t)
4336               (interactive (list t))
4337               (with-current-buffer gnus-article-buffer
4338                 (if interactive
4339                     (call-interactively ',afunc)
4340                   (apply ',afunc args))))))))
4341    '(article-hide-headers
4342      article-verify-x-pgp-sig
4343      article-verify-cancel-lock
4344      article-hide-boring-headers
4345      article-treat-overstrike
4346      article-treat-ansi-sequences
4347      article-fill-long-lines
4348      article-capitalize-sentences
4349      article-remove-cr
4350      article-remove-leading-whitespace
4351      article-display-x-face
4352      article-display-face
4353      article-de-quoted-unreadable
4354      article-de-base64-unreadable
4355      article-decode-HZ
4356      article-wash-html
4357      article-unsplit-urls
4358      article-hide-list-identifiers
4359      article-strip-banner
4360      article-babel
4361      article-hide-pem
4362      article-hide-signature
4363      article-strip-headers-in-body
4364      article-remove-trailing-blank-lines
4365      article-strip-leading-blank-lines
4366      article-strip-multiple-blank-lines
4367      article-strip-leading-space
4368      article-strip-trailing-space
4369      article-strip-blank-lines
4370      article-strip-all-blank-lines
4371      article-date-local
4372      article-date-english
4373      article-date-iso8601
4374      article-date-original
4375      article-treat-date
4376      article-date-ut
4377      article-decode-mime-words
4378      article-decode-charset
4379      article-decode-encoded-words
4380      article-date-user
4381      article-date-lapsed
4382      article-date-combined-lapsed
4383      article-emphasize
4384      article-treat-dumbquotes
4385      article-treat-non-ascii
4386      article-normalize-headers
4387      ;;(article-show-all . gnus-article-show-all-headers)
4388      )))
4389 \f
4390 ;;;
4391 ;;; Gnus article mode
4392 ;;;
4393
4394 (put 'gnus-article-mode 'mode-class 'special)
4395
4396 (set-keymap-parent gnus-article-mode-map widget-keymap)
4397
4398 (gnus-define-keys gnus-article-mode-map
4399   " " gnus-article-goto-next-page
4400   [?\S-\ ] gnus-article-goto-prev-page
4401   "\177" gnus-article-goto-prev-page
4402   [delete] gnus-article-goto-prev-page
4403   "\C-c^" gnus-article-refer-article
4404   "h" gnus-article-show-summary
4405   "s" gnus-article-show-summary
4406   "\C-c\C-m" gnus-article-mail
4407   "?" gnus-article-describe-briefly
4408   "<" beginning-of-buffer
4409   ">" end-of-buffer
4410   "\C-c\C-i" gnus-info-find-node
4411   "\C-c\C-b" gnus-bug
4412   "R" gnus-article-reply-with-original
4413   "F" gnus-article-followup-with-original
4414   "\C-hk" gnus-article-describe-key
4415   "\C-hc" gnus-article-describe-key-briefly
4416   "\C-hb" gnus-article-describe-bindings
4417
4418   "e" gnus-article-read-summary-keys
4419   "\C-d" gnus-article-read-summary-keys
4420   "\M-*" gnus-article-read-summary-keys
4421   "\M-#" gnus-article-read-summary-keys
4422   "\M-^" gnus-article-read-summary-keys
4423   "\M-g" gnus-article-read-summary-keys)
4424
4425 (substitute-key-definition
4426  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4427
4428 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4429   "W" gnus-article-wide-reply-with-original)
4430 (if (featurep 'xemacs)
4431     (set-keymap-default-binding gnus-article-send-map
4432                                 'gnus-article-read-summary-send-keys)
4433   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4434
4435 (defun gnus-article-make-menu-bar ()
4436   (unless (boundp 'gnus-article-commands-menu)
4437     (gnus-summary-make-menu-bar))
4438   (unless (boundp 'gnus-article-article-menu)
4439     (easy-menu-define
4440      gnus-article-article-menu gnus-article-mode-map ""
4441      '("Article"
4442        ["Scroll forwards" gnus-article-goto-next-page t]
4443        ["Scroll backwards" gnus-article-goto-prev-page t]
4444        ["Show summary" gnus-article-show-summary t]
4445        ["Fetch Message-ID at point" gnus-article-refer-article t]
4446        ["Mail to address at point" gnus-article-mail t]
4447        ["Send a bug report" gnus-bug t]))
4448
4449     (easy-menu-define
4450      gnus-article-treatment-menu gnus-article-mode-map ""
4451      ;; Fixme: this should use :active (and maybe :visible).
4452      '("Treatment"
4453        ["Hide headers" gnus-article-hide-headers t]
4454        ["Hide signature" gnus-article-hide-signature t]
4455        ["Hide citation" gnus-article-hide-citation t]
4456        ["Treat overstrike" gnus-article-treat-overstrike t]
4457        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4458        ["Remove carriage return" gnus-article-remove-cr t]
4459        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4460        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4461        ["Remove base64" gnus-article-de-base64-unreadable t]
4462        ["Treat html" gnus-article-wash-html t]
4463        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4464        ["Decode HZ" gnus-article-decode-HZ t]))
4465
4466     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4467
4468     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4469
4470     (gnus-run-hooks 'gnus-article-menu-hook)))
4471
4472 (defvar bookmark-make-record-function)
4473 (defvar shr-put-image-function)
4474
4475 (define-derived-mode gnus-article-mode fundamental-mode "Article"
4476   "Major mode for displaying an article.
4477
4478 All normal editing commands are switched off.
4479
4480 The following commands are available in addition to all summary mode
4481 commands:
4482 \\<gnus-article-mode-map>
4483 \\[gnus-article-next-page]\t Scroll the article one page forwards
4484 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4485 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4486 \\[gnus-article-show-summary]\t Display the summary buffer
4487 \\[gnus-article-mail]\t Send a reply to the address near point
4488 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4489 \\[gnus-info-find-node]\t Go to the Gnus info node"
4490   (gnus-simplify-mode-line)
4491   (make-local-variable 'minor-mode-alist)
4492   (when (gnus-visual-p 'article-menu 'menu)
4493     (gnus-article-make-menu-bar)
4494     (when gnus-summary-tool-bar-map
4495       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4496   (gnus-update-format-specifications nil 'article-mode)
4497   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4498   (set (make-local-variable 'gnus-page-broken) nil)
4499   (make-local-variable 'gnus-article-current-summary)
4500   (make-local-variable 'gnus-article-mime-handles)
4501   (make-local-variable 'gnus-article-decoded-p)
4502   (make-local-variable 'gnus-article-mime-handle-alist)
4503   (make-local-variable 'gnus-article-wash-types)
4504   (make-local-variable 'gnus-article-image-alist)
4505   (make-local-variable 'gnus-article-charset)
4506   (make-local-variable 'gnus-article-ignored-charsets)
4507   (set (make-local-variable 'bookmark-make-record-function)
4508        'gnus-summary-bookmark-make-record)
4509   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4510   ;; face.
4511   (set (make-local-variable 'nobreak-char-display) nil)
4512   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4513   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4514   (setq cursor-in-non-selected-windows nil)
4515   (gnus-set-default-directory)
4516   (buffer-disable-undo)
4517   (setq buffer-read-only t
4518         show-trailing-whitespace nil)
4519   (mm-enable-multibyte))
4520
4521 (defun gnus-article-setup-buffer ()
4522   "Initialize the article buffer."
4523   (let* ((name (if gnus-single-article-buffer "*Article*"
4524                  (concat "*Article "
4525                          (gnus-group-decoded-name gnus-newsgroup-name)
4526                          "*")))
4527          (original
4528           (progn (string-match "\\*Article" name)
4529                  (concat " *Original Article"
4530                          (substring name (match-end 0))))))
4531     (setq gnus-article-buffer name)
4532     (setq gnus-original-article-buffer original)
4533     (setq gnus-article-mime-handle-alist nil)
4534     (with-current-buffer gnus-summary-buffer
4535       ;; This might be a variable local to the summary buffer.
4536       (unless gnus-single-article-buffer
4537         (setq gnus-article-buffer name)
4538         (setq gnus-original-article-buffer original)
4539         (gnus-set-global-variables)))
4540     (gnus-article-setup-highlight-words)
4541     ;; Init original article buffer.
4542     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4543       (mm-enable-multibyte)
4544       (setq major-mode 'gnus-original-article-mode)
4545       (make-local-variable 'gnus-original-article))
4546     (if (and (get-buffer name)
4547              (with-current-buffer name
4548                (if gnus-article-edit-mode
4549                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4550                        (progn
4551                          (set-buffer-modified-p nil)
4552                          (gnus-kill-buffer name)
4553                          (message "")
4554                          nil)
4555                      (error "Action aborted"))
4556                  t)))
4557         (let ((summary gnus-summary-buffer))
4558           (with-current-buffer name
4559             (set (make-local-variable 'gnus-article-edit-mode) nil)
4560             (gnus-article-stop-animations)
4561             (when gnus-article-mime-handles
4562               (mm-destroy-parts gnus-article-mime-handles)
4563               (setq gnus-article-mime-handles nil))
4564             ;; Set it to nil in article-buffer!
4565             (setq gnus-article-mime-handle-alist nil)
4566             (buffer-disable-undo)
4567             (setq buffer-read-only t)
4568             (unless (derived-mode-p 'gnus-article-mode)
4569               (gnus-article-mode))
4570             (set (make-local-variable 'gnus-summary-buffer) summary)
4571             (setq truncate-lines gnus-article-truncate-lines)
4572             (current-buffer)))
4573       (let ((summary gnus-summary-buffer))
4574         (with-current-buffer (gnus-get-buffer-create name)
4575           (gnus-article-mode)
4576           (setq truncate-lines gnus-article-truncate-lines)
4577           (set (make-local-variable 'gnus-summary-buffer) summary)
4578           (gnus-summary-set-local-parameters gnus-newsgroup-name)
4579           (when article-lapsed-timer
4580             (gnus-stop-date-timer))
4581           (when gnus-article-update-date-headers
4582             (gnus-start-date-timer gnus-article-update-date-headers))
4583           (current-buffer))))))
4584
4585 (defun gnus-article-stop-animations ()
4586   (dolist (timer (and (boundp 'timer-list)
4587                       timer-list))
4588     (when (eq (gnus-timer--function timer) 'image-animate-timeout)
4589       (cancel-timer timer))))
4590
4591 (defun gnus-stop-downloads ()
4592   (when (boundp 'url-queue)
4593     (set (intern "url-queue" obarray) nil)))
4594
4595 ;; Set article window start at LINE, where LINE is the number of lines
4596 ;; from the head of the article.
4597 (defun gnus-article-set-window-start (&optional line)
4598   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4599     (when article-window
4600       (set-window-start
4601        article-window
4602        (with-current-buffer gnus-article-buffer
4603          (goto-char (point-min))
4604          (if (not line)
4605              (point-min)
4606            (gnus-message 6 "Moved to bookmark")
4607            (search-forward "\n\n" nil t)
4608            (forward-line line)
4609            (point)))))))
4610
4611 (defun gnus-article-prepare (article &optional all-headers header)
4612   "Prepare ARTICLE in article mode buffer.
4613 ARTICLE should either be an article number or a Message-ID.
4614 If ARTICLE is an id, HEADER should be the article headers.
4615 If ALL-HEADERS is non-nil, no headers are hidden."
4616   (save-excursion
4617     ;; Make sure we start in a summary buffer.
4618     (unless (derived-mode-p 'gnus-summary-mode)
4619       (set-buffer gnus-summary-buffer))
4620     (setq gnus-summary-buffer (current-buffer))
4621     (let* ((gnus-article (if header (mail-header-number header) article))
4622            (summary-buffer (current-buffer))
4623            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4624            (group gnus-newsgroup-name)
4625            result)
4626       (save-excursion
4627         (gnus-article-setup-buffer)
4628         (set-buffer gnus-article-buffer)
4629         ;; Deactivate active regions.
4630         (when (and (boundp 'transient-mark-mode)
4631                    transient-mark-mode)
4632           (setq mark-active nil))
4633         (if (not (setq result (let ((inhibit-read-only t))
4634                                 (gnus-request-article-this-buffer
4635                                  article group))))
4636             ;; There is no such article.
4637             (save-excursion
4638               (when (and (numberp article)
4639                          (not (memq article gnus-newsgroup-sparse)))
4640                 (setq gnus-article-current
4641                       (cons gnus-newsgroup-name article))
4642                 (set-buffer gnus-summary-buffer)
4643                 (setq gnus-current-article article)
4644                 (if (and (memq article gnus-newsgroup-undownloaded)
4645                          (not (gnus-online (gnus-find-method-for-group
4646                                             gnus-newsgroup-name))))
4647                     (progn
4648                       (gnus-summary-set-agent-mark article)
4649                       (message "Message marked for downloading"))
4650                   (gnus-summary-mark-article article gnus-canceled-mark)
4651                   (unless (memq article gnus-newsgroup-sparse)
4652                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4653           (if (or (eq result 'pseudo)
4654                   (eq result 'nneething))
4655               (progn
4656                 (with-current-buffer summary-buffer
4657                   (push article gnus-newsgroup-history)
4658                   (setq gnus-last-article gnus-current-article
4659                         gnus-current-article 0
4660                         gnus-current-headers nil
4661                         gnus-article-current nil)
4662                   (if (eq result 'nneething)
4663                       (gnus-configure-windows 'summary)
4664                     (gnus-configure-windows 'article))
4665                   (gnus-set-global-variables))
4666                 (let ((gnus-article-mime-handle-alist-1
4667                        gnus-article-mime-handle-alist))
4668                   (gnus-set-mode-line 'article)))
4669             ;; The result from the `request' was an actual article -
4670             ;; or at least some text that is now displayed in the
4671             ;; article buffer.
4672             (when (and (numberp article)
4673                        (not (eq article gnus-current-article)))
4674               ;; Seems like a new article has been selected.
4675               ;; `gnus-current-article' must be an article number.
4676               (with-current-buffer summary-buffer
4677                 (push article gnus-newsgroup-history)
4678                 (setq gnus-last-article gnus-current-article
4679                       gnus-current-article article
4680                       gnus-current-headers
4681                       (gnus-summary-article-header gnus-current-article)
4682                       gnus-article-current
4683                       (cons gnus-newsgroup-name gnus-current-article))
4684                 (unless (vectorp gnus-current-headers)
4685                   (setq gnus-current-headers nil))
4686                 (gnus-summary-goto-subject gnus-current-article)
4687                 (when (gnus-summary-show-thread)
4688                   ;; If the summary buffer really was folded, the
4689                   ;; previous goto may not actually have gone to
4690                   ;; the right article, but the thread root instead.
4691                   ;; So we go again.
4692                   (gnus-summary-goto-subject gnus-current-article))
4693                 (gnus-run-hooks 'gnus-mark-article-hook)
4694                 (gnus-set-mode-line 'summary)
4695                 (when (gnus-visual-p 'article-highlight 'highlight)
4696                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4697                 ;; Set the global newsgroup variables here.
4698                 (gnus-set-global-variables)
4699                 (setq gnus-have-all-headers
4700                       (or all-headers gnus-show-all-headers))))
4701             (save-excursion
4702               (gnus-configure-windows 'article))
4703             (when (or (numberp article)
4704                       (stringp article))
4705               (gnus-article-prepare-display)
4706               ;; Do page break.
4707               (goto-char (point-min))
4708               (when gnus-break-pages
4709                 (gnus-narrow-to-page)))
4710             (let ((gnus-article-mime-handle-alist-1
4711                    gnus-article-mime-handle-alist))
4712               (gnus-set-mode-line 'article))
4713             (article-goto-body)
4714             (unless (bobp)
4715               (forward-line -1))
4716             (set-window-point (get-buffer-window (current-buffer)) (point))
4717             (gnus-configure-windows 'article)
4718             (gnus-run-hooks 'gnus-article-prepare-hook)
4719             t))))))
4720
4721 ;;;###autoload
4722 (defun gnus-article-prepare-display ()
4723   "Make the current buffer look like a nice article."
4724   ;; Hooks for getting information from the article.
4725   ;; This hook must be called before being narrowed.
4726   (let ((gnus-article-buffer (current-buffer))
4727         buffer-read-only
4728         (inhibit-read-only t))
4729     (unless (derived-mode-p 'gnus-article-mode)
4730       (gnus-article-mode))
4731     (setq buffer-read-only nil
4732           gnus-article-wash-types nil
4733           gnus-article-image-alist nil)
4734     (gnus-run-hooks 'gnus-tmp-internal-hook)
4735     (when gnus-display-mime-function
4736       (funcall gnus-display-mime-function))
4737     ;; Add attachment buttons to the header.
4738     (when gnus-mime-display-attachment-buttons-in-header
4739       (gnus-mime-buttonize-attachments-in-header))))
4740
4741 ;;;
4742 ;;; Gnus Sticky Article Mode
4743 ;;;
4744
4745 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4746   "Mode for sticky articles."
4747   ;; Release bindings that won't work.
4748   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4749                              gnus-sticky-article-mode-map)
4750   (substitute-key-definition 'gnus-article-refer-article 'undefined
4751                              gnus-sticky-article-mode-map)
4752   (dolist (k '("e" "h" "s" "F" "R"))
4753     (define-key gnus-sticky-article-mode-map k nil))
4754   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4755   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4756   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4757   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4758
4759 (defun gnus-sticky-article (arg)
4760   "Make the current article sticky.
4761 If a prefix ARG is given, ask for a name for this sticky article buffer."
4762   (interactive "P")
4763   (gnus-summary-show-thread)
4764   (gnus-summary-select-article nil nil 'pseudo)
4765   (let (new-art-buf-name)
4766     (gnus-eval-in-buffer-window gnus-article-buffer
4767       (setq new-art-buf-name
4768             (concat
4769              "*Sticky Article: "
4770              (if arg
4771                  (read-from-minibuffer "Sticky article buffer name: ")
4772                (gnus-with-article-headers
4773                  (gnus-article-goto-header "subject")
4774                  (setq new-art-buf-name
4775                        (buffer-substring-no-properties
4776                         (line-beginning-position) (line-end-position)))
4777                  (goto-char (point-min))
4778                  (gnus-article-goto-header "from")
4779                  (setq new-art-buf-name
4780                        (concat
4781                         new-art-buf-name ", "
4782                         (buffer-substring-no-properties
4783                          (line-beginning-position) (line-end-position))))
4784                  (goto-char (point-min))
4785                  (gnus-article-goto-header "date")
4786                  (setq new-art-buf-name
4787                        (concat
4788                         new-art-buf-name ", "
4789                         (buffer-substring-no-properties
4790                          (line-beginning-position) (line-end-position))))))
4791              "*"))
4792       (if (and (gnus-buffer-live-p new-art-buf-name)
4793                (with-current-buffer new-art-buf-name
4794                  (derived-mode-p 'gnus-sticky-article-mode)))
4795           (switch-to-buffer new-art-buf-name)
4796         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4797       (gnus-sticky-article-mode))
4798     (setq gnus-article-buffer new-art-buf-name))
4799   (gnus-summary-recenter)
4800   (gnus-summary-position-point))
4801
4802 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4803   "Kill the given sticky article BUFFER.
4804 If none is given, assume the current buffer and kill it if it has
4805 `gnus-sticky-article-mode'."
4806   (interactive)
4807   (unless buffer
4808     (setq buffer (current-buffer)))
4809   (with-current-buffer buffer
4810     (when (derived-mode-p 'gnus-sticky-article-mode)
4811       (gnus-kill-buffer buffer))))
4812
4813 (defun gnus-kill-sticky-article-buffers (arg)
4814   "Kill all sticky article buffers.
4815 If a prefix ARG is given, ask for confirmation."
4816   (interactive "P")
4817   (dolist (buf (gnus-buffers))
4818     (with-current-buffer buf
4819       (when (derived-mode-p 'gnus-sticky-article-mode)
4820         (if (not arg)
4821             (gnus-kill-buffer buf)
4822           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4823             (gnus-kill-buffer buf)))))))
4824
4825 ;;;
4826 ;;; Gnus MIME viewing functions
4827 ;;;
4828
4829 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4830   "Format of the MIME buttons.
4831
4832 Valid specifiers include:
4833 %t  The MIME type
4834 %T  MIME type, along with additional info
4835 %n  The `name' parameter
4836 %d  The description, if any
4837 %l  The length of the encoded part
4838 %p  The part identifier number
4839 %e  Dots if the part isn't displayed
4840
4841 General format specifiers can also be used.  See Info node
4842 `(gnus)Formatting Variables'.")
4843
4844 (defvar gnus-mime-button-line-format-alist
4845   '((?t gnus-tmp-type ?s)
4846     (?T gnus-tmp-type-long ?s)
4847     (?n gnus-tmp-name ?s)
4848     (?d gnus-tmp-description ?s)
4849     (?p gnus-tmp-id ?s)
4850     (?l gnus-tmp-length ?d)
4851     (?e gnus-tmp-dots ?s)))
4852
4853 (defvar gnus-mime-button-commands
4854   '((gnus-article-press-button "\r" "Toggle Display")
4855     (gnus-mime-view-part "v" "View Interactively...")
4856     (gnus-mime-view-part-as-type "t" "View As Type...")
4857     (gnus-mime-view-part-as-charset "C" "View As charset...")
4858     (gnus-mime-save-part "o" "Save...")
4859     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4860     (gnus-mime-replace-part "r" "Replace part")
4861     (gnus-mime-delete-part "d" "Delete part")
4862     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4863     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4864     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4865     (gnus-mime-view-part-externally "e" "View Externally")
4866     (gnus-mime-print-part "p" "Print")
4867     (gnus-mime-pipe-part "|" "Pipe To Command...")
4868     (gnus-mime-action-on-part "." "Take action on the part...")))
4869
4870 (defun gnus-article-mime-part-status ()
4871   (if gnus-article-mime-handle-alist-1
4872       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4873           " (1 part)"
4874         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4875     ""))
4876
4877 (defvar gnus-mime-button-map
4878   (let ((map (make-sparse-keymap)))
4879     (define-key map gnus-mouse-2 'gnus-article-push-button)
4880     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4881     (dolist (c gnus-mime-button-commands)
4882       (define-key map (cadr c) (car c)))
4883     map))
4884
4885 (easy-menu-define
4886   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4887   `("MIME Part"
4888     ,@(mapcar (lambda (c)
4889                 (vector (caddr c) (car c) :active t))
4890               gnus-mime-button-commands)))
4891
4892 (defvar gnus-url-button-commands
4893   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4894
4895 (defvar gnus-url-button-map
4896   (let ((map (make-sparse-keymap)))
4897     (dolist (c gnus-url-button-commands)
4898       (define-key map (cadr c) (car c)))
4899     map))
4900
4901 (easy-menu-define
4902   gnus-url-button-menu gnus-url-button-map "URL button menu."
4903   `("Url Button"
4904     ,@(mapcar (lambda (c)
4905                 (vector (caddr c) (car c) :active t))
4906               gnus-url-button-commands)))
4907
4908 (defmacro gnus-bind-safe-url-regexp (&rest body)
4909   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4910   `(let ((mm-w3m-safe-url-regexp
4911           (let ((group (if (and (derived-mode-p 'gnus-article-mode)
4912                                 (gnus-buffer-live-p
4913                                  gnus-article-current-summary))
4914                            (with-current-buffer gnus-article-current-summary
4915                              gnus-newsgroup-name)
4916                          gnus-newsgroup-name)))
4917             (if (cond ((not group)
4918                        ;; Maybe we're in a mml-preview buffer
4919                        ;; and no group is selected.
4920                        t)
4921                       ((stringp gnus-safe-html-newsgroups)
4922                        (string-match gnus-safe-html-newsgroups group))
4923                       ((consp gnus-safe-html-newsgroups)
4924                        (member group gnus-safe-html-newsgroups)))
4925                 nil
4926               mm-w3m-safe-url-regexp))))
4927      ,@body))
4928
4929 (defun gnus-mime-button-menu (event prefix)
4930  "Construct a context-sensitive menu of MIME commands."
4931  (interactive "e\nP")
4932  (save-window-excursion
4933    (let ((pos (event-start event)))
4934      (select-window (posn-window pos))
4935      (goto-char (posn-point pos))
4936      (gnus-article-check-buffer)
4937      (popup-menu gnus-mime-button-menu nil prefix))))
4938
4939 (defun gnus-mime-view-all-parts (&optional handles)
4940   "View all the MIME parts."
4941   (interactive)
4942   (with-current-buffer gnus-article-buffer
4943     (let ((handles (or handles gnus-article-mime-handles))
4944           (mail-parse-charset gnus-newsgroup-charset)
4945           (mail-parse-ignored-charsets
4946            (with-current-buffer gnus-summary-buffer
4947              gnus-newsgroup-ignored-charsets)))
4948       (when handles
4949         (mm-remove-parts handles)
4950         (goto-char (point-min))
4951         (or (search-forward "\n\n") (goto-char (point-max)))
4952         (let ((inhibit-read-only t))
4953           (delete-region (point) (point-max))
4954           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4955
4956 (defun gnus-article-jump-to-part (n)
4957   "Jump to MIME part N."
4958   (interactive "P")
4959   (let ((parts (with-current-buffer gnus-article-buffer
4960                  (length gnus-article-mime-handle-alist))))
4961     (when (zerop parts)
4962       (error "No such part"))
4963     (pop-to-buffer gnus-article-buffer)
4964     (or n
4965         (setq n (if (= parts 1)
4966                     1
4967                   (read-number (format "Jump to part (1..%s): " parts)))))
4968     (unless (and (integerp n) (<= n parts) (>= n 1))
4969       (setq n
4970             (progn
4971               (gnus-message 7 "Invalid part `%s', using %s instead."
4972                             n parts)
4973               parts)))
4974     (gnus-message 9 "Jumping to part %s." n)
4975     (cond ((>= gnus-auto-select-part 1)
4976            (while (and (<= n parts)
4977                        (not (gnus-article-goto-part n)))
4978              (setq n (1+ n))))
4979           ((< gnus-auto-select-part 0)
4980            (while (and (>= n 1)
4981                        (not (gnus-article-goto-part n)))
4982              (setq n (1- n))))
4983           (t
4984            (gnus-article-goto-part n)))))
4985
4986 (defvar gnus-mime-buttonized-part-id nil
4987   "ID of a mime part that should be buttonized.
4988 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4989
4990 (eval-when-compile
4991   (defsubst gnus-article-edit-part (handles &optional current-id)
4992     "Edit an article in order to delete a mime part.
4993 This function is exclusively used by `gnus-mime-save-part-and-strip'
4994 and `gnus-mime-delete-part', and not provided at run-time normally."
4995     (gnus-article-edit-article
4996      `(lambda ()
4997         (buffer-disable-undo)
4998         (let ((mail-parse-charset (or gnus-article-charset
4999                                       ',gnus-newsgroup-charset))
5000               (mail-parse-ignored-charsets
5001                (or gnus-article-ignored-charsets
5002                    ',gnus-newsgroup-ignored-charsets))
5003               (mbl mml-buffer-list))
5004           (setq mml-buffer-list nil)
5005           ;; A new text must be inserted before deleting existing ones
5006           ;; at the end so as not to move existing markers of which
5007           ;; the insertion type is t.
5008           (delete-region
5009            (point-min)
5010            (prog1
5011                (goto-char (point-max))
5012              (insert-buffer-substring gnus-original-article-buffer)))
5013           (mime-to-mml ',handles)
5014           (setq gnus-article-mime-handles nil)
5015           (let ((mbl1 mml-buffer-list))
5016             (setq mml-buffer-list mbl)
5017             (set (make-local-variable 'mml-buffer-list) mbl1))
5018           (gnus-make-local-hook 'kill-buffer-hook)
5019           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
5020      `(lambda (no-highlight)
5021         (let ((mail-parse-charset (or gnus-article-charset
5022                                       ',gnus-newsgroup-charset))
5023               (message-options message-options)
5024               (message-options-set-recipient)
5025               (mail-parse-ignored-charsets
5026                (or gnus-article-ignored-charsets
5027                    ',gnus-newsgroup-ignored-charsets)))
5028           (mml-to-mime)
5029           (mml-destroy-buffers)
5030           (remove-hook 'kill-buffer-hook
5031                        'mml-destroy-buffers t)
5032           (kill-local-variable 'mml-buffer-list))
5033         (gnus-summary-edit-article-done
5034          ,(or (mail-header-references gnus-current-headers) "")
5035          ,(gnus-group-read-only-p)
5036          ,gnus-summary-buffer no-highlight))
5037      t)
5038     ;; Force buttonizing this part.
5039     (let ((gnus-mime-buttonized-part-id current-id))
5040       (gnus-article-edit-done))
5041     (gnus-configure-windows 'article)
5042     (when (and current-id (integerp gnus-auto-select-part))
5043       (gnus-article-jump-to-part
5044        (min (max (+ current-id gnus-auto-select-part) 1)
5045             (with-current-buffer gnus-article-buffer
5046               (length gnus-article-mime-handle-alist)))))))
5047
5048 (defun gnus-mime-replace-part (file)
5049   "Replace MIME part under point with an external body."
5050   ;; Useful if file has already been saved to disk
5051   (interactive
5052    (list
5053     (read-file-name "Replace MIME part with file: "
5054                     (or mm-default-directory default-directory)
5055                     nil nil)))
5056   (gnus-mime-save-part-and-strip file))
5057
5058 (defun gnus-mime-save-part-and-strip (&optional file)
5059   "Save the MIME part under point then replace it with an external body.
5060 If FILE is given, use it for the external part."
5061   (interactive)
5062   (gnus-article-check-buffer)
5063   (when (gnus-group-read-only-p)
5064     (error "The current group does not support deleting of parts"))
5065   (when (mm-complicated-handles gnus-article-mime-handles)
5066     (error "\
5067 The current article has a complicated MIME structure, giving up..."))
5068   (let* ((data (get-text-property (point) 'gnus-data))
5069          (id (get-text-property (point) 'gnus-part))
5070          param
5071          (handles gnus-article-mime-handles))
5072     (unless file
5073       (setq file
5074             (and data (mm-save-part data "Delete MIME part and save to: "))))
5075     (when file
5076       (with-current-buffer (mm-handle-buffer data)
5077         (erase-buffer)
5078         (insert "Content-Type: " (mm-handle-media-type data))
5079         (mml-insert-parameter-string (cdr (mm-handle-type data))
5080                                      '(charset))
5081         ;; Add a filename for the sake of saving the part again.
5082         (mml-insert-parameter
5083          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5084         (insert "\n")
5085         (insert "Content-ID: " (message-make-message-id) "\n")
5086         (insert "Content-Transfer-Encoding: binary\n")
5087         (insert "\n"))
5088       (setcdr data
5089               (cdr (mm-make-handle nil
5090                                    `("message/external-body"
5091                                      (access-type . "LOCAL-FILE")
5092                                      (name . ,file)))))
5093       ;; (set-buffer gnus-summary-buffer)
5094       (gnus-article-edit-part handles id))))
5095
5096 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5097 ;; parts...>') but with stripping would be nice.
5098
5099 (defun gnus-mime-delete-part ()
5100   "Delete the MIME part under point.
5101 Replace it with some information about the removed part."
5102   (interactive)
5103   (gnus-article-check-buffer)
5104   (when (gnus-group-read-only-p)
5105     (error "The current group does not support deleting of parts"))
5106   (when (mm-complicated-handles gnus-article-mime-handles)
5107     (error "\
5108 The current article has a complicated MIME structure, giving up..."))
5109   (when (or gnus-expert-user
5110             (gnus-yes-or-no-p "\
5111 Deleting parts may malfunction or destroy the article; continue? "))
5112     (let* ((data (get-text-property (point) 'gnus-data))
5113            (id (get-text-property (point) 'gnus-part))
5114            (handles gnus-article-mime-handles)
5115            (description
5116             (let ((desc (mm-handle-description data)))
5117               (when desc
5118                 (mail-decode-encoded-word-string desc))))
5119            (filename (or (mm-handle-filename data) "(none)"))
5120            (type (mm-handle-media-type data)))
5121       (unless data
5122         (error "No MIME part under point"))
5123       (with-current-buffer (mm-handle-buffer data)
5124         (let ((bsize (buffer-size)))
5125           (erase-buffer)
5126           (insert
5127            (concat
5128             ",----\n"
5129             "| The following attachment has been deleted:\n"
5130             "|\n"
5131             "| Type:           " type "\n"
5132             "| Filename:       " filename "\n"
5133             "| Size (encoded): " (format "%s byte%s\n"
5134                                          bsize (if (= bsize 1)
5135                                                    ""
5136                                                  "s"))
5137             (when description
5138               (concat    "| Description:    " description "\n"))
5139             "`----\n"))
5140           (setcdr data
5141                   (cdr (mm-make-handle
5142                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5143                         (list "attachment")
5144                         (format "Deleted attachment (%s bytes)" bsize))))))
5145       ;; (set-buffer gnus-summary-buffer)
5146       (gnus-article-edit-part handles id))))
5147
5148 (defun gnus-mime-save-part ()
5149   "Save the MIME part under point."
5150   (interactive)
5151   (gnus-article-check-buffer)
5152   (let ((data (get-text-property (point) 'gnus-data)))
5153     (when data
5154       (mm-save-part data))))
5155
5156 (defun gnus-mime-pipe-part (&optional cmd)
5157   "Pipe the MIME part under point to a process.
5158 Use CMD as the process."
5159   (interactive)
5160   (gnus-article-check-buffer)
5161   (let ((data (get-text-property (point) 'gnus-data)))
5162     (when data
5163       (mm-pipe-part data cmd))))
5164
5165 (defun gnus-mime-view-part ()
5166   "Interactively choose a viewing method for the MIME part under point."
5167   (interactive)
5168   (gnus-article-check-buffer)
5169   (let ((data (get-text-property (point) 'gnus-data)))
5170     (when data
5171       (setq gnus-article-mime-handles
5172             (mm-merge-handles
5173              gnus-article-mime-handles (setq data (copy-sequence data))))
5174       (mm-interactively-view-part data))))
5175
5176 (defun gnus-mime-view-part-as-type-internal ()
5177   (gnus-article-check-buffer)
5178   (let* ((handle (get-text-property (point) 'gnus-data))
5179          (name (or
5180                 ;; Content-Type: foo/bar; name=...
5181                 (mail-content-type-get (mm-handle-type handle) 'name)
5182                 ;; Content-Disposition: attachment; filename=...
5183                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5184          (def-type (and name (mm-default-file-encoding name))))
5185     (or (and def-type (cons def-type 0))
5186         (and handle
5187              (equal (mm-handle-media-supertype handle) "text")
5188              '("text/plain" . 0))
5189         '("application/octet-stream" . 0))))
5190
5191 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5192   "Choose a MIME media type, and view the part as such.
5193 If non-nil, PRED is a predicate to use during completion to limit the
5194 available media-types."
5195   (interactive)
5196   (unless mime-type
5197     (setq mime-type
5198           (let ((default (gnus-mime-view-part-as-type-internal)))
5199             (gnus-completing-read
5200              "View as MIME type"
5201              (if pred
5202                  (gnus-remove-if-not pred (mailcap-mime-types))
5203                (mailcap-mime-types))
5204              nil nil nil
5205              (car default)))))
5206   (gnus-article-check-buffer)
5207   (let ((handle (get-text-property (point) 'gnus-data)))
5208     (when handle
5209       (when (equal (mm-handle-media-type handle) "message/external-body")
5210         (unless (mm-handle-cache handle)
5211           (mm-extern-cache-contents handle))
5212         (setq handle (mm-handle-cache handle)))
5213       (setq handle
5214             (mm-make-handle (mm-handle-buffer handle)
5215                             (cons mime-type (cdr (mm-handle-type handle)))
5216                             (mm-handle-encoding handle)
5217                             (mm-handle-undisplayer handle)
5218                             (mm-handle-disposition handle)
5219                             (mm-handle-description handle)
5220                             nil
5221                             (mm-handle-id handle)))
5222       (setq gnus-article-mime-handles
5223             (mm-merge-handles gnus-article-mime-handles handle))
5224       (when (mm-handle-displayed-p handle)
5225         (mm-remove-part handle))
5226       (gnus-mm-display-part handle))))
5227
5228 (defun gnus-mime-copy-part (&optional handle arg)
5229   "Put the MIME part under point into a new buffer.
5230 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5231 are decompressed."
5232   (interactive (list nil current-prefix-arg))
5233   (gnus-article-check-buffer)
5234   (unless handle
5235     (setq handle (get-text-property (point) 'gnus-data)))
5236   (when handle
5237     (let ((filename (mm-handle-filename handle))
5238           contents dont-decode charset coding-system)
5239       (mm-with-unibyte-buffer
5240         (mm-insert-part handle)
5241         (setq contents (or (condition-case nil
5242                                (mm-decompress-buffer filename nil 'sig)
5243                              (error
5244                               (setq dont-decode t)
5245                               nil))
5246                            (buffer-string))))
5247       (setq filename (cond (filename (file-name-nondirectory filename))
5248                            (dont-decode "*raw data*")
5249                            (t "*decoded*")))
5250       (cond
5251        (dont-decode)
5252        ((not arg)
5253         (unless (setq charset (mail-content-type-get
5254                                (mm-handle-type handle) 'charset))
5255           (unless (setq coding-system (mm-with-unibyte-buffer
5256                                         (insert contents)
5257                                         (mm-find-buffer-file-coding-system)))
5258             (setq charset gnus-newsgroup-charset))))
5259        ((numberp arg)
5260         (setq charset (or (cdr (assq arg
5261                                      gnus-summary-show-article-charset-alist))
5262                           (mm-read-coding-system "Charset: ")))))
5263       (switch-to-buffer (generate-new-buffer filename))
5264       (if (or coding-system
5265               (and charset
5266                    (setq coding-system (mm-charset-to-coding-system
5267                                         charset nil t))
5268                    (not (eq coding-system 'ascii))))
5269           (progn
5270             (mm-enable-multibyte)
5271             (insert (mm-decode-coding-string contents coding-system))
5272             (setq buffer-file-coding-system
5273                   (if (boundp 'last-coding-system-used)
5274                       (symbol-value 'last-coding-system-used)
5275                     coding-system)))
5276         (mm-disable-multibyte)
5277         (insert contents)
5278         (setq buffer-file-coding-system mm-binary-coding-system))
5279       ;; We do it this way to make `normal-mode' set the appropriate mode.
5280       (unwind-protect
5281           (progn
5282             (setq buffer-file-name (expand-file-name filename))
5283             (normal-mode))
5284         (setq buffer-file-name nil))
5285       (goto-char (point-min)))))
5286
5287 (defun gnus-mime-print-part (&optional handle filename)
5288   "Print the MIME part under point."
5289   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5290   (gnus-article-check-buffer)
5291   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5292          (contents (and handle (mm-get-part handle)))
5293          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5294          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5295     (when contents
5296         (if printer
5297             (unwind-protect
5298                 (progn
5299                   (mm-save-part-to-file handle file)
5300                   (call-process shell-file-name nil
5301                                 (generate-new-buffer " *mm*")
5302                                 nil
5303                                 shell-command-switch
5304                                 (mm-mailcap-command
5305                                  printer file (mm-handle-type handle))))
5306               (delete-file file))
5307           (with-temp-buffer
5308             (insert contents)
5309             (gnus-print-buffer))
5310           (ps-despool filename)))))
5311
5312 (defun gnus-mime-inline-part (&optional handle arg)
5313   "Insert the MIME part under point into the current buffer.
5314 Compressed files like .gz and .bz2 are decompressed."
5315   (interactive (list nil current-prefix-arg))
5316   (gnus-article-check-buffer)
5317   (let* ((inhibit-read-only t)
5318          (b (point))
5319          (btn ;; position where the MIME button exists
5320           (if handle
5321               (if (eq handle (get-text-property b 'gnus-data))
5322                   b
5323                 (article-goto-body)
5324                 (or (text-property-any (point) (point-max) 'gnus-data handle)
5325                     (text-property-any (point-min) (point) 'gnus-data handle)))
5326             (setq handle (get-text-property b 'gnus-data))
5327             b))
5328          start contents charset coding-system)
5329     (when handle
5330       (when (= b (prog1
5331                      btn
5332                    (setq start (next-single-property-change btn 'gnus-data
5333                                                             nil (point-max))
5334                          btn (previous-single-property-change start
5335                                                               'gnus-data))))
5336         (setq b btn))
5337       (if (and (not arg) (mm-handle-undisplayer handle))
5338           (mm-remove-part handle)
5339         (mm-with-unibyte-buffer
5340           (mm-insert-part handle)
5341           (setq contents
5342                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5343                     (buffer-string))))
5344         (cond
5345          ((not arg)
5346           (unless (setq charset (mail-content-type-get
5347                                  (mm-handle-type handle) 'charset))
5348             (unless (setq coding-system
5349                           (mm-with-unibyte-buffer
5350                             (insert contents)
5351                             (mm-find-buffer-file-coding-system)))
5352               (setq charset gnus-newsgroup-charset))))
5353          ((numberp arg)
5354           (if (mm-handle-undisplayer handle)
5355               (mm-remove-part handle))
5356           (setq charset
5357                 (or (cdr (assq arg
5358                                gnus-summary-show-article-charset-alist))
5359                     (mm-read-coding-system "Charset: "))))
5360          ((mm-handle-undisplayer handle)
5361           (mm-remove-part handle)))
5362         (goto-char start)
5363         (unless (bolp)
5364           ;; This is a header button.
5365           (forward-line 1))
5366         (mm-display-inline handle))
5367       ;; Toggle the button appearance between `[button]...' and `[button]'.
5368       (goto-char btn)
5369       (let ((displayed-p (mm-handle-displayed-p handle)))
5370         (gnus-insert-mime-button handle (get-text-property btn 'gnus-part)
5371                                  (list displayed-p))
5372         (if (featurep 'emacs)
5373             (delete-region
5374              (point)
5375              (next-single-property-change (point) 'gnus-data nil (point-max)))
5376           (let* ((end (next-single-property-change (point) 'gnus-data))
5377                  (annots (annotations-at (or end (point-max)))))
5378             (delete-region (point)
5379                            (if end
5380                                (if annots (1+ end) end)
5381                              (point-max)))
5382             (dolist (annot annots)
5383               (set-extent-endpoints annot (point) (point)))))
5384         (setq start (point))
5385         (if (search-backward "\n\n" nil t)
5386             (progn
5387               (goto-char start)
5388               (unless (or displayed-p (eolp))
5389                 ;; Add extra newline.
5390                 (insert (propertize (buffer-substring (1- start) start)
5391                                     'gnus-undeletable t))))
5392           ;; We're in the article header.
5393           (delete-char -1)
5394           (dolist (ovl (gnus-overlays-in btn (point)))
5395             (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
5396             (gnus-overlay-put ovl 'face nil))
5397           (save-restriction
5398             (message-narrow-to-field)
5399             (let ((gnus-treatment-function-alist
5400                    '((gnus-treat-highlight-headers
5401                       gnus-article-highlight-headers))))
5402               (gnus-treat-article 'head)))))
5403       (goto-char b))))
5404
5405 (defun gnus-mime-set-charset-parameters (handle charset)
5406   "Set CHARSET to parameters in HANDLE.
5407 CHARSET may either be a string or a symbol."
5408   (unless (stringp charset)
5409     (setq charset (symbol-name charset)))
5410   (if (stringp (car handle))
5411       (dolist (h (cdr handle))
5412         (gnus-mime-set-charset-parameters h charset))
5413     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5414                                             "message/external-body")
5415                                      (progn
5416                                        (unless (mm-handle-cache handle)
5417                                          (mm-extern-cache-contents handle))
5418                                        (mm-handle-cache handle))
5419                                    handle)))
5420            (param (assq 'charset (cdr type))))
5421       (if param
5422           (setcdr param charset)
5423         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5424
5425 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5426   "Insert the MIME part under point into the current buffer using the
5427 specified charset."
5428   (interactive (list nil current-prefix-arg))
5429   (gnus-article-check-buffer)
5430   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5431         (fun (get-text-property (point) 'gnus-callback))
5432         (gnus-newsgroup-ignored-charsets 'gnus-all)
5433         charset form preferred parts)
5434     (when handle
5435       (when (prog1
5436                 (and fun
5437                      (setq charset
5438                            (or (cdr (assq
5439                                      arg
5440                                      gnus-summary-show-article-charset-alist))
5441                                (mm-read-coding-system "Charset: "))))
5442               (if (mm-handle-undisplayer handle)
5443                   (mm-remove-part handle)))
5444         (gnus-mime-set-charset-parameters handle charset)
5445         (when (and (consp (setq form (cdr-safe fun)))
5446                    (setq form (ignore-errors
5447                                 (assq 'gnus-mime-display-alternative form)))
5448                    (setq preferred (caddr form))
5449                    (progn
5450                      (when (eq (car preferred) 'quote)
5451                        (setq preferred (cadr preferred)))
5452                      (not (equal preferred
5453                                  (get-text-property (point) 'gnus-data))))
5454                    (setq parts (get-text-property (point) 'gnus-part))
5455                    (setq parts (cdr (assq parts
5456                                           gnus-article-mime-handle-alist)))
5457                    (equal (mm-handle-media-type parts) "multipart/alternative")
5458                    (setq parts (reverse (cdr parts))))
5459           (setcar (cddr form)
5460                   (list 'quote (or (cadr (member preferred parts))
5461                                    (car parts)))))
5462         (funcall fun handle)))))
5463
5464 (defun gnus-mime-view-part-externally (&optional handle)
5465   "View the MIME part under point with an external viewer."
5466   (interactive)
5467   (gnus-article-check-buffer)
5468   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5469          (mm-user-display-methods nil)
5470          (mm-inlined-types nil)
5471          (mail-parse-charset gnus-newsgroup-charset)
5472          (mail-parse-ignored-charsets
5473           (with-current-buffer gnus-summary-buffer
5474             gnus-newsgroup-ignored-charsets))
5475          (type (mm-handle-media-type handle))
5476          (method (mailcap-mime-info type))
5477          (mm-enable-external t))
5478     (if (not (stringp method))
5479         (gnus-mime-view-part-as-type
5480          nil (lambda (type) (stringp (mailcap-mime-info type))))
5481       (when handle
5482         (mm-display-part handle nil t)))))
5483
5484 (defun gnus-mime-view-part-internally (&optional handle)
5485   "View the MIME part under point with an internal viewer.
5486 If no internal viewer is available, use an external viewer."
5487   (interactive)
5488   (gnus-article-check-buffer)
5489   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5490          (mm-inlined-types '(".*"))
5491          (mm-inline-large-images t)
5492          (mail-parse-charset gnus-newsgroup-charset)
5493          (mail-parse-ignored-charsets
5494           (with-current-buffer gnus-summary-buffer
5495             gnus-newsgroup-ignored-charsets))
5496          (inhibit-read-only t))
5497     (if (not (mm-inlinable-p handle))
5498         (gnus-mime-view-part-as-type
5499          nil (lambda (type) (mm-inlinable-p handle type)))
5500       (when handle
5501         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5502
5503 (defun gnus-mime-action-on-part (&optional action)
5504   "Do something with the MIME attachment at \(point\)."
5505   (interactive
5506    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5507   (gnus-article-check-buffer)
5508   (let ((action-pair (assoc action gnus-mime-action-alist)))
5509     (if action-pair
5510         (funcall (cdr action-pair)))))
5511
5512 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5513   "Call FUNCTION on MIME part N.
5514 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5515 If INTERACTIVE, call FUNCTION interactively."
5516   (let (window frame)
5517     ;; Check whether the article is displayed.
5518     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5519                  (setq window (get-buffer-window gnus-article-buffer t))
5520                  (frame-visible-p (setq frame (window-frame window))))
5521       (error "No article is displayed"))
5522     (with-current-buffer gnus-article-buffer
5523       ;; Check whether the article displays the right contents.
5524       (unless (with-current-buffer gnus-summary-buffer
5525                 (eq gnus-current-article (gnus-summary-article-number)))
5526         (error "You should select the right article first"))
5527       (if n
5528           (setq n (prefix-numeric-value n))
5529         (let ((pt (point)))
5530           (setq n (or (get-text-property pt 'gnus-part)
5531                       (and (not (bobp))
5532                            (get-text-property (1- pt) 'gnus-part))
5533                       (get-text-property (prog2
5534                                              (forward-line 1)
5535                                              (point)
5536                                            (goto-char pt))
5537                                          'gnus-part)
5538                       (get-text-property
5539                        (or (and (setq pt (previous-single-property-change
5540                                           pt 'gnus-part))
5541                                 (1- pt))
5542                            (next-single-property-change (point) 'gnus-part)
5543                            (point))
5544                        'gnus-part)
5545                       1))))
5546       ;; Check whether the specified part exists.
5547       (when (> n (length gnus-article-mime-handle-alist))
5548         (error "No such part")))
5549     (unless
5550         (progn
5551           ;; To select the window is needed so that the cursor
5552           ;; might be visible on the MIME button.
5553           (select-window (prog1
5554                              window
5555                            (setq window (selected-window))
5556                            ;; Article may be displayed in the other frame.
5557                            (gnus-select-frame-set-input-focus
5558                             (prog1
5559                                 frame
5560                               (setq frame (selected-frame))))))
5561           (when (gnus-article-goto-part n)
5562             ;; We point the cursor and the arrow at the MIME button
5563             ;; when the `function' prompt the user for something.
5564             (unless (and (pos-visible-in-window-p)
5565                          (> (count-lines (point) (window-end))
5566                             (/ (1- (window-height)) 3)))
5567               (recenter (/ (1- (window-height)) 3)))
5568             (let ((cursor-in-non-selected-windows t)
5569                   (overlay-arrow-string "=>")
5570                   (overlay-arrow-position (point-marker)))
5571               (unwind-protect
5572                   (cond
5573                    ((and no-handle interactive)
5574                     (call-interactively function))
5575                    (no-handle
5576                     (funcall function))
5577                    (interactive
5578                     (call-interactively
5579                      function (get-text-property (point) 'gnus-data)))
5580                    (t
5581                     (funcall function
5582                              (get-text-property (point) 'gnus-data))))
5583                 (set-marker overlay-arrow-position nil)
5584                 (unless gnus-auto-select-part
5585                   (gnus-select-frame-set-input-focus frame)
5586                   (select-window window))))
5587             t))
5588       (if gnus-inhibit-mime-unbuttonizing
5589           ;; This is the default though the program shouldn't reach here.
5590           (error "No such part")
5591         ;; The part which doesn't have the MIME button is selected.
5592         ;; So, we display all the buttons and redo it.
5593         (let ((gnus-inhibit-mime-unbuttonizing t))
5594           (gnus-summary-show-article)
5595           (gnus-article-part-wrapper n function no-handle))))))
5596
5597 (defun gnus-article-pipe-part (n)
5598   "Pipe MIME part N, which is the numerical prefix."
5599   (interactive "P")
5600   (gnus-article-part-wrapper n 'mm-pipe-part))
5601
5602 (defun gnus-article-save-part (n)
5603   "Save MIME part N, which is the numerical prefix."
5604   (interactive "P")
5605   (gnus-article-part-wrapper n 'mm-save-part))
5606
5607 (defun gnus-article-interactively-view-part (n)
5608   "View MIME part N interactively, which is the numerical prefix."
5609   (interactive "P")
5610   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5611
5612 (defun gnus-article-copy-part (n)
5613   "Copy MIME part N, which is the numerical prefix."
5614   (interactive "P")
5615   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5616
5617 (defun gnus-article-view-part-as-charset (n)
5618   "View MIME part N using a specified charset.
5619 N is the numerical prefix."
5620   (interactive "P")
5621   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5622
5623 (defun gnus-article-view-part-externally (n)
5624   "View MIME part N externally, which is the numerical prefix."
5625   (interactive "P")
5626   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5627
5628 (defun gnus-article-inline-part (n)
5629   "Inline MIME part N, which is the numerical prefix."
5630   (interactive "P")
5631   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5632
5633 (defun gnus-article-save-part-and-strip (n)
5634   "Save MIME part N and replace it with an external body.
5635 N is the numerical prefix."
5636   (interactive "P")
5637   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5638
5639 (defun gnus-article-replace-part (n)
5640   "Replace MIME part N with an external body.
5641 N is the numerical prefix."
5642   (interactive "P")
5643   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5644
5645 (defun gnus-article-delete-part (n)
5646   "Delete MIME part N and add some information about the removed part.
5647 N is the numerical prefix."
5648   (interactive "P")
5649   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5650
5651 (defun gnus-article-view-part-as-type (n)
5652   "Choose a MIME media type, and view part N as such.
5653 N is the numerical prefix."
5654   (interactive "P")
5655   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5656
5657 (defun gnus-article-mime-match-handle-first (condition)
5658   (if condition
5659       (let (n)
5660         (dolist (ihandle gnus-article-mime-handle-alist)
5661           (if (and (cond
5662                     ((functionp condition)
5663                      (funcall condition (cdr ihandle)))
5664                     ((eq condition 'undisplayed)
5665                      (not (or (mm-handle-undisplayer (cdr ihandle))
5666                               (equal (mm-handle-media-type (cdr ihandle))
5667                                      "multipart/alternative"))))
5668                     ((eq condition 'undisplayed-alternative)
5669                      (not (mm-handle-undisplayer (cdr ihandle))))
5670                     (t t))
5671                    (gnus-article-goto-part (car ihandle))
5672                    (or (not n) (< (car ihandle) n)))
5673               (setq n (car ihandle))))
5674         (or n 1))
5675     1))
5676
5677 (defun gnus-article-view-part (&optional n)
5678   "View MIME part N, which is the numerical prefix.
5679 If the part is already shown, hide the part.  If N is nil, view
5680 all parts."
5681   (interactive "P")
5682   (with-current-buffer gnus-article-buffer
5683     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5684                              gnus-article-mime-match-handle-function)))
5685     (when (> n (length gnus-article-mime-handle-alist))
5686       (error "No such part"))
5687     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5688       (when (gnus-article-goto-part n)
5689         (if (equal (car handle) "multipart/alternative")
5690             (progn
5691               (beginning-of-line) ;; Make it toggle subparts
5692               (gnus-article-press-button))
5693           (when (eq (gnus-mm-display-part handle) 'internal)
5694             (gnus-set-window-start)))))))
5695
5696 (defsubst gnus-article-mime-total-parts ()
5697   (if (bufferp (car gnus-article-mime-handles))
5698       1 ;; single part
5699     (1- (length gnus-article-mime-handles))))
5700
5701 (defun gnus-mm-display-part (handle)
5702   "Display HANDLE and fix MIME button."
5703   (let ((id (get-text-property (point) 'gnus-part))
5704         (point (point))
5705         (inhibit-read-only t)
5706         (window (selected-window))
5707         (mail-parse-charset gnus-newsgroup-charset)
5708         (mail-parse-ignored-charsets
5709          (if (gnus-buffer-live-p gnus-summary-buffer)
5710              (with-current-buffer gnus-summary-buffer
5711                gnus-newsgroup-ignored-charsets)
5712            nil))
5713         start retval)
5714     (unwind-protect
5715         (progn
5716           (let ((win (gnus-get-buffer-window (current-buffer) t)))
5717             (when win
5718               (select-window win)
5719               (goto-char point)))
5720           (setq start (next-single-property-change point 'gnus-data
5721                                                    nil (point-max))
5722                 point (previous-single-property-change start 'gnus-data))
5723           (if (mm-handle-displayed-p handle)
5724               ;; This will remove the part.
5725               (setq retval (mm-display-part handle))
5726             (let ((part (or (and (mm-inlinable-p handle)
5727                                  (mm-inlined-p handle)
5728                                  t)
5729                             (with-temp-buffer
5730                               (gnus-bind-safe-url-regexp
5731                                (setq retval (mm-display-part handle)))
5732                               (unless (zerop (buffer-size))
5733                                 (buffer-string))))))
5734               (goto-char start)
5735               (unless (bolp)
5736                 ;; This is a header button.
5737                 (forward-line 1))
5738               (cond ((stringp part)
5739                      (save-restriction
5740                        (narrow-to-region (point)
5741                                          (progn
5742                                            (insert part)
5743                                            (unless (bolp) (insert "\n"))
5744                                            (point)))
5745                        (gnus-treat-article nil id
5746                                            (gnus-article-mime-total-parts)
5747                                            (mm-handle-media-type handle))
5748                        (mm-handle-set-undisplayer
5749                         handle
5750                         `(lambda ()
5751                            (let ((inhibit-read-only t))
5752                              (delete-region ,(copy-marker (point-min) t)
5753                                             ,(point-max-marker)))))))
5754                     (part
5755                      (mm-display-inline handle))))))
5756       (goto-char point)
5757       ;; Toggle the button appearance between `[button]...' and `[button]'.
5758       (let ((displayed-p (mm-handle-displayed-p handle)))
5759         (gnus-insert-mime-button handle id (list displayed-p))
5760         (if (featurep 'emacs)
5761             (delete-region
5762              (point)
5763              (next-single-property-change (point) 'gnus-data nil (point-max)))
5764           (let* ((end (next-single-property-change (point) 'gnus-data))
5765                  (annots (annotations-at (or end (point-max)))))
5766             (delete-region (point)
5767                            (if end
5768                                (if annots (1+ end) end)
5769                              (point-max)))
5770             (dolist (annot annots)
5771               (set-extent-endpoints annot (point) (point)))))
5772         (setq start (point))
5773         (if (search-backward "\n\n" nil t)
5774             (progn
5775               (goto-char start)
5776               (unless (or displayed-p (eolp))
5777                 ;; Add extra newline.
5778                 (insert (propertize (buffer-substring (1- start) start)
5779                                     'gnus-undeletable t))))
5780           ;; We're in the article header.
5781           (delete-char -1)
5782           (dolist (ovl (gnus-overlays-in point (point)))
5783             (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
5784             (gnus-overlay-put ovl 'face nil))
5785           (save-restriction
5786             (message-narrow-to-field)
5787             (let ((gnus-treatment-function-alist
5788                    '((gnus-treat-highlight-headers
5789                       gnus-article-highlight-headers))))
5790               (gnus-treat-article 'head)))))
5791       (goto-char point)
5792       (if (window-live-p window)
5793           (select-window window)))
5794     retval))
5795
5796 (defun gnus-article-goto-part (n)
5797   "Go to MIME part N."
5798   (when gnus-break-pages
5799     (widen))
5800   (article-goto-body)
5801   (prog1
5802       (let ((start (or (text-property-any (point) (point-max) 'gnus-part n)
5803                        ;; There may be header buttons.
5804                        (text-property-any (point-min) (point) 'gnus-part n)))
5805             part handle end next handles)
5806         (when start
5807           (goto-char start)
5808           (if (setq handle (get-text-property start 'gnus-data))
5809               start
5810             ;; Go to the displayed subpart, assuming this is
5811             ;; multipart/alternative.
5812             (setq part start
5813                   end (point-at-eol))
5814             (while (and (not handle)
5815                         part
5816                         (< part end)
5817                         (setq next (text-property-not-all part end
5818                                                           'gnus-data nil)))
5819               (setq part next
5820                     handle (get-text-property part 'gnus-data))
5821               (push (cons handle part) handles)
5822               (unless (mm-handle-displayed-p handle)
5823                 (setq handle nil
5824                       part (text-property-any part end 'gnus-data nil))))
5825             (unless handle
5826               ;; No subpart is displayed, so we find preferred one.
5827               (setq part
5828                     (cdr (assq (mm-preferred-alternative
5829                                 (nreverse (mapcar 'car handles)))
5830                                handles))))
5831             (if part
5832                 (goto-char (1+ part))
5833               start))))
5834     (when gnus-break-pages
5835       (gnus-narrow-to-page))))
5836
5837 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5838   (let ((gnus-tmp-name
5839          (or (mm-handle-filename handle)
5840              (mail-content-type-get (mm-handle-type handle) 'url)
5841              ""))
5842         (gnus-tmp-type (mm-handle-media-type handle))
5843         (gnus-tmp-description (or (mm-handle-description handle) ""))
5844         (gnus-tmp-dots
5845          (if (if displayed (car displayed)
5846                (mm-handle-displayed-p handle))
5847              "" "..."))
5848         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5849                            (buffer-size)))
5850         gnus-tmp-type-long b e)
5851     (when (string-match ".*/" gnus-tmp-name)
5852       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5853     (setq gnus-tmp-type-long (concat gnus-tmp-type
5854                                      (and (not (equal gnus-tmp-name ""))
5855                                           (concat "; " gnus-tmp-name))))
5856     (unless (equal gnus-tmp-description "")
5857       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5858     (setq b (point))
5859     (gnus-eval-format
5860      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5861      `(keymap ,gnus-mime-button-map
5862               gnus-callback gnus-mm-display-part
5863               gnus-part ,gnus-tmp-id
5864               article-type annotation
5865               gnus-data ,handle
5866               rear-nonsticky t))
5867     (setq e (if (bolp)
5868                 ;; Exclude a newline.
5869                 (1- (point))
5870               (point)))
5871     (when gnus-article-button-face
5872       (gnus-overlay-put (gnus-make-overlay b e nil t)
5873                         'face gnus-article-button-face))
5874     (widget-convert-button
5875      'link b e
5876      :mime-handle handle
5877      :action 'gnus-widget-press-button
5878      :button-keymap gnus-mime-button-map
5879      :help-echo
5880      (lambda (widget)
5881        ;; Needed to properly clear the message due to a bug in
5882        ;; wid-edit (XEmacs only).
5883        (if (boundp 'help-echo-owns-message)
5884            (setq help-echo-owns-message t))
5885        (format
5886         "%S: %s the MIME part; %S: more options"
5887         (aref gnus-mouse-2 0)
5888         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5889             "hide" "show")
5890         (aref gnus-down-mouse-3 0))))))
5891
5892 (defun gnus-widget-press-button (elems el)
5893   (goto-char (widget-get elems :from))
5894   (gnus-article-press-button))
5895
5896 (defvar gnus-displaying-mime nil)
5897
5898 (defun gnus-display-mime (&optional ihandles)
5899   "Display the MIME parts."
5900   (save-excursion
5901     (save-selected-window
5902       (let ((window (get-buffer-window gnus-article-buffer))
5903             (point (point)))
5904         (when window
5905           (select-window window)
5906           ;; We have to do this since selecting the window
5907           ;; may change the point.  So we set the window point.
5908           (set-window-point window point)))
5909       (let ((handles ihandles)
5910             (inhibit-read-only t)
5911             handle)
5912         (cond (handles)
5913               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5914                (when gnus-article-emulate-mime
5915                  (mm-uu-dissect-text-parts handles)))
5916               (gnus-article-emulate-mime
5917                (setq handles (mm-uu-dissect))))
5918         (when (and (not ihandles)
5919                    (not gnus-displaying-mime))
5920           ;; Top-level call; we clean up.
5921           (when gnus-article-mime-handles
5922             (mm-destroy-parts gnus-article-mime-handles)
5923             (setq gnus-article-mime-handle-alist nil));; A trick.
5924           (setq gnus-article-mime-handles handles)
5925           ;; We allow users to glean info from the handles.
5926           (when gnus-article-mime-part-function
5927             (gnus-mime-part-function handles)))
5928         (if (and handles
5929                  (or (not (stringp (car handles)))
5930                      (cdr handles)))
5931             (progn
5932               (when (and (not ihandles)
5933                          (not gnus-displaying-mime))
5934                 ;; Clean up for mime parts.
5935                 (article-goto-body)
5936                 (delete-region (point) (point-max)))
5937               (let ((gnus-displaying-mime t))
5938                 (gnus-mime-display-part handles)))
5939           (save-restriction
5940             (article-goto-body)
5941             (narrow-to-region (point) (point-max))
5942             (gnus-treat-article nil 1 1 "text/plain")
5943             (widen)))
5944         (unless ihandles
5945           ;; Highlight the headers.
5946           (save-excursion
5947             (save-restriction
5948               (article-goto-body)
5949               (narrow-to-region (point-min) (point))
5950               (gnus-article-save-original-date
5951                (gnus-treat-article 'head)))))))
5952     ;; Cope with broken MIME messages.
5953     (goto-char (point-max))
5954     (unless (bolp)
5955       (insert "\n"))))
5956
5957 (defcustom gnus-mime-display-multipart-as-mixed nil
5958   "Display \"multipart\" parts as  \"multipart/mixed\".
5959
5960 If t, it overrides nil values of
5961 `gnus-mime-display-multipart-alternative-as-mixed' and
5962 `gnus-mime-display-multipart-related-as-mixed'."
5963   :group 'gnus-article-mime
5964   :type 'boolean)
5965
5966 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5967   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5968   :version "22.1"
5969   :group 'gnus-article-mime
5970   :type 'boolean)
5971
5972 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5973   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5974
5975 If displaying \"text/html\" is discouraged \(see
5976 `mm-discouraged-alternatives'\) images or other material inside a
5977 \"multipart/related\" part might be overlooked when this variable is nil."
5978   :version "22.1"
5979   :group 'gnus-article-mime
5980   :type 'boolean)
5981
5982 (defcustom gnus-mime-display-attachment-buttons-in-header t
5983   "Add attachment buttons in the end of the header of an article.
5984 Since MIME attachments tend to be put at the end of an article, we may
5985 overlook them if there is a huge body.  This option offers you a copy
5986 of all non-inlinable MIME parts as buttons shown in front of an article.
5987 If nil, don't show those extra buttons."
5988   :version "25.1"
5989   :group 'gnus-article-mime
5990   :type 'boolean)
5991
5992 (defun gnus-mime-display-part (handle)
5993   (cond
5994    ;; Maybe a broken MIME message.
5995    ((null handle))
5996    ;; Single part.
5997    ((not (stringp (car handle)))
5998     (gnus-mime-display-single handle))
5999    ;; User-defined multipart
6000    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
6001     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
6002              handle))
6003    ;; multipart/alternative
6004    ((and (equal (car handle) "multipart/alternative")
6005          (not (or gnus-mime-display-multipart-as-mixed
6006                   gnus-mime-display-multipart-alternative-as-mixed)))
6007     (let ((id (1+ (length gnus-article-mime-handle-alist))))
6008       (push (cons id handle) gnus-article-mime-handle-alist)
6009       (gnus-mime-display-alternative (cdr handle) nil nil id)))
6010    ;; multipart/related
6011    ((and (equal (car handle) "multipart/related")
6012          (not (or gnus-mime-display-multipart-as-mixed
6013                   gnus-mime-display-multipart-related-as-mixed)))
6014     (gnus-mime-display-part (cadr handle)))
6015    ((equal (car handle) "multipart/signed")
6016     (gnus-add-wash-type 'signed)
6017     (gnus-mime-display-security handle))
6018    ((equal (car handle) "multipart/encrypted")
6019     (gnus-add-wash-type 'encrypted)
6020     (gnus-mime-display-security handle))
6021    ;; Other multiparts are handled like multipart/mixed.
6022    (t
6023     (gnus-mime-display-mixed (cdr handle)))))
6024
6025 (defun gnus-mime-part-function (handles)
6026   (if (stringp (car handles))
6027       (mapcar 'gnus-mime-part-function (cdr handles))
6028     (funcall gnus-article-mime-part-function handles)))
6029
6030 (defun gnus-mime-display-mixed (handles)
6031   (mapcar 'gnus-mime-display-part handles))
6032
6033 (defun gnus-mime-display-single (handle)
6034   (let ((type (mm-handle-media-type handle))
6035         (ignored gnus-ignored-mime-types)
6036         (not-attachment t)
6037         display text)
6038     (catch 'ignored
6039       (progn
6040         (while ignored
6041           (when (string-match (pop ignored) type)
6042             (throw 'ignored nil)))
6043         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
6044                                (with-current-buffer gnus-summary-buffer
6045                                  gnus-inhibit-images)
6046                              gnus-inhibit-images)
6047                            (string-match "\\`image/" type)))
6048                  (setq not-attachment
6049                        (and (not (mm-inline-override-p handle))
6050                             (or (not (mm-handle-disposition handle))
6051                                 (equal (car (mm-handle-disposition handle))
6052                                        "inline")
6053                                 (mm-attachment-override-p handle))))
6054                  (mm-automatic-display-p handle)
6055                  (or (and
6056                       (mm-inlinable-p handle)
6057                       (mm-inlined-p handle))
6058                      (mm-automatic-external-display-p type)))
6059             (setq display t)
6060           (when (equal (mm-handle-media-supertype handle) "text")
6061             (setq text t)))
6062         (let ((id (car (rassq handle gnus-article-mime-handle-alist)))
6063               beg)
6064           (unless id
6065             (setq id (1+ (length gnus-article-mime-handle-alist)))
6066             (push (cons id handle) gnus-article-mime-handle-alist))
6067           (when (and display
6068                      (equal (mm-handle-media-supertype handle) "message"))
6069             (insert-char
6070              ?\n
6071              (cond ((not (bolp)) 2)
6072                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
6073                    (t 1))))
6074           (when (or (not display)
6075                     (not (gnus-unbuttonized-mime-type-p type))
6076                     (eq id gnus-mime-buttonized-part-id))
6077             (gnus-insert-mime-button
6078              handle id (list (or display (and not-attachment text)))))
6079           (setq beg (point))
6080           (cond
6081            (display
6082             (let ((mail-parse-charset gnus-newsgroup-charset)
6083                   (mail-parse-ignored-charsets
6084                    (save-excursion (condition-case ()
6085                                        (set-buffer gnus-summary-buffer)
6086                                      (error))
6087                                    gnus-newsgroup-ignored-charsets)))
6088               (gnus-bind-safe-url-regexp (mm-display-part handle t))))
6089            ((and text not-attachment)
6090             (mm-display-inline handle)))
6091           (goto-char (point-max))
6092           (if (string-match "\\`image/" type)
6093               (gnus-article-insert-newline)
6094             (if (prog1
6095                     (= (skip-chars-backward "\n") -1)
6096                   (forward-char 1))
6097                 (gnus-article-insert-newline)
6098               (put-text-property (point) (point-max) 'gnus-undeletable t))
6099             (goto-char (point-max)))
6100           ;; Do highlighting.
6101           (save-excursion
6102             (save-restriction
6103               (narrow-to-region beg (point))
6104               (if (eq handle gnus-article-mime-handles)
6105                   ;; The format=flowed case.
6106                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
6107                 ;; Don't count signature parts that are never displayed.
6108                 ;; The part number should be re-calculated supposing this
6109                 ;; might be a message/rfc822 part.
6110                 (let (handles)
6111                   (dolist (part gnus-article-mime-handles)
6112                     (unless (or (stringp part)
6113                                 (equal (car (mm-handle-type part))
6114                                        "application/pgp-signature"))
6115                       (push part handles)))
6116                   (gnus-treat-article
6117                    nil (length (memq handle handles)) (length handles)
6118                    (mm-handle-media-type handle)))))))))))
6119
6120 (defun gnus-unbuttonized-mime-type-p (type)
6121   "Say whether TYPE is to be unbuttonized."
6122   (unless gnus-inhibit-mime-unbuttonizing
6123     (when (catch 'found
6124             (let ((types gnus-unbuttonized-mime-types))
6125               (while types
6126                 (when (string-match (pop types) type)
6127                   (throw 'found t)))))
6128       (not (catch 'found
6129              (let ((types gnus-buttonized-mime-types))
6130                (while types
6131                  (when (string-match (pop types) type)
6132                    (throw 'found t)))))))))
6133
6134 (defun gnus-article-insert-newline ()
6135   "Insert a newline, but mark it as undeletable."
6136   (gnus-put-text-property
6137    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
6138
6139 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
6140   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
6141          (ihandles handles)
6142          (point (point))
6143          handle (inhibit-read-only t) from props begend not-pref)
6144     (save-window-excursion
6145       (save-restriction
6146         (when ibegend
6147           (narrow-to-region (car ibegend)
6148                             (or (cdr ibegend)
6149                                 (progn
6150                                   (goto-char (car ibegend))
6151                                   (forward-line 2)
6152                                   (point))))
6153           (delete-region (point-min) (point-max))
6154           (mm-remove-parts handles))
6155         (setq begend (list (point-marker)))
6156         ;; Do the toggle.
6157         (unless (setq not-pref (cadr (member preferred ihandles)))
6158           (setq not-pref (car ihandles)))
6159         (when (or ibegend
6160                   (not preferred)
6161                   (not (gnus-unbuttonized-mime-type-p
6162                         "multipart/alternative")))
6163           (gnus-add-text-properties
6164            (setq from (point))
6165            (progn
6166              (insert (format "%d.  " id))
6167              (point))
6168            `(gnus-callback
6169              (lambda (handles)
6170                (unless ,(not ibegend)
6171                  (setq gnus-article-mime-handle-alist
6172                        ',gnus-article-mime-handle-alist))
6173                (gnus-mime-display-alternative
6174                 ',ihandles ',not-pref ',begend ,id))
6175              keymap ,gnus-mime-button-map
6176              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6177              face ,gnus-article-button-face
6178              gnus-part ,id
6179              article-type multipart
6180              rear-nonsticky t))
6181           (widget-convert-button 'link from (point)
6182                                  :action 'gnus-widget-press-button
6183                                  :button-keymap gnus-widget-button-keymap)
6184           ;; Do the handles
6185           (while (setq handle (pop handles))
6186             (gnus-add-text-properties
6187              (setq from (point))
6188              (progn
6189                (insert (format "(%c) %-18s"
6190                                (if (equal handle preferred) ?* ? )
6191                                (mm-handle-media-type handle)))
6192                (point))
6193              `(gnus-callback
6194                (lambda (handles)
6195                  (unless ,(not ibegend)
6196                    (setq gnus-article-mime-handle-alist
6197                          ',gnus-article-mime-handle-alist))
6198                  (gnus-mime-display-alternative
6199                   ',ihandles ',handle ',begend ,id))
6200                keymap ,gnus-mime-button-map
6201                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6202                face ,gnus-article-button-face
6203                gnus-part ,id
6204                gnus-data ,handle
6205                rear-nonsticky t))
6206             (widget-convert-button 'link from (point)
6207                                    :action 'gnus-widget-press-button
6208                                    :button-keymap gnus-widget-button-keymap)
6209             (insert "  "))
6210           (insert "\n\n"))
6211         (when preferred
6212           (if (stringp (car preferred))
6213               (gnus-display-mime preferred)
6214             (let ((mail-parse-charset gnus-newsgroup-charset)
6215                   (mail-parse-ignored-charsets
6216                    (with-current-buffer gnus-summary-buffer
6217                      gnus-newsgroup-ignored-charsets)))
6218               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6219               ;; Do highlighting.
6220               (save-excursion
6221                 (save-restriction
6222                   (narrow-to-region (car begend) (point-max))
6223                   (gnus-treat-article
6224                    nil (length gnus-article-mime-handle-alist)
6225                    (gnus-article-mime-total-parts)
6226                    (mm-handle-media-type preferred))))))
6227           (goto-char (point-max))
6228           (setcdr begend (point-marker)))))
6229     (when ibegend
6230       (goto-char point)))
6231   ;; Redraw attachment buttons in the header.
6232   (when gnus-mime-display-attachment-buttons-in-header
6233     (gnus-mime-buttonize-attachments-in-header)))
6234
6235 (defconst gnus-article-wash-status-strings
6236   (let ((alist '((cite "c" "Possible hidden citation text"
6237                        " " "All citation text visible")
6238                  (headers "h" "Hidden headers"
6239                           " " "All headers visible.")
6240                  (pgp "p" "Encrypted or signed message status hidden"
6241                       " " "No hidden encryption nor digital signature status")
6242                  (signature "s" "Signature has been hidden"
6243                             " " "Signature is visible")
6244                  (overstrike "o" "Overstrike (^H) characters applied"
6245                              " " "No overstrike characters applied")
6246                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6247                            " " "No /*_emphasis_*/ characters applied")))
6248         result)
6249     (dolist (entry alist result)
6250       (let ((key (nth 0 entry))
6251             (on (copy-sequence (nth 1 entry)))
6252             (on-help (nth 2 entry))
6253             (off (copy-sequence (nth 3 entry)))
6254             (off-help (nth 4 entry)))
6255         (put-text-property 0 1 'help-echo on-help on)
6256         (put-text-property 0 1 'help-echo off-help off)
6257         (push (list key on off) result))))
6258   "Alist of strings describing wash status in the mode line.
6259 Each entry has the form (KEY ON OF), where the KEY is a symbol
6260 representing the particular washing function, ON is the string to use
6261 in the article mode line when the washing function is active, and OFF
6262 is the string to use when it is inactive.")
6263
6264 (defun gnus-article-wash-status-entry (key value)
6265   (let ((entry (assoc key gnus-article-wash-status-strings)))
6266     (if value (nth 1 entry) (nth 2 entry))))
6267
6268 (defun gnus-article-wash-status ()
6269   "Return a string which display status of article washing."
6270   (with-current-buffer gnus-article-buffer
6271     (let ((cite (memq 'cite gnus-article-wash-types))
6272           (headers (memq 'headers gnus-article-wash-types))
6273           (boring (memq 'boring-headers gnus-article-wash-types))
6274           (pgp (memq 'pgp gnus-article-wash-types))
6275           (pem (memq 'pem gnus-article-wash-types))
6276           (signed (memq 'signed gnus-article-wash-types))
6277           (encrypted (memq 'encrypted gnus-article-wash-types))
6278           (signature (memq 'signature gnus-article-wash-types))
6279           (overstrike (memq 'overstrike gnus-article-wash-types))
6280           (emphasis (memq 'emphasis gnus-article-wash-types)))
6281       (concat
6282        (gnus-article-wash-status-entry 'cite cite)
6283        (gnus-article-wash-status-entry 'headers (or headers boring))
6284        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6285        (gnus-article-wash-status-entry 'signature signature)
6286        (gnus-article-wash-status-entry 'overstrike overstrike)
6287        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6288
6289 (defun gnus-add-wash-type (type)
6290   "Add a washing of TYPE to the current status."
6291   (add-to-list 'gnus-article-wash-types type))
6292
6293 (defun gnus-delete-wash-type (type)
6294   "Add a washing of TYPE to the current status."
6295   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6296
6297 (defun gnus-add-image (category image)
6298   "Add IMAGE of CATEGORY to the list of displayed images."
6299   (let ((entry (assq category gnus-article-image-alist)))
6300     (unless entry
6301       (setq entry (list category))
6302       (push entry gnus-article-image-alist))
6303     (nconc entry (list image))))
6304
6305 (defun gnus-delete-images (category)
6306   "Delete all images in CATEGORY."
6307   (let ((entry (assq category gnus-article-image-alist)))
6308     (dolist (image (cdr entry))
6309       (gnus-remove-image image category))
6310     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6311     (gnus-delete-wash-type category)))
6312
6313 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6314
6315 (defun gnus-article-maybe-hide-headers ()
6316   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6317 Provided for backwards compatibility."
6318   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6319                  (not (with-current-buffer gnus-summary-buffer
6320                         gnus-have-all-headers)))
6321              (not gnus-inhibit-hiding))
6322     (gnus-article-hide-headers)))
6323
6324 (declare-function shr-put-image "shr" (data alt &optional flags))
6325
6326 (defun gnus-shr-put-image (data alt &optional flags)
6327   "Put image DATA with a string ALT.  Enable image to be deleted."
6328   (let ((image (if flags
6329                    (shr-put-image data (propertize (or alt "*")
6330                                                    'gnus-image-category 'shr)
6331                                   flags)
6332                  ;; Old `shr-put-image' doesn't take the optional `flags'
6333                  ;; argument.
6334                  (shr-put-image data (propertize (or alt "*")
6335                                                  'gnus-image-category 'shr)))))
6336     (when image
6337       (gnus-add-image 'shr image))))
6338
6339 (defun gnus-mime-buttonize-attachments-in-header (&optional interactive)
6340   "Show attachments as buttons in the end of the header of an article.
6341 This function toggles the display when called interactively.  Note that
6342 buttons to be added to the header are only the ones that aren't inlined
6343 in the body.  Use `gnus-header-face-alist' to highlight buttons."
6344   (interactive (list t))
6345   (gnus-with-article-buffer
6346     (gmm-labels
6347         ;; Function that returns a flattened version of
6348         ;; `gnus-article-mime-handle-alist'.
6349         ((flattened-alist
6350           (&optional alist id all)
6351           (if alist
6352               (let ((i 1) newid flat)
6353                 (dolist (handle alist flat)
6354                   (setq newid (append id (list i))
6355                         i (1+ i))
6356                   (if (stringp (car handle))
6357                       (setq flat (nconc flat (flattened-alist (cdr handle)
6358                                                               newid all)))
6359                     (delq (rassq handle all) all)
6360                     (setq flat (nconc flat (list (cons newid handle)))))))
6361             (let ((flat (list nil)))
6362               ;; Assume that elements of `gnus-article-mime-handle-alist'
6363               ;; are in the decreasing order, but unnumbered subsidiaries
6364               ;; in each element are in the increasing order.
6365               (dolist (handle (reverse gnus-article-mime-handle-alist))
6366                 (if (stringp (cadr handle))
6367                     (setq flat (nconc flat (flattened-alist (cddr handle)
6368                                                             (list (car handle))
6369                                                             flat)))
6370                   (delq (rassq (cdr handle) flat) flat)
6371                   (setq flat (nconc flat (list (cons (list (car handle))
6372                                                      (cdr handle)))))))
6373               (setq flat (cdr flat))
6374               (mapc (lambda (handle)
6375                       (if (cdar handle)
6376                           ;; This is a hidden (i.e. unnumbered) handle.
6377                           (progn
6378                             (setcar handle
6379                                     (1+ (caar gnus-article-mime-handle-alist)))
6380                             (push handle gnus-article-mime-handle-alist))
6381                         (setcar handle (caar handle))))
6382                     flat)
6383               flat))))
6384       (let ((case-fold-search t) buttons handle type st)
6385         (save-excursion
6386           (save-restriction
6387             (widen)
6388             (article-narrow-to-head)
6389             ;; Header buttons exist?
6390             (while (and (not buttons)
6391                         (re-search-forward "^attachments?:[\n ]+" nil t))
6392               (when (get-char-property (match-end 0)
6393                                        'gnus-button-attachment-extra)
6394                 (setq buttons (match-beginning 0))))
6395             (widen)
6396             (when buttons
6397               ;; Delete header buttons.
6398               (delete-region buttons (if (re-search-forward "^[^ ]" nil t)
6399                                          (match-beginning 0)
6400                                        (point-max))))
6401             (unless (and interactive buttons)
6402               ;; Find buttons.
6403               (setq buttons nil)
6404               (dolist (button (flattened-alist))
6405                 (setq handle (cdr button)
6406                       type (mm-handle-media-type handle))
6407                 (when (or (and (if (gnus-buffer-live-p gnus-summary-buffer)
6408                                    (with-current-buffer gnus-summary-buffer
6409                                      gnus-inhibit-images)
6410                                  gnus-inhibit-images)
6411                                (string-match "\\`image/" type))
6412                           (mm-inline-override-p handle)
6413                           (and (mm-handle-disposition handle)
6414                                (not (equal (car (mm-handle-disposition handle))
6415                                            "inline"))
6416                                (not (mm-attachment-override-p handle)))
6417                           (not (mm-automatic-display-p handle))
6418                           (not (or (and (mm-inlinable-p handle)
6419                                         (mm-inlined-p handle))
6420                                    (mm-automatic-external-display-p type))))
6421                   (push button buttons)))
6422               (when buttons
6423                 ;; Add header buttons.
6424                 (article-goto-body)
6425                 (forward-line -1)
6426                 (narrow-to-region (point) (point))
6427                 (insert "Attachment" (if (cdr buttons) "s" "") ":")
6428                 (dolist (button (nreverse buttons))
6429                   (setq st (point))
6430                   (insert " ")
6431                   (mm-handle-set-undisplayer
6432                    (setq handle (copy-sequence (cdr button))) nil)
6433                   (gnus-insert-mime-button handle (car button))
6434                   (skip-chars-backward "\t\n ")
6435                   (delete-region (point) (point-max))
6436                   (when (> (current-column) (window-width))
6437                     (goto-char st)
6438                     (insert "\n")
6439                     (end-of-line)))
6440                 (insert "\n")
6441                 (dolist (ovl (gnus-overlays-in (point-min) (point)))
6442                   (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
6443                   (gnus-overlay-put ovl 'face nil))
6444                 (let ((gnus-treatment-function-alist
6445                        '((gnus-treat-highlight-headers
6446                           gnus-article-highlight-headers))))
6447                   (gnus-treat-article 'head))))))))))
6448
6449 ;;; Article savers.
6450
6451 (defun gnus-output-to-file (file-name)
6452   "Append the current article to a file named FILE-NAME.
6453 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6454 text and used as the value of the coding cookie which is added to the
6455 top of a file.  Otherwise, this function saves a raw article without
6456 the coding cookie."
6457   (let* ((artbuf (current-buffer))
6458          (file-name-coding-system nnmail-pathname-coding-system)
6459          (coding gnus-article-save-coding-system)
6460          (coding-system-for-read (if coding
6461                                      nil ;; Rely on the coding cookie.
6462                                    mm-text-coding-system))
6463          (coding-system-for-write (or coding
6464                                       mm-text-coding-system-for-write
6465                                       mm-text-coding-system))
6466          (exists (file-exists-p file-name)))
6467     (with-temp-buffer
6468       (when exists
6469         (insert-file-contents file-name)
6470         (goto-char (point-min))
6471         ;; Remove the existing coding cookie.
6472         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6473           (delete-region (match-beginning 0) (match-end 0))))
6474       (goto-char (point-max))
6475       (insert-buffer-substring artbuf)
6476       ;; Append newline at end of the buffer as separator, and then
6477       ;; save it to file.
6478       (goto-char (point-max))
6479       (insert "\n")
6480       (when coding
6481         ;; If the coding system is not suitable to encode the text,
6482         ;; ask a user for a proper one.
6483         (when (fboundp 'select-safe-coding-system)
6484           (setq coding (coding-system-base
6485                         (save-window-excursion
6486                           (select-safe-coding-system (point-min) (point-max)
6487                                                      coding))))
6488           (setq coding-system-for-write
6489                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6490                     coding)))
6491         (goto-char (point-min))
6492         ;; Add the coding cookie.
6493         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6494                         coding-system-for-write)))
6495       (if exists
6496           (progn
6497             (write-region (point-min) (point-max) file-name nil 'no-message)
6498             (message "Appended to %s" file-name))
6499         (write-region (point-min) (point-max) file-name))))
6500   t)
6501
6502 (defun gnus-narrow-to-page (&optional arg)
6503   "Narrow the article buffer to a page.
6504 If given a numerical ARG, move forward ARG pages."
6505   (interactive "P")
6506   (setq arg (if arg (prefix-numeric-value arg) 0))
6507   (with-current-buffer gnus-article-buffer
6508     (widen)
6509     ;; Remove any old next/prev buttons.
6510     (when (gnus-visual-p 'page-marker)
6511       (let ((inhibit-read-only t))
6512         (gnus-remove-text-with-property 'gnus-prev)
6513         (gnus-remove-text-with-property 'gnus-next)))
6514     (let (st nd pt)
6515       (when (save-excursion
6516               (cond ((< arg 0)
6517                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6518                          (prog1
6519                              (setq nd (match-beginning 0)
6520                                    pt nd)
6521                            (when (re-search-backward page-delimiter nil t)
6522                              (setq st (match-end 0))))
6523                        (when (re-search-forward page-delimiter nil t)
6524                          (setq nd (match-beginning 0)
6525                                pt (point-min)))))
6526                     ((> arg 0)
6527                      (if (re-search-forward page-delimiter nil 'move arg)
6528                          (prog1
6529                              (setq st (match-end 0)
6530                                    pt st)
6531                            (when (re-search-forward page-delimiter nil t)
6532                              (setq nd (match-beginning 0))))
6533                        (when (re-search-backward page-delimiter nil t)
6534                          (setq st (match-end 0)
6535                                pt (point-max)))))
6536                     (t
6537                      (when (re-search-backward page-delimiter nil t)
6538                        (goto-char (setq st (match-end 0))))
6539                      (when (re-search-forward page-delimiter nil t)
6540                        (setq nd (match-beginning 0)))
6541                      (or st nd))))
6542         (setq gnus-page-broken t)
6543         (when pt (goto-char pt))
6544         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6545         (when (gnus-visual-p 'page-marker)
6546           (save-excursion
6547             (when nd
6548               (goto-char nd)
6549               (gnus-insert-next-page-button))
6550             (when st
6551               (goto-char st)
6552               (gnus-insert-prev-page-button))))))))
6553
6554 ;; Article mode commands
6555
6556 (defun gnus-article-goto-next-page ()
6557   "Show the next page of the article."
6558   (interactive)
6559   (when (gnus-article-next-page)
6560     (goto-char (point-min))
6561     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6562
6563
6564 (defun gnus-article-goto-prev-page ()
6565   "Show the previous page of the article."
6566   (interactive)
6567   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6568       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6569     (gnus-article-prev-page nil)))
6570
6571 ;; This is cleaner but currently breaks `gnus-pick-mode':
6572 ;;
6573 ;; (defun gnus-article-goto-next-page ()
6574 ;;   "Show the next page of the article."
6575 ;;   (interactive)
6576 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6577 ;;     (gnus-summary-next-page)))
6578 ;;
6579 ;; (defun gnus-article-goto-prev-page ()
6580 ;;   "Show the next page of the article."
6581 ;;   (interactive)
6582 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6583 ;;     (gnus-summary-prev-page)))
6584
6585 (defun gnus-article-next-page (&optional lines)
6586   "Show the next page of the current article.
6587 If end of article, return non-nil.  Otherwise return nil.
6588 Argument LINES specifies lines to be scrolled up."
6589   (interactive "p")
6590   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6591   (if (and (not (and gnus-article-over-scroll
6592                      (> (count-lines (window-start) (point-max))
6593                         (if (featurep 'xemacs)
6594                             (or lines (1- (window-height)))
6595                           (+ (or lines (1- (window-height))) scroll-margin)))))
6596            (save-excursion
6597              (end-of-line)
6598              (and (pos-visible-in-window-p)     ;Not continuation line.
6599                   (>= (point) (point-max)))))
6600       ;; Nothing in this page.
6601       (if (or (not gnus-page-broken)
6602               (save-excursion
6603                 (save-restriction
6604                   (widen)
6605                   (forward-line)
6606                   (eobp)))) ;Real end-of-buffer?
6607           (progn
6608             (when gnus-article-over-scroll
6609               (gnus-article-next-page-1 lines))
6610             t)                  ;Nothing more.
6611         (gnus-narrow-to-page 1)         ;Go to next page.
6612         nil)
6613     ;; More in this page.
6614     (gnus-article-next-page-1 lines)
6615     nil))
6616
6617 (defun gnus-article-beginning-of-window ()
6618   "Move point to the beginning of the window.
6619 In Emacs, the point is placed at the line number which `scroll-margin'
6620 specifies."
6621   (if (featurep 'xemacs)
6622       (move-to-window-line 0)
6623     ;; There is an obscure bug in Emacs that makes it impossible to
6624     ;; scroll past big pictures in the article buffer.  Try to fix
6625     ;; this by adding a sanity check by counting the lines visible.
6626     (when (> (count-lines (window-start) (window-end)) 30)
6627       (move-to-window-line
6628        (min (max 0 scroll-margin)
6629             (max 1 (- (window-height)
6630                       (if mode-line-format 1 0)
6631                       (if header-line-format 1 0)
6632                       2)))))))
6633
6634 (defun gnus-article-next-page-1 (lines)
6635   (condition-case ()
6636       (let ((scroll-in-place nil)
6637             (auto-window-vscroll nil))
6638         (scroll-up lines))
6639     (end-of-buffer
6640      ;; Long lines may cause an end-of-buffer error.
6641      (goto-char (point-max))))
6642   (gnus-article-beginning-of-window))
6643
6644 (defun gnus-article-prev-page (&optional lines)
6645   "Show previous page of current article.
6646 Argument LINES specifies lines to be scrolled down."
6647   (interactive "p")
6648   (move-to-window-line 0)
6649   (if (and gnus-page-broken
6650            (bobp)
6651            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6652       (progn
6653         (gnus-narrow-to-page -1)        ;Go to previous page.
6654         (goto-char (point-max))
6655         (recenter (if gnus-article-over-scroll
6656                       (if lines
6657                           (max (if (featurep 'xemacs)
6658                                    lines
6659                                  (+ lines scroll-margin))
6660                                3)
6661                         (- (window-height) 2))
6662                     -1)))
6663     (prog1
6664         (condition-case ()
6665             (let ((scroll-in-place nil))
6666               (scroll-down lines))
6667           (beginning-of-buffer
6668            (goto-char (point-min))))
6669       (gnus-article-beginning-of-window))))
6670
6671 (defun gnus-article-only-boring-p ()
6672   "Decide whether there is only boring text remaining in the article.
6673 Something \"interesting\" is a word of at least two letters that does
6674 not have a face in `gnus-article-boring-faces'."
6675   (when (and gnus-article-skip-boring
6676              (boundp 'gnus-article-boring-faces)
6677              (symbol-value 'gnus-article-boring-faces))
6678     (save-excursion
6679       (let ((inhibit-point-motion-hooks t))
6680         (catch 'only-boring
6681           (while (re-search-forward "\\b\\w\\w" nil t)
6682             (forward-char -1)
6683             (when (not (gnus-intersection
6684                         (gnus-faces-at (point))
6685                         (symbol-value 'gnus-article-boring-faces)))
6686               (throw 'only-boring nil)))
6687           (throw 'only-boring t))))))
6688
6689 (defun gnus-article-refer-article ()
6690   "Read article specified by message-id around point."
6691   (interactive)
6692   (save-excursion
6693     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6694     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6695     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6696         (let ((msg-id (concat "<" (match-string 0) ">")))
6697           (set-buffer gnus-summary-buffer)
6698           (gnus-summary-refer-article msg-id))
6699       (error "No references around point"))))
6700
6701 (defun gnus-article-show-summary ()
6702   "Reconfigure windows to show summary buffer."
6703   (interactive)
6704   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6705       (error "There is no summary buffer for this article buffer")
6706     (gnus-article-set-globals)
6707     (gnus-configure-windows 'article)
6708     (gnus-summary-goto-subject gnus-current-article)
6709     (gnus-summary-position-point)))
6710
6711 (defun gnus-article-describe-briefly ()
6712   "Describe article mode commands briefly."
6713   (interactive)
6714   (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")))
6715
6716 (defun gnus-article-check-buffer ()
6717   "Beep if not in an article buffer."
6718   (unless (derived-mode-p 'gnus-article-mode)
6719     (error "Command invoked outside of a Gnus article buffer")))
6720
6721 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6722   "Read a summary buffer key sequence and execute it from the article buffer."
6723   (interactive "P")
6724   (gnus-article-check-buffer)
6725   (let ((nosaves
6726          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6727            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6728            "=" "^" "\M-^" "|"))
6729         (nosave-but-article
6730          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6731            "An" "Ap" [?A (meta return)] [?A delete]))
6732         (nosave-in-article
6733          '("AS" "\C-d"))
6734         (up-to-top
6735          '("n" "Gn" "p" "Gp"))
6736         keys new-sum-point)
6737     (with-current-buffer gnus-article-current-summary
6738       (let (gnus-pick-mode)
6739         (setq unread-command-events (nconc unread-command-events
6740                                            (list (or key last-command-event)))
6741               keys (if (featurep 'xemacs)
6742                        (events-to-keys (read-key-sequence nil t))
6743                      (read-key-sequence nil t)))))
6744
6745     (message "")
6746
6747     (cond
6748      ((eq (aref keys (1- (length keys))) ?\C-h)
6749       (gnus-article-describe-bindings (substring keys 0 -1)))
6750      ((or (member keys nosaves)
6751           (member keys nosave-but-article)
6752           (member keys nosave-in-article))
6753       (let (func)
6754         (save-window-excursion
6755           (pop-to-buffer gnus-article-current-summary)
6756           ;; We disable the pick minor mode commands.
6757           (let (gnus-pick-mode)
6758             (setq func (key-binding keys t))))
6759         (if (or (not func)
6760                 (numberp func))
6761             (ding)
6762           (unless (member keys nosave-in-article)
6763             (set-buffer gnus-article-current-summary))
6764           (when (and (symbolp func)
6765                      (get func 'disabled))
6766             (error "Function %s disabled" func))
6767           (call-interactively func)
6768           (setq new-sum-point (point)))
6769         (when (member keys nosave-but-article)
6770           (pop-to-buffer gnus-article-buffer))))
6771      (t
6772       ;; These commands should restore window configuration.
6773       (let ((obuf (current-buffer))
6774             (owin (current-window-configuration))
6775             win func in-buffer selected new-sum-start new-sum-hscroll err)
6776         (cond (not-restore-window
6777                (pop-to-buffer gnus-article-current-summary)
6778                (setq win (selected-window)))
6779               ((setq win (get-buffer-window gnus-article-current-summary))
6780                (select-window win))
6781               (t
6782                (let ((summary-buffer gnus-article-current-summary))
6783                  (gnus-configure-windows 'article)
6784                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6785                    (let ((gnus-buffer-configuration
6786                           '((article ((vertical 1.0
6787                                                 (summary 0.25 point)
6788                                                 (article 1.0)))))))
6789                      (gnus-configure-windows 'article))
6790                    (setq win (get-buffer-window summary-buffer 'visible)))
6791                  (gnus-select-frame-set-input-focus (window-frame win))
6792                  (select-window win))))
6793         (setq in-buffer (current-buffer))
6794         ;; We disable the pick minor mode commands.
6795         (setq func (let (gnus-pick-mode)
6796                      (key-binding keys t)))
6797         (when (get func 'disabled)
6798           (error "Function %s disabled" func))
6799         (if (and func
6800                  (functionp func)
6801                  (condition-case code
6802                      (progn
6803                        (call-interactively func)
6804                        t)
6805                    (error
6806                     (setq err code)
6807                     nil)))
6808             (progn
6809               (when (eq win (selected-window))
6810                 (setq new-sum-point (point)
6811                       new-sum-start (window-start win)
6812                       new-sum-hscroll (window-hscroll win)))
6813               (when (or (eq in-buffer (current-buffer))
6814                         (when (eq obuf (current-buffer))
6815                           (set-buffer in-buffer)
6816                           t))
6817                 (setq selected (ignore-errors (gnus-summary-select-article)))
6818                 (set-buffer obuf)
6819                 (unless not-restore-window
6820                   (set-window-configuration owin))
6821                 (when (and (eq selected 'old)
6822                            new-sum-point)
6823                   (set-window-start (get-buffer-window (current-buffer))
6824                                     1)
6825                   (set-window-point (get-buffer-window (current-buffer))
6826                                     (if (article-goto-body)
6827                                         (1- (point))
6828                                       (point))))
6829                 (when (and (not not-restore-window)
6830                            new-sum-point
6831                            (window-live-p win)
6832                            (with-current-buffer (window-buffer win)
6833                              (derived-mode-p 'gnus-summary-mode)))
6834                   (set-window-point win new-sum-point)
6835                   (set-window-start win new-sum-start)
6836                   (set-window-hscroll win new-sum-hscroll))))
6837           (set-window-configuration owin)
6838           (if err
6839               (signal (car err) (cdr err))
6840             (ding))))))))
6841
6842 (defun gnus-article-read-summary-send-keys ()
6843   (interactive)
6844   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6845     (gnus-article-read-summary-keys)))
6846
6847 (defun gnus-article-describe-key (key)
6848   "Display documentation of the function invoked by KEY.
6849 KEY is a string or a vector."
6850   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6851                        (read-key-sequence "Describe key: "))))
6852   (gnus-article-check-buffer)
6853   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6854                                   gnus-article-read-summary-send-keys))
6855       (with-current-buffer gnus-article-current-summary
6856         (setq unread-command-events
6857               (if (featurep 'xemacs)
6858                   (append key nil)
6859                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6860                                         (list 'meta (- x 128))
6861                                       x))
6862                         key)))
6863         (let ((cursor-in-echo-area t)
6864               gnus-pick-mode)
6865           (describe-key (read-key-sequence nil t))))
6866     (describe-key key)))
6867
6868 (defun gnus-article-describe-key-briefly (key &optional insert)
6869   "Display documentation of the function invoked by KEY.
6870 KEY is a string or a vector."
6871   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6872                        (read-key-sequence "Describe key: "))
6873                      current-prefix-arg))
6874   (gnus-article-check-buffer)
6875   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6876                                   gnus-article-read-summary-send-keys))
6877       (with-current-buffer gnus-article-current-summary
6878         (setq unread-command-events
6879               (if (featurep 'xemacs)
6880                   (append key nil)
6881                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6882                                         (list 'meta (- x 128))
6883                                       x))
6884                         key)))
6885         (let ((cursor-in-echo-area t)
6886               gnus-pick-mode)
6887           (describe-key-briefly (read-key-sequence nil t) insert)))
6888     (describe-key-briefly key insert)))
6889
6890 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6891 (defvar gnus-agent-summary-mode)
6892 (defvar gnus-draft-mode)
6893 (defvar help-xref-stack-item)
6894
6895 (defun gnus-article-describe-bindings (&optional prefix)
6896   "Show a list of all defined keys, and their definitions.
6897 The optional argument PREFIX, if non-nil, should be a key sequence;
6898 then we display only bindings that start with that prefix."
6899   (interactive)
6900   (gnus-article-check-buffer)
6901   (let ((keymap (copy-keymap gnus-article-mode-map))
6902         (map (copy-keymap gnus-article-send-map))
6903         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6904         parent agent draft)
6905     (define-key keymap "S" map)
6906     (define-key map [t] nil)
6907     (with-current-buffer gnus-article-current-summary
6908       (set-keymap-parent
6909        keymap
6910        (if (setq parent (keymap-parent gnus-article-mode-map))
6911            (prog1
6912                (setq parent (copy-keymap parent))
6913              (set-keymap-parent parent (current-local-map)))
6914          (current-local-map)))
6915       (set-keymap-parent map (key-binding "S"))
6916       (let (key def gnus-pick-mode)
6917         (while sumkeys
6918           (setq key (pop sumkeys))
6919           (cond ((and (vectorp key) (= (length key) 1)
6920                       (consp (setq def (aref key 0)))
6921                       (numberp (car def)) (numberp (cdr def)))
6922                  (when (< (max (car def) (cdr def)) 128)
6923                    (setq sumkeys
6924                          (append (mapcar
6925                                   #'vector
6926                                   (nreverse (gnus-uncompress-range def)))
6927                                  sumkeys))))
6928                 ((setq def (key-binding key))
6929                  (unless (eq def 'undefined)
6930                    (define-key keymap key def))))))
6931       (when (boundp 'gnus-agent-summary-mode)
6932         (setq agent gnus-agent-summary-mode))
6933       (when (boundp 'gnus-draft-mode)
6934         (setq draft gnus-draft-mode)))
6935     (with-temp-buffer
6936       (use-local-map keymap)
6937       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6938       (set (make-local-variable 'gnus-draft-mode) draft)
6939       (describe-bindings prefix))
6940     (let ((item `((lambda (prefix)
6941                     (with-current-buffer ,(current-buffer)
6942                       (gnus-article-describe-bindings prefix)))
6943                   ,prefix)))
6944       ;; Loading `help-mode' here is necessary if `describe-bindings'
6945       ;; is replaced with something, e.g. `helm-descbinds'.
6946       (require 'help-mode)
6947       (with-current-buffer (let (help-xref-following) (help-buffer))
6948         (setq help-xref-stack-item item)))))
6949
6950 (defun gnus-article-reply-with-original (&optional wide)
6951   "Start composing a reply mail to the current message.
6952 The text in the region will be yanked.  If the region isn't active,
6953 the entire article will be yanked."
6954   (interactive)
6955   (let ((article (cdr gnus-article-current))
6956         contents)
6957     (if (not (gnus-region-active-p))
6958         (with-current-buffer gnus-summary-buffer
6959           (gnus-summary-reply (list (list article)) wide))
6960       (setq contents (buffer-substring (point) (mark t)))
6961       ;; Deactivate active regions.
6962       (when (and (boundp 'transient-mark-mode)
6963                  transient-mark-mode)
6964         (setq mark-active nil))
6965       (with-current-buffer gnus-summary-buffer
6966         (gnus-summary-reply
6967          (list (list article contents)) wide)))))
6968
6969 (defun gnus-article-wide-reply-with-original ()
6970   "Start composing a wide reply mail to the current message.
6971 The text in the region will be yanked.  If the region isn't active,
6972 the entire article will be yanked."
6973   (interactive)
6974   (gnus-article-reply-with-original t))
6975
6976 (defun gnus-article-followup-with-original ()
6977   "Compose a followup to the current article.
6978 The text in the region will be yanked.  If the region isn't active,
6979 the entire article will be yanked."
6980   (interactive)
6981   (let ((article (cdr gnus-article-current))
6982         contents)
6983       (if (not (gnus-region-active-p))
6984           (with-current-buffer gnus-summary-buffer
6985             (gnus-summary-followup (list (list article))))
6986         (setq contents (buffer-substring (point) (mark t)))
6987         ;; Deactivate active regions.
6988         (when (and (boundp 'transient-mark-mode)
6989                    transient-mark-mode)
6990           (setq mark-active nil))
6991         (with-current-buffer gnus-summary-buffer
6992           (gnus-summary-followup
6993            (list (list article contents)))))))
6994
6995 (defun gnus-article-hide (&optional arg force)
6996   "Hide all the gruft in the current article.
6997 This means that signatures, cited text and (some) headers will be
6998 hidden.
6999 If given a prefix, show the hidden text instead."
7000   (interactive (append (gnus-article-hidden-arg) (list 'force)))
7001   (gnus-article-hide-headers arg)
7002   (gnus-article-hide-list-identifiers arg)
7003   (gnus-article-hide-citation-maybe arg force)
7004   (gnus-article-hide-signature arg))
7005
7006 (defun gnus-check-group-server ()
7007   ;; Make sure the connection to the server is alive.
7008   (unless (gnus-server-opened
7009            (gnus-find-method-for-group gnus-newsgroup-name))
7010     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7011     (gnus-request-group gnus-newsgroup-name t)))
7012
7013 (eval-when-compile
7014   (autoload 'nneething-get-file-name "nneething"))
7015
7016 (defun gnus-request-article-this-buffer (article group)
7017   "Get an article and insert it into this buffer."
7018   (let (do-update-line sparse-header)
7019     (prog1
7020         (save-excursion
7021           (erase-buffer)
7022           (gnus-kill-all-overlays)
7023           (setq group (or group gnus-newsgroup-name))
7024
7025           ;; Using `gnus-request-article' directly will insert the article into
7026           ;; `nntp-server-buffer' - so we'll save some time by not having to
7027           ;; copy it from the server buffer into the article buffer.
7028
7029           ;; We only request an article by message-id when we do not have the
7030           ;; headers for it, so we'll have to get those.
7031           (when (stringp article)
7032             (gnus-read-header article))
7033
7034           ;; If the article number is negative, that means that this article
7035           ;; doesn't belong in this newsgroup (possibly), so we find its
7036           ;; message-id and request it by id instead of number.
7037           (when (and (numberp article)
7038                      gnus-summary-buffer
7039                      (get-buffer gnus-summary-buffer)
7040                      (gnus-buffer-exists-p gnus-summary-buffer))
7041             (with-current-buffer gnus-summary-buffer
7042               (let ((header (gnus-summary-article-header article)))
7043                 (when (< article 0)
7044                   (cond
7045                    ((memq article gnus-newsgroup-sparse)
7046                     ;; This is a sparse gap article.
7047                     (setq do-update-line article)
7048                     (setq article (mail-header-id header))
7049                     (setq sparse-header (gnus-read-header article))
7050                     (setq gnus-newsgroup-sparse
7051                           (delq article gnus-newsgroup-sparse)))
7052                    ((vectorp header)
7053                     ;; It's a real article.
7054                     (setq article (mail-header-id header)))
7055                    (t
7056                     ;; It is an extracted pseudo-article.
7057                     (setq article 'pseudo)
7058                     (gnus-request-pseudo-article header))))
7059
7060                 (let ((method (gnus-find-method-for-group
7061                                gnus-newsgroup-name)))
7062                   (when (and (eq (car method) 'nneething)
7063                              (vectorp header))
7064                     (let ((dir (nneething-get-file-name
7065                                 (mail-header-id header))))
7066                       (when (and (stringp dir)
7067                                  (file-directory-p dir))
7068                         (setq article 'nneething)
7069                         (gnus-group-enter-directory dir))))))))
7070
7071           (cond
7072            ;; Refuse to select canceled articles.
7073            ((and (numberp article)
7074                  gnus-summary-buffer
7075                  (get-buffer gnus-summary-buffer)
7076                  (gnus-buffer-exists-p gnus-summary-buffer)
7077                  (eq (cdr (with-current-buffer gnus-summary-buffer
7078                             (assq article gnus-newsgroup-reads)))
7079                      gnus-canceled-mark))
7080             nil)
7081            ;; We first check `gnus-original-article-buffer'.
7082            ((and (get-buffer gnus-original-article-buffer)
7083                  (numberp article)
7084                  (with-current-buffer gnus-original-article-buffer
7085                    (and (equal (car gnus-original-article) group)
7086                         (eq (cdr gnus-original-article) article))))
7087             ;; `insert-buffer-substring' would incorrectly use the
7088             ;; equivalent of string-make-multibyte which amount to decoding
7089             ;; with locale-coding-system, causing failure of
7090             ;; subsequent decoding.
7091             (insert (mm-string-to-multibyte
7092                      (with-current-buffer gnus-original-article-buffer
7093                        (buffer-substring (point-min) (point-max)))))
7094             'article)
7095            ;; Check the backlog.
7096            ((and gnus-keep-backlog
7097                  (gnus-backlog-request-article group article (current-buffer)))
7098             'article)
7099            ;; Check asynchronous pre-fetch.
7100            ((gnus-async-request-fetched-article group article (current-buffer))
7101             (gnus-async-prefetch-next group article gnus-summary-buffer)
7102             (when (and (numberp article) gnus-keep-backlog)
7103               (gnus-backlog-enter-article group article (current-buffer)))
7104             'article)
7105            ;; Check the cache.
7106            ((and gnus-use-cache
7107                  (numberp article)
7108                  (gnus-cache-request-article article group))
7109             'article)
7110            ;; Check the agent cache.
7111            ((gnus-agent-request-article article group)
7112             'article)
7113            ;; Get the article and put into the article buffer.
7114            ((or (stringp article)
7115                 (numberp article))
7116             (let ((gnus-override-method gnus-override-method)
7117                   (methods (and (stringp article)
7118                                 (with-current-buffer gnus-summary-buffer
7119                                   (gnus-refer-article-methods))))
7120                   (backend (car (gnus-find-method-for-group
7121                                  gnus-newsgroup-name)))
7122                   result
7123                   (inhibit-read-only t))
7124               (when (and (null gnus-override-method)
7125                          methods)
7126                 (setq gnus-override-method (pop methods)))
7127               (while (not result)
7128                 (erase-buffer)
7129                 (gnus-kill-all-overlays)
7130                 (let ((gnus-newsgroup-name group))
7131                   (gnus-check-group-server))
7132                 (cond
7133                  ((gnus-request-article article group (current-buffer))
7134                   (when (numberp article)
7135                     (gnus-async-prefetch-next group article
7136                                               gnus-summary-buffer)
7137                     (when gnus-keep-backlog
7138                       (gnus-backlog-enter-article
7139                        group article (current-buffer)))
7140                     (when (and gnus-agent
7141                                (gnus-agent-group-covered-p group))
7142                       (gnus-agent-store-article article group)))
7143                   (setq result 'article))
7144                  (methods
7145                   (setq gnus-override-method (pop methods)))
7146                  ((not (string-match "^400 "
7147                                      (nnheader-get-report backend)))
7148                   ;; If we get 400 server disconnect, reconnect and
7149                   ;; retry; otherwise, assume the article has expired.
7150                   (setq result 'done))))
7151               (and (eq result 'article) 'article)))
7152            ;; It was a pseudo.
7153            (t article)))
7154
7155       ;; Associate this article with the current summary buffer.
7156       (setq gnus-article-current-summary gnus-summary-buffer)
7157
7158       ;; Take the article from the original article buffer
7159       ;; and place it in the buffer it's supposed to be in.
7160       (when (and (get-buffer gnus-article-buffer)
7161                  (equal (buffer-name (current-buffer))
7162                         (buffer-name (get-buffer gnus-article-buffer))))
7163         (save-excursion
7164           (if (get-buffer gnus-original-article-buffer)
7165               (set-buffer gnus-original-article-buffer)
7166             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
7167             (buffer-disable-undo)
7168             (setq major-mode 'gnus-original-article-mode)
7169             (setq buffer-read-only t))
7170           (let ((inhibit-read-only t))
7171             (erase-buffer)
7172             (insert-buffer-substring gnus-article-buffer))
7173           (setq gnus-original-article (cons group article)))
7174
7175         ;; Decode charsets.
7176         (run-hooks 'gnus-article-decode-hook)
7177         ;; Mark article as decoded or not.
7178         (setq gnus-article-decoded-p gnus-article-decode-hook))
7179
7180       ;; Update sparse articles.
7181       (when (and do-update-line
7182                  (or (numberp article)
7183                      (stringp article)))
7184         (let ((buf (current-buffer)))
7185           (set-buffer gnus-summary-buffer)
7186           (gnus-summary-update-article do-update-line sparse-header)
7187           (gnus-summary-goto-subject do-update-line nil t)
7188           (set-window-point (gnus-get-buffer-window (current-buffer) t)
7189                             (point))
7190           (set-buffer buf))))))
7191
7192 (defun gnus-block-private-groups (group)
7193   (if (or (gnus-news-group-p group)
7194           (gnus-member-of-valid 'global group))
7195       ;; Block nothing in news groups.
7196       nil
7197     ;; Block everything anywhere else.
7198     "."))
7199
7200 (defun gnus-blocked-images ()
7201   (if (functionp gnus-blocked-images)
7202       (funcall gnus-blocked-images gnus-newsgroup-name)
7203     gnus-blocked-images))
7204
7205 ;;;
7206 ;;; Article editing
7207 ;;;
7208
7209 (defcustom gnus-article-edit-mode-hook nil
7210   "Hook run in article edit mode buffers."
7211   :group 'gnus-article-various
7212   :type 'hook)
7213
7214 (defvar gnus-article-edit-done-function nil)
7215
7216 (defvar gnus-article-edit-mode-map nil)
7217
7218 ;; Should we be using derived.el for this?
7219 (unless gnus-article-edit-mode-map
7220   (setq gnus-article-edit-mode-map (make-keymap))
7221   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
7222
7223   (gnus-define-keys gnus-article-edit-mode-map
7224     "\C-c?"    describe-mode
7225     "\C-c\C-c" gnus-article-edit-done
7226     "\C-c\C-k" gnus-article-edit-exit
7227     "\C-c\C-f\C-t" message-goto-to
7228     "\C-c\C-f\C-o" message-goto-from
7229     "\C-c\C-f\C-b" message-goto-bcc
7230     ;;"\C-c\C-f\C-w" message-goto-fcc
7231     "\C-c\C-f\C-c" message-goto-cc
7232     "\C-c\C-f\C-s" message-goto-subject
7233     "\C-c\C-f\C-r" message-goto-reply-to
7234     "\C-c\C-f\C-n" message-goto-newsgroups
7235     "\C-c\C-f\C-d" message-goto-distribution
7236     "\C-c\C-f\C-f" message-goto-followup-to
7237     "\C-c\C-f\C-m" message-goto-mail-followup-to
7238     "\C-c\C-f\C-k" message-goto-keywords
7239     "\C-c\C-f\C-u" message-goto-summary
7240     "\C-c\C-f\C-i" message-insert-or-toggle-importance
7241     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
7242     "\C-c\C-b" message-goto-body
7243     "\C-c\C-i" message-goto-signature
7244
7245     "\C-c\C-t" message-insert-to
7246     "\C-c\C-n" message-insert-newsgroups
7247     "\C-c\C-o" message-sort-headers
7248     "\C-c\C-e" message-elide-region
7249     "\C-c\C-v" message-delete-not-region
7250     "\C-c\C-z" message-kill-to-signature
7251     "\M-\r" message-newline-and-reformat
7252     "\C-c\C-a" mml-attach-file
7253     "\C-a" message-beginning-of-line
7254     "\t" message-tab
7255     "\M-;" comment-region)
7256
7257   (gnus-define-keys (gnus-article-edit-wash-map
7258                      "\C-c\C-w" gnus-article-edit-mode-map)
7259     "f" gnus-article-edit-full-stops))
7260
7261 (easy-menu-define
7262   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7263   '("Field"
7264     ["Fetch To" message-insert-to t]
7265     ["Fetch Newsgroups" message-insert-newsgroups t]
7266     "----"
7267     ["To" message-goto-to t]
7268     ["From" message-goto-from t]
7269     ["Subject" message-goto-subject t]
7270     ["Cc" message-goto-cc t]
7271     ["Reply-To" message-goto-reply-to t]
7272     ["Summary" message-goto-summary t]
7273     ["Keywords" message-goto-keywords t]
7274     ["Newsgroups" message-goto-newsgroups t]
7275     ["Followup-To" message-goto-followup-to t]
7276     ["Mail-Followup-To" message-goto-mail-followup-to t]
7277     ["Distribution" message-goto-distribution t]
7278     ["Body" message-goto-body t]
7279     ["Signature" message-goto-signature t]))
7280
7281 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7282   "Major mode for editing articles.
7283 This is an extended text-mode.
7284
7285 \\{gnus-article-edit-mode-map}"
7286   (make-local-variable 'gnus-article-edit-done-function)
7287   (make-local-variable 'gnus-prev-winconf)
7288   (set (make-local-variable 'font-lock-defaults)
7289        '(message-font-lock-keywords t))
7290   (set (make-local-variable 'mail-header-separator) "")
7291   (set (make-local-variable 'gnus-article-edit-mode) t)
7292   (easy-menu-add message-mode-field-menu message-mode-map)
7293   (mml-mode)
7294   (setq buffer-read-only nil)
7295   (buffer-enable-undo)
7296   (widen))
7297
7298 (defun gnus-article-edit (&optional force)
7299   "Edit the current article.
7300 This will have permanent effect only in mail groups.
7301 If FORCE is non-nil, allow editing of articles even in read-only
7302 groups."
7303   (interactive "P")
7304   (when (and (not force)
7305              (gnus-group-read-only-p))
7306     (error "The current newsgroup does not support article editing"))
7307   (gnus-article-date-original)
7308   (gnus-article-edit-article
7309    'ignore
7310    `(lambda (no-highlight)
7311       'ignore
7312       (gnus-summary-edit-article-done
7313        ,(or (mail-header-references gnus-current-headers) "")
7314        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7315
7316 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7317   "Start editing the contents of the current article buffer."
7318   (let ((winconf (current-window-configuration)))
7319     (set-buffer gnus-article-buffer)
7320     (let ((message-auto-save-directory
7321            ;; Don't associate the article buffer with a draft file.
7322            nil))
7323       (gnus-article-edit-mode))
7324     (funcall start-func)
7325     (set-buffer-modified-p nil)
7326     (gnus-configure-windows 'edit-article)
7327     (setq gnus-article-edit-done-function exit-func)
7328     (setq gnus-prev-winconf winconf)
7329     (unless quiet
7330       (gnus-message 6 "C-c C-c to end edits"))))
7331
7332 (defun gnus-article-edit-done (&optional arg)
7333   "Update the article edits and exit."
7334   (interactive "P")
7335   (let ((func gnus-article-edit-done-function)
7336         (buf (current-buffer))
7337         (start (window-start))
7338         (p (point))
7339         (winconf gnus-prev-winconf))
7340     (widen) ;; Widen it in case that users narrowed the buffer.
7341     (funcall func arg)
7342     (set-buffer buf)
7343     ;; The cache and backlog have to be flushed somewhat.
7344     (when gnus-keep-backlog
7345       (gnus-backlog-remove-article
7346        (car gnus-article-current) (cdr gnus-article-current)))
7347     ;; Flush original article as well.
7348     (gnus-flush-original-article-buffer)
7349     (when gnus-use-cache
7350       (gnus-cache-update-article
7351        (car gnus-article-current) (cdr gnus-article-current)))
7352     ;; We remove all text props from the article buffer.
7353     (kill-all-local-variables)
7354     (set-text-properties (point-min) (point-max) nil)
7355     (gnus-article-mode)
7356     (set-window-configuration winconf)
7357     (set-buffer buf)
7358     (set-window-start (get-buffer-window buf) start)
7359     (set-window-point (get-buffer-window buf) (point)))
7360   (gnus-summary-show-article))
7361
7362 (defun gnus-flush-original-article-buffer ()
7363   (when (get-buffer gnus-original-article-buffer)
7364     (with-current-buffer gnus-original-article-buffer
7365       (setq gnus-original-article nil))))
7366
7367 (defun gnus-article-edit-exit ()
7368   "Exit the article editing without updating."
7369   (interactive)
7370   (when (or (not (buffer-modified-p))
7371             (yes-or-no-p "Article modified; kill anyway? "))
7372     (let ((curbuf (current-buffer))
7373           (p (point))
7374           (window-start (window-start)))
7375       (erase-buffer)
7376       (if (gnus-buffer-live-p gnus-original-article-buffer)
7377           (insert-buffer-substring gnus-original-article-buffer))
7378       (let ((winconf gnus-prev-winconf))
7379         (kill-all-local-variables)
7380         (gnus-article-mode)
7381         (set-window-configuration winconf)
7382         ;; Tippy-toe some to make sure that point remains where it was.
7383         (save-current-buffer
7384           (set-buffer curbuf)
7385           (set-window-start (get-buffer-window (current-buffer)) window-start)
7386           (goto-char p))))
7387     (gnus-summary-show-article)))
7388
7389 (defun gnus-article-edit-full-stops ()
7390   "Interactively repair spacing at end of sentences."
7391   (interactive)
7392   (save-excursion
7393     (goto-char (point-min))
7394     (search-forward-regexp "^$" nil t)
7395     (let ((case-fold-search nil))
7396       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7397
7398 ;;;
7399 ;;; Article highlights
7400 ;;;
7401
7402 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7403
7404 ;;; Internal Variables:
7405
7406 (defcustom gnus-button-url-regexp
7407   (concat
7408    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7409    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7410    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7411    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7412        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7413              (punct "!?:;.,"))
7414          (concat
7415           "\\(?:"
7416           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7417           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7418           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)"
7419           "\\(?:" "[" chars punct "]+" "[" chars "]" "\\)?"
7420           "\\|"
7421           "[" chars punct "]+" "[" chars "]"
7422           "\\)"))
7423      (concat ;; XEmacs 21.4 doesn't support POSIX.
7424       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7425       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7426    "\\)")
7427   "Regular expression that matches URLs."
7428   :version "24.4"
7429   :group 'gnus-article-buttons
7430   :type 'regexp)
7431
7432 (defcustom gnus-button-valid-fqdn-regexp
7433   message-valid-fqdn-regexp
7434   "Regular expression that matches a valid FQDN."
7435   :version "22.1"
7436   :group 'gnus-article-buttons
7437   :type 'regexp)
7438
7439 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7440 (defcustom gnus-button-valid-localpart-regexp
7441   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7442   "Regular expression that matches a localpart of mail addresses or MIDs."
7443   :version "22.1"
7444   :group 'gnus-article-buttons
7445   :type 'regexp)
7446
7447 (defcustom gnus-button-man-handler 'manual-entry
7448   "Function to use for displaying man pages.
7449 The function must take at least one argument with a string naming the
7450 man page."
7451   :version "22.1"
7452   :type '(choice (function-item :tag "Man" manual-entry)
7453                  (function-item :tag "Woman" woman)
7454                  (function :tag "Other"))
7455   :group 'gnus-article-buttons)
7456
7457 (defcustom gnus-button-mid-or-mail-regexp
7458   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7459           gnus-button-valid-fqdn-regexp
7460           ">?\\)\\b")
7461   "Regular expression that matches a message ID or a mail address."
7462   :version "22.1"
7463   :group 'gnus-article-buttons
7464   :type 'regexp)
7465
7466 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7467   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7468 Strings like this can be either a message ID or a mail address.  If it is one
7469 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7470 message ID or a mail address, respectively.  If this variable is set to the
7471 symbol `ask', always query the user what do do.  If it is a function, this
7472 function will be called with the string as its only argument.  The function
7473 must return `mid', `mail', `invalid' or `ask'."
7474   :version "22.1"
7475   :group 'gnus-article-buttons
7476   :type '(choice (function-item :tag "Heuristic function"
7477                                 gnus-button-mid-or-mail-heuristic)
7478                  (const ask)
7479                  (const mid)
7480                  (const mail)))
7481
7482 (defcustom gnus-button-mid-or-mail-heuristic-alist
7483   '((-10.0 . ".+\\$.+@")
7484     (-10.0 . "#")
7485     (-10.0 . "\\*")
7486     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7487     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7488     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7489     (-1.0  . "^[^a-z]+@")
7490     ;;
7491     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7492     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7493     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7494     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7495     ;;
7496     (-2.0  . "^[0-9]")
7497     (-1.0  . "^[0-9][0-9]")
7498     ;;
7499     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7500     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7501     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7502     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7503     ;;
7504     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7505     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7506     ;;       "[0-9]{8,}.*\@"
7507     (-3.0
7508      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7509     ;; "[0-9]{12,}.*\@"
7510     ;; compensation for TDMA dated mail addresses:
7511     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7512     ;;
7513     (-20.0 . "\\.fsf@") ;; Gnus
7514     (-20.0 . "^slrn")
7515     (-20.0 . "^Pine")
7516     (-20.0 . "^alpine\\.")
7517     (-20.0 . "_-_") ;; Subject change in thread
7518     ;;
7519     (-20.0 . "\\.ln@") ;; leafnode
7520     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7521     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7522     ;;
7523     ;; (5.0 . "") ;; $local_part_len <= 7
7524     (10.0  . "^[^0-9]+@")
7525     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7526     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7527     (3.0   . "\@stud")
7528     ;;
7529     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7530     ;;
7531     (0.5   . "^[A-Z][a-z]")
7532     (0.5   . "^[A-Z][a-z][a-z]")
7533     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7534     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7535   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7536
7537 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7538 address.  The REGEXP is processed with `case-fold-search' set to nil."
7539   :version "22.1"
7540   :group 'gnus-article-buttons
7541   :type '(repeat (cons (number :tag "Rate")
7542                        (regexp :tag "Regexp"))))
7543
7544 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7545   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7546 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7547 address, `ask' if unsure and `invalid' if the string is invalid."
7548   (let ((case-fold-search nil)
7549         (list gnus-button-mid-or-mail-heuristic-alist)
7550         (result 0) rate regexp lpartlen elem)
7551     (setq lpartlen
7552           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7553     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7554     ;; Certain special cases...
7555     (when (string-match
7556            (concat
7557             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7558             "^[0-9]+\\.[0-9]+@compuserve\\|"
7559             "@public\\.gmane\\.org")
7560            mid-or-mail)
7561       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7562       (setq result 'mail))
7563     (when (string-match "@.*@\\| " mid-or-mail)
7564       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7565       (setq result 'invalid))
7566     ;; Nothing more to do, if result is not a number here...
7567     (when (numberp result)
7568       (while list
7569         (setq elem (car list)
7570               rate (car elem)
7571               regexp (cdr elem)
7572               list (cdr list))
7573         (when (string-match regexp mid-or-mail)
7574           (setq result (+ result rate))
7575           (gnus-message
7576            9 "`%s' matched `%s', rate `%s', result `%s'."
7577            mid-or-mail regexp rate result)))
7578       (when (<= lpartlen 7)
7579         (setq result (+ result 5.0))
7580         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7581                       mid-or-mail result))
7582       (when (>= lpartlen 12)
7583         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7584         (cond
7585          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7586           ;; Long local part should contain realname if e-mail address,
7587           ;; too many digits: message-id.
7588           ;; $score -= 5.0 + 0.1 * $local_part_len;
7589           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7590           (setq result (+ result rate))
7591           (gnus-message
7592            9 "Many digits in `%s', rate `%s', result `%s'."
7593            mid-or-mail rate result))
7594          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7595                         mid-or-mail)
7596           ;; Too few vowels [^aeiouy]{4,}.*\@
7597           (setq result (+ result -5.0))
7598           (gnus-message
7599            9 "Few vowels in `%s', rate `%s', result `%s'."
7600            mid-or-mail -5.0 result))
7601          (t
7602           (setq result (+ result 5.0))
7603           (gnus-message
7604            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7605     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7606     ;; Maybe we should make this a customizable alist: (condition . 'result)
7607     (cond
7608      ((symbolp result) result)
7609      ;; Now convert number into proper results:
7610      ((< result -10.0) 'mid)
7611      ((> result  10.0) 'mail)
7612      (t 'ask))))
7613
7614 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7615   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7616          (url-mid (concat "news" ":" mid-or-mail))
7617          (url-mailto (concat "mailto" ":" mid-or-mail)))
7618     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7619     (when (fboundp pref)
7620       (setq guessed
7621             ;; get rid of surrounding angles...
7622             (funcall pref
7623                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7624       (if (or (eq 'mid guessed) (eq 'mail guessed))
7625           (setq pref guessed)
7626         (setq pref 'ask)))
7627     (if (eq pref 'ask)
7628         (save-window-excursion
7629           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7630               (setq pref 'mail)
7631             (setq pref 'mid))))
7632     (cond ((eq pref 'mid)
7633            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7634            (gnus-button-handle-news url-mid))
7635           ((eq pref 'mail)
7636            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7637            (gnus-url-mailto url-mailto))
7638           (t (gnus-message 3 "Invalid string.")))))
7639
7640 (defun gnus-button-handle-custom (fun arg)
7641   "Call function FUN on argument ARG.
7642 Both FUN and ARG are supposed to be strings.  ARG will be passed
7643 as a symbol to FUN."
7644   (funcall (intern fun)
7645            (if (string-match "^customize-apropos" fun)
7646                arg
7647              (intern arg))))
7648
7649 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7650
7651 (defun gnus-button-handle-describe-function (url)
7652   "Call `describe-function' when pushing the corresponding URL button."
7653   (describe-function
7654    (intern
7655     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7656
7657 (defun gnus-button-handle-describe-variable (url)
7658   "Call `describe-variable' when pushing the corresponding URL button."
7659   (describe-variable
7660    (intern
7661     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7662
7663 (defun gnus-button-handle-symbol (url)
7664 "Display help on variable or function.
7665 Calls `describe-variable' or `describe-function'."
7666   (let ((sym (intern url)))
7667     (cond
7668      ((fboundp sym) (describe-function sym))
7669      ((boundp sym) (describe-variable sym))
7670      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7671
7672 (defun gnus-button-handle-describe-key (url)
7673   "Call `describe-key' when pushing the corresponding URL button."
7674   (let* ((key-string
7675           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7676          (keys (ignore-errors (eval `(kbd ,key-string)))))
7677     (if keys
7678         (describe-key keys)
7679       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7680
7681 (defun gnus-button-handle-apropos (url)
7682   "Call `apropos' when pushing the corresponding URL button."
7683   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7684
7685 (defun gnus-button-handle-apropos-command (url)
7686   "Call `apropos' when pushing the corresponding URL button."
7687   (apropos-command
7688    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7689
7690 (defun gnus-button-handle-apropos-variable (url)
7691   "Call `apropos' when pushing the corresponding URL button."
7692   (funcall
7693    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7694    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7695
7696 (defun gnus-button-handle-apropos-documentation (url)
7697   "Call `apropos' when pushing the corresponding URL button."
7698   (funcall
7699    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7700    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7701
7702 (defun gnus-button-handle-library (url)
7703   "Call `locate-library' when pushing the corresponding URL button."
7704   (gnus-message 9 "url=`%s'" url)
7705   (let* ((lib (locate-library url))
7706          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7707     (if (not lib)
7708         (gnus-message 1 "Cannot locale library `%s'." url)
7709       (find-file-read-only file))))
7710
7711 (defcustom gnus-button-man-level 5
7712   "*Integer that says how many man-related buttons Gnus will show.
7713 The higher the number, the more buttons will appear and the more false
7714 positives are possible.  Note that you can set this variable local to
7715 specific groups.  Setting it higher in Unix groups is probably a good idea.
7716 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7717 how to set variables in specific groups."
7718   :version "22.1"
7719   :group 'gnus-article-buttons
7720   :link '(custom-manual "(gnus)Group Parameters")
7721   :type 'integer)
7722
7723 (defcustom gnus-button-emacs-level 5
7724   "*Integer that says how many emacs-related buttons Gnus will show.
7725 The higher the number, the more buttons will appear and the more false
7726 positives are possible.  Note that you can set this variable local to
7727 specific groups.  Setting it higher in Emacs or Gnus related groups is
7728 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7729 `gnus-parameters' on how to set variables in specific groups."
7730   :version "22.1"
7731   :group 'gnus-article-buttons
7732   :link '(custom-manual "(gnus)Group Parameters")
7733   :type 'integer)
7734
7735 (defcustom gnus-button-message-level 5
7736   "*Integer that says how many buttons for news or mail messages will appear.
7737 The higher the number, the more buttons will appear and the more false
7738 positives are possible."
7739   ;; mail addresses, MIDs, URLs for news, ...
7740   :version "22.1"
7741   :group 'gnus-article-buttons
7742   :type 'integer)
7743
7744 (defcustom gnus-button-browse-level 5
7745   "*Integer that says how many buttons for browsing will appear.
7746 The higher the number, the more buttons will appear and the more false
7747 positives are possible."
7748   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7749   :version "22.1"
7750   :group 'gnus-article-buttons
7751   :type 'integer)
7752
7753 (defcustom gnus-button-alist
7754   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7755      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7756     ((concat "\\b\\(nntp\\|news\\):\\("
7757              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7758      0 t gnus-button-handle-news 2)
7759     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7760      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7761     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7762      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7763     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7764     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7765      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7766     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7767      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7768     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7769      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7770     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7771      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7772     ;; RFC 2368 (The mailto URL scheme)
7773     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7774      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7775     ("\\bmailto:\\([^ \n\t]+\\)"
7776      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7777     ;; Info Konqueror style <info:/foo/bar baz>.
7778     ;; Must come before " Gnus home-grown style".
7779     ("\\binfo://?\\([^'\">\n\t]+\\)"
7780      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7781    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7782     ("\\binfo://\\([^'\">\n\t ]+\\)"
7783      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7784     ;; Info GNOME style <info:foo#bar_baz>
7785     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7786      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7787     ;; Info KDE style <info:(foo)bar baz>
7788     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7789      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7790     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7791      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7792     ("\\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,]*\\)\\)?"
7793      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7794      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7795     ;; This is custom
7796     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7797      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7798     ;; Emacs help commands
7799     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7800      ;; regexp doesn't match arguments containing ` '.
7801      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7802     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7803      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7804     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7805      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7806     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7807      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7808     ;; The following entries may lead to many false positives so don't enable
7809     ;; them by default (use a high button level).
7810     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7811      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7812      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7813     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7814      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7815     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7816      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7817     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7818      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7819     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7820      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7821     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7822      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7823     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7824      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7825     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7826      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7827     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7828      ;; Unlike the other regexps we really have to require quoting
7829      ;; here to determine where it ends.
7830      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7831     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7832     ("<URL: *\\([^\n<>]*\\)>"
7833      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7834     ;; RFC 2396 (2.4.3., delims) ...
7835     ("\"URL: *\\([^\n\"]*\\)\""
7836      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7837     ;; Raw URLs.
7838     (gnus-button-url-regexp
7839      0 (>= gnus-button-browse-level 0) browse-url 0)
7840     ;; man pages
7841     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7842      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7843      gnus-button-handle-man 1)
7844     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7845     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7846      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7847      gnus-button-handle-man 1)
7848     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7849     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7850     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7851      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7852     ;; Recognizing patches to .el files.  This is somewhat obscure,
7853     ;; but considering the percentage of Gnus users who hack Emacs
7854     ;; Lisp files...
7855     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7856      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7857     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7858      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7859     ;; MID or mail: To avoid too many false positives we don't try to catch
7860     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7861     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7862     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7863     ;; so that non-ambiguous entries (see above) match first.
7864     (gnus-button-mid-or-mail-regexp
7865      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7866   "*Alist of regexps matching buttons in article bodies.
7867
7868 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7869 REGEXP: is the string (case insensitive) matching text around the button (can
7870 also be Lisp expression evaluating to a string),
7871 BUTTON: is the number of the regexp grouping actually matching the button,
7872 FORM: is a Lisp expression which must eval to true for the button to
7873 be added,
7874 CALLBACK: is the function to call when the user push this button, and each
7875 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7876
7877 CALLBACK can also be a variable, in that case the value of that
7878 variable it the real callback function."
7879   :group 'gnus-article-buttons
7880   :type '(repeat (list (choice regexp variable sexp)
7881                        (integer :tag "Button")
7882                        (sexp :tag "Form")
7883                        (function :tag "Callback")
7884                        (repeat :tag "Par"
7885                                :inline t
7886                                (integer :tag "Regexp group")))))
7887 (put 'gnus-button-alist 'risky-local-variable t)
7888
7889 (defcustom gnus-header-button-alist
7890   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7891      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7892     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7893      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7894     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7895      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7896     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7897      0 (>= gnus-button-browse-level 0) browse-url 0)
7898     ("^Subject:" gnus-button-url-regexp
7899      0 (>= gnus-button-browse-level 0) browse-url 0)
7900     ("^[^:]+:" gnus-button-url-regexp
7901      0 (>= gnus-button-browse-level 0) browse-url 0)
7902     ("^OpenPGP:.*url=" gnus-button-url-regexp
7903      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7904     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7905      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7906     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7907      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7908   "*Alist of headers and regexps to match buttons in article heads.
7909
7910 This alist is very similar to `gnus-button-alist', except that each
7911 alist has an additional HEADER element first in each entry:
7912
7913 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7914
7915 HEADER is a regexp to match a header.  For a fuller explanation, see
7916 `gnus-button-alist'."
7917   :group 'gnus-article-buttons
7918   :group 'gnus-article-headers
7919   :type '(repeat (list (regexp :tag "Header")
7920                        (choice regexp variable)
7921                        (integer :tag "Button")
7922                        (sexp :tag "Form")
7923                        (function :tag "Callback")
7924                        (repeat :tag "Par"
7925                                :inline t
7926                                (integer :tag "Regexp group")))))
7927 (put 'gnus-header-button-alist 'risky-local-variable t)
7928
7929 ;;; Commands:
7930
7931 (defun gnus-article-push-button (event)
7932   "Check text under the mouse pointer for a callback function.
7933 If the text under the mouse pointer has a `gnus-callback' property,
7934 call it with the value of the `gnus-data' text property."
7935   (interactive "e")
7936   (set-buffer (window-buffer (posn-window (event-start event))))
7937   (let* ((pos (posn-point (event-start event)))
7938          (data (get-text-property pos 'gnus-data))
7939          (fun (get-text-property pos 'gnus-callback)))
7940     (goto-char pos)
7941     (when fun
7942       (funcall fun data))))
7943
7944 (defun gnus-article-press-button ()
7945   "Check text at point for a callback function.
7946 If the text at point has a `gnus-callback' property,
7947 call it with the value of the `gnus-data' text property."
7948   (interactive)
7949   (let ((data (get-text-property (point) 'gnus-data))
7950         (fun (get-text-property (point) 'gnus-callback)))
7951     (when fun
7952       (funcall fun data))))
7953
7954 (defun gnus-article-highlight (&optional force)
7955   "Highlight current article.
7956 This function calls `gnus-article-highlight-headers',
7957 `gnus-article-highlight-citation',
7958 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7959 do the highlighting.  See the documentation for those functions."
7960   (interactive (list 'force))
7961   (gnus-article-highlight-headers)
7962   (gnus-article-highlight-citation force)
7963   (gnus-article-highlight-signature)
7964   (gnus-article-add-buttons)
7965   (gnus-article-add-buttons-to-head))
7966
7967 (defun gnus-article-highlight-some (&optional force)
7968   "Highlight current article.
7969 This function calls `gnus-article-highlight-headers',
7970 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7971 do the highlighting.  See the documentation for those functions."
7972   (interactive (list 'force))
7973   (gnus-article-highlight-headers)
7974   (gnus-article-highlight-signature)
7975   (gnus-article-add-buttons))
7976
7977 (defun gnus-article-highlight-headers ()
7978   "Highlight article headers as specified by `gnus-header-face-alist'."
7979   (interactive)
7980   (gnus-with-article-headers
7981     (let (regexp header-face field-face from hpoints fpoints)
7982       (dolist (entry gnus-header-face-alist)
7983         (goto-char (point-min))
7984         (setq regexp (concat "^\\("
7985                              (if (string-equal "" (nth 0 entry))
7986                                  "[^\t ]"
7987                                (nth 0 entry))
7988                              "\\)")
7989               header-face (nth 1 entry)
7990               field-face (nth 2 entry))
7991         (while (and (re-search-forward regexp nil t)
7992                     (not (eobp)))
7993           (beginning-of-line)
7994           (setq from (point))
7995           (unless (search-forward ":" nil t)
7996             (forward-char 1))
7997           (when (and header-face
7998                      (not (memq (point) hpoints)))
7999             (push (point) hpoints)
8000             (gnus-put-text-property from (point) 'face header-face))
8001           (when (and field-face
8002                      (not (memq (setq from (point)) fpoints)))
8003             (push from fpoints)
8004             (if (re-search-forward "^[^ \t]" nil t)
8005                 (forward-char -2)
8006               (goto-char (point-max)))
8007             (gnus-put-text-property from (point) 'face field-face)))))))
8008
8009 (defun gnus-article-highlight-signature ()
8010   "Highlight the signature in an article.
8011 It does this by highlighting everything after
8012 `gnus-signature-separator' using the face `gnus-signature'."
8013   (interactive)
8014   (gnus-with-article-buffer
8015     (let ((inhibit-point-motion-hooks t))
8016       (save-restriction
8017         (when (and gnus-signature-face
8018                    (gnus-article-narrow-to-signature))
8019           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
8020                             'face gnus-signature-face)
8021           (widen)
8022           (gnus-article-search-signature)
8023           (let ((start (match-beginning 0))
8024                 (end (set-marker (make-marker) (1+ (match-end 0)))))
8025             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
8026                                      end)))))))
8027
8028 (defun gnus-button-in-region-p (b e prop)
8029   "Say whether PROP exists in the region."
8030   (text-property-not-all b e prop nil))
8031
8032 (defun gnus-article-add-buttons ()
8033   "Find external references in the article and make buttons of them.
8034 \"External references\" are things like Message-IDs and URLs, as
8035 specified by `gnus-button-alist'."
8036   (interactive)
8037   (gnus-with-article-buffer
8038     (let ((inhibit-point-motion-hooks t)
8039           (case-fold-search t)
8040           (alist gnus-button-alist)
8041           beg entry regexp)
8042       ;; We skip the headers.
8043       (article-goto-body)
8044       (setq beg (point))
8045       (while (setq entry (pop alist))
8046         (setq regexp (eval (car entry)))
8047         (goto-char beg)
8048         (while (re-search-forward regexp nil t)
8049           (let ((start (match-beginning (nth 1 entry)))
8050                 (end (match-end (nth 1 entry)))
8051                 (from (match-beginning 0)))
8052             (when (and (eval (nth 2 entry))
8053                        (not (gnus-button-in-region-p
8054                              start end 'gnus-callback)))
8055               ;; That optional form returned non-nil, so we add the
8056               ;; button.
8057               (setq from (set-marker (make-marker) from))
8058               (unless (and (eq (car entry) 'gnus-button-url-regexp)
8059                            (gnus-article-extend-url-button from start end))
8060                 (gnus-article-add-button start end
8061                                          'gnus-button-push (list from entry))
8062                 (gnus-put-text-property
8063                  start end
8064                  'gnus-string (buffer-substring-no-properties
8065                                start end))))))))))
8066
8067 (defun gnus-article-extend-url-button (beg start end)
8068   "Extend url button if url is folded into two or more lines.
8069 Return non-nil if button is extended.  BEG is a marker that points to
8070 the beginning position of a text containing url.  START and END are
8071 the endpoints of a url button before it is extended.  The concatenated
8072 url is put as the `gnus-button-url' overlay property on the button."
8073   (let ((opoint (point))
8074         (points (list start end))
8075         url delim regexp)
8076     (prog1
8077         (when (and (progn
8078                      (goto-char end)
8079                      (not (looking-at "[\t ]*[\">]")))
8080                    (progn
8081                      (goto-char start)
8082                      (string-match
8083                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
8084                       (buffer-substring (point-at-bol) start)))
8085                    (progn
8086                      (setq url (list (buffer-substring start end))
8087                            delim (if (match-beginning 1) ">" "\""))
8088                      (beginning-of-line)
8089                      (setq regexp (concat
8090                                    (when (and (looking-at
8091                                                message-cite-prefix-regexp)
8092                                               (< (match-end 0) start))
8093                                      (regexp-quote (match-string 0)))
8094                                    "\
8095 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
8096                                    delim "\\)"))
8097                      (while (progn
8098                               (forward-line 1)
8099                               (and (looking-at regexp)
8100                                    (prog1
8101                                        (match-beginning 1)
8102                                      (push (or (match-string 2)
8103                                                (match-string 1))
8104                                            url)
8105                                      (push (setq end (or (match-end 2)
8106                                                          (match-end 1)))
8107                                            points)
8108                                      (push (or (match-beginning 2)
8109                                                (match-beginning 1))
8110                                            points)))))
8111                      (match-beginning 2)))
8112           (let (gnus-article-mouse-face widget-mouse-face)
8113             (while points
8114               (gnus-article-add-button (pop points) (pop points)
8115                                        'gnus-button-push
8116                                        (list beg (assq 'gnus-button-url-regexp
8117                                                        gnus-button-alist)))))
8118           (let ((overlay (gnus-make-overlay start end)))
8119             (gnus-overlay-put overlay 'evaporate t)
8120             (gnus-overlay-put overlay 'gnus-button-url
8121                               (list (mapconcat 'identity (nreverse url) "")))
8122             (when gnus-article-mouse-face
8123               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
8124           t)
8125       (goto-char opoint))))
8126
8127 ;; Add buttons to the head of an article.
8128 (defun gnus-article-add-buttons-to-head ()
8129   "Add buttons to the head of the article."
8130   (interactive)
8131   (gnus-with-article-headers
8132     (let (beg end)
8133       (dolist (entry gnus-header-button-alist)
8134         ;; Each alist entry.
8135         (goto-char (point-min))
8136         (while (re-search-forward (car entry) nil t)
8137           ;; Each header matching the entry.
8138           (setq beg (match-beginning 0))
8139           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
8140                              (match-beginning 0))
8141                         (point-max)))
8142           (goto-char beg)
8143           (while (re-search-forward (eval (nth 1 entry)) end t)
8144             ;; Each match within a header.
8145             (let* ((entry (cdr entry))
8146                    (start (match-beginning (nth 1 entry)))
8147                    (end (match-end (nth 1 entry)))
8148                    (form (nth 2 entry)))
8149               (goto-char (match-end 0))
8150               (when (eval form)
8151                 (gnus-article-add-button
8152                  start end (nth 3 entry)
8153                  (buffer-substring (match-beginning (nth 4 entry))
8154                                    (match-end (nth 4 entry)))))))
8155           (goto-char end))))))
8156
8157 ;;; External functions:
8158
8159 (defun gnus-article-add-button (from to fun &optional data text)
8160   "Create a button between FROM and TO with callback FUN and data DATA."
8161   (when gnus-article-button-face
8162     (gnus-overlay-put (gnus-make-overlay from to nil t)
8163                       'face gnus-article-button-face))
8164   (gnus-add-text-properties
8165    from to
8166    (nconc (and gnus-article-mouse-face
8167                (list gnus-mouse-face-prop gnus-article-mouse-face))
8168           (list 'gnus-callback fun)
8169           (and data (list 'gnus-data data))))
8170   (widget-convert-button 'link from to :action 'gnus-widget-press-button
8171                          :help-echo (or text "Follow the link")
8172                          :keymap gnus-url-button-map
8173                          :button-keymap gnus-widget-button-keymap))
8174
8175 (defun gnus-article-copy-string ()
8176   "Copy the string in the button to the kill ring."
8177   (interactive)
8178   (gnus-article-check-buffer)
8179   (let ((data (get-text-property (point) 'gnus-string)))
8180     (when data
8181       (with-temp-buffer
8182         (insert data)
8183         (copy-region-as-kill (point-min) (point-max))
8184         (message "Copied %s" data)))))
8185
8186 ;;; Internal functions:
8187
8188 (defun gnus-article-set-globals ()
8189   (with-current-buffer gnus-summary-buffer
8190     (gnus-set-global-variables)))
8191
8192 (defun gnus-signature-toggle (end)
8193   (gnus-with-article-buffer
8194     (let ((inhibit-point-motion-hooks t))
8195       (if (text-property-any end (point-max) 'article-type 'signature)
8196           (progn
8197             (gnus-delete-wash-type 'signature)
8198             (gnus-remove-text-properties-when
8199              'article-type 'signature end (point-max)
8200              (cons 'article-type (cons 'signature
8201                                        gnus-hidden-properties))))
8202         (gnus-add-wash-type 'signature)
8203         (gnus-add-text-properties-when
8204          'article-type nil end (point-max)
8205          (cons 'article-type (cons 'signature
8206                                    gnus-hidden-properties)))))
8207     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
8208       (gnus-set-mode-line 'article))))
8209
8210 (defun gnus-button-push (marker-and-entry)
8211   ;; Push button starting at MARKER.
8212   (save-excursion
8213     (let* ((marker (car marker-and-entry))
8214            (entry (cadr marker-and-entry))
8215            (regexp (car entry))
8216            (inhibit-point-motion-hooks t))
8217       (goto-char marker)
8218       ;; This is obviously true, or something bad is happening :)
8219       ;; But we need it to have the match-data
8220       (when (looking-at (or (if (symbolp regexp)
8221                                 (symbol-value regexp)
8222                               regexp)))
8223         (let ((fun (nth 3 entry))
8224               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
8225                              (get-char-property marker 'gnus-button-url))
8226                         (mapcar (lambda (group)
8227                                   (let ((string (match-string group)))
8228                                     (set-text-properties
8229                                      0 (length string) nil string)
8230                                     string))
8231                                 (nthcdr 4 entry)))))
8232
8233           (cond
8234            ((fboundp fun)
8235             (apply fun args))
8236            ((and (boundp fun)
8237                  (fboundp (symbol-value fun)))
8238             (apply (symbol-value fun) args))
8239            (t
8240             (gnus-message 1 "You must define `%S' to use this button"
8241                           (cons fun args)))))))))
8242
8243 (defun gnus-parse-news-url (url)
8244   (let (scheme server port group message-id articles)
8245     (with-temp-buffer
8246       (insert url)
8247       (goto-char (point-min))
8248       (when (looking-at "\\([A-Za-z]+\\):")
8249         (setq scheme (match-string 1))
8250         (goto-char (match-end 0)))
8251       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8252         (setq server (match-string 1))
8253         (setq port (if (stringp (match-string 3))
8254                        (string-to-number (match-string 3))
8255                      (match-string 3)))
8256         (goto-char (match-end 0)))
8257
8258       (cond
8259        ((looking-at "\\(.*@.*\\)")
8260         (setq message-id (match-string 1)))
8261        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8262         (setq group (match-string 1)
8263               articles (split-string (match-string 2) "-")))
8264        ((looking-at "\\([^/]+\\)/?")
8265         (setq group (match-string 1)))
8266        (t
8267         (error "Unknown news URL syntax"))))
8268     (list scheme server port group message-id articles)))
8269
8270 (defun gnus-button-handle-news (url)
8271   "Fetch a news URL."
8272   (destructuring-bind (scheme server port group message-id articles)
8273       (gnus-parse-news-url url)
8274     (cond
8275      (message-id
8276       (with-current-buffer gnus-summary-buffer
8277         (if server
8278             (let ((gnus-refer-article-method
8279                    (nconc (list (list 'nntp server))
8280                           gnus-refer-article-method))
8281                   (nntp-port-number (or port "nntp")))
8282               (gnus-message 7 "Fetching %s with %s"
8283                             message-id gnus-refer-article-method)
8284               (gnus-summary-refer-article message-id))
8285           (gnus-summary-refer-article message-id))))
8286      (group
8287       (gnus-button-fetch-group url)))))
8288
8289 (defun gnus-button-patch (library line)
8290   "Visit an Emacs Lisp library LIBRARY on line LINE."
8291   (interactive)
8292   (let ((file (locate-library (file-name-nondirectory library))))
8293     (unless file
8294       (error "Couldn't find library %s" library))
8295     (find-file file)
8296     (goto-char (point-min))
8297     (forward-line (1- (string-to-number line)))))
8298
8299 (defun gnus-button-handle-man (url)
8300   "Fetch a man page."
8301   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8302   (when (eq gnus-button-man-handler 'woman)
8303     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8304   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8305   (funcall gnus-button-man-handler url))
8306
8307 (defun gnus-button-handle-info-url (url)
8308   "Fetch an info URL."
8309   (setq url (mm-subst-char-in-string ?+ ?\  url))
8310   (cond
8311    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8312     (gnus-info-find-node
8313      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8314                      "Gnus")
8315              ")" (gnus-url-unhex-string (match-string 2 url)))))
8316    ((string-match "([^)\"]+)[^\"]+" url)
8317     (setq url
8318           (gnus-replace-in-string
8319            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8320     (gnus-info-find-node url))
8321    (t (error "Can't parse %s" url))))
8322
8323 (defun gnus-button-handle-info-url-gnome (url)
8324   "Fetch GNOME style info URL."
8325   (setq url (mm-subst-char-in-string ?_ ?\  url))
8326   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8327       (gnus-info-find-node
8328        (concat "("
8329                (gnus-url-unhex-string
8330                  (match-string 1 url))
8331                ")"
8332                (or (gnus-url-unhex-string
8333                     (match-string 2 url))
8334                    "Top")))
8335     (error "Can't parse %s" url)))
8336
8337 (defun gnus-button-handle-info-url-kde (url)
8338   "Fetch KDE style info URL."
8339   (gnus-info-find-node (gnus-url-unhex-string url)))
8340
8341 ;; (info) will autoload info.el
8342 (declare-function Info-menu "info" (menu-item &optional fork))
8343 (declare-function Info-index-next "info" (num))
8344
8345 (defun gnus-button-handle-info-keystrokes (url)
8346   "Call `info' when pushing the corresponding URL button."
8347   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8348   (let (node indx comma)
8349     (if (string-match
8350          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8351                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8352                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8353                  "\\(?:[ \t\n,]*\\)\\)?")
8354          url)
8355         (setq node (match-string 2 url)
8356               indx (match-string 3 url))
8357       (error "Can't parse %s" url))
8358     (info)
8359     (Info-directory)
8360     (Info-menu node)
8361     (when (> (length indx) 0)
8362       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8363                             "\\([ \t\n,]*\\)")
8364                     indx)
8365       (setq comma (match-string 2 indx))
8366       (setq indx  (match-string 1 indx))
8367       (Info-index indx)
8368       (when comma
8369         (dotimes (i (with-temp-buffer
8370                       (insert comma)
8371                       ;; Note: the XEmacs version of `how-many' takes
8372                       ;; no optional argument.
8373                       (goto-char (point-min))
8374                       (how-many ",")))
8375           (Info-index-next 1)))
8376       nil)))
8377
8378 (autoload 'pgg-snarf-keys-region "pgg")
8379 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8380 (declare-function pgg-display-output-buffer "pgg" (start end status))
8381
8382 (defun gnus-button-openpgp (url)
8383   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8384   (with-temp-buffer
8385     (mm-url-insert-file-contents-external url)
8386     (pgg-snarf-keys-region (point-min) (point-max))
8387     (pgg-display-output-buffer nil nil nil)))
8388
8389 (defun gnus-button-message-id (message-id)
8390   "Fetch MESSAGE-ID."
8391   (with-current-buffer gnus-summary-buffer
8392     (gnus-summary-refer-article message-id)))
8393
8394 (defun gnus-button-fetch-group (address &rest ignore)
8395   "Fetch GROUP specified by ADDRESS."
8396   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8397                       address)
8398     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8399     ;; for nntp:// and news://
8400     (setq address (match-string 3 address)))
8401   (if (not (string-match "[:/]" address))
8402       ;; This is just a simple group url.
8403       (gnus-group-read-ephemeral-group address gnus-select-method)
8404     (if (not
8405          (string-match
8406           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8407           address))
8408         (error "Can't parse %s" address)
8409       (gnus-group-read-ephemeral-group
8410        (match-string 4 address)
8411        `(nntp ,(match-string 1 address)
8412               (nntp-address ,(match-string 1 address))
8413               (nntp-port-number ,(if (match-end 3)
8414                                      (match-string 3 address)
8415                                    "nntp")))
8416        nil nil nil
8417        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8418
8419 (defun gnus-url-parse-query-string (query &optional downcase)
8420   (let (retval pairs cur key val)
8421     (setq pairs (split-string query "&"))
8422     (while pairs
8423       (setq cur (car pairs)
8424             pairs (cdr pairs))
8425       (if (not (string-match "=" cur))
8426           nil                           ; Grace
8427         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8428               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8429         (if downcase
8430             (setq key (downcase key)))
8431         (setq cur (assoc key retval))
8432         (if cur
8433             (setcdr cur (cons val (cdr cur)))
8434           (setq retval (cons (list key val) retval)))))
8435     retval))
8436
8437 (defun gnus-url-mailto (url)
8438   ;; Send mail to someone
8439   (setq url (replace-regexp-in-string "\n" " " url))
8440   (when (string-match "mailto:/*\\(.*\\)" url)
8441     (setq url (substring url (match-beginning 1) nil)))
8442   (let (to args subject func)
8443     (setq args (gnus-url-parse-query-string
8444                 (if (string-match "^\\?" url)
8445                     (substring url 1)
8446                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8447                       (concat "to=" (match-string 1 url) "&"
8448                               (match-string 2 url))
8449                     (concat "to=" url))))
8450           subject (cdr-safe (assoc "subject" args)))
8451     (gnus-msg-mail)
8452     (while args
8453       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8454       (if (fboundp func)
8455           (funcall func)
8456         (message-position-on-field (caar args)))
8457       (insert (gnus-replace-in-string
8458                (mapconcat 'identity (reverse (cdar args)) ", ")
8459                "\r\n" "\n" t))
8460       (setq args (cdr args)))
8461     (if subject
8462         (message-goto-body)
8463       (message-goto-subject))))
8464
8465 (defun gnus-button-embedded-url (address)
8466   "Activate ADDRESS with `browse-url'."
8467   (browse-url (gnus-strip-whitespace address)))
8468
8469 ;;; Next/prev buttons in the article buffer.
8470
8471 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8472 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8473
8474 (defvar gnus-prev-page-map
8475   (let ((map (make-sparse-keymap)))
8476     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8477     (define-key map "\r" 'gnus-button-prev-page)
8478     map))
8479
8480 (defvar gnus-next-page-map
8481   (let ((map (make-sparse-keymap)))
8482     (define-key map gnus-mouse-2 'gnus-button-next-page)
8483     (define-key map "\r" 'gnus-button-next-page)
8484     map))
8485
8486 (defun gnus-insert-prev-page-button ()
8487   (let ((b (point)) e
8488         (inhibit-read-only t))
8489     (gnus-eval-format
8490      gnus-prev-page-line-format nil
8491      `(keymap ,gnus-prev-page-map
8492               gnus-prev t
8493               gnus-callback gnus-article-button-prev-page
8494               article-type annotation))
8495     (setq e (if (bolp)
8496                 ;; Exclude a newline.
8497                 (1- (point))
8498               (point)))
8499     (when gnus-article-button-face
8500       (gnus-overlay-put (gnus-make-overlay b e nil t)
8501                         'face gnus-article-button-face))
8502     (widget-convert-button
8503      'link b e
8504      :action 'gnus-button-prev-page
8505      :button-keymap gnus-prev-page-map)))
8506
8507 (defun gnus-button-next-page (&optional args more-args)
8508   "Go to the next page."
8509   (interactive)
8510   (let ((win (selected-window)))
8511     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8512     (gnus-article-next-page)
8513     (select-window win)))
8514
8515 (defun gnus-button-prev-page (&optional args more-args)
8516   "Go to the prev page."
8517   (interactive)
8518   (let ((win (selected-window)))
8519     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8520     (gnus-article-prev-page)
8521     (select-window win)))
8522
8523 (defun gnus-insert-next-page-button ()
8524   (let ((b (point)) e
8525         (inhibit-read-only t))
8526     (gnus-eval-format gnus-next-page-line-format nil
8527                       `(keymap ,gnus-next-page-map
8528                                gnus-next t
8529                                gnus-callback gnus-article-button-next-page
8530                                article-type annotation))
8531     (setq e (if (bolp)
8532                 ;; Exclude a newline.
8533                 (1- (point))
8534               (point)))
8535     (when gnus-article-button-face
8536       (gnus-overlay-put (gnus-make-overlay b e nil t)
8537                         'face gnus-article-button-face))
8538     (widget-convert-button
8539      'link b e
8540      :action 'gnus-button-next-page
8541      :button-keymap gnus-next-page-map)))
8542
8543 (defun gnus-article-button-next-page (arg)
8544   "Go to the next page."
8545   (interactive "P")
8546   (let ((win (selected-window)))
8547     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8548     (gnus-article-next-page)
8549     (select-window win)))
8550
8551 (defun gnus-article-button-prev-page (arg)
8552   "Go to the prev page."
8553   (interactive "P")
8554   (let ((win (selected-window)))
8555     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8556     (gnus-article-prev-page)
8557     (select-window win)))
8558
8559 (defvar gnus-decode-header-methods
8560   '(mail-decode-encoded-word-region)
8561   "List of methods used to decode headers.
8562
8563 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8564 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8565 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8566 whose names match REGEXP.
8567
8568 For example:
8569 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8570  mail-decode-encoded-word-region
8571  (\"chinese\" . rfc1843-decode-region))
8572 ")
8573
8574 (defvar gnus-decode-header-methods-cache nil)
8575
8576 (defun gnus-multi-decode-header (start end)
8577   "Apply the functions from `gnus-encoded-word-methods' that match."
8578   (unless (and gnus-decode-header-methods-cache
8579                (eq gnus-newsgroup-name
8580                    (car gnus-decode-header-methods-cache)))
8581     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8582     (dolist (x gnus-decode-header-methods)
8583       (if (symbolp x)
8584           (nconc gnus-decode-header-methods-cache (list x))
8585         (if (and gnus-newsgroup-name
8586                  (string-match (car x) gnus-newsgroup-name))
8587             (nconc gnus-decode-header-methods-cache
8588                    (list (cdr x)))))))
8589   (let ((xlist gnus-decode-header-methods-cache))
8590     (pop xlist)
8591     (save-restriction
8592       (narrow-to-region start end)
8593       (while xlist
8594         (funcall (pop xlist) (point-min) (point-max))))))
8595
8596 ;;;
8597 ;;; Treatment top-level handling.
8598 ;;;
8599
8600 (defvar gnus-inhibit-article-treatments nil)
8601
8602 (defun gnus-treat-article (gnus-treat-condition
8603                            &optional part-number total-parts gnus-treat-type)
8604   (let ((gnus-treat-length (- (point-max) (point-min)))
8605         (alist gnus-treatment-function-alist)
8606         (article-goto-body-goes-to-point-min-p t)
8607         (treated-type
8608          (or (not gnus-treat-type)
8609              (catch 'found
8610                (let ((list gnus-article-treat-types))
8611                  (while list
8612                    (when (string-match (pop list) gnus-treat-type)
8613                      (throw 'found t)))))))
8614         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8615         val elem)
8616     (gnus-run-hooks 'gnus-part-display-hook)
8617     (dolist (elem alist)
8618       (setq val
8619             (save-excursion
8620               (when (gnus-buffer-live-p gnus-summary-buffer)
8621                 (set-buffer gnus-summary-buffer))
8622               (symbol-value (car elem))))
8623       (when (and (or (consp val)
8624                      treated-type)
8625                  (or (not gnus-inhibit-article-treatments)
8626                      (eq gnus-treat-condition 'head))
8627                  (gnus-treat-predicate val)
8628                  (or (not (get (car elem) 'highlight))
8629                      highlightp))
8630         (save-restriction
8631           (funcall (cadr elem)))))))
8632
8633 ;; Dynamic variables.
8634 (defvar part-number)
8635 (defvar total-parts)
8636 (defvar gnus-treat-type)
8637 (defvar gnus-treat-condition)
8638 (defvar gnus-treat-length)
8639
8640 (defun gnus-treat-predicate (val)
8641   (cond
8642    ((null val)
8643     nil)
8644    (gnus-treat-condition
8645     (eq gnus-treat-condition val))
8646    ((and (listp val)
8647          (stringp (car val)))
8648     (apply 'gnus-or (mapcar `(lambda (s)
8649                                (string-match s ,(or gnus-newsgroup-name "")))
8650                             val)))
8651    ((listp val)
8652     (let ((pred (pop val)))
8653       (cond
8654        ((eq pred 'or)
8655         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8656        ((eq pred 'and)
8657         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8658        ((eq pred 'not)
8659         (not (gnus-treat-predicate (car val))))
8660        ((eq pred 'typep)
8661         (equal (car val) gnus-treat-type))
8662        ((functionp pred)
8663         (funcall pred))
8664        (t
8665         (error "%S is not a valid predicate" pred)))))
8666    ((eq val t)
8667     t)
8668    ((eq val 'head)
8669     nil)
8670    ((eq val 'first)
8671     (eq part-number 1))
8672    ((eq val 'last)
8673     (eq part-number total-parts))
8674    ((numberp val)
8675     (< gnus-treat-length val))
8676    (t
8677     (error "%S is not a valid value" val))))
8678
8679 (defun gnus-article-encrypt-body (protocol &optional n)
8680   "Encrypt the article body."
8681   (interactive
8682    (list
8683     (or gnus-article-encrypt-protocol
8684         (gnus-completing-read "Encrypt protocol"
8685                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8686                               t))
8687     current-prefix-arg))
8688   ;; User might hit `K E' instead of `K e', so prompt once.
8689   (when (and gnus-article-encrypt-protocol
8690              gnus-novice-user)
8691     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8692       (error "Encrypt aborted")))
8693   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8694     (unless func
8695       (error "Can't find the encrypt protocol %s" protocol))
8696     (if (member gnus-newsgroup-name '("nndraft:delayed"
8697                                       "nndraft:drafts"
8698                                       "nndraft:queue"))
8699         (error "Can't encrypt the article in group %s"
8700                gnus-newsgroup-name))
8701     (gnus-summary-iterate n
8702       (with-current-buffer gnus-summary-buffer
8703         (let ((mail-parse-charset gnus-newsgroup-charset)
8704               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8705               (summary-buffer gnus-summary-buffer)
8706               references point)
8707           (gnus-set-global-variables)
8708           (when (gnus-group-read-only-p)
8709             (error "The current newsgroup does not support article encrypt"))
8710           (gnus-summary-show-article t)
8711           (setq references
8712               (or (mail-header-references gnus-current-headers) ""))
8713           (set-buffer gnus-article-buffer)
8714           (let* ((inhibit-read-only t)
8715                  (headers
8716                   (mapcar (lambda (field)
8717                             (and (save-restriction
8718                                    (message-narrow-to-head)
8719                                    (goto-char (point-min))
8720                                    (search-forward field nil t))
8721                                  (prog2
8722                                      (message-narrow-to-field)
8723                                      (buffer-string)
8724                                    (delete-region (point-min) (point-max))
8725                                    (widen))))
8726                           '("Content-Type:" "Content-Transfer-Encoding:"
8727                             "Content-Disposition:"))))
8728             (message-narrow-to-head)
8729             (message-remove-header "MIME-Version")
8730             (goto-char (point-max))
8731             (setq point (point))
8732             (insert (apply 'concat headers))
8733             (widen)
8734             (narrow-to-region point (point-max))
8735             (let ((message-options message-options))
8736               (message-options-set 'message-sender user-mail-address)
8737               (message-options-set 'message-recipients user-mail-address)
8738               (message-options-set 'message-sign-encrypt 'not)
8739               (funcall func))
8740             (goto-char (point-min))
8741             (insert "MIME-Version: 1.0\n")
8742             (widen)
8743             (gnus-summary-edit-article-done
8744              references nil summary-buffer t))
8745           (when gnus-keep-backlog
8746             (gnus-backlog-remove-article
8747              (car gnus-article-current) (cdr gnus-article-current)))
8748           (gnus-flush-original-article-buffer)
8749           (when gnus-use-cache
8750             (gnus-cache-update-article
8751              (car gnus-article-current) (cdr gnus-article-current))))))))
8752
8753 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8754   "The following specs can be used:
8755 %t  The security MIME type
8756 %i  Additional info
8757 %d  Details
8758 %D  Details if button is pressed")
8759
8760 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8761   "The following specs can be used:
8762 %t  The security MIME type
8763 %i  Additional info
8764 %d  Details
8765 %D  Details if button is pressed")
8766
8767 (defvar gnus-mime-security-button-line-format-alist
8768   '((?t gnus-tmp-type ?s)
8769     (?i gnus-tmp-info ?s)
8770     (?d gnus-tmp-details ?s)
8771     (?D gnus-tmp-pressed-details ?s)))
8772
8773 (defvar gnus-mime-security-button-commands
8774   '((gnus-article-press-button "\r" "Show Detail")
8775     (undefined "v")
8776     (undefined "t")
8777     (undefined "C")
8778     (gnus-mime-security-save-part "o" "Save...")
8779     (undefined "\C-o")
8780     (undefined "r")
8781     (undefined "d")
8782     (undefined "c")
8783     (undefined "i")
8784     (undefined "E")
8785     (undefined "e")
8786     (undefined "p")
8787     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8788     (undefined ".")))
8789
8790 (defvar gnus-mime-security-button-map
8791   (let ((map (make-sparse-keymap)))
8792     (define-key map gnus-mouse-2 'gnus-article-push-button)
8793     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8794     (dolist (c gnus-mime-security-button-commands)
8795       (define-key map (cadr c) (car c)))
8796     map))
8797
8798 (easy-menu-define
8799   gnus-mime-security-button-menu gnus-mime-security-button-map
8800   "Security button menu."
8801   `("Security Part"
8802     ,@(delq nil
8803             (mapcar (lambda (c)
8804                       (unless (eq (car c) 'undefined)
8805                         (vector (caddr c) (car c) :active t)))
8806                     gnus-mime-security-button-commands))))
8807
8808 (defun gnus-mime-security-button-menu (event prefix)
8809   "Construct a context-sensitive menu of security commands."
8810   (interactive "e\nP")
8811   (save-window-excursion
8812     (let ((pos (event-start event)))
8813       (select-window (posn-window pos))
8814       (goto-char (posn-point pos))
8815       (gnus-article-check-buffer)
8816       (popup-menu gnus-mime-security-button-menu nil prefix))))
8817
8818 (defvar gnus-mime-security-details-buffer nil)
8819
8820 (defvar gnus-mime-security-button-pressed nil)
8821
8822 (defvar gnus-mime-security-show-details-inline t
8823   "If non-nil, show details in the article buffer.")
8824
8825 (defun gnus-mime-security-verify-or-decrypt (handle)
8826   (mm-remove-parts (cdr handle))
8827   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8828         point (inhibit-read-only t))
8829     (if region
8830         (goto-char (car region)))
8831     (setq point (point))
8832     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8833       (let* ((mm-verify-option 'known)
8834              (mm-decrypt-option 'known)
8835              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8836         (unless (eq nparts (cdr handle))
8837           (mm-destroy-parts (cdr handle))
8838           (setcdr handle nparts))))
8839     (gnus-mime-display-security handle)
8840     (when region
8841       (delete-region (point) (cdr region))
8842       (set-marker (car region) nil)
8843       (set-marker (cdr region) nil))
8844     (goto-char point)))
8845
8846 (defun gnus-mime-security-show-details (handle)
8847   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8848     (if (not details)
8849         (gnus-message 5 "No details.")
8850       (if gnus-mime-security-show-details-inline
8851           (let ((gnus-mime-security-button-pressed
8852                  (not (get-text-property (point) 'gnus-mime-details)))
8853                 (gnus-mime-security-button-line-format
8854                  (get-text-property (point) 'gnus-line-format))
8855                 (inhibit-read-only t))
8856             (forward-char -1)
8857             (while (eq (get-text-property (point) 'gnus-line-format)
8858                        gnus-mime-security-button-line-format)
8859               (forward-char -1))
8860             (forward-char)
8861             (save-restriction
8862               (narrow-to-region (point) (point))
8863               (gnus-insert-mime-security-button handle))
8864             (delete-region (point)
8865                            (or (text-property-not-all
8866                                 (point) (point-max)
8867                                 'gnus-line-format
8868                                 gnus-mime-security-button-line-format)
8869                                (point-max))))
8870         ;; Not inlined.
8871         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8872             (with-current-buffer gnus-mime-security-details-buffer
8873               (erase-buffer)
8874               t)
8875           (setq gnus-mime-security-details-buffer
8876                 (gnus-get-buffer-create "*MIME Security Details*")))
8877         (with-current-buffer gnus-mime-security-details-buffer
8878           (insert details)
8879           (goto-char (point-min)))
8880         (pop-to-buffer gnus-mime-security-details-buffer)))))
8881
8882 (defun gnus-mime-security-press-button (handle)
8883   (save-excursion
8884     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8885         (gnus-mime-security-show-details handle)
8886       (gnus-mime-security-verify-or-decrypt handle))))
8887
8888 (defun gnus-insert-mime-security-button (handle &optional displayed)
8889   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8890          (gnus-tmp-type
8891           (concat
8892            (or (nth 2 (assoc protocol mm-verify-function-alist))
8893                (nth 2 (assoc protocol mm-decrypt-function-alist))
8894                "Unknown")
8895            (if (equal (car handle) "multipart/signed")
8896                " Signed" " Encrypted")
8897            " Part"))
8898          (gnus-tmp-info
8899           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8900               "Undecided"))
8901          (gnus-tmp-details
8902           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8903          gnus-tmp-pressed-details
8904          b e)
8905     (setq gnus-tmp-details
8906           (if gnus-tmp-details
8907               (concat "\n" gnus-tmp-details)
8908             ""))
8909     (setq gnus-tmp-pressed-details
8910           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8911     (unless (bolp)
8912       (insert "\n"))
8913     (setq b (point))
8914     (gnus-eval-format
8915      gnus-mime-security-button-line-format
8916      gnus-mime-security-button-line-format-alist
8917      `(keymap ,gnus-mime-security-button-map
8918          gnus-callback gnus-mime-security-press-button
8919          gnus-line-format ,gnus-mime-security-button-line-format
8920          gnus-mime-details ,gnus-mime-security-button-pressed
8921          article-type annotation
8922          gnus-data ,handle))
8923     (setq e (if (bolp)
8924                 ;; Exclude a newline.
8925                 (1- (point))
8926               (point)))
8927     (when gnus-article-button-face
8928       (gnus-overlay-put (gnus-make-overlay b e nil t)
8929                         'face gnus-article-button-face))
8930     (widget-convert-button
8931      'link b e
8932      :mime-handle handle
8933      :action 'gnus-widget-press-button
8934      :button-keymap gnus-mime-security-button-map
8935      :help-echo
8936      (lambda (widget)
8937        ;; Needed to properly clear the message due to a bug in
8938        ;; wid-edit (XEmacs only).
8939        (when (boundp 'help-echo-owns-message)
8940          (setq help-echo-owns-message t))
8941        (format
8942         "%S: show detail; %S: more options"
8943         (aref gnus-mouse-2 0)
8944         (aref gnus-down-mouse-3 0))))))
8945
8946 (defun gnus-mime-display-security (handle)
8947   (save-restriction
8948     (narrow-to-region (point) (point))
8949     (unless (gnus-unbuttonized-mime-type-p (car handle))
8950       (gnus-insert-mime-security-button handle))
8951     (gnus-mime-display-part (cadr handle))
8952     (unless (bolp)
8953       (insert "\n"))
8954     (unless (gnus-unbuttonized-mime-type-p (car handle))
8955       (let ((gnus-mime-security-button-line-format
8956              gnus-mime-security-button-end-line-format))
8957         (gnus-insert-mime-security-button handle)))
8958     (mm-set-handle-multipart-parameter
8959      handle 'gnus-region (cons (point-min-marker) (point-max-marker)))
8960     (goto-char (point-max))))
8961
8962 (defun gnus-mime-security-run-function (function)
8963   "Run FUNCTION with the security part under point."
8964   (gnus-article-check-buffer)
8965   (let ((data (get-text-property (point) 'gnus-data))
8966         buffer handle)
8967     (when (and (stringp (car-safe data))
8968                (setq buffer (mm-handle-multipart-original-buffer data))
8969                (setq handle (cadr data)))
8970       (if (bufferp (mm-handle-buffer handle))
8971           (progn
8972             (setq handle (cons buffer (copy-sequence (cdr handle))))
8973             (mm-handle-set-undisplayer handle nil))
8974         (setq handle (mm-make-handle
8975                       buffer
8976                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8977                       nil nil nil nil nil nil)))
8978       (funcall function handle))))
8979
8980 (defun gnus-mime-security-save-part ()
8981   "Save the security part under point."
8982   (interactive)
8983   (gnus-mime-security-run-function 'mm-save-part))
8984
8985 (defun gnus-mime-security-pipe-part ()
8986   "Pipe the security part under point to a process."
8987   (interactive)
8988   (gnus-mime-security-run-function 'mm-pipe-part))
8989
8990 (gnus-ems-redefine)
8991
8992 (provide 'gnus-art)
8993
8994 (run-hooks 'gnus-art-load-hook)
8995
8996 ;;; gnus-art.el ends here