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