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