a3f5311a5d7a317bab6475e918f38bbd099345c8
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2012 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 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (defvar tool-bar-map)
33 (defvar w3m-minor-mode-map)
34
35 (require 'gnus)
36 (require 'gnus-util)
37 (require 'gnus-sum)
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48 (require 'mouse)
49
50 (autoload 'gnus-msg-mail "gnus-msg" nil t)
51 (autoload 'gnus-button-mailto "gnus-msg")
52 (autoload 'gnus-button-reply "gnus-msg" nil t)
53 (autoload 'parse-time-string "parse-time" nil nil)
54 (autoload 'ansi-color-apply-on-region "ansi-color")
55 (autoload 'mm-url-insert-file-contents-external "mm-url")
56 (autoload 'mm-extern-cache-contents "mm-extern")
57
58 (defgroup gnus-article nil
59   "Article display."
60   :link '(custom-manual "(gnus)Article Buffer")
61   :group 'gnus)
62
63 (defgroup gnus-article-treat nil
64   "Treating article parts."
65   :link '(custom-manual "(gnus)Article Hiding")
66   :group 'gnus-article)
67
68 (defgroup gnus-article-hiding nil
69   "Hiding article parts."
70   :link '(custom-manual "(gnus)Article Hiding")
71   :group 'gnus-article)
72
73 (defgroup gnus-article-highlight nil
74   "Article highlighting."
75   :link '(custom-manual "(gnus)Article Highlighting")
76   :group 'gnus-article
77   :group 'gnus-visual)
78
79 (defgroup gnus-article-signature nil
80   "Article signatures."
81   :link '(custom-manual "(gnus)Article Signature")
82   :group 'gnus-article)
83
84 (defgroup gnus-article-headers nil
85   "Article headers."
86   :link '(custom-manual "(gnus)Hiding Headers")
87   :group 'gnus-article)
88
89 (defgroup gnus-article-washing nil
90   "Special commands on articles."
91   :link '(custom-manual "(gnus)Article Washing")
92   :group 'gnus-article)
93
94 (defgroup gnus-article-emphasis nil
95   "Fontisizing articles."
96   :link '(custom-manual "(gnus)Article Fontisizing")
97   :group 'gnus-article)
98
99 (defgroup gnus-article-saving nil
100   "Saving articles."
101   :link '(custom-manual "(gnus)Saving Articles")
102   :group 'gnus-article)
103
104 (defgroup gnus-article-mime nil
105   "Worshiping the MIME wonder."
106   :link '(custom-manual "(gnus)Using MIME")
107   :group 'gnus-article)
108
109 (defgroup gnus-article-buttons nil
110   "Pushable buttons in the article buffer."
111   :link '(custom-manual "(gnus)Article Buttons")
112   :group 'gnus-article)
113
114 (defgroup gnus-article-various nil
115   "Other article options."
116   :link '(custom-manual "(gnus)Misc Article")
117   :group 'gnus-article)
118
119 (defcustom gnus-ignored-headers
120   (mapcar
121    (lambda (header)
122      (concat "^" header ":"))
123    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
124      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
125      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
126      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
127      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
128      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
129      "X-Attribution" "X-Originating-IP" "Delivered-To"
130      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
131      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
132      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
133      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
134      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
135      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
136      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
137      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
138      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
139      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
140      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
141      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
142      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
143      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
144      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
145      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
146      "List-[A-Za-z]+" "X-Listprocessor-Version"
147      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
148      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
149      "X-Received" "Content-length" "X-precedence"
150      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
151      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
152      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
153      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
154      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
155      "X-Content-length" "X-Posting-Agent" "Original-Received"
156      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
157      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
158      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
159      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
160      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
161      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
162      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
163      "Envelope-Sender" "Envelope-Recipients"))
164   "*All headers that start with this regexp will be hidden.
165 This variable can also be a list of regexps of headers to be ignored.
166 If `gnus-visible-headers' is non-nil, this variable will be ignored."
167   :type '(choice regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^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:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-hidden-properties '(invisible t intangible t)
261   "Property list to use for hiding text."
262   :type 'sexp
263   :group 'gnus-article-hiding)
264
265 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
266 ;; frames in a session.
267 (defcustom gnus-article-x-face-command
268   (if (featurep 'xemacs)
269       (if (or (gnus-image-type-available-p 'xface)
270               (gnus-image-type-available-p 'pbm))
271           'gnus-display-x-face-in-from
272         "{ echo \
273 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
274 ; uncompface; } | icontopbm | ee -")
275     (if (gnus-image-type-available-p 'pbm)
276         'gnus-display-x-face-in-from
277       "{ echo \
278 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
279 ; uncompface; } | icontopbm | display -"))
280   "*String or function to be executed to display an X-Face header.
281 If it is a string, the command will be executed in a sub-shell
282 asynchronously.  The compressed face will be piped to this command."
283   :type `(choice string
284                  (function-item gnus-display-x-face-in-from)
285                  function)
286   :version "21.1"
287   :group 'gnus-picon
288   :group 'gnus-article-washing)
289
290 (defcustom gnus-article-x-face-too-ugly nil
291   "Regexp matching posters whose face shouldn't be shown automatically."
292   :type '(choice regexp (const nil))
293   :group 'gnus-article-washing)
294
295 (defcustom gnus-article-banner-alist nil
296   "Banner alist for stripping.
297 For example,
298      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
299   :version "21.1"
300   :type '(repeat (cons symbol regexp))
301   :group 'gnus-article-washing)
302
303 (gnus-define-group-parameter
304  banner
305  :variable-document
306  "Alist of regexps (to match group names) and banner."
307  :variable-group gnus-article-washing
308  :parameter-type
309  '(choice :tag "Banner"
310           :value nil
311           (const :tag "Remove signature" signature)
312           (symbol :tag "Item in `gnus-article-banner-alist'" none)
313           regexp
314           (const :tag "None" nil))
315  :parameter-document
316  "If non-nil, specify how to remove `banners' from articles.
317
318 Symbol `signature' means to remove signatures delimited by
319 `gnus-signature-separator'.  Any other symbol is used to look up a
320 regular expression to match the banner in `gnus-article-banner-alist'.
321 A string is used as a regular expression to match the banner
322 directly.")
323
324 (defcustom gnus-article-address-banner-alist nil
325   "Alist of mail addresses and banners.
326 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
327 to match a mail address in the From: header, BANNER is one of a symbol
328 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
329 If ADDRESS matches author's mail address, it will remove things like
330 advertisements.  For example:
331
332 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
333 "
334   :type '(repeat
335           (cons
336            (regexp :tag "Address")
337            (choice :tag "Banner" :value nil
338                    (const :tag "Remove signature" signature)
339                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
340                    regexp
341                    (const :tag "None" nil))))
342   :version "22.1"
343   :group 'gnus-article-washing)
344
345 (defmacro gnus-emphasis-custom-with-format (&rest body)
346   `(let ((format "\
347 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
348 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
349      ,@body))
350
351 (defun gnus-emphasis-custom-value-to-external (value)
352   (gnus-emphasis-custom-with-format
353    (if (consp (car value))
354        (list (format format (car (car value)) (cdr (car value)))
355              2
356              (if (nth 1 value) 2 3)
357              (nth 2 value))
358      value)))
359
360 (defun gnus-emphasis-custom-value-to-internal (value)
361   (gnus-emphasis-custom-with-format
362    (let ((regexp (concat "\\`"
363                          (format (regexp-quote format)
364                                  "\\([^()]+\\)" "\\([^()]+\\)")
365                          "\\'"))
366          pattern)
367      (if (string-match regexp (setq pattern (car value)))
368          (list (cons (match-string 1 pattern) (match-string 2 pattern))
369                (= (nth 2 value) 2)
370                (nth 3 value))
371        value))))
372
373 (defcustom gnus-emphasis-alist
374   (let ((types
375          '(("\\*" "\\*" bold nil 2)
376            ("_" "_" underline)
377            ("/" "/" italic)
378            ("_/" "/_" underline-italic)
379            ("_\\*" "\\*_" underline-bold)
380            ("\\*/" "/\\*" bold-italic)
381            ("_\\*/" "/\\*_" underline-bold-italic))))
382     (nconc
383      (gnus-emphasis-custom-with-format
384       (mapcar (lambda (spec)
385                 (list (format format (car spec) (cadr spec))
386                       (or (nth 3 spec) 2)
387                       (or (nth 4 spec) 3)
388                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
389               types))
390      '(;; I've never seen anyone use this strikethru convention whereas I've
391        ;; several times seen it triggered by normal text.  --Stef
392        ;; Miles suggests that this form is sometimes used but for italics,
393        ;; so maybe we should map it to `italic'.
394        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
395        ;; 2 3 gnus-emphasis-strikethru)
396        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
397         2 3 gnus-emphasis-underline))))
398   "*Alist that says how to fontify certain phrases.
399 Each item looks like this:
400
401   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
402
403 The first element is a regular expression to be matched.  The second
404 is a number that says what regular expression grouping used to find
405 the entire emphasized word.  The third is a number that says what
406 regexp grouping should be displayed and highlighted.  The fourth
407 is the face used for highlighting."
408   :type
409   '(repeat
410     (menu-choice
411      :format "%[Customizing Style%]\n%v"
412      :indent 2
413      (group :tag "Default"
414             :value ("" 0 0 default)
415             :value-create
416             (lambda (widget)
417               (let ((value (widget-get
418                             (cadr (widget-get (widget-get widget :parent)
419                                               :args))
420                             :value)))
421                 (if (not (eq (nth 2 value) 'default))
422                     (widget-put
423                      widget
424                      :value
425                      (gnus-emphasis-custom-value-to-external value))))
426               (widget-group-value-create widget))
427             regexp
428             (integer :format "Match group: %v")
429             (integer :format "Emphasize group: %v")
430             face)
431      (group :tag "Simple"
432             :value (("_" . "_") nil default)
433             (cons :format "%v"
434                   (regexp :format "Start regexp: %v")
435                   (regexp :format "End regexp: %v"))
436             (boolean :format "Show start and end patterns: %[%v%]\n"
437                      :on " On " :off " Off ")
438             face)))
439   :get (lambda (symbol)
440          (mapcar 'gnus-emphasis-custom-value-to-internal
441                  (default-value symbol)))
442   :set (lambda (symbol value)
443          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
444                                      value)))
445   :group 'gnus-article-emphasis)
446
447 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
448   "A regexp to describe whitespace which should not be emphasized.
449 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
450 The former avoids underlining of leading and trailing whitespace,
451 and the latter avoids underlining any whitespace at all."
452   :version "21.1"
453   :group 'gnus-article-emphasis
454   :type 'regexp)
455
456 (defface gnus-emphasis-bold '((t (:bold t)))
457   "Face used for displaying strong emphasized text (*word*)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-italic '((t (:italic t)))
461   "Face used for displaying italic emphasized text (/word/)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline '((t (:underline t)))
465   "Face used for displaying underlined emphasized text (_word_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
469   "Face used for displaying underlined bold emphasized text (_*word*_)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
473   "Face used for displaying underlined italic emphasized text (_/word/_)."
474   :group 'gnus-article-emphasis)
475
476 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
477   "Face used for displaying bold italic emphasized text (/*word*/)."
478   :group 'gnus-article-emphasis)
479
480 (defface gnus-emphasis-underline-bold-italic
481   '((t (:bold t :italic t :underline t)))
482   "Face used for displaying underlined bold italic emphasized text.
483 Example: (_/*word*/_)."
484   :group 'gnus-article-emphasis)
485
486 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
487                                       '((t (:strikethru t)))
488                                     '((t (:strike-through t))))
489   "Face used for displaying strike-through text (-word-)."
490   :group 'gnus-article-emphasis)
491
492 (defface gnus-emphasis-highlight-words
493   '((t (:background "black" :foreground "yellow")))
494   "Face used for displaying highlighted words."
495   :group 'gnus-article-emphasis)
496
497 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
498   "Format for display of Date headers in article bodies.
499 See `format-time-string' for the possible values.
500
501 The variable can also be function, which should return a complete Date
502 header.  The function is called with one argument, the time, which can
503 be fed to `format-time-string'."
504   :type '(choice string function)
505   :link '(custom-manual "(gnus)Article Date")
506   :group 'gnus-article-washing)
507
508 (defcustom gnus-save-all-headers t
509   "*If non-nil, don't remove any headers before saving.
510 This will be overridden by the `:headers' property that the symbol of
511 the saver function, which is specified by `gnus-default-article-saver',
512 might have."
513   :group 'gnus-article-saving
514   :type 'boolean)
515
516 (defcustom gnus-prompt-before-saving 'always
517   "*This variable says how much prompting is to be done when saving articles.
518 If it is nil, no prompting will be done, and the articles will be
519 saved to the default files.  If this variable is `always', each and
520 every article that is saved will be preceded by a prompt, even when
521 saving large batches of articles.  If this variable is neither nil not
522 `always', there the user will be prompted once for a file name for
523 each invocation of the saving commands."
524   :group 'gnus-article-saving
525   :type '(choice (item always)
526                  (item :tag "never" nil)
527                  (sexp :tag "once" :format "%t\n" :value t)))
528
529 (defcustom gnus-saved-headers gnus-visible-headers
530   "Headers to keep if `gnus-save-all-headers' is nil.
531 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
532 If that variable is nil, however, all headers that match this regexp
533 will be kept while the rest will be deleted before saving.  This and
534 `gnus-save-all-headers' will be overridden by the `:headers' property
535 that the symbol of the saver function, which is specified by
536 `gnus-default-article-saver', might have."
537   :group 'gnus-article-saving
538   :type 'regexp)
539
540 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
541 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
542   "A function to save articles in your favorite format.
543 The function will be called by way of the `gnus-summary-save-article'
544 command, and friends such as `gnus-summary-save-article-rmail'.
545
546 Gnus provides the following functions:
547
548 * gnus-summary-save-in-rmail (Rmail format)
549 * gnus-summary-save-in-mail (Unix mail format)
550 * gnus-summary-save-in-folder (MH folder)
551 * gnus-summary-save-in-file (article format)
552 * gnus-summary-save-body-in-file (article body)
553 * gnus-summary-save-in-vm (use VM's folder format)
554 * gnus-summary-write-to-file (article format -- overwrite)
555 * gnus-summary-write-body-to-file (article body -- overwrite)
556 * gnus-summary-save-in-pipe (article format)
557
558 The symbol of each function may have the following properties:
559
560 * :decode
561 The value non-nil means save decoded articles.  This is meaningful
562 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
563 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
564 `gnus-summary-save-in-pipe'.
565
566 * :function
567 The value specifies an alternative function which appends, not
568 overwrites, articles to a file.  This implies that when saving many
569 articles at a time, `gnus-prompt-before-saving' is bound to t and all
570 articles are saved in a single file.  This is meaningful only with
571 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
572
573 * :headers
574 The value specifies the symbol of a variable of which the value
575 specifies headers to be saved.  If it is omitted,
576 `gnus-save-all-headers' and `gnus-saved-headers' control what
577 headers should be saved."
578   :group 'gnus-article-saving
579   :type '(radio (function-item gnus-summary-save-in-rmail)
580                 (function-item gnus-summary-save-in-mail)
581                 (function-item gnus-summary-save-in-folder)
582                 (function-item gnus-summary-save-in-file)
583                 (function-item gnus-summary-save-body-in-file)
584                 (function-item gnus-summary-save-in-vm)
585                 (function-item gnus-summary-write-to-file)
586                 (function-item gnus-summary-write-body-to-file)
587                 (function-item gnus-summary-save-in-pipe)
588                 (function)))
589
590 (defcustom gnus-article-save-coding-system
591   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
592       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
593       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
594       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
595   "Coding system used to save decoded articles to a file.
596
597 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
598 which can safely encode any characters in text.  This is used by the
599 commands including:
600
601 * gnus-summary-save-article-file
602 * gnus-summary-save-article-body-file
603 * gnus-summary-write-article-file
604 * gnus-summary-write-article-body-file
605
606 and the functions to which you may set `gnus-default-article-saver':
607
608 * gnus-summary-save-in-file
609 * gnus-summary-save-body-in-file
610 * gnus-summary-write-to-file
611 * gnus-summary-write-body-to-file
612
613 Those commands and functions save just text displayed in the article
614 buffer to a file if the value of this variable is non-nil.  Note that
615 buttonized MIME parts will be lost in a saved file in that case.
616 Otherwise, raw articles will be saved."
617   :group 'gnus-article-saving
618   :type `(choice
619           :format "%{%t%}:\n %[Value Menu%] %v"
620           (const :tag "Save raw articles" nil)
621           ,@(delq nil
622                   (mapcar
623                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
624                    '((const :tag "UTF-8" utf-8)
625                      (const :tag "iso-2022-7bit" iso-2022-7bit)
626                      (const :tag "Emacs internal" emacs-mule)
627                      (const :tag "escape-quoted" escape-quoted))))
628           (symbol :tag "Coding system")))
629
630 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
631   "A function generating a file name to save articles in Rmail format.
632 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
633   :group 'gnus-article-saving
634   :type 'function)
635
636 (defcustom gnus-mail-save-name 'gnus-plain-save-name
637   "A function generating a file name to save articles in Unix mail format.
638 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
639   :group 'gnus-article-saving
640   :type 'function)
641
642 (defcustom gnus-folder-save-name 'gnus-folder-save-name
643   "A function generating a file name to save articles in MH folder.
644 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
645   :group 'gnus-article-saving
646   :type 'function)
647
648 (defcustom gnus-file-save-name 'gnus-numeric-save-name
649   "A function generating a file name to save articles in article format.
650 The function is called with NEWSGROUP, HEADERS, and optional
651 LAST-FILE."
652   :group 'gnus-article-saving
653   :type 'function)
654
655 (defcustom gnus-split-methods
656   '((gnus-article-archive-name)
657     (gnus-article-nndoc-name))
658   "*Variable used to suggest where articles are to be saved.
659 For instance, if you would like to save articles related to Gnus in
660 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
661 you could set this variable to something like:
662
663  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
664    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
665
666 This variable is an alist where the key is the match and the
667 value is a list of possible files to save in if the match is
668 non-nil.
669
670 If the match is a string, it is used as a regexp match on the
671 article.  If the match is a symbol, that symbol will be funcalled
672 from the buffer of the article to be saved with the newsgroup as the
673 parameter.  If it is a list, it will be evalled in the same buffer.
674
675 If this form or function returns a string, this string will be used as a
676 possible file name; and if it returns a non-nil list, that list will be
677 used as possible file names."
678   :group 'gnus-article-saving
679   :type '(repeat (choice (list :value (fun) function)
680                          (cons :value ("" "") regexp (repeat string))
681                          (sexp :value nil))))
682
683 (defcustom gnus-page-delimiter "^\^L"
684   "*Regexp describing what to use as article page delimiters.
685 The default value is \"^\^L\", which is a form linefeed at the
686 beginning of a line."
687   :type 'regexp
688   :group 'gnus-article-various)
689
690 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
691   "*The format specification for the article mode line.
692 See `gnus-summary-mode-line-format' for a closer description.
693
694 The following additional specs are available:
695
696 %w  The article washing status.
697 %m  The number of MIME parts in the article."
698   :version "24.1"
699   :type 'string
700   :group 'gnus-article-various)
701
702 (defcustom gnus-article-mode-hook nil
703   "*A hook for Gnus article mode."
704   :type 'hook
705   :group 'gnus-article-various)
706
707 (when (featurep 'xemacs)
708   ;; Extracted from gnus-xmas-define in order to preserve user settings
709   (when (fboundp 'turn-off-scroll-in-place)
710     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
711   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
712   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
713
714 (defcustom gnus-article-menu-hook nil
715   "*Hook run after the creation of the article mode menu."
716   :type 'hook
717   :group 'gnus-article-various)
718
719 (defcustom gnus-article-prepare-hook nil
720   "*A hook called after an article has been prepared in the article buffer."
721   :type 'hook
722   :group 'gnus-article-various)
723
724 (defcustom gnus-copy-article-ignored-headers nil
725   "List of headers to be removed when copying an article.
726 Each element is a regular expression."
727   :version "23.1" ;; No Gnus
728   :type '(repeat regexp)
729   :group 'gnus-article-various)
730
731 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
732                         "Gnus 5.10 (Emacs 22.1)")
733
734 (defface gnus-button
735   '((t (:weight bold)))
736   "Face used for highlighting a button in the article buffer."
737   :group 'gnus-article-buttons)
738
739 (defcustom gnus-article-button-face 'gnus-button
740   "Face used for highlighting buttons in the article buffer.
741
742 An article button is a piece of text that you can activate by pressing
743 `RET' or `mouse-2' above it."
744   :type 'face
745   :group 'gnus-article-buttons)
746
747 (defcustom gnus-article-mouse-face 'highlight
748   "Face used for mouse highlighting in the article buffer.
749
750 Article buttons will be displayed in this face when the cursor is
751 above them."
752   :type 'face
753   :group 'gnus-article-buttons)
754
755 (defcustom gnus-signature-face 'gnus-signature
756   "Face used for highlighting a signature in the article buffer.
757 Obsolete; use the face `gnus-signature' for customizations instead."
758   :type 'face
759   :group 'gnus-article-highlight
760   :group 'gnus-article-signature)
761
762 (defface gnus-signature
763   '((t
764      (:italic t)))
765   "Face used for highlighting a signature in the article buffer."
766   :group 'gnus-article-highlight
767   :group 'gnus-article-signature)
768 ;; backward-compatibility alias
769 (put 'gnus-signature-face 'face-alias 'gnus-signature)
770 (put 'gnus-signature-face 'obsolete-face "22.1")
771
772 (defface gnus-header-from
773   '((((class color)
774       (background dark))
775      (:foreground "PaleGreen1"))
776     (((class color)
777       (background light))
778      (:foreground "red3"))
779     (t
780      (:italic t)))
781   "Face used for displaying from headers."
782   :group 'gnus-article-headers
783   :group 'gnus-article-highlight)
784 ;; backward-compatibility alias
785 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
786 (put 'gnus-header-from-face 'obsolete-face "22.1")
787
788 (defface gnus-header-subject
789   '((((class color)
790       (background dark))
791      (:foreground "SeaGreen1"))
792     (((class color)
793       (background light))
794      (:foreground "red4"))
795     (t
796      (:bold t :italic t)))
797   "Face used for displaying subject headers."
798   :group 'gnus-article-headers
799   :group 'gnus-article-highlight)
800 ;; backward-compatibility alias
801 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
802 (put 'gnus-header-subject-face 'obsolete-face "22.1")
803
804 (defface gnus-header-newsgroups
805   '((((class color)
806       (background dark))
807      (:foreground "yellow" :italic t))
808     (((class color)
809       (background light))
810      (:foreground "MidnightBlue" :italic t))
811     (t
812      (:italic t)))
813   "Face used for displaying newsgroups headers.
814 In the default setup this face is only used for crossposted
815 articles."
816   :group 'gnus-article-headers
817   :group 'gnus-article-highlight)
818 ;; backward-compatibility alias
819 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
820 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
821
822 (defface gnus-header-name
823   '((((class color)
824       (background dark))
825      (:foreground "SpringGreen2"))
826     (((class color)
827       (background light))
828      (:foreground "maroon"))
829     (t
830      (:bold t)))
831   "Face used for displaying header names."
832   :group 'gnus-article-headers
833   :group 'gnus-article-highlight)
834 ;; backward-compatibility alias
835 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
836 (put 'gnus-header-name-face 'obsolete-face "22.1")
837
838 (defface gnus-header-content
839   '((((class color)
840       (background dark))
841      (:foreground "SpringGreen1" :italic t))
842     (((class color)
843       (background light))
844      (:foreground "indianred4" :italic t))
845     (t
846      (:italic t)))  "Face used for displaying header content."
847   :group 'gnus-article-headers
848   :group 'gnus-article-highlight)
849 ;; backward-compatibility alias
850 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
851 (put 'gnus-header-content-face 'obsolete-face "22.1")
852
853 (defcustom gnus-header-face-alist
854   '(("From" nil gnus-header-from)
855     ("Subject" nil gnus-header-subject)
856     ("Newsgroups:.*," nil gnus-header-newsgroups)
857     ("" gnus-header-name gnus-header-content))
858   "*Controls highlighting of article headers.
859
860 An alist of the form (HEADER NAME CONTENT).
861
862 HEADER is a regular expression which should match the name of a
863 header and NAME and CONTENT are either face names or nil.
864
865 The name of each header field will be displayed using the face
866 specified by the first element in the list where HEADER matches
867 the header name and NAME is non-nil.  Similarly, the content will
868 be displayed by the first non-nil matching CONTENT face."
869   :group 'gnus-article-headers
870   :group 'gnus-article-highlight
871   :type '(repeat (list (regexp :tag "Header")
872                        (choice :tag "Name"
873                                (item :tag "skip" nil)
874                                (face :value default))
875                        (choice :tag "Content"
876                                (item :tag "skip" nil)
877                                (face :value default)))))
878
879 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
880                                           '((xface . (:face gnus-x-face)))
881                                         '((pbm . (:face gnus-x-face))
882                                           (png . nil)))
883   "Alist of image types and properties applied to Face and X-Face images.
884 Here are examples:
885
886 ;; Specify the altitude of Face images in the From header.
887 \(setq gnus-face-properties-alist
888       '((pbm . (:face gnus-x-face :ascent 80))
889         (png . (:ascent 80))))
890
891 ;; Show Face images as pressed buttons.
892 \(setq gnus-face-properties-alist
893       '((pbm . (:face gnus-x-face :relief -2))
894         (png . (:relief -2))))
895
896 See the manual for the valid properties for various image types.
897 Currently, `pbm' is used for X-Face images and `png' is used for Face
898 images in Emacs.  Only the `:face' property is effective on the `xface'
899 image type in XEmacs if it is built with the libcompface library."
900   :version "23.1" ;; No Gnus
901   :group 'gnus-article-headers
902   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
903
904 (defcustom gnus-article-decode-hook
905   '(article-decode-charset article-decode-encoded-words
906                            article-decode-group-name article-decode-idna-rhs)
907   "*Hook run to decode charsets in articles."
908   :group 'gnus-article-headers
909   :type 'hook)
910
911 (defcustom gnus-display-mime-function 'gnus-display-mime
912   "Function to display MIME articles."
913   :group 'gnus-article-mime
914   :type 'function)
915
916 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
917   "Function used to decode headers.")
918
919 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
920   "Function used to decode addresses.")
921
922 (defvar gnus-article-dumbquotes-map
923   '((?\200 "EUR")
924     (?\202 ",")
925     (?\203 "f")
926     (?\204 ",,")
927     (?\205 "...")
928     (?\213 "<")
929     (?\214 "OE")
930     (?\221 "`")
931     (?\222 "'")
932     (?\223 "``")
933     (?\224 "\"")
934     (?\225 "*")
935     (?\226 "-")
936     (?\227 "--")
937     (?\230 "~")
938     (?\231 "(TM)")
939     (?\233 ">")
940     (?\234 "oe")
941     (?\264 "'"))
942   "Table for MS-to-Latin1 translation.")
943
944 (defcustom gnus-ignored-mime-types nil
945   "List of MIME types that should be ignored by Gnus."
946   :version "21.1"
947   :group 'gnus-article-mime
948   :type '(repeat regexp))
949
950 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
951   "List of MIME types that should not be given buttons when rendered inline.
952 See also `gnus-buttonized-mime-types' which may override this variable.
953 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
954   :version "21.1"
955   :group 'gnus-article-mime
956   :type '(repeat regexp))
957
958 (defcustom gnus-buttonized-mime-types nil
959   "List of MIME types that should be given buttons when rendered inline.
960 If set, this variable overrides `gnus-unbuttonized-mime-types'.
961 To see e.g. security buttons you could set this to
962 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
963 this list to display radio buttons that allow you to choose one of two
964 media types those mails include.  See also `mm-discouraged-alternatives'.
965 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
966   :version "22.1"
967   :group 'gnus-article-mime
968   :type '(repeat regexp))
969
970 (defcustom gnus-inhibit-mime-unbuttonizing nil
971   "If non-nil, all MIME parts get buttons.
972 When nil (the default value), then some MIME parts do not get buttons,
973 as described by the variables `gnus-buttonized-mime-types' and
974 `gnus-unbuttonized-mime-types'."
975   :version "22.1"
976   :group 'gnus-article-mime
977   :type 'boolean)
978
979 (defcustom gnus-body-boundary-delimiter "_"
980   "String used to delimit header and body.
981 This variable is used by `gnus-article-treat-body-boundary' which can
982 be controlled by `gnus-treat-body-boundary'."
983   :version "22.1"
984   :group 'gnus-article-various
985   :type '(choice (item :tag "None" :value nil)
986                  string))
987
988 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
989                                   "/usr/share/picons")
990   "Defines the location of the faces database.
991 For information on obtaining this database of pretty pictures, please
992 see http://www.cs.indiana.edu/picons/ftp/index.html"
993   :version "22.1"
994   :type '(repeat directory)
995   :link '(url-link :tag "download"
996                    "http://www.cs.indiana.edu/picons/ftp/index.html")
997   :link '(custom-manual "(gnus)Picons")
998   :group 'gnus-picon)
999
1000 (defun gnus-picons-installed-p ()
1001   "Say whether picons are installed on your machine."
1002   (let ((installed nil))
1003     (dolist (database gnus-picon-databases)
1004       (when (file-exists-p database)
1005         (setq installed t)))
1006     installed))
1007
1008 (defcustom gnus-article-mime-part-function nil
1009   "Function called with a MIME handle as the argument.
1010 This is meant for people who want to do something automatic based
1011 on parts -- for instance, adding Vcard info to a database."
1012   :group 'gnus-article-mime
1013   :type '(choice (const nil)
1014                  function))
1015
1016 (defcustom gnus-mime-multipart-functions nil
1017   "An alist of MIME types to functions to display them."
1018   :version "21.1"
1019   :group 'gnus-article-mime
1020   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1021
1022 (defcustom gnus-article-date-headers '(combined-lapsed)
1023   "A list of Date header formats to display.
1024 Valid formats are `ut' (universal time), `local' (local time
1025 zone), `english' (readable English), `lapsed' (elapsed time),
1026 `combined-lapsed' (both the original date and the elapsed time),
1027 `original' (the original date header), `iso8601' (ISO8601
1028 format), and `user-defined' (a user-defined format defined by the
1029 `gnus-article-time-format' variable).
1030
1031 You have as many date headers as you want in the article buffer.
1032 Some of these headers are updated automatically.  See
1033 `gnus-article-update-date-headers' for details."
1034   :version "24.1"
1035   :group 'gnus-article-headers
1036   :type '(repeat
1037           (item :tag "Universal time (UT)" :value 'ut)
1038           (item :tag "Local time zone" :value 'local)
1039           (item :tag "Readable English" :value 'english)
1040           (item :tag "Elapsed time" :value 'lapsed)
1041           (item :tag "Original and elapsed time" :value 'combined-lapsed)
1042           (item :tag "Original date header" :value 'original)
1043           (item :tag "ISO8601 format" :value 'iso8601)
1044           (item :tag "User-defined" :value 'user-defined)))
1045
1046 (defcustom gnus-article-update-date-headers nil
1047   "A number that says how often to update the date header (in seconds).
1048 If nil, don't update it at all."
1049   :version "24.1"
1050   :group 'gnus-article-headers
1051   :type '(choice
1052           (item :tag "Don't update" :value nil)
1053           integer))
1054
1055 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1056   "Function called with a MIME handle as the argument.
1057 This is meant for people who want to view first matched part.
1058 For `undisplayed-alternative' (default), the first undisplayed
1059 part or alternative part is used.  For `undisplayed', the first
1060 undisplayed part is used.  For a function, the first part which
1061 the function return t is used.  For nil, the first part is
1062 used."
1063   :version "21.1"
1064   :group 'gnus-article-mime
1065   :type '(choice
1066           (item :tag "first" :value nil)
1067           (item :tag "undisplayed" :value undisplayed)
1068           (item :tag "undisplayed or alternative"
1069                 :value undisplayed-alternative)
1070           (function)))
1071
1072 (defcustom gnus-mime-action-alist
1073   '(("save to file" . gnus-mime-save-part)
1074     ("save and strip" . gnus-mime-save-part-and-strip)
1075     ("replace with file" . gnus-mime-replace-part)
1076     ("delete part" . gnus-mime-delete-part)
1077     ("display as text" . gnus-mime-inline-part)
1078     ("view the part" . gnus-mime-view-part)
1079     ("pipe to command" . gnus-mime-pipe-part)
1080     ("toggle display" . gnus-article-press-button)
1081     ("toggle display" . gnus-article-view-part-as-charset)
1082     ("view as type" . gnus-mime-view-part-as-type)
1083     ("view internally" . gnus-mime-view-part-internally)
1084     ("view externally" . gnus-mime-view-part-externally))
1085   "An alist of actions that run on the MIME attachment."
1086   :group 'gnus-article-mime
1087   :type '(repeat (cons (string :tag "name")
1088                        (function))))
1089
1090 (defcustom gnus-auto-select-part 1
1091   "Advance to next MIME part when deleting or stripping parts.
1092
1093 When 0, point will be placed on the same part as before.  When
1094 positive (negative), move point forward (backwards) this many
1095 parts.  When nil, redisplay article."
1096   :version "23.1" ;; No Gnus
1097   :group 'gnus-article-mime
1098   :type '(choice (const nil :tag "Redisplay article.")
1099                  (const 1 :tag "Next part.")
1100                  (const 0 :tag "Current part.")
1101                  integer))
1102
1103 ;;;
1104 ;;; The treatment variables
1105 ;;;
1106
1107 (defvar gnus-part-display-hook nil
1108   "Hook called on parts that are to receive treatment.")
1109
1110 (defvar gnus-article-treat-custom
1111   '(choice (const :tag "Off" nil)
1112            (const :tag "On" t)
1113            (const :tag "Header" head)
1114            (const :tag "First" first)
1115            (const :tag "Last" last)
1116            (integer :tag "Less")
1117            (repeat :tag "Groups" regexp)
1118            (sexp :tag "Predicate")))
1119
1120 (defvar gnus-article-treat-head-custom
1121   '(choice (const :tag "Off" nil)
1122            (const :tag "Header" head)))
1123
1124 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1125                                    "text/x-patch" "text/html")
1126   "Part types eligible for treatment.")
1127
1128 (defvar gnus-inhibit-treatment nil
1129   "Whether to inhibit treatment.")
1130
1131 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1132   "Highlight the signature.
1133 Valid values are nil, t, `head', `first', `last', an integer or a
1134 predicate.  See Info node `(gnus)Customizing Articles'."
1135   :group 'gnus-article-treat
1136   :link '(custom-manual "(gnus)Customizing Articles")
1137   :type gnus-article-treat-custom)
1138 (put 'gnus-treat-highlight-signature 'highlight t)
1139
1140 (defcustom gnus-treat-buttonize 100000
1141   "Add buttons.
1142 Valid values are nil, t, `head', `first', `last', an integer or a
1143 predicate.  See Info node `(gnus)Customizing Articles'."
1144   :group 'gnus-article-treat
1145   :link '(custom-manual "(gnus)Customizing Articles")
1146   :type gnus-article-treat-custom)
1147 (put 'gnus-treat-buttonize 'highlight t)
1148
1149 (defcustom gnus-treat-buttonize-head 'head
1150   "Add buttons to the head.
1151 Valid values are nil, t, `head', `first', `last', an integer or a
1152 predicate.  See Info node `(gnus)Customizing Articles'."
1153   :group 'gnus-article-treat
1154   :link '(custom-manual "(gnus)Customizing Articles")
1155   :type gnus-article-treat-head-custom)
1156 (put 'gnus-treat-buttonize-head 'highlight t)
1157
1158 (defcustom gnus-treat-date 'head
1159   "Display dates according to the `gnus-article-date-headers' variable.
1160 Valid values are nil, t, `head', `first', `last', an integer or a
1161 predicate.  See Info node `(gnus)Customizing Articles'."
1162   :version "24.1"
1163   :group 'gnus-article-treat
1164   :link '(custom-manual "(gnus)Customizing Articles")
1165   :type gnus-article-treat-head-custom)
1166
1167 (defcustom gnus-treat-emphasize 50000
1168   "Emphasize text.
1169 Valid values are nil, t, `head', `first', `last', an integer or a
1170 predicate.  See Info node `(gnus)Customizing Articles'."
1171   :group 'gnus-article-treat
1172   :link '(custom-manual "(gnus)Customizing Articles")
1173   :type gnus-article-treat-custom)
1174 (put 'gnus-treat-emphasize 'highlight t)
1175
1176 (defcustom gnus-treat-strip-cr nil
1177   "Remove carriage returns.
1178 Valid values are nil, t, `head', `first', `last', an integer or a
1179 predicate.  See Info node `(gnus)Customizing Articles'."
1180   :version "22.1"
1181   :group 'gnus-article-treat
1182   :link '(custom-manual "(gnus)Customizing Articles")
1183   :type gnus-article-treat-custom)
1184
1185 (defcustom gnus-treat-unsplit-urls nil
1186   "Remove newlines from within URLs.
1187 Valid values are nil, t, `head', `first', `last', an integer or a
1188 predicate.  See Info node `(gnus)Customizing Articles'."
1189   :version "22.1"
1190   :group 'gnus-article-treat
1191   :link '(custom-manual "(gnus)Customizing Articles")
1192   :type gnus-article-treat-custom)
1193
1194 (defcustom gnus-treat-leading-whitespace nil
1195   "Remove leading whitespace in headers.
1196 Valid values are nil, t, `head', `first', `last', an integer or a
1197 predicate.  See Info node `(gnus)Customizing Articles'."
1198   :version "22.1"
1199   :group 'gnus-article-treat
1200   :link '(custom-manual "(gnus)Customizing Articles")
1201   :type gnus-article-treat-custom)
1202
1203 (defcustom gnus-treat-hide-headers 'head
1204   "Hide headers.
1205 Valid values are nil, t, `head', `first', `last', an integer or a
1206 predicate.  See Info node `(gnus)Customizing Articles'."
1207   :group 'gnus-article-treat
1208   :link '(custom-manual "(gnus)Customizing Articles")
1209   :type gnus-article-treat-head-custom)
1210
1211 (defcustom gnus-treat-hide-boring-headers nil
1212   "Hide boring headers.
1213 Valid values are nil, t, `head', `first', `last', an integer or a
1214 predicate.  See Info node `(gnus)Customizing Articles'."
1215   :group 'gnus-article-treat
1216   :link '(custom-manual "(gnus)Customizing Articles")
1217   :type gnus-article-treat-head-custom)
1218
1219 (defcustom gnus-treat-hide-signature nil
1220   "Hide the signature.
1221 Valid values are nil, t, `head', `first', `last', an integer or a
1222 predicate.  See Info node `(gnus)Customizing Articles'."
1223   :group 'gnus-article-treat
1224   :link '(custom-manual "(gnus)Customizing Articles")
1225   :type gnus-article-treat-custom)
1226
1227 (defcustom gnus-treat-fill-article nil
1228   "Fill the article.
1229 Valid values are nil, t, `head', `first', `last', an integer or a
1230 predicate.  See Info node `(gnus)Customizing Articles'."
1231   :group 'gnus-article-treat
1232   :link '(custom-manual "(gnus)Customizing Articles")
1233   :type gnus-article-treat-custom)
1234
1235 (defcustom gnus-treat-hide-citation nil
1236   "Hide cited text.
1237 Valid values are nil, t, `head', `first', `last', an integer or a
1238 predicate.  See Info node `(gnus)Customizing Articles'.
1239
1240 See `gnus-article-highlight-citation' for variables used to
1241 control what it hides."
1242   :group 'gnus-article-treat
1243   :link '(custom-manual "(gnus)Customizing Articles")
1244   :type gnus-article-treat-custom)
1245
1246 (defcustom gnus-treat-hide-citation-maybe nil
1247   "Hide cited text according to certain conditions.
1248 Valid values are nil, t, `head', `first', `last', an integer or a
1249 predicate.  See Info node `(gnus)Customizing Articles'.
1250
1251 See `gnus-cite-hide-percentage' and `gnus-cite-hide-absolute' for
1252 how to control what it hides."
1253   :group 'gnus-article-treat
1254   :link '(custom-manual "(gnus)Customizing Articles")
1255   :type gnus-article-treat-custom)
1256
1257 (defcustom gnus-treat-strip-list-identifiers 'head
1258   "Strip list identifiers from `gnus-list-identifiers`.
1259 Valid values are nil, t, `head', `first', `last', an integer or a
1260 predicate.  See Info node `(gnus)Customizing Articles'."
1261   :version "21.1"
1262   :group 'gnus-article-treat
1263   :link '(custom-manual "(gnus)Customizing Articles")
1264   :type gnus-article-treat-custom)
1265
1266 (gnus-define-group-parameter
1267  list-identifier
1268  :variable-document
1269  "Alist of regexps and correspondent identifiers."
1270  :variable-group gnus-article-washing
1271  :parameter-type
1272  '(choice :tag "Identifier"
1273           :value nil
1274           (symbol :tag "Item in `gnus-list-identifiers'" none)
1275           regexp
1276           (const :tag "None" nil))
1277  :parameter-document
1278  "If non-nil, specify how to remove `identifiers' from articles' subject.
1279
1280 Any symbol is used to look up a regular expression to match the
1281 banner in `gnus-list-identifiers'.  A string is used as a regular
1282 expression to match the identifier directly.")
1283
1284 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1285                         "Gnus 5.10 (Emacs 22.1)")
1286
1287 (defcustom gnus-treat-strip-pem nil
1288   "Strip PEM signatures.
1289 Valid values are nil, t, `head', `first', `last', an integer or a
1290 predicate.  See Info node `(gnus)Customizing Articles'."
1291   :group 'gnus-article-treat
1292   :link '(custom-manual "(gnus)Customizing Articles")
1293   :type gnus-article-treat-custom)
1294
1295 (defcustom gnus-treat-strip-banner t
1296   "Strip banners from articles.
1297 The banner to be stripped is specified in the `banner' group parameter.
1298 Valid values are nil, t, `head', `first', `last', an integer or a
1299 predicate.  See Info node `(gnus)Customizing Articles'."
1300   :group 'gnus-article-treat
1301   :link '(custom-manual "(gnus)Customizing Articles")
1302   :type gnus-article-treat-custom)
1303
1304 (defcustom gnus-treat-highlight-headers 'head
1305   "Highlight the headers.
1306 Valid values are nil, t, `head', `first', `last', an integer or a
1307 predicate.  See Info node `(gnus)Customizing Articles'."
1308   :group 'gnus-article-treat
1309   :link '(custom-manual "(gnus)Customizing Articles")
1310   :type gnus-article-treat-head-custom)
1311 (put 'gnus-treat-highlight-headers 'highlight t)
1312
1313 (defcustom gnus-treat-highlight-citation t
1314   "Highlight cited text.
1315 Valid values are nil, t, `head', `first', `last', an integer or a
1316 predicate.  See Info node `(gnus)Customizing Articles'."
1317   :group 'gnus-article-treat
1318   :link '(custom-manual "(gnus)Customizing Articles")
1319   :type gnus-article-treat-custom)
1320 (put 'gnus-treat-highlight-citation 'highlight t)
1321
1322 (defcustom gnus-treat-strip-headers-in-body t
1323   "Strip the X-No-Archive header line from the beginning of the body.
1324 Valid values are nil, t, `head', `first', `last', an integer or a
1325 predicate.  See Info node `(gnus)Customizing Articles'."
1326   :version "21.1"
1327   :group 'gnus-article-treat
1328   :link '(custom-manual "(gnus)Customizing Articles")
1329   :type gnus-article-treat-custom)
1330
1331 (defcustom gnus-treat-strip-trailing-blank-lines nil
1332   "Strip trailing blank lines.
1333 Valid values are nil, t, `head', `first', `last', an integer or a
1334 predicate.  See Info node `(gnus)Customizing Articles'.
1335
1336 When set to t, it also strips trailing blanks in all MIME parts.
1337 Consider to use `last' instead."
1338   :group 'gnus-article-treat
1339   :link '(custom-manual "(gnus)Customizing Articles")
1340   :type gnus-article-treat-custom)
1341
1342 (defcustom gnus-treat-strip-leading-blank-lines nil
1343   "Strip leading blank lines.
1344 Valid values are nil, t, `head', `first', `last', an integer or a
1345 predicate.  See Info node `(gnus)Customizing Articles'.
1346
1347 When set to t, it also strips trailing blanks in all MIME parts."
1348   :group 'gnus-article-treat
1349   :link '(custom-manual "(gnus)Customizing Articles")
1350   :type gnus-article-treat-custom)
1351
1352 (defcustom gnus-treat-strip-multiple-blank-lines nil
1353   "Strip multiple blank lines.
1354 Valid values are nil, t, `head', `first', `last', an integer or a
1355 predicate.  See Info node `(gnus)Customizing Articles'."
1356   :group 'gnus-article-treat
1357   :link '(custom-manual "(gnus)Customizing Articles")
1358   :type gnus-article-treat-custom)
1359
1360 (defcustom gnus-treat-unfold-headers 'head
1361   "Unfold folded header lines.
1362 Valid values are nil, t, `head', `first', `last', an integer or a
1363 predicate.  See Info node `(gnus)Customizing Articles'."
1364   :version "22.1"
1365   :group 'gnus-article-treat
1366   :link '(custom-manual "(gnus)Customizing Articles")
1367   :type gnus-article-treat-custom)
1368
1369 (defcustom gnus-article-unfold-long-headers nil
1370   "If non-nil, allow unfolding headers even if the header is long.
1371 If it is a regexp, only long headers matching this regexp are unfolded.
1372 If it is t, all long headers are unfolded.
1373
1374 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1375   :version "23.1" ;; No Gnus
1376   :group 'gnus-article-treat
1377   :type '(choice (const nil)
1378                  (const :tag "all" t)
1379                  (regexp)))
1380
1381 (defcustom gnus-treat-fold-headers nil
1382   "Fold headers.
1383 Valid values are nil, t, `head', `first', `last', an integer or a
1384 predicate.  See Info node `(gnus)Customizing Articles'."
1385   :version "22.1"
1386   :group 'gnus-article-treat
1387   :link '(custom-manual "(gnus)Customizing Articles")
1388   :type gnus-article-treat-custom)
1389
1390 (defcustom gnus-treat-fold-newsgroups 'head
1391   "Fold the Newsgroups and Followup-To headers.
1392 Valid values are nil, t, `head', `first', `last', an integer or a
1393 predicate.  See Info node `(gnus)Customizing Articles'."
1394   :version "22.1"
1395   :group 'gnus-article-treat
1396   :link '(custom-manual "(gnus)Customizing Articles")
1397   :type gnus-article-treat-custom)
1398
1399 (defcustom gnus-treat-overstrike t
1400   "Treat overstrike highlighting.
1401 Valid values are nil, t, `head', `first', `last', an integer or a
1402 predicate.  See Info node `(gnus)Customizing Articles'."
1403   :group 'gnus-article-treat
1404   :link '(custom-manual "(gnus)Customizing Articles")
1405   :type gnus-article-treat-custom)
1406 (put 'gnus-treat-overstrike 'highlight t)
1407
1408 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1409   "Treat ANSI SGR control sequences.
1410 Valid values are nil, t, `head', `first', `last', an integer or a
1411 predicate.  See Info node `(gnus)Customizing Articles'."
1412   :group 'gnus-article-treat
1413   :link '(custom-manual "(gnus)Customizing Articles")
1414   :type gnus-article-treat-custom)
1415
1416 (make-obsolete-variable 'gnus-treat-display-xface
1417                         'gnus-treat-display-x-face "Emacs 22.1")
1418
1419 (defcustom gnus-treat-display-x-face
1420   (and (not noninteractive)
1421        (gnus-image-type-available-p 'xbm)
1422        (if (featurep 'xemacs)
1423            (featurep 'xface)
1424          (condition-case nil
1425              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1426                   (executable-find "icontopbm"))
1427            ;; shell-command-to-string may signal an error, e.g. if
1428            ;; shell-file-name is not found.
1429            (error nil)))
1430        'head)
1431   "Display X-Face headers.
1432 Valid values are nil and `head'.
1433 See Info node `(gnus)Customizing Articles' and Info node
1434 `(gnus)X-Face' for details."
1435   :group 'gnus-article-treat
1436   :version "21.1"
1437   :link '(custom-manual "(gnus)Customizing Articles")
1438   :link '(custom-manual "(gnus)X-Face")
1439   :type gnus-article-treat-head-custom
1440   :set (lambda (symbol value)
1441          (set-default
1442           symbol
1443           (cond ((or (boundp symbol) (get symbol 'saved-value))
1444                  value)
1445                 ((boundp 'gnus-treat-display-xface)
1446                  (message "\
1447 ** gnus-treat-display-xface is an obsolete variable;\
1448  use gnus-treat-display-x-face instead")
1449                  (default-value 'gnus-treat-display-xface))
1450                 ((get 'gnus-treat-display-xface 'saved-value)
1451                  (message "\
1452 ** gnus-treat-display-xface is an obsolete variable;\
1453  use gnus-treat-display-x-face instead")
1454                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1455                 (t
1456                  value)))))
1457 (put 'gnus-treat-display-x-face 'highlight t)
1458
1459 (defcustom gnus-treat-display-face
1460   (and (not noninteractive)
1461        (gnus-image-type-available-p 'png)
1462        'head)
1463   "Display Face headers.
1464 Valid values are nil, t, `head', `first', `last', an integer or a
1465 predicate.  See Info node `(gnus)Customizing Articles' and Info
1466 node `(gnus)Face' for details."
1467   :group 'gnus-article-treat
1468   :version "22.1"
1469   :link '(custom-manual "(gnus)Customizing Articles")
1470   :link '(custom-manual "(gnus)X-Face")
1471   :type gnus-article-treat-head-custom)
1472 (put 'gnus-treat-display-face 'highlight t)
1473
1474 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1475   "Display smileys.
1476 Valid values are nil, t, `head', `first', `last', an integer or a
1477 predicate.  See Info node `(gnus)Customizing Articles' and Info
1478 node `(gnus)Smileys' for details."
1479   :group 'gnus-article-treat
1480   :version "21.1"
1481   :link '(custom-manual "(gnus)Customizing Articles")
1482   :link '(custom-manual "(gnus)Smileys")
1483   :type gnus-article-treat-custom)
1484 (put 'gnus-treat-display-smileys 'highlight t)
1485
1486 (defcustom gnus-treat-from-picon
1487   (if (and (gnus-image-type-available-p 'xpm)
1488            (gnus-picons-installed-p))
1489       'head nil)
1490   "Display picons in the From header.
1491 Valid values are nil, t, `head', `first', `last', an integer or a
1492 predicate.  See Info node `(gnus)Customizing Articles' and Info
1493 node `(gnus)Picons' for details."
1494   :version "22.1"
1495   :group 'gnus-article-treat
1496   :group 'gnus-picon
1497   :link '(custom-manual "(gnus)Customizing Articles")
1498   :link '(custom-manual "(gnus)Picons")
1499   :type gnus-article-treat-head-custom)
1500 (put 'gnus-treat-from-picon 'highlight t)
1501
1502 (defcustom gnus-treat-mail-picon
1503   (if (and (gnus-image-type-available-p 'xpm)
1504            (gnus-picons-installed-p))
1505       'head nil)
1506   "Display picons in To and Cc headers.
1507 Valid values are nil, t, `head', `first', `last', an integer or a
1508 predicate.  See Info node `(gnus)Customizing Articles' and Info
1509 node `(gnus)Picons' for details."
1510   :version "22.1"
1511   :group 'gnus-article-treat
1512   :group 'gnus-picon
1513   :link '(custom-manual "(gnus)Customizing Articles")
1514   :link '(custom-manual "(gnus)Picons")
1515   :type gnus-article-treat-head-custom)
1516 (put 'gnus-treat-mail-picon 'highlight t)
1517
1518 (defcustom gnus-treat-newsgroups-picon
1519   (if (and (gnus-image-type-available-p 'xpm)
1520            (gnus-picons-installed-p))
1521       'head nil)
1522   "Display picons in the Newsgroups and Followup-To headers.
1523 Valid values are nil, t, `head', `first', `last', an integer or a
1524 predicate.  See Info node `(gnus)Customizing Articles' and Info
1525 node `(gnus)Picons' for details."
1526   :version "22.1"
1527   :group 'gnus-article-treat
1528   :group 'gnus-picon
1529   :link '(custom-manual "(gnus)Customizing Articles")
1530   :link '(custom-manual "(gnus)Picons")
1531   :type gnus-article-treat-head-custom)
1532 (put 'gnus-treat-newsgroups-picon 'highlight t)
1533
1534 (defcustom gnus-treat-from-gravatar nil
1535   "Display gravatars in the From header.
1536 Valid values are nil, t, `head', `first', `last', an integer or a
1537 predicate.  See Info node `(gnus)Customizing Articles' and Info
1538 node `(gnus)Gravatars' for details."
1539   :version "24.1"
1540   :group 'gnus-article-treat
1541   :group 'gnus-gravatar
1542   :link '(custom-manual "(gnus)Customizing Articles")
1543   :link '(custom-manual "(gnus)Gravatars")
1544   :type gnus-article-treat-head-custom)
1545 (put 'gnus-treat-from-gravatar 'highlight t)
1546
1547 (defcustom gnus-treat-mail-gravatar nil
1548   "Display gravatars in To and Cc headers.
1549 Valid values are nil, t, `head', `first', `last', an integer or a
1550 predicate.  See Info node `(gnus)Customizing Articles' and Info
1551 node `(gnus)Gravatars' for details."
1552   :version "24.1"
1553   :group 'gnus-article-treat
1554   :group 'gnus-gravatar
1555   :link '(custom-manual "(gnus)Customizing Articles")
1556   :link '(custom-manual "(gnus)Gravatars")
1557   :type gnus-article-treat-head-custom)
1558 (put 'gnus-treat-mail-gravatar 'highlight t)
1559
1560 (defcustom gnus-treat-body-boundary
1561   (if (or gnus-treat-newsgroups-picon
1562           gnus-treat-mail-picon
1563           gnus-treat-from-picon
1564           gnus-treat-from-gravatar
1565           gnus-treat-mail-gravatar)
1566       ;; If there's much decoration, the user might prefer a boundary.
1567       'head
1568     nil)
1569   "Draw a boundary at the end of the headers.
1570 Valid values are nil and `head'.
1571 See Info node `(gnus)Customizing Articles' for details."
1572   :version "22.1"
1573   :group 'gnus-article-treat
1574   :link '(custom-manual "(gnus)Customizing Articles")
1575   :type gnus-article-treat-head-custom)
1576
1577 (defcustom gnus-treat-capitalize-sentences nil
1578   "Capitalize sentence-starting words.
1579 Valid values are nil, t, `head', `first', `last', an integer or a
1580 predicate.  See Info node `(gnus)Customizing Articles'."
1581   :version "21.1"
1582   :group 'gnus-article-treat
1583   :link '(custom-manual "(gnus)Customizing Articles")
1584   :type gnus-article-treat-custom)
1585
1586 (defcustom gnus-treat-wash-html nil
1587   "Format as HTML.
1588 Valid values are nil, t, `head', `first', `last', an integer or a
1589 predicate.  See Info node `(gnus)Customizing Articles'."
1590   :version "22.1"
1591   :group 'gnus-article-treat
1592   :link '(custom-manual "(gnus)Customizing Articles")
1593   :type gnus-article-treat-custom)
1594
1595 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1596   "Fill long lines.
1597 Valid values are nil, t, `head', `first', `last', an integer or a
1598 predicate.  See Info node `(gnus)Customizing Articles'."
1599   :version "24.1"
1600   :group 'gnus-article-treat
1601   :link '(custom-manual "(gnus)Customizing Articles")
1602   :type gnus-article-treat-custom)
1603
1604 (defcustom gnus-treat-x-pgp-sig nil
1605   "Verify X-PGP-Sig.
1606 To automatically treat X-PGP-Sig, set it to head.
1607 Valid values are nil, t, `head', `first', `last', an integer or a
1608 predicate.  See Info node `(gnus)Customizing Articles'."
1609   :version "22.1"
1610   :group 'gnus-article-treat
1611   :group 'mime-security
1612   :link '(custom-manual "(gnus)Customizing Articles")
1613   :type gnus-article-treat-custom)
1614
1615 (defvar gnus-article-encrypt-protocol-alist
1616   '(("PGP" . mml2015-self-encrypt)))
1617
1618 ;; Set to nil if more than one protocol added to
1619 ;; gnus-article-encrypt-protocol-alist.
1620 (defcustom gnus-article-encrypt-protocol "PGP"
1621   "The protocol used for encrypt articles.
1622 It is a string, such as \"PGP\". If nil, ask user."
1623   :version "22.1"
1624   :type 'string
1625   :group 'mime-security)
1626
1627 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1628                               (mm-coding-system-p 'utf-8)
1629                               (executable-find idna-program))
1630   "Whether IDNA decoding of headers is used when viewing messages.
1631 This requires GNU Libidn, and by default only enabled if it is found."
1632   :version "22.1"
1633   :group 'gnus-article-headers
1634   :type 'boolean)
1635
1636 (defcustom gnus-article-over-scroll nil
1637   "If non-nil, allow scrolling the article buffer even when there no more text."
1638   :version "22.1"
1639   :group 'gnus-article
1640   :type 'boolean)
1641
1642 (defcustom gnus-inhibit-images nil
1643   "Non-nil means inhibit displaying of images inline in the article body."
1644   :version "24.1"
1645   :group 'gnus-article
1646   :type 'boolean)
1647
1648 (defcustom gnus-blocked-images 'gnus-block-private-groups
1649   "Images that have URLs matching this regexp will be blocked.
1650 This can also be a function to be evaluated.  If so, it will be
1651 called with the group name as the parameter, and should return a
1652 regexp."
1653   :version "24.1"
1654   :group 'gnus-art
1655   :type 'regexp)
1656
1657 ;;; Internal variables
1658
1659 (defvar gnus-english-month-names
1660   '("January" "February" "March" "April" "May" "June" "July" "August"
1661     "September" "October" "November" "December"))
1662
1663 (defvar article-goto-body-goes-to-point-min-p nil)
1664 (defvar gnus-article-wash-types nil)
1665 (defvar gnus-article-emphasis-alist nil)
1666 (defvar gnus-article-image-alist nil)
1667
1668 (defvar gnus-article-mime-handle-alist-1 nil)
1669 (defvar gnus-treatment-function-alist
1670   '((gnus-treat-strip-cr gnus-article-remove-cr)
1671     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1672     (gnus-treat-strip-banner gnus-article-strip-banner)
1673     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1674     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1675     (gnus-treat-buttonize gnus-article-add-buttons)
1676     (gnus-treat-fill-article gnus-article-fill-cited-article)
1677     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1678     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1679     (gnus-treat-display-x-face gnus-article-display-x-face)
1680     (gnus-treat-display-face gnus-article-display-face)
1681     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1682     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1683     (gnus-treat-hide-signature gnus-article-hide-signature)
1684     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1685     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1686     (gnus-treat-from-picon gnus-treat-from-picon)
1687     (gnus-treat-mail-picon gnus-treat-mail-picon)
1688     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1689     (gnus-treat-strip-pem gnus-article-hide-pem)
1690     (gnus-treat-date gnus-article-treat-date)
1691     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1692     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1693     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1694     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1695     (gnus-treat-strip-trailing-blank-lines
1696      gnus-article-remove-trailing-blank-lines)
1697     (gnus-treat-strip-leading-blank-lines
1698      gnus-article-strip-leading-blank-lines)
1699     (gnus-treat-strip-multiple-blank-lines
1700      gnus-article-strip-multiple-blank-lines)
1701     (gnus-treat-overstrike gnus-article-treat-overstrike)
1702     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1703     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1704     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1705     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1706     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1707     (gnus-treat-display-smileys gnus-treat-smiley)
1708     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1709     (gnus-treat-wash-html gnus-article-wash-html)
1710     (gnus-treat-emphasize gnus-article-emphasize)
1711     (gnus-treat-hide-citation gnus-article-hide-citation)
1712     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1713     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1714     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1715
1716 (defvar gnus-article-mime-handle-alist nil)
1717 (defvar article-lapsed-timer nil)
1718 (defvar gnus-article-current-summary nil)
1719
1720 (defvar gnus-article-mode-syntax-table
1721   (let ((table (copy-syntax-table text-mode-syntax-table)))
1722     ;; This causes the citation match run O(2^n).
1723     ;; (modify-syntax-entry ?- "w" table)
1724     (modify-syntax-entry ?> ")<" table)
1725     (modify-syntax-entry ?< "(>" table)
1726     ;; make M-. in article buffers work for `foo' strings
1727     (modify-syntax-entry ?' " " table)
1728     (modify-syntax-entry ?` " " table)
1729     table)
1730   "Syntax table used in article mode buffers.
1731 Initialized from `text-mode-syntax-table.")
1732
1733 (defvar gnus-save-article-buffer nil)
1734
1735 (defvar gnus-number-of-articles-to-be-saved nil)
1736
1737 (defvar gnus-inhibit-hiding nil)
1738
1739 (defvar gnus-article-edit-mode nil)
1740
1741 ;;; Macros for dealing with the article buffer.
1742
1743 (defmacro gnus-with-article-headers (&rest forms)
1744   `(with-current-buffer gnus-article-buffer
1745      (save-restriction
1746        (let ((inhibit-read-only t)
1747              (inhibit-point-motion-hooks t)
1748              (case-fold-search t))
1749          (article-narrow-to-head)
1750          ,@forms))))
1751
1752 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1753 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1754
1755 (defmacro gnus-with-article-buffer (&rest forms)
1756   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1757      (with-current-buffer gnus-article-buffer
1758        (let ((inhibit-read-only t))
1759          ,@forms))))
1760
1761 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1762 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1763
1764 (defun gnus-article-goto-header (header)
1765   "Go to HEADER, which is a regular expression."
1766   (re-search-forward (concat "^\\(" header "\\):") nil t))
1767
1768 (defsubst gnus-article-hide-text (b e props)
1769   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1770   (gnus-add-text-properties-when 'article-type nil b e props)
1771   (when (memq 'intangible props)
1772     (put-text-property
1773      (max (1- b) (point-min))
1774      b 'intangible (cddr (memq 'intangible props)))))
1775
1776 (defsubst gnus-article-unhide-text (b e)
1777   "Remove hidden text properties from region between B and E."
1778   (remove-text-properties b e gnus-hidden-properties)
1779   (when (memq 'intangible gnus-hidden-properties)
1780     (put-text-property (max (1- b) (point-min))
1781                        b 'intangible nil)))
1782
1783 (defun gnus-article-hide-text-type (b e type)
1784   "Hide text of TYPE between B and E."
1785   (gnus-add-wash-type type)
1786   (gnus-article-hide-text
1787    b e (cons 'article-type (cons type gnus-hidden-properties))))
1788
1789 (defun gnus-article-unhide-text-type (b e type)
1790   "Unhide text of TYPE between B and E."
1791   (gnus-delete-wash-type type)
1792   (remove-text-properties
1793    b e (cons 'article-type (cons type gnus-hidden-properties)))
1794   (when (memq 'intangible gnus-hidden-properties)
1795     (put-text-property (max (1- b) (point-min))
1796                        b 'intangible nil)))
1797
1798 (defun gnus-article-delete-text-of-type (type)
1799   "Delete text of TYPE in the current buffer."
1800   (save-excursion
1801     (let ((b (point-min)))
1802       (if (eq type 'multipart)
1803           ;; Remove MIME buttons associated with multipart/alternative parts.
1804           (progn
1805             (goto-char b)
1806             (while (if (get-text-property (point) 'gnus-part)
1807                        (setq b (point))
1808                      (when (setq b (next-single-property-change (point)
1809                                                                 'gnus-part))
1810                        (goto-char b)
1811                        t))
1812               (end-of-line)
1813               (skip-chars-forward "\n")
1814               (when (eq (get-text-property b 'article-type) 'multipart)
1815                 (delete-region b (point)))))
1816         (while (setq b (text-property-any b (point-max) 'article-type type))
1817           (delete-region
1818            b (or (text-property-not-all b (point-max) 'article-type type)
1819                  (point-max))))))))
1820
1821 (defun gnus-article-delete-invisible-text ()
1822   "Delete all invisible text in the current buffer."
1823   (save-excursion
1824     (let ((b (point-min)))
1825       (while (setq b (text-property-any b (point-max) 'invisible t))
1826         (delete-region
1827          b (or (text-property-not-all b (point-max) 'invisible t)
1828                (point-max)))))))
1829
1830 (defsubst gnus-article-header-rank ()
1831   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1832   (let ((list gnus-sorted-header-list)
1833         (i 1))
1834     (while list
1835       (if (looking-at (car list))
1836           (setq list nil)
1837         (setq list (cdr list))
1838         (incf i)))
1839       i))
1840
1841 (defun article-hide-headers (&optional arg delete)
1842   "Hide unwanted headers and possibly sort them as well."
1843   (interactive)
1844   ;; This function might be inhibited.
1845   (unless gnus-inhibit-hiding
1846     (let ((inhibit-read-only t)
1847           (case-fold-search t)
1848           (max (1+ (length gnus-sorted-header-list)))
1849           (inhibit-point-motion-hooks t)
1850           (cur (current-buffer))
1851           ignored visible beg)
1852       (save-excursion
1853         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1854         ;; group parameters, so we should go to the summary buffer.
1855         (when (prog1
1856                   (condition-case nil
1857                       (progn (set-buffer gnus-summary-buffer) t)
1858                     (error nil))
1859                 (setq ignored (when (not gnus-visible-headers)
1860                                 (cond ((stringp gnus-ignored-headers)
1861                                        gnus-ignored-headers)
1862                                       ((listp gnus-ignored-headers)
1863                                        (mapconcat 'identity
1864                                                   gnus-ignored-headers
1865                                                   "\\|"))))
1866                       visible (cond ((stringp gnus-visible-headers)
1867                                      gnus-visible-headers)
1868                                     ((and gnus-visible-headers
1869                                           (listp gnus-visible-headers))
1870                                      (mapconcat 'identity
1871                                                 gnus-visible-headers
1872                                                 "\\|")))))
1873           (set-buffer cur))
1874         (save-restriction
1875           ;; First we narrow to just the headers.
1876           (article-narrow-to-head)
1877           ;; Hide any "From " lines at the beginning of (mail) articles.
1878           (while (looking-at "From ")
1879             (forward-line 1))
1880           (unless (bobp)
1881             (delete-region (point-min) (point)))
1882           ;; Then treat the rest of the header lines.
1883           ;; Then we use the two regular expressions
1884           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1885           ;; select which header lines is to remain visible in the
1886           ;; article buffer.
1887           (while (re-search-forward "^[^ \t:]*:" nil t)
1888             (beginning-of-line)
1889             ;; Mark the rank of the header.
1890             (put-text-property
1891              (point) (1+ (point)) 'message-rank
1892              (if (or (and visible (looking-at visible))
1893                      (and ignored
1894                           (not (looking-at ignored))))
1895                  (gnus-article-header-rank)
1896                (+ 2 max)))
1897             (forward-line 1))
1898           (message-sort-headers-1)
1899           (when (setq beg (text-property-any
1900                            (point-min) (point-max) 'message-rank (+ 2 max)))
1901             ;; We delete the unwanted headers.
1902             (gnus-add-wash-type 'headers)
1903             (add-text-properties (point-min) (+ 5 (point-min))
1904                                  '(article-type headers dummy-invisible t))
1905             (delete-region beg (point-max))))))))
1906
1907 (defun article-hide-boring-headers (&optional arg)
1908   "Toggle hiding of headers that aren't very interesting.
1909 If given a negative prefix, always show; if given a positive prefix,
1910 always hide."
1911   (interactive (gnus-article-hidden-arg))
1912   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1913              (not gnus-show-all-headers))
1914     (save-excursion
1915       (save-restriction
1916         (let ((inhibit-read-only t)
1917               (inhibit-point-motion-hooks t))
1918           (article-narrow-to-head)
1919           (dolist (elem gnus-boring-article-headers)
1920             (goto-char (point-min))
1921             (cond
1922              ;; Hide empty headers.
1923              ((eq elem 'empty)
1924               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1925                 (forward-line -1)
1926                 (gnus-article-hide-text-type
1927                  (point-at-bol)
1928                  (progn
1929                    (end-of-line)
1930                    (if (re-search-forward "^[^ \t]" nil t)
1931                        (match-beginning 0)
1932                      (point-max)))
1933                  'boring-headers)))
1934              ;; Hide boring Newsgroups header.
1935              ((eq elem 'newsgroups)
1936               (when (gnus-string-equal
1937                      (gnus-fetch-field "newsgroups")
1938                      (gnus-group-real-name
1939                       (if (boundp 'gnus-newsgroup-name)
1940                           gnus-newsgroup-name
1941                         "")))
1942                 (gnus-article-hide-header "newsgroups")))
1943              ((eq elem 'to-address)
1944               (let ((to (message-fetch-field "to"))
1945                     (to-address
1946                      (gnus-parameter-to-address
1947                       (if (boundp 'gnus-newsgroup-name)
1948                           gnus-newsgroup-name ""))))
1949                 (when (and to to-address
1950                            (ignore-errors
1951                              (gnus-string-equal
1952                               ;; only one address in To
1953                               (nth 1 (mail-extract-address-components to))
1954                               to-address)))
1955                   (gnus-article-hide-header "to"))))
1956              ((eq elem 'to-list)
1957               (let ((to (message-fetch-field "to"))
1958                     (to-list
1959                      (gnus-parameter-to-list
1960                       (if (boundp 'gnus-newsgroup-name)
1961                           gnus-newsgroup-name ""))))
1962                 (when (and to to-list
1963                            (ignore-errors
1964                              (gnus-string-equal
1965                               ;; only one address in To
1966                               (nth 1 (mail-extract-address-components to))
1967                               to-list)))
1968                   (gnus-article-hide-header "to"))))
1969              ((eq elem 'cc-list)
1970               (let ((cc (message-fetch-field "cc"))
1971                     (to-list
1972                      (gnus-parameter-to-list
1973                       (if (boundp 'gnus-newsgroup-name)
1974                           gnus-newsgroup-name ""))))
1975                 (when (and cc to-list
1976                            (ignore-errors
1977                              (gnus-string-equal
1978                               ;; only one address in CC
1979                               (nth 1 (mail-extract-address-components cc))
1980                               to-list)))
1981                   (gnus-article-hide-header "cc"))))
1982              ((eq elem 'followup-to)
1983               (when (gnus-string-equal
1984                      (message-fetch-field "followup-to")
1985                      (message-fetch-field "newsgroups"))
1986                 (gnus-article-hide-header "followup-to")))
1987              ((eq elem 'reply-to)
1988               (if (gnus-group-find-parameter
1989                    gnus-newsgroup-name 'broken-reply-to)
1990                   (gnus-article-hide-header "reply-to")
1991                 (let ((from (message-fetch-field "from"))
1992                       (reply-to (message-fetch-field "reply-to")))
1993                   (when
1994                       (and
1995                        from reply-to
1996                        (ignore-errors
1997                          (equal
1998                           (sort (mapcar
1999                                  (lambda (x) (downcase (cadr x)))
2000                                  (mail-extract-address-components from t))
2001                                 'string<)
2002                           (sort (mapcar
2003                                  (lambda (x) (downcase (cadr x)))
2004                                  (mail-extract-address-components reply-to t))
2005                                 'string<))))
2006                     (gnus-article-hide-header "reply-to")))))
2007              ((eq elem 'date)
2008               (let ((date (with-current-buffer gnus-original-article-buffer
2009                             ;; If date in `gnus-article-buffer' is localized
2010                             ;; (`gnus-treat-date-user-defined'),
2011                             ;; `days-between' might fail.
2012                             (message-fetch-field "date"))))
2013                 (when (and date
2014                            (< (days-between (current-time-string) date)
2015                               4))
2016                   (gnus-article-hide-header "date"))))
2017              ((eq elem 'long-to)
2018               (let ((to (message-fetch-field "to"))
2019                     (cc (message-fetch-field "cc")))
2020                 (when (> (length to) 1024)
2021                   (gnus-article-hide-header "to"))
2022                 (when (> (length cc) 1024)
2023                   (gnus-article-hide-header "cc"))))
2024              ((eq elem 'many-to)
2025               (let ((to-count 0)
2026                     (cc-count 0))
2027                 (goto-char (point-min))
2028                 (while (re-search-forward "^to:" nil t)
2029                   (setq to-count (1+ to-count)))
2030                 (when (> to-count 1)
2031                   (while (> to-count 0)
2032                     (goto-char (point-min))
2033                     (save-restriction
2034                       (re-search-forward "^to:" nil nil to-count)
2035                       (forward-line -1)
2036                       (narrow-to-region (point) (point-max))
2037                       (gnus-article-hide-header "to"))
2038                     (setq to-count (1- to-count))))
2039                 (goto-char (point-min))
2040                 (while (re-search-forward "^cc:" nil t)
2041                   (setq cc-count (1+ cc-count)))
2042                 (when (> cc-count 1)
2043                   (while (> cc-count 0)
2044                     (goto-char (point-min))
2045                     (save-restriction
2046                       (re-search-forward "^cc:" nil nil cc-count)
2047                       (forward-line -1)
2048                       (narrow-to-region (point) (point-max))
2049                       (gnus-article-hide-header "cc"))
2050                     (setq cc-count (1- cc-count)))))))))))))
2051
2052 (defun gnus-article-hide-header (header)
2053   (save-excursion
2054     (goto-char (point-min))
2055     (when (re-search-forward (concat "^" header ":") nil t)
2056       (gnus-article-hide-text-type
2057        (point-at-bol)
2058        (progn
2059          (end-of-line)
2060          (if (re-search-forward "^[^ \t]" nil t)
2061              (match-beginning 0)
2062            (point-max)))
2063        'boring-headers))))
2064
2065 (defvar gnus-article-normalized-header-length 40
2066   "Length of normalized headers.")
2067
2068 (defun article-normalize-headers ()
2069   "Make all header lines 40 characters long."
2070   (interactive)
2071   (let ((inhibit-read-only t)
2072         column)
2073     (save-excursion
2074       (save-restriction
2075         (article-narrow-to-head)
2076         (while (not (eobp))
2077           (cond
2078            ((< (setq column (- (point-at-eol) (point)))
2079                gnus-article-normalized-header-length)
2080             (end-of-line)
2081             (insert (make-string
2082                      (- gnus-article-normalized-header-length column)
2083                      ? )))
2084            ((> column gnus-article-normalized-header-length)
2085             (gnus-put-text-property
2086              (progn
2087                (forward-char gnus-article-normalized-header-length)
2088                (point))
2089              (point-at-eol)
2090              'invisible t))
2091            (t
2092             ;; Do nothing.
2093             ))
2094           (forward-line 1))))))
2095
2096 (defun article-treat-dumbquotes ()
2097   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2098 Note that this function guesses whether a character is a sm*rtq**t* or
2099 not, so it should only be used interactively.
2100
2101 Sm*rtq**t*s are M****s***'s unilateral extension to the
2102 iso-8859-1 character map in an attempt to provide more quoting
2103 characters.  If you see something like \\222 or \\264 where
2104 you're expecting some kind of apostrophe or quotation mark, then
2105 try this wash."
2106   (interactive)
2107   (article-translate-strings gnus-article-dumbquotes-map))
2108
2109 (defvar org-entities)
2110
2111 (defun article-treat-non-ascii ()
2112   "Translate many Unicode characters into their ASCII equivalents."
2113   (interactive)
2114   (require 'org-entities)
2115   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2116     (dolist (elem org-entities)
2117       (when (and (listp elem)
2118                  (= (length (nth 6 elem)) 1))
2119         (if (featurep 'xemacs)
2120             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2121           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2122     (save-excursion
2123       (when (article-goto-body)
2124         (let ((inhibit-read-only t)
2125               replace props)
2126           (while (not (eobp))
2127             (if (not (setq replace (if (featurep 'xemacs)
2128                                        (get-char-table (following-char) table)
2129                                      (aref table (following-char)))))
2130                 (forward-char 1)
2131               (if (prog1
2132                       (setq props (text-properties-at (point)))
2133                     (delete-char 1))
2134                   (add-text-properties (point) (progn (insert replace) (point))
2135                                        props)
2136                 (insert replace)))))))))
2137
2138 (defun article-translate-strings (map)
2139   "Translate all string in the body of the article according to MAP.
2140 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2141   (save-excursion
2142     (when (article-goto-body)
2143       (let ((inhibit-read-only t))
2144         (dolist (elem map)
2145           (let ((from (car elem))
2146                 (to (cadr elem)))
2147             (save-excursion
2148               (if (stringp from)
2149                   (while (search-forward from nil t)
2150                     (replace-match to))
2151                 (while (not (eobp))
2152                   (if (eq (following-char) from)
2153                       (progn
2154                         (delete-char 1)
2155                         (insert to))
2156                     (forward-char 1)))))))))))
2157
2158 (defun article-treat-overstrike ()
2159   "Translate overstrikes into bold text."
2160   (interactive)
2161   (save-excursion
2162     (when (article-goto-body)
2163       (let ((inhibit-read-only t))
2164         (while (search-forward "\b" nil t)
2165           (let ((next (char-after))
2166                 (previous (char-after (- (point) 2))))
2167             ;; We do the boldification/underlining by hiding the
2168             ;; overstrikes and putting the proper text property
2169             ;; on the letters.
2170             (cond
2171              ((eq next previous)
2172               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2173               (put-text-property (point) (1+ (point)) 'face 'bold))
2174              ((eq next ?_)
2175               (gnus-article-hide-text-type
2176                (1- (point)) (1+ (point)) 'overstrike)
2177               (put-text-property
2178                (- (point) 2) (1- (point)) 'face 'underline))
2179              ((eq previous ?_)
2180               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2181               (put-text-property
2182                (point) (1+ (point)) 'face 'underline)))))))))
2183
2184 (defun article-treat-ansi-sequences ()
2185   "Translate ANSI SGR control sequences into overlays or extents."
2186   (interactive)
2187   (save-excursion
2188     (when (article-goto-body)
2189       (let ((inhibit-read-only t))
2190         (ansi-color-apply-on-region (point) (point-max))))))
2191
2192 (defun gnus-article-treat-unfold-headers ()
2193   "Unfold folded message headers.
2194 Only the headers that fit into the current window width will be
2195 unfolded."
2196   (interactive)
2197   (gnus-with-article-headers
2198     (let (length)
2199       (while (not (eobp))
2200         (save-restriction
2201           (mail-header-narrow-to-field)
2202           (let* ((header (buffer-string))
2203                  (unfoldable
2204                   (or (equal gnus-article-unfold-long-headers t)
2205                       (and (stringp gnus-article-unfold-long-headers)
2206                            (string-match gnus-article-unfold-long-headers
2207                                          header)))))
2208             (with-temp-buffer
2209               (insert header)
2210               (goto-char (point-min))
2211               (while (re-search-forward "\n[\t ]" nil t)
2212                 (replace-match " " t t)))
2213             (setq length (- (point-max) (point-min) 1))
2214             (when (or unfoldable
2215                       (< length (window-width)))
2216               (while (re-search-forward "\n[\t ]" nil t)
2217                 (replace-match " " t t))))
2218           (goto-char (point-max)))))))
2219
2220 (defun gnus-article-treat-fold-headers ()
2221   "Fold message headers."
2222   (interactive)
2223   (gnus-with-article-headers
2224     (while (not (eobp))
2225       (save-restriction
2226         (mail-header-narrow-to-field)
2227         (mail-header-fold-field)
2228         (goto-char (point-max))))))
2229
2230 (defun gnus-treat-smiley ()
2231   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2232   (interactive)
2233   (gnus-with-article-buffer
2234     (if (memq 'smiley gnus-article-wash-types)
2235         (gnus-delete-images 'smiley)
2236       (article-goto-body)
2237       (let ((images (smiley-region (point) (point-max))))
2238         (when images
2239           (gnus-add-wash-type 'smiley)
2240           (dolist (image images)
2241             (gnus-add-image 'smiley image)))))))
2242
2243 (defun gnus-article-remove-images ()
2244   "Remove all images from the article buffer."
2245   (interactive)
2246   (gnus-with-article-buffer
2247     (save-restriction
2248       (widen)
2249       (dolist (elem gnus-article-image-alist)
2250         (gnus-delete-images (car elem))))))
2251
2252 (autoload 'w3m-toggle-inline-images "w3m")
2253
2254 (defun gnus-article-show-images ()
2255   "Show any images that are in the HTML-rendered article buffer.
2256 This only works if the article in question is HTML."
2257   (interactive)
2258   (gnus-with-article-buffer
2259     (save-restriction
2260       (widen)
2261       (if (eq mm-text-html-renderer 'w3m)
2262           (let ((mm-inline-text-html-with-images nil))
2263             (w3m-toggle-inline-images))
2264         (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2265                                                         'image-displayer))
2266           (destructuring-bind (start end function) region
2267             (funcall function (get-text-property start 'image-url)
2268                      start end)))))))
2269
2270 (defun gnus-article-treat-fold-newsgroups ()
2271   "Unfold folded message headers.
2272 Only the headers that fit into the current window width will be
2273 unfolded."
2274   (interactive)
2275   (gnus-with-article-headers
2276     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2277       (save-restriction
2278         (mail-header-narrow-to-field)
2279         (while (re-search-forward ", *" nil t)
2280           (replace-match ", " t t))
2281         (mail-header-fold-field)
2282         (goto-char (point-max))))))
2283
2284 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2285   "Value of `truncate-lines' in Gnus Article buffer.
2286 Valid values are nil, t, `head', `first', `last', an integer or a
2287 predicate.  See Info node `(gnus)Customizing Articles'."
2288   :version "23.1" ;; No Gnus
2289   :group 'gnus-article
2290   ;; :link '(custom-manual "(gnus)Customizing Articles")
2291   :type 'boolean)
2292
2293 (defun gnus-article-toggle-truncate-lines (&optional arg)
2294   "Toggle whether to fold or truncate long lines in article the buffer.
2295 If ARG is non-nil and not a number, toggle
2296 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2297 long lines if and only if arg is positive."
2298   (interactive "P")
2299   (cond
2300    ((and (numberp arg) (> arg 0))
2301     (setq gnus-article-truncate-lines t))
2302    ((numberp arg)
2303     (setq gnus-article-truncate-lines nil))
2304    (arg
2305     (setq gnus-article-truncate-lines
2306           (not gnus-article-truncate-lines))))
2307   (gnus-with-article-buffer
2308     (cond
2309      ((and (numberp arg) (> arg 0))
2310       (setq truncate-lines nil))
2311      ((numberp arg)
2312       (setq truncate-lines t)))
2313     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2314     ;; `toggle-truncate-lines' needs an argument.
2315     (toggle-truncate-lines)))
2316
2317 (defun gnus-article-treat-body-boundary ()
2318   "Place a boundary line at the end of the headers."
2319   (interactive)
2320   (when (and gnus-body-boundary-delimiter
2321              (> (length gnus-body-boundary-delimiter) 0))
2322     (gnus-with-article-headers
2323       (goto-char (point-max))
2324       (let ((start (point)))
2325         (insert "X-Boundary: ")
2326         (gnus-add-text-properties start (point) '(invisible t intangible t))
2327        (insert (let (str (max (window-width)))
2328                  (if (featurep 'xemacs)
2329                      (setq max (1- max)))
2330                  (while (>= max (length str))
2331                     (setq str (concat str gnus-body-boundary-delimiter)))
2332                  (substring str 0 max))
2333                 "\n")
2334         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2335
2336 (defun article-fill-long-lines ()
2337   "Fill lines that are wider than the window width."
2338   (interactive)
2339   (save-excursion
2340     (let ((inhibit-read-only t)
2341           (width (window-width (get-buffer-window (current-buffer)))))
2342       (save-restriction
2343         (article-goto-body)
2344         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2345           (while (not (eobp))
2346             (end-of-line)
2347             (when (>= (current-column) (min fill-column width))
2348               (narrow-to-region (min (1+ (point)) (point-max))
2349                                 (point-at-bol))
2350               (let ((goback (point-marker)))
2351                 (fill-paragraph nil)
2352                 (goto-char (marker-position goback)))
2353               (widen))
2354             (forward-line 1)))))))
2355
2356 (defun article-capitalize-sentences ()
2357   "Capitalize the first word in each sentence."
2358   (interactive)
2359   (save-excursion
2360     (let ((inhibit-read-only t)
2361           (paragraph-start "^[\n\^L]"))
2362       (article-goto-body)
2363       (while (not (eobp))
2364         (capitalize-word 1)
2365         (forward-sentence)))))
2366
2367 (defun article-remove-cr ()
2368   "Remove trailing CRs and then translate remaining CRs into LFs."
2369   (interactive)
2370   (save-excursion
2371     (let ((inhibit-read-only t))
2372       (goto-char (point-min))
2373       (while (re-search-forward "\r+$" nil t)
2374         (replace-match "" t t))
2375       (goto-char (point-min))
2376       (while (search-forward "\r" nil t)
2377         (replace-match "\n" t t)))))
2378
2379 (defun article-remove-trailing-blank-lines ()
2380   "Remove all trailing blank lines from the article."
2381   (interactive)
2382   (save-excursion
2383     (let ((inhibit-read-only t))
2384       (goto-char (point-max))
2385       (delete-region
2386        (point)
2387        (progn
2388          (while (and (not (bobp))
2389                      (looking-at "^[ \t]*$")
2390                      (not (gnus-annotation-in-region-p
2391                            (point) (point-at-eol))))
2392            (forward-line -1))
2393          (forward-line 1)
2394          (point))))))
2395
2396 (defvar gnus-face-properties-alist)
2397
2398 (defun article-display-face (&optional force)
2399   "Display any Face headers in the header."
2400   (interactive (list 'force))
2401   (let ((wash-face-p buffer-read-only))
2402     (gnus-with-article-headers
2403       ;; When displaying parts, this function can be called several times on
2404       ;; the same article, without any intended toggle semantic (as typing `W
2405       ;; D d' would have). So face deletion must occur only when we come from
2406       ;; an interactive command, that is when the *Article* buffer is
2407       ;; read-only.
2408       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2409           (gnus-delete-images 'face)
2410         (let ((from (message-fetch-field "from"))
2411               face faces)
2412           (save-current-buffer
2413             (when (and wash-face-p
2414                        (gnus-buffer-live-p gnus-original-article-buffer)
2415                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2416               (set-buffer gnus-original-article-buffer))
2417             (save-restriction
2418               (mail-narrow-to-head)
2419               (when (or force
2420                         ;; Check whether this face is censored.
2421                         (not (and gnus-article-x-face-too-ugly
2422                                   (or from
2423                                       (setq from (message-fetch-field "from")))
2424                                   (string-match gnus-article-x-face-too-ugly
2425                                                 from))))
2426                 (while (gnus-article-goto-header "Face")
2427                   (push (mail-header-field-value) faces)))))
2428           (when faces
2429             (goto-char (point-min))
2430             (let (png image)
2431               (unless (setq from (gnus-article-goto-header "from"))
2432                 (insert "From:")
2433                 (setq from (point))
2434                 (insert " [no `from' set]\n"))
2435               (while faces
2436                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2437                   (setq image
2438                         (apply 'gnus-create-image png 'png t
2439                                (cdr (assq 'png gnus-face-properties-alist))))
2440                   (goto-char from)
2441                   (when image
2442                     (gnus-add-wash-type 'face)
2443                     (gnus-add-image 'face image)
2444                     (gnus-put-image image nil 'face)))))))))))
2445
2446 (defun article-display-x-face (&optional force)
2447   "Look for an X-Face header and display it if present."
2448   (interactive (list 'force))
2449   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2450     (gnus-with-article-headers
2451       ;; Delete the old process, if any.
2452       (when (process-status "article-x-face")
2453         (delete-process "article-x-face"))
2454       ;; See the comment in `article-display-face'.
2455       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2456           ;; We have already displayed X-Faces, so we remove them
2457           ;; instead.
2458           (gnus-delete-images 'xface)
2459         ;; Display X-Faces.
2460         (let ((from (message-fetch-field "from"))
2461               x-faces face)
2462           (save-current-buffer
2463             (when (and wash-face-p
2464                        (gnus-buffer-live-p gnus-original-article-buffer)
2465                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2466               ;; If type `W f', use gnus-original-article-buffer,
2467               ;; otherwise use the current buffer because displaying
2468               ;; RFC822 parts calls this function too.
2469               (set-buffer gnus-original-article-buffer))
2470             (save-restriction
2471               (mail-narrow-to-head)
2472               (and gnus-article-x-face-command
2473                    (or force
2474                        ;; Check whether this face is censored.
2475                        (not (and gnus-article-x-face-too-ugly
2476                                  (or from
2477                                      (setq from (message-fetch-field "from")))
2478                                  (string-match gnus-article-x-face-too-ugly
2479                                                from))))
2480                    (while (gnus-article-goto-header "X-Face")
2481                      (push (mail-header-field-value) x-faces)))))
2482           (when x-faces
2483             ;; We display the face.
2484             (cond ((functionp gnus-article-x-face-command)
2485                    ;; The command is a lisp function, so we call it.
2486                    (mapc gnus-article-x-face-command x-faces))
2487                   ((stringp gnus-article-x-face-command)
2488                    ;; The command is a string, so we interpret the command
2489                    ;; as a, well, command, and fork it off.
2490                    (let ((process-connection-type nil))
2491                      (gnus-set-process-query-on-exit-flag
2492                       (start-process
2493                        "article-x-face" nil shell-file-name
2494                        shell-command-switch gnus-article-x-face-command)
2495                       nil)
2496                      ;; Sending multiple EOFs to xv doesn't work,
2497                      ;; so we only do a single external face.
2498                      (with-temp-buffer
2499                        (insert (car x-faces))
2500                        (process-send-region "article-x-face"
2501                                             (point-min) (point-max)))
2502                      (process-send-eof "article-x-face")))
2503                   (t
2504                    (error "`%s' set to `%s' is not a function"
2505                           gnus-article-x-face-command
2506                           'gnus-article-x-face-command)))))))))
2507
2508 (defun article-decode-mime-words ()
2509   "Decode all MIME-encoded words in the article."
2510   (interactive)
2511   (gnus-with-article-buffer
2512     (let ((inhibit-point-motion-hooks t)
2513           (mail-parse-charset gnus-newsgroup-charset)
2514           (mail-parse-ignored-charsets
2515            (with-current-buffer gnus-summary-buffer
2516              gnus-newsgroup-ignored-charsets)))
2517       (mail-decode-encoded-word-region (point-min) (point-max)))))
2518
2519 (defun article-decode-charset (&optional prompt)
2520   "Decode charset-encoded text in the article.
2521 If PROMPT (the prefix), prompt for a coding system to use."
2522   (interactive "P")
2523   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2524         (inhibit-read-only t)
2525         (mail-parse-charset gnus-newsgroup-charset)
2526         (mail-parse-ignored-charsets
2527          (save-excursion (condition-case nil
2528                              (set-buffer gnus-summary-buffer)
2529                            (error))
2530                          gnus-newsgroup-ignored-charsets))
2531         ct cte ctl charset format)
2532     (save-excursion
2533       (save-restriction
2534         (article-narrow-to-head)
2535         (setq ct (message-fetch-field "Content-Type" t)
2536               cte (message-fetch-field "Content-Transfer-Encoding" t)
2537               ctl (and ct (mail-header-parse-content-type ct))
2538               charset (cond
2539                        (prompt
2540                         (mm-read-coding-system "Charset to decode: "))
2541                        (ctl
2542                         (mail-content-type-get ctl 'charset)))
2543               format (and ctl (mail-content-type-get ctl 'format)))
2544         (when cte
2545           (setq cte (mail-header-strip cte)))
2546         (if (and ctl (not (string-match "/" (car ctl))))
2547             (setq ctl nil))
2548         (goto-char (point-max)))
2549       (forward-line 1)
2550       (save-restriction
2551         (narrow-to-region (point) (point-max))
2552         (when (and (eq mail-parse-charset 'gnus-decoded)
2553                    (eq (mm-body-7-or-8) '8bit))
2554           ;; The text code could have been decoded.
2555           (setq charset mail-parse-charset))
2556         (when (and (or (not ctl)
2557                        (equal (car ctl) "text/plain"))
2558                    (not format)) ;; article with format will decode later.
2559           (mm-decode-body
2560            charset (and cte (intern (downcase
2561                                      (gnus-strip-whitespace cte))))
2562            (car ctl)))))))
2563
2564 (defun article-decode-encoded-words ()
2565   "Remove encoded-word encoding from headers."
2566   (let ((inhibit-point-motion-hooks t)
2567         (mail-parse-charset gnus-newsgroup-charset)
2568         (mail-parse-ignored-charsets
2569          (save-excursion (condition-case nil
2570                              (set-buffer gnus-summary-buffer)
2571                            (error))
2572                          gnus-newsgroup-ignored-charsets))
2573         (inhibit-read-only t)
2574         end start)
2575     (goto-char (point-min))
2576     (when (search-forward "\n\n" nil 'move)
2577       (forward-line -1))
2578     (setq end (point))
2579     (while (not (bobp))
2580       (while (progn
2581                (forward-line -1)
2582                (and (not (bobp))
2583                     (memq (char-after) '(?\t ? )))))
2584       (setq start (point))
2585       (if (looking-at "\
2586 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2587 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2588           (funcall gnus-decode-address-function start end)
2589         (funcall gnus-decode-header-function start end))
2590       (goto-char (setq end start)))))
2591
2592 (defun article-decode-group-name ()
2593   "Decode group names in Newsgroups, Followup-To and Xref headers."
2594   (let ((inhibit-point-motion-hooks t)
2595         (inhibit-read-only t)
2596         (method (gnus-find-method-for-group gnus-newsgroup-name))
2597         regexp)
2598     (when (and (or gnus-group-name-charset-method-alist
2599                    gnus-group-name-charset-group-alist)
2600                (gnus-buffer-live-p gnus-original-article-buffer))
2601       (save-restriction
2602         (article-narrow-to-head)
2603         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2604           (with-current-buffer gnus-original-article-buffer
2605             (goto-char (point-min)))
2606           (setq regexp (concat "^" header
2607                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2608           (while (re-search-forward regexp nil t)
2609             (replace-match (save-match-data
2610                              (gnus-decode-newsgroups
2611                               ;; XXX how to use data in article buffer?
2612                               (with-current-buffer gnus-original-article-buffer
2613                                 (re-search-forward regexp nil t)
2614                                 (match-string 1))
2615                               gnus-newsgroup-name method))
2616                            t t nil 1))
2617           (goto-char (point-min)))))))
2618
2619 (autoload 'idna-to-unicode "idna")
2620
2621 (defun article-decode-idna-rhs ()
2622   "Decode IDNA strings in RHS in various headers in current buffer.
2623 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2624 Mail-Reply-To: and Mail-Followup-To:."
2625   (when gnus-use-idna
2626     (save-restriction
2627       (let ((inhibit-point-motion-hooks t)
2628             (inhibit-read-only t))
2629         (article-narrow-to-head)
2630         (goto-char (point-min))
2631         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2632           (let (ace unicode)
2633             (when (save-match-data
2634                     (and (setq ace (match-string 1))
2635                          (save-excursion
2636                            (and (re-search-backward "^[^ \t]" nil t)
2637                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2638                          (setq unicode (idna-to-unicode ace))))
2639               (unless (string= ace unicode)
2640                 (replace-match unicode nil nil nil 1)))))))))
2641
2642 (defun article-de-quoted-unreadable (&optional force read-charset)
2643   "Translate a quoted-printable-encoded article.
2644 If FORCE, decode the article whether it is marked as quoted-printable
2645 or not.
2646 If READ-CHARSET, ask for a coding system."
2647   (interactive (list 'force current-prefix-arg))
2648   (save-excursion
2649     (let ((inhibit-read-only t) type charset)
2650       (if (gnus-buffer-live-p gnus-original-article-buffer)
2651           (with-current-buffer gnus-original-article-buffer
2652             (setq type
2653                   (gnus-fetch-field "content-transfer-encoding"))
2654             (let* ((ct (gnus-fetch-field "content-type"))
2655                    (ctl (and ct (mail-header-parse-content-type ct))))
2656               (setq charset (and ctl
2657                                  (mail-content-type-get ctl 'charset)))
2658               (if (stringp charset)
2659                   (setq charset (intern (downcase charset)))))))
2660       (if read-charset
2661           (setq charset (mm-read-coding-system "Charset: " charset)))
2662       (unless charset
2663         (setq charset gnus-newsgroup-charset))
2664       (when (or force
2665                 (and type (let ((case-fold-search t))
2666                             (string-match "quoted-printable" type))))
2667         (article-goto-body)
2668         (quoted-printable-decode-region
2669          (point) (point-max) (mm-charset-to-coding-system charset))))))
2670
2671 (defun article-de-base64-unreadable (&optional force read-charset)
2672   "Translate a base64 article.
2673 If FORCE, decode the article whether it is marked as base64 not.
2674 If READ-CHARSET, ask for a coding system."
2675   (interactive (list 'force current-prefix-arg))
2676   (save-excursion
2677     (let ((inhibit-read-only t) type charset)
2678       (if (gnus-buffer-live-p gnus-original-article-buffer)
2679           (with-current-buffer gnus-original-article-buffer
2680             (setq type
2681                   (gnus-fetch-field "content-transfer-encoding"))
2682             (let* ((ct (gnus-fetch-field "content-type"))
2683                    (ctl (and ct (mail-header-parse-content-type ct))))
2684               (setq charset (and ctl
2685                                  (mail-content-type-get ctl 'charset)))
2686               (if (stringp charset)
2687                   (setq charset (intern (downcase charset)))))))
2688       (if read-charset
2689           (setq charset (mm-read-coding-system "Charset: " charset)))
2690       (unless charset
2691         (setq charset gnus-newsgroup-charset))
2692       (when (or force
2693                 (and type (let ((case-fold-search t))
2694                             (string-match "base64" type))))
2695         (article-goto-body)
2696         (save-restriction
2697           (narrow-to-region (point) (point-max))
2698           (base64-decode-region (point-min) (point-max))
2699           (mm-decode-coding-region
2700            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2701
2702 (eval-when-compile
2703   (require 'rfc1843))
2704
2705 (defun article-decode-HZ ()
2706   "Translate a HZ-encoded article."
2707   (interactive)
2708   (require 'rfc1843)
2709   (save-excursion
2710     (let ((inhibit-read-only t))
2711       (rfc1843-decode-region (point-min) (point-max)))))
2712
2713 (defun article-unsplit-urls ()
2714   "Remove the newlines that some other mailers insert into URLs."
2715   (interactive)
2716   (save-excursion
2717     (let ((inhibit-read-only t))
2718       (goto-char (point-min))
2719       (while (re-search-forward
2720               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2721         (replace-match "\\1\\3" t)))
2722     (when (gmm-called-interactively-p 'any)
2723       (gnus-treat-article nil))))
2724
2725 (defun article-wash-html ()
2726   "Format an HTML article."
2727   (interactive)
2728   (let ((handles nil)
2729         (buffer-read-only nil))
2730     (when (gnus-buffer-live-p gnus-original-article-buffer)
2731       (with-current-buffer gnus-original-article-buffer
2732         (setq handles (mm-dissect-buffer t t))))
2733     (article-goto-body)
2734     (delete-region (point) (point-max))
2735     (mm-enable-multibyte)
2736     (mm-inline-text-html handles)))
2737
2738 (defvar gnus-article-browse-html-temp-list nil
2739   "List of temporary files created by `gnus-article-browse-html-parts'.
2740 Internal variable.")
2741
2742 (defcustom gnus-article-browse-delete-temp 'ask
2743   "What to do with temporary files from `gnus-article-browse-html-parts'.
2744 If nil, don't delete temporary files.  If it is t, delete them on
2745 exit from the summary buffer.  If it is the symbol `file', query
2746 on each file, if it is `ask' ask once when exiting from the
2747 summary buffer."
2748   :group 'gnus-article
2749   :version "23.1" ;; No Gnus
2750   :type '(choice (const :tag "Don't delete" nil)
2751                  (const :tag "Don't ask" t)
2752                  (const :tag "Ask" ask)
2753                  (const :tag "Ask for each file" file)))
2754
2755 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2756
2757 (defun gnus-article-browse-delete-temp-files (&optional how)
2758   "Delete temp-files created by `gnus-article-browse-html-parts'."
2759   (when (and gnus-article-browse-html-temp-list
2760              (progn
2761                (or how (setq how gnus-article-browse-delete-temp))
2762                (if (eq how 'ask)
2763                    (let ((files (length gnus-article-browse-html-temp-list)))
2764                      (or (gnus-y-or-n-p
2765                           (if (= files 1)
2766                               "Delete the temporary HTML file? "
2767                             (format "Delete all %s temporary HTML files? "
2768                                     files)))
2769                          (setq gnus-article-browse-html-temp-list nil)))
2770                  how)))
2771     (dolist (file gnus-article-browse-html-temp-list)
2772       (cond ((file-directory-p file)
2773              (when (or (not (eq how 'file))
2774                        (gnus-y-or-n-p
2775                         (format
2776                          "Delete temporary HTML file(s) in directory `%s'? "
2777                          (file-name-as-directory file))))
2778                (gnus-delete-directory file)))
2779             ((file-exists-p file)
2780              (when (or (not (eq how 'file))
2781                        (gnus-y-or-n-p
2782                         (format "Delete temporary HTML file `%s'? " file)))
2783                (delete-file file)))))
2784     ;; Also remove file from the list when not deleted or if file doesn't
2785     ;; exist anymore.
2786     (setq gnus-article-browse-html-temp-list nil))
2787   gnus-article-browse-html-temp-list)
2788
2789 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2790   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2791 Return file name."
2792   (save-match-data
2793     (let (file type)
2794       (catch 'found
2795         (dolist (handle handles)
2796           (cond
2797            ((not (listp handle)))
2798            ((equal (mm-handle-media-supertype handle) "multipart")
2799             (when (setq file (gnus-article-browse-html-save-cid-content
2800                               cid handle directory))
2801               (throw 'found file)))
2802            ((equal (concat "<" cid ">") (mm-handle-id handle))
2803             (setq file
2804                   (expand-file-name
2805                    (or (mm-handle-filename handle)
2806                        (concat
2807                         (make-temp-name "cid")
2808                         (car (rassoc (car (mm-handle-type handle)) mailcap-mime-extensions))))
2809                    directory))
2810             (mm-save-part-to-file handle file)
2811             (throw 'found file))))))))
2812
2813 (defun gnus-article-browse-html-parts (list &optional header)
2814   "View all \"text/html\" parts from LIST.
2815 Recurse into multiparts.  The optional HEADER that should be a decoded
2816 message header will be added to the bodies of the \"text/html\" parts."
2817   ;; Internal function used by `gnus-article-browse-html-article'.
2818   (let (type file charset content cid-dir tmp-file showed)
2819     ;; Find and show the html-parts.
2820     (dolist (handle list)
2821       ;; If HTML, show it:
2822       (cond ((not (listp handle)))
2823             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2824                  (and (equal (car type) "message/external-body")
2825                       (or header
2826                           (setq file (mm-handle-filename handle)))
2827                       (or (mm-handle-cache handle)
2828                           (condition-case code
2829                               (progn (mm-extern-cache-contents handle) t)
2830                             (error
2831                              (gnus-message 3 "%s" (error-message-string code))
2832                              (when (>= gnus-verbose 3) (sit-for 2))
2833                              nil)))
2834                       (progn
2835                         (setq handle (mm-handle-cache handle)
2836                               type (mm-handle-type handle))
2837                         (equal (car type) "text/html"))))
2838              (setq charset (mail-content-type-get type 'charset)
2839                    content (mm-get-part handle))
2840              (with-temp-buffer
2841                (if (eq charset 'gnus-decoded)
2842                    (mm-enable-multibyte)
2843                  (mm-disable-multibyte))
2844                (insert content)
2845                ;; resolve cid contents
2846                (let ((case-fold-search t)
2847                      cid-file)
2848                  (goto-char (point-min))
2849                  (while (re-search-forward "\
2850 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2851                                            nil t)
2852                    (unless cid-dir
2853                      (setq cid-dir (mm-make-temp-file "cid" t))
2854                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2855                    (setq file nil
2856                          content nil)
2857                    (when (setq cid-file
2858                                (gnus-article-browse-html-save-cid-content
2859                                 (match-string 2)
2860                                 (with-current-buffer gnus-article-buffer
2861                                   gnus-article-mime-handles)
2862                                 cid-dir))
2863                      (when (eq system-type 'cygwin)
2864                        (setq cid-file
2865                              (concat "/" (substring
2866                                           (with-output-to-string
2867                                             (call-process "cygpath" nil
2868                                                           standard-output
2869                                                           nil "-m" cid-file))
2870                                           0 -1))))
2871                      (replace-match (concat "file://" cid-file)
2872                                     nil nil nil 1))))
2873                (unless content (setq content (buffer-string))))
2874              (when (or charset header (not file))
2875                (setq tmp-file (mm-make-temp-file
2876                                ;; Do we need to care for 8.3 filenames?
2877                                "mm-" nil ".html")))
2878              ;; Add a meta html tag to specify charset and a header.
2879              (cond
2880               (header
2881                (let (title eheader body hcharset coding)
2882                  (with-temp-buffer
2883                    (mm-enable-multibyte)
2884                    (setq case-fold-search t)
2885                    (insert header "\n")
2886                    (setq title (message-fetch-field "subject"))
2887                    (goto-char (point-min))
2888                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|\\(&\\)\\|\n"
2889                                              nil t)
2890                      (replace-match (cond ((match-beginning 1) "&lt;")
2891                                           ((match-beginning 2) "&gt;")
2892                                           ((match-beginning 3) "&amp;")
2893                                           (t "<br>\n"))))
2894                    (goto-char (point-min))
2895                    (insert "<div align=\"left\">\n")
2896                    (goto-char (point-max))
2897                    (insert "</div>\n<hr>\n")
2898                    ;; We have to examine charset one by one since
2899                    ;; charset specified in parts might be different.
2900                    (if (eq charset 'gnus-decoded)
2901                        (setq charset 'utf-8
2902                              eheader (mm-encode-coding-string (buffer-string)
2903                                                               charset)
2904                              title (when title
2905                                      (mm-encode-coding-string title charset))
2906                              body (mm-encode-coding-string content charset))
2907                      (setq hcharset (mm-find-mime-charset-region (point-min)
2908                                                                  (point-max)))
2909                      (cond ((= (length hcharset) 1)
2910                             (setq hcharset (car hcharset)
2911                                   coding (mm-charset-to-coding-system
2912                                           hcharset)))
2913                            ((> (length hcharset) 1)
2914                             (setq hcharset 'utf-8
2915                                   coding hcharset)))
2916                      (if coding
2917                          (if charset
2918                              (progn
2919                                (setq body
2920                                      (mm-charset-to-coding-system charset))
2921                                (if (eq coding body)
2922                                    (setq eheader (mm-encode-coding-string
2923                                                   (buffer-string) coding)
2924                                          title (when title
2925                                                  (mm-encode-coding-string
2926                                                   title coding))
2927                                          body content)
2928                                  (setq charset 'utf-8
2929                                        eheader (mm-encode-coding-string
2930                                                 (buffer-string) charset)
2931                                        title (when title
2932                                                (mm-encode-coding-string
2933                                                 title charset))
2934                                        body (mm-encode-coding-string
2935                                              (mm-decode-coding-string
2936                                               content body)
2937                                              charset))))
2938                            (setq charset hcharset
2939                                  eheader (mm-encode-coding-string
2940                                           (buffer-string) coding)
2941                                  title (when title
2942                                          (mm-encode-coding-string
2943                                           title coding))
2944                                  body content))
2945                        (setq eheader (mm-string-as-unibyte (buffer-string))
2946                              body content)))
2947                    (erase-buffer)
2948                    (mm-disable-multibyte)
2949                    (insert body)
2950                    (when charset
2951                      (mm-add-meta-html-tag handle charset t))
2952                    (when title
2953                      (goto-char (point-min))
2954                      (unless (search-forward "<title>" nil t)
2955                        (re-search-forward "<head>\\s-*" nil t)
2956                        (insert "<title>" title "</title>\n")))
2957                    (goto-char (point-min))
2958                    (or (re-search-forward
2959                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2960                        (re-search-forward
2961                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2962                    (insert eheader)
2963                    (mm-write-region (point-min) (point-max)
2964                                     tmp-file nil nil nil 'binary t))))
2965               (charset
2966                (mm-with-unibyte-buffer
2967                  (insert (if (eq charset 'gnus-decoded)
2968                              (mm-encode-coding-string content
2969                                                       (setq charset 'utf-8))
2970                            content))
2971                  (if (or (mm-add-meta-html-tag handle charset)
2972                          (not file))
2973                      (mm-write-region (point-min) (point-max)
2974                                       tmp-file nil nil nil 'binary t)
2975                    (setq tmp-file nil))))
2976               (tmp-file
2977                (mm-save-part-to-file handle tmp-file)))
2978              (when tmp-file
2979                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2980              (add-hook 'gnus-summary-prepare-exit-hook
2981                        'gnus-article-browse-delete-temp-files)
2982              (add-hook 'gnus-exit-gnus-hook
2983                        (lambda  ()
2984                          (gnus-article-browse-delete-temp-files t)))
2985              ;; FIXME: Warn if there's an <img> tag?
2986              (browse-url-of-file (or tmp-file (expand-file-name file)))
2987              (setq showed t))
2988             ;; If multipart, recurse
2989             ((equal (mm-handle-media-supertype handle) "multipart")
2990              (when (gnus-article-browse-html-parts handle header)
2991                (setq showed t)))
2992             ((equal (mm-handle-media-type handle) "message/rfc822")
2993              (mm-with-multibyte-buffer
2994                (mm-insert-part handle)
2995                (setq handle (mm-dissect-buffer t t))
2996                (when (and (bufferp (car handle))
2997                           (stringp (car (mm-handle-type handle))))
2998                  (setq handle (list handle)))
2999                (when header
3000                  (article-decode-encoded-words)
3001                  (let ((gnus-visible-headers
3002                         (or (get 'gnus-visible-headers 'standard-value)
3003                             gnus-visible-headers)))
3004                    (article-hide-headers))
3005                  (goto-char (point-min))
3006                  (search-forward "\n\n" nil 'move)
3007                  (skip-chars-backward "\t\n ")
3008                  (setq header (buffer-substring (point-min) (point)))))
3009              (when (prog1
3010                        (gnus-article-browse-html-parts handle header)
3011                      (mm-destroy-parts handle))
3012                (setq showed t)))))
3013     showed))
3014
3015 (defun gnus-article-browse-html-article (&optional arg)
3016   "View \"text/html\" parts of the current article with a WWW browser.
3017 Inline images embedded in a message using the cid scheme, as they are
3018 generally considered to be safe, will be processed properly.
3019 The message header is added to the beginning of every html part unless
3020 the prefix argument ARG is given.
3021
3022 Warning: Spammers use links to images (using the http scheme) in HTML
3023 articles to verify whether you have read the message.  As
3024 `gnus-article-browse-html-article' passes the HTML content to the
3025 browser without eliminating these \"web bugs\" you should only
3026 use it for mails from trusted senders.
3027
3028 If you always want to display HTML parts in the browser, set
3029 `mm-text-html-renderer' to nil.
3030
3031 This command creates temporary files to pass HTML contents including
3032 images if any to the browser, and deletes them when exiting the group
3033 \(if you want)."
3034   ;; Cf. `mm-w3m-safe-url-regexp'
3035   (interactive "P")
3036   (if arg
3037       (gnus-summary-show-article)
3038     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3039                                     gnus-visible-headers))
3040           ;; As we insert a <hr>, there's no need for the body boundary.
3041           (gnus-treat-body-boundary nil))
3042       (gnus-summary-show-article)))
3043   (with-current-buffer gnus-article-buffer
3044     (let ((header (unless arg
3045                     (save-restriction
3046                       (widen)
3047                       (buffer-substring-no-properties
3048                        (goto-char (point-min))
3049                        (if (search-forward "\n\n" nil t)
3050                            (match-beginning 0)
3051                          (goto-char (point-max))
3052                          (skip-chars-backward "\t\n ")
3053                          (point))))))
3054           parts)
3055       (set-buffer gnus-original-article-buffer)
3056       (setq parts (mm-dissect-buffer t t))
3057       ;; If singlepart, enforce a list.
3058       (when (and (bufferp (car parts))
3059                  (stringp (car (mm-handle-type parts))))
3060         (setq parts (list parts)))
3061       ;; Process the list
3062       (unless (gnus-article-browse-html-parts parts header)
3063         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3064       (mm-destroy-parts parts)
3065       (unless arg
3066         (gnus-summary-show-article)))))
3067
3068 (defun article-hide-list-identifiers ()
3069   "Remove list identifies from the Subject header.
3070 The `gnus-list-identifiers' variable specifies what to do."
3071   (interactive)
3072   (let ((inhibit-point-motion-hooks t)
3073         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3074         (inhibit-read-only t))
3075     (when regexp
3076       (save-excursion
3077         (save-restriction
3078           (article-narrow-to-head)
3079           (goto-char (point-min))
3080           (while (re-search-forward
3081                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3082                   nil t)
3083             (delete-region (match-beginning 2) (match-end 0))
3084             (beginning-of-line))
3085           (when (re-search-forward
3086                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3087             (delete-region (match-beginning 1) (match-end 1))))))))
3088
3089 (defun article-hide-pem (&optional arg)
3090   "Toggle hiding of any PEM headers and signatures in the current article.
3091 If given a negative prefix, always show; if given a positive prefix,
3092 always hide."
3093   (interactive (gnus-article-hidden-arg))
3094   (unless (gnus-article-check-hidden-text 'pem arg)
3095     (save-excursion
3096       (let ((inhibit-read-only t) end)
3097         (goto-char (point-min))
3098         ;; Hide the horrendously ugly "header".
3099         (when (and (search-forward
3100                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3101                     nil t)
3102                    (setq end (1+ (match-beginning 0))))
3103           (gnus-add-wash-type 'pem)
3104           (gnus-article-hide-text-type
3105            end
3106            (if (search-forward "\n\n" nil t)
3107                (match-end 0)
3108              (point-max))
3109            'pem)
3110           ;; Hide the trailer as well
3111           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3112                                 nil t)
3113             (gnus-article-hide-text-type
3114              (match-beginning 0) (match-end 0) 'pem)))))))
3115
3116 (defun article-strip-banner ()
3117   "Strip the banners specified by the `banner' group parameter and by
3118 `gnus-article-address-banner-alist'."
3119   (interactive)
3120   (save-excursion
3121     (save-restriction
3122       (let ((inhibit-point-motion-hooks t))
3123         (when (gnus-parameter-banner gnus-newsgroup-name)
3124           (article-really-strip-banner
3125            (gnus-parameter-banner gnus-newsgroup-name)))
3126         (when gnus-article-address-banner-alist
3127           ;; Note that the From header is decoded here, so it is
3128           ;; required that the *-extract-address-components function
3129           ;; supports non-ASCII text.
3130           (let ((from (save-restriction
3131                         (widen)
3132                         (article-narrow-to-head)
3133                         (mail-fetch-field "from"))))
3134             (when (and from
3135                        (setq from
3136                              (cadr (funcall gnus-extract-address-components
3137                                             from))))
3138               (catch 'found
3139                 (dolist (pair gnus-article-address-banner-alist)
3140                   (when (string-match (car pair) from)
3141                     (throw 'found
3142                            (article-really-strip-banner (cdr pair)))))))))))))
3143
3144 (defun article-really-strip-banner (banner)
3145   "Strip the banner specified by the argument."
3146   (save-excursion
3147     (save-restriction
3148       (let ((inhibit-point-motion-hooks t)
3149             (gnus-signature-limit nil)
3150             (inhibit-read-only t))
3151         (article-goto-body)
3152         (cond
3153          ((eq banner 'signature)
3154           (when (gnus-article-narrow-to-signature)
3155             (widen)
3156             (forward-line -1)
3157             (delete-region (point) (point-max))))
3158          ((symbolp banner)
3159           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3160               (while (re-search-forward banner nil t)
3161                 (delete-region (match-beginning 0) (match-end 0)))))
3162          ((stringp banner)
3163           (while (re-search-forward banner nil t)
3164             (delete-region (match-beginning 0) (match-end 0)))))))))
3165
3166 (defun article-babel ()
3167   "Translate article using an online translation service."
3168   (interactive)
3169   (require 'babel)
3170   (gnus-with-article-buffer
3171     (when (article-goto-body)
3172       (let* ((start (point))
3173              (end (point-max))
3174              (orig (buffer-substring start end))
3175              (trans (babel-as-string orig)))
3176         (save-restriction
3177           (narrow-to-region start end)
3178           (delete-region start end)
3179           (insert trans))))))
3180
3181 (defun article-hide-signature (&optional arg)
3182   "Hide the signature in the current article.
3183 If given a negative prefix, always show; if given a positive prefix,
3184 always hide."
3185   (interactive (gnus-article-hidden-arg))
3186   (unless (gnus-article-check-hidden-text 'signature arg)
3187     (save-excursion
3188       (save-restriction
3189         (let ((inhibit-read-only t))
3190           (when (gnus-article-narrow-to-signature)
3191             (gnus-article-hide-text-type
3192              (point-min) (point-max) 'signature))))))
3193   (gnus-set-mode-line 'article))
3194
3195 (defun article-strip-headers-in-body ()
3196   "Strip offensive headers from bodies."
3197   (interactive)
3198   (save-excursion
3199     (article-goto-body)
3200     (let ((case-fold-search t))
3201       (when (looking-at "x-no-archive:")
3202         (gnus-delete-line)))))
3203
3204 (defun article-strip-leading-blank-lines ()
3205   "Remove all blank lines from the beginning of the article."
3206   (interactive)
3207   (save-excursion
3208     (let ((inhibit-point-motion-hooks t)
3209           (inhibit-read-only t))
3210       (when (article-goto-body)
3211         (while (and (not (eobp))
3212                     (looking-at "[ \t]*$"))
3213           (gnus-delete-line))))))
3214
3215 (defun article-narrow-to-head ()
3216   "Narrow the buffer to the head of the message.
3217 Point is left at the beginning of the narrowed-to region."
3218   (narrow-to-region
3219    (goto-char (point-min))
3220    (cond
3221     ;; Absolutely no headers displayed.
3222     ((looking-at "\n")
3223      (point))
3224     ;; Normal headers.
3225     ((search-forward "\n\n" nil 1)
3226      (1- (point)))
3227     ;; Nothing but headers.
3228     (t
3229      (point-max))))
3230   (goto-char (point-min)))
3231
3232 (defun article-goto-body ()
3233   "Place point at the start of the body."
3234   (goto-char (point-min))
3235   (cond
3236    ;; This variable is only bound when dealing with separate
3237    ;; MIME body parts.
3238    (article-goto-body-goes-to-point-min-p
3239     t)
3240    ((search-forward "\n\n" nil t)
3241     t)
3242    (t
3243     (goto-char (point-max))
3244     nil)))
3245
3246 (defun article-strip-multiple-blank-lines ()
3247   "Replace consecutive blank lines with one empty line."
3248   (interactive)
3249   (save-excursion
3250     (let ((inhibit-point-motion-hooks t)
3251           (inhibit-read-only t))
3252       ;; First make all blank lines empty.
3253       (article-goto-body)
3254       (while (re-search-forward "^[ \t]+$" nil t)
3255         (unless (gnus-annotation-in-region-p
3256                  (match-beginning 0) (match-end 0))
3257           (replace-match "" nil t)))
3258       ;; Then replace multiple empty lines with a single empty line.
3259       (article-goto-body)
3260       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3261         (unless (gnus-annotation-in-region-p
3262                  (match-beginning 0) (match-end 0))
3263           (delete-region (match-beginning 1) (match-end 1)))))))
3264
3265 (defun article-strip-leading-space ()
3266   "Remove all white space from the beginning of the lines in the article."
3267   (interactive)
3268   (save-excursion
3269     (let ((inhibit-point-motion-hooks t)
3270           (inhibit-read-only t))
3271       (article-goto-body)
3272       (while (re-search-forward "^[ \t]+" nil t)
3273         (replace-match "" t t)))))
3274
3275 (defun article-strip-trailing-space ()
3276   "Remove all white space from the end 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-blank-lines ()
3286   "Strip leading, trailing and multiple blank lines."
3287   (interactive)
3288   (article-strip-leading-blank-lines)
3289   (article-remove-trailing-blank-lines)
3290   (article-strip-multiple-blank-lines))
3291
3292 (defun article-strip-all-blank-lines ()
3293   "Strip all blank lines."
3294   (interactive)
3295   (save-excursion
3296     (let ((inhibit-point-motion-hooks t)
3297           (inhibit-read-only t))
3298       (article-goto-body)
3299       (while (re-search-forward "^[ \t]*\n" nil t)
3300         (replace-match "" t t)))))
3301
3302 (defun gnus-article-narrow-to-signature ()
3303   "Narrow to the signature; return t if a signature is found, else nil."
3304   (let ((inhibit-point-motion-hooks t))
3305     (when (gnus-article-search-signature)
3306       (forward-line 1)
3307       ;; Check whether we have some limits to what we consider
3308       ;; to be a signature.
3309       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3310                       (list gnus-signature-limit)))
3311             limit limited)
3312         (while (setq limit (pop limits))
3313           (if (or (and (integerp limit)
3314                        (< (- (point-max) (point)) limit))
3315                   (and (floatp limit)
3316                        (< (count-lines (point) (point-max)) limit))
3317                   (and (functionp limit)
3318                        (funcall limit))
3319                   (and (stringp limit)
3320                        (not (re-search-forward limit nil t))))
3321               ()                        ; This limit did not succeed.
3322             (setq limited t
3323                   limits nil)))
3324         (unless limited
3325           (narrow-to-region (point) (point-max))
3326           t)))))
3327
3328 (defun gnus-article-search-signature ()
3329   "Search the current buffer for the signature separator.
3330 Put point at the beginning of the signature separator."
3331   (let ((cur (point)))
3332     (goto-char (point-max))
3333     (if (if (stringp gnus-signature-separator)
3334             (re-search-backward gnus-signature-separator nil t)
3335           (let ((seps gnus-signature-separator))
3336             (while (and seps
3337                         (not (re-search-backward (car seps) nil t)))
3338               (pop seps))
3339             seps))
3340         t
3341       (goto-char cur)
3342       nil)))
3343
3344 (defun gnus-article-hidden-arg ()
3345   "Return the current prefix arg as a number, or 0 if no prefix."
3346   (list (if current-prefix-arg
3347             (prefix-numeric-value current-prefix-arg)
3348           0)))
3349
3350 (defun gnus-article-check-hidden-text (type arg)
3351   "Return nil if hiding is necessary.
3352 Arg can be nil or a number.  nil and positive means hide, negative
3353 means show, 0 means toggle."
3354   (save-excursion
3355     (save-restriction
3356       (let ((hide (gnus-article-hidden-text-p type)))
3357         (cond
3358          ((or (null arg)
3359               (> arg 0))
3360           nil)
3361          ((< arg 0)
3362           (gnus-article-show-hidden-text type)
3363           t)
3364          (t
3365           (if (eq hide 'hidden)
3366               (progn
3367                 (gnus-article-show-hidden-text type)
3368                 t)
3369             nil)))))))
3370
3371 (defun gnus-article-hidden-text-p (type)
3372   "Say whether the current buffer contains hidden text of type TYPE."
3373   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3374     (while (and pos
3375                 (not (get-text-property pos 'invisible))
3376                 (not (get-text-property pos 'dummy-invisible)))
3377       (setq pos
3378             (text-property-any (1+ pos) (point-max) 'article-type type)))
3379     (if pos
3380         'hidden
3381       nil)))
3382
3383 (defun gnus-article-show-hidden-text (type &optional dummy)
3384   "Show all hidden text of type TYPE.
3385 Originally it is hide instead of DUMMY."
3386   (let ((inhibit-read-only t)
3387         (inhibit-point-motion-hooks t))
3388     (gnus-remove-text-properties-when
3389      'article-type type
3390      (point-min) (point-max)
3391      (cons 'article-type (cons type
3392                                gnus-hidden-properties)))
3393     (gnus-delete-wash-type type)))
3394
3395 (defconst article-time-units
3396   `((year . ,(* 365.25 24 60 60))
3397     (week . ,(* 7 24 60 60))
3398     (day . ,(* 24 60 60))
3399     (hour . ,(* 60 60))
3400     (minute . 60)
3401     (second . 1))
3402   "Mapping from time units to seconds.")
3403
3404 (defun gnus-article-forward-header ()
3405   "Move point to the start of the next header.
3406 If the current header is a continuation header, this can be several
3407 lines forward."
3408   (let ((ended nil))
3409     (while (not ended)
3410       (forward-line 1)
3411       (if (looking-at "[ \t]+[^ \t]")
3412           (forward-line 1)
3413         (setq ended t)))))
3414
3415 (defun article-treat-date ()
3416   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3417                        (with-current-buffer gnus-summary-buffer
3418                          gnus-article-date-headers)
3419                      gnus-article-date-headers)
3420                    t))
3421
3422 (defun article-date-ut (&optional type highlight date-position)
3423   "Convert DATE date to TYPE in the current article.
3424 The default type is `ut'.  See `gnus-article-date-headers' for
3425 possible values."
3426   (interactive (list 'ut t))
3427   (let* ((case-fold-search t)
3428          (inhibit-read-only t)
3429          (inhibit-point-motion-hooks t)
3430          (first t)
3431          (visible-date (mail-fetch-field "Date"))
3432          pos date bface eface)
3433     (save-excursion
3434       (goto-char (point-min))
3435       (when (re-search-forward "^Date:" nil t)
3436         (setq bface (get-text-property (point-at-bol) 'face)
3437               eface (get-text-property (1- (point-at-eol)) 'face)))
3438       ;; Delete any old Date headers.
3439       (if date-position
3440           (progn
3441             (goto-char date-position)
3442             (setq date (get-text-property (point) 'original-date))
3443             (delete-region (point)
3444                            (progn
3445                              (gnus-article-forward-header)
3446                              (point)))
3447             (article-transform-date date type bface eface))
3448         (save-restriction
3449           (widen)
3450           (goto-char (point-min))
3451           (while (or (get-text-property (setq pos (point)) 'original-date)
3452                      (and (setq pos (next-single-property-change
3453                                      (point) 'original-date))
3454                           (goto-char pos)))
3455             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3456                                       (1+ (match-beginning 0))
3457                                     (point-max)))
3458             (goto-char (point-min))
3459             (while (re-search-forward "^Date:" nil t)
3460               (setq date (get-text-property (match-beginning 0) 'original-date))
3461               (delete-region (point-at-bol) (progn
3462                                               (gnus-article-forward-header)
3463                                               (point))))
3464             (when (and (not date)
3465                        visible-date)
3466               (setq date visible-date))
3467             (when date
3468               (article-transform-date date type bface eface))
3469             (goto-char (point-max))
3470             (widen)))))))
3471
3472 (defun article-transform-date (date type bface eface)
3473   (dolist (this-type (cond
3474                       ((null type)
3475                        (list 'ut))
3476                       ((atom type)
3477                        (list type))
3478                       (t
3479                        type)))
3480     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3481     (forward-line -1)
3482     (beginning-of-line)
3483     (put-text-property (point) (1+ (point))
3484                        'original-date date)
3485     (put-text-property (point) (1+ (point))
3486                        'gnus-date-type this-type)
3487     ;; Do highlighting.
3488     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3489       (put-text-property (match-beginning 1) (1+ (match-end 1))
3490                          'face bface)
3491       (put-text-property (match-beginning 2) (match-end 2)
3492                          'face eface))
3493     (forward-line 1)))
3494
3495 (defun article-make-date-line (date type)
3496   "Return a DATE line of TYPE."
3497   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3498                              combined-lapsed))
3499     (error "Unknown conversion type: %s" type))
3500   (condition-case ()
3501       (let ((time (ignore-errors (date-to-time date))))
3502         (cond
3503          ;; Convert to the local timezone.
3504          ((eq type 'local)
3505           (concat "Date: " (message-make-date time)))
3506          ;; Convert to Universal Time.
3507          ((eq type 'ut)
3508           (concat "Date: "
3509                   (substring
3510                    (message-make-date
3511                     (let* ((e (parse-time-string date))
3512                            (tm (apply 'encode-time e))
3513                            (ms (car tm))
3514                            (ls (- (cadr tm) (car (current-time-zone time)))))
3515                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3516                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3517                             (t (list ms ls)))))
3518                    0 -5)
3519                   "UT"))
3520          ;; Get the original date from the article.
3521          ((eq type 'original)
3522           (concat "Date: " (if (string-match "\n+$" date)
3523                                (substring date 0 (match-beginning 0))
3524                              date)))
3525          ;; Let the user define the format.
3526          ((eq type 'user-defined)
3527           (let ((format (or (condition-case nil
3528                                 (with-current-buffer gnus-summary-buffer
3529                                   gnus-article-time-format)
3530                               (error nil))
3531                             gnus-article-time-format)))
3532             (if (functionp format)
3533                 (funcall format time)
3534               (concat "Date: " (format-time-string format time)))))
3535          ;; ISO 8601.
3536          ((eq type 'iso8601)
3537           (let ((tz (car (current-time-zone time))))
3538             (concat
3539              "Date: "
3540              (format-time-string "%Y%m%dT%H%M%S" time)
3541              (format "%s%02d%02d"
3542                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3543                      (/ (% (abs tz) 3600) 60)))))
3544          ;; Do a lapsed format.
3545          ((eq type 'lapsed)
3546           (concat "Date: " (article-lapsed-string time)))
3547          ;; A combined date/lapsed format.
3548          ((eq type 'combined-lapsed)
3549           (let ((date-string (article-make-date-line date 'original))
3550                 (segments 3)
3551                 lapsed-string)
3552             (while (and
3553                     time
3554                     (setq lapsed-string
3555                           (concat " (" (article-lapsed-string time segments) ")"))
3556                     (> (+ (length date-string)
3557                           (length lapsed-string))
3558                        (+ fill-column 6))
3559                     (> segments 0))
3560               (setq segments (1- segments)))
3561             (if (> segments 0)
3562                 (concat date-string lapsed-string)
3563               date-string)))
3564          ;; Display the date in proper English
3565          ((eq type 'english)
3566           (let ((dtime (decode-time time)))
3567             (concat
3568              "Date: the "
3569              (number-to-string (nth 3 dtime))
3570              (let ((digit (% (nth 3 dtime) 10)))
3571                (cond
3572                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3573                 ((= digit 1) "st")
3574                 ((= digit 2) "nd")
3575                 ((= digit 3) "rd")
3576                 (t "th")))
3577              " of "
3578              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3579              " "
3580              (number-to-string (nth 5 dtime))
3581              " at "
3582              (format "%02d" (nth 2 dtime))
3583              ":"
3584              (format "%02d" (nth 1 dtime)))))))
3585     (foo
3586      (format "Date: %s (from Gnus)" date))))
3587
3588 (defun article-lapsed-string (time &optional max-segments)
3589   ;; If the date is seriously mangled, the timezone functions are
3590   ;; liable to bug out, so we ignore all errors.
3591   (let* ((now (current-time))
3592          (real-time (subtract-time now time))
3593          (real-sec (and real-time
3594                         (+ (* (float (car real-time)) 65536)
3595                            (cadr real-time))))
3596          (sec (and real-time (abs real-sec)))
3597          (segments 0)
3598          num prev)
3599     (unless max-segments
3600       (setq max-segments (length article-time-units)))
3601     (cond
3602      ((null real-time)
3603       "Unknown")
3604      ((zerop sec)
3605       "Now")
3606      (t
3607       (concat
3608        ;; This is a bit convoluted, but basically we go
3609        ;; through the time units for years, weeks, etc,
3610        ;; and divide things to see whether that results
3611        ;; in positive answers.
3612        (mapconcat
3613         (lambda (unit)
3614           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3615                   (>= segments max-segments))
3616               ;; The (remaining) seconds are too few to
3617               ;; be divided into this time unit.
3618               ""
3619             ;; It's big enough, so we output it.
3620             (setq sec (- sec (* num (cdr unit))))
3621             (prog1
3622                 (concat (if prev ", " "") (int-to-string
3623                                            (floor num))
3624                         " " (symbol-name (car unit))
3625                         (if (> num 1) "s" ""))
3626               (setq prev t
3627                     segments (1+ segments)))))
3628         article-time-units "")
3629        ;; If dates are odd, then it might appear like the
3630        ;; article was sent in the future.
3631        (if (> real-sec 0)
3632            " ago"
3633          " in the future"))))))
3634
3635 (defun article-date-local (&optional highlight)
3636   "Convert the current article date to the local timezone."
3637   (interactive (list t))
3638   (article-date-ut 'local highlight))
3639
3640 (defun article-date-english (&optional highlight)
3641   "Convert the current article date to something that is proper English."
3642   (interactive (list t))
3643   (article-date-ut 'english highlight))
3644
3645 (defun article-date-original (&optional highlight)
3646   "Convert the current article date to what it was originally.
3647 This is only useful if you have used some other date conversion
3648 function and want to see what the date was before converting."
3649   (interactive (list t))
3650   (article-date-ut 'original highlight))
3651
3652 (defun article-date-lapsed (&optional highlight)
3653   "Convert the current article date to time lapsed since it was sent."
3654   (interactive (list t))
3655   (article-date-ut 'lapsed highlight))
3656
3657 (defun article-date-combined-lapsed (&optional highlight)
3658   "Convert the current article date to time lapsed since it was sent."
3659   (interactive (list t))
3660   (article-date-ut 'combined-lapsed highlight))
3661
3662 (defun article-update-date-lapsed ()
3663   "Function to be run from a timer to update the lapsed time line."
3664   (save-match-data
3665     (let ((buffer (current-buffer)))
3666       (ignore-errors
3667         (walk-windows
3668          (lambda (w)
3669            (set-buffer (window-buffer w))
3670            (when (eq major-mode 'gnus-article-mode)
3671              (let ((old-line (count-lines (point-min) (point)))
3672                    (old-column (- (point) (line-beginning-position)))
3673                    (window-start
3674                     (window-start (get-buffer-window (current-buffer)))))
3675                (goto-char (point-min))
3676                (while (re-search-forward "^Date:" nil t)
3677                  (let ((type (get-text-property (match-beginning 0)
3678                                                 'gnus-date-type)))
3679                    (when (memq type '(lapsed combined-lapsed user-format))
3680                      (when (and window-start
3681                                 (not (= window-start
3682                                         (save-excursion
3683                                           (forward-line 1)
3684                                           (point)))))
3685                        (setq window-start nil))
3686                      (save-excursion
3687                        (article-date-ut type t (match-beginning 0)))
3688                      (forward-line 1)
3689                      (when window-start
3690                        (set-window-start (get-buffer-window (current-buffer))
3691                                          (point))))))
3692                (goto-char (point-min))
3693                (when (> old-column 0)
3694                  (setq old-line (1- old-line)))
3695                (forward-line old-line)
3696                (end-of-line)
3697                (when (> (current-column) old-column)
3698                  (beginning-of-line)
3699                  (forward-char old-column)))))
3700          nil 'visible))
3701       (set-buffer buffer))))
3702
3703 (defun gnus-start-date-timer (&optional n)
3704   "Start a timer to update the Date headers in the article buffers.
3705 The numerical prefix says how frequently (in seconds) the function
3706 is to run."
3707   (interactive "p")
3708   (unless n
3709     (setq n 1))
3710   (gnus-stop-date-timer)
3711   (setq article-lapsed-timer
3712         (run-at-time 1 n 'article-update-date-lapsed)))
3713
3714 (defun gnus-stop-date-timer ()
3715   "Stop the Date timer."
3716   (interactive)
3717   (when article-lapsed-timer
3718     (nnheader-cancel-timer article-lapsed-timer)
3719     (setq article-lapsed-timer nil)))
3720
3721 (defun article-date-user (&optional highlight)
3722   "Convert the current article date to the user-defined format.
3723 This format is defined by the `gnus-article-time-format' variable."
3724   (interactive (list t))
3725   (article-date-ut 'user highlight))
3726
3727 (defun article-date-iso8601 (&optional highlight)
3728   "Convert the current article date to ISO8601."
3729   (interactive (list t))
3730   (article-date-ut 'iso8601 highlight))
3731
3732 (defmacro gnus-article-save-original-date (&rest forms)
3733   "Save the original date as a text property and evaluate FORMS."
3734   `(let* ((case-fold-search t)
3735           (start (progn
3736                    (goto-char (point-min))
3737                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3738                               (not (bolp)))
3739                      (match-end 0))))
3740           (date (when (and start
3741                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3742                                               nil t))
3743                   (buffer-substring-no-properties start
3744                                                   (match-beginning 0)))))
3745      (goto-char (point-max))
3746      (skip-chars-backward "\n")
3747      (put-text-property (point-min) (point) 'original-date date)
3748      ,@forms
3749      (goto-char (point-max))
3750      (skip-chars-backward "\n")
3751      (put-text-property (point-min) (point) 'original-date date)))
3752
3753 ;; (defun article-show-all ()
3754 ;;   "Show all hidden text in the article buffer."
3755 ;;   (interactive)
3756 ;;   (save-excursion
3757 ;;     (let ((inhibit-read-only t))
3758 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3759
3760 (defun article-remove-leading-whitespace ()
3761   "Remove excessive whitespace from all headers."
3762   (interactive)
3763   (save-excursion
3764     (save-restriction
3765       (let ((inhibit-read-only t))
3766         (article-narrow-to-head)
3767         (goto-char (point-min))
3768         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3769           (delete-region (match-beginning 1) (match-end 1)))))))
3770
3771 (defun article-emphasize (&optional arg)
3772   "Emphasize text according to `gnus-emphasis-alist'."
3773   (interactive (gnus-article-hidden-arg))
3774   (unless (gnus-article-check-hidden-text 'emphasis arg)
3775     (save-excursion
3776       (let ((alist (or
3777                     (condition-case nil
3778                         (with-current-buffer gnus-summary-buffer
3779                           gnus-article-emphasis-alist)
3780                       (error))
3781                     gnus-emphasis-alist))
3782             (inhibit-read-only t)
3783             (props (append '(article-type emphasis)
3784                            gnus-hidden-properties))
3785             regexp elem beg invisible visible face)
3786         (article-goto-body)
3787         (setq beg (point))
3788         (while (setq elem (pop alist))
3789           (goto-char beg)
3790           (setq regexp (car elem)
3791                 invisible (nth 1 elem)
3792                 visible (nth 2 elem)
3793                 face (nth 3 elem))
3794           (while (re-search-forward regexp nil t)
3795             (when (and (match-beginning visible) (match-beginning invisible))
3796               (gnus-article-hide-text
3797                (match-beginning invisible) (match-end invisible) props)
3798               (gnus-article-unhide-text-type
3799                (match-beginning visible) (match-end visible) 'emphasis)
3800               (gnus-put-overlay-excluding-newlines
3801                (match-beginning visible) (match-end visible) 'face face)
3802               (gnus-add-wash-type 'emphasis)
3803               (goto-char (match-end invisible)))))))))
3804
3805 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3806   "Setup newsgroup emphasis alist."
3807   (unless gnus-article-emphasis-alist
3808     (let ((name (and gnus-newsgroup-name
3809                      (gnus-group-real-name gnus-newsgroup-name))))
3810       (make-local-variable 'gnus-article-emphasis-alist)
3811       (setq gnus-article-emphasis-alist
3812             (nconc
3813              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3814                (while (setq elem (pop alist))
3815                  (when (and name (string-match (car elem) name))
3816                    (setq alist nil
3817                          highlight (copy-sequence (cdr elem)))))
3818                highlight)
3819              (copy-sequence highlight-words)
3820              (if gnus-newsgroup-name
3821                  (copy-sequence (gnus-group-find-parameter
3822                                  gnus-newsgroup-name 'highlight-words t)))
3823              gnus-emphasis-alist)))))
3824
3825 (defvar gnus-summary-article-menu)
3826 (defvar gnus-summary-post-menu)
3827
3828 ;;; Saving functions.
3829
3830 (defun gnus-article-save (save-buffer file &optional num)
3831   "Save the currently selected article."
3832   (when (or (get gnus-default-article-saver :headers)
3833             (not gnus-save-all-headers))
3834     ;; Remove headers according to `gnus-saved-headers' or the value
3835     ;; of the `:headers' property that the saver function might have.
3836     (let ((gnus-visible-headers
3837            (or (symbol-value (get gnus-default-article-saver :headers))
3838                gnus-saved-headers gnus-visible-headers))
3839           ;; Ignore group parameter.  See `article-hide-headers'.
3840           (gnus-summary-buffer nil))
3841       (with-current-buffer save-buffer
3842         (article-hide-headers 1 t))))
3843   (save-window-excursion
3844     (if (not gnus-default-article-saver)
3845         (error "No default saver is defined")
3846       ;; !!! Magic!  The saving functions all save
3847       ;; `gnus-save-article-buffer' (or so they think), but we
3848       ;; bind that variable to our save-buffer.
3849       (set-buffer gnus-article-buffer)
3850       (let* ((gnus-save-article-buffer save-buffer)
3851              (filename
3852               (cond
3853                ((not gnus-prompt-before-saving) 'default)
3854                ((eq gnus-prompt-before-saving 'always) nil)
3855                (t file)))
3856              (gnus-number-of-articles-to-be-saved
3857               (when (eq gnus-prompt-before-saving t)
3858                 num)))                  ; Magic
3859         (set-buffer gnus-article-current-summary)
3860         (funcall gnus-default-article-saver filename)))))
3861
3862 (defun gnus-read-save-file-name (prompt &optional filename
3863                                         function group headers variable
3864                                         dir-var)
3865   (let ((default-name
3866           (funcall function group headers (symbol-value variable)))
3867         result)
3868     (setq result
3869           (expand-file-name
3870            (cond
3871             ((eq filename 'default)
3872              default-name)
3873             ((eq filename t)
3874              default-name)
3875             (filename filename)
3876             (t
3877              (when (symbol-value dir-var)
3878                (setq default-name (expand-file-name
3879                                    (file-name-nondirectory default-name)
3880                                    (symbol-value dir-var))))
3881              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3882                     (prompt
3883                      (format prompt
3884                              (if (and gnus-number-of-articles-to-be-saved
3885                                       (> gnus-number-of-articles-to-be-saved 1))
3886                                  (format "these %d articles"
3887                                          gnus-number-of-articles-to-be-saved)
3888                                "this article")))
3889                     (file
3890                      ;; Let the split methods have their say.
3891                      (cond
3892                       ;; No split name was found.
3893                       ((null split-name)
3894                        (read-file-name
3895                         (concat prompt " (default "
3896                                 (file-name-nondirectory default-name) "): ")
3897                         (file-name-directory default-name)
3898                         default-name))
3899                       ;; A single group name is returned.
3900                       ((stringp split-name)
3901                        (setq default-name
3902                              (funcall function split-name headers
3903                                       (symbol-value variable)))
3904                        (read-file-name
3905                         (concat prompt " (default "
3906                                 (file-name-nondirectory default-name) "): ")
3907                         (file-name-directory default-name)
3908                         default-name))
3909                       ;; A single split name was found
3910                       ((= 1 (length split-name))
3911                        (let* ((name (expand-file-name
3912                                      (car split-name)
3913                                      gnus-article-save-directory))
3914                               (dir (cond ((file-directory-p name)
3915                                           (file-name-as-directory name))
3916                                          ((file-exists-p name) name)
3917                                          (t gnus-article-save-directory))))
3918                          (read-file-name
3919                           (concat prompt " (default " name "): ")
3920                           dir name)))
3921                       ;; A list of splits was found.
3922                       (t
3923                        (setq split-name (nreverse split-name))
3924                        (let (result)
3925                          (let ((file-name-history
3926                                 (nconc split-name file-name-history)))
3927                            (setq result
3928                                  (expand-file-name
3929                                   (read-file-name
3930                                    (concat prompt " (`M-p' for defaults): ")
3931                                    gnus-article-save-directory
3932                                    (car split-name))
3933                                   gnus-article-save-directory)))
3934                          (car (push result file-name-history)))))))
3935                ;; Create the directory.
3936                (gnus-make-directory (file-name-directory file))
3937                ;; If we have read a directory, we append the default file name.
3938                (when (file-directory-p file)
3939                  (setq file (expand-file-name (file-name-nondirectory
3940                                                default-name)
3941                                               (file-name-as-directory file))))
3942                ;; Possibly translate some characters.
3943                (nnheader-translate-file-chars file))))))
3944     (gnus-make-directory (file-name-directory result))
3945     (when variable
3946       (set variable result))
3947     (when dir-var
3948       (set dir-var (file-name-directory result)))
3949     result))
3950
3951 (defun gnus-article-archive-name (group)
3952   "Return the first instance of an \"Archive-name\" in the current buffer."
3953   (let ((case-fold-search t))
3954     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3955       (nnheader-concat gnus-article-save-directory
3956                        (match-string 1)))))
3957
3958 (defun gnus-article-nndoc-name (group)
3959   "If GROUP is an nndoc group, return the name of the parent group."
3960   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3961     (gnus-group-get-parameter group 'save-article-group)))
3962
3963 (defun gnus-summary-save-in-rmail (&optional filename)
3964   "Append this article to Rmail file.
3965 Optional argument FILENAME specifies file name.
3966 Directory to save to is default to `gnus-article-save-directory'."
3967   (setq filename (gnus-read-save-file-name
3968                   "Save %s in rmail file" filename
3969                   gnus-rmail-save-name gnus-newsgroup-name
3970                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3971   (with-current-buffer gnus-save-article-buffer
3972     (save-excursion
3973       (save-restriction
3974         (widen)
3975         ;; Note that unlike gnus-summary-save-in-mail, there is no
3976         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3977         ;; not use Babyl.
3978         (gnus-output-to-rmail filename))))
3979   filename)
3980
3981 (defun gnus-summary-save-in-mail (&optional filename)
3982   "Append this article to Unix mail file.
3983 Optional argument FILENAME specifies file name.
3984 Directory to save to is default to `gnus-article-save-directory'."
3985   (setq filename (gnus-read-save-file-name
3986                   "Save %s in Unix mail file" filename
3987                   gnus-mail-save-name gnus-newsgroup-name
3988                   gnus-current-headers 'gnus-newsgroup-last-mail))
3989   (with-current-buffer gnus-save-article-buffer
3990     (save-excursion
3991       (save-restriction
3992         (widen)
3993         (if (and (file-readable-p filename)
3994                  (file-regular-p filename)
3995                  (mail-file-babyl-p filename))
3996             (gnus-output-to-rmail filename)
3997           (gnus-output-to-mail filename)))))
3998   filename)
3999
4000 (put 'gnus-summary-save-in-file :decode t)
4001 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4002 (defun gnus-summary-save-in-file (&optional filename overwrite)
4003   "Append this article to file.
4004 Optional argument FILENAME specifies file name.
4005 Directory to save to is default to `gnus-article-save-directory'."
4006   (setq filename (gnus-read-save-file-name
4007                   "Save %s in file" filename
4008                   gnus-file-save-name gnus-newsgroup-name
4009                   gnus-current-headers 'gnus-newsgroup-last-file))
4010   (with-current-buffer gnus-save-article-buffer
4011     (save-excursion
4012       (save-restriction
4013         (widen)
4014         (when (and overwrite
4015                    (file-exists-p filename))
4016           (delete-file filename))
4017         (gnus-output-to-file filename))))
4018   filename)
4019
4020 (put 'gnus-summary-write-to-file :decode t)
4021 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4022 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4023 (defun gnus-summary-write-to-file (&optional filename)
4024   "Write this article to a file, overwriting it if the file exists.
4025 Optional argument FILENAME specifies file name.
4026 The directory to save in defaults to `gnus-article-save-directory'."
4027   (setq filename (gnus-read-save-file-name
4028                   "Save %s in file" filename
4029                   gnus-file-save-name gnus-newsgroup-name
4030                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4031   (gnus-summary-save-in-file filename t))
4032
4033 (put 'gnus-summary-save-body-in-file :decode t)
4034 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4035   "Append this article body to a file.
4036 Optional argument FILENAME specifies file name.
4037 The directory to save in defaults to `gnus-article-save-directory'."
4038   (setq filename (gnus-read-save-file-name
4039                   "Save %s body in file" filename
4040                   gnus-file-save-name gnus-newsgroup-name
4041                   gnus-current-headers 'gnus-newsgroup-last-file))
4042   (with-current-buffer gnus-save-article-buffer
4043     (save-excursion
4044       (save-restriction
4045         (widen)
4046         (when (article-goto-body)
4047           (narrow-to-region (point) (point-max)))
4048         (when (and overwrite
4049                    (file-exists-p filename))
4050           (delete-file filename))
4051         (gnus-output-to-file filename))))
4052   filename)
4053
4054 (put 'gnus-summary-write-body-to-file :decode t)
4055 (put 'gnus-summary-write-body-to-file
4056      :function 'gnus-summary-save-body-in-file)
4057 (defun gnus-summary-write-body-to-file (&optional filename)
4058   "Write this article body to a file, overwriting it if the file exists.
4059 Optional argument FILENAME specifies file name.
4060 The directory to save in defaults to `gnus-article-save-directory'."
4061   (setq filename (gnus-read-save-file-name
4062                   "Save %s body in file" filename
4063                   gnus-file-save-name gnus-newsgroup-name
4064                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4065   (gnus-summary-save-body-in-file filename t))
4066
4067 (put 'gnus-summary-save-in-pipe :decode t)
4068 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4069 (defun gnus-summary-save-in-pipe (&optional command raw)
4070   "Pipe this article to subprocess COMMAND.
4071 Valid values for COMMAND include:
4072   a string
4073     The executable command name and possibly arguments.
4074   nil
4075     You will be prompted for the command in the minibuffer.
4076   the symbol `default'
4077     It will be replaced with the command which the variable
4078     `gnus-summary-pipe-output-default-command' holds or the command
4079     last used for saving.
4080 Non-nil value for RAW overrides `:decode' and `:headers' properties
4081 and the raw article including all headers will be piped."
4082   (let ((article (gnus-summary-article-number))
4083         (decode (unless raw
4084                   (get 'gnus-summary-save-in-pipe :decode)))
4085         save-buffer default)
4086     (if article
4087         (if (vectorp (gnus-summary-article-header article))
4088             (save-current-buffer
4089               (gnus-summary-select-article decode decode nil article)
4090               (insert-buffer-substring
4091                (prog1
4092                    (if decode
4093                        gnus-article-buffer
4094                      gnus-original-article-buffer)
4095                  (setq save-buffer
4096                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4097               ;; Remove unwanted headers.
4098               (when (and (not raw)
4099                          (or (get 'gnus-summary-save-in-pipe :headers)
4100                              (not gnus-save-all-headers)))
4101                 (let ((gnus-visible-headers
4102                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4103                                               :headers))
4104                            gnus-saved-headers gnus-visible-headers))
4105                       (gnus-summary-buffer nil))
4106                   (article-hide-headers 1 t))))
4107           (error "%d is not a real article" article))
4108       (error "No article to pipe"))
4109     (setq default (or gnus-summary-pipe-output-default-command
4110                       gnus-last-shell-command))
4111     (unless (stringp command)
4112       (setq command
4113             (if (and (eq command 'default) default)
4114                 default
4115               (gnus-read-shell-command "Shell command on this article: "
4116                                        default))))
4117     (when (string-equal command "")
4118       (if default
4119           (setq command default)
4120         (error "A command is required")))
4121     (with-current-buffer save-buffer
4122       (save-restriction
4123         (widen)
4124         (shell-command-on-region (point-min) (point-max) command nil)))
4125     (gnus-kill-buffer save-buffer))
4126   (setq gnus-summary-pipe-output-default-command command))
4127
4128 (defun gnus-summary-pipe-to-muttprint (&optional command)
4129   "Pipe this article to muttprint."
4130   (unless (stringp command)
4131     (setq command (read-string
4132                    "Print using command: " gnus-summary-muttprint-program
4133                    nil gnus-summary-muttprint-program)))
4134   (let ((gnus-summary-pipe-output-default-command
4135          gnus-summary-pipe-output-default-command))
4136     (gnus-summary-save-in-pipe command))
4137   (setq gnus-summary-muttprint-program command))
4138
4139 ;;; Article file names when saving.
4140
4141 (defun gnus-capitalize-newsgroup (newsgroup)
4142   "Capitalize NEWSGROUP name."
4143   (when (not (zerop (length newsgroup)))
4144     (concat (char-to-string (upcase (aref newsgroup 0)))
4145             (substring newsgroup 1))))
4146
4147 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4148   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4149 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4150 Otherwise, it is like ~/News/news/group/num."
4151   (let ((default
4152           (expand-file-name
4153            (concat (if (gnus-use-long-file-name 'not-save)
4154                        (gnus-capitalize-newsgroup newsgroup)
4155                      (gnus-newsgroup-directory-form newsgroup))
4156                    "/" (int-to-string (mail-header-number headers)))
4157            gnus-article-save-directory)))
4158     (if (and last-file
4159              (string-equal (file-name-directory default)
4160                            (file-name-directory last-file))
4161              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4162         default
4163       (or last-file default))))
4164
4165 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4166   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4167 If variable `gnus-use-long-file-name' is non-nil, it is
4168 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4169   (let ((default
4170           (expand-file-name
4171            (concat (if (gnus-use-long-file-name 'not-save)
4172                        newsgroup
4173                      (gnus-newsgroup-directory-form newsgroup))
4174                    "/" (int-to-string (mail-header-number headers)))
4175            gnus-article-save-directory)))
4176     (if (and last-file
4177              (string-equal (file-name-directory default)
4178                            (file-name-directory last-file))
4179              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4180         default
4181       (or last-file default))))
4182
4183 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4184   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4185 If variable `gnus-use-long-file-name' is non-nil, it is
4186 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4187   (or last-file
4188       (expand-file-name
4189        (if (gnus-use-long-file-name 'not-save)
4190            newsgroup
4191          (file-relative-name
4192           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4193           default-directory))
4194        gnus-article-save-directory)))
4195
4196 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4197   "Generate file name from sender."
4198   (let ((from (mail-header-from headers)))
4199     (expand-file-name
4200      (if (and from (string-match "\\([^ <]+\\)@" from))
4201          (match-string 1 from)
4202        "nobody")
4203      gnus-article-save-directory)))
4204
4205 (defun article-verify-x-pgp-sig ()
4206   "Verify X-PGP-Sig."
4207   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4208   (interactive)
4209   (if (gnus-buffer-live-p gnus-original-article-buffer)
4210       (let ((sig (with-current-buffer gnus-original-article-buffer
4211                    (gnus-fetch-field "X-PGP-Sig")))
4212             items info headers)
4213         (when (and sig
4214                    mml2015-use
4215                    (mml2015-clear-verify-function))
4216           (with-temp-buffer
4217             (insert-buffer-substring gnus-original-article-buffer)
4218             (setq items (split-string sig))
4219             (message-narrow-to-head)
4220             (let ((inhibit-point-motion-hooks t)
4221                   (case-fold-search t))
4222               ;; Don't verify multiple headers.
4223               (setq headers (mapconcat (lambda (header)
4224                                          (concat header ": "
4225                                                  (mail-fetch-field header)
4226                                                  "\n"))
4227                                        (split-string (nth 1 items) ",") "")))
4228             (delete-region (point-min) (point-max))
4229             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4230             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4231             (insert headers)
4232             (widen)
4233             (forward-line)
4234             (while (not (eobp))
4235               (if (looking-at "^-")
4236                   (insert "- "))
4237               (forward-line))
4238             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4239             (insert "Version: " (car items) "\n\n")
4240             (insert (mapconcat 'identity (cddr items) "\n"))
4241             (insert "\n-----END PGP SIGNATURE-----\n")
4242             (let ((mm-security-handle (list (format "multipart/signed"))))
4243               (mml2015-clean-buffer)
4244               (let ((coding-system-for-write (or gnus-newsgroup-charset
4245                                                  'iso-8859-1)))
4246                 (funcall (mml2015-clear-verify-function)))
4247               (setq info
4248                     (or (mm-handle-multipart-ctl-parameter
4249                          mm-security-handle 'gnus-details)
4250                         (mm-handle-multipart-ctl-parameter
4251                          mm-security-handle 'gnus-info)))))
4252           (when info
4253             (let ((inhibit-read-only t) bface eface)
4254               (save-restriction
4255                 (message-narrow-to-head)
4256                 (goto-char (point-max))
4257                 (forward-line -1)
4258                 (setq bface (get-text-property (point-at-bol) 'face)
4259                       eface (get-text-property (1- (point-at-eol)) 'face))
4260                 (message-remove-header "X-Gnus-PGP-Verify")
4261                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4262                     (forward-line)
4263                   (goto-char (point-max)))
4264                 (narrow-to-region (point) (point))
4265                 (insert "X-Gnus-PGP-Verify: " info "\n")
4266                 (goto-char (point-min))
4267                 (forward-line)
4268                 (while (not (eobp))
4269                   (if (not (looking-at "^[ \t]"))
4270                       (insert " "))
4271                   (forward-line))
4272                 ;; Do highlighting.
4273                 (goto-char (point-min))
4274                 (when (looking-at "\\([^:]+\\): *")
4275                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4276                                      'face bface)
4277                   (put-text-property (match-end 0) (point-max)
4278                                      'face eface)))))))))
4279
4280 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4281
4282 (defun article-verify-cancel-lock ()
4283   "Verify Cancel-Lock header."
4284   (interactive)
4285   (if (gnus-buffer-live-p gnus-original-article-buffer)
4286       (canlock-verify gnus-original-article-buffer)))
4287
4288 (eval-and-compile
4289   (mapc
4290    (lambda (func)
4291      (let (afunc gfunc)
4292        (if (consp func)
4293            (setq afunc (car func)
4294                  gfunc (cdr func))
4295          (setq afunc func
4296                gfunc (intern (format "gnus-%s" func))))
4297        (defalias gfunc
4298          (when (fboundp afunc)
4299            `(lambda (&optional interactive &rest args)
4300               ,(documentation afunc t)
4301               (interactive (list t))
4302               (with-current-buffer gnus-article-buffer
4303                 (if interactive
4304                     (call-interactively ',afunc)
4305                   (apply ',afunc args))))))))
4306    '(article-hide-headers
4307      article-verify-x-pgp-sig
4308      article-verify-cancel-lock
4309      article-hide-boring-headers
4310      article-treat-overstrike
4311      article-treat-ansi-sequences
4312      article-fill-long-lines
4313      article-capitalize-sentences
4314      article-remove-cr
4315      article-remove-leading-whitespace
4316      article-display-x-face
4317      article-display-face
4318      article-de-quoted-unreadable
4319      article-de-base64-unreadable
4320      article-decode-HZ
4321      article-wash-html
4322      article-unsplit-urls
4323      article-hide-list-identifiers
4324      article-strip-banner
4325      article-babel
4326      article-hide-pem
4327      article-hide-signature
4328      article-strip-headers-in-body
4329      article-remove-trailing-blank-lines
4330      article-strip-leading-blank-lines
4331      article-strip-multiple-blank-lines
4332      article-strip-leading-space
4333      article-strip-trailing-space
4334      article-strip-blank-lines
4335      article-strip-all-blank-lines
4336      article-date-local
4337      article-date-english
4338      article-date-iso8601
4339      article-date-original
4340      article-treat-date
4341      article-date-ut
4342      article-decode-mime-words
4343      article-decode-charset
4344      article-decode-encoded-words
4345      article-date-user
4346      article-date-lapsed
4347      article-date-combined-lapsed
4348      article-emphasize
4349      article-treat-dumbquotes
4350      article-treat-non-ascii
4351      article-normalize-headers
4352      ;;(article-show-all . gnus-article-show-all-headers)
4353      )))
4354 \f
4355 ;;;
4356 ;;; Gnus article mode
4357 ;;;
4358
4359 (put 'gnus-article-mode 'mode-class 'special)
4360
4361 (set-keymap-parent gnus-article-mode-map widget-keymap)
4362
4363 (gnus-define-keys gnus-article-mode-map
4364   " " gnus-article-goto-next-page
4365   "\177" gnus-article-goto-prev-page
4366   [delete] gnus-article-goto-prev-page
4367   [backspace] gnus-article-goto-prev-page
4368   "\C-c^" gnus-article-refer-article
4369   "h" gnus-article-show-summary
4370   "s" gnus-article-show-summary
4371   "\C-c\C-m" gnus-article-mail
4372   "?" gnus-article-describe-briefly
4373   "<" beginning-of-buffer
4374   ">" end-of-buffer
4375   "\C-c\C-i" gnus-info-find-node
4376   "\C-c\C-b" gnus-bug
4377   "R" gnus-article-reply-with-original
4378   "F" gnus-article-followup-with-original
4379   "\C-hk" gnus-article-describe-key
4380   "\C-hc" gnus-article-describe-key-briefly
4381   "\C-hb" gnus-article-describe-bindings
4382
4383   "e" gnus-article-read-summary-keys
4384   "\C-d" gnus-article-read-summary-keys
4385   "\M-*" gnus-article-read-summary-keys
4386   "\M-#" gnus-article-read-summary-keys
4387   "\M-^" gnus-article-read-summary-keys
4388   "\M-g" gnus-article-read-summary-keys)
4389
4390 (substitute-key-definition
4391  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4392
4393 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4394   "W" gnus-article-wide-reply-with-original)
4395 (if (featurep 'xemacs)
4396     (set-keymap-default-binding gnus-article-send-map
4397                                 'gnus-article-read-summary-send-keys)
4398   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4399
4400 (defun gnus-article-make-menu-bar ()
4401   (unless (boundp 'gnus-article-commands-menu)
4402     (gnus-summary-make-menu-bar))
4403   (unless (boundp 'gnus-article-article-menu)
4404     (easy-menu-define
4405      gnus-article-article-menu gnus-article-mode-map ""
4406      '("Article"
4407        ["Scroll forwards" gnus-article-goto-next-page t]
4408        ["Scroll backwards" gnus-article-goto-prev-page t]
4409        ["Show summary" gnus-article-show-summary t]
4410        ["Fetch Message-ID at point" gnus-article-refer-article t]
4411        ["Mail to address at point" gnus-article-mail t]
4412        ["Send a bug report" gnus-bug t]))
4413
4414     (easy-menu-define
4415      gnus-article-treatment-menu gnus-article-mode-map ""
4416      ;; Fixme: this should use :active (and maybe :visible).
4417      '("Treatment"
4418        ["Hide headers" gnus-article-hide-headers t]
4419        ["Hide signature" gnus-article-hide-signature t]
4420        ["Hide citation" gnus-article-hide-citation t]
4421        ["Treat overstrike" gnus-article-treat-overstrike t]
4422        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4423        ["Remove carriage return" gnus-article-remove-cr t]
4424        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4425        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4426        ["Remove base64" gnus-article-de-base64-unreadable t]
4427        ["Treat html" gnus-article-wash-html t]
4428        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4429        ["Decode HZ" gnus-article-decode-HZ t]))
4430
4431     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4432
4433     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4434
4435     (gnus-run-hooks 'gnus-article-menu-hook)))
4436
4437 (defvar bookmark-make-record-function)
4438 (defvar shr-put-image-function)
4439
4440 (defun gnus-article-mode ()
4441   "Major mode for displaying an article.
4442
4443 All normal editing commands are switched off.
4444
4445 The following commands are available in addition to all summary mode
4446 commands:
4447 \\<gnus-article-mode-map>
4448 \\[gnus-article-next-page]\t Scroll the article one page forwards
4449 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4450 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4451 \\[gnus-article-show-summary]\t Display the summary buffer
4452 \\[gnus-article-mail]\t Send a reply to the address near point
4453 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4454 \\[gnus-info-find-node]\t Go to the Gnus info node"
4455   (interactive)
4456   (kill-all-local-variables)
4457   (gnus-simplify-mode-line)
4458   (setq mode-name "Article")
4459   (setq major-mode 'gnus-article-mode)
4460   (make-local-variable 'minor-mode-alist)
4461   (use-local-map gnus-article-mode-map)
4462   (when (gnus-visual-p 'article-menu 'menu)
4463     (gnus-article-make-menu-bar)
4464     (when gnus-summary-tool-bar-map
4465       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4466   (gnus-update-format-specifications nil 'article-mode)
4467   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4468   (set (make-local-variable 'gnus-page-broken) nil)
4469   (make-local-variable 'gnus-article-current-summary)
4470   (make-local-variable 'gnus-article-mime-handles)
4471   (make-local-variable 'gnus-article-decoded-p)
4472   (make-local-variable 'gnus-article-mime-handle-alist)
4473   (make-local-variable 'gnus-article-wash-types)
4474   (make-local-variable 'gnus-article-image-alist)
4475   (make-local-variable 'gnus-article-charset)
4476   (make-local-variable 'gnus-article-ignored-charsets)
4477   (set (make-local-variable 'bookmark-make-record-function)
4478        'gnus-summary-bookmark-make-record)
4479   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4480   ;; face.
4481   (set (make-local-variable 'nobreak-char-display) nil)
4482   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4483   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4484   (setq cursor-in-non-selected-windows nil)
4485   (gnus-set-default-directory)
4486   (buffer-disable-undo)
4487   (setq buffer-read-only t
4488         show-trailing-whitespace nil)
4489   (set-syntax-table gnus-article-mode-syntax-table)
4490   (mm-enable-multibyte)
4491   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4492
4493 (defun gnus-article-setup-buffer ()
4494   "Initialize the article buffer."
4495   (let* ((name (if gnus-single-article-buffer "*Article*"
4496                  (concat "*Article "
4497                          (gnus-group-decoded-name gnus-newsgroup-name)
4498                          "*")))
4499          (original
4500           (progn (string-match "\\*Article" name)
4501                  (concat " *Original Article"
4502                          (substring name (match-end 0))))))
4503     (setq gnus-article-buffer name)
4504     (setq gnus-original-article-buffer original)
4505     (setq gnus-article-mime-handle-alist nil)
4506     (with-current-buffer gnus-summary-buffer
4507       ;; This might be a variable local to the summary buffer.
4508       (unless gnus-single-article-buffer
4509         (setq gnus-article-buffer name)
4510         (setq gnus-original-article-buffer original)
4511         (gnus-set-global-variables)))
4512     (gnus-article-setup-highlight-words)
4513     ;; Init original article buffer.
4514     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4515       (mm-enable-multibyte)
4516       (setq major-mode 'gnus-original-article-mode)
4517       (make-local-variable 'gnus-original-article))
4518     (if (and (get-buffer name)
4519              (with-current-buffer name
4520                (if gnus-article-edit-mode
4521                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4522                        (progn
4523                          (set-buffer-modified-p nil)
4524                          (gnus-kill-buffer name)
4525                          (message "")
4526                          nil)
4527                      (error "Action aborted"))
4528                  t)))
4529         (with-current-buffer name
4530           (set (make-local-variable 'gnus-article-edit-mode) nil)
4531           (gnus-article-stop-animations)
4532           (when gnus-article-mime-handles
4533             (mm-destroy-parts gnus-article-mime-handles)
4534             (setq gnus-article-mime-handles nil))
4535           ;; Set it to nil in article-buffer!
4536           (setq gnus-article-mime-handle-alist nil)
4537           (buffer-disable-undo)
4538           (setq buffer-read-only t)
4539           (unless (eq major-mode 'gnus-article-mode)
4540             (gnus-article-mode))
4541           (setq truncate-lines gnus-article-truncate-lines)
4542           (current-buffer))
4543       (let ((summary gnus-summary-buffer))
4544         (with-current-buffer (gnus-get-buffer-create name)
4545           (gnus-article-mode)
4546           (setq truncate-lines gnus-article-truncate-lines)
4547           (set (make-local-variable 'gnus-summary-buffer) summary)
4548           (gnus-summary-set-local-parameters gnus-newsgroup-name)
4549           (when article-lapsed-timer
4550             (gnus-stop-date-timer))
4551           (when gnus-article-update-date-headers
4552             (gnus-start-date-timer gnus-article-update-date-headers))
4553           (current-buffer))))))
4554
4555 (defun gnus-article-stop-animations ()
4556   (dolist (timer (and (boundp 'timer-list)
4557                       timer-list))
4558     (when (eq (gnus-timer--function timer) 'image-animate-timeout)
4559       (cancel-timer timer))))
4560
4561 (defun gnus-stop-downloads ()
4562   (when (boundp 'url-queue)
4563     (set (intern "url-queue" obarray) nil)))
4564
4565 ;; Set article window start at LINE, where LINE is the number of lines
4566 ;; from the head of the article.
4567 (defun gnus-article-set-window-start (&optional line)
4568   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4569     (when article-window
4570       (set-window-start
4571        article-window
4572        (with-current-buffer gnus-article-buffer
4573          (goto-char (point-min))
4574          (if (not line)
4575              (point-min)
4576            (gnus-message 6 "Moved to bookmark")
4577            (search-forward "\n\n" nil t)
4578            (forward-line line)
4579            (point)))))))
4580
4581 (defun gnus-article-prepare (article &optional all-headers header)
4582   "Prepare ARTICLE in article mode buffer.
4583 ARTICLE should either be an article number or a Message-ID.
4584 If ARTICLE is an id, HEADER should be the article headers.
4585 If ALL-HEADERS is non-nil, no headers are hidden."
4586   (save-excursion
4587     ;; Make sure we start in a summary buffer.
4588     (unless (eq major-mode 'gnus-summary-mode)
4589       (set-buffer gnus-summary-buffer))
4590     (setq gnus-summary-buffer (current-buffer))
4591     (let* ((gnus-article (if header (mail-header-number header) article))
4592            (summary-buffer (current-buffer))
4593            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4594            (group gnus-newsgroup-name)
4595            result)
4596       (save-excursion
4597         (gnus-article-setup-buffer)
4598         (set-buffer gnus-article-buffer)
4599         ;; Deactivate active regions.
4600         (when (and (boundp 'transient-mark-mode)
4601                    transient-mark-mode)
4602           (setq mark-active nil))
4603         (if (not (setq result (let ((inhibit-read-only t))
4604                                 (gnus-request-article-this-buffer
4605                                  article group))))
4606             ;; There is no such article.
4607             (save-excursion
4608               (when (and (numberp article)
4609                          (not (memq article gnus-newsgroup-sparse)))
4610                 (setq gnus-article-current
4611                       (cons gnus-newsgroup-name article))
4612                 (set-buffer gnus-summary-buffer)
4613                 (setq gnus-current-article article)
4614                 (if (and (memq article gnus-newsgroup-undownloaded)
4615                          (not (gnus-online (gnus-find-method-for-group
4616                                             gnus-newsgroup-name))))
4617                     (progn
4618                       (gnus-summary-set-agent-mark article)
4619                       (message "Message marked for downloading"))
4620                   (gnus-summary-mark-article article gnus-canceled-mark)
4621                   (unless (memq article gnus-newsgroup-sparse)
4622                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4623           (if (or (eq result 'pseudo)
4624                   (eq result 'nneething))
4625               (progn
4626                 (with-current-buffer summary-buffer
4627                   (push article gnus-newsgroup-history)
4628                   (setq gnus-last-article gnus-current-article
4629                         gnus-current-article 0
4630                         gnus-current-headers nil
4631                         gnus-article-current nil)
4632                   (if (eq result 'nneething)
4633                       (gnus-configure-windows 'summary)
4634                     (gnus-configure-windows 'article))
4635                   (gnus-set-global-variables))
4636                 (let ((gnus-article-mime-handle-alist-1
4637                        gnus-article-mime-handle-alist))
4638                   (gnus-set-mode-line 'article)))
4639             ;; The result from the `request' was an actual article -
4640             ;; or at least some text that is now displayed in the
4641             ;; article buffer.
4642             (when (and (numberp article)
4643                        (not (eq article gnus-current-article)))
4644               ;; Seems like a new article has been selected.
4645               ;; `gnus-current-article' must be an article number.
4646               (with-current-buffer summary-buffer
4647                 (push article gnus-newsgroup-history)
4648                 (setq gnus-last-article gnus-current-article
4649                       gnus-current-article article
4650                       gnus-current-headers
4651                       (gnus-summary-article-header gnus-current-article)
4652                       gnus-article-current
4653                       (cons gnus-newsgroup-name gnus-current-article))
4654                 (unless (vectorp gnus-current-headers)
4655                   (setq gnus-current-headers nil))
4656                 (gnus-summary-goto-subject gnus-current-article)
4657                 (when (gnus-summary-show-thread)
4658                   ;; If the summary buffer really was folded, the
4659                   ;; previous goto may not actually have gone to
4660                   ;; the right article, but the thread root instead.
4661                   ;; So we go again.
4662                   (gnus-summary-goto-subject gnus-current-article))
4663                 (gnus-run-hooks 'gnus-mark-article-hook)
4664                 (gnus-set-mode-line 'summary)
4665                 (when (gnus-visual-p 'article-highlight 'highlight)
4666                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4667                 ;; Set the global newsgroup variables here.
4668                 (gnus-set-global-variables)
4669                 (setq gnus-have-all-headers
4670                       (or all-headers gnus-show-all-headers))))
4671             (save-excursion
4672               (gnus-configure-windows 'article))
4673             (when (or (numberp article)
4674                       (stringp article))
4675               (gnus-article-prepare-display)
4676               ;; Do page break.
4677               (goto-char (point-min))
4678               (when gnus-break-pages
4679                 (gnus-narrow-to-page)))
4680             (let ((gnus-article-mime-handle-alist-1
4681                    gnus-article-mime-handle-alist))
4682               (gnus-set-mode-line 'article))
4683             (article-goto-body)
4684             (unless (bobp)
4685               (forward-line -1))
4686             (set-window-point (get-buffer-window (current-buffer)) (point))
4687             (gnus-configure-windows 'article)
4688             (gnus-run-hooks 'gnus-article-prepare-hook)
4689             t))))))
4690
4691 ;;;###autoload
4692 (defun gnus-article-prepare-display ()
4693   "Make the current buffer look like a nice article."
4694   ;; Hooks for getting information from the article.
4695   ;; This hook must be called before being narrowed.
4696   (let ((gnus-article-buffer (current-buffer))
4697         buffer-read-only
4698         (inhibit-read-only t))
4699     (unless (eq major-mode 'gnus-article-mode)
4700       (gnus-article-mode))
4701     (setq buffer-read-only nil
4702           gnus-article-wash-types nil
4703           gnus-article-image-alist nil)
4704     (gnus-run-hooks 'gnus-tmp-internal-hook)
4705     (when gnus-display-mime-function
4706       (funcall gnus-display-mime-function))))
4707
4708 ;;;
4709 ;;; Gnus Sticky Article Mode
4710 ;;;
4711
4712 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4713   "Mode for sticky articles."
4714   ;; Release bindings that won't work.
4715   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4716                              gnus-sticky-article-mode-map)
4717   (substitute-key-definition 'gnus-article-refer-article 'undefined
4718                              gnus-sticky-article-mode-map)
4719   (dolist (k '("e" "h" "s" "F" "R"))
4720     (define-key gnus-sticky-article-mode-map k nil))
4721   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4722   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4723   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4724   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4725
4726 (defun gnus-sticky-article (arg)
4727   "Make the current article sticky.
4728 If a prefix ARG is given, ask for a name for this sticky article buffer."
4729   (interactive "P")
4730   (gnus-summary-show-thread)
4731   (gnus-summary-select-article nil nil 'pseudo)
4732   (let (new-art-buf-name)
4733     (gnus-eval-in-buffer-window gnus-article-buffer
4734       (setq new-art-buf-name
4735             (concat
4736              "*Sticky Article: "
4737              (if arg
4738                  (read-from-minibuffer "Sticky article buffer name: ")
4739                (gnus-with-article-headers
4740                  (gnus-article-goto-header "subject")
4741                  (setq new-art-buf-name
4742                        (buffer-substring-no-properties
4743                         (line-beginning-position) (line-end-position)))
4744                  (goto-char (point-min))
4745                  (gnus-article-goto-header "from")
4746                  (setq new-art-buf-name
4747                        (concat
4748                         new-art-buf-name ", "
4749                         (buffer-substring-no-properties
4750                          (line-beginning-position) (line-end-position))))
4751                  (goto-char (point-min))
4752                  (gnus-article-goto-header "date")
4753                  (setq new-art-buf-name
4754                        (concat
4755                         new-art-buf-name ", "
4756                         (buffer-substring-no-properties
4757                          (line-beginning-position) (line-end-position))))))
4758              "*"))
4759       (if (and (gnus-buffer-live-p new-art-buf-name)
4760                (with-current-buffer new-art-buf-name
4761                  (eq major-mode 'gnus-sticky-article-mode)))
4762           (switch-to-buffer new-art-buf-name)
4763         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4764       (gnus-sticky-article-mode))
4765     (setq gnus-article-buffer new-art-buf-name))
4766   (gnus-summary-recenter)
4767   (gnus-summary-position-point))
4768
4769 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4770   "Kill the given sticky article BUFFER.
4771 If none is given, assume the current buffer and kill it if it has
4772 `gnus-sticky-article-mode'."
4773   (interactive)
4774   (unless buffer
4775     (setq buffer (current-buffer)))
4776   (with-current-buffer buffer
4777     (when (eq major-mode 'gnus-sticky-article-mode)
4778       (gnus-kill-buffer buffer))))
4779
4780 (defun gnus-kill-sticky-article-buffers (arg)
4781   "Kill all sticky article buffers.
4782 If a prefix ARG is given, ask for confirmation."
4783   (interactive "P")
4784   (dolist (buf (gnus-buffers))
4785     (with-current-buffer buf
4786       (when (eq major-mode 'gnus-sticky-article-mode)
4787        (if (not arg)
4788            (gnus-kill-buffer buf)
4789          (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4790            (gnus-kill-buffer buf)))))))
4791
4792 ;;;
4793 ;;; Gnus MIME viewing functions
4794 ;;;
4795
4796 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4797   "Format of the MIME buttons.
4798
4799 Valid specifiers include:
4800 %t  The MIME type
4801 %T  MIME type, along with additional info
4802 %n  The `name' parameter
4803 %d  The description, if any
4804 %l  The length of the encoded part
4805 %p  The part identifier number
4806 %e  Dots if the part isn't displayed
4807
4808 General format specifiers can also be used.  See Info node
4809 `(gnus)Formatting Variables'.")
4810
4811 (defvar gnus-mime-button-line-format-alist
4812   '((?t gnus-tmp-type ?s)
4813     (?T gnus-tmp-type-long ?s)
4814     (?n gnus-tmp-name ?s)
4815     (?d gnus-tmp-description ?s)
4816     (?p gnus-tmp-id ?s)
4817     (?l gnus-tmp-length ?d)
4818     (?e gnus-tmp-dots ?s)))
4819
4820 (defvar gnus-mime-button-commands
4821   '((gnus-article-press-button "\r" "Toggle Display")
4822     (gnus-mime-view-part "v" "View Interactively...")
4823     (gnus-mime-view-part-as-type "t" "View As Type...")
4824     (gnus-mime-view-part-as-charset "C" "View As charset...")
4825     (gnus-mime-save-part "o" "Save...")
4826     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4827     (gnus-mime-replace-part "r" "Replace part")
4828     (gnus-mime-delete-part "d" "Delete part")
4829     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4830     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4831     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4832     (gnus-mime-view-part-externally "e" "View Externally")
4833     (gnus-mime-print-part "p" "Print")
4834     (gnus-mime-pipe-part "|" "Pipe To Command...")
4835     (gnus-mime-action-on-part "." "Take action on the part...")))
4836
4837 (defun gnus-article-mime-part-status ()
4838   (if gnus-article-mime-handle-alist-1
4839       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4840           " (1 part)"
4841         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4842     ""))
4843
4844 (defvar gnus-mime-button-map
4845   (let ((map (make-sparse-keymap)))
4846     (define-key map gnus-mouse-2 'gnus-article-push-button)
4847     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4848     (dolist (c gnus-mime-button-commands)
4849       (define-key map (cadr c) (car c)))
4850     map))
4851
4852 (easy-menu-define
4853   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4854   `("MIME Part"
4855     ,@(mapcar (lambda (c)
4856                 (vector (caddr c) (car c) :active t))
4857               gnus-mime-button-commands)))
4858
4859 (defvar gnus-url-button-commands
4860   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4861
4862 (defvar gnus-url-button-map
4863   (let ((map (make-sparse-keymap)))
4864     (dolist (c gnus-url-button-commands)
4865       (define-key map (cadr c) (car c)))
4866     map))
4867
4868 (easy-menu-define
4869   gnus-url-button-menu gnus-url-button-map "URL button menu."
4870   `("Url Button"
4871     ,@(mapcar (lambda (c)
4872                 (vector (caddr c) (car c) :active t))
4873               gnus-url-button-commands)))
4874
4875 (defmacro gnus-bind-safe-url-regexp (&rest body)
4876   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4877   `(let ((mm-w3m-safe-url-regexp
4878           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4879                                 (gnus-buffer-live-p
4880                                  gnus-article-current-summary))
4881                            (with-current-buffer gnus-article-current-summary
4882                              gnus-newsgroup-name)
4883                          gnus-newsgroup-name)))
4884             (if (cond ((not group)
4885                        ;; Maybe we're in a mml-preview buffer
4886                        ;; and no group is selected.
4887                        t)
4888                       ((stringp gnus-safe-html-newsgroups)
4889                        (string-match gnus-safe-html-newsgroups group))
4890                       ((consp gnus-safe-html-newsgroups)
4891                        (member group gnus-safe-html-newsgroups)))
4892                 nil
4893               mm-w3m-safe-url-regexp))))
4894      ,@body))
4895
4896 (defun gnus-mime-button-menu (event prefix)
4897  "Construct a context-sensitive menu of MIME commands."
4898  (interactive "e\nP")
4899  (save-window-excursion
4900    (let ((pos (event-start event)))
4901      (select-window (posn-window pos))
4902      (goto-char (posn-point pos))
4903      (gnus-article-check-buffer)
4904      (popup-menu gnus-mime-button-menu nil prefix))))
4905
4906 (defun gnus-mime-view-all-parts (&optional handles)
4907   "View all the MIME parts."
4908   (interactive)
4909   (with-current-buffer gnus-article-buffer
4910     (let ((handles (or handles gnus-article-mime-handles))
4911           (mail-parse-charset gnus-newsgroup-charset)
4912           (mail-parse-ignored-charsets
4913            (with-current-buffer gnus-summary-buffer
4914              gnus-newsgroup-ignored-charsets)))
4915       (when handles
4916         (mm-remove-parts handles)
4917         (goto-char (point-min))
4918         (or (search-forward "\n\n") (goto-char (point-max)))
4919         (let ((inhibit-read-only t))
4920           (delete-region (point) (point-max))
4921           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4922
4923 (defun gnus-article-jump-to-part (n)
4924   "Jump to MIME part N."
4925   (interactive "P")
4926   (let ((parts (with-current-buffer gnus-article-buffer
4927                  (length gnus-article-mime-handle-alist))))
4928     (when (zerop parts)
4929       (error "No such part"))
4930     (pop-to-buffer gnus-article-buffer)
4931     (or n
4932         (setq n (if (= parts 1)
4933                     1
4934                   (read-number (format "Jump to part (1..%s): " parts)))))
4935     (unless (and (integerp n) (<= n parts) (>= n 1))
4936       (setq n
4937             (progn
4938               (gnus-message 7 "Invalid part `%s', using %s instead."
4939                             n parts)
4940               parts)))
4941     (gnus-message 9 "Jumping to part %s." n)
4942     (cond ((>= gnus-auto-select-part 1)
4943            (while (and (<= n parts)
4944                        (not (gnus-article-goto-part n)))
4945              (setq n (1+ n))))
4946           ((< gnus-auto-select-part 0)
4947            (while (and (>= n 1)
4948                        (not (gnus-article-goto-part n)))
4949              (setq n (1- n))))
4950           (t
4951            (gnus-article-goto-part n)))))
4952
4953 (defvar gnus-mime-buttonized-part-id nil
4954   "ID of a mime part that should be buttonized.
4955 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4956
4957 (eval-when-compile
4958   (defsubst gnus-article-edit-part (handles &optional current-id)
4959     "Edit an article in order to delete a mime part.
4960 This function is exclusively used by `gnus-mime-save-part-and-strip'
4961 and `gnus-mime-delete-part', and not provided at run-time normally."
4962     (gnus-article-edit-article
4963      `(lambda ()
4964         (buffer-disable-undo)
4965         (erase-buffer)
4966         (let ((mail-parse-charset (or gnus-article-charset
4967                                       ',gnus-newsgroup-charset))
4968               (mail-parse-ignored-charsets
4969                (or gnus-article-ignored-charsets
4970                    ',gnus-newsgroup-ignored-charsets))
4971               (mbl mml-buffer-list))
4972           (setq mml-buffer-list nil)
4973           (insert-buffer-substring gnus-original-article-buffer)
4974           (mime-to-mml ',handles)
4975           (setq gnus-article-mime-handles nil)
4976           (let ((mbl1 mml-buffer-list))
4977             (setq mml-buffer-list mbl)
4978             (set (make-local-variable 'mml-buffer-list) mbl1))
4979           (gnus-make-local-hook 'kill-buffer-hook)
4980           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4981      `(lambda (no-highlight)
4982         (let ((mail-parse-charset (or gnus-article-charset
4983                                       ',gnus-newsgroup-charset))
4984               (message-options message-options)
4985               (message-options-set-recipient)
4986               (mail-parse-ignored-charsets
4987                (or gnus-article-ignored-charsets
4988                    ',gnus-newsgroup-ignored-charsets)))
4989           (mml-to-mime)
4990           (mml-destroy-buffers)
4991           (remove-hook 'kill-buffer-hook
4992                        'mml-destroy-buffers t)
4993           (kill-local-variable 'mml-buffer-list))
4994         (gnus-summary-edit-article-done
4995          ,(or (mail-header-references gnus-current-headers) "")
4996          ,(gnus-group-read-only-p)
4997          ,gnus-summary-buffer no-highlight))
4998      t)
4999     ;; Force buttonizing this part.
5000     (let ((gnus-mime-buttonized-part-id current-id))
5001       (gnus-article-edit-done))
5002     (gnus-configure-windows 'article)
5003     (when (and current-id (integerp gnus-auto-select-part))
5004       (gnus-article-jump-to-part
5005        (min (max (+ current-id gnus-auto-select-part) 1)
5006             (with-current-buffer gnus-article-buffer
5007               (length gnus-article-mime-handle-alist)))))))
5008
5009 (defun gnus-mime-replace-part (file)
5010   "Replace MIME part under point with an external body."
5011   ;; Useful if file has already been saved to disk
5012   (interactive
5013    (list
5014     (read-file-name "Replace MIME part with file: "
5015                     (or mm-default-directory default-directory)
5016                     nil nil)))
5017   (gnus-mime-save-part-and-strip file))
5018
5019 (defun gnus-mime-save-part-and-strip (&optional file)
5020   "Save the MIME part under point then replace it with an external body.
5021 If FILE is given, use it for the external part."
5022   (interactive)
5023   (gnus-article-check-buffer)
5024   (when (gnus-group-read-only-p)
5025     (error "The current group does not support deleting of parts"))
5026   (when (mm-complicated-handles gnus-article-mime-handles)
5027     (error "\
5028 The current article has a complicated MIME structure, giving up..."))
5029   (let* ((data (get-text-property (point) 'gnus-data))
5030          (id (get-text-property (point) 'gnus-part))
5031          param
5032          (handles gnus-article-mime-handles))
5033     (unless file
5034       (setq file
5035             (and data (mm-save-part data "Delete MIME part and save to: "))))
5036     (when file
5037       (with-current-buffer (mm-handle-buffer data)
5038         (erase-buffer)
5039         (insert "Content-Type: " (mm-handle-media-type data))
5040         (mml-insert-parameter-string (cdr (mm-handle-type data))
5041                                      '(charset))
5042         ;; Add a filename for the sake of saving the part again.
5043         (mml-insert-parameter
5044          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5045         (insert "\n")
5046         (insert "Content-ID: " (message-make-message-id) "\n")
5047         (insert "Content-Transfer-Encoding: binary\n")
5048         (insert "\n"))
5049       (setcdr data
5050               (cdr (mm-make-handle nil
5051                                    `("message/external-body"
5052                                      (access-type . "LOCAL-FILE")
5053                                      (name . ,file)))))
5054       ;; (set-buffer gnus-summary-buffer)
5055       (gnus-article-edit-part handles id))))
5056
5057 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5058 ;; parts...>') but with stripping would be nice.
5059
5060 (defun gnus-mime-delete-part ()
5061   "Delete the MIME part under point.
5062 Replace it with some information about the removed part."
5063   (interactive)
5064   (gnus-article-check-buffer)
5065   (when (gnus-group-read-only-p)
5066     (error "The current group does not support deleting of parts"))
5067   (when (mm-complicated-handles gnus-article-mime-handles)
5068     (error "\
5069 The current article has a complicated MIME structure, giving up..."))
5070   (when (or gnus-expert-user
5071             (gnus-yes-or-no-p "\
5072 Deleting parts may malfunction or destroy the article; continue? "))
5073     (let* ((data (get-text-property (point) 'gnus-data))
5074            (id (get-text-property (point) 'gnus-part))
5075            (handles gnus-article-mime-handles)
5076            (description
5077             (let ((desc (mm-handle-description data)))
5078               (when desc
5079                 (mail-decode-encoded-word-string desc))))
5080            (filename (or (mm-handle-filename data) "(none)"))
5081            (type (mm-handle-media-type data)))
5082       (unless data
5083         (error "No MIME part under point"))
5084       (with-current-buffer (mm-handle-buffer data)
5085         (let ((bsize (buffer-size)))
5086           (erase-buffer)
5087           (insert
5088            (concat
5089             ",----\n"
5090             "| The following attachment has been deleted:\n"
5091             "|\n"
5092             "| Type:           " type "\n"
5093             "| Filename:       " filename "\n"
5094             "| Size (encoded): " (format "%s byte%s\n"
5095                                          bsize (if (= bsize 1)
5096                                                    ""
5097                                                  "s"))
5098             (when description
5099               (concat    "| Description:    " description "\n"))
5100             "`----\n"))
5101           (setcdr data
5102                   (cdr (mm-make-handle
5103                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5104                         (list "attachment")
5105                         (format "Deleted attachment (%s bytes)" bsize))))))
5106       ;; (set-buffer gnus-summary-buffer)
5107       (gnus-article-edit-part handles id))))
5108
5109 (defun gnus-mime-save-part ()
5110   "Save the MIME part under point."
5111   (interactive)
5112   (gnus-article-check-buffer)
5113   (let ((data (get-text-property (point) 'gnus-data)))
5114     (when data
5115       (mm-save-part data))))
5116
5117 (defun gnus-mime-pipe-part (&optional cmd)
5118   "Pipe the MIME part under point to a process.
5119 Use CMD as the process."
5120   (interactive)
5121   (gnus-article-check-buffer)
5122   (let ((data (get-text-property (point) 'gnus-data)))
5123     (when data
5124       (mm-pipe-part data cmd))))
5125
5126 (defun gnus-mime-view-part ()
5127   "Interactively choose a viewing method for the MIME part under point."
5128   (interactive)
5129   (gnus-article-check-buffer)
5130   (let ((data (get-text-property (point) 'gnus-data)))
5131     (when data
5132       (setq gnus-article-mime-handles
5133             (mm-merge-handles
5134              gnus-article-mime-handles (setq data (copy-sequence data))))
5135       (mm-interactively-view-part data))))
5136
5137 (defun gnus-mime-view-part-as-type-internal ()
5138   (gnus-article-check-buffer)
5139   (let* ((handle (get-text-property (point) 'gnus-data))
5140          (name (or
5141                 ;; Content-Type: foo/bar; name=...
5142                 (mail-content-type-get (mm-handle-type handle) 'name)
5143                 ;; Content-Disposition: attachment; filename=...
5144                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5145          (def-type (and name (mm-default-file-encoding name))))
5146     (or (and def-type (cons def-type 0))
5147         (and handle
5148              (equal (mm-handle-media-supertype handle) "text")
5149              '("text/plain" . 0))
5150         '("application/octet-stream" . 0))))
5151
5152 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5153   "Choose a MIME media type, and view the part as such.
5154 If non-nil, PRED is a predicate to use during completion to limit the
5155 available media-types."
5156   (interactive)
5157   (unless mime-type
5158     (setq mime-type
5159           (let ((default (gnus-mime-view-part-as-type-internal)))
5160             (gnus-completing-read
5161              "View as MIME type"
5162              (if pred
5163                  (gnus-remove-if-not pred (mailcap-mime-types))
5164                (mailcap-mime-types))
5165              nil nil nil
5166              (car default)))))
5167   (gnus-article-check-buffer)
5168   (let ((handle (get-text-property (point) 'gnus-data)))
5169     (when handle
5170       (when (equal (mm-handle-media-type handle) "message/external-body")
5171         (unless (mm-handle-cache handle)
5172           (mm-extern-cache-contents handle))
5173         (setq handle (mm-handle-cache handle)))
5174       (setq handle
5175             (mm-make-handle (mm-handle-buffer handle)
5176                             (cons mime-type (cdr (mm-handle-type handle)))
5177                             (mm-handle-encoding handle)
5178                             (mm-handle-undisplayer handle)
5179                             (mm-handle-disposition handle)
5180                             (mm-handle-description handle)
5181                             nil
5182                             (mm-handle-id handle)))
5183       (setq gnus-article-mime-handles
5184             (mm-merge-handles gnus-article-mime-handles handle))
5185       (when (mm-handle-displayed-p handle)
5186         (mm-remove-part handle))
5187       (gnus-mm-display-part handle))))
5188
5189 (defun gnus-mime-copy-part (&optional handle arg)
5190   "Put the MIME part under point into a new buffer.
5191 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5192 are decompressed."
5193   (interactive (list nil current-prefix-arg))
5194   (gnus-article-check-buffer)
5195   (unless handle
5196     (setq handle (get-text-property (point) 'gnus-data)))
5197   (when handle
5198     (let ((filename (mm-handle-filename handle))
5199           contents dont-decode charset coding-system)
5200       (mm-with-unibyte-buffer
5201         (mm-insert-part handle)
5202         (setq contents (or (condition-case nil
5203                                (mm-decompress-buffer filename nil 'sig)
5204                              (error
5205                               (setq dont-decode t)
5206                               nil))
5207                            (buffer-string))))
5208       (setq filename (cond (filename (file-name-nondirectory filename))
5209                            (dont-decode "*raw data*")
5210                            (t "*decoded*")))
5211       (cond
5212        (dont-decode)
5213        ((not arg)
5214         (unless (setq charset (mail-content-type-get
5215                                (mm-handle-type handle) 'charset))
5216           (unless (setq coding-system (mm-with-unibyte-buffer
5217                                         (insert contents)
5218                                         (mm-find-buffer-file-coding-system)))
5219             (setq charset gnus-newsgroup-charset))))
5220        ((numberp arg)
5221         (setq charset (or (cdr (assq arg
5222                                      gnus-summary-show-article-charset-alist))
5223                           (mm-read-coding-system "Charset: ")))))
5224       (switch-to-buffer (generate-new-buffer filename))
5225       (if (or coding-system
5226               (and charset
5227                    (setq coding-system (mm-charset-to-coding-system charset))
5228                    (not (eq coding-system 'ascii))))
5229           (progn
5230             (mm-enable-multibyte)
5231             (insert (mm-decode-coding-string contents coding-system))
5232             (setq buffer-file-coding-system
5233                   (if (boundp 'last-coding-system-used)
5234                       (symbol-value 'last-coding-system-used)
5235                     coding-system)))
5236         (mm-disable-multibyte)
5237         (insert contents)
5238         (setq buffer-file-coding-system mm-binary-coding-system))
5239       ;; We do it this way to make `normal-mode' set the appropriate mode.
5240       (unwind-protect
5241           (progn
5242             (setq buffer-file-name (expand-file-name filename))
5243             (normal-mode))
5244         (setq buffer-file-name nil))
5245       (goto-char (point-min)))))
5246
5247 (defun gnus-mime-print-part (&optional handle filename)
5248   "Print the MIME part under point."
5249   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5250   (gnus-article-check-buffer)
5251   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5252          (contents (and handle (mm-get-part handle)))
5253          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5254          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5255     (when contents
5256         (if printer
5257             (unwind-protect
5258                 (progn
5259                   (mm-save-part-to-file handle file)
5260                   (call-process shell-file-name nil
5261                                 (generate-new-buffer " *mm*")
5262                                 nil
5263                                 shell-command-switch
5264                                 (mm-mailcap-command
5265                                  printer file (mm-handle-type handle))))
5266               (delete-file file))
5267           (with-temp-buffer
5268             (insert contents)
5269             (gnus-print-buffer))
5270           (ps-despool filename)))))
5271
5272 (defun gnus-mime-inline-part (&optional handle arg)
5273   "Insert the MIME part under point into the current buffer.
5274 Compressed files like .gz and .bz2 are decompressed."
5275   (interactive (list nil current-prefix-arg))
5276   (gnus-article-check-buffer)
5277   (unless handle
5278     (setq handle (get-text-property (point) 'gnus-data)))
5279   (when handle
5280     (let ((b (point))
5281           (inhibit-read-only t)
5282           contents charset coding-system)
5283       (if (and (not arg) (mm-handle-undisplayer handle))
5284           (mm-remove-part handle)
5285         (mm-with-unibyte-buffer
5286           (mm-insert-part handle)
5287           (setq contents
5288                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5289                     (buffer-string))))
5290         (cond
5291          ((not arg)
5292           (unless (setq charset (mail-content-type-get
5293                                  (mm-handle-type handle) 'charset))
5294             (unless (setq coding-system
5295                           (mm-with-unibyte-buffer
5296                             (insert contents)
5297                             (mm-find-buffer-file-coding-system)))
5298               (setq charset gnus-newsgroup-charset))))
5299          ((numberp arg)
5300           (if (mm-handle-undisplayer handle)
5301               (mm-remove-part handle))
5302           (setq charset
5303                 (or (cdr (assq arg
5304                                gnus-summary-show-article-charset-alist))
5305                     (mm-read-coding-system "Charset: "))))
5306          ((mm-handle-undisplayer handle)
5307           (mm-remove-part handle)))
5308         (forward-line 2)
5309         (mm-display-inline handle)
5310         (goto-char b)))))
5311
5312 (defun gnus-mime-set-charset-parameters (handle charset)
5313   "Set CHARSET to parameters in HANDLE.
5314 CHARSET may either be a string or a symbol."
5315   (unless (stringp charset)
5316     (setq charset (symbol-name charset)))
5317   (if (stringp (car handle))
5318       (dolist (h (cdr handle))
5319         (gnus-mime-set-charset-parameters h charset))
5320     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5321                                             "message/external-body")
5322                                      (progn
5323                                        (unless (mm-handle-cache handle)
5324                                          (mm-extern-cache-contents handle))
5325                                        (mm-handle-cache handle))
5326                                    handle)))
5327            (param (assq 'charset (cdr type))))
5328       (if param
5329           (setcdr param charset)
5330         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5331
5332 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5333   "Insert the MIME part under point into the current buffer using the
5334 specified charset."
5335   (interactive (list nil current-prefix-arg))
5336   (gnus-article-check-buffer)
5337   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5338         (fun (get-text-property (point) 'gnus-callback))
5339         (gnus-newsgroup-ignored-charsets 'gnus-all)
5340         charset form preferred parts)
5341     (when handle
5342       (when (prog1
5343                 (and fun
5344                      (setq charset
5345                            (or (cdr (assq
5346                                      arg
5347                                      gnus-summary-show-article-charset-alist))
5348                                (mm-read-coding-system "Charset: "))))
5349               (if (mm-handle-undisplayer handle)
5350                   (mm-remove-part handle)))
5351         (gnus-mime-set-charset-parameters handle charset)
5352         (when (and (consp (setq form (cdr-safe fun)))
5353                    (setq form (ignore-errors
5354                                 (assq 'gnus-mime-display-alternative form)))
5355                    (setq preferred (caddr form))
5356                    (progn
5357                      (when (eq (car preferred) 'quote)
5358                        (setq preferred (cadr preferred)))
5359                      (not (equal preferred
5360                                  (get-text-property (point) 'gnus-data))))
5361                    (setq parts (get-text-property (point) 'gnus-part))
5362                    (setq parts (cdr (assq parts
5363                                           gnus-article-mime-handle-alist)))
5364                    (equal (mm-handle-media-type parts) "multipart/alternative")
5365                    (setq parts (reverse (cdr parts))))
5366           (setcar (cddr form)
5367                   (list 'quote (or (cadr (member preferred parts))
5368                                    (car parts)))))
5369         (funcall fun handle)))))
5370
5371 (defun gnus-mime-view-part-externally (&optional handle)
5372   "View the MIME part under point with an external viewer."
5373   (interactive)
5374   (gnus-article-check-buffer)
5375   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5376          (mm-user-display-methods nil)
5377          (mm-inlined-types nil)
5378          (mail-parse-charset gnus-newsgroup-charset)
5379          (mail-parse-ignored-charsets
5380           (with-current-buffer gnus-summary-buffer
5381             gnus-newsgroup-ignored-charsets))
5382          (type (mm-handle-media-type handle))
5383          (method (mailcap-mime-info type))
5384          (mm-enable-external t))
5385     (if (not (stringp method))
5386         (gnus-mime-view-part-as-type
5387          nil (lambda (type) (stringp (mailcap-mime-info type))))
5388       (when handle
5389         (mm-display-part handle nil t)))))
5390
5391 (defun gnus-mime-view-part-internally (&optional handle)
5392   "View the MIME part under point with an internal viewer.
5393 If no internal viewer is available, use an external viewer."
5394   (interactive)
5395   (gnus-article-check-buffer)
5396   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5397          (mm-inlined-types '(".*"))
5398          (mm-inline-large-images t)
5399          (mail-parse-charset gnus-newsgroup-charset)
5400          (mail-parse-ignored-charsets
5401           (with-current-buffer gnus-summary-buffer
5402             gnus-newsgroup-ignored-charsets))
5403          (inhibit-read-only t))
5404     (if (not (mm-inlinable-p handle))
5405         (gnus-mime-view-part-as-type
5406          nil (lambda (type) (mm-inlinable-p handle type)))
5407       (when handle
5408         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5409
5410 (defun gnus-mime-action-on-part (&optional action)
5411   "Do something with the MIME attachment at \(point\)."
5412   (interactive
5413    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5414   (gnus-article-check-buffer)
5415   (let ((action-pair (assoc action gnus-mime-action-alist)))
5416     (if action-pair
5417         (funcall (cdr action-pair)))))
5418
5419 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5420   "Call FUNCTION on MIME part N.
5421 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5422 If INTERACTIVE, call FUNCTION interactively."
5423   (let (window frame)
5424     ;; Check whether the article is displayed.
5425     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5426                  (setq window (get-buffer-window gnus-article-buffer t))
5427                  (frame-visible-p (setq frame (window-frame window))))
5428       (error "No article is displayed"))
5429     (with-current-buffer gnus-article-buffer
5430       ;; Check whether the article displays the right contents.
5431       (unless (with-current-buffer gnus-summary-buffer
5432                 (eq gnus-current-article (gnus-summary-article-number)))
5433         (error "You should select the right article first"))
5434       (if n
5435           (setq n (prefix-numeric-value n))
5436         (let ((pt (point)))
5437           (setq n (or (get-text-property pt 'gnus-part)
5438                       (and (not (bobp))
5439                            (get-text-property (1- pt) 'gnus-part))
5440                       (get-text-property (prog2
5441                                              (forward-line 1)
5442                                              (point)
5443                                            (goto-char pt))
5444                                          'gnus-part)
5445                       (get-text-property
5446                        (or (and (setq pt (previous-single-property-change
5447                                           pt 'gnus-part))
5448                                 (1- pt))
5449                            (next-single-property-change (point) 'gnus-part)
5450                            (point))
5451                        'gnus-part)
5452                       1))))
5453       ;; Check whether the specified part exists.
5454       (when (> n (length gnus-article-mime-handle-alist))
5455         (error "No such part")))
5456     (unless
5457         (progn
5458           ;; To select the window is needed so that the cursor
5459           ;; might be visible on the MIME button.
5460           (select-window (prog1
5461                              window
5462                            (setq window (selected-window))
5463                            ;; Article may be displayed in the other frame.
5464                            (gnus-select-frame-set-input-focus
5465                             (prog1
5466                                 frame
5467                               (setq frame (selected-frame))))))
5468           (when (gnus-article-goto-part n)
5469             ;; We point the cursor and the arrow at the MIME button
5470             ;; when the `function' prompt the user for something.
5471             (unless (and (pos-visible-in-window-p)
5472                          (> (count-lines (point) (window-end))
5473                             (/ (1- (window-height)) 3)))
5474               (recenter (/ (1- (window-height)) 3)))
5475             (let ((cursor-in-non-selected-windows t)
5476                   (overlay-arrow-string "=>")
5477                   (overlay-arrow-position (point-marker)))
5478               (unwind-protect
5479                   (cond
5480                    ((and no-handle interactive)
5481                     (call-interactively function))
5482                    (no-handle
5483                     (funcall function))
5484                    (interactive
5485                     (call-interactively
5486                      function (get-text-property (point) 'gnus-data)))
5487                    (t
5488                     (funcall function
5489                              (get-text-property (point) 'gnus-data))))
5490                 (set-marker overlay-arrow-position nil)
5491                 (unless gnus-auto-select-part
5492                   (gnus-select-frame-set-input-focus frame)
5493                   (select-window window))))
5494             t))
5495       (if gnus-inhibit-mime-unbuttonizing
5496           ;; This is the default though the program shouldn't reach here.
5497           (error "No such part")
5498         ;; The part which doesn't have the MIME button is selected.
5499         ;; So, we display all the buttons and redo it.
5500         (let ((gnus-inhibit-mime-unbuttonizing t))
5501           (gnus-summary-show-article)
5502           (gnus-article-part-wrapper n function no-handle))))))
5503
5504 (defun gnus-article-pipe-part (n)
5505   "Pipe MIME part N, which is the numerical prefix."
5506   (interactive "P")
5507   (gnus-article-part-wrapper n 'mm-pipe-part))
5508
5509 (defun gnus-article-save-part (n)
5510   "Save MIME part N, which is the numerical prefix."
5511   (interactive "P")
5512   (gnus-article-part-wrapper n 'mm-save-part))
5513
5514 (defun gnus-article-interactively-view-part (n)
5515   "View MIME part N interactively, which is the numerical prefix."
5516   (interactive "P")
5517   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5518
5519 (defun gnus-article-copy-part (n)
5520   "Copy MIME part N, which is the numerical prefix."
5521   (interactive "P")
5522   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5523
5524 (defun gnus-article-view-part-as-charset (n)
5525   "View MIME part N using a specified charset.
5526 N is the numerical prefix."
5527   (interactive "P")
5528   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5529
5530 (defun gnus-article-view-part-externally (n)
5531   "View MIME part N externally, which is the numerical prefix."
5532   (interactive "P")
5533   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5534
5535 (defun gnus-article-inline-part (n)
5536   "Inline MIME part N, which is the numerical prefix."
5537   (interactive "P")
5538   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5539
5540 (defun gnus-article-save-part-and-strip (n)
5541   "Save MIME part N and replace it with an external body.
5542 N is the numerical prefix."
5543   (interactive "P")
5544   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5545
5546 (defun gnus-article-replace-part (n)
5547   "Replace MIME part N with an external body.
5548 N is the numerical prefix."
5549   (interactive "P")
5550   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5551
5552 (defun gnus-article-delete-part (n)
5553   "Delete MIME part N and add some information about the removed part.
5554 N is the numerical prefix."
5555   (interactive "P")
5556   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5557
5558 (defun gnus-article-view-part-as-type (n)
5559   "Choose a MIME media type, and view part N as such.
5560 N is the numerical prefix."
5561   (interactive "P")
5562   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5563
5564 (defun gnus-article-mime-match-handle-first (condition)
5565   (if condition
5566       (let (n)
5567         (dolist (ihandle gnus-article-mime-handle-alist)
5568           (if (and (cond
5569                     ((functionp condition)
5570                      (funcall condition (cdr ihandle)))
5571                     ((eq condition 'undisplayed)
5572                      (not (or (mm-handle-undisplayer (cdr ihandle))
5573                               (equal (mm-handle-media-type (cdr ihandle))
5574                                      "multipart/alternative"))))
5575                     ((eq condition 'undisplayed-alternative)
5576                      (not (mm-handle-undisplayer (cdr ihandle))))
5577                     (t t))
5578                    (gnus-article-goto-part (car ihandle))
5579                    (or (not n) (< (car ihandle) n)))
5580               (setq n (car ihandle))))
5581         (or n 1))
5582     1))
5583
5584 (defun gnus-article-view-part (&optional n)
5585   "View MIME part N, which is the numerical prefix.
5586 If the part is already shown, hide the part.  If N is nil, view
5587 all parts."
5588   (interactive "P")
5589   (with-current-buffer gnus-article-buffer
5590     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5591                              gnus-article-mime-match-handle-function)))
5592     (when (> n (length gnus-article-mime-handle-alist))
5593       (error "No such part"))
5594     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5595       (when (gnus-article-goto-part n)
5596         (if (equal (car handle) "multipart/alternative")
5597             (progn
5598               (beginning-of-line) ;; Make it toggle subparts
5599               (gnus-article-press-button))
5600           (when (eq (gnus-mm-display-part handle) 'internal)
5601             (gnus-set-window-start)))))))
5602
5603 (defsubst gnus-article-mime-total-parts ()
5604   (if (bufferp (car gnus-article-mime-handles))
5605       1 ;; single part
5606     (1- (length gnus-article-mime-handles))))
5607
5608 (defun gnus-mm-display-part (handle)
5609   "Display HANDLE and fix MIME button."
5610   (let ((id (get-text-property (point) 'gnus-part))
5611         (point (point))
5612         (inhibit-read-only t))
5613     (forward-line 1)
5614     (prog1
5615         (let ((window (selected-window))
5616               (mail-parse-charset gnus-newsgroup-charset)
5617               (mail-parse-ignored-charsets
5618                (if (gnus-buffer-live-p gnus-summary-buffer)
5619                    (with-current-buffer gnus-summary-buffer
5620                      gnus-newsgroup-ignored-charsets)
5621                  nil)))
5622           (save-excursion
5623             (unwind-protect
5624                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5625                       (beg (point)))
5626                   (when win
5627                     (select-window win))
5628                   (goto-char point)
5629                   (forward-line)
5630                   (if (mm-handle-displayed-p handle)
5631                       ;; This will remove the part.
5632                       (mm-display-part handle)
5633                     (save-restriction
5634                       (narrow-to-region (point)
5635                                         (if (eobp) (point) (1+ (point))))
5636                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5637                       ;; We narrow to the part itself and
5638                       ;; then call the treatment functions.
5639                       (goto-char (point-min))
5640                       (forward-line 1)
5641                       (narrow-to-region (point) (point-max))
5642                       (gnus-treat-article
5643                        nil id
5644                        (gnus-article-mime-total-parts)
5645                        (mm-handle-media-type handle)))))
5646               (if (window-live-p window)
5647                   (select-window window)))))
5648       (goto-char point)
5649       (gnus-delete-line)
5650       (gnus-insert-mime-button
5651        handle id (list (mm-handle-displayed-p handle)))
5652       (goto-char point))))
5653
5654 (defun gnus-article-goto-part (n)
5655   "Go to MIME part N."
5656   (when gnus-break-pages
5657     (widen))
5658   (prog1
5659       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5660             part handle end next handles)
5661         (when start
5662           (goto-char start)
5663           (if (setq handle (get-text-property start 'gnus-data))
5664               start
5665             ;; Go to the displayed subpart, assuming this is
5666             ;; multipart/alternative.
5667             (setq part start
5668                   end (point-at-eol))
5669             (while (and (not handle)
5670                         part
5671                         (< part end)
5672                         (setq next (text-property-not-all part end
5673                                                           'gnus-data nil)))
5674               (setq part next
5675                     handle (get-text-property part 'gnus-data))
5676               (push (cons handle part) handles)
5677               (unless (mm-handle-displayed-p handle)
5678                 (setq handle nil
5679                       part (text-property-any part end 'gnus-data nil))))
5680             (unless handle
5681               ;; No subpart is displayed, so we find preferred one.
5682               (setq part
5683                     (cdr (assq (mm-preferred-alternative
5684                                 (nreverse (mapcar 'car handles)))
5685                                handles))))
5686             (if part
5687                 (goto-char (1+ part))
5688               start))))
5689     (when gnus-break-pages
5690       (gnus-narrow-to-page))))
5691
5692 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5693   (let ((gnus-tmp-name
5694          (or (mm-handle-filename handle)
5695              (mail-content-type-get (mm-handle-type handle) 'url)
5696              ""))
5697         (gnus-tmp-type (mm-handle-media-type handle))
5698         (gnus-tmp-description (or (mm-handle-description handle) ""))
5699         (gnus-tmp-dots
5700          (if (if displayed (car displayed)
5701                (mm-handle-displayed-p handle))
5702              "" "..."))
5703         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5704                            (buffer-size)))
5705         gnus-tmp-type-long b e)
5706     (when (string-match ".*/" gnus-tmp-name)
5707       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5708     (setq gnus-tmp-type-long (concat gnus-tmp-type
5709                                      (and (not (equal gnus-tmp-name ""))
5710                                           (concat "; " gnus-tmp-name))))
5711     (unless (equal gnus-tmp-description "")
5712       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5713     (unless (bolp)
5714       (insert "\n"))
5715     (setq b (point))
5716     (gnus-eval-format
5717      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5718      `(keymap ,gnus-mime-button-map
5719               gnus-callback gnus-mm-display-part
5720               gnus-part ,gnus-tmp-id
5721               article-type annotation
5722               gnus-data ,handle
5723               rear-nonsticky t))
5724     (setq e (if (bolp)
5725                 ;; Exclude a newline.
5726                 (1- (point))
5727               (point)))
5728     (when gnus-article-button-face
5729       (gnus-overlay-put (gnus-make-overlay b e nil t)
5730                         'face gnus-article-button-face))
5731     (widget-convert-button
5732      'link b e
5733      :mime-handle handle
5734      :action 'gnus-widget-press-button
5735      :button-keymap gnus-mime-button-map
5736      :help-echo
5737      (lambda (widget)
5738        ;; Needed to properly clear the message due to a bug in
5739        ;; wid-edit (XEmacs only).
5740        (if (boundp 'help-echo-owns-message)
5741            (setq help-echo-owns-message t))
5742        (format
5743         "%S: %s the MIME part; %S: more options"
5744         (aref gnus-mouse-2 0)
5745         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5746             "hide" "show")
5747         (aref gnus-down-mouse-3 0))))))
5748
5749 (defun gnus-widget-press-button (elems el)
5750   (goto-char (widget-get elems :from))
5751   (gnus-article-press-button))
5752
5753 (defvar gnus-displaying-mime nil)
5754
5755 (defun gnus-display-mime (&optional ihandles)
5756   "Display the MIME parts."
5757   (save-excursion
5758     (save-selected-window
5759       (let ((window (get-buffer-window gnus-article-buffer))
5760             (point (point)))
5761         (when window
5762           (select-window window)
5763           ;; We have to do this since selecting the window
5764           ;; may change the point.  So we set the window point.
5765           (set-window-point window point)))
5766       (let ((handles ihandles)
5767             (inhibit-read-only t)
5768             handle)
5769         (cond (handles)
5770               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5771                (when gnus-article-emulate-mime
5772                  (mm-uu-dissect-text-parts handles)))
5773               (gnus-article-emulate-mime
5774                (setq handles (mm-uu-dissect))))
5775         (when (and (not ihandles)
5776                    (not gnus-displaying-mime))
5777           ;; Top-level call; we clean up.
5778           (when gnus-article-mime-handles
5779             (mm-destroy-parts gnus-article-mime-handles)
5780             (setq gnus-article-mime-handle-alist nil));; A trick.
5781           (setq gnus-article-mime-handles handles)
5782           ;; We allow users to glean info from the handles.
5783           (when gnus-article-mime-part-function
5784             (gnus-mime-part-function handles)))
5785         (if (and handles
5786                  (or (not (stringp (car handles)))
5787                      (cdr handles)))
5788             (progn
5789               (when (and (not ihandles)
5790                          (not gnus-displaying-mime))
5791                 ;; Clean up for mime parts.
5792                 (article-goto-body)
5793                 (delete-region (point) (point-max)))
5794               (let ((gnus-displaying-mime t))
5795                 (gnus-mime-display-part handles)))
5796           (save-restriction
5797             (article-goto-body)
5798             (narrow-to-region (point) (point-max))
5799             (gnus-treat-article nil 1 1 "text/plain")
5800             (widen)))
5801         (unless ihandles
5802           ;; Highlight the headers.
5803           (save-excursion
5804             (save-restriction
5805               (article-goto-body)
5806               (narrow-to-region (point-min) (point))
5807               (gnus-article-save-original-date
5808                (gnus-treat-article 'head)))))))
5809     ;; Cope with broken MIME messages.
5810     (goto-char (point-max))
5811     (unless (bolp)
5812       (insert "\n"))))
5813
5814 (defcustom gnus-mime-display-multipart-as-mixed nil
5815   "Display \"multipart\" parts as  \"multipart/mixed\".
5816
5817 If t, it overrides nil values of
5818 `gnus-mime-display-multipart-alternative-as-mixed' and
5819 `gnus-mime-display-multipart-related-as-mixed'."
5820   :group 'gnus-article-mime
5821   :type 'boolean)
5822
5823 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5824   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5825   :version "22.1"
5826   :group 'gnus-article-mime
5827   :type 'boolean)
5828
5829 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5830   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5831
5832 If displaying \"text/html\" is discouraged \(see
5833 `mm-discouraged-alternatives'\) images or other material inside a
5834 \"multipart/related\" part might be overlooked when this variable is nil."
5835   :version "22.1"
5836   :group 'gnus-article-mime
5837   :type 'boolean)
5838
5839 (defun gnus-mime-display-part (handle)
5840   (cond
5841    ;; Maybe a broken MIME message.
5842    ((null handle))
5843    ;; Single part.
5844    ((not (stringp (car handle)))
5845     (gnus-mime-display-single handle))
5846    ;; User-defined multipart
5847    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5848     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5849              handle))
5850    ;; multipart/alternative
5851    ((and (equal (car handle) "multipart/alternative")
5852          (not (or gnus-mime-display-multipart-as-mixed
5853                   gnus-mime-display-multipart-alternative-as-mixed)))
5854     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5855       (push (cons id handle) gnus-article-mime-handle-alist)
5856       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5857    ;; multipart/related
5858    ((and (equal (car handle) "multipart/related")
5859          (not (or gnus-mime-display-multipart-as-mixed
5860                   gnus-mime-display-multipart-related-as-mixed)))
5861     ;;;!!!We should find the start part, but we just default
5862     ;;;!!!to the first part.
5863     ;;(gnus-mime-display-part (cadr handle))
5864     ;;;!!! Most multipart/related is an HTML message plus images.
5865     ;;;!!! Unfortunately we are unable to let W3 display those
5866     ;;;!!! included images, so we just display it as a mixed multipart.
5867     ;;(gnus-mime-display-mixed (cdr handle))
5868     ;;;!!! No, w3 can display everything just fine.
5869     (gnus-mime-display-part (cadr handle)))
5870    ((equal (car handle) "multipart/signed")
5871     (gnus-add-wash-type 'signed)
5872     (gnus-mime-display-security handle))
5873    ((equal (car handle) "multipart/encrypted")
5874     (gnus-add-wash-type 'encrypted)
5875     (gnus-mime-display-security handle))
5876    ;; Other multiparts are handled like multipart/mixed.
5877    (t
5878     (gnus-mime-display-mixed (cdr handle)))))
5879
5880 (defun gnus-mime-part-function (handles)
5881   (if (stringp (car handles))
5882       (mapcar 'gnus-mime-part-function (cdr handles))
5883     (funcall gnus-article-mime-part-function handles)))
5884
5885 (defun gnus-mime-display-mixed (handles)
5886   (mapcar 'gnus-mime-display-part handles))
5887
5888 (defun gnus-mime-display-single (handle)
5889   (let ((type (mm-handle-media-type handle))
5890         (ignored gnus-ignored-mime-types)
5891         (not-attachment t)
5892         (move nil)
5893         display text)
5894     (catch 'ignored
5895       (progn
5896         (while ignored
5897           (when (string-match (pop ignored) type)
5898             (throw 'ignored nil)))
5899         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5900                                (with-current-buffer gnus-summary-buffer
5901                                  gnus-inhibit-images)
5902                              gnus-inhibit-images)
5903                            (string-match "\\`image/" type)))
5904                  (setq not-attachment
5905                        (and (not (mm-inline-override-p handle))
5906                             (or (not (mm-handle-disposition handle))
5907                                 (equal (car (mm-handle-disposition handle))
5908                                        "inline")
5909                                 (mm-attachment-override-p handle))))
5910                  (mm-automatic-display-p handle)
5911                  (or (and
5912                       (mm-inlinable-p handle)
5913                       (mm-inlined-p handle))
5914                      (mm-automatic-external-display-p type)))
5915             (setq display t)
5916           (when (equal (mm-handle-media-supertype handle) "text")
5917             (setq text t)))
5918         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5919               beg)
5920           (push (cons id handle) gnus-article-mime-handle-alist)
5921           (when (and display
5922                      (equal (mm-handle-media-supertype handle) "message"))
5923             (insert-char
5924              ?\n
5925              (cond ((not (bolp)) 2)
5926                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5927                    (t 1))))
5928           (when (or (not display)
5929                     (not (gnus-unbuttonized-mime-type-p type))
5930                     (eq id gnus-mime-buttonized-part-id))
5931             (gnus-insert-mime-button
5932              handle id (list (or display (and not-attachment text))))
5933             (gnus-article-insert-newline)
5934             ;; Remember modify the number of forward lines.
5935             (setq move t))
5936           (setq beg (point))
5937           (cond
5938            (display
5939             (when move
5940               (forward-line -1)
5941               (setq beg (point)))
5942             (let ((mail-parse-charset gnus-newsgroup-charset)
5943                   (mail-parse-ignored-charsets
5944                    (save-excursion (condition-case ()
5945                                        (set-buffer gnus-summary-buffer)
5946                                      (error))
5947                                    gnus-newsgroup-ignored-charsets)))
5948               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5949             (goto-char (point-max)))
5950            ((and text not-attachment)
5951             (when move
5952               (forward-line -1)
5953               (setq beg (point)))
5954             (gnus-article-insert-newline)
5955             (mm-display-inline handle)
5956             (goto-char (point-max))))
5957           ;; Do highlighting.
5958           (save-excursion
5959             (save-restriction
5960               (narrow-to-region beg (point))
5961               (if (eq handle gnus-article-mime-handles)
5962                   ;; The format=flowed case.
5963                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5964                 ;; Don't count signature parts that are never displayed.
5965                 ;; The part number should be re-calculated supposing this
5966                 ;; might be a message/rfc822 part.
5967                 (let (handles)
5968                   (dolist (part gnus-article-mime-handles)
5969                     (unless (or (stringp part)
5970                                 (equal (car (mm-handle-type part))
5971                                        "application/pgp-signature"))
5972                       (push part handles)))
5973                   (gnus-treat-article
5974                    nil (length (memq handle handles)) (length handles)
5975                    (mm-handle-media-type handle)))))))))))
5976
5977 (defun gnus-unbuttonized-mime-type-p (type)
5978   "Say whether TYPE is to be unbuttonized."
5979   (unless gnus-inhibit-mime-unbuttonizing
5980     (when (catch 'found
5981             (let ((types gnus-unbuttonized-mime-types))
5982               (while types
5983                 (when (string-match (pop types) type)
5984                   (throw 'found t)))))
5985       (not (catch 'found
5986              (let ((types gnus-buttonized-mime-types))
5987                (while types
5988                  (when (string-match (pop types) type)
5989                    (throw 'found t)))))))))
5990
5991 (defun gnus-article-insert-newline ()
5992   "Insert a newline, but mark it as undeletable."
5993   (gnus-put-text-property
5994    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5995
5996 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5997   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5998          (ihandles handles)
5999          (point (point))
6000          handle (inhibit-read-only t) from props begend not-pref)
6001     (save-window-excursion
6002       (save-restriction
6003         (when ibegend
6004           (narrow-to-region (car ibegend)
6005                             (or (cdr ibegend)
6006                                 (progn
6007                                   (goto-char (car ibegend))
6008                                   (forward-line 2)
6009                                   (point))))
6010           (delete-region (point-min) (point-max))
6011           (mm-remove-parts handles))
6012         (setq begend (list (point-marker)))
6013         ;; Do the toggle.
6014         (unless (setq not-pref (cadr (member preferred ihandles)))
6015           (setq not-pref (car ihandles)))
6016         (when (or ibegend
6017                   (not preferred)
6018                   (not (gnus-unbuttonized-mime-type-p
6019                         "multipart/alternative")))
6020           (gnus-add-text-properties
6021            (setq from (point))
6022            (progn
6023              (insert (format "%d.  " id))
6024              (point))
6025            `(gnus-callback
6026              (lambda (handles)
6027                (unless ,(not ibegend)
6028                  (setq gnus-article-mime-handle-alist
6029                        ',gnus-article-mime-handle-alist))
6030                (gnus-mime-display-alternative
6031                 ',ihandles ',not-pref ',begend ,id))
6032              keymap ,gnus-mime-button-map
6033              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6034              face ,gnus-article-button-face
6035              gnus-part ,id
6036              article-type multipart
6037              rear-nonsticky t))
6038           (widget-convert-button 'link from (point)
6039                                  :action 'gnus-widget-press-button
6040                                  :button-keymap gnus-widget-button-keymap)
6041           ;; Do the handles
6042           (while (setq handle (pop handles))
6043             (gnus-add-text-properties
6044              (setq from (point))
6045              (progn
6046                (insert (format "(%c) %-18s"
6047                                (if (equal handle preferred) ?* ? )
6048                                (mm-handle-media-type handle)))
6049                (point))
6050              `(gnus-callback
6051                (lambda (handles)
6052                  (unless ,(not ibegend)
6053                    (setq gnus-article-mime-handle-alist
6054                          ',gnus-article-mime-handle-alist))
6055                  (gnus-mime-display-alternative
6056                   ',ihandles ',handle ',begend ,id))
6057                keymap ,gnus-mime-button-map
6058                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6059                face ,gnus-article-button-face
6060                gnus-part ,id
6061                gnus-data ,handle
6062                rear-nonsticky t))
6063             (widget-convert-button 'link from (point)
6064                                    :action 'gnus-widget-press-button
6065                                    :button-keymap gnus-widget-button-keymap)
6066             (insert "  "))
6067           (insert "\n\n"))
6068         (when preferred
6069           (if (stringp (car preferred))
6070               (gnus-display-mime preferred)
6071             (let ((mail-parse-charset gnus-newsgroup-charset)
6072                   (mail-parse-ignored-charsets
6073                    (with-current-buffer gnus-summary-buffer
6074                      gnus-newsgroup-ignored-charsets)))
6075               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6076               ;; Do highlighting.
6077               (save-excursion
6078                 (save-restriction
6079                   (narrow-to-region (car begend) (point-max))
6080                   (gnus-treat-article
6081                    nil (length gnus-article-mime-handle-alist)
6082                    (gnus-article-mime-total-parts)
6083                    (mm-handle-media-type preferred))))))
6084           (goto-char (point-max))
6085           (setcdr begend (point-marker)))))
6086     (when ibegend
6087       (goto-char point))))
6088
6089 (defconst gnus-article-wash-status-strings
6090   (let ((alist '((cite "c" "Possible hidden citation text"
6091                        " " "All citation text visible")
6092                  (headers "h" "Hidden headers"
6093                           " " "All headers visible.")
6094                  (pgp "p" "Encrypted or signed message status hidden"
6095                       " " "No hidden encryption nor digital signature status")
6096                  (signature "s" "Signature has been hidden"
6097                             " " "Signature is visible")
6098                  (overstrike "o" "Overstrike (^H) characters applied"
6099                              " " "No overstrike characters applied")
6100                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6101                            " " "No /*_emphasis_*/ characters applied")))
6102         result)
6103     (dolist (entry alist result)
6104       (let ((key (nth 0 entry))
6105             (on (copy-sequence (nth 1 entry)))
6106             (on-help (nth 2 entry))
6107             (off (copy-sequence (nth 3 entry)))
6108             (off-help (nth 4 entry)))
6109         (put-text-property 0 1 'help-echo on-help on)
6110         (put-text-property 0 1 'help-echo off-help off)
6111         (push (list key on off) result))))
6112   "Alist of strings describing wash status in the mode line.
6113 Each entry has the form (KEY ON OF), where the KEY is a symbol
6114 representing the particular washing function, ON is the string to use
6115 in the article mode line when the washing function is active, and OFF
6116 is the string to use when it is inactive.")
6117
6118 (defun gnus-article-wash-status-entry (key value)
6119   (let ((entry (assoc key gnus-article-wash-status-strings)))
6120     (if value (nth 1 entry) (nth 2 entry))))
6121
6122 (defun gnus-article-wash-status ()
6123   "Return a string which display status of article washing."
6124   (with-current-buffer gnus-article-buffer
6125     (let ((cite (memq 'cite gnus-article-wash-types))
6126           (headers (memq 'headers gnus-article-wash-types))
6127           (boring (memq 'boring-headers gnus-article-wash-types))
6128           (pgp (memq 'pgp gnus-article-wash-types))
6129           (pem (memq 'pem gnus-article-wash-types))
6130           (signed (memq 'signed gnus-article-wash-types))
6131           (encrypted (memq 'encrypted gnus-article-wash-types))
6132           (signature (memq 'signature gnus-article-wash-types))
6133           (overstrike (memq 'overstrike gnus-article-wash-types))
6134           (emphasis (memq 'emphasis gnus-article-wash-types)))
6135       (concat
6136        (gnus-article-wash-status-entry 'cite cite)
6137        (gnus-article-wash-status-entry 'headers (or headers boring))
6138        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6139        (gnus-article-wash-status-entry 'signature signature)
6140        (gnus-article-wash-status-entry 'overstrike overstrike)
6141        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6142
6143 (defun gnus-add-wash-type (type)
6144   "Add a washing of TYPE to the current status."
6145   (add-to-list 'gnus-article-wash-types type))
6146
6147 (defun gnus-delete-wash-type (type)
6148   "Add a washing of TYPE to the current status."
6149   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6150
6151 (defun gnus-add-image (category image)
6152   "Add IMAGE of CATEGORY to the list of displayed images."
6153   (let ((entry (assq category gnus-article-image-alist)))
6154     (unless entry
6155       (setq entry (list category))
6156       (push entry gnus-article-image-alist))
6157     (nconc entry (list image))))
6158
6159 (defun gnus-delete-images (category)
6160   "Delete all images in CATEGORY."
6161   (let ((entry (assq category gnus-article-image-alist)))
6162     (dolist (image (cdr entry))
6163       (gnus-remove-image image category))
6164     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6165     (gnus-delete-wash-type category)))
6166
6167 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6168
6169 (defun gnus-article-maybe-hide-headers ()
6170   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6171 Provided for backwards compatibility."
6172   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6173                  (not (with-current-buffer gnus-summary-buffer
6174                         gnus-have-all-headers)))
6175              (not gnus-inhibit-hiding))
6176     (gnus-article-hide-headers)))
6177
6178 (declare-function shr-put-image "shr" (data alt &optional flags))
6179
6180 (defun gnus-shr-put-image (data alt &optional flags)
6181   "Put image DATA with a string ALT.  Enable image to be deleted."
6182   (let ((image (shr-put-image data (propertize (or alt "*")
6183                                                'gnus-image-category 'shr)
6184                               flags)))
6185     (when image
6186       (gnus-add-image 'shr image))))
6187
6188 ;;; Article savers.
6189
6190 (defun gnus-output-to-file (file-name)
6191   "Append the current article to a file named FILE-NAME.
6192 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6193 text and used as the value of the coding cookie which is added to the
6194 top of a file.  Otherwise, this function saves a raw article without
6195 the coding cookie."
6196   (let* ((artbuf (current-buffer))
6197          (file-name-coding-system nnmail-pathname-coding-system)
6198          (coding gnus-article-save-coding-system)
6199          (coding-system-for-read (if coding
6200                                      nil ;; Rely on the coding cookie.
6201                                    mm-text-coding-system))
6202          (coding-system-for-write (or coding
6203                                       mm-text-coding-system-for-write
6204                                       mm-text-coding-system))
6205          (exists (file-exists-p file-name)))
6206     (with-temp-buffer
6207       (when exists
6208         (insert-file-contents file-name)
6209         (goto-char (point-min))
6210         ;; Remove the existing coding cookie.
6211         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6212           (delete-region (match-beginning 0) (match-end 0))))
6213       (goto-char (point-max))
6214       (insert-buffer-substring artbuf)
6215       ;; Append newline at end of the buffer as separator, and then
6216       ;; save it to file.
6217       (goto-char (point-max))
6218       (insert "\n")
6219       (when coding
6220         ;; If the coding system is not suitable to encode the text,
6221         ;; ask a user for a proper one.
6222         (when (fboundp 'select-safe-coding-system)
6223           (setq coding (coding-system-base
6224                         (save-window-excursion
6225                           (select-safe-coding-system (point-min) (point-max)
6226                                                      coding))))
6227           (setq coding-system-for-write
6228                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6229                     coding)))
6230         (goto-char (point-min))
6231         ;; Add the coding cookie.
6232         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6233                         coding-system-for-write)))
6234       (if exists
6235           (progn
6236             (write-region (point-min) (point-max) file-name nil 'no-message)
6237             (message "Appended to %s" file-name))
6238         (write-region (point-min) (point-max) file-name))))
6239   t)
6240
6241 (defun gnus-narrow-to-page (&optional arg)
6242   "Narrow the article buffer to a page.
6243 If given a numerical ARG, move forward ARG pages."
6244   (interactive "P")
6245   (setq arg (if arg (prefix-numeric-value arg) 0))
6246   (with-current-buffer gnus-article-buffer
6247     (widen)
6248     ;; Remove any old next/prev buttons.
6249     (when (gnus-visual-p 'page-marker)
6250       (let ((inhibit-read-only t))
6251         (gnus-remove-text-with-property 'gnus-prev)
6252         (gnus-remove-text-with-property 'gnus-next)))
6253     (let (st nd pt)
6254       (when (save-excursion
6255               (cond ((< arg 0)
6256                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6257                          (prog1
6258                              (setq nd (match-beginning 0)
6259                                    pt nd)
6260                            (when (re-search-backward page-delimiter nil t)
6261                              (setq st (match-end 0))))
6262                        (when (re-search-forward page-delimiter nil t)
6263                          (setq nd (match-beginning 0)
6264                                pt (point-min)))))
6265                     ((> arg 0)
6266                      (if (re-search-forward page-delimiter nil 'move arg)
6267                          (prog1
6268                              (setq st (match-end 0)
6269                                    pt st)
6270                            (when (re-search-forward page-delimiter nil t)
6271                              (setq nd (match-beginning 0))))
6272                        (when (re-search-backward page-delimiter nil t)
6273                          (setq st (match-end 0)
6274                                pt (point-max)))))
6275                     (t
6276                      (when (re-search-backward page-delimiter nil t)
6277                        (goto-char (setq st (match-end 0))))
6278                      (when (re-search-forward page-delimiter nil t)
6279                        (setq nd (match-beginning 0)))
6280                      (or st nd))))
6281         (setq gnus-page-broken t)
6282         (when pt (goto-char pt))
6283         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6284         (when (gnus-visual-p 'page-marker)
6285           (save-excursion
6286             (when nd
6287               (goto-char nd)
6288               (gnus-insert-next-page-button))
6289             (when st
6290               (goto-char st)
6291               (gnus-insert-prev-page-button))))))))
6292
6293 ;; Article mode commands
6294
6295 (defun gnus-article-goto-next-page ()
6296   "Show the next page of the article."
6297   (interactive)
6298   (when (gnus-article-next-page)
6299     (goto-char (point-min))
6300     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6301
6302
6303 (defun gnus-article-goto-prev-page ()
6304   "Show the previous page of the article."
6305   (interactive)
6306   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6307       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6308     (gnus-article-prev-page nil)))
6309
6310 ;; This is cleaner but currently breaks `gnus-pick-mode':
6311 ;;
6312 ;; (defun gnus-article-goto-next-page ()
6313 ;;   "Show the next page of the article."
6314 ;;   (interactive)
6315 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6316 ;;     (gnus-summary-next-page)))
6317 ;;
6318 ;; (defun gnus-article-goto-prev-page ()
6319 ;;   "Show the next page of the article."
6320 ;;   (interactive)
6321 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6322 ;;     (gnus-summary-prev-page)))
6323
6324 (defun gnus-article-next-page (&optional lines)
6325   "Show the next page of the current article.
6326 If end of article, return non-nil.  Otherwise return nil.
6327 Argument LINES specifies lines to be scrolled up."
6328   (interactive "p")
6329   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6330   (if (and (not (and gnus-article-over-scroll
6331                      (> (count-lines (window-start) (point-max))
6332                         (if (featurep 'xemacs)
6333                             (or lines (1- (window-height)))
6334                           (+ (or lines (1- (window-height))) scroll-margin)))))
6335            (save-excursion
6336              (end-of-line)
6337              (and (pos-visible-in-window-p)     ;Not continuation line.
6338                   (>= (point) (point-max)))))
6339       ;; Nothing in this page.
6340       (if (or (not gnus-page-broken)
6341               (save-excursion
6342                 (save-restriction
6343                   (widen)
6344                   (forward-line)
6345                   (eobp)))) ;Real end-of-buffer?
6346           (progn
6347             (when gnus-article-over-scroll
6348               (gnus-article-next-page-1 lines))
6349             t)                  ;Nothing more.
6350         (gnus-narrow-to-page 1)         ;Go to next page.
6351         nil)
6352     ;; More in this page.
6353     (gnus-article-next-page-1 lines)
6354     nil))
6355
6356 (defun gnus-article-beginning-of-window ()
6357   "Move point to the beginning of the window.
6358 In Emacs, the point is placed at the line number which `scroll-margin'
6359 specifies."
6360   (if (featurep 'xemacs)
6361       (move-to-window-line 0)
6362     ;; There is an obscure bug in Emacs that makes it impossible to
6363     ;; scroll past big pictures in the article buffer.  Try to fix
6364     ;; this by adding a sanity check by counting the lines visible.
6365     (when (> (count-lines (window-start) (window-end)) 30)
6366       (move-to-window-line
6367        (min (max 0 scroll-margin)
6368             (max 1 (- (window-height)
6369                       (if mode-line-format 1 0)
6370                       (if header-line-format 1 0)
6371                       2)))))))
6372
6373 (defun gnus-article-next-page-1 (lines)
6374   (condition-case ()
6375       (let ((scroll-in-place nil)
6376             (auto-window-vscroll nil))
6377         (scroll-up lines))
6378     (end-of-buffer
6379      ;; Long lines may cause an end-of-buffer error.
6380      (goto-char (point-max))))
6381   (gnus-article-beginning-of-window))
6382
6383 (defun gnus-article-prev-page (&optional lines)
6384   "Show previous page of current article.
6385 Argument LINES specifies lines to be scrolled down."
6386   (interactive "p")
6387   (move-to-window-line 0)
6388   (if (and gnus-page-broken
6389            (bobp)
6390            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6391       (progn
6392         (gnus-narrow-to-page -1)        ;Go to previous page.
6393         (goto-char (point-max))
6394         (recenter (if gnus-article-over-scroll
6395                       (if lines
6396                           (max (if (featurep 'xemacs)
6397                                    lines
6398                                  (+ lines scroll-margin))
6399                                3)
6400                         (- (window-height) 2))
6401                     -1)))
6402     (prog1
6403         (condition-case ()
6404             (let ((scroll-in-place nil))
6405               (scroll-down lines))
6406           (beginning-of-buffer
6407            (goto-char (point-min))))
6408       (gnus-article-beginning-of-window))))
6409
6410 (defun gnus-article-only-boring-p ()
6411   "Decide whether there is only boring text remaining in the article.
6412 Something \"interesting\" is a word of at least two letters that does
6413 not have a face in `gnus-article-boring-faces'."
6414   (when (and gnus-article-skip-boring
6415              (boundp 'gnus-article-boring-faces)
6416              (symbol-value 'gnus-article-boring-faces))
6417     (save-excursion
6418       (let ((inhibit-point-motion-hooks t))
6419         (catch 'only-boring
6420           (while (re-search-forward "\\b\\w\\w" nil t)
6421             (forward-char -1)
6422             (when (not (gnus-intersection
6423                         (gnus-faces-at (point))
6424                         (symbol-value 'gnus-article-boring-faces)))
6425               (throw 'only-boring nil)))
6426           (throw 'only-boring t))))))
6427
6428 (defun gnus-article-refer-article ()
6429   "Read article specified by message-id around point."
6430   (interactive)
6431   (save-excursion
6432     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6433     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6434     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6435         (let ((msg-id (concat "<" (match-string 0) ">")))
6436           (set-buffer gnus-summary-buffer)
6437           (gnus-summary-refer-article msg-id))
6438       (error "No references around point"))))
6439
6440 (defun gnus-article-show-summary ()
6441   "Reconfigure windows to show summary buffer."
6442   (interactive)
6443   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6444       (error "There is no summary buffer for this article buffer")
6445     (gnus-article-set-globals)
6446     (gnus-configure-windows 'article)
6447     (gnus-summary-goto-subject gnus-current-article)
6448     (gnus-summary-position-point)))
6449
6450 (defun gnus-article-describe-briefly ()
6451   "Describe article mode commands briefly."
6452   (interactive)
6453   (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")))
6454
6455 (defun gnus-article-check-buffer ()
6456   "Beep if not in an article buffer."
6457   (unless (equal major-mode 'gnus-article-mode)
6458     (error "Command invoked outside of a Gnus article buffer")))
6459
6460 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6461   "Read a summary buffer key sequence and execute it from the article buffer."
6462   (interactive "P")
6463   (gnus-article-check-buffer)
6464   (let ((nosaves
6465          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6466            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6467            "=" "^" "\M-^" "|"))
6468         (nosave-but-article
6469          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6470            "An" "Ap" [?A (meta return)] [?A delete]))
6471         (nosave-in-article
6472          '("AS" "\C-d"))
6473         (up-to-top
6474          '("n" "Gn" "p" "Gp"))
6475         keys new-sum-point)
6476     (with-current-buffer gnus-article-current-summary
6477       (let (gnus-pick-mode)
6478         (setq unread-command-events (nconc unread-command-events
6479                                            (list (or key last-command-event)))
6480               keys (if (featurep 'xemacs)
6481                        (events-to-keys (read-key-sequence nil t))
6482                      (read-key-sequence nil t)))))
6483
6484     (message "")
6485
6486     (cond
6487      ((eq (aref keys (1- (length keys))) ?\C-h)
6488       (gnus-article-describe-bindings (substring keys 0 -1)))
6489      ((or (member keys nosaves)
6490           (member keys nosave-but-article)
6491           (member keys nosave-in-article))
6492       (let (func)
6493         (save-window-excursion
6494           (pop-to-buffer gnus-article-current-summary)
6495           ;; We disable the pick minor mode commands.
6496           (let (gnus-pick-mode)
6497             (setq func (key-binding keys t))))
6498         (if (or (not func)
6499                 (numberp func))
6500             (ding)
6501           (unless (member keys nosave-in-article)
6502             (set-buffer gnus-article-current-summary))
6503           (when (and (symbolp func)
6504                      (get func 'disabled))
6505             (error "Function %s disabled" func))
6506           (call-interactively func)
6507           (setq new-sum-point (point)))
6508         (when (member keys nosave-but-article)
6509           (pop-to-buffer gnus-article-buffer))))
6510      (t
6511       ;; These commands should restore window configuration.
6512       (let ((obuf (current-buffer))
6513             (owin (current-window-configuration))
6514             win func in-buffer selected new-sum-start new-sum-hscroll err)
6515         (cond (not-restore-window
6516                (pop-to-buffer gnus-article-current-summary)
6517                (setq win (selected-window)))
6518               ((setq win (get-buffer-window gnus-article-current-summary))
6519                (select-window win))
6520               (t
6521                (let ((summary-buffer gnus-article-current-summary))
6522                  (gnus-configure-windows 'article)
6523                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6524                    (let ((gnus-buffer-configuration
6525                           '((article ((vertical 1.0
6526                                                 (summary 0.25 point)
6527                                                 (article 1.0)))))))
6528                      (gnus-configure-windows 'article))
6529                    (setq win (get-buffer-window summary-buffer 'visible)))
6530                  (gnus-select-frame-set-input-focus (window-frame win))
6531                  (select-window win))))
6532         (setq in-buffer (current-buffer))
6533         ;; We disable the pick minor mode commands.
6534         (setq func (let (gnus-pick-mode)
6535                      (key-binding keys t)))
6536         (when (get func 'disabled)
6537           (error "Function %s disabled" func))
6538         (if (and func
6539                  (functionp func)
6540                  (condition-case code
6541                      (progn
6542                        (call-interactively func)
6543                        t)
6544                    (error
6545                     (setq err code)
6546                     nil)))
6547             (progn
6548               (when (eq win (selected-window))
6549                 (setq new-sum-point (point)
6550                       new-sum-start (window-start win)
6551                       new-sum-hscroll (window-hscroll win)))
6552               (when (or (eq in-buffer (current-buffer))
6553                         (when (eq obuf (current-buffer))
6554                           (set-buffer in-buffer)
6555                           t))
6556                 (setq selected (gnus-summary-select-article))
6557                 (set-buffer obuf)
6558                 (unless not-restore-window
6559                   (set-window-configuration owin))
6560                 (when (and (eq selected 'old)
6561                            new-sum-point)
6562                   (set-window-start (get-buffer-window (current-buffer))
6563                                     1)
6564                   (set-window-point (get-buffer-window (current-buffer))
6565                                     (if (article-goto-body)
6566                                         (1- (point))
6567                                       (point))))
6568                 (when (and (not not-restore-window)
6569                            new-sum-point
6570                            (window-live-p win)
6571                            (with-current-buffer (window-buffer win)
6572                              (eq major-mode 'gnus-summary-mode)))
6573                   (set-window-point win new-sum-point)
6574                   (set-window-start win new-sum-start)
6575                   (set-window-hscroll win new-sum-hscroll))))
6576           (set-window-configuration owin)
6577           (if err
6578               (signal (car err) (cdr err))
6579             (ding))))))))
6580
6581 (defun gnus-article-read-summary-send-keys ()
6582   (interactive)
6583   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6584     (gnus-article-read-summary-keys)))
6585
6586 (defun gnus-article-describe-key (key)
6587   "Display documentation of the function invoked by KEY.
6588 KEY is a string or a vector."
6589   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6590                        (read-key-sequence "Describe key: "))))
6591   (gnus-article-check-buffer)
6592   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6593                                   gnus-article-read-summary-send-keys))
6594       (with-current-buffer gnus-article-current-summary
6595         (setq unread-command-events
6596               (if (featurep 'xemacs)
6597                   (append key nil)
6598                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6599                                         (list 'meta (- x 128))
6600                                       x))
6601                         key)))
6602         (let ((cursor-in-echo-area t)
6603               gnus-pick-mode)
6604           (describe-key (read-key-sequence nil t))))
6605     (describe-key key)))
6606
6607 (defun gnus-article-describe-key-briefly (key &optional insert)
6608   "Display documentation of the function invoked by KEY.
6609 KEY is a string or a vector."
6610   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6611                        (read-key-sequence "Describe key: "))
6612                      current-prefix-arg))
6613   (gnus-article-check-buffer)
6614   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6615                                   gnus-article-read-summary-send-keys))
6616       (with-current-buffer gnus-article-current-summary
6617         (setq unread-command-events
6618               (if (featurep 'xemacs)
6619                   (append key nil)
6620                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6621                                         (list 'meta (- x 128))
6622                                       x))
6623                         key)))
6624         (let ((cursor-in-echo-area t)
6625               gnus-pick-mode)
6626           (describe-key-briefly (read-key-sequence nil t) insert)))
6627     (describe-key-briefly key insert)))
6628
6629 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6630 (defvar gnus-agent-summary-mode)
6631 (defvar gnus-draft-mode)
6632 ;; Calling help-buffer will autoload help-mode.
6633 (defvar help-xref-stack-item)
6634 ;; Emacs 22 doesn't load it in the batch mode.
6635 (eval-when-compile
6636   (autoload 'help-buffer "help-mode"))
6637
6638 (defun gnus-article-describe-bindings (&optional prefix)
6639   "Show a list of all defined keys, and their definitions.
6640 The optional argument PREFIX, if non-nil, should be a key sequence;
6641 then we display only bindings that start with that prefix."
6642   (interactive)
6643   (gnus-article-check-buffer)
6644   (let ((keymap (copy-keymap gnus-article-mode-map))
6645         (map (copy-keymap gnus-article-send-map))
6646         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6647         parent agent draft)
6648     (define-key keymap "S" map)
6649     (define-key map [t] nil)
6650     (with-current-buffer gnus-article-current-summary
6651       (set-keymap-parent
6652        keymap
6653        (if (setq parent (keymap-parent gnus-article-mode-map))
6654            (prog1
6655                (setq parent (copy-keymap parent))
6656              (set-keymap-parent parent (current-local-map)))
6657          (current-local-map)))
6658       (set-keymap-parent map (key-binding "S"))
6659       (let (key def gnus-pick-mode)
6660         (while sumkeys
6661           (setq key (pop sumkeys))
6662           (cond ((and (vectorp key) (= (length key) 1)
6663                       (consp (setq def (aref key 0)))
6664                       (numberp (car def)) (numberp (cdr def)))
6665                  (when (< (max (car def) (cdr def)) 128)
6666                    (setq sumkeys
6667                          (append (mapcar
6668                                   #'vector
6669                                   (nreverse (gnus-uncompress-range def)))
6670                                  sumkeys))))
6671                 ((setq def (key-binding key))
6672                  (unless (eq def 'undefined)
6673                    (define-key keymap key def))))))
6674       (when (boundp 'gnus-agent-summary-mode)
6675         (setq agent gnus-agent-summary-mode))
6676       (when (boundp 'gnus-draft-mode)
6677         (setq draft gnus-draft-mode)))
6678     (with-temp-buffer
6679       (use-local-map keymap)
6680       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6681       (set (make-local-variable 'gnus-draft-mode) draft)
6682       (describe-bindings prefix))
6683     (let ((item `((lambda (prefix)
6684                     (with-current-buffer ,(current-buffer)
6685                       (gnus-article-describe-bindings prefix)))
6686                   ,prefix)))
6687       (with-current-buffer (let (help-xref-following) (help-buffer))
6688         (setq help-xref-stack-item item)))))
6689
6690 (defun gnus-article-reply-with-original (&optional wide)
6691   "Start composing a reply mail to the current message.
6692 The text in the region will be yanked.  If the region isn't active,
6693 the entire article will be yanked."
6694   (interactive)
6695   (let ((article (cdr gnus-article-current))
6696         contents)
6697     (if (not (gnus-region-active-p))
6698         (with-current-buffer gnus-summary-buffer
6699           (gnus-summary-reply (list (list article)) wide))
6700       (setq contents (buffer-substring (point) (mark t)))
6701       ;; Deactivate active regions.
6702       (when (and (boundp 'transient-mark-mode)
6703                  transient-mark-mode)
6704         (setq mark-active nil))
6705       (with-current-buffer gnus-summary-buffer
6706         (gnus-summary-reply
6707          (list (list article contents)) wide)))))
6708
6709 (defun gnus-article-wide-reply-with-original ()
6710   "Start composing a wide reply mail to the current message.
6711 The text in the region will be yanked.  If the region isn't active,
6712 the entire article will be yanked."
6713   (interactive)
6714   (gnus-article-reply-with-original t))
6715
6716 (defun gnus-article-followup-with-original ()
6717   "Compose a followup to the current article.
6718 The text in the region will be yanked.  If the region isn't active,
6719 the entire article will be yanked."
6720   (interactive)
6721   (let ((article (cdr gnus-article-current))
6722         contents)
6723       (if (not (gnus-region-active-p))
6724           (with-current-buffer gnus-summary-buffer
6725             (gnus-summary-followup (list (list article))))
6726         (setq contents (buffer-substring (point) (mark t)))
6727         ;; Deactivate active regions.
6728         (when (and (boundp 'transient-mark-mode)
6729                    transient-mark-mode)
6730           (setq mark-active nil))
6731         (with-current-buffer gnus-summary-buffer
6732           (gnus-summary-followup
6733            (list (list article contents)))))))
6734
6735 (defun gnus-article-hide (&optional arg force)
6736   "Hide all the gruft in the current article.
6737 This means that signatures, cited text and (some) headers will be
6738 hidden.
6739 If given a prefix, show the hidden text instead."
6740   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6741   (gnus-article-hide-headers arg)
6742   (gnus-article-hide-list-identifiers arg)
6743   (gnus-article-hide-citation-maybe arg force)
6744   (gnus-article-hide-signature arg))
6745
6746 (defun gnus-check-group-server ()
6747   ;; Make sure the connection to the server is alive.
6748   (unless (gnus-server-opened
6749            (gnus-find-method-for-group gnus-newsgroup-name))
6750     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6751     (gnus-request-group gnus-newsgroup-name t)))
6752
6753 (eval-when-compile
6754   (autoload 'nneething-get-file-name "nneething"))
6755
6756 (defun gnus-request-article-this-buffer (article group)
6757   "Get an article and insert it into this buffer."
6758   (let (do-update-line sparse-header)
6759     (prog1
6760         (save-excursion
6761           (erase-buffer)
6762           (gnus-kill-all-overlays)
6763           (setq group (or group gnus-newsgroup-name))
6764
6765           ;; Using `gnus-request-article' directly will insert the article into
6766           ;; `nntp-server-buffer' - so we'll save some time by not having to
6767           ;; copy it from the server buffer into the article buffer.
6768
6769           ;; We only request an article by message-id when we do not have the
6770           ;; headers for it, so we'll have to get those.
6771           (when (stringp article)
6772             (gnus-read-header article))
6773
6774           ;; If the article number is negative, that means that this article
6775           ;; doesn't belong in this newsgroup (possibly), so we find its
6776           ;; message-id and request it by id instead of number.
6777           (when (and (numberp article)
6778                      gnus-summary-buffer
6779                      (get-buffer gnus-summary-buffer)
6780                      (gnus-buffer-exists-p gnus-summary-buffer))
6781             (with-current-buffer gnus-summary-buffer
6782               (let ((header (gnus-summary-article-header article)))
6783                 (when (< article 0)
6784                   (cond
6785                    ((memq article gnus-newsgroup-sparse)
6786                     ;; This is a sparse gap article.
6787                     (setq do-update-line article)
6788                     (setq article (mail-header-id header))
6789                     (setq sparse-header (gnus-read-header article))
6790                     (setq gnus-newsgroup-sparse
6791                           (delq article gnus-newsgroup-sparse)))
6792                    ((vectorp header)
6793                     ;; It's a real article.
6794                     (setq article (mail-header-id header)))
6795                    (t
6796                     ;; It is an extracted pseudo-article.
6797                     (setq article 'pseudo)
6798                     (gnus-request-pseudo-article header))))
6799
6800                 (let ((method (gnus-find-method-for-group
6801                                gnus-newsgroup-name)))
6802                   (when (and (eq (car method) 'nneething)
6803                              (vectorp header))
6804                     (let ((dir (nneething-get-file-name
6805                                 (mail-header-id header))))
6806                       (when (and (stringp dir)
6807                                  (file-directory-p dir))
6808                         (setq article 'nneething)
6809                         (gnus-group-enter-directory dir))))))))
6810
6811           (cond
6812            ;; Refuse to select canceled articles.
6813            ((and (numberp article)
6814                  gnus-summary-buffer
6815                  (get-buffer gnus-summary-buffer)
6816                  (gnus-buffer-exists-p gnus-summary-buffer)
6817                  (eq (cdr (with-current-buffer gnus-summary-buffer
6818                             (assq article gnus-newsgroup-reads)))
6819                      gnus-canceled-mark))
6820             nil)
6821            ;; We first check `gnus-original-article-buffer'.
6822            ((and (get-buffer gnus-original-article-buffer)
6823                  (numberp article)
6824                  (with-current-buffer gnus-original-article-buffer
6825                    (and (equal (car gnus-original-article) group)
6826                         (eq (cdr gnus-original-article) article))))
6827             ;; `insert-buffer-substring' would incorrectly use the
6828             ;; equivalent of string-make-multibyte which amount to decoding
6829             ;; with locale-coding-system, causing failure of
6830             ;; subsequent decoding.
6831             (insert (mm-string-to-multibyte
6832                      (with-current-buffer gnus-original-article-buffer
6833                        (buffer-substring (point-min) (point-max)))))
6834             'article)
6835            ;; Check the backlog.
6836            ((and gnus-keep-backlog
6837                  (gnus-backlog-request-article group article (current-buffer)))
6838             'article)
6839            ;; Check asynchronous pre-fetch.
6840            ((gnus-async-request-fetched-article group article (current-buffer))
6841             (gnus-async-prefetch-next group article gnus-summary-buffer)
6842             (when (and (numberp article) gnus-keep-backlog)
6843               (gnus-backlog-enter-article group article (current-buffer)))
6844             'article)
6845            ;; Check the cache.
6846            ((and gnus-use-cache
6847                  (numberp article)
6848                  (gnus-cache-request-article article group))
6849             'article)
6850            ;; Check the agent cache.
6851            ((gnus-agent-request-article article group)
6852             'article)
6853            ;; Get the article and put into the article buffer.
6854            ((or (stringp article)
6855                 (numberp article))
6856             (let ((gnus-override-method gnus-override-method)
6857                   (methods (and (stringp article)
6858                                 (with-current-buffer gnus-summary-buffer
6859                                   (gnus-refer-article-methods))))
6860                   (backend (car (gnus-find-method-for-group
6861                                  gnus-newsgroup-name)))
6862                   result
6863                   (inhibit-read-only t))
6864               (when (and (null gnus-override-method)
6865                          methods)
6866                 (setq gnus-override-method (pop methods)))
6867               (while (not result)
6868                 (erase-buffer)
6869                 (gnus-kill-all-overlays)
6870                 (let ((gnus-newsgroup-name group))
6871                   (gnus-check-group-server))
6872                 (cond
6873                  ((gnus-request-article article group (current-buffer))
6874                   (when (numberp article)
6875                     (gnus-async-prefetch-next group article
6876                                               gnus-summary-buffer)
6877                     (when gnus-keep-backlog
6878                       (gnus-backlog-enter-article
6879                        group article (current-buffer)))
6880                     (when (and gnus-agent
6881                                (gnus-agent-group-covered-p group))
6882                       (gnus-agent-store-article article group)))
6883                   (setq result 'article))
6884                  (methods
6885                   (setq gnus-override-method (pop methods)))
6886                  ((not (string-match "^400 "
6887                                      (nnheader-get-report backend)))
6888                   ;; If we get 400 server disconnect, reconnect and
6889                   ;; retry; otherwise, assume the article has expired.
6890                   (setq result 'done))))
6891               (and (eq result 'article) 'article)))
6892            ;; It was a pseudo.
6893            (t article)))
6894
6895       ;; Associate this article with the current summary buffer.
6896       (setq gnus-article-current-summary gnus-summary-buffer)
6897
6898       ;; Take the article from the original article buffer
6899       ;; and place it in the buffer it's supposed to be in.
6900       (when (and (get-buffer gnus-article-buffer)
6901                  (equal (buffer-name (current-buffer))
6902                         (buffer-name (get-buffer gnus-article-buffer))))
6903         (save-excursion
6904           (if (get-buffer gnus-original-article-buffer)
6905               (set-buffer gnus-original-article-buffer)
6906             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6907             (buffer-disable-undo)
6908             (setq major-mode 'gnus-original-article-mode)
6909             (setq buffer-read-only t))
6910           (let ((inhibit-read-only t))
6911             (erase-buffer)
6912             (insert-buffer-substring gnus-article-buffer))
6913           (setq gnus-original-article (cons group article)))
6914
6915         ;; Decode charsets.
6916         (run-hooks 'gnus-article-decode-hook)
6917         ;; Mark article as decoded or not.
6918         (setq gnus-article-decoded-p gnus-article-decode-hook))
6919
6920       ;; Update sparse articles.
6921       (when (and do-update-line
6922                  (or (numberp article)
6923                      (stringp article)))
6924         (let ((buf (current-buffer)))
6925           (set-buffer gnus-summary-buffer)
6926           (gnus-summary-update-article do-update-line sparse-header)
6927           (gnus-summary-goto-subject do-update-line nil t)
6928           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6929                             (point))
6930           (set-buffer buf))))))
6931
6932 (defun gnus-block-private-groups (group)
6933   (if (gnus-news-group-p group)
6934       ;; Block nothing in news groups.
6935       nil
6936     ;; Block everything anywhere else.
6937     "."))
6938
6939 (defun gnus-blocked-images ()
6940   (if (functionp gnus-blocked-images)
6941       (funcall gnus-blocked-images gnus-newsgroup-name)
6942     gnus-blocked-images))
6943
6944 ;;;
6945 ;;; Article editing
6946 ;;;
6947
6948 (defcustom gnus-article-edit-mode-hook nil
6949   "Hook run in article edit mode buffers."
6950   :group 'gnus-article-various
6951   :type 'hook)
6952
6953 (defvar gnus-article-edit-done-function nil)
6954
6955 (defvar gnus-article-edit-mode-map nil)
6956
6957 ;; Should we be using derived.el for this?
6958 (unless gnus-article-edit-mode-map
6959   (setq gnus-article-edit-mode-map (make-keymap))
6960   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6961
6962   (gnus-define-keys gnus-article-edit-mode-map
6963     "\C-c?"    describe-mode
6964     "\C-c\C-c" gnus-article-edit-done
6965     "\C-c\C-k" gnus-article-edit-exit
6966     "\C-c\C-f\C-t" message-goto-to
6967     "\C-c\C-f\C-o" message-goto-from
6968     "\C-c\C-f\C-b" message-goto-bcc
6969     ;;"\C-c\C-f\C-w" message-goto-fcc
6970     "\C-c\C-f\C-c" message-goto-cc
6971     "\C-c\C-f\C-s" message-goto-subject
6972     "\C-c\C-f\C-r" message-goto-reply-to
6973     "\C-c\C-f\C-n" message-goto-newsgroups
6974     "\C-c\C-f\C-d" message-goto-distribution
6975     "\C-c\C-f\C-f" message-goto-followup-to
6976     "\C-c\C-f\C-m" message-goto-mail-followup-to
6977     "\C-c\C-f\C-k" message-goto-keywords
6978     "\C-c\C-f\C-u" message-goto-summary
6979     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6980     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6981     "\C-c\C-b" message-goto-body
6982     "\C-c\C-i" message-goto-signature
6983
6984     "\C-c\C-t" message-insert-to
6985     "\C-c\C-n" message-insert-newsgroups
6986     "\C-c\C-o" message-sort-headers
6987     "\C-c\C-e" message-elide-region
6988     "\C-c\C-v" message-delete-not-region
6989     "\C-c\C-z" message-kill-to-signature
6990     "\M-\r" message-newline-and-reformat
6991     "\C-c\C-a" mml-attach-file
6992     "\C-a" message-beginning-of-line
6993     "\t" message-tab
6994     "\M-;" comment-region)
6995
6996   (gnus-define-keys (gnus-article-edit-wash-map
6997                      "\C-c\C-w" gnus-article-edit-mode-map)
6998     "f" gnus-article-edit-full-stops))
6999
7000 (easy-menu-define
7001   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7002   '("Field"
7003     ["Fetch To" message-insert-to t]
7004     ["Fetch Newsgroups" message-insert-newsgroups t]
7005     "----"
7006     ["To" message-goto-to t]
7007     ["From" message-goto-from t]
7008     ["Subject" message-goto-subject t]
7009     ["Cc" message-goto-cc t]
7010     ["Reply-To" message-goto-reply-to t]
7011     ["Summary" message-goto-summary t]
7012     ["Keywords" message-goto-keywords t]
7013     ["Newsgroups" message-goto-newsgroups t]
7014     ["Followup-To" message-goto-followup-to t]
7015     ["Mail-Followup-To" message-goto-mail-followup-to t]
7016     ["Distribution" message-goto-distribution t]
7017     ["Body" message-goto-body t]
7018     ["Signature" message-goto-signature t]))
7019
7020 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7021   "Major mode for editing articles.
7022 This is an extended text-mode.
7023
7024 \\{gnus-article-edit-mode-map}"
7025   (make-local-variable 'gnus-article-edit-done-function)
7026   (make-local-variable 'gnus-prev-winconf)
7027   (set (make-local-variable 'font-lock-defaults)
7028        '(message-font-lock-keywords t))
7029   (set (make-local-variable 'mail-header-separator) "")
7030   (set (make-local-variable 'gnus-article-edit-mode) t)
7031   (easy-menu-add message-mode-field-menu message-mode-map)
7032   (mml-mode)
7033   (setq buffer-read-only nil)
7034   (buffer-enable-undo)
7035   (widen))
7036
7037 (defun gnus-article-edit (&optional force)
7038   "Edit the current article.
7039 This will have permanent effect only in mail groups.
7040 If FORCE is non-nil, allow editing of articles even in read-only
7041 groups."
7042   (interactive "P")
7043   (when (and (not force)
7044              (gnus-group-read-only-p))
7045     (error "The current newsgroup does not support article editing"))
7046   (gnus-article-date-original)
7047   (gnus-article-edit-article
7048    'ignore
7049    `(lambda (no-highlight)
7050       'ignore
7051       (gnus-summary-edit-article-done
7052        ,(or (mail-header-references gnus-current-headers) "")
7053        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7054
7055 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7056   "Start editing the contents of the current article buffer."
7057   (let ((winconf (current-window-configuration)))
7058     (set-buffer gnus-article-buffer)
7059     (let ((message-auto-save-directory
7060            ;; Don't associate the article buffer with a draft file.
7061            nil))
7062       (gnus-article-edit-mode))
7063     (funcall start-func)
7064     (set-buffer-modified-p nil)
7065     (gnus-configure-windows 'edit-article)
7066     (setq gnus-article-edit-done-function exit-func)
7067     (setq gnus-prev-winconf winconf)
7068     (unless quiet
7069       (gnus-message 6 "C-c C-c to end edits"))))
7070
7071 (defun gnus-article-edit-done (&optional arg)
7072   "Update the article edits and exit."
7073   (interactive "P")
7074   (let ((func gnus-article-edit-done-function)
7075         (buf (current-buffer))
7076         (start (window-start))
7077         (p (point))
7078         (winconf gnus-prev-winconf))
7079     (widen) ;; Widen it in case that users narrowed the buffer.
7080     (funcall func arg)
7081     (set-buffer buf)
7082     ;; The cache and backlog have to be flushed somewhat.
7083     (when gnus-keep-backlog
7084       (gnus-backlog-remove-article
7085        (car gnus-article-current) (cdr gnus-article-current)))
7086     ;; Flush original article as well.
7087     (gnus-flush-original-article-buffer)
7088     (when gnus-use-cache
7089       (gnus-cache-update-article
7090        (car gnus-article-current) (cdr gnus-article-current)))
7091     ;; We remove all text props from the article buffer.
7092     (kill-all-local-variables)
7093     (set-text-properties (point-min) (point-max) nil)
7094     (gnus-article-mode)
7095     (set-window-configuration winconf)
7096     (set-buffer buf)
7097     (set-window-start (get-buffer-window buf) start)
7098     (set-window-point (get-buffer-window buf) (point)))
7099   (gnus-summary-show-article))
7100
7101 (defun gnus-flush-original-article-buffer ()
7102   (when (get-buffer gnus-original-article-buffer)
7103     (with-current-buffer gnus-original-article-buffer
7104       (setq gnus-original-article nil))))
7105
7106 (defun gnus-article-edit-exit ()
7107   "Exit the article editing without updating."
7108   (interactive)
7109   (when (or (not (buffer-modified-p))
7110             (yes-or-no-p "Article modified; kill anyway? "))
7111     (let ((curbuf (current-buffer))
7112           (p (point))
7113           (window-start (window-start)))
7114       (erase-buffer)
7115       (if (gnus-buffer-live-p gnus-original-article-buffer)
7116           (insert-buffer-substring gnus-original-article-buffer))
7117       (let ((winconf gnus-prev-winconf))
7118         (kill-all-local-variables)
7119         (gnus-article-mode)
7120         (set-window-configuration winconf)
7121         ;; Tippy-toe some to make sure that point remains where it was.
7122         (save-current-buffer
7123           (set-buffer curbuf)
7124           (set-window-start (get-buffer-window (current-buffer)) window-start)
7125           (goto-char p))))
7126     (gnus-summary-show-article)))
7127
7128 (defun gnus-article-edit-full-stops ()
7129   "Interactively repair spacing at end of sentences."
7130   (interactive)
7131   (save-excursion
7132     (goto-char (point-min))
7133     (search-forward-regexp "^$" nil t)
7134     (let ((case-fold-search nil))
7135       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7136
7137 ;;;
7138 ;;; Article highlights
7139 ;;;
7140
7141 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7142
7143 ;;; Internal Variables:
7144
7145 (defcustom gnus-button-url-regexp
7146   (concat
7147    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7148    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7149    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7150    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7151        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7152              (punct "!?:;.,"))
7153          (concat
7154           "\\(?:"
7155           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7156           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7157           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7158           "\\|"
7159           "[" chars punct     "]+" "[" chars "]"
7160           "\\)"))
7161      (concat ;; XEmacs 21.4 doesn't support POSIX.
7162       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7163       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7164    "\\)")
7165   "Regular expression that matches URLs."
7166   :group 'gnus-article-buttons
7167   :type 'regexp)
7168
7169 (defcustom gnus-button-valid-fqdn-regexp
7170   message-valid-fqdn-regexp
7171   "Regular expression that matches a valid FQDN."
7172   :version "22.1"
7173   :group 'gnus-article-buttons
7174   :type 'regexp)
7175
7176 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7177 (defcustom gnus-button-valid-localpart-regexp
7178   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7179   "Regular expression that matches a localpart of mail addresses or MIDs."
7180   :version "22.1"
7181   :group 'gnus-article-buttons
7182   :type 'regexp)
7183
7184 (defcustom gnus-button-man-handler 'manual-entry
7185   "Function to use for displaying man pages.
7186 The function must take at least one argument with a string naming the
7187 man page."
7188   :version "22.1"
7189   :type '(choice (function-item :tag "Man" manual-entry)
7190                  (function-item :tag "Woman" woman)
7191                  (function :tag "Other"))
7192   :group 'gnus-article-buttons)
7193
7194 (defcustom gnus-button-mid-or-mail-regexp
7195   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7196           gnus-button-valid-fqdn-regexp
7197           ">?\\)\\b")
7198   "Regular expression that matches a message ID or a mail address."
7199   :version "22.1"
7200   :group 'gnus-article-buttons
7201   :type 'regexp)
7202
7203 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7204   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7205 Strings like this can be either a message ID or a mail address.  If it is one
7206 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7207 message ID or a mail address, respectively.  If this variable is set to the
7208 symbol `ask', always query the user what do do.  If it is a function, this
7209 function will be called with the string as its only argument.  The function
7210 must return `mid', `mail', `invalid' or `ask'."
7211   :version "22.1"
7212   :group 'gnus-article-buttons
7213   :type '(choice (function-item :tag "Heuristic function"
7214                                 gnus-button-mid-or-mail-heuristic)
7215                  (const ask)
7216                  (const mid)
7217                  (const mail)))
7218
7219 (defcustom gnus-button-mid-or-mail-heuristic-alist
7220   '((-10.0 . ".+\\$.+@")
7221     (-10.0 . "#")
7222     (-10.0 . "\\*")
7223     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7224     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7225     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7226     (-1.0  . "^[^a-z]+@")
7227     ;;
7228     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7229     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7230     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7231     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7232     ;;
7233     (-2.0  . "^[0-9]")
7234     (-1.0  . "^[0-9][0-9]")
7235     ;;
7236     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7237     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7238     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7239     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7240     ;;
7241     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7242     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7243     ;;       "[0-9]{8,}.*\@"
7244     (-3.0
7245      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7246     ;; "[0-9]{12,}.*\@"
7247     ;; compensation for TDMA dated mail addresses:
7248     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7249     ;;
7250     (-20.0 . "\\.fsf@") ;; Gnus
7251     (-20.0 . "^slrn")
7252     (-20.0 . "^Pine")
7253     (-20.0 . "^alpine\\.")
7254     (-20.0 . "_-_") ;; Subject change in thread
7255     ;;
7256     (-20.0 . "\\.ln@") ;; leafnode
7257     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7258     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7259     ;;
7260     ;; (5.0 . "") ;; $local_part_len <= 7
7261     (10.0  . "^[^0-9]+@")
7262     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7263     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7264     (3.0   . "\@stud")
7265     ;;
7266     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7267     ;;
7268     (0.5   . "^[A-Z][a-z]")
7269     (0.5   . "^[A-Z][a-z][a-z]")
7270     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7271     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7272   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7273
7274 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7275 address.  The REGEXP is processed with `case-fold-search' set to nil."
7276   :version "22.1"
7277   :group 'gnus-article-buttons
7278   :type '(repeat (cons (number :tag "Rate")
7279                        (regexp :tag "Regexp"))))
7280
7281 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7282   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7283 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7284 address, `ask' if unsure and `invalid' if the string is invalid."
7285   (let ((case-fold-search nil)
7286         (list gnus-button-mid-or-mail-heuristic-alist)
7287         (result 0) rate regexp lpartlen elem)
7288     (setq lpartlen
7289           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7290     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7291     ;; Certain special cases...
7292     (when (string-match
7293            (concat
7294             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7295             "^[0-9]+\\.[0-9]+@compuserve\\|"
7296             "@public\\.gmane\\.org")
7297            mid-or-mail)
7298       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7299       (setq result 'mail))
7300     (when (string-match "@.*@\\| " mid-or-mail)
7301       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7302       (setq result 'invalid))
7303     ;; Nothing more to do, if result is not a number here...
7304     (when (numberp result)
7305       (while list
7306         (setq elem (car list)
7307               rate (car elem)
7308               regexp (cdr elem)
7309               list (cdr list))
7310         (when (string-match regexp mid-or-mail)
7311           (setq result (+ result rate))
7312           (gnus-message
7313            9 "`%s' matched `%s', rate `%s', result `%s'."
7314            mid-or-mail regexp rate result)))
7315       (when (<= lpartlen 7)
7316         (setq result (+ result 5.0))
7317         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7318                       mid-or-mail result))
7319       (when (>= lpartlen 12)
7320         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7321         (cond
7322          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7323           ;; Long local part should contain realname if e-mail address,
7324           ;; too many digits: message-id.
7325           ;; $score -= 5.0 + 0.1 * $local_part_len;
7326           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7327           (setq result (+ result rate))
7328           (gnus-message
7329            9 "Many digits in `%s', rate `%s', result `%s'."
7330            mid-or-mail rate result))
7331          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7332                         mid-or-mail)
7333           ;; Too few vowels [^aeiouy]{4,}.*\@
7334           (setq result (+ result -5.0))
7335           (gnus-message
7336            9 "Few vowels in `%s', rate `%s', result `%s'."
7337            mid-or-mail -5.0 result))
7338          (t
7339           (setq result (+ result 5.0))
7340           (gnus-message
7341            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7342     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7343     ;; Maybe we should make this a customizable alist: (condition . 'result)
7344     (cond
7345      ((symbolp result) result)
7346      ;; Now convert number into proper results:
7347      ((< result -10.0) 'mid)
7348      ((> result  10.0) 'mail)
7349      (t 'ask))))
7350
7351 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7352   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7353          (url-mid (concat "news" ":" mid-or-mail))
7354          (url-mailto (concat "mailto" ":" mid-or-mail)))
7355     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7356     (when (fboundp pref)
7357       (setq guessed
7358             ;; get rid of surrounding angles...
7359             (funcall pref
7360                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7361       (if (or (eq 'mid guessed) (eq 'mail guessed))
7362           (setq pref guessed)
7363         (setq pref 'ask)))
7364     (if (eq pref 'ask)
7365         (save-window-excursion
7366           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7367               (setq pref 'mail)
7368             (setq pref 'mid))))
7369     (cond ((eq pref 'mid)
7370            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7371            (gnus-button-handle-news url-mid))
7372           ((eq pref 'mail)
7373            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7374            (gnus-url-mailto url-mailto))
7375           (t (gnus-message 3 "Invalid string.")))))
7376
7377 (defun gnus-button-handle-custom (fun arg)
7378   "Call function FUN on argument ARG.
7379 Both FUN and ARG are supposed to be strings.  ARG will be passed
7380 as a symbol to FUN."
7381   (funcall (intern fun)
7382            (if (string-match "^customize-apropos" fun)
7383                arg
7384              (intern arg))))
7385
7386 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7387
7388 (defun gnus-button-handle-describe-function (url)
7389   "Call `describe-function' when pushing the corresponding URL button."
7390   (describe-function
7391    (intern
7392     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7393
7394 (defun gnus-button-handle-describe-variable (url)
7395   "Call `describe-variable' when pushing the corresponding URL button."
7396   (describe-variable
7397    (intern
7398     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7399
7400 (defun gnus-button-handle-symbol (url)
7401 "Display help on variable or function.
7402 Calls `describe-variable' or `describe-function'."
7403   (let ((sym (intern url)))
7404     (cond
7405      ((fboundp sym) (describe-function sym))
7406      ((boundp sym) (describe-variable sym))
7407      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7408
7409 (defun gnus-button-handle-describe-key (url)
7410   "Call `describe-key' when pushing the corresponding URL button."
7411   (let* ((key-string
7412           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7413          (keys (ignore-errors (eval `(kbd ,key-string)))))
7414     (if keys
7415         (describe-key keys)
7416       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7417
7418 (defun gnus-button-handle-apropos (url)
7419   "Call `apropos' when pushing the corresponding URL button."
7420   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7421
7422 (defun gnus-button-handle-apropos-command (url)
7423   "Call `apropos' when pushing the corresponding URL button."
7424   (apropos-command
7425    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7426
7427 (defun gnus-button-handle-apropos-variable (url)
7428   "Call `apropos' when pushing the corresponding URL button."
7429   (funcall
7430    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7431    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7432
7433 (defun gnus-button-handle-apropos-documentation (url)
7434   "Call `apropos' when pushing the corresponding URL button."
7435   (funcall
7436    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7437    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7438
7439 (defun gnus-button-handle-library (url)
7440   "Call `locate-library' when pushing the corresponding URL button."
7441   (gnus-message 9 "url=`%s'" url)
7442   (let* ((lib (locate-library url))
7443          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7444     (if (not lib)
7445         (gnus-message 1 "Cannot locale library `%s'." url)
7446       (find-file-read-only file))))
7447
7448 (defcustom gnus-button-man-level 5
7449   "*Integer that says how many man-related buttons Gnus will show.
7450 The higher the number, the more buttons will appear and the more false
7451 positives are possible.  Note that you can set this variable local to
7452 specific groups.  Setting it higher in Unix groups is probably a good idea.
7453 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7454 how to set variables in specific groups."
7455   :version "22.1"
7456   :group 'gnus-article-buttons
7457   :link '(custom-manual "(gnus)Group Parameters")
7458   :type 'integer)
7459
7460 (defcustom gnus-button-emacs-level 5
7461   "*Integer that says how many emacs-related buttons Gnus will show.
7462 The higher the number, the more buttons will appear and the more false
7463 positives are possible.  Note that you can set this variable local to
7464 specific groups.  Setting it higher in Emacs or Gnus related groups is
7465 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7466 `gnus-parameters' on how to set variables in specific groups."
7467   :version "22.1"
7468   :group 'gnus-article-buttons
7469   :link '(custom-manual "(gnus)Group Parameters")
7470   :type 'integer)
7471
7472 (defcustom gnus-button-message-level 5
7473   "*Integer that says how many buttons for news or mail messages will appear.
7474 The higher the number, the more buttons will appear and the more false
7475 positives are possible."
7476   ;; mail addresses, MIDs, URLs for news, ...
7477   :version "22.1"
7478   :group 'gnus-article-buttons
7479   :type 'integer)
7480
7481 (defcustom gnus-button-browse-level 5
7482   "*Integer that says how many buttons for browsing will appear.
7483 The higher the number, the more buttons will appear and the more false
7484 positives are possible."
7485   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7486   :version "22.1"
7487   :group 'gnus-article-buttons
7488   :type 'integer)
7489
7490 (defcustom gnus-button-alist
7491   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7492      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7493     ((concat "\\b\\(nntp\\|news\\):\\("
7494              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7495      0 t gnus-button-handle-news 2)
7496     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7497      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7498     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7499      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7500     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7501     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7502      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7503     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7504      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7505     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7506      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7507     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7508      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7509     ;; RFC 2368 (The mailto URL scheme)
7510     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7511      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7512     ("\\bmailto:\\([^ \n\t]+\\)"
7513      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7514     ;; Info Konqueror style <info:/foo/bar baz>.
7515     ;; Must come before " Gnus home-grown style".
7516     ("\\binfo://?\\([^'\">\n\t]+\\)"
7517      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7518    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7519     ("\\binfo://\\([^'\">\n\t ]+\\)"
7520      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7521     ;; Info GNOME style <info:foo#bar_baz>
7522     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7523      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7524     ;; Info KDE style <info:(foo)bar baz>
7525     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7526      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7527     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7528      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7529     ("\\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,]*\\)\\)?"
7530      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7531      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7532     ;; This is custom
7533     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7534      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7535     ;; Emacs help commands
7536     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7537      ;; regexp doesn't match arguments containing ` '.
7538      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7539     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7540      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7541     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7542      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7543     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7544      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7545     ;; The following entries may lead to many false positives so don't enable
7546     ;; them by default (use a high button level).
7547     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7548      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7549      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7550     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7551      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7552     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7553      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7554     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7555      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7556     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7557      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7558     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7559      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7560     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7561      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7562     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7563      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7564     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7565      ;; Unlike the other regexps we really have to require quoting
7566      ;; here to determine where it ends.
7567      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7568     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7569     ("<URL: *\\([^\n<>]*\\)>"
7570      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7571     ;; RFC 2396 (2.4.3., delims) ...
7572     ("\"URL: *\\([^\n\"]*\\)\""
7573      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7574     ;; Raw URLs.
7575     (gnus-button-url-regexp
7576      0 (>= gnus-button-browse-level 0) browse-url 0)
7577     ;; man pages
7578     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7579      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7580      gnus-button-handle-man 1)
7581     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7582     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7583      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7584      gnus-button-handle-man 1)
7585     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7586     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7587     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7588      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7589     ;; Recognizing patches to .el files.  This is somewhat obscure,
7590     ;; but considering the percentage of Gnus users who hack Emacs
7591     ;; Lisp files...
7592     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7593      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7594     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7595      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7596     ;; MID or mail: To avoid too many false positives we don't try to catch
7597     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7598     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7599     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7600     ;; so that non-ambiguous entries (see above) match first.
7601     (gnus-button-mid-or-mail-regexp
7602      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7603   "*Alist of regexps matching buttons in article bodies.
7604
7605 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7606 REGEXP: is the string (case insensitive) matching text around the button (can
7607 also be Lisp expression evaluating to a string),
7608 BUTTON: is the number of the regexp grouping actually matching the button,
7609 FORM: is a Lisp expression which must eval to true for the button to
7610 be added,
7611 CALLBACK: is the function to call when the user push this button, and each
7612 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7613
7614 CALLBACK can also be a variable, in that case the value of that
7615 variable it the real callback function."
7616   :group 'gnus-article-buttons
7617   :type '(repeat (list (choice regexp variable sexp)
7618                        (integer :tag "Button")
7619                        (sexp :tag "Form")
7620                        (function :tag "Callback")
7621                        (repeat :tag "Par"
7622                                :inline t
7623                                (integer :tag "Regexp group")))))
7624 (put 'gnus-button-alist 'risky-local-variable t)
7625
7626 (defcustom gnus-header-button-alist
7627   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7628      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7629     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7630      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7631     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7632      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7633     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7634      0 (>= gnus-button-browse-level 0) browse-url 0)
7635     ("^Subject:" gnus-button-url-regexp
7636      0 (>= gnus-button-browse-level 0) browse-url 0)
7637     ("^[^:]+:" gnus-button-url-regexp
7638      0 (>= gnus-button-browse-level 0) browse-url 0)
7639     ("^OpenPGP:.*url=" gnus-button-url-regexp
7640      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7641     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7642      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7643     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7644      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7645   "*Alist of headers and regexps to match buttons in article heads.
7646
7647 This alist is very similar to `gnus-button-alist', except that each
7648 alist has an additional HEADER element first in each entry:
7649
7650 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7651
7652 HEADER is a regexp to match a header.  For a fuller explanation, see
7653 `gnus-button-alist'."
7654   :group 'gnus-article-buttons
7655   :group 'gnus-article-headers
7656   :type '(repeat (list (regexp :tag "Header")
7657                        (choice regexp variable)
7658                        (integer :tag "Button")
7659                        (sexp :tag "Form")
7660                        (function :tag "Callback")
7661                        (repeat :tag "Par"
7662                                :inline t
7663                                (integer :tag "Regexp group")))))
7664 (put 'gnus-header-button-alist 'risky-local-variable t)
7665
7666 ;;; Commands:
7667
7668 (defun gnus-article-push-button (event)
7669   "Check text under the mouse pointer for a callback function.
7670 If the text under the mouse pointer has a `gnus-callback' property,
7671 call it with the value of the `gnus-data' text property."
7672   (interactive "e")
7673   (set-buffer (window-buffer (posn-window (event-start event))))
7674   (let* ((pos (posn-point (event-start event)))
7675          (data (get-text-property pos 'gnus-data))
7676          (fun (get-text-property pos 'gnus-callback)))
7677     (goto-char pos)
7678     (when fun
7679       (funcall fun data))))
7680
7681 (defun gnus-article-press-button ()
7682   "Check text at point for a callback function.
7683 If the text at point has a `gnus-callback' property,
7684 call it with the value of the `gnus-data' text property."
7685   (interactive)
7686   (let ((data (get-text-property (point) 'gnus-data))
7687         (fun (get-text-property (point) 'gnus-callback)))
7688     (when fun
7689       (funcall fun data))))
7690
7691 (defun gnus-article-highlight (&optional force)
7692   "Highlight current article.
7693 This function calls `gnus-article-highlight-headers',
7694 `gnus-article-highlight-citation',
7695 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7696 do the highlighting.  See the documentation for those functions."
7697   (interactive (list 'force))
7698   (gnus-article-highlight-headers)
7699   (gnus-article-highlight-citation force)
7700   (gnus-article-highlight-signature)
7701   (gnus-article-add-buttons)
7702   (gnus-article-add-buttons-to-head))
7703
7704 (defun gnus-article-highlight-some (&optional force)
7705   "Highlight current article.
7706 This function calls `gnus-article-highlight-headers',
7707 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7708 do the highlighting.  See the documentation for those functions."
7709   (interactive (list 'force))
7710   (gnus-article-highlight-headers)
7711   (gnus-article-highlight-signature)
7712   (gnus-article-add-buttons))
7713
7714 (defun gnus-article-highlight-headers ()
7715   "Highlight article headers as specified by `gnus-header-face-alist'."
7716   (interactive)
7717   (gnus-with-article-headers
7718     (let (regexp header-face field-face from hpoints fpoints)
7719       (dolist (entry gnus-header-face-alist)
7720         (goto-char (point-min))
7721         (setq regexp (concat "^\\("
7722                              (if (string-equal "" (nth 0 entry))
7723                                  "[^\t ]"
7724                                (nth 0 entry))
7725                              "\\)")
7726               header-face (nth 1 entry)
7727               field-face (nth 2 entry))
7728         (while (and (re-search-forward regexp nil t)
7729                     (not (eobp)))
7730           (beginning-of-line)
7731           (setq from (point))
7732           (unless (search-forward ":" nil t)
7733             (forward-char 1))
7734           (when (and header-face
7735                      (not (memq (point) hpoints)))
7736             (push (point) hpoints)
7737             (gnus-put-text-property from (point) 'face header-face))
7738           (when (and field-face
7739                      (not (memq (setq from (point)) fpoints)))
7740             (push from fpoints)
7741             (if (re-search-forward "^[^ \t]" nil t)
7742                 (forward-char -2)
7743               (goto-char (point-max)))
7744             (gnus-put-text-property from (point) 'face field-face)))))))
7745
7746 (defun gnus-article-highlight-signature ()
7747   "Highlight the signature in an article.
7748 It does this by highlighting everything after
7749 `gnus-signature-separator' using the face `gnus-signature'."
7750   (interactive)
7751   (gnus-with-article-buffer
7752     (let ((inhibit-point-motion-hooks t))
7753       (save-restriction
7754         (when (and gnus-signature-face
7755                    (gnus-article-narrow-to-signature))
7756           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7757                             'face gnus-signature-face)
7758           (widen)
7759           (gnus-article-search-signature)
7760           (let ((start (match-beginning 0))
7761                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7762             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7763                                      end)))))))
7764
7765 (defun gnus-button-in-region-p (b e prop)
7766   "Say whether PROP exists in the region."
7767   (text-property-not-all b e prop nil))
7768
7769 (defun gnus-article-add-buttons ()
7770   "Find external references in the article and make buttons of them.
7771 \"External references\" are things like Message-IDs and URLs, as
7772 specified by `gnus-button-alist'."
7773   (interactive)
7774   (gnus-with-article-buffer
7775     (let ((inhibit-point-motion-hooks t)
7776           (case-fold-search t)
7777           (alist gnus-button-alist)
7778           beg entry regexp)
7779       ;; We skip the headers.
7780       (article-goto-body)
7781       (setq beg (point))
7782       (while (setq entry (pop alist))
7783         (setq regexp (eval (car entry)))
7784         (goto-char beg)
7785         (while (re-search-forward regexp nil t)
7786           (let ((start (match-beginning (nth 1 entry)))
7787                 (end (match-end (nth 1 entry)))
7788                 (from (match-beginning 0)))
7789             (when (and (eval (nth 2 entry))
7790                        (not (gnus-button-in-region-p
7791                              start end 'gnus-callback)))
7792               ;; That optional form returned non-nil, so we add the
7793               ;; button.
7794               (setq from (set-marker (make-marker) from))
7795               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7796                            (gnus-article-extend-url-button from start end))
7797                 (gnus-article-add-button start end
7798                                          'gnus-button-push (list from entry))
7799                 (gnus-put-text-property
7800                  start end
7801                  'gnus-string (buffer-substring-no-properties
7802                                start end))))))))))
7803
7804 (defun gnus-article-extend-url-button (beg start end)
7805   "Extend url button if url is folded into two or more lines.
7806 Return non-nil if button is extended.  BEG is a marker that points to
7807 the beginning position of a text containing url.  START and END are
7808 the endpoints of a url button before it is extended.  The concatenated
7809 url is put as the `gnus-button-url' overlay property on the button."
7810   (let ((opoint (point))
7811         (points (list start end))
7812         url delim regexp)
7813     (prog1
7814         (when (and (progn
7815                      (goto-char end)
7816                      (not (looking-at "[\t ]*[\">]")))
7817                    (progn
7818                      (goto-char start)
7819                      (string-match
7820                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7821                       (buffer-substring (point-at-bol) start)))
7822                    (progn
7823                      (setq url (list (buffer-substring start end))
7824                            delim (if (match-beginning 1) ">" "\""))
7825                      (beginning-of-line)
7826                      (setq regexp (concat
7827                                    (when (and (looking-at
7828                                                message-cite-prefix-regexp)
7829                                               (< (match-end 0) start))
7830                                      (regexp-quote (match-string 0)))
7831                                    "\
7832 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7833                                    delim "\\)"))
7834                      (while (progn
7835                               (forward-line 1)
7836                               (and (looking-at regexp)
7837                                    (prog1
7838                                        (match-beginning 1)
7839                                      (push (or (match-string 2)
7840                                                (match-string 1))
7841                                            url)
7842                                      (push (setq end (or (match-end 2)
7843                                                          (match-end 1)))
7844                                            points)
7845                                      (push (or (match-beginning 2)
7846                                                (match-beginning 1))
7847                                            points)))))
7848                      (match-beginning 2)))
7849           (let (gnus-article-mouse-face widget-mouse-face)
7850             (while points
7851               (gnus-article-add-button (pop points) (pop points)
7852                                        'gnus-button-push beg)))
7853           (let ((overlay (gnus-make-overlay start end)))
7854             (gnus-overlay-put overlay 'evaporate t)
7855             (gnus-overlay-put overlay 'gnus-button-url
7856                               (list (mapconcat 'identity (nreverse url) "")))
7857             (when gnus-article-mouse-face
7858               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7859           t)
7860       (goto-char opoint))))
7861
7862 ;; Add buttons to the head of an article.
7863 (defun gnus-article-add-buttons-to-head ()
7864   "Add buttons to the head of the article."
7865   (interactive)
7866   (gnus-with-article-headers
7867     (let (beg end)
7868       (dolist (entry gnus-header-button-alist)
7869         ;; Each alist entry.
7870         (goto-char (point-min))
7871         (while (re-search-forward (car entry) nil t)
7872           ;; Each header matching the entry.
7873           (setq beg (match-beginning 0))
7874           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7875                              (match-beginning 0))
7876                         (point-max)))
7877           (goto-char beg)
7878           (while (re-search-forward (eval (nth 1 entry)) end t)
7879             ;; Each match within a header.
7880             (let* ((entry (cdr entry))
7881                    (start (match-beginning (nth 1 entry)))
7882                    (end (match-end (nth 1 entry)))
7883                    (form (nth 2 entry)))
7884               (goto-char (match-end 0))
7885               (when (eval form)
7886                 (gnus-article-add-button
7887                  start end (nth 3 entry)
7888                  (buffer-substring (match-beginning (nth 4 entry))
7889                                    (match-end (nth 4 entry)))))))
7890           (goto-char end))))))
7891
7892 ;;; External functions:
7893
7894 (defun gnus-article-add-button (from to fun &optional data text)
7895   "Create a button between FROM and TO with callback FUN and data DATA."
7896   (when gnus-article-button-face
7897     (gnus-overlay-put (gnus-make-overlay from to nil t)
7898                       'face gnus-article-button-face))
7899   (gnus-add-text-properties
7900    from to
7901    (nconc (and gnus-article-mouse-face
7902                (list gnus-mouse-face-prop gnus-article-mouse-face))
7903           (list 'gnus-callback fun)
7904           (and data (list 'gnus-data data))))
7905   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7906                          :help-echo (or text "Follow the link")
7907                          :keymap gnus-url-button-map
7908                          :button-keymap gnus-widget-button-keymap))
7909
7910 (defun gnus-article-copy-string ()
7911   "Copy the string in the button to the kill ring."
7912   (interactive)
7913   (gnus-article-check-buffer)
7914   (let ((data (get-text-property (point) 'gnus-string)))
7915     (when data
7916       (with-temp-buffer
7917         (insert data)
7918         (copy-region-as-kill (point-min) (point-max))
7919         (message "Copied %s" data)))))
7920
7921 ;;; Internal functions:
7922
7923 (defun gnus-article-set-globals ()
7924   (with-current-buffer gnus-summary-buffer
7925     (gnus-set-global-variables)))
7926
7927 (defun gnus-signature-toggle (end)
7928   (gnus-with-article-buffer
7929     (let ((inhibit-point-motion-hooks t))
7930       (if (text-property-any end (point-max) 'article-type 'signature)
7931           (progn
7932             (gnus-delete-wash-type 'signature)
7933             (gnus-remove-text-properties-when
7934              'article-type 'signature end (point-max)
7935              (cons 'article-type (cons 'signature
7936                                        gnus-hidden-properties))))
7937         (gnus-add-wash-type 'signature)
7938         (gnus-add-text-properties-when
7939          'article-type nil end (point-max)
7940          (cons 'article-type (cons 'signature
7941                                    gnus-hidden-properties)))))
7942     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7943       (gnus-set-mode-line 'article))))
7944
7945 (defun gnus-button-push (marker-and-entry)
7946   ;; Push button starting at MARKER.
7947   (save-excursion
7948     (let* ((marker (car marker-and-entry))
7949            (entry (cadr marker-and-entry))
7950            (regexp (car entry))
7951            (inhibit-point-motion-hooks t))
7952       (goto-char marker)
7953       ;; This is obviously true, or something bad is happening :)
7954       ;; But we need it to have the match-data
7955       (when (looking-at (or (if (symbolp regexp)
7956                                 (symbol-value regexp)
7957                               regexp)))
7958         (let ((fun (nth 3 entry))
7959               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7960                              (get-char-property marker 'gnus-button-url))
7961                         (mapcar (lambda (group)
7962                                   (let ((string (match-string group)))
7963                                     (set-text-properties
7964                                      0 (length string) nil string)
7965                                     string))
7966                                 (nthcdr 4 entry)))))
7967
7968           (cond
7969            ((fboundp fun)
7970             (apply fun args))
7971            ((and (boundp fun)
7972                  (fboundp (symbol-value fun)))
7973             (apply (symbol-value fun) args))
7974            (t
7975             (gnus-message 1 "You must define `%S' to use this button"
7976                           (cons fun args)))))))))
7977
7978 (defun gnus-parse-news-url (url)
7979   (let (scheme server port group message-id articles)
7980     (with-temp-buffer
7981       (insert url)
7982       (goto-char (point-min))
7983       (when (looking-at "\\([A-Za-z]+\\):")
7984         (setq scheme (match-string 1))
7985         (goto-char (match-end 0)))
7986       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7987         (setq server (match-string 1))
7988         (setq port (if (stringp (match-string 3))
7989                        (string-to-number (match-string 3))
7990                      (match-string 3)))
7991         (goto-char (match-end 0)))
7992
7993       (cond
7994        ((looking-at "\\(.*@.*\\)")
7995         (setq message-id (match-string 1)))
7996        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7997         (setq group (match-string 1)
7998               articles (split-string (match-string 2) "-")))
7999        ((looking-at "\\([^/]+\\)/?")
8000         (setq group (match-string 1)))
8001        (t
8002         (error "Unknown news URL syntax"))))
8003     (list scheme server port group message-id articles)))
8004
8005 (defun gnus-button-handle-news (url)
8006   "Fetch a news URL."
8007   (destructuring-bind (scheme server port group message-id articles)
8008       (gnus-parse-news-url url)
8009     (cond
8010      (message-id
8011       (with-current-buffer gnus-summary-buffer
8012         (if server
8013             (let ((gnus-refer-article-method
8014                    (nconc (list (list 'nntp server))
8015                           gnus-refer-article-method))
8016                   (nntp-port-number (or port "nntp")))
8017               (gnus-message 7 "Fetching %s with %s"
8018                             message-id gnus-refer-article-method)
8019               (gnus-summary-refer-article message-id))
8020           (gnus-summary-refer-article message-id))))
8021      (group
8022       (gnus-button-fetch-group url)))))
8023
8024 (defun gnus-button-patch (library line)
8025   "Visit an Emacs Lisp library LIBRARY on line LINE."
8026   (interactive)
8027   (let ((file (locate-library (file-name-nondirectory library))))
8028     (unless file
8029       (error "Couldn't find library %s" library))
8030     (find-file file)
8031     (goto-char (point-min))
8032     (forward-line (1- (string-to-number line)))))
8033
8034 (defun gnus-button-handle-man (url)
8035   "Fetch a man page."
8036   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8037   (when (eq gnus-button-man-handler 'woman)
8038     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8039   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8040   (funcall gnus-button-man-handler url))
8041
8042 (defun gnus-button-handle-info-url (url)
8043   "Fetch an info URL."
8044   (setq url (mm-subst-char-in-string ?+ ?\  url))
8045   (cond
8046    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8047     (gnus-info-find-node
8048      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8049                      "Gnus")
8050              ")" (gnus-url-unhex-string (match-string 2 url)))))
8051    ((string-match "([^)\"]+)[^\"]+" url)
8052     (setq url
8053           (gnus-replace-in-string
8054            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8055     (gnus-info-find-node url))
8056    (t (error "Can't parse %s" url))))
8057
8058 (defun gnus-button-handle-info-url-gnome (url)
8059   "Fetch GNOME style info URL."
8060   (setq url (mm-subst-char-in-string ?_ ?\  url))
8061   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8062       (gnus-info-find-node
8063        (concat "("
8064                (gnus-url-unhex-string
8065                  (match-string 1 url))
8066                ")"
8067                (or (gnus-url-unhex-string
8068                     (match-string 2 url))
8069                    "Top")))
8070     (error "Can't parse %s" url)))
8071
8072 (defun gnus-button-handle-info-url-kde (url)
8073   "Fetch KDE style info URL."
8074   (gnus-info-find-node (gnus-url-unhex-string url)))
8075
8076 ;; (info) will autoload info.el
8077 (declare-function Info-menu "info" (menu-item &optional fork))
8078 (declare-function Info-index-next "info" (num))
8079
8080 (defun gnus-button-handle-info-keystrokes (url)
8081   "Call `info' when pushing the corresponding URL button."
8082   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8083   (let (node indx comma)
8084     (if (string-match
8085          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8086                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8087                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8088                  "\\(?:[ \t\n,]*\\)\\)?")
8089          url)
8090         (setq node (match-string 2 url)
8091               indx (match-string 3 url))
8092       (error "Can't parse %s" url))
8093     (info)
8094     (Info-directory)
8095     (Info-menu node)
8096     (when (> (length indx) 0)
8097       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8098                             "\\([ \t\n,]*\\)")
8099                     indx)
8100       (setq comma (match-string 2 indx))
8101       (setq indx  (match-string 1 indx))
8102       (Info-index indx)
8103       (when comma
8104         (dotimes (i (with-temp-buffer
8105                       (insert comma)
8106                       ;; Note: the XEmacs version of `how-many' takes
8107                       ;; no optional argument.
8108                       (goto-char (point-min))
8109                       (how-many ",")))
8110           (Info-index-next 1)))
8111       nil)))
8112
8113 (autoload 'pgg-snarf-keys-region "pgg")
8114 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8115 (declare-function pgg-display-output-buffer "pgg" (start end status))
8116
8117 (defun gnus-button-openpgp (url)
8118   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8119   (with-temp-buffer
8120     (mm-url-insert-file-contents-external url)
8121     (pgg-snarf-keys-region (point-min) (point-max))
8122     (pgg-display-output-buffer nil nil nil)))
8123
8124 (defun gnus-button-message-id (message-id)
8125   "Fetch MESSAGE-ID."
8126   (with-current-buffer gnus-summary-buffer
8127     (gnus-summary-refer-article message-id)))
8128
8129 (defun gnus-button-fetch-group (address &rest ignore)
8130   "Fetch GROUP specified by ADDRESS."
8131   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8132                       address)
8133     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8134     ;; for nntp:// and news://
8135     (setq address (match-string 3 address)))
8136   (if (not (string-match "[:/]" address))
8137       ;; This is just a simple group url.
8138       (gnus-group-read-ephemeral-group address gnus-select-method)
8139     (if (not
8140          (string-match
8141           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8142           address))
8143         (error "Can't parse %s" address)
8144       (gnus-group-read-ephemeral-group
8145        (match-string 4 address)
8146        `(nntp ,(match-string 1 address)
8147               (nntp-address ,(match-string 1 address))
8148               (nntp-port-number ,(if (match-end 3)
8149                                      (match-string 3 address)
8150                                    "nntp")))
8151        nil nil nil
8152        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8153
8154 (defun gnus-url-parse-query-string (query &optional downcase)
8155   (let (retval pairs cur key val)
8156     (setq pairs (split-string query "&"))
8157     (while pairs
8158       (setq cur (car pairs)
8159             pairs (cdr pairs))
8160       (if (not (string-match "=" cur))
8161           nil                           ; Grace
8162         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8163               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8164         (if downcase
8165             (setq key (downcase key)))
8166         (setq cur (assoc key retval))
8167         (if cur
8168             (setcdr cur (cons val (cdr cur)))
8169           (setq retval (cons (list key val) retval)))))
8170     retval))
8171
8172 (defun gnus-url-mailto (url)
8173   ;; Send mail to someone
8174   (setq url (replace-regexp-in-string "\n" " " url))
8175   (when (string-match "mailto:/*\\(.*\\)" url)
8176     (setq url (substring url (match-beginning 1) nil)))
8177   (let (to args subject func)
8178     (setq args (gnus-url-parse-query-string
8179                 (if (string-match "^\\?" url)
8180                     (substring url 1)
8181                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8182                       (concat "to=" (match-string 1 url) "&"
8183                               (match-string 2 url))
8184                     (concat "to=" url))))
8185           subject (cdr-safe (assoc "subject" args)))
8186     (gnus-msg-mail)
8187     (while args
8188       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8189       (if (fboundp func)
8190           (funcall func)
8191         (message-position-on-field (caar args)))
8192       (insert (gnus-replace-in-string
8193                (mapconcat 'identity (reverse (cdar args)) ", ")
8194                "\r\n" "\n" t))
8195       (setq args (cdr args)))
8196     (if subject
8197         (message-goto-body)
8198       (message-goto-subject))))
8199
8200 (defun gnus-button-embedded-url (address)
8201   "Activate ADDRESS with `browse-url'."
8202   (browse-url (gnus-strip-whitespace address)))
8203
8204 ;;; Next/prev buttons in the article buffer.
8205
8206 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8207 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8208
8209 (defvar gnus-prev-page-map
8210   (let ((map (make-sparse-keymap)))
8211     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8212     (define-key map "\r" 'gnus-button-prev-page)
8213     map))
8214
8215 (defvar gnus-next-page-map
8216   (let ((map (make-sparse-keymap)))
8217     (define-key map gnus-mouse-2 'gnus-button-next-page)
8218     (define-key map "\r" 'gnus-button-next-page)
8219     map))
8220
8221 (defun gnus-insert-prev-page-button ()
8222   (let ((b (point)) e
8223         (inhibit-read-only t))
8224     (gnus-eval-format
8225      gnus-prev-page-line-format nil
8226      `(keymap ,gnus-prev-page-map
8227               gnus-prev t
8228               gnus-callback gnus-article-button-prev-page
8229               article-type annotation))
8230     (setq e (if (bolp)
8231                 ;; Exclude a newline.
8232                 (1- (point))
8233               (point)))
8234     (when gnus-article-button-face
8235       (gnus-overlay-put (gnus-make-overlay b e nil t)
8236                         'face gnus-article-button-face))
8237     (widget-convert-button
8238      'link b e
8239      :action 'gnus-button-prev-page
8240      :button-keymap gnus-prev-page-map)))
8241
8242 (defun gnus-button-next-page (&optional args more-args)
8243   "Go to the next page."
8244   (interactive)
8245   (let ((win (selected-window)))
8246     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8247     (gnus-article-next-page)
8248     (select-window win)))
8249
8250 (defun gnus-button-prev-page (&optional args more-args)
8251   "Go to the prev page."
8252   (interactive)
8253   (let ((win (selected-window)))
8254     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8255     (gnus-article-prev-page)
8256     (select-window win)))
8257
8258 (defun gnus-insert-next-page-button ()
8259   (let ((b (point)) e
8260         (inhibit-read-only t))
8261     (gnus-eval-format gnus-next-page-line-format nil
8262                       `(keymap ,gnus-next-page-map
8263                                gnus-next t
8264                                gnus-callback gnus-article-button-next-page
8265                                article-type annotation))
8266     (setq e (if (bolp)
8267                 ;; Exclude a newline.
8268                 (1- (point))
8269               (point)))
8270     (when gnus-article-button-face
8271       (gnus-overlay-put (gnus-make-overlay b e nil t)
8272                         'face gnus-article-button-face))
8273     (widget-convert-button
8274      'link b e
8275      :action 'gnus-button-next-page
8276      :button-keymap gnus-next-page-map)))
8277
8278 (defun gnus-article-button-next-page (arg)
8279   "Go to the next page."
8280   (interactive "P")
8281   (let ((win (selected-window)))
8282     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8283     (gnus-article-next-page)
8284     (select-window win)))
8285
8286 (defun gnus-article-button-prev-page (arg)
8287   "Go to the prev page."
8288   (interactive "P")
8289   (let ((win (selected-window)))
8290     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8291     (gnus-article-prev-page)
8292     (select-window win)))
8293
8294 (defvar gnus-decode-header-methods
8295   '(mail-decode-encoded-word-region)
8296   "List of methods used to decode headers.
8297
8298 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8299 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8300 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8301 whose names match REGEXP.
8302
8303 For example:
8304 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8305  mail-decode-encoded-word-region
8306  (\"chinese\" . rfc1843-decode-region))
8307 ")
8308
8309 (defvar gnus-decode-header-methods-cache nil)
8310
8311 (defun gnus-multi-decode-header (start end)
8312   "Apply the functions from `gnus-encoded-word-methods' that match."
8313   (unless (and gnus-decode-header-methods-cache
8314                (eq gnus-newsgroup-name
8315                    (car gnus-decode-header-methods-cache)))
8316     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8317     (dolist (x gnus-decode-header-methods)
8318       (if (symbolp x)
8319           (nconc gnus-decode-header-methods-cache (list x))
8320         (if (and gnus-newsgroup-name
8321                  (string-match (car x) gnus-newsgroup-name))
8322             (nconc gnus-decode-header-methods-cache
8323                    (list (cdr x)))))))
8324   (let ((xlist gnus-decode-header-methods-cache))
8325     (pop xlist)
8326     (save-restriction
8327       (narrow-to-region start end)
8328       (while xlist
8329         (funcall (pop xlist) (point-min) (point-max))))))
8330
8331 ;;;
8332 ;;; Treatment top-level handling.
8333 ;;;
8334
8335 (defvar gnus-inhibit-article-treatments nil)
8336
8337 (defun gnus-treat-article (gnus-treat-condition
8338                            &optional part-number total-parts gnus-treat-type)
8339   (let ((gnus-treat-length (- (point-max) (point-min)))
8340         (alist gnus-treatment-function-alist)
8341         (article-goto-body-goes-to-point-min-p t)
8342         (treated-type
8343          (or (not gnus-treat-type)
8344              (catch 'found
8345                (let ((list gnus-article-treat-types))
8346                  (while list
8347                    (when (string-match (pop list) gnus-treat-type)
8348                      (throw 'found t)))))))
8349         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8350         val elem)
8351     (gnus-run-hooks 'gnus-part-display-hook)
8352     (dolist (elem alist)
8353       (setq val
8354             (save-excursion
8355               (when (gnus-buffer-live-p gnus-summary-buffer)
8356                 (set-buffer gnus-summary-buffer))
8357               (symbol-value (car elem))))
8358       (when (and (or (consp val)
8359                      treated-type)
8360                  (or (not gnus-inhibit-article-treatments)
8361                      (eq gnus-treat-condition 'head))
8362                  (gnus-treat-predicate val)
8363                  (or (not (get (car elem) 'highlight))
8364                      highlightp))
8365         (save-restriction
8366           (funcall (cadr elem)))))))
8367
8368 ;; Dynamic variables.
8369 (defvar part-number)
8370 (defvar total-parts)
8371 (defvar gnus-treat-type)
8372 (defvar gnus-treat-condition)
8373 (defvar gnus-treat-length)
8374
8375 (defun gnus-treat-predicate (val)
8376   (cond
8377    ((null val)
8378     nil)
8379    (gnus-treat-condition
8380     (eq gnus-treat-condition val))
8381    ((and (listp val)
8382          (stringp (car val)))
8383     (apply 'gnus-or (mapcar `(lambda (s)
8384                                (string-match s ,(or gnus-newsgroup-name "")))
8385                             val)))
8386    ((listp val)
8387     (let ((pred (pop val)))
8388       (cond
8389        ((eq pred 'or)
8390         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8391        ((eq pred 'and)
8392         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8393        ((eq pred 'not)
8394         (not (gnus-treat-predicate (car val))))
8395        ((eq pred 'typep)
8396         (equal (car val) gnus-treat-type))
8397        (t
8398         (error "%S is not a valid predicate" pred)))))
8399    ((eq val t)
8400     t)
8401    ((eq val 'head)
8402     nil)
8403    ((eq val 'first)
8404     (eq part-number 1))
8405    ((eq val 'last)
8406     (eq part-number total-parts))
8407    ((numberp val)
8408     (< gnus-treat-length val))
8409    (t
8410     (error "%S is not a valid value" val))))
8411
8412 (defun gnus-article-encrypt-body (protocol &optional n)
8413   "Encrypt the article body."
8414   (interactive
8415    (list
8416     (or gnus-article-encrypt-protocol
8417         (gnus-completing-read "Encrypt protocol"
8418                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8419                               t))
8420     current-prefix-arg))
8421   ;; User might hit `K E' instead of `K e', so prompt once.
8422   (when (and gnus-article-encrypt-protocol
8423              gnus-novice-user)
8424     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8425       (error "Encrypt aborted")))
8426   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8427     (unless func
8428       (error "Can't find the encrypt protocol %s" protocol))
8429     (if (member gnus-newsgroup-name '("nndraft:delayed"
8430                                       "nndraft:drafts"
8431                                       "nndraft:queue"))
8432         (error "Can't encrypt the article in group %s"
8433                gnus-newsgroup-name))
8434     (gnus-summary-iterate n
8435       (with-current-buffer gnus-summary-buffer
8436         (let ((mail-parse-charset gnus-newsgroup-charset)
8437               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8438               (summary-buffer gnus-summary-buffer)
8439               references point)
8440           (gnus-set-global-variables)
8441           (when (gnus-group-read-only-p)
8442             (error "The current newsgroup does not support article encrypt"))
8443           (gnus-summary-show-article t)
8444           (setq references
8445               (or (mail-header-references gnus-current-headers) ""))
8446           (set-buffer gnus-article-buffer)
8447           (let* ((inhibit-read-only t)
8448                  (headers
8449                   (mapcar (lambda (field)
8450                             (and (save-restriction
8451                                    (message-narrow-to-head)
8452                                    (goto-char (point-min))
8453                                    (search-forward field nil t))
8454                                  (prog2
8455                                      (message-narrow-to-field)
8456                                      (buffer-string)
8457                                    (delete-region (point-min) (point-max))
8458                                    (widen))))
8459                           '("Content-Type:" "Content-Transfer-Encoding:"
8460                             "Content-Disposition:"))))
8461             (message-narrow-to-head)
8462             (message-remove-header "MIME-Version")
8463             (goto-char (point-max))
8464             (setq point (point))
8465             (insert (apply 'concat headers))
8466             (widen)
8467             (narrow-to-region point (point-max))
8468             (let ((message-options message-options))
8469               (message-options-set 'message-sender user-mail-address)
8470               (message-options-set 'message-recipients user-mail-address)
8471               (message-options-set 'message-sign-encrypt 'not)
8472               (funcall func))
8473             (goto-char (point-min))
8474             (insert "MIME-Version: 1.0\n")
8475             (widen)
8476             (gnus-summary-edit-article-done
8477              references nil summary-buffer t))
8478           (when gnus-keep-backlog
8479             (gnus-backlog-remove-article
8480              (car gnus-article-current) (cdr gnus-article-current)))
8481           (gnus-flush-original-article-buffer)
8482           (when gnus-use-cache
8483             (gnus-cache-update-article
8484              (car gnus-article-current) (cdr gnus-article-current))))))))
8485
8486 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8487   "The following specs can be used:
8488 %t  The security MIME type
8489 %i  Additional info
8490 %d  Details
8491 %D  Details if button is pressed")
8492
8493 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8494   "The following specs can be used:
8495 %t  The security MIME type
8496 %i  Additional info
8497 %d  Details
8498 %D  Details if button is pressed")
8499
8500 (defvar gnus-mime-security-button-line-format-alist
8501   '((?t gnus-tmp-type ?s)
8502     (?i gnus-tmp-info ?s)
8503     (?d gnus-tmp-details ?s)
8504     (?D gnus-tmp-pressed-details ?s)))
8505
8506 (defvar gnus-mime-security-button-commands
8507   '((gnus-article-press-button "\r" "Show Detail")
8508     (undefined "v")
8509     (undefined "t")
8510     (undefined "C")
8511     (gnus-mime-security-save-part "o" "Save...")
8512     (undefined "\C-o")
8513     (undefined "r")
8514     (undefined "d")
8515     (undefined "c")
8516     (undefined "i")
8517     (undefined "E")
8518     (undefined "e")
8519     (undefined "p")
8520     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8521     (undefined ".")))
8522
8523 (defvar gnus-mime-security-button-map
8524   (let ((map (make-sparse-keymap)))
8525     (define-key map gnus-mouse-2 'gnus-article-push-button)
8526     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8527     (dolist (c gnus-mime-security-button-commands)
8528       (define-key map (cadr c) (car c)))
8529     map))
8530
8531 (easy-menu-define
8532   gnus-mime-security-button-menu gnus-mime-security-button-map
8533   "Security button menu."
8534   `("Security Part"
8535     ,@(delq nil
8536             (mapcar (lambda (c)
8537                       (unless (eq (car c) 'undefined)
8538                         (vector (caddr c) (car c) :active t)))
8539                     gnus-mime-security-button-commands))))
8540
8541 (defun gnus-mime-security-button-menu (event prefix)
8542   "Construct a context-sensitive menu of security commands."
8543   (interactive "e\nP")
8544   (save-window-excursion
8545     (let ((pos (event-start event)))
8546       (select-window (posn-window pos))
8547       (goto-char (posn-point pos))
8548       (gnus-article-check-buffer)
8549       (popup-menu gnus-mime-security-button-menu nil prefix))))
8550
8551 (defvar gnus-mime-security-details-buffer nil)
8552
8553 (defvar gnus-mime-security-button-pressed nil)
8554
8555 (defvar gnus-mime-security-show-details-inline t
8556   "If non-nil, show details in the article buffer.")
8557
8558 (defun gnus-mime-security-verify-or-decrypt (handle)
8559   (mm-remove-parts (cdr handle))
8560   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8561         point (inhibit-read-only t))
8562     (if region
8563         (goto-char (car region)))
8564     (setq point (point))
8565     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8566       (let* ((mm-verify-option 'known)
8567              (mm-decrypt-option 'known)
8568              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8569         (unless (eq nparts (cdr handle))
8570           (mm-destroy-parts (cdr handle))
8571           (setcdr handle nparts))))
8572     (gnus-mime-display-security handle)
8573     (when region
8574       (delete-region (point) (cdr region))
8575       (set-marker (car region) nil)
8576       (set-marker (cdr region) nil))
8577     (goto-char point)))
8578
8579 (defun gnus-mime-security-show-details (handle)
8580   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8581     (if (not details)
8582         (gnus-message 5 "No details.")
8583       (if gnus-mime-security-show-details-inline
8584           (let ((gnus-mime-security-button-pressed
8585                  (not (get-text-property (point) 'gnus-mime-details)))
8586                 (gnus-mime-security-button-line-format
8587                  (get-text-property (point) 'gnus-line-format))
8588                 (inhibit-read-only t))
8589             (forward-char -1)
8590             (while (eq (get-text-property (point) 'gnus-line-format)
8591                        gnus-mime-security-button-line-format)
8592               (forward-char -1))
8593             (forward-char)
8594             (save-restriction
8595               (narrow-to-region (point) (point))
8596               (gnus-insert-mime-security-button handle))
8597             (delete-region (point)
8598                            (or (text-property-not-all
8599                                 (point) (point-max)
8600                                 'gnus-line-format
8601                                 gnus-mime-security-button-line-format)
8602                                (point-max))))
8603         ;; Not inlined.
8604         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8605             (with-current-buffer gnus-mime-security-details-buffer
8606               (erase-buffer)
8607               t)
8608           (setq gnus-mime-security-details-buffer
8609                 (gnus-get-buffer-create "*MIME Security Details*")))
8610         (with-current-buffer gnus-mime-security-details-buffer
8611           (insert details)
8612           (goto-char (point-min)))
8613         (pop-to-buffer gnus-mime-security-details-buffer)))))
8614
8615 (defun gnus-mime-security-press-button (handle)
8616   (save-excursion
8617     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8618         (gnus-mime-security-show-details handle)
8619       (gnus-mime-security-verify-or-decrypt handle))))
8620
8621 (defun gnus-insert-mime-security-button (handle &optional displayed)
8622   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8623          (gnus-tmp-type
8624           (concat
8625            (or (nth 2 (assoc protocol mm-verify-function-alist))
8626                (nth 2 (assoc protocol mm-decrypt-function-alist))
8627                "Unknown")
8628            (if (equal (car handle) "multipart/signed")
8629                " Signed" " Encrypted")
8630            " Part"))
8631          (gnus-tmp-info
8632           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8633               "Undecided"))
8634          (gnus-tmp-details
8635           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8636          gnus-tmp-pressed-details
8637          b e)
8638     (setq gnus-tmp-details
8639           (if gnus-tmp-details
8640               (concat "\n" gnus-tmp-details)
8641             ""))
8642     (setq gnus-tmp-pressed-details
8643           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8644     (unless (bolp)
8645       (insert "\n"))
8646     (setq b (point))
8647     (gnus-eval-format
8648      gnus-mime-security-button-line-format
8649      gnus-mime-security-button-line-format-alist
8650      `(keymap ,gnus-mime-security-button-map
8651          gnus-callback gnus-mime-security-press-button
8652          gnus-line-format ,gnus-mime-security-button-line-format
8653          gnus-mime-details ,gnus-mime-security-button-pressed
8654          article-type annotation
8655          gnus-data ,handle))
8656     (setq e (if (bolp)
8657                 ;; Exclude a newline.
8658                 (1- (point))
8659               (point)))
8660     (when gnus-article-button-face
8661       (gnus-overlay-put (gnus-make-overlay b e nil t)
8662                         'face gnus-article-button-face))
8663     (widget-convert-button
8664      'link b e
8665      :mime-handle handle
8666      :action 'gnus-widget-press-button
8667      :button-keymap gnus-mime-security-button-map
8668      :help-echo
8669      (lambda (widget)
8670        ;; Needed to properly clear the message due to a bug in
8671        ;; wid-edit (XEmacs only).
8672        (when (boundp 'help-echo-owns-message)
8673          (setq help-echo-owns-message t))
8674        (format
8675         "%S: show detail; %S: more options"
8676         (aref gnus-mouse-2 0)
8677         (aref gnus-down-mouse-3 0))))))
8678
8679 (defun gnus-mime-display-security (handle)
8680   (save-restriction
8681     (narrow-to-region (point) (point))
8682     (unless (gnus-unbuttonized-mime-type-p (car handle))
8683       (gnus-insert-mime-security-button handle))
8684     (gnus-mime-display-part (cadr handle))
8685     (unless (bolp)
8686       (insert "\n"))
8687     (unless (gnus-unbuttonized-mime-type-p (car handle))
8688       (let ((gnus-mime-security-button-line-format
8689              gnus-mime-security-button-end-line-format))
8690         (gnus-insert-mime-security-button handle)))
8691     (mm-set-handle-multipart-parameter
8692      handle 'gnus-region
8693      (cons (set-marker (make-marker) (point-min))
8694            (set-marker (make-marker) (point-max))))
8695     (goto-char (point-max))))
8696
8697 (defun gnus-mime-security-run-function (function)
8698   "Run FUNCTION with the security part under point."
8699   (gnus-article-check-buffer)
8700   (let ((data (get-text-property (point) 'gnus-data))
8701         buffer handle)
8702     (when (and (stringp (car-safe data))
8703                (setq buffer (mm-handle-multipart-original-buffer data))
8704                (setq handle (cadr data)))
8705       (if (bufferp (mm-handle-buffer handle))
8706           (progn
8707             (setq handle (cons buffer (copy-sequence (cdr handle))))
8708             (mm-handle-set-undisplayer handle nil))
8709         (setq handle (mm-make-handle
8710                       buffer
8711                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8712                       nil nil nil nil nil nil)))
8713       (funcall function handle))))
8714
8715 (defun gnus-mime-security-save-part ()
8716   "Save the security part under point."
8717   (interactive)
8718   (gnus-mime-security-run-function 'mm-save-part))
8719
8720 (defun gnus-mime-security-pipe-part ()
8721   "Pipe the security part under point to a process."
8722   (interactive)
8723   (gnus-mime-security-run-function 'mm-pipe-part))
8724
8725 (gnus-ems-redefine)
8726
8727 (provide 'gnus-art)
8728
8729 (run-hooks 'gnus-art-load-hook)
8730
8731 ;;; gnus-art.el ends here