gnus-art.el (gnus-article-describe-bindings): Add a comment
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2013 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   [?\S-\ ] gnus-article-goto-prev-page
4366   "\177" gnus-article-goto-prev-page
4367   [delete] gnus-article-goto-prev-page
4368   [backspace] gnus-article-goto-prev-page
4369   "\C-c^" gnus-article-refer-article
4370   "h" gnus-article-show-summary
4371   "s" gnus-article-show-summary
4372   "\C-c\C-m" gnus-article-mail
4373   "?" gnus-article-describe-briefly
4374   "<" beginning-of-buffer
4375   ">" end-of-buffer
4376   "\C-c\C-i" gnus-info-find-node
4377   "\C-c\C-b" gnus-bug
4378   "R" gnus-article-reply-with-original
4379   "F" gnus-article-followup-with-original
4380   "\C-hk" gnus-article-describe-key
4381   "\C-hc" gnus-article-describe-key-briefly
4382   "\C-hb" gnus-article-describe-bindings
4383
4384   "e" gnus-article-read-summary-keys
4385   "\C-d" gnus-article-read-summary-keys
4386   "\M-*" gnus-article-read-summary-keys
4387   "\M-#" gnus-article-read-summary-keys
4388   "\M-^" gnus-article-read-summary-keys
4389   "\M-g" gnus-article-read-summary-keys)
4390
4391 (substitute-key-definition
4392  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4393
4394 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4395   "W" gnus-article-wide-reply-with-original)
4396 (if (featurep 'xemacs)
4397     (set-keymap-default-binding gnus-article-send-map
4398                                 'gnus-article-read-summary-send-keys)
4399   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4400
4401 (defun gnus-article-make-menu-bar ()
4402   (unless (boundp 'gnus-article-commands-menu)
4403     (gnus-summary-make-menu-bar))
4404   (unless (boundp 'gnus-article-article-menu)
4405     (easy-menu-define
4406      gnus-article-article-menu gnus-article-mode-map ""
4407      '("Article"
4408        ["Scroll forwards" gnus-article-goto-next-page t]
4409        ["Scroll backwards" gnus-article-goto-prev-page t]
4410        ["Show summary" gnus-article-show-summary t]
4411        ["Fetch Message-ID at point" gnus-article-refer-article t]
4412        ["Mail to address at point" gnus-article-mail t]
4413        ["Send a bug report" gnus-bug t]))
4414
4415     (easy-menu-define
4416      gnus-article-treatment-menu gnus-article-mode-map ""
4417      ;; Fixme: this should use :active (and maybe :visible).
4418      '("Treatment"
4419        ["Hide headers" gnus-article-hide-headers t]
4420        ["Hide signature" gnus-article-hide-signature t]
4421        ["Hide citation" gnus-article-hide-citation t]
4422        ["Treat overstrike" gnus-article-treat-overstrike t]
4423        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4424        ["Remove carriage return" gnus-article-remove-cr t]
4425        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4426        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4427        ["Remove base64" gnus-article-de-base64-unreadable t]
4428        ["Treat html" gnus-article-wash-html t]
4429        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4430        ["Decode HZ" gnus-article-decode-HZ t]))
4431
4432     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4433
4434     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4435
4436     (gnus-run-hooks 'gnus-article-menu-hook)))
4437
4438 (defvar bookmark-make-record-function)
4439 (defvar shr-put-image-function)
4440
4441 (defun gnus-article-mode ()
4442   "Major mode for displaying an article.
4443
4444 All normal editing commands are switched off.
4445
4446 The following commands are available in addition to all summary mode
4447 commands:
4448 \\<gnus-article-mode-map>
4449 \\[gnus-article-next-page]\t Scroll the article one page forwards
4450 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4451 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4452 \\[gnus-article-show-summary]\t Display the summary buffer
4453 \\[gnus-article-mail]\t Send a reply to the address near point
4454 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4455 \\[gnus-info-find-node]\t Go to the Gnus info node"
4456   (interactive)
4457   (kill-all-local-variables)
4458   (gnus-simplify-mode-line)
4459   (setq mode-name "Article")
4460   (setq major-mode 'gnus-article-mode)
4461   (make-local-variable 'minor-mode-alist)
4462   (use-local-map gnus-article-mode-map)
4463   (when (gnus-visual-p 'article-menu 'menu)
4464     (gnus-article-make-menu-bar)
4465     (when gnus-summary-tool-bar-map
4466       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4467   (gnus-update-format-specifications nil 'article-mode)
4468   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4469   (set (make-local-variable 'gnus-page-broken) nil)
4470   (make-local-variable 'gnus-article-current-summary)
4471   (make-local-variable 'gnus-article-mime-handles)
4472   (make-local-variable 'gnus-article-decoded-p)
4473   (make-local-variable 'gnus-article-mime-handle-alist)
4474   (make-local-variable 'gnus-article-wash-types)
4475   (make-local-variable 'gnus-article-image-alist)
4476   (make-local-variable 'gnus-article-charset)
4477   (make-local-variable 'gnus-article-ignored-charsets)
4478   (set (make-local-variable 'bookmark-make-record-function)
4479        'gnus-summary-bookmark-make-record)
4480   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4481   ;; face.
4482   (set (make-local-variable 'nobreak-char-display) nil)
4483   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4484   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4485   (setq cursor-in-non-selected-windows nil)
4486   (gnus-set-default-directory)
4487   (buffer-disable-undo)
4488   (setq buffer-read-only t
4489         show-trailing-whitespace nil)
4490   (set-syntax-table gnus-article-mode-syntax-table)
4491   (mm-enable-multibyte)
4492   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4493
4494 (defun gnus-article-setup-buffer ()
4495   "Initialize the article buffer."
4496   (let* ((name (if gnus-single-article-buffer "*Article*"
4497                  (concat "*Article "
4498                          (gnus-group-decoded-name gnus-newsgroup-name)
4499                          "*")))
4500          (original
4501           (progn (string-match "\\*Article" name)
4502                  (concat " *Original Article"
4503                          (substring name (match-end 0))))))
4504     (setq gnus-article-buffer name)
4505     (setq gnus-original-article-buffer original)
4506     (setq gnus-article-mime-handle-alist nil)
4507     (with-current-buffer gnus-summary-buffer
4508       ;; This might be a variable local to the summary buffer.
4509       (unless gnus-single-article-buffer
4510         (setq gnus-article-buffer name)
4511         (setq gnus-original-article-buffer original)
4512         (gnus-set-global-variables)))
4513     (gnus-article-setup-highlight-words)
4514     ;; Init original article buffer.
4515     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4516       (mm-enable-multibyte)
4517       (setq major-mode 'gnus-original-article-mode)
4518       (make-local-variable 'gnus-original-article))
4519     (if (and (get-buffer name)
4520              (with-current-buffer name
4521                (if gnus-article-edit-mode
4522                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4523                        (progn
4524                          (set-buffer-modified-p nil)
4525                          (gnus-kill-buffer name)
4526                          (message "")
4527                          nil)
4528                      (error "Action aborted"))
4529                  t)))
4530         (with-current-buffer name
4531           (set (make-local-variable 'gnus-article-edit-mode) nil)
4532           (gnus-article-stop-animations)
4533           (when gnus-article-mime-handles
4534             (mm-destroy-parts gnus-article-mime-handles)
4535             (setq gnus-article-mime-handles nil))
4536           ;; Set it to nil in article-buffer!
4537           (setq gnus-article-mime-handle-alist nil)
4538           (buffer-disable-undo)
4539           (setq buffer-read-only t)
4540           (unless (eq major-mode 'gnus-article-mode)
4541             (gnus-article-mode))
4542           (setq truncate-lines gnus-article-truncate-lines)
4543           (current-buffer))
4544       (let ((summary gnus-summary-buffer))
4545         (with-current-buffer (gnus-get-buffer-create name)
4546           (gnus-article-mode)
4547           (setq truncate-lines gnus-article-truncate-lines)
4548           (set (make-local-variable 'gnus-summary-buffer) summary)
4549           (gnus-summary-set-local-parameters gnus-newsgroup-name)
4550           (when article-lapsed-timer
4551             (gnus-stop-date-timer))
4552           (when gnus-article-update-date-headers
4553             (gnus-start-date-timer gnus-article-update-date-headers))
4554           (current-buffer))))))
4555
4556 (defun gnus-article-stop-animations ()
4557   (dolist (timer (and (boundp 'timer-list)
4558                       timer-list))
4559     (when (eq (gnus-timer--function timer) 'image-animate-timeout)
4560       (cancel-timer timer))))
4561
4562 (defun gnus-stop-downloads ()
4563   (when (boundp 'url-queue)
4564     (set (intern "url-queue" obarray) nil)))
4565
4566 ;; Set article window start at LINE, where LINE is the number of lines
4567 ;; from the head of the article.
4568 (defun gnus-article-set-window-start (&optional line)
4569   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4570     (when article-window
4571       (set-window-start
4572        article-window
4573        (with-current-buffer gnus-article-buffer
4574          (goto-char (point-min))
4575          (if (not line)
4576              (point-min)
4577            (gnus-message 6 "Moved to bookmark")
4578            (search-forward "\n\n" nil t)
4579            (forward-line line)
4580            (point)))))))
4581
4582 (defun gnus-article-prepare (article &optional all-headers header)
4583   "Prepare ARTICLE in article mode buffer.
4584 ARTICLE should either be an article number or a Message-ID.
4585 If ARTICLE is an id, HEADER should be the article headers.
4586 If ALL-HEADERS is non-nil, no headers are hidden."
4587   (save-excursion
4588     ;; Make sure we start in a summary buffer.
4589     (unless (eq major-mode 'gnus-summary-mode)
4590       (set-buffer gnus-summary-buffer))
4591     (setq gnus-summary-buffer (current-buffer))
4592     (let* ((gnus-article (if header (mail-header-number header) article))
4593            (summary-buffer (current-buffer))
4594            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4595            (group gnus-newsgroup-name)
4596            result)
4597       (save-excursion
4598         (gnus-article-setup-buffer)
4599         (set-buffer gnus-article-buffer)
4600         ;; Deactivate active regions.
4601         (when (and (boundp 'transient-mark-mode)
4602                    transient-mark-mode)
4603           (setq mark-active nil))
4604         (if (not (setq result (let ((inhibit-read-only t))
4605                                 (gnus-request-article-this-buffer
4606                                  article group))))
4607             ;; There is no such article.
4608             (save-excursion
4609               (when (and (numberp article)
4610                          (not (memq article gnus-newsgroup-sparse)))
4611                 (setq gnus-article-current
4612                       (cons gnus-newsgroup-name article))
4613                 (set-buffer gnus-summary-buffer)
4614                 (setq gnus-current-article article)
4615                 (if (and (memq article gnus-newsgroup-undownloaded)
4616                          (not (gnus-online (gnus-find-method-for-group
4617                                             gnus-newsgroup-name))))
4618                     (progn
4619                       (gnus-summary-set-agent-mark article)
4620                       (message "Message marked for downloading"))
4621                   (gnus-summary-mark-article article gnus-canceled-mark)
4622                   (unless (memq article gnus-newsgroup-sparse)
4623                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4624           (if (or (eq result 'pseudo)
4625                   (eq result 'nneething))
4626               (progn
4627                 (with-current-buffer summary-buffer
4628                   (push article gnus-newsgroup-history)
4629                   (setq gnus-last-article gnus-current-article
4630                         gnus-current-article 0
4631                         gnus-current-headers nil
4632                         gnus-article-current nil)
4633                   (if (eq result 'nneething)
4634                       (gnus-configure-windows 'summary)
4635                     (gnus-configure-windows 'article))
4636                   (gnus-set-global-variables))
4637                 (let ((gnus-article-mime-handle-alist-1
4638                        gnus-article-mime-handle-alist))
4639                   (gnus-set-mode-line 'article)))
4640             ;; The result from the `request' was an actual article -
4641             ;; or at least some text that is now displayed in the
4642             ;; article buffer.
4643             (when (and (numberp article)
4644                        (not (eq article gnus-current-article)))
4645               ;; Seems like a new article has been selected.
4646               ;; `gnus-current-article' must be an article number.
4647               (with-current-buffer summary-buffer
4648                 (push article gnus-newsgroup-history)
4649                 (setq gnus-last-article gnus-current-article
4650                       gnus-current-article article
4651                       gnus-current-headers
4652                       (gnus-summary-article-header gnus-current-article)
4653                       gnus-article-current
4654                       (cons gnus-newsgroup-name gnus-current-article))
4655                 (unless (vectorp gnus-current-headers)
4656                   (setq gnus-current-headers nil))
4657                 (gnus-summary-goto-subject gnus-current-article)
4658                 (when (gnus-summary-show-thread)
4659                   ;; If the summary buffer really was folded, the
4660                   ;; previous goto may not actually have gone to
4661                   ;; the right article, but the thread root instead.
4662                   ;; So we go again.
4663                   (gnus-summary-goto-subject gnus-current-article))
4664                 (gnus-run-hooks 'gnus-mark-article-hook)
4665                 (gnus-set-mode-line 'summary)
4666                 (when (gnus-visual-p 'article-highlight 'highlight)
4667                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4668                 ;; Set the global newsgroup variables here.
4669                 (gnus-set-global-variables)
4670                 (setq gnus-have-all-headers
4671                       (or all-headers gnus-show-all-headers))))
4672             (save-excursion
4673               (gnus-configure-windows 'article))
4674             (when (or (numberp article)
4675                       (stringp article))
4676               (gnus-article-prepare-display)
4677               ;; Do page break.
4678               (goto-char (point-min))
4679               (when gnus-break-pages
4680                 (gnus-narrow-to-page)))
4681             (let ((gnus-article-mime-handle-alist-1
4682                    gnus-article-mime-handle-alist))
4683               (gnus-set-mode-line 'article))
4684             (article-goto-body)
4685             (unless (bobp)
4686               (forward-line -1))
4687             (set-window-point (get-buffer-window (current-buffer)) (point))
4688             (gnus-configure-windows 'article)
4689             (gnus-run-hooks 'gnus-article-prepare-hook)
4690             t))))))
4691
4692 ;;;###autoload
4693 (defun gnus-article-prepare-display ()
4694   "Make the current buffer look like a nice article."
4695   ;; Hooks for getting information from the article.
4696   ;; This hook must be called before being narrowed.
4697   (let ((gnus-article-buffer (current-buffer))
4698         buffer-read-only
4699         (inhibit-read-only t))
4700     (unless (eq major-mode 'gnus-article-mode)
4701       (gnus-article-mode))
4702     (setq buffer-read-only nil
4703           gnus-article-wash-types nil
4704           gnus-article-image-alist nil)
4705     (gnus-run-hooks 'gnus-tmp-internal-hook)
4706     (when gnus-display-mime-function
4707       (funcall gnus-display-mime-function))))
4708
4709 ;;;
4710 ;;; Gnus Sticky Article Mode
4711 ;;;
4712
4713 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4714   "Mode for sticky articles."
4715   ;; Release bindings that won't work.
4716   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4717                              gnus-sticky-article-mode-map)
4718   (substitute-key-definition 'gnus-article-refer-article 'undefined
4719                              gnus-sticky-article-mode-map)
4720   (dolist (k '("e" "h" "s" "F" "R"))
4721     (define-key gnus-sticky-article-mode-map k nil))
4722   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4723   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4724   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4725   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4726
4727 (defun gnus-sticky-article (arg)
4728   "Make the current article sticky.
4729 If a prefix ARG is given, ask for a name for this sticky article buffer."
4730   (interactive "P")
4731   (gnus-summary-show-thread)
4732   (gnus-summary-select-article nil nil 'pseudo)
4733   (let (new-art-buf-name)
4734     (gnus-eval-in-buffer-window gnus-article-buffer
4735       (setq new-art-buf-name
4736             (concat
4737              "*Sticky Article: "
4738              (if arg
4739                  (read-from-minibuffer "Sticky article buffer name: ")
4740                (gnus-with-article-headers
4741                  (gnus-article-goto-header "subject")
4742                  (setq new-art-buf-name
4743                        (buffer-substring-no-properties
4744                         (line-beginning-position) (line-end-position)))
4745                  (goto-char (point-min))
4746                  (gnus-article-goto-header "from")
4747                  (setq new-art-buf-name
4748                        (concat
4749                         new-art-buf-name ", "
4750                         (buffer-substring-no-properties
4751                          (line-beginning-position) (line-end-position))))
4752                  (goto-char (point-min))
4753                  (gnus-article-goto-header "date")
4754                  (setq new-art-buf-name
4755                        (concat
4756                         new-art-buf-name ", "
4757                         (buffer-substring-no-properties
4758                          (line-beginning-position) (line-end-position))))))
4759              "*"))
4760       (if (and (gnus-buffer-live-p new-art-buf-name)
4761                (with-current-buffer new-art-buf-name
4762                  (eq major-mode 'gnus-sticky-article-mode)))
4763           (switch-to-buffer new-art-buf-name)
4764         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4765       (gnus-sticky-article-mode))
4766     (setq gnus-article-buffer new-art-buf-name))
4767   (gnus-summary-recenter)
4768   (gnus-summary-position-point))
4769
4770 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4771   "Kill the given sticky article BUFFER.
4772 If none is given, assume the current buffer and kill it if it has
4773 `gnus-sticky-article-mode'."
4774   (interactive)
4775   (unless buffer
4776     (setq buffer (current-buffer)))
4777   (with-current-buffer buffer
4778     (when (eq major-mode 'gnus-sticky-article-mode)
4779       (gnus-kill-buffer buffer))))
4780
4781 (defun gnus-kill-sticky-article-buffers (arg)
4782   "Kill all sticky article buffers.
4783 If a prefix ARG is given, ask for confirmation."
4784   (interactive "P")
4785   (dolist (buf (gnus-buffers))
4786     (with-current-buffer buf
4787       (when (eq major-mode 'gnus-sticky-article-mode)
4788        (if (not arg)
4789            (gnus-kill-buffer buf)
4790          (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4791            (gnus-kill-buffer buf)))))))
4792
4793 ;;;
4794 ;;; Gnus MIME viewing functions
4795 ;;;
4796
4797 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4798   "Format of the MIME buttons.
4799
4800 Valid specifiers include:
4801 %t  The MIME type
4802 %T  MIME type, along with additional info
4803 %n  The `name' parameter
4804 %d  The description, if any
4805 %l  The length of the encoded part
4806 %p  The part identifier number
4807 %e  Dots if the part isn't displayed
4808
4809 General format specifiers can also be used.  See Info node
4810 `(gnus)Formatting Variables'.")
4811
4812 (defvar gnus-mime-button-line-format-alist
4813   '((?t gnus-tmp-type ?s)
4814     (?T gnus-tmp-type-long ?s)
4815     (?n gnus-tmp-name ?s)
4816     (?d gnus-tmp-description ?s)
4817     (?p gnus-tmp-id ?s)
4818     (?l gnus-tmp-length ?d)
4819     (?e gnus-tmp-dots ?s)))
4820
4821 (defvar gnus-mime-button-commands
4822   '((gnus-article-press-button "\r" "Toggle Display")
4823     (gnus-mime-view-part "v" "View Interactively...")
4824     (gnus-mime-view-part-as-type "t" "View As Type...")
4825     (gnus-mime-view-part-as-charset "C" "View As charset...")
4826     (gnus-mime-save-part "o" "Save...")
4827     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4828     (gnus-mime-replace-part "r" "Replace part")
4829     (gnus-mime-delete-part "d" "Delete part")
4830     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4831     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4832     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4833     (gnus-mime-view-part-externally "e" "View Externally")
4834     (gnus-mime-print-part "p" "Print")
4835     (gnus-mime-pipe-part "|" "Pipe To Command...")
4836     (gnus-mime-action-on-part "." "Take action on the part...")))
4837
4838 (defun gnus-article-mime-part-status ()
4839   (if gnus-article-mime-handle-alist-1
4840       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4841           " (1 part)"
4842         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4843     ""))
4844
4845 (defvar gnus-mime-button-map
4846   (let ((map (make-sparse-keymap)))
4847     (define-key map gnus-mouse-2 'gnus-article-push-button)
4848     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4849     (dolist (c gnus-mime-button-commands)
4850       (define-key map (cadr c) (car c)))
4851     map))
4852
4853 (easy-menu-define
4854   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4855   `("MIME Part"
4856     ,@(mapcar (lambda (c)
4857                 (vector (caddr c) (car c) :active t))
4858               gnus-mime-button-commands)))
4859
4860 (defvar gnus-url-button-commands
4861   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4862
4863 (defvar gnus-url-button-map
4864   (let ((map (make-sparse-keymap)))
4865     (dolist (c gnus-url-button-commands)
4866       (define-key map (cadr c) (car c)))
4867     map))
4868
4869 (easy-menu-define
4870   gnus-url-button-menu gnus-url-button-map "URL button menu."
4871   `("Url Button"
4872     ,@(mapcar (lambda (c)
4873                 (vector (caddr c) (car c) :active t))
4874               gnus-url-button-commands)))
4875
4876 (defmacro gnus-bind-safe-url-regexp (&rest body)
4877   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4878   `(let ((mm-w3m-safe-url-regexp
4879           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4880                                 (gnus-buffer-live-p
4881                                  gnus-article-current-summary))
4882                            (with-current-buffer gnus-article-current-summary
4883                              gnus-newsgroup-name)
4884                          gnus-newsgroup-name)))
4885             (if (cond ((not group)
4886                        ;; Maybe we're in a mml-preview buffer
4887                        ;; and no group is selected.
4888                        t)
4889                       ((stringp gnus-safe-html-newsgroups)
4890                        (string-match gnus-safe-html-newsgroups group))
4891                       ((consp gnus-safe-html-newsgroups)
4892                        (member group gnus-safe-html-newsgroups)))
4893                 nil
4894               mm-w3m-safe-url-regexp))))
4895      ,@body))
4896
4897 (defun gnus-mime-button-menu (event prefix)
4898  "Construct a context-sensitive menu of MIME commands."
4899  (interactive "e\nP")
4900  (save-window-excursion
4901    (let ((pos (event-start event)))
4902      (select-window (posn-window pos))
4903      (goto-char (posn-point pos))
4904      (gnus-article-check-buffer)
4905      (popup-menu gnus-mime-button-menu nil prefix))))
4906
4907 (defun gnus-mime-view-all-parts (&optional handles)
4908   "View all the MIME parts."
4909   (interactive)
4910   (with-current-buffer gnus-article-buffer
4911     (let ((handles (or handles gnus-article-mime-handles))
4912           (mail-parse-charset gnus-newsgroup-charset)
4913           (mail-parse-ignored-charsets
4914            (with-current-buffer gnus-summary-buffer
4915              gnus-newsgroup-ignored-charsets)))
4916       (when handles
4917         (mm-remove-parts handles)
4918         (goto-char (point-min))
4919         (or (search-forward "\n\n") (goto-char (point-max)))
4920         (let ((inhibit-read-only t))
4921           (delete-region (point) (point-max))
4922           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4923
4924 (defun gnus-article-jump-to-part (n)
4925   "Jump to MIME part N."
4926   (interactive "P")
4927   (let ((parts (with-current-buffer gnus-article-buffer
4928                  (length gnus-article-mime-handle-alist))))
4929     (when (zerop parts)
4930       (error "No such part"))
4931     (pop-to-buffer gnus-article-buffer)
4932     (or n
4933         (setq n (if (= parts 1)
4934                     1
4935                   (read-number (format "Jump to part (1..%s): " parts)))))
4936     (unless (and (integerp n) (<= n parts) (>= n 1))
4937       (setq n
4938             (progn
4939               (gnus-message 7 "Invalid part `%s', using %s instead."
4940                             n parts)
4941               parts)))
4942     (gnus-message 9 "Jumping to part %s." n)
4943     (cond ((>= gnus-auto-select-part 1)
4944            (while (and (<= n parts)
4945                        (not (gnus-article-goto-part n)))
4946              (setq n (1+ n))))
4947           ((< gnus-auto-select-part 0)
4948            (while (and (>= n 1)
4949                        (not (gnus-article-goto-part n)))
4950              (setq n (1- n))))
4951           (t
4952            (gnus-article-goto-part n)))))
4953
4954 (defvar gnus-mime-buttonized-part-id nil
4955   "ID of a mime part that should be buttonized.
4956 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4957
4958 (eval-when-compile
4959   (defsubst gnus-article-edit-part (handles &optional current-id)
4960     "Edit an article in order to delete a mime part.
4961 This function is exclusively used by `gnus-mime-save-part-and-strip'
4962 and `gnus-mime-delete-part', and not provided at run-time normally."
4963     (gnus-article-edit-article
4964      `(lambda ()
4965         (buffer-disable-undo)
4966         (erase-buffer)
4967         (let ((mail-parse-charset (or gnus-article-charset
4968                                       ',gnus-newsgroup-charset))
4969               (mail-parse-ignored-charsets
4970                (or gnus-article-ignored-charsets
4971                    ',gnus-newsgroup-ignored-charsets))
4972               (mbl mml-buffer-list))
4973           (setq mml-buffer-list nil)
4974           (insert-buffer-substring gnus-original-article-buffer)
4975           (mime-to-mml ',handles)
4976           (setq gnus-article-mime-handles nil)
4977           (let ((mbl1 mml-buffer-list))
4978             (setq mml-buffer-list mbl)
4979             (set (make-local-variable 'mml-buffer-list) mbl1))
4980           (gnus-make-local-hook 'kill-buffer-hook)
4981           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4982      `(lambda (no-highlight)
4983         (let ((mail-parse-charset (or gnus-article-charset
4984                                       ',gnus-newsgroup-charset))
4985               (message-options message-options)
4986               (message-options-set-recipient)
4987               (mail-parse-ignored-charsets
4988                (or gnus-article-ignored-charsets
4989                    ',gnus-newsgroup-ignored-charsets)))
4990           (mml-to-mime)
4991           (mml-destroy-buffers)
4992           (remove-hook 'kill-buffer-hook
4993                        'mml-destroy-buffers t)
4994           (kill-local-variable 'mml-buffer-list))
4995         (gnus-summary-edit-article-done
4996          ,(or (mail-header-references gnus-current-headers) "")
4997          ,(gnus-group-read-only-p)
4998          ,gnus-summary-buffer no-highlight))
4999      t)
5000     ;; Force buttonizing this part.
5001     (let ((gnus-mime-buttonized-part-id current-id))
5002       (gnus-article-edit-done))
5003     (gnus-configure-windows 'article)
5004     (when (and current-id (integerp gnus-auto-select-part))
5005       (gnus-article-jump-to-part
5006        (min (max (+ current-id gnus-auto-select-part) 1)
5007             (with-current-buffer gnus-article-buffer
5008               (length gnus-article-mime-handle-alist)))))))
5009
5010 (defun gnus-mime-replace-part (file)
5011   "Replace MIME part under point with an external body."
5012   ;; Useful if file has already been saved to disk
5013   (interactive
5014    (list
5015     (read-file-name "Replace MIME part with file: "
5016                     (or mm-default-directory default-directory)
5017                     nil nil)))
5018   (gnus-mime-save-part-and-strip file))
5019
5020 (defun gnus-mime-save-part-and-strip (&optional file)
5021   "Save the MIME part under point then replace it with an external body.
5022 If FILE is given, use it for the external part."
5023   (interactive)
5024   (gnus-article-check-buffer)
5025   (when (gnus-group-read-only-p)
5026     (error "The current group does not support deleting of parts"))
5027   (when (mm-complicated-handles gnus-article-mime-handles)
5028     (error "\
5029 The current article has a complicated MIME structure, giving up..."))
5030   (let* ((data (get-text-property (point) 'gnus-data))
5031          (id (get-text-property (point) 'gnus-part))
5032          param
5033          (handles gnus-article-mime-handles))
5034     (unless file
5035       (setq file
5036             (and data (mm-save-part data "Delete MIME part and save to: "))))
5037     (when file
5038       (with-current-buffer (mm-handle-buffer data)
5039         (erase-buffer)
5040         (insert "Content-Type: " (mm-handle-media-type data))
5041         (mml-insert-parameter-string (cdr (mm-handle-type data))
5042                                      '(charset))
5043         ;; Add a filename for the sake of saving the part again.
5044         (mml-insert-parameter
5045          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5046         (insert "\n")
5047         (insert "Content-ID: " (message-make-message-id) "\n")
5048         (insert "Content-Transfer-Encoding: binary\n")
5049         (insert "\n"))
5050       (setcdr data
5051               (cdr (mm-make-handle nil
5052                                    `("message/external-body"
5053                                      (access-type . "LOCAL-FILE")
5054                                      (name . ,file)))))
5055       ;; (set-buffer gnus-summary-buffer)
5056       (gnus-article-edit-part handles id))))
5057
5058 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5059 ;; parts...>') but with stripping would be nice.
5060
5061 (defun gnus-mime-delete-part ()
5062   "Delete the MIME part under point.
5063 Replace it with some information about the removed part."
5064   (interactive)
5065   (gnus-article-check-buffer)
5066   (when (gnus-group-read-only-p)
5067     (error "The current group does not support deleting of parts"))
5068   (when (mm-complicated-handles gnus-article-mime-handles)
5069     (error "\
5070 The current article has a complicated MIME structure, giving up..."))
5071   (when (or gnus-expert-user
5072             (gnus-yes-or-no-p "\
5073 Deleting parts may malfunction or destroy the article; continue? "))
5074     (let* ((data (get-text-property (point) 'gnus-data))
5075            (id (get-text-property (point) 'gnus-part))
5076            (handles gnus-article-mime-handles)
5077            (description
5078             (let ((desc (mm-handle-description data)))
5079               (when desc
5080                 (mail-decode-encoded-word-string desc))))
5081            (filename (or (mm-handle-filename data) "(none)"))
5082            (type (mm-handle-media-type data)))
5083       (unless data
5084         (error "No MIME part under point"))
5085       (with-current-buffer (mm-handle-buffer data)
5086         (let ((bsize (buffer-size)))
5087           (erase-buffer)
5088           (insert
5089            (concat
5090             ",----\n"
5091             "| The following attachment has been deleted:\n"
5092             "|\n"
5093             "| Type:           " type "\n"
5094             "| Filename:       " filename "\n"
5095             "| Size (encoded): " (format "%s byte%s\n"
5096                                          bsize (if (= bsize 1)
5097                                                    ""
5098                                                  "s"))
5099             (when description
5100               (concat    "| Description:    " description "\n"))
5101             "`----\n"))
5102           (setcdr data
5103                   (cdr (mm-make-handle
5104                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5105                         (list "attachment")
5106                         (format "Deleted attachment (%s bytes)" bsize))))))
5107       ;; (set-buffer gnus-summary-buffer)
5108       (gnus-article-edit-part handles id))))
5109
5110 (defun gnus-mime-save-part ()
5111   "Save the MIME part under point."
5112   (interactive)
5113   (gnus-article-check-buffer)
5114   (let ((data (get-text-property (point) 'gnus-data)))
5115     (when data
5116       (mm-save-part data))))
5117
5118 (defun gnus-mime-pipe-part (&optional cmd)
5119   "Pipe the MIME part under point to a process.
5120 Use CMD as the process."
5121   (interactive)
5122   (gnus-article-check-buffer)
5123   (let ((data (get-text-property (point) 'gnus-data)))
5124     (when data
5125       (mm-pipe-part data cmd))))
5126
5127 (defun gnus-mime-view-part ()
5128   "Interactively choose a viewing method for the MIME part under point."
5129   (interactive)
5130   (gnus-article-check-buffer)
5131   (let ((data (get-text-property (point) 'gnus-data)))
5132     (when data
5133       (setq gnus-article-mime-handles
5134             (mm-merge-handles
5135              gnus-article-mime-handles (setq data (copy-sequence data))))
5136       (mm-interactively-view-part data))))
5137
5138 (defun gnus-mime-view-part-as-type-internal ()
5139   (gnus-article-check-buffer)
5140   (let* ((handle (get-text-property (point) 'gnus-data))
5141          (name (or
5142                 ;; Content-Type: foo/bar; name=...
5143                 (mail-content-type-get (mm-handle-type handle) 'name)
5144                 ;; Content-Disposition: attachment; filename=...
5145                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5146          (def-type (and name (mm-default-file-encoding name))))
5147     (or (and def-type (cons def-type 0))
5148         (and handle
5149              (equal (mm-handle-media-supertype handle) "text")
5150              '("text/plain" . 0))
5151         '("application/octet-stream" . 0))))
5152
5153 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5154   "Choose a MIME media type, and view the part as such.
5155 If non-nil, PRED is a predicate to use during completion to limit the
5156 available media-types."
5157   (interactive)
5158   (unless mime-type
5159     (setq mime-type
5160           (let ((default (gnus-mime-view-part-as-type-internal)))
5161             (gnus-completing-read
5162              "View as MIME type"
5163              (if pred
5164                  (gnus-remove-if-not pred (mailcap-mime-types))
5165                (mailcap-mime-types))
5166              nil nil nil
5167              (car default)))))
5168   (gnus-article-check-buffer)
5169   (let ((handle (get-text-property (point) 'gnus-data)))
5170     (when handle
5171       (when (equal (mm-handle-media-type handle) "message/external-body")
5172         (unless (mm-handle-cache handle)
5173           (mm-extern-cache-contents handle))
5174         (setq handle (mm-handle-cache handle)))
5175       (setq handle
5176             (mm-make-handle (mm-handle-buffer handle)
5177                             (cons mime-type (cdr (mm-handle-type handle)))
5178                             (mm-handle-encoding handle)
5179                             (mm-handle-undisplayer handle)
5180                             (mm-handle-disposition handle)
5181                             (mm-handle-description handle)
5182                             nil
5183                             (mm-handle-id handle)))
5184       (setq gnus-article-mime-handles
5185             (mm-merge-handles gnus-article-mime-handles handle))
5186       (when (mm-handle-displayed-p handle)
5187         (mm-remove-part handle))
5188       (gnus-mm-display-part handle))))
5189
5190 (defun gnus-mime-copy-part (&optional handle arg)
5191   "Put the MIME part under point into a new buffer.
5192 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5193 are decompressed."
5194   (interactive (list nil current-prefix-arg))
5195   (gnus-article-check-buffer)
5196   (unless handle
5197     (setq handle (get-text-property (point) 'gnus-data)))
5198   (when handle
5199     (let ((filename (mm-handle-filename handle))
5200           contents dont-decode charset coding-system)
5201       (mm-with-unibyte-buffer
5202         (mm-insert-part handle)
5203         (setq contents (or (condition-case nil
5204                                (mm-decompress-buffer filename nil 'sig)
5205                              (error
5206                               (setq dont-decode t)
5207                               nil))
5208                            (buffer-string))))
5209       (setq filename (cond (filename (file-name-nondirectory filename))
5210                            (dont-decode "*raw data*")
5211                            (t "*decoded*")))
5212       (cond
5213        (dont-decode)
5214        ((not arg)
5215         (unless (setq charset (mail-content-type-get
5216                                (mm-handle-type handle) 'charset))
5217           (unless (setq coding-system (mm-with-unibyte-buffer
5218                                         (insert contents)
5219                                         (mm-find-buffer-file-coding-system)))
5220             (setq charset gnus-newsgroup-charset))))
5221        ((numberp arg)
5222         (setq charset (or (cdr (assq arg
5223                                      gnus-summary-show-article-charset-alist))
5224                           (mm-read-coding-system "Charset: ")))))
5225       (switch-to-buffer (generate-new-buffer filename))
5226       (if (or coding-system
5227               (and charset
5228                    (setq coding-system (mm-charset-to-coding-system charset))
5229                    (not (eq coding-system 'ascii))))
5230           (progn
5231             (mm-enable-multibyte)
5232             (insert (mm-decode-coding-string contents coding-system))
5233             (setq buffer-file-coding-system
5234                   (if (boundp 'last-coding-system-used)
5235                       (symbol-value 'last-coding-system-used)
5236                     coding-system)))
5237         (mm-disable-multibyte)
5238         (insert contents)
5239         (setq buffer-file-coding-system mm-binary-coding-system))
5240       ;; We do it this way to make `normal-mode' set the appropriate mode.
5241       (unwind-protect
5242           (progn
5243             (setq buffer-file-name (expand-file-name filename))
5244             (normal-mode))
5245         (setq buffer-file-name nil))
5246       (goto-char (point-min)))))
5247
5248 (defun gnus-mime-print-part (&optional handle filename)
5249   "Print the MIME part under point."
5250   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5251   (gnus-article-check-buffer)
5252   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5253          (contents (and handle (mm-get-part handle)))
5254          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5255          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5256     (when contents
5257         (if printer
5258             (unwind-protect
5259                 (progn
5260                   (mm-save-part-to-file handle file)
5261                   (call-process shell-file-name nil
5262                                 (generate-new-buffer " *mm*")
5263                                 nil
5264                                 shell-command-switch
5265                                 (mm-mailcap-command
5266                                  printer file (mm-handle-type handle))))
5267               (delete-file file))
5268           (with-temp-buffer
5269             (insert contents)
5270             (gnus-print-buffer))
5271           (ps-despool filename)))))
5272
5273 (defun gnus-mime-inline-part (&optional handle arg)
5274   "Insert the MIME part under point into the current buffer.
5275 Compressed files like .gz and .bz2 are decompressed."
5276   (interactive (list nil current-prefix-arg))
5277   (gnus-article-check-buffer)
5278   (unless handle
5279     (setq handle (get-text-property (point) 'gnus-data)))
5280   (when handle
5281     (let ((b (point))
5282           (inhibit-read-only t)
5283           contents charset coding-system)
5284       (if (and (not arg) (mm-handle-undisplayer handle))
5285           (mm-remove-part handle)
5286         (mm-with-unibyte-buffer
5287           (mm-insert-part handle)
5288           (setq contents
5289                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5290                     (buffer-string))))
5291         (cond
5292          ((not arg)
5293           (unless (setq charset (mail-content-type-get
5294                                  (mm-handle-type handle) 'charset))
5295             (unless (setq coding-system
5296                           (mm-with-unibyte-buffer
5297                             (insert contents)
5298                             (mm-find-buffer-file-coding-system)))
5299               (setq charset gnus-newsgroup-charset))))
5300          ((numberp arg)
5301           (if (mm-handle-undisplayer handle)
5302               (mm-remove-part handle))
5303           (setq charset
5304                 (or (cdr (assq arg
5305                                gnus-summary-show-article-charset-alist))
5306                     (mm-read-coding-system "Charset: "))))
5307          ((mm-handle-undisplayer handle)
5308           (mm-remove-part handle)))
5309         (forward-line 2)
5310         (mm-display-inline handle)
5311         (goto-char b)))))
5312
5313 (defun gnus-mime-set-charset-parameters (handle charset)
5314   "Set CHARSET to parameters in HANDLE.
5315 CHARSET may either be a string or a symbol."
5316   (unless (stringp charset)
5317     (setq charset (symbol-name charset)))
5318   (if (stringp (car handle))
5319       (dolist (h (cdr handle))
5320         (gnus-mime-set-charset-parameters h charset))
5321     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5322                                             "message/external-body")
5323                                      (progn
5324                                        (unless (mm-handle-cache handle)
5325                                          (mm-extern-cache-contents handle))
5326                                        (mm-handle-cache handle))
5327                                    handle)))
5328            (param (assq 'charset (cdr type))))
5329       (if param
5330           (setcdr param charset)
5331         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5332
5333 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5334   "Insert the MIME part under point into the current buffer using the
5335 specified charset."
5336   (interactive (list nil current-prefix-arg))
5337   (gnus-article-check-buffer)
5338   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5339         (fun (get-text-property (point) 'gnus-callback))
5340         (gnus-newsgroup-ignored-charsets 'gnus-all)
5341         charset form preferred parts)
5342     (when handle
5343       (when (prog1
5344                 (and fun
5345                      (setq charset
5346                            (or (cdr (assq
5347                                      arg
5348                                      gnus-summary-show-article-charset-alist))
5349                                (mm-read-coding-system "Charset: "))))
5350               (if (mm-handle-undisplayer handle)
5351                   (mm-remove-part handle)))
5352         (gnus-mime-set-charset-parameters handle charset)
5353         (when (and (consp (setq form (cdr-safe fun)))
5354                    (setq form (ignore-errors
5355                                 (assq 'gnus-mime-display-alternative form)))
5356                    (setq preferred (caddr form))
5357                    (progn
5358                      (when (eq (car preferred) 'quote)
5359                        (setq preferred (cadr preferred)))
5360                      (not (equal preferred
5361                                  (get-text-property (point) 'gnus-data))))
5362                    (setq parts (get-text-property (point) 'gnus-part))
5363                    (setq parts (cdr (assq parts
5364                                           gnus-article-mime-handle-alist)))
5365                    (equal (mm-handle-media-type parts) "multipart/alternative")
5366                    (setq parts (reverse (cdr parts))))
5367           (setcar (cddr form)
5368                   (list 'quote (or (cadr (member preferred parts))
5369                                    (car parts)))))
5370         (funcall fun handle)))))
5371
5372 (defun gnus-mime-view-part-externally (&optional handle)
5373   "View the MIME part under point with an external viewer."
5374   (interactive)
5375   (gnus-article-check-buffer)
5376   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5377          (mm-user-display-methods nil)
5378          (mm-inlined-types nil)
5379          (mail-parse-charset gnus-newsgroup-charset)
5380          (mail-parse-ignored-charsets
5381           (with-current-buffer gnus-summary-buffer
5382             gnus-newsgroup-ignored-charsets))
5383          (type (mm-handle-media-type handle))
5384          (method (mailcap-mime-info type))
5385          (mm-enable-external t))
5386     (if (not (stringp method))
5387         (gnus-mime-view-part-as-type
5388          nil (lambda (type) (stringp (mailcap-mime-info type))))
5389       (when handle
5390         (mm-display-part handle nil t)))))
5391
5392 (defun gnus-mime-view-part-internally (&optional handle)
5393   "View the MIME part under point with an internal viewer.
5394 If no internal viewer is available, use an external viewer."
5395   (interactive)
5396   (gnus-article-check-buffer)
5397   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5398          (mm-inlined-types '(".*"))
5399          (mm-inline-large-images t)
5400          (mail-parse-charset gnus-newsgroup-charset)
5401          (mail-parse-ignored-charsets
5402           (with-current-buffer gnus-summary-buffer
5403             gnus-newsgroup-ignored-charsets))
5404          (inhibit-read-only t))
5405     (if (not (mm-inlinable-p handle))
5406         (gnus-mime-view-part-as-type
5407          nil (lambda (type) (mm-inlinable-p handle type)))
5408       (when handle
5409         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5410
5411 (defun gnus-mime-action-on-part (&optional action)
5412   "Do something with the MIME attachment at \(point\)."
5413   (interactive
5414    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5415   (gnus-article-check-buffer)
5416   (let ((action-pair (assoc action gnus-mime-action-alist)))
5417     (if action-pair
5418         (funcall (cdr action-pair)))))
5419
5420 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5421   "Call FUNCTION on MIME part N.
5422 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5423 If INTERACTIVE, call FUNCTION interactively."
5424   (let (window frame)
5425     ;; Check whether the article is displayed.
5426     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5427                  (setq window (get-buffer-window gnus-article-buffer t))
5428                  (frame-visible-p (setq frame (window-frame window))))
5429       (error "No article is displayed"))
5430     (with-current-buffer gnus-article-buffer
5431       ;; Check whether the article displays the right contents.
5432       (unless (with-current-buffer gnus-summary-buffer
5433                 (eq gnus-current-article (gnus-summary-article-number)))
5434         (error "You should select the right article first"))
5435       (if n
5436           (setq n (prefix-numeric-value n))
5437         (let ((pt (point)))
5438           (setq n (or (get-text-property pt 'gnus-part)
5439                       (and (not (bobp))
5440                            (get-text-property (1- pt) 'gnus-part))
5441                       (get-text-property (prog2
5442                                              (forward-line 1)
5443                                              (point)
5444                                            (goto-char pt))
5445                                          'gnus-part)
5446                       (get-text-property
5447                        (or (and (setq pt (previous-single-property-change
5448                                           pt 'gnus-part))
5449                                 (1- pt))
5450                            (next-single-property-change (point) 'gnus-part)
5451                            (point))
5452                        'gnus-part)
5453                       1))))
5454       ;; Check whether the specified part exists.
5455       (when (> n (length gnus-article-mime-handle-alist))
5456         (error "No such part")))
5457     (unless
5458         (progn
5459           ;; To select the window is needed so that the cursor
5460           ;; might be visible on the MIME button.
5461           (select-window (prog1
5462                              window
5463                            (setq window (selected-window))
5464                            ;; Article may be displayed in the other frame.
5465                            (gnus-select-frame-set-input-focus
5466                             (prog1
5467                                 frame
5468                               (setq frame (selected-frame))))))
5469           (when (gnus-article-goto-part n)
5470             ;; We point the cursor and the arrow at the MIME button
5471             ;; when the `function' prompt the user for something.
5472             (unless (and (pos-visible-in-window-p)
5473                          (> (count-lines (point) (window-end))
5474                             (/ (1- (window-height)) 3)))
5475               (recenter (/ (1- (window-height)) 3)))
5476             (let ((cursor-in-non-selected-windows t)
5477                   (overlay-arrow-string "=>")
5478                   (overlay-arrow-position (point-marker)))
5479               (unwind-protect
5480                   (cond
5481                    ((and no-handle interactive)
5482                     (call-interactively function))
5483                    (no-handle
5484                     (funcall function))
5485                    (interactive
5486                     (call-interactively
5487                      function (get-text-property (point) 'gnus-data)))
5488                    (t
5489                     (funcall function
5490                              (get-text-property (point) 'gnus-data))))
5491                 (set-marker overlay-arrow-position nil)
5492                 (unless gnus-auto-select-part
5493                   (gnus-select-frame-set-input-focus frame)
5494                   (select-window window))))
5495             t))
5496       (if gnus-inhibit-mime-unbuttonizing
5497           ;; This is the default though the program shouldn't reach here.
5498           (error "No such part")
5499         ;; The part which doesn't have the MIME button is selected.
5500         ;; So, we display all the buttons and redo it.
5501         (let ((gnus-inhibit-mime-unbuttonizing t))
5502           (gnus-summary-show-article)
5503           (gnus-article-part-wrapper n function no-handle))))))
5504
5505 (defun gnus-article-pipe-part (n)
5506   "Pipe MIME part N, which is the numerical prefix."
5507   (interactive "P")
5508   (gnus-article-part-wrapper n 'mm-pipe-part))
5509
5510 (defun gnus-article-save-part (n)
5511   "Save MIME part N, which is the numerical prefix."
5512   (interactive "P")
5513   (gnus-article-part-wrapper n 'mm-save-part))
5514
5515 (defun gnus-article-interactively-view-part (n)
5516   "View MIME part N interactively, which is the numerical prefix."
5517   (interactive "P")
5518   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5519
5520 (defun gnus-article-copy-part (n)
5521   "Copy MIME part N, which is the numerical prefix."
5522   (interactive "P")
5523   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5524
5525 (defun gnus-article-view-part-as-charset (n)
5526   "View MIME part N using a specified charset.
5527 N is the numerical prefix."
5528   (interactive "P")
5529   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5530
5531 (defun gnus-article-view-part-externally (n)
5532   "View MIME part N externally, which is the numerical prefix."
5533   (interactive "P")
5534   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5535
5536 (defun gnus-article-inline-part (n)
5537   "Inline MIME part N, which is the numerical prefix."
5538   (interactive "P")
5539   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5540
5541 (defun gnus-article-save-part-and-strip (n)
5542   "Save MIME part N and replace it with an external body.
5543 N is the numerical prefix."
5544   (interactive "P")
5545   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5546
5547 (defun gnus-article-replace-part (n)
5548   "Replace MIME part N with an external body.
5549 N is the numerical prefix."
5550   (interactive "P")
5551   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5552
5553 (defun gnus-article-delete-part (n)
5554   "Delete MIME part N and add some information about the removed part.
5555 N is the numerical prefix."
5556   (interactive "P")
5557   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5558
5559 (defun gnus-article-view-part-as-type (n)
5560   "Choose a MIME media type, and view part N as such.
5561 N is the numerical prefix."
5562   (interactive "P")
5563   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5564
5565 (defun gnus-article-mime-match-handle-first (condition)
5566   (if condition
5567       (let (n)
5568         (dolist (ihandle gnus-article-mime-handle-alist)
5569           (if (and (cond
5570                     ((functionp condition)
5571                      (funcall condition (cdr ihandle)))
5572                     ((eq condition 'undisplayed)
5573                      (not (or (mm-handle-undisplayer (cdr ihandle))
5574                               (equal (mm-handle-media-type (cdr ihandle))
5575                                      "multipart/alternative"))))
5576                     ((eq condition 'undisplayed-alternative)
5577                      (not (mm-handle-undisplayer (cdr ihandle))))
5578                     (t t))
5579                    (gnus-article-goto-part (car ihandle))
5580                    (or (not n) (< (car ihandle) n)))
5581               (setq n (car ihandle))))
5582         (or n 1))
5583     1))
5584
5585 (defun gnus-article-view-part (&optional n)
5586   "View MIME part N, which is the numerical prefix.
5587 If the part is already shown, hide the part.  If N is nil, view
5588 all parts."
5589   (interactive "P")
5590   (with-current-buffer gnus-article-buffer
5591     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5592                              gnus-article-mime-match-handle-function)))
5593     (when (> n (length gnus-article-mime-handle-alist))
5594       (error "No such part"))
5595     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5596       (when (gnus-article-goto-part n)
5597         (if (equal (car handle) "multipart/alternative")
5598             (progn
5599               (beginning-of-line) ;; Make it toggle subparts
5600               (gnus-article-press-button))
5601           (when (eq (gnus-mm-display-part handle) 'internal)
5602             (gnus-set-window-start)))))))
5603
5604 (defsubst gnus-article-mime-total-parts ()
5605   (if (bufferp (car gnus-article-mime-handles))
5606       1 ;; single part
5607     (1- (length gnus-article-mime-handles))))
5608
5609 (defun gnus-mm-display-part (handle)
5610   "Display HANDLE and fix MIME button."
5611   (let ((id (get-text-property (point) 'gnus-part))
5612         (point (point))
5613         (inhibit-read-only t))
5614     (forward-line 1)
5615     (prog1
5616         (let ((window (selected-window))
5617               (mail-parse-charset gnus-newsgroup-charset)
5618               (mail-parse-ignored-charsets
5619                (if (gnus-buffer-live-p gnus-summary-buffer)
5620                    (with-current-buffer gnus-summary-buffer
5621                      gnus-newsgroup-ignored-charsets)
5622                  nil)))
5623           (save-excursion
5624             (unwind-protect
5625                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5626                       (beg (point)))
5627                   (when win
5628                     (select-window win))
5629                   (goto-char point)
5630                   (forward-line)
5631                   (if (mm-handle-displayed-p handle)
5632                       ;; This will remove the part.
5633                       (mm-display-part handle)
5634                     (save-restriction
5635                       (narrow-to-region (point)
5636                                         (if (eobp) (point) (1+ (point))))
5637                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5638                       ;; We narrow to the part itself and
5639                       ;; then call the treatment functions.
5640                       (goto-char (point-min))
5641                       (forward-line 1)
5642                       (narrow-to-region (point) (point-max))
5643                       (gnus-treat-article
5644                        nil id
5645                        (gnus-article-mime-total-parts)
5646                        (mm-handle-media-type handle)))))
5647               (if (window-live-p window)
5648                   (select-window window)))))
5649       (goto-char point)
5650       (gnus-delete-line)
5651       (gnus-insert-mime-button
5652        handle id (list (mm-handle-displayed-p handle)))
5653       (goto-char point))))
5654
5655 (defun gnus-article-goto-part (n)
5656   "Go to MIME part N."
5657   (when gnus-break-pages
5658     (widen))
5659   (prog1
5660       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5661             part handle end next handles)
5662         (when start
5663           (goto-char start)
5664           (if (setq handle (get-text-property start 'gnus-data))
5665               start
5666             ;; Go to the displayed subpart, assuming this is
5667             ;; multipart/alternative.
5668             (setq part start
5669                   end (point-at-eol))
5670             (while (and (not handle)
5671                         part
5672                         (< part end)
5673                         (setq next (text-property-not-all part end
5674                                                           'gnus-data nil)))
5675               (setq part next
5676                     handle (get-text-property part 'gnus-data))
5677               (push (cons handle part) handles)
5678               (unless (mm-handle-displayed-p handle)
5679                 (setq handle nil
5680                       part (text-property-any part end 'gnus-data nil))))
5681             (unless handle
5682               ;; No subpart is displayed, so we find preferred one.
5683               (setq part
5684                     (cdr (assq (mm-preferred-alternative
5685                                 (nreverse (mapcar 'car handles)))
5686                                handles))))
5687             (if part
5688                 (goto-char (1+ part))
5689               start))))
5690     (when gnus-break-pages
5691       (gnus-narrow-to-page))))
5692
5693 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5694   (let ((gnus-tmp-name
5695          (or (mm-handle-filename handle)
5696              (mail-content-type-get (mm-handle-type handle) 'url)
5697              ""))
5698         (gnus-tmp-type (mm-handle-media-type handle))
5699         (gnus-tmp-description (or (mm-handle-description handle) ""))
5700         (gnus-tmp-dots
5701          (if (if displayed (car displayed)
5702                (mm-handle-displayed-p handle))
5703              "" "..."))
5704         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5705                            (buffer-size)))
5706         gnus-tmp-type-long b e)
5707     (when (string-match ".*/" gnus-tmp-name)
5708       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5709     (setq gnus-tmp-type-long (concat gnus-tmp-type
5710                                      (and (not (equal gnus-tmp-name ""))
5711                                           (concat "; " gnus-tmp-name))))
5712     (unless (equal gnus-tmp-description "")
5713       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5714     (unless (bolp)
5715       (insert "\n"))
5716     (setq b (point))
5717     (gnus-eval-format
5718      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5719      `(keymap ,gnus-mime-button-map
5720               gnus-callback gnus-mm-display-part
5721               gnus-part ,gnus-tmp-id
5722               article-type annotation
5723               gnus-data ,handle
5724               rear-nonsticky t))
5725     (setq e (if (bolp)
5726                 ;; Exclude a newline.
5727                 (1- (point))
5728               (point)))
5729     (when gnus-article-button-face
5730       (gnus-overlay-put (gnus-make-overlay b e nil t)
5731                         'face gnus-article-button-face))
5732     (widget-convert-button
5733      'link b e
5734      :mime-handle handle
5735      :action 'gnus-widget-press-button
5736      :button-keymap gnus-mime-button-map
5737      :help-echo
5738      (lambda (widget)
5739        ;; Needed to properly clear the message due to a bug in
5740        ;; wid-edit (XEmacs only).
5741        (if (boundp 'help-echo-owns-message)
5742            (setq help-echo-owns-message t))
5743        (format
5744         "%S: %s the MIME part; %S: more options"
5745         (aref gnus-mouse-2 0)
5746         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5747             "hide" "show")
5748         (aref gnus-down-mouse-3 0))))))
5749
5750 (defun gnus-widget-press-button (elems el)
5751   (goto-char (widget-get elems :from))
5752   (gnus-article-press-button))
5753
5754 (defvar gnus-displaying-mime nil)
5755
5756 (defun gnus-display-mime (&optional ihandles)
5757   "Display the MIME parts."
5758   (save-excursion
5759     (save-selected-window
5760       (let ((window (get-buffer-window gnus-article-buffer))
5761             (point (point)))
5762         (when window
5763           (select-window window)
5764           ;; We have to do this since selecting the window
5765           ;; may change the point.  So we set the window point.
5766           (set-window-point window point)))
5767       (let ((handles ihandles)
5768             (inhibit-read-only t)
5769             handle)
5770         (cond (handles)
5771               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5772                (when gnus-article-emulate-mime
5773                  (mm-uu-dissect-text-parts handles)))
5774               (gnus-article-emulate-mime
5775                (setq handles (mm-uu-dissect))))
5776         (when (and (not ihandles)
5777                    (not gnus-displaying-mime))
5778           ;; Top-level call; we clean up.
5779           (when gnus-article-mime-handles
5780             (mm-destroy-parts gnus-article-mime-handles)
5781             (setq gnus-article-mime-handle-alist nil));; A trick.
5782           (setq gnus-article-mime-handles handles)
5783           ;; We allow users to glean info from the handles.
5784           (when gnus-article-mime-part-function
5785             (gnus-mime-part-function handles)))
5786         (if (and handles
5787                  (or (not (stringp (car handles)))
5788                      (cdr handles)))
5789             (progn
5790               (when (and (not ihandles)
5791                          (not gnus-displaying-mime))
5792                 ;; Clean up for mime parts.
5793                 (article-goto-body)
5794                 (delete-region (point) (point-max)))
5795               (let ((gnus-displaying-mime t))
5796                 (gnus-mime-display-part handles)))
5797           (save-restriction
5798             (article-goto-body)
5799             (narrow-to-region (point) (point-max))
5800             (gnus-treat-article nil 1 1 "text/plain")
5801             (widen)))
5802         (unless ihandles
5803           ;; Highlight the headers.
5804           (save-excursion
5805             (save-restriction
5806               (article-goto-body)
5807               (narrow-to-region (point-min) (point))
5808               (gnus-article-save-original-date
5809                (gnus-treat-article 'head)))))))
5810     ;; Cope with broken MIME messages.
5811     (goto-char (point-max))
5812     (unless (bolp)
5813       (insert "\n"))))
5814
5815 (defcustom gnus-mime-display-multipart-as-mixed nil
5816   "Display \"multipart\" parts as  \"multipart/mixed\".
5817
5818 If t, it overrides nil values of
5819 `gnus-mime-display-multipart-alternative-as-mixed' and
5820 `gnus-mime-display-multipart-related-as-mixed'."
5821   :group 'gnus-article-mime
5822   :type 'boolean)
5823
5824 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5825   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5826   :version "22.1"
5827   :group 'gnus-article-mime
5828   :type 'boolean)
5829
5830 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5831   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5832
5833 If displaying \"text/html\" is discouraged \(see
5834 `mm-discouraged-alternatives'\) images or other material inside a
5835 \"multipart/related\" part might be overlooked when this variable is nil."
5836   :version "22.1"
5837   :group 'gnus-article-mime
5838   :type 'boolean)
5839
5840 (defun gnus-mime-display-part (handle)
5841   (cond
5842    ;; Maybe a broken MIME message.
5843    ((null handle))
5844    ;; Single part.
5845    ((not (stringp (car handle)))
5846     (gnus-mime-display-single handle))
5847    ;; User-defined multipart
5848    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5849     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5850              handle))
5851    ;; multipart/alternative
5852    ((and (equal (car handle) "multipart/alternative")
5853          (not (or gnus-mime-display-multipart-as-mixed
5854                   gnus-mime-display-multipart-alternative-as-mixed)))
5855     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5856       (push (cons id handle) gnus-article-mime-handle-alist)
5857       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5858    ;; multipart/related
5859    ((and (equal (car handle) "multipart/related")
5860          (not (or gnus-mime-display-multipart-as-mixed
5861                   gnus-mime-display-multipart-related-as-mixed)))
5862     ;;;!!!We should find the start part, but we just default
5863     ;;;!!!to the first part.
5864     ;;(gnus-mime-display-part (cadr handle))
5865     ;;;!!! Most multipart/related is an HTML message plus images.
5866     ;;;!!! Unfortunately we are unable to let W3 display those
5867     ;;;!!! included images, so we just display it as a mixed multipart.
5868     ;;(gnus-mime-display-mixed (cdr handle))
5869     ;;;!!! No, w3 can display everything just fine.
5870     (gnus-mime-display-part (cadr handle)))
5871    ((equal (car handle) "multipart/signed")
5872     (gnus-add-wash-type 'signed)
5873     (gnus-mime-display-security handle))
5874    ((equal (car handle) "multipart/encrypted")
5875     (gnus-add-wash-type 'encrypted)
5876     (gnus-mime-display-security handle))
5877    ;; Other multiparts are handled like multipart/mixed.
5878    (t
5879     (gnus-mime-display-mixed (cdr handle)))))
5880
5881 (defun gnus-mime-part-function (handles)
5882   (if (stringp (car handles))
5883       (mapcar 'gnus-mime-part-function (cdr handles))
5884     (funcall gnus-article-mime-part-function handles)))
5885
5886 (defun gnus-mime-display-mixed (handles)
5887   (mapcar 'gnus-mime-display-part handles))
5888
5889 (defun gnus-mime-display-single (handle)
5890   (let ((type (mm-handle-media-type handle))
5891         (ignored gnus-ignored-mime-types)
5892         (not-attachment t)
5893         (move nil)
5894         display text)
5895     (catch 'ignored
5896       (progn
5897         (while ignored
5898           (when (string-match (pop ignored) type)
5899             (throw 'ignored nil)))
5900         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5901                                (with-current-buffer gnus-summary-buffer
5902                                  gnus-inhibit-images)
5903                              gnus-inhibit-images)
5904                            (string-match "\\`image/" type)))
5905                  (setq not-attachment
5906                        (and (not (mm-inline-override-p handle))
5907                             (or (not (mm-handle-disposition handle))
5908                                 (equal (car (mm-handle-disposition handle))
5909                                        "inline")
5910                                 (mm-attachment-override-p handle))))
5911                  (mm-automatic-display-p handle)
5912                  (or (and
5913                       (mm-inlinable-p handle)
5914                       (mm-inlined-p handle))
5915                      (mm-automatic-external-display-p type)))
5916             (setq display t)
5917           (when (equal (mm-handle-media-supertype handle) "text")
5918             (setq text t)))
5919         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5920               beg)
5921           (push (cons id handle) gnus-article-mime-handle-alist)
5922           (when (and display
5923                      (equal (mm-handle-media-supertype handle) "message"))
5924             (insert-char
5925              ?\n
5926              (cond ((not (bolp)) 2)
5927                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5928                    (t 1))))
5929           (when (or (not display)
5930                     (not (gnus-unbuttonized-mime-type-p type))
5931                     (eq id gnus-mime-buttonized-part-id))
5932             (gnus-insert-mime-button
5933              handle id (list (or display (and not-attachment text))))
5934             (gnus-article-insert-newline)
5935             ;; Remember modify the number of forward lines.
5936             (setq move t))
5937           (setq beg (point))
5938           (cond
5939            (display
5940             (when move
5941               (forward-line -1)
5942               (setq beg (point)))
5943             (let ((mail-parse-charset gnus-newsgroup-charset)
5944                   (mail-parse-ignored-charsets
5945                    (save-excursion (condition-case ()
5946                                        (set-buffer gnus-summary-buffer)
5947                                      (error))
5948                                    gnus-newsgroup-ignored-charsets)))
5949               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5950             (goto-char (point-max)))
5951            ((and text not-attachment)
5952             (when move
5953               (forward-line -1)
5954               (setq beg (point)))
5955             (gnus-article-insert-newline)
5956             (mm-display-inline handle)
5957             (goto-char (point-max))))
5958           ;; Do highlighting.
5959           (save-excursion
5960             (save-restriction
5961               (narrow-to-region beg (point))
5962               (if (eq handle gnus-article-mime-handles)
5963                   ;; The format=flowed case.
5964                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5965                 ;; Don't count signature parts that are never displayed.
5966                 ;; The part number should be re-calculated supposing this
5967                 ;; might be a message/rfc822 part.
5968                 (let (handles)
5969                   (dolist (part gnus-article-mime-handles)
5970                     (unless (or (stringp part)
5971                                 (equal (car (mm-handle-type part))
5972                                        "application/pgp-signature"))
5973                       (push part handles)))
5974                   (gnus-treat-article
5975                    nil (length (memq handle handles)) (length handles)
5976                    (mm-handle-media-type handle)))))))))))
5977
5978 (defun gnus-unbuttonized-mime-type-p (type)
5979   "Say whether TYPE is to be unbuttonized."
5980   (unless gnus-inhibit-mime-unbuttonizing
5981     (when (catch 'found
5982             (let ((types gnus-unbuttonized-mime-types))
5983               (while types
5984                 (when (string-match (pop types) type)
5985                   (throw 'found t)))))
5986       (not (catch 'found
5987              (let ((types gnus-buttonized-mime-types))
5988                (while types
5989                  (when (string-match (pop types) type)
5990                    (throw 'found t)))))))))
5991
5992 (defun gnus-article-insert-newline ()
5993   "Insert a newline, but mark it as undeletable."
5994   (gnus-put-text-property
5995    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5996
5997 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5998   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5999          (ihandles handles)
6000          (point (point))
6001          handle (inhibit-read-only t) from props begend not-pref)
6002     (save-window-excursion
6003       (save-restriction
6004         (when ibegend
6005           (narrow-to-region (car ibegend)
6006                             (or (cdr ibegend)
6007                                 (progn
6008                                   (goto-char (car ibegend))
6009                                   (forward-line 2)
6010                                   (point))))
6011           (delete-region (point-min) (point-max))
6012           (mm-remove-parts handles))
6013         (setq begend (list (point-marker)))
6014         ;; Do the toggle.
6015         (unless (setq not-pref (cadr (member preferred ihandles)))
6016           (setq not-pref (car ihandles)))
6017         (when (or ibegend
6018                   (not preferred)
6019                   (not (gnus-unbuttonized-mime-type-p
6020                         "multipart/alternative")))
6021           (gnus-add-text-properties
6022            (setq from (point))
6023            (progn
6024              (insert (format "%d.  " id))
6025              (point))
6026            `(gnus-callback
6027              (lambda (handles)
6028                (unless ,(not ibegend)
6029                  (setq gnus-article-mime-handle-alist
6030                        ',gnus-article-mime-handle-alist))
6031                (gnus-mime-display-alternative
6032                 ',ihandles ',not-pref ',begend ,id))
6033              keymap ,gnus-mime-button-map
6034              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6035              face ,gnus-article-button-face
6036              gnus-part ,id
6037              article-type multipart
6038              rear-nonsticky t))
6039           (widget-convert-button 'link from (point)
6040                                  :action 'gnus-widget-press-button
6041                                  :button-keymap gnus-widget-button-keymap)
6042           ;; Do the handles
6043           (while (setq handle (pop handles))
6044             (gnus-add-text-properties
6045              (setq from (point))
6046              (progn
6047                (insert (format "(%c) %-18s"
6048                                (if (equal handle preferred) ?* ? )
6049                                (mm-handle-media-type handle)))
6050                (point))
6051              `(gnus-callback
6052                (lambda (handles)
6053                  (unless ,(not ibegend)
6054                    (setq gnus-article-mime-handle-alist
6055                          ',gnus-article-mime-handle-alist))
6056                  (gnus-mime-display-alternative
6057                   ',ihandles ',handle ',begend ,id))
6058                keymap ,gnus-mime-button-map
6059                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6060                face ,gnus-article-button-face
6061                gnus-part ,id
6062                gnus-data ,handle
6063                rear-nonsticky t))
6064             (widget-convert-button 'link from (point)
6065                                    :action 'gnus-widget-press-button
6066                                    :button-keymap gnus-widget-button-keymap)
6067             (insert "  "))
6068           (insert "\n\n"))
6069         (when preferred
6070           (if (stringp (car preferred))
6071               (gnus-display-mime preferred)
6072             (let ((mail-parse-charset gnus-newsgroup-charset)
6073                   (mail-parse-ignored-charsets
6074                    (with-current-buffer gnus-summary-buffer
6075                      gnus-newsgroup-ignored-charsets)))
6076               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6077               ;; Do highlighting.
6078               (save-excursion
6079                 (save-restriction
6080                   (narrow-to-region (car begend) (point-max))
6081                   (gnus-treat-article
6082                    nil (length gnus-article-mime-handle-alist)
6083                    (gnus-article-mime-total-parts)
6084                    (mm-handle-media-type preferred))))))
6085           (goto-char (point-max))
6086           (setcdr begend (point-marker)))))
6087     (when ibegend
6088       (goto-char point))))
6089
6090 (defconst gnus-article-wash-status-strings
6091   (let ((alist '((cite "c" "Possible hidden citation text"
6092                        " " "All citation text visible")
6093                  (headers "h" "Hidden headers"
6094                           " " "All headers visible.")
6095                  (pgp "p" "Encrypted or signed message status hidden"
6096                       " " "No hidden encryption nor digital signature status")
6097                  (signature "s" "Signature has been hidden"
6098                             " " "Signature is visible")
6099                  (overstrike "o" "Overstrike (^H) characters applied"
6100                              " " "No overstrike characters applied")
6101                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6102                            " " "No /*_emphasis_*/ characters applied")))
6103         result)
6104     (dolist (entry alist result)
6105       (let ((key (nth 0 entry))
6106             (on (copy-sequence (nth 1 entry)))
6107             (on-help (nth 2 entry))
6108             (off (copy-sequence (nth 3 entry)))
6109             (off-help (nth 4 entry)))
6110         (put-text-property 0 1 'help-echo on-help on)
6111         (put-text-property 0 1 'help-echo off-help off)
6112         (push (list key on off) result))))
6113   "Alist of strings describing wash status in the mode line.
6114 Each entry has the form (KEY ON OF), where the KEY is a symbol
6115 representing the particular washing function, ON is the string to use
6116 in the article mode line when the washing function is active, and OFF
6117 is the string to use when it is inactive.")
6118
6119 (defun gnus-article-wash-status-entry (key value)
6120   (let ((entry (assoc key gnus-article-wash-status-strings)))
6121     (if value (nth 1 entry) (nth 2 entry))))
6122
6123 (defun gnus-article-wash-status ()
6124   "Return a string which display status of article washing."
6125   (with-current-buffer gnus-article-buffer
6126     (let ((cite (memq 'cite gnus-article-wash-types))
6127           (headers (memq 'headers gnus-article-wash-types))
6128           (boring (memq 'boring-headers gnus-article-wash-types))
6129           (pgp (memq 'pgp gnus-article-wash-types))
6130           (pem (memq 'pem gnus-article-wash-types))
6131           (signed (memq 'signed gnus-article-wash-types))
6132           (encrypted (memq 'encrypted gnus-article-wash-types))
6133           (signature (memq 'signature gnus-article-wash-types))
6134           (overstrike (memq 'overstrike gnus-article-wash-types))
6135           (emphasis (memq 'emphasis gnus-article-wash-types)))
6136       (concat
6137        (gnus-article-wash-status-entry 'cite cite)
6138        (gnus-article-wash-status-entry 'headers (or headers boring))
6139        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6140        (gnus-article-wash-status-entry 'signature signature)
6141        (gnus-article-wash-status-entry 'overstrike overstrike)
6142        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6143
6144 (defun gnus-add-wash-type (type)
6145   "Add a washing of TYPE to the current status."
6146   (add-to-list 'gnus-article-wash-types type))
6147
6148 (defun gnus-delete-wash-type (type)
6149   "Add a washing of TYPE to the current status."
6150   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6151
6152 (defun gnus-add-image (category image)
6153   "Add IMAGE of CATEGORY to the list of displayed images."
6154   (let ((entry (assq category gnus-article-image-alist)))
6155     (unless entry
6156       (setq entry (list category))
6157       (push entry gnus-article-image-alist))
6158     (nconc entry (list image))))
6159
6160 (defun gnus-delete-images (category)
6161   "Delete all images in CATEGORY."
6162   (let ((entry (assq category gnus-article-image-alist)))
6163     (dolist (image (cdr entry))
6164       (gnus-remove-image image category))
6165     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6166     (gnus-delete-wash-type category)))
6167
6168 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6169
6170 (defun gnus-article-maybe-hide-headers ()
6171   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6172 Provided for backwards compatibility."
6173   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6174                  (not (with-current-buffer gnus-summary-buffer
6175                         gnus-have-all-headers)))
6176              (not gnus-inhibit-hiding))
6177     (gnus-article-hide-headers)))
6178
6179 (declare-function shr-put-image "shr" (data alt &optional flags))
6180
6181 (defun gnus-shr-put-image (data alt &optional flags)
6182   "Put image DATA with a string ALT.  Enable image to be deleted."
6183   (let ((image (shr-put-image data (propertize (or alt "*")
6184                                                'gnus-image-category 'shr)
6185                               flags)))
6186     (when image
6187       (gnus-add-image 'shr image))))
6188
6189 ;;; Article savers.
6190
6191 (defun gnus-output-to-file (file-name)
6192   "Append the current article to a file named FILE-NAME.
6193 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6194 text and used as the value of the coding cookie which is added to the
6195 top of a file.  Otherwise, this function saves a raw article without
6196 the coding cookie."
6197   (let* ((artbuf (current-buffer))
6198          (file-name-coding-system nnmail-pathname-coding-system)
6199          (coding gnus-article-save-coding-system)
6200          (coding-system-for-read (if coding
6201                                      nil ;; Rely on the coding cookie.
6202                                    mm-text-coding-system))
6203          (coding-system-for-write (or coding
6204                                       mm-text-coding-system-for-write
6205                                       mm-text-coding-system))
6206          (exists (file-exists-p file-name)))
6207     (with-temp-buffer
6208       (when exists
6209         (insert-file-contents file-name)
6210         (goto-char (point-min))
6211         ;; Remove the existing coding cookie.
6212         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6213           (delete-region (match-beginning 0) (match-end 0))))
6214       (goto-char (point-max))
6215       (insert-buffer-substring artbuf)
6216       ;; Append newline at end of the buffer as separator, and then
6217       ;; save it to file.
6218       (goto-char (point-max))
6219       (insert "\n")
6220       (when coding
6221         ;; If the coding system is not suitable to encode the text,
6222         ;; ask a user for a proper one.
6223         (when (fboundp 'select-safe-coding-system)
6224           (setq coding (coding-system-base
6225                         (save-window-excursion
6226                           (select-safe-coding-system (point-min) (point-max)
6227                                                      coding))))
6228           (setq coding-system-for-write
6229                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6230                     coding)))
6231         (goto-char (point-min))
6232         ;; Add the coding cookie.
6233         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6234                         coding-system-for-write)))
6235       (if exists
6236           (progn
6237             (write-region (point-min) (point-max) file-name nil 'no-message)
6238             (message "Appended to %s" file-name))
6239         (write-region (point-min) (point-max) file-name))))
6240   t)
6241
6242 (defun gnus-narrow-to-page (&optional arg)
6243   "Narrow the article buffer to a page.
6244 If given a numerical ARG, move forward ARG pages."
6245   (interactive "P")
6246   (setq arg (if arg (prefix-numeric-value arg) 0))
6247   (with-current-buffer gnus-article-buffer
6248     (widen)
6249     ;; Remove any old next/prev buttons.
6250     (when (gnus-visual-p 'page-marker)
6251       (let ((inhibit-read-only t))
6252         (gnus-remove-text-with-property 'gnus-prev)
6253         (gnus-remove-text-with-property 'gnus-next)))
6254     (let (st nd pt)
6255       (when (save-excursion
6256               (cond ((< arg 0)
6257                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6258                          (prog1
6259                              (setq nd (match-beginning 0)
6260                                    pt nd)
6261                            (when (re-search-backward page-delimiter nil t)
6262                              (setq st (match-end 0))))
6263                        (when (re-search-forward page-delimiter nil t)
6264                          (setq nd (match-beginning 0)
6265                                pt (point-min)))))
6266                     ((> arg 0)
6267                      (if (re-search-forward page-delimiter nil 'move arg)
6268                          (prog1
6269                              (setq st (match-end 0)
6270                                    pt st)
6271                            (when (re-search-forward page-delimiter nil t)
6272                              (setq nd (match-beginning 0))))
6273                        (when (re-search-backward page-delimiter nil t)
6274                          (setq st (match-end 0)
6275                                pt (point-max)))))
6276                     (t
6277                      (when (re-search-backward page-delimiter nil t)
6278                        (goto-char (setq st (match-end 0))))
6279                      (when (re-search-forward page-delimiter nil t)
6280                        (setq nd (match-beginning 0)))
6281                      (or st nd))))
6282         (setq gnus-page-broken t)
6283         (when pt (goto-char pt))
6284         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6285         (when (gnus-visual-p 'page-marker)
6286           (save-excursion
6287             (when nd
6288               (goto-char nd)
6289               (gnus-insert-next-page-button))
6290             (when st
6291               (goto-char st)
6292               (gnus-insert-prev-page-button))))))))
6293
6294 ;; Article mode commands
6295
6296 (defun gnus-article-goto-next-page ()
6297   "Show the next page of the article."
6298   (interactive)
6299   (when (gnus-article-next-page)
6300     (goto-char (point-min))
6301     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6302
6303
6304 (defun gnus-article-goto-prev-page ()
6305   "Show the previous page of the article."
6306   (interactive)
6307   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6308       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6309     (gnus-article-prev-page nil)))
6310
6311 ;; This is cleaner but currently breaks `gnus-pick-mode':
6312 ;;
6313 ;; (defun gnus-article-goto-next-page ()
6314 ;;   "Show the next page of the article."
6315 ;;   (interactive)
6316 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6317 ;;     (gnus-summary-next-page)))
6318 ;;
6319 ;; (defun gnus-article-goto-prev-page ()
6320 ;;   "Show the next page of the article."
6321 ;;   (interactive)
6322 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6323 ;;     (gnus-summary-prev-page)))
6324
6325 (defun gnus-article-next-page (&optional lines)
6326   "Show the next page of the current article.
6327 If end of article, return non-nil.  Otherwise return nil.
6328 Argument LINES specifies lines to be scrolled up."
6329   (interactive "p")
6330   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6331   (if (and (not (and gnus-article-over-scroll
6332                      (> (count-lines (window-start) (point-max))
6333                         (if (featurep 'xemacs)
6334                             (or lines (1- (window-height)))
6335                           (+ (or lines (1- (window-height))) scroll-margin)))))
6336            (save-excursion
6337              (end-of-line)
6338              (and (pos-visible-in-window-p)     ;Not continuation line.
6339                   (>= (point) (point-max)))))
6340       ;; Nothing in this page.
6341       (if (or (not gnus-page-broken)
6342               (save-excursion
6343                 (save-restriction
6344                   (widen)
6345                   (forward-line)
6346                   (eobp)))) ;Real end-of-buffer?
6347           (progn
6348             (when gnus-article-over-scroll
6349               (gnus-article-next-page-1 lines))
6350             t)                  ;Nothing more.
6351         (gnus-narrow-to-page 1)         ;Go to next page.
6352         nil)
6353     ;; More in this page.
6354     (gnus-article-next-page-1 lines)
6355     nil))
6356
6357 (defun gnus-article-beginning-of-window ()
6358   "Move point to the beginning of the window.
6359 In Emacs, the point is placed at the line number which `scroll-margin'
6360 specifies."
6361   (if (featurep 'xemacs)
6362       (move-to-window-line 0)
6363     ;; There is an obscure bug in Emacs that makes it impossible to
6364     ;; scroll past big pictures in the article buffer.  Try to fix
6365     ;; this by adding a sanity check by counting the lines visible.
6366     (when (> (count-lines (window-start) (window-end)) 30)
6367       (move-to-window-line
6368        (min (max 0 scroll-margin)
6369             (max 1 (- (window-height)
6370                       (if mode-line-format 1 0)
6371                       (if header-line-format 1 0)
6372                       2)))))))
6373
6374 (defun gnus-article-next-page-1 (lines)
6375   (condition-case ()
6376       (let ((scroll-in-place nil)
6377             (auto-window-vscroll nil))
6378         (scroll-up lines))
6379     (end-of-buffer
6380      ;; Long lines may cause an end-of-buffer error.
6381      (goto-char (point-max))))
6382   (gnus-article-beginning-of-window))
6383
6384 (defun gnus-article-prev-page (&optional lines)
6385   "Show previous page of current article.
6386 Argument LINES specifies lines to be scrolled down."
6387   (interactive "p")
6388   (move-to-window-line 0)
6389   (if (and gnus-page-broken
6390            (bobp)
6391            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6392       (progn
6393         (gnus-narrow-to-page -1)        ;Go to previous page.
6394         (goto-char (point-max))
6395         (recenter (if gnus-article-over-scroll
6396                       (if lines
6397                           (max (if (featurep 'xemacs)
6398                                    lines
6399                                  (+ lines scroll-margin))
6400                                3)
6401                         (- (window-height) 2))
6402                     -1)))
6403     (prog1
6404         (condition-case ()
6405             (let ((scroll-in-place nil))
6406               (scroll-down lines))
6407           (beginning-of-buffer
6408            (goto-char (point-min))))
6409       (gnus-article-beginning-of-window))))
6410
6411 (defun gnus-article-only-boring-p ()
6412   "Decide whether there is only boring text remaining in the article.
6413 Something \"interesting\" is a word of at least two letters that does
6414 not have a face in `gnus-article-boring-faces'."
6415   (when (and gnus-article-skip-boring
6416              (boundp 'gnus-article-boring-faces)
6417              (symbol-value 'gnus-article-boring-faces))
6418     (save-excursion
6419       (let ((inhibit-point-motion-hooks t))
6420         (catch 'only-boring
6421           (while (re-search-forward "\\b\\w\\w" nil t)
6422             (forward-char -1)
6423             (when (not (gnus-intersection
6424                         (gnus-faces-at (point))
6425                         (symbol-value 'gnus-article-boring-faces)))
6426               (throw 'only-boring nil)))
6427           (throw 'only-boring t))))))
6428
6429 (defun gnus-article-refer-article ()
6430   "Read article specified by message-id around point."
6431   (interactive)
6432   (save-excursion
6433     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6434     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6435     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6436         (let ((msg-id (concat "<" (match-string 0) ">")))
6437           (set-buffer gnus-summary-buffer)
6438           (gnus-summary-refer-article msg-id))
6439       (error "No references around point"))))
6440
6441 (defun gnus-article-show-summary ()
6442   "Reconfigure windows to show summary buffer."
6443   (interactive)
6444   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6445       (error "There is no summary buffer for this article buffer")
6446     (gnus-article-set-globals)
6447     (gnus-configure-windows 'article)
6448     (gnus-summary-goto-subject gnus-current-article)
6449     (gnus-summary-position-point)))
6450
6451 (defun gnus-article-describe-briefly ()
6452   "Describe article mode commands briefly."
6453   (interactive)
6454   (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")))
6455
6456 (defun gnus-article-check-buffer ()
6457   "Beep if not in an article buffer."
6458   (unless (equal major-mode 'gnus-article-mode)
6459     (error "Command invoked outside of a Gnus article buffer")))
6460
6461 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6462   "Read a summary buffer key sequence and execute it from the article buffer."
6463   (interactive "P")
6464   (gnus-article-check-buffer)
6465   (let ((nosaves
6466          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6467            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6468            "=" "^" "\M-^" "|"))
6469         (nosave-but-article
6470          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6471            "An" "Ap" [?A (meta return)] [?A delete]))
6472         (nosave-in-article
6473          '("AS" "\C-d"))
6474         (up-to-top
6475          '("n" "Gn" "p" "Gp"))
6476         keys new-sum-point)
6477     (with-current-buffer gnus-article-current-summary
6478       (let (gnus-pick-mode)
6479         (setq unread-command-events (nconc unread-command-events
6480                                            (list (or key last-command-event)))
6481               keys (if (featurep 'xemacs)
6482                        (events-to-keys (read-key-sequence nil t))
6483                      (read-key-sequence nil t)))))
6484
6485     (message "")
6486
6487     (cond
6488      ((eq (aref keys (1- (length keys))) ?\C-h)
6489       (gnus-article-describe-bindings (substring keys 0 -1)))
6490      ((or (member keys nosaves)
6491           (member keys nosave-but-article)
6492           (member keys nosave-in-article))
6493       (let (func)
6494         (save-window-excursion
6495           (pop-to-buffer gnus-article-current-summary)
6496           ;; We disable the pick minor mode commands.
6497           (let (gnus-pick-mode)
6498             (setq func (key-binding keys t))))
6499         (if (or (not func)
6500                 (numberp func))
6501             (ding)
6502           (unless (member keys nosave-in-article)
6503             (set-buffer gnus-article-current-summary))
6504           (when (and (symbolp func)
6505                      (get func 'disabled))
6506             (error "Function %s disabled" func))
6507           (call-interactively func)
6508           (setq new-sum-point (point)))
6509         (when (member keys nosave-but-article)
6510           (pop-to-buffer gnus-article-buffer))))
6511      (t
6512       ;; These commands should restore window configuration.
6513       (let ((obuf (current-buffer))
6514             (owin (current-window-configuration))
6515             win func in-buffer selected new-sum-start new-sum-hscroll err)
6516         (cond (not-restore-window
6517                (pop-to-buffer gnus-article-current-summary)
6518                (setq win (selected-window)))
6519               ((setq win (get-buffer-window gnus-article-current-summary))
6520                (select-window win))
6521               (t
6522                (let ((summary-buffer gnus-article-current-summary))
6523                  (gnus-configure-windows 'article)
6524                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6525                    (let ((gnus-buffer-configuration
6526                           '((article ((vertical 1.0
6527                                                 (summary 0.25 point)
6528                                                 (article 1.0)))))))
6529                      (gnus-configure-windows 'article))
6530                    (setq win (get-buffer-window summary-buffer 'visible)))
6531                  (gnus-select-frame-set-input-focus (window-frame win))
6532                  (select-window win))))
6533         (setq in-buffer (current-buffer))
6534         ;; We disable the pick minor mode commands.
6535         (setq func (let (gnus-pick-mode)
6536                      (key-binding keys t)))
6537         (when (get func 'disabled)
6538           (error "Function %s disabled" func))
6539         (if (and func
6540                  (functionp func)
6541                  (condition-case code
6542                      (progn
6543                        (call-interactively func)
6544                        t)
6545                    (error
6546                     (setq err code)
6547                     nil)))
6548             (progn
6549               (when (eq win (selected-window))
6550                 (setq new-sum-point (point)
6551                       new-sum-start (window-start win)
6552                       new-sum-hscroll (window-hscroll win)))
6553               (when (or (eq in-buffer (current-buffer))
6554                         (when (eq obuf (current-buffer))
6555                           (set-buffer in-buffer)
6556                           t))
6557                 (setq selected (gnus-summary-select-article))
6558                 (set-buffer obuf)
6559                 (unless not-restore-window
6560                   (set-window-configuration owin))
6561                 (when (and (eq selected 'old)
6562                            new-sum-point)
6563                   (set-window-start (get-buffer-window (current-buffer))
6564                                     1)
6565                   (set-window-point (get-buffer-window (current-buffer))
6566                                     (if (article-goto-body)
6567                                         (1- (point))
6568                                       (point))))
6569                 (when (and (not not-restore-window)
6570                            new-sum-point
6571                            (window-live-p win)
6572                            (with-current-buffer (window-buffer win)
6573                              (eq major-mode 'gnus-summary-mode)))
6574                   (set-window-point win new-sum-point)
6575                   (set-window-start win new-sum-start)
6576                   (set-window-hscroll win new-sum-hscroll))))
6577           (set-window-configuration owin)
6578           (if err
6579               (signal (car err) (cdr err))
6580             (ding))))))))
6581
6582 (defun gnus-article-read-summary-send-keys ()
6583   (interactive)
6584   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6585     (gnus-article-read-summary-keys)))
6586
6587 (defun gnus-article-describe-key (key)
6588   "Display documentation of the function invoked by KEY.
6589 KEY is a string or a vector."
6590   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6591                        (read-key-sequence "Describe key: "))))
6592   (gnus-article-check-buffer)
6593   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6594                                   gnus-article-read-summary-send-keys))
6595       (with-current-buffer gnus-article-current-summary
6596         (setq unread-command-events
6597               (if (featurep 'xemacs)
6598                   (append key nil)
6599                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6600                                         (list 'meta (- x 128))
6601                                       x))
6602                         key)))
6603         (let ((cursor-in-echo-area t)
6604               gnus-pick-mode)
6605           (describe-key (read-key-sequence nil t))))
6606     (describe-key key)))
6607
6608 (defun gnus-article-describe-key-briefly (key &optional insert)
6609   "Display documentation of the function invoked by KEY.
6610 KEY is a string or a vector."
6611   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6612                        (read-key-sequence "Describe key: "))
6613                      current-prefix-arg))
6614   (gnus-article-check-buffer)
6615   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6616                                   gnus-article-read-summary-send-keys))
6617       (with-current-buffer gnus-article-current-summary
6618         (setq unread-command-events
6619               (if (featurep 'xemacs)
6620                   (append key nil)
6621                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6622                                         (list 'meta (- x 128))
6623                                       x))
6624                         key)))
6625         (let ((cursor-in-echo-area t)
6626               gnus-pick-mode)
6627           (describe-key-briefly (read-key-sequence nil t) insert)))
6628     (describe-key-briefly key insert)))
6629
6630 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6631 (defvar gnus-agent-summary-mode)
6632 (defvar gnus-draft-mode)
6633 (defvar help-xref-stack-item)
6634
6635 (defun gnus-article-describe-bindings (&optional prefix)
6636   "Show a list of all defined keys, and their definitions.
6637 The optional argument PREFIX, if non-nil, should be a key sequence;
6638 then we display only bindings that start with that prefix."
6639   (interactive)
6640   (gnus-article-check-buffer)
6641   (let ((keymap (copy-keymap gnus-article-mode-map))
6642         (map (copy-keymap gnus-article-send-map))
6643         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6644         parent agent draft)
6645     (define-key keymap "S" map)
6646     (define-key map [t] nil)
6647     (with-current-buffer gnus-article-current-summary
6648       (set-keymap-parent
6649        keymap
6650        (if (setq parent (keymap-parent gnus-article-mode-map))
6651            (prog1
6652                (setq parent (copy-keymap parent))
6653              (set-keymap-parent parent (current-local-map)))
6654          (current-local-map)))
6655       (set-keymap-parent map (key-binding "S"))
6656       (let (key def gnus-pick-mode)
6657         (while sumkeys
6658           (setq key (pop sumkeys))
6659           (cond ((and (vectorp key) (= (length key) 1)
6660                       (consp (setq def (aref key 0)))
6661                       (numberp (car def)) (numberp (cdr def)))
6662                  (when (< (max (car def) (cdr def)) 128)
6663                    (setq sumkeys
6664                          (append (mapcar
6665                                   #'vector
6666                                   (nreverse (gnus-uncompress-range def)))
6667                                  sumkeys))))
6668                 ((setq def (key-binding key))
6669                  (unless (eq def 'undefined)
6670                    (define-key keymap key def))))))
6671       (when (boundp 'gnus-agent-summary-mode)
6672         (setq agent gnus-agent-summary-mode))
6673       (when (boundp 'gnus-draft-mode)
6674         (setq draft gnus-draft-mode)))
6675     (with-temp-buffer
6676       (use-local-map keymap)
6677       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6678       (set (make-local-variable 'gnus-draft-mode) draft)
6679       (describe-bindings prefix))
6680     (let ((item `((lambda (prefix)
6681                     (with-current-buffer ,(current-buffer)
6682                       (gnus-article-describe-bindings prefix)))
6683                   ,prefix)))
6684       ;; Loading `help-mode' here is necessary if `describe-bindings'
6685       ;; is replaced with something, e.g. `helm-descbinds'.
6686       (require 'help-mode)
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 (cons (point-min-marker) (point-max-marker)))
8693     (goto-char (point-max))))
8694
8695 (defun gnus-mime-security-run-function (function)
8696   "Run FUNCTION with the security part under point."
8697   (gnus-article-check-buffer)
8698   (let ((data (get-text-property (point) 'gnus-data))
8699         buffer handle)
8700     (when (and (stringp (car-safe data))
8701                (setq buffer (mm-handle-multipart-original-buffer data))
8702                (setq handle (cadr data)))
8703       (if (bufferp (mm-handle-buffer handle))
8704           (progn
8705             (setq handle (cons buffer (copy-sequence (cdr handle))))
8706             (mm-handle-set-undisplayer handle nil))
8707         (setq handle (mm-make-handle
8708                       buffer
8709                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8710                       nil nil nil nil nil nil)))
8711       (funcall function handle))))
8712
8713 (defun gnus-mime-security-save-part ()
8714   "Save the security part under point."
8715   (interactive)
8716   (gnus-mime-security-run-function 'mm-save-part))
8717
8718 (defun gnus-mime-security-pipe-part ()
8719   "Pipe the security part under point to a process."
8720   (interactive)
8721   (gnus-mime-security-run-function 'mm-pipe-part))
8722
8723 (gnus-ems-redefine)
8724
8725 (provide 'gnus-art)
8726
8727 (run-hooks 'gnus-art-load-hook)
8728
8729 ;;; gnus-art.el ends here