1949e97df4294c079f1752aed0f92976f6b64dc6
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2014 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile
28   (require 'cl))
29 (defvar tool-bar-map)
30 (defvar w3m-minor-mode-map)
31
32 (require 'gnus)
33 (require 'gnus-util)
34 (require 'gnus-sum)
35 (require 'gnus-spec)
36 (require 'gnus-int)
37 (require 'gnus-win)
38 (require 'mm-bodies)
39 (require 'mail-parse)
40 (require 'mm-decode)
41 (require 'mm-view)
42 (require 'wid-edit)
43 (require 'mm-uu)
44 (require 'message)
45 (require 'mouse)
46
47 (autoload 'gnus-msg-mail "gnus-msg" nil t)
48 (autoload 'gnus-button-mailto "gnus-msg")
49 (autoload 'gnus-button-reply "gnus-msg" nil t)
50 (autoload 'parse-time-string "parse-time" nil nil)
51 (autoload 'ansi-color-apply-on-region "ansi-color")
52 (autoload 'mm-url-insert-file-contents-external "mm-url")
53 (autoload 'mm-extern-cache-contents "mm-extern")
54
55 (defgroup gnus-article nil
56   "Article display."
57   :link '(custom-manual "(gnus)Article Buffer")
58   :group 'gnus)
59
60 (defgroup gnus-article-treat nil
61   "Treating article parts."
62   :link '(custom-manual "(gnus)Article Hiding")
63   :group 'gnus-article)
64
65 (defgroup gnus-article-hiding nil
66   "Hiding article parts."
67   :link '(custom-manual "(gnus)Article Hiding")
68   :group 'gnus-article)
69
70 (defgroup gnus-article-highlight nil
71   "Article highlighting."
72   :link '(custom-manual "(gnus)Article Highlighting")
73   :group 'gnus-article
74   :group 'gnus-visual)
75
76 (defgroup gnus-article-signature nil
77   "Article signatures."
78   :link '(custom-manual "(gnus)Article Signature")
79   :group 'gnus-article)
80
81 (defgroup gnus-article-headers nil
82   "Article headers."
83   :link '(custom-manual "(gnus)Hiding Headers")
84   :group 'gnus-article)
85
86 (defgroup gnus-article-washing nil
87   "Special commands on articles."
88   :link '(custom-manual "(gnus)Article Washing")
89   :group 'gnus-article)
90
91 (defgroup gnus-article-emphasis nil
92   "Fontisizing articles."
93   :link '(custom-manual "(gnus)Article Fontisizing")
94   :group 'gnus-article)
95
96 (defgroup gnus-article-saving nil
97   "Saving articles."
98   :link '(custom-manual "(gnus)Saving Articles")
99   :group 'gnus-article)
100
101 (defgroup gnus-article-mime nil
102   "Worshiping the MIME wonder."
103   :link '(custom-manual "(gnus)Using MIME")
104   :group 'gnus-article)
105
106 (defgroup gnus-article-buttons nil
107   "Pushable buttons in the article buffer."
108   :link '(custom-manual "(gnus)Article Buttons")
109   :group 'gnus-article)
110
111 (defgroup gnus-article-various nil
112   "Other article options."
113   :link '(custom-manual "(gnus)Misc Article")
114   :group 'gnus-article)
115
116 (defcustom gnus-ignored-headers
117   (mapcar
118    (lambda (header)
119      (concat "^" header ":"))
120    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
121      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
122      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
123      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
124      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
125      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
126      "X-Attribution" "X-Originating-IP" "Delivered-To"
127      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
128      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
129      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
130      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
131      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
132      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
133      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
134      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
135      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
136      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
137      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
138      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
139      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
140      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
141      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
142      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
143      "List-[A-Za-z]+" "X-Listprocessor-Version"
144      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
145      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
146      "X-Received" "Content-length" "X-precedence"
147      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
148      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
149      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
150      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
151      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
152      "X-Content-length" "X-Posting-Agent" "Original-Received"
153      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
154      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
155      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
156      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
157      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
158      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
159      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
160      "Envelope-Sender" "Envelope-Recipients"))
161   "*All headers that start with this regexp will be hidden.
162 This variable can also be a list of regexps of headers to be ignored.
163 If `gnus-visible-headers' is non-nil, this variable will be ignored."
164   :type '(choice regexp
165                  (repeat regexp))
166   :group 'gnus-article-hiding)
167
168 (defcustom gnus-visible-headers
169   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:"
170   "*All headers that do not match this regexp will be hidden.
171 This variable can also be a list of regexp of headers to remain visible.
172 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
173   :type '(choice
174           (repeat :value-to-internal (lambda (widget value)
175                                        (custom-split-regexp-maybe value))
176                   :match (lambda (widget value)
177                            (or (stringp value)
178                                (widget-editable-list-match widget value)))
179                   regexp)
180           (const :tag "Use gnus-ignored-headers" nil)
181           regexp)
182   :group 'gnus-article-hiding)
183
184 (defcustom gnus-sorted-header-list
185   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
186     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
187   "*This variable is a list of regular expressions.
188 If it is non-nil, headers that match the regular expressions will
189 be placed first in the article buffer in the sequence specified by
190 this list."
191   :type '(repeat regexp)
192   :group 'gnus-article-hiding)
193
194 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
195   "Headers that are only to be displayed if they have interesting data.
196 Possible values in this list are:
197
198   'empty       Headers with no content.
199   'newsgroups  Newsgroup identical to Gnus group.
200   'to-address  To identical to To-address.
201   'to-list     To identical to To-list.
202   'cc-list     CC identical to To-list.
203   'followup-to Followup-to identical to Newsgroups.
204   'reply-to    Reply-to identical to From.
205   'date        Date less than four days old.
206   'long-to     To and/or Cc longer than 1024 characters.
207   'many-to     Multiple To and/or Cc."
208   :type '(set (const :tag "Headers with no content." empty)
209               (const :tag "Newsgroups identical to Gnus group." newsgroups)
210               (const :tag "To identical to To-address." to-address)
211               (const :tag "To identical to To-list." to-list)
212               (const :tag "CC identical to To-list." cc-list)
213               (const :tag "Followup-to identical to Newsgroups." followup-to)
214               (const :tag "Reply-to identical to From." reply-to)
215               (const :tag "Date less than four days old." date)
216               (const :tag "To and/or Cc longer than 1024 characters." long-to)
217               (const :tag "Multiple To and/or Cc headers." many-to))
218   :group 'gnus-article-hiding)
219
220 (defcustom gnus-article-skip-boring nil
221   "Skip over text that is not worth reading.
222 By default, if you set this t, then Gnus will display citations and
223 signatures, but will never scroll down to show you a page consisting
224 only of boring text.  Boring text is controlled by
225 `gnus-article-boring-faces'."
226   :version "22.1"
227   :type 'boolean
228   :group 'gnus-article-hiding)
229
230 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
231   "Regexp matching signature separator.
232 This can also be a list of regexps.  In that case, it will be checked
233 from head to tail looking for a separator.  Searches will be done from
234 the end of the buffer."
235   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
236                  (regexp)
237                  (repeat :tag "List of regexp" regexp))
238   :group 'gnus-article-signature)
239
240 (defcustom gnus-signature-limit nil
241   "Provide a limit to what is considered a signature.
242 If it is a number, no signature may not be longer (in characters) than
243 that number.  If it is a floating point number, no signature may be
244 longer (in lines) than that number.  If it is a function, the function
245 will be called without any parameters, and if it returns nil, there is
246 no signature in the buffer.  If it is a string, it will be used as a
247 regexp.  If it matches, the text in question is not a signature.
248
249 This can also be a list of the above values."
250   :type '(choice (const nil)
251                  (integer :value 200)
252                  (number :value 4.0)
253                  function
254                  (regexp :value ".*"))
255   :group 'gnus-article-signature)
256
257 (defcustom gnus-hidden-properties '(invisible t intangible t)
258   "Property list to use for hiding text."
259   :type 'sexp
260   :group 'gnus-article-hiding)
261
262 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
263 ;; frames in a session.
264 (defcustom gnus-article-x-face-command
265   (if (featurep 'xemacs)
266       (if (or (gnus-image-type-available-p 'xface)
267               (gnus-image-type-available-p 'pbm))
268           'gnus-display-x-face-in-from
269         "{ echo \
270 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
271 ; uncompface; } | icontopbm | ee -")
272     (if (gnus-image-type-available-p 'pbm)
273         'gnus-display-x-face-in-from
274       "{ echo \
275 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
276 ; uncompface; } | icontopbm | display -"))
277   "*String or function to be executed to display an X-Face header.
278 If it is a string, the command will be executed in a sub-shell
279 asynchronously.  The compressed face will be piped to this command."
280   :type `(choice string
281                  (function-item gnus-display-x-face-in-from)
282                  function)
283   :version "21.1"
284   :group 'gnus-picon
285   :group 'gnus-article-washing)
286
287 (defcustom gnus-article-x-face-too-ugly nil
288   "Regexp matching posters whose face shouldn't be shown automatically."
289   :type '(choice regexp (const nil))
290   :group 'gnus-article-washing)
291
292 (defcustom gnus-article-banner-alist nil
293   "Banner alist for stripping.
294 For example,
295      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
296   :version "21.1"
297   :type '(repeat (cons symbol regexp))
298   :group 'gnus-article-washing)
299
300 (gnus-define-group-parameter
301  banner
302  :variable-document
303  "Alist of regexps (to match group names) and banner."
304  :variable-group gnus-article-washing
305  :parameter-type
306  '(choice :tag "Banner"
307           :value nil
308           (const :tag "Remove signature" signature)
309           (symbol :tag "Item in `gnus-article-banner-alist'" none)
310           regexp
311           (const :tag "None" nil))
312  :parameter-document
313  "If non-nil, specify how to remove `banners' from articles.
314
315 Symbol `signature' means to remove signatures delimited by
316 `gnus-signature-separator'.  Any other symbol is used to look up a
317 regular expression to match the banner in `gnus-article-banner-alist'.
318 A string is used as a regular expression to match the banner
319 directly.")
320
321 (defcustom gnus-article-address-banner-alist nil
322   "Alist of mail addresses and banners.
323 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
324 to match a mail address in the From: header, BANNER is one of a symbol
325 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
326 If ADDRESS matches author's mail address, it will remove things like
327 advertisements.  For example:
328
329 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
330 "
331   :type '(repeat
332           (cons
333            (regexp :tag "Address")
334            (choice :tag "Banner" :value nil
335                    (const :tag "Remove signature" signature)
336                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
337                    regexp
338                    (const :tag "None" nil))))
339   :version "22.1"
340   :group 'gnus-article-washing)
341
342 (defmacro gnus-emphasis-custom-with-format (&rest body)
343   `(let ((format "\
344 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
345 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
346      ,@body))
347
348 (defun gnus-emphasis-custom-value-to-external (value)
349   (gnus-emphasis-custom-with-format
350    (if (consp (car value))
351        (list (format format (car (car value)) (cdr (car value)))
352              2
353              (if (nth 1 value) 2 3)
354              (nth 2 value))
355      value)))
356
357 (defun gnus-emphasis-custom-value-to-internal (value)
358   (gnus-emphasis-custom-with-format
359    (let ((regexp (concat "\\`"
360                          (format (regexp-quote format)
361                                  "\\([^()]+\\)" "\\([^()]+\\)")
362                          "\\'"))
363          pattern)
364      (if (string-match regexp (setq pattern (car value)))
365          (list (cons (match-string 1 pattern) (match-string 2 pattern))
366                (= (nth 2 value) 2)
367                (nth 3 value))
368        value))))
369
370 (defcustom gnus-emphasis-alist
371   (let ((types
372          '(("\\*" "\\*" bold nil 2)
373            ("_" "_" underline)
374            ("/" "/" italic)
375            ("_/" "/_" underline-italic)
376            ("_\\*" "\\*_" underline-bold)
377            ("\\*/" "/\\*" bold-italic)
378            ("_\\*/" "/\\*_" underline-bold-italic))))
379     (nconc
380      (gnus-emphasis-custom-with-format
381       (mapcar (lambda (spec)
382                 (list (format format (car spec) (cadr spec))
383                       (or (nth 3 spec) 2)
384                       (or (nth 4 spec) 3)
385                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
386               types))
387      '(;; I've never seen anyone use this strikethru convention whereas I've
388        ;; several times seen it triggered by normal text.  --Stef
389        ;; Miles suggests that this form is sometimes used but for italics,
390        ;; so maybe we should map it to `italic'.
391        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
392        ;; 2 3 gnus-emphasis-strikethru)
393        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
394         2 3 gnus-emphasis-underline))))
395   "*Alist that says how to fontify certain phrases.
396 Each item looks like this:
397
398   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
399
400 The first element is a regular expression to be matched.  The second
401 is a number that says what regular expression grouping used to find
402 the entire emphasized word.  The third is a number that says what
403 regexp grouping should be displayed and highlighted.  The fourth
404 is the face used for highlighting."
405   :type
406   '(repeat
407     (menu-choice
408      :format "%[Customizing Style%]\n%v"
409      :indent 2
410      (group :tag "Default"
411             :value ("" 0 0 default)
412             :value-create
413             (lambda (widget)
414               (let ((value (widget-get
415                             (cadr (widget-get (widget-get widget :parent)
416                                               :args))
417                             :value)))
418                 (if (not (eq (nth 2 value) 'default))
419                     (widget-put
420                      widget
421                      :value
422                      (gnus-emphasis-custom-value-to-external value))))
423               (widget-group-value-create widget))
424             regexp
425             (integer :format "Match group: %v")
426             (integer :format "Emphasize group: %v")
427             face)
428      (group :tag "Simple"
429             :value (("_" . "_") nil default)
430             (cons :format "%v"
431                   (regexp :format "Start regexp: %v")
432                   (regexp :format "End regexp: %v"))
433             (boolean :format "Show start and end patterns: %[%v%]\n"
434                      :on " On " :off " Off ")
435             face)))
436   :get (lambda (symbol)
437          (mapcar 'gnus-emphasis-custom-value-to-internal
438                  (default-value symbol)))
439   :set (lambda (symbol value)
440          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
441                                      value)))
442   :group 'gnus-article-emphasis)
443
444 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
445   "A regexp to describe whitespace which should not be emphasized.
446 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
447 The former avoids underlining of leading and trailing whitespace,
448 and the latter avoids underlining any whitespace at all."
449   :version "21.1"
450   :group 'gnus-article-emphasis
451   :type 'regexp)
452
453 (defface gnus-emphasis-bold '((t (:bold t)))
454   "Face used for displaying strong emphasized text (*word*)."
455   :group 'gnus-article-emphasis)
456
457 (defface gnus-emphasis-italic '((t (:italic t)))
458   "Face used for displaying italic emphasized text (/word/)."
459   :group 'gnus-article-emphasis)
460
461 (defface gnus-emphasis-underline '((t (:underline t)))
462   "Face used for displaying underlined emphasized text (_word_)."
463   :group 'gnus-article-emphasis)
464
465 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
466   "Face used for displaying underlined bold emphasized text (_*word*_)."
467   :group 'gnus-article-emphasis)
468
469 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
470   "Face used for displaying underlined italic emphasized text (_/word/_)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
474   "Face used for displaying bold italic emphasized text (/*word*/)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-underline-bold-italic
478   '((t (:bold t :italic t :underline t)))
479   "Face used for displaying underlined bold italic emphasized text.
480 Example: (_/*word*/_)."
481   :group 'gnus-article-emphasis)
482
483 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
484                                       '((t (:strikethru t)))
485                                     '((t (:strike-through t))))
486   "Face used for displaying strike-through text (-word-)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-highlight-words
490   '((t (:background "black" :foreground "yellow")))
491   "Face used for displaying highlighted words."
492   :group 'gnus-article-emphasis)
493
494 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
495   "Format for display of Date headers in article bodies.
496 See `format-time-string' for the possible values.
497
498 The variable can also be function, which should return a complete Date
499 header.  The function is called with one argument, the time, which can
500 be fed to `format-time-string'."
501   :type '(choice string function)
502   :link '(custom-manual "(gnus)Article Date")
503   :group 'gnus-article-washing)
504
505 (defcustom gnus-save-all-headers t
506   "*If non-nil, don't remove any headers before saving.
507 This will be overridden by the `:headers' property that the symbol of
508 the saver function, which is specified by `gnus-default-article-saver',
509 might have."
510   :group 'gnus-article-saving
511   :type 'boolean)
512
513 (defcustom gnus-prompt-before-saving 'always
514   "*This variable says how much prompting is to be done when saving articles.
515 If it is nil, no prompting will be done, and the articles will be
516 saved to the default files.  If this variable is `always', each and
517 every article that is saved will be preceded by a prompt, even when
518 saving large batches of articles.  If this variable is neither nil not
519 `always', there the user will be prompted once for a file name for
520 each invocation of the saving commands."
521   :group 'gnus-article-saving
522   :type '(choice (item always)
523                  (item :tag "never" nil)
524                  (sexp :tag "once" :format "%t\n" :value t)))
525
526 (defcustom gnus-saved-headers gnus-visible-headers
527   "Headers to keep if `gnus-save-all-headers' is nil.
528 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
529 If that variable is nil, however, all headers that match this regexp
530 will be kept while the rest will be deleted before saving.  This and
531 `gnus-save-all-headers' will be overridden by the `:headers' property
532 that the symbol of the saver function, which is specified by
533 `gnus-default-article-saver', might have."
534   :group 'gnus-article-saving
535   :type 'regexp)
536
537 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
538 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
539   "A function to save articles in your favorite format.
540 The function will be called by way of the `gnus-summary-save-article'
541 command, and friends such as `gnus-summary-save-article-rmail'.
542
543 Gnus provides the following functions:
544
545 * gnus-summary-save-in-rmail (Rmail format)
546 * gnus-summary-save-in-mail (Unix mail format)
547 * gnus-summary-save-in-folder (MH folder)
548 * gnus-summary-save-in-file (article format)
549 * gnus-summary-save-body-in-file (article body)
550 * gnus-summary-save-in-vm (use VM's folder format)
551 * gnus-summary-write-to-file (article format -- overwrite)
552 * gnus-summary-write-body-to-file (article body -- overwrite)
553 * gnus-summary-save-in-pipe (article format)
554
555 The symbol of each function may have the following properties:
556
557 * :decode
558 The value non-nil means save decoded articles.  This is meaningful
559 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
560 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
561 `gnus-summary-save-in-pipe'.
562
563 * :function
564 The value specifies an alternative function which appends, not
565 overwrites, articles to a file.  This implies that when saving many
566 articles at a time, `gnus-prompt-before-saving' is bound to t and all
567 articles are saved in a single file.  This is meaningful only with
568 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
569
570 * :headers
571 The value specifies the symbol of a variable of which the value
572 specifies headers to be saved.  If it is omitted,
573 `gnus-save-all-headers' and `gnus-saved-headers' control what
574 headers should be saved."
575   :group 'gnus-article-saving
576   :type '(radio (function-item gnus-summary-save-in-rmail)
577                 (function-item gnus-summary-save-in-mail)
578                 (function-item gnus-summary-save-in-folder)
579                 (function-item gnus-summary-save-in-file)
580                 (function-item gnus-summary-save-body-in-file)
581                 (function-item gnus-summary-save-in-vm)
582                 (function-item gnus-summary-write-to-file)
583                 (function-item gnus-summary-write-body-to-file)
584                 (function-item gnus-summary-save-in-pipe)
585                 (function)))
586
587 (defcustom gnus-article-save-coding-system
588   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
589       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
590       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
591       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
592   "Coding system used to save decoded articles to a file.
593
594 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
595 which can safely encode any characters in text.  This is used by the
596 commands including:
597
598 * gnus-summary-save-article-file
599 * gnus-summary-save-article-body-file
600 * gnus-summary-write-article-file
601 * gnus-summary-write-article-body-file
602
603 and the functions to which you may set `gnus-default-article-saver':
604
605 * gnus-summary-save-in-file
606 * gnus-summary-save-body-in-file
607 * gnus-summary-write-to-file
608 * gnus-summary-write-body-to-file
609
610 Those commands and functions save just text displayed in the article
611 buffer to a file if the value of this variable is non-nil.  Note that
612 buttonized MIME parts will be lost in a saved file in that case.
613 Otherwise, raw articles will be saved."
614   :group 'gnus-article-saving
615   :type `(choice
616           :format "%{%t%}:\n %[Value Menu%] %v"
617           (const :tag "Save raw articles" nil)
618           ,@(delq nil
619                   (mapcar
620                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
621                    '((const :tag "UTF-8" utf-8)
622                      (const :tag "iso-2022-7bit" iso-2022-7bit)
623                      (const :tag "Emacs internal" emacs-mule)
624                      (const :tag "escape-quoted" escape-quoted))))
625           (symbol :tag "Coding system")))
626
627 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
628   "A function generating a file name to save articles in Rmail format.
629 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
630   :group 'gnus-article-saving
631   :type 'function)
632
633 (defcustom gnus-mail-save-name 'gnus-plain-save-name
634   "A function generating a file name to save articles in Unix mail 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-folder-save-name 'gnus-folder-save-name
640   "A function generating a file name to save articles in MH folder.
641 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
642   :group 'gnus-article-saving
643   :type 'function)
644
645 (defcustom gnus-file-save-name 'gnus-numeric-save-name
646   "A function generating a file name to save articles in article format.
647 The function is called with NEWSGROUP, HEADERS, and optional
648 LAST-FILE."
649   :group 'gnus-article-saving
650   :type 'function)
651
652 (defcustom gnus-split-methods
653   '((gnus-article-archive-name)
654     (gnus-article-nndoc-name))
655   "*Variable used to suggest where articles are to be saved.
656 For instance, if you would like to save articles related to Gnus in
657 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
658 you could set this variable to something like:
659
660  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
661    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
662
663 This variable is an alist where the key is the match and the
664 value is a list of possible files to save in if the match is
665 non-nil.
666
667 If the match is a string, it is used as a regexp match on the
668 article.  If the match is a symbol, that symbol will be funcalled
669 from the buffer of the article to be saved with the newsgroup as the
670 parameter.  If it is a list, it will be evalled in the same buffer.
671
672 If this form or function returns a string, this string will be used as a
673 possible file name; and if it returns a non-nil list, that list will be
674 used as possible file names."
675   :group 'gnus-article-saving
676   :type '(repeat (choice (list :value (fun) function)
677                          (cons :value ("" "") regexp (repeat string))
678                          (sexp :value nil))))
679
680 (defcustom gnus-page-delimiter "^\^L"
681   "*Regexp describing what to use as article page delimiters.
682 The default value is \"^\^L\", which is a form linefeed at the
683 beginning of a line."
684   :type 'regexp
685   :group 'gnus-article-various)
686
687 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
688   "*The format specification for the article mode line.
689 See `gnus-summary-mode-line-format' for a closer description.
690
691 The following additional specs are available:
692
693 %w  The article washing status.
694 %m  The number of MIME parts in the article."
695   :version "24.1"
696   :type 'string
697   :group 'gnus-article-various)
698
699 (defcustom gnus-article-mode-hook nil
700   "*A hook for Gnus article mode."
701   :type 'hook
702   :group 'gnus-article-various)
703
704 (when (featurep 'xemacs)
705   ;; Extracted from gnus-xmas-define in order to preserve user settings
706   (when (fboundp 'turn-off-scroll-in-place)
707     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
708   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
709   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
710
711 (defcustom gnus-article-menu-hook nil
712   "*Hook run after the creation of the article mode menu."
713   :type 'hook
714   :group 'gnus-article-various)
715
716 (defcustom gnus-article-prepare-hook nil
717   "*A hook called after an article has been prepared in the article buffer."
718   :type 'hook
719   :group 'gnus-article-various)
720
721 (defcustom gnus-copy-article-ignored-headers nil
722   "List of headers to be removed when copying an article.
723 Each element is a regular expression."
724   :version "23.1" ;; No Gnus
725   :type '(repeat regexp)
726   :group 'gnus-article-various)
727
728 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
729                         "Gnus 5.10 (Emacs 22.1)")
730
731 (defface gnus-button
732   '((t (:weight bold)))
733   "Face used for highlighting a button in the article buffer."
734   :group 'gnus-article-buttons)
735
736 (defcustom gnus-article-button-face 'gnus-button
737   "Face used for highlighting buttons in the article buffer.
738
739 An article button is a piece of text that you can activate by pressing
740 `RET' or `mouse-2' above it."
741   :type 'face
742   :group 'gnus-article-buttons)
743
744 (defcustom gnus-article-mouse-face 'highlight
745   "Face used for mouse highlighting in the article buffer.
746
747 Article buttons will be displayed in this face when the cursor is
748 above them."
749   :type 'face
750   :group 'gnus-article-buttons)
751
752 (defcustom gnus-signature-face 'gnus-signature
753   "Face used for highlighting a signature in the article buffer.
754 Obsolete; use the face `gnus-signature' for customizations instead."
755   :type 'face
756   :group 'gnus-article-highlight
757   :group 'gnus-article-signature)
758
759 (defface gnus-signature
760   '((t
761      (:italic t)))
762   "Face used for highlighting a signature in the article buffer."
763   :group 'gnus-article-highlight
764   :group 'gnus-article-signature)
765 ;; backward-compatibility alias
766 (put 'gnus-signature-face 'face-alias 'gnus-signature)
767 (put 'gnus-signature-face 'obsolete-face "22.1")
768
769 (defface gnus-header-from
770   '((((class color)
771       (background dark))
772      (:foreground "PaleGreen1"))
773     (((class color)
774       (background light))
775      (:foreground "red3"))
776     (t
777      (:italic t)))
778   "Face used for displaying from headers."
779   :group 'gnus-article-headers
780   :group 'gnus-article-highlight)
781 ;; backward-compatibility alias
782 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
783 (put 'gnus-header-from-face 'obsolete-face "22.1")
784
785 (defface gnus-header-subject
786   '((((class color)
787       (background dark))
788      (:foreground "SeaGreen1"))
789     (((class color)
790       (background light))
791      (:foreground "red4"))
792     (t
793      (:bold t :italic t)))
794   "Face used for displaying subject headers."
795   :group 'gnus-article-headers
796   :group 'gnus-article-highlight)
797 ;; backward-compatibility alias
798 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
799 (put 'gnus-header-subject-face 'obsolete-face "22.1")
800
801 (defface gnus-header-newsgroups
802   '((((class color)
803       (background dark))
804      (:foreground "yellow" :italic t))
805     (((class color)
806       (background light))
807      (:foreground "MidnightBlue" :italic t))
808     (t
809      (:italic t)))
810   "Face used for displaying newsgroups headers.
811 In the default setup this face is only used for crossposted
812 articles."
813   :group 'gnus-article-headers
814   :group 'gnus-article-highlight)
815 ;; backward-compatibility alias
816 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
817 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
818
819 (defface gnus-header-name
820   '((((class color)
821       (background dark))
822      (:foreground "SpringGreen2"))
823     (((class color)
824       (background light))
825      (:foreground "maroon"))
826     (t
827      (:bold t)))
828   "Face used for displaying header names."
829   :group 'gnus-article-headers
830   :group 'gnus-article-highlight)
831 ;; backward-compatibility alias
832 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
833 (put 'gnus-header-name-face 'obsolete-face "22.1")
834
835 (defface gnus-header-content
836   '((((class color)
837       (background dark))
838      (:foreground "SpringGreen1" :italic t))
839     (((class color)
840       (background light))
841      (:foreground "indianred4" :italic t))
842     (t
843      (:italic t)))  "Face used for displaying header content."
844   :group 'gnus-article-headers
845   :group 'gnus-article-highlight)
846 ;; backward-compatibility alias
847 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
848 (put 'gnus-header-content-face 'obsolete-face "22.1")
849
850 (defcustom gnus-header-face-alist
851   '(("From" nil gnus-header-from)
852     ("Subject" nil gnus-header-subject)
853     ("Newsgroups:.*," nil gnus-header-newsgroups)
854     ("" gnus-header-name gnus-header-content))
855   "*Controls highlighting of article headers.
856
857 An alist of the form (HEADER NAME CONTENT).
858
859 HEADER is a regular expression which should match the name of a
860 header and NAME and CONTENT are either face names or nil.
861
862 The name of each header field will be displayed using the face
863 specified by the first element in the list where HEADER matches
864 the header name and NAME is non-nil.  Similarly, the content will
865 be displayed by the first non-nil matching CONTENT face."
866   :group 'gnus-article-headers
867   :group 'gnus-article-highlight
868   :type '(repeat (list (regexp :tag "Header")
869                        (choice :tag "Name"
870                                (item :tag "skip" nil)
871                                (face :value default))
872                        (choice :tag "Content"
873                                (item :tag "skip" nil)
874                                (face :value default)))))
875
876 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
877                                           '((xface . (:face gnus-x-face)))
878                                         '((pbm . (:face gnus-x-face))
879                                           (png . nil)))
880   "Alist of image types and properties applied to Face and X-Face images.
881 Here are examples:
882
883 ;; Specify the altitude of Face images in the From header.
884 \(setq gnus-face-properties-alist
885       '((pbm . (:face gnus-x-face :ascent 80))
886         (png . (:ascent 80))))
887
888 ;; Show Face images as pressed buttons.
889 \(setq gnus-face-properties-alist
890       '((pbm . (:face gnus-x-face :relief -2))
891         (png . (:relief -2))))
892
893 See the manual for the valid properties for various image types.
894 Currently, `pbm' is used for X-Face images and `png' is used for Face
895 images in Emacs.  Only the `:face' property is effective on the `xface'
896 image type in XEmacs if it is built with the libcompface library."
897   :version "23.1" ;; No Gnus
898   :group 'gnus-article-headers
899   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
900
901 (defcustom gnus-article-decode-hook
902   '(article-decode-charset article-decode-encoded-words
903                            article-decode-group-name article-decode-idna-rhs)
904   "*Hook run to decode charsets in articles."
905   :group 'gnus-article-headers
906   :type 'hook)
907
908 (defcustom gnus-display-mime-function 'gnus-display-mime
909   "Function to display MIME articles."
910   :group 'gnus-article-mime
911   :type 'function)
912
913 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
914   "Function used to decode headers.")
915
916 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
917   "Function used to decode addresses.")
918
919 (defvar gnus-article-dumbquotes-map
920   '((?\200 "EUR")
921     (?\202 ",")
922     (?\203 "f")
923     (?\204 ",,")
924     (?\205 "...")
925     (?\213 "<")
926     (?\214 "OE")
927     (?\221 "`")
928     (?\222 "'")
929     (?\223 "``")
930     (?\224 "\"")
931     (?\225 "*")
932     (?\226 "-")
933     (?\227 "--")
934     (?\230 "~")
935     (?\231 "(TM)")
936     (?\233 ">")
937     (?\234 "oe")
938     (?\264 "'"))
939   "Table for MS-to-Latin1 translation.")
940
941 (defcustom gnus-ignored-mime-types nil
942   "List of MIME types that should be ignored by Gnus."
943   :version "21.1"
944   :group 'gnus-article-mime
945   :type '(repeat regexp))
946
947 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
948   "List of MIME types that should not be given buttons when rendered inline.
949 See also `gnus-buttonized-mime-types' which may override this variable.
950 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
951   :version "21.1"
952   :group 'gnus-article-mime
953   :type '(repeat regexp))
954
955 (defcustom gnus-buttonized-mime-types nil
956   "List of MIME types that should be given buttons when rendered inline.
957 If set, this variable overrides `gnus-unbuttonized-mime-types'.
958 To see e.g. security buttons you could set this to
959 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
960 this list to display radio buttons that allow you to choose one of two
961 media types those mails include.  See also `mm-discouraged-alternatives'.
962 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
963   :version "22.1"
964   :group 'gnus-article-mime
965   :type '(repeat regexp))
966
967 (defcustom gnus-inhibit-mime-unbuttonizing nil
968   "If non-nil, all MIME parts get buttons.
969 When nil (the default value), then some MIME parts do not get buttons,
970 as described by the variables `gnus-buttonized-mime-types' and
971 `gnus-unbuttonized-mime-types'."
972   :version "22.1"
973   :group 'gnus-article-mime
974   :type 'boolean)
975
976 (defcustom gnus-body-boundary-delimiter "_"
977   "String used to delimit header and body.
978 This variable is used by `gnus-article-treat-body-boundary' which can
979 be controlled by `gnus-treat-body-boundary'."
980   :version "22.1"
981   :group 'gnus-article-various
982   :type '(choice (item :tag "None" :value nil)
983                  string))
984
985 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
986                                   "/usr/share/picons")
987   "Defines the location of the faces database.
988 For information on obtaining this database of pretty pictures, please
989 see http://www.cs.indiana.edu/picons/ftp/index.html"
990   :version "22.1"
991   :type '(repeat directory)
992   :link '(url-link :tag "download"
993                    "http://www.cs.indiana.edu/picons/ftp/index.html")
994   :link '(custom-manual "(gnus)Picons")
995   :group 'gnus-picon)
996
997 (defun gnus-picons-installed-p ()
998   "Say whether picons are installed on your machine."
999   (let ((installed nil))
1000     (dolist (database gnus-picon-databases)
1001       (when (file-exists-p database)
1002         (setq installed t)))
1003     installed))
1004
1005 (defcustom gnus-article-mime-part-function nil
1006   "Function called with a MIME handle as the argument.
1007 This is meant for people who want to do something automatic based
1008 on parts -- for instance, adding Vcard info to a database."
1009   :group 'gnus-article-mime
1010   :type '(choice (const nil)
1011                  function))
1012
1013 (defcustom gnus-mime-multipart-functions nil
1014   "An alist of MIME types to functions to display them."
1015   :version "21.1"
1016   :group 'gnus-article-mime
1017   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1018
1019 (defcustom gnus-article-date-headers '(combined-lapsed)
1020   "A list of Date header formats to display.
1021 Valid formats are `ut' (universal time), `local' (local time
1022 zone), `english' (readable English), `lapsed' (elapsed time),
1023 `combined-lapsed' (both the original date and the elapsed time),
1024 `original' (the original date header), `iso8601' (ISO8601
1025 format), and `user-defined' (a user-defined format defined by the
1026 `gnus-article-time-format' variable).
1027
1028 You have as many date headers as you want in the article buffer.
1029 Some of these headers are updated automatically.  See
1030 `gnus-article-update-date-headers' for details."
1031   :version "24.1"
1032   :group 'gnus-article-headers
1033   :type '(set
1034           (const :tag "Universal time (UT)" ut)
1035           (const :tag "Local time zone" local)
1036           (const :tag "Readable English" english)
1037           (const :tag "Elapsed time" lapsed)
1038           (const :tag "Original and elapsed time" combined-lapsed)
1039           (const :tag "Original date header" original)
1040           (const :tag "ISO8601 format" iso8601)
1041           (const :tag "User-defined" user-defined)))
1042
1043 (defcustom gnus-article-update-date-headers nil
1044   "A number that says how often to update the date header (in seconds).
1045 If nil, don't update it at all."
1046   :version "24.1"
1047   :group 'gnus-article-headers
1048   :type '(choice
1049           (item :tag "Don't update" :value nil)
1050           integer))
1051
1052 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1053   "Function called with a MIME handle as the argument.
1054 This is meant for people who want to view first matched part.
1055 For `undisplayed-alternative' (default), the first undisplayed
1056 part or alternative part is used.  For `undisplayed', the first
1057 undisplayed part is used.  For a function, the first part which
1058 the function return t is used.  For nil, the first part is
1059 used."
1060   :version "21.1"
1061   :group 'gnus-article-mime
1062   :type '(choice
1063           (item :tag "first" :value nil)
1064           (item :tag "undisplayed" :value undisplayed)
1065           (item :tag "undisplayed or alternative"
1066                 :value undisplayed-alternative)
1067           (function)))
1068
1069 (defcustom gnus-mime-action-alist
1070   '(("save to file" . gnus-mime-save-part)
1071     ("save and strip" . gnus-mime-save-part-and-strip)
1072     ("replace with file" . gnus-mime-replace-part)
1073     ("delete part" . gnus-mime-delete-part)
1074     ("display as text" . gnus-mime-inline-part)
1075     ("view the part" . gnus-mime-view-part)
1076     ("pipe to command" . gnus-mime-pipe-part)
1077     ("toggle display" . gnus-article-press-button)
1078     ("toggle display" . gnus-article-view-part-as-charset)
1079     ("view as type" . gnus-mime-view-part-as-type)
1080     ("view internally" . gnus-mime-view-part-internally)
1081     ("view externally" . gnus-mime-view-part-externally))
1082   "An alist of actions that run on the MIME attachment."
1083   :group 'gnus-article-mime
1084   :type '(repeat (cons (string :tag "name")
1085                        (function))))
1086
1087 (defcustom gnus-auto-select-part 1
1088   "Advance to next MIME part when deleting or stripping parts.
1089
1090 When 0, point will be placed on the same part as before.  When
1091 positive (negative), move point forward (backwards) this many
1092 parts.  When nil, redisplay article."
1093   :version "23.1" ;; No Gnus
1094   :group 'gnus-article-mime
1095   :type '(choice (const nil :tag "Redisplay article.")
1096                  (const 1 :tag "Next part.")
1097                  (const 0 :tag "Current part.")
1098                  integer))
1099
1100 ;;;
1101 ;;; The treatment variables
1102 ;;;
1103
1104 (defvar gnus-part-display-hook nil
1105   "Hook called on parts that are to receive treatment.")
1106
1107 (defvar gnus-article-treat-custom
1108   '(choice (const :tag "Off" nil)
1109            (const :tag "On" t)
1110            (const :tag "Header" head)
1111            (const :tag "First" first)
1112            (const :tag "Last" last)
1113            (integer :tag "Less")
1114            (repeat :tag "Groups" regexp)
1115            (sexp :tag "Predicate")))
1116
1117 (defvar gnus-article-treat-head-custom
1118   '(choice (const :tag "Off" nil)
1119            (const :tag "Header" head)))
1120
1121 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1122                                    "text/x-patch" "text/html")
1123   "Part types eligible for treatment.")
1124
1125 (defvar gnus-inhibit-treatment nil
1126   "Whether to inhibit treatment.")
1127
1128 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1129   "Highlight the signature.
1130 Valid values are nil, t, `head', `first', `last', an integer or a
1131 predicate.  See Info node `(gnus)Customizing Articles'."
1132   :group 'gnus-article-treat
1133   :link '(custom-manual "(gnus)Customizing Articles")
1134   :type gnus-article-treat-custom)
1135 (put 'gnus-treat-highlight-signature 'highlight t)
1136
1137 (defcustom gnus-treat-buttonize 100000
1138   "Add buttons.
1139 Valid values are nil, t, `head', `first', `last', an integer or a
1140 predicate.  See Info node `(gnus)Customizing Articles'."
1141   :group 'gnus-article-treat
1142   :link '(custom-manual "(gnus)Customizing Articles")
1143   :type gnus-article-treat-custom)
1144 (put 'gnus-treat-buttonize 'highlight t)
1145
1146 (defcustom gnus-treat-buttonize-head 'head
1147   "Add buttons to the head.
1148 Valid values are nil, t, `head', `first', `last', an integer or a
1149 predicate.  See Info node `(gnus)Customizing Articles'."
1150   :group 'gnus-article-treat
1151   :link '(custom-manual "(gnus)Customizing Articles")
1152   :type gnus-article-treat-head-custom)
1153 (put 'gnus-treat-buttonize-head 'highlight t)
1154
1155 (defcustom gnus-treat-date 'head
1156   "Display dates according to the `gnus-article-date-headers' variable.
1157 Valid values are nil, t, `head', `first', `last', an integer or a
1158 predicate.  See Info node `(gnus)Customizing Articles'."
1159   :version "24.1"
1160   :group 'gnus-article-treat
1161   :link '(custom-manual "(gnus)Customizing Articles")
1162   :type gnus-article-treat-head-custom)
1163
1164 (defcustom gnus-treat-emphasize 50000
1165   "Emphasize text.
1166 Valid values are nil, t, `head', `first', `last', an integer or a
1167 predicate.  See Info node `(gnus)Customizing Articles'."
1168   :group 'gnus-article-treat
1169   :link '(custom-manual "(gnus)Customizing Articles")
1170   :type gnus-article-treat-custom)
1171 (put 'gnus-treat-emphasize 'highlight t)
1172
1173 (defcustom gnus-treat-strip-cr nil
1174   "Remove carriage returns.
1175 Valid values are nil, t, `head', `first', `last', an integer or a
1176 predicate.  See Info node `(gnus)Customizing Articles'."
1177   :version "22.1"
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-custom)
1181
1182 (defcustom gnus-treat-unsplit-urls nil
1183   "Remove newlines from within URLs.
1184 Valid values are nil, t, `head', `first', `last', an integer or a
1185 predicate.  See Info node `(gnus)Customizing Articles'."
1186   :version "22.1"
1187   :group 'gnus-article-treat
1188   :link '(custom-manual "(gnus)Customizing Articles")
1189   :type gnus-article-treat-custom)
1190
1191 (defcustom gnus-treat-leading-whitespace nil
1192   "Remove leading whitespace in headers.
1193 Valid values are nil, t, `head', `first', `last', an integer or a
1194 predicate.  See Info node `(gnus)Customizing Articles'."
1195   :version "22.1"
1196   :group 'gnus-article-treat
1197   :link '(custom-manual "(gnus)Customizing Articles")
1198   :type gnus-article-treat-custom)
1199
1200 (defcustom gnus-treat-hide-headers 'head
1201   "Hide headers.
1202 Valid values are nil, t, `head', `first', `last', an integer or a
1203 predicate.  See Info node `(gnus)Customizing Articles'."
1204   :group 'gnus-article-treat
1205   :link '(custom-manual "(gnus)Customizing Articles")
1206   :type gnus-article-treat-head-custom)
1207
1208 (defcustom gnus-treat-hide-boring-headers nil
1209   "Hide boring headers.
1210 Valid values are nil, t, `head', `first', `last', an integer or a
1211 predicate.  See Info node `(gnus)Customizing Articles'."
1212   :group 'gnus-article-treat
1213   :link '(custom-manual "(gnus)Customizing Articles")
1214   :type gnus-article-treat-head-custom)
1215
1216 (defcustom gnus-treat-hide-signature nil
1217   "Hide the signature.
1218 Valid values are nil, t, `head', `first', `last', an integer or a
1219 predicate.  See Info node `(gnus)Customizing Articles'."
1220   :group 'gnus-article-treat
1221   :link '(custom-manual "(gnus)Customizing Articles")
1222   :type gnus-article-treat-custom)
1223
1224 (defcustom gnus-treat-fill-article nil
1225   "Fill the article.
1226 Valid values are nil, t, `head', `first', `last', an integer or a
1227 predicate.  See Info node `(gnus)Customizing Articles'."
1228   :group 'gnus-article-treat
1229   :link '(custom-manual "(gnus)Customizing Articles")
1230   :type gnus-article-treat-custom)
1231
1232 (defcustom gnus-treat-hide-citation nil
1233   "Hide cited text.
1234 Valid values are nil, t, `head', `first', `last', an integer or a
1235 predicate.  See Info node `(gnus)Customizing Articles'.
1236
1237 See `gnus-article-highlight-citation' for variables used to
1238 control what it hides."
1239   :group 'gnus-article-treat
1240   :link '(custom-manual "(gnus)Customizing Articles")
1241   :type gnus-article-treat-custom)
1242
1243 (defcustom gnus-treat-hide-citation-maybe nil
1244   "Hide cited text according to certain conditions.
1245 Valid values are nil, t, `head', `first', `last', an integer or a
1246 predicate.  See Info node `(gnus)Customizing Articles'.
1247
1248 See `gnus-cite-hide-percentage' and `gnus-cite-hide-absolute' for
1249 how to control what it hides."
1250   :group 'gnus-article-treat
1251   :link '(custom-manual "(gnus)Customizing Articles")
1252   :type gnus-article-treat-custom)
1253
1254 (defcustom gnus-treat-strip-list-identifiers 'head
1255   "Strip list identifiers from `gnus-list-identifiers`.
1256 Valid values are nil, t, `head', `first', `last', an integer or a
1257 predicate.  See Info node `(gnus)Customizing Articles'."
1258   :version "21.1"
1259   :group 'gnus-article-treat
1260   :link '(custom-manual "(gnus)Customizing Articles")
1261   :type gnus-article-treat-custom)
1262
1263 (gnus-define-group-parameter
1264  list-identifier
1265  :variable-document
1266  "Alist of regexps and correspondent identifiers."
1267  :variable-group gnus-article-washing
1268  :parameter-type
1269  '(choice :tag "Identifier"
1270           :value nil
1271           (symbol :tag "Item in `gnus-list-identifiers'" none)
1272           regexp
1273           (const :tag "None" nil))
1274  :parameter-document
1275  "If non-nil, specify how to remove `identifiers' from articles' subject.
1276
1277 Any symbol is used to look up a regular expression to match the
1278 banner in `gnus-list-identifiers'.  A string is used as a regular
1279 expression to match the identifier directly.")
1280
1281 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1282                         "Gnus 5.10 (Emacs 22.1)")
1283
1284 (defcustom gnus-treat-strip-pem nil
1285   "Strip PEM signatures.
1286 Valid values are nil, t, `head', `first', `last', an integer or a
1287 predicate.  See Info node `(gnus)Customizing Articles'."
1288   :group 'gnus-article-treat
1289   :link '(custom-manual "(gnus)Customizing Articles")
1290   :type gnus-article-treat-custom)
1291
1292 (defcustom gnus-treat-strip-banner t
1293   "Strip banners from articles.
1294 The banner to be stripped is specified in the `banner' group parameter.
1295 Valid values are nil, t, `head', `first', `last', an integer or a
1296 predicate.  See Info node `(gnus)Customizing Articles'."
1297   :group 'gnus-article-treat
1298   :link '(custom-manual "(gnus)Customizing Articles")
1299   :type gnus-article-treat-custom)
1300
1301 (defcustom gnus-treat-highlight-headers 'head
1302   "Highlight the headers.
1303 Valid values are nil, t, `head', `first', `last', an integer or a
1304 predicate.  See Info node `(gnus)Customizing Articles'."
1305   :group 'gnus-article-treat
1306   :link '(custom-manual "(gnus)Customizing Articles")
1307   :type gnus-article-treat-head-custom)
1308 (put 'gnus-treat-highlight-headers 'highlight t)
1309
1310 (defcustom gnus-treat-highlight-citation t
1311   "Highlight cited text.
1312 Valid values are nil, t, `head', `first', `last', an integer or a
1313 predicate.  See Info node `(gnus)Customizing Articles'."
1314   :group 'gnus-article-treat
1315   :link '(custom-manual "(gnus)Customizing Articles")
1316   :type gnus-article-treat-custom)
1317 (put 'gnus-treat-highlight-citation 'highlight t)
1318
1319 (defcustom gnus-treat-strip-headers-in-body t
1320   "Strip the X-No-Archive header line from the beginning of the body.
1321 Valid values are nil, t, `head', `first', `last', an integer or a
1322 predicate.  See Info node `(gnus)Customizing Articles'."
1323   :version "21.1"
1324   :group 'gnus-article-treat
1325   :link '(custom-manual "(gnus)Customizing Articles")
1326   :type gnus-article-treat-custom)
1327
1328 (defcustom gnus-treat-strip-trailing-blank-lines nil
1329   "Strip trailing blank lines.
1330 Valid values are nil, t, `head', `first', `last', an integer or a
1331 predicate.  See Info node `(gnus)Customizing Articles'.
1332
1333 When set to t, it also strips trailing blanks in all MIME parts.
1334 Consider to use `last' instead."
1335   :group 'gnus-article-treat
1336   :link '(custom-manual "(gnus)Customizing Articles")
1337   :type gnus-article-treat-custom)
1338
1339 (defcustom gnus-treat-strip-leading-blank-lines nil
1340   "Strip leading blank lines.
1341 Valid values are nil, t, `head', `first', `last', an integer or a
1342 predicate.  See Info node `(gnus)Customizing Articles'.
1343
1344 When set to t, it also strips trailing blanks in all MIME parts."
1345   :group 'gnus-article-treat
1346   :link '(custom-manual "(gnus)Customizing Articles")
1347   :type gnus-article-treat-custom)
1348
1349 (defcustom gnus-treat-strip-multiple-blank-lines nil
1350   "Strip multiple blank lines.
1351 Valid values are nil, t, `head', `first', `last', an integer or a
1352 predicate.  See Info node `(gnus)Customizing Articles'."
1353   :group 'gnus-article-treat
1354   :link '(custom-manual "(gnus)Customizing Articles")
1355   :type gnus-article-treat-custom)
1356
1357 (defcustom gnus-treat-unfold-headers 'head
1358   "Unfold folded header lines.
1359 Valid values are nil, t, `head', `first', `last', an integer or a
1360 predicate.  See Info node `(gnus)Customizing Articles'."
1361   :version "22.1"
1362   :group 'gnus-article-treat
1363   :link '(custom-manual "(gnus)Customizing Articles")
1364   :type gnus-article-treat-custom)
1365
1366 (defcustom gnus-article-unfold-long-headers nil
1367   "If non-nil, allow unfolding headers even if the header is long.
1368 If it is a regexp, only long headers matching this regexp are unfolded.
1369 If it is t, all long headers are unfolded.
1370
1371 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1372   :version "23.1" ;; No Gnus
1373   :group 'gnus-article-treat
1374   :type '(choice (const nil)
1375                  (const :tag "all" t)
1376                  (regexp)))
1377
1378 (defcustom gnus-treat-fold-headers nil
1379   "Fold headers.
1380 Valid values are nil, t, `head', `first', `last', an integer or a
1381 predicate.  See Info node `(gnus)Customizing Articles'."
1382   :version "22.1"
1383   :group 'gnus-article-treat
1384   :link '(custom-manual "(gnus)Customizing Articles")
1385   :type gnus-article-treat-custom)
1386
1387 (defcustom gnus-treat-fold-newsgroups 'head
1388   "Fold the Newsgroups and Followup-To headers.
1389 Valid values are nil, t, `head', `first', `last', an integer or a
1390 predicate.  See Info node `(gnus)Customizing Articles'."
1391   :version "22.1"
1392   :group 'gnus-article-treat
1393   :link '(custom-manual "(gnus)Customizing Articles")
1394   :type gnus-article-treat-custom)
1395
1396 (defcustom gnus-treat-overstrike t
1397   "Treat overstrike highlighting.
1398 Valid values are nil, t, `head', `first', `last', an integer or a
1399 predicate.  See Info node `(gnus)Customizing Articles'."
1400   :group 'gnus-article-treat
1401   :link '(custom-manual "(gnus)Customizing Articles")
1402   :type gnus-article-treat-custom)
1403 (put 'gnus-treat-overstrike 'highlight t)
1404
1405 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1406   "Treat ANSI SGR control sequences.
1407 Valid values are nil, t, `head', `first', `last', an integer or a
1408 predicate.  See Info node `(gnus)Customizing Articles'."
1409   :group 'gnus-article-treat
1410   :link '(custom-manual "(gnus)Customizing Articles")
1411   :type gnus-article-treat-custom)
1412
1413 (make-obsolete-variable 'gnus-treat-display-xface
1414                         'gnus-treat-display-x-face "Emacs 22.1")
1415
1416 (defcustom gnus-treat-display-x-face
1417   (and (not noninteractive)
1418        (gnus-image-type-available-p 'xbm)
1419        (if (featurep 'xemacs)
1420            (featurep 'xface)
1421          (condition-case nil
1422              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1423                   (executable-find "icontopbm"))
1424            ;; shell-command-to-string may signal an error, e.g. if
1425            ;; shell-file-name is not found.
1426            (error nil)))
1427        'head)
1428   "Display X-Face headers.
1429 Valid values are nil and `head'.
1430 See Info node `(gnus)Customizing Articles' and Info node
1431 `(gnus)X-Face' for details."
1432   :group 'gnus-article-treat
1433   :version "21.1"
1434   :link '(custom-manual "(gnus)Customizing Articles")
1435   :link '(custom-manual "(gnus)X-Face")
1436   :type gnus-article-treat-head-custom
1437   :set (lambda (symbol value)
1438          (set-default
1439           symbol
1440           (cond ((or (boundp symbol) (get symbol 'saved-value))
1441                  value)
1442                 ((boundp 'gnus-treat-display-xface)
1443                  (message "\
1444 ** gnus-treat-display-xface is an obsolete variable;\
1445  use gnus-treat-display-x-face instead")
1446                  (default-value 'gnus-treat-display-xface))
1447                 ((get 'gnus-treat-display-xface 'saved-value)
1448                  (message "\
1449 ** gnus-treat-display-xface is an obsolete variable;\
1450  use gnus-treat-display-x-face instead")
1451                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1452                 (t
1453                  value)))))
1454 (put 'gnus-treat-display-x-face 'highlight t)
1455
1456 (defcustom gnus-treat-display-face
1457   (and (not noninteractive)
1458        (gnus-image-type-available-p 'png)
1459        'head)
1460   "Display Face headers.
1461 Valid values are nil, t, `head', `first', `last', an integer or a
1462 predicate.  See Info node `(gnus)Customizing Articles' and Info
1463 node `(gnus)Face' for details."
1464   :group 'gnus-article-treat
1465   :version "22.1"
1466   :link '(custom-manual "(gnus)Customizing Articles")
1467   :link '(custom-manual "(gnus)X-Face")
1468   :type gnus-article-treat-head-custom)
1469 (put 'gnus-treat-display-face 'highlight t)
1470
1471 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1472   "Display smileys.
1473 Valid values are nil, t, `head', `first', `last', an integer or a
1474 predicate.  See Info node `(gnus)Customizing Articles' and Info
1475 node `(gnus)Smileys' for details."
1476   :group 'gnus-article-treat
1477   :version "21.1"
1478   :link '(custom-manual "(gnus)Customizing Articles")
1479   :link '(custom-manual "(gnus)Smileys")
1480   :type gnus-article-treat-custom)
1481 (put 'gnus-treat-display-smileys 'highlight t)
1482
1483 (defcustom gnus-treat-from-picon
1484   (if (and (gnus-image-type-available-p 'xpm)
1485            (gnus-picons-installed-p))
1486       'head nil)
1487   "Display picons in the From header.
1488 Valid values are nil, t, `head', `first', `last', an integer or a
1489 predicate.  See Info node `(gnus)Customizing Articles' and Info
1490 node `(gnus)Picons' for details."
1491   :version "22.1"
1492   :group 'gnus-article-treat
1493   :group 'gnus-picon
1494   :link '(custom-manual "(gnus)Customizing Articles")
1495   :link '(custom-manual "(gnus)Picons")
1496   :type gnus-article-treat-head-custom)
1497 (put 'gnus-treat-from-picon 'highlight t)
1498
1499 (defcustom gnus-treat-mail-picon
1500   (if (and (gnus-image-type-available-p 'xpm)
1501            (gnus-picons-installed-p))
1502       'head nil)
1503   "Display picons in To and Cc headers.
1504 Valid values are nil, t, `head', `first', `last', an integer or a
1505 predicate.  See Info node `(gnus)Customizing Articles' and Info
1506 node `(gnus)Picons' for details."
1507   :version "22.1"
1508   :group 'gnus-article-treat
1509   :group 'gnus-picon
1510   :link '(custom-manual "(gnus)Customizing Articles")
1511   :link '(custom-manual "(gnus)Picons")
1512   :type gnus-article-treat-head-custom)
1513 (put 'gnus-treat-mail-picon 'highlight t)
1514
1515 (defcustom gnus-treat-newsgroups-picon
1516   (if (and (gnus-image-type-available-p 'xpm)
1517            (gnus-picons-installed-p))
1518       'head nil)
1519   "Display picons in the Newsgroups and Followup-To headers.
1520 Valid values are nil, t, `head', `first', `last', an integer or a
1521 predicate.  See Info node `(gnus)Customizing Articles' and Info
1522 node `(gnus)Picons' for details."
1523   :version "22.1"
1524   :group 'gnus-article-treat
1525   :group 'gnus-picon
1526   :link '(custom-manual "(gnus)Customizing Articles")
1527   :link '(custom-manual "(gnus)Picons")
1528   :type gnus-article-treat-head-custom)
1529 (put 'gnus-treat-newsgroups-picon 'highlight t)
1530
1531 (defcustom gnus-treat-from-gravatar nil
1532   "Display gravatars in the From header.
1533 Valid values are nil, t, `head', `first', `last', an integer or a
1534 predicate.  See Info node `(gnus)Customizing Articles' and Info
1535 node `(gnus)Gravatars' for details."
1536   :version "24.1"
1537   :group 'gnus-article-treat
1538   :group 'gnus-gravatar
1539   :link '(custom-manual "(gnus)Customizing Articles")
1540   :link '(custom-manual "(gnus)Gravatars")
1541   :type gnus-article-treat-head-custom)
1542 (put 'gnus-treat-from-gravatar 'highlight t)
1543
1544 (defcustom gnus-treat-mail-gravatar nil
1545   "Display gravatars in To and Cc headers.
1546 Valid values are nil, t, `head', `first', `last', an integer or a
1547 predicate.  See Info node `(gnus)Customizing Articles' and Info
1548 node `(gnus)Gravatars' for details."
1549   :version "24.1"
1550   :group 'gnus-article-treat
1551   :group 'gnus-gravatar
1552   :link '(custom-manual "(gnus)Customizing Articles")
1553   :link '(custom-manual "(gnus)Gravatars")
1554   :type gnus-article-treat-head-custom)
1555 (put 'gnus-treat-mail-gravatar 'highlight t)
1556
1557 (defcustom gnus-treat-body-boundary
1558   (if (or gnus-treat-newsgroups-picon
1559           gnus-treat-mail-picon
1560           gnus-treat-from-picon
1561           gnus-treat-from-gravatar
1562           gnus-treat-mail-gravatar)
1563       ;; If there's much decoration, the user might prefer a boundary.
1564       'head
1565     nil)
1566   "Draw a boundary at the end of the headers.
1567 Valid values are nil and `head'.
1568 See Info node `(gnus)Customizing Articles' for details."
1569   :version "22.1"
1570   :group 'gnus-article-treat
1571   :link '(custom-manual "(gnus)Customizing Articles")
1572   :type gnus-article-treat-head-custom)
1573
1574 (defcustom gnus-treat-capitalize-sentences nil
1575   "Capitalize sentence-starting words.
1576 Valid values are nil, t, `head', `first', `last', an integer or a
1577 predicate.  See Info node `(gnus)Customizing Articles'."
1578   :version "21.1"
1579   :group 'gnus-article-treat
1580   :link '(custom-manual "(gnus)Customizing Articles")
1581   :type gnus-article-treat-custom)
1582
1583 (defcustom gnus-treat-wash-html nil
1584   "Format as HTML.
1585 Valid values are nil, t, `head', `first', `last', an integer or a
1586 predicate.  See Info node `(gnus)Customizing Articles'."
1587   :version "22.1"
1588   :group 'gnus-article-treat
1589   :link '(custom-manual "(gnus)Customizing Articles")
1590   :type gnus-article-treat-custom)
1591
1592 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1593   "Fill long lines.
1594 Valid values are nil, t, `head', `first', `last', an integer or a
1595 predicate.  See Info node `(gnus)Customizing Articles'."
1596   :version "24.1"
1597   :group 'gnus-article-treat
1598   :link '(custom-manual "(gnus)Customizing Articles")
1599   :type gnus-article-treat-custom)
1600
1601 (defcustom gnus-treat-x-pgp-sig nil
1602   "Verify X-PGP-Sig.
1603 To automatically treat X-PGP-Sig, set it to head.
1604 Valid values are nil, t, `head', `first', `last', an integer or a
1605 predicate.  See Info node `(gnus)Customizing Articles'."
1606   :version "22.1"
1607   :group 'gnus-article-treat
1608   :group 'mime-security
1609   :link '(custom-manual "(gnus)Customizing Articles")
1610   :type gnus-article-treat-custom)
1611
1612 (defvar gnus-article-encrypt-protocol-alist
1613   '(("PGP" . mml2015-self-encrypt)))
1614
1615 ;; Set to nil if more than one protocol added to
1616 ;; gnus-article-encrypt-protocol-alist.
1617 (defcustom gnus-article-encrypt-protocol "PGP"
1618   "The protocol used for encrypt articles.
1619 It is a string, such as \"PGP\". If nil, ask user."
1620   :version "22.1"
1621   :type 'string
1622   :group 'mime-security)
1623
1624 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1625                               (mm-coding-system-p 'utf-8)
1626                               (executable-find idna-program))
1627   "Whether IDNA decoding of headers is used when viewing messages.
1628 This requires GNU Libidn, and by default only enabled if it is found."
1629   :version "22.1"
1630   :group 'gnus-article-headers
1631   :type 'boolean)
1632
1633 (defcustom gnus-article-over-scroll nil
1634   "If non-nil, allow scrolling the article buffer even when there no more text."
1635   :version "22.1"
1636   :group 'gnus-article
1637   :type 'boolean)
1638
1639 (defcustom gnus-inhibit-images nil
1640   "Non-nil means inhibit displaying of images inline in the article body."
1641   :version "24.1"
1642   :group 'gnus-article
1643   :type 'boolean)
1644
1645 (defcustom gnus-blocked-images 'gnus-block-private-groups
1646   "Images that have URLs matching this regexp will be blocked.
1647 This can also be a function to be evaluated.  If so, it will be
1648 called with the group name as the parameter, and should return a
1649 regexp."
1650   :version "24.1"
1651   :group 'gnus-art
1652   :type '(choice regexp function))
1653
1654 ;;; Internal variables
1655
1656 (defvar gnus-english-month-names
1657   '("January" "February" "March" "April" "May" "June" "July" "August"
1658     "September" "October" "November" "December"))
1659
1660 (defvar article-goto-body-goes-to-point-min-p nil)
1661 (defvar gnus-article-wash-types nil)
1662 (defvar gnus-article-emphasis-alist nil)
1663 (defvar gnus-article-image-alist nil)
1664
1665 (defvar gnus-article-mime-handle-alist-1 nil)
1666 (defvar gnus-treatment-function-alist
1667   '((gnus-treat-strip-cr gnus-article-remove-cr)
1668     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1669     (gnus-treat-strip-banner gnus-article-strip-banner)
1670     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1671     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1672     (gnus-treat-buttonize gnus-article-add-buttons)
1673     (gnus-treat-fill-article gnus-article-fill-cited-article)
1674     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1675     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1676     (gnus-treat-display-x-face gnus-article-display-x-face)
1677     (gnus-treat-display-face gnus-article-display-face)
1678     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1679     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1680     (gnus-treat-hide-signature gnus-article-hide-signature)
1681     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1682     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1683     (gnus-treat-from-picon gnus-treat-from-picon)
1684     (gnus-treat-mail-picon gnus-treat-mail-picon)
1685     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1686     (gnus-treat-strip-pem gnus-article-hide-pem)
1687     (gnus-treat-date gnus-article-treat-date)
1688     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1689     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1690     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1691     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1692     (gnus-treat-strip-trailing-blank-lines
1693      gnus-article-remove-trailing-blank-lines)
1694     (gnus-treat-strip-leading-blank-lines
1695      gnus-article-strip-leading-blank-lines)
1696     (gnus-treat-strip-multiple-blank-lines
1697      gnus-article-strip-multiple-blank-lines)
1698     (gnus-treat-overstrike gnus-article-treat-overstrike)
1699     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1700     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1701     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1702     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1703     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1704     (gnus-treat-display-smileys gnus-treat-smiley)
1705     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1706     (gnus-treat-wash-html gnus-article-wash-html)
1707     (gnus-treat-emphasize gnus-article-emphasize)
1708     (gnus-treat-hide-citation gnus-article-hide-citation)
1709     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1710     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1711     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1712
1713 (defvar gnus-article-mime-handle-alist nil)
1714 (defvar article-lapsed-timer nil)
1715 (defvar gnus-article-current-summary nil)
1716
1717 (defvar gnus-article-mode-syntax-table
1718   (let ((table (copy-syntax-table text-mode-syntax-table)))
1719     ;; This causes the citation match run O(2^n).
1720     ;; (modify-syntax-entry ?- "w" table)
1721     (modify-syntax-entry ?> ")<" table)
1722     (modify-syntax-entry ?< "(>" table)
1723     ;; make M-. in article buffers work for `foo' strings
1724     (modify-syntax-entry ?' " " table)
1725     (modify-syntax-entry ?` " " table)
1726     table)
1727   "Syntax table used in article mode buffers.
1728 Initialized from `text-mode-syntax-table.")
1729
1730 (defvar gnus-save-article-buffer nil)
1731
1732 (defvar gnus-number-of-articles-to-be-saved nil)
1733
1734 (defvar gnus-inhibit-hiding nil)
1735
1736 (defvar gnus-article-edit-mode nil)
1737
1738 ;;; Macros for dealing with the article buffer.
1739
1740 (defmacro gnus-with-article-headers (&rest forms)
1741   `(with-current-buffer gnus-article-buffer
1742      (save-restriction
1743        (let ((inhibit-read-only t)
1744              (inhibit-point-motion-hooks t)
1745              (case-fold-search t))
1746          (article-narrow-to-head)
1747          ,@forms))))
1748
1749 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1750 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1751
1752 (defmacro gnus-with-article-buffer (&rest forms)
1753   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1754      (with-current-buffer gnus-article-buffer
1755        (let ((inhibit-read-only t))
1756          ,@forms))))
1757
1758 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1759 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1760
1761 (defun gnus-article-goto-header (header)
1762   "Go to HEADER, which is a regular expression."
1763   (re-search-forward (concat "^\\(" header "\\):") nil t))
1764
1765 (defsubst gnus-article-hide-text (b e props)
1766   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1767   (gnus-add-text-properties-when 'article-type nil b e props)
1768   (when (memq 'intangible props)
1769     (put-text-property
1770      (max (1- b) (point-min))
1771      b 'intangible (cddr (memq 'intangible props)))))
1772
1773 (defsubst gnus-article-unhide-text (b e)
1774   "Remove hidden text properties from region between B and E."
1775   (remove-text-properties b e gnus-hidden-properties)
1776   (when (memq 'intangible gnus-hidden-properties)
1777     (put-text-property (max (1- b) (point-min))
1778                        b 'intangible nil)))
1779
1780 (defun gnus-article-hide-text-type (b e type)
1781   "Hide text of TYPE between B and E."
1782   (gnus-add-wash-type type)
1783   (gnus-article-hide-text
1784    b e (cons 'article-type (cons type gnus-hidden-properties))))
1785
1786 (defun gnus-article-unhide-text-type (b e type)
1787   "Unhide text of TYPE between B and E."
1788   (gnus-delete-wash-type type)
1789   (remove-text-properties
1790    b e (cons 'article-type (cons type gnus-hidden-properties)))
1791   (when (memq 'intangible gnus-hidden-properties)
1792     (put-text-property (max (1- b) (point-min))
1793                        b 'intangible nil)))
1794
1795 (defun gnus-article-delete-text-of-type (type)
1796   "Delete text of TYPE in the current buffer."
1797   (save-excursion
1798     (let ((b (point-min)))
1799       (if (eq type 'multipart)
1800           ;; Remove MIME buttons associated with multipart/alternative parts.
1801           (progn
1802             (goto-char b)
1803             (while (if (get-text-property (point) 'gnus-part)
1804                        (setq b (point))
1805                      (when (setq b (next-single-property-change (point)
1806                                                                 'gnus-part))
1807                        (goto-char b)
1808                        t))
1809               (end-of-line)
1810               (skip-chars-forward "\n")
1811               (when (eq (get-text-property b 'article-type) 'multipart)
1812                 (delete-region b (point)))))
1813         (while (setq b (text-property-any b (point-max) 'article-type type))
1814           (delete-region
1815            b (or (text-property-not-all b (point-max) 'article-type type)
1816                  (point-max))))))))
1817
1818 (defun gnus-article-delete-invisible-text ()
1819   "Delete all invisible text in the current buffer."
1820   (save-excursion
1821     (let ((b (point-min)))
1822       (while (setq b (text-property-any b (point-max) 'invisible t))
1823         (delete-region
1824          b (or (text-property-not-all b (point-max) 'invisible t)
1825                (point-max)))))))
1826
1827 (defsubst gnus-article-header-rank ()
1828   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1829   (let ((list gnus-sorted-header-list)
1830         (i 1))
1831     (while list
1832       (if (looking-at (car list))
1833           (setq list nil)
1834         (setq list (cdr list))
1835         (incf i)))
1836       i))
1837
1838 (defun article-hide-headers (&optional arg delete)
1839   "Hide unwanted headers and possibly sort them as well."
1840   (interactive)
1841   ;; This function might be inhibited.
1842   (unless gnus-inhibit-hiding
1843     (let ((inhibit-read-only t)
1844           (case-fold-search t)
1845           (max (1+ (length gnus-sorted-header-list)))
1846           (inhibit-point-motion-hooks t)
1847           (cur (current-buffer))
1848           ignored visible beg)
1849       (save-excursion
1850         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1851         ;; group parameters, so we should go to the summary buffer.
1852         (when (prog1
1853                   (condition-case nil
1854                       (progn (set-buffer gnus-summary-buffer) t)
1855                     (error nil))
1856                 (setq ignored (when (not gnus-visible-headers)
1857                                 (cond ((stringp gnus-ignored-headers)
1858                                        gnus-ignored-headers)
1859                                       ((listp gnus-ignored-headers)
1860                                        (mapconcat 'identity
1861                                                   gnus-ignored-headers
1862                                                   "\\|"))))
1863                       visible (cond ((stringp gnus-visible-headers)
1864                                      gnus-visible-headers)
1865                                     ((and gnus-visible-headers
1866                                           (listp gnus-visible-headers))
1867                                      (mapconcat 'identity
1868                                                 gnus-visible-headers
1869                                                 "\\|")))))
1870           (set-buffer cur))
1871         (save-restriction
1872           ;; First we narrow to just the headers.
1873           (article-narrow-to-head)
1874           ;; Hide any "From " lines at the beginning of (mail) articles.
1875           (while (looking-at "From ")
1876             (forward-line 1))
1877           (unless (bobp)
1878             (delete-region (point-min) (point)))
1879           ;; Then treat the rest of the header lines.
1880           ;; Then we use the two regular expressions
1881           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1882           ;; select which header lines is to remain visible in the
1883           ;; article buffer.
1884           (while (re-search-forward "^[^ \t:]*:" nil t)
1885             (beginning-of-line)
1886             ;; Mark the rank of the header.
1887             (put-text-property
1888              (point) (1+ (point)) 'message-rank
1889              (if (or (and visible (looking-at visible))
1890                      (and ignored
1891                           (not (looking-at ignored))))
1892                  (gnus-article-header-rank)
1893                (+ 2 max)))
1894             (forward-line 1))
1895           (message-sort-headers-1)
1896           (when (setq beg (text-property-any
1897                            (point-min) (point-max) 'message-rank (+ 2 max)))
1898             ;; We delete the unwanted headers.
1899             (gnus-add-wash-type 'headers)
1900             (add-text-properties (point-min) (+ 5 (point-min))
1901                                  '(article-type headers dummy-invisible t))
1902             (delete-region beg (point-max))))))))
1903
1904 (defun article-hide-boring-headers (&optional arg)
1905   "Toggle hiding of headers that aren't very interesting.
1906 If given a negative prefix, always show; if given a positive prefix,
1907 always hide."
1908   (interactive (gnus-article-hidden-arg))
1909   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1910              (not gnus-show-all-headers))
1911     (save-excursion
1912       (save-restriction
1913         (let ((inhibit-read-only t)
1914               (inhibit-point-motion-hooks t))
1915           (article-narrow-to-head)
1916           (dolist (elem gnus-boring-article-headers)
1917             (goto-char (point-min))
1918             (cond
1919              ;; Hide empty headers.
1920              ((eq elem 'empty)
1921               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1922                 (forward-line -1)
1923                 (gnus-article-hide-text-type
1924                  (point-at-bol)
1925                  (progn
1926                    (end-of-line)
1927                    (if (re-search-forward "^[^ \t]" nil t)
1928                        (match-beginning 0)
1929                      (point-max)))
1930                  'boring-headers)))
1931              ;; Hide boring Newsgroups header.
1932              ((eq elem 'newsgroups)
1933               (when (gnus-string-equal
1934                      (gnus-fetch-field "newsgroups")
1935                      (gnus-group-real-name
1936                       (if (boundp 'gnus-newsgroup-name)
1937                           gnus-newsgroup-name
1938                         "")))
1939                 (gnus-article-hide-header "newsgroups")))
1940              ((eq elem 'to-address)
1941               (let ((to (message-fetch-field "to"))
1942                     (to-address
1943                      (gnus-parameter-to-address
1944                       (if (boundp 'gnus-newsgroup-name)
1945                           gnus-newsgroup-name ""))))
1946                 (when (and to to-address
1947                            (ignore-errors
1948                              (gnus-string-equal
1949                               ;; only one address in To
1950                               (nth 1 (mail-extract-address-components to))
1951                               to-address)))
1952                   (gnus-article-hide-header "to"))))
1953              ((eq elem 'to-list)
1954               (let ((to (message-fetch-field "to"))
1955                     (to-list
1956                      (gnus-parameter-to-list
1957                       (if (boundp 'gnus-newsgroup-name)
1958                           gnus-newsgroup-name ""))))
1959                 (when (and to to-list
1960                            (ignore-errors
1961                              (gnus-string-equal
1962                               ;; only one address in To
1963                               (nth 1 (mail-extract-address-components to))
1964                               to-list)))
1965                   (gnus-article-hide-header "to"))))
1966              ((eq elem 'cc-list)
1967               (let ((cc (message-fetch-field "cc"))
1968                     (to-list
1969                      (gnus-parameter-to-list
1970                       (if (boundp 'gnus-newsgroup-name)
1971                           gnus-newsgroup-name ""))))
1972                 (when (and cc to-list
1973                            (ignore-errors
1974                              (gnus-string-equal
1975                               ;; only one address in CC
1976                               (nth 1 (mail-extract-address-components cc))
1977                               to-list)))
1978                   (gnus-article-hide-header "cc"))))
1979              ((eq elem 'followup-to)
1980               (when (gnus-string-equal
1981                      (message-fetch-field "followup-to")
1982                      (message-fetch-field "newsgroups"))
1983                 (gnus-article-hide-header "followup-to")))
1984              ((eq elem 'reply-to)
1985               (if (gnus-group-find-parameter
1986                    gnus-newsgroup-name 'broken-reply-to)
1987                   (gnus-article-hide-header "reply-to")
1988                 (let ((from (message-fetch-field "from"))
1989                       (reply-to (message-fetch-field "reply-to")))
1990                   (when
1991                       (and
1992                        from reply-to
1993                        (ignore-errors
1994                          (equal
1995                           (sort (mapcar
1996                                  (lambda (x) (downcase (cadr x)))
1997                                  (mail-extract-address-components from t))
1998                                 'string<)
1999                           (sort (mapcar
2000                                  (lambda (x) (downcase (cadr x)))
2001                                  (mail-extract-address-components reply-to t))
2002                                 'string<))))
2003                     (gnus-article-hide-header "reply-to")))))
2004              ((eq elem 'date)
2005               (let ((date (with-current-buffer gnus-original-article-buffer
2006                             ;; If date in `gnus-article-buffer' is localized
2007                             ;; (`gnus-treat-date-user-defined'),
2008                             ;; `days-between' might fail.
2009                             (message-fetch-field "date"))))
2010                 (when (and date
2011                            (< (days-between (current-time-string) date)
2012                               4))
2013                   (gnus-article-hide-header "date"))))
2014              ((eq elem 'long-to)
2015               (let ((to (message-fetch-field "to"))
2016                     (cc (message-fetch-field "cc")))
2017                 (when (> (length to) 1024)
2018                   (gnus-article-hide-header "to"))
2019                 (when (> (length cc) 1024)
2020                   (gnus-article-hide-header "cc"))))
2021              ((eq elem 'many-to)
2022               (let ((to-count 0)
2023                     (cc-count 0))
2024                 (goto-char (point-min))
2025                 (while (re-search-forward "^to:" nil t)
2026                   (setq to-count (1+ to-count)))
2027                 (when (> to-count 1)
2028                   (while (> to-count 0)
2029                     (goto-char (point-min))
2030                     (save-restriction
2031                       (re-search-forward "^to:" nil nil to-count)
2032                       (forward-line -1)
2033                       (narrow-to-region (point) (point-max))
2034                       (gnus-article-hide-header "to"))
2035                     (setq to-count (1- to-count))))
2036                 (goto-char (point-min))
2037                 (while (re-search-forward "^cc:" nil t)
2038                   (setq cc-count (1+ cc-count)))
2039                 (when (> cc-count 1)
2040                   (while (> cc-count 0)
2041                     (goto-char (point-min))
2042                     (save-restriction
2043                       (re-search-forward "^cc:" nil nil cc-count)
2044                       (forward-line -1)
2045                       (narrow-to-region (point) (point-max))
2046                       (gnus-article-hide-header "cc"))
2047                     (setq cc-count (1- cc-count)))))))))))))
2048
2049 (defun gnus-article-hide-header (header)
2050   (save-excursion
2051     (goto-char (point-min))
2052     (when (re-search-forward (concat "^" header ":") nil t)
2053       (gnus-article-hide-text-type
2054        (point-at-bol)
2055        (progn
2056          (end-of-line)
2057          (if (re-search-forward "^[^ \t]" nil t)
2058              (match-beginning 0)
2059            (point-max)))
2060        'boring-headers))))
2061
2062 (defvar gnus-article-normalized-header-length 40
2063   "Length of normalized headers.")
2064
2065 (defun article-normalize-headers ()
2066   "Make all header lines 40 characters long."
2067   (interactive)
2068   (let ((inhibit-read-only t)
2069         column)
2070     (save-excursion
2071       (save-restriction
2072         (article-narrow-to-head)
2073         (while (not (eobp))
2074           (cond
2075            ((< (setq column (- (point-at-eol) (point)))
2076                gnus-article-normalized-header-length)
2077             (end-of-line)
2078             (insert (make-string
2079                      (- gnus-article-normalized-header-length column)
2080                      ? )))
2081            ((> column gnus-article-normalized-header-length)
2082             (gnus-put-text-property
2083              (progn
2084                (forward-char gnus-article-normalized-header-length)
2085                (point))
2086              (point-at-eol)
2087              'invisible t))
2088            (t
2089             ;; Do nothing.
2090             ))
2091           (forward-line 1))))))
2092
2093 (defun article-treat-dumbquotes ()
2094   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2095 Note that this function guesses whether a character is a sm*rtq**t* or
2096 not, so it should only be used interactively.
2097
2098 Sm*rtq**t*s are M****s***'s unilateral extension to the
2099 iso-8859-1 character map in an attempt to provide more quoting
2100 characters.  If you see something like \\222 or \\264 where
2101 you're expecting some kind of apostrophe or quotation mark, then
2102 try this wash."
2103   (interactive)
2104   (article-translate-strings gnus-article-dumbquotes-map))
2105
2106 (defvar org-entities)
2107
2108 (defun article-treat-non-ascii ()
2109   "Translate many Unicode characters into their ASCII equivalents."
2110   (interactive)
2111   (require 'org-entities)
2112   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2113     (dolist (elem org-entities)
2114       (when (and (listp elem)
2115                  (= (length (nth 6 elem)) 1))
2116         (if (featurep 'xemacs)
2117             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2118           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2119     (save-excursion
2120       (when (article-goto-body)
2121         (let ((inhibit-read-only t)
2122               replace props)
2123           (while (not (eobp))
2124             (if (not (setq replace (if (featurep 'xemacs)
2125                                        (get-char-table (following-char) table)
2126                                      (aref table (following-char)))))
2127                 (forward-char 1)
2128               (if (prog1
2129                       (setq props (text-properties-at (point)))
2130                     (delete-char 1))
2131                   (add-text-properties (point) (progn (insert replace) (point))
2132                                        props)
2133                 (insert replace)))))))))
2134
2135 (defun article-translate-strings (map)
2136   "Translate all string in the body of the article according to MAP.
2137 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2138   (save-excursion
2139     (when (article-goto-body)
2140       (let ((inhibit-read-only t))
2141         (dolist (elem map)
2142           (let ((from (car elem))
2143                 (to (cadr elem)))
2144             (save-excursion
2145               (if (stringp from)
2146                   (while (search-forward from nil t)
2147                     (replace-match to))
2148                 (while (not (eobp))
2149                   (if (eq (following-char) from)
2150                       (progn
2151                         (delete-char 1)
2152                         (insert to))
2153                     (forward-char 1)))))))))))
2154
2155 (defun article-treat-overstrike ()
2156   "Translate overstrikes into bold text."
2157   (interactive)
2158   (save-excursion
2159     (when (article-goto-body)
2160       (let ((inhibit-read-only t))
2161         (while (search-forward "\b" nil t)
2162           (let ((next (char-after))
2163                 (previous (char-after (- (point) 2))))
2164             ;; We do the boldification/underlining by hiding the
2165             ;; overstrikes and putting the proper text property
2166             ;; on the letters.
2167             (cond
2168              ((eq next previous)
2169               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2170               (put-text-property (point) (1+ (point)) 'face 'bold))
2171              ((eq next ?_)
2172               (gnus-article-hide-text-type
2173                (1- (point)) (1+ (point)) 'overstrike)
2174               (put-text-property
2175                (- (point) 2) (1- (point)) 'face 'underline))
2176              ((eq previous ?_)
2177               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2178               (put-text-property
2179                (point) (1+ (point)) 'face 'underline)))))))))
2180
2181 (defun article-treat-ansi-sequences ()
2182   "Translate ANSI SGR control sequences into overlays or extents."
2183   (interactive)
2184   (save-excursion
2185     (when (article-goto-body)
2186       (let ((inhibit-read-only t))
2187         (ansi-color-apply-on-region (point) (point-max))))))
2188
2189 (defun gnus-article-treat-unfold-headers ()
2190   "Unfold folded message headers.
2191 Only the headers that fit into the current window width will be
2192 unfolded."
2193   (interactive)
2194   (gnus-with-article-headers
2195     (let (length)
2196       (while (not (eobp))
2197         (save-restriction
2198           (mail-header-narrow-to-field)
2199           (let* ((header (buffer-string))
2200                  (unfoldable
2201                   (or (equal gnus-article-unfold-long-headers t)
2202                       (and (stringp gnus-article-unfold-long-headers)
2203                            (string-match gnus-article-unfold-long-headers
2204                                          header)))))
2205             (with-temp-buffer
2206               (insert header)
2207               (goto-char (point-min))
2208               (while (re-search-forward "\n[\t ]" nil t)
2209                 (replace-match " " t t)))
2210             (setq length (- (point-max) (point-min) 1))
2211             (when (or unfoldable
2212                       (< length (window-width)))
2213               (while (re-search-forward "\n[\t ]" nil t)
2214                 (replace-match " " t t))))
2215           (goto-char (point-max)))))))
2216
2217 (defun gnus-article-treat-fold-headers ()
2218   "Fold message headers."
2219   (interactive)
2220   (gnus-with-article-headers
2221     (while (not (eobp))
2222       (save-restriction
2223         (mail-header-narrow-to-field)
2224         (mail-header-fold-field)
2225         (goto-char (point-max))))))
2226
2227 (defun gnus-treat-smiley ()
2228   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2229   (interactive)
2230   (gnus-with-article-buffer
2231     (if (memq 'smiley gnus-article-wash-types)
2232         (gnus-delete-images 'smiley)
2233       (article-goto-body)
2234       (let ((images (smiley-region (point) (point-max))))
2235         (when images
2236           (gnus-add-wash-type 'smiley)
2237           (dolist (image images)
2238             (gnus-add-image 'smiley image)))))))
2239
2240 (defun gnus-article-remove-images ()
2241   "Remove all images from the article buffer."
2242   (interactive)
2243   (gnus-with-article-buffer
2244     (save-restriction
2245       (widen)
2246       (dolist (elem gnus-article-image-alist)
2247         (gnus-delete-images (car elem))))))
2248
2249 (autoload 'w3m-toggle-inline-images "w3m")
2250
2251 (defun gnus-article-show-images ()
2252   "Show any images that are in the HTML-rendered article buffer.
2253 This only works if the article in question is HTML."
2254   (interactive)
2255   (gnus-with-article-buffer
2256     (save-restriction
2257       (widen)
2258       (if (eq mm-text-html-renderer 'w3m)
2259           (let ((mm-inline-text-html-with-images nil))
2260             (w3m-toggle-inline-images))
2261         (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2262                                                         'image-displayer))
2263           (destructuring-bind (start end function) region
2264             (funcall function (get-text-property start 'image-url)
2265                      start end)))))))
2266
2267 (defun gnus-article-treat-fold-newsgroups ()
2268   "Unfold folded message headers.
2269 Only the headers that fit into the current window width will be
2270 unfolded."
2271   (interactive)
2272   (gnus-with-article-headers
2273     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2274       (save-restriction
2275         (mail-header-narrow-to-field)
2276         (while (re-search-forward ", *" nil t)
2277           (replace-match ", " t t))
2278         (mail-header-fold-field)
2279         (goto-char (point-max))))))
2280
2281 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2282   "Value of `truncate-lines' in Gnus Article buffer.
2283 Valid values are nil, t, `head', `first', `last', an integer or a
2284 predicate.  See Info node `(gnus)Customizing Articles'."
2285   :version "23.1" ;; No Gnus
2286   :group 'gnus-article
2287   ;; :link '(custom-manual "(gnus)Customizing Articles")
2288   :type 'boolean)
2289
2290 (defun gnus-article-toggle-truncate-lines (&optional arg)
2291   "Toggle whether to fold or truncate long lines in article the buffer.
2292 If ARG is non-nil and not a number, toggle
2293 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2294 long lines if and only if arg is positive."
2295   (interactive "P")
2296   (cond
2297    ((and (numberp arg) (> arg 0))
2298     (setq gnus-article-truncate-lines t))
2299    ((numberp arg)
2300     (setq gnus-article-truncate-lines nil))
2301    (arg
2302     (setq gnus-article-truncate-lines
2303           (not gnus-article-truncate-lines))))
2304   (gnus-with-article-buffer
2305     (cond
2306      ((and (numberp arg) (> arg 0))
2307       (setq truncate-lines nil))
2308      ((numberp arg)
2309       (setq truncate-lines t)))
2310     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2311     ;; `toggle-truncate-lines' needs an argument.
2312     (toggle-truncate-lines)))
2313
2314 (defun gnus-article-treat-body-boundary ()
2315   "Place a boundary line at the end of the headers."
2316   (interactive)
2317   (when (and gnus-body-boundary-delimiter
2318              (> (length gnus-body-boundary-delimiter) 0))
2319     (gnus-with-article-headers
2320       (goto-char (point-max))
2321       (let ((start (point)))
2322         (insert "X-Boundary: ")
2323         (gnus-add-text-properties start (point) '(invisible t intangible t))
2324        (insert (let (str (max (window-width)))
2325                  (if (featurep 'xemacs)
2326                      (setq max (1- max)))
2327                  (while (>= max (length str))
2328                     (setq str (concat str gnus-body-boundary-delimiter)))
2329                  (substring str 0 max))
2330                 "\n")
2331         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2332
2333 (defun article-fill-long-lines ()
2334   "Fill lines that are wider than the window width."
2335   (interactive)
2336   (save-excursion
2337     (let ((inhibit-read-only t)
2338           (width (window-width (get-buffer-window (current-buffer)))))
2339       (save-restriction
2340         (article-goto-body)
2341         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2342           (while (not (eobp))
2343             (end-of-line)
2344             (when (>= (current-column) (min fill-column width))
2345               (narrow-to-region (min (1+ (point)) (point-max))
2346                                 (point-at-bol))
2347               (let ((goback (point-marker)))
2348                 (fill-paragraph nil)
2349                 (goto-char (marker-position goback)))
2350               (widen))
2351             (forward-line 1)))))))
2352
2353 (defun article-capitalize-sentences ()
2354   "Capitalize the first word in each sentence."
2355   (interactive)
2356   (save-excursion
2357     (let ((inhibit-read-only t)
2358           (paragraph-start "^[\n\^L]"))
2359       (article-goto-body)
2360       (while (not (eobp))
2361         (capitalize-word 1)
2362         (forward-sentence)))))
2363
2364 (defun article-remove-cr ()
2365   "Remove trailing CRs and then translate remaining CRs into LFs."
2366   (interactive)
2367   (save-excursion
2368     (let ((inhibit-read-only t))
2369       (goto-char (point-min))
2370       (while (re-search-forward "\r+$" nil t)
2371         (replace-match "" t t))
2372       (goto-char (point-min))
2373       (while (search-forward "\r" nil t)
2374         (replace-match "\n" t t)))))
2375
2376 (defun article-remove-trailing-blank-lines ()
2377   "Remove all trailing blank lines from the article."
2378   (interactive)
2379   (save-excursion
2380     (let ((inhibit-read-only t))
2381       (goto-char (point-max))
2382       (delete-region
2383        (point)
2384        (progn
2385          (while (and (not (bobp))
2386                      (looking-at "^[ \t]*$")
2387                      (not (gnus-annotation-in-region-p
2388                            (point) (point-at-eol))))
2389            (forward-line -1))
2390          (forward-line 1)
2391          (point))))))
2392
2393 (defvar gnus-face-properties-alist)
2394
2395 (defun article-display-face (&optional force)
2396   "Display any Face headers in the header."
2397   (interactive (list 'force))
2398   (let ((wash-face-p buffer-read-only))
2399     (gnus-with-article-headers
2400       ;; When displaying parts, this function can be called several times on
2401       ;; the same article, without any intended toggle semantic (as typing `W
2402       ;; D d' would have). So face deletion must occur only when we come from
2403       ;; an interactive command, that is when the *Article* buffer is
2404       ;; read-only.
2405       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2406           (gnus-delete-images 'face)
2407         (let ((from (message-fetch-field "from"))
2408               face faces)
2409           (save-current-buffer
2410             (when (and wash-face-p
2411                        (gnus-buffer-live-p gnus-original-article-buffer)
2412                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2413               (set-buffer gnus-original-article-buffer))
2414             (save-restriction
2415               (mail-narrow-to-head)
2416               (when (or force
2417                         ;; Check whether this face is censored.
2418                         (not (and gnus-article-x-face-too-ugly
2419                                   (or from
2420                                       (setq from (message-fetch-field "from")))
2421                                   (string-match gnus-article-x-face-too-ugly
2422                                                 from))))
2423                 (while (gnus-article-goto-header "Face")
2424                   (push (mail-header-field-value) faces)))))
2425           (when faces
2426             (goto-char (point-min))
2427             (let (png image)
2428               (unless (setq from (gnus-article-goto-header "from"))
2429                 (insert "From:")
2430                 (setq from (point))
2431                 (insert " [no `from' set]\n"))
2432               (while faces
2433                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2434                   (setq image
2435                         (apply 'gnus-create-image png 'png t
2436                                (cdr (assq 'png gnus-face-properties-alist))))
2437                   (goto-char from)
2438                   (when image
2439                     (gnus-add-wash-type 'face)
2440                     (gnus-add-image 'face image)
2441                     (gnus-put-image image nil 'face)))))))))))
2442
2443 (defun article-display-x-face (&optional force)
2444   "Look for an X-Face header and display it if present."
2445   (interactive (list 'force))
2446   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2447     (gnus-with-article-headers
2448       ;; Delete the old process, if any.
2449       (when (process-status "article-x-face")
2450         (delete-process "article-x-face"))
2451       ;; See the comment in `article-display-face'.
2452       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2453           ;; We have already displayed X-Faces, so we remove them
2454           ;; instead.
2455           (gnus-delete-images 'xface)
2456         ;; Display X-Faces.
2457         (let ((from (message-fetch-field "from"))
2458               x-faces face)
2459           (save-current-buffer
2460             (when (and wash-face-p
2461                        (gnus-buffer-live-p gnus-original-article-buffer)
2462                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2463               ;; If type `W f', use gnus-original-article-buffer,
2464               ;; otherwise use the current buffer because displaying
2465               ;; RFC822 parts calls this function too.
2466               (set-buffer gnus-original-article-buffer))
2467             (save-restriction
2468               (mail-narrow-to-head)
2469               (and gnus-article-x-face-command
2470                    (or force
2471                        ;; Check whether this face is censored.
2472                        (not (and gnus-article-x-face-too-ugly
2473                                  (or from
2474                                      (setq from (message-fetch-field "from")))
2475                                  (string-match gnus-article-x-face-too-ugly
2476                                                from))))
2477                    (while (gnus-article-goto-header "X-Face")
2478                      (push (mail-header-field-value) x-faces)))))
2479           (when x-faces
2480             ;; We display the face.
2481             (cond ((functionp gnus-article-x-face-command)
2482                    ;; The command is a lisp function, so we call it.
2483                    (mapc gnus-article-x-face-command x-faces))
2484                   ((stringp gnus-article-x-face-command)
2485                    ;; The command is a string, so we interpret the command
2486                    ;; as a, well, command, and fork it off.
2487                    (let ((process-connection-type nil))
2488                      (gnus-set-process-query-on-exit-flag
2489                       (start-process
2490                        "article-x-face" nil shell-file-name
2491                        shell-command-switch gnus-article-x-face-command)
2492                       nil)
2493                      ;; Sending multiple EOFs to xv doesn't work,
2494                      ;; so we only do a single external face.
2495                      (with-temp-buffer
2496                        (insert (car x-faces))
2497                        (process-send-region "article-x-face"
2498                                             (point-min) (point-max)))
2499                      (process-send-eof "article-x-face")))
2500                   (t
2501                    (error "`%s' set to `%s' is not a function"
2502                           gnus-article-x-face-command
2503                           'gnus-article-x-face-command)))))))))
2504
2505 (defun article-decode-mime-words ()
2506   "Decode all MIME-encoded words in the article."
2507   (interactive)
2508   (gnus-with-article-buffer
2509     (let ((inhibit-point-motion-hooks t)
2510           (mail-parse-charset gnus-newsgroup-charset)
2511           (mail-parse-ignored-charsets
2512            (with-current-buffer gnus-summary-buffer
2513              gnus-newsgroup-ignored-charsets)))
2514       (mail-decode-encoded-word-region (point-min) (point-max)))))
2515
2516 (defun article-decode-charset (&optional prompt)
2517   "Decode charset-encoded text in the article.
2518 If PROMPT (the prefix), prompt for a coding system to use."
2519   (interactive "P")
2520   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2521         (inhibit-read-only t)
2522         (mail-parse-charset gnus-newsgroup-charset)
2523         (mail-parse-ignored-charsets
2524          (save-excursion (condition-case nil
2525                              (set-buffer gnus-summary-buffer)
2526                            (error))
2527                          gnus-newsgroup-ignored-charsets))
2528         ct cte ctl charset format)
2529     (save-excursion
2530       (save-restriction
2531         (article-narrow-to-head)
2532         (setq ct (message-fetch-field "Content-Type" t)
2533               cte (message-fetch-field "Content-Transfer-Encoding" t)
2534               ctl (and ct (mail-header-parse-content-type ct))
2535               charset (cond
2536                        (prompt
2537                         (mm-read-coding-system "Charset to decode: "))
2538                        (ctl
2539                         (mail-content-type-get ctl 'charset)))
2540               format (and ctl (mail-content-type-get ctl 'format)))
2541         (when cte
2542           (setq cte (mail-header-strip cte)))
2543         (if (and ctl (not (string-match "/" (car ctl))))
2544             (setq ctl nil))
2545         (goto-char (point-max)))
2546       (forward-line 1)
2547       (save-restriction
2548         (narrow-to-region (point) (point-max))
2549         (when (and (eq mail-parse-charset 'gnus-decoded)
2550                    (eq (mm-body-7-or-8) '8bit))
2551           ;; The text code could have been decoded.
2552           (setq charset mail-parse-charset))
2553         (when (and (or (not ctl)
2554                        (equal (car ctl) "text/plain"))
2555                    (not format)) ;; article with format will decode later.
2556           (mm-decode-body
2557            charset (and cte (intern (downcase
2558                                      (gnus-strip-whitespace cte))))
2559            (car ctl)))))))
2560
2561 (defun article-decode-encoded-words ()
2562   "Remove encoded-word encoding from headers."
2563   (let ((inhibit-point-motion-hooks t)
2564         (mail-parse-charset gnus-newsgroup-charset)
2565         (mail-parse-ignored-charsets
2566          (save-excursion (condition-case nil
2567                              (set-buffer gnus-summary-buffer)
2568                            (error))
2569                          gnus-newsgroup-ignored-charsets))
2570         (inhibit-read-only t)
2571         end start)
2572     (goto-char (point-min))
2573     (when (search-forward "\n\n" nil 'move)
2574       (forward-line -1))
2575     (setq end (point))
2576     (while (not (bobp))
2577       (while (progn
2578                (forward-line -1)
2579                (and (not (bobp))
2580                     (memq (char-after) '(?\t ? )))))
2581       (setq start (point))
2582       (if (looking-at "\
2583 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2584 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2585           (funcall gnus-decode-address-function start end)
2586         (funcall gnus-decode-header-function start end))
2587       (goto-char (setq end start)))))
2588
2589 (defun article-decode-group-name ()
2590   "Decode group names in Newsgroups, Followup-To and Xref headers."
2591   (let ((inhibit-point-motion-hooks t)
2592         (inhibit-read-only t)
2593         (method (gnus-find-method-for-group gnus-newsgroup-name))
2594         regexp)
2595     (when (and (or gnus-group-name-charset-method-alist
2596                    gnus-group-name-charset-group-alist)
2597                (gnus-buffer-live-p gnus-original-article-buffer))
2598       (save-restriction
2599         (article-narrow-to-head)
2600         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2601           (with-current-buffer gnus-original-article-buffer
2602             (goto-char (point-min)))
2603           (setq regexp (concat "^" header
2604                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2605           (while (re-search-forward regexp nil t)
2606             (replace-match (save-match-data
2607                              (gnus-decode-newsgroups
2608                               ;; XXX how to use data in article buffer?
2609                               (with-current-buffer gnus-original-article-buffer
2610                                 (re-search-forward regexp nil t)
2611                                 (match-string 1))
2612                               gnus-newsgroup-name method))
2613                            t t nil 1))
2614           (goto-char (point-min)))))))
2615
2616 (autoload 'idna-to-unicode "idna")
2617
2618 (defun article-decode-idna-rhs ()
2619   "Decode IDNA strings in RHS in various headers in current buffer.
2620 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2621 Mail-Reply-To: and Mail-Followup-To:."
2622   (when gnus-use-idna
2623     (save-restriction
2624       (let ((inhibit-point-motion-hooks t)
2625             (inhibit-read-only t))
2626         (article-narrow-to-head)
2627         (goto-char (point-min))
2628         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2629           (let (ace unicode)
2630             (when (save-match-data
2631                     (and (setq ace (match-string 1))
2632                          (save-excursion
2633                            (and (re-search-backward "^[^ \t]" nil t)
2634                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2635                          (setq unicode (idna-to-unicode ace))))
2636               (unless (string= ace unicode)
2637                 (replace-match unicode nil nil nil 1)))))))))
2638
2639 (defun article-de-quoted-unreadable (&optional force read-charset)
2640   "Translate a quoted-printable-encoded article.
2641 If FORCE, decode the article whether it is marked as quoted-printable
2642 or not.
2643 If READ-CHARSET, ask for a coding system."
2644   (interactive (list 'force current-prefix-arg))
2645   (save-excursion
2646     (let ((inhibit-read-only t) type charset)
2647       (if (gnus-buffer-live-p gnus-original-article-buffer)
2648           (with-current-buffer gnus-original-article-buffer
2649             (setq type
2650                   (gnus-fetch-field "content-transfer-encoding"))
2651             (let* ((ct (gnus-fetch-field "content-type"))
2652                    (ctl (and ct (mail-header-parse-content-type ct))))
2653               (setq charset (and ctl
2654                                  (mail-content-type-get ctl 'charset)))
2655               (if (stringp charset)
2656                   (setq charset (intern (downcase charset)))))))
2657       (if read-charset
2658           (setq charset (mm-read-coding-system "Charset: " charset)))
2659       (unless charset
2660         (setq charset gnus-newsgroup-charset))
2661       (when (or force
2662                 (and type (let ((case-fold-search t))
2663                             (string-match "quoted-printable" type))))
2664         (article-goto-body)
2665         (quoted-printable-decode-region
2666          (point) (point-max) (mm-charset-to-coding-system charset nil t))))))
2667
2668 (defun article-de-base64-unreadable (&optional force read-charset)
2669   "Translate a base64 article.
2670 If FORCE, decode the article whether it is marked as base64 not.
2671 If READ-CHARSET, ask for a coding system."
2672   (interactive (list 'force current-prefix-arg))
2673   (save-excursion
2674     (let ((inhibit-read-only t) type charset)
2675       (if (gnus-buffer-live-p gnus-original-article-buffer)
2676           (with-current-buffer gnus-original-article-buffer
2677             (setq type
2678                   (gnus-fetch-field "content-transfer-encoding"))
2679             (let* ((ct (gnus-fetch-field "content-type"))
2680                    (ctl (and ct (mail-header-parse-content-type ct))))
2681               (setq charset (and ctl
2682                                  (mail-content-type-get ctl 'charset)))
2683               (if (stringp charset)
2684                   (setq charset (intern (downcase charset)))))))
2685       (if read-charset
2686           (setq charset (mm-read-coding-system "Charset: " charset)))
2687       (unless charset
2688         (setq charset gnus-newsgroup-charset))
2689       (when (or force
2690                 (and type (let ((case-fold-search t))
2691                             (string-match "base64" type))))
2692         (article-goto-body)
2693         (save-restriction
2694           (narrow-to-region (point) (point-max))
2695           (base64-decode-region (point-min) (point-max))
2696           (mm-decode-coding-region
2697            (point-min) (point-max)
2698            (mm-charset-to-coding-system charset nil t)))))))
2699
2700 (eval-when-compile
2701   (require 'rfc1843))
2702
2703 (defun article-decode-HZ ()
2704   "Translate a HZ-encoded article."
2705   (interactive)
2706   (require 'rfc1843)
2707   (save-excursion
2708     (let ((inhibit-read-only t))
2709       (rfc1843-decode-region (point-min) (point-max)))))
2710
2711 (defun article-unsplit-urls ()
2712   "Remove the newlines that some other mailers insert into URLs."
2713   (interactive)
2714   (save-excursion
2715     (let ((inhibit-read-only t))
2716       (goto-char (point-min))
2717       (while (re-search-forward
2718               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2719         (replace-match "\\1\\3" t)))
2720     (when (gmm-called-interactively-p 'any)
2721       (gnus-treat-article nil))))
2722
2723 (defun article-wash-html ()
2724   "Format an HTML article."
2725   (interactive)
2726   (let ((handles nil)
2727         (buffer-read-only nil))
2728     (when (gnus-buffer-live-p gnus-original-article-buffer)
2729       (with-current-buffer gnus-original-article-buffer
2730         (setq handles (mm-dissect-buffer t t))))
2731     (article-goto-body)
2732     (delete-region (point) (point-max))
2733     (mm-enable-multibyte)
2734     (mm-inline-text-html handles)))
2735
2736 (defvar gnus-article-browse-html-temp-list nil
2737   "List of temporary files created by `gnus-article-browse-html-parts'.
2738 Internal variable.")
2739
2740 (defcustom gnus-article-browse-delete-temp 'ask
2741   "What to do with temporary files from `gnus-article-browse-html-parts'.
2742 If nil, don't delete temporary files.  If it is t, delete them on
2743 exit from the summary buffer.  If it is the symbol `file', query
2744 on each file, if it is `ask' ask once when exiting from the
2745 summary buffer."
2746   :group 'gnus-article
2747   :version "23.1" ;; No Gnus
2748   :type '(choice (const :tag "Don't delete" nil)
2749                  (const :tag "Don't ask" t)
2750                  (const :tag "Ask" ask)
2751                  (const :tag "Ask for each file" file)))
2752
2753 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2754
2755 (defun gnus-article-browse-delete-temp-files (&optional how)
2756   "Delete temp-files created by `gnus-article-browse-html-parts'."
2757   (when (and gnus-article-browse-html-temp-list
2758              (progn
2759                (or how (setq how gnus-article-browse-delete-temp))
2760                (if (eq how 'ask)
2761                    (let ((files (length gnus-article-browse-html-temp-list)))
2762                      (or (gnus-y-or-n-p
2763                           (if (= files 1)
2764                               "Delete the temporary HTML file? "
2765                             (format "Delete all %s temporary HTML files? "
2766                                     files)))
2767                          (setq gnus-article-browse-html-temp-list nil)))
2768                  how)))
2769     (dolist (file gnus-article-browse-html-temp-list)
2770       (cond ((file-directory-p file)
2771              (when (or (not (eq how 'file))
2772                        (gnus-y-or-n-p
2773                         (format
2774                          "Delete temporary HTML file(s) in directory `%s'? "
2775                          (file-name-as-directory file))))
2776                (gnus-delete-directory file)))
2777             ((file-exists-p file)
2778              (when (or (not (eq how 'file))
2779                        (gnus-y-or-n-p
2780                         (format "Delete temporary HTML file `%s'? " file)))
2781                (delete-file file)))))
2782     ;; Also remove file from the list when not deleted or if file doesn't
2783     ;; exist anymore.
2784     (setq gnus-article-browse-html-temp-list nil))
2785   gnus-article-browse-html-temp-list)
2786
2787 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2788   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2789 Return file name."
2790   (save-match-data
2791     (let (file type)
2792       (catch 'found
2793         (dolist (handle handles)
2794           (cond
2795            ((not (listp handle)))
2796            ;; Exclude broken handles that `gnus-summary-enter-digest-group'
2797            ;; may create.
2798            ((not (or (bufferp (car handle)) (stringp (car handle)))))
2799            ((equal (mm-handle-media-supertype handle) "multipart")
2800             (when (setq file (gnus-article-browse-html-save-cid-content
2801                               cid handle directory))
2802               (throw 'found file)))
2803            ((equal (concat "<" cid ">") (mm-handle-id handle))
2804             (setq file
2805                   (expand-file-name
2806                    (or (mm-handle-filename handle)
2807                        (concat
2808                         (make-temp-name "cid")
2809                         (car (rassoc (car (mm-handle-type handle))
2810                                      mailcap-mime-extensions))))
2811                    directory))
2812             (mm-save-part-to-file handle file)
2813             (throw 'found file))))))))
2814
2815 (defun gnus-article-browse-html-parts (list &optional header)
2816   "View all \"text/html\" parts from LIST.
2817 Recurse into multiparts.  The optional HEADER that should be a decoded
2818 message header will be added to the bodies of the \"text/html\" parts."
2819   ;; Internal function used by `gnus-article-browse-html-article'.
2820   (let (type file charset content cid-dir tmp-file showed)
2821     ;; Find and show the html-parts.
2822     (dolist (handle list)
2823       ;; If HTML, show it:
2824       (cond ((not (listp handle)))
2825             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2826                  (and (equal (car type) "message/external-body")
2827                       (or header
2828                           (setq file (mm-handle-filename handle)))
2829                       (or (mm-handle-cache handle)
2830                           (condition-case code
2831                               (progn (mm-extern-cache-contents handle) t)
2832                             (error
2833                              (gnus-message 3 "%s" (error-message-string code))
2834                              (when (>= gnus-verbose 3) (sit-for 2))
2835                              nil)))
2836                       (progn
2837                         (setq handle (mm-handle-cache handle)
2838                               type (mm-handle-type handle))
2839                         (equal (car type) "text/html"))))
2840              (setq charset (mail-content-type-get type 'charset)
2841                    content (mm-get-part handle))
2842              (with-temp-buffer
2843                (if (eq charset 'gnus-decoded)
2844                    (mm-enable-multibyte)
2845                  (mm-disable-multibyte))
2846                (insert content)
2847                ;; resolve cid contents
2848                (let ((case-fold-search t)
2849                      cid-file)
2850                  (goto-char (point-min))
2851                  (while (re-search-forward "\
2852 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2853                                            nil t)
2854                    (unless cid-dir
2855                      (setq cid-dir (mm-make-temp-file "cid" t))
2856                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2857                    (setq file nil
2858                          content nil)
2859                    (when (setq cid-file
2860                                (gnus-article-browse-html-save-cid-content
2861                                 (match-string 2)
2862                                 (with-current-buffer gnus-article-buffer
2863                                   gnus-article-mime-handles)
2864                                 cid-dir))
2865                      (when (eq system-type 'cygwin)
2866                        (setq cid-file
2867                              (concat "/" (substring
2868                                           (with-output-to-string
2869                                             (call-process "cygpath" nil
2870                                                           standard-output
2871                                                           nil "-m" cid-file))
2872                                           0 -1))))
2873                      (replace-match (concat "file://" cid-file)
2874                                     nil nil nil 1))))
2875                (unless content (setq content (buffer-string))))
2876              (when (or charset header (not file))
2877                (setq tmp-file (mm-make-temp-file
2878                                ;; Do we need to care for 8.3 filenames?
2879                                "mm-" nil ".html")))
2880              ;; Add a meta html tag to specify charset and a header.
2881              (cond
2882               (header
2883                (let (title eheader body hcharset coding)
2884                  (with-temp-buffer
2885                    (mm-enable-multibyte)
2886                    (setq case-fold-search t)
2887                    (insert header "\n")
2888                    (setq title (message-fetch-field "subject"))
2889                    (goto-char (point-min))
2890                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|\\(&\\)\\|\n"
2891                                              nil t)
2892                      (replace-match (cond ((match-beginning 1) "&lt;")
2893                                           ((match-beginning 2) "&gt;")
2894                                           ((match-beginning 3) "&amp;")
2895                                           (t "<br>\n"))))
2896                    (goto-char (point-min))
2897                    (while (re-search-forward "^[\t ]+" nil t)
2898                      (dotimes (i (prog1
2899                                      (current-column)
2900                                    (delete-region (match-beginning 0)
2901                                                   (match-end 0))))
2902                        (insert "&nbsp;")))
2903                    (goto-char (point-min))
2904                    (insert "<div align=\"left\">\n")
2905                    (goto-char (point-max))
2906                    (insert "</div>\n<hr>\n")
2907                    ;; We have to examine charset one by one since
2908                    ;; charset specified in parts might be different.
2909                    (if (eq charset 'gnus-decoded)
2910                        (setq charset 'utf-8
2911                              eheader (mm-encode-coding-string (buffer-string)
2912                                                               charset)
2913                              title (when title
2914                                      (mm-encode-coding-string title charset))
2915                              body (mm-encode-coding-string content charset))
2916                      (setq hcharset (mm-find-mime-charset-region (point-min)
2917                                                                  (point-max)))
2918                      (cond ((= (length hcharset) 1)
2919                             (setq hcharset (car hcharset)
2920                                   coding (mm-charset-to-coding-system
2921                                           hcharset nil t)))
2922                            ((> (length hcharset) 1)
2923                             (setq hcharset 'utf-8
2924                                   coding hcharset)))
2925                      (if coding
2926                          (if charset
2927                              (progn
2928                                (setq body
2929                                      (mm-charset-to-coding-system charset
2930                                                                   nil t))
2931                                (if (eq coding body)
2932                                    (setq eheader (mm-encode-coding-string
2933                                                   (buffer-string) coding)
2934                                          title (when title
2935                                                  (mm-encode-coding-string
2936                                                   title coding))
2937                                          body content)
2938                                  (setq charset 'utf-8
2939                                        eheader (mm-encode-coding-string
2940                                                 (buffer-string) charset)
2941                                        title (when title
2942                                                (mm-encode-coding-string
2943                                                 title charset))
2944                                        body (mm-encode-coding-string
2945                                              (mm-decode-coding-string
2946                                               content body)
2947                                              charset))))
2948                            (setq charset hcharset
2949                                  eheader (mm-encode-coding-string
2950                                           (buffer-string) coding)
2951                                  title (when title
2952                                          (mm-encode-coding-string
2953                                           title coding))
2954                                  body content))
2955                        (setq eheader (mm-string-as-unibyte (buffer-string))
2956                              body content)))
2957                    (erase-buffer)
2958                    (mm-disable-multibyte)
2959                    (insert body)
2960                    (when charset
2961                      (mm-add-meta-html-tag handle charset t))
2962                    (when title
2963                      (goto-char (point-min))
2964                      (unless (search-forward "<title>" nil t)
2965                        (re-search-forward "<head>\\s-*" nil t)
2966                        (insert "<title>" title "</title>\n")))
2967                    (goto-char (point-min))
2968                    (or (re-search-forward
2969                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2970                        (re-search-forward
2971                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2972                    (insert eheader)
2973                    (mm-write-region (point-min) (point-max)
2974                                     tmp-file nil nil nil 'binary t))))
2975               (charset
2976                (mm-with-unibyte-buffer
2977                  (insert (if (eq charset 'gnus-decoded)
2978                              (mm-encode-coding-string content
2979                                                       (setq charset 'utf-8))
2980                            content))
2981                  (if (or (mm-add-meta-html-tag handle charset)
2982                          (not file))
2983                      (mm-write-region (point-min) (point-max)
2984                                       tmp-file nil nil nil 'binary t)
2985                    (setq tmp-file nil))))
2986               (tmp-file
2987                (mm-save-part-to-file handle tmp-file)))
2988              (when tmp-file
2989                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2990              (add-hook 'gnus-summary-prepare-exit-hook
2991                        'gnus-article-browse-delete-temp-files)
2992              (add-hook 'gnus-exit-gnus-hook
2993                        (lambda  ()
2994                          (gnus-article-browse-delete-temp-files t)))
2995              ;; FIXME: Warn if there's an <img> tag?
2996              (browse-url-of-file (or tmp-file (expand-file-name file)))
2997              (setq showed t))
2998             ;; If multipart, recurse
2999             ((equal (mm-handle-media-supertype handle) "multipart")
3000              (when (gnus-article-browse-html-parts handle header)
3001                (setq showed t)))
3002             ((equal (mm-handle-media-type handle) "message/rfc822")
3003              (mm-with-multibyte-buffer
3004                (mm-insert-part handle)
3005                (setq handle (mm-dissect-buffer t t))
3006                (when (and (bufferp (car handle))
3007                           (stringp (car (mm-handle-type handle))))
3008                  (setq handle (list handle)))
3009                (when header
3010                  (article-decode-encoded-words)
3011                  (let ((gnus-visible-headers
3012                         (or (get 'gnus-visible-headers 'standard-value)
3013                             gnus-visible-headers)))
3014                    (article-hide-headers))
3015                  (goto-char (point-min))
3016                  (search-forward "\n\n" nil 'move)
3017                  (skip-chars-backward "\t\n ")
3018                  (setq header (buffer-substring (point-min) (point)))))
3019              (when (prog1
3020                        (gnus-article-browse-html-parts handle header)
3021                      (mm-destroy-parts handle))
3022                (setq showed t)))))
3023     showed))
3024
3025 (defun gnus-article-browse-html-article (&optional arg)
3026   "View \"text/html\" parts of the current article with a WWW browser.
3027 Inline images embedded in a message using the cid scheme, as they are
3028 generally considered to be safe, will be processed properly.
3029 The message header is added to the beginning of every html part unless
3030 the prefix argument ARG is given.
3031
3032 Warning: Spammers use links to images (using the http scheme) in HTML
3033 articles to verify whether you have read the message.  As
3034 `gnus-article-browse-html-article' passes the HTML content to the
3035 browser without eliminating these \"web bugs\" you should only
3036 use it for mails from trusted senders.
3037
3038 If you always want to display HTML parts in the browser, set
3039 `mm-text-html-renderer' to nil.
3040
3041 This command creates temporary files to pass HTML contents including
3042 images if any to the browser, and deletes them when exiting the group
3043 \(if you want)."
3044   ;; Cf. `mm-w3m-safe-url-regexp'
3045   (interactive "P")
3046   (if arg
3047       (gnus-summary-show-article)
3048     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3049                                     gnus-visible-headers))
3050           ;; As we insert a <hr>, there's no need for the body boundary.
3051           (gnus-treat-body-boundary nil))
3052       (gnus-summary-show-article)))
3053   (with-current-buffer gnus-article-buffer
3054     (let ((header (unless arg
3055                     (save-restriction
3056                       (widen)
3057                       (buffer-substring-no-properties
3058                        (goto-char (point-min))
3059                        (if (search-forward "\n\n" nil t)
3060                            (match-beginning 0)
3061                          (goto-char (point-max))
3062                          (skip-chars-backward "\t\n ")
3063                          (point))))))
3064           parts)
3065       (set-buffer gnus-original-article-buffer)
3066       (setq parts (mm-dissect-buffer t t))
3067       ;; If singlepart, enforce a list.
3068       (when (and (bufferp (car parts))
3069                  (stringp (car (mm-handle-type parts))))
3070         (setq parts (list parts)))
3071       ;; Process the list
3072       (unless (gnus-article-browse-html-parts parts header)
3073         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3074       (mm-destroy-parts parts)
3075       (unless arg
3076         (gnus-summary-show-article)))))
3077
3078 (defun article-hide-list-identifiers ()
3079   "Remove list identifies from the Subject header.
3080 The `gnus-list-identifiers' variable specifies what to do."
3081   (interactive)
3082   (let ((inhibit-point-motion-hooks t)
3083         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3084         (inhibit-read-only t))
3085     (when regexp
3086       (save-excursion
3087         (save-restriction
3088           (article-narrow-to-head)
3089           (goto-char (point-min))
3090           (while (re-search-forward
3091                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3092                   nil t)
3093             (delete-region (match-beginning 2) (match-end 0))
3094             (beginning-of-line))
3095           (when (re-search-forward
3096                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3097             (delete-region (match-beginning 1) (match-end 1))))))))
3098
3099 (defun article-hide-pem (&optional arg)
3100   "Toggle hiding of any PEM headers and signatures in the current article.
3101 If given a negative prefix, always show; if given a positive prefix,
3102 always hide."
3103   (interactive (gnus-article-hidden-arg))
3104   (unless (gnus-article-check-hidden-text 'pem arg)
3105     (save-excursion
3106       (let ((inhibit-read-only t) end)
3107         (goto-char (point-min))
3108         ;; Hide the horrendously ugly "header".
3109         (when (and (search-forward
3110                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3111                     nil t)
3112                    (setq end (1+ (match-beginning 0))))
3113           (gnus-add-wash-type 'pem)
3114           (gnus-article-hide-text-type
3115            end
3116            (if (search-forward "\n\n" nil t)
3117                (match-end 0)
3118              (point-max))
3119            'pem)
3120           ;; Hide the trailer as well
3121           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3122                                 nil t)
3123             (gnus-article-hide-text-type
3124              (match-beginning 0) (match-end 0) 'pem)))))))
3125
3126 (defun article-strip-banner ()
3127   "Strip the banners specified by the `banner' group parameter and by
3128 `gnus-article-address-banner-alist'."
3129   (interactive)
3130   (save-excursion
3131     (save-restriction
3132       (let ((inhibit-point-motion-hooks t))
3133         (when (gnus-parameter-banner gnus-newsgroup-name)
3134           (article-really-strip-banner
3135            (gnus-parameter-banner gnus-newsgroup-name)))
3136         (when gnus-article-address-banner-alist
3137           ;; Note that the From header is decoded here, so it is
3138           ;; required that the *-extract-address-components function
3139           ;; supports non-ASCII text.
3140           (let ((from (save-restriction
3141                         (widen)
3142                         (article-narrow-to-head)
3143                         (mail-fetch-field "from"))))
3144             (when (and from
3145                        (setq from
3146                              (cadr (funcall gnus-extract-address-components
3147                                             from))))
3148               (catch 'found
3149                 (dolist (pair gnus-article-address-banner-alist)
3150                   (when (string-match (car pair) from)
3151                     (throw 'found
3152                            (article-really-strip-banner (cdr pair)))))))))))))
3153
3154 (defun article-really-strip-banner (banner)
3155   "Strip the banner specified by the argument."
3156   (save-excursion
3157     (save-restriction
3158       (let ((inhibit-point-motion-hooks t)
3159             (gnus-signature-limit nil)
3160             (inhibit-read-only t))
3161         (article-goto-body)
3162         (cond
3163          ((eq banner 'signature)
3164           (when (gnus-article-narrow-to-signature)
3165             (widen)
3166             (forward-line -1)
3167             (delete-region (point) (point-max))))
3168          ((symbolp banner)
3169           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3170               (while (re-search-forward banner nil t)
3171                 (delete-region (match-beginning 0) (match-end 0)))))
3172          ((stringp banner)
3173           (while (re-search-forward banner nil t)
3174             (delete-region (match-beginning 0) (match-end 0)))))))))
3175
3176 (defun article-babel ()
3177   "Translate article using an online translation service."
3178   (interactive)
3179   (require 'babel)
3180   (gnus-with-article-buffer
3181     (when (article-goto-body)
3182       (let* ((start (point))
3183              (end (point-max))
3184              (orig (buffer-substring start end))
3185              (trans (babel-as-string orig)))
3186         (save-restriction
3187           (narrow-to-region start end)
3188           (delete-region start end)
3189           (insert trans))))))
3190
3191 (defun article-hide-signature (&optional arg)
3192   "Hide the signature in the current article.
3193 If given a negative prefix, always show; if given a positive prefix,
3194 always hide."
3195   (interactive (gnus-article-hidden-arg))
3196   (unless (gnus-article-check-hidden-text 'signature arg)
3197     (save-excursion
3198       (save-restriction
3199         (let ((inhibit-read-only t))
3200           (when (gnus-article-narrow-to-signature)
3201             (gnus-article-hide-text-type
3202              (point-min) (point-max) 'signature))))))
3203   (gnus-set-mode-line 'article))
3204
3205 (defun article-strip-headers-in-body ()
3206   "Strip offensive headers from bodies."
3207   (interactive)
3208   (save-excursion
3209     (article-goto-body)
3210     (let ((case-fold-search t))
3211       (when (looking-at "x-no-archive:")
3212         (gnus-delete-line)))))
3213
3214 (defun article-strip-leading-blank-lines ()
3215   "Remove all blank lines from the beginning of the article."
3216   (interactive)
3217   (save-excursion
3218     (let ((inhibit-point-motion-hooks t)
3219           (inhibit-read-only t))
3220       (when (article-goto-body)
3221         (while (and (not (eobp))
3222                     (looking-at "[ \t]*$"))
3223           (gnus-delete-line))))))
3224
3225 (defun article-narrow-to-head ()
3226   "Narrow the buffer to the head of the message.
3227 Point is left at the beginning of the narrowed-to region."
3228   (narrow-to-region
3229    (goto-char (point-min))
3230    (cond
3231     ;; Absolutely no headers displayed.
3232     ((looking-at "\n")
3233      (point))
3234     ;; Normal headers.
3235     ((search-forward "\n\n" nil 1)
3236      (1- (point)))
3237     ;; Nothing but headers.
3238     (t
3239      (point-max))))
3240   (goto-char (point-min)))
3241
3242 (defun article-goto-body ()
3243   "Place point at the start of the body."
3244   (goto-char (point-min))
3245   (cond
3246    ;; This variable is only bound when dealing with separate
3247    ;; MIME body parts.
3248    (article-goto-body-goes-to-point-min-p
3249     t)
3250    ((search-forward "\n\n" nil t)
3251     t)
3252    (t
3253     (goto-char (point-max))
3254     nil)))
3255
3256 (defun article-strip-multiple-blank-lines ()
3257   "Replace consecutive blank lines with one empty line."
3258   (interactive)
3259   (save-excursion
3260     (let ((inhibit-point-motion-hooks t)
3261           (inhibit-read-only t))
3262       ;; First make all blank lines empty.
3263       (article-goto-body)
3264       (while (re-search-forward "^[ \t]+$" nil t)
3265         (unless (gnus-annotation-in-region-p
3266                  (match-beginning 0) (match-end 0))
3267           (replace-match "" nil t)))
3268       ;; Then replace multiple empty lines with a single empty line.
3269       (article-goto-body)
3270       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3271         (unless (gnus-annotation-in-region-p
3272                  (match-beginning 0) (match-end 0))
3273           (delete-region (match-beginning 1) (match-end 1)))))))
3274
3275 (defun article-strip-leading-space ()
3276   "Remove all white space from the beginning of the lines in the article."
3277   (interactive)
3278   (save-excursion
3279     (let ((inhibit-point-motion-hooks t)
3280           (inhibit-read-only t))
3281       (article-goto-body)
3282       (while (re-search-forward "^[ \t]+" nil t)
3283         (replace-match "" t t)))))
3284
3285 (defun article-strip-trailing-space ()
3286   "Remove all white space from the end of the lines in the article."
3287   (interactive)
3288   (save-excursion
3289     (let ((inhibit-point-motion-hooks t)
3290           (inhibit-read-only t))
3291       (article-goto-body)
3292       (while (re-search-forward "[ \t]+$" nil t)
3293         (replace-match "" t t)))))
3294
3295 (defun article-strip-blank-lines ()
3296   "Strip leading, trailing and multiple blank lines."
3297   (interactive)
3298   (article-strip-leading-blank-lines)
3299   (article-remove-trailing-blank-lines)
3300   (article-strip-multiple-blank-lines))
3301
3302 (defun article-strip-all-blank-lines ()
3303   "Strip all blank lines."
3304   (interactive)
3305   (save-excursion
3306     (let ((inhibit-point-motion-hooks t)
3307           (inhibit-read-only t))
3308       (article-goto-body)
3309       (while (re-search-forward "^[ \t]*\n" nil t)
3310         (replace-match "" t t)))))
3311
3312 (defun gnus-article-narrow-to-signature ()
3313   "Narrow to the signature; return t if a signature is found, else nil."
3314   (let ((inhibit-point-motion-hooks t))
3315     (when (gnus-article-search-signature)
3316       (forward-line 1)
3317       ;; Check whether we have some limits to what we consider
3318       ;; to be a signature.
3319       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3320                       (list gnus-signature-limit)))
3321             limit limited)
3322         (while (setq limit (pop limits))
3323           (if (or (and (integerp limit)
3324                        (< (- (point-max) (point)) limit))
3325                   (and (floatp limit)
3326                        (< (count-lines (point) (point-max)) limit))
3327                   (and (functionp limit)
3328                        (funcall limit))
3329                   (and (stringp limit)
3330                        (not (re-search-forward limit nil t))))
3331               ()                        ; This limit did not succeed.
3332             (setq limited t
3333                   limits nil)))
3334         (unless limited
3335           (narrow-to-region (point) (point-max))
3336           t)))))
3337
3338 (defun gnus-article-search-signature ()
3339   "Search the current buffer for the signature separator.
3340 Put point at the beginning of the signature separator."
3341   (let ((cur (point)))
3342     (goto-char (point-max))
3343     (if (if (stringp gnus-signature-separator)
3344             (re-search-backward gnus-signature-separator nil t)
3345           (let ((seps gnus-signature-separator))
3346             (while (and seps
3347                         (not (re-search-backward (car seps) nil t)))
3348               (pop seps))
3349             seps))
3350         t
3351       (goto-char cur)
3352       nil)))
3353
3354 (defun gnus-article-hidden-arg ()
3355   "Return the current prefix arg as a number, or 0 if no prefix."
3356   (list (if current-prefix-arg
3357             (prefix-numeric-value current-prefix-arg)
3358           0)))
3359
3360 (defun gnus-article-check-hidden-text (type arg)
3361   "Return nil if hiding is necessary.
3362 Arg can be nil or a number.  nil and positive means hide, negative
3363 means show, 0 means toggle."
3364   (save-excursion
3365     (save-restriction
3366       (let ((hide (gnus-article-hidden-text-p type)))
3367         (cond
3368          ((or (null arg)
3369               (> arg 0))
3370           nil)
3371          ((< arg 0)
3372           (gnus-article-show-hidden-text type)
3373           t)
3374          (t
3375           (if (eq hide 'hidden)
3376               (progn
3377                 (gnus-article-show-hidden-text type)
3378                 t)
3379             nil)))))))
3380
3381 (defun gnus-article-hidden-text-p (type)
3382   "Say whether the current buffer contains hidden text of type TYPE."
3383   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3384     (while (and pos
3385                 (not (get-text-property pos 'invisible))
3386                 (not (get-text-property pos 'dummy-invisible)))
3387       (setq pos
3388             (text-property-any (1+ pos) (point-max) 'article-type type)))
3389     (if pos
3390         'hidden
3391       nil)))
3392
3393 (defun gnus-article-show-hidden-text (type &optional dummy)
3394   "Show all hidden text of type TYPE.
3395 Originally it is hide instead of DUMMY."
3396   (let ((inhibit-read-only t)
3397         (inhibit-point-motion-hooks t))
3398     (gnus-remove-text-properties-when
3399      'article-type type
3400      (point-min) (point-max)
3401      (cons 'article-type (cons type
3402                                gnus-hidden-properties)))
3403     (gnus-delete-wash-type type)))
3404
3405 (defconst article-time-units
3406   `((year . ,(* 365.25 24 60 60))
3407     (week . ,(* 7 24 60 60))
3408     (day . ,(* 24 60 60))
3409     (hour . ,(* 60 60))
3410     (minute . 60)
3411     (second . 1))
3412   "Mapping from time units to seconds.")
3413
3414 (defun gnus-article-forward-header ()
3415   "Move point to the start of the next header.
3416 If the current header is a continuation header, this can be several
3417 lines forward."
3418   (let ((ended nil))
3419     (while (not ended)
3420       (forward-line 1)
3421       (if (looking-at "[ \t]+[^ \t]")
3422           (forward-line 1)
3423         (setq ended t)))))
3424
3425 (defun article-treat-date ()
3426   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3427                        (with-current-buffer gnus-summary-buffer
3428                          gnus-article-date-headers)
3429                      gnus-article-date-headers)
3430                    t))
3431
3432 (defun article-date-ut (&optional type highlight date-position)
3433   "Convert DATE date to TYPE in the current article.
3434 The default type is `ut'.  See `gnus-article-date-headers' for
3435 possible values."
3436   (interactive (list 'ut t))
3437   (let* ((case-fold-search t)
3438          (inhibit-read-only t)
3439          (inhibit-point-motion-hooks t)
3440          (first t)
3441          (visible-date (mail-fetch-field "Date"))
3442          pos date bface eface)
3443     (save-excursion
3444       (if date-position
3445           (progn
3446             (goto-char date-position)
3447             (setq date (get-text-property (point) 'original-date))
3448             (when (looking-at "[^:]+:[\t ]*")
3449               (setq bface (get-text-property (match-beginning 0) 'face)
3450                     eface (get-text-property (match-end 0) 'face)))
3451             (delete-region (point)
3452                            (progn
3453                              (gnus-article-forward-header)
3454                              (point)))
3455             (article-transform-date date type bface eface))
3456         (save-restriction
3457           (widen)
3458           (goto-char (point-min))
3459           (while (or (get-text-property (setq pos (point)) 'original-date)
3460                      (and (setq pos (next-single-property-change
3461                                      (point) 'original-date))
3462                           (goto-char pos)))
3463             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3464                                       (1+ (match-beginning 0))
3465                                     (point-max)))
3466             (while (setq pos (text-property-not-all pos (point-max)
3467                                                     'gnus-date-type nil))
3468               (setq date (get-text-property pos 'original-date))
3469               (goto-char pos)
3470               (when (looking-at "[^:]+:[\t ]*")
3471                 (setq bface (get-text-property (match-beginning 0) 'face)
3472                       eface (get-text-property (match-end 0) 'face)))
3473               (delete-region pos (or (text-property-any pos (point-max)
3474                                                         'gnus-date-type nil)
3475                                      (point-max))))
3476             (unless date ;; the 1st time
3477               (goto-char (point-min))
3478               (while (re-search-forward "^Date:[\t ]*" nil t)
3479                 (setq date (get-text-property (match-beginning 0)
3480                                               'original-date)
3481                       bface (get-text-property (match-beginning 0) 'face)
3482                       eface (get-text-property (match-end 0) 'face))
3483                 (delete-region (point-at-bol) (progn
3484                                                 (gnus-article-forward-header)
3485                                                 (point)))))
3486             (when (and (not date)
3487                        visible-date)
3488               (setq date visible-date))
3489             (when date
3490               (article-transform-date date type bface eface))
3491             (goto-char (point-max))
3492             (widen)))))))
3493
3494 (defun article-transform-date (date type bface eface)
3495   (dolist (this-type (cond
3496                       ((null type)
3497                        (list 'ut))
3498                       ((atom type)
3499                        (list type))
3500                       (t
3501                        type)))
3502     (goto-char
3503      (prog1
3504          (point)
3505        (add-text-properties
3506         (point)
3507         (progn
3508           (insert (article-make-date-line date (or this-type 'ut)) "\n")
3509           (point))
3510         (list 'original-date date 'gnus-date-type this-type))))
3511     ;; Do highlighting.
3512     (when (looking-at
3513            "\\([^:]+:\\)[\t ]*\\(\\(?:[^\t\n ]+[\t ]+\\)*[^\t\n ]+\\)?")
3514       (put-text-property (match-beginning 1) (match-end 1) 'face bface)
3515       (when (match-beginning 2)
3516         (put-text-property (match-beginning 2) (match-end 2) 'face eface))
3517       (while (and (zerop (forward-line 1))
3518                   (looking-at "[\t ]+\\(\\(?:[^\t\n ]+[\t ]+\\)*[^\t\n ]+\\)?"))
3519         (when (match-beginning 1)
3520           (put-text-property (match-beginning 1) (match-end 1) 'face eface))))))
3521
3522 (defun article-make-date-line (date type)
3523   "Return a DATE line of TYPE."
3524   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3525                              combined-lapsed))
3526     (error "Unknown conversion type: %s" type))
3527   (condition-case ()
3528       (let ((time (ignore-errors (date-to-time date))))
3529         (cond
3530          ;; Convert to the local timezone.
3531          ((eq type 'local)
3532           (concat "Date: " (message-make-date time)))
3533          ;; Convert to Universal Time.
3534          ((eq type 'ut)
3535           (concat "Date: "
3536                   (substring
3537                    (message-make-date
3538                     (let* ((e (parse-time-string date))
3539                            (tm (apply 'encode-time e))
3540                            (ms (car tm))
3541                            (ls (- (cadr tm) (car (current-time-zone time)))))
3542                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3543                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3544                             (t (list ms ls)))))
3545                    0 -5)
3546                   "UT"))
3547          ;; Get the original date from the article.
3548          ((eq type 'original)
3549           (concat "Date: " (if (string-match "\n+$" date)
3550                                (substring date 0 (match-beginning 0))
3551                              date)))
3552          ;; Let the user define the format.
3553          ((eq type 'user-defined)
3554           (let ((format (or (condition-case nil
3555                                 (with-current-buffer gnus-summary-buffer
3556                                   gnus-article-time-format)
3557                               (error nil))
3558                             gnus-article-time-format)))
3559             (if (functionp format)
3560                 (funcall format time)
3561               (concat "Date: " (format-time-string format time)))))
3562          ;; ISO 8601.
3563          ((eq type 'iso8601)
3564           (let ((tz (car (current-time-zone time))))
3565             (concat
3566              "Date: "
3567              (format-time-string "%Y%m%dT%H%M%S" time)
3568              (format "%s%02d%02d"
3569                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3570                      (/ (% (abs tz) 3600) 60)))))
3571          ;; Do a lapsed format.
3572          ((eq type 'lapsed)
3573           (concat "Date: " (article-lapsed-string time)))
3574          ;; A combined date/lapsed format.
3575          ((eq type 'combined-lapsed)
3576           (let ((date-string (article-make-date-line date 'original))
3577                 (segments 3)
3578                 lapsed-string)
3579             (while (and
3580                     time
3581                     (setq lapsed-string
3582                           (concat " (" (article-lapsed-string time segments) ")"))
3583                     (> (+ (length date-string)
3584                           (length lapsed-string))
3585                        (+ fill-column 6))
3586                     (> segments 0))
3587               (setq segments (1- segments)))
3588             (if (> segments 0)
3589                 (concat date-string lapsed-string)
3590               date-string)))
3591          ;; Display the date in proper English
3592          ((eq type 'english)
3593           (let ((dtime (decode-time time)))
3594             (concat
3595              "Date: the "
3596              (number-to-string (nth 3 dtime))
3597              (let ((digit (% (nth 3 dtime) 10)))
3598                (cond
3599                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3600                 ((= digit 1) "st")
3601                 ((= digit 2) "nd")
3602                 ((= digit 3) "rd")
3603                 (t "th")))
3604              " of "
3605              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3606              " "
3607              (number-to-string (nth 5 dtime))
3608              " at "
3609              (format "%02d" (nth 2 dtime))
3610              ":"
3611              (format "%02d" (nth 1 dtime)))))))
3612     (foo
3613      (format "Date: %s (from Gnus)" date))))
3614
3615 (defun article-lapsed-string (time &optional max-segments)
3616   ;; If the date is seriously mangled, the timezone functions are
3617   ;; liable to bug out, so we ignore all errors.
3618   (let* ((now (current-time))
3619          (real-time (subtract-time now time))
3620          (real-sec (and real-time
3621                         (+ (* (float (car real-time)) 65536)
3622                            (cadr real-time))))
3623          (sec (and real-time (abs real-sec)))
3624          (segments 0)
3625          num prev)
3626     (unless max-segments
3627       (setq max-segments (length article-time-units)))
3628     (cond
3629      ((null real-time)
3630       "Unknown")
3631      ((zerop sec)
3632       "Now")
3633      (t
3634       (concat
3635        ;; This is a bit convoluted, but basically we go
3636        ;; through the time units for years, weeks, etc,
3637        ;; and divide things to see whether that results
3638        ;; in positive answers.
3639        (mapconcat
3640         (lambda (unit)
3641           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3642                   (>= segments max-segments))
3643               ;; The (remaining) seconds are too few to
3644               ;; be divided into this time unit.
3645               ""
3646             ;; It's big enough, so we output it.
3647             (setq sec (- sec (* num (cdr unit))))
3648             (prog1
3649                 (concat (if prev ", " "") (int-to-string
3650                                            (floor num))
3651                         " " (symbol-name (car unit))
3652                         (if (> num 1) "s" ""))
3653               (setq prev t
3654                     segments (1+ segments)))))
3655         article-time-units "")
3656        ;; If dates are odd, then it might appear like the
3657        ;; article was sent in the future.
3658        (if (> real-sec 0)
3659            " ago"
3660          " in the future"))))))
3661
3662 (defun article-date-local (&optional highlight)
3663   "Convert the current article date to the local timezone."
3664   (interactive (list t))
3665   (article-date-ut 'local highlight))
3666
3667 (defun article-date-english (&optional highlight)
3668   "Convert the current article date to something that is proper English."
3669   (interactive (list t))
3670   (article-date-ut 'english highlight))
3671
3672 (defun article-date-original (&optional highlight)
3673   "Convert the current article date to what it was originally.
3674 This is only useful if you have used some other date conversion
3675 function and want to see what the date was before converting."
3676   (interactive (list t))
3677   (article-date-ut 'original highlight))
3678
3679 (defun article-date-lapsed (&optional highlight)
3680   "Convert the current article date to time lapsed since it was sent."
3681   (interactive (list t))
3682   (article-date-ut 'lapsed highlight))
3683
3684 (defun article-date-combined-lapsed (&optional highlight)
3685   "Convert the current article date to time lapsed since it was sent."
3686   (interactive (list t))
3687   (article-date-ut 'combined-lapsed highlight))
3688
3689 (defun article-update-date-lapsed ()
3690   "Function to be run from a timer to update the lapsed time line."
3691   (save-match-data
3692     (let ((buffer (current-buffer)))
3693       (ignore-errors
3694         (walk-windows
3695          (lambda (w)
3696            (set-buffer (window-buffer w))
3697            (when (derived-mode-p 'gnus-article-mode)
3698              (let ((old-line (count-lines (point-min) (point)))
3699                    (old-column (- (point) (line-beginning-position)))
3700                    (window-start (window-start w))
3701                    (pos (point-min))
3702                    type next end)
3703                (while (setq pos (text-property-not-all pos (point-max)
3704                                                        'gnus-date-type nil))
3705                  (setq next (or (next-single-property-change pos
3706                                                              'gnus-date-type)
3707                                 (point-max)))
3708                  (setq type (get-text-property pos 'gnus-date-type))
3709                  (when (memq type '(lapsed combined-lapsed user-defined))
3710                    (article-date-ut type t pos)
3711                    (setq end (or (next-single-property-change pos
3712                                                               'gnus-date-type)
3713                                  (point-max)))
3714                    (when window-start
3715                      (if (/= window-start next)
3716                          (setq window-start nil)
3717                        (set-window-start w end)))
3718                    (setq next end))
3719                  (setq pos next))
3720                (goto-char (point-min))
3721                (when (> old-column 0)
3722                  (setq old-line (1- old-line)))
3723                (forward-line old-line)
3724                (end-of-line)
3725                (when (> (current-column) old-column)
3726                  (beginning-of-line)
3727                  (forward-char old-column)))))
3728          nil 'visible))
3729       (set-buffer buffer))))
3730
3731 (defun gnus-start-date-timer (&optional n)
3732   "Start a timer to update the Date headers in the article buffers.
3733 The numerical prefix says how frequently (in seconds) the function
3734 is to run."
3735   (interactive "p")
3736   (unless n
3737     (setq n 1))
3738   (gnus-stop-date-timer)
3739   (setq article-lapsed-timer
3740         (run-at-time 1 n 'article-update-date-lapsed)))
3741
3742 (defun gnus-stop-date-timer ()
3743   "Stop the Date timer."
3744   (interactive)
3745   (when article-lapsed-timer
3746     (nnheader-cancel-timer article-lapsed-timer)
3747     (setq article-lapsed-timer nil)))
3748
3749 (defun article-date-user (&optional highlight)
3750   "Convert the current article date to the user-defined format.
3751 This format is defined by the `gnus-article-time-format' variable."
3752   (interactive (list t))
3753   (article-date-ut 'user highlight))
3754
3755 (defun article-date-iso8601 (&optional highlight)
3756   "Convert the current article date to ISO8601."
3757   (interactive (list t))
3758   (article-date-ut 'iso8601 highlight))
3759
3760 (defmacro gnus-article-save-original-date (&rest forms)
3761   "Save the original date as a text property and evaluate FORMS."
3762   `(let* ((case-fold-search t)
3763           (start (progn
3764                    (goto-char (point-min))
3765                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3766                               (not (bolp)))
3767                      (match-end 0))))
3768           (date (when (and start
3769                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3770                                               nil t))
3771                   (buffer-substring-no-properties start
3772                                                   (match-beginning 0)))))
3773      (goto-char (point-max))
3774      (skip-chars-backward "\n")
3775      (put-text-property (point-min) (point) 'original-date date)
3776      ,@forms
3777      (goto-char (point-max))
3778      (skip-chars-backward "\n")
3779      (put-text-property (point-min) (point) 'original-date date)))
3780
3781 ;; (defun article-show-all ()
3782 ;;   "Show all hidden text in the article buffer."
3783 ;;   (interactive)
3784 ;;   (save-excursion
3785 ;;     (let ((inhibit-read-only t))
3786 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3787
3788 (defun article-remove-leading-whitespace ()
3789   "Remove excessive whitespace from all headers."
3790   (interactive)
3791   (save-excursion
3792     (save-restriction
3793       (let ((inhibit-read-only t))
3794         (article-narrow-to-head)
3795         (goto-char (point-min))
3796         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3797           (delete-region (match-beginning 1) (match-end 1)))))))
3798
3799 (defun article-emphasize (&optional arg)
3800   "Emphasize text according to `gnus-emphasis-alist'."
3801   (interactive (gnus-article-hidden-arg))
3802   (unless (gnus-article-check-hidden-text 'emphasis arg)
3803     (save-excursion
3804       (let ((alist (or
3805                     (condition-case nil
3806                         (with-current-buffer gnus-summary-buffer
3807                           gnus-article-emphasis-alist)
3808                       (error))
3809                     gnus-emphasis-alist))
3810             (inhibit-read-only t)
3811             (props (append '(article-type emphasis)
3812                            gnus-hidden-properties))
3813             regexp elem beg invisible visible face)
3814         (article-goto-body)
3815         (setq beg (point))
3816         (while (setq elem (pop alist))
3817           (goto-char beg)
3818           (setq regexp (car elem)
3819                 invisible (nth 1 elem)
3820                 visible (nth 2 elem)
3821                 face (nth 3 elem))
3822           (while (re-search-forward regexp nil t)
3823             (when (and (match-beginning visible) (match-beginning invisible))
3824               (gnus-article-hide-text
3825                (match-beginning invisible) (match-end invisible) props)
3826               (gnus-article-unhide-text-type
3827                (match-beginning visible) (match-end visible) 'emphasis)
3828               (gnus-put-overlay-excluding-newlines
3829                (match-beginning visible) (match-end visible) 'face face)
3830               (gnus-add-wash-type 'emphasis)
3831               (goto-char (match-end invisible)))))))))
3832
3833 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3834   "Setup newsgroup emphasis alist."
3835   (unless gnus-article-emphasis-alist
3836     (let ((name (and gnus-newsgroup-name
3837                      (gnus-group-real-name gnus-newsgroup-name))))
3838       (make-local-variable 'gnus-article-emphasis-alist)
3839       (setq gnus-article-emphasis-alist
3840             (nconc
3841              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3842                (while (setq elem (pop alist))
3843                  (when (and name (string-match (car elem) name))
3844                    (setq alist nil
3845                          highlight (copy-sequence (cdr elem)))))
3846                highlight)
3847              (copy-sequence highlight-words)
3848              (if gnus-newsgroup-name
3849                  (copy-sequence (gnus-group-find-parameter
3850                                  gnus-newsgroup-name 'highlight-words t)))
3851              gnus-emphasis-alist)))))
3852
3853 (defvar gnus-summary-article-menu)
3854 (defvar gnus-summary-post-menu)
3855
3856 ;;; Saving functions.
3857
3858 (defun gnus-article-save (save-buffer file &optional num)
3859   "Save the currently selected article."
3860   (when (or (get gnus-default-article-saver :headers)
3861             (not gnus-save-all-headers))
3862     ;; Remove headers according to `gnus-saved-headers' or the value
3863     ;; of the `:headers' property that the saver function might have.
3864     (let ((gnus-visible-headers
3865            (or (symbol-value (get gnus-default-article-saver :headers))
3866                gnus-saved-headers gnus-visible-headers))
3867           ;; Ignore group parameter.  See `article-hide-headers'.
3868           (gnus-summary-buffer nil))
3869       (with-current-buffer save-buffer
3870         (article-hide-headers 1 t))))
3871   (save-window-excursion
3872     (if (not gnus-default-article-saver)
3873         (error "No default saver is defined")
3874       ;; !!! Magic!  The saving functions all save
3875       ;; `gnus-save-article-buffer' (or so they think), but we
3876       ;; bind that variable to our save-buffer.
3877       (set-buffer gnus-article-buffer)
3878       (let* ((gnus-save-article-buffer save-buffer)
3879              (filename
3880               (cond
3881                ((not gnus-prompt-before-saving) 'default)
3882                ((eq gnus-prompt-before-saving 'always) nil)
3883                (t file)))
3884              (gnus-number-of-articles-to-be-saved
3885               (when (eq gnus-prompt-before-saving t)
3886                 num)))                  ; Magic
3887         (set-buffer gnus-article-current-summary)
3888         (funcall gnus-default-article-saver filename)))))
3889
3890 (defun gnus-read-save-file-name (prompt &optional filename
3891                                         function group headers variable
3892                                         dir-var)
3893   (let ((default-name
3894           (funcall function group headers (symbol-value variable)))
3895         result)
3896     (setq result
3897           (expand-file-name
3898            (cond
3899             ((eq filename 'default)
3900              default-name)
3901             ((eq filename t)
3902              default-name)
3903             (filename filename)
3904             (t
3905              (when (symbol-value dir-var)
3906                (setq default-name (expand-file-name
3907                                    (file-name-nondirectory default-name)
3908                                    (symbol-value dir-var))))
3909              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3910                     (prompt
3911                      (format prompt
3912                              (if (and gnus-number-of-articles-to-be-saved
3913                                       (> gnus-number-of-articles-to-be-saved 1))
3914                                  (format "these %d articles"
3915                                          gnus-number-of-articles-to-be-saved)
3916                                "this article")))
3917                     (file
3918                      ;; Let the split methods have their say.
3919                      (cond
3920                       ;; No split name was found.
3921                       ((null split-name)
3922                        (read-file-name
3923                         (concat prompt " (default "
3924                                 (file-name-nondirectory default-name) "): ")
3925                         (file-name-directory default-name)
3926                         default-name))
3927                       ;; A single group name is returned.
3928                       ((stringp split-name)
3929                        (setq default-name
3930                              (funcall function split-name headers
3931                                       (symbol-value variable)))
3932                        (read-file-name
3933                         (concat prompt " (default "
3934                                 (file-name-nondirectory default-name) "): ")
3935                         (file-name-directory default-name)
3936                         default-name))
3937                       ;; A single split name was found
3938                       ((= 1 (length split-name))
3939                        (let* ((name (expand-file-name
3940                                      (car split-name)
3941                                      gnus-article-save-directory))
3942                               (dir (cond ((file-directory-p name)
3943                                           (file-name-as-directory name))
3944                                          ((file-exists-p name) name)
3945                                          (t gnus-article-save-directory))))
3946                          (read-file-name
3947                           (concat prompt " (default " name "): ")
3948                           dir name)))
3949                       ;; A list of splits was found.
3950                       (t
3951                        (setq split-name (nreverse split-name))
3952                        (let (result)
3953                          (let ((file-name-history
3954                                 (nconc split-name file-name-history)))
3955                            (setq result
3956                                  (expand-file-name
3957                                   (read-file-name
3958                                    (concat prompt " (`M-p' for defaults): ")
3959                                    gnus-article-save-directory
3960                                    (car split-name))
3961                                   gnus-article-save-directory)))
3962                          (car (push result file-name-history)))))))
3963                ;; Create the directory.
3964                (gnus-make-directory (file-name-directory file))
3965                ;; If we have read a directory, we append the default file name.
3966                (when (file-directory-p file)
3967                  (setq file (expand-file-name (file-name-nondirectory
3968                                                default-name)
3969                                               (file-name-as-directory file))))
3970                ;; Possibly translate some characters.
3971                (nnheader-translate-file-chars file))))))
3972     (gnus-make-directory (file-name-directory result))
3973     (when variable
3974       (set variable result))
3975     (when dir-var
3976       (set dir-var (file-name-directory result)))
3977     result))
3978
3979 (defun gnus-article-archive-name (group)
3980   "Return the first instance of an \"Archive-name\" in the current buffer."
3981   (let ((case-fold-search t))
3982     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3983       (nnheader-concat gnus-article-save-directory
3984                        (match-string 1)))))
3985
3986 (defun gnus-article-nndoc-name (group)
3987   "If GROUP is an nndoc group, return the name of the parent group."
3988   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3989     (gnus-group-get-parameter group 'save-article-group)))
3990
3991 (defun gnus-summary-save-in-rmail (&optional filename)
3992   "Append this article to Rmail file.
3993 Optional argument FILENAME specifies file name.
3994 Directory to save to is default to `gnus-article-save-directory'."
3995   (setq filename (gnus-read-save-file-name
3996                   "Save %s in rmail file" filename
3997                   gnus-rmail-save-name gnus-newsgroup-name
3998                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3999   (with-current-buffer gnus-save-article-buffer
4000     (save-excursion
4001       (save-restriction
4002         (widen)
4003         ;; Note that unlike gnus-summary-save-in-mail, there is no
4004         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
4005         ;; not use Babyl.
4006         (gnus-output-to-rmail filename))))
4007   filename)
4008
4009 (defun gnus-summary-save-in-mail (&optional filename)
4010   "Append this article to Unix mail file.
4011 Optional argument FILENAME specifies file name.
4012 Directory to save to is default to `gnus-article-save-directory'."
4013   (setq filename (gnus-read-save-file-name
4014                   "Save %s in Unix mail file" filename
4015                   gnus-mail-save-name gnus-newsgroup-name
4016                   gnus-current-headers 'gnus-newsgroup-last-mail))
4017   (with-current-buffer gnus-save-article-buffer
4018     (save-excursion
4019       (save-restriction
4020         (widen)
4021         (if (and (file-readable-p filename)
4022                  (file-regular-p filename)
4023                  (mail-file-babyl-p filename))
4024             (gnus-output-to-rmail filename)
4025           (gnus-output-to-mail filename)))))
4026   filename)
4027
4028 (put 'gnus-summary-save-in-file :decode t)
4029 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4030 (defun gnus-summary-save-in-file (&optional filename overwrite)
4031   "Append this article to file.
4032 Optional argument FILENAME specifies file name.
4033 Directory to save to is default to `gnus-article-save-directory'."
4034   (setq filename (gnus-read-save-file-name
4035                   "Save %s in file" filename
4036                   gnus-file-save-name gnus-newsgroup-name
4037                   gnus-current-headers 'gnus-newsgroup-last-file))
4038   (with-current-buffer gnus-save-article-buffer
4039     (save-excursion
4040       (save-restriction
4041         (widen)
4042         (when (and overwrite
4043                    (file-exists-p filename))
4044           (delete-file filename))
4045         (gnus-output-to-file filename))))
4046   filename)
4047
4048 (put 'gnus-summary-write-to-file :decode t)
4049 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4050 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4051 (defun gnus-summary-write-to-file (&optional filename)
4052   "Write this article to a file, overwriting it if the file exists.
4053 Optional argument FILENAME specifies file name.
4054 The directory to save in defaults to `gnus-article-save-directory'."
4055   (setq filename (gnus-read-save-file-name
4056                   "Save %s in file" filename
4057                   gnus-file-save-name gnus-newsgroup-name
4058                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4059   (gnus-summary-save-in-file filename t))
4060
4061 (put 'gnus-summary-save-body-in-file :decode t)
4062 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4063   "Append this article body to a file.
4064 Optional argument FILENAME specifies file name.
4065 The directory to save in defaults to `gnus-article-save-directory'."
4066   (setq filename (gnus-read-save-file-name
4067                   "Save %s body in file" filename
4068                   gnus-file-save-name gnus-newsgroup-name
4069                   gnus-current-headers 'gnus-newsgroup-last-file))
4070   (with-current-buffer gnus-save-article-buffer
4071     (save-excursion
4072       (save-restriction
4073         (widen)
4074         (when (article-goto-body)
4075           (narrow-to-region (point) (point-max)))
4076         (when (and overwrite
4077                    (file-exists-p filename))
4078           (delete-file filename))
4079         (gnus-output-to-file filename))))
4080   filename)
4081
4082 (put 'gnus-summary-write-body-to-file :decode t)
4083 (put 'gnus-summary-write-body-to-file
4084      :function 'gnus-summary-save-body-in-file)
4085 (defun gnus-summary-write-body-to-file (&optional filename)
4086   "Write this article body to a file, overwriting it if the file exists.
4087 Optional argument FILENAME specifies file name.
4088 The directory to save in defaults to `gnus-article-save-directory'."
4089   (setq filename (gnus-read-save-file-name
4090                   "Save %s body in file" filename
4091                   gnus-file-save-name gnus-newsgroup-name
4092                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4093   (gnus-summary-save-body-in-file filename t))
4094
4095 (put 'gnus-summary-save-in-pipe :decode t)
4096 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4097 (defun gnus-summary-save-in-pipe (&optional command raw)
4098   "Pipe this article to subprocess COMMAND.
4099 Valid values for COMMAND include:
4100   a string
4101     The executable command name and possibly arguments.
4102   nil
4103     You will be prompted for the command in the minibuffer.
4104   the symbol `default'
4105     It will be replaced with the command which the variable
4106     `gnus-summary-pipe-output-default-command' holds or the command
4107     last used for saving.
4108 Non-nil value for RAW overrides `:decode' and `:headers' properties
4109 and the raw article including all headers will be piped."
4110   (let ((article (gnus-summary-article-number))
4111         (decode (unless raw
4112                   (get 'gnus-summary-save-in-pipe :decode)))
4113         save-buffer default)
4114     (if article
4115         (if (vectorp (gnus-summary-article-header article))
4116             (save-current-buffer
4117               (gnus-summary-select-article decode decode nil article)
4118               (insert-buffer-substring
4119                (prog1
4120                    (if decode
4121                        gnus-article-buffer
4122                      gnus-original-article-buffer)
4123                  (setq save-buffer
4124                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4125               ;; Remove unwanted headers.
4126               (when (and (not raw)
4127                          (or (get 'gnus-summary-save-in-pipe :headers)
4128                              (not gnus-save-all-headers)))
4129                 (let ((gnus-visible-headers
4130                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4131                                               :headers))
4132                            gnus-saved-headers gnus-visible-headers))
4133                       (gnus-summary-buffer nil))
4134                   (article-hide-headers 1 t))))
4135           (error "%d is not a real article" article))
4136       (error "No article to pipe"))
4137     (setq default (or gnus-summary-pipe-output-default-command
4138                       gnus-last-shell-command))
4139     (unless (stringp command)
4140       (setq command
4141             (if (and (eq command 'default) default)
4142                 default
4143               (gnus-read-shell-command "Shell command on this article: "
4144                                        default))))
4145     (when (string-equal command "")
4146       (if default
4147           (setq command default)
4148         (error "A command is required")))
4149     (with-current-buffer save-buffer
4150       (save-restriction
4151         (widen)
4152         (shell-command-on-region (point-min) (point-max) command nil)))
4153     (gnus-kill-buffer save-buffer))
4154   (setq gnus-summary-pipe-output-default-command command))
4155
4156 (defun gnus-summary-pipe-to-muttprint (&optional command)
4157   "Pipe this article to muttprint."
4158   (unless (stringp command)
4159     (setq command (read-string
4160                    "Print using command: " gnus-summary-muttprint-program
4161                    nil gnus-summary-muttprint-program)))
4162   (let ((gnus-summary-pipe-output-default-command
4163          gnus-summary-pipe-output-default-command))
4164     (gnus-summary-save-in-pipe command))
4165   (setq gnus-summary-muttprint-program command))
4166
4167 ;;; Article file names when saving.
4168
4169 (defun gnus-capitalize-newsgroup (newsgroup)
4170   "Capitalize NEWSGROUP name."
4171   (when (not (zerop (length newsgroup)))
4172     (concat (char-to-string (upcase (aref newsgroup 0)))
4173             (substring newsgroup 1))))
4174
4175 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4176   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4177 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4178 Otherwise, it is like ~/News/news/group/num."
4179   (let ((default
4180           (expand-file-name
4181            (concat (if (gnus-use-long-file-name 'not-save)
4182                        (gnus-capitalize-newsgroup newsgroup)
4183                      (gnus-newsgroup-directory-form newsgroup))
4184                    "/" (int-to-string (mail-header-number headers)))
4185            gnus-article-save-directory)))
4186     (if (and last-file
4187              (string-equal (file-name-directory default)
4188                            (file-name-directory last-file))
4189              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4190         default
4191       (or last-file default))))
4192
4193 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4194   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4195 If variable `gnus-use-long-file-name' is non-nil, it is
4196 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4197   (let ((default
4198           (expand-file-name
4199            (concat (if (gnus-use-long-file-name 'not-save)
4200                        newsgroup
4201                      (gnus-newsgroup-directory-form newsgroup))
4202                    "/" (int-to-string (mail-header-number headers)))
4203            gnus-article-save-directory)))
4204     (if (and last-file
4205              (string-equal (file-name-directory default)
4206                            (file-name-directory last-file))
4207              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4208         default
4209       (or last-file default))))
4210
4211 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4212   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4213 If variable `gnus-use-long-file-name' is non-nil, it is
4214 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4215   (or last-file
4216       (expand-file-name
4217        (if (gnus-use-long-file-name 'not-save)
4218            newsgroup
4219          (file-relative-name
4220           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4221           default-directory))
4222        gnus-article-save-directory)))
4223
4224 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4225   "Generate file name from sender."
4226   (let ((from (mail-header-from headers)))
4227     (expand-file-name
4228      (if (and from (string-match "\\([^ <]+\\)@" from))
4229          (match-string 1 from)
4230        "nobody")
4231      gnus-article-save-directory)))
4232
4233 (defun article-verify-x-pgp-sig ()
4234   "Verify X-PGP-Sig."
4235   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4236   (interactive)
4237   (if (gnus-buffer-live-p gnus-original-article-buffer)
4238       (let ((sig (with-current-buffer gnus-original-article-buffer
4239                    (gnus-fetch-field "X-PGP-Sig")))
4240             items info headers)
4241         (when (and sig
4242                    mml2015-use
4243                    (mml2015-clear-verify-function))
4244           (with-temp-buffer
4245             (insert-buffer-substring gnus-original-article-buffer)
4246             (setq items (split-string sig))
4247             (message-narrow-to-head)
4248             (let ((inhibit-point-motion-hooks t)
4249                   (case-fold-search t))
4250               ;; Don't verify multiple headers.
4251               (setq headers (mapconcat (lambda (header)
4252                                          (concat header ": "
4253                                                  (mail-fetch-field header)
4254                                                  "\n"))
4255                                        (split-string (nth 1 items) ",") "")))
4256             (delete-region (point-min) (point-max))
4257             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4258             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4259             (insert headers)
4260             (widen)
4261             (forward-line)
4262             (while (not (eobp))
4263               (if (looking-at "^-")
4264                   (insert "- "))
4265               (forward-line))
4266             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4267             (insert "Version: " (car items) "\n\n")
4268             (insert (mapconcat 'identity (cddr items) "\n"))
4269             (insert "\n-----END PGP SIGNATURE-----\n")
4270             (let ((mm-security-handle (list (format "multipart/signed"))))
4271               (mml2015-clean-buffer)
4272               (let ((coding-system-for-write (or gnus-newsgroup-charset
4273                                                  'iso-8859-1)))
4274                 (funcall (mml2015-clear-verify-function)))
4275               (setq info
4276                     (or (mm-handle-multipart-ctl-parameter
4277                          mm-security-handle 'gnus-details)
4278                         (mm-handle-multipart-ctl-parameter
4279                          mm-security-handle 'gnus-info)))))
4280           (when info
4281             (let ((inhibit-read-only t) bface eface)
4282               (save-restriction
4283                 (message-narrow-to-head)
4284                 (goto-char (point-max))
4285                 (forward-line -1)
4286                 (setq bface (get-text-property (point-at-bol) 'face)
4287                       eface (get-text-property (1- (point-at-eol)) 'face))
4288                 (message-remove-header "X-Gnus-PGP-Verify")
4289                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4290                     (forward-line)
4291                   (goto-char (point-max)))
4292                 (narrow-to-region (point) (point))
4293                 (insert "X-Gnus-PGP-Verify: " info "\n")
4294                 (goto-char (point-min))
4295                 (forward-line)
4296                 (while (not (eobp))
4297                   (if (not (looking-at "^[ \t]"))
4298                       (insert " "))
4299                   (forward-line))
4300                 ;; Do highlighting.
4301                 (goto-char (point-min))
4302                 (when (looking-at "\\([^:]+\\): *")
4303                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4304                                      'face bface)
4305                   (put-text-property (match-end 0) (point-max)
4306                                      'face eface)))))))))
4307
4308 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4309
4310 (defun article-verify-cancel-lock ()
4311   "Verify Cancel-Lock header."
4312   (interactive)
4313   (if (gnus-buffer-live-p gnus-original-article-buffer)
4314       (canlock-verify gnus-original-article-buffer)))
4315
4316 (eval-and-compile
4317   (mapc
4318    (lambda (func)
4319      (let (afunc gfunc)
4320        (if (consp func)
4321            (setq afunc (car func)
4322                  gfunc (cdr func))
4323          (setq afunc func
4324                gfunc (intern (format "gnus-%s" func))))
4325        (defalias gfunc
4326          (when (fboundp afunc)
4327            `(lambda (&optional interactive &rest args)
4328               ,(documentation afunc t)
4329               (interactive (list t))
4330               (with-current-buffer gnus-article-buffer
4331                 (if interactive
4332                     (call-interactively ',afunc)
4333                   (apply ',afunc args))))))))
4334    '(article-hide-headers
4335      article-verify-x-pgp-sig
4336      article-verify-cancel-lock
4337      article-hide-boring-headers
4338      article-treat-overstrike
4339      article-treat-ansi-sequences
4340      article-fill-long-lines
4341      article-capitalize-sentences
4342      article-remove-cr
4343      article-remove-leading-whitespace
4344      article-display-x-face
4345      article-display-face
4346      article-de-quoted-unreadable
4347      article-de-base64-unreadable
4348      article-decode-HZ
4349      article-wash-html
4350      article-unsplit-urls
4351      article-hide-list-identifiers
4352      article-strip-banner
4353      article-babel
4354      article-hide-pem
4355      article-hide-signature
4356      article-strip-headers-in-body
4357      article-remove-trailing-blank-lines
4358      article-strip-leading-blank-lines
4359      article-strip-multiple-blank-lines
4360      article-strip-leading-space
4361      article-strip-trailing-space
4362      article-strip-blank-lines
4363      article-strip-all-blank-lines
4364      article-date-local
4365      article-date-english
4366      article-date-iso8601
4367      article-date-original
4368      article-treat-date
4369      article-date-ut
4370      article-decode-mime-words
4371      article-decode-charset
4372      article-decode-encoded-words
4373      article-date-user
4374      article-date-lapsed
4375      article-date-combined-lapsed
4376      article-emphasize
4377      article-treat-dumbquotes
4378      article-treat-non-ascii
4379      article-normalize-headers
4380      ;;(article-show-all . gnus-article-show-all-headers)
4381      )))
4382 \f
4383 ;;;
4384 ;;; Gnus article mode
4385 ;;;
4386
4387 (put 'gnus-article-mode 'mode-class 'special)
4388
4389 (set-keymap-parent gnus-article-mode-map widget-keymap)
4390
4391 (gnus-define-keys gnus-article-mode-map
4392   " " gnus-article-goto-next-page
4393   [?\S-\ ] gnus-article-goto-prev-page
4394   "\177" gnus-article-goto-prev-page
4395   [delete] gnus-article-goto-prev-page
4396   "\C-c^" gnus-article-refer-article
4397   "h" gnus-article-show-summary
4398   "s" gnus-article-show-summary
4399   "\C-c\C-m" gnus-article-mail
4400   "?" gnus-article-describe-briefly
4401   "<" beginning-of-buffer
4402   ">" end-of-buffer
4403   "\C-c\C-i" gnus-info-find-node
4404   "\C-c\C-b" gnus-bug
4405   "R" gnus-article-reply-with-original
4406   "F" gnus-article-followup-with-original
4407   "\C-hk" gnus-article-describe-key
4408   "\C-hc" gnus-article-describe-key-briefly
4409   "\C-hb" gnus-article-describe-bindings
4410
4411   "e" gnus-article-read-summary-keys
4412   "\C-d" gnus-article-read-summary-keys
4413   "\M-*" gnus-article-read-summary-keys
4414   "\M-#" gnus-article-read-summary-keys
4415   "\M-^" gnus-article-read-summary-keys
4416   "\M-g" gnus-article-read-summary-keys)
4417
4418 (substitute-key-definition
4419  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4420
4421 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4422   "W" gnus-article-wide-reply-with-original)
4423 (if (featurep 'xemacs)
4424     (set-keymap-default-binding gnus-article-send-map
4425                                 'gnus-article-read-summary-send-keys)
4426   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4427
4428 (defun gnus-article-make-menu-bar ()
4429   (unless (boundp 'gnus-article-commands-menu)
4430     (gnus-summary-make-menu-bar))
4431   (unless (boundp 'gnus-article-article-menu)
4432     (easy-menu-define
4433      gnus-article-article-menu gnus-article-mode-map ""
4434      '("Article"
4435        ["Scroll forwards" gnus-article-goto-next-page t]
4436        ["Scroll backwards" gnus-article-goto-prev-page t]
4437        ["Show summary" gnus-article-show-summary t]
4438        ["Fetch Message-ID at point" gnus-article-refer-article t]
4439        ["Mail to address at point" gnus-article-mail t]
4440        ["Send a bug report" gnus-bug t]))
4441
4442     (easy-menu-define
4443      gnus-article-treatment-menu gnus-article-mode-map ""
4444      ;; Fixme: this should use :active (and maybe :visible).
4445      '("Treatment"
4446        ["Hide headers" gnus-article-hide-headers t]
4447        ["Hide signature" gnus-article-hide-signature t]
4448        ["Hide citation" gnus-article-hide-citation t]
4449        ["Treat overstrike" gnus-article-treat-overstrike t]
4450        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4451        ["Remove carriage return" gnus-article-remove-cr t]
4452        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4453        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4454        ["Remove base64" gnus-article-de-base64-unreadable t]
4455        ["Treat html" gnus-article-wash-html t]
4456        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4457        ["Decode HZ" gnus-article-decode-HZ t]))
4458
4459     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4460
4461     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4462
4463     (gnus-run-hooks 'gnus-article-menu-hook)))
4464
4465 (defvar bookmark-make-record-function)
4466 (defvar shr-put-image-function)
4467
4468 (define-derived-mode gnus-article-mode fundamental-mode "Article"
4469   "Major mode for displaying an article.
4470
4471 All normal editing commands are switched off.
4472
4473 The following commands are available in addition to all summary mode
4474 commands:
4475 \\<gnus-article-mode-map>
4476 \\[gnus-article-next-page]\t Scroll the article one page forwards
4477 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4478 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4479 \\[gnus-article-show-summary]\t Display the summary buffer
4480 \\[gnus-article-mail]\t Send a reply to the address near point
4481 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4482 \\[gnus-info-find-node]\t Go to the Gnus info node"
4483   (gnus-simplify-mode-line)
4484   (make-local-variable 'minor-mode-alist)
4485   (when (gnus-visual-p 'article-menu 'menu)
4486     (gnus-article-make-menu-bar)
4487     (when gnus-summary-tool-bar-map
4488       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4489   (gnus-update-format-specifications nil 'article-mode)
4490   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4491   (set (make-local-variable 'gnus-page-broken) nil)
4492   (make-local-variable 'gnus-article-current-summary)
4493   (make-local-variable 'gnus-article-mime-handles)
4494   (make-local-variable 'gnus-article-decoded-p)
4495   (make-local-variable 'gnus-article-mime-handle-alist)
4496   (make-local-variable 'gnus-article-wash-types)
4497   (make-local-variable 'gnus-article-image-alist)
4498   (make-local-variable 'gnus-article-charset)
4499   (make-local-variable 'gnus-article-ignored-charsets)
4500   (set (make-local-variable 'bookmark-make-record-function)
4501        'gnus-summary-bookmark-make-record)
4502   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4503   ;; face.
4504   (set (make-local-variable 'nobreak-char-display) nil)
4505   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4506   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4507   (setq cursor-in-non-selected-windows nil)
4508   (gnus-set-default-directory)
4509   (buffer-disable-undo)
4510   (setq buffer-read-only t
4511         show-trailing-whitespace nil)
4512   (mm-enable-multibyte))
4513
4514 (defun gnus-article-setup-buffer ()
4515   "Initialize the article buffer."
4516   (let* ((name (if gnus-single-article-buffer "*Article*"
4517                  (concat "*Article "
4518                          (gnus-group-decoded-name gnus-newsgroup-name)
4519                          "*")))
4520          (original
4521           (progn (string-match "\\*Article" name)
4522                  (concat " *Original Article"
4523                          (substring name (match-end 0))))))
4524     (setq gnus-article-buffer name)
4525     (setq gnus-original-article-buffer original)
4526     (setq gnus-article-mime-handle-alist nil)
4527     (with-current-buffer gnus-summary-buffer
4528       ;; This might be a variable local to the summary buffer.
4529       (unless gnus-single-article-buffer
4530         (setq gnus-article-buffer name)
4531         (setq gnus-original-article-buffer original)
4532         (gnus-set-global-variables)))
4533     (gnus-article-setup-highlight-words)
4534     ;; Init original article buffer.
4535     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4536       (mm-enable-multibyte)
4537       (setq major-mode 'gnus-original-article-mode)
4538       (make-local-variable 'gnus-original-article))
4539     (if (and (get-buffer name)
4540              (with-current-buffer name
4541                (if gnus-article-edit-mode
4542                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4543                        (progn
4544                          (set-buffer-modified-p nil)
4545                          (gnus-kill-buffer name)
4546                          (message "")
4547                          nil)
4548                      (error "Action aborted"))
4549                  t)))
4550         (let ((summary gnus-summary-buffer))
4551           (with-current-buffer name
4552             (set (make-local-variable 'gnus-article-edit-mode) nil)
4553             (gnus-article-stop-animations)
4554             (when gnus-article-mime-handles
4555               (mm-destroy-parts gnus-article-mime-handles)
4556               (setq gnus-article-mime-handles nil))
4557             ;; Set it to nil in article-buffer!
4558             (setq gnus-article-mime-handle-alist nil)
4559             (buffer-disable-undo)
4560             (setq buffer-read-only t)
4561             (unless (derived-mode-p 'gnus-article-mode)
4562               (gnus-article-mode))
4563             (set (make-local-variable 'gnus-summary-buffer) summary)
4564             (setq truncate-lines gnus-article-truncate-lines)
4565             (current-buffer)))
4566       (let ((summary gnus-summary-buffer))
4567         (with-current-buffer (gnus-get-buffer-create name)
4568           (gnus-article-mode)
4569           (setq truncate-lines gnus-article-truncate-lines)
4570           (set (make-local-variable 'gnus-summary-buffer) summary)
4571           (gnus-summary-set-local-parameters gnus-newsgroup-name)
4572           (when article-lapsed-timer
4573             (gnus-stop-date-timer))
4574           (when gnus-article-update-date-headers
4575             (gnus-start-date-timer gnus-article-update-date-headers))
4576           (current-buffer))))))
4577
4578 (defun gnus-article-stop-animations ()
4579   (dolist (timer (and (boundp 'timer-list)
4580                       timer-list))
4581     (when (eq (gnus-timer--function timer) 'image-animate-timeout)
4582       (cancel-timer timer))))
4583
4584 (defun gnus-stop-downloads ()
4585   (when (boundp 'url-queue)
4586     (set (intern "url-queue" obarray) nil)))
4587
4588 ;; Set article window start at LINE, where LINE is the number of lines
4589 ;; from the head of the article.
4590 (defun gnus-article-set-window-start (&optional line)
4591   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4592     (when article-window
4593       (set-window-start
4594        article-window
4595        (with-current-buffer gnus-article-buffer
4596          (goto-char (point-min))
4597          (if (not line)
4598              (point-min)
4599            (gnus-message 6 "Moved to bookmark")
4600            (search-forward "\n\n" nil t)
4601            (forward-line line)
4602            (point)))))))
4603
4604 (defun gnus-article-prepare (article &optional all-headers header)
4605   "Prepare ARTICLE in article mode buffer.
4606 ARTICLE should either be an article number or a Message-ID.
4607 If ARTICLE is an id, HEADER should be the article headers.
4608 If ALL-HEADERS is non-nil, no headers are hidden."
4609   (save-excursion
4610     ;; Make sure we start in a summary buffer.
4611     (unless (derived-mode-p 'gnus-summary-mode)
4612       (set-buffer gnus-summary-buffer))
4613     (setq gnus-summary-buffer (current-buffer))
4614     (let* ((gnus-article (if header (mail-header-number header) article))
4615            (summary-buffer (current-buffer))
4616            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4617            (group gnus-newsgroup-name)
4618            result)
4619       (save-excursion
4620         (gnus-article-setup-buffer)
4621         (set-buffer gnus-article-buffer)
4622         ;; Deactivate active regions.
4623         (when (and (boundp 'transient-mark-mode)
4624                    transient-mark-mode)
4625           (setq mark-active nil))
4626         (if (not (setq result (let ((inhibit-read-only t))
4627                                 (gnus-request-article-this-buffer
4628                                  article group))))
4629             ;; There is no such article.
4630             (save-excursion
4631               (when (and (numberp article)
4632                          (not (memq article gnus-newsgroup-sparse)))
4633                 (setq gnus-article-current
4634                       (cons gnus-newsgroup-name article))
4635                 (set-buffer gnus-summary-buffer)
4636                 (setq gnus-current-article article)
4637                 (if (and (memq article gnus-newsgroup-undownloaded)
4638                          (not (gnus-online (gnus-find-method-for-group
4639                                             gnus-newsgroup-name))))
4640                     (progn
4641                       (gnus-summary-set-agent-mark article)
4642                       (message "Message marked for downloading"))
4643                   (gnus-summary-mark-article article gnus-canceled-mark)
4644                   (unless (memq article gnus-newsgroup-sparse)
4645                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4646           (if (or (eq result 'pseudo)
4647                   (eq result 'nneething))
4648               (progn
4649                 (with-current-buffer summary-buffer
4650                   (push article gnus-newsgroup-history)
4651                   (setq gnus-last-article gnus-current-article
4652                         gnus-current-article 0
4653                         gnus-current-headers nil
4654                         gnus-article-current nil)
4655                   (if (eq result 'nneething)
4656                       (gnus-configure-windows 'summary)
4657                     (gnus-configure-windows 'article))
4658                   (gnus-set-global-variables))
4659                 (let ((gnus-article-mime-handle-alist-1
4660                        gnus-article-mime-handle-alist))
4661                   (gnus-set-mode-line 'article)))
4662             ;; The result from the `request' was an actual article -
4663             ;; or at least some text that is now displayed in the
4664             ;; article buffer.
4665             (when (and (numberp article)
4666                        (not (eq article gnus-current-article)))
4667               ;; Seems like a new article has been selected.
4668               ;; `gnus-current-article' must be an article number.
4669               (with-current-buffer summary-buffer
4670                 (push article gnus-newsgroup-history)
4671                 (setq gnus-last-article gnus-current-article
4672                       gnus-current-article article
4673                       gnus-current-headers
4674                       (gnus-summary-article-header gnus-current-article)
4675                       gnus-article-current
4676                       (cons gnus-newsgroup-name gnus-current-article))
4677                 (unless (vectorp gnus-current-headers)
4678                   (setq gnus-current-headers nil))
4679                 (gnus-summary-goto-subject gnus-current-article)
4680                 (when (gnus-summary-show-thread)
4681                   ;; If the summary buffer really was folded, the
4682                   ;; previous goto may not actually have gone to
4683                   ;; the right article, but the thread root instead.
4684                   ;; So we go again.
4685                   (gnus-summary-goto-subject gnus-current-article))
4686                 (gnus-run-hooks 'gnus-mark-article-hook)
4687                 (gnus-set-mode-line 'summary)
4688                 (when (gnus-visual-p 'article-highlight 'highlight)
4689                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4690                 ;; Set the global newsgroup variables here.
4691                 (gnus-set-global-variables)
4692                 (setq gnus-have-all-headers
4693                       (or all-headers gnus-show-all-headers))))
4694             (save-excursion
4695               (gnus-configure-windows 'article))
4696             (when (or (numberp article)
4697                       (stringp article))
4698               (gnus-article-prepare-display)
4699               ;; Do page break.
4700               (goto-char (point-min))
4701               (when gnus-break-pages
4702                 (gnus-narrow-to-page)))
4703             (let ((gnus-article-mime-handle-alist-1
4704                    gnus-article-mime-handle-alist))
4705               (gnus-set-mode-line 'article))
4706             (article-goto-body)
4707             (unless (bobp)
4708               (forward-line -1))
4709             (set-window-point (get-buffer-window (current-buffer)) (point))
4710             (gnus-configure-windows 'article)
4711             (gnus-run-hooks 'gnus-article-prepare-hook)
4712             t))))))
4713
4714 ;;;###autoload
4715 (defun gnus-article-prepare-display ()
4716   "Make the current buffer look like a nice article."
4717   ;; Hooks for getting information from the article.
4718   ;; This hook must be called before being narrowed.
4719   (let ((gnus-article-buffer (current-buffer))
4720         buffer-read-only
4721         (inhibit-read-only t))
4722     (unless (derived-mode-p 'gnus-article-mode)
4723       (gnus-article-mode))
4724     (setq buffer-read-only nil
4725           gnus-article-wash-types nil
4726           gnus-article-image-alist nil)
4727     (gnus-run-hooks 'gnus-tmp-internal-hook)
4728     (when gnus-display-mime-function
4729       (funcall gnus-display-mime-function))
4730     ;; Add attachment buttons to the header.
4731     (when gnus-mime-display-attachment-buttons-in-header
4732       (gnus-mime-buttonize-attachments-in-header))))
4733
4734 ;;;
4735 ;;; Gnus Sticky Article Mode
4736 ;;;
4737
4738 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4739   "Mode for sticky articles."
4740   ;; Release bindings that won't work.
4741   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4742                              gnus-sticky-article-mode-map)
4743   (substitute-key-definition 'gnus-article-refer-article 'undefined
4744                              gnus-sticky-article-mode-map)
4745   (dolist (k '("e" "h" "s" "F" "R"))
4746     (define-key gnus-sticky-article-mode-map k nil))
4747   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4748   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4749   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4750   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4751
4752 (defun gnus-sticky-article (arg)
4753   "Make the current article sticky.
4754 If a prefix ARG is given, ask for a name for this sticky article buffer."
4755   (interactive "P")
4756   (gnus-summary-show-thread)
4757   (gnus-summary-select-article nil nil 'pseudo)
4758   (let (new-art-buf-name)
4759     (gnus-eval-in-buffer-window gnus-article-buffer
4760       (setq new-art-buf-name
4761             (concat
4762              "*Sticky Article: "
4763              (if arg
4764                  (read-from-minibuffer "Sticky article buffer name: ")
4765                (gnus-with-article-headers
4766                  (gnus-article-goto-header "subject")
4767                  (setq new-art-buf-name
4768                        (buffer-substring-no-properties
4769                         (line-beginning-position) (line-end-position)))
4770                  (goto-char (point-min))
4771                  (gnus-article-goto-header "from")
4772                  (setq new-art-buf-name
4773                        (concat
4774                         new-art-buf-name ", "
4775                         (buffer-substring-no-properties
4776                          (line-beginning-position) (line-end-position))))
4777                  (goto-char (point-min))
4778                  (gnus-article-goto-header "date")
4779                  (setq new-art-buf-name
4780                        (concat
4781                         new-art-buf-name ", "
4782                         (buffer-substring-no-properties
4783                          (line-beginning-position) (line-end-position))))))
4784              "*"))
4785       (if (and (gnus-buffer-live-p new-art-buf-name)
4786                (with-current-buffer new-art-buf-name
4787                  (derived-mode-p 'gnus-sticky-article-mode)))
4788           (switch-to-buffer new-art-buf-name)
4789         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4790       (gnus-sticky-article-mode))
4791     (setq gnus-article-buffer new-art-buf-name))
4792   (gnus-summary-recenter)
4793   (gnus-summary-position-point))
4794
4795 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4796   "Kill the given sticky article BUFFER.
4797 If none is given, assume the current buffer and kill it if it has
4798 `gnus-sticky-article-mode'."
4799   (interactive)
4800   (unless buffer
4801     (setq buffer (current-buffer)))
4802   (with-current-buffer buffer
4803     (when (derived-mode-p 'gnus-sticky-article-mode)
4804       (gnus-kill-buffer buffer))))
4805
4806 (defun gnus-kill-sticky-article-buffers (arg)
4807   "Kill all sticky article buffers.
4808 If a prefix ARG is given, ask for confirmation."
4809   (interactive "P")
4810   (dolist (buf (gnus-buffers))
4811     (with-current-buffer buf
4812       (when (derived-mode-p 'gnus-sticky-article-mode)
4813         (if (not arg)
4814             (gnus-kill-buffer buf)
4815           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4816             (gnus-kill-buffer buf)))))))
4817
4818 ;;;
4819 ;;; Gnus MIME viewing functions
4820 ;;;
4821
4822 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4823   "Format of the MIME buttons.
4824
4825 Valid specifiers include:
4826 %t  The MIME type
4827 %T  MIME type, along with additional info
4828 %n  The `name' parameter
4829 %d  The description, if any
4830 %l  The length of the encoded part
4831 %p  The part identifier number
4832 %e  Dots if the part isn't displayed
4833
4834 General format specifiers can also be used.  See Info node
4835 `(gnus)Formatting Variables'.")
4836
4837 (defvar gnus-mime-button-line-format-alist
4838   '((?t gnus-tmp-type ?s)
4839     (?T gnus-tmp-type-long ?s)
4840     (?n gnus-tmp-name ?s)
4841     (?d gnus-tmp-description ?s)
4842     (?p gnus-tmp-id ?s)
4843     (?l gnus-tmp-length ?d)
4844     (?e gnus-tmp-dots ?s)))
4845
4846 (defvar gnus-mime-button-commands
4847   '((gnus-article-press-button "\r" "Toggle Display")
4848     (gnus-mime-view-part "v" "View Interactively...")
4849     (gnus-mime-view-part-as-type "t" "View As Type...")
4850     (gnus-mime-view-part-as-charset "C" "View As charset...")
4851     (gnus-mime-save-part "o" "Save...")
4852     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4853     (gnus-mime-replace-part "r" "Replace part")
4854     (gnus-mime-delete-part "d" "Delete part")
4855     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4856     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4857     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4858     (gnus-mime-view-part-externally "e" "View Externally")
4859     (gnus-mime-print-part "p" "Print")
4860     (gnus-mime-pipe-part "|" "Pipe To Command...")
4861     (gnus-mime-action-on-part "." "Take action on the part...")))
4862
4863 (defun gnus-article-mime-part-status ()
4864   (if gnus-article-mime-handle-alist-1
4865       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4866           " (1 part)"
4867         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4868     ""))
4869
4870 (defvar gnus-mime-button-map
4871   (let ((map (make-sparse-keymap)))
4872     (define-key map gnus-mouse-2 'gnus-article-push-button)
4873     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4874     (dolist (c gnus-mime-button-commands)
4875       (define-key map (cadr c) (car c)))
4876     map))
4877
4878 (easy-menu-define
4879   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4880   `("MIME Part"
4881     ,@(mapcar (lambda (c)
4882                 (vector (caddr c) (car c) :active t))
4883               gnus-mime-button-commands)))
4884
4885 (defvar gnus-url-button-commands
4886   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4887
4888 (defvar gnus-url-button-map
4889   (let ((map (make-sparse-keymap)))
4890     (dolist (c gnus-url-button-commands)
4891       (define-key map (cadr c) (car c)))
4892     map))
4893
4894 (easy-menu-define
4895   gnus-url-button-menu gnus-url-button-map "URL button menu."
4896   `("Url Button"
4897     ,@(mapcar (lambda (c)
4898                 (vector (caddr c) (car c) :active t))
4899               gnus-url-button-commands)))
4900
4901 (defmacro gnus-bind-safe-url-regexp (&rest body)
4902   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4903   `(let ((mm-w3m-safe-url-regexp
4904           (let ((group (if (and (derived-mode-p 'gnus-article-mode)
4905                                 (gnus-buffer-live-p
4906                                  gnus-article-current-summary))
4907                            (with-current-buffer gnus-article-current-summary
4908                              gnus-newsgroup-name)
4909                          gnus-newsgroup-name)))
4910             (if (cond ((not group)
4911                        ;; Maybe we're in a mml-preview buffer
4912                        ;; and no group is selected.
4913                        t)
4914                       ((stringp gnus-safe-html-newsgroups)
4915                        (string-match gnus-safe-html-newsgroups group))
4916                       ((consp gnus-safe-html-newsgroups)
4917                        (member group gnus-safe-html-newsgroups)))
4918                 nil
4919               mm-w3m-safe-url-regexp))))
4920      ,@body))
4921
4922 (defun gnus-mime-button-menu (event prefix)
4923  "Construct a context-sensitive menu of MIME commands."
4924  (interactive "e\nP")
4925  (save-window-excursion
4926    (let ((pos (event-start event)))
4927      (select-window (posn-window pos))
4928      (goto-char (posn-point pos))
4929      (gnus-article-check-buffer)
4930      (popup-menu gnus-mime-button-menu nil prefix))))
4931
4932 (defun gnus-mime-view-all-parts (&optional handles)
4933   "View all the MIME parts."
4934   (interactive)
4935   (with-current-buffer gnus-article-buffer
4936     (let ((handles (or handles gnus-article-mime-handles))
4937           (mail-parse-charset gnus-newsgroup-charset)
4938           (mail-parse-ignored-charsets
4939            (with-current-buffer gnus-summary-buffer
4940              gnus-newsgroup-ignored-charsets)))
4941       (when handles
4942         (mm-remove-parts handles)
4943         (goto-char (point-min))
4944         (or (search-forward "\n\n") (goto-char (point-max)))
4945         (let ((inhibit-read-only t))
4946           (delete-region (point) (point-max))
4947           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4948
4949 (defun gnus-article-jump-to-part (n)
4950   "Jump to MIME part N."
4951   (interactive "P")
4952   (let ((parts (with-current-buffer gnus-article-buffer
4953                  (length gnus-article-mime-handle-alist))))
4954     (when (zerop parts)
4955       (error "No such part"))
4956     (pop-to-buffer gnus-article-buffer)
4957     (or n
4958         (setq n (if (= parts 1)
4959                     1
4960                   (read-number (format "Jump to part (1..%s): " parts)))))
4961     (unless (and (integerp n) (<= n parts) (>= n 1))
4962       (setq n
4963             (progn
4964               (gnus-message 7 "Invalid part `%s', using %s instead."
4965                             n parts)
4966               parts)))
4967     (gnus-message 9 "Jumping to part %s." n)
4968     (cond ((>= gnus-auto-select-part 1)
4969            (while (and (<= n parts)
4970                        (not (gnus-article-goto-part n)))
4971              (setq n (1+ n))))
4972           ((< gnus-auto-select-part 0)
4973            (while (and (>= n 1)
4974                        (not (gnus-article-goto-part n)))
4975              (setq n (1- n))))
4976           (t
4977            (gnus-article-goto-part n)))))
4978
4979 (defvar gnus-mime-buttonized-part-id nil
4980   "ID of a mime part that should be buttonized.
4981 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4982
4983 (eval-when-compile
4984   (defsubst gnus-article-edit-part (handles &optional current-id)
4985     "Edit an article in order to delete a mime part.
4986 This function is exclusively used by `gnus-mime-save-part-and-strip'
4987 and `gnus-mime-delete-part', and not provided at run-time normally."
4988     (gnus-article-edit-article
4989      `(lambda ()
4990         (buffer-disable-undo)
4991         (erase-buffer)
4992         (let ((mail-parse-charset (or gnus-article-charset
4993                                       ',gnus-newsgroup-charset))
4994               (mail-parse-ignored-charsets
4995                (or gnus-article-ignored-charsets
4996                    ',gnus-newsgroup-ignored-charsets))
4997               (mbl mml-buffer-list))
4998           (setq mml-buffer-list nil)
4999           (insert-buffer-substring gnus-original-article-buffer)
5000           (mime-to-mml ',handles)
5001           (setq gnus-article-mime-handles nil)
5002           (let ((mbl1 mml-buffer-list))
5003             (setq mml-buffer-list mbl)
5004             (set (make-local-variable 'mml-buffer-list) mbl1))
5005           (gnus-make-local-hook 'kill-buffer-hook)
5006           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
5007      `(lambda (no-highlight)
5008         (let ((mail-parse-charset (or gnus-article-charset
5009                                       ',gnus-newsgroup-charset))
5010               (message-options message-options)
5011               (message-options-set-recipient)
5012               (mail-parse-ignored-charsets
5013                (or gnus-article-ignored-charsets
5014                    ',gnus-newsgroup-ignored-charsets)))
5015           (mml-to-mime)
5016           (mml-destroy-buffers)
5017           (remove-hook 'kill-buffer-hook
5018                        'mml-destroy-buffers t)
5019           (kill-local-variable 'mml-buffer-list))
5020         (gnus-summary-edit-article-done
5021          ,(or (mail-header-references gnus-current-headers) "")
5022          ,(gnus-group-read-only-p)
5023          ,gnus-summary-buffer no-highlight))
5024      t)
5025     ;; Force buttonizing this part.
5026     (let ((gnus-mime-buttonized-part-id current-id))
5027       (gnus-article-edit-done))
5028     (gnus-configure-windows 'article)
5029     (when (and current-id (integerp gnus-auto-select-part))
5030       (gnus-article-jump-to-part
5031        (min (max (+ current-id gnus-auto-select-part) 1)
5032             (with-current-buffer gnus-article-buffer
5033               (length gnus-article-mime-handle-alist)))))))
5034
5035 (defun gnus-mime-replace-part (file)
5036   "Replace MIME part under point with an external body."
5037   ;; Useful if file has already been saved to disk
5038   (interactive
5039    (list
5040     (read-file-name "Replace MIME part with file: "
5041                     (or mm-default-directory default-directory)
5042                     nil nil)))
5043   (gnus-mime-save-part-and-strip file))
5044
5045 (defun gnus-mime-save-part-and-strip (&optional file)
5046   "Save the MIME part under point then replace it with an external body.
5047 If FILE is given, use it for the external part."
5048   (interactive)
5049   (gnus-article-check-buffer)
5050   (when (gnus-group-read-only-p)
5051     (error "The current group does not support deleting of parts"))
5052   (when (mm-complicated-handles gnus-article-mime-handles)
5053     (error "\
5054 The current article has a complicated MIME structure, giving up..."))
5055   (let* ((data (get-text-property (point) 'gnus-data))
5056          (id (get-text-property (point) 'gnus-part))
5057          param
5058          (handles gnus-article-mime-handles))
5059     (unless file
5060       (setq file
5061             (and data (mm-save-part data "Delete MIME part and save to: "))))
5062     (when file
5063       (with-current-buffer (mm-handle-buffer data)
5064         (erase-buffer)
5065         (insert "Content-Type: " (mm-handle-media-type data))
5066         (mml-insert-parameter-string (cdr (mm-handle-type data))
5067                                      '(charset))
5068         ;; Add a filename for the sake of saving the part again.
5069         (mml-insert-parameter
5070          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5071         (insert "\n")
5072         (insert "Content-ID: " (message-make-message-id) "\n")
5073         (insert "Content-Transfer-Encoding: binary\n")
5074         (insert "\n"))
5075       (setcdr data
5076               (cdr (mm-make-handle nil
5077                                    `("message/external-body"
5078                                      (access-type . "LOCAL-FILE")
5079                                      (name . ,file)))))
5080       ;; (set-buffer gnus-summary-buffer)
5081       (gnus-article-edit-part handles id))))
5082
5083 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5084 ;; parts...>') but with stripping would be nice.
5085
5086 (defun gnus-mime-delete-part ()
5087   "Delete the MIME part under point.
5088 Replace it with some information about the removed part."
5089   (interactive)
5090   (gnus-article-check-buffer)
5091   (when (gnus-group-read-only-p)
5092     (error "The current group does not support deleting of parts"))
5093   (when (mm-complicated-handles gnus-article-mime-handles)
5094     (error "\
5095 The current article has a complicated MIME structure, giving up..."))
5096   (when (or gnus-expert-user
5097             (gnus-yes-or-no-p "\
5098 Deleting parts may malfunction or destroy the article; continue? "))
5099     (let* ((data (get-text-property (point) 'gnus-data))
5100            (id (get-text-property (point) 'gnus-part))
5101            (handles gnus-article-mime-handles)
5102            (description
5103             (let ((desc (mm-handle-description data)))
5104               (when desc
5105                 (mail-decode-encoded-word-string desc))))
5106            (filename (or (mm-handle-filename data) "(none)"))
5107            (type (mm-handle-media-type data)))
5108       (unless data
5109         (error "No MIME part under point"))
5110       (with-current-buffer (mm-handle-buffer data)
5111         (let ((bsize (buffer-size)))
5112           (erase-buffer)
5113           (insert
5114            (concat
5115             ",----\n"
5116             "| The following attachment has been deleted:\n"
5117             "|\n"
5118             "| Type:           " type "\n"
5119             "| Filename:       " filename "\n"
5120             "| Size (encoded): " (format "%s byte%s\n"
5121                                          bsize (if (= bsize 1)
5122                                                    ""
5123                                                  "s"))
5124             (when description
5125               (concat    "| Description:    " description "\n"))
5126             "`----\n"))
5127           (setcdr data
5128                   (cdr (mm-make-handle
5129                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5130                         (list "attachment")
5131                         (format "Deleted attachment (%s bytes)" bsize))))))
5132       ;; (set-buffer gnus-summary-buffer)
5133       (gnus-article-edit-part handles id))))
5134
5135 (defun gnus-mime-save-part ()
5136   "Save the MIME part under point."
5137   (interactive)
5138   (gnus-article-check-buffer)
5139   (let ((data (get-text-property (point) 'gnus-data)))
5140     (when data
5141       (mm-save-part data))))
5142
5143 (defun gnus-mime-pipe-part (&optional cmd)
5144   "Pipe the MIME part under point to a process.
5145 Use CMD as the process."
5146   (interactive)
5147   (gnus-article-check-buffer)
5148   (let ((data (get-text-property (point) 'gnus-data)))
5149     (when data
5150       (mm-pipe-part data cmd))))
5151
5152 (defun gnus-mime-view-part ()
5153   "Interactively choose a viewing method for the MIME part under point."
5154   (interactive)
5155   (gnus-article-check-buffer)
5156   (let ((data (get-text-property (point) 'gnus-data)))
5157     (when data
5158       (setq gnus-article-mime-handles
5159             (mm-merge-handles
5160              gnus-article-mime-handles (setq data (copy-sequence data))))
5161       (mm-interactively-view-part data))))
5162
5163 (defun gnus-mime-view-part-as-type-internal ()
5164   (gnus-article-check-buffer)
5165   (let* ((handle (get-text-property (point) 'gnus-data))
5166          (name (or
5167                 ;; Content-Type: foo/bar; name=...
5168                 (mail-content-type-get (mm-handle-type handle) 'name)
5169                 ;; Content-Disposition: attachment; filename=...
5170                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5171          (def-type (and name (mm-default-file-encoding name))))
5172     (or (and def-type (cons def-type 0))
5173         (and handle
5174              (equal (mm-handle-media-supertype handle) "text")
5175              '("text/plain" . 0))
5176         '("application/octet-stream" . 0))))
5177
5178 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5179   "Choose a MIME media type, and view the part as such.
5180 If non-nil, PRED is a predicate to use during completion to limit the
5181 available media-types."
5182   (interactive)
5183   (unless mime-type
5184     (setq mime-type
5185           (let ((default (gnus-mime-view-part-as-type-internal)))
5186             (gnus-completing-read
5187              "View as MIME type"
5188              (if pred
5189                  (gnus-remove-if-not pred (mailcap-mime-types))
5190                (mailcap-mime-types))
5191              nil nil nil
5192              (car default)))))
5193   (gnus-article-check-buffer)
5194   (let ((handle (get-text-property (point) 'gnus-data)))
5195     (when handle
5196       (when (equal (mm-handle-media-type handle) "message/external-body")
5197         (unless (mm-handle-cache handle)
5198           (mm-extern-cache-contents handle))
5199         (setq handle (mm-handle-cache handle)))
5200       (setq handle
5201             (mm-make-handle (mm-handle-buffer handle)
5202                             (cons mime-type (cdr (mm-handle-type handle)))
5203                             (mm-handle-encoding handle)
5204                             (mm-handle-undisplayer handle)
5205                             (mm-handle-disposition handle)
5206                             (mm-handle-description handle)
5207                             nil
5208                             (mm-handle-id handle)))
5209       (setq gnus-article-mime-handles
5210             (mm-merge-handles gnus-article-mime-handles handle))
5211       (when (mm-handle-displayed-p handle)
5212         (mm-remove-part handle))
5213       (gnus-mm-display-part handle))))
5214
5215 (defun gnus-mime-copy-part (&optional handle arg)
5216   "Put the MIME part under point into a new buffer.
5217 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5218 are decompressed."
5219   (interactive (list nil current-prefix-arg))
5220   (gnus-article-check-buffer)
5221   (unless handle
5222     (setq handle (get-text-property (point) 'gnus-data)))
5223   (when handle
5224     (let ((filename (mm-handle-filename handle))
5225           contents dont-decode charset coding-system)
5226       (mm-with-unibyte-buffer
5227         (mm-insert-part handle)
5228         (setq contents (or (condition-case nil
5229                                (mm-decompress-buffer filename nil 'sig)
5230                              (error
5231                               (setq dont-decode t)
5232                               nil))
5233                            (buffer-string))))
5234       (setq filename (cond (filename (file-name-nondirectory filename))
5235                            (dont-decode "*raw data*")
5236                            (t "*decoded*")))
5237       (cond
5238        (dont-decode)
5239        ((not arg)
5240         (unless (setq charset (mail-content-type-get
5241                                (mm-handle-type handle) 'charset))
5242           (unless (setq coding-system (mm-with-unibyte-buffer
5243                                         (insert contents)
5244                                         (mm-find-buffer-file-coding-system)))
5245             (setq charset gnus-newsgroup-charset))))
5246        ((numberp arg)
5247         (setq charset (or (cdr (assq arg
5248                                      gnus-summary-show-article-charset-alist))
5249                           (mm-read-coding-system "Charset: ")))))
5250       (switch-to-buffer (generate-new-buffer filename))
5251       (if (or coding-system
5252               (and charset
5253                    (setq coding-system (mm-charset-to-coding-system
5254                                         charset nil t))
5255                    (not (eq coding-system 'ascii))))
5256           (progn
5257             (mm-enable-multibyte)
5258             (insert (mm-decode-coding-string contents coding-system))
5259             (setq buffer-file-coding-system
5260                   (if (boundp 'last-coding-system-used)
5261                       (symbol-value 'last-coding-system-used)
5262                     coding-system)))
5263         (mm-disable-multibyte)
5264         (insert contents)
5265         (setq buffer-file-coding-system mm-binary-coding-system))
5266       ;; We do it this way to make `normal-mode' set the appropriate mode.
5267       (unwind-protect
5268           (progn
5269             (setq buffer-file-name (expand-file-name filename))
5270             (normal-mode))
5271         (setq buffer-file-name nil))
5272       (goto-char (point-min)))))
5273
5274 (defun gnus-mime-print-part (&optional handle filename)
5275   "Print the MIME part under point."
5276   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5277   (gnus-article-check-buffer)
5278   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5279          (contents (and handle (mm-get-part handle)))
5280          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5281          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5282     (when contents
5283         (if printer
5284             (unwind-protect
5285                 (progn
5286                   (mm-save-part-to-file handle file)
5287                   (call-process shell-file-name nil
5288                                 (generate-new-buffer " *mm*")
5289                                 nil
5290                                 shell-command-switch
5291                                 (mm-mailcap-command
5292                                  printer file (mm-handle-type handle))))
5293               (delete-file file))
5294           (with-temp-buffer
5295             (insert contents)
5296             (gnus-print-buffer))
5297           (ps-despool filename)))))
5298
5299 (defun gnus-mime-inline-part (&optional handle arg)
5300   "Insert the MIME part under point into the current buffer.
5301 Compressed files like .gz and .bz2 are decompressed."
5302   (interactive (list nil current-prefix-arg))
5303   (gnus-article-check-buffer)
5304   (let* ((inhibit-read-only t)
5305          (b (point))
5306          (btn ;; position where the MIME button exists
5307           (if handle
5308               (if (eq handle (get-text-property b 'gnus-data))
5309                   b
5310                 (article-goto-body)
5311                 (or (text-property-any (point) (point-max) 'gnus-data handle)
5312                     (text-property-any (point-min) (point) 'gnus-data handle)))
5313             (setq handle (get-text-property b 'gnus-data))
5314             b))
5315          contents charset coding-system)
5316     (when handle
5317       (when (= b (prog1
5318                      btn
5319                    (setq btn (previous-single-property-change
5320                               (or (next-single-property-change btn 'gnus-data)
5321                                   (point-max))
5322                               'gnus-data))))
5323         (setq b btn))
5324       (if (and (not arg) (mm-handle-undisplayer handle))
5325           (mm-remove-part handle)
5326         (mm-with-unibyte-buffer
5327           (mm-insert-part handle)
5328           (setq contents
5329                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5330                     (buffer-string))))
5331         (cond
5332          ((not arg)
5333           (unless (setq charset (mail-content-type-get
5334                                  (mm-handle-type handle) 'charset))
5335             (unless (setq coding-system
5336                           (mm-with-unibyte-buffer
5337                             (insert contents)
5338                             (mm-find-buffer-file-coding-system)))
5339               (setq charset gnus-newsgroup-charset))))
5340          ((numberp arg)
5341           (if (mm-handle-undisplayer handle)
5342               (mm-remove-part handle))
5343           (setq charset
5344                 (or (cdr (assq arg
5345                                gnus-summary-show-article-charset-alist))
5346                     (mm-read-coding-system "Charset: "))))
5347          ((mm-handle-undisplayer handle)
5348           (mm-remove-part handle)))
5349         (forward-line 1)
5350         (mm-display-inline handle))
5351       ;; Toggle the button appearance between `[button]...' and `[button]'.
5352       (goto-char btn)
5353       (gnus-insert-mime-button handle (get-text-property btn 'gnus-part)
5354                                (list (mm-handle-displayed-p handle)))
5355       (if (featurep 'emacs)
5356           (delete-region
5357            (point)
5358            (or (text-property-any (point) (point-max) 'gnus-data nil)
5359                (point-max)))
5360         (let* ((end (text-property-any (point) (point-max) 'gnus-data nil))
5361                (annots (annotations-at end)))
5362           (delete-region (point)
5363                          (if end
5364                              (if annots (1+ end) end)
5365                            (point-max)))
5366           (dolist (annot annots)
5367             (set-extent-endpoints annot (point) (point)))))
5368       (unless (search-backward "\n\n" nil t)
5369         ;; We're in the article header.
5370         (delete-char -1)
5371         (dolist (ovl (gnus-overlays-in btn (point)))
5372           (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
5373           (gnus-overlay-put ovl 'face nil))
5374         (save-restriction
5375           (message-narrow-to-field)
5376           (let ((gnus-treatment-function-alist
5377                  '((gnus-treat-highlight-headers
5378                     gnus-article-highlight-headers))))
5379             (gnus-treat-article 'head))))
5380       (goto-char b))))
5381
5382 (defun gnus-mime-set-charset-parameters (handle charset)
5383   "Set CHARSET to parameters in HANDLE.
5384 CHARSET may either be a string or a symbol."
5385   (unless (stringp charset)
5386     (setq charset (symbol-name charset)))
5387   (if (stringp (car handle))
5388       (dolist (h (cdr handle))
5389         (gnus-mime-set-charset-parameters h charset))
5390     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5391                                             "message/external-body")
5392                                      (progn
5393                                        (unless (mm-handle-cache handle)
5394                                          (mm-extern-cache-contents handle))
5395                                        (mm-handle-cache handle))
5396                                    handle)))
5397            (param (assq 'charset (cdr type))))
5398       (if param
5399           (setcdr param charset)
5400         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5401
5402 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5403   "Insert the MIME part under point into the current buffer using the
5404 specified charset."
5405   (interactive (list nil current-prefix-arg))
5406   (gnus-article-check-buffer)
5407   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5408         (fun (get-text-property (point) 'gnus-callback))
5409         (gnus-newsgroup-ignored-charsets 'gnus-all)
5410         charset form preferred parts)
5411     (when handle
5412       (when (prog1
5413                 (and fun
5414                      (setq charset
5415                            (or (cdr (assq
5416                                      arg
5417                                      gnus-summary-show-article-charset-alist))
5418                                (mm-read-coding-system "Charset: "))))
5419               (if (mm-handle-undisplayer handle)
5420                   (mm-remove-part handle)))
5421         (gnus-mime-set-charset-parameters handle charset)
5422         (when (and (consp (setq form (cdr-safe fun)))
5423                    (setq form (ignore-errors
5424                                 (assq 'gnus-mime-display-alternative form)))
5425                    (setq preferred (caddr form))
5426                    (progn
5427                      (when (eq (car preferred) 'quote)
5428                        (setq preferred (cadr preferred)))
5429                      (not (equal preferred
5430                                  (get-text-property (point) 'gnus-data))))
5431                    (setq parts (get-text-property (point) 'gnus-part))
5432                    (setq parts (cdr (assq parts
5433                                           gnus-article-mime-handle-alist)))
5434                    (equal (mm-handle-media-type parts) "multipart/alternative")
5435                    (setq parts (reverse (cdr parts))))
5436           (setcar (cddr form)
5437                   (list 'quote (or (cadr (member preferred parts))
5438                                    (car parts)))))
5439         (funcall fun handle)))))
5440
5441 (defun gnus-mime-view-part-externally (&optional handle)
5442   "View the MIME part under point with an external viewer."
5443   (interactive)
5444   (gnus-article-check-buffer)
5445   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5446          (mm-user-display-methods nil)
5447          (mm-inlined-types nil)
5448          (mail-parse-charset gnus-newsgroup-charset)
5449          (mail-parse-ignored-charsets
5450           (with-current-buffer gnus-summary-buffer
5451             gnus-newsgroup-ignored-charsets))
5452          (type (mm-handle-media-type handle))
5453          (method (mailcap-mime-info type))
5454          (mm-enable-external t))
5455     (if (not (stringp method))
5456         (gnus-mime-view-part-as-type
5457          nil (lambda (type) (stringp (mailcap-mime-info type))))
5458       (when handle
5459         (mm-display-part handle nil t)))))
5460
5461 (defun gnus-mime-view-part-internally (&optional handle)
5462   "View the MIME part under point with an internal viewer.
5463 If no internal viewer is available, use an external viewer."
5464   (interactive)
5465   (gnus-article-check-buffer)
5466   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5467          (mm-inlined-types '(".*"))
5468          (mm-inline-large-images t)
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          (inhibit-read-only t))
5474     (if (not (mm-inlinable-p handle))
5475         (gnus-mime-view-part-as-type
5476          nil (lambda (type) (mm-inlinable-p handle type)))
5477       (when handle
5478         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5479
5480 (defun gnus-mime-action-on-part (&optional action)
5481   "Do something with the MIME attachment at \(point\)."
5482   (interactive
5483    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5484   (gnus-article-check-buffer)
5485   (let ((action-pair (assoc action gnus-mime-action-alist)))
5486     (if action-pair
5487         (funcall (cdr action-pair)))))
5488
5489 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5490   "Call FUNCTION on MIME part N.
5491 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5492 If INTERACTIVE, call FUNCTION interactively."
5493   (let (window frame)
5494     ;; Check whether the article is displayed.
5495     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5496                  (setq window (get-buffer-window gnus-article-buffer t))
5497                  (frame-visible-p (setq frame (window-frame window))))
5498       (error "No article is displayed"))
5499     (with-current-buffer gnus-article-buffer
5500       ;; Check whether the article displays the right contents.
5501       (unless (with-current-buffer gnus-summary-buffer
5502                 (eq gnus-current-article (gnus-summary-article-number)))
5503         (error "You should select the right article first"))
5504       (if n
5505           (setq n (prefix-numeric-value n))
5506         (let ((pt (point)))
5507           (setq n (or (get-text-property pt 'gnus-part)
5508                       (and (not (bobp))
5509                            (get-text-property (1- pt) 'gnus-part))
5510                       (get-text-property (prog2
5511                                              (forward-line 1)
5512                                              (point)
5513                                            (goto-char pt))
5514                                          'gnus-part)
5515                       (get-text-property
5516                        (or (and (setq pt (previous-single-property-change
5517                                           pt 'gnus-part))
5518                                 (1- pt))
5519                            (next-single-property-change (point) 'gnus-part)
5520                            (point))
5521                        'gnus-part)
5522                       1))))
5523       ;; Check whether the specified part exists.
5524       (when (> n (length gnus-article-mime-handle-alist))
5525         (error "No such part")))
5526     (unless
5527         (progn
5528           ;; To select the window is needed so that the cursor
5529           ;; might be visible on the MIME button.
5530           (select-window (prog1
5531                              window
5532                            (setq window (selected-window))
5533                            ;; Article may be displayed in the other frame.
5534                            (gnus-select-frame-set-input-focus
5535                             (prog1
5536                                 frame
5537                               (setq frame (selected-frame))))))
5538           (when (gnus-article-goto-part n)
5539             ;; We point the cursor and the arrow at the MIME button
5540             ;; when the `function' prompt the user for something.
5541             (unless (and (pos-visible-in-window-p)
5542                          (> (count-lines (point) (window-end))
5543                             (/ (1- (window-height)) 3)))
5544               (recenter (/ (1- (window-height)) 3)))
5545             (let ((cursor-in-non-selected-windows t)
5546                   (overlay-arrow-string "=>")
5547                   (overlay-arrow-position (point-marker)))
5548               (unwind-protect
5549                   (cond
5550                    ((and no-handle interactive)
5551                     (call-interactively function))
5552                    (no-handle
5553                     (funcall function))
5554                    (interactive
5555                     (call-interactively
5556                      function (get-text-property (point) 'gnus-data)))
5557                    (t
5558                     (funcall function
5559                              (get-text-property (point) 'gnus-data))))
5560                 (set-marker overlay-arrow-position nil)
5561                 (unless gnus-auto-select-part
5562                   (gnus-select-frame-set-input-focus frame)
5563                   (select-window window))))
5564             t))
5565       (if gnus-inhibit-mime-unbuttonizing
5566           ;; This is the default though the program shouldn't reach here.
5567           (error "No such part")
5568         ;; The part which doesn't have the MIME button is selected.
5569         ;; So, we display all the buttons and redo it.
5570         (let ((gnus-inhibit-mime-unbuttonizing t))
5571           (gnus-summary-show-article)
5572           (gnus-article-part-wrapper n function no-handle))))))
5573
5574 (defun gnus-article-pipe-part (n)
5575   "Pipe MIME part N, which is the numerical prefix."
5576   (interactive "P")
5577   (gnus-article-part-wrapper n 'mm-pipe-part))
5578
5579 (defun gnus-article-save-part (n)
5580   "Save MIME part N, which is the numerical prefix."
5581   (interactive "P")
5582   (gnus-article-part-wrapper n 'mm-save-part))
5583
5584 (defun gnus-article-interactively-view-part (n)
5585   "View MIME part N interactively, which is the numerical prefix."
5586   (interactive "P")
5587   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5588
5589 (defun gnus-article-copy-part (n)
5590   "Copy MIME part N, which is the numerical prefix."
5591   (interactive "P")
5592   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5593
5594 (defun gnus-article-view-part-as-charset (n)
5595   "View MIME part N using a specified charset.
5596 N is the numerical prefix."
5597   (interactive "P")
5598   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5599
5600 (defun gnus-article-view-part-externally (n)
5601   "View MIME part N externally, which is the numerical prefix."
5602   (interactive "P")
5603   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5604
5605 (defun gnus-article-inline-part (n)
5606   "Inline MIME part N, which is the numerical prefix."
5607   (interactive "P")
5608   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5609
5610 (defun gnus-article-save-part-and-strip (n)
5611   "Save MIME part N and replace it with an external body.
5612 N is the numerical prefix."
5613   (interactive "P")
5614   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5615
5616 (defun gnus-article-replace-part (n)
5617   "Replace MIME part N with an external body.
5618 N is the numerical prefix."
5619   (interactive "P")
5620   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5621
5622 (defun gnus-article-delete-part (n)
5623   "Delete MIME part N and add some information about the removed part.
5624 N is the numerical prefix."
5625   (interactive "P")
5626   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5627
5628 (defun gnus-article-view-part-as-type (n)
5629   "Choose a MIME media type, and view part N as such.
5630 N is the numerical prefix."
5631   (interactive "P")
5632   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5633
5634 (defun gnus-article-mime-match-handle-first (condition)
5635   (if condition
5636       (let (n)
5637         (dolist (ihandle gnus-article-mime-handle-alist)
5638           (if (and (cond
5639                     ((functionp condition)
5640                      (funcall condition (cdr ihandle)))
5641                     ((eq condition 'undisplayed)
5642                      (not (or (mm-handle-undisplayer (cdr ihandle))
5643                               (equal (mm-handle-media-type (cdr ihandle))
5644                                      "multipart/alternative"))))
5645                     ((eq condition 'undisplayed-alternative)
5646                      (not (mm-handle-undisplayer (cdr ihandle))))
5647                     (t t))
5648                    (gnus-article-goto-part (car ihandle))
5649                    (or (not n) (< (car ihandle) n)))
5650               (setq n (car ihandle))))
5651         (or n 1))
5652     1))
5653
5654 (defun gnus-article-view-part (&optional n)
5655   "View MIME part N, which is the numerical prefix.
5656 If the part is already shown, hide the part.  If N is nil, view
5657 all parts."
5658   (interactive "P")
5659   (with-current-buffer gnus-article-buffer
5660     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5661                              gnus-article-mime-match-handle-function)))
5662     (when (> n (length gnus-article-mime-handle-alist))
5663       (error "No such part"))
5664     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5665       (when (gnus-article-goto-part n)
5666         (if (equal (car handle) "multipart/alternative")
5667             (progn
5668               (beginning-of-line) ;; Make it toggle subparts
5669               (gnus-article-press-button))
5670           (when (eq (gnus-mm-display-part handle) 'internal)
5671             (gnus-set-window-start)))))))
5672
5673 (defsubst gnus-article-mime-total-parts ()
5674   (if (bufferp (car gnus-article-mime-handles))
5675       1 ;; single part
5676     (1- (length gnus-article-mime-handles))))
5677
5678 (defun gnus-mm-display-part (handle)
5679   "Display HANDLE and fix MIME button."
5680   (let ((id (get-text-property (point) 'gnus-part))
5681         (point (point))
5682         (inhibit-read-only t)
5683         (window (selected-window))
5684         (mail-parse-charset gnus-newsgroup-charset)
5685         (mail-parse-ignored-charsets
5686          (if (gnus-buffer-live-p gnus-summary-buffer)
5687              (with-current-buffer gnus-summary-buffer
5688                gnus-newsgroup-ignored-charsets)
5689            nil))
5690         retval)
5691     (unwind-protect
5692         (progn
5693           (let ((win (gnus-get-buffer-window (current-buffer) t)))
5694             (when win
5695               (select-window win)
5696               (goto-char point)))
5697           (setq point (previous-single-property-change
5698                        (or (next-single-property-change point 'gnus-data)
5699                            (point-max))
5700                        'gnus-data))
5701           (if (mm-handle-displayed-p handle)
5702               ;; This will remove the part.
5703               (setq retval (mm-display-part handle))
5704             (let ((part (or (and (mm-inlinable-p handle)
5705                                  (mm-inlined-p handle)
5706                                  t)
5707                             (with-temp-buffer
5708                               (gnus-bind-safe-url-regexp
5709                                (setq retval (mm-display-part handle)))
5710                               (unless (zerop (buffer-size))
5711                                 (buffer-string))))))
5712               (forward-line)
5713               (cond ((stringp part)
5714                      (save-restriction
5715                        (narrow-to-region (point)
5716                                          (progn
5717                                            (insert part)
5718                                            (unless (bolp) (insert "\n"))
5719                                            (point)))
5720                        (gnus-treat-article nil id
5721                                            (gnus-article-mime-total-parts)
5722                                            (mm-handle-media-type handle))
5723                        (mm-handle-set-undisplayer
5724                         handle
5725                         `(lambda ()
5726                            (let ((inhibit-read-only t))
5727                              (delete-region ,(copy-marker (point-min) t)
5728                                             ,(point-max-marker)))))))
5729                     (part
5730                      (mm-display-inline handle))))))
5731       (goto-char point)
5732       ;; Toggle the button appearance between `[button]...' and `[button]'.
5733       (gnus-insert-mime-button handle id (list (mm-handle-displayed-p handle)))
5734       (if (featurep 'emacs)
5735           (delete-region
5736            (point)
5737            (or (text-property-any (point) (point-max) 'gnus-data nil)
5738                (point-max)))
5739         (let* ((end (text-property-any (point) (point-max) 'gnus-data nil))
5740                (annots (annotations-at end)))
5741           (delete-region (point)
5742                          (if end
5743                              (if annots (1+ end) end)
5744                            (point-max)))
5745           (dolist (annot annots)
5746             (set-extent-endpoints annot (point) (point)))))
5747       (unless (search-backward "\n\n" nil t)
5748         ;; We're in the article header.
5749         (delete-char -1)
5750         (dolist (ovl (gnus-overlays-in point (point)))
5751           (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
5752           (gnus-overlay-put ovl 'face nil))
5753         (save-restriction
5754           (message-narrow-to-field)
5755           (let ((gnus-treatment-function-alist
5756                  '((gnus-treat-highlight-headers
5757                     gnus-article-highlight-headers))))
5758             (gnus-treat-article 'head))))
5759       (goto-char point)
5760       (if (window-live-p window)
5761           (select-window window)))
5762     retval))
5763
5764 (defun gnus-article-goto-part (n)
5765   "Go to MIME part N."
5766   (when gnus-break-pages
5767     (widen))
5768   (article-goto-body)
5769   (prog1
5770       (let ((start (or (text-property-any (point) (point-max) 'gnus-part n)
5771                        ;; There may be header buttons.
5772                        (text-property-any (point-min) (point) 'gnus-part n)))
5773             part handle end next handles)
5774         (when start
5775           (goto-char start)
5776           (if (setq handle (get-text-property start 'gnus-data))
5777               start
5778             ;; Go to the displayed subpart, assuming this is
5779             ;; multipart/alternative.
5780             (setq part start
5781                   end (point-at-eol))
5782             (while (and (not handle)
5783                         part
5784                         (< part end)
5785                         (setq next (text-property-not-all part end
5786                                                           'gnus-data nil)))
5787               (setq part next
5788                     handle (get-text-property part 'gnus-data))
5789               (push (cons handle part) handles)
5790               (unless (mm-handle-displayed-p handle)
5791                 (setq handle nil
5792                       part (text-property-any part end 'gnus-data nil))))
5793             (unless handle
5794               ;; No subpart is displayed, so we find preferred one.
5795               (setq part
5796                     (cdr (assq (mm-preferred-alternative
5797                                 (nreverse (mapcar 'car handles)))
5798                                handles))))
5799             (if part
5800                 (goto-char (1+ part))
5801               start))))
5802     (when gnus-break-pages
5803       (gnus-narrow-to-page))))
5804
5805 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5806   (let ((gnus-tmp-name
5807          (or (mm-handle-filename handle)
5808              (mail-content-type-get (mm-handle-type handle) 'url)
5809              ""))
5810         (gnus-tmp-type (mm-handle-media-type handle))
5811         (gnus-tmp-description (or (mm-handle-description handle) ""))
5812         (gnus-tmp-dots
5813          (if (if displayed (car displayed)
5814                (mm-handle-displayed-p handle))
5815              "" "..."))
5816         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5817                            (buffer-size)))
5818         gnus-tmp-type-long b e)
5819     (when (string-match ".*/" gnus-tmp-name)
5820       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5821     (setq gnus-tmp-type-long (concat gnus-tmp-type
5822                                      (and (not (equal gnus-tmp-name ""))
5823                                           (concat "; " gnus-tmp-name))))
5824     (unless (equal gnus-tmp-description "")
5825       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5826     (setq b (point))
5827     (gnus-eval-format
5828      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5829      `(keymap ,gnus-mime-button-map
5830               gnus-callback gnus-mm-display-part
5831               gnus-part ,gnus-tmp-id
5832               article-type annotation
5833               gnus-data ,handle
5834               rear-nonsticky t))
5835     (setq e (if (bolp)
5836                 ;; Exclude a newline.
5837                 (1- (point))
5838               (point)))
5839     (when gnus-article-button-face
5840       (gnus-overlay-put (gnus-make-overlay b e nil t)
5841                         'face gnus-article-button-face))
5842     (widget-convert-button
5843      'link b e
5844      :mime-handle handle
5845      :action 'gnus-widget-press-button
5846      :button-keymap gnus-mime-button-map
5847      :help-echo
5848      (lambda (widget)
5849        ;; Needed to properly clear the message due to a bug in
5850        ;; wid-edit (XEmacs only).
5851        (if (boundp 'help-echo-owns-message)
5852            (setq help-echo-owns-message t))
5853        (format
5854         "%S: %s the MIME part; %S: more options"
5855         (aref gnus-mouse-2 0)
5856         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5857             "hide" "show")
5858         (aref gnus-down-mouse-3 0))))))
5859
5860 (defun gnus-widget-press-button (elems el)
5861   (goto-char (widget-get elems :from))
5862   (gnus-article-press-button))
5863
5864 (defvar gnus-displaying-mime nil)
5865
5866 (defun gnus-display-mime (&optional ihandles)
5867   "Display the MIME parts."
5868   (save-excursion
5869     (save-selected-window
5870       (let ((window (get-buffer-window gnus-article-buffer))
5871             (point (point)))
5872         (when window
5873           (select-window window)
5874           ;; We have to do this since selecting the window
5875           ;; may change the point.  So we set the window point.
5876           (set-window-point window point)))
5877       (let ((handles ihandles)
5878             (inhibit-read-only t)
5879             handle)
5880         (cond (handles)
5881               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5882                (when gnus-article-emulate-mime
5883                  (mm-uu-dissect-text-parts handles)))
5884               (gnus-article-emulate-mime
5885                (setq handles (mm-uu-dissect))))
5886         (when (and (not ihandles)
5887                    (not gnus-displaying-mime))
5888           ;; Top-level call; we clean up.
5889           (when gnus-article-mime-handles
5890             (mm-destroy-parts gnus-article-mime-handles)
5891             (setq gnus-article-mime-handle-alist nil));; A trick.
5892           (setq gnus-article-mime-handles handles)
5893           ;; We allow users to glean info from the handles.
5894           (when gnus-article-mime-part-function
5895             (gnus-mime-part-function handles)))
5896         (if (and handles
5897                  (or (not (stringp (car handles)))
5898                      (cdr handles)))
5899             (progn
5900               (when (and (not ihandles)
5901                          (not gnus-displaying-mime))
5902                 ;; Clean up for mime parts.
5903                 (article-goto-body)
5904                 (delete-region (point) (point-max)))
5905               (let ((gnus-displaying-mime t))
5906                 (gnus-mime-display-part handles)))
5907           (save-restriction
5908             (article-goto-body)
5909             (narrow-to-region (point) (point-max))
5910             (gnus-treat-article nil 1 1 "text/plain")
5911             (widen)))
5912         (unless ihandles
5913           ;; Highlight the headers.
5914           (save-excursion
5915             (save-restriction
5916               (article-goto-body)
5917               (narrow-to-region (point-min) (point))
5918               (gnus-article-save-original-date
5919                (gnus-treat-article 'head)))))))
5920     ;; Cope with broken MIME messages.
5921     (goto-char (point-max))
5922     (unless (bolp)
5923       (insert "\n"))))
5924
5925 (defcustom gnus-mime-display-multipart-as-mixed nil
5926   "Display \"multipart\" parts as  \"multipart/mixed\".
5927
5928 If t, it overrides nil values of
5929 `gnus-mime-display-multipart-alternative-as-mixed' and
5930 `gnus-mime-display-multipart-related-as-mixed'."
5931   :group 'gnus-article-mime
5932   :type 'boolean)
5933
5934 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5935   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5936   :version "22.1"
5937   :group 'gnus-article-mime
5938   :type 'boolean)
5939
5940 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5941   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5942
5943 If displaying \"text/html\" is discouraged \(see
5944 `mm-discouraged-alternatives'\) images or other material inside a
5945 \"multipart/related\" part might be overlooked when this variable is nil."
5946   :version "22.1"
5947   :group 'gnus-article-mime
5948   :type 'boolean)
5949
5950 (defcustom gnus-mime-display-attachment-buttons-in-header t
5951   "Add attachment buttons in the end of the header of an article.
5952 Since MIME attachments tend to be put at the end of an article, we may
5953 overlook them if there is a huge body.  This option offers you a copy
5954 of all non-inlinable MIME parts as buttons shown in front of an article.
5955 If nil, don't show those extra buttons."
5956   :version "24.5"
5957   :group 'gnus-article-mime
5958   :type 'boolean)
5959
5960 (defun gnus-mime-display-part (handle)
5961   (cond
5962    ;; Maybe a broken MIME message.
5963    ((null handle))
5964    ;; Single part.
5965    ((not (stringp (car handle)))
5966     (gnus-mime-display-single handle))
5967    ;; User-defined multipart
5968    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5969     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5970              handle))
5971    ;; multipart/alternative
5972    ((and (equal (car handle) "multipart/alternative")
5973          (not (or gnus-mime-display-multipart-as-mixed
5974                   gnus-mime-display-multipart-alternative-as-mixed)))
5975     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5976       (push (cons id handle) gnus-article-mime-handle-alist)
5977       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5978    ;; multipart/related
5979    ((and (equal (car handle) "multipart/related")
5980          (not (or gnus-mime-display-multipart-as-mixed
5981                   gnus-mime-display-multipart-related-as-mixed)))
5982     (gnus-mime-display-part (cadr handle)))
5983    ((equal (car handle) "multipart/signed")
5984     (gnus-add-wash-type 'signed)
5985     (gnus-mime-display-security handle))
5986    ((equal (car handle) "multipart/encrypted")
5987     (gnus-add-wash-type 'encrypted)
5988     (gnus-mime-display-security handle))
5989    ;; Other multiparts are handled like multipart/mixed.
5990    (t
5991     (gnus-mime-display-mixed (cdr handle)))))
5992
5993 (defun gnus-mime-part-function (handles)
5994   (if (stringp (car handles))
5995       (mapcar 'gnus-mime-part-function (cdr handles))
5996     (funcall gnus-article-mime-part-function handles)))
5997
5998 (defun gnus-mime-display-mixed (handles)
5999   (mapcar 'gnus-mime-display-part handles))
6000
6001 (defun gnus-mime-display-single (handle)
6002   (let ((type (mm-handle-media-type handle))
6003         (ignored gnus-ignored-mime-types)
6004         (not-attachment t)
6005         display text)
6006     (catch 'ignored
6007       (progn
6008         (while ignored
6009           (when (string-match (pop ignored) type)
6010             (throw 'ignored nil)))
6011         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
6012                                (with-current-buffer gnus-summary-buffer
6013                                  gnus-inhibit-images)
6014                              gnus-inhibit-images)
6015                            (string-match "\\`image/" type)))
6016                  (setq not-attachment
6017                        (and (not (mm-inline-override-p handle))
6018                             (or (not (mm-handle-disposition handle))
6019                                 (equal (car (mm-handle-disposition handle))
6020                                        "inline")
6021                                 (mm-attachment-override-p handle))))
6022                  (mm-automatic-display-p handle)
6023                  (or (and
6024                       (mm-inlinable-p handle)
6025                       (mm-inlined-p handle))
6026                      (mm-automatic-external-display-p type)))
6027             (setq display t)
6028           (when (equal (mm-handle-media-supertype handle) "text")
6029             (setq text t)))
6030         (let ((id (car (rassq handle gnus-article-mime-handle-alist)))
6031               beg)
6032           (unless id
6033             (setq id (1+ (length gnus-article-mime-handle-alist)))
6034             (push (cons id handle) gnus-article-mime-handle-alist))
6035           (when (and display
6036                      (equal (mm-handle-media-supertype handle) "message"))
6037             (insert-char
6038              ?\n
6039              (cond ((not (bolp)) 2)
6040                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
6041                    (t 1))))
6042           (when (or (not display)
6043                     (not (gnus-unbuttonized-mime-type-p type))
6044                     (eq id gnus-mime-buttonized-part-id))
6045             (gnus-insert-mime-button
6046              handle id (list (or display (and not-attachment text)))))
6047           (setq beg (point))
6048           (cond
6049            (display
6050             (let ((mail-parse-charset gnus-newsgroup-charset)
6051                   (mail-parse-ignored-charsets
6052                    (save-excursion (condition-case ()
6053                                        (set-buffer gnus-summary-buffer)
6054                                      (error))
6055                                    gnus-newsgroup-ignored-charsets)))
6056               (gnus-bind-safe-url-regexp (mm-display-part handle t))))
6057            ((and text not-attachment)
6058             (mm-display-inline handle)))
6059           (goto-char (point-max))
6060           (if (string-match "\\`image/" type)
6061               (gnus-article-insert-newline)
6062             (if (prog1
6063                     (= (skip-chars-backward "\n") -1)
6064                   (forward-char 1))
6065                 (gnus-article-insert-newline)
6066               (put-text-property (point) (point-max) 'gnus-undeletable t))
6067             (goto-char (point-max)))
6068           ;; Do highlighting.
6069           (save-excursion
6070             (save-restriction
6071               (narrow-to-region beg (point))
6072               (if (eq handle gnus-article-mime-handles)
6073                   ;; The format=flowed case.
6074                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
6075                 ;; Don't count signature parts that are never displayed.
6076                 ;; The part number should be re-calculated supposing this
6077                 ;; might be a message/rfc822 part.
6078                 (let (handles)
6079                   (dolist (part gnus-article-mime-handles)
6080                     (unless (or (stringp part)
6081                                 (equal (car (mm-handle-type part))
6082                                        "application/pgp-signature"))
6083                       (push part handles)))
6084                   (gnus-treat-article
6085                    nil (length (memq handle handles)) (length handles)
6086                    (mm-handle-media-type handle)))))))))))
6087
6088 (defun gnus-unbuttonized-mime-type-p (type)
6089   "Say whether TYPE is to be unbuttonized."
6090   (unless gnus-inhibit-mime-unbuttonizing
6091     (when (catch 'found
6092             (let ((types gnus-unbuttonized-mime-types))
6093               (while types
6094                 (when (string-match (pop types) type)
6095                   (throw 'found t)))))
6096       (not (catch 'found
6097              (let ((types gnus-buttonized-mime-types))
6098                (while types
6099                  (when (string-match (pop types) type)
6100                    (throw 'found t)))))))))
6101
6102 (defun gnus-article-insert-newline ()
6103   "Insert a newline, but mark it as undeletable."
6104   (gnus-put-text-property
6105    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
6106
6107 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
6108   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
6109          (ihandles handles)
6110          (point (point))
6111          handle (inhibit-read-only t) from props begend not-pref)
6112     (save-window-excursion
6113       (save-restriction
6114         (when ibegend
6115           (narrow-to-region (car ibegend)
6116                             (or (cdr ibegend)
6117                                 (progn
6118                                   (goto-char (car ibegend))
6119                                   (forward-line 2)
6120                                   (point))))
6121           (delete-region (point-min) (point-max))
6122           (mm-remove-parts handles))
6123         (setq begend (list (point-marker)))
6124         ;; Do the toggle.
6125         (unless (setq not-pref (cadr (member preferred ihandles)))
6126           (setq not-pref (car ihandles)))
6127         (when (or ibegend
6128                   (not preferred)
6129                   (not (gnus-unbuttonized-mime-type-p
6130                         "multipart/alternative")))
6131           (gnus-add-text-properties
6132            (setq from (point))
6133            (progn
6134              (insert (format "%d.  " id))
6135              (point))
6136            `(gnus-callback
6137              (lambda (handles)
6138                (unless ,(not ibegend)
6139                  (setq gnus-article-mime-handle-alist
6140                        ',gnus-article-mime-handle-alist))
6141                (gnus-mime-display-alternative
6142                 ',ihandles ',not-pref ',begend ,id))
6143              keymap ,gnus-mime-button-map
6144              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6145              face ,gnus-article-button-face
6146              gnus-part ,id
6147              article-type multipart
6148              rear-nonsticky t))
6149           (widget-convert-button 'link from (point)
6150                                  :action 'gnus-widget-press-button
6151                                  :button-keymap gnus-widget-button-keymap)
6152           ;; Do the handles
6153           (while (setq handle (pop handles))
6154             (gnus-add-text-properties
6155              (setq from (point))
6156              (progn
6157                (insert (format "(%c) %-18s"
6158                                (if (equal handle preferred) ?* ? )
6159                                (mm-handle-media-type handle)))
6160                (point))
6161              `(gnus-callback
6162                (lambda (handles)
6163                  (unless ,(not ibegend)
6164                    (setq gnus-article-mime-handle-alist
6165                          ',gnus-article-mime-handle-alist))
6166                  (gnus-mime-display-alternative
6167                   ',ihandles ',handle ',begend ,id))
6168                keymap ,gnus-mime-button-map
6169                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6170                face ,gnus-article-button-face
6171                gnus-part ,id
6172                gnus-data ,handle
6173                rear-nonsticky t))
6174             (widget-convert-button 'link from (point)
6175                                    :action 'gnus-widget-press-button
6176                                    :button-keymap gnus-widget-button-keymap)
6177             (insert "  "))
6178           (insert "\n\n"))
6179         (when preferred
6180           (if (stringp (car preferred))
6181               (gnus-display-mime preferred)
6182             (let ((mail-parse-charset gnus-newsgroup-charset)
6183                   (mail-parse-ignored-charsets
6184                    (with-current-buffer gnus-summary-buffer
6185                      gnus-newsgroup-ignored-charsets)))
6186               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6187               ;; Do highlighting.
6188               (save-excursion
6189                 (save-restriction
6190                   (narrow-to-region (car begend) (point-max))
6191                   (gnus-treat-article
6192                    nil (length gnus-article-mime-handle-alist)
6193                    (gnus-article-mime-total-parts)
6194                    (mm-handle-media-type preferred))))))
6195           (goto-char (point-max))
6196           (setcdr begend (point-marker)))))
6197     (when ibegend
6198       (goto-char point)))
6199   ;; Redraw attachment buttons in the header.
6200   (when gnus-mime-display-attachment-buttons-in-header
6201     (gnus-mime-buttonize-attachments-in-header)))
6202
6203 (defconst gnus-article-wash-status-strings
6204   (let ((alist '((cite "c" "Possible hidden citation text"
6205                        " " "All citation text visible")
6206                  (headers "h" "Hidden headers"
6207                           " " "All headers visible.")
6208                  (pgp "p" "Encrypted or signed message status hidden"
6209                       " " "No hidden encryption nor digital signature status")
6210                  (signature "s" "Signature has been hidden"
6211                             " " "Signature is visible")
6212                  (overstrike "o" "Overstrike (^H) characters applied"
6213                              " " "No overstrike characters applied")
6214                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6215                            " " "No /*_emphasis_*/ characters applied")))
6216         result)
6217     (dolist (entry alist result)
6218       (let ((key (nth 0 entry))
6219             (on (copy-sequence (nth 1 entry)))
6220             (on-help (nth 2 entry))
6221             (off (copy-sequence (nth 3 entry)))
6222             (off-help (nth 4 entry)))
6223         (put-text-property 0 1 'help-echo on-help on)
6224         (put-text-property 0 1 'help-echo off-help off)
6225         (push (list key on off) result))))
6226   "Alist of strings describing wash status in the mode line.
6227 Each entry has the form (KEY ON OF), where the KEY is a symbol
6228 representing the particular washing function, ON is the string to use
6229 in the article mode line when the washing function is active, and OFF
6230 is the string to use when it is inactive.")
6231
6232 (defun gnus-article-wash-status-entry (key value)
6233   (let ((entry (assoc key gnus-article-wash-status-strings)))
6234     (if value (nth 1 entry) (nth 2 entry))))
6235
6236 (defun gnus-article-wash-status ()
6237   "Return a string which display status of article washing."
6238   (with-current-buffer gnus-article-buffer
6239     (let ((cite (memq 'cite gnus-article-wash-types))
6240           (headers (memq 'headers gnus-article-wash-types))
6241           (boring (memq 'boring-headers gnus-article-wash-types))
6242           (pgp (memq 'pgp gnus-article-wash-types))
6243           (pem (memq 'pem gnus-article-wash-types))
6244           (signed (memq 'signed gnus-article-wash-types))
6245           (encrypted (memq 'encrypted gnus-article-wash-types))
6246           (signature (memq 'signature gnus-article-wash-types))
6247           (overstrike (memq 'overstrike gnus-article-wash-types))
6248           (emphasis (memq 'emphasis gnus-article-wash-types)))
6249       (concat
6250        (gnus-article-wash-status-entry 'cite cite)
6251        (gnus-article-wash-status-entry 'headers (or headers boring))
6252        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6253        (gnus-article-wash-status-entry 'signature signature)
6254        (gnus-article-wash-status-entry 'overstrike overstrike)
6255        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6256
6257 (defun gnus-add-wash-type (type)
6258   "Add a washing of TYPE to the current status."
6259   (add-to-list 'gnus-article-wash-types type))
6260
6261 (defun gnus-delete-wash-type (type)
6262   "Add a washing of TYPE to the current status."
6263   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6264
6265 (defun gnus-add-image (category image)
6266   "Add IMAGE of CATEGORY to the list of displayed images."
6267   (let ((entry (assq category gnus-article-image-alist)))
6268     (unless entry
6269       (setq entry (list category))
6270       (push entry gnus-article-image-alist))
6271     (nconc entry (list image))))
6272
6273 (defun gnus-delete-images (category)
6274   "Delete all images in CATEGORY."
6275   (let ((entry (assq category gnus-article-image-alist)))
6276     (dolist (image (cdr entry))
6277       (gnus-remove-image image category))
6278     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6279     (gnus-delete-wash-type category)))
6280
6281 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6282
6283 (defun gnus-article-maybe-hide-headers ()
6284   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6285 Provided for backwards compatibility."
6286   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6287                  (not (with-current-buffer gnus-summary-buffer
6288                         gnus-have-all-headers)))
6289              (not gnus-inhibit-hiding))
6290     (gnus-article-hide-headers)))
6291
6292 (declare-function shr-put-image "shr" (data alt &optional flags))
6293
6294 (defun gnus-shr-put-image (data alt &optional flags)
6295   "Put image DATA with a string ALT.  Enable image to be deleted."
6296   (let ((image (if flags
6297                    (shr-put-image data (propertize (or alt "*")
6298                                                    'gnus-image-category 'shr)
6299                                   flags)
6300                  ;; Old `shr-put-image' doesn't take the optional `flags'
6301                  ;; argument.
6302                  (shr-put-image data (propertize (or alt "*")
6303                                                  'gnus-image-category 'shr)))))
6304     (when image
6305       (gnus-add-image 'shr image))))
6306
6307 (defun gnus-mime-buttonize-attachments-in-header (&optional interactive)
6308   "Show attachments as buttons in the end of the header of an article.
6309 This function toggles the display when called interactively.  Note that
6310 buttons to be added to the header are only the ones that aren't inlined
6311 in the body.  Use `gnus-header-face-alist' to highlight buttons."
6312   (interactive (list t))
6313   (gnus-with-article-buffer
6314     (gmm-labels
6315         ;; Function that returns a flattened version of
6316         ;; `gnus-article-mime-handle-alist'.
6317         ((flattened-alist
6318           (&optional alist id all)
6319           (if alist
6320               (let ((i 1) newid flat)
6321                 (dolist (handle alist flat)
6322                   (setq newid (append id (list i))
6323                         i (1+ i))
6324                   (if (stringp (car handle))
6325                       (setq flat (nconc flat (flattened-alist (cdr handle)
6326                                                               newid all)))
6327                     (delq (rassq handle all) all)
6328                     (setq flat (nconc flat (list (cons newid handle)))))))
6329             (let ((flat (list nil)))
6330               ;; Assume that elements of `gnus-article-mime-handle-alist'
6331               ;; are in the decreasing order, but unnumbered subsidiaries
6332               ;; in each element are in the increasing order.
6333               (dolist (handle (reverse gnus-article-mime-handle-alist))
6334                 (if (stringp (cadr handle))
6335                     (setq flat (nconc flat (flattened-alist (cddr handle)
6336                                                             (list (car handle))
6337                                                             flat)))
6338                   (delq (rassq (cdr handle) flat) flat)
6339                   (setq flat (nconc flat (list (cons (list (car handle))
6340                                                      (cdr handle)))))))
6341               (setq flat (cdr flat))
6342               (mapc (lambda (handle)
6343                       (if (cdar handle)
6344                           ;; This is a hidden (i.e. unnumbered) handle.
6345                           (progn
6346                             (setcar handle
6347                                     (1+ (caar gnus-article-mime-handle-alist)))
6348                             (push handle gnus-article-mime-handle-alist))
6349                         (setcar handle (caar handle))))
6350                     flat)
6351               flat))))
6352       (let ((case-fold-search t) buttons st handle)
6353         (save-excursion
6354           (save-restriction
6355             (widen)
6356             (article-narrow-to-head)
6357             ;; Header buttons exist?
6358             (while (and (not buttons)
6359                         (re-search-forward "^attachments?:[\n ]+" nil t))
6360               (when (get-char-property (match-end 0)
6361                                        'gnus-button-attachment-extra)
6362                 (setq buttons (match-beginning 0))))
6363             (widen)
6364             (when buttons
6365               ;; Delete header buttons.
6366               (delete-region buttons (if (re-search-forward "^[^ ]" nil t)
6367                                          (match-beginning 0)
6368                                        (point-max))))
6369             (unless (and interactive buttons)
6370               ;; Find buttons.
6371               (setq buttons nil)
6372               (dolist (handle (flattened-alist))
6373                 (when (and (not (stringp (cadr handle)))
6374                            (or (equal (car (mm-handle-disposition
6375                                             (cdr handle)))
6376                                       "attachment")
6377                                (not (and (mm-inlinable-p (cdr handle))
6378                                          (mm-inlined-p (cdr handle))))))
6379                   (push handle buttons)))
6380               (when buttons
6381                 ;; Add header buttons.
6382                 (article-goto-body)
6383                 (forward-line -1)
6384                 (narrow-to-region (point) (point))
6385                 (insert "Attachment" (if (cdr buttons) "s" "") ":")
6386                 (dolist (button (nreverse buttons))
6387                   (setq st (point))
6388                   (insert " ")
6389                   (mm-handle-set-undisplayer
6390                    (setq handle (copy-sequence (cdr button))) nil)
6391                   (gnus-insert-mime-button handle (car button))
6392                   (skip-chars-backward "\t\n ")
6393                   (delete-region (point) (point-max))
6394                   (when (> (current-column) (window-width))
6395                     (goto-char st)
6396                     (insert "\n")
6397                     (end-of-line)))
6398                 (insert "\n")
6399                 (dolist (ovl (gnus-overlays-in (point-min) (point)))
6400                   (gnus-overlay-put ovl 'gnus-button-attachment-extra t)
6401                   (gnus-overlay-put ovl 'face nil))
6402                 (let ((gnus-treatment-function-alist
6403                        '((gnus-treat-highlight-headers
6404                           gnus-article-highlight-headers))))
6405                   (gnus-treat-article 'head))))))))))
6406
6407 ;;; Article savers.
6408
6409 (defun gnus-output-to-file (file-name)
6410   "Append the current article to a file named FILE-NAME.
6411 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6412 text and used as the value of the coding cookie which is added to the
6413 top of a file.  Otherwise, this function saves a raw article without
6414 the coding cookie."
6415   (let* ((artbuf (current-buffer))
6416          (file-name-coding-system nnmail-pathname-coding-system)
6417          (coding gnus-article-save-coding-system)
6418          (coding-system-for-read (if coding
6419                                      nil ;; Rely on the coding cookie.
6420                                    mm-text-coding-system))
6421          (coding-system-for-write (or coding
6422                                       mm-text-coding-system-for-write
6423                                       mm-text-coding-system))
6424          (exists (file-exists-p file-name)))
6425     (with-temp-buffer
6426       (when exists
6427         (insert-file-contents file-name)
6428         (goto-char (point-min))
6429         ;; Remove the existing coding cookie.
6430         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6431           (delete-region (match-beginning 0) (match-end 0))))
6432       (goto-char (point-max))
6433       (insert-buffer-substring artbuf)
6434       ;; Append newline at end of the buffer as separator, and then
6435       ;; save it to file.
6436       (goto-char (point-max))
6437       (insert "\n")
6438       (when coding
6439         ;; If the coding system is not suitable to encode the text,
6440         ;; ask a user for a proper one.
6441         (when (fboundp 'select-safe-coding-system)
6442           (setq coding (coding-system-base
6443                         (save-window-excursion
6444                           (select-safe-coding-system (point-min) (point-max)
6445                                                      coding))))
6446           (setq coding-system-for-write
6447                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6448                     coding)))
6449         (goto-char (point-min))
6450         ;; Add the coding cookie.
6451         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6452                         coding-system-for-write)))
6453       (if exists
6454           (progn
6455             (write-region (point-min) (point-max) file-name nil 'no-message)
6456             (message "Appended to %s" file-name))
6457         (write-region (point-min) (point-max) file-name))))
6458   t)
6459
6460 (defun gnus-narrow-to-page (&optional arg)
6461   "Narrow the article buffer to a page.
6462 If given a numerical ARG, move forward ARG pages."
6463   (interactive "P")
6464   (setq arg (if arg (prefix-numeric-value arg) 0))
6465   (with-current-buffer gnus-article-buffer
6466     (widen)
6467     ;; Remove any old next/prev buttons.
6468     (when (gnus-visual-p 'page-marker)
6469       (let ((inhibit-read-only t))
6470         (gnus-remove-text-with-property 'gnus-prev)
6471         (gnus-remove-text-with-property 'gnus-next)))
6472     (let (st nd pt)
6473       (when (save-excursion
6474               (cond ((< arg 0)
6475                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6476                          (prog1
6477                              (setq nd (match-beginning 0)
6478                                    pt nd)
6479                            (when (re-search-backward page-delimiter nil t)
6480                              (setq st (match-end 0))))
6481                        (when (re-search-forward page-delimiter nil t)
6482                          (setq nd (match-beginning 0)
6483                                pt (point-min)))))
6484                     ((> arg 0)
6485                      (if (re-search-forward page-delimiter nil 'move arg)
6486                          (prog1
6487                              (setq st (match-end 0)
6488                                    pt st)
6489                            (when (re-search-forward page-delimiter nil t)
6490                              (setq nd (match-beginning 0))))
6491                        (when (re-search-backward page-delimiter nil t)
6492                          (setq st (match-end 0)
6493                                pt (point-max)))))
6494                     (t
6495                      (when (re-search-backward page-delimiter nil t)
6496                        (goto-char (setq st (match-end 0))))
6497                      (when (re-search-forward page-delimiter nil t)
6498                        (setq nd (match-beginning 0)))
6499                      (or st nd))))
6500         (setq gnus-page-broken t)
6501         (when pt (goto-char pt))
6502         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6503         (when (gnus-visual-p 'page-marker)
6504           (save-excursion
6505             (when nd
6506               (goto-char nd)
6507               (gnus-insert-next-page-button))
6508             (when st
6509               (goto-char st)
6510               (gnus-insert-prev-page-button))))))))
6511
6512 ;; Article mode commands
6513
6514 (defun gnus-article-goto-next-page ()
6515   "Show the next page of the article."
6516   (interactive)
6517   (when (gnus-article-next-page)
6518     (goto-char (point-min))
6519     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6520
6521
6522 (defun gnus-article-goto-prev-page ()
6523   "Show the previous page of the article."
6524   (interactive)
6525   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6526       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6527     (gnus-article-prev-page nil)))
6528
6529 ;; This is cleaner but currently breaks `gnus-pick-mode':
6530 ;;
6531 ;; (defun gnus-article-goto-next-page ()
6532 ;;   "Show the next page of the article."
6533 ;;   (interactive)
6534 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6535 ;;     (gnus-summary-next-page)))
6536 ;;
6537 ;; (defun gnus-article-goto-prev-page ()
6538 ;;   "Show the next page of the article."
6539 ;;   (interactive)
6540 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6541 ;;     (gnus-summary-prev-page)))
6542
6543 (defun gnus-article-next-page (&optional lines)
6544   "Show the next page of the current article.
6545 If end of article, return non-nil.  Otherwise return nil.
6546 Argument LINES specifies lines to be scrolled up."
6547   (interactive "p")
6548   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6549   (if (and (not (and gnus-article-over-scroll
6550                      (> (count-lines (window-start) (point-max))
6551                         (if (featurep 'xemacs)
6552                             (or lines (1- (window-height)))
6553                           (+ (or lines (1- (window-height))) scroll-margin)))))
6554            (save-excursion
6555              (end-of-line)
6556              (and (pos-visible-in-window-p)     ;Not continuation line.
6557                   (>= (point) (point-max)))))
6558       ;; Nothing in this page.
6559       (if (or (not gnus-page-broken)
6560               (save-excursion
6561                 (save-restriction
6562                   (widen)
6563                   (forward-line)
6564                   (eobp)))) ;Real end-of-buffer?
6565           (progn
6566             (when gnus-article-over-scroll
6567               (gnus-article-next-page-1 lines))
6568             t)                  ;Nothing more.
6569         (gnus-narrow-to-page 1)         ;Go to next page.
6570         nil)
6571     ;; More in this page.
6572     (gnus-article-next-page-1 lines)
6573     nil))
6574
6575 (defun gnus-article-beginning-of-window ()
6576   "Move point to the beginning of the window.
6577 In Emacs, the point is placed at the line number which `scroll-margin'
6578 specifies."
6579   (if (featurep 'xemacs)
6580       (move-to-window-line 0)
6581     ;; There is an obscure bug in Emacs that makes it impossible to
6582     ;; scroll past big pictures in the article buffer.  Try to fix
6583     ;; this by adding a sanity check by counting the lines visible.
6584     (when (> (count-lines (window-start) (window-end)) 30)
6585       (move-to-window-line
6586        (min (max 0 scroll-margin)
6587             (max 1 (- (window-height)
6588                       (if mode-line-format 1 0)
6589                       (if header-line-format 1 0)
6590                       2)))))))
6591
6592 (defun gnus-article-next-page-1 (lines)
6593   (condition-case ()
6594       (let ((scroll-in-place nil)
6595             (auto-window-vscroll nil))
6596         (scroll-up lines))
6597     (end-of-buffer
6598      ;; Long lines may cause an end-of-buffer error.
6599      (goto-char (point-max))))
6600   (gnus-article-beginning-of-window))
6601
6602 (defun gnus-article-prev-page (&optional lines)
6603   "Show previous page of current article.
6604 Argument LINES specifies lines to be scrolled down."
6605   (interactive "p")
6606   (move-to-window-line 0)
6607   (if (and gnus-page-broken
6608            (bobp)
6609            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6610       (progn
6611         (gnus-narrow-to-page -1)        ;Go to previous page.
6612         (goto-char (point-max))
6613         (recenter (if gnus-article-over-scroll
6614                       (if lines
6615                           (max (if (featurep 'xemacs)
6616                                    lines
6617                                  (+ lines scroll-margin))
6618                                3)
6619                         (- (window-height) 2))
6620                     -1)))
6621     (prog1
6622         (condition-case ()
6623             (let ((scroll-in-place nil))
6624               (scroll-down lines))
6625           (beginning-of-buffer
6626            (goto-char (point-min))))
6627       (gnus-article-beginning-of-window))))
6628
6629 (defun gnus-article-only-boring-p ()
6630   "Decide whether there is only boring text remaining in the article.
6631 Something \"interesting\" is a word of at least two letters that does
6632 not have a face in `gnus-article-boring-faces'."
6633   (when (and gnus-article-skip-boring
6634              (boundp 'gnus-article-boring-faces)
6635              (symbol-value 'gnus-article-boring-faces))
6636     (save-excursion
6637       (let ((inhibit-point-motion-hooks t))
6638         (catch 'only-boring
6639           (while (re-search-forward "\\b\\w\\w" nil t)
6640             (forward-char -1)
6641             (when (not (gnus-intersection
6642                         (gnus-faces-at (point))
6643                         (symbol-value 'gnus-article-boring-faces)))
6644               (throw 'only-boring nil)))
6645           (throw 'only-boring t))))))
6646
6647 (defun gnus-article-refer-article ()
6648   "Read article specified by message-id around point."
6649   (interactive)
6650   (save-excursion
6651     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6652     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6653     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6654         (let ((msg-id (concat "<" (match-string 0) ">")))
6655           (set-buffer gnus-summary-buffer)
6656           (gnus-summary-refer-article msg-id))
6657       (error "No references around point"))))
6658
6659 (defun gnus-article-show-summary ()
6660   "Reconfigure windows to show summary buffer."
6661   (interactive)
6662   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6663       (error "There is no summary buffer for this article buffer")
6664     (gnus-article-set-globals)
6665     (gnus-configure-windows 'article)
6666     (gnus-summary-goto-subject gnus-current-article)
6667     (gnus-summary-position-point)))
6668
6669 (defun gnus-article-describe-briefly ()
6670   "Describe article mode commands briefly."
6671   (interactive)
6672   (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")))
6673
6674 (defun gnus-article-check-buffer ()
6675   "Beep if not in an article buffer."
6676   (unless (derived-mode-p 'gnus-article-mode)
6677     (error "Command invoked outside of a Gnus article buffer")))
6678
6679 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6680   "Read a summary buffer key sequence and execute it from the article buffer."
6681   (interactive "P")
6682   (gnus-article-check-buffer)
6683   (let ((nosaves
6684          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6685            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6686            "=" "^" "\M-^" "|"))
6687         (nosave-but-article
6688          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6689            "An" "Ap" [?A (meta return)] [?A delete]))
6690         (nosave-in-article
6691          '("AS" "\C-d"))
6692         (up-to-top
6693          '("n" "Gn" "p" "Gp"))
6694         keys new-sum-point)
6695     (with-current-buffer gnus-article-current-summary
6696       (let (gnus-pick-mode)
6697         (setq unread-command-events (nconc unread-command-events
6698                                            (list (or key last-command-event)))
6699               keys (if (featurep 'xemacs)
6700                        (events-to-keys (read-key-sequence nil t))
6701                      (read-key-sequence nil t)))))
6702
6703     (message "")
6704
6705     (cond
6706      ((eq (aref keys (1- (length keys))) ?\C-h)
6707       (gnus-article-describe-bindings (substring keys 0 -1)))
6708      ((or (member keys nosaves)
6709           (member keys nosave-but-article)
6710           (member keys nosave-in-article))
6711       (let (func)
6712         (save-window-excursion
6713           (pop-to-buffer gnus-article-current-summary)
6714           ;; We disable the pick minor mode commands.
6715           (let (gnus-pick-mode)
6716             (setq func (key-binding keys t))))
6717         (if (or (not func)
6718                 (numberp func))
6719             (ding)
6720           (unless (member keys nosave-in-article)
6721             (set-buffer gnus-article-current-summary))
6722           (when (and (symbolp func)
6723                      (get func 'disabled))
6724             (error "Function %s disabled" func))
6725           (call-interactively func)
6726           (setq new-sum-point (point)))
6727         (when (member keys nosave-but-article)
6728           (pop-to-buffer gnus-article-buffer))))
6729      (t
6730       ;; These commands should restore window configuration.
6731       (let ((obuf (current-buffer))
6732             (owin (current-window-configuration))
6733             win func in-buffer selected new-sum-start new-sum-hscroll err)
6734         (cond (not-restore-window
6735                (pop-to-buffer gnus-article-current-summary)
6736                (setq win (selected-window)))
6737               ((setq win (get-buffer-window gnus-article-current-summary))
6738                (select-window win))
6739               (t
6740                (let ((summary-buffer gnus-article-current-summary))
6741                  (gnus-configure-windows 'article)
6742                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6743                    (let ((gnus-buffer-configuration
6744                           '((article ((vertical 1.0
6745                                                 (summary 0.25 point)
6746                                                 (article 1.0)))))))
6747                      (gnus-configure-windows 'article))
6748                    (setq win (get-buffer-window summary-buffer 'visible)))
6749                  (gnus-select-frame-set-input-focus (window-frame win))
6750                  (select-window win))))
6751         (setq in-buffer (current-buffer))
6752         ;; We disable the pick minor mode commands.
6753         (setq func (let (gnus-pick-mode)
6754                      (key-binding keys t)))
6755         (when (get func 'disabled)
6756           (error "Function %s disabled" func))
6757         (if (and func
6758                  (functionp func)
6759                  (condition-case code
6760                      (progn
6761                        (call-interactively func)
6762                        t)
6763                    (error
6764                     (setq err code)
6765                     nil)))
6766             (progn
6767               (when (eq win (selected-window))
6768                 (setq new-sum-point (point)
6769                       new-sum-start (window-start win)
6770                       new-sum-hscroll (window-hscroll win)))
6771               (when (or (eq in-buffer (current-buffer))
6772                         (when (eq obuf (current-buffer))
6773                           (set-buffer in-buffer)
6774                           t))
6775                 (setq selected (gnus-summary-select-article))
6776                 (set-buffer obuf)
6777                 (unless not-restore-window
6778                   (set-window-configuration owin))
6779                 (when (and (eq selected 'old)
6780                            new-sum-point)
6781                   (set-window-start (get-buffer-window (current-buffer))
6782                                     1)
6783                   (set-window-point (get-buffer-window (current-buffer))
6784                                     (if (article-goto-body)
6785                                         (1- (point))
6786                                       (point))))
6787                 (when (and (not not-restore-window)
6788                            new-sum-point
6789                            (window-live-p win)
6790                            (with-current-buffer (window-buffer win)
6791                              (derived-mode-p 'gnus-summary-mode)))
6792                   (set-window-point win new-sum-point)
6793                   (set-window-start win new-sum-start)
6794                   (set-window-hscroll win new-sum-hscroll))))
6795           (set-window-configuration owin)
6796           (if err
6797               (signal (car err) (cdr err))
6798             (ding))))))))
6799
6800 (defun gnus-article-read-summary-send-keys ()
6801   (interactive)
6802   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6803     (gnus-article-read-summary-keys)))
6804
6805 (defun gnus-article-describe-key (key)
6806   "Display documentation of the function invoked by KEY.
6807 KEY is a string or a vector."
6808   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6809                        (read-key-sequence "Describe key: "))))
6810   (gnus-article-check-buffer)
6811   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6812                                   gnus-article-read-summary-send-keys))
6813       (with-current-buffer gnus-article-current-summary
6814         (setq unread-command-events
6815               (if (featurep 'xemacs)
6816                   (append key nil)
6817                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6818                                         (list 'meta (- x 128))
6819                                       x))
6820                         key)))
6821         (let ((cursor-in-echo-area t)
6822               gnus-pick-mode)
6823           (describe-key (read-key-sequence nil t))))
6824     (describe-key key)))
6825
6826 (defun gnus-article-describe-key-briefly (key &optional insert)
6827   "Display documentation of the function invoked by KEY.
6828 KEY is a string or a vector."
6829   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6830                        (read-key-sequence "Describe key: "))
6831                      current-prefix-arg))
6832   (gnus-article-check-buffer)
6833   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6834                                   gnus-article-read-summary-send-keys))
6835       (with-current-buffer gnus-article-current-summary
6836         (setq unread-command-events
6837               (if (featurep 'xemacs)
6838                   (append key nil)
6839                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6840                                         (list 'meta (- x 128))
6841                                       x))
6842                         key)))
6843         (let ((cursor-in-echo-area t)
6844               gnus-pick-mode)
6845           (describe-key-briefly (read-key-sequence nil t) insert)))
6846     (describe-key-briefly key insert)))
6847
6848 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6849 (defvar gnus-agent-summary-mode)
6850 (defvar gnus-draft-mode)
6851 (defvar help-xref-stack-item)
6852
6853 (defun gnus-article-describe-bindings (&optional prefix)
6854   "Show a list of all defined keys, and their definitions.
6855 The optional argument PREFIX, if non-nil, should be a key sequence;
6856 then we display only bindings that start with that prefix."
6857   (interactive)
6858   (gnus-article-check-buffer)
6859   (let ((keymap (copy-keymap gnus-article-mode-map))
6860         (map (copy-keymap gnus-article-send-map))
6861         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6862         parent agent draft)
6863     (define-key keymap "S" map)
6864     (define-key map [t] nil)
6865     (with-current-buffer gnus-article-current-summary
6866       (set-keymap-parent
6867        keymap
6868        (if (setq parent (keymap-parent gnus-article-mode-map))
6869            (prog1
6870                (setq parent (copy-keymap parent))
6871              (set-keymap-parent parent (current-local-map)))
6872          (current-local-map)))
6873       (set-keymap-parent map (key-binding "S"))
6874       (let (key def gnus-pick-mode)
6875         (while sumkeys
6876           (setq key (pop sumkeys))
6877           (cond ((and (vectorp key) (= (length key) 1)
6878                       (consp (setq def (aref key 0)))
6879                       (numberp (car def)) (numberp (cdr def)))
6880                  (when (< (max (car def) (cdr def)) 128)
6881                    (setq sumkeys
6882                          (append (mapcar
6883                                   #'vector
6884                                   (nreverse (gnus-uncompress-range def)))
6885                                  sumkeys))))
6886                 ((setq def (key-binding key))
6887                  (unless (eq def 'undefined)
6888                    (define-key keymap key def))))))
6889       (when (boundp 'gnus-agent-summary-mode)
6890         (setq agent gnus-agent-summary-mode))
6891       (when (boundp 'gnus-draft-mode)
6892         (setq draft gnus-draft-mode)))
6893     (with-temp-buffer
6894       (use-local-map keymap)
6895       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6896       (set (make-local-variable 'gnus-draft-mode) draft)
6897       (describe-bindings prefix))
6898     (let ((item `((lambda (prefix)
6899                     (with-current-buffer ,(current-buffer)
6900                       (gnus-article-describe-bindings prefix)))
6901                   ,prefix)))
6902       ;; Loading `help-mode' here is necessary if `describe-bindings'
6903       ;; is replaced with something, e.g. `helm-descbinds'.
6904       (require 'help-mode)
6905       (with-current-buffer (let (help-xref-following) (help-buffer))
6906         (setq help-xref-stack-item item)))))
6907
6908 (defun gnus-article-reply-with-original (&optional wide)
6909   "Start composing a reply mail to the current message.
6910 The text in the region will be yanked.  If the region isn't active,
6911 the entire article will be yanked."
6912   (interactive)
6913   (let ((article (cdr gnus-article-current))
6914         contents)
6915     (if (not (gnus-region-active-p))
6916         (with-current-buffer gnus-summary-buffer
6917           (gnus-summary-reply (list (list article)) wide))
6918       (setq contents (buffer-substring (point) (mark t)))
6919       ;; Deactivate active regions.
6920       (when (and (boundp 'transient-mark-mode)
6921                  transient-mark-mode)
6922         (setq mark-active nil))
6923       (with-current-buffer gnus-summary-buffer
6924         (gnus-summary-reply
6925          (list (list article contents)) wide)))))
6926
6927 (defun gnus-article-wide-reply-with-original ()
6928   "Start composing a wide reply mail to the current message.
6929 The text in the region will be yanked.  If the region isn't active,
6930 the entire article will be yanked."
6931   (interactive)
6932   (gnus-article-reply-with-original t))
6933
6934 (defun gnus-article-followup-with-original ()
6935   "Compose a followup to the current article.
6936 The text in the region will be yanked.  If the region isn't active,
6937 the entire article will be yanked."
6938   (interactive)
6939   (let ((article (cdr gnus-article-current))
6940         contents)
6941       (if (not (gnus-region-active-p))
6942           (with-current-buffer gnus-summary-buffer
6943             (gnus-summary-followup (list (list article))))
6944         (setq contents (buffer-substring (point) (mark t)))
6945         ;; Deactivate active regions.
6946         (when (and (boundp 'transient-mark-mode)
6947                    transient-mark-mode)
6948           (setq mark-active nil))
6949         (with-current-buffer gnus-summary-buffer
6950           (gnus-summary-followup
6951            (list (list article contents)))))))
6952
6953 (defun gnus-article-hide (&optional arg force)
6954   "Hide all the gruft in the current article.
6955 This means that signatures, cited text and (some) headers will be
6956 hidden.
6957 If given a prefix, show the hidden text instead."
6958   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6959   (gnus-article-hide-headers arg)
6960   (gnus-article-hide-list-identifiers arg)
6961   (gnus-article-hide-citation-maybe arg force)
6962   (gnus-article-hide-signature arg))
6963
6964 (defun gnus-check-group-server ()
6965   ;; Make sure the connection to the server is alive.
6966   (unless (gnus-server-opened
6967            (gnus-find-method-for-group gnus-newsgroup-name))
6968     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6969     (gnus-request-group gnus-newsgroup-name t)))
6970
6971 (eval-when-compile
6972   (autoload 'nneething-get-file-name "nneething"))
6973
6974 (defun gnus-request-article-this-buffer (article group)
6975   "Get an article and insert it into this buffer."
6976   (let (do-update-line sparse-header)
6977     (prog1
6978         (save-excursion
6979           (erase-buffer)
6980           (gnus-kill-all-overlays)
6981           (setq group (or group gnus-newsgroup-name))
6982
6983           ;; Using `gnus-request-article' directly will insert the article into
6984           ;; `nntp-server-buffer' - so we'll save some time by not having to
6985           ;; copy it from the server buffer into the article buffer.
6986
6987           ;; We only request an article by message-id when we do not have the
6988           ;; headers for it, so we'll have to get those.
6989           (when (stringp article)
6990             (gnus-read-header article))
6991
6992           ;; If the article number is negative, that means that this article
6993           ;; doesn't belong in this newsgroup (possibly), so we find its
6994           ;; message-id and request it by id instead of number.
6995           (when (and (numberp article)
6996                      gnus-summary-buffer
6997                      (get-buffer gnus-summary-buffer)
6998                      (gnus-buffer-exists-p gnus-summary-buffer))
6999             (with-current-buffer gnus-summary-buffer
7000               (let ((header (gnus-summary-article-header article)))
7001                 (when (< article 0)
7002                   (cond
7003                    ((memq article gnus-newsgroup-sparse)
7004                     ;; This is a sparse gap article.
7005                     (setq do-update-line article)
7006                     (setq article (mail-header-id header))
7007                     (setq sparse-header (gnus-read-header article))
7008                     (setq gnus-newsgroup-sparse
7009                           (delq article gnus-newsgroup-sparse)))
7010                    ((vectorp header)
7011                     ;; It's a real article.
7012                     (setq article (mail-header-id header)))
7013                    (t
7014                     ;; It is an extracted pseudo-article.
7015                     (setq article 'pseudo)
7016                     (gnus-request-pseudo-article header))))
7017
7018                 (let ((method (gnus-find-method-for-group
7019                                gnus-newsgroup-name)))
7020                   (when (and (eq (car method) 'nneething)
7021                              (vectorp header))
7022                     (let ((dir (nneething-get-file-name
7023                                 (mail-header-id header))))
7024                       (when (and (stringp dir)
7025                                  (file-directory-p dir))
7026                         (setq article 'nneething)
7027                         (gnus-group-enter-directory dir))))))))
7028
7029           (cond
7030            ;; Refuse to select canceled articles.
7031            ((and (numberp article)
7032                  gnus-summary-buffer
7033                  (get-buffer gnus-summary-buffer)
7034                  (gnus-buffer-exists-p gnus-summary-buffer)
7035                  (eq (cdr (with-current-buffer gnus-summary-buffer
7036                             (assq article gnus-newsgroup-reads)))
7037                      gnus-canceled-mark))
7038             nil)
7039            ;; We first check `gnus-original-article-buffer'.
7040            ((and (get-buffer gnus-original-article-buffer)
7041                  (numberp article)
7042                  (with-current-buffer gnus-original-article-buffer
7043                    (and (equal (car gnus-original-article) group)
7044                         (eq (cdr gnus-original-article) article))))
7045             ;; `insert-buffer-substring' would incorrectly use the
7046             ;; equivalent of string-make-multibyte which amount to decoding
7047             ;; with locale-coding-system, causing failure of
7048             ;; subsequent decoding.
7049             (insert (mm-string-to-multibyte
7050                      (with-current-buffer gnus-original-article-buffer
7051                        (buffer-substring (point-min) (point-max)))))
7052             'article)
7053            ;; Check the backlog.
7054            ((and gnus-keep-backlog
7055                  (gnus-backlog-request-article group article (current-buffer)))
7056             'article)
7057            ;; Check asynchronous pre-fetch.
7058            ((gnus-async-request-fetched-article group article (current-buffer))
7059             (gnus-async-prefetch-next group article gnus-summary-buffer)
7060             (when (and (numberp article) gnus-keep-backlog)
7061               (gnus-backlog-enter-article group article (current-buffer)))
7062             'article)
7063            ;; Check the cache.
7064            ((and gnus-use-cache
7065                  (numberp article)
7066                  (gnus-cache-request-article article group))
7067             'article)
7068            ;; Check the agent cache.
7069            ((gnus-agent-request-article article group)
7070             'article)
7071            ;; Get the article and put into the article buffer.
7072            ((or (stringp article)
7073                 (numberp article))
7074             (let ((gnus-override-method gnus-override-method)
7075                   (methods (and (stringp article)
7076                                 (with-current-buffer gnus-summary-buffer
7077                                   (gnus-refer-article-methods))))
7078                   (backend (car (gnus-find-method-for-group
7079                                  gnus-newsgroup-name)))
7080                   result
7081                   (inhibit-read-only t))
7082               (when (and (null gnus-override-method)
7083                          methods)
7084                 (setq gnus-override-method (pop methods)))
7085               (while (not result)
7086                 (erase-buffer)
7087                 (gnus-kill-all-overlays)
7088                 (let ((gnus-newsgroup-name group))
7089                   (gnus-check-group-server))
7090                 (cond
7091                  ((gnus-request-article article group (current-buffer))
7092                   (when (numberp article)
7093                     (gnus-async-prefetch-next group article
7094                                               gnus-summary-buffer)
7095                     (when gnus-keep-backlog
7096                       (gnus-backlog-enter-article
7097                        group article (current-buffer)))
7098                     (when (and gnus-agent
7099                                (gnus-agent-group-covered-p group))
7100                       (gnus-agent-store-article article group)))
7101                   (setq result 'article))
7102                  (methods
7103                   (setq gnus-override-method (pop methods)))
7104                  ((not (string-match "^400 "
7105                                      (nnheader-get-report backend)))
7106                   ;; If we get 400 server disconnect, reconnect and
7107                   ;; retry; otherwise, assume the article has expired.
7108                   (setq result 'done))))
7109               (and (eq result 'article) 'article)))
7110            ;; It was a pseudo.
7111            (t article)))
7112
7113       ;; Associate this article with the current summary buffer.
7114       (setq gnus-article-current-summary gnus-summary-buffer)
7115
7116       ;; Take the article from the original article buffer
7117       ;; and place it in the buffer it's supposed to be in.
7118       (when (and (get-buffer gnus-article-buffer)
7119                  (equal (buffer-name (current-buffer))
7120                         (buffer-name (get-buffer gnus-article-buffer))))
7121         (save-excursion
7122           (if (get-buffer gnus-original-article-buffer)
7123               (set-buffer gnus-original-article-buffer)
7124             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
7125             (buffer-disable-undo)
7126             (setq major-mode 'gnus-original-article-mode)
7127             (setq buffer-read-only t))
7128           (let ((inhibit-read-only t))
7129             (erase-buffer)
7130             (insert-buffer-substring gnus-article-buffer))
7131           (setq gnus-original-article (cons group article)))
7132
7133         ;; Decode charsets.
7134         (run-hooks 'gnus-article-decode-hook)
7135         ;; Mark article as decoded or not.
7136         (setq gnus-article-decoded-p gnus-article-decode-hook))
7137
7138       ;; Update sparse articles.
7139       (when (and do-update-line
7140                  (or (numberp article)
7141                      (stringp article)))
7142         (let ((buf (current-buffer)))
7143           (set-buffer gnus-summary-buffer)
7144           (gnus-summary-update-article do-update-line sparse-header)
7145           (gnus-summary-goto-subject do-update-line nil t)
7146           (set-window-point (gnus-get-buffer-window (current-buffer) t)
7147                             (point))
7148           (set-buffer buf))))))
7149
7150 (defun gnus-block-private-groups (group)
7151   (if (or (gnus-news-group-p group)
7152           (gnus-member-of-valid 'global group))
7153       ;; Block nothing in news groups.
7154       nil
7155     ;; Block everything anywhere else.
7156     "."))
7157
7158 (defun gnus-blocked-images ()
7159   (if (functionp gnus-blocked-images)
7160       (funcall gnus-blocked-images gnus-newsgroup-name)
7161     gnus-blocked-images))
7162
7163 ;;;
7164 ;;; Article editing
7165 ;;;
7166
7167 (defcustom gnus-article-edit-mode-hook nil
7168   "Hook run in article edit mode buffers."
7169   :group 'gnus-article-various
7170   :type 'hook)
7171
7172 (defvar gnus-article-edit-done-function nil)
7173
7174 (defvar gnus-article-edit-mode-map nil)
7175
7176 ;; Should we be using derived.el for this?
7177 (unless gnus-article-edit-mode-map
7178   (setq gnus-article-edit-mode-map (make-keymap))
7179   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
7180
7181   (gnus-define-keys gnus-article-edit-mode-map
7182     "\C-c?"    describe-mode
7183     "\C-c\C-c" gnus-article-edit-done
7184     "\C-c\C-k" gnus-article-edit-exit
7185     "\C-c\C-f\C-t" message-goto-to
7186     "\C-c\C-f\C-o" message-goto-from
7187     "\C-c\C-f\C-b" message-goto-bcc
7188     ;;"\C-c\C-f\C-w" message-goto-fcc
7189     "\C-c\C-f\C-c" message-goto-cc
7190     "\C-c\C-f\C-s" message-goto-subject
7191     "\C-c\C-f\C-r" message-goto-reply-to
7192     "\C-c\C-f\C-n" message-goto-newsgroups
7193     "\C-c\C-f\C-d" message-goto-distribution
7194     "\C-c\C-f\C-f" message-goto-followup-to
7195     "\C-c\C-f\C-m" message-goto-mail-followup-to
7196     "\C-c\C-f\C-k" message-goto-keywords
7197     "\C-c\C-f\C-u" message-goto-summary
7198     "\C-c\C-f\C-i" message-insert-or-toggle-importance
7199     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
7200     "\C-c\C-b" message-goto-body
7201     "\C-c\C-i" message-goto-signature
7202
7203     "\C-c\C-t" message-insert-to
7204     "\C-c\C-n" message-insert-newsgroups
7205     "\C-c\C-o" message-sort-headers
7206     "\C-c\C-e" message-elide-region
7207     "\C-c\C-v" message-delete-not-region
7208     "\C-c\C-z" message-kill-to-signature
7209     "\M-\r" message-newline-and-reformat
7210     "\C-c\C-a" mml-attach-file
7211     "\C-a" message-beginning-of-line
7212     "\t" message-tab
7213     "\M-;" comment-region)
7214
7215   (gnus-define-keys (gnus-article-edit-wash-map
7216                      "\C-c\C-w" gnus-article-edit-mode-map)
7217     "f" gnus-article-edit-full-stops))
7218
7219 (easy-menu-define
7220   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7221   '("Field"
7222     ["Fetch To" message-insert-to t]
7223     ["Fetch Newsgroups" message-insert-newsgroups t]
7224     "----"
7225     ["To" message-goto-to t]
7226     ["From" message-goto-from t]
7227     ["Subject" message-goto-subject t]
7228     ["Cc" message-goto-cc t]
7229     ["Reply-To" message-goto-reply-to t]
7230     ["Summary" message-goto-summary t]
7231     ["Keywords" message-goto-keywords t]
7232     ["Newsgroups" message-goto-newsgroups t]
7233     ["Followup-To" message-goto-followup-to t]
7234     ["Mail-Followup-To" message-goto-mail-followup-to t]
7235     ["Distribution" message-goto-distribution t]
7236     ["Body" message-goto-body t]
7237     ["Signature" message-goto-signature t]))
7238
7239 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7240   "Major mode for editing articles.
7241 This is an extended text-mode.
7242
7243 \\{gnus-article-edit-mode-map}"
7244   (make-local-variable 'gnus-article-edit-done-function)
7245   (make-local-variable 'gnus-prev-winconf)
7246   (set (make-local-variable 'font-lock-defaults)
7247        '(message-font-lock-keywords t))
7248   (set (make-local-variable 'mail-header-separator) "")
7249   (set (make-local-variable 'gnus-article-edit-mode) t)
7250   (easy-menu-add message-mode-field-menu message-mode-map)
7251   (mml-mode)
7252   (setq buffer-read-only nil)
7253   (buffer-enable-undo)
7254   (widen))
7255
7256 (defun gnus-article-edit (&optional force)
7257   "Edit the current article.
7258 This will have permanent effect only in mail groups.
7259 If FORCE is non-nil, allow editing of articles even in read-only
7260 groups."
7261   (interactive "P")
7262   (when (and (not force)
7263              (gnus-group-read-only-p))
7264     (error "The current newsgroup does not support article editing"))
7265   (gnus-article-date-original)
7266   (gnus-article-edit-article
7267    'ignore
7268    `(lambda (no-highlight)
7269       'ignore
7270       (gnus-summary-edit-article-done
7271        ,(or (mail-header-references gnus-current-headers) "")
7272        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7273
7274 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7275   "Start editing the contents of the current article buffer."
7276   (let ((winconf (current-window-configuration)))
7277     (set-buffer gnus-article-buffer)
7278     (let ((message-auto-save-directory
7279            ;; Don't associate the article buffer with a draft file.
7280            nil))
7281       (gnus-article-edit-mode))
7282     (funcall start-func)
7283     (set-buffer-modified-p nil)
7284     (gnus-configure-windows 'edit-article)
7285     (setq gnus-article-edit-done-function exit-func)
7286     (setq gnus-prev-winconf winconf)
7287     (unless quiet
7288       (gnus-message 6 "C-c C-c to end edits"))))
7289
7290 (defun gnus-article-edit-done (&optional arg)
7291   "Update the article edits and exit."
7292   (interactive "P")
7293   (let ((func gnus-article-edit-done-function)
7294         (buf (current-buffer))
7295         (start (window-start))
7296         (p (point))
7297         (winconf gnus-prev-winconf))
7298     (widen) ;; Widen it in case that users narrowed the buffer.
7299     (funcall func arg)
7300     (set-buffer buf)
7301     ;; The cache and backlog have to be flushed somewhat.
7302     (when gnus-keep-backlog
7303       (gnus-backlog-remove-article
7304        (car gnus-article-current) (cdr gnus-article-current)))
7305     ;; Flush original article as well.
7306     (gnus-flush-original-article-buffer)
7307     (when gnus-use-cache
7308       (gnus-cache-update-article
7309        (car gnus-article-current) (cdr gnus-article-current)))
7310     ;; We remove all text props from the article buffer.
7311     (kill-all-local-variables)
7312     (set-text-properties (point-min) (point-max) nil)
7313     (gnus-article-mode)
7314     (set-window-configuration winconf)
7315     (set-buffer buf)
7316     (set-window-start (get-buffer-window buf) start)
7317     (set-window-point (get-buffer-window buf) (point)))
7318   (gnus-summary-show-article))
7319
7320 (defun gnus-flush-original-article-buffer ()
7321   (when (get-buffer gnus-original-article-buffer)
7322     (with-current-buffer gnus-original-article-buffer
7323       (setq gnus-original-article nil))))
7324
7325 (defun gnus-article-edit-exit ()
7326   "Exit the article editing without updating."
7327   (interactive)
7328   (when (or (not (buffer-modified-p))
7329             (yes-or-no-p "Article modified; kill anyway? "))
7330     (let ((curbuf (current-buffer))
7331           (p (point))
7332           (window-start (window-start)))
7333       (erase-buffer)
7334       (if (gnus-buffer-live-p gnus-original-article-buffer)
7335           (insert-buffer-substring gnus-original-article-buffer))
7336       (let ((winconf gnus-prev-winconf))
7337         (kill-all-local-variables)
7338         (gnus-article-mode)
7339         (set-window-configuration winconf)
7340         ;; Tippy-toe some to make sure that point remains where it was.
7341         (save-current-buffer
7342           (set-buffer curbuf)
7343           (set-window-start (get-buffer-window (current-buffer)) window-start)
7344           (goto-char p))))
7345     (gnus-summary-show-article)))
7346
7347 (defun gnus-article-edit-full-stops ()
7348   "Interactively repair spacing at end of sentences."
7349   (interactive)
7350   (save-excursion
7351     (goto-char (point-min))
7352     (search-forward-regexp "^$" nil t)
7353     (let ((case-fold-search nil))
7354       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7355
7356 ;;;
7357 ;;; Article highlights
7358 ;;;
7359
7360 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7361
7362 ;;; Internal Variables:
7363
7364 (defcustom gnus-button-url-regexp
7365   (concat
7366    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7367    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7368    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7369    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7370        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7371              (punct "!?:;.,"))
7372          (concat
7373           "\\(?:"
7374           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7375           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7376           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)"
7377           "\\(?:" "[" chars punct "]+" "[" chars "]" "\\)?"
7378           "\\|"
7379           "[" chars punct "]+" "[" chars "]"
7380           "\\)"))
7381      (concat ;; XEmacs 21.4 doesn't support POSIX.
7382       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7383       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7384    "\\)")
7385   "Regular expression that matches URLs."
7386   :version "24.4"
7387   :group 'gnus-article-buttons
7388   :type 'regexp)
7389
7390 (defcustom gnus-button-valid-fqdn-regexp
7391   message-valid-fqdn-regexp
7392   "Regular expression that matches a valid FQDN."
7393   :version "22.1"
7394   :group 'gnus-article-buttons
7395   :type 'regexp)
7396
7397 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7398 (defcustom gnus-button-valid-localpart-regexp
7399   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7400   "Regular expression that matches a localpart of mail addresses or MIDs."
7401   :version "22.1"
7402   :group 'gnus-article-buttons
7403   :type 'regexp)
7404
7405 (defcustom gnus-button-man-handler 'manual-entry
7406   "Function to use for displaying man pages.
7407 The function must take at least one argument with a string naming the
7408 man page."
7409   :version "22.1"
7410   :type '(choice (function-item :tag "Man" manual-entry)
7411                  (function-item :tag "Woman" woman)
7412                  (function :tag "Other"))
7413   :group 'gnus-article-buttons)
7414
7415 (defcustom gnus-button-mid-or-mail-regexp
7416   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7417           gnus-button-valid-fqdn-regexp
7418           ">?\\)\\b")
7419   "Regular expression that matches a message ID or a mail address."
7420   :version "22.1"
7421   :group 'gnus-article-buttons
7422   :type 'regexp)
7423
7424 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7425   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7426 Strings like this can be either a message ID or a mail address.  If it is one
7427 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7428 message ID or a mail address, respectively.  If this variable is set to the
7429 symbol `ask', always query the user what do do.  If it is a function, this
7430 function will be called with the string as its only argument.  The function
7431 must return `mid', `mail', `invalid' or `ask'."
7432   :version "22.1"
7433   :group 'gnus-article-buttons
7434   :type '(choice (function-item :tag "Heuristic function"
7435                                 gnus-button-mid-or-mail-heuristic)
7436                  (const ask)
7437                  (const mid)
7438                  (const mail)))
7439
7440 (defcustom gnus-button-mid-or-mail-heuristic-alist
7441   '((-10.0 . ".+\\$.+@")
7442     (-10.0 . "#")
7443     (-10.0 . "\\*")
7444     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7445     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7446     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7447     (-1.0  . "^[^a-z]+@")
7448     ;;
7449     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7450     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7451     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7452     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7453     ;;
7454     (-2.0  . "^[0-9]")
7455     (-1.0  . "^[0-9][0-9]")
7456     ;;
7457     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7458     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7459     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7460     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7461     ;;
7462     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7463     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7464     ;;       "[0-9]{8,}.*\@"
7465     (-3.0
7466      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7467     ;; "[0-9]{12,}.*\@"
7468     ;; compensation for TDMA dated mail addresses:
7469     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7470     ;;
7471     (-20.0 . "\\.fsf@") ;; Gnus
7472     (-20.0 . "^slrn")
7473     (-20.0 . "^Pine")
7474     (-20.0 . "^alpine\\.")
7475     (-20.0 . "_-_") ;; Subject change in thread
7476     ;;
7477     (-20.0 . "\\.ln@") ;; leafnode
7478     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7479     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7480     ;;
7481     ;; (5.0 . "") ;; $local_part_len <= 7
7482     (10.0  . "^[^0-9]+@")
7483     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7484     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7485     (3.0   . "\@stud")
7486     ;;
7487     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7488     ;;
7489     (0.5   . "^[A-Z][a-z]")
7490     (0.5   . "^[A-Z][a-z][a-z]")
7491     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7492     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7493   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7494
7495 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7496 address.  The REGEXP is processed with `case-fold-search' set to nil."
7497   :version "22.1"
7498   :group 'gnus-article-buttons
7499   :type '(repeat (cons (number :tag "Rate")
7500                        (regexp :tag "Regexp"))))
7501
7502 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7503   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7504 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7505 address, `ask' if unsure and `invalid' if the string is invalid."
7506   (let ((case-fold-search nil)
7507         (list gnus-button-mid-or-mail-heuristic-alist)
7508         (result 0) rate regexp lpartlen elem)
7509     (setq lpartlen
7510           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7511     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7512     ;; Certain special cases...
7513     (when (string-match
7514            (concat
7515             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7516             "^[0-9]+\\.[0-9]+@compuserve\\|"
7517             "@public\\.gmane\\.org")
7518            mid-or-mail)
7519       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7520       (setq result 'mail))
7521     (when (string-match "@.*@\\| " mid-or-mail)
7522       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7523       (setq result 'invalid))
7524     ;; Nothing more to do, if result is not a number here...
7525     (when (numberp result)
7526       (while list
7527         (setq elem (car list)
7528               rate (car elem)
7529               regexp (cdr elem)
7530               list (cdr list))
7531         (when (string-match regexp mid-or-mail)
7532           (setq result (+ result rate))
7533           (gnus-message
7534            9 "`%s' matched `%s', rate `%s', result `%s'."
7535            mid-or-mail regexp rate result)))
7536       (when (<= lpartlen 7)
7537         (setq result (+ result 5.0))
7538         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7539                       mid-or-mail result))
7540       (when (>= lpartlen 12)
7541         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7542         (cond
7543          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7544           ;; Long local part should contain realname if e-mail address,
7545           ;; too many digits: message-id.
7546           ;; $score -= 5.0 + 0.1 * $local_part_len;
7547           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7548           (setq result (+ result rate))
7549           (gnus-message
7550            9 "Many digits in `%s', rate `%s', result `%s'."
7551            mid-or-mail rate result))
7552          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7553                         mid-or-mail)
7554           ;; Too few vowels [^aeiouy]{4,}.*\@
7555           (setq result (+ result -5.0))
7556           (gnus-message
7557            9 "Few vowels in `%s', rate `%s', result `%s'."
7558            mid-or-mail -5.0 result))
7559          (t
7560           (setq result (+ result 5.0))
7561           (gnus-message
7562            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7563     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7564     ;; Maybe we should make this a customizable alist: (condition . 'result)
7565     (cond
7566      ((symbolp result) result)
7567      ;; Now convert number into proper results:
7568      ((< result -10.0) 'mid)
7569      ((> result  10.0) 'mail)
7570      (t 'ask))))
7571
7572 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7573   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7574          (url-mid (concat "news" ":" mid-or-mail))
7575          (url-mailto (concat "mailto" ":" mid-or-mail)))
7576     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7577     (when (fboundp pref)
7578       (setq guessed
7579             ;; get rid of surrounding angles...
7580             (funcall pref
7581                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7582       (if (or (eq 'mid guessed) (eq 'mail guessed))
7583           (setq pref guessed)
7584         (setq pref 'ask)))
7585     (if (eq pref 'ask)
7586         (save-window-excursion
7587           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7588               (setq pref 'mail)
7589             (setq pref 'mid))))
7590     (cond ((eq pref 'mid)
7591            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7592            (gnus-button-handle-news url-mid))
7593           ((eq pref 'mail)
7594            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7595            (gnus-url-mailto url-mailto))
7596           (t (gnus-message 3 "Invalid string.")))))
7597
7598 (defun gnus-button-handle-custom (fun arg)
7599   "Call function FUN on argument ARG.
7600 Both FUN and ARG are supposed to be strings.  ARG will be passed
7601 as a symbol to FUN."
7602   (funcall (intern fun)
7603            (if (string-match "^customize-apropos" fun)
7604                arg
7605              (intern arg))))
7606
7607 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7608
7609 (defun gnus-button-handle-describe-function (url)
7610   "Call `describe-function' when pushing the corresponding URL button."
7611   (describe-function
7612    (intern
7613     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7614
7615 (defun gnus-button-handle-describe-variable (url)
7616   "Call `describe-variable' when pushing the corresponding URL button."
7617   (describe-variable
7618    (intern
7619     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7620
7621 (defun gnus-button-handle-symbol (url)
7622 "Display help on variable or function.
7623 Calls `describe-variable' or `describe-function'."
7624   (let ((sym (intern url)))
7625     (cond
7626      ((fboundp sym) (describe-function sym))
7627      ((boundp sym) (describe-variable sym))
7628      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7629
7630 (defun gnus-button-handle-describe-key (url)
7631   "Call `describe-key' when pushing the corresponding URL button."
7632   (let* ((key-string
7633           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7634          (keys (ignore-errors (eval `(kbd ,key-string)))))
7635     (if keys
7636         (describe-key keys)
7637       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7638
7639 (defun gnus-button-handle-apropos (url)
7640   "Call `apropos' when pushing the corresponding URL button."
7641   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7642
7643 (defun gnus-button-handle-apropos-command (url)
7644   "Call `apropos' when pushing the corresponding URL button."
7645   (apropos-command
7646    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7647
7648 (defun gnus-button-handle-apropos-variable (url)
7649   "Call `apropos' when pushing the corresponding URL button."
7650   (funcall
7651    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7652    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7653
7654 (defun gnus-button-handle-apropos-documentation (url)
7655   "Call `apropos' when pushing the corresponding URL button."
7656   (funcall
7657    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7658    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7659
7660 (defun gnus-button-handle-library (url)
7661   "Call `locate-library' when pushing the corresponding URL button."
7662   (gnus-message 9 "url=`%s'" url)
7663   (let* ((lib (locate-library url))
7664          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7665     (if (not lib)
7666         (gnus-message 1 "Cannot locale library `%s'." url)
7667       (find-file-read-only file))))
7668
7669 (defcustom gnus-button-man-level 5
7670   "*Integer that says how many man-related buttons Gnus will show.
7671 The higher the number, the more buttons will appear and the more false
7672 positives are possible.  Note that you can set this variable local to
7673 specific groups.  Setting it higher in Unix groups is probably a good idea.
7674 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7675 how to set variables in specific groups."
7676   :version "22.1"
7677   :group 'gnus-article-buttons
7678   :link '(custom-manual "(gnus)Group Parameters")
7679   :type 'integer)
7680
7681 (defcustom gnus-button-emacs-level 5
7682   "*Integer that says how many emacs-related buttons Gnus will show.
7683 The higher the number, the more buttons will appear and the more false
7684 positives are possible.  Note that you can set this variable local to
7685 specific groups.  Setting it higher in Emacs or Gnus related groups is
7686 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7687 `gnus-parameters' on how to set variables in specific groups."
7688   :version "22.1"
7689   :group 'gnus-article-buttons
7690   :link '(custom-manual "(gnus)Group Parameters")
7691   :type 'integer)
7692
7693 (defcustom gnus-button-message-level 5
7694   "*Integer that says how many buttons for news or mail messages will appear.
7695 The higher the number, the more buttons will appear and the more false
7696 positives are possible."
7697   ;; mail addresses, MIDs, URLs for news, ...
7698   :version "22.1"
7699   :group 'gnus-article-buttons
7700   :type 'integer)
7701
7702 (defcustom gnus-button-browse-level 5
7703   "*Integer that says how many buttons for browsing will appear.
7704 The higher the number, the more buttons will appear and the more false
7705 positives are possible."
7706   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7707   :version "22.1"
7708   :group 'gnus-article-buttons
7709   :type 'integer)
7710
7711 (defcustom gnus-button-alist
7712   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7713      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7714     ((concat "\\b\\(nntp\\|news\\):\\("
7715              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7716      0 t gnus-button-handle-news 2)
7717     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7718      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7719     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7720      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7721     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7722     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7723      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7724     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7725      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7726     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7727      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7728     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7729      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7730     ;; RFC 2368 (The mailto URL scheme)
7731     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7732      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7733     ("\\bmailto:\\([^ \n\t]+\\)"
7734      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7735     ;; Info Konqueror style <info:/foo/bar baz>.
7736     ;; Must come before " Gnus home-grown style".
7737     ("\\binfo://?\\([^'\">\n\t]+\\)"
7738      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7739    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7740     ("\\binfo://\\([^'\">\n\t ]+\\)"
7741      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7742     ;; Info GNOME style <info:foo#bar_baz>
7743     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7744      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7745     ;; Info KDE style <info:(foo)bar baz>
7746     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7747      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7748     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7749      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7750     ("\\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,]*\\)\\)?"
7751      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7752      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7753     ;; This is custom
7754     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7755      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7756     ;; Emacs help commands
7757     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7758      ;; regexp doesn't match arguments containing ` '.
7759      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7760     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7761      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7762     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7763      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7764     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7765      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7766     ;; The following entries may lead to many false positives so don't enable
7767     ;; them by default (use a high button level).
7768     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7769      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7770      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7771     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7772      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7773     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7774      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7775     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7776      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7777     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7778      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7779     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7780      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7781     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7782      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7783     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7784      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7785     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7786      ;; Unlike the other regexps we really have to require quoting
7787      ;; here to determine where it ends.
7788      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7789     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7790     ("<URL: *\\([^\n<>]*\\)>"
7791      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7792     ;; RFC 2396 (2.4.3., delims) ...
7793     ("\"URL: *\\([^\n\"]*\\)\""
7794      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7795     ;; Raw URLs.
7796     (gnus-button-url-regexp
7797      0 (>= gnus-button-browse-level 0) browse-url 0)
7798     ;; man pages
7799     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7800      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7801      gnus-button-handle-man 1)
7802     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7803     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7804      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7805      gnus-button-handle-man 1)
7806     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7807     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7808     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7809      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7810     ;; Recognizing patches to .el files.  This is somewhat obscure,
7811     ;; but considering the percentage of Gnus users who hack Emacs
7812     ;; Lisp files...
7813     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7814      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7815     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7816      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7817     ;; MID or mail: To avoid too many false positives we don't try to catch
7818     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7819     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7820     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7821     ;; so that non-ambiguous entries (see above) match first.
7822     (gnus-button-mid-or-mail-regexp
7823      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7824   "*Alist of regexps matching buttons in article bodies.
7825
7826 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7827 REGEXP: is the string (case insensitive) matching text around the button (can
7828 also be Lisp expression evaluating to a string),
7829 BUTTON: is the number of the regexp grouping actually matching the button,
7830 FORM: is a Lisp expression which must eval to true for the button to
7831 be added,
7832 CALLBACK: is the function to call when the user push this button, and each
7833 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7834
7835 CALLBACK can also be a variable, in that case the value of that
7836 variable it the real callback function."
7837   :group 'gnus-article-buttons
7838   :type '(repeat (list (choice regexp variable sexp)
7839                        (integer :tag "Button")
7840                        (sexp :tag "Form")
7841                        (function :tag "Callback")
7842                        (repeat :tag "Par"
7843                                :inline t
7844                                (integer :tag "Regexp group")))))
7845 (put 'gnus-button-alist 'risky-local-variable t)
7846
7847 (defcustom gnus-header-button-alist
7848   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7849      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7850     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7851      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7852     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7853      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7854     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7855      0 (>= gnus-button-browse-level 0) browse-url 0)
7856     ("^Subject:" gnus-button-url-regexp
7857      0 (>= gnus-button-browse-level 0) browse-url 0)
7858     ("^[^:]+:" gnus-button-url-regexp
7859      0 (>= gnus-button-browse-level 0) browse-url 0)
7860     ("^OpenPGP:.*url=" gnus-button-url-regexp
7861      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7862     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7863      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7864     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7865      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7866   "*Alist of headers and regexps to match buttons in article heads.
7867
7868 This alist is very similar to `gnus-button-alist', except that each
7869 alist has an additional HEADER element first in each entry:
7870
7871 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7872
7873 HEADER is a regexp to match a header.  For a fuller explanation, see
7874 `gnus-button-alist'."
7875   :group 'gnus-article-buttons
7876   :group 'gnus-article-headers
7877   :type '(repeat (list (regexp :tag "Header")
7878                        (choice regexp variable)
7879                        (integer :tag "Button")
7880                        (sexp :tag "Form")
7881                        (function :tag "Callback")
7882                        (repeat :tag "Par"
7883                                :inline t
7884                                (integer :tag "Regexp group")))))
7885 (put 'gnus-header-button-alist 'risky-local-variable t)
7886
7887 ;;; Commands:
7888
7889 (defun gnus-article-push-button (event)
7890   "Check text under the mouse pointer for a callback function.
7891 If the text under the mouse pointer has a `gnus-callback' property,
7892 call it with the value of the `gnus-data' text property."
7893   (interactive "e")
7894   (set-buffer (window-buffer (posn-window (event-start event))))
7895   (let* ((pos (posn-point (event-start event)))
7896          (data (get-text-property pos 'gnus-data))
7897          (fun (get-text-property pos 'gnus-callback)))
7898     (goto-char pos)
7899     (when fun
7900       (funcall fun data))))
7901
7902 (defun gnus-article-press-button ()
7903   "Check text at point for a callback function.
7904 If the text at point has a `gnus-callback' property,
7905 call it with the value of the `gnus-data' text property."
7906   (interactive)
7907   (let ((data (get-text-property (point) 'gnus-data))
7908         (fun (get-text-property (point) 'gnus-callback)))
7909     (when fun
7910       (funcall fun data))))
7911
7912 (defun gnus-article-highlight (&optional force)
7913   "Highlight current article.
7914 This function calls `gnus-article-highlight-headers',
7915 `gnus-article-highlight-citation',
7916 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7917 do the highlighting.  See the documentation for those functions."
7918   (interactive (list 'force))
7919   (gnus-article-highlight-headers)
7920   (gnus-article-highlight-citation force)
7921   (gnus-article-highlight-signature)
7922   (gnus-article-add-buttons)
7923   (gnus-article-add-buttons-to-head))
7924
7925 (defun gnus-article-highlight-some (&optional force)
7926   "Highlight current article.
7927 This function calls `gnus-article-highlight-headers',
7928 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7929 do the highlighting.  See the documentation for those functions."
7930   (interactive (list 'force))
7931   (gnus-article-highlight-headers)
7932   (gnus-article-highlight-signature)
7933   (gnus-article-add-buttons))
7934
7935 (defun gnus-article-highlight-headers ()
7936   "Highlight article headers as specified by `gnus-header-face-alist'."
7937   (interactive)
7938   (gnus-with-article-headers
7939     (let (regexp header-face field-face from hpoints fpoints)
7940       (dolist (entry gnus-header-face-alist)
7941         (goto-char (point-min))
7942         (setq regexp (concat "^\\("
7943                              (if (string-equal "" (nth 0 entry))
7944                                  "[^\t ]"
7945                                (nth 0 entry))
7946                              "\\)")
7947               header-face (nth 1 entry)
7948               field-face (nth 2 entry))
7949         (while (and (re-search-forward regexp nil t)
7950                     (not (eobp)))
7951           (beginning-of-line)
7952           (setq from (point))
7953           (unless (search-forward ":" nil t)
7954             (forward-char 1))
7955           (when (and header-face
7956                      (not (memq (point) hpoints)))
7957             (push (point) hpoints)
7958             (gnus-put-text-property from (point) 'face header-face))
7959           (when (and field-face
7960                      (not (memq (setq from (point)) fpoints)))
7961             (push from fpoints)
7962             (if (re-search-forward "^[^ \t]" nil t)
7963                 (forward-char -2)
7964               (goto-char (point-max)))
7965             (gnus-put-text-property from (point) 'face field-face)))))))
7966
7967 (defun gnus-article-highlight-signature ()
7968   "Highlight the signature in an article.
7969 It does this by highlighting everything after
7970 `gnus-signature-separator' using the face `gnus-signature'."
7971   (interactive)
7972   (gnus-with-article-buffer
7973     (let ((inhibit-point-motion-hooks t))
7974       (save-restriction
7975         (when (and gnus-signature-face
7976                    (gnus-article-narrow-to-signature))
7977           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7978                             'face gnus-signature-face)
7979           (widen)
7980           (gnus-article-search-signature)
7981           (let ((start (match-beginning 0))
7982                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7983             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7984                                      end)))))))
7985
7986 (defun gnus-button-in-region-p (b e prop)
7987   "Say whether PROP exists in the region."
7988   (text-property-not-all b e prop nil))
7989
7990 (defun gnus-article-add-buttons ()
7991   "Find external references in the article and make buttons of them.
7992 \"External references\" are things like Message-IDs and URLs, as
7993 specified by `gnus-button-alist'."
7994   (interactive)
7995   (gnus-with-article-buffer
7996     (let ((inhibit-point-motion-hooks t)
7997           (case-fold-search t)
7998           (alist gnus-button-alist)
7999           beg entry regexp)
8000       ;; We skip the headers.
8001       (article-goto-body)
8002       (setq beg (point))
8003       (while (setq entry (pop alist))
8004         (setq regexp (eval (car entry)))
8005         (goto-char beg)
8006         (while (re-search-forward regexp nil t)
8007           (let ((start (match-beginning (nth 1 entry)))
8008                 (end (match-end (nth 1 entry)))
8009                 (from (match-beginning 0)))
8010             (when (and (eval (nth 2 entry))
8011                        (not (gnus-button-in-region-p
8012                              start end 'gnus-callback)))
8013               ;; That optional form returned non-nil, so we add the
8014               ;; button.
8015               (setq from (set-marker (make-marker) from))
8016               (unless (and (eq (car entry) 'gnus-button-url-regexp)
8017                            (gnus-article-extend-url-button from start end))
8018                 (gnus-article-add-button start end
8019                                          'gnus-button-push (list from entry))
8020                 (gnus-put-text-property
8021                  start end
8022                  'gnus-string (buffer-substring-no-properties
8023                                start end))))))))))
8024
8025 (defun gnus-article-extend-url-button (beg start end)
8026   "Extend url button if url is folded into two or more lines.
8027 Return non-nil if button is extended.  BEG is a marker that points to
8028 the beginning position of a text containing url.  START and END are
8029 the endpoints of a url button before it is extended.  The concatenated
8030 url is put as the `gnus-button-url' overlay property on the button."
8031   (let ((opoint (point))
8032         (points (list start end))
8033         url delim regexp)
8034     (prog1
8035         (when (and (progn
8036                      (goto-char end)
8037                      (not (looking-at "[\t ]*[\">]")))
8038                    (progn
8039                      (goto-char start)
8040                      (string-match
8041                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
8042                       (buffer-substring (point-at-bol) start)))
8043                    (progn
8044                      (setq url (list (buffer-substring start end))
8045                            delim (if (match-beginning 1) ">" "\""))
8046                      (beginning-of-line)
8047                      (setq regexp (concat
8048                                    (when (and (looking-at
8049                                                message-cite-prefix-regexp)
8050                                               (< (match-end 0) start))
8051                                      (regexp-quote (match-string 0)))
8052                                    "\
8053 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
8054                                    delim "\\)"))
8055                      (while (progn
8056                               (forward-line 1)
8057                               (and (looking-at regexp)
8058                                    (prog1
8059                                        (match-beginning 1)
8060                                      (push (or (match-string 2)
8061                                                (match-string 1))
8062                                            url)
8063                                      (push (setq end (or (match-end 2)
8064                                                          (match-end 1)))
8065                                            points)
8066                                      (push (or (match-beginning 2)
8067                                                (match-beginning 1))
8068                                            points)))))
8069                      (match-beginning 2)))
8070           (let (gnus-article-mouse-face widget-mouse-face)
8071             (while points
8072               (gnus-article-add-button (pop points) (pop points)
8073                                        'gnus-button-push
8074                                        (list beg (assq 'gnus-button-url-regexp
8075                                                        gnus-button-alist)))))
8076           (let ((overlay (gnus-make-overlay start end)))
8077             (gnus-overlay-put overlay 'evaporate t)
8078             (gnus-overlay-put overlay 'gnus-button-url
8079                               (list (mapconcat 'identity (nreverse url) "")))
8080             (when gnus-article-mouse-face
8081               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
8082           t)
8083       (goto-char opoint))))
8084
8085 ;; Add buttons to the head of an article.
8086 (defun gnus-article-add-buttons-to-head ()
8087   "Add buttons to the head of the article."
8088   (interactive)
8089   (gnus-with-article-headers
8090     (let (beg end)
8091       (dolist (entry gnus-header-button-alist)
8092         ;; Each alist entry.
8093         (goto-char (point-min))
8094         (while (re-search-forward (car entry) nil t)
8095           ;; Each header matching the entry.
8096           (setq beg (match-beginning 0))
8097           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
8098                              (match-beginning 0))
8099                         (point-max)))
8100           (goto-char beg)
8101           (while (re-search-forward (eval (nth 1 entry)) end t)
8102             ;; Each match within a header.
8103             (let* ((entry (cdr entry))
8104                    (start (match-beginning (nth 1 entry)))
8105                    (end (match-end (nth 1 entry)))
8106                    (form (nth 2 entry)))
8107               (goto-char (match-end 0))
8108               (when (eval form)
8109                 (gnus-article-add-button
8110                  start end (nth 3 entry)
8111                  (buffer-substring (match-beginning (nth 4 entry))
8112                                    (match-end (nth 4 entry)))))))
8113           (goto-char end))))))
8114
8115 ;;; External functions:
8116
8117 (defun gnus-article-add-button (from to fun &optional data text)
8118   "Create a button between FROM and TO with callback FUN and data DATA."
8119   (when gnus-article-button-face
8120     (gnus-overlay-put (gnus-make-overlay from to nil t)
8121                       'face gnus-article-button-face))
8122   (gnus-add-text-properties
8123    from to
8124    (nconc (and gnus-article-mouse-face
8125                (list gnus-mouse-face-prop gnus-article-mouse-face))
8126           (list 'gnus-callback fun)
8127           (and data (list 'gnus-data data))))
8128   (widget-convert-button 'link from to :action 'gnus-widget-press-button
8129                          :help-echo (or text "Follow the link")
8130                          :keymap gnus-url-button-map
8131                          :button-keymap gnus-widget-button-keymap))
8132
8133 (defun gnus-article-copy-string ()
8134   "Copy the string in the button to the kill ring."
8135   (interactive)
8136   (gnus-article-check-buffer)
8137   (let ((data (get-text-property (point) 'gnus-string)))
8138     (when data
8139       (with-temp-buffer
8140         (insert data)
8141         (copy-region-as-kill (point-min) (point-max))
8142         (message "Copied %s" data)))))
8143
8144 ;;; Internal functions:
8145
8146 (defun gnus-article-set-globals ()
8147   (with-current-buffer gnus-summary-buffer
8148     (gnus-set-global-variables)))
8149
8150 (defun gnus-signature-toggle (end)
8151   (gnus-with-article-buffer
8152     (let ((inhibit-point-motion-hooks t))
8153       (if (text-property-any end (point-max) 'article-type 'signature)
8154           (progn
8155             (gnus-delete-wash-type 'signature)
8156             (gnus-remove-text-properties-when
8157              'article-type 'signature end (point-max)
8158              (cons 'article-type (cons 'signature
8159                                        gnus-hidden-properties))))
8160         (gnus-add-wash-type 'signature)
8161         (gnus-add-text-properties-when
8162          'article-type nil end (point-max)
8163          (cons 'article-type (cons 'signature
8164                                    gnus-hidden-properties)))))
8165     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
8166       (gnus-set-mode-line 'article))))
8167
8168 (defun gnus-button-push (marker-and-entry)
8169   ;; Push button starting at MARKER.
8170   (save-excursion
8171     (let* ((marker (car marker-and-entry))
8172            (entry (cadr marker-and-entry))
8173            (regexp (car entry))
8174            (inhibit-point-motion-hooks t))
8175       (goto-char marker)
8176       ;; This is obviously true, or something bad is happening :)
8177       ;; But we need it to have the match-data
8178       (when (looking-at (or (if (symbolp regexp)
8179                                 (symbol-value regexp)
8180                               regexp)))
8181         (let ((fun (nth 3 entry))
8182               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
8183                              (get-char-property marker 'gnus-button-url))
8184                         (mapcar (lambda (group)
8185                                   (let ((string (match-string group)))
8186                                     (set-text-properties
8187                                      0 (length string) nil string)
8188                                     string))
8189                                 (nthcdr 4 entry)))))
8190
8191           (cond
8192            ((fboundp fun)
8193             (apply fun args))
8194            ((and (boundp fun)
8195                  (fboundp (symbol-value fun)))
8196             (apply (symbol-value fun) args))
8197            (t
8198             (gnus-message 1 "You must define `%S' to use this button"
8199                           (cons fun args)))))))))
8200
8201 (defun gnus-parse-news-url (url)
8202   (let (scheme server port group message-id articles)
8203     (with-temp-buffer
8204       (insert url)
8205       (goto-char (point-min))
8206       (when (looking-at "\\([A-Za-z]+\\):")
8207         (setq scheme (match-string 1))
8208         (goto-char (match-end 0)))
8209       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8210         (setq server (match-string 1))
8211         (setq port (if (stringp (match-string 3))
8212                        (string-to-number (match-string 3))
8213                      (match-string 3)))
8214         (goto-char (match-end 0)))
8215
8216       (cond
8217        ((looking-at "\\(.*@.*\\)")
8218         (setq message-id (match-string 1)))
8219        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8220         (setq group (match-string 1)
8221               articles (split-string (match-string 2) "-")))
8222        ((looking-at "\\([^/]+\\)/?")
8223         (setq group (match-string 1)))
8224        (t
8225         (error "Unknown news URL syntax"))))
8226     (list scheme server port group message-id articles)))
8227
8228 (defun gnus-button-handle-news (url)
8229   "Fetch a news URL."
8230   (destructuring-bind (scheme server port group message-id articles)
8231       (gnus-parse-news-url url)
8232     (cond
8233      (message-id
8234       (with-current-buffer gnus-summary-buffer
8235         (if server
8236             (let ((gnus-refer-article-method
8237                    (nconc (list (list 'nntp server))
8238                           gnus-refer-article-method))
8239                   (nntp-port-number (or port "nntp")))
8240               (gnus-message 7 "Fetching %s with %s"
8241                             message-id gnus-refer-article-method)
8242               (gnus-summary-refer-article message-id))
8243           (gnus-summary-refer-article message-id))))
8244      (group
8245       (gnus-button-fetch-group url)))))
8246
8247 (defun gnus-button-patch (library line)
8248   "Visit an Emacs Lisp library LIBRARY on line LINE."
8249   (interactive)
8250   (let ((file (locate-library (file-name-nondirectory library))))
8251     (unless file
8252       (error "Couldn't find library %s" library))
8253     (find-file file)
8254     (goto-char (point-min))
8255     (forward-line (1- (string-to-number line)))))
8256
8257 (defun gnus-button-handle-man (url)
8258   "Fetch a man page."
8259   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8260   (when (eq gnus-button-man-handler 'woman)
8261     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8262   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8263   (funcall gnus-button-man-handler url))
8264
8265 (defun gnus-button-handle-info-url (url)
8266   "Fetch an info URL."
8267   (setq url (mm-subst-char-in-string ?+ ?\  url))
8268   (cond
8269    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8270     (gnus-info-find-node
8271      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8272                      "Gnus")
8273              ")" (gnus-url-unhex-string (match-string 2 url)))))
8274    ((string-match "([^)\"]+)[^\"]+" url)
8275     (setq url
8276           (gnus-replace-in-string
8277            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8278     (gnus-info-find-node url))
8279    (t (error "Can't parse %s" url))))
8280
8281 (defun gnus-button-handle-info-url-gnome (url)
8282   "Fetch GNOME style info URL."
8283   (setq url (mm-subst-char-in-string ?_ ?\  url))
8284   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8285       (gnus-info-find-node
8286        (concat "("
8287                (gnus-url-unhex-string
8288                  (match-string 1 url))
8289                ")"
8290                (or (gnus-url-unhex-string
8291                     (match-string 2 url))
8292                    "Top")))
8293     (error "Can't parse %s" url)))
8294
8295 (defun gnus-button-handle-info-url-kde (url)
8296   "Fetch KDE style info URL."
8297   (gnus-info-find-node (gnus-url-unhex-string url)))
8298
8299 ;; (info) will autoload info.el
8300 (declare-function Info-menu "info" (menu-item &optional fork))
8301 (declare-function Info-index-next "info" (num))
8302
8303 (defun gnus-button-handle-info-keystrokes (url)
8304   "Call `info' when pushing the corresponding URL button."
8305   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8306   (let (node indx comma)
8307     (if (string-match
8308          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8309                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8310                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8311                  "\\(?:[ \t\n,]*\\)\\)?")
8312          url)
8313         (setq node (match-string 2 url)
8314               indx (match-string 3 url))
8315       (error "Can't parse %s" url))
8316     (info)
8317     (Info-directory)
8318     (Info-menu node)
8319     (when (> (length indx) 0)
8320       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8321                             "\\([ \t\n,]*\\)")
8322                     indx)
8323       (setq comma (match-string 2 indx))
8324       (setq indx  (match-string 1 indx))
8325       (Info-index indx)
8326       (when comma
8327         (dotimes (i (with-temp-buffer
8328                       (insert comma)
8329                       ;; Note: the XEmacs version of `how-many' takes
8330                       ;; no optional argument.
8331                       (goto-char (point-min))
8332                       (how-many ",")))
8333           (Info-index-next 1)))
8334       nil)))
8335
8336 (autoload 'pgg-snarf-keys-region "pgg")
8337 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8338 (declare-function pgg-display-output-buffer "pgg" (start end status))
8339
8340 (defun gnus-button-openpgp (url)
8341   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8342   (with-temp-buffer
8343     (mm-url-insert-file-contents-external url)
8344     (pgg-snarf-keys-region (point-min) (point-max))
8345     (pgg-display-output-buffer nil nil nil)))
8346
8347 (defun gnus-button-message-id (message-id)
8348   "Fetch MESSAGE-ID."
8349   (with-current-buffer gnus-summary-buffer
8350     (gnus-summary-refer-article message-id)))
8351
8352 (defun gnus-button-fetch-group (address &rest ignore)
8353   "Fetch GROUP specified by ADDRESS."
8354   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8355                       address)
8356     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8357     ;; for nntp:// and news://
8358     (setq address (match-string 3 address)))
8359   (if (not (string-match "[:/]" address))
8360       ;; This is just a simple group url.
8361       (gnus-group-read-ephemeral-group address gnus-select-method)
8362     (if (not
8363          (string-match
8364           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8365           address))
8366         (error "Can't parse %s" address)
8367       (gnus-group-read-ephemeral-group
8368        (match-string 4 address)
8369        `(nntp ,(match-string 1 address)
8370               (nntp-address ,(match-string 1 address))
8371               (nntp-port-number ,(if (match-end 3)
8372                                      (match-string 3 address)
8373                                    "nntp")))
8374        nil nil nil
8375        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8376
8377 (defun gnus-url-parse-query-string (query &optional downcase)
8378   (let (retval pairs cur key val)
8379     (setq pairs (split-string query "&"))
8380     (while pairs
8381       (setq cur (car pairs)
8382             pairs (cdr pairs))
8383       (if (not (string-match "=" cur))
8384           nil                           ; Grace
8385         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8386               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8387         (if downcase
8388             (setq key (downcase key)))
8389         (setq cur (assoc key retval))
8390         (if cur
8391             (setcdr cur (cons val (cdr cur)))
8392           (setq retval (cons (list key val) retval)))))
8393     retval))
8394
8395 (defun gnus-url-mailto (url)
8396   ;; Send mail to someone
8397   (setq url (replace-regexp-in-string "\n" " " url))
8398   (when (string-match "mailto:/*\\(.*\\)" url)
8399     (setq url (substring url (match-beginning 1) nil)))
8400   (let (to args subject func)
8401     (setq args (gnus-url-parse-query-string
8402                 (if (string-match "^\\?" url)
8403                     (substring url 1)
8404                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8405                       (concat "to=" (match-string 1 url) "&"
8406                               (match-string 2 url))
8407                     (concat "to=" url))))
8408           subject (cdr-safe (assoc "subject" args)))
8409     (gnus-msg-mail)
8410     (while args
8411       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8412       (if (fboundp func)
8413           (funcall func)
8414         (message-position-on-field (caar args)))
8415       (insert (gnus-replace-in-string
8416                (mapconcat 'identity (reverse (cdar args)) ", ")
8417                "\r\n" "\n" t))
8418       (setq args (cdr args)))
8419     (if subject
8420         (message-goto-body)
8421       (message-goto-subject))))
8422
8423 (defun gnus-button-embedded-url (address)
8424   "Activate ADDRESS with `browse-url'."
8425   (browse-url (gnus-strip-whitespace address)))
8426
8427 ;;; Next/prev buttons in the article buffer.
8428
8429 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8430 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8431
8432 (defvar gnus-prev-page-map
8433   (let ((map (make-sparse-keymap)))
8434     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8435     (define-key map "\r" 'gnus-button-prev-page)
8436     map))
8437
8438 (defvar gnus-next-page-map
8439   (let ((map (make-sparse-keymap)))
8440     (define-key map gnus-mouse-2 'gnus-button-next-page)
8441     (define-key map "\r" 'gnus-button-next-page)
8442     map))
8443
8444 (defun gnus-insert-prev-page-button ()
8445   (let ((b (point)) e
8446         (inhibit-read-only t))
8447     (gnus-eval-format
8448      gnus-prev-page-line-format nil
8449      `(keymap ,gnus-prev-page-map
8450               gnus-prev t
8451               gnus-callback gnus-article-button-prev-page
8452               article-type annotation))
8453     (setq e (if (bolp)
8454                 ;; Exclude a newline.
8455                 (1- (point))
8456               (point)))
8457     (when gnus-article-button-face
8458       (gnus-overlay-put (gnus-make-overlay b e nil t)
8459                         'face gnus-article-button-face))
8460     (widget-convert-button
8461      'link b e
8462      :action 'gnus-button-prev-page
8463      :button-keymap gnus-prev-page-map)))
8464
8465 (defun gnus-button-next-page (&optional args more-args)
8466   "Go to the next page."
8467   (interactive)
8468   (let ((win (selected-window)))
8469     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8470     (gnus-article-next-page)
8471     (select-window win)))
8472
8473 (defun gnus-button-prev-page (&optional args more-args)
8474   "Go to the prev page."
8475   (interactive)
8476   (let ((win (selected-window)))
8477     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8478     (gnus-article-prev-page)
8479     (select-window win)))
8480
8481 (defun gnus-insert-next-page-button ()
8482   (let ((b (point)) e
8483         (inhibit-read-only t))
8484     (gnus-eval-format gnus-next-page-line-format nil
8485                       `(keymap ,gnus-next-page-map
8486                                gnus-next t
8487                                gnus-callback gnus-article-button-next-page
8488                                article-type annotation))
8489     (setq e (if (bolp)
8490                 ;; Exclude a newline.
8491                 (1- (point))
8492               (point)))
8493     (when gnus-article-button-face
8494       (gnus-overlay-put (gnus-make-overlay b e nil t)
8495                         'face gnus-article-button-face))
8496     (widget-convert-button
8497      'link b e
8498      :action 'gnus-button-next-page
8499      :button-keymap gnus-next-page-map)))
8500
8501 (defun gnus-article-button-next-page (arg)
8502   "Go to the next page."
8503   (interactive "P")
8504   (let ((win (selected-window)))
8505     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8506     (gnus-article-next-page)
8507     (select-window win)))
8508
8509 (defun gnus-article-button-prev-page (arg)
8510   "Go to the prev page."
8511   (interactive "P")
8512   (let ((win (selected-window)))
8513     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8514     (gnus-article-prev-page)
8515     (select-window win)))
8516
8517 (defvar gnus-decode-header-methods
8518   '(mail-decode-encoded-word-region)
8519   "List of methods used to decode headers.
8520
8521 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8522 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8523 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8524 whose names match REGEXP.
8525
8526 For example:
8527 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8528  mail-decode-encoded-word-region
8529  (\"chinese\" . rfc1843-decode-region))
8530 ")
8531
8532 (defvar gnus-decode-header-methods-cache nil)
8533
8534 (defun gnus-multi-decode-header (start end)
8535   "Apply the functions from `gnus-encoded-word-methods' that match."
8536   (unless (and gnus-decode-header-methods-cache
8537                (eq gnus-newsgroup-name
8538                    (car gnus-decode-header-methods-cache)))
8539     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8540     (dolist (x gnus-decode-header-methods)
8541       (if (symbolp x)
8542           (nconc gnus-decode-header-methods-cache (list x))
8543         (if (and gnus-newsgroup-name
8544                  (string-match (car x) gnus-newsgroup-name))
8545             (nconc gnus-decode-header-methods-cache
8546                    (list (cdr x)))))))
8547   (let ((xlist gnus-decode-header-methods-cache))
8548     (pop xlist)
8549     (save-restriction
8550       (narrow-to-region start end)
8551       (while xlist
8552         (funcall (pop xlist) (point-min) (point-max))))))
8553
8554 ;;;
8555 ;;; Treatment top-level handling.
8556 ;;;
8557
8558 (defvar gnus-inhibit-article-treatments nil)
8559
8560 (defun gnus-treat-article (gnus-treat-condition
8561                            &optional part-number total-parts gnus-treat-type)
8562   (let ((gnus-treat-length (- (point-max) (point-min)))
8563         (alist gnus-treatment-function-alist)
8564         (article-goto-body-goes-to-point-min-p t)
8565         (treated-type
8566          (or (not gnus-treat-type)
8567              (catch 'found
8568                (let ((list gnus-article-treat-types))
8569                  (while list
8570                    (when (string-match (pop list) gnus-treat-type)
8571                      (throw 'found t)))))))
8572         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8573         val elem)
8574     (gnus-run-hooks 'gnus-part-display-hook)
8575     (dolist (elem alist)
8576       (setq val
8577             (save-excursion
8578               (when (gnus-buffer-live-p gnus-summary-buffer)
8579                 (set-buffer gnus-summary-buffer))
8580               (symbol-value (car elem))))
8581       (when (and (or (consp val)
8582                      treated-type)
8583                  (or (not gnus-inhibit-article-treatments)
8584                      (eq gnus-treat-condition 'head))
8585                  (gnus-treat-predicate val)
8586                  (or (not (get (car elem) 'highlight))
8587                      highlightp))
8588         (save-restriction
8589           (funcall (cadr elem)))))))
8590
8591 ;; Dynamic variables.
8592 (defvar part-number)
8593 (defvar total-parts)
8594 (defvar gnus-treat-type)
8595 (defvar gnus-treat-condition)
8596 (defvar gnus-treat-length)
8597
8598 (defun gnus-treat-predicate (val)
8599   (cond
8600    ((null val)
8601     nil)
8602    (gnus-treat-condition
8603     (eq gnus-treat-condition val))
8604    ((and (listp val)
8605          (stringp (car val)))
8606     (apply 'gnus-or (mapcar `(lambda (s)
8607                                (string-match s ,(or gnus-newsgroup-name "")))
8608                             val)))
8609    ((listp val)
8610     (let ((pred (pop val)))
8611       (cond
8612        ((eq pred 'or)
8613         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8614        ((eq pred 'and)
8615         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8616        ((eq pred 'not)
8617         (not (gnus-treat-predicate (car val))))
8618        ((eq pred 'typep)
8619         (equal (car val) gnus-treat-type))
8620        ((functionp pred)
8621         (funcall pred))
8622        (t
8623         (error "%S is not a valid predicate" pred)))))
8624    ((eq val t)
8625     t)
8626    ((eq val 'head)
8627     nil)
8628    ((eq val 'first)
8629     (eq part-number 1))
8630    ((eq val 'last)
8631     (eq part-number total-parts))
8632    ((numberp val)
8633     (< gnus-treat-length val))
8634    (t
8635     (error "%S is not a valid value" val))))
8636
8637 (defun gnus-article-encrypt-body (protocol &optional n)
8638   "Encrypt the article body."
8639   (interactive
8640    (list
8641     (or gnus-article-encrypt-protocol
8642         (gnus-completing-read "Encrypt protocol"
8643                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8644                               t))
8645     current-prefix-arg))
8646   ;; User might hit `K E' instead of `K e', so prompt once.
8647   (when (and gnus-article-encrypt-protocol
8648              gnus-novice-user)
8649     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8650       (error "Encrypt aborted")))
8651   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8652     (unless func
8653       (error "Can't find the encrypt protocol %s" protocol))
8654     (if (member gnus-newsgroup-name '("nndraft:delayed"
8655                                       "nndraft:drafts"
8656                                       "nndraft:queue"))
8657         (error "Can't encrypt the article in group %s"
8658                gnus-newsgroup-name))
8659     (gnus-summary-iterate n
8660       (with-current-buffer gnus-summary-buffer
8661         (let ((mail-parse-charset gnus-newsgroup-charset)
8662               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8663               (summary-buffer gnus-summary-buffer)
8664               references point)
8665           (gnus-set-global-variables)
8666           (when (gnus-group-read-only-p)
8667             (error "The current newsgroup does not support article encrypt"))
8668           (gnus-summary-show-article t)
8669           (setq references
8670               (or (mail-header-references gnus-current-headers) ""))
8671           (set-buffer gnus-article-buffer)
8672           (let* ((inhibit-read-only t)
8673                  (headers
8674                   (mapcar (lambda (field)
8675                             (and (save-restriction
8676                                    (message-narrow-to-head)
8677                                    (goto-char (point-min))
8678                                    (search-forward field nil t))
8679                                  (prog2
8680                                      (message-narrow-to-field)
8681                                      (buffer-string)
8682                                    (delete-region (point-min) (point-max))
8683                                    (widen))))
8684                           '("Content-Type:" "Content-Transfer-Encoding:"
8685                             "Content-Disposition:"))))
8686             (message-narrow-to-head)
8687             (message-remove-header "MIME-Version")
8688             (goto-char (point-max))
8689             (setq point (point))
8690             (insert (apply 'concat headers))
8691             (widen)
8692             (narrow-to-region point (point-max))
8693             (let ((message-options message-options))
8694               (message-options-set 'message-sender user-mail-address)
8695               (message-options-set 'message-recipients user-mail-address)
8696               (message-options-set 'message-sign-encrypt 'not)
8697               (funcall func))
8698             (goto-char (point-min))
8699             (insert "MIME-Version: 1.0\n")
8700             (widen)
8701             (gnus-summary-edit-article-done
8702              references nil summary-buffer t))
8703           (when gnus-keep-backlog
8704             (gnus-backlog-remove-article
8705              (car gnus-article-current) (cdr gnus-article-current)))
8706           (gnus-flush-original-article-buffer)
8707           (when gnus-use-cache
8708             (gnus-cache-update-article
8709              (car gnus-article-current) (cdr gnus-article-current))))))))
8710
8711 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8712   "The following specs can be used:
8713 %t  The security MIME type
8714 %i  Additional info
8715 %d  Details
8716 %D  Details if button is pressed")
8717
8718 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8719   "The following specs can be used:
8720 %t  The security MIME type
8721 %i  Additional info
8722 %d  Details
8723 %D  Details if button is pressed")
8724
8725 (defvar gnus-mime-security-button-line-format-alist
8726   '((?t gnus-tmp-type ?s)
8727     (?i gnus-tmp-info ?s)
8728     (?d gnus-tmp-details ?s)
8729     (?D gnus-tmp-pressed-details ?s)))
8730
8731 (defvar gnus-mime-security-button-commands
8732   '((gnus-article-press-button "\r" "Show Detail")
8733     (undefined "v")
8734     (undefined "t")
8735     (undefined "C")
8736     (gnus-mime-security-save-part "o" "Save...")
8737     (undefined "\C-o")
8738     (undefined "r")
8739     (undefined "d")
8740     (undefined "c")
8741     (undefined "i")
8742     (undefined "E")
8743     (undefined "e")
8744     (undefined "p")
8745     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8746     (undefined ".")))
8747
8748 (defvar gnus-mime-security-button-map
8749   (let ((map (make-sparse-keymap)))
8750     (define-key map gnus-mouse-2 'gnus-article-push-button)
8751     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8752     (dolist (c gnus-mime-security-button-commands)
8753       (define-key map (cadr c) (car c)))
8754     map))
8755
8756 (easy-menu-define
8757   gnus-mime-security-button-menu gnus-mime-security-button-map
8758   "Security button menu."
8759   `("Security Part"
8760     ,@(delq nil
8761             (mapcar (lambda (c)
8762                       (unless (eq (car c) 'undefined)
8763                         (vector (caddr c) (car c) :active t)))
8764                     gnus-mime-security-button-commands))))
8765
8766 (defun gnus-mime-security-button-menu (event prefix)
8767   "Construct a context-sensitive menu of security commands."
8768   (interactive "e\nP")
8769   (save-window-excursion
8770     (let ((pos (event-start event)))
8771       (select-window (posn-window pos))
8772       (goto-char (posn-point pos))
8773       (gnus-article-check-buffer)
8774       (popup-menu gnus-mime-security-button-menu nil prefix))))
8775
8776 (defvar gnus-mime-security-details-buffer nil)
8777
8778 (defvar gnus-mime-security-button-pressed nil)
8779
8780 (defvar gnus-mime-security-show-details-inline t
8781   "If non-nil, show details in the article buffer.")
8782
8783 (defun gnus-mime-security-verify-or-decrypt (handle)
8784   (mm-remove-parts (cdr handle))
8785   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8786         point (inhibit-read-only t))
8787     (if region
8788         (goto-char (car region)))
8789     (setq point (point))
8790     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8791       (let* ((mm-verify-option 'known)
8792              (mm-decrypt-option 'known)
8793              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8794         (unless (eq nparts (cdr handle))
8795           (mm-destroy-parts (cdr handle))
8796           (setcdr handle nparts))))
8797     (gnus-mime-display-security handle)
8798     (when region
8799       (delete-region (point) (cdr region))
8800       (set-marker (car region) nil)
8801       (set-marker (cdr region) nil))
8802     (goto-char point)))
8803
8804 (defun gnus-mime-security-show-details (handle)
8805   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8806     (if (not details)
8807         (gnus-message 5 "No details.")
8808       (if gnus-mime-security-show-details-inline
8809           (let ((gnus-mime-security-button-pressed
8810                  (not (get-text-property (point) 'gnus-mime-details)))
8811                 (gnus-mime-security-button-line-format
8812                  (get-text-property (point) 'gnus-line-format))
8813                 (inhibit-read-only t))
8814             (forward-char -1)
8815             (while (eq (get-text-property (point) 'gnus-line-format)
8816                        gnus-mime-security-button-line-format)
8817               (forward-char -1))
8818             (forward-char)
8819             (save-restriction
8820               (narrow-to-region (point) (point))
8821               (gnus-insert-mime-security-button handle))
8822             (delete-region (point)
8823                            (or (text-property-not-all
8824                                 (point) (point-max)
8825                                 'gnus-line-format
8826                                 gnus-mime-security-button-line-format)
8827                                (point-max))))
8828         ;; Not inlined.
8829         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8830             (with-current-buffer gnus-mime-security-details-buffer
8831               (erase-buffer)
8832               t)
8833           (setq gnus-mime-security-details-buffer
8834                 (gnus-get-buffer-create "*MIME Security Details*")))
8835         (with-current-buffer gnus-mime-security-details-buffer
8836           (insert details)
8837           (goto-char (point-min)))
8838         (pop-to-buffer gnus-mime-security-details-buffer)))))
8839
8840 (defun gnus-mime-security-press-button (handle)
8841   (save-excursion
8842     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8843         (gnus-mime-security-show-details handle)
8844       (gnus-mime-security-verify-or-decrypt handle))))
8845
8846 (defun gnus-insert-mime-security-button (handle &optional displayed)
8847   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8848          (gnus-tmp-type
8849           (concat
8850            (or (nth 2 (assoc protocol mm-verify-function-alist))
8851                (nth 2 (assoc protocol mm-decrypt-function-alist))
8852                "Unknown")
8853            (if (equal (car handle) "multipart/signed")
8854                " Signed" " Encrypted")
8855            " Part"))
8856          (gnus-tmp-info
8857           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8858               "Undecided"))
8859          (gnus-tmp-details
8860           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8861          gnus-tmp-pressed-details
8862          b e)
8863     (setq gnus-tmp-details
8864           (if gnus-tmp-details
8865               (concat "\n" gnus-tmp-details)
8866             ""))
8867     (setq gnus-tmp-pressed-details
8868           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8869     (unless (bolp)
8870       (insert "\n"))
8871     (setq b (point))
8872     (gnus-eval-format
8873      gnus-mime-security-button-line-format
8874      gnus-mime-security-button-line-format-alist
8875      `(keymap ,gnus-mime-security-button-map
8876          gnus-callback gnus-mime-security-press-button
8877          gnus-line-format ,gnus-mime-security-button-line-format
8878          gnus-mime-details ,gnus-mime-security-button-pressed
8879          article-type annotation
8880          gnus-data ,handle))
8881     (setq e (if (bolp)
8882                 ;; Exclude a newline.
8883                 (1- (point))
8884               (point)))
8885     (when gnus-article-button-face
8886       (gnus-overlay-put (gnus-make-overlay b e nil t)
8887                         'face gnus-article-button-face))
8888     (widget-convert-button
8889      'link b e
8890      :mime-handle handle
8891      :action 'gnus-widget-press-button
8892      :button-keymap gnus-mime-security-button-map
8893      :help-echo
8894      (lambda (widget)
8895        ;; Needed to properly clear the message due to a bug in
8896        ;; wid-edit (XEmacs only).
8897        (when (boundp 'help-echo-owns-message)
8898          (setq help-echo-owns-message t))
8899        (format
8900         "%S: show detail; %S: more options"
8901         (aref gnus-mouse-2 0)
8902         (aref gnus-down-mouse-3 0))))))
8903
8904 (defun gnus-mime-display-security (handle)
8905   (save-restriction
8906     (narrow-to-region (point) (point))
8907     (unless (gnus-unbuttonized-mime-type-p (car handle))
8908       (gnus-insert-mime-security-button handle))
8909     (gnus-mime-display-part (cadr handle))
8910     (unless (bolp)
8911       (insert "\n"))
8912     (unless (gnus-unbuttonized-mime-type-p (car handle))
8913       (let ((gnus-mime-security-button-line-format
8914              gnus-mime-security-button-end-line-format))
8915         (gnus-insert-mime-security-button handle)))
8916     (mm-set-handle-multipart-parameter
8917      handle 'gnus-region (cons (point-min-marker) (point-max-marker)))
8918     (goto-char (point-max))))
8919
8920 (defun gnus-mime-security-run-function (function)
8921   "Run FUNCTION with the security part under point."
8922   (gnus-article-check-buffer)
8923   (let ((data (get-text-property (point) 'gnus-data))
8924         buffer handle)
8925     (when (and (stringp (car-safe data))
8926                (setq buffer (mm-handle-multipart-original-buffer data))
8927                (setq handle (cadr data)))
8928       (if (bufferp (mm-handle-buffer handle))
8929           (progn
8930             (setq handle (cons buffer (copy-sequence (cdr handle))))
8931             (mm-handle-set-undisplayer handle nil))
8932         (setq handle (mm-make-handle
8933                       buffer
8934                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8935                       nil nil nil nil nil nil)))
8936       (funcall function handle))))
8937
8938 (defun gnus-mime-security-save-part ()
8939   "Save the security part under point."
8940   (interactive)
8941   (gnus-mime-security-run-function 'mm-save-part))
8942
8943 (defun gnus-mime-security-pipe-part ()
8944   "Pipe the security part under point to a process."
8945   (interactive)
8946   (gnus-mime-security-run-function 'mm-pipe-part))
8947
8948 (gnus-ems-redefine)
8949
8950 (provide 'gnus-art)
8951
8952 (run-hooks 'gnus-art-load-hook)
8953
8954 ;;; gnus-art.el ends here