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