gmm-utils.el (gmm-called-interactively-p): Revert. This seems to causes Emacs to...
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (defvar tool-bar-map)
33 (defvar w3m-minor-mode-map)
34
35 (require 'gnus)
36 (require 'gnus-util)
37 (require 'gnus-sum)
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48 (require 'mouse)
49
50 (autoload 'gnus-msg-mail "gnus-msg" nil t)
51 (autoload 'gnus-button-mailto "gnus-msg")
52 (autoload 'gnus-button-reply "gnus-msg" nil t)
53 (autoload 'parse-time-string "parse-time" nil nil)
54 (autoload 'ansi-color-apply-on-region "ansi-color")
55 (autoload 'mm-url-insert-file-contents-external "mm-url")
56 (autoload 'mm-extern-cache-contents "mm-extern")
57
58 (defgroup gnus-article nil
59   "Article display."
60   :link '(custom-manual "(gnus)Article Buffer")
61   :group 'gnus)
62
63 (defgroup gnus-article-treat nil
64   "Treating article parts."
65   :link '(custom-manual "(gnus)Article Hiding")
66   :group 'gnus-article)
67
68 (defgroup gnus-article-hiding nil
69   "Hiding article parts."
70   :link '(custom-manual "(gnus)Article Hiding")
71   :group 'gnus-article)
72
73 (defgroup gnus-article-highlight nil
74   "Article highlighting."
75   :link '(custom-manual "(gnus)Article Highlighting")
76   :group 'gnus-article
77   :group 'gnus-visual)
78
79 (defgroup gnus-article-signature nil
80   "Article signatures."
81   :link '(custom-manual "(gnus)Article Signature")
82   :group 'gnus-article)
83
84 (defgroup gnus-article-headers nil
85   "Article headers."
86   :link '(custom-manual "(gnus)Hiding Headers")
87   :group 'gnus-article)
88
89 (defgroup gnus-article-washing nil
90   "Special commands on articles."
91   :link '(custom-manual "(gnus)Article Washing")
92   :group 'gnus-article)
93
94 (defgroup gnus-article-emphasis nil
95   "Fontisizing articles."
96   :link '(custom-manual "(gnus)Article Fontisizing")
97   :group 'gnus-article)
98
99 (defgroup gnus-article-saving nil
100   "Saving articles."
101   :link '(custom-manual "(gnus)Saving Articles")
102   :group 'gnus-article)
103
104 (defgroup gnus-article-mime nil
105   "Worshiping the MIME wonder."
106   :link '(custom-manual "(gnus)Using MIME")
107   :group 'gnus-article)
108
109 (defgroup gnus-article-buttons nil
110   "Pushable buttons in the article buffer."
111   :link '(custom-manual "(gnus)Article Buttons")
112   :group 'gnus-article)
113
114 (defgroup gnus-article-various nil
115   "Other article options."
116   :link '(custom-manual "(gnus)Misc Article")
117   :group 'gnus-article)
118
119 (defcustom gnus-ignored-headers
120   (mapcar
121    (lambda (header)
122      (concat "^" header ":"))
123    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
124      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
125      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
126      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
127      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
128      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
129      "X-Attribution" "X-Originating-IP" "Delivered-To"
130      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
131      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
132      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
133      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
134      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
135      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
136      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
137      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
138      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
139      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
140      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
141      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
142      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
143      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
144      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
145      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
146      "List-[A-Za-z]+" "X-Listprocessor-Version"
147      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
148      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
149      "X-Received" "Content-length" "X-precedence"
150      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
151      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
152      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
153      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
154      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
155      "X-Content-length" "X-Posting-Agent" "Original-Received"
156      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
157      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
158      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
159      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
160      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
161      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
162      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
163      "Envelope-Sender" "Envelope-Recipients"))
164   "*All headers that start with this regexp will be hidden.
165 This variable can also be a list of regexps of headers to be ignored.
166 If `gnus-visible-headers' is non-nil, this variable will be ignored."
167   :type '(choice regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-hidden-properties '(invisible t intangible t)
261   "Property list to use for hiding text."
262   :type 'sexp
263   :group 'gnus-article-hiding)
264
265 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
266 ;; frames in a session.
267 (defcustom gnus-article-x-face-command
268   (if (featurep 'xemacs)
269       (if (or (gnus-image-type-available-p 'xface)
270               (gnus-image-type-available-p 'pbm))
271           'gnus-display-x-face-in-from
272         "{ echo \
273 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
274 ; uncompface; } | icontopbm | ee -")
275     (if (gnus-image-type-available-p 'pbm)
276         'gnus-display-x-face-in-from
277       "{ echo \
278 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
279 ; uncompface; } | icontopbm | display -"))
280   "*String or function to be executed to display an X-Face header.
281 If it is a string, the command will be executed in a sub-shell
282 asynchronously.  The compressed face will be piped to this command."
283   :type `(choice string
284                  (function-item gnus-display-x-face-in-from)
285                  function)
286   :version "21.1"
287   :group 'gnus-picon
288   :group 'gnus-article-washing)
289
290 (defcustom gnus-article-x-face-too-ugly nil
291   "Regexp matching posters whose face shouldn't be shown automatically."
292   :type '(choice regexp (const nil))
293   :group 'gnus-article-washing)
294
295 (defcustom gnus-article-banner-alist nil
296   "Banner alist for stripping.
297 For example,
298      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
299   :version "21.1"
300   :type '(repeat (cons symbol regexp))
301   :group 'gnus-article-washing)
302
303 (gnus-define-group-parameter
304  banner
305  :variable-document
306  "Alist of regexps (to match group names) and banner."
307  :variable-group gnus-article-washing
308  :parameter-type
309  '(choice :tag "Banner"
310           :value nil
311           (const :tag "Remove signature" signature)
312           (symbol :tag "Item in `gnus-article-banner-alist'" none)
313           regexp
314           (const :tag "None" nil))
315  :parameter-document
316  "If non-nil, specify how to remove `banners' from articles.
317
318 Symbol `signature' means to remove signatures delimited by
319 `gnus-signature-separator'.  Any other symbol is used to look up a
320 regular expression to match the banner in `gnus-article-banner-alist'.
321 A string is used as a regular expression to match the banner
322 directly.")
323
324 (defcustom gnus-article-address-banner-alist nil
325   "Alist of mail addresses and banners.
326 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
327 to match a mail address in the From: header, BANNER is one of a symbol
328 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
329 If ADDRESS matches author's mail address, it will remove things like
330 advertisements.  For example:
331
332 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
333 "
334   :type '(repeat
335           (cons
336            (regexp :tag "Address")
337            (choice :tag "Banner" :value nil
338                    (const :tag "Remove signature" signature)
339                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
340                    regexp
341                    (const :tag "None" nil))))
342   :version "22.1"
343   :group 'gnus-article-washing)
344
345 (defmacro gnus-emphasis-custom-with-format (&rest body)
346   `(let ((format "\
347 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
348 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
349      ,@body))
350
351 (defun gnus-emphasis-custom-value-to-external (value)
352   (gnus-emphasis-custom-with-format
353    (if (consp (car value))
354        (list (format format (car (car value)) (cdr (car value)))
355              2
356              (if (nth 1 value) 2 3)
357              (nth 2 value))
358      value)))
359
360 (defun gnus-emphasis-custom-value-to-internal (value)
361   (gnus-emphasis-custom-with-format
362    (let ((regexp (concat "\\`"
363                          (format (regexp-quote format)
364                                  "\\([^()]+\\)" "\\([^()]+\\)")
365                          "\\'"))
366          pattern)
367      (if (string-match regexp (setq pattern (car value)))
368          (list (cons (match-string 1 pattern) (match-string 2 pattern))
369                (= (nth 2 value) 2)
370                (nth 3 value))
371        value))))
372
373 (defcustom gnus-emphasis-alist
374   (let ((types
375          '(("\\*" "\\*" bold nil 2)
376            ("_" "_" underline)
377            ("/" "/" italic)
378            ("_/" "/_" underline-italic)
379            ("_\\*" "\\*_" underline-bold)
380            ("\\*/" "/\\*" bold-italic)
381            ("_\\*/" "/\\*_" underline-bold-italic))))
382     (nconc
383      (gnus-emphasis-custom-with-format
384       (mapcar (lambda (spec)
385                 (list (format format (car spec) (cadr spec))
386                       (or (nth 3 spec) 2)
387                       (or (nth 4 spec) 3)
388                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
389               types))
390      '(;; I've never seen anyone use this strikethru convention whereas I've
391        ;; several times seen it triggered by normal text.  --Stef
392        ;; Miles suggests that this form is sometimes used but for italics,
393        ;; so maybe we should map it to `italic'.
394        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
395        ;; 2 3 gnus-emphasis-strikethru)
396        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
397         2 3 gnus-emphasis-underline))))
398   "*Alist that says how to fontify certain phrases.
399 Each item looks like this:
400
401   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
402
403 The first element is a regular expression to be matched.  The second
404 is a number that says what regular expression grouping used to find
405 the entire emphasized word.  The third is a number that says what
406 regexp grouping should be displayed and highlighted.  The fourth
407 is the face used for highlighting."
408   :type
409   '(repeat
410     (menu-choice
411      :format "%[Customizing Style%]\n%v"
412      :indent 2
413      (group :tag "Default"
414             :value ("" 0 0 default)
415             :value-create
416             (lambda (widget)
417               (let ((value (widget-get
418                             (cadr (widget-get (widget-get widget :parent)
419                                               :args))
420                             :value)))
421                 (if (not (eq (nth 2 value) 'default))
422                     (widget-put
423                      widget
424                      :value
425                      (gnus-emphasis-custom-value-to-external value))))
426               (widget-group-value-create widget))
427             regexp
428             (integer :format "Match group: %v")
429             (integer :format "Emphasize group: %v")
430             face)
431      (group :tag "Simple"
432             :value (("_" . "_") nil default)
433             (cons :format "%v"
434                   (regexp :format "Start regexp: %v")
435                   (regexp :format "End regexp: %v"))
436             (boolean :format "Show start and end patterns: %[%v%]\n"
437                      :on " On " :off " Off ")
438             face)))
439   :get (lambda (symbol)
440          (mapcar 'gnus-emphasis-custom-value-to-internal
441                  (default-value symbol)))
442   :set (lambda (symbol value)
443          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
444                                      value)))
445   :group 'gnus-article-emphasis)
446
447 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
448   "A regexp to describe whitespace which should not be emphasized.
449 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
450 The former avoids underlining of leading and trailing whitespace,
451 and the latter avoids underlining any whitespace at all."
452   :version "21.1"
453   :group 'gnus-article-emphasis
454   :type 'regexp)
455
456 (defface gnus-emphasis-bold '((t (:bold t)))
457   "Face used for displaying strong emphasized text (*word*)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-italic '((t (:italic t)))
461   "Face used for displaying italic emphasized text (/word/)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline '((t (:underline t)))
465   "Face used for displaying underlined emphasized text (_word_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
469   "Face used for displaying underlined bold emphasized text (_*word*_)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
473   "Face used for displaying underlined italic emphasized text (_/word/_)."
474   :group 'gnus-article-emphasis)
475
476 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
477   "Face used for displaying bold italic emphasized text (/*word*/)."
478   :group 'gnus-article-emphasis)
479
480 (defface gnus-emphasis-underline-bold-italic
481   '((t (:bold t :italic t :underline t)))
482   "Face used for displaying underlined bold italic emphasized text.
483 Example: (_/*word*/_)."
484   :group 'gnus-article-emphasis)
485
486 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
487                                       '((t (:strikethru t)))
488                                     '((t (:strike-through t))))
489   "Face used for displaying strike-through text (-word-)."
490   :group 'gnus-article-emphasis)
491
492 (defface gnus-emphasis-highlight-words
493   '((t (:background "black" :foreground "yellow")))
494   "Face used for displaying highlighted words."
495   :group 'gnus-article-emphasis)
496
497 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
498   "Format for display of Date headers in article bodies.
499 See `format-time-string' for the possible values.
500
501 The variable can also be function, which should return a complete Date
502 header.  The function is called with one argument, the time, which can
503 be fed to `format-time-string'."
504   :type '(choice string function)
505   :link '(custom-manual "(gnus)Article Date")
506   :group 'gnus-article-washing)
507
508 (defcustom gnus-save-all-headers t
509   "*If non-nil, don't remove any headers before saving.
510 This will be overridden by the `:headers' property that the symbol of
511 the saver function, which is specified by `gnus-default-article-saver',
512 might have."
513   :group 'gnus-article-saving
514   :type 'boolean)
515
516 (defcustom gnus-prompt-before-saving 'always
517   "*This variable says how much prompting is to be done when saving articles.
518 If it is nil, no prompting will be done, and the articles will be
519 saved to the default files.  If this variable is `always', each and
520 every article that is saved will be preceded by a prompt, even when
521 saving large batches of articles.  If this variable is neither nil not
522 `always', there the user will be prompted once for a file name for
523 each invocation of the saving commands."
524   :group 'gnus-article-saving
525   :type '(choice (item always)
526                  (item :tag "never" nil)
527                  (sexp :tag "once" :format "%t\n" :value t)))
528
529 (defcustom gnus-saved-headers gnus-visible-headers
530   "Headers to keep if `gnus-save-all-headers' is nil.
531 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
532 If that variable is nil, however, all headers that match this regexp
533 will be kept while the rest will be deleted before saving.  This and
534 `gnus-save-all-headers' will be overridden by the `:headers' property
535 that the symbol of the saver function, which is specified by
536 `gnus-default-article-saver', might have."
537   :group 'gnus-article-saving
538   :type 'regexp)
539
540 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
541 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
542   "A function to save articles in your favorite format.
543 The function will be called by way of the `gnus-summary-save-article'
544 command, and friends such as `gnus-summary-save-article-rmail'.
545
546 Gnus provides the following functions:
547
548 * gnus-summary-save-in-rmail (Rmail format)
549 * gnus-summary-save-in-mail (Unix mail format)
550 * gnus-summary-save-in-folder (MH folder)
551 * gnus-summary-save-in-file (article format)
552 * gnus-summary-save-body-in-file (article body)
553 * gnus-summary-save-in-vm (use VM's folder format)
554 * gnus-summary-write-to-file (article format -- overwrite)
555 * gnus-summary-write-body-to-file (article body -- overwrite)
556 * gnus-summary-save-in-pipe (article format)
557
558 The symbol of each function may have the following properties:
559
560 * :decode
561 The value non-nil means save decoded articles.  This is meaningful
562 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
563 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
564 `gnus-summary-save-in-pipe'.
565
566 * :function
567 The value specifies an alternative function which appends, not
568 overwrites, articles to a file.  This implies that when saving many
569 articles at a time, `gnus-prompt-before-saving' is bound to t and all
570 articles are saved in a single file.  This is meaningful only with
571 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
572
573 * :headers
574 The value specifies the symbol of a variable of which the value
575 specifies headers to be saved.  If it is omitted,
576 `gnus-save-all-headers' and `gnus-saved-headers' control what
577 headers should be saved."
578   :group 'gnus-article-saving
579   :type '(radio (function-item gnus-summary-save-in-rmail)
580                 (function-item gnus-summary-save-in-mail)
581                 (function-item gnus-summary-save-in-folder)
582                 (function-item gnus-summary-save-in-file)
583                 (function-item gnus-summary-save-body-in-file)
584                 (function-item gnus-summary-save-in-vm)
585                 (function-item gnus-summary-write-to-file)
586                 (function-item gnus-summary-write-body-to-file)
587                 (function-item gnus-summary-save-in-pipe)
588                 (function)))
589
590 (defcustom gnus-article-save-coding-system
591   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
592       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
593       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
594       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
595   "Coding system used to save decoded articles to a file.
596
597 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
598 which can safely encode any characters in text.  This is used by the
599 commands including:
600
601 * gnus-summary-save-article-file
602 * gnus-summary-save-article-body-file
603 * gnus-summary-write-article-file
604 * gnus-summary-write-article-body-file
605
606 and the functions to which you may set `gnus-default-article-saver':
607
608 * gnus-summary-save-in-file
609 * gnus-summary-save-body-in-file
610 * gnus-summary-write-to-file
611 * gnus-summary-write-body-to-file
612
613 Those commands and functions save just text displayed in the article
614 buffer to a file if the value of this variable is non-nil.  Note that
615 buttonized MIME parts will be lost in a saved file in that case.
616 Otherwise, raw articles will be saved."
617   :group 'gnus-article-saving
618   :type `(choice
619           :format "%{%t%}:\n %[Value Menu%] %v"
620           (const :tag "Save raw articles" nil)
621           ,@(delq nil
622                   (mapcar
623                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
624                    '((const :tag "UTF-8" utf-8)
625                      (const :tag "iso-2022-7bit" iso-2022-7bit)
626                      (const :tag "Emacs internal" emacs-mule)
627                      (const :tag "escape-quoted" escape-quoted))))
628           (symbol :tag "Coding system")))
629
630 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
631   "A function generating a file name to save articles in Rmail format.
632 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
633   :group 'gnus-article-saving
634   :type 'function)
635
636 (defcustom gnus-mail-save-name 'gnus-plain-save-name
637   "A function generating a file name to save articles in Unix mail format.
638 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
639   :group 'gnus-article-saving
640   :type 'function)
641
642 (defcustom gnus-folder-save-name 'gnus-folder-save-name
643   "A function generating a file name to save articles in MH folder.
644 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
645   :group 'gnus-article-saving
646   :type 'function)
647
648 (defcustom gnus-file-save-name 'gnus-numeric-save-name
649   "A function generating a file name to save articles in article format.
650 The function is called with NEWSGROUP, HEADERS, and optional
651 LAST-FILE."
652   :group 'gnus-article-saving
653   :type 'function)
654
655 (defcustom gnus-split-methods
656   '((gnus-article-archive-name)
657     (gnus-article-nndoc-name))
658   "*Variable used to suggest where articles are to be saved.
659 For instance, if you would like to save articles related to Gnus in
660 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
661 you could set this variable to something like:
662
663  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
664    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
665
666 This variable is an alist where the key is the match and the
667 value is a list of possible files to save in if the match is
668 non-nil.
669
670 If the match is a string, it is used as a regexp match on the
671 article.  If the match is a symbol, that symbol will be funcalled
672 from the buffer of the article to be saved with the newsgroup as the
673 parameter.  If it is a list, it will be evalled in the same buffer.
674
675 If this form or function returns a string, this string will be used as a
676 possible file name; and if it returns a non-nil list, that list will be
677 used as possible file names."
678   :group 'gnus-article-saving
679   :type '(repeat (choice (list :value (fun) function)
680                          (cons :value ("" "") regexp (repeat string))
681                          (sexp :value nil))))
682
683 (defcustom gnus-page-delimiter "^\^L"
684   "*Regexp describing what to use as article page delimiters.
685 The default value is \"^\^L\", which is a form linefeed at the
686 beginning of a line."
687   :type 'regexp
688   :group 'gnus-article-various)
689
690 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
691   "*The format specification for the article mode line.
692 See `gnus-summary-mode-line-format' for a closer description.
693
694 The following additional specs are available:
695
696 %w  The article washing status.
697 %m  The number of MIME parts in the article."
698   :version "24.1"
699   :type 'string
700   :group 'gnus-article-various)
701
702 (defcustom gnus-article-mode-hook nil
703   "*A hook for Gnus article mode."
704   :type 'hook
705   :group 'gnus-article-various)
706
707 (when (featurep 'xemacs)
708   ;; Extracted from gnus-xmas-define in order to preserve user settings
709   (when (fboundp 'turn-off-scroll-in-place)
710     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
711   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
712   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
713
714 (defcustom gnus-article-menu-hook nil
715   "*Hook run after the creation of the article mode menu."
716   :type 'hook
717   :group 'gnus-article-various)
718
719 (defcustom gnus-article-prepare-hook nil
720   "*A hook called after an article has been prepared in the article buffer."
721   :type 'hook
722   :group 'gnus-article-various)
723
724 (defcustom gnus-copy-article-ignored-headers nil
725   "List of headers to be removed when copying an article.
726 Each element is a regular expression."
727   :version "23.1" ;; No Gnus
728   :type '(repeat regexp)
729   :group 'gnus-article-various)
730
731 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
732                         "Gnus 5.10 (Emacs 22.1)")
733
734 (defface gnus-button
735   '((t (:weight bold)))
736   "Face used for highlighting a button in the article buffer."
737   :group 'gnus-article-buttons)
738
739 (defcustom gnus-article-button-face 'gnus-button
740   "Face used for highlighting buttons in the article buffer.
741
742 An article button is a piece of text that you can activate by pressing
743 `RET' or `mouse-2' above it."
744   :type 'face
745   :group 'gnus-article-buttons)
746
747 (defcustom gnus-article-mouse-face 'highlight
748   "Face used for mouse highlighting in the article buffer.
749
750 Article buttons will be displayed in this face when the cursor is
751 above them."
752   :type 'face
753   :group 'gnus-article-buttons)
754
755 (defcustom gnus-signature-face 'gnus-signature
756   "Face used for highlighting a signature in the article buffer.
757 Obsolete; use the face `gnus-signature' for customizations instead."
758   :type 'face
759   :group 'gnus-article-highlight
760   :group 'gnus-article-signature)
761
762 (defface gnus-signature
763   '((t
764      (:italic t)))
765   "Face used for highlighting a signature in the article buffer."
766   :group 'gnus-article-highlight
767   :group 'gnus-article-signature)
768 ;; backward-compatibility alias
769 (put 'gnus-signature-face 'face-alias 'gnus-signature)
770 (put 'gnus-signature-face 'obsolete-face "22.1")
771
772 (defface gnus-header-from
773   '((((class color)
774       (background dark))
775      (:foreground "PaleGreen1"))
776     (((class color)
777       (background light))
778      (:foreground "red3"))
779     (t
780      (:italic t)))
781   "Face used for displaying from headers."
782   :group 'gnus-article-headers
783   :group 'gnus-article-highlight)
784 ;; backward-compatibility alias
785 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
786 (put 'gnus-header-from-face 'obsolete-face "22.1")
787
788 (defface gnus-header-subject
789   '((((class color)
790       (background dark))
791      (:foreground "SeaGreen1"))
792     (((class color)
793       (background light))
794      (:foreground "red4"))
795     (t
796      (:bold t :italic t)))
797   "Face used for displaying subject headers."
798   :group 'gnus-article-headers
799   :group 'gnus-article-highlight)
800 ;; backward-compatibility alias
801 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
802 (put 'gnus-header-subject-face 'obsolete-face "22.1")
803
804 (defface gnus-header-newsgroups
805   '((((class color)
806       (background dark))
807      (:foreground "yellow" :italic t))
808     (((class color)
809       (background light))
810      (:foreground "MidnightBlue" :italic t))
811     (t
812      (:italic t)))
813   "Face used for displaying newsgroups headers.
814 In the default setup this face is only used for crossposted
815 articles."
816   :group 'gnus-article-headers
817   :group 'gnus-article-highlight)
818 ;; backward-compatibility alias
819 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
820 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
821
822 (defface gnus-header-name
823   '((((class color)
824       (background dark))
825      (:foreground "SpringGreen2"))
826     (((class color)
827       (background light))
828      (:foreground "maroon"))
829     (t
830      (:bold t)))
831   "Face used for displaying header names."
832   :group 'gnus-article-headers
833   :group 'gnus-article-highlight)
834 ;; backward-compatibility alias
835 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
836 (put 'gnus-header-name-face 'obsolete-face "22.1")
837
838 (defface gnus-header-content
839   '((((class color)
840       (background dark))
841      (:foreground "SpringGreen1" :italic t))
842     (((class color)
843       (background light))
844      (:foreground "indianred4" :italic t))
845     (t
846      (:italic t)))  "Face used for displaying header content."
847   :group 'gnus-article-headers
848   :group 'gnus-article-highlight)
849 ;; backward-compatibility alias
850 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
851 (put 'gnus-header-content-face 'obsolete-face "22.1")
852
853 (defcustom gnus-header-face-alist
854   '(("From" nil gnus-header-from)
855     ("Subject" nil gnus-header-subject)
856     ("Newsgroups:.*," nil gnus-header-newsgroups)
857     ("" gnus-header-name gnus-header-content))
858   "*Controls highlighting of article headers.
859
860 An alist of the form (HEADER NAME CONTENT).
861
862 HEADER is a regular expression which should match the name of a
863 header and NAME and CONTENT are either face names or nil.
864
865 The name of each header field will be displayed using the face
866 specified by the first element in the list where HEADER matches
867 the header name and NAME is non-nil.  Similarly, the content will
868 be displayed by the first non-nil matching CONTENT face."
869   :group 'gnus-article-headers
870   :group 'gnus-article-highlight
871   :type '(repeat (list (regexp :tag "Header")
872                        (choice :tag "Name"
873                                (item :tag "skip" nil)
874                                (face :value default))
875                        (choice :tag "Content"
876                                (item :tag "skip" nil)
877                                (face :value default)))))
878
879 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
880                                           '((xface . (:face gnus-x-face)))
881                                         '((pbm . (:face gnus-x-face))
882                                           (png . nil)))
883   "Alist of image types and properties applied to Face and X-Face images.
884 Here are examples:
885
886 ;; Specify the altitude of Face images in the From header.
887 \(setq gnus-face-properties-alist
888       '((pbm . (:face gnus-x-face :ascent 80))
889         (png . (:ascent 80))))
890
891 ;; Show Face images as pressed buttons.
892 \(setq gnus-face-properties-alist
893       '((pbm . (:face gnus-x-face :relief -2))
894         (png . (:relief -2))))
895
896 See the manual for the valid properties for various image types.
897 Currently, `pbm' is used for X-Face images and `png' is used for Face
898 images in Emacs.  Only the `:face' property is effective on the `xface'
899 image type in XEmacs if it is built with the libcompface library."
900   :version "23.1" ;; No Gnus
901   :group 'gnus-article-headers
902   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
903
904 (defcustom gnus-article-decode-hook
905   '(article-decode-charset article-decode-encoded-words
906                            article-decode-group-name article-decode-idna-rhs)
907   "*Hook run to decode charsets in articles."
908   :group 'gnus-article-headers
909   :type 'hook)
910
911 (defcustom gnus-display-mime-function 'gnus-display-mime
912   "Function to display MIME articles."
913   :group 'gnus-article-mime
914   :type 'function)
915
916 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
917   "Function used to decode headers.")
918
919 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
920   "Function used to decode addresses.")
921
922 (defvar gnus-article-dumbquotes-map
923   '((?\200 "EUR")
924     (?\202 ",")
925     (?\203 "f")
926     (?\204 ",,")
927     (?\205 "...")
928     (?\213 "<")
929     (?\214 "OE")
930     (?\221 "`")
931     (?\222 "'")
932     (?\223 "``")
933     (?\224 "\"")
934     (?\225 "*")
935     (?\226 "-")
936     (?\227 "--")
937     (?\230 "~")
938     (?\231 "(TM)")
939     (?\233 ">")
940     (?\234 "oe")
941     (?\264 "'"))
942   "Table for MS-to-Latin1 translation.")
943
944 (defcustom gnus-ignored-mime-types nil
945   "List of MIME types that should be ignored by Gnus."
946   :version "21.1"
947   :group 'gnus-article-mime
948   :type '(repeat regexp))
949
950 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
951   "List of MIME types that should not be given buttons when rendered inline.
952 See also `gnus-buttonized-mime-types' which may override this variable.
953 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
954   :version "21.1"
955   :group 'gnus-article-mime
956   :type '(repeat regexp))
957
958 (defcustom gnus-buttonized-mime-types nil
959   "List of MIME types that should be given buttons when rendered inline.
960 If set, this variable overrides `gnus-unbuttonized-mime-types'.
961 To see e.g. security buttons you could set this to
962 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
963 this list to display radio buttons that allow you to choose one of two
964 media types those mails include.  See also `mm-discouraged-alternatives'.
965 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
966   :version "22.1"
967   :group 'gnus-article-mime
968   :type '(repeat regexp))
969
970 (defcustom gnus-inhibit-mime-unbuttonizing nil
971   "If non-nil, all MIME parts get buttons.
972 When nil (the default value), then some MIME parts do not get buttons,
973 as described by the variables `gnus-buttonized-mime-types' and
974 `gnus-unbuttonized-mime-types'."
975   :version "22.1"
976   :group 'gnus-article-mime
977   :type 'boolean)
978
979 (defcustom gnus-body-boundary-delimiter "_"
980   "String used to delimit header and body.
981 This variable is used by `gnus-article-treat-body-boundary' which can
982 be controlled by `gnus-treat-body-boundary'."
983   :version "22.1"
984   :group 'gnus-article-various
985   :type '(choice (item :tag "None" :value nil)
986                  string))
987
988 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
989                                   "/usr/share/picons")
990   "Defines the location of the faces database.
991 For information on obtaining this database of pretty pictures, please
992 see http://www.cs.indiana.edu/picons/ftp/index.html"
993   :version "22.1"
994   :type '(repeat directory)
995   :link '(url-link :tag "download"
996                    "http://www.cs.indiana.edu/picons/ftp/index.html")
997   :link '(custom-manual "(gnus)Picons")
998   :group 'gnus-picon)
999
1000 (defun gnus-picons-installed-p ()
1001   "Say whether picons are installed on your machine."
1002   (let ((installed nil))
1003     (dolist (database gnus-picon-databases)
1004       (when (file-exists-p database)
1005         (setq installed t)))
1006     installed))
1007
1008 (defcustom gnus-article-mime-part-function nil
1009   "Function called with a MIME handle as the argument.
1010 This is meant for people who want to do something automatic based
1011 on parts -- for instance, adding Vcard info to a database."
1012   :group 'gnus-article-mime
1013   :type '(choice (const nil)
1014                  function))
1015
1016 (defcustom gnus-mime-multipart-functions nil
1017   "An alist of MIME types to functions to display them."
1018   :version "21.1"
1019   :group 'gnus-article-mime
1020   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1021
1022 (defcustom gnus-article-date-headers '(combined-lapsed)
1023   "A list of Date header formats to display.
1024 Valid formats are `ut' (universal time), `local' (local time
1025 zone), `english' (readable English), `lapsed' (elapsed time),
1026 `combined-lapsed' (both the original date and the elapsed time),
1027 `original' (the original date header), `iso8601' (ISO8601
1028 format), and `user-defined' (a user-defined format defined by the
1029 `gnus-article-time-format' variable).
1030
1031 You have as many date headers as you want in the article buffer.
1032 Some of these headers are updated automatically.  See
1033 `gnus-article-update-date-headers' for details."
1034   :version "24.1"
1035   :group 'gnus-article-headers
1036   :type '(repeat
1037           (item :tag "Universal time (UT)" :value 'ut)
1038           (item :tag "Local time zone" :value 'local)
1039           (item :tag "Readable English" :value 'english)
1040           (item :tag "Elapsed time" :value 'lapsed)
1041           (item :tag "Original and elapsed time" :value 'combined-lapsed)
1042           (item :tag "Original date header" :value 'original)
1043           (item :tag "ISO8601 format" :value 'iso8601)
1044           (item :tag "User-defined" :value 'user-defined)))
1045
1046 (defcustom gnus-article-update-date-headers nil
1047   "A number that says how often to update the date header (in seconds).
1048 If nil, don't update it at all."
1049   :version "24.1"
1050   :group 'gnus-article-headers
1051   :type '(choice
1052           (item :tag "Don't update" :value nil)
1053           integer))
1054
1055 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1056   "Function called with a MIME handle as the argument.
1057 This is meant for people who want to view first matched part.
1058 For `undisplayed-alternative' (default), the first undisplayed
1059 part or alternative part is used.  For `undisplayed', the first
1060 undisplayed part is used.  For a function, the first part which
1061 the function return t is used.  For nil, the first part is
1062 used."
1063   :version "21.1"
1064   :group 'gnus-article-mime
1065   :type '(choice
1066           (item :tag "first" :value nil)
1067           (item :tag "undisplayed" :value undisplayed)
1068           (item :tag "undisplayed or alternative"
1069                 :value undisplayed-alternative)
1070           (function)))
1071
1072 (defcustom gnus-mime-action-alist
1073   '(("save to file" . gnus-mime-save-part)
1074     ("save and strip" . gnus-mime-save-part-and-strip)
1075     ("replace with file" . gnus-mime-replace-part)
1076     ("delete part" . gnus-mime-delete-part)
1077     ("display as text" . gnus-mime-inline-part)
1078     ("view the part" . gnus-mime-view-part)
1079     ("pipe to command" . gnus-mime-pipe-part)
1080     ("toggle display" . gnus-article-press-button)
1081     ("toggle display" . gnus-article-view-part-as-charset)
1082     ("view as type" . gnus-mime-view-part-as-type)
1083     ("view internally" . gnus-mime-view-part-internally)
1084     ("view externally" . gnus-mime-view-part-externally))
1085   "An alist of actions that run on the MIME attachment."
1086   :group 'gnus-article-mime
1087   :type '(repeat (cons (string :tag "name")
1088                        (function))))
1089
1090 (defcustom gnus-auto-select-part 1
1091   "Advance to next MIME part when deleting or stripping parts.
1092
1093 When 0, point will be placed on the same part as before.  When
1094 positive (negative), move point forward (backwards) this many
1095 parts.  When nil, redisplay article."
1096   :version "23.1" ;; No Gnus
1097   :group 'gnus-article-mime
1098   :type '(choice (const nil :tag "Redisplay article.")
1099                  (const 1 :tag "Next part.")
1100                  (const 0 :tag "Current part.")
1101                  integer))
1102
1103 ;;;
1104 ;;; The treatment variables
1105 ;;;
1106
1107 (defvar gnus-part-display-hook nil
1108   "Hook called on parts that are to receive treatment.")
1109
1110 (defvar gnus-article-treat-custom
1111   '(choice (const :tag "Off" nil)
1112            (const :tag "On" t)
1113            (const :tag "Header" head)
1114            (const :tag "First" first)
1115            (const :tag "Last" last)
1116            (integer :tag "Less")
1117            (repeat :tag "Groups" regexp)
1118            (sexp :tag "Predicate")))
1119
1120 (defvar gnus-article-treat-head-custom
1121   '(choice (const :tag "Off" nil)
1122            (const :tag "Header" head)))
1123
1124 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1125                                    "text/x-patch")
1126   "Parts to treat.")
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 (interactive-p)
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 "\\(<\\)\\|\\(>\\)\\|&" nil t)
2889                      (replace-match (cond ((match-beginning 1) "&lt;")
2890                                           ((match-beginning 2) "&gt;")
2891                                           (t "&amp;"))))
2892                    (goto-char (point-min))
2893                    (insert "<pre>\n")
2894                    (goto-char (point-max))
2895                    (insert "</pre>\n<hr>\n")
2896                    ;; We have to examine charset one by one since
2897                    ;; charset specified in parts might be different.
2898                    (if (eq charset 'gnus-decoded)
2899                        (setq charset 'utf-8
2900                              eheader (mm-encode-coding-string (buffer-string)
2901                                                               charset)
2902                              title (when title
2903                                      (mm-encode-coding-string title charset))
2904                              body (mm-encode-coding-string content charset))
2905                      (setq hcharset (mm-find-mime-charset-region (point-min)
2906                                                                  (point-max)))
2907                      (cond ((= (length hcharset) 1)
2908                             (setq hcharset (car hcharset)
2909                                   coding (mm-charset-to-coding-system
2910                                           hcharset)))
2911                            ((> (length hcharset) 1)
2912                             (setq hcharset 'utf-8
2913                                   coding hcharset)))
2914                      (if coding
2915                          (if charset
2916                              (progn
2917                                (setq body
2918                                      (mm-charset-to-coding-system charset))
2919                                (if (eq coding body)
2920                                    (setq eheader (mm-encode-coding-string
2921                                                   (buffer-string) coding)
2922                                          title (when title
2923                                                  (mm-encode-coding-string
2924                                                   title coding))
2925                                          body content)
2926                                  (setq charset 'utf-8
2927                                        eheader (mm-encode-coding-string
2928                                                 (buffer-string) charset)
2929                                        title (when title
2930                                                (mm-encode-coding-string
2931                                                 title charset))
2932                                        body (mm-encode-coding-string
2933                                              (mm-decode-coding-string
2934                                               content body)
2935                                              charset))))
2936                            (setq charset hcharset
2937                                  eheader (mm-encode-coding-string
2938                                           (buffer-string) coding)
2939                                  title (when title
2940                                          (mm-encode-coding-string
2941                                           title coding))
2942                                  body content))
2943                        (setq eheader (mm-string-as-unibyte (buffer-string))
2944                              body content)))
2945                    (erase-buffer)
2946                    (mm-disable-multibyte)
2947                    (insert body)
2948                    (when charset
2949                      (mm-add-meta-html-tag handle charset t))
2950                    (when title
2951                      (goto-char (point-min))
2952                      (unless (search-forward "<title>" nil t)
2953                        (re-search-forward "<head>\\s-*" nil t)
2954                        (insert "<title>" title "</title>\n")))
2955                    (goto-char (point-min))
2956                    (or (re-search-forward
2957                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2958                        (re-search-forward
2959                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2960                    (insert eheader)
2961                    (mm-write-region (point-min) (point-max)
2962                                     tmp-file nil nil nil 'binary t))))
2963               (charset
2964                (mm-with-unibyte-buffer
2965                  (insert (if (eq charset 'gnus-decoded)
2966                              (mm-encode-coding-string content
2967                                                       (setq charset 'utf-8))
2968                            content))
2969                  (if (or (mm-add-meta-html-tag handle charset)
2970                          (not file))
2971                      (mm-write-region (point-min) (point-max)
2972                                       tmp-file nil nil nil 'binary t)
2973                    (setq tmp-file nil))))
2974               (tmp-file
2975                (mm-save-part-to-file handle tmp-file)))
2976              (when tmp-file
2977                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2978              (add-hook 'gnus-summary-prepare-exit-hook
2979                        'gnus-article-browse-delete-temp-files)
2980              (add-hook 'gnus-exit-gnus-hook
2981                        (lambda  ()
2982                          (gnus-article-browse-delete-temp-files t)))
2983              ;; FIXME: Warn if there's an <img> tag?
2984              (browse-url-of-file (or tmp-file (expand-file-name file)))
2985              (setq showed t))
2986             ;; If multipart, recurse
2987             ((equal (mm-handle-media-supertype handle) "multipart")
2988              (when (gnus-article-browse-html-parts handle header)
2989                (setq showed t)))
2990             ((equal (mm-handle-media-type handle) "message/rfc822")
2991              (mm-with-multibyte-buffer
2992                (mm-insert-part handle)
2993                (setq handle (mm-dissect-buffer t t))
2994                (when (and (bufferp (car handle))
2995                           (stringp (car (mm-handle-type handle))))
2996                  (setq handle (list handle)))
2997                (when header
2998                  (article-decode-encoded-words)
2999                  (let ((gnus-visible-headers
3000                         (or (get 'gnus-visible-headers 'standard-value)
3001                             gnus-visible-headers)))
3002                    (article-hide-headers))
3003                  (goto-char (point-min))
3004                  (search-forward "\n\n" nil 'move)
3005                  (skip-chars-backward "\t\n ")
3006                  (setq header (buffer-substring (point-min) (point)))))
3007              (when (prog1
3008                        (gnus-article-browse-html-parts handle header)
3009                      (mm-destroy-parts handle))
3010                (setq showed t)))))
3011     showed))
3012
3013 (defun gnus-article-browse-html-article (&optional arg)
3014   "View \"text/html\" parts of the current article with a WWW browser.
3015 Inline images embedded in a message using the cid scheme, as they are
3016 generally considered to be safe, will be processed properly.
3017 The message header is added to the beginning of every html part unless
3018 the prefix argument ARG is given.
3019
3020 Warning: Spammers use links to images (using the http scheme) in HTML
3021 articles to verify whether you have read the message.  As
3022 `gnus-article-browse-html-article' passes the HTML content to the
3023 browser without eliminating these \"web bugs\" you should only
3024 use it for mails from trusted senders.
3025
3026 If you always want to display HTML parts in the browser, set
3027 `mm-text-html-renderer' to nil.
3028
3029 This command creates temporary files to pass HTML contents including
3030 images if any to the browser, and deletes them when exiting the group
3031 \(if you want)."
3032   ;; Cf. `mm-w3m-safe-url-regexp'
3033   (interactive "P")
3034   (if arg
3035       (gnus-summary-show-article)
3036     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3037                                     gnus-visible-headers))
3038           ;; As we insert a <hr>, there's no need for the body boundary.
3039           (gnus-treat-body-boundary nil))
3040       (gnus-summary-show-article)))
3041   (with-current-buffer gnus-article-buffer
3042     (let ((header (unless arg
3043                     (save-restriction
3044                       (widen)
3045                       (buffer-substring-no-properties
3046                        (goto-char (point-min))
3047                        (if (search-forward "\n\n" nil t)
3048                            (match-beginning 0)
3049                          (goto-char (point-max))
3050                          (skip-chars-backward "\t\n ")
3051                          (point))))))
3052           parts)
3053       (set-buffer gnus-original-article-buffer)
3054       (setq parts (mm-dissect-buffer t t))
3055       ;; If singlepart, enforce a list.
3056       (when (and (bufferp (car parts))
3057                  (stringp (car (mm-handle-type parts))))
3058         (setq parts (list parts)))
3059       ;; Process the list
3060       (unless (gnus-article-browse-html-parts parts header)
3061         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3062       (mm-destroy-parts parts)
3063       (unless arg
3064         (gnus-summary-show-article)))))
3065
3066 (defun article-hide-list-identifiers ()
3067   "Remove list identifies from the Subject header.
3068 The `gnus-list-identifiers' variable specifies what to do."
3069   (interactive)
3070   (let ((inhibit-point-motion-hooks t)
3071         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3072         (inhibit-read-only t))
3073     (when regexp
3074       (save-excursion
3075         (save-restriction
3076           (article-narrow-to-head)
3077           (goto-char (point-min))
3078           (while (re-search-forward
3079                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3080                   nil t)
3081             (delete-region (match-beginning 2) (match-end 0))
3082             (beginning-of-line))
3083           (when (re-search-forward
3084                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3085             (delete-region (match-beginning 1) (match-end 1))))))))
3086
3087 (defun article-hide-pem (&optional arg)
3088   "Toggle hiding of any PEM headers and signatures in the current article.
3089 If given a negative prefix, always show; if given a positive prefix,
3090 always hide."
3091   (interactive (gnus-article-hidden-arg))
3092   (unless (gnus-article-check-hidden-text 'pem arg)
3093     (save-excursion
3094       (let ((inhibit-read-only t) end)
3095         (goto-char (point-min))
3096         ;; Hide the horrendously ugly "header".
3097         (when (and (search-forward
3098                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3099                     nil t)
3100                    (setq end (1+ (match-beginning 0))))
3101           (gnus-add-wash-type 'pem)
3102           (gnus-article-hide-text-type
3103            end
3104            (if (search-forward "\n\n" nil t)
3105                (match-end 0)
3106              (point-max))
3107            'pem)
3108           ;; Hide the trailer as well
3109           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3110                                 nil t)
3111             (gnus-article-hide-text-type
3112              (match-beginning 0) (match-end 0) 'pem)))))))
3113
3114 (defun article-strip-banner ()
3115   "Strip the banners specified by the `banner' group parameter and by
3116 `gnus-article-address-banner-alist'."
3117   (interactive)
3118   (save-excursion
3119     (save-restriction
3120       (let ((inhibit-point-motion-hooks t))
3121         (when (gnus-parameter-banner gnus-newsgroup-name)
3122           (article-really-strip-banner
3123            (gnus-parameter-banner gnus-newsgroup-name)))
3124         (when gnus-article-address-banner-alist
3125           ;; Note that the From header is decoded here, so it is
3126           ;; required that the *-extract-address-components function
3127           ;; supports non-ASCII text.
3128           (let ((from (save-restriction
3129                         (widen)
3130                         (article-narrow-to-head)
3131                         (mail-fetch-field "from"))))
3132             (when (and from
3133                        (setq from
3134                              (cadr (funcall gnus-extract-address-components
3135                                             from))))
3136               (catch 'found
3137                 (dolist (pair gnus-article-address-banner-alist)
3138                   (when (string-match (car pair) from)
3139                     (throw 'found
3140                            (article-really-strip-banner (cdr pair)))))))))))))
3141
3142 (defun article-really-strip-banner (banner)
3143   "Strip the banner specified by the argument."
3144   (save-excursion
3145     (save-restriction
3146       (let ((inhibit-point-motion-hooks t)
3147             (gnus-signature-limit nil)
3148             (inhibit-read-only t))
3149         (article-goto-body)
3150         (cond
3151          ((eq banner 'signature)
3152           (when (gnus-article-narrow-to-signature)
3153             (widen)
3154             (forward-line -1)
3155             (delete-region (point) (point-max))))
3156          ((symbolp banner)
3157           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3158               (while (re-search-forward banner nil t)
3159                 (delete-region (match-beginning 0) (match-end 0)))))
3160          ((stringp banner)
3161           (while (re-search-forward banner nil t)
3162             (delete-region (match-beginning 0) (match-end 0)))))))))
3163
3164 (defun article-babel ()
3165   "Translate article using an online translation service."
3166   (interactive)
3167   (require 'babel)
3168   (gnus-with-article-buffer
3169     (when (article-goto-body)
3170       (let* ((start (point))
3171              (end (point-max))
3172              (orig (buffer-substring start end))
3173              (trans (babel-as-string orig)))
3174         (save-restriction
3175           (narrow-to-region start end)
3176           (delete-region start end)
3177           (insert trans))))))
3178
3179 (defun article-hide-signature (&optional arg)
3180   "Hide the signature in the current article.
3181 If given a negative prefix, always show; if given a positive prefix,
3182 always hide."
3183   (interactive (gnus-article-hidden-arg))
3184   (unless (gnus-article-check-hidden-text 'signature arg)
3185     (save-excursion
3186       (save-restriction
3187         (let ((inhibit-read-only t))
3188           (when (gnus-article-narrow-to-signature)
3189             (gnus-article-hide-text-type
3190              (point-min) (point-max) 'signature))))))
3191   (gnus-set-mode-line 'article))
3192
3193 (defun article-strip-headers-in-body ()
3194   "Strip offensive headers from bodies."
3195   (interactive)
3196   (save-excursion
3197     (article-goto-body)
3198     (let ((case-fold-search t))
3199       (when (looking-at "x-no-archive:")
3200         (gnus-delete-line)))))
3201
3202 (defun article-strip-leading-blank-lines ()
3203   "Remove all blank lines from the beginning of the article."
3204   (interactive)
3205   (save-excursion
3206     (let ((inhibit-point-motion-hooks t)
3207           (inhibit-read-only t))
3208       (when (article-goto-body)
3209         (while (and (not (eobp))
3210                     (looking-at "[ \t]*$"))
3211           (gnus-delete-line))))))
3212
3213 (defun article-narrow-to-head ()
3214   "Narrow the buffer to the head of the message.
3215 Point is left at the beginning of the narrowed-to region."
3216   (narrow-to-region
3217    (goto-char (point-min))
3218    (cond
3219     ;; Absolutely no headers displayed.
3220     ((looking-at "\n")
3221      (point))
3222     ;; Normal headers.
3223     ((search-forward "\n\n" nil 1)
3224      (1- (point)))
3225     ;; Nothing but headers.
3226     (t
3227      (point-max))))
3228   (goto-char (point-min)))
3229
3230 (defun article-goto-body ()
3231   "Place point at the start of the body."
3232   (goto-char (point-min))
3233   (cond
3234    ;; This variable is only bound when dealing with separate
3235    ;; MIME body parts.
3236    (article-goto-body-goes-to-point-min-p
3237     t)
3238    ((search-forward "\n\n" nil t)
3239     t)
3240    (t
3241     (goto-char (point-max))
3242     nil)))
3243
3244 (defun article-strip-multiple-blank-lines ()
3245   "Replace consecutive blank lines with one empty line."
3246   (interactive)
3247   (save-excursion
3248     (let ((inhibit-point-motion-hooks t)
3249           (inhibit-read-only t))
3250       ;; First make all blank lines empty.
3251       (article-goto-body)
3252       (while (re-search-forward "^[ \t]+$" nil t)
3253         (unless (gnus-annotation-in-region-p
3254                  (match-beginning 0) (match-end 0))
3255           (replace-match "" nil t)))
3256       ;; Then replace multiple empty lines with a single empty line.
3257       (article-goto-body)
3258       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3259         (unless (gnus-annotation-in-region-p
3260                  (match-beginning 0) (match-end 0))
3261           (delete-region (match-beginning 1) (match-end 1)))))))
3262
3263 (defun article-strip-leading-space ()
3264   "Remove all white space from the beginning of the lines in the article."
3265   (interactive)
3266   (save-excursion
3267     (let ((inhibit-point-motion-hooks t)
3268           (inhibit-read-only t))
3269       (article-goto-body)
3270       (while (re-search-forward "^[ \t]+" nil t)
3271         (replace-match "" t t)))))
3272
3273 (defun article-strip-trailing-space ()
3274   "Remove all white space from the end of the lines in the article."
3275   (interactive)
3276   (save-excursion
3277     (let ((inhibit-point-motion-hooks t)
3278           (inhibit-read-only t))
3279       (article-goto-body)
3280       (while (re-search-forward "[ \t]+$" nil t)
3281         (replace-match "" t t)))))
3282
3283 (defun article-strip-blank-lines ()
3284   "Strip leading, trailing and multiple blank lines."
3285   (interactive)
3286   (article-strip-leading-blank-lines)
3287   (article-remove-trailing-blank-lines)
3288   (article-strip-multiple-blank-lines))
3289
3290 (defun article-strip-all-blank-lines ()
3291   "Strip all blank lines."
3292   (interactive)
3293   (save-excursion
3294     (let ((inhibit-point-motion-hooks t)
3295           (inhibit-read-only t))
3296       (article-goto-body)
3297       (while (re-search-forward "^[ \t]*\n" nil t)
3298         (replace-match "" t t)))))
3299
3300 (defun gnus-article-narrow-to-signature ()
3301   "Narrow to the signature; return t if a signature is found, else nil."
3302   (let ((inhibit-point-motion-hooks t))
3303     (when (gnus-article-search-signature)
3304       (forward-line 1)
3305       ;; Check whether we have some limits to what we consider
3306       ;; to be a signature.
3307       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3308                       (list gnus-signature-limit)))
3309             limit limited)
3310         (while (setq limit (pop limits))
3311           (if (or (and (integerp limit)
3312                        (< (- (point-max) (point)) limit))
3313                   (and (floatp limit)
3314                        (< (count-lines (point) (point-max)) limit))
3315                   (and (functionp limit)
3316                        (funcall limit))
3317                   (and (stringp limit)
3318                        (not (re-search-forward limit nil t))))
3319               ()                        ; This limit did not succeed.
3320             (setq limited t
3321                   limits nil)))
3322         (unless limited
3323           (narrow-to-region (point) (point-max))
3324           t)))))
3325
3326 (defun gnus-article-search-signature ()
3327   "Search the current buffer for the signature separator.
3328 Put point at the beginning of the signature separator."
3329   (let ((cur (point)))
3330     (goto-char (point-max))
3331     (if (if (stringp gnus-signature-separator)
3332             (re-search-backward gnus-signature-separator nil t)
3333           (let ((seps gnus-signature-separator))
3334             (while (and seps
3335                         (not (re-search-backward (car seps) nil t)))
3336               (pop seps))
3337             seps))
3338         t
3339       (goto-char cur)
3340       nil)))
3341
3342 (defun gnus-article-hidden-arg ()
3343   "Return the current prefix arg as a number, or 0 if no prefix."
3344   (list (if current-prefix-arg
3345             (prefix-numeric-value current-prefix-arg)
3346           0)))
3347
3348 (defun gnus-article-check-hidden-text (type arg)
3349   "Return nil if hiding is necessary.
3350 Arg can be nil or a number.  nil and positive means hide, negative
3351 means show, 0 means toggle."
3352   (save-excursion
3353     (save-restriction
3354       (let ((hide (gnus-article-hidden-text-p type)))
3355         (cond
3356          ((or (null arg)
3357               (> arg 0))
3358           nil)
3359          ((< arg 0)
3360           (gnus-article-show-hidden-text type)
3361           t)
3362          (t
3363           (if (eq hide 'hidden)
3364               (progn
3365                 (gnus-article-show-hidden-text type)
3366                 t)
3367             nil)))))))
3368
3369 (defun gnus-article-hidden-text-p (type)
3370   "Say whether the current buffer contains hidden text of type TYPE."
3371   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3372     (while (and pos
3373                 (not (get-text-property pos 'invisible))
3374                 (not (get-text-property pos 'dummy-invisible)))
3375       (setq pos
3376             (text-property-any (1+ pos) (point-max) 'article-type type)))
3377     (if pos
3378         'hidden
3379       nil)))
3380
3381 (defun gnus-article-show-hidden-text (type &optional dummy)
3382   "Show all hidden text of type TYPE.
3383 Originally it is hide instead of DUMMY."
3384   (let ((inhibit-read-only t)
3385         (inhibit-point-motion-hooks t))
3386     (gnus-remove-text-properties-when
3387      'article-type type
3388      (point-min) (point-max)
3389      (cons 'article-type (cons type
3390                                gnus-hidden-properties)))
3391     (gnus-delete-wash-type type)))
3392
3393 (defconst article-time-units
3394   `((year . ,(* 365.25 24 60 60))
3395     (week . ,(* 7 24 60 60))
3396     (day . ,(* 24 60 60))
3397     (hour . ,(* 60 60))
3398     (minute . 60)
3399     (second . 1))
3400   "Mapping from time units to seconds.")
3401
3402 (defun gnus-article-forward-header ()
3403   "Move point to the start of the next header.
3404 If the current header is a continuation header, this can be several
3405 lines forward."
3406   (let ((ended nil))
3407     (while (not ended)
3408       (forward-line 1)
3409       (if (looking-at "[ \t]+[^ \t]")
3410           (forward-line 1)
3411         (setq ended t)))))
3412
3413 (defun article-treat-date ()
3414   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3415                        (with-current-buffer gnus-summary-buffer
3416                          gnus-article-date-headers)
3417                      gnus-article-date-headers)
3418                    t))
3419
3420 (defun article-date-ut (&optional type highlight date-position)
3421   "Convert DATE date to TYPE in the current article.
3422 The default type is `ut'.  See `gnus-article-date-headers' for
3423 possible values."
3424   (interactive (list 'ut t))
3425   (let* ((case-fold-search t)
3426          (inhibit-read-only t)
3427          (inhibit-point-motion-hooks t)
3428          (first t)
3429          (visible-date (mail-fetch-field "Date"))
3430          pos date bface eface)
3431     (save-excursion
3432       (goto-char (point-min))
3433       (when (re-search-forward "^Date:" nil t)
3434         (setq bface (get-text-property (point-at-bol) 'face)
3435               eface (get-text-property (1- (point-at-eol)) 'face)))
3436       ;; Delete any old Date headers.
3437       (if date-position
3438           (progn
3439             (goto-char date-position)
3440             (setq date (get-text-property (point) 'original-date))
3441             (delete-region (point)
3442                            (progn
3443                              (gnus-article-forward-header)
3444                              (point)))
3445             (article-transform-date date type bface eface))
3446         (save-restriction
3447           (widen)
3448           (goto-char (point-min))
3449           (while (or (get-text-property (setq pos (point)) 'original-date)
3450                      (and (setq pos (next-single-property-change
3451                                      (point) 'original-date))
3452                           (goto-char pos)))
3453             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3454                                       (1+ (match-beginning 0))
3455                                     (point-max)))
3456             (goto-char (point-min))
3457             (while (re-search-forward "^Date:" nil t)
3458               (setq date (get-text-property (match-beginning 0) 'original-date))
3459               (delete-region (point-at-bol) (progn
3460                                               (gnus-article-forward-header)
3461                                               (point))))
3462             (when (and (not date)
3463                        visible-date)
3464               (setq date visible-date))
3465             (when date
3466               (article-transform-date date type bface eface))
3467             (goto-char (point-max))
3468             (widen)))))))
3469
3470 (defun article-transform-date (date type bface eface)
3471   (dolist (this-type (cond
3472                       ((null type)
3473                        (list 'ut))
3474                       ((atom type)
3475                        (list type))
3476                       (t
3477                        type)))
3478     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3479     (forward-line -1)
3480     (beginning-of-line)
3481     (put-text-property (point) (1+ (point))
3482                        'original-date date)
3483     (put-text-property (point) (1+ (point))
3484                        'gnus-date-type this-type)
3485     ;; Do highlighting.
3486     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3487       (put-text-property (match-beginning 1) (1+ (match-end 1))
3488                          'face bface)
3489       (put-text-property (match-beginning 2) (match-end 2)
3490                          'face eface))
3491     (forward-line 1)))
3492
3493 (defun article-make-date-line (date type)
3494   "Return a DATE line of TYPE."
3495   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3496                              combined-lapsed))
3497     (error "Unknown conversion type: %s" type))
3498   (condition-case ()
3499       (let ((time (ignore-errors (date-to-time date))))
3500         (cond
3501          ;; Convert to the local timezone.
3502          ((eq type 'local)
3503           (concat "Date: " (message-make-date time)))
3504          ;; Convert to Universal Time.
3505          ((eq type 'ut)
3506           (concat "Date: "
3507                   (substring
3508                    (message-make-date
3509                     (let* ((e (parse-time-string date))
3510                            (tm (apply 'encode-time e))
3511                            (ms (car tm))
3512                            (ls (- (cadr tm) (car (current-time-zone time)))))
3513                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3514                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3515                             (t (list ms ls)))))
3516                    0 -5)
3517                   "UT"))
3518          ;; Get the original date from the article.
3519          ((eq type 'original)
3520           (concat "Date: " (if (string-match "\n+$" date)
3521                                (substring date 0 (match-beginning 0))
3522                              date)))
3523          ;; Let the user define the format.
3524          ((eq type 'user-defined)
3525           (let ((format (or (condition-case nil
3526                                 (with-current-buffer gnus-summary-buffer
3527                                   gnus-article-time-format)
3528                               (error nil))
3529                             gnus-article-time-format)))
3530             (if (functionp format)
3531                 (funcall format time)
3532               (concat "Date: " (format-time-string format time)))))
3533          ;; ISO 8601.
3534          ((eq type 'iso8601)
3535           (let ((tz (car (current-time-zone time))))
3536             (concat
3537              "Date: "
3538              (format-time-string "%Y%m%dT%H%M%S" time)
3539              (format "%s%02d%02d"
3540                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3541                      (/ (% (abs tz) 3600) 60)))))
3542          ;; Do a lapsed format.
3543          ((eq type 'lapsed)
3544           (concat "Date: " (article-lapsed-string time)))
3545          ;; A combined date/lapsed format.
3546          ((eq type 'combined-lapsed)
3547           (let ((date-string (article-make-date-line date 'original))
3548                 (segments 3)
3549                 lapsed-string)
3550             (while (and
3551                     time
3552                     (setq lapsed-string
3553                           (concat " (" (article-lapsed-string time segments) ")"))
3554                     (> (+ (length date-string)
3555                           (length lapsed-string))
3556                        (+ fill-column 6))
3557                     (> segments 0))
3558               (setq segments (1- segments)))
3559             (if (> segments 0)
3560                 (concat date-string lapsed-string)
3561               date-string)))
3562          ;; Display the date in proper English
3563          ((eq type 'english)
3564           (let ((dtime (decode-time time)))
3565             (concat
3566              "Date: the "
3567              (number-to-string (nth 3 dtime))
3568              (let ((digit (% (nth 3 dtime) 10)))
3569                (cond
3570                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3571                 ((= digit 1) "st")
3572                 ((= digit 2) "nd")
3573                 ((= digit 3) "rd")
3574                 (t "th")))
3575              " of "
3576              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3577              " "
3578              (number-to-string (nth 5 dtime))
3579              " at "
3580              (format "%02d" (nth 2 dtime))
3581              ":"
3582              (format "%02d" (nth 1 dtime)))))))
3583     (foo
3584      (format "Date: %s (from Gnus)" date))))
3585
3586 (defun article-lapsed-string (time &optional max-segments)
3587   ;; If the date is seriously mangled, the timezone functions are
3588   ;; liable to bug out, so we ignore all errors.
3589   (let* ((now (current-time))
3590          (real-time (subtract-time now time))
3591          (real-sec (and real-time
3592                         (+ (* (float (car real-time)) 65536)
3593                            (cadr real-time))))
3594          (sec (and real-time (abs real-sec)))
3595          (segments 0)
3596          num prev)
3597     (unless max-segments
3598       (setq max-segments (length article-time-units)))
3599     (cond
3600      ((null real-time)
3601       "Unknown")
3602      ((zerop sec)
3603       "Now")
3604      (t
3605       (concat
3606        ;; This is a bit convoluted, but basically we go
3607        ;; through the time units for years, weeks, etc,
3608        ;; and divide things to see whether that results
3609        ;; in positive answers.
3610        (mapconcat
3611         (lambda (unit)
3612           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3613                   (>= segments max-segments))
3614               ;; The (remaining) seconds are too few to
3615               ;; be divided into this time unit.
3616               ""
3617             ;; It's big enough, so we output it.
3618             (setq sec (- sec (* num (cdr unit))))
3619             (prog1
3620                 (concat (if prev ", " "") (int-to-string
3621                                            (floor num))
3622                         " " (symbol-name (car unit))
3623                         (if (> num 1) "s" ""))
3624               (setq prev t
3625                     segments (1+ segments)))))
3626         article-time-units "")
3627        ;; If dates are odd, then it might appear like the
3628        ;; article was sent in the future.
3629        (if (> real-sec 0)
3630            " ago"
3631          " in the future"))))))
3632
3633 (defun article-date-local (&optional highlight)
3634   "Convert the current article date to the local timezone."
3635   (interactive (list t))
3636   (article-date-ut 'local highlight))
3637
3638 (defun article-date-english (&optional highlight)
3639   "Convert the current article date to something that is proper English."
3640   (interactive (list t))
3641   (article-date-ut 'english highlight))
3642
3643 (defun article-date-original (&optional highlight)
3644   "Convert the current article date to what it was originally.
3645 This is only useful if you have used some other date conversion
3646 function and want to see what the date was before converting."
3647   (interactive (list t))
3648   (article-date-ut 'original highlight))
3649
3650 (defun article-date-lapsed (&optional highlight)
3651   "Convert the current article date to time lapsed since it was sent."
3652   (interactive (list t))
3653   (article-date-ut 'lapsed highlight))
3654
3655 (defun article-date-combined-lapsed (&optional highlight)
3656   "Convert the current article date to time lapsed since it was sent."
3657   (interactive (list t))
3658   (article-date-ut 'combined-lapsed highlight))
3659
3660 (defun article-update-date-lapsed ()
3661   "Function to be run from a timer to update the lapsed time line."
3662   (save-match-data
3663     (let ((buffer (current-buffer)))
3664       (ignore-errors
3665         (walk-windows
3666          (lambda (w)
3667            (set-buffer (window-buffer w))
3668            (when (eq major-mode 'gnus-article-mode)
3669              (let ((old-line (count-lines (point-min) (point)))
3670                    (old-column (- (point) (line-beginning-position)))
3671                    (window-start
3672                     (window-start (get-buffer-window (current-buffer)))))
3673                (goto-char (point-min))
3674                (while (re-search-forward "^Date:" nil t)
3675                  (let ((type (get-text-property (match-beginning 0)
3676                                                 'gnus-date-type)))
3677                    (when (memq type '(lapsed combined-lapsed user-format))
3678                      (when (and window-start
3679                                 (not (= window-start
3680                                         (save-excursion
3681                                           (forward-line 1)
3682                                           (point)))))
3683                        (setq window-start nil))
3684                      (save-excursion
3685                        (article-date-ut type t (match-beginning 0)))
3686                      (forward-line 1)
3687                      (when window-start
3688                        (set-window-start (get-buffer-window (current-buffer))
3689                                          (point))))))
3690                (goto-char (point-min))
3691                (when (> old-column 0)
3692                  (setq old-line (1- old-line)))
3693                (forward-line old-line)
3694                (end-of-line)
3695                (when (> (current-column) old-column)
3696                  (beginning-of-line)
3697                  (forward-char old-column)))))
3698          nil 'visible))
3699       (set-buffer buffer))))
3700
3701 (defun gnus-start-date-timer (&optional n)
3702   "Start a timer to update the Date headers in the article buffers.
3703 The numerical prefix says how frequently (in seconds) the function
3704 is to run."
3705   (interactive "p")
3706   (unless n
3707     (setq n 1))
3708   (gnus-stop-date-timer)
3709   (setq article-lapsed-timer
3710         (run-at-time 1 n 'article-update-date-lapsed)))
3711
3712 (defun gnus-stop-date-timer ()
3713   "Stop the Date timer."
3714   (interactive)
3715   (when article-lapsed-timer
3716     (nnheader-cancel-timer article-lapsed-timer)
3717     (setq article-lapsed-timer nil)))
3718
3719 (defun article-date-user (&optional highlight)
3720   "Convert the current article date to the user-defined format.
3721 This format is defined by the `gnus-article-time-format' variable."
3722   (interactive (list t))
3723   (article-date-ut 'user highlight))
3724
3725 (defun article-date-iso8601 (&optional highlight)
3726   "Convert the current article date to ISO8601."
3727   (interactive (list t))
3728   (article-date-ut 'iso8601 highlight))
3729
3730 (defmacro gnus-article-save-original-date (&rest forms)
3731   "Save the original date as a text property and evaluate FORMS."
3732   `(let* ((case-fold-search t)
3733           (start (progn
3734                    (goto-char (point-min))
3735                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3736                               (not (bolp)))
3737                      (match-end 0))))
3738           (date (when (and start
3739                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3740                                               nil t))
3741                   (buffer-substring-no-properties start
3742                                                   (match-beginning 0)))))
3743      (goto-char (point-max))
3744      (skip-chars-backward "\n")
3745      (put-text-property (point-min) (point) 'original-date date)
3746      ,@forms
3747      (goto-char (point-max))
3748      (skip-chars-backward "\n")
3749      (put-text-property (point-min) (point) 'original-date date)))
3750
3751 ;; (defun article-show-all ()
3752 ;;   "Show all hidden text in the article buffer."
3753 ;;   (interactive)
3754 ;;   (save-excursion
3755 ;;     (let ((inhibit-read-only t))
3756 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3757
3758 (defun article-remove-leading-whitespace ()
3759   "Remove excessive whitespace from all headers."
3760   (interactive)
3761   (save-excursion
3762     (save-restriction
3763       (let ((inhibit-read-only t))
3764         (article-narrow-to-head)
3765         (goto-char (point-min))
3766         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3767           (delete-region (match-beginning 1) (match-end 1)))))))
3768
3769 (defun article-emphasize (&optional arg)
3770   "Emphasize text according to `gnus-emphasis-alist'."
3771   (interactive (gnus-article-hidden-arg))
3772   (unless (gnus-article-check-hidden-text 'emphasis arg)
3773     (save-excursion
3774       (let ((alist (or
3775                     (condition-case nil
3776                         (with-current-buffer gnus-summary-buffer
3777                           gnus-article-emphasis-alist)
3778                       (error))
3779                     gnus-emphasis-alist))
3780             (inhibit-read-only t)
3781             (props (append '(article-type emphasis)
3782                            gnus-hidden-properties))
3783             regexp elem beg invisible visible face)
3784         (article-goto-body)
3785         (setq beg (point))
3786         (while (setq elem (pop alist))
3787           (goto-char beg)
3788           (setq regexp (car elem)
3789                 invisible (nth 1 elem)
3790                 visible (nth 2 elem)
3791                 face (nth 3 elem))
3792           (while (re-search-forward regexp nil t)
3793             (when (and (match-beginning visible) (match-beginning invisible))
3794               (gnus-article-hide-text
3795                (match-beginning invisible) (match-end invisible) props)
3796               (gnus-article-unhide-text-type
3797                (match-beginning visible) (match-end visible) 'emphasis)
3798               (gnus-put-overlay-excluding-newlines
3799                (match-beginning visible) (match-end visible) 'face face)
3800               (gnus-add-wash-type 'emphasis)
3801               (goto-char (match-end invisible)))))))))
3802
3803 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3804   "Setup newsgroup emphasis alist."
3805   (unless gnus-article-emphasis-alist
3806     (let ((name (and gnus-newsgroup-name
3807                      (gnus-group-real-name gnus-newsgroup-name))))
3808       (make-local-variable 'gnus-article-emphasis-alist)
3809       (setq gnus-article-emphasis-alist
3810             (nconc
3811              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3812                (while (setq elem (pop alist))
3813                  (when (and name (string-match (car elem) name))
3814                    (setq alist nil
3815                          highlight (copy-sequence (cdr elem)))))
3816                highlight)
3817              (copy-sequence highlight-words)
3818              (if gnus-newsgroup-name
3819                  (copy-sequence (gnus-group-find-parameter
3820                                  gnus-newsgroup-name 'highlight-words t)))
3821              gnus-emphasis-alist)))))
3822
3823 (defvar gnus-summary-article-menu)
3824 (defvar gnus-summary-post-menu)
3825
3826 ;;; Saving functions.
3827
3828 (defun gnus-article-save (save-buffer file &optional num)
3829   "Save the currently selected article."
3830   (when (or (get gnus-default-article-saver :headers)
3831             (not gnus-save-all-headers))
3832     ;; Remove headers according to `gnus-saved-headers' or the value
3833     ;; of the `:headers' property that the saver function might have.
3834     (let ((gnus-visible-headers
3835            (or (symbol-value (get gnus-default-article-saver :headers))
3836                gnus-saved-headers gnus-visible-headers))
3837           ;; Ignore group parameter.  See `article-hide-headers'.
3838           (gnus-summary-buffer nil))
3839       (with-current-buffer save-buffer
3840         (article-hide-headers 1 t))))
3841   (save-window-excursion
3842     (if (not gnus-default-article-saver)
3843         (error "No default saver is defined")
3844       ;; !!! Magic!  The saving functions all save
3845       ;; `gnus-save-article-buffer' (or so they think), but we
3846       ;; bind that variable to our save-buffer.
3847       (set-buffer gnus-article-buffer)
3848       (let* ((gnus-save-article-buffer save-buffer)
3849              (filename
3850               (cond
3851                ((not gnus-prompt-before-saving) 'default)
3852                ((eq gnus-prompt-before-saving 'always) nil)
3853                (t file)))
3854              (gnus-number-of-articles-to-be-saved
3855               (when (eq gnus-prompt-before-saving t)
3856                 num)))                  ; Magic
3857         (set-buffer gnus-article-current-summary)
3858         (funcall gnus-default-article-saver filename)))))
3859
3860 (defun gnus-read-save-file-name (prompt &optional filename
3861                                         function group headers variable
3862                                         dir-var)
3863   (let ((default-name
3864           (funcall function group headers (symbol-value variable)))
3865         result)
3866     (setq result
3867           (expand-file-name
3868            (cond
3869             ((eq filename 'default)
3870              default-name)
3871             ((eq filename t)
3872              default-name)
3873             (filename filename)
3874             (t
3875              (when (symbol-value dir-var)
3876                (setq default-name (expand-file-name
3877                                    (file-name-nondirectory default-name)
3878                                    (symbol-value dir-var))))
3879              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3880                     (prompt
3881                      (format prompt
3882                              (if (and gnus-number-of-articles-to-be-saved
3883                                       (> gnus-number-of-articles-to-be-saved 1))
3884                                  (format "these %d articles"
3885                                          gnus-number-of-articles-to-be-saved)
3886                                "this article")))
3887                     (file
3888                      ;; Let the split methods have their say.
3889                      (cond
3890                       ;; No split name was found.
3891                       ((null split-name)
3892                        (read-file-name
3893                         (concat prompt " (default "
3894                                 (file-name-nondirectory default-name) "): ")
3895                         (file-name-directory default-name)
3896                         default-name))
3897                       ;; A single group name is returned.
3898                       ((stringp split-name)
3899                        (setq default-name
3900                              (funcall function split-name headers
3901                                       (symbol-value variable)))
3902                        (read-file-name
3903                         (concat prompt " (default "
3904                                 (file-name-nondirectory default-name) "): ")
3905                         (file-name-directory default-name)
3906                         default-name))
3907                       ;; A single split name was found
3908                       ((= 1 (length split-name))
3909                        (let* ((name (expand-file-name
3910                                      (car split-name)
3911                                      gnus-article-save-directory))
3912                               (dir (cond ((file-directory-p name)
3913                                           (file-name-as-directory name))
3914                                          ((file-exists-p name) name)
3915                                          (t gnus-article-save-directory))))
3916                          (read-file-name
3917                           (concat prompt " (default " name "): ")
3918                           dir name)))
3919                       ;; A list of splits was found.
3920                       (t
3921                        (setq split-name (nreverse split-name))
3922                        (let (result)
3923                          (let ((file-name-history
3924                                 (nconc split-name file-name-history)))
3925                            (setq result
3926                                  (expand-file-name
3927                                   (read-file-name
3928                                    (concat prompt " (`M-p' for defaults): ")
3929                                    gnus-article-save-directory
3930                                    (car split-name))
3931                                   gnus-article-save-directory)))
3932                          (car (push result file-name-history)))))))
3933                ;; Create the directory.
3934                (gnus-make-directory (file-name-directory file))
3935                ;; If we have read a directory, we append the default file name.
3936                (when (file-directory-p file)
3937                  (setq file (expand-file-name (file-name-nondirectory
3938                                                default-name)
3939                                               (file-name-as-directory file))))
3940                ;; Possibly translate some characters.
3941                (nnheader-translate-file-chars file))))))
3942     (gnus-make-directory (file-name-directory result))
3943     (when variable
3944       (set variable result))
3945     (when dir-var
3946       (set dir-var (file-name-directory result)))
3947     result))
3948
3949 (defun gnus-article-archive-name (group)
3950   "Return the first instance of an \"Archive-name\" in the current buffer."
3951   (let ((case-fold-search t))
3952     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3953       (nnheader-concat gnus-article-save-directory
3954                        (match-string 1)))))
3955
3956 (defun gnus-article-nndoc-name (group)
3957   "If GROUP is an nndoc group, return the name of the parent group."
3958   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3959     (gnus-group-get-parameter group 'save-article-group)))
3960
3961 (defun gnus-summary-save-in-rmail (&optional filename)
3962   "Append this article to Rmail file.
3963 Optional argument FILENAME specifies file name.
3964 Directory to save to is default to `gnus-article-save-directory'."
3965   (setq filename (gnus-read-save-file-name
3966                   "Save %s in rmail file" filename
3967                   gnus-rmail-save-name gnus-newsgroup-name
3968                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3969   (with-current-buffer gnus-save-article-buffer
3970     (save-excursion
3971       (save-restriction
3972         (widen)
3973         ;; Note that unlike gnus-summary-save-in-mail, there is no
3974         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3975         ;; not use Babyl.
3976         (gnus-output-to-rmail filename))))
3977   filename)
3978
3979 (defun gnus-summary-save-in-mail (&optional filename)
3980   "Append this article to Unix mail file.
3981 Optional argument FILENAME specifies file name.
3982 Directory to save to is default to `gnus-article-save-directory'."
3983   (setq filename (gnus-read-save-file-name
3984                   "Save %s in Unix mail file" filename
3985                   gnus-mail-save-name gnus-newsgroup-name
3986                   gnus-current-headers 'gnus-newsgroup-last-mail))
3987   (with-current-buffer gnus-save-article-buffer
3988     (save-excursion
3989       (save-restriction
3990         (widen)
3991         (if (and (file-readable-p filename)
3992                  (file-regular-p filename)
3993                  (mail-file-babyl-p filename))
3994             (gnus-output-to-rmail filename)
3995           (gnus-output-to-mail filename)))))
3996   filename)
3997
3998 (put 'gnus-summary-save-in-file :decode t)
3999 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4000 (defun gnus-summary-save-in-file (&optional filename overwrite)
4001   "Append this article to file.
4002 Optional argument FILENAME specifies file name.
4003 Directory to save to is default to `gnus-article-save-directory'."
4004   (setq filename (gnus-read-save-file-name
4005                   "Save %s in file" filename
4006                   gnus-file-save-name gnus-newsgroup-name
4007                   gnus-current-headers 'gnus-newsgroup-last-file))
4008   (with-current-buffer gnus-save-article-buffer
4009     (save-excursion
4010       (save-restriction
4011         (widen)
4012         (when (and overwrite
4013                    (file-exists-p filename))
4014           (delete-file filename))
4015         (gnus-output-to-file filename))))
4016   filename)
4017
4018 (put 'gnus-summary-write-to-file :decode t)
4019 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4020 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4021 (defun gnus-summary-write-to-file (&optional filename)
4022   "Write this article to a file, overwriting it if the file exists.
4023 Optional argument FILENAME specifies file name.
4024 The directory to save in defaults to `gnus-article-save-directory'."
4025   (setq filename (gnus-read-save-file-name
4026                   "Save %s in file" filename
4027                   gnus-file-save-name gnus-newsgroup-name
4028                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4029   (gnus-summary-save-in-file filename t))
4030
4031 (put 'gnus-summary-save-body-in-file :decode t)
4032 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4033   "Append this article body to a file.
4034 Optional argument FILENAME specifies file name.
4035 The directory to save in defaults to `gnus-article-save-directory'."
4036   (setq filename (gnus-read-save-file-name
4037                   "Save %s body in file" filename
4038                   gnus-file-save-name gnus-newsgroup-name
4039                   gnus-current-headers 'gnus-newsgroup-last-file))
4040   (with-current-buffer gnus-save-article-buffer
4041     (save-excursion
4042       (save-restriction
4043         (widen)
4044         (when (article-goto-body)
4045           (narrow-to-region (point) (point-max)))
4046         (when (and overwrite
4047                    (file-exists-p filename))
4048           (delete-file filename))
4049         (gnus-output-to-file filename))))
4050   filename)
4051
4052 (put 'gnus-summary-write-body-to-file :decode t)
4053 (put 'gnus-summary-write-body-to-file
4054      :function 'gnus-summary-save-body-in-file)
4055 (defun gnus-summary-write-body-to-file (&optional filename)
4056   "Write this article body to a file, overwriting it if the file exists.
4057 Optional argument FILENAME specifies file name.
4058 The directory to save in defaults to `gnus-article-save-directory'."
4059   (setq filename (gnus-read-save-file-name
4060                   "Save %s body in file" filename
4061                   gnus-file-save-name gnus-newsgroup-name
4062                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4063   (gnus-summary-save-body-in-file filename t))
4064
4065 (put 'gnus-summary-save-in-pipe :decode t)
4066 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4067 (defun gnus-summary-save-in-pipe (&optional command raw)
4068   "Pipe this article to subprocess COMMAND.
4069 Valid values for COMMAND include:
4070   a string
4071     The executable command name and possibly arguments.
4072   nil
4073     You will be prompted for the command in the minibuffer.
4074   the symbol `default'
4075     It will be replaced with the command which the variable
4076     `gnus-summary-pipe-output-default-command' holds or the command
4077     last used for saving.
4078 Non-nil value for RAW overrides `:decode' and `:headers' properties
4079 and the raw article including all headers will be piped."
4080   (let ((article (gnus-summary-article-number))
4081         (decode (unless raw
4082                   (get 'gnus-summary-save-in-pipe :decode)))
4083         save-buffer default)
4084     (if article
4085         (if (vectorp (gnus-summary-article-header article))
4086             (save-current-buffer
4087               (gnus-summary-select-article decode decode nil article)
4088               (insert-buffer-substring
4089                (prog1
4090                    (if decode
4091                        gnus-article-buffer
4092                      gnus-original-article-buffer)
4093                  (setq save-buffer
4094                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4095               ;; Remove unwanted headers.
4096               (when (and (not raw)
4097                          (or (get 'gnus-summary-save-in-pipe :headers)
4098                              (not gnus-save-all-headers)))
4099                 (let ((gnus-visible-headers
4100                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4101                                               :headers))
4102                            gnus-saved-headers gnus-visible-headers))
4103                       (gnus-summary-buffer nil))
4104                   (article-hide-headers 1 t))))
4105           (error "%d is not a real article" article))
4106       (error "No article to pipe"))
4107     (setq default (or gnus-summary-pipe-output-default-command
4108                       gnus-last-shell-command))
4109     (unless (stringp command)
4110       (setq command
4111             (if (and (eq command 'default) default)
4112                 default
4113               (gnus-read-shell-command "Shell command on this article: "
4114                                        default))))
4115     (when (string-equal command "")
4116       (if default
4117           (setq command default)
4118         (error "A command is required")))
4119     (with-current-buffer save-buffer
4120       (save-restriction
4121         (widen)
4122         (shell-command-on-region (point-min) (point-max) command nil)))
4123     (gnus-kill-buffer save-buffer))
4124   (setq gnus-summary-pipe-output-default-command command))
4125
4126 (defun gnus-summary-pipe-to-muttprint (&optional command)
4127   "Pipe this article to muttprint."
4128   (unless (stringp command)
4129     (setq command (read-string
4130                    "Print using command: " gnus-summary-muttprint-program
4131                    nil gnus-summary-muttprint-program)))
4132   (let ((gnus-summary-pipe-output-default-command
4133          gnus-summary-pipe-output-default-command))
4134     (gnus-summary-save-in-pipe command))
4135   (setq gnus-summary-muttprint-program command))
4136
4137 ;;; Article file names when saving.
4138
4139 (defun gnus-capitalize-newsgroup (newsgroup)
4140   "Capitalize NEWSGROUP name."
4141   (when (not (zerop (length newsgroup)))
4142     (concat (char-to-string (upcase (aref newsgroup 0)))
4143             (substring newsgroup 1))))
4144
4145 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4146   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4147 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4148 Otherwise, it is like ~/News/news/group/num."
4149   (let ((default
4150           (expand-file-name
4151            (concat (if (gnus-use-long-file-name 'not-save)
4152                        (gnus-capitalize-newsgroup newsgroup)
4153                      (gnus-newsgroup-directory-form newsgroup))
4154                    "/" (int-to-string (mail-header-number headers)))
4155            gnus-article-save-directory)))
4156     (if (and last-file
4157              (string-equal (file-name-directory default)
4158                            (file-name-directory last-file))
4159              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4160         default
4161       (or last-file default))))
4162
4163 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4164   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4165 If variable `gnus-use-long-file-name' is non-nil, it is
4166 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4167   (let ((default
4168           (expand-file-name
4169            (concat (if (gnus-use-long-file-name 'not-save)
4170                        newsgroup
4171                      (gnus-newsgroup-directory-form newsgroup))
4172                    "/" (int-to-string (mail-header-number headers)))
4173            gnus-article-save-directory)))
4174     (if (and last-file
4175              (string-equal (file-name-directory default)
4176                            (file-name-directory last-file))
4177              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4178         default
4179       (or last-file default))))
4180
4181 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4182   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4183 If variable `gnus-use-long-file-name' is non-nil, it is
4184 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4185   (or last-file
4186       (expand-file-name
4187        (if (gnus-use-long-file-name 'not-save)
4188            newsgroup
4189          (file-relative-name
4190           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4191           default-directory))
4192        gnus-article-save-directory)))
4193
4194 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4195   "Generate file name from sender."
4196   (let ((from (mail-header-from headers)))
4197     (expand-file-name
4198      (if (and from (string-match "\\([^ <]+\\)@" from))
4199          (match-string 1 from)
4200        "nobody")
4201      gnus-article-save-directory)))
4202
4203 (defun article-verify-x-pgp-sig ()
4204   "Verify X-PGP-Sig."
4205   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4206   (interactive)
4207   (if (gnus-buffer-live-p gnus-original-article-buffer)
4208       (let ((sig (with-current-buffer gnus-original-article-buffer
4209                    (gnus-fetch-field "X-PGP-Sig")))
4210             items info headers)
4211         (when (and sig
4212                    mml2015-use
4213                    (mml2015-clear-verify-function))
4214           (with-temp-buffer
4215             (insert-buffer-substring gnus-original-article-buffer)
4216             (setq items (split-string sig))
4217             (message-narrow-to-head)
4218             (let ((inhibit-point-motion-hooks t)
4219                   (case-fold-search t))
4220               ;; Don't verify multiple headers.
4221               (setq headers (mapconcat (lambda (header)
4222                                          (concat header ": "
4223                                                  (mail-fetch-field header)
4224                                                  "\n"))
4225                                        (split-string (nth 1 items) ",") "")))
4226             (delete-region (point-min) (point-max))
4227             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4228             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4229             (insert headers)
4230             (widen)
4231             (forward-line)
4232             (while (not (eobp))
4233               (if (looking-at "^-")
4234                   (insert "- "))
4235               (forward-line))
4236             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4237             (insert "Version: " (car items) "\n\n")
4238             (insert (mapconcat 'identity (cddr items) "\n"))
4239             (insert "\n-----END PGP SIGNATURE-----\n")
4240             (let ((mm-security-handle (list (format "multipart/signed"))))
4241               (mml2015-clean-buffer)
4242               (let ((coding-system-for-write (or gnus-newsgroup-charset
4243                                                  'iso-8859-1)))
4244                 (funcall (mml2015-clear-verify-function)))
4245               (setq info
4246                     (or (mm-handle-multipart-ctl-parameter
4247                          mm-security-handle 'gnus-details)
4248                         (mm-handle-multipart-ctl-parameter
4249                          mm-security-handle 'gnus-info)))))
4250           (when info
4251             (let ((inhibit-read-only t) bface eface)
4252               (save-restriction
4253                 (message-narrow-to-head)
4254                 (goto-char (point-max))
4255                 (forward-line -1)
4256                 (setq bface (get-text-property (point-at-bol) 'face)
4257                       eface (get-text-property (1- (point-at-eol)) 'face))
4258                 (message-remove-header "X-Gnus-PGP-Verify")
4259                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4260                     (forward-line)
4261                   (goto-char (point-max)))
4262                 (narrow-to-region (point) (point))
4263                 (insert "X-Gnus-PGP-Verify: " info "\n")
4264                 (goto-char (point-min))
4265                 (forward-line)
4266                 (while (not (eobp))
4267                   (if (not (looking-at "^[ \t]"))
4268                       (insert " "))
4269                   (forward-line))
4270                 ;; Do highlighting.
4271                 (goto-char (point-min))
4272                 (when (looking-at "\\([^:]+\\): *")
4273                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4274                                      'face bface)
4275                   (put-text-property (match-end 0) (point-max)
4276                                      'face eface)))))))))
4277
4278 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4279
4280 (defun article-verify-cancel-lock ()
4281   "Verify Cancel-Lock header."
4282   (interactive)
4283   (if (gnus-buffer-live-p gnus-original-article-buffer)
4284       (canlock-verify gnus-original-article-buffer)))
4285
4286 (eval-and-compile
4287   (mapc
4288    (lambda (func)
4289      (let (afunc gfunc)
4290        (if (consp func)
4291            (setq afunc (car func)
4292                  gfunc (cdr func))
4293          (setq afunc func
4294                gfunc (intern (format "gnus-%s" func))))
4295        (defalias gfunc
4296          (when (fboundp afunc)
4297            `(lambda (&optional interactive &rest args)
4298               ,(documentation afunc t)
4299               (interactive (list t))
4300               (with-current-buffer gnus-article-buffer
4301                 (if interactive
4302                     (call-interactively ',afunc)
4303                   (apply ',afunc args))))))))
4304    '(article-hide-headers
4305      article-verify-x-pgp-sig
4306      article-verify-cancel-lock
4307      article-hide-boring-headers
4308      article-treat-overstrike
4309      article-treat-ansi-sequences
4310      article-fill-long-lines
4311      article-capitalize-sentences
4312      article-remove-cr
4313      article-remove-leading-whitespace
4314      article-display-x-face
4315      article-display-face
4316      article-de-quoted-unreadable
4317      article-de-base64-unreadable
4318      article-decode-HZ
4319      article-wash-html
4320      article-unsplit-urls
4321      article-hide-list-identifiers
4322      article-strip-banner
4323      article-babel
4324      article-hide-pem
4325      article-hide-signature
4326      article-strip-headers-in-body
4327      article-remove-trailing-blank-lines
4328      article-strip-leading-blank-lines
4329      article-strip-multiple-blank-lines
4330      article-strip-leading-space
4331      article-strip-trailing-space
4332      article-strip-blank-lines
4333      article-strip-all-blank-lines
4334      article-date-local
4335      article-date-english
4336      article-date-iso8601
4337      article-date-original
4338      article-treat-date
4339      article-date-ut
4340      article-decode-mime-words
4341      article-decode-charset
4342      article-decode-encoded-words
4343      article-date-user
4344      article-date-lapsed
4345      article-date-combined-lapsed
4346      article-emphasize
4347      article-treat-dumbquotes
4348      article-treat-non-ascii
4349      article-normalize-headers
4350      ;;(article-show-all . gnus-article-show-all-headers)
4351      )))
4352 \f
4353 ;;;
4354 ;;; Gnus article mode
4355 ;;;
4356
4357 (put 'gnus-article-mode 'mode-class 'special)
4358
4359 (set-keymap-parent gnus-article-mode-map widget-keymap)
4360
4361 (gnus-define-keys gnus-article-mode-map
4362   " " gnus-article-goto-next-page
4363   "\177" gnus-article-goto-prev-page
4364   [delete] gnus-article-goto-prev-page
4365   [backspace] gnus-article-goto-prev-page
4366   "\C-c^" gnus-article-refer-article
4367   "h" gnus-article-show-summary
4368   "s" gnus-article-show-summary
4369   "\C-c\C-m" gnus-article-mail
4370   "?" gnus-article-describe-briefly
4371   "<" beginning-of-buffer
4372   ">" end-of-buffer
4373   "\C-c\C-i" gnus-info-find-node
4374   "\C-c\C-b" gnus-bug
4375   "R" gnus-article-reply-with-original
4376   "F" gnus-article-followup-with-original
4377   "\C-hk" gnus-article-describe-key
4378   "\C-hc" gnus-article-describe-key-briefly
4379   "\C-hb" gnus-article-describe-bindings
4380
4381   "e" gnus-article-read-summary-keys
4382   "\C-d" gnus-article-read-summary-keys
4383   "\M-*" gnus-article-read-summary-keys
4384   "\M-#" gnus-article-read-summary-keys
4385   "\M-^" gnus-article-read-summary-keys
4386   "\M-g" gnus-article-read-summary-keys)
4387
4388 (substitute-key-definition
4389  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4390
4391 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4392   "W" gnus-article-wide-reply-with-original)
4393 (if (featurep 'xemacs)
4394     (set-keymap-default-binding gnus-article-send-map
4395                                 'gnus-article-read-summary-send-keys)
4396   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4397
4398 (defun gnus-article-make-menu-bar ()
4399   (unless (boundp 'gnus-article-commands-menu)
4400     (gnus-summary-make-menu-bar))
4401   (unless (boundp 'gnus-article-article-menu)
4402     (easy-menu-define
4403      gnus-article-article-menu gnus-article-mode-map ""
4404      '("Article"
4405        ["Scroll forwards" gnus-article-goto-next-page t]
4406        ["Scroll backwards" gnus-article-goto-prev-page t]
4407        ["Show summary" gnus-article-show-summary t]
4408        ["Fetch Message-ID at point" gnus-article-refer-article t]
4409        ["Mail to address at point" gnus-article-mail t]
4410        ["Send a bug report" gnus-bug t]))
4411
4412     (easy-menu-define
4413      gnus-article-treatment-menu gnus-article-mode-map ""
4414      ;; Fixme: this should use :active (and maybe :visible).
4415      '("Treatment"
4416        ["Hide headers" gnus-article-hide-headers t]
4417        ["Hide signature" gnus-article-hide-signature t]
4418        ["Hide citation" gnus-article-hide-citation t]
4419        ["Treat overstrike" gnus-article-treat-overstrike t]
4420        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4421        ["Remove carriage return" gnus-article-remove-cr t]
4422        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4423        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4424        ["Remove base64" gnus-article-de-base64-unreadable t]
4425        ["Treat html" gnus-article-wash-html t]
4426        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4427        ["Decode HZ" gnus-article-decode-HZ t]))
4428
4429     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4430
4431     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4432
4433     (gnus-run-hooks 'gnus-article-menu-hook)))
4434
4435 (defvar bookmark-make-record-function)
4436 (defvar shr-put-image-function)
4437
4438 (defun gnus-article-mode ()
4439   "Major mode for displaying an article.
4440
4441 All normal editing commands are switched off.
4442
4443 The following commands are available in addition to all summary mode
4444 commands:
4445 \\<gnus-article-mode-map>
4446 \\[gnus-article-next-page]\t Scroll the article one page forwards
4447 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4448 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4449 \\[gnus-article-show-summary]\t Display the summary buffer
4450 \\[gnus-article-mail]\t Send a reply to the address near point
4451 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4452 \\[gnus-info-find-node]\t Go to the Gnus info node"
4453   (interactive)
4454   (kill-all-local-variables)
4455   (gnus-simplify-mode-line)
4456   (setq mode-name "Article")
4457   (setq major-mode 'gnus-article-mode)
4458   (make-local-variable 'minor-mode-alist)
4459   (use-local-map gnus-article-mode-map)
4460   (when (gnus-visual-p 'article-menu 'menu)
4461     (gnus-article-make-menu-bar)
4462     (when gnus-summary-tool-bar-map
4463       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4464   (gnus-update-format-specifications nil 'article-mode)
4465   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4466   (set (make-local-variable 'gnus-page-broken) nil)
4467   (make-local-variable 'gnus-article-current-summary)
4468   (make-local-variable 'gnus-article-mime-handles)
4469   (make-local-variable 'gnus-article-decoded-p)
4470   (make-local-variable 'gnus-article-mime-handle-alist)
4471   (make-local-variable 'gnus-article-wash-types)
4472   (make-local-variable 'gnus-article-image-alist)
4473   (make-local-variable 'gnus-article-charset)
4474   (make-local-variable 'gnus-article-ignored-charsets)
4475   (set (make-local-variable 'bookmark-make-record-function)
4476        'gnus-summary-bookmark-make-record)
4477   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4478   ;; face.
4479   (set (make-local-variable 'nobreak-char-display) nil)
4480   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4481   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4482   (setq cursor-in-non-selected-windows nil)
4483   (gnus-set-default-directory)
4484   (buffer-disable-undo)
4485   (setq buffer-read-only t
4486         show-trailing-whitespace nil)
4487   (set-syntax-table gnus-article-mode-syntax-table)
4488   (mm-enable-multibyte)
4489   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4490
4491 (defun gnus-article-setup-buffer ()
4492   "Initialize the article buffer."
4493   (let* ((name (if gnus-single-article-buffer "*Article*"
4494                  (concat "*Article "
4495                          (gnus-group-decoded-name gnus-newsgroup-name)
4496                          "*")))
4497          (original
4498           (progn (string-match "\\*Article" name)
4499                  (concat " *Original Article"
4500                          (substring name (match-end 0))))))
4501     (setq gnus-article-buffer name)
4502     (setq gnus-original-article-buffer original)
4503     (setq gnus-article-mime-handle-alist nil)
4504     (with-current-buffer gnus-summary-buffer
4505       ;; This might be a variable local to the summary buffer.
4506       (unless gnus-single-article-buffer
4507         (setq gnus-article-buffer name)
4508         (setq gnus-original-article-buffer original)
4509         (gnus-set-global-variables)))
4510     (gnus-article-setup-highlight-words)
4511     ;; Init original article buffer.
4512     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4513       (mm-enable-multibyte)
4514       (setq major-mode 'gnus-original-article-mode)
4515       (make-local-variable 'gnus-original-article))
4516     (if (and (get-buffer name)
4517              (with-current-buffer name
4518                (if gnus-article-edit-mode
4519                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4520                        (progn
4521                          (set-buffer-modified-p nil)
4522                          (gnus-kill-buffer name)
4523                          (message "")
4524                          nil)
4525                      (error "Action aborted"))
4526                  t)))
4527         (with-current-buffer name
4528           (set (make-local-variable 'gnus-article-edit-mode) nil)
4529           (gnus-article-stop-animations)
4530           (when gnus-article-mime-handles
4531             (mm-destroy-parts gnus-article-mime-handles)
4532             (setq gnus-article-mime-handles nil))
4533           ;; Set it to nil in article-buffer!
4534           (setq gnus-article-mime-handle-alist nil)
4535           (buffer-disable-undo)
4536           (setq buffer-read-only t)
4537           (unless (eq major-mode 'gnus-article-mode)
4538             (gnus-article-mode))
4539           (setq truncate-lines gnus-article-truncate-lines)
4540           (current-buffer))
4541       (with-current-buffer (gnus-get-buffer-create name)
4542         (gnus-article-mode)
4543         (setq truncate-lines gnus-article-truncate-lines)
4544         (make-local-variable 'gnus-summary-buffer)
4545         (setq gnus-summary-buffer
4546               (gnus-summary-buffer-name gnus-newsgroup-name))
4547         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4548         (when article-lapsed-timer
4549           (gnus-stop-date-timer))
4550         (when gnus-article-update-date-headers
4551           (gnus-start-date-timer gnus-article-update-date-headers))
4552         (current-buffer)))))
4553
4554 (defun gnus-article-stop-animations ()
4555   (dolist (timer (and (boundp 'timer-list)
4556                       timer-list))
4557     (when (eq (gnus-timer--function timer) 'image-animate-timeout)
4558       (cancel-timer timer))))
4559
4560 (defun gnus-stop-downloads ()
4561   (when (boundp 'url-queue)
4562     (set (intern "url-queue" obarray) nil)))
4563
4564 ;; Set article window start at LINE, where LINE is the number of lines
4565 ;; from the head of the article.
4566 (defun gnus-article-set-window-start (&optional line)
4567   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4568     (when article-window
4569       (set-window-start
4570        article-window
4571        (with-current-buffer gnus-article-buffer
4572          (goto-char (point-min))
4573          (if (not line)
4574              (point-min)
4575            (gnus-message 6 "Moved to bookmark")
4576            (search-forward "\n\n" nil t)
4577            (forward-line line)
4578            (point)))))))
4579
4580 (defun gnus-article-prepare (article &optional all-headers header)
4581   "Prepare ARTICLE in article mode buffer.
4582 ARTICLE should either be an article number or a Message-ID.
4583 If ARTICLE is an id, HEADER should be the article headers.
4584 If ALL-HEADERS is non-nil, no headers are hidden."
4585   (save-excursion
4586     ;; Make sure we start in a summary buffer.
4587     (unless (eq major-mode 'gnus-summary-mode)
4588       (set-buffer gnus-summary-buffer))
4589     (setq gnus-summary-buffer (current-buffer))
4590     (let* ((gnus-article (if header (mail-header-number header) article))
4591            (summary-buffer (current-buffer))
4592            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4593            (group gnus-newsgroup-name)
4594            result)
4595       (save-excursion
4596         (gnus-article-setup-buffer)
4597         (set-buffer gnus-article-buffer)
4598         ;; Deactivate active regions.
4599         (when (and (boundp 'transient-mark-mode)
4600                    transient-mark-mode)
4601           (setq mark-active nil))
4602         (if (not (setq result (let ((inhibit-read-only t))
4603                                 (gnus-request-article-this-buffer
4604                                  article group))))
4605             ;; There is no such article.
4606             (save-excursion
4607               (when (and (numberp article)
4608                          (not (memq article gnus-newsgroup-sparse)))
4609                 (setq gnus-article-current
4610                       (cons gnus-newsgroup-name article))
4611                 (set-buffer gnus-summary-buffer)
4612                 (setq gnus-current-article article)
4613                 (if (and (memq article gnus-newsgroup-undownloaded)
4614                          (not (gnus-online (gnus-find-method-for-group
4615                                             gnus-newsgroup-name))))
4616                     (progn
4617                       (gnus-summary-set-agent-mark article)
4618                       (message "Message marked for downloading"))
4619                   (gnus-summary-mark-article article gnus-canceled-mark)
4620                   (unless (memq article gnus-newsgroup-sparse)
4621                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4622           (if (or (eq result 'pseudo)
4623                   (eq result 'nneething))
4624               (progn
4625                 (with-current-buffer summary-buffer
4626                   (push article gnus-newsgroup-history)
4627                   (setq gnus-last-article gnus-current-article
4628                         gnus-current-article 0
4629                         gnus-current-headers nil
4630                         gnus-article-current nil)
4631                   (if (eq result 'nneething)
4632                       (gnus-configure-windows 'summary)
4633                     (gnus-configure-windows 'article))
4634                   (gnus-set-global-variables))
4635                 (let ((gnus-article-mime-handle-alist-1
4636                        gnus-article-mime-handle-alist))
4637                   (gnus-set-mode-line 'article)))
4638             ;; The result from the `request' was an actual article -
4639             ;; or at least some text that is now displayed in the
4640             ;; article buffer.
4641             (when (and (numberp article)
4642                        (not (eq article gnus-current-article)))
4643               ;; Seems like a new article has been selected.
4644               ;; `gnus-current-article' must be an article number.
4645               (with-current-buffer summary-buffer
4646                 (push article gnus-newsgroup-history)
4647                 (setq gnus-last-article gnus-current-article
4648                       gnus-current-article article
4649                       gnus-current-headers
4650                       (gnus-summary-article-header gnus-current-article)
4651                       gnus-article-current
4652                       (cons gnus-newsgroup-name gnus-current-article))
4653                 (unless (vectorp gnus-current-headers)
4654                   (setq gnus-current-headers nil))
4655                 (gnus-summary-goto-subject gnus-current-article)
4656                 (when (gnus-summary-show-thread)
4657                   ;; If the summary buffer really was folded, the
4658                   ;; previous goto may not actually have gone to
4659                   ;; the right article, but the thread root instead.
4660                   ;; So we go again.
4661                   (gnus-summary-goto-subject gnus-current-article))
4662                 (gnus-run-hooks 'gnus-mark-article-hook)
4663                 (gnus-set-mode-line 'summary)
4664                 (when (gnus-visual-p 'article-highlight 'highlight)
4665                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4666                 ;; Set the global newsgroup variables here.
4667                 (gnus-set-global-variables)
4668                 (setq gnus-have-all-headers
4669                       (or all-headers gnus-show-all-headers))))
4670             (save-excursion
4671               (gnus-configure-windows 'article))
4672             (when (or (numberp article)
4673                       (stringp article))
4674               (gnus-article-prepare-display)
4675               ;; Do page break.
4676               (goto-char (point-min))
4677               (when gnus-break-pages
4678                 (gnus-narrow-to-page)))
4679             (let ((gnus-article-mime-handle-alist-1
4680                    gnus-article-mime-handle-alist))
4681               (gnus-set-mode-line 'article))
4682             (article-goto-body)
4683             (unless (bobp)
4684               (forward-line -1))
4685             (set-window-point (get-buffer-window (current-buffer)) (point))
4686             (gnus-configure-windows 'article)
4687             (gnus-run-hooks 'gnus-article-prepare-hook)
4688             t))))))
4689
4690 ;;;###autoload
4691 (defun gnus-article-prepare-display ()
4692   "Make the current buffer look like a nice article."
4693   ;; Hooks for getting information from the article.
4694   ;; This hook must be called before being narrowed.
4695   (let ((gnus-article-buffer (current-buffer))
4696         buffer-read-only
4697         (inhibit-read-only t))
4698     (unless (eq major-mode 'gnus-article-mode)
4699       (gnus-article-mode))
4700     (setq buffer-read-only nil
4701           gnus-article-wash-types nil
4702           gnus-article-image-alist nil)
4703     (gnus-run-hooks 'gnus-tmp-internal-hook)
4704     (when gnus-display-mime-function
4705       (funcall gnus-display-mime-function))))
4706
4707 ;;;
4708 ;;; Gnus Sticky Article Mode
4709 ;;;
4710
4711 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4712   "Mode for sticky articles."
4713   ;; Release bindings that won't work.
4714   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4715                              gnus-sticky-article-mode-map)
4716   (substitute-key-definition 'gnus-article-refer-article 'undefined
4717                              gnus-sticky-article-mode-map)
4718   (dolist (k '("e" "h" "s" "F" "R"))
4719     (define-key gnus-sticky-article-mode-map k nil))
4720   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4721   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4722   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4723   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4724
4725 (defun gnus-sticky-article (arg)
4726   "Make the current article sticky.
4727 If a prefix ARG is given, ask for a name for this sticky article buffer."
4728   (interactive "P")
4729   (gnus-summary-show-thread)
4730   (gnus-summary-select-article nil nil 'pseudo)
4731   (let (new-art-buf-name)
4732     (gnus-eval-in-buffer-window gnus-article-buffer
4733       (setq new-art-buf-name
4734             (concat
4735              "*Sticky Article: "
4736              (if arg
4737                  (read-from-minibuffer "Sticky article buffer name: ")
4738                (gnus-with-article-headers
4739                  (gnus-article-goto-header "subject")
4740                  (setq new-art-buf-name
4741                        (buffer-substring-no-properties
4742                         (line-beginning-position) (line-end-position)))
4743                  (goto-char (point-min))
4744                  (gnus-article-goto-header "from")
4745                  (setq new-art-buf-name
4746                        (concat
4747                         new-art-buf-name ", "
4748                         (buffer-substring-no-properties
4749                          (line-beginning-position) (line-end-position))))
4750                  (goto-char (point-min))
4751                  (gnus-article-goto-header "date")
4752                  (setq new-art-buf-name
4753                        (concat
4754                         new-art-buf-name ", "
4755                         (buffer-substring-no-properties
4756                          (line-beginning-position) (line-end-position))))))
4757              "*"))
4758       (if (and (gnus-buffer-live-p new-art-buf-name)
4759                (with-current-buffer new-art-buf-name
4760                  (eq major-mode 'gnus-sticky-article-mode)))
4761           (switch-to-buffer new-art-buf-name)
4762         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4763       (gnus-sticky-article-mode))
4764     (setq gnus-article-buffer new-art-buf-name))
4765   (gnus-summary-recenter)
4766   (gnus-summary-position-point))
4767
4768 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4769   "Kill the given sticky article BUFFER.
4770 If none is given, assume the current buffer and kill it if it has
4771 `gnus-sticky-article-mode'."
4772   (interactive)
4773   (unless buffer
4774     (setq buffer (current-buffer)))
4775   (with-current-buffer buffer
4776     (when (eq major-mode 'gnus-sticky-article-mode)
4777       (gnus-kill-buffer buffer))))
4778
4779 (defun gnus-kill-sticky-article-buffers (arg)
4780   "Kill all sticky article buffers.
4781 If a prefix ARG is given, ask for confirmation."
4782   (interactive "P")
4783   (dolist (buf (gnus-buffers))
4784     (with-current-buffer buf
4785       (when (eq major-mode 'gnus-sticky-article-mode)
4786        (if (not arg)
4787            (gnus-kill-buffer buf)
4788          (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4789            (gnus-kill-buffer buf)))))))
4790
4791 ;;;
4792 ;;; Gnus MIME viewing functions
4793 ;;;
4794
4795 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4796   "Format of the MIME buttons.
4797
4798 Valid specifiers include:
4799 %t  The MIME type
4800 %T  MIME type, along with additional info
4801 %n  The `name' parameter
4802 %d  The description, if any
4803 %l  The length of the encoded part
4804 %p  The part identifier number
4805 %e  Dots if the part isn't displayed
4806
4807 General format specifiers can also be used.  See Info node
4808 `(gnus)Formatting Variables'.")
4809
4810 (defvar gnus-mime-button-line-format-alist
4811   '((?t gnus-tmp-type ?s)
4812     (?T gnus-tmp-type-long ?s)
4813     (?n gnus-tmp-name ?s)
4814     (?d gnus-tmp-description ?s)
4815     (?p gnus-tmp-id ?s)
4816     (?l gnus-tmp-length ?d)
4817     (?e gnus-tmp-dots ?s)))
4818
4819 (defvar gnus-mime-button-commands
4820   '((gnus-article-press-button "\r" "Toggle Display")
4821     (gnus-mime-view-part "v" "View Interactively...")
4822     (gnus-mime-view-part-as-type "t" "View As Type...")
4823     (gnus-mime-view-part-as-charset "C" "View As charset...")
4824     (gnus-mime-save-part "o" "Save...")
4825     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4826     (gnus-mime-replace-part "r" "Replace part")
4827     (gnus-mime-delete-part "d" "Delete part")
4828     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4829     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4830     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4831     (gnus-mime-view-part-externally "e" "View Externally")
4832     (gnus-mime-print-part "p" "Print")
4833     (gnus-mime-pipe-part "|" "Pipe To Command...")
4834     (gnus-mime-action-on-part "." "Take action on the part...")))
4835
4836 (defun gnus-article-mime-part-status ()
4837   (if gnus-article-mime-handle-alist-1
4838       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4839           " (1 part)"
4840         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4841     ""))
4842
4843 (defvar gnus-mime-button-map
4844   (let ((map (make-sparse-keymap)))
4845     (define-key map gnus-mouse-2 'gnus-article-push-button)
4846     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4847     (dolist (c gnus-mime-button-commands)
4848       (define-key map (cadr c) (car c)))
4849     map))
4850
4851 (easy-menu-define
4852   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4853   `("MIME Part"
4854     ,@(mapcar (lambda (c)
4855                 (vector (caddr c) (car c) :active t))
4856               gnus-mime-button-commands)))
4857
4858 (defvar gnus-url-button-commands
4859   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4860
4861 (defvar gnus-url-button-map
4862   (let ((map (make-sparse-keymap)))
4863     (dolist (c gnus-url-button-commands)
4864       (define-key map (cadr c) (car c)))
4865     map))
4866
4867 (easy-menu-define
4868   gnus-url-button-menu gnus-url-button-map "URL button menu."
4869   `("Url Button"
4870     ,@(mapcar (lambda (c)
4871                 (vector (caddr c) (car c) :active t))
4872               gnus-url-button-commands)))
4873
4874 (defmacro gnus-bind-safe-url-regexp (&rest body)
4875   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4876   `(let ((mm-w3m-safe-url-regexp
4877           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4878                                 (gnus-buffer-live-p
4879                                  gnus-article-current-summary))
4880                            (with-current-buffer gnus-article-current-summary
4881                              gnus-newsgroup-name)
4882                          gnus-newsgroup-name)))
4883             (if (cond ((not group)
4884                        ;; Maybe we're in a mml-preview buffer
4885                        ;; and no group is selected.
4886                        t)
4887                       ((stringp gnus-safe-html-newsgroups)
4888                        (string-match gnus-safe-html-newsgroups group))
4889                       ((consp gnus-safe-html-newsgroups)
4890                        (member group gnus-safe-html-newsgroups)))
4891                 nil
4892               mm-w3m-safe-url-regexp))))
4893      ,@body))
4894
4895 (defun gnus-mime-button-menu (event prefix)
4896  "Construct a context-sensitive menu of MIME commands."
4897  (interactive "e\nP")
4898  (save-window-excursion
4899    (let ((pos (event-start event)))
4900      (select-window (posn-window pos))
4901      (goto-char (posn-point pos))
4902      (gnus-article-check-buffer)
4903      (popup-menu gnus-mime-button-menu nil prefix))))
4904
4905 (defun gnus-mime-view-all-parts (&optional handles)
4906   "View all the MIME parts."
4907   (interactive)
4908   (with-current-buffer gnus-article-buffer
4909     (let ((handles (or handles gnus-article-mime-handles))
4910           (mail-parse-charset gnus-newsgroup-charset)
4911           (mail-parse-ignored-charsets
4912            (with-current-buffer gnus-summary-buffer
4913              gnus-newsgroup-ignored-charsets)))
4914       (when handles
4915         (mm-remove-parts handles)
4916         (goto-char (point-min))
4917         (or (search-forward "\n\n") (goto-char (point-max)))
4918         (let ((inhibit-read-only t))
4919           (delete-region (point) (point-max))
4920           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4921
4922 (defun gnus-article-jump-to-part (n)
4923   "Jump to MIME part N."
4924   (interactive "P")
4925   (let ((parts (with-current-buffer gnus-article-buffer
4926                  (length gnus-article-mime-handle-alist))))
4927     (when (zerop parts)
4928       (error "No such part"))
4929     (pop-to-buffer gnus-article-buffer)
4930     (or n
4931         (setq n (if (= parts 1)
4932                     1
4933                   (read-number (format "Jump to part (1..%s): " parts)))))
4934     (unless (and (integerp n) (<= n parts) (>= n 1))
4935       (setq n
4936             (progn
4937               (gnus-message 7 "Invalid part `%s', using %s instead."
4938                             n parts)
4939               parts)))
4940     (gnus-message 9 "Jumping to part %s." n)
4941     (cond ((>= gnus-auto-select-part 1)
4942            (while (and (<= n parts)
4943                        (not (gnus-article-goto-part n)))
4944              (setq n (1+ n))))
4945           ((< gnus-auto-select-part 0)
4946            (while (and (>= n 1)
4947                        (not (gnus-article-goto-part n)))
4948              (setq n (1- n))))
4949           (t
4950            (gnus-article-goto-part n)))))
4951
4952 (defvar gnus-mime-buttonized-part-id nil
4953   "ID of a mime part that should be buttonized.
4954 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4955
4956 (eval-when-compile
4957   (defsubst gnus-article-edit-part (handles &optional current-id)
4958     "Edit an article in order to delete a mime part.
4959 This function is exclusively used by `gnus-mime-save-part-and-strip'
4960 and `gnus-mime-delete-part', and not provided at run-time normally."
4961     (gnus-article-edit-article
4962      `(lambda ()
4963         (buffer-disable-undo)
4964         (erase-buffer)
4965         (let ((mail-parse-charset (or gnus-article-charset
4966                                       ',gnus-newsgroup-charset))
4967               (mail-parse-ignored-charsets
4968                (or gnus-article-ignored-charsets
4969                    ',gnus-newsgroup-ignored-charsets))
4970               (mbl mml-buffer-list))
4971           (setq mml-buffer-list nil)
4972           (insert-buffer-substring gnus-original-article-buffer)
4973           (mime-to-mml ',handles)
4974           (setq gnus-article-mime-handles nil)
4975           (let ((mbl1 mml-buffer-list))
4976             (setq mml-buffer-list mbl)
4977             (set (make-local-variable 'mml-buffer-list) mbl1))
4978           (gnus-make-local-hook 'kill-buffer-hook)
4979           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4980      `(lambda (no-highlight)
4981         (let ((mail-parse-charset (or gnus-article-charset
4982                                       ',gnus-newsgroup-charset))
4983               (message-options message-options)
4984               (message-options-set-recipient)
4985               (mail-parse-ignored-charsets
4986                (or gnus-article-ignored-charsets
4987                    ',gnus-newsgroup-ignored-charsets)))
4988           (mml-to-mime)
4989           (mml-destroy-buffers)
4990           (remove-hook 'kill-buffer-hook
4991                        'mml-destroy-buffers t)
4992           (kill-local-variable 'mml-buffer-list))
4993         (gnus-summary-edit-article-done
4994          ,(or (mail-header-references gnus-current-headers) "")
4995          ,(gnus-group-read-only-p)
4996          ,gnus-summary-buffer no-highlight))
4997      t)
4998     ;; Force buttonizing this part.
4999     (let ((gnus-mime-buttonized-part-id current-id))
5000       (gnus-article-edit-done))
5001     (gnus-configure-windows 'article)
5002     (when (and current-id (integerp gnus-auto-select-part))
5003       (gnus-article-jump-to-part
5004        (min (max (+ current-id gnus-auto-select-part) 1)
5005             (with-current-buffer gnus-article-buffer
5006               (length gnus-article-mime-handle-alist)))))))
5007
5008 (defun gnus-mime-replace-part (file)
5009   "Replace MIME part under point with an external body."
5010   ;; Useful if file has already been saved to disk
5011   (interactive
5012    (list
5013     (read-file-name "Replace MIME part with file: "
5014                     (or mm-default-directory default-directory)
5015                     nil nil)))
5016   (gnus-mime-save-part-and-strip file))
5017
5018 (defun gnus-mime-save-part-and-strip (&optional file)
5019   "Save the MIME part under point then replace it with an external body.
5020 If FILE is given, use it for the external part."
5021   (interactive)
5022   (gnus-article-check-buffer)
5023   (when (gnus-group-read-only-p)
5024     (error "The current group does not support deleting of parts"))
5025   (when (mm-complicated-handles gnus-article-mime-handles)
5026     (error "\
5027 The current article has a complicated MIME structure, giving up..."))
5028   (let* ((data (get-text-property (point) 'gnus-data))
5029          (id (get-text-property (point) 'gnus-part))
5030          param
5031          (handles gnus-article-mime-handles))
5032     (unless file
5033       (setq file
5034             (and data (mm-save-part data "Delete MIME part and save to: "))))
5035     (when file
5036       (with-current-buffer (mm-handle-buffer data)
5037         (erase-buffer)
5038         (insert "Content-Type: " (mm-handle-media-type data))
5039         (mml-insert-parameter-string (cdr (mm-handle-type data))
5040                                      '(charset))
5041         ;; Add a filename for the sake of saving the part again.
5042         (mml-insert-parameter
5043          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5044         (insert "\n")
5045         (insert "Content-ID: " (message-make-message-id) "\n")
5046         (insert "Content-Transfer-Encoding: binary\n")
5047         (insert "\n"))
5048       (setcdr data
5049               (cdr (mm-make-handle nil
5050                                    `("message/external-body"
5051                                      (access-type . "LOCAL-FILE")
5052                                      (name . ,file)))))
5053       ;; (set-buffer gnus-summary-buffer)
5054       (gnus-article-edit-part handles id))))
5055
5056 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5057 ;; parts...>') but with stripping would be nice.
5058
5059 (defun gnus-mime-delete-part ()
5060   "Delete the MIME part under point.
5061 Replace it with some information about the removed part."
5062   (interactive)
5063   (gnus-article-check-buffer)
5064   (when (gnus-group-read-only-p)
5065     (error "The current group does not support deleting of parts"))
5066   (when (mm-complicated-handles gnus-article-mime-handles)
5067     (error "\
5068 The current article has a complicated MIME structure, giving up..."))
5069   (when (or gnus-expert-user
5070             (gnus-yes-or-no-p "\
5071 Deleting parts may malfunction or destroy the article; continue? "))
5072     (let* ((data (get-text-property (point) 'gnus-data))
5073            (id (get-text-property (point) 'gnus-part))
5074            (handles gnus-article-mime-handles)
5075            (description
5076             (let ((desc (mm-handle-description data)))
5077               (when desc
5078                 (mail-decode-encoded-word-string desc))))
5079            (filename (or (mm-handle-filename data) "(none)"))
5080            (type (mm-handle-media-type data)))
5081       (unless data
5082         (error "No MIME part under point"))
5083       (with-current-buffer (mm-handle-buffer data)
5084         (let ((bsize (buffer-size)))
5085           (erase-buffer)
5086           (insert
5087            (concat
5088             ",----\n"
5089             "| The following attachment has been deleted:\n"
5090             "|\n"
5091             "| Type:           " type "\n"
5092             "| Filename:       " filename "\n"
5093             "| Size (encoded): " (format "%s byte%s\n"
5094                                          bsize (if (= bsize 1)
5095                                                    ""
5096                                                  "s"))
5097             (when description
5098               (concat    "| Description:    " description "\n"))
5099             "`----\n"))
5100           (setcdr data
5101                   (cdr (mm-make-handle
5102                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5103                         (list "attachment")
5104                         (format "Deleted attachment (%s bytes)" bsize))))))
5105       ;; (set-buffer gnus-summary-buffer)
5106       (gnus-article-edit-part handles id))))
5107
5108 (defun gnus-mime-save-part ()
5109   "Save the MIME part under point."
5110   (interactive)
5111   (gnus-article-check-buffer)
5112   (let ((data (get-text-property (point) 'gnus-data)))
5113     (when data
5114       (mm-save-part data))))
5115
5116 (defun gnus-mime-pipe-part (&optional cmd)
5117   "Pipe the MIME part under point to a process.
5118 Use CMD as the process."
5119   (interactive)
5120   (gnus-article-check-buffer)
5121   (let ((data (get-text-property (point) 'gnus-data)))
5122     (when data
5123       (mm-pipe-part data cmd))))
5124
5125 (defun gnus-mime-view-part ()
5126   "Interactively choose a viewing method for the MIME part under point."
5127   (interactive)
5128   (gnus-article-check-buffer)
5129   (let ((data (get-text-property (point) 'gnus-data)))
5130     (when data
5131       (setq gnus-article-mime-handles
5132             (mm-merge-handles
5133              gnus-article-mime-handles (setq data (copy-sequence data))))
5134       (mm-interactively-view-part data))))
5135
5136 (defun gnus-mime-view-part-as-type-internal ()
5137   (gnus-article-check-buffer)
5138   (let* ((handle (get-text-property (point) 'gnus-data))
5139          (name (or
5140                 ;; Content-Type: foo/bar; name=...
5141                 (mail-content-type-get (mm-handle-type handle) 'name)
5142                 ;; Content-Disposition: attachment; filename=...
5143                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5144          (def-type (and name (mm-default-file-encoding name))))
5145     (or (and def-type (cons def-type 0))
5146         (and handle
5147              (equal (mm-handle-media-supertype handle) "text")
5148              '("text/plain" . 0))
5149         '("application/octet-stream" . 0))))
5150
5151 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5152   "Choose a MIME media type, and view the part as such.
5153 If non-nil, PRED is a predicate to use during completion to limit the
5154 available media-types."
5155   (interactive)
5156   (unless mime-type
5157     (setq mime-type
5158           (let ((default (gnus-mime-view-part-as-type-internal)))
5159             (gnus-completing-read
5160              "View as MIME type"
5161              (if pred
5162                  (gnus-remove-if-not pred (mailcap-mime-types))
5163                (mailcap-mime-types))
5164              nil nil nil
5165              (car default)))))
5166   (gnus-article-check-buffer)
5167   (let ((handle (get-text-property (point) 'gnus-data)))
5168     (when handle
5169       (when (equal (mm-handle-media-type handle) "message/external-body")
5170         (unless (mm-handle-cache handle)
5171           (mm-extern-cache-contents handle))
5172         (setq handle (mm-handle-cache handle)))
5173       (setq handle
5174             (mm-make-handle (mm-handle-buffer handle)
5175                             (cons mime-type (cdr (mm-handle-type handle)))
5176                             (mm-handle-encoding handle)
5177                             (mm-handle-undisplayer handle)
5178                             (mm-handle-disposition handle)
5179                             (mm-handle-description handle)
5180                             nil
5181                             (mm-handle-id handle)))
5182       (setq gnus-article-mime-handles
5183             (mm-merge-handles gnus-article-mime-handles handle))
5184       (when (mm-handle-displayed-p handle)
5185         (mm-remove-part handle))
5186       (gnus-mm-display-part handle))))
5187
5188 (defun gnus-mime-copy-part (&optional handle arg)
5189   "Put the MIME part under point into a new buffer.
5190 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5191 are decompressed."
5192   (interactive (list nil current-prefix-arg))
5193   (gnus-article-check-buffer)
5194   (unless handle
5195     (setq handle (get-text-property (point) 'gnus-data)))
5196   (when handle
5197     (let ((filename (mm-handle-filename handle))
5198           contents dont-decode charset coding-system)
5199       (mm-with-unibyte-buffer
5200         (mm-insert-part handle)
5201         (setq contents (or (condition-case nil
5202                                (mm-decompress-buffer filename nil 'sig)
5203                              (error
5204                               (setq dont-decode t)
5205                               nil))
5206                            (buffer-string))))
5207       (setq filename (cond (filename (file-name-nondirectory filename))
5208                            (dont-decode "*raw data*")
5209                            (t "*decoded*")))
5210       (cond
5211        (dont-decode)
5212        ((not arg)
5213         (unless (setq charset (mail-content-type-get
5214                                (mm-handle-type handle) 'charset))
5215           (unless (setq coding-system (mm-with-unibyte-buffer
5216                                         (insert contents)
5217                                         (mm-find-buffer-file-coding-system)))
5218             (setq charset gnus-newsgroup-charset))))
5219        ((numberp arg)
5220         (setq charset (or (cdr (assq arg
5221                                      gnus-summary-show-article-charset-alist))
5222                           (mm-read-coding-system "Charset: ")))))
5223       (switch-to-buffer (generate-new-buffer filename))
5224       (if (or coding-system
5225               (and charset
5226                    (setq coding-system (mm-charset-to-coding-system charset))
5227                    (not (eq coding-system 'ascii))))
5228           (progn
5229             (mm-enable-multibyte)
5230             (insert (mm-decode-coding-string contents coding-system))
5231             (setq buffer-file-coding-system
5232                   (if (boundp 'last-coding-system-used)
5233                       (symbol-value 'last-coding-system-used)
5234                     coding-system)))
5235         (mm-disable-multibyte)
5236         (insert contents)
5237         (setq buffer-file-coding-system mm-binary-coding-system))
5238       ;; We do it this way to make `normal-mode' set the appropriate mode.
5239       (unwind-protect
5240           (progn
5241             (setq buffer-file-name (expand-file-name filename))
5242             (normal-mode))
5243         (setq buffer-file-name nil))
5244       (goto-char (point-min)))))
5245
5246 (defun gnus-mime-print-part (&optional handle filename)
5247   "Print the MIME part under point."
5248   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5249   (gnus-article-check-buffer)
5250   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5251          (contents (and handle (mm-get-part handle)))
5252          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5253          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5254     (when contents
5255         (if printer
5256             (unwind-protect
5257                 (progn
5258                   (mm-save-part-to-file handle file)
5259                   (call-process shell-file-name nil
5260                                 (generate-new-buffer " *mm*")
5261                                 nil
5262                                 shell-command-switch
5263                                 (mm-mailcap-command
5264                                  printer file (mm-handle-type handle))))
5265               (delete-file file))
5266           (with-temp-buffer
5267             (insert contents)
5268             (gnus-print-buffer))
5269           (ps-despool filename)))))
5270
5271 (defun gnus-mime-inline-part (&optional handle arg)
5272   "Insert the MIME part under point into the current buffer.
5273 Compressed files like .gz and .bz2 are decompressed."
5274   (interactive (list nil current-prefix-arg))
5275   (gnus-article-check-buffer)
5276   (unless handle
5277     (setq handle (get-text-property (point) 'gnus-data)))
5278   (when handle
5279     (let ((b (point))
5280           (inhibit-read-only t)
5281           contents charset coding-system)
5282       (if (and (not arg) (mm-handle-undisplayer handle))
5283           (mm-remove-part handle)
5284         (mm-with-unibyte-buffer
5285           (mm-insert-part handle)
5286           (setq contents
5287                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5288                     (buffer-string))))
5289         (cond
5290          ((not arg)
5291           (unless (setq charset (mail-content-type-get
5292                                  (mm-handle-type handle) 'charset))
5293             (unless (setq coding-system
5294                           (mm-with-unibyte-buffer
5295                             (insert contents)
5296                             (mm-find-buffer-file-coding-system)))
5297               (setq charset gnus-newsgroup-charset))))
5298          ((numberp arg)
5299           (if (mm-handle-undisplayer handle)
5300               (mm-remove-part handle))
5301           (setq charset
5302                 (or (cdr (assq arg
5303                                gnus-summary-show-article-charset-alist))
5304                     (mm-read-coding-system "Charset: "))))
5305          ((mm-handle-undisplayer handle)
5306           (mm-remove-part handle)))
5307         (forward-line 2)
5308         (mm-display-inline handle)
5309         (goto-char b)))))
5310
5311 (defun gnus-mime-set-charset-parameters (handle charset)
5312   "Set CHARSET to parameters in HANDLE.
5313 CHARSET may either be a string or a symbol."
5314   (unless (stringp charset)
5315     (setq charset (symbol-name charset)))
5316   (if (stringp (car handle))
5317       (dolist (h (cdr handle))
5318         (gnus-mime-set-charset-parameters h charset))
5319     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5320                                             "message/external-body")
5321                                      (progn
5322                                        (unless (mm-handle-cache handle)
5323                                          (mm-extern-cache-contents handle))
5324                                        (mm-handle-cache handle))
5325                                    handle)))
5326            (param (assq 'charset (cdr type))))
5327       (if param
5328           (setcdr param charset)
5329         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5330
5331 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5332   "Insert the MIME part under point into the current buffer using the
5333 specified charset."
5334   (interactive (list nil current-prefix-arg))
5335   (gnus-article-check-buffer)
5336   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5337         (fun (get-text-property (point) 'gnus-callback))
5338         (gnus-newsgroup-ignored-charsets 'gnus-all)
5339         charset form preferred parts)
5340     (when handle
5341       (when (prog1
5342                 (and fun
5343                      (setq charset
5344                            (or (cdr (assq
5345                                      arg
5346                                      gnus-summary-show-article-charset-alist))
5347                                (mm-read-coding-system "Charset: "))))
5348               (if (mm-handle-undisplayer handle)
5349                   (mm-remove-part handle)))
5350         (gnus-mime-set-charset-parameters handle charset)
5351         (when (and (consp (setq form (cdr-safe fun)))
5352                    (setq form (ignore-errors
5353                                 (assq 'gnus-mime-display-alternative form)))
5354                    (setq preferred (caddr form))
5355                    (progn
5356                      (when (eq (car preferred) 'quote)
5357                        (setq preferred (cadr preferred)))
5358                      (not (equal preferred
5359                                  (get-text-property (point) 'gnus-data))))
5360                    (setq parts (get-text-property (point) 'gnus-part))
5361                    (setq parts (cdr (assq parts
5362                                           gnus-article-mime-handle-alist)))
5363                    (equal (mm-handle-media-type parts) "multipart/alternative")
5364                    (setq parts (reverse (cdr parts))))
5365           (setcar (cddr form)
5366                   (list 'quote (or (cadr (member preferred parts))
5367                                    (car parts)))))
5368         (funcall fun handle)))))
5369
5370 (defun gnus-mime-view-part-externally (&optional handle)
5371   "View the MIME part under point with an external viewer."
5372   (interactive)
5373   (gnus-article-check-buffer)
5374   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5375          (mm-user-display-methods nil)
5376          (mm-inlined-types nil)
5377          (mail-parse-charset gnus-newsgroup-charset)
5378          (mail-parse-ignored-charsets
5379           (with-current-buffer gnus-summary-buffer
5380             gnus-newsgroup-ignored-charsets))
5381          (type (mm-handle-media-type handle))
5382          (method (mailcap-mime-info type))
5383          (mm-enable-external t))
5384     (if (not (stringp method))
5385         (gnus-mime-view-part-as-type
5386          nil (lambda (type) (stringp (mailcap-mime-info type))))
5387       (when handle
5388         (mm-display-part handle nil t)))))
5389
5390 (defun gnus-mime-view-part-internally (&optional handle)
5391   "View the MIME part under point with an internal viewer.
5392 If no internal viewer is available, use an external viewer."
5393   (interactive)
5394   (gnus-article-check-buffer)
5395   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5396          (mm-inlined-types '(".*"))
5397          (mm-inline-large-images t)
5398          (mail-parse-charset gnus-newsgroup-charset)
5399          (mail-parse-ignored-charsets
5400           (with-current-buffer gnus-summary-buffer
5401             gnus-newsgroup-ignored-charsets))
5402          (inhibit-read-only t))
5403     (if (not (mm-inlinable-p handle))
5404         (gnus-mime-view-part-as-type
5405          nil (lambda (type) (mm-inlinable-p handle type)))
5406       (when handle
5407         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5408
5409 (defun gnus-mime-action-on-part (&optional action)
5410   "Do something with the MIME attachment at \(point\)."
5411   (interactive
5412    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5413   (gnus-article-check-buffer)
5414   (let ((action-pair (assoc action gnus-mime-action-alist)))
5415     (if action-pair
5416         (funcall (cdr action-pair)))))
5417
5418 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5419   "Call FUNCTION on MIME part N.
5420 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5421 If INTERACTIVE, call FUNCTION interactively."
5422   (let (window frame)
5423     ;; Check whether the article is displayed.
5424     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5425                  (setq window (get-buffer-window gnus-article-buffer t))
5426                  (frame-visible-p (setq frame (window-frame window))))
5427       (error "No article is displayed"))
5428     (with-current-buffer gnus-article-buffer
5429       ;; Check whether the article displays the right contents.
5430       (unless (with-current-buffer gnus-summary-buffer
5431                 (eq gnus-current-article (gnus-summary-article-number)))
5432         (error "You should select the right article first"))
5433       (if n
5434           (setq n (prefix-numeric-value n))
5435         (let ((pt (point)))
5436           (setq n (or (get-text-property pt 'gnus-part)
5437                       (and (not (bobp))
5438                            (get-text-property (1- pt) 'gnus-part))
5439                       (get-text-property (prog2
5440                                              (forward-line 1)
5441                                              (point)
5442                                            (goto-char pt))
5443                                          'gnus-part)
5444                       (get-text-property
5445                        (or (and (setq pt (previous-single-property-change
5446                                           pt 'gnus-part))
5447                                 (1- pt))
5448                            (next-single-property-change (point) 'gnus-part)
5449                            (point))
5450                        'gnus-part)
5451                       1))))
5452       ;; Check whether the specified part exists.
5453       (when (> n (length gnus-article-mime-handle-alist))
5454         (error "No such part")))
5455     (unless
5456         (progn
5457           ;; To select the window is needed so that the cursor
5458           ;; might be visible on the MIME button.
5459           (select-window (prog1
5460                              window
5461                            (setq window (selected-window))
5462                            ;; Article may be displayed in the other frame.
5463                            (gnus-select-frame-set-input-focus
5464                             (prog1
5465                                 frame
5466                               (setq frame (selected-frame))))))
5467           (when (gnus-article-goto-part n)
5468             ;; We point the cursor and the arrow at the MIME button
5469             ;; when the `function' prompt the user for something.
5470             (unless (and (pos-visible-in-window-p)
5471                          (> (count-lines (point) (window-end))
5472                             (/ (1- (window-height)) 3)))
5473               (recenter (/ (1- (window-height)) 3)))
5474             (let ((cursor-in-non-selected-windows t)
5475                   (overlay-arrow-string "=>")
5476                   (overlay-arrow-position (point-marker)))
5477               (unwind-protect
5478                   (cond
5479                    ((and no-handle interactive)
5480                     (call-interactively function))
5481                    (no-handle
5482                     (funcall function))
5483                    (interactive
5484                     (call-interactively
5485                      function (get-text-property (point) 'gnus-data)))
5486                    (t
5487                     (funcall function
5488                              (get-text-property (point) 'gnus-data))))
5489                 (set-marker overlay-arrow-position nil)
5490                 (unless gnus-auto-select-part
5491                   (gnus-select-frame-set-input-focus frame)
5492                   (select-window window))))
5493             t))
5494       (if gnus-inhibit-mime-unbuttonizing
5495           ;; This is the default though the program shouldn't reach here.
5496           (error "No such part")
5497         ;; The part which doesn't have the MIME button is selected.
5498         ;; So, we display all the buttons and redo it.
5499         (let ((gnus-inhibit-mime-unbuttonizing t))
5500           (gnus-summary-show-article)
5501           (gnus-article-part-wrapper n function no-handle))))))
5502
5503 (defun gnus-article-pipe-part (n)
5504   "Pipe MIME part N, which is the numerical prefix."
5505   (interactive "P")
5506   (gnus-article-part-wrapper n 'mm-pipe-part))
5507
5508 (defun gnus-article-save-part (n)
5509   "Save MIME part N, which is the numerical prefix."
5510   (interactive "P")
5511   (gnus-article-part-wrapper n 'mm-save-part))
5512
5513 (defun gnus-article-interactively-view-part (n)
5514   "View MIME part N interactively, which is the numerical prefix."
5515   (interactive "P")
5516   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5517
5518 (defun gnus-article-copy-part (n)
5519   "Copy MIME part N, which is the numerical prefix."
5520   (interactive "P")
5521   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5522
5523 (defun gnus-article-view-part-as-charset (n)
5524   "View MIME part N using a specified charset.
5525 N is the numerical prefix."
5526   (interactive "P")
5527   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5528
5529 (defun gnus-article-view-part-externally (n)
5530   "View MIME part N externally, which is the numerical prefix."
5531   (interactive "P")
5532   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5533
5534 (defun gnus-article-inline-part (n)
5535   "Inline MIME part N, which is the numerical prefix."
5536   (interactive "P")
5537   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5538
5539 (defun gnus-article-save-part-and-strip (n)
5540   "Save MIME part N and replace it with an external body.
5541 N is the numerical prefix."
5542   (interactive "P")
5543   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5544
5545 (defun gnus-article-replace-part (n)
5546   "Replace MIME part N with an external body.
5547 N is the numerical prefix."
5548   (interactive "P")
5549   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5550
5551 (defun gnus-article-delete-part (n)
5552   "Delete MIME part N and add some information about the removed part.
5553 N is the numerical prefix."
5554   (interactive "P")
5555   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5556
5557 (defun gnus-article-view-part-as-type (n)
5558   "Choose a MIME media type, and view part N as such.
5559 N is the numerical prefix."
5560   (interactive "P")
5561   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5562
5563 (defun gnus-article-mime-match-handle-first (condition)
5564   (if condition
5565       (let (n)
5566         (dolist (ihandle gnus-article-mime-handle-alist)
5567           (if (and (cond
5568                     ((functionp condition)
5569                      (funcall condition (cdr ihandle)))
5570                     ((eq condition 'undisplayed)
5571                      (not (or (mm-handle-undisplayer (cdr ihandle))
5572                               (equal (mm-handle-media-type (cdr ihandle))
5573                                      "multipart/alternative"))))
5574                     ((eq condition 'undisplayed-alternative)
5575                      (not (mm-handle-undisplayer (cdr ihandle))))
5576                     (t t))
5577                    (gnus-article-goto-part (car ihandle))
5578                    (or (not n) (< (car ihandle) n)))
5579               (setq n (car ihandle))))
5580         (or n 1))
5581     1))
5582
5583 (defun gnus-article-view-part (&optional n)
5584   "View MIME part N, which is the numerical prefix.
5585 If the part is already shown, hide the part.  If N is nil, view
5586 all parts."
5587   (interactive "P")
5588   (with-current-buffer gnus-article-buffer
5589     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5590                              gnus-article-mime-match-handle-function)))
5591     (when (> n (length gnus-article-mime-handle-alist))
5592       (error "No such part"))
5593     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5594       (when (gnus-article-goto-part n)
5595         (if (equal (car handle) "multipart/alternative")
5596             (progn
5597               (beginning-of-line) ;; Make it toggle subparts
5598               (gnus-article-press-button))
5599           (when (eq (gnus-mm-display-part handle) 'internal)
5600             (gnus-set-window-start)))))))
5601
5602 (defsubst gnus-article-mime-total-parts ()
5603   (if (bufferp (car gnus-article-mime-handles))
5604       1 ;; single part
5605     (1- (length gnus-article-mime-handles))))
5606
5607 (defun gnus-mm-display-part (handle)
5608   "Display HANDLE and fix MIME button."
5609   (let ((id (get-text-property (point) 'gnus-part))
5610         (point (point))
5611         (inhibit-read-only t))
5612     (forward-line 1)
5613     (prog1
5614         (let ((window (selected-window))
5615               (mail-parse-charset gnus-newsgroup-charset)
5616               (mail-parse-ignored-charsets
5617                (if (gnus-buffer-live-p gnus-summary-buffer)
5618                    (with-current-buffer gnus-summary-buffer
5619                      gnus-newsgroup-ignored-charsets)
5620                  nil)))
5621           (save-excursion
5622             (unwind-protect
5623                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5624                       (beg (point)))
5625                   (when win
5626                     (select-window win))
5627                   (goto-char point)
5628                   (forward-line)
5629                   (if (mm-handle-displayed-p handle)
5630                       ;; This will remove the part.
5631                       (mm-display-part handle)
5632                     (save-restriction
5633                       (narrow-to-region (point)
5634                                         (if (eobp) (point) (1+ (point))))
5635                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5636                       ;; We narrow to the part itself and
5637                       ;; then call the treatment functions.
5638                       (goto-char (point-min))
5639                       (forward-line 1)
5640                       (narrow-to-region (point) (point-max))
5641                       (gnus-treat-article
5642                        nil id
5643                        (gnus-article-mime-total-parts)
5644                        (mm-handle-media-type handle)))))
5645               (if (window-live-p window)
5646                   (select-window window)))))
5647       (goto-char point)
5648       (gnus-delete-line)
5649       (gnus-insert-mime-button
5650        handle id (list (mm-handle-displayed-p handle)))
5651       (goto-char point))))
5652
5653 (defun gnus-article-goto-part (n)
5654   "Go to MIME part N."
5655   (when gnus-break-pages
5656     (widen))
5657   (prog1
5658       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5659             part handle end next handles)
5660         (when start
5661           (goto-char start)
5662           (if (setq handle (get-text-property start 'gnus-data))
5663               start
5664             ;; Go to the displayed subpart, assuming this is
5665             ;; multipart/alternative.
5666             (setq part start
5667                   end (point-at-eol))
5668             (while (and (not handle)
5669                         part
5670                         (< part end)
5671                         (setq next (text-property-not-all part end
5672                                                           'gnus-data nil)))
5673               (setq part next
5674                     handle (get-text-property part 'gnus-data))
5675               (push (cons handle part) handles)
5676               (unless (mm-handle-displayed-p handle)
5677                 (setq handle nil
5678                       part (text-property-any part end 'gnus-data nil))))
5679             (unless handle
5680               ;; No subpart is displayed, so we find preferred one.
5681               (setq part
5682                     (cdr (assq (mm-preferred-alternative
5683                                 (nreverse (mapcar 'car handles)))
5684                                handles))))
5685             (if part
5686                 (goto-char (1+ part))
5687               start))))
5688     (when gnus-break-pages
5689       (gnus-narrow-to-page))))
5690
5691 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5692   (let ((gnus-tmp-name
5693          (or (mm-handle-filename handle)
5694              (mail-content-type-get (mm-handle-type handle) 'url)
5695              ""))
5696         (gnus-tmp-type (mm-handle-media-type handle))
5697         (gnus-tmp-description (or (mm-handle-description handle) ""))
5698         (gnus-tmp-dots
5699          (if (if displayed (car displayed)
5700                (mm-handle-displayed-p handle))
5701              "" "..."))
5702         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5703                            (buffer-size)))
5704         gnus-tmp-type-long b e)
5705     (when (string-match ".*/" gnus-tmp-name)
5706       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5707     (setq gnus-tmp-type-long (concat gnus-tmp-type
5708                                      (and (not (equal gnus-tmp-name ""))
5709                                           (concat "; " gnus-tmp-name))))
5710     (unless (equal gnus-tmp-description "")
5711       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5712     (unless (bolp)
5713       (insert "\n"))
5714     (setq b (point))
5715     (gnus-eval-format
5716      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5717      `(keymap ,gnus-mime-button-map
5718               gnus-callback gnus-mm-display-part
5719               gnus-part ,gnus-tmp-id
5720               article-type annotation
5721               gnus-data ,handle
5722               rear-nonsticky t))
5723     (setq e (if (bolp)
5724                 ;; Exclude a newline.
5725                 (1- (point))
5726               (point)))
5727     (when gnus-article-button-face
5728       (gnus-overlay-put (gnus-make-overlay b e nil t)
5729                         'face gnus-article-button-face))
5730     (widget-convert-button
5731      'link b e
5732      :mime-handle handle
5733      :action 'gnus-widget-press-button
5734      :button-keymap gnus-mime-button-map
5735      :help-echo
5736      (lambda (widget)
5737        ;; Needed to properly clear the message due to a bug in
5738        ;; wid-edit (XEmacs only).
5739        (if (boundp 'help-echo-owns-message)
5740            (setq help-echo-owns-message t))
5741        (format
5742         "%S: %s the MIME part; %S: more options"
5743         (aref gnus-mouse-2 0)
5744         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5745             "hide" "show")
5746         (aref gnus-down-mouse-3 0))))))
5747
5748 (defun gnus-widget-press-button (elems el)
5749   (goto-char (widget-get elems :from))
5750   (gnus-article-press-button))
5751
5752 (defvar gnus-displaying-mime nil)
5753
5754 (defun gnus-display-mime (&optional ihandles)
5755   "Display the MIME parts."
5756   (save-excursion
5757     (save-selected-window
5758       (let ((window (get-buffer-window gnus-article-buffer))
5759             (point (point)))
5760         (when window
5761           (select-window window)
5762           ;; We have to do this since selecting the window
5763           ;; may change the point.  So we set the window point.
5764           (set-window-point window point)))
5765       (let ((handles ihandles)
5766             (inhibit-read-only t)
5767             handle)
5768         (cond (handles)
5769               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5770                (when gnus-article-emulate-mime
5771                  (mm-uu-dissect-text-parts handles)))
5772               (gnus-article-emulate-mime
5773                (setq handles (mm-uu-dissect))))
5774         (when (and (not ihandles)
5775                    (not gnus-displaying-mime))
5776           ;; Top-level call; we clean up.
5777           (when gnus-article-mime-handles
5778             (mm-destroy-parts gnus-article-mime-handles)
5779             (setq gnus-article-mime-handle-alist nil));; A trick.
5780           (setq gnus-article-mime-handles handles)
5781           ;; We allow users to glean info from the handles.
5782           (when gnus-article-mime-part-function
5783             (gnus-mime-part-function handles)))
5784         (if (and handles
5785                  (or (not (stringp (car handles)))
5786                      (cdr handles)))
5787             (progn
5788               (when (and (not ihandles)
5789                          (not gnus-displaying-mime))
5790                 ;; Clean up for mime parts.
5791                 (article-goto-body)
5792                 (delete-region (point) (point-max)))
5793               (let ((gnus-displaying-mime t))
5794                 (gnus-mime-display-part handles)))
5795           (save-restriction
5796             (article-goto-body)
5797             (narrow-to-region (point) (point-max))
5798             (gnus-treat-article nil 1 1 "text/plain")
5799             (widen)))
5800         (unless ihandles
5801           ;; Highlight the headers.
5802           (save-excursion
5803             (save-restriction
5804               (article-goto-body)
5805               (narrow-to-region (point-min) (point))
5806               (gnus-article-save-original-date
5807                (gnus-treat-article 'head)))))))
5808     ;; Cope with broken MIME messages.
5809     (goto-char (point-max))
5810     (unless (bolp)
5811       (insert "\n"))))
5812
5813 (defcustom gnus-mime-display-multipart-as-mixed nil
5814   "Display \"multipart\" parts as  \"multipart/mixed\".
5815
5816 If t, it overrides nil values of
5817 `gnus-mime-display-multipart-alternative-as-mixed' and
5818 `gnus-mime-display-multipart-related-as-mixed'."
5819   :group 'gnus-article-mime
5820   :type 'boolean)
5821
5822 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5823   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5824   :version "22.1"
5825   :group 'gnus-article-mime
5826   :type 'boolean)
5827
5828 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5829   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5830
5831 If displaying \"text/html\" is discouraged \(see
5832 `mm-discouraged-alternatives'\) images or other material inside a
5833 \"multipart/related\" part might be overlooked when this variable is nil."
5834   :version "22.1"
5835   :group 'gnus-article-mime
5836   :type 'boolean)
5837
5838 (defun gnus-mime-display-part (handle)
5839   (cond
5840    ;; Maybe a broken MIME message.
5841    ((null handle))
5842    ;; Single part.
5843    ((not (stringp (car handle)))
5844     (gnus-mime-display-single handle))
5845    ;; User-defined multipart
5846    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5847     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5848              handle))
5849    ;; multipart/alternative
5850    ((and (equal (car handle) "multipart/alternative")
5851          (not (or gnus-mime-display-multipart-as-mixed
5852                   gnus-mime-display-multipart-alternative-as-mixed)))
5853     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5854       (push (cons id handle) gnus-article-mime-handle-alist)
5855       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5856    ;; multipart/related
5857    ((and (equal (car handle) "multipart/related")
5858          (not (or gnus-mime-display-multipart-as-mixed
5859                   gnus-mime-display-multipart-related-as-mixed)))
5860     ;;;!!!We should find the start part, but we just default
5861     ;;;!!!to the first part.
5862     ;;(gnus-mime-display-part (cadr handle))
5863     ;;;!!! Most multipart/related is an HTML message plus images.
5864     ;;;!!! Unfortunately we are unable to let W3 display those
5865     ;;;!!! included images, so we just display it as a mixed multipart.
5866     ;;(gnus-mime-display-mixed (cdr handle))
5867     ;;;!!! No, w3 can display everything just fine.
5868     (gnus-mime-display-part (cadr handle)))
5869    ((equal (car handle) "multipart/signed")
5870     (gnus-add-wash-type 'signed)
5871     (gnus-mime-display-security handle))
5872    ((equal (car handle) "multipart/encrypted")
5873     (gnus-add-wash-type 'encrypted)
5874     (gnus-mime-display-security handle))
5875    ;; Other multiparts are handled like multipart/mixed.
5876    (t
5877     (gnus-mime-display-mixed (cdr handle)))))
5878
5879 (defun gnus-mime-part-function (handles)
5880   (if (stringp (car handles))
5881       (mapcar 'gnus-mime-part-function (cdr handles))
5882     (funcall gnus-article-mime-part-function handles)))
5883
5884 (defun gnus-mime-display-mixed (handles)
5885   (mapcar 'gnus-mime-display-part handles))
5886
5887 (defun gnus-mime-display-single (handle)
5888   (let ((type (mm-handle-media-type handle))
5889         (ignored gnus-ignored-mime-types)
5890         (not-attachment t)
5891         (move nil)
5892         display text)
5893     (catch 'ignored
5894       (progn
5895         (while ignored
5896           (when (string-match (pop ignored) type)
5897             (throw 'ignored nil)))
5898         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5899                                (with-current-buffer gnus-summary-buffer
5900                                  gnus-inhibit-images)
5901                              gnus-inhibit-images)
5902                            (string-match "\\`image/" type)))
5903                  (setq not-attachment
5904                        (and (not (mm-inline-override-p handle))
5905                             (or (not (mm-handle-disposition handle))
5906                                 (equal (car (mm-handle-disposition handle))
5907                                        "inline")
5908                                 (mm-attachment-override-p handle))))
5909                  (mm-automatic-display-p handle)
5910                  (or (and
5911                       (mm-inlinable-p handle)
5912                       (mm-inlined-p handle))
5913                      (mm-automatic-external-display-p type)))
5914             (setq display t)
5915           (when (equal (mm-handle-media-supertype handle) "text")
5916             (setq text t)))
5917         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5918               beg)
5919           (push (cons id handle) gnus-article-mime-handle-alist)
5920           (when (and display
5921                      (equal (mm-handle-media-supertype handle) "message"))
5922             (insert-char
5923              ?\n
5924              (cond ((not (bolp)) 2)
5925                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5926                    (t 1))))
5927           (when (or (not display)
5928                     (not (gnus-unbuttonized-mime-type-p type))
5929                     (eq id gnus-mime-buttonized-part-id))
5930             (gnus-insert-mime-button
5931              handle id (list (or display (and not-attachment text))))
5932             (gnus-article-insert-newline)
5933             ;; Remember modify the number of forward lines.
5934             (setq move t))
5935           (setq beg (point))
5936           (cond
5937            (display
5938             (when move
5939               (forward-line -1)
5940               (setq beg (point)))
5941             (let ((mail-parse-charset gnus-newsgroup-charset)
5942                   (mail-parse-ignored-charsets
5943                    (save-excursion (condition-case ()
5944                                        (set-buffer gnus-summary-buffer)
5945                                      (error))
5946                                    gnus-newsgroup-ignored-charsets)))
5947               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5948             (goto-char (point-max)))
5949            ((and text not-attachment)
5950             (when move
5951               (forward-line -1)
5952               (setq beg (point)))
5953             (gnus-article-insert-newline)
5954             (mm-display-inline handle)
5955             (goto-char (point-max))))
5956           ;; Do highlighting.
5957           (save-excursion
5958             (save-restriction
5959               (narrow-to-region beg (point))
5960               (if (eq handle gnus-article-mime-handles)
5961                   ;; The format=flowed case.
5962                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5963                 ;; Don't count signature parts that are never displayed.
5964                 ;; The part number should be re-calculated supposing this
5965                 ;; might be a message/rfc822 part.
5966                 (let (handles)
5967                   (dolist (part gnus-article-mime-handles)
5968                     (unless (or (stringp part)
5969                                 (equal (car (mm-handle-type part))
5970                                        "application/pgp-signature"))
5971                       (push part handles)))
5972                   (gnus-treat-article
5973                    nil (length (memq handle handles)) (length handles)
5974                    (mm-handle-media-type handle)))))))))))
5975
5976 (defun gnus-unbuttonized-mime-type-p (type)
5977   "Say whether TYPE is to be unbuttonized."
5978   (unless gnus-inhibit-mime-unbuttonizing
5979     (when (catch 'found
5980             (let ((types gnus-unbuttonized-mime-types))
5981               (while types
5982                 (when (string-match (pop types) type)
5983                   (throw 'found t)))))
5984       (not (catch 'found
5985              (let ((types gnus-buttonized-mime-types))
5986                (while types
5987                  (when (string-match (pop types) type)
5988                    (throw 'found t)))))))))
5989
5990 (defun gnus-article-insert-newline ()
5991   "Insert a newline, but mark it as undeletable."
5992   (gnus-put-text-property
5993    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5994
5995 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5996   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5997          (ihandles handles)
5998          (point (point))
5999          handle (inhibit-read-only t) from props begend not-pref)
6000     (save-window-excursion
6001       (save-restriction
6002         (when ibegend
6003           (narrow-to-region (car ibegend)
6004                             (or (cdr ibegend)
6005                                 (progn
6006                                   (goto-char (car ibegend))
6007                                   (forward-line 2)
6008                                   (point))))
6009           (delete-region (point-min) (point-max))
6010           (mm-remove-parts handles))
6011         (setq begend (list (point-marker)))
6012         ;; Do the toggle.
6013         (unless (setq not-pref (cadr (member preferred ihandles)))
6014           (setq not-pref (car ihandles)))
6015         (when (or ibegend
6016                   (not preferred)
6017                   (not (gnus-unbuttonized-mime-type-p
6018                         "multipart/alternative")))
6019           (gnus-add-text-properties
6020            (setq from (point))
6021            (progn
6022              (insert (format "%d.  " id))
6023              (point))
6024            `(gnus-callback
6025              (lambda (handles)
6026                (unless ,(not ibegend)
6027                  (setq gnus-article-mime-handle-alist
6028                        ',gnus-article-mime-handle-alist))
6029                (gnus-mime-display-alternative
6030                 ',ihandles ',not-pref ',begend ,id))
6031              keymap ,gnus-mime-button-map
6032              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6033              face ,gnus-article-button-face
6034              gnus-part ,id
6035              article-type multipart
6036              rear-nonsticky t))
6037           (widget-convert-button 'link from (point)
6038                                  :action 'gnus-widget-press-button
6039                                  :button-keymap gnus-widget-button-keymap)
6040           ;; Do the handles
6041           (while (setq handle (pop handles))
6042             (gnus-add-text-properties
6043              (setq from (point))
6044              (progn
6045                (insert (format "(%c) %-18s"
6046                                (if (equal handle preferred) ?* ? )
6047                                (mm-handle-media-type handle)))
6048                (point))
6049              `(gnus-callback
6050                (lambda (handles)
6051                  (unless ,(not ibegend)
6052                    (setq gnus-article-mime-handle-alist
6053                          ',gnus-article-mime-handle-alist))
6054                  (gnus-mime-display-alternative
6055                   ',ihandles ',handle ',begend ,id))
6056                keymap ,gnus-mime-button-map
6057                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6058                face ,gnus-article-button-face
6059                gnus-part ,id
6060                gnus-data ,handle
6061                rear-nonsticky t))
6062             (widget-convert-button 'link from (point)
6063                                    :action 'gnus-widget-press-button
6064                                    :button-keymap gnus-widget-button-keymap)
6065             (insert "  "))
6066           (insert "\n\n"))
6067         (when preferred
6068           (if (stringp (car preferred))
6069               (gnus-display-mime preferred)
6070             (let ((mail-parse-charset gnus-newsgroup-charset)
6071                   (mail-parse-ignored-charsets
6072                    (with-current-buffer gnus-summary-buffer
6073                      gnus-newsgroup-ignored-charsets)))
6074               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6075               ;; Do highlighting.
6076               (save-excursion
6077                 (save-restriction
6078                   (narrow-to-region (car begend) (point-max))
6079                   (gnus-treat-article
6080                    nil (length gnus-article-mime-handle-alist)
6081                    (gnus-article-mime-total-parts)
6082                    (mm-handle-media-type preferred))))))
6083           (goto-char (point-max))
6084           (setcdr begend (point-marker)))))
6085     (when ibegend
6086       (goto-char point))))
6087
6088 (defconst gnus-article-wash-status-strings
6089   (let ((alist '((cite "c" "Possible hidden citation text"
6090                        " " "All citation text visible")
6091                  (headers "h" "Hidden headers"
6092                           " " "All headers visible.")
6093                  (pgp "p" "Encrypted or signed message status hidden"
6094                       " " "No hidden encryption nor digital signature status")
6095                  (signature "s" "Signature has been hidden"
6096                             " " "Signature is visible")
6097                  (overstrike "o" "Overstrike (^H) characters applied"
6098                              " " "No overstrike characters applied")
6099                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6100                            " " "No /*_emphasis_*/ characters applied")))
6101         result)
6102     (dolist (entry alist result)
6103       (let ((key (nth 0 entry))
6104             (on (copy-sequence (nth 1 entry)))
6105             (on-help (nth 2 entry))
6106             (off (copy-sequence (nth 3 entry)))
6107             (off-help (nth 4 entry)))
6108         (put-text-property 0 1 'help-echo on-help on)
6109         (put-text-property 0 1 'help-echo off-help off)
6110         (push (list key on off) result))))
6111   "Alist of strings describing wash status in the mode line.
6112 Each entry has the form (KEY ON OF), where the KEY is a symbol
6113 representing the particular washing function, ON is the string to use
6114 in the article mode line when the washing function is active, and OFF
6115 is the string to use when it is inactive.")
6116
6117 (defun gnus-article-wash-status-entry (key value)
6118   (let ((entry (assoc key gnus-article-wash-status-strings)))
6119     (if value (nth 1 entry) (nth 2 entry))))
6120
6121 (defun gnus-article-wash-status ()
6122   "Return a string which display status of article washing."
6123   (with-current-buffer gnus-article-buffer
6124     (let ((cite (memq 'cite gnus-article-wash-types))
6125           (headers (memq 'headers gnus-article-wash-types))
6126           (boring (memq 'boring-headers gnus-article-wash-types))
6127           (pgp (memq 'pgp gnus-article-wash-types))
6128           (pem (memq 'pem gnus-article-wash-types))
6129           (signed (memq 'signed gnus-article-wash-types))
6130           (encrypted (memq 'encrypted gnus-article-wash-types))
6131           (signature (memq 'signature gnus-article-wash-types))
6132           (overstrike (memq 'overstrike gnus-article-wash-types))
6133           (emphasis (memq 'emphasis gnus-article-wash-types)))
6134       (concat
6135        (gnus-article-wash-status-entry 'cite cite)
6136        (gnus-article-wash-status-entry 'headers (or headers boring))
6137        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6138        (gnus-article-wash-status-entry 'signature signature)
6139        (gnus-article-wash-status-entry 'overstrike overstrike)
6140        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6141
6142 (defun gnus-add-wash-type (type)
6143   "Add a washing of TYPE to the current status."
6144   (add-to-list 'gnus-article-wash-types type))
6145
6146 (defun gnus-delete-wash-type (type)
6147   "Add a washing of TYPE to the current status."
6148   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6149
6150 (defun gnus-add-image (category image)
6151   "Add IMAGE of CATEGORY to the list of displayed images."
6152   (let ((entry (assq category gnus-article-image-alist)))
6153     (unless entry
6154       (setq entry (list category))
6155       (push entry gnus-article-image-alist))
6156     (nconc entry (list image))))
6157
6158 (defun gnus-delete-images (category)
6159   "Delete all images in CATEGORY."
6160   (let ((entry (assq category gnus-article-image-alist)))
6161     (dolist (image (cdr entry))
6162       (gnus-remove-image image category))
6163     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6164     (gnus-delete-wash-type category)))
6165
6166 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6167
6168 (defun gnus-article-maybe-hide-headers ()
6169   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6170 Provided for backwards compatibility."
6171   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6172                  (not (with-current-buffer gnus-summary-buffer
6173                         gnus-have-all-headers)))
6174              (not gnus-inhibit-hiding))
6175     (gnus-article-hide-headers)))
6176
6177 (declare-function shr-put-image "shr" (data alt &optional flags))
6178
6179 (defun gnus-shr-put-image (data alt &optional flags)
6180   "Put image DATA with a string ALT.  Enable image to be deleted."
6181   (let ((image (shr-put-image data (propertize (or alt "*")
6182                                                'gnus-image-category 'shr)
6183                               flags)))
6184     (when image
6185       (gnus-add-image 'shr image))))
6186
6187 ;;; Article savers.
6188
6189 (defun gnus-output-to-file (file-name)
6190   "Append the current article to a file named FILE-NAME.
6191 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6192 text and used as the value of the coding cookie which is added to the
6193 top of a file.  Otherwise, this function saves a raw article without
6194 the coding cookie."
6195   (let* ((artbuf (current-buffer))
6196          (file-name-coding-system nnmail-pathname-coding-system)
6197          (coding gnus-article-save-coding-system)
6198          (coding-system-for-read (if coding
6199                                      nil ;; Rely on the coding cookie.
6200                                    mm-text-coding-system))
6201          (coding-system-for-write (or coding
6202                                       mm-text-coding-system-for-write
6203                                       mm-text-coding-system))
6204          (exists (file-exists-p file-name)))
6205     (with-temp-buffer
6206       (when exists
6207         (insert-file-contents file-name)
6208         (goto-char (point-min))
6209         ;; Remove the existing coding cookie.
6210         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6211           (delete-region (match-beginning 0) (match-end 0))))
6212       (goto-char (point-max))
6213       (insert-buffer-substring artbuf)
6214       ;; Append newline at end of the buffer as separator, and then
6215       ;; save it to file.
6216       (goto-char (point-max))
6217       (insert "\n")
6218       (when coding
6219         ;; If the coding system is not suitable to encode the text,
6220         ;; ask a user for a proper one.
6221         (when (fboundp 'select-safe-coding-system)
6222           (setq coding (coding-system-base
6223                         (save-window-excursion
6224                           (select-safe-coding-system (point-min) (point-max)
6225                                                      coding))))
6226           (setq coding-system-for-write
6227                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6228                     coding)))
6229         (goto-char (point-min))
6230         ;; Add the coding cookie.
6231         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6232                         coding-system-for-write)))
6233       (if exists
6234           (progn
6235             (write-region (point-min) (point-max) file-name nil 'no-message)
6236             (message "Appended to %s" file-name))
6237         (write-region (point-min) (point-max) file-name))))
6238   t)
6239
6240 (defun gnus-narrow-to-page (&optional arg)
6241   "Narrow the article buffer to a page.
6242 If given a numerical ARG, move forward ARG pages."
6243   (interactive "P")
6244   (setq arg (if arg (prefix-numeric-value arg) 0))
6245   (with-current-buffer gnus-article-buffer
6246     (widen)
6247     ;; Remove any old next/prev buttons.
6248     (when (gnus-visual-p 'page-marker)
6249       (let ((inhibit-read-only t))
6250         (gnus-remove-text-with-property 'gnus-prev)
6251         (gnus-remove-text-with-property 'gnus-next)))
6252     (let (st nd pt)
6253       (when (save-excursion
6254               (cond ((< arg 0)
6255                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6256                          (prog1
6257                              (setq nd (match-beginning 0)
6258                                    pt nd)
6259                            (when (re-search-backward page-delimiter nil t)
6260                              (setq st (match-end 0))))
6261                        (when (re-search-forward page-delimiter nil t)
6262                          (setq nd (match-beginning 0)
6263                                pt (point-min)))))
6264                     ((> arg 0)
6265                      (if (re-search-forward page-delimiter nil 'move arg)
6266                          (prog1
6267                              (setq st (match-end 0)
6268                                    pt st)
6269                            (when (re-search-forward page-delimiter nil t)
6270                              (setq nd (match-beginning 0))))
6271                        (when (re-search-backward page-delimiter nil t)
6272                          (setq st (match-end 0)
6273                                pt (point-max)))))
6274                     (t
6275                      (when (re-search-backward page-delimiter nil t)
6276                        (goto-char (setq st (match-end 0))))
6277                      (when (re-search-forward page-delimiter nil t)
6278                        (setq nd (match-beginning 0)))
6279                      (or st nd))))
6280         (setq gnus-page-broken t)
6281         (when pt (goto-char pt))
6282         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6283         (when (gnus-visual-p 'page-marker)
6284           (save-excursion
6285             (when nd
6286               (goto-char nd)
6287               (gnus-insert-next-page-button))
6288             (when st
6289               (goto-char st)
6290               (gnus-insert-prev-page-button))))))))
6291
6292 ;; Article mode commands
6293
6294 (defun gnus-article-goto-next-page ()
6295   "Show the next page of the article."
6296   (interactive)
6297   (when (gnus-article-next-page)
6298     (goto-char (point-min))
6299     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6300
6301
6302 (defun gnus-article-goto-prev-page ()
6303   "Show the previous page of the article."
6304   (interactive)
6305   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6306       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6307     (gnus-article-prev-page nil)))
6308
6309 ;; This is cleaner but currently breaks `gnus-pick-mode':
6310 ;;
6311 ;; (defun gnus-article-goto-next-page ()
6312 ;;   "Show the next page of the article."
6313 ;;   (interactive)
6314 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6315 ;;     (gnus-summary-next-page)))
6316 ;;
6317 ;; (defun gnus-article-goto-prev-page ()
6318 ;;   "Show the next page of the article."
6319 ;;   (interactive)
6320 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6321 ;;     (gnus-summary-prev-page)))
6322
6323 (defun gnus-article-next-page (&optional lines)
6324   "Show the next page of the current article.
6325 If end of article, return non-nil.  Otherwise return nil.
6326 Argument LINES specifies lines to be scrolled up."
6327   (interactive "p")
6328   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6329   (if (and (not (and gnus-article-over-scroll
6330                      (> (count-lines (window-start) (point-max))
6331                         (if (featurep 'xemacs)
6332                             (or lines (1- (window-height)))
6333                           (+ (or lines (1- (window-height))) scroll-margin)))))
6334            (save-excursion
6335              (end-of-line)
6336              (and (pos-visible-in-window-p)     ;Not continuation line.
6337                   (>= (point) (point-max)))))
6338       ;; Nothing in this page.
6339       (if (or (not gnus-page-broken)
6340               (save-excursion
6341                 (save-restriction
6342                   (widen)
6343                   (forward-line)
6344                   (eobp)))) ;Real end-of-buffer?
6345           (progn
6346             (when gnus-article-over-scroll
6347               (gnus-article-next-page-1 lines))
6348             t)                  ;Nothing more.
6349         (gnus-narrow-to-page 1)         ;Go to next page.
6350         nil)
6351     ;; More in this page.
6352     (gnus-article-next-page-1 lines)
6353     nil))
6354
6355 (defun gnus-article-beginning-of-window ()
6356   "Move point to the beginning of the window.
6357 In Emacs, the point is placed at the line number which `scroll-margin'
6358 specifies."
6359   (if (featurep 'xemacs)
6360       (move-to-window-line 0)
6361     ;; There is an obscure bug in Emacs that makes it impossible to
6362     ;; scroll past big pictures in the article buffer.  Try to fix
6363     ;; this by adding a sanity check by counting the lines visible.
6364     (when (> (count-lines (window-start) (window-end)) 30)
6365       (move-to-window-line
6366        (min (max 0 scroll-margin)
6367             (max 1 (- (window-height)
6368                       (if mode-line-format 1 0)
6369                       (if header-line-format 1 0)
6370                       2)))))))
6371
6372 (defun gnus-article-next-page-1 (lines)
6373   (condition-case ()
6374       (let ((scroll-in-place nil)
6375             (auto-window-vscroll nil))
6376         (scroll-up lines))
6377     (end-of-buffer
6378      ;; Long lines may cause an end-of-buffer error.
6379      (goto-char (point-max))))
6380   (gnus-article-beginning-of-window))
6381
6382 (defun gnus-article-prev-page (&optional lines)
6383   "Show previous page of current article.
6384 Argument LINES specifies lines to be scrolled down."
6385   (interactive "p")
6386   (move-to-window-line 0)
6387   (if (and gnus-page-broken
6388            (bobp)
6389            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6390       (progn
6391         (gnus-narrow-to-page -1)        ;Go to previous page.
6392         (goto-char (point-max))
6393         (recenter (if gnus-article-over-scroll
6394                       (if lines
6395                           (max (if (featurep 'xemacs)
6396                                    lines
6397                                  (+ lines scroll-margin))
6398                                3)
6399                         (- (window-height) 2))
6400                     -1)))
6401     (prog1
6402         (condition-case ()
6403             (let ((scroll-in-place nil))
6404               (scroll-down lines))
6405           (beginning-of-buffer
6406            (goto-char (point-min))))
6407       (gnus-article-beginning-of-window))))
6408
6409 (defun gnus-article-only-boring-p ()
6410   "Decide whether there is only boring text remaining in the article.
6411 Something \"interesting\" is a word of at least two letters that does
6412 not have a face in `gnus-article-boring-faces'."
6413   (when (and gnus-article-skip-boring
6414              (boundp 'gnus-article-boring-faces)
6415              (symbol-value 'gnus-article-boring-faces))
6416     (save-excursion
6417       (let ((inhibit-point-motion-hooks t))
6418         (catch 'only-boring
6419           (while (re-search-forward "\\b\\w\\w" nil t)
6420             (forward-char -1)
6421             (when (not (gnus-intersection
6422                         (gnus-faces-at (point))
6423                         (symbol-value 'gnus-article-boring-faces)))
6424               (throw 'only-boring nil)))
6425           (throw 'only-boring t))))))
6426
6427 (defun gnus-article-refer-article ()
6428   "Read article specified by message-id around point."
6429   (interactive)
6430   (save-excursion
6431     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6432     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6433     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6434         (let ((msg-id (concat "<" (match-string 0) ">")))
6435           (set-buffer gnus-summary-buffer)
6436           (gnus-summary-refer-article msg-id))
6437       (error "No references around point"))))
6438
6439 (defun gnus-article-show-summary ()
6440   "Reconfigure windows to show summary buffer."
6441   (interactive)
6442   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6443       (error "There is no summary buffer for this article buffer")
6444     (gnus-article-set-globals)
6445     (gnus-configure-windows 'article)
6446     (gnus-summary-goto-subject gnus-current-article)
6447     (gnus-summary-position-point)))
6448
6449 (defun gnus-article-describe-briefly ()
6450   "Describe article mode commands briefly."
6451   (interactive)
6452   (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")))
6453
6454 (defun gnus-article-check-buffer ()
6455   "Beep if not in an article buffer."
6456   (unless (equal major-mode 'gnus-article-mode)
6457     (error "Command invoked outside of a Gnus article buffer")))
6458
6459 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6460   "Read a summary buffer key sequence and execute it from the article buffer."
6461   (interactive "P")
6462   (gnus-article-check-buffer)
6463   (let ((nosaves
6464          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6465            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6466            "=" "^" "\M-^" "|"))
6467         (nosave-but-article
6468          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6469            "An" "Ap" [?A (meta return)] [?A delete]))
6470         (nosave-in-article
6471          '("AS" "\C-d"))
6472         (up-to-top
6473          '("n" "Gn" "p" "Gp"))
6474         keys new-sum-point)
6475     (with-current-buffer gnus-article-current-summary
6476       (let (gnus-pick-mode)
6477         (setq unread-command-events (nconc unread-command-events
6478                                            (list (or key last-command-event)))
6479               keys (if (featurep 'xemacs)
6480                        (events-to-keys (read-key-sequence nil t))
6481                      (read-key-sequence nil t)))))
6482
6483     (message "")
6484
6485     (cond
6486      ((eq (aref keys (1- (length keys))) ?\C-h)
6487       (gnus-article-describe-bindings (substring keys 0 -1)))
6488      ((or (member keys nosaves)
6489           (member keys nosave-but-article)
6490           (member keys nosave-in-article))
6491       (let (func)
6492         (save-window-excursion
6493           (pop-to-buffer gnus-article-current-summary)
6494           ;; We disable the pick minor mode commands.
6495           (let (gnus-pick-mode)
6496             (setq func (key-binding keys t))))
6497         (if (or (not func)
6498                 (numberp func))
6499             (ding)
6500           (unless (member keys nosave-in-article)
6501             (set-buffer gnus-article-current-summary))
6502           (when (and (symbolp func)
6503                      (get func 'disabled))
6504             (error "Function %s disabled" func))
6505           (call-interactively func)
6506           (setq new-sum-point (point)))
6507         (when (member keys nosave-but-article)
6508           (pop-to-buffer gnus-article-buffer))))
6509      (t
6510       ;; These commands should restore window configuration.
6511       (let ((obuf (current-buffer))
6512             (owin (current-window-configuration))
6513             win func in-buffer selected new-sum-start new-sum-hscroll err)
6514         (cond (not-restore-window
6515                (pop-to-buffer gnus-article-current-summary)
6516                (setq win (selected-window)))
6517               ((setq win (get-buffer-window gnus-article-current-summary))
6518                (select-window win))
6519               (t
6520                (let ((summary-buffer gnus-article-current-summary))
6521                  (gnus-configure-windows 'article)
6522                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6523                    (let ((gnus-buffer-configuration
6524                           '((article ((vertical 1.0
6525                                                 (summary 0.25 point)
6526                                                 (article 1.0)))))))
6527                      (gnus-configure-windows 'article))
6528                    (setq win (get-buffer-window summary-buffer 'visible)))
6529                  (gnus-select-frame-set-input-focus (window-frame win))
6530                  (select-window win))))
6531         (setq in-buffer (current-buffer))
6532         ;; We disable the pick minor mode commands.
6533         (setq func (let (gnus-pick-mode)
6534                      (key-binding keys t)))
6535         (when (get func 'disabled)
6536           (error "Function %s disabled" func))
6537         (if (and func
6538                  (functionp func)
6539                  (condition-case code
6540                      (progn
6541                        (call-interactively func)
6542                        t)
6543                    (error
6544                     (setq err code)
6545                     nil)))
6546             (progn
6547               (when (eq win (selected-window))
6548                 (setq new-sum-point (point)
6549                       new-sum-start (window-start win)
6550                       new-sum-hscroll (window-hscroll win)))
6551               (when (or (eq in-buffer (current-buffer))
6552                         (when (eq obuf (current-buffer))
6553                           (set-buffer in-buffer)
6554                           t))
6555                 (setq selected (gnus-summary-select-article))
6556                 (set-buffer obuf)
6557                 (unless not-restore-window
6558                   (set-window-configuration owin))
6559                 (when (and (eq selected 'old)
6560                            new-sum-point)
6561                   (set-window-start (get-buffer-window (current-buffer))
6562                                     1)
6563                   (set-window-point (get-buffer-window (current-buffer))
6564                                     (if (article-goto-body)
6565                                         (1- (point))
6566                                       (point))))
6567                 (when (and (not not-restore-window)
6568                            new-sum-point
6569                            (window-live-p win)
6570                            (with-current-buffer (window-buffer win)
6571                              (eq major-mode 'gnus-summary-mode)))
6572                   (set-window-point win new-sum-point)
6573                   (set-window-start win new-sum-start)
6574                   (set-window-hscroll win new-sum-hscroll))))
6575           (set-window-configuration owin)
6576           (if err
6577               (signal (car err) (cdr err))
6578             (ding))))))))
6579
6580 (defun gnus-article-read-summary-send-keys ()
6581   (interactive)
6582   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6583     (gnus-article-read-summary-keys)))
6584
6585 (defun gnus-article-describe-key (key)
6586   "Display documentation of the function invoked by KEY.
6587 KEY is a string or a vector."
6588   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6589                        (read-key-sequence "Describe key: "))))
6590   (gnus-article-check-buffer)
6591   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6592                                   gnus-article-read-summary-send-keys))
6593       (with-current-buffer gnus-article-current-summary
6594         (setq unread-command-events
6595               (if (featurep 'xemacs)
6596                   (append key nil)
6597                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6598                                         (list 'meta (- x 128))
6599                                       x))
6600                         key)))
6601         (let ((cursor-in-echo-area t)
6602               gnus-pick-mode)
6603           (describe-key (read-key-sequence nil t))))
6604     (describe-key key)))
6605
6606 (defun gnus-article-describe-key-briefly (key &optional insert)
6607   "Display documentation of the function invoked by KEY.
6608 KEY is a string or a vector."
6609   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6610                        (read-key-sequence "Describe key: "))
6611                      current-prefix-arg))
6612   (gnus-article-check-buffer)
6613   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6614                                   gnus-article-read-summary-send-keys))
6615       (with-current-buffer gnus-article-current-summary
6616         (setq unread-command-events
6617               (if (featurep 'xemacs)
6618                   (append key nil)
6619                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6620                                         (list 'meta (- x 128))
6621                                       x))
6622                         key)))
6623         (let ((cursor-in-echo-area t)
6624               gnus-pick-mode)
6625           (describe-key-briefly (read-key-sequence nil t) insert)))
6626     (describe-key-briefly key insert)))
6627
6628 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6629 (defvar gnus-agent-summary-mode)
6630 (defvar gnus-draft-mode)
6631 ;; Calling help-buffer will autoload help-mode.
6632 (defvar help-xref-stack-item)
6633 ;; Emacs 22 doesn't load it in the batch mode.
6634 (eval-when-compile
6635   (autoload 'help-buffer "help-mode"))
6636
6637 (defun gnus-article-describe-bindings (&optional prefix)
6638   "Show a list of all defined keys, and their definitions.
6639 The optional argument PREFIX, if non-nil, should be a key sequence;
6640 then we display only bindings that start with that prefix."
6641   (interactive)
6642   (gnus-article-check-buffer)
6643   (let ((keymap (copy-keymap gnus-article-mode-map))
6644         (map (copy-keymap gnus-article-send-map))
6645         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6646         parent agent draft)
6647     (define-key keymap "S" map)
6648     (define-key map [t] nil)
6649     (with-current-buffer gnus-article-current-summary
6650       (set-keymap-parent
6651        keymap
6652        (if (setq parent (keymap-parent gnus-article-mode-map))
6653            (prog1
6654                (setq parent (copy-keymap parent))
6655              (set-keymap-parent parent (current-local-map)))
6656          (current-local-map)))
6657       (set-keymap-parent map (key-binding "S"))
6658       (let (key def gnus-pick-mode)
6659         (while sumkeys
6660           (setq key (pop sumkeys))
6661           (cond ((and (vectorp key) (= (length key) 1)
6662                       (consp (setq def (aref key 0)))
6663                       (numberp (car def)) (numberp (cdr def)))
6664                  (when (< (max (car def) (cdr def)) 128)
6665                    (setq sumkeys
6666                          (append (mapcar
6667                                   #'vector
6668                                   (nreverse (gnus-uncompress-range def)))
6669                                  sumkeys))))
6670                 ((setq def (key-binding key))
6671                  (unless (eq def 'undefined)
6672                    (define-key keymap key def))))))
6673       (when (boundp 'gnus-agent-summary-mode)
6674         (setq agent gnus-agent-summary-mode))
6675       (when (boundp 'gnus-draft-mode)
6676         (setq draft gnus-draft-mode)))
6677     (with-temp-buffer
6678       (use-local-map keymap)
6679       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6680       (set (make-local-variable 'gnus-draft-mode) draft)
6681       (describe-bindings prefix))
6682     (let ((item `((lambda (prefix)
6683                     (with-current-buffer ,(current-buffer)
6684                       (gnus-article-describe-bindings prefix)))
6685                   ,prefix)))
6686       (with-current-buffer (let (help-xref-following) (help-buffer))
6687         (setq help-xref-stack-item item)))))
6688
6689 (defun gnus-article-reply-with-original (&optional wide)
6690   "Start composing a reply mail to the current message.
6691 The text in the region will be yanked.  If the region isn't active,
6692 the entire article will be yanked."
6693   (interactive)
6694   (let ((article (cdr gnus-article-current))
6695         contents)
6696     (if (not (gnus-region-active-p))
6697         (with-current-buffer gnus-summary-buffer
6698           (gnus-summary-reply (list (list article)) wide))
6699       (setq contents (buffer-substring (point) (mark t)))
6700       ;; Deactivate active regions.
6701       (when (and (boundp 'transient-mark-mode)
6702                  transient-mark-mode)
6703         (setq mark-active nil))
6704       (with-current-buffer gnus-summary-buffer
6705         (gnus-summary-reply
6706          (list (list article contents)) wide)))))
6707
6708 (defun gnus-article-wide-reply-with-original ()
6709   "Start composing a wide reply mail to the current message.
6710 The text in the region will be yanked.  If the region isn't active,
6711 the entire article will be yanked."
6712   (interactive)
6713   (gnus-article-reply-with-original t))
6714
6715 (defun gnus-article-followup-with-original ()
6716   "Compose a followup to the current article.
6717 The text in the region will be yanked.  If the region isn't active,
6718 the entire article will be yanked."
6719   (interactive)
6720   (let ((article (cdr gnus-article-current))
6721         contents)
6722       (if (not (gnus-region-active-p))
6723           (with-current-buffer gnus-summary-buffer
6724             (gnus-summary-followup (list (list article))))
6725         (setq contents (buffer-substring (point) (mark t)))
6726         ;; Deactivate active regions.
6727         (when (and (boundp 'transient-mark-mode)
6728                    transient-mark-mode)
6729           (setq mark-active nil))
6730         (with-current-buffer gnus-summary-buffer
6731           (gnus-summary-followup
6732            (list (list article contents)))))))
6733
6734 (defun gnus-article-hide (&optional arg force)
6735   "Hide all the gruft in the current article.
6736 This means that signatures, cited text and (some) headers will be
6737 hidden.
6738 If given a prefix, show the hidden text instead."
6739   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6740   (gnus-article-hide-headers arg)
6741   (gnus-article-hide-list-identifiers arg)
6742   (gnus-article-hide-citation-maybe arg force)
6743   (gnus-article-hide-signature arg))
6744
6745 (defun gnus-check-group-server ()
6746   ;; Make sure the connection to the server is alive.
6747   (unless (gnus-server-opened
6748            (gnus-find-method-for-group gnus-newsgroup-name))
6749     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6750     (gnus-request-group gnus-newsgroup-name t)))
6751
6752 (eval-when-compile
6753   (autoload 'nneething-get-file-name "nneething"))
6754
6755 (defun gnus-request-article-this-buffer (article group)
6756   "Get an article and insert it into this buffer."
6757   (let (do-update-line sparse-header)
6758     (prog1
6759         (save-excursion
6760           (erase-buffer)
6761           (gnus-kill-all-overlays)
6762           (setq group (or group gnus-newsgroup-name))
6763
6764           ;; Using `gnus-request-article' directly will insert the article into
6765           ;; `nntp-server-buffer' - so we'll save some time by not having to
6766           ;; copy it from the server buffer into the article buffer.
6767
6768           ;; We only request an article by message-id when we do not have the
6769           ;; headers for it, so we'll have to get those.
6770           (when (stringp article)
6771             (gnus-read-header article))
6772
6773           ;; If the article number is negative, that means that this article
6774           ;; doesn't belong in this newsgroup (possibly), so we find its
6775           ;; message-id and request it by id instead of number.
6776           (when (and (numberp article)
6777                      gnus-summary-buffer
6778                      (get-buffer gnus-summary-buffer)
6779                      (gnus-buffer-exists-p gnus-summary-buffer))
6780             (with-current-buffer gnus-summary-buffer
6781               (let ((header (gnus-summary-article-header article)))
6782                 (when (< article 0)
6783                   (cond
6784                    ((memq article gnus-newsgroup-sparse)
6785                     ;; This is a sparse gap article.
6786                     (setq do-update-line article)
6787                     (setq article (mail-header-id header))
6788                     (setq sparse-header (gnus-read-header article))
6789                     (setq gnus-newsgroup-sparse
6790                           (delq article gnus-newsgroup-sparse)))
6791                    ((vectorp header)
6792                     ;; It's a real article.
6793                     (setq article (mail-header-id header)))
6794                    (t
6795                     ;; It is an extracted pseudo-article.
6796                     (setq article 'pseudo)
6797                     (gnus-request-pseudo-article header))))
6798
6799                 (let ((method (gnus-find-method-for-group
6800                                gnus-newsgroup-name)))
6801                   (when (and (eq (car method) 'nneething)
6802                              (vectorp header))
6803                     (let ((dir (nneething-get-file-name
6804                                 (mail-header-id header))))
6805                       (when (and (stringp dir)
6806                                  (file-directory-p dir))
6807                         (setq article 'nneething)
6808                         (gnus-group-enter-directory dir))))))))
6809
6810           (cond
6811            ;; Refuse to select canceled articles.
6812            ((and (numberp article)
6813                  gnus-summary-buffer
6814                  (get-buffer gnus-summary-buffer)
6815                  (gnus-buffer-exists-p gnus-summary-buffer)
6816                  (eq (cdr (with-current-buffer gnus-summary-buffer
6817                             (assq article gnus-newsgroup-reads)))
6818                      gnus-canceled-mark))
6819             nil)
6820            ;; We first check `gnus-original-article-buffer'.
6821            ((and (get-buffer gnus-original-article-buffer)
6822                  (numberp article)
6823                  (with-current-buffer gnus-original-article-buffer
6824                    (and (equal (car gnus-original-article) group)
6825                         (eq (cdr gnus-original-article) article))))
6826             ;; `insert-buffer-substring' would incorrectly use the
6827             ;; equivalent of string-make-multibyte which amount to decoding
6828             ;; with locale-coding-system, causing failure of
6829             ;; subsequent decoding.
6830             (insert (mm-string-to-multibyte
6831                      (with-current-buffer gnus-original-article-buffer
6832                        (buffer-substring (point-min) (point-max)))))
6833             'article)
6834            ;; Check the backlog.
6835            ((and gnus-keep-backlog
6836                  (gnus-backlog-request-article group article (current-buffer)))
6837             'article)
6838            ;; Check asynchronous pre-fetch.
6839            ((gnus-async-request-fetched-article group article (current-buffer))
6840             (gnus-async-prefetch-next group article gnus-summary-buffer)
6841             (when (and (numberp article) gnus-keep-backlog)
6842               (gnus-backlog-enter-article group article (current-buffer)))
6843             'article)
6844            ;; Check the cache.
6845            ((and gnus-use-cache
6846                  (numberp article)
6847                  (gnus-cache-request-article article group))
6848             'article)
6849            ;; Check the agent cache.
6850            ((gnus-agent-request-article article group)
6851             'article)
6852            ;; Get the article and put into the article buffer.
6853            ((or (stringp article)
6854                 (numberp article))
6855             (let ((gnus-override-method gnus-override-method)
6856                   (methods (and (stringp article)
6857                                 (with-current-buffer gnus-summary-buffer
6858                                   (gnus-refer-article-methods))))
6859                   (backend (car (gnus-find-method-for-group
6860                                  gnus-newsgroup-name)))
6861                   result
6862                   (inhibit-read-only t))
6863               (when (and (null gnus-override-method)
6864                          methods)
6865                 (setq gnus-override-method (pop methods)))
6866               (while (not result)
6867                 (erase-buffer)
6868                 (gnus-kill-all-overlays)
6869                 (let ((gnus-newsgroup-name group))
6870                   (gnus-check-group-server))
6871                 (cond
6872                  ((gnus-request-article article group (current-buffer))
6873                   (when (numberp article)
6874                     (gnus-async-prefetch-next group article
6875                                               gnus-summary-buffer)
6876                     (when gnus-keep-backlog
6877                       (gnus-backlog-enter-article
6878                        group article (current-buffer)))
6879                     (when (and gnus-agent
6880                                (gnus-agent-group-covered-p group))
6881                       (gnus-agent-store-article article group)))
6882                   (setq result 'article))
6883                  (methods
6884                   (setq gnus-override-method (pop methods)))
6885                  ((not (string-match "^400 "
6886                                      (nnheader-get-report backend)))
6887                   ;; If we get 400 server disconnect, reconnect and
6888                   ;; retry; otherwise, assume the article has expired.
6889                   (setq result 'done))))
6890               (and (eq result 'article) 'article)))
6891            ;; It was a pseudo.
6892            (t article)))
6893
6894       ;; Associate this article with the current summary buffer.
6895       (setq gnus-article-current-summary gnus-summary-buffer)
6896
6897       ;; Take the article from the original article buffer
6898       ;; and place it in the buffer it's supposed to be in.
6899       (when (and (get-buffer gnus-article-buffer)
6900                  (equal (buffer-name (current-buffer))
6901                         (buffer-name (get-buffer gnus-article-buffer))))
6902         (save-excursion
6903           (if (get-buffer gnus-original-article-buffer)
6904               (set-buffer gnus-original-article-buffer)
6905             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6906             (buffer-disable-undo)
6907             (setq major-mode 'gnus-original-article-mode)
6908             (setq buffer-read-only t))
6909           (let ((inhibit-read-only t))
6910             (erase-buffer)
6911             (insert-buffer-substring gnus-article-buffer))
6912           (setq gnus-original-article (cons group article)))
6913
6914         ;; Decode charsets.
6915         (run-hooks 'gnus-article-decode-hook)
6916         ;; Mark article as decoded or not.
6917         (setq gnus-article-decoded-p gnus-article-decode-hook))
6918
6919       ;; Update sparse articles.
6920       (when (and do-update-line
6921                  (or (numberp article)
6922                      (stringp article)))
6923         (let ((buf (current-buffer)))
6924           (set-buffer gnus-summary-buffer)
6925           (gnus-summary-update-article do-update-line sparse-header)
6926           (gnus-summary-goto-subject do-update-line nil t)
6927           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6928                             (point))
6929           (set-buffer buf))))))
6930
6931 (defun gnus-block-private-groups (group)
6932   (if (gnus-news-group-p group)
6933       ;; Block nothing in news groups.
6934       nil
6935     ;; Block everything anywhere else.
6936     "."))
6937
6938 (defun gnus-blocked-images ()
6939   (if (functionp gnus-blocked-images)
6940       (funcall gnus-blocked-images gnus-newsgroup-name)
6941     gnus-blocked-images))
6942
6943 ;;;
6944 ;;; Article editing
6945 ;;;
6946
6947 (defcustom gnus-article-edit-mode-hook nil
6948   "Hook run in article edit mode buffers."
6949   :group 'gnus-article-various
6950   :type 'hook)
6951
6952 (defvar gnus-article-edit-done-function nil)
6953
6954 (defvar gnus-article-edit-mode-map nil)
6955
6956 ;; Should we be using derived.el for this?
6957 (unless gnus-article-edit-mode-map
6958   (setq gnus-article-edit-mode-map (make-keymap))
6959   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6960
6961   (gnus-define-keys gnus-article-edit-mode-map
6962     "\C-c?"    describe-mode
6963     "\C-c\C-c" gnus-article-edit-done
6964     "\C-c\C-k" gnus-article-edit-exit
6965     "\C-c\C-f\C-t" message-goto-to
6966     "\C-c\C-f\C-o" message-goto-from
6967     "\C-c\C-f\C-b" message-goto-bcc
6968     ;;"\C-c\C-f\C-w" message-goto-fcc
6969     "\C-c\C-f\C-c" message-goto-cc
6970     "\C-c\C-f\C-s" message-goto-subject
6971     "\C-c\C-f\C-r" message-goto-reply-to
6972     "\C-c\C-f\C-n" message-goto-newsgroups
6973     "\C-c\C-f\C-d" message-goto-distribution
6974     "\C-c\C-f\C-f" message-goto-followup-to
6975     "\C-c\C-f\C-m" message-goto-mail-followup-to
6976     "\C-c\C-f\C-k" message-goto-keywords
6977     "\C-c\C-f\C-u" message-goto-summary
6978     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6979     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6980     "\C-c\C-b" message-goto-body
6981     "\C-c\C-i" message-goto-signature
6982
6983     "\C-c\C-t" message-insert-to
6984     "\C-c\C-n" message-insert-newsgroups
6985     "\C-c\C-o" message-sort-headers
6986     "\C-c\C-e" message-elide-region
6987     "\C-c\C-v" message-delete-not-region
6988     "\C-c\C-z" message-kill-to-signature
6989     "\M-\r" message-newline-and-reformat
6990     "\C-c\C-a" mml-attach-file
6991     "\C-a" message-beginning-of-line
6992     "\t" message-tab
6993     "\M-;" comment-region)
6994
6995   (gnus-define-keys (gnus-article-edit-wash-map
6996                      "\C-c\C-w" gnus-article-edit-mode-map)
6997     "f" gnus-article-edit-full-stops))
6998
6999 (easy-menu-define
7000   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7001   '("Field"
7002     ["Fetch To" message-insert-to t]
7003     ["Fetch Newsgroups" message-insert-newsgroups t]
7004     "----"
7005     ["To" message-goto-to t]
7006     ["From" message-goto-from t]
7007     ["Subject" message-goto-subject t]
7008     ["Cc" message-goto-cc t]
7009     ["Reply-To" message-goto-reply-to t]
7010     ["Summary" message-goto-summary t]
7011     ["Keywords" message-goto-keywords t]
7012     ["Newsgroups" message-goto-newsgroups t]
7013     ["Followup-To" message-goto-followup-to t]
7014     ["Mail-Followup-To" message-goto-mail-followup-to t]
7015     ["Distribution" message-goto-distribution t]
7016     ["Body" message-goto-body t]
7017     ["Signature" message-goto-signature t]))
7018
7019 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7020   "Major mode for editing articles.
7021 This is an extended text-mode.
7022
7023 \\{gnus-article-edit-mode-map}"
7024   (make-local-variable 'gnus-article-edit-done-function)
7025   (make-local-variable 'gnus-prev-winconf)
7026   (set (make-local-variable 'font-lock-defaults)
7027        '(message-font-lock-keywords t))
7028   (set (make-local-variable 'mail-header-separator) "")
7029   (set (make-local-variable 'gnus-article-edit-mode) t)
7030   (easy-menu-add message-mode-field-menu message-mode-map)
7031   (mml-mode)
7032   (setq buffer-read-only nil)
7033   (buffer-enable-undo)
7034   (widen))
7035
7036 (defun gnus-article-edit (&optional force)
7037   "Edit the current article.
7038 This will have permanent effect only in mail groups.
7039 If FORCE is non-nil, allow editing of articles even in read-only
7040 groups."
7041   (interactive "P")
7042   (when (and (not force)
7043              (gnus-group-read-only-p))
7044     (error "The current newsgroup does not support article editing"))
7045   (gnus-article-date-original)
7046   (gnus-article-edit-article
7047    'ignore
7048    `(lambda (no-highlight)
7049       'ignore
7050       (gnus-summary-edit-article-done
7051        ,(or (mail-header-references gnus-current-headers) "")
7052        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7053
7054 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7055   "Start editing the contents of the current article buffer."
7056   (let ((winconf (current-window-configuration)))
7057     (set-buffer gnus-article-buffer)
7058     (let ((message-auto-save-directory
7059            ;; Don't associate the article buffer with a draft file.
7060            nil))
7061       (gnus-article-edit-mode))
7062     (funcall start-func)
7063     (set-buffer-modified-p nil)
7064     (gnus-configure-windows 'edit-article)
7065     (setq gnus-article-edit-done-function exit-func)
7066     (setq gnus-prev-winconf winconf)
7067     (unless quiet
7068       (gnus-message 6 "C-c C-c to end edits"))))
7069
7070 (defun gnus-article-edit-done (&optional arg)
7071   "Update the article edits and exit."
7072   (interactive "P")
7073   (let ((func gnus-article-edit-done-function)
7074         (buf (current-buffer))
7075         (start (window-start))
7076         (p (point))
7077         (winconf gnus-prev-winconf))
7078     (widen) ;; Widen it in case that users narrowed the buffer.
7079     (funcall func arg)
7080     (set-buffer buf)
7081     ;; The cache and backlog have to be flushed somewhat.
7082     (when gnus-keep-backlog
7083       (gnus-backlog-remove-article
7084        (car gnus-article-current) (cdr gnus-article-current)))
7085     ;; Flush original article as well.
7086     (gnus-flush-original-article-buffer)
7087     (when gnus-use-cache
7088       (gnus-cache-update-article
7089        (car gnus-article-current) (cdr gnus-article-current)))
7090     ;; We remove all text props from the article buffer.
7091     (kill-all-local-variables)
7092     (set-text-properties (point-min) (point-max) nil)
7093     (gnus-article-mode)
7094     (set-window-configuration winconf)
7095     (set-buffer buf)
7096     (set-window-start (get-buffer-window buf) start)
7097     (set-window-point (get-buffer-window buf) (point)))
7098   (gnus-summary-show-article))
7099
7100 (defun gnus-flush-original-article-buffer ()
7101   (when (get-buffer gnus-original-article-buffer)
7102     (with-current-buffer gnus-original-article-buffer
7103       (setq gnus-original-article nil))))
7104
7105 (defun gnus-article-edit-exit ()
7106   "Exit the article editing without updating."
7107   (interactive)
7108   (when (or (not (buffer-modified-p))
7109             (yes-or-no-p "Article modified; kill anyway? "))
7110     (let ((curbuf (current-buffer))
7111           (p (point))
7112           (window-start (window-start)))
7113       (erase-buffer)
7114       (if (gnus-buffer-live-p gnus-original-article-buffer)
7115           (insert-buffer-substring gnus-original-article-buffer))
7116       (let ((winconf gnus-prev-winconf))
7117         (kill-all-local-variables)
7118         (gnus-article-mode)
7119         (set-window-configuration winconf)
7120         ;; Tippy-toe some to make sure that point remains where it was.
7121         (save-current-buffer
7122           (set-buffer curbuf)
7123           (set-window-start (get-buffer-window (current-buffer)) window-start)
7124           (goto-char p))))
7125     (gnus-summary-show-article)))
7126
7127 (defun gnus-article-edit-full-stops ()
7128   "Interactively repair spacing at end of sentences."
7129   (interactive)
7130   (save-excursion
7131     (goto-char (point-min))
7132     (search-forward-regexp "^$" nil t)
7133     (let ((case-fold-search nil))
7134       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7135
7136 ;;;
7137 ;;; Article highlights
7138 ;;;
7139
7140 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7141
7142 ;;; Internal Variables:
7143
7144 (defcustom gnus-button-url-regexp
7145   (concat
7146    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7147    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7148    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7149    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7150        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7151              (punct "!?:;.,"))
7152          (concat
7153           "\\(?:"
7154           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7155           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7156           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7157           "\\|"
7158           "[" chars punct     "]+" "[" chars "]"
7159           "\\)"))
7160      (concat ;; XEmacs 21.4 doesn't support POSIX.
7161       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7162       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7163    "\\)")
7164   "Regular expression that matches URLs."
7165   :group 'gnus-article-buttons
7166   :type 'regexp)
7167
7168 (defcustom gnus-button-valid-fqdn-regexp
7169   message-valid-fqdn-regexp
7170   "Regular expression that matches a valid FQDN."
7171   :version "22.1"
7172   :group 'gnus-article-buttons
7173   :type 'regexp)
7174
7175 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7176 (defcustom gnus-button-valid-localpart-regexp
7177   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7178   "Regular expression that matches a localpart of mail addresses or MIDs."
7179   :version "22.1"
7180   :group 'gnus-article-buttons
7181   :type 'regexp)
7182
7183 (defcustom gnus-button-man-handler 'manual-entry
7184   "Function to use for displaying man pages.
7185 The function must take at least one argument with a string naming the
7186 man page."
7187   :version "22.1"
7188   :type '(choice (function-item :tag "Man" manual-entry)
7189                  (function-item :tag "Woman" woman)
7190                  (function :tag "Other"))
7191   :group 'gnus-article-buttons)
7192
7193 (defcustom gnus-button-mid-or-mail-regexp
7194   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7195           gnus-button-valid-fqdn-regexp
7196           ">?\\)\\b")
7197   "Regular expression that matches a message ID or a mail address."
7198   :version "22.1"
7199   :group 'gnus-article-buttons
7200   :type 'regexp)
7201
7202 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7203   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7204 Strings like this can be either a message ID or a mail address.  If it is one
7205 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7206 message ID or a mail address, respectively.  If this variable is set to the
7207 symbol `ask', always query the user what do do.  If it is a function, this
7208 function will be called with the string as its only argument.  The function
7209 must return `mid', `mail', `invalid' or `ask'."
7210   :version "22.1"
7211   :group 'gnus-article-buttons
7212   :type '(choice (function-item :tag "Heuristic function"
7213                                 gnus-button-mid-or-mail-heuristic)
7214                  (const ask)
7215                  (const mid)
7216                  (const mail)))
7217
7218 (defcustom gnus-button-mid-or-mail-heuristic-alist
7219   '((-10.0 . ".+\\$.+@")
7220     (-10.0 . "#")
7221     (-10.0 . "\\*")
7222     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7223     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7224     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7225     (-1.0  . "^[^a-z]+@")
7226     ;;
7227     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7228     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7229     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7230     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7231     ;;
7232     (-2.0  . "^[0-9]")
7233     (-1.0  . "^[0-9][0-9]")
7234     ;;
7235     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7236     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7237     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7238     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7239     ;;
7240     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7241     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7242     ;;       "[0-9]{8,}.*\@"
7243     (-3.0
7244      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7245     ;; "[0-9]{12,}.*\@"
7246     ;; compensation for TDMA dated mail addresses:
7247     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7248     ;;
7249     (-20.0 . "\\.fsf@") ;; Gnus
7250     (-20.0 . "^slrn")
7251     (-20.0 . "^Pine")
7252     (-20.0 . "^alpine\\.")
7253     (-20.0 . "_-_") ;; Subject change in thread
7254     ;;
7255     (-20.0 . "\\.ln@") ;; leafnode
7256     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7257     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7258     ;;
7259     ;; (5.0 . "") ;; $local_part_len <= 7
7260     (10.0  . "^[^0-9]+@")
7261     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7262     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7263     (3.0   . "\@stud")
7264     ;;
7265     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7266     ;;
7267     (0.5   . "^[A-Z][a-z]")
7268     (0.5   . "^[A-Z][a-z][a-z]")
7269     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7270     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7271   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7272
7273 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7274 address.  The REGEXP is processed with `case-fold-search' set to nil."
7275   :version "22.1"
7276   :group 'gnus-article-buttons
7277   :type '(repeat (cons (number :tag "Rate")
7278                        (regexp :tag "Regexp"))))
7279
7280 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7281   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7282 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7283 address, `ask' if unsure and `invalid' if the string is invalid."
7284   (let ((case-fold-search nil)
7285         (list gnus-button-mid-or-mail-heuristic-alist)
7286         (result 0) rate regexp lpartlen elem)
7287     (setq lpartlen
7288           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7289     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7290     ;; Certain special cases...
7291     (when (string-match
7292            (concat
7293             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7294             "^[0-9]+\\.[0-9]+@compuserve\\|"
7295             "@public\\.gmane\\.org")
7296            mid-or-mail)
7297       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7298       (setq result 'mail))
7299     (when (string-match "@.*@\\| " mid-or-mail)
7300       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7301       (setq result 'invalid))
7302     ;; Nothing more to do, if result is not a number here...
7303     (when (numberp result)
7304       (while list
7305         (setq elem (car list)
7306               rate (car elem)
7307               regexp (cdr elem)
7308               list (cdr list))
7309         (when (string-match regexp mid-or-mail)
7310           (setq result (+ result rate))
7311           (gnus-message
7312            9 "`%s' matched `%s', rate `%s', result `%s'."
7313            mid-or-mail regexp rate result)))
7314       (when (<= lpartlen 7)
7315         (setq result (+ result 5.0))
7316         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7317                       mid-or-mail result))
7318       (when (>= lpartlen 12)
7319         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7320         (cond
7321          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7322           ;; Long local part should contain realname if e-mail address,
7323           ;; too many digits: message-id.
7324           ;; $score -= 5.0 + 0.1 * $local_part_len;
7325           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7326           (setq result (+ result rate))
7327           (gnus-message
7328            9 "Many digits in `%s', rate `%s', result `%s'."
7329            mid-or-mail rate result))
7330          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7331                         mid-or-mail)
7332           ;; Too few vowels [^aeiouy]{4,}.*\@
7333           (setq result (+ result -5.0))
7334           (gnus-message
7335            9 "Few vowels in `%s', rate `%s', result `%s'."
7336            mid-or-mail -5.0 result))
7337          (t
7338           (setq result (+ result 5.0))
7339           (gnus-message
7340            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7341     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7342     ;; Maybe we should make this a customizable alist: (condition . 'result)
7343     (cond
7344      ((symbolp result) result)
7345      ;; Now convert number into proper results:
7346      ((< result -10.0) 'mid)
7347      ((> result  10.0) 'mail)
7348      (t 'ask))))
7349
7350 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7351   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7352          (url-mid (concat "news" ":" mid-or-mail))
7353          (url-mailto (concat "mailto" ":" mid-or-mail)))
7354     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7355     (when (fboundp pref)
7356       (setq guessed
7357             ;; get rid of surrounding angles...
7358             (funcall pref
7359                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7360       (if (or (eq 'mid guessed) (eq 'mail guessed))
7361           (setq pref guessed)
7362         (setq pref 'ask)))
7363     (if (eq pref 'ask)
7364         (save-window-excursion
7365           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7366               (setq pref 'mail)
7367             (setq pref 'mid))))
7368     (cond ((eq pref 'mid)
7369            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7370            (gnus-button-handle-news url-mid))
7371           ((eq pref 'mail)
7372            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7373            (gnus-url-mailto url-mailto))
7374           (t (gnus-message 3 "Invalid string.")))))
7375
7376 (defun gnus-button-handle-custom (fun arg)
7377   "Call function FUN on argument ARG.
7378 Both FUN and ARG are supposed to be strings.  ARG will be passed
7379 as a symbol to FUN."
7380   (funcall (intern fun)
7381            (if (string-match "^customize-apropos" fun)
7382                arg
7383              (intern arg))))
7384
7385 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7386
7387 (defun gnus-button-handle-describe-function (url)
7388   "Call `describe-function' when pushing the corresponding URL button."
7389   (describe-function
7390    (intern
7391     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7392
7393 (defun gnus-button-handle-describe-variable (url)
7394   "Call `describe-variable' when pushing the corresponding URL button."
7395   (describe-variable
7396    (intern
7397     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7398
7399 (defun gnus-button-handle-symbol (url)
7400 "Display help on variable or function.
7401 Calls `describe-variable' or `describe-function'."
7402   (let ((sym (intern url)))
7403     (cond
7404      ((fboundp sym) (describe-function sym))
7405      ((boundp sym) (describe-variable sym))
7406      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7407
7408 (defun gnus-button-handle-describe-key (url)
7409   "Call `describe-key' when pushing the corresponding URL button."
7410   (let* ((key-string
7411           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7412          (keys (ignore-errors (eval `(kbd ,key-string)))))
7413     (if keys
7414         (describe-key keys)
7415       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7416
7417 (defun gnus-button-handle-apropos (url)
7418   "Call `apropos' when pushing the corresponding URL button."
7419   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7420
7421 (defun gnus-button-handle-apropos-command (url)
7422   "Call `apropos' when pushing the corresponding URL button."
7423   (apropos-command
7424    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7425
7426 (defun gnus-button-handle-apropos-variable (url)
7427   "Call `apropos' when pushing the corresponding URL button."
7428   (funcall
7429    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7430    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7431
7432 (defun gnus-button-handle-apropos-documentation (url)
7433   "Call `apropos' when pushing the corresponding URL button."
7434   (funcall
7435    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7436    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7437
7438 (defun gnus-button-handle-library (url)
7439   "Call `locate-library' when pushing the corresponding URL button."
7440   (gnus-message 9 "url=`%s'" url)
7441   (let* ((lib (locate-library url))
7442          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7443     (if (not lib)
7444         (gnus-message 1 "Cannot locale library `%s'." url)
7445       (find-file-read-only file))))
7446
7447 (defcustom gnus-button-man-level 5
7448   "*Integer that says how many man-related buttons Gnus will show.
7449 The higher the number, the more buttons will appear and the more false
7450 positives are possible.  Note that you can set this variable local to
7451 specific groups.  Setting it higher in Unix groups is probably a good idea.
7452 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7453 how to set variables in specific groups."
7454   :version "22.1"
7455   :group 'gnus-article-buttons
7456   :link '(custom-manual "(gnus)Group Parameters")
7457   :type 'integer)
7458
7459 (defcustom gnus-button-emacs-level 5
7460   "*Integer that says how many emacs-related buttons Gnus will show.
7461 The higher the number, the more buttons will appear and the more false
7462 positives are possible.  Note that you can set this variable local to
7463 specific groups.  Setting it higher in Emacs or Gnus related groups is
7464 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7465 `gnus-parameters' on how to set variables in specific groups."
7466   :version "22.1"
7467   :group 'gnus-article-buttons
7468   :link '(custom-manual "(gnus)Group Parameters")
7469   :type 'integer)
7470
7471 (defcustom gnus-button-message-level 5
7472   "*Integer that says how many buttons for news or mail messages will appear.
7473 The higher the number, the more buttons will appear and the more false
7474 positives are possible."
7475   ;; mail addresses, MIDs, URLs for news, ...
7476   :version "22.1"
7477   :group 'gnus-article-buttons
7478   :type 'integer)
7479
7480 (defcustom gnus-button-browse-level 5
7481   "*Integer that says how many buttons for browsing will appear.
7482 The higher the number, the more buttons will appear and the more false
7483 positives are possible."
7484   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7485   :version "22.1"
7486   :group 'gnus-article-buttons
7487   :type 'integer)
7488
7489 (defcustom gnus-button-alist
7490   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7491      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7492     ((concat "\\b\\(nntp\\|news\\):\\("
7493              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7494      0 t gnus-button-handle-news 2)
7495     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7496      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7497     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7498      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7499     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7500     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7501      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7502     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7503      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7504     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7505      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7506     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7507      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7508     ;; RFC 2368 (The mailto URL scheme)
7509     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7510      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7511     ("\\bmailto:\\([^ \n\t]+\\)"
7512      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7513     ;; Info Konqueror style <info:/foo/bar baz>.
7514     ;; Must come before " Gnus home-grown style".
7515     ("\\binfo://?\\([^'\">\n\t]+\\)"
7516      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7517    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7518     ("\\binfo://\\([^'\">\n\t ]+\\)"
7519      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7520     ;; Info GNOME style <info:foo#bar_baz>
7521     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7522      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7523     ;; Info KDE style <info:(foo)bar baz>
7524     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7525      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7526     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7527      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7528     ("\\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,]*\\)\\)?"
7529      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7530      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7531     ;; This is custom
7532     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7533      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7534     ;; Emacs help commands
7535     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7536      ;; regexp doesn't match arguments containing ` '.
7537      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7538     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7539      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7540     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7541      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7542     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7543      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7544     ;; The following entries may lead to many false positives so don't enable
7545     ;; them by default (use a high button level).
7546     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7547      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7548      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7549     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7550      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7551     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7552      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7553     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7554      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7555     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7556      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7557     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7558      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7559     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7560      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7561     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7562      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7563     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7564      ;; Unlike the other regexps we really have to require quoting
7565      ;; here to determine where it ends.
7566      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7567     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7568     ("<URL: *\\([^\n<>]*\\)>"
7569      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7570     ;; RFC 2396 (2.4.3., delims) ...
7571     ("\"URL: *\\([^\n\"]*\\)\""
7572      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7573     ;; Raw URLs.
7574     (gnus-button-url-regexp
7575      0 (>= gnus-button-browse-level 0) browse-url 0)
7576     ;; man pages
7577     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7578      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7579      gnus-button-handle-man 1)
7580     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7581     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7582      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7583      gnus-button-handle-man 1)
7584     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7585     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7586     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7587      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7588     ;; Recognizing patches to .el files.  This is somewhat obscure,
7589     ;; but considering the percentage of Gnus users who hack Emacs
7590     ;; Lisp files...
7591     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7592      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7593     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7594      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7595     ;; MID or mail: To avoid too many false positives we don't try to catch
7596     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7597     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7598     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7599     ;; so that non-ambiguous entries (see above) match first.
7600     (gnus-button-mid-or-mail-regexp
7601      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7602   "*Alist of regexps matching buttons in article bodies.
7603
7604 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7605 REGEXP: is the string (case insensitive) matching text around the button (can
7606 also be Lisp expression evaluating to a string),
7607 BUTTON: is the number of the regexp grouping actually matching the button,
7608 FORM: is a Lisp expression which must eval to true for the button to
7609 be added,
7610 CALLBACK: is the function to call when the user push this button, and each
7611 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7612
7613 CALLBACK can also be a variable, in that case the value of that
7614 variable it the real callback function."
7615   :group 'gnus-article-buttons
7616   :type '(repeat (list (choice regexp variable sexp)
7617                        (integer :tag "Button")
7618                        (sexp :tag "Form")
7619                        (function :tag "Callback")
7620                        (repeat :tag "Par"
7621                                :inline t
7622                                (integer :tag "Regexp group")))))
7623 (put 'gnus-button-alist 'risky-local-variable t)
7624
7625 (defcustom gnus-header-button-alist
7626   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7627      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7628     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7629      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7630     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7631      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7632     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7633      0 (>= gnus-button-browse-level 0) browse-url 0)
7634     ("^Subject:" gnus-button-url-regexp
7635      0 (>= gnus-button-browse-level 0) browse-url 0)
7636     ("^[^:]+:" gnus-button-url-regexp
7637      0 (>= gnus-button-browse-level 0) browse-url 0)
7638     ("^OpenPGP:.*url=" gnus-button-url-regexp
7639      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7640     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7641      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7642     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7643      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7644   "*Alist of headers and regexps to match buttons in article heads.
7645
7646 This alist is very similar to `gnus-button-alist', except that each
7647 alist has an additional HEADER element first in each entry:
7648
7649 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7650
7651 HEADER is a regexp to match a header.  For a fuller explanation, see
7652 `gnus-button-alist'."
7653   :group 'gnus-article-buttons
7654   :group 'gnus-article-headers
7655   :type '(repeat (list (regexp :tag "Header")
7656                        (choice regexp variable)
7657                        (integer :tag "Button")
7658                        (sexp :tag "Form")
7659                        (function :tag "Callback")
7660                        (repeat :tag "Par"
7661                                :inline t
7662                                (integer :tag "Regexp group")))))
7663 (put 'gnus-header-button-alist 'risky-local-variable t)
7664
7665 ;;; Commands:
7666
7667 (defun gnus-article-push-button (event)
7668   "Check text under the mouse pointer for a callback function.
7669 If the text under the mouse pointer has a `gnus-callback' property,
7670 call it with the value of the `gnus-data' text property."
7671   (interactive "e")
7672   (set-buffer (window-buffer (posn-window (event-start event))))
7673   (let* ((pos (posn-point (event-start event)))
7674          (data (get-text-property pos 'gnus-data))
7675          (fun (get-text-property pos 'gnus-callback)))
7676     (goto-char pos)
7677     (when fun
7678       (funcall fun data))))
7679
7680 (defun gnus-article-press-button ()
7681   "Check text at point for a callback function.
7682 If the text at point has a `gnus-callback' property,
7683 call it with the value of the `gnus-data' text property."
7684   (interactive)
7685   (let ((data (get-text-property (point) 'gnus-data))
7686         (fun (get-text-property (point) 'gnus-callback)))
7687     (when fun
7688       (funcall fun data))))
7689
7690 (defun gnus-article-highlight (&optional force)
7691   "Highlight current article.
7692 This function calls `gnus-article-highlight-headers',
7693 `gnus-article-highlight-citation',
7694 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7695 do the highlighting.  See the documentation for those functions."
7696   (interactive (list 'force))
7697   (gnus-article-highlight-headers)
7698   (gnus-article-highlight-citation force)
7699   (gnus-article-highlight-signature)
7700   (gnus-article-add-buttons)
7701   (gnus-article-add-buttons-to-head))
7702
7703 (defun gnus-article-highlight-some (&optional force)
7704   "Highlight current article.
7705 This function calls `gnus-article-highlight-headers',
7706 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7707 do the highlighting.  See the documentation for those functions."
7708   (interactive (list 'force))
7709   (gnus-article-highlight-headers)
7710   (gnus-article-highlight-signature)
7711   (gnus-article-add-buttons))
7712
7713 (defun gnus-article-highlight-headers ()
7714   "Highlight article headers as specified by `gnus-header-face-alist'."
7715   (interactive)
7716   (gnus-with-article-headers
7717     (let (regexp header-face field-face from hpoints fpoints)
7718       (dolist (entry gnus-header-face-alist)
7719         (goto-char (point-min))
7720         (setq regexp (concat "^\\("
7721                              (if (string-equal "" (nth 0 entry))
7722                                  "[^\t ]"
7723                                (nth 0 entry))
7724                              "\\)")
7725               header-face (nth 1 entry)
7726               field-face (nth 2 entry))
7727         (while (and (re-search-forward regexp nil t)
7728                     (not (eobp)))
7729           (beginning-of-line)
7730           (setq from (point))
7731           (unless (search-forward ":" nil t)
7732             (forward-char 1))
7733           (when (and header-face
7734                      (not (memq (point) hpoints)))
7735             (push (point) hpoints)
7736             (gnus-put-text-property from (point) 'face header-face))
7737           (when (and field-face
7738                      (not (memq (setq from (point)) fpoints)))
7739             (push from fpoints)
7740             (if (re-search-forward "^[^ \t]" nil t)
7741                 (forward-char -2)
7742               (goto-char (point-max)))
7743             (gnus-put-text-property from (point) 'face field-face)))))))
7744
7745 (defun gnus-article-highlight-signature ()
7746   "Highlight the signature in an article.
7747 It does this by highlighting everything after
7748 `gnus-signature-separator' using the face `gnus-signature'."
7749   (interactive)
7750   (gnus-with-article-buffer
7751     (let ((inhibit-point-motion-hooks t))
7752       (save-restriction
7753         (when (and gnus-signature-face
7754                    (gnus-article-narrow-to-signature))
7755           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7756                             'face gnus-signature-face)
7757           (widen)
7758           (gnus-article-search-signature)
7759           (let ((start (match-beginning 0))
7760                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7761             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7762                                      end)))))))
7763
7764 (defun gnus-button-in-region-p (b e prop)
7765   "Say whether PROP exists in the region."
7766   (text-property-not-all b e prop nil))
7767
7768 (defun gnus-article-add-buttons ()
7769   "Find external references in the article and make buttons of them.
7770 \"External references\" are things like Message-IDs and URLs, as
7771 specified by `gnus-button-alist'."
7772   (interactive)
7773   (gnus-with-article-buffer
7774     (let ((inhibit-point-motion-hooks t)
7775           (case-fold-search t)
7776           (alist gnus-button-alist)
7777           beg entry regexp)
7778       ;; We skip the headers.
7779       (article-goto-body)
7780       (setq beg (point))
7781       (while (setq entry (pop alist))
7782         (setq regexp (eval (car entry)))
7783         (goto-char beg)
7784         (while (re-search-forward regexp nil t)
7785           (let ((start (match-beginning (nth 1 entry)))
7786                 (end (match-end (nth 1 entry)))
7787                 (from (match-beginning 0)))
7788             (when (and (eval (nth 2 entry))
7789                        (not (gnus-button-in-region-p
7790                              start end 'gnus-callback)))
7791               ;; That optional form returned non-nil, so we add the
7792               ;; button.
7793               (setq from (set-marker (make-marker) from))
7794               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7795                            (gnus-article-extend-url-button from start end))
7796                 (gnus-article-add-button start end
7797                                          'gnus-button-push (list from entry))
7798                 (gnus-put-text-property
7799                  start end
7800                  'gnus-string (buffer-substring-no-properties
7801                                start end))))))))))
7802
7803 (defun gnus-article-extend-url-button (beg start end)
7804   "Extend url button if url is folded into two or more lines.
7805 Return non-nil if button is extended.  BEG is a marker that points to
7806 the beginning position of a text containing url.  START and END are
7807 the endpoints of a url button before it is extended.  The concatenated
7808 url is put as the `gnus-button-url' overlay property on the button."
7809   (let ((opoint (point))
7810         (points (list start end))
7811         url delim regexp)
7812     (prog1
7813         (when (and (progn
7814                      (goto-char end)
7815                      (not (looking-at "[\t ]*[\">]")))
7816                    (progn
7817                      (goto-char start)
7818                      (string-match
7819                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7820                       (buffer-substring (point-at-bol) start)))
7821                    (progn
7822                      (setq url (list (buffer-substring start end))
7823                            delim (if (match-beginning 1) ">" "\""))
7824                      (beginning-of-line)
7825                      (setq regexp (concat
7826                                    (when (and (looking-at
7827                                                message-cite-prefix-regexp)
7828                                               (< (match-end 0) start))
7829                                      (regexp-quote (match-string 0)))
7830                                    "\
7831 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7832                                    delim "\\)"))
7833                      (while (progn
7834                               (forward-line 1)
7835                               (and (looking-at regexp)
7836                                    (prog1
7837                                        (match-beginning 1)
7838                                      (push (or (match-string 2)
7839                                                (match-string 1))
7840                                            url)
7841                                      (push (setq end (or (match-end 2)
7842                                                          (match-end 1)))
7843                                            points)
7844                                      (push (or (match-beginning 2)
7845                                                (match-beginning 1))
7846                                            points)))))
7847                      (match-beginning 2)))
7848           (let (gnus-article-mouse-face widget-mouse-face)
7849             (while points
7850               (gnus-article-add-button (pop points) (pop points)
7851                                        'gnus-button-push beg)))
7852           (let ((overlay (gnus-make-overlay start end)))
7853             (gnus-overlay-put overlay 'evaporate t)
7854             (gnus-overlay-put overlay 'gnus-button-url
7855                               (list (mapconcat 'identity (nreverse url) "")))
7856             (when gnus-article-mouse-face
7857               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7858           t)
7859       (goto-char opoint))))
7860
7861 ;; Add buttons to the head of an article.
7862 (defun gnus-article-add-buttons-to-head ()
7863   "Add buttons to the head of the article."
7864   (interactive)
7865   (gnus-with-article-headers
7866     (let (beg end)
7867       (dolist (entry gnus-header-button-alist)
7868         ;; Each alist entry.
7869         (goto-char (point-min))
7870         (while (re-search-forward (car entry) nil t)
7871           ;; Each header matching the entry.
7872           (setq beg (match-beginning 0))
7873           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7874                              (match-beginning 0))
7875                         (point-max)))
7876           (goto-char beg)
7877           (while (re-search-forward (eval (nth 1 entry)) end t)
7878             ;; Each match within a header.
7879             (let* ((entry (cdr entry))
7880                    (start (match-beginning (nth 1 entry)))
7881                    (end (match-end (nth 1 entry)))
7882                    (form (nth 2 entry)))
7883               (goto-char (match-end 0))
7884               (when (eval form)
7885                 (gnus-article-add-button
7886                  start end (nth 3 entry)
7887                  (buffer-substring (match-beginning (nth 4 entry))
7888                                    (match-end (nth 4 entry)))))))
7889           (goto-char end))))))
7890
7891 ;;; External functions:
7892
7893 (defun gnus-article-add-button (from to fun &optional data text)
7894   "Create a button between FROM and TO with callback FUN and data DATA."
7895   (when gnus-article-button-face
7896     (gnus-overlay-put (gnus-make-overlay from to nil t)
7897                       'face gnus-article-button-face))
7898   (gnus-add-text-properties
7899    from to
7900    (nconc (and gnus-article-mouse-face
7901                (list gnus-mouse-face-prop gnus-article-mouse-face))
7902           (list 'gnus-callback fun)
7903           (and data (list 'gnus-data data))))
7904   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7905                          :help-echo (or text "Follow the link")
7906                          :keymap gnus-url-button-map
7907                          :button-keymap gnus-widget-button-keymap))
7908
7909 (defun gnus-article-copy-string ()
7910   "Copy the string in the button to the kill ring."
7911   (interactive)
7912   (gnus-article-check-buffer)
7913   (let ((data (get-text-property (point) 'gnus-string)))
7914     (when data
7915       (with-temp-buffer
7916         (insert data)
7917         (copy-region-as-kill (point-min) (point-max))
7918         (message "Copied %s" data)))))
7919
7920 ;;; Internal functions:
7921
7922 (defun gnus-article-set-globals ()
7923   (with-current-buffer gnus-summary-buffer
7924     (gnus-set-global-variables)))
7925
7926 (defun gnus-signature-toggle (end)
7927   (gnus-with-article-buffer
7928     (let ((inhibit-point-motion-hooks t))
7929       (if (text-property-any end (point-max) 'article-type 'signature)
7930           (progn
7931             (gnus-delete-wash-type 'signature)
7932             (gnus-remove-text-properties-when
7933              'article-type 'signature end (point-max)
7934              (cons 'article-type (cons 'signature
7935                                        gnus-hidden-properties))))
7936         (gnus-add-wash-type 'signature)
7937         (gnus-add-text-properties-when
7938          'article-type nil end (point-max)
7939          (cons 'article-type (cons 'signature
7940                                    gnus-hidden-properties)))))
7941     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7942       (gnus-set-mode-line 'article))))
7943
7944 (defun gnus-button-push (marker-and-entry)
7945   ;; Push button starting at MARKER.
7946   (save-excursion
7947     (let* ((marker (car marker-and-entry))
7948            (entry (cadr marker-and-entry))
7949            (regexp (car entry))
7950            (inhibit-point-motion-hooks t))
7951       (goto-char marker)
7952       ;; This is obviously true, or something bad is happening :)
7953       ;; But we need it to have the match-data
7954       (when (looking-at (or (if (symbolp regexp)
7955                                 (symbol-value regexp)
7956                               regexp)))
7957         (let ((fun (nth 3 entry))
7958               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7959                              (get-char-property marker 'gnus-button-url))
7960                         (mapcar (lambda (group)
7961                                   (let ((string (match-string group)))
7962                                     (set-text-properties
7963                                      0 (length string) nil string)
7964                                     string))
7965                                 (nthcdr 4 entry)))))
7966
7967           (cond
7968            ((fboundp fun)
7969             (apply fun args))
7970            ((and (boundp fun)
7971                  (fboundp (symbol-value fun)))
7972             (apply (symbol-value fun) args))
7973            (t
7974             (gnus-message 1 "You must define `%S' to use this button"
7975                           (cons fun args)))))))))
7976
7977 (defun gnus-parse-news-url (url)
7978   (let (scheme server port group message-id articles)
7979     (with-temp-buffer
7980       (insert url)
7981       (goto-char (point-min))
7982       (when (looking-at "\\([A-Za-z]+\\):")
7983         (setq scheme (match-string 1))
7984         (goto-char (match-end 0)))
7985       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7986         (setq server (match-string 1))
7987         (setq port (if (stringp (match-string 3))
7988                        (string-to-number (match-string 3))
7989                      (match-string 3)))
7990         (goto-char (match-end 0)))
7991
7992       (cond
7993        ((looking-at "\\(.*@.*\\)")
7994         (setq message-id (match-string 1)))
7995        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7996         (setq group (match-string 1)
7997               articles (split-string (match-string 2) "-")))
7998        ((looking-at "\\([^/]+\\)/?")
7999         (setq group (match-string 1)))
8000        (t
8001         (error "Unknown news URL syntax"))))
8002     (list scheme server port group message-id articles)))
8003
8004 (defun gnus-button-handle-news (url)
8005   "Fetch a news URL."
8006   (destructuring-bind (scheme server port group message-id articles)
8007       (gnus-parse-news-url url)
8008     (cond
8009      (message-id
8010       (with-current-buffer gnus-summary-buffer
8011         (if server
8012             (let ((gnus-refer-article-method
8013                    (nconc (list (list 'nntp server))
8014                           gnus-refer-article-method))
8015                   (nntp-port-number (or port "nntp")))
8016               (gnus-message 7 "Fetching %s with %s"
8017                             message-id gnus-refer-article-method)
8018               (gnus-summary-refer-article message-id))
8019           (gnus-summary-refer-article message-id))))
8020      (group
8021       (gnus-button-fetch-group url)))))
8022
8023 (defun gnus-button-patch (library line)
8024   "Visit an Emacs Lisp library LIBRARY on line LINE."
8025   (interactive)
8026   (let ((file (locate-library (file-name-nondirectory library))))
8027     (unless file
8028       (error "Couldn't find library %s" library))
8029     (find-file file)
8030     (goto-char (point-min))
8031     (forward-line (1- (string-to-number line)))))
8032
8033 (defun gnus-button-handle-man (url)
8034   "Fetch a man page."
8035   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8036   (when (eq gnus-button-man-handler 'woman)
8037     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8038   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8039   (funcall gnus-button-man-handler url))
8040
8041 (defun gnus-button-handle-info-url (url)
8042   "Fetch an info URL."
8043   (setq url (mm-subst-char-in-string ?+ ?\  url))
8044   (cond
8045    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8046     (gnus-info-find-node
8047      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8048                      "Gnus")
8049              ")" (gnus-url-unhex-string (match-string 2 url)))))
8050    ((string-match "([^)\"]+)[^\"]+" url)
8051     (setq url
8052           (gnus-replace-in-string
8053            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8054     (gnus-info-find-node url))
8055    (t (error "Can't parse %s" url))))
8056
8057 (defun gnus-button-handle-info-url-gnome (url)
8058   "Fetch GNOME style info URL."
8059   (setq url (mm-subst-char-in-string ?_ ?\  url))
8060   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8061       (gnus-info-find-node
8062        (concat "("
8063                (gnus-url-unhex-string
8064                  (match-string 1 url))
8065                ")"
8066                (or (gnus-url-unhex-string
8067                     (match-string 2 url))
8068                    "Top")))
8069     (error "Can't parse %s" url)))
8070
8071 (defun gnus-button-handle-info-url-kde (url)
8072   "Fetch KDE style info URL."
8073   (gnus-info-find-node (gnus-url-unhex-string url)))
8074
8075 ;; (info) will autoload info.el
8076 (declare-function Info-menu "info" (menu-item &optional fork))
8077 (declare-function Info-index-next "info" (num))
8078
8079 (defun gnus-button-handle-info-keystrokes (url)
8080   "Call `info' when pushing the corresponding URL button."
8081   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8082   (let (node indx comma)
8083     (if (string-match
8084          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8085                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8086                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8087                  "\\(?:[ \t\n,]*\\)\\)?")
8088          url)
8089         (setq node (match-string 2 url)
8090               indx (match-string 3 url))
8091       (error "Can't parse %s" url))
8092     (info)
8093     (Info-directory)
8094     (Info-menu node)
8095     (when (> (length indx) 0)
8096       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8097                             "\\([ \t\n,]*\\)")
8098                     indx)
8099       (setq comma (match-string 2 indx))
8100       (setq indx  (match-string 1 indx))
8101       (Info-index indx)
8102       (when comma
8103         (dotimes (i (with-temp-buffer
8104                       (insert comma)
8105                       ;; Note: the XEmacs version of `how-many' takes
8106                       ;; no optional argument.
8107                       (goto-char (point-min))
8108                       (how-many ",")))
8109           (Info-index-next 1)))
8110       nil)))
8111
8112 (autoload 'pgg-snarf-keys-region "pgg")
8113 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8114 (declare-function pgg-display-output-buffer "pgg" (start end status))
8115
8116 (defun gnus-button-openpgp (url)
8117   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8118   (with-temp-buffer
8119     (mm-url-insert-file-contents-external url)
8120     (pgg-snarf-keys-region (point-min) (point-max))
8121     (pgg-display-output-buffer nil nil nil)))
8122
8123 (defun gnus-button-message-id (message-id)
8124   "Fetch MESSAGE-ID."
8125   (with-current-buffer gnus-summary-buffer
8126     (gnus-summary-refer-article message-id)))
8127
8128 (defun gnus-button-fetch-group (address &rest ignore)
8129   "Fetch GROUP specified by ADDRESS."
8130   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8131                       address)
8132     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8133     ;; for nntp:// and news://
8134     (setq address (match-string 3 address)))
8135   (if (not (string-match "[:/]" address))
8136       ;; This is just a simple group url.
8137       (gnus-group-read-ephemeral-group address gnus-select-method)
8138     (if (not
8139          (string-match
8140           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8141           address))
8142         (error "Can't parse %s" address)
8143       (gnus-group-read-ephemeral-group
8144        (match-string 4 address)
8145        `(nntp ,(match-string 1 address)
8146               (nntp-address ,(match-string 1 address))
8147               (nntp-port-number ,(if (match-end 3)
8148                                      (match-string 3 address)
8149                                    "nntp")))
8150        nil nil nil
8151        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8152
8153 (defun gnus-url-parse-query-string (query &optional downcase)
8154   (let (retval pairs cur key val)
8155     (setq pairs (split-string query "&"))
8156     (while pairs
8157       (setq cur (car pairs)
8158             pairs (cdr pairs))
8159       (if (not (string-match "=" cur))
8160           nil                           ; Grace
8161         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8162               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8163         (if downcase
8164             (setq key (downcase key)))
8165         (setq cur (assoc key retval))
8166         (if cur
8167             (setcdr cur (cons val (cdr cur)))
8168           (setq retval (cons (list key val) retval)))))
8169     retval))
8170
8171 (defun gnus-url-mailto (url)
8172   ;; Send mail to someone
8173   (setq url (replace-regexp-in-string "\n" " " url))
8174   (when (string-match "mailto:/*\\(.*\\)" url)
8175     (setq url (substring url (match-beginning 1) nil)))
8176   (let (to args subject func)
8177     (setq args (gnus-url-parse-query-string
8178                 (if (string-match "^\\?" url)
8179                     (substring url 1)
8180                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8181                       (concat "to=" (match-string 1 url) "&"
8182                               (match-string 2 url))
8183                     (concat "to=" url))))
8184           subject (cdr-safe (assoc "subject" args)))
8185     (gnus-msg-mail)
8186     (while args
8187       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8188       (if (fboundp func)
8189           (funcall func)
8190         (message-position-on-field (caar args)))
8191       (insert (gnus-replace-in-string
8192                (mapconcat 'identity (reverse (cdar args)) ", ")
8193                "\r\n" "\n" t))
8194       (setq args (cdr args)))
8195     (if subject
8196         (message-goto-body)
8197       (message-goto-subject))))
8198
8199 (defun gnus-button-embedded-url (address)
8200   "Activate ADDRESS with `browse-url'."
8201   (browse-url (gnus-strip-whitespace address)))
8202
8203 ;;; Next/prev buttons in the article buffer.
8204
8205 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8206 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8207
8208 (defvar gnus-prev-page-map
8209   (let ((map (make-sparse-keymap)))
8210     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8211     (define-key map "\r" 'gnus-button-prev-page)
8212     map))
8213
8214 (defvar gnus-next-page-map
8215   (let ((map (make-sparse-keymap)))
8216     (define-key map gnus-mouse-2 'gnus-button-next-page)
8217     (define-key map "\r" 'gnus-button-next-page)
8218     map))
8219
8220 (defun gnus-insert-prev-page-button ()
8221   (let ((b (point)) e
8222         (inhibit-read-only t))
8223     (gnus-eval-format
8224      gnus-prev-page-line-format nil
8225      `(keymap ,gnus-prev-page-map
8226               gnus-prev t
8227               gnus-callback gnus-article-button-prev-page
8228               article-type annotation))
8229     (setq e (if (bolp)
8230                 ;; Exclude a newline.
8231                 (1- (point))
8232               (point)))
8233     (when gnus-article-button-face
8234       (gnus-overlay-put (gnus-make-overlay b e nil t)
8235                         'face gnus-article-button-face))
8236     (widget-convert-button
8237      'link b e
8238      :action 'gnus-button-prev-page
8239      :button-keymap gnus-prev-page-map)))
8240
8241 (defun gnus-button-next-page (&optional args more-args)
8242   "Go to the next page."
8243   (interactive)
8244   (let ((win (selected-window)))
8245     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8246     (gnus-article-next-page)
8247     (select-window win)))
8248
8249 (defun gnus-button-prev-page (&optional args more-args)
8250   "Go to the prev page."
8251   (interactive)
8252   (let ((win (selected-window)))
8253     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8254     (gnus-article-prev-page)
8255     (select-window win)))
8256
8257 (defun gnus-insert-next-page-button ()
8258   (let ((b (point)) e
8259         (inhibit-read-only t))
8260     (gnus-eval-format gnus-next-page-line-format nil
8261                       `(keymap ,gnus-next-page-map
8262                                gnus-next t
8263                                gnus-callback gnus-article-button-next-page
8264                                article-type annotation))
8265     (setq e (if (bolp)
8266                 ;; Exclude a newline.
8267                 (1- (point))
8268               (point)))
8269     (when gnus-article-button-face
8270       (gnus-overlay-put (gnus-make-overlay b e nil t)
8271                         'face gnus-article-button-face))
8272     (widget-convert-button
8273      'link b e
8274      :action 'gnus-button-next-page
8275      :button-keymap gnus-next-page-map)))
8276
8277 (defun gnus-article-button-next-page (arg)
8278   "Go to the next page."
8279   (interactive "P")
8280   (let ((win (selected-window)))
8281     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8282     (gnus-article-next-page)
8283     (select-window win)))
8284
8285 (defun gnus-article-button-prev-page (arg)
8286   "Go to the prev page."
8287   (interactive "P")
8288   (let ((win (selected-window)))
8289     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8290     (gnus-article-prev-page)
8291     (select-window win)))
8292
8293 (defvar gnus-decode-header-methods
8294   '(mail-decode-encoded-word-region)
8295   "List of methods used to decode headers.
8296
8297 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8298 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8299 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8300 whose names match REGEXP.
8301
8302 For example:
8303 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8304  mail-decode-encoded-word-region
8305  (\"chinese\" . rfc1843-decode-region))
8306 ")
8307
8308 (defvar gnus-decode-header-methods-cache nil)
8309
8310 (defun gnus-multi-decode-header (start end)
8311   "Apply the functions from `gnus-encoded-word-methods' that match."
8312   (unless (and gnus-decode-header-methods-cache
8313                (eq gnus-newsgroup-name
8314                    (car gnus-decode-header-methods-cache)))
8315     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8316     (dolist (x gnus-decode-header-methods)
8317       (if (symbolp x)
8318           (nconc gnus-decode-header-methods-cache (list x))
8319         (if (and gnus-newsgroup-name
8320                  (string-match (car x) gnus-newsgroup-name))
8321             (nconc gnus-decode-header-methods-cache
8322                    (list (cdr x)))))))
8323   (let ((xlist gnus-decode-header-methods-cache))
8324     (pop xlist)
8325     (save-restriction
8326       (narrow-to-region start end)
8327       (while xlist
8328         (funcall (pop xlist) (point-min) (point-max))))))
8329
8330 ;;;
8331 ;;; Treatment top-level handling.
8332 ;;;
8333
8334 (defvar gnus-inhibit-article-treatments nil)
8335
8336 (defun gnus-treat-article (gnus-treat-condition
8337                            &optional part-number total-parts gnus-treat-type)
8338   (let ((gnus-treat-length (- (point-max) (point-min)))
8339         (alist gnus-treatment-function-alist)
8340         (article-goto-body-goes-to-point-min-p t)
8341         (treated-type
8342          (or (not gnus-treat-type)
8343              (catch 'found
8344                (let ((list gnus-article-treat-types))
8345                  (while list
8346                    (when (string-match (pop list) gnus-treat-type)
8347                      (throw 'found t)))))))
8348         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8349         val elem)
8350     (gnus-run-hooks 'gnus-part-display-hook)
8351     (dolist (elem alist)
8352       (setq val
8353             (save-excursion
8354               (when (gnus-buffer-live-p gnus-summary-buffer)
8355                 (set-buffer gnus-summary-buffer))
8356               (symbol-value (car elem))))
8357       (when (and (or (consp val)
8358                      treated-type)
8359                  (or (not gnus-inhibit-article-treatments)
8360                      (eq gnus-treat-condition 'head))
8361                  (gnus-treat-predicate val)
8362                  (or (not (get (car elem) 'highlight))
8363                      highlightp))
8364         (save-restriction
8365           (funcall (cadr elem)))))))
8366
8367 ;; Dynamic variables.
8368 (defvar part-number)
8369 (defvar total-parts)
8370 (defvar gnus-treat-type)
8371 (defvar gnus-treat-condition)
8372 (defvar gnus-treat-length)
8373
8374 (defun gnus-treat-predicate (val)
8375   (cond
8376    ((null val)
8377     nil)
8378    (gnus-treat-condition
8379     (eq gnus-treat-condition val))
8380    ((and (listp val)
8381          (stringp (car val)))
8382     (apply 'gnus-or (mapcar `(lambda (s)
8383                                (string-match s ,(or gnus-newsgroup-name "")))
8384                             val)))
8385    ((listp val)
8386     (let ((pred (pop val)))
8387       (cond
8388        ((eq pred 'or)
8389         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8390        ((eq pred 'and)
8391         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8392        ((eq pred 'not)
8393         (not (gnus-treat-predicate (car val))))
8394        ((eq pred 'typep)
8395         (equal (car val) gnus-treat-type))
8396        (t
8397         (error "%S is not a valid predicate" pred)))))
8398    ((eq val t)
8399     t)
8400    ((eq val 'head)
8401     nil)
8402    ((eq val 'first)
8403     (eq part-number 1))
8404    ((eq val 'last)
8405     (eq part-number total-parts))
8406    ((numberp val)
8407     (< gnus-treat-length val))
8408    (t
8409     (error "%S is not a valid value" val))))
8410
8411 (defun gnus-article-encrypt-body (protocol &optional n)
8412   "Encrypt the article body."
8413   (interactive
8414    (list
8415     (or gnus-article-encrypt-protocol
8416         (gnus-completing-read "Encrypt protocol"
8417                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8418                               t))
8419     current-prefix-arg))
8420   ;; User might hit `K E' instead of `K e', so prompt once.
8421   (when (and gnus-article-encrypt-protocol
8422              gnus-novice-user)
8423     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8424       (error "Encrypt aborted")))
8425   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8426     (unless func
8427       (error "Can't find the encrypt protocol %s" protocol))
8428     (if (member gnus-newsgroup-name '("nndraft:delayed"
8429                                       "nndraft:drafts"
8430                                       "nndraft:queue"))
8431         (error "Can't encrypt the article in group %s"
8432                gnus-newsgroup-name))
8433     (gnus-summary-iterate n
8434       (with-current-buffer gnus-summary-buffer
8435         (let ((mail-parse-charset gnus-newsgroup-charset)
8436               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8437               (summary-buffer gnus-summary-buffer)
8438               references point)
8439           (gnus-set-global-variables)
8440           (when (gnus-group-read-only-p)
8441             (error "The current newsgroup does not support article encrypt"))
8442           (gnus-summary-show-article t)
8443           (setq references
8444               (or (mail-header-references gnus-current-headers) ""))
8445           (set-buffer gnus-article-buffer)
8446           (let* ((inhibit-read-only t)
8447                  (headers
8448                   (mapcar (lambda (field)
8449                             (and (save-restriction
8450                                    (message-narrow-to-head)
8451                                    (goto-char (point-min))
8452                                    (search-forward field nil t))
8453                                  (prog2
8454                                      (message-narrow-to-field)
8455                                      (buffer-string)
8456                                    (delete-region (point-min) (point-max))
8457                                    (widen))))
8458                           '("Content-Type:" "Content-Transfer-Encoding:"
8459                             "Content-Disposition:"))))
8460             (message-narrow-to-head)
8461             (message-remove-header "MIME-Version")
8462             (goto-char (point-max))
8463             (setq point (point))
8464             (insert (apply 'concat headers))
8465             (widen)
8466             (narrow-to-region point (point-max))
8467             (let ((message-options message-options))
8468               (message-options-set 'message-sender user-mail-address)
8469               (message-options-set 'message-recipients user-mail-address)
8470               (message-options-set 'message-sign-encrypt 'not)
8471               (funcall func))
8472             (goto-char (point-min))
8473             (insert "MIME-Version: 1.0\n")
8474             (widen)
8475             (gnus-summary-edit-article-done
8476              references nil summary-buffer t))
8477           (when gnus-keep-backlog
8478             (gnus-backlog-remove-article
8479              (car gnus-article-current) (cdr gnus-article-current)))
8480           (gnus-flush-original-article-buffer)
8481           (when gnus-use-cache
8482             (gnus-cache-update-article
8483              (car gnus-article-current) (cdr gnus-article-current))))))))
8484
8485 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8486   "The following specs can be used:
8487 %t  The security MIME type
8488 %i  Additional info
8489 %d  Details
8490 %D  Details if button is pressed")
8491
8492 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8493   "The following specs can be used:
8494 %t  The security MIME type
8495 %i  Additional info
8496 %d  Details
8497 %D  Details if button is pressed")
8498
8499 (defvar gnus-mime-security-button-line-format-alist
8500   '((?t gnus-tmp-type ?s)
8501     (?i gnus-tmp-info ?s)
8502     (?d gnus-tmp-details ?s)
8503     (?D gnus-tmp-pressed-details ?s)))
8504
8505 (defvar gnus-mime-security-button-commands
8506   '((gnus-article-press-button "\r" "Show Detail")
8507     (undefined "v")
8508     (undefined "t")
8509     (undefined "C")
8510     (gnus-mime-security-save-part "o" "Save...")
8511     (undefined "\C-o")
8512     (undefined "r")
8513     (undefined "d")
8514     (undefined "c")
8515     (undefined "i")
8516     (undefined "E")
8517     (undefined "e")
8518     (undefined "p")
8519     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8520     (undefined ".")))
8521
8522 (defvar gnus-mime-security-button-map
8523   (let ((map (make-sparse-keymap)))
8524     (define-key map gnus-mouse-2 'gnus-article-push-button)
8525     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8526     (dolist (c gnus-mime-security-button-commands)
8527       (define-key map (cadr c) (car c)))
8528     map))
8529
8530 (easy-menu-define
8531   gnus-mime-security-button-menu gnus-mime-security-button-map
8532   "Security button menu."
8533   `("Security Part"
8534     ,@(delq nil
8535             (mapcar (lambda (c)
8536                       (unless (eq (car c) 'undefined)
8537                         (vector (caddr c) (car c) :active t)))
8538                     gnus-mime-security-button-commands))))
8539
8540 (defun gnus-mime-security-button-menu (event prefix)
8541   "Construct a context-sensitive menu of security commands."
8542   (interactive "e\nP")
8543   (save-window-excursion
8544     (let ((pos (event-start event)))
8545       (select-window (posn-window pos))
8546       (goto-char (posn-point pos))
8547       (gnus-article-check-buffer)
8548       (popup-menu gnus-mime-security-button-menu nil prefix))))
8549
8550 (defvar gnus-mime-security-details-buffer nil)
8551
8552 (defvar gnus-mime-security-button-pressed nil)
8553
8554 (defvar gnus-mime-security-show-details-inline t
8555   "If non-nil, show details in the article buffer.")
8556
8557 (defun gnus-mime-security-verify-or-decrypt (handle)
8558   (mm-remove-parts (cdr handle))
8559   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8560         point (inhibit-read-only t))
8561     (if region
8562         (goto-char (car region)))
8563     (setq point (point))
8564     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8565       (let* ((mm-verify-option 'known)
8566              (mm-decrypt-option 'known)
8567              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8568         (unless (eq nparts (cdr handle))
8569           (mm-destroy-parts (cdr handle))
8570           (setcdr handle nparts))))
8571     (gnus-mime-display-security handle)
8572     (when region
8573       (delete-region (point) (cdr region))
8574       (set-marker (car region) nil)
8575       (set-marker (cdr region) nil))
8576     (goto-char point)))
8577
8578 (defun gnus-mime-security-show-details (handle)
8579   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8580     (if (not details)
8581         (gnus-message 5 "No details.")
8582       (if gnus-mime-security-show-details-inline
8583           (let ((gnus-mime-security-button-pressed
8584                  (not (get-text-property (point) 'gnus-mime-details)))
8585                 (gnus-mime-security-button-line-format
8586                  (get-text-property (point) 'gnus-line-format))
8587                 (inhibit-read-only t))
8588             (forward-char -1)
8589             (while (eq (get-text-property (point) 'gnus-line-format)
8590                        gnus-mime-security-button-line-format)
8591               (forward-char -1))
8592             (forward-char)
8593             (save-restriction
8594               (narrow-to-region (point) (point))
8595               (gnus-insert-mime-security-button handle))
8596             (delete-region (point)
8597                            (or (text-property-not-all
8598                                 (point) (point-max)
8599                                 'gnus-line-format
8600                                 gnus-mime-security-button-line-format)
8601                                (point-max))))
8602         ;; Not inlined.
8603         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8604             (with-current-buffer gnus-mime-security-details-buffer
8605               (erase-buffer)
8606               t)
8607           (setq gnus-mime-security-details-buffer
8608                 (gnus-get-buffer-create "*MIME Security Details*")))
8609         (with-current-buffer gnus-mime-security-details-buffer
8610           (insert details)
8611           (goto-char (point-min)))
8612         (pop-to-buffer gnus-mime-security-details-buffer)))))
8613
8614 (defun gnus-mime-security-press-button (handle)
8615   (save-excursion
8616     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8617         (gnus-mime-security-show-details handle)
8618       (gnus-mime-security-verify-or-decrypt handle))))
8619
8620 (defun gnus-insert-mime-security-button (handle &optional displayed)
8621   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8622          (gnus-tmp-type
8623           (concat
8624            (or (nth 2 (assoc protocol mm-verify-function-alist))
8625                (nth 2 (assoc protocol mm-decrypt-function-alist))
8626                "Unknown")
8627            (if (equal (car handle) "multipart/signed")
8628                " Signed" " Encrypted")
8629            " Part"))
8630          (gnus-tmp-info
8631           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8632               "Undecided"))
8633          (gnus-tmp-details
8634           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8635          gnus-tmp-pressed-details
8636          b e)
8637     (setq gnus-tmp-details
8638           (if gnus-tmp-details
8639               (concat "\n" gnus-tmp-details)
8640             ""))
8641     (setq gnus-tmp-pressed-details
8642           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8643     (unless (bolp)
8644       (insert "\n"))
8645     (setq b (point))
8646     (gnus-eval-format
8647      gnus-mime-security-button-line-format
8648      gnus-mime-security-button-line-format-alist
8649      `(keymap ,gnus-mime-security-button-map
8650          gnus-callback gnus-mime-security-press-button
8651          gnus-line-format ,gnus-mime-security-button-line-format
8652          gnus-mime-details ,gnus-mime-security-button-pressed
8653          article-type annotation
8654          gnus-data ,handle))
8655     (setq e (if (bolp)
8656                 ;; Exclude a newline.
8657                 (1- (point))
8658               (point)))
8659     (when gnus-article-button-face
8660       (gnus-overlay-put (gnus-make-overlay b e nil t)
8661                         'face gnus-article-button-face))
8662     (widget-convert-button
8663      'link b e
8664      :mime-handle handle
8665      :action 'gnus-widget-press-button
8666      :button-keymap gnus-mime-security-button-map
8667      :help-echo
8668      (lambda (widget)
8669        ;; Needed to properly clear the message due to a bug in
8670        ;; wid-edit (XEmacs only).
8671        (when (boundp 'help-echo-owns-message)
8672          (setq help-echo-owns-message t))
8673        (format
8674         "%S: show detail; %S: more options"
8675         (aref gnus-mouse-2 0)
8676         (aref gnus-down-mouse-3 0))))))
8677
8678 (defun gnus-mime-display-security (handle)
8679   (save-restriction
8680     (narrow-to-region (point) (point))
8681     (unless (gnus-unbuttonized-mime-type-p (car handle))
8682       (gnus-insert-mime-security-button handle))
8683     (gnus-mime-display-part (cadr handle))
8684     (unless (bolp)
8685       (insert "\n"))
8686     (unless (gnus-unbuttonized-mime-type-p (car handle))
8687       (let ((gnus-mime-security-button-line-format
8688              gnus-mime-security-button-end-line-format))
8689         (gnus-insert-mime-security-button handle)))
8690     (mm-set-handle-multipart-parameter
8691      handle 'gnus-region
8692      (cons (set-marker (make-marker) (point-min))
8693            (set-marker (make-marker) (point-max))))
8694     (goto-char (point-max))))
8695
8696 (defun gnus-mime-security-run-function (function)
8697   "Run FUNCTION with the security part under point."
8698   (gnus-article-check-buffer)
8699   (let ((data (get-text-property (point) 'gnus-data))
8700         buffer handle)
8701     (when (and (stringp (car-safe data))
8702                (setq buffer (mm-handle-multipart-original-buffer data))
8703                (setq handle (cadr data)))
8704       (if (bufferp (mm-handle-buffer handle))
8705           (progn
8706             (setq handle (cons buffer (copy-sequence (cdr handle))))
8707             (mm-handle-set-undisplayer handle nil))
8708         (setq handle (mm-make-handle
8709                       buffer
8710                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8711                       nil nil nil nil nil nil)))
8712       (funcall function handle))))
8713
8714 (defun gnus-mime-security-save-part ()
8715   "Save the security part under point."
8716   (interactive)
8717   (gnus-mime-security-run-function 'mm-save-part))
8718
8719 (defun gnus-mime-security-pipe-part ()
8720   "Pipe the security part under point to a process."
8721   (interactive)
8722   (gnus-mime-security-run-function 'mm-pipe-part))
8723
8724 (gnus-ems-redefine)
8725
8726 (provide 'gnus-art)
8727
8728 (run-hooks 'gnus-art-load-hook)
8729
8730 ;;; gnus-art.el ends here