lisp/ChangeLog: Spelling fixes
[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     (when (and current-id (integerp gnus-auto-select-part))
5069       (gnus-article-jump-to-part
5070        (min (max (+ current-id gnus-auto-select-part) 1)
5071             (with-current-buffer gnus-article-buffer
5072               (length gnus-article-mime-handle-alist)))))))
5073
5074 (defun gnus-mime-replace-part (file)
5075   "Replace MIME part under point with an external body."
5076   ;; Useful if file has already been saved to disk
5077   (interactive
5078    (list
5079     (read-file-name "Replace MIME part with file: "
5080                     (or mm-default-directory default-directory)
5081                     nil nil)))
5082   (gnus-mime-save-part-and-strip file))
5083
5084 (defun gnus-mime-save-part-and-strip (&optional file)
5085   "Save the MIME part under point then replace it with an external body.
5086 If FILE is given, use it for the external part."
5087   (interactive)
5088   (gnus-article-check-buffer)
5089   (when (gnus-group-read-only-p)
5090     (error "The current group does not support deleting of parts"))
5091   (when (mm-complicated-handles gnus-article-mime-handles)
5092     (error "\
5093 The current article has a complicated MIME structure, giving up..."))
5094   (let* ((data (get-text-property (point) 'gnus-data))
5095          (id (get-text-property (point) 'gnus-part))
5096          (handles gnus-article-mime-handles))
5097     (unless file
5098       (setq file
5099             (and data (mm-save-part data "Delete MIME part and save to: "))))
5100     (when file
5101       (with-current-buffer (mm-handle-buffer data)
5102         (erase-buffer)
5103         (insert "Content-Type: " (mm-handle-media-type data))
5104         (mml-insert-parameter-string (cdr (mm-handle-type data))
5105                                      '(charset))
5106         ;; Add a filename for the sake of saving the part again.
5107         (mml-insert-parameter
5108          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5109         (insert "\n")
5110         (insert "Content-ID: " (message-make-message-id) "\n")
5111         (insert "Content-Transfer-Encoding: binary\n")
5112         (insert "\n"))
5113       (setcdr data
5114               (cdr (mm-make-handle nil
5115                                    `("message/external-body"
5116                                      (access-type . "LOCAL-FILE")
5117                                      (name . ,file)))))
5118       ;; (set-buffer gnus-summary-buffer)
5119       (gnus-article-edit-part handles id))))
5120
5121 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5122 ;; parts...>') but with stripping would be nice.
5123
5124 (defun gnus-mime-delete-part ()
5125   "Delete the MIME part under point.
5126 Replace it with some information about the removed part."
5127   (interactive)
5128   (gnus-article-check-buffer)
5129   (when (gnus-group-read-only-p)
5130     (error "The current group does not support deleting of parts"))
5131   (when (mm-complicated-handles gnus-article-mime-handles)
5132     (error "\
5133 The current article has a complicated MIME structure, giving up..."))
5134   (when (or gnus-expert-user
5135             (gnus-yes-or-no-p "\
5136 Deleting parts may malfunction or destroy the article; continue? "))
5137     (let* ((data (get-text-property (point) 'gnus-data))
5138            (id (get-text-property (point) 'gnus-part))
5139            (handles gnus-article-mime-handles)
5140            (description
5141             (let ((desc (mm-handle-description data)))
5142               (when desc
5143                 (mail-decode-encoded-word-string desc))))
5144            (filename (or (mm-handle-filename data) "(none)"))
5145            (type (mm-handle-media-type data)))
5146       (unless data
5147         (error "No MIME part under point"))
5148       (with-current-buffer (mm-handle-buffer data)
5149         (let ((bsize (buffer-size)))
5150           (erase-buffer)
5151           (insert
5152            (concat
5153             ",----\n"
5154             "| The following attachment has been deleted:\n"
5155             "|\n"
5156             "| Type:           " type "\n"
5157             "| Filename:       " filename "\n"
5158             "| Size (encoded): " (format "%s byte%s\n"
5159                                          bsize (if (= bsize 1)
5160                                                    ""
5161                                                  "s"))
5162             (when description
5163               (concat    "| Description:    " description "\n"))
5164             "`----\n"))
5165           (setcdr data
5166                   (cdr (mm-make-handle
5167                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5168                         (list "attachment")
5169                         (format "Deleted attachment (%s bytes)" bsize))))))
5170       ;; (set-buffer gnus-summary-buffer)
5171       (gnus-article-edit-part handles id))))
5172
5173 (defun gnus-mime-save-part ()
5174   "Save the MIME part under point."
5175   (interactive)
5176   (gnus-article-check-buffer)
5177   (let ((data (get-text-property (point) 'gnus-data)))
5178     (when data
5179       (mm-save-part data))))
5180
5181 (defun gnus-mime-pipe-part (&optional cmd)
5182   "Pipe the MIME part under point to a process.
5183 Use CMD as the process."
5184   (interactive)
5185   (gnus-article-check-buffer)
5186   (let ((data (get-text-property (point) 'gnus-data)))
5187     (when data
5188       (mm-pipe-part data cmd))))
5189
5190 (defun gnus-mime-view-part ()
5191   "Interactively choose a viewing method for the MIME part under point."
5192   (interactive)
5193   (gnus-article-check-buffer)
5194   (let ((data (get-text-property (point) 'gnus-data)))
5195     (when data
5196       (setq gnus-article-mime-handles
5197             (mm-merge-handles
5198              gnus-article-mime-handles (setq data (copy-sequence data))))
5199       (mm-interactively-view-part data))))
5200
5201 (defun gnus-mime-view-part-as-type-internal ()
5202   (gnus-article-check-buffer)
5203   (let* ((handle (get-text-property (point) 'gnus-data))
5204          (name (or
5205                 ;; Content-Type: foo/bar; name=...
5206                 (mail-content-type-get (mm-handle-type handle) 'name)
5207                 ;; Content-Disposition: attachment; filename=...
5208                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5209          (def-type (and name (mm-default-file-encoding name))))
5210     (or (and def-type (cons def-type 0))
5211         (and handle
5212              (equal (mm-handle-media-supertype handle) "text")
5213              '("text/plain" . 0))
5214         '("application/octet-stream" . 0))))
5215
5216 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5217   "Choose a MIME media type, and view the part as such.
5218 If non-nil, PRED is a predicate to use during completion to limit the
5219 available media-types."
5220   (interactive)
5221   (unless mime-type
5222     (setq mime-type
5223           (let ((default (gnus-mime-view-part-as-type-internal)))
5224             (gnus-completing-read
5225              "View as MIME type"
5226              (if pred
5227                  (gnus-remove-if-not pred (mailcap-mime-types))
5228                (mailcap-mime-types))
5229              nil nil nil
5230              (car default)))))
5231   (gnus-article-check-buffer)
5232   (let ((handle (get-text-property (point) 'gnus-data)))
5233     (when handle
5234       (when (equal (mm-handle-media-type handle) "message/external-body")
5235         (unless (mm-handle-cache handle)
5236           (mm-extern-cache-contents handle))
5237         (setq handle (mm-handle-cache handle)))
5238       (setq handle
5239             (mm-make-handle (mm-handle-buffer handle)
5240                             (cons mime-type (cdr (mm-handle-type handle)))
5241                             (mm-handle-encoding handle)
5242                             (mm-handle-undisplayer handle)
5243                             (mm-handle-disposition handle)
5244                             (mm-handle-description handle)
5245                             nil
5246                             (mm-handle-id handle)))
5247       (setq gnus-article-mime-handles
5248             (mm-merge-handles gnus-article-mime-handles handle))
5249       (when (mm-handle-displayed-p handle)
5250         (mm-remove-part handle))
5251       (gnus-mm-display-part handle))))
5252
5253 (defun gnus-mime-copy-part (&optional handle arg)
5254   "Put the MIME part under point into a new buffer.
5255 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5256 are decompressed."
5257   (interactive (list nil current-prefix-arg))
5258   (gnus-article-check-buffer)
5259   (unless handle
5260     (setq handle (get-text-property (point) 'gnus-data)))
5261   (when handle
5262     (let ((filename (mm-handle-filename handle))
5263           contents dont-decode charset coding-system)
5264       (mm-with-unibyte-buffer
5265         (mm-insert-part handle)
5266         (setq contents (or (condition-case nil
5267                                (mm-decompress-buffer filename nil 'sig)
5268                              (error
5269                               (setq dont-decode t)
5270                               nil))
5271                            (buffer-string))))
5272       (setq filename (cond (filename (file-name-nondirectory filename))
5273                            (dont-decode "*raw data*")
5274                            (t "*decoded*")))
5275       (cond
5276        (dont-decode)
5277        ((not arg)
5278         (unless (setq charset (mail-content-type-get
5279                                (mm-handle-type handle) 'charset))
5280           (unless (setq coding-system (mm-with-unibyte-buffer
5281                                         (insert contents)
5282                                         (mm-find-buffer-file-coding-system)))
5283             (setq charset gnus-newsgroup-charset))))
5284        ((numberp arg)
5285         (setq charset (or (cdr (assq arg
5286                                      gnus-summary-show-article-charset-alist))
5287                           (mm-read-coding-system "Charset: ")))))
5288       (switch-to-buffer (generate-new-buffer filename))
5289       (if (or coding-system
5290               (and charset
5291                    (setq coding-system (mm-charset-to-coding-system
5292                                         charset nil t))
5293                    (not (eq coding-system 'ascii))))
5294           (progn
5295             (mm-enable-multibyte)
5296             (insert (mm-decode-coding-string contents coding-system))
5297             (setq buffer-file-coding-system
5298                   (if (boundp 'last-coding-system-used)
5299                       (symbol-value 'last-coding-system-used)
5300                     coding-system)))
5301         (mm-disable-multibyte)
5302         (insert contents)
5303         (setq buffer-file-coding-system mm-binary-coding-system))
5304       ;; We do it this way to make `normal-mode' set the appropriate mode.
5305       (unwind-protect
5306           (progn
5307             (setq buffer-file-name (expand-file-name filename))
5308             (normal-mode))
5309         (setq buffer-file-name nil))
5310       (goto-char (point-min)))))
5311
5312 (defun gnus-mime-print-part (&optional handle filename)
5313   "Print the MIME part under point."
5314   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5315   (gnus-article-check-buffer)
5316   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5317          (contents (and handle (mm-get-part handle)))
5318          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5319          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5320     (when contents
5321         (if printer
5322             (unwind-protect
5323                 (progn
5324                   (mm-save-part-to-file handle file)
5325                   (call-process shell-file-name nil
5326                                 (generate-new-buffer " *mm*")
5327                                 nil
5328                                 shell-command-switch
5329                                 (mm-mailcap-command
5330                                  printer file (mm-handle-type handle))))
5331               (delete-file file))
5332           (with-temp-buffer
5333             (insert contents)
5334             (gnus-print-buffer))
5335           (ps-despool filename)))))
5336
5337 (defun gnus-mime-inline-part (&optional handle arg)
5338   "Insert the MIME part under point into the current buffer.
5339 Compressed files like .gz and .bz2 are decompressed."
5340   (interactive (list nil current-prefix-arg))
5341   (gnus-article-check-buffer)
5342   (let* ((inhibit-read-only t)
5343          (b (point))
5344          (btn ;; position where the MIME button exists
5345           (if handle
5346               (if (eq handle (get-text-property b 'gnus-data))
5347                   b
5348                 (article-goto-body)
5349                 (or (text-property-any (point) (point-max) 'gnus-data handle)
5350                     (text-property-any (point-min) (point) 'gnus-data handle)))
5351             (setq handle (get-text-property b 'gnus-data))
5352             b))
5353          start)
5354     (when handle
5355       (when (= b (prog1
5356                      btn
5357                    (setq start (next-single-property-change btn 'gnus-data
5358                                                             nil (point-max))
5359                          btn (previous-single-property-change start
5360                                                               'gnus-data))))
5361         (setq b btn))
5362       (if (and (not arg) (mm-handle-undisplayer handle))
5363           (mm-remove-part handle)
5364         (cond
5365          ((not arg) nil)
5366          ((numberp arg)
5367           (if (mm-handle-undisplayer handle)
5368               (mm-remove-part handle)))
5369          ((mm-handle-undisplayer handle)
5370           (mm-remove-part handle)))
5371         (goto-char start)
5372         (unless (bolp)
5373           ;; This is a header button.
5374           (forward-line 1))
5375         (mm-display-inline handle))
5376       ;; Toggle the button appearance between `[button]...' and `[button]'.
5377       (goto-char btn)
5378       (let ((displayed-p (mm-handle-displayed-p handle)))
5379         (gnus-insert-mime-button handle (get-text-property btn 'gnus-part)
5380                                  (list displayed-p))
5381         (if (featurep 'emacs)
5382             (delete-region
5383              (point)
5384              (next-single-property-change (point) 'gnus-data nil (point-max)))
5385           (let* ((end (next-single-property-change (point) 'gnus-data))
5386                  (annots (annotations-at (or end (point-max)))))
5387             (delete-region (point)
5388                            (if end
5389                                (if annots (1+ end) end)
5390                              (point-max)))
5391             (dolist (annot annots)
5392               (set-extent-endpoints annot (point) (point)))))
5393         (setq start (point))
5394         (if (search-backward "\n\n" nil t)
5395             (progn
5396               (goto-char start)
5397               (unless (or displayed-p (eolp))
5398                 ;; Add extra newline.
5399                 (insert (propertize (buffer-substring (1- start) start)
5400                                     'gnus-undeletable t))))
5401           ;; We're in the article header.
5402           (delete-char -1)
5403           (dolist (ovl (gnus-overlays-in btn (point)))
5404             (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
5405             (gnus-overlay-put ovl 'face nil))
5406           (save-restriction
5407             (message-narrow-to-field)
5408             (let ((gnus-treatment-function-alist
5409                    '((gnus-treat-highlight-headers
5410                       gnus-article-highlight-headers))))
5411               (gnus-treat-article 'head)))))
5412       (goto-char b))))
5413
5414 (defun gnus-mime-set-charset-parameters (handle charset)
5415   "Set CHARSET to parameters in HANDLE.
5416 CHARSET may either be a string or a symbol."
5417   (unless (stringp charset)
5418     (setq charset (symbol-name charset)))
5419   (if (stringp (car handle))
5420       (dolist (h (cdr handle))
5421         (gnus-mime-set-charset-parameters h charset))
5422     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5423                                             "message/external-body")
5424                                      (progn
5425                                        (unless (mm-handle-cache handle)
5426                                          (mm-extern-cache-contents handle))
5427                                        (mm-handle-cache handle))
5428                                    handle)))
5429            (param (assq 'charset (cdr type))))
5430       (if param
5431           (setcdr param charset)
5432         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5433
5434 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5435   "Insert the MIME part under point into the current buffer using the
5436 specified charset."
5437   (interactive (list nil current-prefix-arg))
5438   (gnus-article-check-buffer)
5439   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5440         (fun (get-text-property (point) 'gnus-callback))
5441         (gnus-newsgroup-ignored-charsets 'gnus-all)
5442         charset form preferred parts)
5443     (when handle
5444       (when (prog1
5445                 (and fun
5446                      (setq charset
5447                            (or (cdr (assq
5448                                      arg
5449                                      gnus-summary-show-article-charset-alist))
5450                                (mm-read-coding-system "Charset: "))))
5451               (if (mm-handle-undisplayer handle)
5452                   (mm-remove-part handle)))
5453         (gnus-mime-set-charset-parameters handle charset)
5454         (when (and (consp (setq form (cdr-safe fun)))
5455                    (setq form (ignore-errors
5456                                 (assq 'gnus-mime-display-alternative form)))
5457                    (setq preferred (caddr form))
5458                    (progn
5459                      (when (eq (car preferred) 'quote)
5460                        (setq preferred (cadr preferred)))
5461                      (not (equal preferred
5462                                  (get-text-property (point) 'gnus-data))))
5463                    (setq parts (get-text-property (point) 'gnus-part))
5464                    (setq parts (cdr (assq parts
5465                                           gnus-article-mime-handle-alist)))
5466                    (equal (mm-handle-media-type parts) "multipart/alternative")
5467                    (setq parts (reverse (cdr parts))))
5468           (setcar (cddr form)
5469                   (list 'quote (or (cadr (member preferred parts))
5470                                    (car parts)))))
5471         (funcall fun handle)))))
5472
5473 (defun gnus-mime-view-part-externally (&optional handle)
5474   "View the MIME part under point with an external viewer."
5475   (interactive)
5476   (gnus-article-check-buffer)
5477   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5478          (mm-inlined-types nil)
5479          (mail-parse-charset gnus-newsgroup-charset)
5480          (mail-parse-ignored-charsets
5481           (with-current-buffer gnus-summary-buffer
5482             gnus-newsgroup-ignored-charsets))
5483          (type (mm-handle-media-type handle))
5484          (method (mailcap-mime-info type))
5485          (mm-enable-external t))
5486     (if (not (stringp method))
5487         (gnus-mime-view-part-as-type
5488          nil (lambda (type) (stringp (mailcap-mime-info type))))
5489       (when handle
5490         (mm-display-part handle nil t)))))
5491
5492 (defun gnus-mime-view-part-internally (&optional handle)
5493   "View the MIME part under point with an internal viewer.
5494 If no internal viewer is available, use an external viewer."
5495   (interactive)
5496   (gnus-article-check-buffer)
5497   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5498          (mm-inlined-types '(".*"))
5499          (mm-inline-large-images t)
5500          (mail-parse-charset gnus-newsgroup-charset)
5501          (mail-parse-ignored-charsets
5502           (with-current-buffer gnus-summary-buffer
5503             gnus-newsgroup-ignored-charsets))
5504          (inhibit-read-only t))
5505     (if (not (mm-inlinable-p handle))
5506         (gnus-mime-view-part-as-type
5507          nil (lambda (type) (mm-inlinable-p handle type)))
5508       (when handle
5509         (gnus-bind-safe-url-regexp
5510          (mm-display-part handle nil t))))))
5511
5512 (defun gnus-mime-action-on-part (&optional action)
5513   "Do something with the MIME attachment at \(point\)."
5514   (interactive
5515    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5516   (gnus-article-check-buffer)
5517   (let ((action-pair (assoc action gnus-mime-action-alist)))
5518     (if action-pair
5519         (funcall (cdr action-pair)))))
5520
5521 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5522   "Call FUNCTION on MIME part N.
5523 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5524 If INTERACTIVE, call FUNCTION interactively."
5525   (let (window frame)
5526     ;; Check whether the article is displayed.
5527     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5528                  (setq window (get-buffer-window gnus-article-buffer t))
5529                  (frame-visible-p (setq frame (window-frame window))))
5530       (error "No article is displayed"))
5531     (with-current-buffer gnus-article-buffer
5532       ;; Check whether the article displays the right contents.
5533       (unless (with-current-buffer gnus-summary-buffer
5534                 (eq gnus-current-article (gnus-summary-article-number)))
5535         (error "You should select the right article first"))
5536       (if n
5537           (setq n (prefix-numeric-value n))
5538         (let ((pt (point)))
5539           (setq n (or (get-text-property pt 'gnus-part)
5540                       (and (not (bobp))
5541                            (get-text-property (1- pt) 'gnus-part))
5542                       (get-text-property (prog2
5543                                              (forward-line 1)
5544                                              (point)
5545                                            (goto-char pt))
5546                                          'gnus-part)
5547                       (get-text-property
5548                        (or (and (setq pt (previous-single-property-change
5549                                           pt 'gnus-part))
5550                                 (1- pt))
5551                            (next-single-property-change (point) 'gnus-part)
5552                            (point))
5553                        'gnus-part)
5554                       1))))
5555       ;; Check whether the specified part exists.
5556       (when (> n (length gnus-article-mime-handle-alist))
5557         (error "No such part")))
5558     (unless
5559         (progn
5560           ;; To select the window is needed so that the cursor
5561           ;; might be visible on the MIME button.
5562           (select-window (prog1
5563                              window
5564                            (setq window (selected-window))
5565                            ;; Article may be displayed in the other frame.
5566                            (gnus-select-frame-set-input-focus
5567                             (prog1
5568                                 frame
5569                               (setq frame (selected-frame))))))
5570           (when (gnus-article-goto-part n)
5571             ;; We point the cursor and the arrow at the MIME button
5572             ;; when the `function' prompt the user for something.
5573             (unless (and (pos-visible-in-window-p)
5574                          (> (count-lines (point) (window-end))
5575                             (/ (1- (window-height)) 3)))
5576               (recenter (/ (1- (window-height)) 3)))
5577             (let ((cursor-in-non-selected-windows t)
5578                   (overlay-arrow-string "=>")
5579                   (overlay-arrow-position (point-marker)))
5580               (unwind-protect
5581                   (cond
5582                    ((and no-handle interactive)
5583                     (call-interactively function))
5584                    (no-handle
5585                     (funcall function))
5586                    (interactive
5587                     (call-interactively
5588                      function (get-text-property (point) 'gnus-data)))
5589                    (t
5590                     (funcall function
5591                              (get-text-property (point) 'gnus-data))))
5592                 (set-marker overlay-arrow-position nil)
5593                 (unless gnus-auto-select-part
5594                   (gnus-select-frame-set-input-focus frame)
5595                   (select-window window))))
5596             t))
5597       (if gnus-inhibit-mime-unbuttonizing
5598           ;; This is the default though the program shouldn't reach here.
5599           (error "No such part")
5600         ;; The part which doesn't have the MIME button is selected.
5601         ;; So, we display all the buttons and redo it.
5602         (let ((gnus-inhibit-mime-unbuttonizing t))
5603           (gnus-summary-show-article)
5604           (gnus-article-part-wrapper n function no-handle))))))
5605
5606 (defun gnus-article-pipe-part (n)
5607   "Pipe MIME part N, which is the numerical prefix."
5608   (interactive "P")
5609   (gnus-article-part-wrapper n 'mm-pipe-part))
5610
5611 (defun gnus-article-save-part (n)
5612   "Save MIME part N, which is the numerical prefix."
5613   (interactive "P")
5614   (gnus-article-part-wrapper n 'mm-save-part))
5615
5616 (defun gnus-article-interactively-view-part (n)
5617   "View MIME part N interactively, which is the numerical prefix."
5618   (interactive "P")
5619   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5620
5621 (defun gnus-article-copy-part (n)
5622   "Copy MIME part N, which is the numerical prefix."
5623   (interactive "P")
5624   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5625
5626 (defun gnus-article-view-part-as-charset (n)
5627   "View MIME part N using a specified charset.
5628 N is the numerical prefix."
5629   (interactive "P")
5630   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5631
5632 (defun gnus-article-view-part-externally (n)
5633   "View MIME part N externally, which is the numerical prefix."
5634   (interactive "P")
5635   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5636
5637 (defun gnus-article-inline-part (n)
5638   "Inline MIME part N, which is the numerical prefix."
5639   (interactive "P")
5640   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5641
5642 (defun gnus-article-save-part-and-strip (n)
5643   "Save MIME part N and replace it with an external body.
5644 N is the numerical prefix."
5645   (interactive "P")
5646   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5647
5648 (defun gnus-article-replace-part (n)
5649   "Replace MIME part N with an external body.
5650 N is the numerical prefix."
5651   (interactive "P")
5652   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5653
5654 (defun gnus-article-delete-part (n)
5655   "Delete MIME part N and add some information about the removed part.
5656 N is the numerical prefix."
5657   (interactive "P")
5658   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5659
5660 (defun gnus-article-view-part-as-type (n)
5661   "Choose a MIME media type, and view part N as such.
5662 N is the numerical prefix."
5663   (interactive "P")
5664   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5665
5666 (defun gnus-article-mime-match-handle-first (condition)
5667   (if condition
5668       (let (n)
5669         (dolist (ihandle gnus-article-mime-handle-alist)
5670           (if (and (cond
5671                     ((functionp condition)
5672                      (funcall condition (cdr ihandle)))
5673                     ((eq condition 'undisplayed)
5674                      (not (or (mm-handle-undisplayer (cdr ihandle))
5675                               (equal (mm-handle-media-type (cdr ihandle))
5676                                      "multipart/alternative"))))
5677                     ((eq condition 'undisplayed-alternative)
5678                      (not (mm-handle-undisplayer (cdr ihandle))))
5679                     (t t))
5680                    (gnus-article-goto-part (car ihandle))
5681                    (or (not n) (< (car ihandle) n)))
5682               (setq n (car ihandle))))
5683         (or n 1))
5684     1))
5685
5686 (defun gnus-article-view-part (&optional n)
5687   "View MIME part N, which is the numerical prefix.
5688 If the part is already shown, hide the part.  If N is nil, view
5689 all parts."
5690   (interactive "P")
5691   (with-current-buffer gnus-article-buffer
5692     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5693                              gnus-article-mime-match-handle-function)))
5694     (when (> n (length gnus-article-mime-handle-alist))
5695       (error "No such part"))
5696     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5697       (when (gnus-article-goto-part n)
5698         (if (equal (car handle) "multipart/alternative")
5699             (progn
5700               (beginning-of-line) ;; Make it toggle subparts
5701               (gnus-article-press-button))
5702           (when (eq (gnus-mm-display-part handle) 'internal)
5703             (gnus-set-window-start)))))))
5704
5705 (defsubst gnus-article-mime-total-parts ()
5706   (if (bufferp (car gnus-article-mime-handles))
5707       1 ;; single part
5708     (1- (length gnus-article-mime-handles))))
5709
5710 (defun gnus-mm-display-part (handle)
5711   "Display HANDLE and fix MIME button."
5712   (let ((id (get-text-property (point) 'gnus-part))
5713         (point (point))
5714         (inhibit-read-only t)
5715         (window (selected-window))
5716         (mail-parse-charset gnus-newsgroup-charset)
5717         (mail-parse-ignored-charsets
5718          (if (gnus-buffer-live-p gnus-summary-buffer)
5719              (with-current-buffer gnus-summary-buffer
5720                gnus-newsgroup-ignored-charsets)
5721            nil))
5722         start retval)
5723     (unwind-protect
5724         (progn
5725           (let ((win (gnus-get-buffer-window (current-buffer) t)))
5726             (when win
5727               (select-window win)
5728               (goto-char point)))
5729           (setq start (next-single-property-change point 'gnus-data
5730                                                    nil (point-max))
5731                 point (previous-single-property-change start 'gnus-data))
5732           (if (mm-handle-displayed-p handle)
5733               ;; This will remove the part.
5734               (setq retval (mm-display-part handle))
5735             (let ((part (or (and (mm-inlinable-p handle)
5736                                  (mm-inlined-p handle)
5737                                  t)
5738                             (with-temp-buffer
5739                               (gnus-bind-safe-url-regexp
5740                                (setq retval (mm-display-part handle)))
5741                               (unless (zerop (buffer-size))
5742                                 (buffer-string))))))
5743               (goto-char start)
5744               (unless (bolp)
5745                 ;; This is a header button.
5746                 (forward-line 1))
5747               (cond ((stringp part)
5748                      (save-restriction
5749                        (narrow-to-region (point)
5750                                          (progn
5751                                            (insert part)
5752                                            (unless (bolp) (insert "\n"))
5753                                            (point)))
5754                        (gnus-treat-article nil id
5755                                            (gnus-article-mime-total-parts)
5756                                            (mm-handle-media-type handle))
5757                        (mm-handle-set-undisplayer
5758                         handle
5759                         `(lambda ()
5760                            (let ((inhibit-read-only t))
5761                              (delete-region ,(copy-marker (point-min) t)
5762                                             ,(point-max-marker)))))))
5763                     (part
5764                      (mm-display-inline handle))))))
5765       (goto-char point)
5766       ;; Toggle the button appearance between `[button]...' and `[button]'.
5767       (let ((displayed-p (mm-handle-displayed-p handle)))
5768         (gnus-insert-mime-button handle id (list displayed-p))
5769         (if (featurep 'emacs)
5770             (delete-region
5771              (point)
5772              (next-single-property-change (point) 'gnus-data nil (point-max)))
5773           (let* ((end (next-single-property-change (point) 'gnus-data))
5774                  (annots (annotations-at (or end (point-max)))))
5775             (delete-region (point)
5776                            (if end
5777                                (if annots (1+ end) end)
5778                              (point-max)))
5779             (dolist (annot annots)
5780               (set-extent-endpoints annot (point) (point)))))
5781         (setq start (point))
5782         (if (search-backward "\n\n" nil t)
5783             (progn
5784               (goto-char start)
5785               (unless (or displayed-p (eolp))
5786                 ;; Add extra newline.
5787                 (insert (propertize (buffer-substring (1- start) start)
5788                                     'gnus-undeletable t))))
5789           ;; We're in the article header.
5790           (delete-char -1)
5791           (dolist (ovl (gnus-overlays-in point (point)))
5792             (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
5793             (gnus-overlay-put ovl 'face nil))
5794           (save-restriction
5795             (message-narrow-to-field)
5796             (let ((gnus-treatment-function-alist
5797                    '((gnus-treat-highlight-headers
5798                       gnus-article-highlight-headers))))
5799               (gnus-treat-article 'head)))))
5800       (goto-char point)
5801       (if (window-live-p window)
5802           (select-window window)))
5803     retval))
5804
5805 (defun gnus-article-goto-part (n)
5806   "Go to MIME part N."
5807   (when gnus-break-pages
5808     (widen))
5809   (article-goto-body)
5810   (prog1
5811       (let ((start (or (text-property-any (point) (point-max) 'gnus-part n)
5812                        ;; There may be header buttons.
5813                        (text-property-any (point-min) (point) 'gnus-part n)))
5814             part handle end next handles)
5815         (when start
5816           (goto-char start)
5817           (if (setq handle (get-text-property start 'gnus-data))
5818               start
5819             ;; Go to the displayed subpart, assuming this is
5820             ;; multipart/alternative.
5821             (setq part start
5822                   end (point-at-eol))
5823             (while (and (not handle)
5824                         part
5825                         (< part end)
5826                         (setq next (text-property-not-all part end
5827                                                           'gnus-data nil)))
5828               (setq part next
5829                     handle (get-text-property part 'gnus-data))
5830               (push (cons handle part) handles)
5831               (unless (mm-handle-displayed-p handle)
5832                 (setq handle nil
5833                       part (text-property-any part end 'gnus-data nil))))
5834             (unless handle
5835               ;; No subpart is displayed, so we find preferred one.
5836               (setq part
5837                     (cdr (assq (mm-preferred-alternative
5838                                 (nreverse (mapcar 'car handles)))
5839                                handles))))
5840             (if part
5841                 (goto-char (1+ part))
5842               start))))
5843     (when gnus-break-pages
5844       (gnus-narrow-to-page))))
5845
5846 (defun gnus-insert-mime-button (handle id &optional displayed)
5847   (let ((gnus-tmp-name
5848          (or (mm-handle-filename handle)
5849              (mail-content-type-get (mm-handle-type handle) 'url)
5850              ""))
5851         (gnus-tmp-id id)
5852         (gnus-tmp-type (mm-handle-media-type handle))
5853         (gnus-tmp-description (or (mm-handle-description handle) ""))
5854         (gnus-tmp-dots
5855          (if (if displayed (car displayed)
5856                (mm-handle-displayed-p handle))
5857              "" "..."))
5858         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5859                            (buffer-size)))
5860         gnus-tmp-type-long b e)
5861     (when (string-match ".*/" gnus-tmp-name)
5862       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5863     (setq gnus-tmp-type-long (concat gnus-tmp-type
5864                                      (and (not (equal gnus-tmp-name ""))
5865                                           (concat "; " gnus-tmp-name))))
5866     (unless (equal gnus-tmp-description "")
5867       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5868     (setq b (point))
5869     (gnus-eval-format
5870      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5871      `(keymap ,gnus-mime-button-map
5872               gnus-callback gnus-mm-display-part
5873               gnus-part ,gnus-tmp-id
5874               article-type annotation
5875               gnus-data ,handle
5876               rear-nonsticky t))
5877     (setq e (if (bolp)
5878                 ;; Exclude a newline.
5879                 (1- (point))
5880               (point)))
5881     (when gnus-article-button-face
5882       (gnus-overlay-put (gnus-make-overlay b e nil t)
5883                         'face gnus-article-button-face))
5884     (widget-convert-button
5885      'link b e
5886      :mime-handle handle
5887      :action 'gnus-widget-press-button
5888      :button-keymap gnus-mime-button-map
5889      :help-echo
5890      (lambda (widget)
5891        ;; Needed to properly clear the message due to a bug in
5892        ;; wid-edit (XEmacs only).
5893        (if (boundp 'help-echo-owns-message)
5894            (setq help-echo-owns-message t))
5895        (format
5896         "%S: %s the MIME part; %S: more options"
5897         (aref gnus-mouse-2 0)
5898         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5899             "hide" "show")
5900         (aref gnus-down-mouse-3 0))))))
5901
5902 (defun gnus-widget-press-button (elems _el)
5903   (goto-char (widget-get elems :from))
5904   (gnus-article-press-button))
5905
5906 (defvar gnus-displaying-mime nil)
5907
5908 (defun gnus-display-mime (&optional ihandles)
5909   "Display the MIME parts."
5910   (save-excursion
5911     (save-selected-window
5912       (let ((window (get-buffer-window gnus-article-buffer))
5913             (point (point)))
5914         (when window
5915           (select-window window)
5916           ;; We have to do this since selecting the window
5917           ;; may change the point.  So we set the window point.
5918           (set-window-point window point)))
5919       (let ((handles ihandles)
5920             (inhibit-read-only t))
5921         (cond (handles)
5922               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5923                (when gnus-article-emulate-mime
5924                  (mm-uu-dissect-text-parts handles)))
5925               (gnus-article-emulate-mime
5926                (setq handles (mm-uu-dissect))))
5927         (when (and (not ihandles)
5928                    (not gnus-displaying-mime))
5929           ;; Top-level call; we clean up.
5930           (when gnus-article-mime-handles
5931             (mm-destroy-parts gnus-article-mime-handles)
5932             (setq gnus-article-mime-handle-alist nil));; A trick.
5933           (setq gnus-article-mime-handles handles)
5934           ;; We allow users to glean info from the handles.
5935           (when gnus-article-mime-part-function
5936             (gnus-mime-part-function handles)))
5937         (if (and handles
5938                  (or (not (stringp (car handles)))
5939                      (cdr handles)))
5940             (progn
5941               (when (and (not ihandles)
5942                          (not gnus-displaying-mime))
5943                 ;; Clean up for mime parts.
5944                 (article-goto-body)
5945                 (delete-region (point) (point-max)))
5946               (let ((gnus-displaying-mime t))
5947                 (gnus-mime-display-part handles)))
5948           (save-restriction
5949             (article-goto-body)
5950             (narrow-to-region (point) (point-max))
5951             (gnus-treat-article nil 1 1 "text/plain")
5952             (widen)))
5953         (unless ihandles
5954           ;; Highlight the headers.
5955           (save-excursion
5956             (save-restriction
5957               (article-goto-body)
5958               (narrow-to-region (point-min) (point))
5959               (gnus-article-save-original-date
5960                (gnus-treat-article 'head)))))))
5961     ;; Cope with broken MIME messages.
5962     (goto-char (point-max))
5963     (unless (bolp)
5964       (insert "\n"))))
5965
5966 (defcustom gnus-mime-display-multipart-as-mixed nil
5967   "Display \"multipart\" parts as  \"multipart/mixed\".
5968
5969 If t, it overrides nil values of
5970 `gnus-mime-display-multipart-alternative-as-mixed' and
5971 `gnus-mime-display-multipart-related-as-mixed'."
5972   :group 'gnus-article-mime
5973   :type 'boolean)
5974
5975 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5976   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5977   :version "22.1"
5978   :group 'gnus-article-mime
5979   :type 'boolean)
5980
5981 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5982   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5983
5984 If displaying \"text/html\" is discouraged \(see
5985 `mm-discouraged-alternatives'\) images or other material inside a
5986 \"multipart/related\" part might be overlooked when this variable is nil."
5987   :version "22.1"
5988   :group 'gnus-article-mime
5989   :type 'boolean)
5990
5991 (defcustom gnus-mime-display-attachment-buttons-in-header t
5992   "Add attachment buttons in the end of the header of an article.
5993 Since MIME attachments tend to be put at the end of an article, we may
5994 overlook them if there is a huge body.  This option offers you a copy
5995 of all non-inlinable MIME parts as buttons shown in front of an article.
5996 If nil, don't show those extra buttons."
5997   :version "25.1"
5998   :group 'gnus-article-mime
5999   :type 'boolean)
6000
6001 (defun gnus-mime-display-part (handle)
6002   (cond
6003    ;; Maybe a broken MIME message.
6004    ((null handle))
6005    ;; Single part.
6006    ((not (stringp (car handle)))
6007     (gnus-mime-display-single handle))
6008    ;; User-defined multipart
6009    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
6010     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
6011              handle))
6012    ;; multipart/alternative
6013    ((and (equal (car handle) "multipart/alternative")
6014          (not (or gnus-mime-display-multipart-as-mixed
6015                   gnus-mime-display-multipart-alternative-as-mixed)))
6016     (let ((id (1+ (length gnus-article-mime-handle-alist))))
6017       (push (cons id handle) gnus-article-mime-handle-alist)
6018       (gnus-mime-display-alternative (cdr handle) nil nil id)))
6019    ;; multipart/related
6020    ((and (equal (car handle) "multipart/related")
6021          (not (or gnus-mime-display-multipart-as-mixed
6022                   gnus-mime-display-multipart-related-as-mixed)))
6023     (gnus-mime-display-part (cadr handle)))
6024    ((equal (car handle) "multipart/signed")
6025     (gnus-add-wash-type 'signed)
6026     (gnus-mime-display-security handle))
6027    ((equal (car handle) "multipart/encrypted")
6028     (gnus-add-wash-type 'encrypted)
6029     (gnus-mime-display-security handle))
6030    ;; Other multiparts are handled like multipart/mixed.
6031    (t
6032     (gnus-mime-display-mixed (cdr handle)))))
6033
6034 (defun gnus-mime-part-function (handles)
6035   (if (stringp (car handles))
6036       (mapcar 'gnus-mime-part-function (cdr handles))
6037     (funcall gnus-article-mime-part-function handles)))
6038
6039 (defun gnus-mime-display-mixed (handles)
6040   (mapcar 'gnus-mime-display-part handles))
6041
6042 (defun gnus-mime-display-single (handle)
6043   (let ((type (mm-handle-media-type handle))
6044         (ignored gnus-ignored-mime-types)
6045         (not-attachment t)
6046         display text)
6047     (catch 'ignored
6048       (progn
6049         (while ignored
6050           (when (string-match (pop ignored) type)
6051             (throw 'ignored nil)))
6052         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
6053                                (with-current-buffer gnus-summary-buffer
6054                                  gnus-inhibit-images)
6055                              gnus-inhibit-images)
6056                            (string-match "\\`image/" type)))
6057                  (setq not-attachment
6058                        (and (not (mm-inline-override-p handle))
6059                             (or (not (mm-handle-disposition handle))
6060                                 (equal (car (mm-handle-disposition handle))
6061                                        "inline")
6062                                 (mm-attachment-override-p handle))))
6063                  (mm-automatic-display-p handle)
6064                  (or (and
6065                       (mm-inlinable-p handle)
6066                       (mm-inlined-p handle))
6067                      (mm-automatic-external-display-p type)))
6068             (setq display t)
6069           (when (equal (mm-handle-media-supertype handle) "text")
6070             (setq text t)))
6071         (let ((id (car (rassq handle gnus-article-mime-handle-alist)))
6072               beg)
6073           (unless id
6074             (setq id (1+ (length gnus-article-mime-handle-alist)))
6075             (push (cons id handle) gnus-article-mime-handle-alist))
6076           (when (and display
6077                      (equal (mm-handle-media-supertype handle) "message"))
6078             (insert-char
6079              ?\n
6080              (cond ((not (bolp)) 2)
6081                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
6082                    (t 1))))
6083           (when (or (not display)
6084                     (not (gnus-unbuttonized-mime-type-p type))
6085                     (eq id gnus-mime-buttonized-part-id))
6086             (gnus-insert-mime-button
6087              handle id (list (or display (and not-attachment text)))))
6088           (setq beg (point))
6089           (cond
6090            (display
6091             (let ((mail-parse-charset gnus-newsgroup-charset)
6092                   (mail-parse-ignored-charsets
6093                    (save-excursion (condition-case ()
6094                                        (set-buffer gnus-summary-buffer)
6095                                      (error))
6096                                    gnus-newsgroup-ignored-charsets)))
6097               (gnus-bind-safe-url-regexp (mm-display-part handle t))))
6098            ((and text not-attachment)
6099             (mm-display-inline handle)))
6100           (goto-char (point-max))
6101           (if (string-match "\\`image/" type)
6102               (gnus-article-insert-newline)
6103             (if (prog1
6104                     (= (skip-chars-backward "\n") -1)
6105                   (forward-char 1))
6106                 (gnus-article-insert-newline)
6107               (put-text-property (point) (point-max) 'gnus-undeletable t))
6108             (goto-char (point-max)))
6109           ;; Do highlighting.
6110           (save-excursion
6111             (save-restriction
6112               (narrow-to-region beg (point))
6113               (if (eq handle gnus-article-mime-handles)
6114                   ;; The format=flowed case.
6115                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
6116                 ;; Don't count signature parts that are never displayed.
6117                 ;; The part number should be re-calculated supposing this
6118                 ;; might be a message/rfc822 part.
6119                 (let (handles)
6120                   (dolist (part gnus-article-mime-handles)
6121                     (unless (or (stringp part)
6122                                 (equal (car (mm-handle-type part))
6123                                        "application/pgp-signature"))
6124                       (push part handles)))
6125                   (gnus-treat-article
6126                    nil (length (memq handle handles)) (length handles)
6127                    (mm-handle-media-type handle)))))))))))
6128
6129 (defun gnus-unbuttonized-mime-type-p (type)
6130   "Say whether TYPE is to be unbuttonized."
6131   (unless gnus-inhibit-mime-unbuttonizing
6132     (when (catch 'found
6133             (let ((types gnus-unbuttonized-mime-types))
6134               (while types
6135                 (when (string-match (pop types) type)
6136                   (throw 'found t)))))
6137       (not (catch 'found
6138              (let ((types gnus-buttonized-mime-types))
6139                (while types
6140                  (when (string-match (pop types) type)
6141                    (throw 'found t)))))))))
6142
6143 (defun gnus-article-insert-newline ()
6144   "Insert a newline, but mark it as undeletable."
6145   (gnus-put-text-property
6146    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
6147
6148 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
6149   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
6150          (ihandles handles)
6151          (point (point))
6152          handle (inhibit-read-only t) from begend not-pref)
6153     (save-window-excursion
6154       (save-restriction
6155         (when ibegend
6156           (narrow-to-region (car ibegend)
6157                             (or (cdr ibegend)
6158                                 (progn
6159                                   (goto-char (car ibegend))
6160                                   (forward-line 2)
6161                                   (point))))
6162           (delete-region (point-min) (point-max))
6163           (mm-remove-parts handles))
6164         (setq begend (list (point-marker)))
6165         ;; Do the toggle.
6166         (unless (setq not-pref (cadr (member preferred ihandles)))
6167           (setq not-pref (car ihandles)))
6168         (when (or ibegend
6169                   (not preferred)
6170                   (not (gnus-unbuttonized-mime-type-p
6171                         "multipart/alternative")))
6172           (gnus-add-text-properties
6173            (setq from (point))
6174            (progn
6175              (insert (format "%d.  " id))
6176              (point))
6177            `(gnus-callback
6178              (lambda (handles)
6179                (unless ,(not ibegend)
6180                  (setq gnus-article-mime-handle-alist
6181                        ',gnus-article-mime-handle-alist))
6182                (gnus-mime-display-alternative
6183                 ',ihandles ',not-pref ',begend ,id))
6184              keymap ,gnus-mime-button-map
6185              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6186              face ,gnus-article-button-face
6187              gnus-part ,id
6188              article-type multipart
6189              rear-nonsticky t))
6190           (widget-convert-button 'link from (point)
6191                                  :action 'gnus-widget-press-button
6192                                  :button-keymap gnus-widget-button-keymap)
6193           ;; Do the handles
6194           (while (setq handle (pop handles))
6195             (gnus-add-text-properties
6196              (setq from (point))
6197              (progn
6198                (insert (format "(%c) %-18s"
6199                                (if (equal handle preferred) ?* ? )
6200                                (mm-handle-media-type handle)))
6201                (point))
6202              `(gnus-callback
6203                (lambda (handles)
6204                  (unless ,(not ibegend)
6205                    (setq gnus-article-mime-handle-alist
6206                          ',gnus-article-mime-handle-alist))
6207                  (gnus-mime-display-alternative
6208                   ',ihandles ',handle ',begend ,id))
6209                keymap ,gnus-mime-button-map
6210                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6211                face ,gnus-article-button-face
6212                gnus-part ,id
6213                gnus-data ,handle
6214                rear-nonsticky t))
6215             (widget-convert-button 'link from (point)
6216                                    :action 'gnus-widget-press-button
6217                                    :button-keymap gnus-widget-button-keymap)
6218             (insert "  "))
6219           (insert "\n\n"))
6220         (when preferred
6221           (if (stringp (car preferred))
6222               (gnus-display-mime preferred)
6223             (let ((mail-parse-charset gnus-newsgroup-charset)
6224                   (mail-parse-ignored-charsets
6225                    (with-current-buffer gnus-summary-buffer
6226                      gnus-newsgroup-ignored-charsets)))
6227               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6228               ;; Do highlighting.
6229               (save-excursion
6230                 (save-restriction
6231                   (narrow-to-region (car begend) (point-max))
6232                   (gnus-treat-article
6233                    nil (length gnus-article-mime-handle-alist)
6234                    (gnus-article-mime-total-parts)
6235                    (mm-handle-media-type preferred))))))
6236           (goto-char (point-max))
6237           (setcdr begend (point-marker)))))
6238     (when ibegend
6239       (goto-char point)))
6240   ;; Redraw attachment buttons in the header.
6241   (when gnus-mime-display-attachment-buttons-in-header
6242     (gnus-mime-buttonize-attachments-in-header)))
6243
6244 (defconst gnus-article-wash-status-strings
6245   (let ((alist '((cite "c" "Possible hidden citation text"
6246                        " " "All citation text visible")
6247                  (headers "h" "Hidden headers"
6248                           " " "All headers visible.")
6249                  (pgp "p" "Encrypted or signed message status hidden"
6250                       " " "No hidden encryption nor digital signature status")
6251                  (signature "s" "Signature has been hidden"
6252                             " " "Signature is visible")
6253                  (overstrike "o" "Overstrike (^H) characters applied"
6254                              " " "No overstrike characters applied")
6255                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6256                            " " "No /*_emphasis_*/ characters applied")))
6257         result)
6258     (dolist (entry alist result)
6259       (let ((key (nth 0 entry))
6260             (on (copy-sequence (nth 1 entry)))
6261             (on-help (nth 2 entry))
6262             (off (copy-sequence (nth 3 entry)))
6263             (off-help (nth 4 entry)))
6264         (put-text-property 0 1 'help-echo on-help on)
6265         (put-text-property 0 1 'help-echo off-help off)
6266         (push (list key on off) result))))
6267   "Alist of strings describing wash status in the mode line.
6268 Each entry has the form (KEY ON OF), where the KEY is a symbol
6269 representing the particular washing function, ON is the string to use
6270 in the article mode line when the washing function is active, and OFF
6271 is the string to use when it is inactive.")
6272
6273 (defun gnus-article-wash-status-entry (key value)
6274   (let ((entry (assoc key gnus-article-wash-status-strings)))
6275     (if value (nth 1 entry) (nth 2 entry))))
6276
6277 (defun gnus-article-wash-status ()
6278   "Return a string which display status of article washing."
6279   (with-current-buffer gnus-article-buffer
6280     (let ((cite (memq 'cite gnus-article-wash-types))
6281           (headers (memq 'headers gnus-article-wash-types))
6282           (boring (memq 'boring-headers gnus-article-wash-types))
6283           (pgp (memq 'pgp gnus-article-wash-types))
6284           (pem (memq 'pem gnus-article-wash-types))
6285           (signed (memq 'signed gnus-article-wash-types))
6286           (encrypted (memq 'encrypted gnus-article-wash-types))
6287           (signature (memq 'signature gnus-article-wash-types))
6288           (overstrike (memq 'overstrike gnus-article-wash-types))
6289           (emphasis (memq 'emphasis gnus-article-wash-types)))
6290       (concat
6291        (gnus-article-wash-status-entry 'cite cite)
6292        (gnus-article-wash-status-entry 'headers (or headers boring))
6293        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6294        (gnus-article-wash-status-entry 'signature signature)
6295        (gnus-article-wash-status-entry 'overstrike overstrike)
6296        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6297
6298 (defun gnus-add-wash-type (type)
6299   "Add a washing of TYPE to the current status."
6300   (add-to-list 'gnus-article-wash-types type))
6301
6302 (defun gnus-delete-wash-type (type)
6303   "Add a washing of TYPE to the current status."
6304   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6305
6306 (defun gnus-add-image (category image)
6307   "Add IMAGE of CATEGORY to the list of displayed images."
6308   (let ((entry (assq category gnus-article-image-alist)))
6309     (unless entry
6310       (setq entry (list category))
6311       (push entry gnus-article-image-alist))
6312     (nconc entry (list image))))
6313
6314 (defun gnus-delete-images (category)
6315   "Delete all images in CATEGORY."
6316   (let ((entry (assq category gnus-article-image-alist)))
6317     (dolist (image (cdr entry))
6318       (gnus-remove-image image category))
6319     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6320     (gnus-delete-wash-type category)))
6321
6322 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6323
6324 (defun gnus-article-maybe-hide-headers ()
6325   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6326 Provided for backwards compatibility."
6327   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6328                  (not (with-current-buffer gnus-summary-buffer
6329                         gnus-have-all-headers)))
6330              (not gnus-inhibit-hiding))
6331     (gnus-article-hide-headers)))
6332
6333 (declare-function shr-put-image "shr" (data alt &optional flags))
6334
6335 (defun gnus-shr-put-image (data alt &optional flags)
6336   "Put image DATA with a string ALT.  Enable image to be deleted."
6337   (let ((image (if flags
6338                    (shr-put-image data (propertize (or alt "*")
6339                                                    'gnus-image-category 'shr)
6340                                   flags)
6341                  ;; Old `shr-put-image' doesn't take the optional `flags'
6342                  ;; argument.
6343                  (shr-put-image data (propertize (or alt "*")
6344                                                  'gnus-image-category 'shr)))))
6345     (when image
6346       (gnus-add-image 'shr image))))
6347
6348 (defun gnus-article-mime-handles (&optional alist id all)
6349   (if alist
6350       (let ((i 1) newid flat)
6351         (dolist (handle alist flat)
6352           (setq newid (append id (list i))
6353                 i (1+ i))
6354           (if (stringp (car handle))
6355               (setq flat (nconc flat (gnus-article-mime-handles
6356                                       (cdr handle) newid all)))
6357             (delq (rassq handle all) all)
6358             (setq flat (nconc flat (list (cons newid handle)))))))
6359     (let ((flat (list nil)))
6360       ;; Assume that elements of `gnus-article-mime-handle-alist'
6361       ;; are in the decreasing order, but unnumbered subsidiaries
6362       ;; in each element are in the increasing order.
6363       (dolist (handle (reverse gnus-article-mime-handle-alist))
6364         (if (stringp (cadr handle))
6365             (setq flat (nconc flat (gnus-article-mime-handles
6366                                     (cddr handle) (list (car handle)) flat)))
6367           (delq (rassq (cdr handle) flat) flat)
6368           (setq flat (nconc flat (list (cons (list (car handle))
6369                                              (cdr handle)))))))
6370       (setq flat (cdr flat))
6371       (mapc (lambda (handle)
6372               (if (cdar handle)
6373                   ;; This is a hidden (i.e. unnumbered) handle.
6374                   (progn
6375                     (setcar handle
6376                             (1+ (caar gnus-article-mime-handle-alist)))
6377                     (push handle gnus-article-mime-handle-alist))
6378                 (setcar handle (caar handle))))
6379             flat)
6380       flat)))
6381
6382 (defun gnus-mime-buttonize-attachments-in-header (&optional interactive)
6383   "Show attachments as buttons in the end of the header of an article.
6384 This function toggles the display when called interactively.  Note that
6385 buttons to be added to the header are only the ones that aren't inlined
6386 in the body.  Use `gnus-header-face-alist' to highlight buttons."
6387   (interactive (list t))
6388   (gnus-with-article-buffer
6389     (let ((case-fold-search t) buttons handle type st)
6390       (save-excursion
6391         (save-restriction
6392           (widen)
6393           (article-narrow-to-head)
6394           ;; Header buttons exist?
6395           (while (and (not buttons)
6396                       (re-search-forward "^attachments?:[\n ]+" nil t))
6397             (when (get-char-property (match-end 0)
6398                                      'gnus-button-attachment-extra)
6399               (setq buttons (match-beginning 0))))
6400           (widen)
6401           (when buttons
6402             ;; Delete header buttons.
6403             (delete-region buttons (if (re-search-forward "^[^ ]" nil t)
6404                                        (match-beginning 0)
6405                                      (point-max))))
6406           (unless (and interactive buttons)
6407             ;; Find buttons.
6408             (setq buttons nil)
6409             (dolist (button (gnus-article-mime-handles))
6410               (setq handle (cdr button)
6411                     type (mm-handle-media-type handle))
6412               (when (or (and (if (gnus-buffer-live-p gnus-summary-buffer)
6413                                  (with-current-buffer gnus-summary-buffer
6414                                    gnus-inhibit-images)
6415                                gnus-inhibit-images)
6416                              (string-match "\\`image/" type))
6417                         (mm-inline-override-p handle)
6418                         (and (mm-handle-disposition handle)
6419                              (not (equal (car (mm-handle-disposition handle))
6420                                          "inline"))
6421                              (not (mm-attachment-override-p handle)))
6422                         (not (mm-automatic-display-p handle))
6423                         (not (or (and (mm-inlinable-p handle)
6424                                       (mm-inlined-p handle))
6425                                  (mm-automatic-external-display-p type))))
6426                 (push button buttons)))
6427             (when buttons
6428               ;; Add header buttons.
6429               (article-goto-body)
6430               (forward-line -1)
6431               (narrow-to-region (point) (point))
6432               (insert "Attachment" (if (cdr buttons) "s" "") ":")
6433               (dolist (button (nreverse buttons))
6434                 (setq st (point))
6435                 (insert " ")
6436                 (mm-handle-set-undisplayer
6437                  (setq handle (copy-sequence (cdr button))) nil)
6438                 (gnus-insert-mime-button handle (car button))
6439                 (skip-chars-backward "\t\n ")
6440                 (delete-region (point) (point-max))
6441                 (when (> (current-column) (window-width))
6442                   (goto-char st)
6443                   (insert "\n")
6444                   (end-of-line)))
6445               (insert "\n")
6446               (dolist (ovl (gnus-overlays-in (point-min) (point)))
6447                 (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
6448                 (gnus-overlay-put ovl 'face nil))
6449               (let ((gnus-treatment-function-alist
6450                      '((gnus-treat-highlight-headers
6451                         gnus-article-highlight-headers))))
6452                 (gnus-treat-article 'head)))))))))
6453
6454 ;;; Article savers.
6455
6456 (defun gnus-output-to-file (file-name)
6457   "Append the current article to a file named FILE-NAME.
6458 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6459 text and used as the value of the coding cookie which is added to the
6460 top of a file.  Otherwise, this function saves a raw article without
6461 the coding cookie."
6462   (let* ((artbuf (current-buffer))
6463          (file-name-coding-system nnmail-pathname-coding-system)
6464          (coding gnus-article-save-coding-system)
6465          (coding-system-for-read (if coding
6466                                      nil ;; Rely on the coding cookie.
6467                                    mm-text-coding-system))
6468          (coding-system-for-write (or coding
6469                                       mm-text-coding-system-for-write
6470                                       mm-text-coding-system))
6471          (exists (file-exists-p file-name)))
6472     (with-temp-buffer
6473       (when exists
6474         (insert-file-contents file-name)
6475         (goto-char (point-min))
6476         ;; Remove the existing coding cookie.
6477         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6478           (delete-region (match-beginning 0) (match-end 0))))
6479       (goto-char (point-max))
6480       (insert-buffer-substring artbuf)
6481       ;; Append newline at end of the buffer as separator, and then
6482       ;; save it to file.
6483       (goto-char (point-max))
6484       (insert "\n")
6485       (when coding
6486         ;; If the coding system is not suitable to encode the text,
6487         ;; ask a user for a proper one.
6488         (when (fboundp 'select-safe-coding-system)
6489           (setq coding (coding-system-base
6490                         (save-window-excursion
6491                           (select-safe-coding-system (point-min) (point-max)
6492                                                      coding))))
6493           (setq coding-system-for-write
6494                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6495                     coding)))
6496         (goto-char (point-min))
6497         ;; Add the coding cookie.
6498         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6499                         coding-system-for-write)))
6500       (if exists
6501           (progn
6502             (write-region (point-min) (point-max) file-name nil 'no-message)
6503             (message "Appended to %s" file-name))
6504         (write-region (point-min) (point-max) file-name))))
6505   t)
6506
6507 (defun gnus-narrow-to-page (&optional arg)
6508   "Narrow the article buffer to a page.
6509 If given a numerical ARG, move forward ARG pages."
6510   (interactive "P")
6511   (setq arg (if arg (prefix-numeric-value arg) 0))
6512   (with-current-buffer gnus-article-buffer
6513     (widen)
6514     ;; Remove any old next/prev buttons.
6515     (when (gnus-visual-p 'page-marker)
6516       (let ((inhibit-read-only t))
6517         (gnus-remove-text-with-property 'gnus-prev)
6518         (gnus-remove-text-with-property 'gnus-next)))
6519     (let (st nd pt)
6520       (when (save-excursion
6521               (cond ((< arg 0)
6522                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6523                          (prog1
6524                              (setq nd (match-beginning 0)
6525                                    pt nd)
6526                            (when (re-search-backward page-delimiter nil t)
6527                              (setq st (match-end 0))))
6528                        (when (re-search-forward page-delimiter nil t)
6529                          (setq nd (match-beginning 0)
6530                                pt (point-min)))))
6531                     ((> arg 0)
6532                      (if (re-search-forward page-delimiter nil 'move arg)
6533                          (prog1
6534                              (setq st (match-end 0)
6535                                    pt st)
6536                            (when (re-search-forward page-delimiter nil t)
6537                              (setq nd (match-beginning 0))))
6538                        (when (re-search-backward page-delimiter nil t)
6539                          (setq st (match-end 0)
6540                                pt (point-max)))))
6541                     (t
6542                      (when (re-search-backward page-delimiter nil t)
6543                        (goto-char (setq st (match-end 0))))
6544                      (when (re-search-forward page-delimiter nil t)
6545                        (setq nd (match-beginning 0)))
6546                      (or st nd))))
6547         (setq gnus-page-broken t)
6548         (when pt (goto-char pt))
6549         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6550         (when (gnus-visual-p 'page-marker)
6551           (save-excursion
6552             (when nd
6553               (goto-char nd)
6554               (gnus-insert-next-page-button))
6555             (when st
6556               (goto-char st)
6557               (gnus-insert-prev-page-button))))))))
6558
6559 ;; Article mode commands
6560
6561 (defun gnus-article-goto-next-page ()
6562   "Show the next page of the article."
6563   (interactive)
6564   (when (gnus-article-next-page)
6565     (goto-char (point-min))
6566     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6567
6568
6569 (defun gnus-article-goto-prev-page ()
6570   "Show the previous page of the article."
6571   (interactive)
6572   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6573       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6574     (gnus-article-prev-page nil)))
6575
6576 ;; This is cleaner but currently breaks `gnus-pick-mode':
6577 ;;
6578 ;; (defun gnus-article-goto-next-page ()
6579 ;;   "Show the next page of the article."
6580 ;;   (interactive)
6581 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6582 ;;     (gnus-summary-next-page)))
6583 ;;
6584 ;; (defun gnus-article-goto-prev-page ()
6585 ;;   "Show the next page of the article."
6586 ;;   (interactive)
6587 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6588 ;;     (gnus-summary-prev-page)))
6589
6590 (defun gnus-article-next-page (&optional lines)
6591   "Show the next page of the current article.
6592 If end of article, return non-nil.  Otherwise return nil.
6593 Argument LINES specifies lines to be scrolled up."
6594   (interactive "p")
6595   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6596   (if (and (not (and gnus-article-over-scroll
6597                      (> (count-lines (window-start) (point-max))
6598                         (if (featurep 'xemacs)
6599                             (or lines (1- (window-height)))
6600                           (+ (or lines (1- (window-height))) scroll-margin)))))
6601            (save-excursion
6602              (end-of-line)
6603              (and (pos-visible-in-window-p)     ;Not continuation line.
6604                   (>= (point) (point-max)))))
6605       ;; Nothing in this page.
6606       (if (or (not gnus-page-broken)
6607               (save-excursion
6608                 (save-restriction
6609                   (widen)
6610                   (forward-line)
6611                   (eobp)))) ;Real end-of-buffer?
6612           (progn
6613             (when gnus-article-over-scroll
6614               (gnus-article-next-page-1 lines))
6615             t)                  ;Nothing more.
6616         (gnus-narrow-to-page 1)         ;Go to next page.
6617         nil)
6618     ;; More in this page.
6619     (gnus-article-next-page-1 lines)
6620     nil))
6621
6622 (defun gnus-article-beginning-of-window ()
6623   "Move point to the beginning of the window.
6624 In Emacs, the point is placed at the line number which `scroll-margin'
6625 specifies."
6626   (if (featurep 'xemacs)
6627       (move-to-window-line 0)
6628     ;; There is an obscure bug in Emacs that makes it impossible to
6629     ;; scroll past big pictures in the article buffer.  Try to fix
6630     ;; this by adding a sanity check by counting the lines visible.
6631     (when (> (count-lines (window-start) (window-end)) 30)
6632       (move-to-window-line
6633        (min (max 0 scroll-margin)
6634             (max 1 (- (window-height)
6635                       (if mode-line-format 1 0)
6636                       (if header-line-format 1 0)
6637                       2)))))))
6638
6639 (defvar scroll-in-place)
6640
6641 (defun gnus-article-next-page-1 (lines)
6642   (condition-case ()
6643       (let ((scroll-in-place nil)
6644             (auto-window-vscroll nil))
6645         (scroll-up lines))
6646     (end-of-buffer
6647      ;; Long lines may cause an end-of-buffer error.
6648      (goto-char (point-max))))
6649   (gnus-article-beginning-of-window))
6650
6651 (defun gnus-article-prev-page (&optional lines)
6652   "Show previous page of current article.
6653 Argument LINES specifies lines to be scrolled down."
6654   (interactive "p")
6655   (move-to-window-line 0)
6656   (if (and gnus-page-broken
6657            (bobp)
6658            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6659       (progn
6660         (gnus-narrow-to-page -1)        ;Go to previous page.
6661         (goto-char (point-max))
6662         (recenter (if gnus-article-over-scroll
6663                       (if lines
6664                           (max (if (featurep 'xemacs)
6665                                    lines
6666                                  (+ lines scroll-margin))
6667                                3)
6668                         (- (window-height) 2))
6669                     -1)))
6670     (prog1
6671         (condition-case ()
6672             (let ((scroll-in-place nil))
6673               (scroll-down lines))
6674           (beginning-of-buffer
6675            (goto-char (point-min))))
6676       (gnus-article-beginning-of-window))))
6677
6678 (defun gnus-article-only-boring-p ()
6679   "Decide whether there is only boring text remaining in the article.
6680 Something \"interesting\" is a word of at least two letters that does
6681 not have a face in `gnus-article-boring-faces'."
6682   (when (and gnus-article-skip-boring
6683              (boundp 'gnus-article-boring-faces)
6684              (symbol-value 'gnus-article-boring-faces))
6685     (save-excursion
6686       (let ((inhibit-point-motion-hooks t))
6687         (catch 'only-boring
6688           (while (re-search-forward "\\b\\w\\w" nil t)
6689             (forward-char -1)
6690             (when (not (gnus-intersection
6691                         (gnus-faces-at (point))
6692                         (symbol-value 'gnus-article-boring-faces)))
6693               (throw 'only-boring nil)))
6694           (throw 'only-boring t))))))
6695
6696 (defun gnus-article-refer-article ()
6697   "Read article specified by message-id around point."
6698   (interactive)
6699   (save-excursion
6700     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6701     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6702     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6703         (let ((msg-id (concat "<" (match-string 0) ">")))
6704           (set-buffer gnus-summary-buffer)
6705           (gnus-summary-refer-article msg-id))
6706       (error "No references around point"))))
6707
6708 (defun gnus-article-show-summary ()
6709   "Reconfigure windows to show summary buffer."
6710   (interactive)
6711   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6712       (error "There is no summary buffer for this article buffer")
6713     (gnus-article-set-globals)
6714     (gnus-configure-windows 'article)
6715     (gnus-summary-goto-subject gnus-current-article)
6716     (gnus-summary-position-point)))
6717
6718 (defun gnus-article-describe-briefly ()
6719   "Describe article mode commands briefly."
6720   (interactive)
6721   (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")))
6722
6723 (defun gnus-article-check-buffer ()
6724   "Beep if not in an article buffer."
6725   (unless (derived-mode-p 'gnus-article-mode)
6726     (error "Command invoked outside of a Gnus article buffer")))
6727
6728 (defvar gnus-pick-mode)
6729
6730 (defun gnus-article-read-summary-keys (&optional _arg key not-restore-window)
6731   "Read a summary buffer key sequence and execute it from the article buffer."
6732   (interactive "P")
6733   (gnus-article-check-buffer)
6734   (let ((nosaves
6735          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6736            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6737            "=" "^" "\M-^" "|"))
6738         (nosave-but-article
6739          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6740            "An" "Ap" [?A (meta return)] [?A delete]))
6741         (nosave-in-article
6742          '("AS" "\C-d"))
6743         keys new-sum-point)
6744     (with-current-buffer gnus-article-current-summary
6745       (let (gnus-pick-mode)
6746         (setq unread-command-events (nconc unread-command-events
6747                                            (list (or key last-command-event)))
6748               keys (if (featurep 'xemacs)
6749                        (events-to-keys (read-key-sequence nil t))
6750                      (read-key-sequence nil t)))))
6751
6752     (message "")
6753
6754     (cond
6755      ((eq (aref keys (1- (length keys))) ?\C-h)
6756       (gnus-article-describe-bindings (substring keys 0 -1)))
6757      ((or (member keys nosaves)
6758           (member keys nosave-but-article)
6759           (member keys nosave-in-article))
6760       (let (func)
6761         (save-window-excursion
6762           (pop-to-buffer gnus-article-current-summary)
6763           ;; We disable the pick minor mode commands.
6764           (let (gnus-pick-mode)
6765             (setq func (key-binding keys t))))
6766         (if (or (not func)
6767                 (numberp func))
6768             (ding)
6769           (unless (member keys nosave-in-article)
6770             (set-buffer gnus-article-current-summary))
6771           (when (and (symbolp func)
6772                      (get func 'disabled))
6773             (error "Function %s disabled" func))
6774           (call-interactively func)
6775           (setq new-sum-point (point)))
6776         (when (member keys nosave-but-article)
6777           (pop-to-buffer gnus-article-buffer))))
6778      (t
6779       ;; These commands should restore window configuration.
6780       (let ((obuf (current-buffer))
6781             (owin (current-window-configuration))
6782             win func in-buffer selected new-sum-start new-sum-hscroll err)
6783         (cond (not-restore-window
6784                (pop-to-buffer gnus-article-current-summary)
6785                (setq win (selected-window)))
6786               ((setq win (get-buffer-window gnus-article-current-summary))
6787                (select-window win))
6788               (t
6789                (let ((summary-buffer gnus-article-current-summary))
6790                  (gnus-configure-windows 'article)
6791                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6792                    (let ((gnus-buffer-configuration
6793                           '((article ((vertical 1.0
6794                                                 (summary 0.25 point)
6795                                                 (article 1.0)))))))
6796                      (gnus-configure-windows 'article))
6797                    (setq win (get-buffer-window summary-buffer 'visible)))
6798                  (gnus-select-frame-set-input-focus (window-frame win))
6799                  (select-window win))))
6800         (setq in-buffer (current-buffer))
6801         ;; We disable the pick minor mode commands.
6802         (setq func (let (gnus-pick-mode)
6803                      (key-binding keys t)))
6804         (when (get func 'disabled)
6805           (error "Function %s disabled" func))
6806         (if (and func
6807                  (functionp func)
6808                  (condition-case code
6809                      (progn
6810                        (call-interactively func)
6811                        t)
6812                    (error
6813                     (setq err code)
6814                     nil)))
6815             (progn
6816               (when (eq win (selected-window))
6817                 (setq new-sum-point (point)
6818                       new-sum-start (window-start win)
6819                       new-sum-hscroll (window-hscroll win)))
6820               (when (or (eq in-buffer (current-buffer))
6821                         (when (eq obuf (current-buffer))
6822                           (set-buffer in-buffer)
6823                           t))
6824                 (setq selected (ignore-errors (gnus-summary-select-article)))
6825                 (set-buffer obuf)
6826                 (unless not-restore-window
6827                   (set-window-configuration owin))
6828                 (when (and (eq selected 'old)
6829                            new-sum-point)
6830                   (set-window-start (get-buffer-window (current-buffer))
6831                                     1)
6832                   (set-window-point (get-buffer-window (current-buffer))
6833                                     (if (article-goto-body)
6834                                         (1- (point))
6835                                       (point))))
6836                 (when (and (not not-restore-window)
6837                            new-sum-point
6838                            (window-live-p win)
6839                            (with-current-buffer (window-buffer win)
6840                              (derived-mode-p 'gnus-summary-mode)))
6841                   (set-window-point win new-sum-point)
6842                   (set-window-start win new-sum-start)
6843                   (set-window-hscroll win new-sum-hscroll))))
6844           (set-window-configuration owin)
6845           (if err
6846               (signal (car err) (cdr err))
6847             (ding))))))))
6848
6849 (defun gnus-article-read-summary-send-keys ()
6850   (interactive)
6851   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6852     (gnus-article-read-summary-keys)))
6853
6854 (defun gnus-article-describe-key (key)
6855   "Display documentation of the function invoked by KEY.
6856 KEY is a string or a vector."
6857   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6858                        (read-key-sequence "Describe key: "))))
6859   (gnus-article-check-buffer)
6860   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6861                                   gnus-article-read-summary-send-keys))
6862       (with-current-buffer gnus-article-current-summary
6863         (setq unread-command-events
6864               (if (featurep 'xemacs)
6865                   (append key nil)
6866                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6867                                         (list 'meta (- x 128))
6868                                       x))
6869                         key)))
6870         (let ((cursor-in-echo-area t)
6871               gnus-pick-mode)
6872           (describe-key (read-key-sequence nil t))))
6873     (describe-key key)))
6874
6875 (defun gnus-article-describe-key-briefly (key &optional insert)
6876   "Display documentation of the function invoked by KEY.
6877 KEY is a string or a vector."
6878   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6879                        (read-key-sequence "Describe key: "))
6880                      current-prefix-arg))
6881   (gnus-article-check-buffer)
6882   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6883                                   gnus-article-read-summary-send-keys))
6884       (with-current-buffer gnus-article-current-summary
6885         (setq unread-command-events
6886               (if (featurep 'xemacs)
6887                   (append key nil)
6888                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6889                                         (list 'meta (- x 128))
6890                                       x))
6891                         key)))
6892         (let ((cursor-in-echo-area t)
6893               gnus-pick-mode)
6894           (describe-key-briefly (read-key-sequence nil t) insert)))
6895     (describe-key-briefly key insert)))
6896
6897 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6898 (defvar gnus-agent-summary-mode)
6899 (defvar gnus-draft-mode)
6900 (defvar help-xref-stack-item)
6901 (defvar help-xref-following)
6902
6903 (defun gnus-article-describe-bindings (&optional prefix)
6904   "Show a list of all defined keys, and their definitions.
6905 The optional argument PREFIX, if non-nil, should be a key sequence;
6906 then we display only bindings that start with that prefix."
6907   (interactive)
6908   (gnus-article-check-buffer)
6909   (let ((keymap (copy-keymap gnus-article-mode-map))
6910         (map (copy-keymap gnus-article-send-map))
6911         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6912         parent agent draft)
6913     (define-key keymap "S" map)
6914     (define-key map [t] nil)
6915     (with-current-buffer gnus-article-current-summary
6916       (set-keymap-parent
6917        keymap
6918        (if (setq parent (keymap-parent gnus-article-mode-map))
6919            (prog1
6920                (setq parent (copy-keymap parent))
6921              (set-keymap-parent parent (current-local-map)))
6922          (current-local-map)))
6923       (set-keymap-parent map (key-binding "S"))
6924       (let (key def gnus-pick-mode)
6925         (while sumkeys
6926           (setq key (pop sumkeys))
6927           (cond ((and (vectorp key) (= (length key) 1)
6928                       (consp (setq def (aref key 0)))
6929                       (numberp (car def)) (numberp (cdr def)))
6930                  (when (< (max (car def) (cdr def)) 128)
6931                    (setq sumkeys
6932                          (append (mapcar
6933                                   #'vector
6934                                   (nreverse (gnus-uncompress-range def)))
6935                                  sumkeys))))
6936                 ((setq def (key-binding key))
6937                  (unless (eq def 'undefined)
6938                    (define-key keymap key def))))))
6939       (when (boundp 'gnus-agent-summary-mode)
6940         (setq agent gnus-agent-summary-mode))
6941       (when (boundp 'gnus-draft-mode)
6942         (setq draft gnus-draft-mode)))
6943     (with-temp-buffer
6944       (use-local-map keymap)
6945       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6946       (set (make-local-variable 'gnus-draft-mode) draft)
6947       (describe-bindings prefix))
6948     (let ((item `((lambda (prefix)
6949                     (with-current-buffer ,(current-buffer)
6950                       (gnus-article-describe-bindings prefix)))
6951                   ,prefix)))
6952       ;; Loading `help-mode' here is necessary if `describe-bindings'
6953       ;; is replaced with something, e.g. `helm-descbinds'.
6954       (require 'help-mode)
6955       (with-current-buffer (let (help-xref-following) (help-buffer))
6956         (setq help-xref-stack-item item)))))
6957
6958 (defun gnus-article-reply-with-original (&optional wide)
6959   "Start composing a reply mail to the current message.
6960 The text in the region will be yanked.  If the region isn't active,
6961 the entire article will be yanked."
6962   (interactive)
6963   (let ((article (cdr gnus-article-current))
6964         contents)
6965     (if (not (gnus-region-active-p))
6966         (with-current-buffer gnus-summary-buffer
6967           (gnus-summary-reply (list (list article)) wide))
6968       (setq contents (buffer-substring (point) (mark t)))
6969       ;; Deactivate active regions.
6970       (when (and (boundp 'transient-mark-mode)
6971                  transient-mark-mode)
6972         (setq mark-active nil))
6973       (with-current-buffer gnus-summary-buffer
6974         (gnus-summary-reply
6975          (list (list article contents)) wide)))))
6976
6977 (defun gnus-article-wide-reply-with-original ()
6978   "Start composing a wide reply mail to the current message.
6979 The text in the region will be yanked.  If the region isn't active,
6980 the entire article will be yanked."
6981   (interactive)
6982   (gnus-article-reply-with-original t))
6983
6984 (defun gnus-article-followup-with-original ()
6985   "Compose a followup to the current article.
6986 The text in the region will be yanked.  If the region isn't active,
6987 the entire article will be yanked."
6988   (interactive)
6989   (let ((article (cdr gnus-article-current))
6990         contents)
6991       (if (not (gnus-region-active-p))
6992           (with-current-buffer gnus-summary-buffer
6993             (gnus-summary-followup (list (list article))))
6994         (setq contents (buffer-substring (point) (mark t)))
6995         ;; Deactivate active regions.
6996         (when (and (boundp 'transient-mark-mode)
6997                    transient-mark-mode)
6998           (setq mark-active nil))
6999         (with-current-buffer gnus-summary-buffer
7000           (gnus-summary-followup
7001            (list (list article contents)))))))
7002
7003 (defun gnus-article-hide (&optional arg force)
7004   "Hide all the gruft in the current article.
7005 This means that signatures, cited text and (some) headers will be
7006 hidden.
7007 If given a prefix, show the hidden text instead."
7008   (interactive (append (gnus-article-hidden-arg) (list 'force)))
7009   (gnus-article-hide-headers arg)
7010   (gnus-article-hide-list-identifiers arg)
7011   (gnus-article-hide-citation-maybe arg force)
7012   (gnus-article-hide-signature arg))
7013
7014 (defun gnus-check-group-server ()
7015   ;; Make sure the connection to the server is alive.
7016   (unless (gnus-server-opened
7017            (gnus-find-method-for-group gnus-newsgroup-name))
7018     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7019     (gnus-request-group gnus-newsgroup-name t)))
7020
7021 (eval-when-compile
7022   (autoload 'nneething-get-file-name "nneething"))
7023
7024 (defun gnus-request-article-this-buffer (article group)
7025   "Get an article and insert it into this buffer."
7026   (let (do-update-line sparse-header)
7027     (prog1
7028         (save-excursion
7029           (erase-buffer)
7030           (gnus-kill-all-overlays)
7031           (setq group (or group gnus-newsgroup-name))
7032
7033           ;; Using `gnus-request-article' directly will insert the article into
7034           ;; `nntp-server-buffer' - so we'll save some time by not having to
7035           ;; copy it from the server buffer into the article buffer.
7036
7037           ;; We only request an article by message-id when we do not have the
7038           ;; headers for it, so we'll have to get those.
7039           (when (stringp article)
7040             (gnus-read-header article))
7041
7042           ;; If the article number is negative, that means that this article
7043           ;; doesn't belong in this newsgroup (possibly), so we find its
7044           ;; message-id and request it by id instead of number.
7045           (when (and (numberp article)
7046                      gnus-summary-buffer
7047                      (get-buffer gnus-summary-buffer)
7048                      (gnus-buffer-exists-p gnus-summary-buffer))
7049             (with-current-buffer gnus-summary-buffer
7050               (let ((header (gnus-summary-article-header article)))
7051                 (when (< article 0)
7052                   (cond
7053                    ((memq article gnus-newsgroup-sparse)
7054                     ;; This is a sparse gap article.
7055                     (setq do-update-line article)
7056                     (setq article (mail-header-id header))
7057                     (setq sparse-header (gnus-read-header article))
7058                     (setq gnus-newsgroup-sparse
7059                           (delq article gnus-newsgroup-sparse)))
7060                    ((vectorp header)
7061                     ;; It's a real article.
7062                     (setq article (mail-header-id header)))
7063                    (t
7064                     ;; It is an extracted pseudo-article.
7065                     (setq article 'pseudo)
7066                     (gnus-request-pseudo-article header))))
7067
7068                 (let ((method (gnus-find-method-for-group
7069                                gnus-newsgroup-name)))
7070                   (when (and (eq (car method) 'nneething)
7071                              (vectorp header))
7072                     (let ((dir (nneething-get-file-name
7073                                 (mail-header-id header))))
7074                       (when (and (stringp dir)
7075                                  (file-directory-p dir))
7076                         (setq article 'nneething)
7077                         (gnus-group-enter-directory dir))))))))
7078
7079           (cond
7080            ;; Refuse to select canceled articles.
7081            ((and (numberp article)
7082                  gnus-summary-buffer
7083                  (get-buffer gnus-summary-buffer)
7084                  (gnus-buffer-exists-p gnus-summary-buffer)
7085                  (eq (cdr (with-current-buffer gnus-summary-buffer
7086                             (assq article gnus-newsgroup-reads)))
7087                      gnus-canceled-mark))
7088             nil)
7089            ;; We first check `gnus-original-article-buffer'.
7090            ((and (get-buffer gnus-original-article-buffer)
7091                  (numberp article)
7092                  (with-current-buffer gnus-original-article-buffer
7093                    (and (equal (car gnus-original-article) group)
7094                         (eq (cdr gnus-original-article) article))))
7095             ;; `insert-buffer-substring' would incorrectly use the
7096             ;; equivalent of string-make-multibyte which amount to decoding
7097             ;; with locale-coding-system, causing failure of
7098             ;; subsequent decoding.
7099             (insert (mm-string-to-multibyte
7100                      (with-current-buffer gnus-original-article-buffer
7101                        (buffer-substring (point-min) (point-max)))))
7102             'article)
7103            ;; Check the backlog.
7104            ((and gnus-keep-backlog
7105                  (gnus-backlog-request-article group article (current-buffer)))
7106             'article)
7107            ;; Check asynchronous pre-fetch.
7108            ((gnus-async-request-fetched-article group article (current-buffer))
7109             (gnus-async-prefetch-next group article gnus-summary-buffer)
7110             (when (and (numberp article) gnus-keep-backlog)
7111               (gnus-backlog-enter-article group article (current-buffer)))
7112             'article)
7113            ;; Check the cache.
7114            ((and gnus-use-cache
7115                  (numberp article)
7116                  (gnus-cache-request-article article group))
7117             'article)
7118            ;; Check the agent cache.
7119            ((gnus-agent-request-article article group)
7120             'article)
7121            ;; Get the article and put into the article buffer.
7122            ((or (stringp article)
7123                 (numberp article))
7124             (let ((gnus-override-method gnus-override-method)
7125                   (methods (and (stringp article)
7126                                 (with-current-buffer gnus-summary-buffer
7127                                   (gnus-refer-article-methods))))
7128                   (backend (car (gnus-find-method-for-group
7129                                  gnus-newsgroup-name)))
7130                   result
7131                   (inhibit-read-only t))
7132               (when (and (null gnus-override-method)
7133                          methods)
7134                 (setq gnus-override-method (pop methods)))
7135               (while (not result)
7136                 (erase-buffer)
7137                 (gnus-kill-all-overlays)
7138                 (let ((gnus-newsgroup-name group))
7139                   (gnus-check-group-server))
7140                 (cond
7141                  ((gnus-request-article article group (current-buffer))
7142                   (when (numberp article)
7143                     (gnus-async-prefetch-next group article
7144                                               gnus-summary-buffer)
7145                     (when gnus-keep-backlog
7146                       (gnus-backlog-enter-article
7147                        group article (current-buffer)))
7148                     (when (and gnus-agent
7149                                (gnus-agent-group-covered-p group))
7150                       (gnus-agent-store-article article group)))
7151                   (setq result 'article))
7152                  (methods
7153                   (setq gnus-override-method (pop methods)))
7154                  ((not (string-match "^400 "
7155                                      (nnheader-get-report backend)))
7156                   ;; If we get 400 server disconnect, reconnect and
7157                   ;; retry; otherwise, assume the article has expired.
7158                   (setq result 'done))))
7159               (and (eq result 'article) 'article)))
7160            ;; It was a pseudo.
7161            (t article)))
7162
7163       ;; Associate this article with the current summary buffer.
7164       (setq gnus-article-current-summary gnus-summary-buffer)
7165
7166       ;; Take the article from the original article buffer
7167       ;; and place it in the buffer it's supposed to be in.
7168       (when (and (get-buffer gnus-article-buffer)
7169                  (equal (buffer-name (current-buffer))
7170                         (buffer-name (get-buffer gnus-article-buffer))))
7171         (save-excursion
7172           (if (get-buffer gnus-original-article-buffer)
7173               (set-buffer gnus-original-article-buffer)
7174             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
7175             (buffer-disable-undo)
7176             (setq major-mode 'gnus-original-article-mode)
7177             (setq buffer-read-only t))
7178           (let ((inhibit-read-only t))
7179             (erase-buffer)
7180             (insert-buffer-substring gnus-article-buffer))
7181           (setq gnus-original-article (cons group article)))
7182
7183         ;; Decode charsets.
7184         (run-hooks 'gnus-article-decode-hook)
7185         ;; Mark article as decoded or not.
7186         (setq gnus-article-decoded-p gnus-article-decode-hook))
7187
7188       ;; Update sparse articles.
7189       (when (and do-update-line
7190                  (or (numberp article)
7191                      (stringp article)))
7192         (let ((buf (current-buffer)))
7193           (set-buffer gnus-summary-buffer)
7194           (gnus-summary-update-article do-update-line sparse-header)
7195           (gnus-summary-goto-subject do-update-line nil t)
7196           (set-window-point (gnus-get-buffer-window (current-buffer) t)
7197                             (point))
7198           (set-buffer buf))))))
7199
7200 (defun gnus-block-private-groups (group)
7201   (if (or (gnus-news-group-p group)
7202           (gnus-member-of-valid 'global group))
7203       ;; Block nothing in news groups.
7204       nil
7205     ;; Block everything anywhere else.
7206     "."))
7207
7208 (defun gnus-blocked-images ()
7209   (if (functionp gnus-blocked-images)
7210       (funcall gnus-blocked-images gnus-newsgroup-name)
7211     gnus-blocked-images))
7212
7213 ;;;
7214 ;;; Article editing
7215 ;;;
7216
7217 (defcustom gnus-article-edit-mode-hook nil
7218   "Hook run in article edit mode buffers."
7219   :group 'gnus-article-various
7220   :type 'hook)
7221
7222 (defvar gnus-article-edit-done-function nil)
7223
7224 (defvar gnus-article-edit-mode-map nil)
7225
7226 ;; Should we be using derived.el for this?
7227 (unless gnus-article-edit-mode-map
7228   (setq gnus-article-edit-mode-map (make-keymap))
7229   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
7230
7231   (gnus-define-keys gnus-article-edit-mode-map
7232     "\C-c?"    describe-mode
7233     "\C-c\C-c" gnus-article-edit-done
7234     "\C-c\C-k" gnus-article-edit-exit
7235     "\C-c\C-f\C-t" message-goto-to
7236     "\C-c\C-f\C-o" message-goto-from
7237     "\C-c\C-f\C-b" message-goto-bcc
7238     ;;"\C-c\C-f\C-w" message-goto-fcc
7239     "\C-c\C-f\C-c" message-goto-cc
7240     "\C-c\C-f\C-s" message-goto-subject
7241     "\C-c\C-f\C-r" message-goto-reply-to
7242     "\C-c\C-f\C-n" message-goto-newsgroups
7243     "\C-c\C-f\C-d" message-goto-distribution
7244     "\C-c\C-f\C-f" message-goto-followup-to
7245     "\C-c\C-f\C-m" message-goto-mail-followup-to
7246     "\C-c\C-f\C-k" message-goto-keywords
7247     "\C-c\C-f\C-u" message-goto-summary
7248     "\C-c\C-f\C-i" message-insert-or-toggle-importance
7249     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
7250     "\C-c\C-b" message-goto-body
7251     "\C-c\C-i" message-goto-signature
7252
7253     "\C-c\C-t" message-insert-to
7254     "\C-c\C-n" message-insert-newsgroups
7255     "\C-c\C-o" message-sort-headers
7256     "\C-c\C-e" message-elide-region
7257     "\C-c\C-v" message-delete-not-region
7258     "\C-c\C-z" message-kill-to-signature
7259     "\M-\r" message-newline-and-reformat
7260     "\C-c\C-a" mml-attach-file
7261     "\C-a" message-beginning-of-line
7262     "\t" message-tab
7263     "\M-;" comment-region)
7264
7265   (gnus-define-keys (gnus-article-edit-wash-map
7266                      "\C-c\C-w" gnus-article-edit-mode-map)
7267     "f" gnus-article-edit-full-stops))
7268
7269 (easy-menu-define
7270   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7271   '("Field"
7272     ["Fetch To" message-insert-to t]
7273     ["Fetch Newsgroups" message-insert-newsgroups t]
7274     "----"
7275     ["To" message-goto-to t]
7276     ["From" message-goto-from t]
7277     ["Subject" message-goto-subject t]
7278     ["Cc" message-goto-cc t]
7279     ["Reply-To" message-goto-reply-to t]
7280     ["Summary" message-goto-summary t]
7281     ["Keywords" message-goto-keywords t]
7282     ["Newsgroups" message-goto-newsgroups t]
7283     ["Followup-To" message-goto-followup-to t]
7284     ["Mail-Followup-To" message-goto-mail-followup-to t]
7285     ["Distribution" message-goto-distribution t]
7286     ["Body" message-goto-body t]
7287     ["Signature" message-goto-signature t]))
7288
7289 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7290   "Major mode for editing articles.
7291 This is an extended text-mode.
7292
7293 \\{gnus-article-edit-mode-map}"
7294   (make-local-variable 'gnus-article-edit-done-function)
7295   (make-local-variable 'gnus-prev-winconf)
7296   (set (make-local-variable 'font-lock-defaults)
7297        '(message-font-lock-keywords t))
7298   (set (make-local-variable 'mail-header-separator) "")
7299   (set (make-local-variable 'gnus-article-edit-mode) t)
7300   (easy-menu-add message-mode-field-menu message-mode-map)
7301   (mml-mode)
7302   (setq buffer-read-only nil)
7303   (buffer-enable-undo)
7304   (widen))
7305
7306 (defun gnus-article-edit (&optional force)
7307   "Edit the current article.
7308 This will have permanent effect only in mail groups.
7309 If FORCE is non-nil, allow editing of articles even in read-only
7310 groups."
7311   (interactive "P")
7312   (when (and (not force)
7313              (gnus-group-read-only-p))
7314     (error "The current newsgroup does not support article editing"))
7315   (gnus-article-date-original)
7316   (gnus-article-edit-article
7317    'ignore
7318    `(lambda (no-highlight)
7319       'ignore
7320       (gnus-summary-edit-article-done
7321        ,(or (mail-header-references gnus-current-headers) "")
7322        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7323
7324 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7325   "Start editing the contents of the current article buffer."
7326   (let ((winconf (current-window-configuration)))
7327     (set-buffer gnus-article-buffer)
7328     (let ((message-auto-save-directory
7329            ;; Don't associate the article buffer with a draft file.
7330            nil))
7331       (gnus-article-edit-mode))
7332     (funcall start-func)
7333     (set-buffer-modified-p nil)
7334     (gnus-configure-windows 'edit-article)
7335     (setq gnus-article-edit-done-function exit-func)
7336     (setq gnus-prev-winconf winconf)
7337     (unless quiet
7338       (gnus-message 6 "C-c C-c to end edits"))))
7339
7340 (defun gnus-article-edit-done (&optional arg)
7341   "Update the article edits and exit."
7342   (interactive "P")
7343   (let ((func gnus-article-edit-done-function)
7344         (buf (current-buffer))
7345         (start (window-start))
7346         (winconf gnus-prev-winconf))
7347     (widen) ;; Widen it in case that users narrowed the buffer.
7348     (funcall func arg)
7349     (set-buffer buf)
7350     ;; The cache and backlog have to be flushed somewhat.
7351     (when gnus-keep-backlog
7352       (gnus-backlog-remove-article
7353        (car gnus-article-current) (cdr gnus-article-current)))
7354     ;; Flush original article as well.
7355     (gnus-flush-original-article-buffer)
7356     (when gnus-use-cache
7357       (gnus-cache-update-article
7358        (car gnus-article-current) (cdr gnus-article-current)))
7359     ;; We remove all text props from the article buffer.
7360     (kill-all-local-variables)
7361     (set-text-properties (point-min) (point-max) nil)
7362     (gnus-article-mode)
7363     (set-window-configuration winconf)
7364     (set-buffer buf)
7365     (set-window-start (get-buffer-window buf) start)
7366     (set-window-point (get-buffer-window buf) (point)))
7367   (gnus-summary-show-article))
7368
7369 (defun gnus-flush-original-article-buffer ()
7370   (when (get-buffer gnus-original-article-buffer)
7371     (with-current-buffer gnus-original-article-buffer
7372       (setq gnus-original-article nil))))
7373
7374 (defun gnus-article-edit-exit ()
7375   "Exit the article editing without updating."
7376   (interactive)
7377   (when (or (not (buffer-modified-p))
7378             (yes-or-no-p "Article modified; kill anyway? "))
7379     (let ((curbuf (current-buffer))
7380           (p (point))
7381           (window-start (window-start)))
7382       (erase-buffer)
7383       (if (gnus-buffer-live-p gnus-original-article-buffer)
7384           (insert-buffer-substring gnus-original-article-buffer))
7385       (let ((winconf gnus-prev-winconf))
7386         (kill-all-local-variables)
7387         (gnus-article-mode)
7388         (set-window-configuration winconf)
7389         ;; Tippy-toe some to make sure that point remains where it was.
7390         (save-current-buffer
7391           (set-buffer curbuf)
7392           (set-window-start (get-buffer-window (current-buffer)) window-start)
7393           (goto-char p))))
7394     (gnus-summary-show-article)))
7395
7396 (defun gnus-article-edit-full-stops ()
7397   "Interactively repair spacing at end of sentences."
7398   (interactive)
7399   (save-excursion
7400     (goto-char (point-min))
7401     (search-forward-regexp "^$" nil t)
7402     (let ((case-fold-search nil))
7403       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7404
7405 ;;;
7406 ;;; Article highlights
7407 ;;;
7408
7409 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7410
7411 ;;; Internal Variables:
7412
7413 (defcustom gnus-button-url-regexp
7414   (concat
7415    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7416    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7417    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7418    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7419        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7420              (punct "!?:;.,"))
7421          (concat
7422           "\\(?:"
7423           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7424           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7425           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)"
7426           "\\(?:" "[" chars punct "]+" "[" chars "]" "\\)?"
7427           "\\|"
7428           "[" chars punct "]+" "[" chars "]"
7429           "\\)"))
7430      (concat ;; XEmacs 21.4 doesn't support POSIX.
7431       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7432       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7433    "\\)")
7434   "Regular expression that matches URLs."
7435   :version "24.4"
7436   :group 'gnus-article-buttons
7437   :type 'regexp)
7438
7439 (defcustom gnus-button-valid-fqdn-regexp
7440   message-valid-fqdn-regexp
7441   "Regular expression that matches a valid FQDN."
7442   :version "22.1"
7443   :group 'gnus-article-buttons
7444   :type 'regexp)
7445
7446 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7447 (defcustom gnus-button-valid-localpart-regexp
7448   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7449   "Regular expression that matches a localpart of mail addresses or MIDs."
7450   :version "22.1"
7451   :group 'gnus-article-buttons
7452   :type 'regexp)
7453
7454 (defcustom gnus-button-man-handler 'manual-entry
7455   "Function to use for displaying man pages.
7456 The function must take at least one argument with a string naming the
7457 man page."
7458   :version "22.1"
7459   :type '(choice (function-item :tag "Man" manual-entry)
7460                  (function-item :tag "Woman" woman)
7461                  (function :tag "Other"))
7462   :group 'gnus-article-buttons)
7463
7464 (defcustom gnus-button-mid-or-mail-regexp
7465   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7466           gnus-button-valid-fqdn-regexp
7467           ">?\\)\\b")
7468   "Regular expression that matches a message ID or a mail address."
7469   :version "22.1"
7470   :group 'gnus-article-buttons
7471   :type 'regexp)
7472
7473 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7474   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7475 Strings like this can be either a message ID or a mail address.  If it is one
7476 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7477 message ID or a mail address, respectively.  If this variable is set to the
7478 symbol `ask', always query the user what do do.  If it is a function, this
7479 function will be called with the string as its only argument.  The function
7480 must return `mid', `mail', `invalid' or `ask'."
7481   :version "22.1"
7482   :group 'gnus-article-buttons
7483   :type '(choice (function-item :tag "Heuristic function"
7484                                 gnus-button-mid-or-mail-heuristic)
7485                  (const ask)
7486                  (const mid)
7487                  (const mail)))
7488
7489 (defcustom gnus-button-mid-or-mail-heuristic-alist
7490   '((-10.0 . ".+\\$.+@")
7491     (-10.0 . "#")
7492     (-10.0 . "\\*")
7493     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7494     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7495     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7496     (-1.0  . "^[^a-z]+@")
7497     ;;
7498     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7499     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7500     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7501     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7502     ;;
7503     (-2.0  . "^[0-9]")
7504     (-1.0  . "^[0-9][0-9]")
7505     ;;
7506     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7507     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7508     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7509     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7510     ;;
7511     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7512     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7513     ;;       "[0-9]{8,}.*\@"
7514     (-3.0
7515      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7516     ;; "[0-9]{12,}.*\@"
7517     ;; compensation for TDMA dated mail addresses:
7518     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7519     ;;
7520     (-20.0 . "\\.fsf@") ;; Gnus
7521     (-20.0 . "^slrn")
7522     (-20.0 . "^Pine")
7523     (-20.0 . "^alpine\\.")
7524     (-20.0 . "_-_") ;; Subject change in thread
7525     ;;
7526     (-20.0 . "\\.ln@") ;; leafnode
7527     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7528     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7529     ;;
7530     ;; (5.0 . "") ;; $local_part_len <= 7
7531     (10.0  . "^[^0-9]+@")
7532     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7533     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7534     (3.0   . "\@stud")
7535     ;;
7536     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7537     ;;
7538     (0.5   . "^[A-Z][a-z]")
7539     (0.5   . "^[A-Z][a-z][a-z]")
7540     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7541     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7542   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7543
7544 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7545 address.  The REGEXP is processed with `case-fold-search' set to nil."
7546   :version "22.1"
7547   :group 'gnus-article-buttons
7548   :type '(repeat (cons (number :tag "Rate")
7549                        (regexp :tag "Regexp"))))
7550
7551 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7552   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7553 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7554 address, `ask' if unsure and `invalid' if the string is invalid."
7555   (let ((case-fold-search nil)
7556         (list gnus-button-mid-or-mail-heuristic-alist)
7557         (result 0) rate regexp lpartlen elem)
7558     (setq lpartlen
7559           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7560     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7561     ;; Certain special cases...
7562     (when (string-match
7563            (concat
7564             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7565             "^[0-9]+\\.[0-9]+@compuserve\\|"
7566             "@public\\.gmane\\.org")
7567            mid-or-mail)
7568       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7569       (setq result 'mail))
7570     (when (string-match "@.*@\\| " mid-or-mail)
7571       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7572       (setq result 'invalid))
7573     ;; Nothing more to do, if result is not a number here...
7574     (when (numberp result)
7575       (while list
7576         (setq elem (car list)
7577               rate (car elem)
7578               regexp (cdr elem)
7579               list (cdr list))
7580         (when (string-match regexp mid-or-mail)
7581           (setq result (+ result rate))
7582           (gnus-message
7583            9 "`%s' matched `%s', rate `%s', result `%s'."
7584            mid-or-mail regexp rate result)))
7585       (when (<= lpartlen 7)
7586         (setq result (+ result 5.0))
7587         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7588                       mid-or-mail result))
7589       (when (>= lpartlen 12)
7590         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7591         (cond
7592          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7593           ;; Long local part should contain realname if e-mail address,
7594           ;; too many digits: message-id.
7595           ;; $score -= 5.0 + 0.1 * $local_part_len;
7596           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7597           (setq result (+ result rate))
7598           (gnus-message
7599            9 "Many digits in `%s', rate `%s', result `%s'."
7600            mid-or-mail rate result))
7601          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7602                         mid-or-mail)
7603           ;; Too few vowels [^aeiouy]{4,}.*\@
7604           (setq result (+ result -5.0))
7605           (gnus-message
7606            9 "Few vowels in `%s', rate `%s', result `%s'."
7607            mid-or-mail -5.0 result))
7608          (t
7609           (setq result (+ result 5.0))
7610           (gnus-message
7611            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7612     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7613     ;; Maybe we should make this a customizable alist: (condition . 'result)
7614     (cond
7615      ((symbolp result) result)
7616      ;; Now convert number into proper results:
7617      ((< result -10.0) 'mid)
7618      ((> result  10.0) 'mail)
7619      (t 'ask))))
7620
7621 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7622   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7623          (url-mid (concat "news" ":" mid-or-mail))
7624          (url-mailto (concat "mailto" ":" mid-or-mail)))
7625     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7626     (when (fboundp pref)
7627       (setq guessed
7628             ;; get rid of surrounding angles...
7629             (funcall pref
7630                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7631       (if (or (eq 'mid guessed) (eq 'mail guessed))
7632           (setq pref guessed)
7633         (setq pref 'ask)))
7634     (if (eq pref 'ask)
7635         (save-window-excursion
7636           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7637               (setq pref 'mail)
7638             (setq pref 'mid))))
7639     (cond ((eq pref 'mid)
7640            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7641            (gnus-button-handle-news url-mid))
7642           ((eq pref 'mail)
7643            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7644            (gnus-url-mailto url-mailto))
7645           (t (gnus-message 3 "Invalid string.")))))
7646
7647 (defun gnus-button-handle-custom (fun arg)
7648   "Call function FUN on argument ARG.
7649 Both FUN and ARG are supposed to be strings.  ARG will be passed
7650 as a symbol to FUN."
7651   (funcall (intern fun)
7652            (if (string-match "^customize-apropos" fun)
7653                arg
7654              (intern arg))))
7655
7656 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7657
7658 (defun gnus-button-handle-describe-function (url)
7659   "Call `describe-function' when pushing the corresponding URL button."
7660   (describe-function
7661    (intern
7662     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7663
7664 (defun gnus-button-handle-describe-variable (url)
7665   "Call `describe-variable' when pushing the corresponding URL button."
7666   (describe-variable
7667    (intern
7668     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7669
7670 (defun gnus-button-handle-symbol (url)
7671 "Display help on variable or function.
7672 Calls `describe-variable' or `describe-function'."
7673   (let ((sym (intern url)))
7674     (cond
7675      ((fboundp sym) (describe-function sym))
7676      ((boundp sym) (describe-variable sym))
7677      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7678
7679 (defun gnus-button-handle-describe-key (url)
7680   "Call `describe-key' when pushing the corresponding URL button."
7681   (let* ((key-string
7682           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7683          (keys (ignore-errors (eval `(kbd ,key-string)))))
7684     (if keys
7685         (describe-key keys)
7686       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7687
7688 (defun gnus-button-handle-apropos (url)
7689   "Call `apropos' when pushing the corresponding URL button."
7690   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7691
7692 (defun gnus-button-handle-apropos-command (url)
7693   "Call `apropos' when pushing the corresponding URL button."
7694   (apropos-command
7695    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7696
7697 (defun gnus-button-handle-apropos-variable (url)
7698   "Call `apropos' when pushing the corresponding URL button."
7699   (funcall
7700    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7701    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7702
7703 (defun gnus-button-handle-apropos-documentation (url)
7704   "Call `apropos' when pushing the corresponding URL button."
7705   (funcall
7706    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7707    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7708
7709 (defun gnus-button-handle-library (url)
7710   "Call `locate-library' when pushing the corresponding URL button."
7711   (gnus-message 9 "url=`%s'" url)
7712   (let* ((lib (locate-library url))
7713          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7714     (if (not lib)
7715         (gnus-message 1 "Cannot locale library `%s'." url)
7716       (find-file-read-only file))))
7717
7718 (defcustom gnus-button-man-level 5
7719   "*Integer that says how many man-related buttons Gnus will show.
7720 The higher the number, the more buttons will appear and the more false
7721 positives are possible.  Note that you can set this variable local to
7722 specific groups.  Setting it higher in Unix groups is probably a good idea.
7723 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7724 how to set variables in specific groups."
7725   :version "22.1"
7726   :group 'gnus-article-buttons
7727   :link '(custom-manual "(gnus)Group Parameters")
7728   :type 'integer)
7729
7730 (defcustom gnus-button-emacs-level 5
7731   "*Integer that says how many emacs-related buttons Gnus will show.
7732 The higher the number, the more buttons will appear and the more false
7733 positives are possible.  Note that you can set this variable local to
7734 specific groups.  Setting it higher in Emacs or Gnus related groups is
7735 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7736 `gnus-parameters' on how to set variables in specific groups."
7737   :version "22.1"
7738   :group 'gnus-article-buttons
7739   :link '(custom-manual "(gnus)Group Parameters")
7740   :type 'integer)
7741
7742 (defcustom gnus-button-message-level 5
7743   "*Integer that says how many buttons for news or mail messages will appear.
7744 The higher the number, the more buttons will appear and the more false
7745 positives are possible."
7746   ;; mail addresses, MIDs, URLs for news, ...
7747   :version "22.1"
7748   :group 'gnus-article-buttons
7749   :type 'integer)
7750
7751 (defcustom gnus-button-browse-level 5
7752   "*Integer that says how many buttons for browsing will appear.
7753 The higher the number, the more buttons will appear and the more false
7754 positives are possible."
7755   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7756   :version "22.1"
7757   :group 'gnus-article-buttons
7758   :type 'integer)
7759
7760 (defcustom gnus-button-alist
7761   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7762      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7763     ((concat "\\b\\(nntp\\|news\\):\\("
7764              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7765      0 t gnus-button-handle-news 2)
7766     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7767      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7768     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7769      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7770     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7771     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7772      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7773     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7774      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7775     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7776      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7777     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7778      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7779     ;; RFC 2368 (The mailto URL scheme)
7780     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7781      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7782     ("\\bmailto:\\([^ \n\t]+\\)"
7783      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7784     ;; Info Konqueror style <info:/foo/bar baz>.
7785     ;; Must come before " Gnus home-grown style".
7786     ("\\binfo://?\\([^'\">\n\t]+\\)"
7787      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7788    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7789     ("\\binfo://\\([^'\">\n\t ]+\\)"
7790      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7791     ;; Info GNOME style <info:foo#bar_baz>
7792     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7793      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7794     ;; Info KDE style <info:(foo)bar baz>
7795     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7796      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7797     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7798      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7799     ("\\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,]*\\)\\)?"
7800      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7801      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7802     ;; This is custom
7803     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7804      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7805     ;; Emacs help commands
7806     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7807      ;; regexp doesn't match arguments containing ` '.
7808      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7809     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7810      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7811     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7812      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7813     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7814      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7815     ;; The following entries may lead to many false positives so don't enable
7816     ;; them by default (use a high button level).
7817     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7818      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7819      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7820     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7821      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7822     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7823      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7824     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7825      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7826     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7827      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7828     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7829      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7830     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7831      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7832     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7833      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7834     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7835      ;; Unlike the other regexps we really have to require quoting
7836      ;; here to determine where it ends.
7837      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7838     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7839     ("<URL: *\\([^\n<>]*\\)>"
7840      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7841     ;; RFC 2396 (2.4.3., delims) ...
7842     ("\"URL: *\\([^\n\"]*\\)\""
7843      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7844     ;; Raw URLs.
7845     (gnus-button-url-regexp
7846      0 (>= gnus-button-browse-level 0) browse-url 0)
7847     ;; man pages
7848     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7849      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7850      gnus-button-handle-man 1)
7851     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7852     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7853      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7854      gnus-button-handle-man 1)
7855     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7856     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7857     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7858      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7859     ;; Recognizing patches to .el files.  This is somewhat obscure,
7860     ;; but considering the percentage of Gnus users who hack Emacs
7861     ;; Lisp files...
7862     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7863      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7864     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7865      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7866     ;; MID or mail: To avoid too many false positives we don't try to catch
7867     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7868     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7869     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7870     ;; so that non-ambiguous entries (see above) match first.
7871     (gnus-button-mid-or-mail-regexp
7872      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7873   "*Alist of regexps matching buttons in article bodies.
7874
7875 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7876 REGEXP: is the string (case insensitive) matching text around the button (can
7877 also be Lisp expression evaluating to a string),
7878 BUTTON: is the number of the regexp grouping actually matching the button,
7879 FORM: is a Lisp expression which must eval to true for the button to
7880 be added,
7881 CALLBACK: is the function to call when the user push this button, and each
7882 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7883
7884 CALLBACK can also be a variable, in that case the value of that
7885 variable it the real callback function."
7886   :group 'gnus-article-buttons
7887   :type '(repeat (list (choice regexp variable sexp)
7888                        (integer :tag "Button")
7889                        (sexp :tag "Form")
7890                        (function :tag "Callback")
7891                        (repeat :tag "Par"
7892                                :inline t
7893                                (integer :tag "Regexp group")))))
7894 (put 'gnus-button-alist 'risky-local-variable t)
7895
7896 (defcustom gnus-header-button-alist
7897   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7898      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7899     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7900      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7901     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7902      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7903     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7904      0 (>= gnus-button-browse-level 0) browse-url 0)
7905     ("^Subject:" gnus-button-url-regexp
7906      0 (>= gnus-button-browse-level 0) browse-url 0)
7907     ("^[^:]+:" gnus-button-url-regexp
7908      0 (>= gnus-button-browse-level 0) browse-url 0)
7909     ("^OpenPGP:.*url=" gnus-button-url-regexp
7910      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7911     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7912      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7913     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7914      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7915   "*Alist of headers and regexps to match buttons in article heads.
7916
7917 This alist is very similar to `gnus-button-alist', except that each
7918 alist has an additional HEADER element first in each entry:
7919
7920 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7921
7922 HEADER is a regexp to match a header.  For a fuller explanation, see
7923 `gnus-button-alist'."
7924   :group 'gnus-article-buttons
7925   :group 'gnus-article-headers
7926   :type '(repeat (list (regexp :tag "Header")
7927                        (choice regexp variable)
7928                        (integer :tag "Button")
7929                        (sexp :tag "Form")
7930                        (function :tag "Callback")
7931                        (repeat :tag "Par"
7932                                :inline t
7933                                (integer :tag "Regexp group")))))
7934 (put 'gnus-header-button-alist 'risky-local-variable t)
7935
7936 ;;; Commands:
7937
7938 (defun gnus-article-push-button (event)
7939   "Check text under the mouse pointer for a callback function.
7940 If the text under the mouse pointer has a `gnus-callback' property,
7941 call it with the value of the `gnus-data' text property."
7942   (interactive "e")
7943   (set-buffer (window-buffer (posn-window (event-start event))))
7944   (let* ((pos (posn-point (event-start event)))
7945          (data (get-text-property pos 'gnus-data))
7946          (fun (get-text-property pos 'gnus-callback)))
7947     (goto-char pos)
7948     (when fun
7949       (funcall fun data))))
7950
7951 (defun gnus-article-press-button ()
7952   "Check text at point for a callback function.
7953 If the text at point has a `gnus-callback' property,
7954 call it with the value of the `gnus-data' text property."
7955   (interactive)
7956   (let ((data (get-text-property (point) 'gnus-data))
7957         (fun (get-text-property (point) 'gnus-callback)))
7958     (when fun
7959       (funcall fun data))))
7960
7961 (defun gnus-article-highlight (&optional force)
7962   "Highlight current article.
7963 This function calls `gnus-article-highlight-headers',
7964 `gnus-article-highlight-citation',
7965 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7966 do the highlighting.  See the documentation for those functions."
7967   (interactive (list 'force))
7968   (gnus-article-highlight-headers)
7969   (gnus-article-highlight-citation force)
7970   (gnus-article-highlight-signature)
7971   (gnus-article-add-buttons)
7972   (gnus-article-add-buttons-to-head))
7973
7974 (defun gnus-article-highlight-some (&optional _force)
7975   "Highlight current article.
7976 This function calls `gnus-article-highlight-headers',
7977 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7978 do the highlighting.  See the documentation for those functions."
7979   (interactive (list 'force))
7980   (gnus-article-highlight-headers)
7981   (gnus-article-highlight-signature)
7982   (gnus-article-add-buttons))
7983
7984 (defun gnus-article-highlight-headers ()
7985   "Highlight article headers as specified by `gnus-header-face-alist'."
7986   (interactive)
7987   (gnus-with-article-headers
7988     (let (regexp header-face field-face from hpoints fpoints)
7989       (dolist (entry gnus-header-face-alist)
7990         (goto-char (point-min))
7991         (setq regexp (concat "^\\("
7992                              (if (string-equal "" (nth 0 entry))
7993                                  "[^\t ]"
7994                                (nth 0 entry))
7995                              "\\)")
7996               header-face (nth 1 entry)
7997               field-face (nth 2 entry))
7998         (while (and (re-search-forward regexp nil t)
7999                     (not (eobp)))
8000           (beginning-of-line)
8001           (setq from (point))
8002           (unless (search-forward ":" nil t)
8003             (forward-char 1))
8004           (when (and header-face
8005                      (not (memq (point) hpoints)))
8006             (push (point) hpoints)
8007             (gnus-put-text-property from (point) 'face header-face))
8008           (when (and field-face
8009                      (not (memq (setq from (point)) fpoints)))
8010             (push from fpoints)
8011             (if (re-search-forward "^[^ \t]" nil t)
8012                 (forward-char -2)
8013               (goto-char (point-max)))
8014             (gnus-put-text-property from (point) 'face field-face)))))))
8015
8016 (defun gnus-article-highlight-signature ()
8017   "Highlight the signature in an article.
8018 It does this by highlighting everything after
8019 `gnus-signature-separator' using the face `gnus-signature'."
8020   (interactive)
8021   (gnus-with-article-buffer
8022     (let ((inhibit-point-motion-hooks t))
8023       (save-restriction
8024         (when (and gnus-signature-face
8025                    (gnus-article-narrow-to-signature))
8026           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
8027                             'face gnus-signature-face)
8028           (widen)
8029           (gnus-article-search-signature)
8030           (let ((start (match-beginning 0))
8031                 (end (set-marker (make-marker) (1+ (match-end 0)))))
8032             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
8033                                      end)))))))
8034
8035 (defun gnus-button-in-region-p (b e prop)
8036   "Say whether PROP exists in the region."
8037   (text-property-not-all b e prop nil))
8038
8039 (defun gnus-article-add-buttons ()
8040   "Find external references in the article and make buttons of them.
8041 \"External references\" are things like Message-IDs and URLs, as
8042 specified by `gnus-button-alist'."
8043   (interactive)
8044   (gnus-with-article-buffer
8045     (let ((inhibit-point-motion-hooks t)
8046           (case-fold-search t)
8047           (alist gnus-button-alist)
8048           beg entry regexp)
8049       ;; We skip the headers.
8050       (article-goto-body)
8051       (setq beg (point))
8052       (while (setq entry (pop alist))
8053         (setq regexp (eval (car entry)))
8054         (goto-char beg)
8055         (while (re-search-forward regexp nil t)
8056           (let ((start (match-beginning (nth 1 entry)))
8057                 (end (match-end (nth 1 entry)))
8058                 (from (match-beginning 0)))
8059             (when (and (eval (nth 2 entry))
8060                        (not (gnus-button-in-region-p
8061                              start end 'gnus-callback)))
8062               ;; That optional form returned non-nil, so we add the
8063               ;; button.
8064               (setq from (set-marker (make-marker) from))
8065               (unless (and (eq (car entry) 'gnus-button-url-regexp)
8066                            (gnus-article-extend-url-button from start end))
8067                 (gnus-article-add-button start end
8068                                          'gnus-button-push (list from entry))
8069                 (gnus-put-text-property
8070                  start end
8071                  'gnus-string (buffer-substring-no-properties
8072                                start end))))))))))
8073
8074 (defun gnus-article-extend-url-button (beg start end)
8075   "Extend url button if url is folded into two or more lines.
8076 Return non-nil if button is extended.  BEG is a marker that points to
8077 the beginning position of a text containing url.  START and END are
8078 the endpoints of a url button before it is extended.  The concatenated
8079 url is put as the `gnus-button-url' overlay property on the button."
8080   (let ((opoint (point))
8081         (points (list start end))
8082         url delim regexp)
8083     (prog1
8084         (when (and (progn
8085                      (goto-char end)
8086                      (not (looking-at "[\t ]*[\">]")))
8087                    (progn
8088                      (goto-char start)
8089                      (string-match
8090                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
8091                       (buffer-substring (point-at-bol) start)))
8092                    (progn
8093                      (setq url (list (buffer-substring start end))
8094                            delim (if (match-beginning 1) ">" "\""))
8095                      (beginning-of-line)
8096                      (setq regexp (concat
8097                                    (when (and (looking-at
8098                                                message-cite-prefix-regexp)
8099                                               (< (match-end 0) start))
8100                                      (regexp-quote (match-string 0)))
8101                                    "\
8102 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
8103                                    delim "\\)"))
8104                      (while (progn
8105                               (forward-line 1)
8106                               (and (looking-at regexp)
8107                                    (prog1
8108                                        (match-beginning 1)
8109                                      (push (or (match-string 2)
8110                                                (match-string 1))
8111                                            url)
8112                                      (push (setq end (or (match-end 2)
8113                                                          (match-end 1)))
8114                                            points)
8115                                      (push (or (match-beginning 2)
8116                                                (match-beginning 1))
8117                                            points)))))
8118                      (match-beginning 2)))
8119           (let (gnus-article-mouse-face widget-mouse-face)
8120             (while points
8121               (gnus-article-add-button (pop points) (pop points)
8122                                        'gnus-button-push
8123                                        (list beg (assq 'gnus-button-url-regexp
8124                                                        gnus-button-alist)))))
8125           (let ((overlay (gnus-make-overlay start end)))
8126             (gnus-overlay-put overlay 'evaporate t)
8127             (gnus-overlay-put overlay 'gnus-button-url
8128                               (list (mapconcat 'identity (nreverse url) "")))
8129             (when gnus-article-mouse-face
8130               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
8131           t)
8132       (goto-char opoint))))
8133
8134 ;; Add buttons to the head of an article.
8135 (defun gnus-article-add-buttons-to-head ()
8136   "Add buttons to the head of the article."
8137   (interactive)
8138   (gnus-with-article-headers
8139     (let (beg end)
8140       (dolist (entry gnus-header-button-alist)
8141         ;; Each alist entry.
8142         (goto-char (point-min))
8143         (while (re-search-forward (car entry) nil t)
8144           ;; Each header matching the entry.
8145           (setq beg (match-beginning 0))
8146           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
8147                              (match-beginning 0))
8148                         (point-max)))
8149           (goto-char beg)
8150           (while (re-search-forward (eval (nth 1 entry)) end t)
8151             ;; Each match within a header.
8152             (let* ((entry (cdr entry))
8153                    (start (match-beginning (nth 1 entry)))
8154                    (end (match-end (nth 1 entry)))
8155                    (form (nth 2 entry)))
8156               (goto-char (match-end 0))
8157               (when (eval form)
8158                 (gnus-article-add-button
8159                  start end (nth 3 entry)
8160                  (buffer-substring (match-beginning (nth 4 entry))
8161                                    (match-end (nth 4 entry)))))))
8162           (goto-char end))))))
8163
8164 ;;; External functions:
8165
8166 (defun gnus-article-add-button (from to fun &optional data text)
8167   "Create a button between FROM and TO with callback FUN and data DATA."
8168   (when gnus-article-button-face
8169     (gnus-overlay-put (gnus-make-overlay from to nil t)
8170                       'face gnus-article-button-face))
8171   (gnus-add-text-properties
8172    from to
8173    (nconc (and gnus-article-mouse-face
8174                (list gnus-mouse-face-prop gnus-article-mouse-face))
8175           (list 'gnus-callback fun)
8176           (and data (list 'gnus-data data))))
8177   (widget-convert-button 'link from to :action 'gnus-widget-press-button
8178                          :help-echo (or text "Follow the link")
8179                          :keymap gnus-url-button-map
8180                          :button-keymap gnus-widget-button-keymap))
8181
8182 (defun gnus-article-copy-string ()
8183   "Copy the string in the button to the kill ring."
8184   (interactive)
8185   (gnus-article-check-buffer)
8186   (let ((data (get-text-property (point) 'gnus-string)))
8187     (when data
8188       (with-temp-buffer
8189         (insert data)
8190         (copy-region-as-kill (point-min) (point-max))
8191         (message "Copied %s" data)))))
8192
8193 ;;; Internal functions:
8194
8195 (defun gnus-article-set-globals ()
8196   (with-current-buffer gnus-summary-buffer
8197     (gnus-set-global-variables)))
8198
8199 (defun gnus-signature-toggle (end)
8200   (gnus-with-article-buffer
8201     (let ((inhibit-point-motion-hooks t))
8202       (if (text-property-any end (point-max) 'article-type 'signature)
8203           (progn
8204             (gnus-delete-wash-type 'signature)
8205             (gnus-remove-text-properties-when
8206              'article-type 'signature end (point-max)
8207              (cons 'article-type (cons 'signature
8208                                        gnus-hidden-properties))))
8209         (gnus-add-wash-type 'signature)
8210         (gnus-add-text-properties-when
8211          'article-type nil end (point-max)
8212          (cons 'article-type (cons 'signature
8213                                    gnus-hidden-properties)))))
8214     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
8215       (gnus-set-mode-line 'article))))
8216
8217 (defun gnus-button-push (marker-and-entry)
8218   ;; Push button starting at MARKER.
8219   (save-excursion
8220     (let* ((marker (car marker-and-entry))
8221            (entry (cadr marker-and-entry))
8222            (regexp (car entry))
8223            (inhibit-point-motion-hooks t))
8224       (goto-char marker)
8225       ;; This is obviously true, or something bad is happening :)
8226       ;; But we need it to have the match-data
8227       (when (looking-at (or (if (symbolp regexp)
8228                                 (symbol-value regexp)
8229                               regexp)))
8230         (let ((fun (nth 3 entry))
8231               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
8232                              (get-char-property marker 'gnus-button-url))
8233                         (mapcar (lambda (group)
8234                                   (let ((string (match-string group)))
8235                                     (set-text-properties
8236                                      0 (length string) nil string)
8237                                     string))
8238                                 (nthcdr 4 entry)))))
8239
8240           (cond
8241            ((fboundp fun)
8242             (apply fun args))
8243            ((and (boundp fun)
8244                  (fboundp (symbol-value fun)))
8245             (apply (symbol-value fun) args))
8246            (t
8247             (gnus-message 1 "You must define `%S' to use this button"
8248                           (cons fun args)))))))))
8249
8250 (defun gnus-parse-news-url (url)
8251   (let (scheme server port group message-id articles)
8252     (with-temp-buffer
8253       (insert url)
8254       (goto-char (point-min))
8255       (when (looking-at "\\([A-Za-z]+\\):")
8256         (setq scheme (match-string 1))
8257         (goto-char (match-end 0)))
8258       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8259         (setq server (match-string 1))
8260         (setq port (if (stringp (match-string 3))
8261                        (string-to-number (match-string 3))
8262                      (match-string 3)))
8263         (goto-char (match-end 0)))
8264
8265       (cond
8266        ((looking-at "\\(.*@.*\\)")
8267         (setq message-id (match-string 1)))
8268        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8269         (setq group (match-string 1)
8270               articles (split-string (match-string 2) "-")))
8271        ((looking-at "\\([^/]+\\)/?")
8272         (setq group (match-string 1)))
8273        (t
8274         (error "Unknown news URL syntax"))))
8275     (list scheme server port group message-id articles)))
8276
8277 (defvar nntp-port-number)
8278
8279 (defun gnus-button-handle-news (url)
8280   "Fetch a news URL."
8281   (destructuring-bind (_scheme server port group message-id _articles)
8282       (gnus-parse-news-url url)
8283     (cond
8284      (message-id
8285       (with-current-buffer gnus-summary-buffer
8286         (if server
8287             (let ((gnus-refer-article-method
8288                    (nconc (list (list 'nntp server))
8289                           gnus-refer-article-method))
8290                   (nntp-port-number (or port "nntp")))
8291               (gnus-message 7 "Fetching %s with %s"
8292                             message-id gnus-refer-article-method)
8293               (gnus-summary-refer-article message-id))
8294           (gnus-summary-refer-article message-id))))
8295      (group
8296       (gnus-button-fetch-group url)))))
8297
8298 (defun gnus-button-patch (library line)
8299   "Visit an Emacs Lisp library LIBRARY on line LINE."
8300   (interactive)
8301   (let ((file (locate-library (file-name-nondirectory library))))
8302     (unless file
8303       (error "Couldn't find library %s" library))
8304     (find-file file)
8305     (goto-char (point-min))
8306     (forward-line (1- (string-to-number line)))))
8307
8308 (defun gnus-button-handle-man (url)
8309   "Fetch a man page."
8310   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8311   (when (eq gnus-button-man-handler 'woman)
8312     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8313   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8314   (funcall gnus-button-man-handler url))
8315
8316 (defun gnus-button-handle-info-url (url)
8317   "Fetch an info URL."
8318   (setq url (mm-subst-char-in-string ?+ ?\  url))
8319   (cond
8320    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8321     (gnus-info-find-node
8322      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8323                      "Gnus")
8324              ")" (gnus-url-unhex-string (match-string 2 url)))))
8325    ((string-match "([^)\"]+)[^\"]+" url)
8326     (setq url
8327           (gnus-replace-in-string
8328            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8329     (gnus-info-find-node url))
8330    (t (error "Can't parse %s" url))))
8331
8332 (defun gnus-button-handle-info-url-gnome (url)
8333   "Fetch GNOME style info URL."
8334   (setq url (mm-subst-char-in-string ?_ ?\  url))
8335   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8336       (gnus-info-find-node
8337        (concat "("
8338                (gnus-url-unhex-string
8339                  (match-string 1 url))
8340                ")"
8341                (or (gnus-url-unhex-string
8342                     (match-string 2 url))
8343                    "Top")))
8344     (error "Can't parse %s" url)))
8345
8346 (defun gnus-button-handle-info-url-kde (url)
8347   "Fetch KDE style info URL."
8348   (gnus-info-find-node (gnus-url-unhex-string url)))
8349
8350 ;; (info) will autoload info.el
8351 (declare-function Info-menu "info" (menu-item &optional fork))
8352 (declare-function Info-index-next "info" (num))
8353
8354 (defun gnus-button-handle-info-keystrokes (url)
8355   "Call `info' when pushing the corresponding URL button."
8356   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8357   (let (node indx comma)
8358     (if (string-match
8359          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8360                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8361                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8362                  "\\(?:[ \t\n,]*\\)\\)?")
8363          url)
8364         (setq node (match-string 2 url)
8365               indx (match-string 3 url))
8366       (error "Can't parse %s" url))
8367     (info)
8368     (Info-directory)
8369     (Info-menu node)
8370     (when (> (length indx) 0)
8371       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8372                             "\\([ \t\n,]*\\)")
8373                     indx)
8374       (setq comma (match-string 2 indx))
8375       (setq indx  (match-string 1 indx))
8376       (Info-index indx)
8377       (when comma
8378         (dotimes (i (with-temp-buffer
8379                       (insert comma)
8380                       ;; Note: the XEmacs version of `how-many' takes
8381                       ;; no optional argument.
8382                       (goto-char (point-min))
8383                       (how-many ",")))
8384           (Info-index-next 1)))
8385       nil)))
8386
8387 (autoload 'pgg-snarf-keys-region "pgg")
8388 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8389 (declare-function pgg-display-output-buffer "pgg" (start end status))
8390
8391 (defun gnus-button-openpgp (url)
8392   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8393   (with-temp-buffer
8394     (mm-url-insert-file-contents-external url)
8395     (pgg-snarf-keys-region (point-min) (point-max))
8396     (pgg-display-output-buffer nil nil nil)))
8397
8398 (defun gnus-button-message-id (message-id)
8399   "Fetch MESSAGE-ID."
8400   (with-current-buffer gnus-summary-buffer
8401     (gnus-summary-refer-article message-id)))
8402
8403 (defun gnus-button-fetch-group (address &rest _ignore)
8404   "Fetch GROUP specified by ADDRESS."
8405   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8406                       address)
8407     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8408     ;; for nntp:// and news://
8409     (setq address (match-string 3 address)))
8410   (if (not (string-match "[:/]" address))
8411       ;; This is just a simple group url.
8412       (gnus-group-read-ephemeral-group address gnus-select-method)
8413     (if (not
8414          (string-match
8415           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8416           address))
8417         (error "Can't parse %s" address)
8418       (gnus-group-read-ephemeral-group
8419        (match-string 4 address)
8420        `(nntp ,(match-string 1 address)
8421               (nntp-address ,(match-string 1 address))
8422               (nntp-port-number ,(if (match-end 3)
8423                                      (match-string 3 address)
8424                                    "nntp")))
8425        nil nil nil
8426        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8427
8428 (defun gnus-url-parse-query-string (query &optional downcase)
8429   (let (retval pairs cur key val)
8430     (setq pairs (split-string query "&"))
8431     (while pairs
8432       (setq cur (car pairs)
8433             pairs (cdr pairs))
8434       (if (not (string-match "=" cur))
8435           nil                           ; Grace
8436         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8437               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8438         (if downcase
8439             (setq key (downcase key)))
8440         (setq cur (assoc key retval))
8441         (if cur
8442             (setcdr cur (cons val (cdr cur)))
8443           (setq retval (cons (list key val) retval)))))
8444     retval))
8445
8446 (defun gnus-url-mailto (url)
8447   ;; Send mail to someone
8448   (setq url (replace-regexp-in-string "\n" " " url))
8449   (when (string-match "mailto:/*\\(.*\\)" url)
8450     (setq url (substring url (match-beginning 1) nil)))
8451   (let* ((args (gnus-url-parse-query-string
8452                 (if (string-match "^\\?" url)
8453                     (substring url 1)
8454                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8455                       (concat "to=" (match-string 1 url) "&"
8456                               (match-string 2 url))
8457                     (concat "to=" url)))))
8458          (subject (cdr-safe (assoc "subject" args)))
8459          func)
8460     (gnus-msg-mail)
8461     (while args
8462       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8463       (if (fboundp func)
8464           (funcall func)
8465         (message-position-on-field (caar args)))
8466       (insert (gnus-replace-in-string
8467                (mapconcat 'identity (reverse (cdar args)) ", ")
8468                "\r\n" "\n" t))
8469       (setq args (cdr args)))
8470     (if subject
8471         (message-goto-body)
8472       (message-goto-subject))))
8473
8474 (defun gnus-button-embedded-url (address)
8475   "Activate ADDRESS with `browse-url'."
8476   (browse-url (gnus-strip-whitespace address)))
8477
8478 ;;; Next/prev buttons in the article buffer.
8479
8480 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8481 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8482
8483 (defvar gnus-prev-page-map
8484   (let ((map (make-sparse-keymap)))
8485     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8486     (define-key map "\r" 'gnus-button-prev-page)
8487     map))
8488
8489 (defvar gnus-next-page-map
8490   (let ((map (make-sparse-keymap)))
8491     (define-key map gnus-mouse-2 'gnus-button-next-page)
8492     (define-key map "\r" 'gnus-button-next-page)
8493     map))
8494
8495 (defun gnus-insert-prev-page-button ()
8496   (let ((b (point)) e
8497         (inhibit-read-only t))
8498     (gnus-eval-format
8499      gnus-prev-page-line-format nil
8500      `(keymap ,gnus-prev-page-map
8501               gnus-prev t
8502               gnus-callback gnus-article-button-prev-page
8503               article-type annotation))
8504     (setq e (if (bolp)
8505                 ;; Exclude a newline.
8506                 (1- (point))
8507               (point)))
8508     (when gnus-article-button-face
8509       (gnus-overlay-put (gnus-make-overlay b e nil t)
8510                         'face gnus-article-button-face))
8511     (widget-convert-button
8512      'link b e
8513      :action 'gnus-button-prev-page
8514      :button-keymap gnus-prev-page-map)))
8515
8516 (defun gnus-button-next-page (&optional _args _more-args)
8517   "Go to the next page."
8518   (interactive)
8519   (let ((win (selected-window)))
8520     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8521     (gnus-article-next-page)
8522     (select-window win)))
8523
8524 (defun gnus-button-prev-page (&optional _args _more-args)
8525   "Go to the prev page."
8526   (interactive)
8527   (let ((win (selected-window)))
8528     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8529     (gnus-article-prev-page)
8530     (select-window win)))
8531
8532 (defun gnus-insert-next-page-button ()
8533   (let ((b (point)) e
8534         (inhibit-read-only t))
8535     (gnus-eval-format gnus-next-page-line-format nil
8536                       `(keymap ,gnus-next-page-map
8537                                gnus-next t
8538                                gnus-callback gnus-article-button-next-page
8539                                article-type annotation))
8540     (setq e (if (bolp)
8541                 ;; Exclude a newline.
8542                 (1- (point))
8543               (point)))
8544     (when gnus-article-button-face
8545       (gnus-overlay-put (gnus-make-overlay b e nil t)
8546                         'face gnus-article-button-face))
8547     (widget-convert-button
8548      'link b e
8549      :action 'gnus-button-next-page
8550      :button-keymap gnus-next-page-map)))
8551
8552 (defun gnus-article-button-next-page (_arg)
8553   "Go to the next page."
8554   (interactive "P")
8555   (let ((win (selected-window)))
8556     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8557     (gnus-article-next-page)
8558     (select-window win)))
8559
8560 (defun gnus-article-button-prev-page (_arg)
8561   "Go to the prev page."
8562   (interactive "P")
8563   (let ((win (selected-window)))
8564     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8565     (gnus-article-prev-page)
8566     (select-window win)))
8567
8568 (defvar gnus-decode-header-methods
8569   '(mail-decode-encoded-word-region)
8570   "List of methods used to decode headers.
8571
8572 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8573 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8574 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8575 whose names match REGEXP.
8576
8577 For example:
8578 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8579  mail-decode-encoded-word-region
8580  (\"chinese\" . rfc1843-decode-region))
8581 ")
8582
8583 (defvar gnus-decode-header-methods-cache nil)
8584
8585 (defun gnus-multi-decode-header (start end)
8586   "Apply the functions from `gnus-encoded-word-methods' that match."
8587   (unless (and gnus-decode-header-methods-cache
8588                (eq gnus-newsgroup-name
8589                    (car gnus-decode-header-methods-cache)))
8590     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8591     (dolist (x gnus-decode-header-methods)
8592       (if (symbolp x)
8593           (nconc gnus-decode-header-methods-cache (list x))
8594         (if (and gnus-newsgroup-name
8595                  (string-match (car x) gnus-newsgroup-name))
8596             (nconc gnus-decode-header-methods-cache
8597                    (list (cdr x)))))))
8598   (let ((xlist gnus-decode-header-methods-cache))
8599     (pop xlist)
8600     (save-restriction
8601       (narrow-to-region start end)
8602       (while xlist
8603         (funcall (pop xlist) (point-min) (point-max))))))
8604
8605 ;;;
8606 ;;; Treatment top-level handling.
8607 ;;;
8608
8609 (defvar gnus-inhibit-article-treatments nil)
8610
8611 ;; Dynamic variables.
8612 (defvar part-number)                    ;FIXME: Lacks a "gnus-" prefix.
8613 (defvar total-parts)                    ;FIXME: Lacks a "gnus-" prefix.
8614 (defvar gnus-treat-type)
8615 (defvar gnus-treat-condition)
8616 (defvar gnus-treat-length)
8617
8618 (defun gnus-treat-article (condition
8619                            &optional part-num total type)
8620   (let ((gnus-treat-condition condition)
8621         (part-number part-num)
8622         (total-parts total)
8623         (gnus-treat-type type)
8624         (gnus-treat-length (- (point-max) (point-min)))
8625         (alist gnus-treatment-function-alist)
8626         (article-goto-body-goes-to-point-min-p t)
8627         (treated-type
8628          (or (not type)
8629              (catch 'found
8630                (let ((list gnus-article-treat-types))
8631                  (while list
8632                    (when (string-match (pop list) type)
8633                      (throw 'found t)))))))
8634         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8635         val)
8636     (gnus-run-hooks 'gnus-part-display-hook)
8637     (dolist (elem alist)
8638       (setq val
8639             (save-excursion
8640               (when (gnus-buffer-live-p gnus-summary-buffer)
8641                 (set-buffer gnus-summary-buffer))
8642               (symbol-value (car elem))))
8643       (when (and (or (consp val)
8644                      treated-type)
8645                  (or (not gnus-inhibit-article-treatments)
8646                      (eq gnus-treat-condition 'head))
8647                  (gnus-treat-predicate val)
8648                  (or (not (get (car elem) 'highlight))
8649                      highlightp))
8650         (save-restriction
8651           (funcall (cadr elem)))))))
8652
8653 (defun gnus-treat-predicate (val)
8654   (cond
8655    ((null val)
8656     nil)
8657    (gnus-treat-condition
8658     (eq gnus-treat-condition val))
8659    ((and (listp val)
8660          (stringp (car val)))
8661     (apply 'gnus-or (mapcar `(lambda (s)
8662                                (string-match s ,(or gnus-newsgroup-name "")))
8663                             val)))
8664    ((listp val)
8665     (let ((pred (pop val)))
8666       (cond
8667        ((eq pred 'or)
8668         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8669        ((eq pred 'and)
8670         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8671        ((eq pred 'not)
8672         (not (gnus-treat-predicate (car val))))
8673        ((eq pred 'typep)
8674         (equal (car val) gnus-treat-type))
8675        ((functionp pred)
8676         (funcall pred))
8677        (t
8678         (error "%S is not a valid predicate" pred)))))
8679    ((eq val t)
8680     t)
8681    ((eq val 'head)
8682     nil)
8683    ((eq val 'first)
8684     (eq part-number 1))
8685    ((eq val 'last)
8686     (eq part-number total-parts))
8687    ((numberp val)
8688     (< gnus-treat-length val))
8689    (t
8690     (error "%S is not a valid value" val))))
8691
8692 (defun gnus-article-encrypt-body (protocol &optional n)
8693   "Encrypt the article body."
8694   (interactive
8695    (list
8696     (or gnus-article-encrypt-protocol
8697         (gnus-completing-read "Encrypt protocol"
8698                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8699                               t))
8700     current-prefix-arg))
8701   ;; User might hit `K E' instead of `K e', so prompt once.
8702   (when (and gnus-article-encrypt-protocol
8703              gnus-novice-user)
8704     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8705       (error "Encrypt aborted")))
8706   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8707     (unless func
8708       (error "Can't find the encrypt protocol %s" protocol))
8709     (if (member gnus-newsgroup-name '("nndraft:delayed"
8710                                       "nndraft:drafts"
8711                                       "nndraft:queue"))
8712         (error "Can't encrypt the article in group %s"
8713                gnus-newsgroup-name))
8714     (gnus-summary-iterate n
8715       (with-current-buffer gnus-summary-buffer
8716         (let ((mail-parse-charset gnus-newsgroup-charset)
8717               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8718               (summary-buffer gnus-summary-buffer)
8719               references point)
8720           (gnus-set-global-variables)
8721           (when (gnus-group-read-only-p)
8722             (error "The current newsgroup does not support article encrypt"))
8723           (gnus-summary-show-article t)
8724           (setq references
8725               (or (mail-header-references gnus-current-headers) ""))
8726           (set-buffer gnus-article-buffer)
8727           (let* ((inhibit-read-only t)
8728                  (headers
8729                   (mapcar (lambda (field)
8730                             (and (save-restriction
8731                                    (message-narrow-to-head)
8732                                    (goto-char (point-min))
8733                                    (search-forward field nil t))
8734                                  (prog2
8735                                      (message-narrow-to-field)
8736                                      (buffer-string)
8737                                    (delete-region (point-min) (point-max))
8738                                    (widen))))
8739                           '("Content-Type:" "Content-Transfer-Encoding:"
8740                             "Content-Disposition:"))))
8741             (message-narrow-to-head)
8742             (message-remove-header "MIME-Version")
8743             (goto-char (point-max))
8744             (setq point (point))
8745             (insert (apply 'concat headers))
8746             (widen)
8747             (narrow-to-region point (point-max))
8748             (let ((message-options message-options))
8749               (message-options-set 'message-sender user-mail-address)
8750               (message-options-set 'message-recipients user-mail-address)
8751               (message-options-set 'message-sign-encrypt 'not)
8752               (funcall func))
8753             (goto-char (point-min))
8754             (insert "MIME-Version: 1.0\n")
8755             (widen)
8756             (gnus-summary-edit-article-done
8757              references nil summary-buffer t))
8758           (when gnus-keep-backlog
8759             (gnus-backlog-remove-article
8760              (car gnus-article-current) (cdr gnus-article-current)))
8761           (gnus-flush-original-article-buffer)
8762           (when gnus-use-cache
8763             (gnus-cache-update-article
8764              (car gnus-article-current) (cdr gnus-article-current))))))))
8765
8766 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8767   "The following specs can be used:
8768 %t  The security MIME type
8769 %i  Additional info
8770 %d  Details
8771 %D  Details if button is pressed")
8772
8773 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8774   "The following specs can be used:
8775 %t  The security MIME type
8776 %i  Additional info
8777 %d  Details
8778 %D  Details if button is pressed")
8779
8780 (defvar gnus-mime-security-button-line-format-alist
8781   '((?t gnus-tmp-type ?s)
8782     (?i gnus-tmp-info ?s)
8783     (?d gnus-tmp-details ?s)
8784     (?D gnus-tmp-pressed-details ?s)))
8785
8786 (defvar gnus-mime-security-button-commands
8787   '((gnus-article-press-button "\r" "Show Detail")
8788     (undefined "v")
8789     (undefined "t")
8790     (undefined "C")
8791     (gnus-mime-security-save-part "o" "Save...")
8792     (undefined "\C-o")
8793     (undefined "r")
8794     (undefined "d")
8795     (undefined "c")
8796     (undefined "i")
8797     (undefined "E")
8798     (undefined "e")
8799     (undefined "p")
8800     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8801     (undefined ".")))
8802
8803 (defvar gnus-mime-security-button-map
8804   (let ((map (make-sparse-keymap)))
8805     (define-key map gnus-mouse-2 'gnus-article-push-button)
8806     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8807     (dolist (c gnus-mime-security-button-commands)
8808       (define-key map (cadr c) (car c)))
8809     map))
8810
8811 (easy-menu-define
8812   gnus-mime-security-button-menu gnus-mime-security-button-map
8813   "Security button menu."
8814   `("Security Part"
8815     ,@(delq nil
8816             (mapcar (lambda (c)
8817                       (unless (eq (car c) 'undefined)
8818                         (vector (caddr c) (car c) :active t)))
8819                     gnus-mime-security-button-commands))))
8820
8821 (defun gnus-mime-security-button-menu (event prefix)
8822   "Construct a context-sensitive menu of security commands."
8823   (interactive "e\nP")
8824   (save-window-excursion
8825     (let ((pos (event-start event)))
8826       (select-window (posn-window pos))
8827       (goto-char (posn-point pos))
8828       (gnus-article-check-buffer)
8829       (popup-menu gnus-mime-security-button-menu nil prefix))))
8830
8831 (defvar gnus-mime-security-details-buffer nil)
8832
8833 (defvar gnus-mime-security-button-pressed nil)
8834
8835 (defvar gnus-mime-security-show-details-inline t
8836   "If non-nil, show details in the article buffer.")
8837
8838 (defun gnus-mime-security-verify-or-decrypt (handle)
8839   (mm-remove-parts (cdr handle))
8840   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8841         point (inhibit-read-only t))
8842     (if region
8843         (goto-char (car region)))
8844     (setq point (point))
8845     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8846       (let* ((mm-verify-option 'known)
8847              (mm-decrypt-option 'known)
8848              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8849         (unless (eq nparts (cdr handle))
8850           (mm-destroy-parts (cdr handle))
8851           (setcdr handle nparts))))
8852     (gnus-mime-display-security handle)
8853     (when region
8854       (delete-region (point) (cdr region))
8855       (set-marker (car region) nil)
8856       (set-marker (cdr region) nil))
8857     (goto-char point)))
8858
8859 (defun gnus-mime-security-show-details (handle)
8860   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8861     (if (not details)
8862         (gnus-message 5 "No details.")
8863       (if gnus-mime-security-show-details-inline
8864           (let ((gnus-mime-security-button-pressed
8865                  (not (get-text-property (point) 'gnus-mime-details)))
8866                 (gnus-mime-security-button-line-format
8867                  (get-text-property (point) 'gnus-line-format))
8868                 (inhibit-read-only t))
8869             (forward-char -1)
8870             (while (eq (get-text-property (point) 'gnus-line-format)
8871                        gnus-mime-security-button-line-format)
8872               (forward-char -1))
8873             (forward-char)
8874             (save-restriction
8875               (narrow-to-region (point) (point))
8876               (gnus-insert-mime-security-button handle))
8877             (delete-region (point)
8878                            (or (text-property-not-all
8879                                 (point) (point-max)
8880                                 'gnus-line-format
8881                                 gnus-mime-security-button-line-format)
8882                                (point-max))))
8883         ;; Not inlined.
8884         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8885             (with-current-buffer gnus-mime-security-details-buffer
8886               (erase-buffer)
8887               t)
8888           (setq gnus-mime-security-details-buffer
8889                 (gnus-get-buffer-create "*MIME Security Details*")))
8890         (with-current-buffer gnus-mime-security-details-buffer
8891           (insert details)
8892           (goto-char (point-min)))
8893         (pop-to-buffer gnus-mime-security-details-buffer)))))
8894
8895 (defun gnus-mime-security-press-button (handle)
8896   (save-excursion
8897     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8898         (gnus-mime-security-show-details handle)
8899       (gnus-mime-security-verify-or-decrypt handle))))
8900
8901 (defun gnus-insert-mime-security-button (handle &optional _displayed)
8902   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8903          (gnus-tmp-type
8904           (concat
8905            (or (nth 2 (assoc protocol mm-verify-function-alist))
8906                (nth 2 (assoc protocol mm-decrypt-function-alist))
8907                "Unknown")
8908            (if (equal (car handle) "multipart/signed")
8909                " Signed" " Encrypted")
8910            " Part"))
8911          (gnus-tmp-info
8912           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8913               "Undecided"))
8914          (gnus-tmp-details
8915           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8916          gnus-tmp-pressed-details
8917          b e)
8918     (setq gnus-tmp-details
8919           (if gnus-tmp-details
8920               (concat "\n" gnus-tmp-details)
8921             ""))
8922     (setq gnus-tmp-pressed-details
8923           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8924     (unless (bolp)
8925       (insert "\n"))
8926     (setq b (point))
8927     (gnus-eval-format
8928      gnus-mime-security-button-line-format
8929      gnus-mime-security-button-line-format-alist
8930      `(keymap ,gnus-mime-security-button-map
8931          gnus-callback gnus-mime-security-press-button
8932          gnus-line-format ,gnus-mime-security-button-line-format
8933          gnus-mime-details ,gnus-mime-security-button-pressed
8934          article-type annotation
8935          gnus-data ,handle))
8936     (setq e (if (bolp)
8937                 ;; Exclude a newline.
8938                 (1- (point))
8939               (point)))
8940     (when gnus-article-button-face
8941       (gnus-overlay-put (gnus-make-overlay b e nil t)
8942                         'face gnus-article-button-face))
8943     (widget-convert-button
8944      'link b e
8945      :mime-handle handle
8946      :action 'gnus-widget-press-button
8947      :button-keymap gnus-mime-security-button-map
8948      :help-echo
8949      (lambda (_widget)
8950        ;; Needed to properly clear the message due to a bug in
8951        ;; wid-edit (XEmacs only).
8952        (when (boundp 'help-echo-owns-message)
8953          (setq help-echo-owns-message t))
8954        (format
8955         "%S: show detail; %S: more options"
8956         (aref gnus-mouse-2 0)
8957         (aref gnus-down-mouse-3 0))))))
8958
8959 (defun gnus-mime-display-security (handle)
8960   (save-restriction
8961     (narrow-to-region (point) (point))
8962     (unless (gnus-unbuttonized-mime-type-p (car handle))
8963       (gnus-insert-mime-security-button handle))
8964     (gnus-mime-display-part (cadr handle))
8965     (unless (bolp)
8966       (insert "\n"))
8967     (unless (gnus-unbuttonized-mime-type-p (car handle))
8968       (let ((gnus-mime-security-button-line-format
8969              gnus-mime-security-button-end-line-format))
8970         (gnus-insert-mime-security-button handle)))
8971     (mm-set-handle-multipart-parameter
8972      handle 'gnus-region (cons (point-min-marker) (point-max-marker)))
8973     (goto-char (point-max))))
8974
8975 (defun gnus-mime-security-run-function (function)
8976   "Run FUNCTION with the security part under point."
8977   (gnus-article-check-buffer)
8978   (let ((data (get-text-property (point) 'gnus-data))
8979         buffer handle)
8980     (when (and (stringp (car-safe data))
8981                (setq buffer (mm-handle-multipart-original-buffer data))
8982                (setq handle (cadr data)))
8983       (if (bufferp (mm-handle-buffer handle))
8984           (progn
8985             (setq handle (cons buffer (copy-sequence (cdr handle))))
8986             (mm-handle-set-undisplayer handle nil))
8987         (setq handle (mm-make-handle
8988                       buffer
8989                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8990                       nil nil nil nil nil nil)))
8991       (funcall function handle))))
8992
8993 (defun gnus-mime-security-save-part ()
8994   "Save the security part under point."
8995   (interactive)
8996   (gnus-mime-security-run-function 'mm-save-part))
8997
8998 (defun gnus-mime-security-pipe-part ()
8999   "Pipe the security part under point to a process."
9000   (interactive)
9001   (gnus-mime-security-run-function 'mm-pipe-part))
9002
9003 (gnus-ems-redefine)
9004
9005 (provide 'gnus-art)
9006
9007 (run-hooks 'gnus-art-load-hook)
9008
9009 ;;; gnus-art.el ends here