616f8cd584159232fa470ddfc97143f0500896a2
[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 (require 'gmm-utils)
50
51 (autoload 'gnus-msg-mail "gnus-msg" nil t)
52 (autoload 'gnus-button-mailto "gnus-msg")
53 (autoload 'gnus-button-reply "gnus-msg" nil t)
54 (autoload 'parse-time-string "parse-time" nil nil)
55 (autoload 'ansi-color-apply-on-region "ansi-color")
56 (autoload 'mm-url-insert-file-contents-external "mm-url")
57 (autoload 'mm-extern-cache-contents "mm-extern")
58
59 (defgroup gnus-article nil
60   "Article display."
61   :link '(custom-manual "(gnus)Article Buffer")
62   :group 'gnus)
63
64 (defgroup gnus-article-treat nil
65   "Treating article parts."
66   :link '(custom-manual "(gnus)Article Hiding")
67   :group 'gnus-article)
68
69 (defgroup gnus-article-hiding nil
70   "Hiding article parts."
71   :link '(custom-manual "(gnus)Article Hiding")
72   :group 'gnus-article)
73
74 (defgroup gnus-article-highlight nil
75   "Article highlighting."
76   :link '(custom-manual "(gnus)Article Highlighting")
77   :group 'gnus-article
78   :group 'gnus-visual)
79
80 (defgroup gnus-article-signature nil
81   "Article signatures."
82   :link '(custom-manual "(gnus)Article Signature")
83   :group 'gnus-article)
84
85 (defgroup gnus-article-headers nil
86   "Article headers."
87   :link '(custom-manual "(gnus)Hiding Headers")
88   :group 'gnus-article)
89
90 (defgroup gnus-article-washing nil
91   "Special commands on articles."
92   :link '(custom-manual "(gnus)Article Washing")
93   :group 'gnus-article)
94
95 (defgroup gnus-article-emphasis nil
96   "Fontisizing articles."
97   :link '(custom-manual "(gnus)Article Fontisizing")
98   :group 'gnus-article)
99
100 (defgroup gnus-article-saving nil
101   "Saving articles."
102   :link '(custom-manual "(gnus)Saving Articles")
103   :group 'gnus-article)
104
105 (defgroup gnus-article-mime nil
106   "Worshiping the MIME wonder."
107   :link '(custom-manual "(gnus)Using MIME")
108   :group 'gnus-article)
109
110 (defgroup gnus-article-buttons nil
111   "Pushable buttons in the article buffer."
112   :link '(custom-manual "(gnus)Article Buttons")
113   :group 'gnus-article)
114
115 (defgroup gnus-article-various nil
116   "Other article options."
117   :link '(custom-manual "(gnus)Misc Article")
118   :group 'gnus-article)
119
120 (defcustom gnus-ignored-headers
121   (mapcar
122    (lambda (header)
123      (concat "^" header ":"))
124    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
125      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
126      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
127      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
128      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
129      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
130      "X-Attribution" "X-Originating-IP" "Delivered-To"
131      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
132      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
133      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
134      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
135      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
136      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
137      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
138      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
139      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
140      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
141      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
142      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
143      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
144      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
145      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
146      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
147      "List-[A-Za-z]+" "X-Listprocessor-Version"
148      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
149      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
150      "X-Received" "Content-length" "X-precedence"
151      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
152      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
153      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
154      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
155      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
156      "X-Content-length" "X-Posting-Agent" "Original-Received"
157      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
158      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
159      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
160      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
161      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
162      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
163      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
164      "Envelope-Sender" "Envelope-Recipients"))
165   "*All headers that start with this regexp will be hidden.
166 This variable can also be a list of regexps of headers to be ignored.
167 If `gnus-visible-headers' is non-nil, this variable will be ignored."
168   :type '(choice regexp
169                  (repeat regexp))
170   :group 'gnus-article-hiding)
171
172 (defcustom gnus-visible-headers
173   "^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:"
174   "*All headers that do not match this regexp will be hidden.
175 This variable can also be a list of regexp of headers to remain visible.
176 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
177   :type '(choice
178           (repeat :value-to-internal (lambda (widget value)
179                                        (custom-split-regexp-maybe value))
180                   :match (lambda (widget value)
181                            (or (stringp value)
182                                (widget-editable-list-match widget value)))
183                   regexp)
184           (const :tag "Use gnus-ignored-headers" nil)
185           regexp)
186   :group 'gnus-article-hiding)
187
188 (defcustom gnus-sorted-header-list
189   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
190     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
191   "*This variable is a list of regular expressions.
192 If it is non-nil, headers that match the regular expressions will
193 be placed first in the article buffer in the sequence specified by
194 this list."
195   :type '(repeat regexp)
196   :group 'gnus-article-hiding)
197
198 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
199   "Headers that are only to be displayed if they have interesting data.
200 Possible values in this list are:
201
202   'empty       Headers with no content.
203   'newsgroups  Newsgroup identical to Gnus group.
204   'to-address  To identical to To-address.
205   'to-list     To identical to To-list.
206   'cc-list     CC identical to To-list.
207   'followup-to Followup-to identical to Newsgroups.
208   'reply-to    Reply-to identical to From.
209   'date        Date less than four days old.
210   'long-to     To and/or Cc longer than 1024 characters.
211   'many-to     Multiple To and/or Cc."
212   :type '(set (const :tag "Headers with no content." empty)
213               (const :tag "Newsgroups identical to Gnus group." newsgroups)
214               (const :tag "To identical to To-address." to-address)
215               (const :tag "To identical to To-list." to-list)
216               (const :tag "CC identical to To-list." cc-list)
217               (const :tag "Followup-to identical to Newsgroups." followup-to)
218               (const :tag "Reply-to identical to From." reply-to)
219               (const :tag "Date less than four days old." date)
220               (const :tag "To and/or Cc longer than 1024 characters." long-to)
221               (const :tag "Multiple To and/or Cc headers." many-to))
222   :group 'gnus-article-hiding)
223
224 (defcustom gnus-article-skip-boring nil
225   "Skip over text that is not worth reading.
226 By default, if you set this t, then Gnus will display citations and
227 signatures, but will never scroll down to show you a page consisting
228 only of boring text.  Boring text is controlled by
229 `gnus-article-boring-faces'."
230   :version "22.1"
231   :type 'boolean
232   :group 'gnus-article-hiding)
233
234 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
235   "Regexp matching signature separator.
236 This can also be a list of regexps.  In that case, it will be checked
237 from head to tail looking for a separator.  Searches will be done from
238 the end of the buffer."
239   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
240                  (regexp)
241                  (repeat :tag "List of regexp" regexp))
242   :group 'gnus-article-signature)
243
244 (defcustom gnus-signature-limit nil
245   "Provide a limit to what is considered a signature.
246 If it is a number, no signature may not be longer (in characters) than
247 that number.  If it is a floating point number, no signature may be
248 longer (in lines) than that number.  If it is a function, the function
249 will be called without any parameters, and if it returns nil, there is
250 no signature in the buffer.  If it is a string, it will be used as a
251 regexp.  If it matches, the text in question is not a signature.
252
253 This can also be a list of the above values."
254   :type '(choice (const nil)
255                  (integer :value 200)
256                  (number :value 4.0)
257                  function
258                  (regexp :value ".*"))
259   :group 'gnus-article-signature)
260
261 (defcustom gnus-hidden-properties '(invisible t intangible t)
262   "Property list to use for hiding text."
263   :type 'sexp
264   :group 'gnus-article-hiding)
265
266 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
267 ;; frames in a session.
268 (defcustom gnus-article-x-face-command
269   (if (featurep 'xemacs)
270       (if (or (gnus-image-type-available-p 'xface)
271               (gnus-image-type-available-p 'pbm))
272           'gnus-display-x-face-in-from
273         "{ echo \
274 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
275 ; uncompface; } | icontopbm | ee -")
276     (if (gnus-image-type-available-p 'pbm)
277         'gnus-display-x-face-in-from
278       "{ echo \
279 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
280 ; uncompface; } | icontopbm | display -"))
281   "*String or function to be executed to display an X-Face header.
282 If it is a string, the command will be executed in a sub-shell
283 asynchronously.  The compressed face will be piped to this command."
284   :type `(choice string
285                  (function-item gnus-display-x-face-in-from)
286                  function)
287   :version "21.1"
288   :group 'gnus-picon
289   :group 'gnus-article-washing)
290
291 (defcustom gnus-article-x-face-too-ugly nil
292   "Regexp matching posters whose face shouldn't be shown automatically."
293   :type '(choice regexp (const nil))
294   :group 'gnus-article-washing)
295
296 (defcustom gnus-article-banner-alist nil
297   "Banner alist for stripping.
298 For example,
299      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
300   :version "21.1"
301   :type '(repeat (cons symbol regexp))
302   :group 'gnus-article-washing)
303
304 (gnus-define-group-parameter
305  banner
306  :variable-document
307  "Alist of regexps (to match group names) and banner."
308  :variable-group gnus-article-washing
309  :parameter-type
310  '(choice :tag "Banner"
311           :value nil
312           (const :tag "Remove signature" signature)
313           (symbol :tag "Item in `gnus-article-banner-alist'" none)
314           regexp
315           (const :tag "None" nil))
316  :parameter-document
317  "If non-nil, specify how to remove `banners' from articles.
318
319 Symbol `signature' means to remove signatures delimited by
320 `gnus-signature-separator'.  Any other symbol is used to look up a
321 regular expression to match the banner in `gnus-article-banner-alist'.
322 A string is used as a regular expression to match the banner
323 directly.")
324
325 (defcustom gnus-article-address-banner-alist nil
326   "Alist of mail addresses and banners.
327 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
328 to match a mail address in the From: header, BANNER is one of a symbol
329 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
330 If ADDRESS matches author's mail address, it will remove things like
331 advertisements.  For example:
332
333 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
334 "
335   :type '(repeat
336           (cons
337            (regexp :tag "Address")
338            (choice :tag "Banner" :value nil
339                    (const :tag "Remove signature" signature)
340                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
341                    regexp
342                    (const :tag "None" nil))))
343   :version "22.1"
344   :group 'gnus-article-washing)
345
346 (defmacro gnus-emphasis-custom-with-format (&rest body)
347   `(let ((format "\
348 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
349 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
350      ,@body))
351
352 (defun gnus-emphasis-custom-value-to-external (value)
353   (gnus-emphasis-custom-with-format
354    (if (consp (car value))
355        (list (format format (car (car value)) (cdr (car value)))
356              2
357              (if (nth 1 value) 2 3)
358              (nth 2 value))
359      value)))
360
361 (defun gnus-emphasis-custom-value-to-internal (value)
362   (gnus-emphasis-custom-with-format
363    (let ((regexp (concat "\\`"
364                          (format (regexp-quote format)
365                                  "\\([^()]+\\)" "\\([^()]+\\)")
366                          "\\'"))
367          pattern)
368      (if (string-match regexp (setq pattern (car value)))
369          (list (cons (match-string 1 pattern) (match-string 2 pattern))
370                (= (nth 2 value) 2)
371                (nth 3 value))
372        value))))
373
374 (defcustom gnus-emphasis-alist
375   (let ((types
376          '(("\\*" "\\*" bold nil 2)
377            ("_" "_" underline)
378            ("/" "/" italic)
379            ("_/" "/_" underline-italic)
380            ("_\\*" "\\*_" underline-bold)
381            ("\\*/" "/\\*" bold-italic)
382            ("_\\*/" "/\\*_" underline-bold-italic))))
383     (nconc
384      (gnus-emphasis-custom-with-format
385       (mapcar (lambda (spec)
386                 (list (format format (car spec) (cadr spec))
387                       (or (nth 3 spec) 2)
388                       (or (nth 4 spec) 3)
389                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
390               types))
391      '(;; I've never seen anyone use this strikethru convention whereas I've
392        ;; several times seen it triggered by normal text.  --Stef
393        ;; Miles suggests that this form is sometimes used but for italics,
394        ;; so maybe we should map it to `italic'.
395        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
396        ;; 2 3 gnus-emphasis-strikethru)
397        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
398         2 3 gnus-emphasis-underline))))
399   "*Alist that says how to fontify certain phrases.
400 Each item looks like this:
401
402   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
403
404 The first element is a regular expression to be matched.  The second
405 is a number that says what regular expression grouping used to find
406 the entire emphasized word.  The third is a number that says what
407 regexp grouping should be displayed and highlighted.  The fourth
408 is the face used for highlighting."
409   :type
410   '(repeat
411     (menu-choice
412      :format "%[Customizing Style%]\n%v"
413      :indent 2
414      (group :tag "Default"
415             :value ("" 0 0 default)
416             :value-create
417             (lambda (widget)
418               (let ((value (widget-get
419                             (cadr (widget-get (widget-get widget :parent)
420                                               :args))
421                             :value)))
422                 (if (not (eq (nth 2 value) 'default))
423                     (widget-put
424                      widget
425                      :value
426                      (gnus-emphasis-custom-value-to-external value))))
427               (widget-group-value-create widget))
428             regexp
429             (integer :format "Match group: %v")
430             (integer :format "Emphasize group: %v")
431             face)
432      (group :tag "Simple"
433             :value (("_" . "_") nil default)
434             (cons :format "%v"
435                   (regexp :format "Start regexp: %v")
436                   (regexp :format "End regexp: %v"))
437             (boolean :format "Show start and end patterns: %[%v%]\n"
438                      :on " On " :off " Off ")
439             face)))
440   :get (lambda (symbol)
441          (mapcar 'gnus-emphasis-custom-value-to-internal
442                  (default-value symbol)))
443   :set (lambda (symbol value)
444          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
445                                      value)))
446   :group 'gnus-article-emphasis)
447
448 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
449   "A regexp to describe whitespace which should not be emphasized.
450 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
451 The former avoids underlining of leading and trailing whitespace,
452 and the latter avoids underlining any whitespace at all."
453   :version "21.1"
454   :group 'gnus-article-emphasis
455   :type 'regexp)
456
457 (defface gnus-emphasis-bold '((t (:bold t)))
458   "Face used for displaying strong emphasized text (*word*)."
459   :group 'gnus-article-emphasis)
460
461 (defface gnus-emphasis-italic '((t (:italic t)))
462   "Face used for displaying italic emphasized text (/word/)."
463   :group 'gnus-article-emphasis)
464
465 (defface gnus-emphasis-underline '((t (:underline t)))
466   "Face used for displaying underlined emphasized text (_word_)."
467   :group 'gnus-article-emphasis)
468
469 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
470   "Face used for displaying underlined bold emphasized text (_*word*_)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
474   "Face used for displaying underlined italic emphasized text (_/word/_)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
478   "Face used for displaying bold italic emphasized text (/*word*/)."
479   :group 'gnus-article-emphasis)
480
481 (defface gnus-emphasis-underline-bold-italic
482   '((t (:bold t :italic t :underline t)))
483   "Face used for displaying underlined bold italic emphasized text.
484 Example: (_/*word*/_)."
485   :group 'gnus-article-emphasis)
486
487 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
488                                       '((t (:strikethru t)))
489                                     '((t (:strike-through t))))
490   "Face used for displaying strike-through text (-word-)."
491   :group 'gnus-article-emphasis)
492
493 (defface gnus-emphasis-highlight-words
494   '((t (:background "black" :foreground "yellow")))
495   "Face used for displaying highlighted words."
496   :group 'gnus-article-emphasis)
497
498 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
499   "Format for display of Date headers in article bodies.
500 See `format-time-string' for the possible values.
501
502 The variable can also be function, which should return a complete Date
503 header.  The function is called with one argument, the time, which can
504 be fed to `format-time-string'."
505   :type '(choice string function)
506   :link '(custom-manual "(gnus)Article Date")
507   :group 'gnus-article-washing)
508
509 (defcustom gnus-save-all-headers t
510   "*If non-nil, don't remove any headers before saving.
511 This will be overridden by the `:headers' property that the symbol of
512 the saver function, which is specified by `gnus-default-article-saver',
513 might have."
514   :group 'gnus-article-saving
515   :type 'boolean)
516
517 (defcustom gnus-prompt-before-saving 'always
518   "*This variable says how much prompting is to be done when saving articles.
519 If it is nil, no prompting will be done, and the articles will be
520 saved to the default files.  If this variable is `always', each and
521 every article that is saved will be preceded by a prompt, even when
522 saving large batches of articles.  If this variable is neither nil not
523 `always', there the user will be prompted once for a file name for
524 each invocation of the saving commands."
525   :group 'gnus-article-saving
526   :type '(choice (item always)
527                  (item :tag "never" nil)
528                  (sexp :tag "once" :format "%t\n" :value t)))
529
530 (defcustom gnus-saved-headers gnus-visible-headers
531   "Headers to keep if `gnus-save-all-headers' is nil.
532 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
533 If that variable is nil, however, all headers that match this regexp
534 will be kept while the rest will be deleted before saving.  This and
535 `gnus-save-all-headers' will be overridden by the `:headers' property
536 that the symbol of the saver function, which is specified by
537 `gnus-default-article-saver', might have."
538   :group 'gnus-article-saving
539   :type 'regexp)
540
541 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
542 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
543   "A function to save articles in your favorite format.
544 The function will be called by way of the `gnus-summary-save-article'
545 command, and friends such as `gnus-summary-save-article-rmail'.
546
547 Gnus provides the following functions:
548
549 * gnus-summary-save-in-rmail (Rmail format)
550 * gnus-summary-save-in-mail (Unix mail format)
551 * gnus-summary-save-in-folder (MH folder)
552 * gnus-summary-save-in-file (article format)
553 * gnus-summary-save-body-in-file (article body)
554 * gnus-summary-save-in-vm (use VM's folder format)
555 * gnus-summary-write-to-file (article format -- overwrite)
556 * gnus-summary-write-body-to-file (article body -- overwrite)
557 * gnus-summary-save-in-pipe (article format)
558
559 The symbol of each function may have the following properties:
560
561 * :decode
562 The value non-nil means save decoded articles.  This is meaningful
563 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
564 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
565 `gnus-summary-save-in-pipe'.
566
567 * :function
568 The value specifies an alternative function which appends, not
569 overwrites, articles to a file.  This implies that when saving many
570 articles at a time, `gnus-prompt-before-saving' is bound to t and all
571 articles are saved in a single file.  This is meaningful only with
572 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
573
574 * :headers
575 The value specifies the symbol of a variable of which the value
576 specifies headers to be saved.  If it is omitted,
577 `gnus-save-all-headers' and `gnus-saved-headers' control what
578 headers should be saved."
579   :group 'gnus-article-saving
580   :type '(radio (function-item gnus-summary-save-in-rmail)
581                 (function-item gnus-summary-save-in-mail)
582                 (function-item gnus-summary-save-in-folder)
583                 (function-item gnus-summary-save-in-file)
584                 (function-item gnus-summary-save-body-in-file)
585                 (function-item gnus-summary-save-in-vm)
586                 (function-item gnus-summary-write-to-file)
587                 (function-item gnus-summary-write-body-to-file)
588                 (function-item gnus-summary-save-in-pipe)
589                 (function)))
590
591 (defcustom gnus-article-save-coding-system
592   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
593       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
594       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
595       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
596   "Coding system used to save decoded articles to a file.
597
598 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
599 which can safely encode any characters in text.  This is used by the
600 commands including:
601
602 * gnus-summary-save-article-file
603 * gnus-summary-save-article-body-file
604 * gnus-summary-write-article-file
605 * gnus-summary-write-article-body-file
606
607 and the functions to which you may set `gnus-default-article-saver':
608
609 * gnus-summary-save-in-file
610 * gnus-summary-save-body-in-file
611 * gnus-summary-write-to-file
612 * gnus-summary-write-body-to-file
613
614 Those commands and functions save just text displayed in the article
615 buffer to a file if the value of this variable is non-nil.  Note that
616 buttonized MIME parts will be lost in a saved file in that case.
617 Otherwise, raw articles will be saved."
618   :group 'gnus-article-saving
619   :type `(choice
620           :format "%{%t%}:\n %[Value Menu%] %v"
621           (const :tag "Save raw articles" nil)
622           ,@(delq nil
623                   (mapcar
624                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
625                    '((const :tag "UTF-8" utf-8)
626                      (const :tag "iso-2022-7bit" iso-2022-7bit)
627                      (const :tag "Emacs internal" emacs-mule)
628                      (const :tag "escape-quoted" escape-quoted))))
629           (symbol :tag "Coding system")))
630
631 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
632   "A function generating a file name to save articles in Rmail format.
633 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
634   :group 'gnus-article-saving
635   :type 'function)
636
637 (defcustom gnus-mail-save-name 'gnus-plain-save-name
638   "A function generating a file name to save articles in Unix mail format.
639 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
640   :group 'gnus-article-saving
641   :type 'function)
642
643 (defcustom gnus-folder-save-name 'gnus-folder-save-name
644   "A function generating a file name to save articles in MH folder.
645 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
646   :group 'gnus-article-saving
647   :type 'function)
648
649 (defcustom gnus-file-save-name 'gnus-numeric-save-name
650   "A function generating a file name to save articles in article format.
651 The function is called with NEWSGROUP, HEADERS, and optional
652 LAST-FILE."
653   :group 'gnus-article-saving
654   :type 'function)
655
656 (defcustom gnus-split-methods
657   '((gnus-article-archive-name)
658     (gnus-article-nndoc-name))
659   "*Variable used to suggest where articles are to be saved.
660 For instance, if you would like to save articles related to Gnus in
661 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
662 you could set this variable to something like:
663
664  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
665    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
666
667 This variable is an alist where the key is the match and the
668 value is a list of possible files to save in if the match is
669 non-nil.
670
671 If the match is a string, it is used as a regexp match on the
672 article.  If the match is a symbol, that symbol will be funcalled
673 from the buffer of the article to be saved with the newsgroup as the
674 parameter.  If it is a list, it will be evalled in the same buffer.
675
676 If this form or function returns a string, this string will be used as a
677 possible file name; and if it returns a non-nil list, that list will be
678 used as possible file names."
679   :group 'gnus-article-saving
680   :type '(repeat (choice (list :value (fun) function)
681                          (cons :value ("" "") regexp (repeat string))
682                          (sexp :value nil))))
683
684 (defcustom gnus-page-delimiter "^\^L"
685   "*Regexp describing what to use as article page delimiters.
686 The default value is \"^\^L\", which is a form linefeed at the
687 beginning of a line."
688   :type 'regexp
689   :group 'gnus-article-various)
690
691 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
692   "*The format specification for the article mode line.
693 See `gnus-summary-mode-line-format' for a closer description.
694
695 The following additional specs are available:
696
697 %w  The article washing status.
698 %m  The number of MIME parts in the article."
699   :version "24.1"
700   :type 'string
701   :group 'gnus-article-various)
702
703 (defcustom gnus-article-mode-hook nil
704   "*A hook for Gnus article mode."
705   :type 'hook
706   :group 'gnus-article-various)
707
708 (when (featurep 'xemacs)
709   ;; Extracted from gnus-xmas-define in order to preserve user settings
710   (when (fboundp 'turn-off-scroll-in-place)
711     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
712   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
713   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
714
715 (defcustom gnus-article-menu-hook nil
716   "*Hook run after the creation of the article mode menu."
717   :type 'hook
718   :group 'gnus-article-various)
719
720 (defcustom gnus-article-prepare-hook nil
721   "*A hook called after an article has been prepared in the article buffer."
722   :type 'hook
723   :group 'gnus-article-various)
724
725 (defcustom gnus-copy-article-ignored-headers nil
726   "List of headers to be removed when copying an article.
727 Each element is a regular expression."
728   :version "23.1" ;; No Gnus
729   :type '(repeat regexp)
730   :group 'gnus-article-various)
731
732 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
733                         "Gnus 5.10 (Emacs 22.1)")
734
735 (defface gnus-button
736   '((t (:weight bold)))
737   "Face used for highlighting a button in the article buffer."
738   :group 'gnus-article-buttons)
739
740 (defcustom gnus-article-button-face 'gnus-button
741   "Face used for highlighting buttons in the article buffer.
742
743 An article button is a piece of text that you can activate by pressing
744 `RET' or `mouse-2' above it."
745   :type 'face
746   :group 'gnus-article-buttons)
747
748 (defcustom gnus-article-mouse-face 'highlight
749   "Face used for mouse highlighting in the article buffer.
750
751 Article buttons will be displayed in this face when the cursor is
752 above them."
753   :type 'face
754   :group 'gnus-article-buttons)
755
756 (defcustom gnus-signature-face 'gnus-signature
757   "Face used for highlighting a signature in the article buffer.
758 Obsolete; use the face `gnus-signature' for customizations instead."
759   :type 'face
760   :group 'gnus-article-highlight
761   :group 'gnus-article-signature)
762
763 (defface gnus-signature
764   '((t
765      (:italic t)))
766   "Face used for highlighting a signature in the article buffer."
767   :group 'gnus-article-highlight
768   :group 'gnus-article-signature)
769 ;; backward-compatibility alias
770 (put 'gnus-signature-face 'face-alias 'gnus-signature)
771 (put 'gnus-signature-face 'obsolete-face "22.1")
772
773 (defface gnus-header-from
774   '((((class color)
775       (background dark))
776      (:foreground "PaleGreen1"))
777     (((class color)
778       (background light))
779      (:foreground "red3"))
780     (t
781      (:italic t)))
782   "Face used for displaying from headers."
783   :group 'gnus-article-headers
784   :group 'gnus-article-highlight)
785 ;; backward-compatibility alias
786 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
787 (put 'gnus-header-from-face 'obsolete-face "22.1")
788
789 (defface gnus-header-subject
790   '((((class color)
791       (background dark))
792      (:foreground "SeaGreen1"))
793     (((class color)
794       (background light))
795      (:foreground "red4"))
796     (t
797      (:bold t :italic t)))
798   "Face used for displaying subject headers."
799   :group 'gnus-article-headers
800   :group 'gnus-article-highlight)
801 ;; backward-compatibility alias
802 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
803 (put 'gnus-header-subject-face 'obsolete-face "22.1")
804
805 (defface gnus-header-newsgroups
806   '((((class color)
807       (background dark))
808      (:foreground "yellow" :italic t))
809     (((class color)
810       (background light))
811      (:foreground "MidnightBlue" :italic t))
812     (t
813      (:italic t)))
814   "Face used for displaying newsgroups headers.
815 In the default setup this face is only used for crossposted
816 articles."
817   :group 'gnus-article-headers
818   :group 'gnus-article-highlight)
819 ;; backward-compatibility alias
820 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
821 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
822
823 (defface gnus-header-name
824   '((((class color)
825       (background dark))
826      (:foreground "SpringGreen2"))
827     (((class color)
828       (background light))
829      (:foreground "maroon"))
830     (t
831      (:bold t)))
832   "Face used for displaying header names."
833   :group 'gnus-article-headers
834   :group 'gnus-article-highlight)
835 ;; backward-compatibility alias
836 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
837 (put 'gnus-header-name-face 'obsolete-face "22.1")
838
839 (defface gnus-header-content
840   '((((class color)
841       (background dark))
842      (:foreground "SpringGreen1" :italic t))
843     (((class color)
844       (background light))
845      (:foreground "indianred4" :italic t))
846     (t
847      (:italic t)))  "Face used for displaying header content."
848   :group 'gnus-article-headers
849   :group 'gnus-article-highlight)
850 ;; backward-compatibility alias
851 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
852 (put 'gnus-header-content-face 'obsolete-face "22.1")
853
854 (defcustom gnus-header-face-alist
855   '(("From" nil gnus-header-from)
856     ("Subject" nil gnus-header-subject)
857     ("Newsgroups:.*," nil gnus-header-newsgroups)
858     ("" gnus-header-name gnus-header-content))
859   "*Controls highlighting of article headers.
860
861 An alist of the form (HEADER NAME CONTENT).
862
863 HEADER is a regular expression which should match the name of a
864 header and NAME and CONTENT are either face names or nil.
865
866 The name of each header field will be displayed using the face
867 specified by the first element in the list where HEADER matches
868 the header name and NAME is non-nil.  Similarly, the content will
869 be displayed by the first non-nil matching CONTENT face."
870   :group 'gnus-article-headers
871   :group 'gnus-article-highlight
872   :type '(repeat (list (regexp :tag "Header")
873                        (choice :tag "Name"
874                                (item :tag "skip" nil)
875                                (face :value default))
876                        (choice :tag "Content"
877                                (item :tag "skip" nil)
878                                (face :value default)))))
879
880 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
881                                           '((xface . (:face gnus-x-face)))
882                                         '((pbm . (:face gnus-x-face))
883                                           (png . nil)))
884   "Alist of image types and properties applied to Face and X-Face images.
885 Here are examples:
886
887 ;; Specify the altitude of Face images in the From header.
888 \(setq gnus-face-properties-alist
889       '((pbm . (:face gnus-x-face :ascent 80))
890         (png . (:ascent 80))))
891
892 ;; Show Face images as pressed buttons.
893 \(setq gnus-face-properties-alist
894       '((pbm . (:face gnus-x-face :relief -2))
895         (png . (:relief -2))))
896
897 See the manual for the valid properties for various image types.
898 Currently, `pbm' is used for X-Face images and `png' is used for Face
899 images in Emacs.  Only the `:face' property is effective on the `xface'
900 image type in XEmacs if it is built with the libcompface library."
901   :version "23.1" ;; No Gnus
902   :group 'gnus-article-headers
903   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
904
905 (defcustom gnus-article-decode-hook
906   '(article-decode-charset article-decode-encoded-words
907                            article-decode-group-name article-decode-idna-rhs)
908   "*Hook run to decode charsets in articles."
909   :group 'gnus-article-headers
910   :type 'hook)
911
912 (defcustom gnus-display-mime-function 'gnus-display-mime
913   "Function to display MIME articles."
914   :group 'gnus-article-mime
915   :type 'function)
916
917 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
918   "Function used to decode headers.")
919
920 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
921   "Function used to decode addresses.")
922
923 (defvar gnus-article-dumbquotes-map
924   '((?\200 "EUR")
925     (?\202 ",")
926     (?\203 "f")
927     (?\204 ",,")
928     (?\205 "...")
929     (?\213 "<")
930     (?\214 "OE")
931     (?\221 "`")
932     (?\222 "'")
933     (?\223 "``")
934     (?\224 "\"")
935     (?\225 "*")
936     (?\226 "-")
937     (?\227 "--")
938     (?\230 "~")
939     (?\231 "(TM)")
940     (?\233 ">")
941     (?\234 "oe")
942     (?\264 "'"))
943   "Table for MS-to-Latin1 translation.")
944
945 (defcustom gnus-ignored-mime-types nil
946   "List of MIME types that should be ignored by Gnus."
947   :version "21.1"
948   :group 'gnus-article-mime
949   :type '(repeat regexp))
950
951 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
952   "List of MIME types that should not be given buttons when rendered inline.
953 See also `gnus-buttonized-mime-types' which may override this variable.
954 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
955   :version "21.1"
956   :group 'gnus-article-mime
957   :type '(repeat regexp))
958
959 (defcustom gnus-buttonized-mime-types nil
960   "List of MIME types that should be given buttons when rendered inline.
961 If set, this variable overrides `gnus-unbuttonized-mime-types'.
962 To see e.g. security buttons you could set this to
963 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
964 this list to display radio buttons that allow you to choose one of two
965 media types those mails include.  See also `mm-discouraged-alternatives'.
966 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
967   :version "22.1"
968   :group 'gnus-article-mime
969   :type '(repeat regexp))
970
971 (defcustom gnus-inhibit-mime-unbuttonizing nil
972   "If non-nil, all MIME parts get buttons.
973 When nil (the default value), then some MIME parts do not get buttons,
974 as described by the variables `gnus-buttonized-mime-types' and
975 `gnus-unbuttonized-mime-types'."
976   :version "22.1"
977   :group 'gnus-article-mime
978   :type 'boolean)
979
980 (defcustom gnus-body-boundary-delimiter "_"
981   "String used to delimit header and body.
982 This variable is used by `gnus-article-treat-body-boundary' which can
983 be controlled by `gnus-treat-body-boundary'."
984   :version "22.1"
985   :group 'gnus-article-various
986   :type '(choice (item :tag "None" :value nil)
987                  string))
988
989 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
990                                   "/usr/share/picons")
991   "Defines the location of the faces database.
992 For information on obtaining this database of pretty pictures, please
993 see http://www.cs.indiana.edu/picons/ftp/index.html"
994   :version "22.1"
995   :type '(repeat directory)
996   :link '(url-link :tag "download"
997                    "http://www.cs.indiana.edu/picons/ftp/index.html")
998   :link '(custom-manual "(gnus)Picons")
999   :group 'gnus-picon)
1000
1001 (defun gnus-picons-installed-p ()
1002   "Say whether picons are installed on your machine."
1003   (let ((installed nil))
1004     (dolist (database gnus-picon-databases)
1005       (when (file-exists-p database)
1006         (setq installed t)))
1007     installed))
1008
1009 (defcustom gnus-article-mime-part-function nil
1010   "Function called with a MIME handle as the argument.
1011 This is meant for people who want to do something automatic based
1012 on parts -- for instance, adding Vcard info to a database."
1013   :group 'gnus-article-mime
1014   :type '(choice (const nil)
1015                  function))
1016
1017 (defcustom gnus-mime-multipart-functions nil
1018   "An alist of MIME types to functions to display them."
1019   :version "21.1"
1020   :group 'gnus-article-mime
1021   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1022
1023 (defcustom gnus-article-date-headers '(combined-lapsed)
1024   "A list of Date header formats to display.
1025 Valid formats are `ut' (universal time), `local' (local time
1026 zone), `english' (readable English), `lapsed' (elapsed time),
1027 `combined-lapsed' (both the original date and the elapsed time),
1028 `original' (the original date header), `iso8601' (ISO8601
1029 format), and `user-defined' (a user-defined format defined by the
1030 `gnus-article-time-format' variable).
1031
1032 You have as many date headers as you want in the article buffer.
1033 Some of these headers are updated automatically.  See
1034 `gnus-article-update-date-headers' for details."
1035   :version "24.1"
1036   :group 'gnus-article-headers
1037   :type '(repeat
1038           (item :tag "Universal time (UT)" :value 'ut)
1039           (item :tag "Local time zone" :value 'local)
1040           (item :tag "Readable English" :value 'english)
1041           (item :tag "Elapsed time" :value 'lapsed)
1042           (item :tag "Original and elapsed time" :value 'combined-lapsed)
1043           (item :tag "Original date header" :value 'original)
1044           (item :tag "ISO8601 format" :value 'iso8601)
1045           (item :tag "User-defined" :value 'user-defined)))
1046
1047 (defcustom gnus-article-update-date-headers nil
1048   "A number that says how often to update the date header (in seconds).
1049 If nil, don't update it at all."
1050   :version "24.1"
1051   :group 'gnus-article-headers
1052   :type '(choice
1053           (item :tag "Don't update" :value nil)
1054           integer))
1055
1056 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1057   "Function called with a MIME handle as the argument.
1058 This is meant for people who want to view first matched part.
1059 For `undisplayed-alternative' (default), the first undisplayed
1060 part or alternative part is used.  For `undisplayed', the first
1061 undisplayed part is used.  For a function, the first part which
1062 the function return t is used.  For nil, the first part is
1063 used."
1064   :version "21.1"
1065   :group 'gnus-article-mime
1066   :type '(choice
1067           (item :tag "first" :value nil)
1068           (item :tag "undisplayed" :value undisplayed)
1069           (item :tag "undisplayed or alternative"
1070                 :value undisplayed-alternative)
1071           (function)))
1072
1073 (defcustom gnus-mime-action-alist
1074   '(("save to file" . gnus-mime-save-part)
1075     ("save and strip" . gnus-mime-save-part-and-strip)
1076     ("replace with file" . gnus-mime-replace-part)
1077     ("delete part" . gnus-mime-delete-part)
1078     ("display as text" . gnus-mime-inline-part)
1079     ("view the part" . gnus-mime-view-part)
1080     ("pipe to command" . gnus-mime-pipe-part)
1081     ("toggle display" . gnus-article-press-button)
1082     ("toggle display" . gnus-article-view-part-as-charset)
1083     ("view as type" . gnus-mime-view-part-as-type)
1084     ("view internally" . gnus-mime-view-part-internally)
1085     ("view externally" . gnus-mime-view-part-externally))
1086   "An alist of actions that run on the MIME attachment."
1087   :group 'gnus-article-mime
1088   :type '(repeat (cons (string :tag "name")
1089                        (function))))
1090
1091 (defcustom gnus-auto-select-part 1
1092   "Advance to next MIME part when deleting or stripping parts.
1093
1094 When 0, point will be placed on the same part as before.  When
1095 positive (negative), move point forward (backwards) this many
1096 parts.  When nil, redisplay article."
1097   :version "23.1" ;; No Gnus
1098   :group 'gnus-article-mime
1099   :type '(choice (const nil :tag "Redisplay article.")
1100                  (const 1 :tag "Next part.")
1101                  (const 0 :tag "Current part.")
1102                  integer))
1103
1104 ;;;
1105 ;;; The treatment variables
1106 ;;;
1107
1108 (defvar gnus-part-display-hook nil
1109   "Hook called on parts that are to receive treatment.")
1110
1111 (defvar gnus-article-treat-custom
1112   '(choice (const :tag "Off" nil)
1113            (const :tag "On" t)
1114            (const :tag "Header" head)
1115            (const :tag "First" first)
1116            (const :tag "Last" last)
1117            (integer :tag "Less")
1118            (repeat :tag "Groups" regexp)
1119            (sexp :tag "Predicate")))
1120
1121 (defvar gnus-article-treat-head-custom
1122   '(choice (const :tag "Off" nil)
1123            (const :tag "Header" head)))
1124
1125 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1126                                    "text/x-patch")
1127   "Parts to treat.")
1128
1129 (defvar gnus-inhibit-treatment nil
1130   "Whether to inhibit treatment.")
1131
1132 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1133   "Highlight the signature.
1134 Valid values are nil, t, `head', `first', `last', an integer or a
1135 predicate.  See Info node `(gnus)Customizing Articles'."
1136   :group 'gnus-article-treat
1137   :link '(custom-manual "(gnus)Customizing Articles")
1138   :type gnus-article-treat-custom)
1139 (put 'gnus-treat-highlight-signature 'highlight t)
1140
1141 (defcustom gnus-treat-buttonize 100000
1142   "Add buttons.
1143 Valid values are nil, t, `head', `first', `last', an integer or a
1144 predicate.  See Info node `(gnus)Customizing Articles'."
1145   :group 'gnus-article-treat
1146   :link '(custom-manual "(gnus)Customizing Articles")
1147   :type gnus-article-treat-custom)
1148 (put 'gnus-treat-buttonize 'highlight t)
1149
1150 (defcustom gnus-treat-buttonize-head 'head
1151   "Add buttons to the head.
1152 Valid values are nil, t, `head', `first', `last', an integer or a
1153 predicate.  See Info node `(gnus)Customizing Articles'."
1154   :group 'gnus-article-treat
1155   :link '(custom-manual "(gnus)Customizing Articles")
1156   :type gnus-article-treat-head-custom)
1157 (put 'gnus-treat-buttonize-head 'highlight t)
1158
1159 (defcustom gnus-treat-date 'head
1160   "Display dates according to the `gnus-article-date-headers' variable.
1161 Valid values are nil, t, `head', `first', `last', an integer or a
1162 predicate.  See Info node `(gnus)Customizing Articles'."
1163   :version "24.1"
1164   :group 'gnus-article-treat
1165   :link '(custom-manual "(gnus)Customizing Articles")
1166   :type gnus-article-treat-head-custom)
1167
1168 (defcustom gnus-treat-emphasize 50000
1169   "Emphasize text.
1170 Valid values are nil, t, `head', `first', `last', an integer or a
1171 predicate.  See Info node `(gnus)Customizing Articles'."
1172   :group 'gnus-article-treat
1173   :link '(custom-manual "(gnus)Customizing Articles")
1174   :type gnus-article-treat-custom)
1175 (put 'gnus-treat-emphasize 'highlight t)
1176
1177 (defcustom gnus-treat-strip-cr nil
1178   "Remove carriage returns.
1179 Valid values are nil, t, `head', `first', `last', an integer or a
1180 predicate.  See Info node `(gnus)Customizing Articles'."
1181   :version "22.1"
1182   :group 'gnus-article-treat
1183   :link '(custom-manual "(gnus)Customizing Articles")
1184   :type gnus-article-treat-custom)
1185
1186 (defcustom gnus-treat-unsplit-urls nil
1187   "Remove newlines from within URLs.
1188 Valid values are nil, t, `head', `first', `last', an integer or a
1189 predicate.  See Info node `(gnus)Customizing Articles'."
1190   :version "22.1"
1191   :group 'gnus-article-treat
1192   :link '(custom-manual "(gnus)Customizing Articles")
1193   :type gnus-article-treat-custom)
1194
1195 (defcustom gnus-treat-leading-whitespace nil
1196   "Remove leading whitespace in headers.
1197 Valid values are nil, t, `head', `first', `last', an integer or a
1198 predicate.  See Info node `(gnus)Customizing Articles'."
1199   :version "22.1"
1200   :group 'gnus-article-treat
1201   :link '(custom-manual "(gnus)Customizing Articles")
1202   :type gnus-article-treat-custom)
1203
1204 (defcustom gnus-treat-hide-headers 'head
1205   "Hide headers.
1206 Valid values are nil, t, `head', `first', `last', an integer or a
1207 predicate.  See Info node `(gnus)Customizing Articles'."
1208   :group 'gnus-article-treat
1209   :link '(custom-manual "(gnus)Customizing Articles")
1210   :type gnus-article-treat-head-custom)
1211
1212 (defcustom gnus-treat-hide-boring-headers nil
1213   "Hide boring headers.
1214 Valid values are nil, t, `head', `first', `last', an integer or a
1215 predicate.  See Info node `(gnus)Customizing Articles'."
1216   :group 'gnus-article-treat
1217   :link '(custom-manual "(gnus)Customizing Articles")
1218   :type gnus-article-treat-head-custom)
1219
1220 (defcustom gnus-treat-hide-signature nil
1221   "Hide the signature.
1222 Valid values are nil, t, `head', `first', `last', an integer or a
1223 predicate.  See Info node `(gnus)Customizing Articles'."
1224   :group 'gnus-article-treat
1225   :link '(custom-manual "(gnus)Customizing Articles")
1226   :type gnus-article-treat-custom)
1227
1228 (defcustom gnus-treat-fill-article nil
1229   "Fill the article.
1230 Valid values are nil, t, `head', `first', `last', an integer or a
1231 predicate.  See Info node `(gnus)Customizing Articles'."
1232   :group 'gnus-article-treat
1233   :link '(custom-manual "(gnus)Customizing Articles")
1234   :type gnus-article-treat-custom)
1235
1236 (defcustom gnus-treat-hide-citation nil
1237   "Hide cited text.
1238 Valid values are nil, t, `head', `first', `last', an integer or a
1239 predicate.  See Info node `(gnus)Customizing Articles'.
1240
1241 See `gnus-article-highlight-citation' for variables used to
1242 control what it hides."
1243   :group 'gnus-article-treat
1244   :link '(custom-manual "(gnus)Customizing Articles")
1245   :type gnus-article-treat-custom)
1246
1247 (defcustom gnus-treat-hide-citation-maybe nil
1248   "Hide cited text according to certain conditions.
1249 Valid values are nil, t, `head', `first', `last', an integer or a
1250 predicate.  See Info node `(gnus)Customizing Articles'.
1251
1252 See `gnus-cite-hide-percentage' and `gnus-cite-hide-absolute' for
1253 how to control what it hides."
1254   :group 'gnus-article-treat
1255   :link '(custom-manual "(gnus)Customizing Articles")
1256   :type gnus-article-treat-custom)
1257
1258 (defcustom gnus-treat-strip-list-identifiers 'head
1259   "Strip list identifiers from `gnus-list-identifiers`.
1260 Valid values are nil, t, `head', `first', `last', an integer or a
1261 predicate.  See Info node `(gnus)Customizing Articles'."
1262   :version "21.1"
1263   :group 'gnus-article-treat
1264   :link '(custom-manual "(gnus)Customizing Articles")
1265   :type gnus-article-treat-custom)
1266
1267 (gnus-define-group-parameter
1268  list-identifier
1269  :variable-document
1270  "Alist of regexps and correspondent identifiers."
1271  :variable-group gnus-article-washing
1272  :parameter-type
1273  '(choice :tag "Identifier"
1274           :value nil
1275           (symbol :tag "Item in `gnus-list-identifiers'" none)
1276           regexp
1277           (const :tag "None" nil))
1278  :parameter-document
1279  "If non-nil, specify how to remove `identifiers' from articles' subject.
1280
1281 Any symbol is used to look up a regular expression to match the
1282 banner in `gnus-list-identifiers'.  A string is used as a regular
1283 expression to match the identifier directly.")
1284
1285 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1286                         "Gnus 5.10 (Emacs 22.1)")
1287
1288 (defcustom gnus-treat-strip-pem nil
1289   "Strip PEM signatures.
1290 Valid values are nil, t, `head', `first', `last', an integer or a
1291 predicate.  See Info node `(gnus)Customizing Articles'."
1292   :group 'gnus-article-treat
1293   :link '(custom-manual "(gnus)Customizing Articles")
1294   :type gnus-article-treat-custom)
1295
1296 (defcustom gnus-treat-strip-banner t
1297   "Strip banners from articles.
1298 The banner to be stripped is specified in the `banner' group parameter.
1299 Valid values are nil, t, `head', `first', `last', an integer or a
1300 predicate.  See Info node `(gnus)Customizing Articles'."
1301   :group 'gnus-article-treat
1302   :link '(custom-manual "(gnus)Customizing Articles")
1303   :type gnus-article-treat-custom)
1304
1305 (defcustom gnus-treat-highlight-headers 'head
1306   "Highlight the headers.
1307 Valid values are nil, t, `head', `first', `last', an integer or a
1308 predicate.  See Info node `(gnus)Customizing Articles'."
1309   :group 'gnus-article-treat
1310   :link '(custom-manual "(gnus)Customizing Articles")
1311   :type gnus-article-treat-head-custom)
1312 (put 'gnus-treat-highlight-headers 'highlight t)
1313
1314 (defcustom gnus-treat-highlight-citation t
1315   "Highlight cited text.
1316 Valid values are nil, t, `head', `first', `last', an integer or a
1317 predicate.  See Info node `(gnus)Customizing Articles'."
1318   :group 'gnus-article-treat
1319   :link '(custom-manual "(gnus)Customizing Articles")
1320   :type gnus-article-treat-custom)
1321 (put 'gnus-treat-highlight-citation 'highlight t)
1322
1323 (defcustom gnus-treat-strip-headers-in-body t
1324   "Strip the X-No-Archive header line from the beginning of the body.
1325 Valid values are nil, t, `head', `first', `last', an integer or a
1326 predicate.  See Info node `(gnus)Customizing Articles'."
1327   :version "21.1"
1328   :group 'gnus-article-treat
1329   :link '(custom-manual "(gnus)Customizing Articles")
1330   :type gnus-article-treat-custom)
1331
1332 (defcustom gnus-treat-strip-trailing-blank-lines nil
1333   "Strip trailing blank lines.
1334 Valid values are nil, t, `head', `first', `last', an integer or a
1335 predicate.  See Info node `(gnus)Customizing Articles'.
1336
1337 When set to t, it also strips trailing blanks in all MIME parts.
1338 Consider to use `last' instead."
1339   :group 'gnus-article-treat
1340   :link '(custom-manual "(gnus)Customizing Articles")
1341   :type gnus-article-treat-custom)
1342
1343 (defcustom gnus-treat-strip-leading-blank-lines nil
1344   "Strip leading blank lines.
1345 Valid values are nil, t, `head', `first', `last', an integer or a
1346 predicate.  See Info node `(gnus)Customizing Articles'.
1347
1348 When set to t, it also strips trailing blanks in all MIME parts."
1349   :group 'gnus-article-treat
1350   :link '(custom-manual "(gnus)Customizing Articles")
1351   :type gnus-article-treat-custom)
1352
1353 (defcustom gnus-treat-strip-multiple-blank-lines nil
1354   "Strip multiple blank lines.
1355 Valid values are nil, t, `head', `first', `last', an integer or a
1356 predicate.  See Info node `(gnus)Customizing Articles'."
1357   :group 'gnus-article-treat
1358   :link '(custom-manual "(gnus)Customizing Articles")
1359   :type gnus-article-treat-custom)
1360
1361 (defcustom gnus-treat-unfold-headers 'head
1362   "Unfold folded header lines.
1363 Valid values are nil, t, `head', `first', `last', an integer or a
1364 predicate.  See Info node `(gnus)Customizing Articles'."
1365   :version "22.1"
1366   :group 'gnus-article-treat
1367   :link '(custom-manual "(gnus)Customizing Articles")
1368   :type gnus-article-treat-custom)
1369
1370 (defcustom gnus-article-unfold-long-headers nil
1371   "If non-nil, allow unfolding headers even if the header is long.
1372 If it is a regexp, only long headers matching this regexp are unfolded.
1373 If it is t, all long headers are unfolded.
1374
1375 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1376   :version "23.1" ;; No Gnus
1377   :group 'gnus-article-treat
1378   :type '(choice (const nil)
1379                  (const :tag "all" t)
1380                  (regexp)))
1381
1382 (defcustom gnus-treat-fold-headers nil
1383   "Fold headers.
1384 Valid values are nil, t, `head', `first', `last', an integer or a
1385 predicate.  See Info node `(gnus)Customizing Articles'."
1386   :version "22.1"
1387   :group 'gnus-article-treat
1388   :link '(custom-manual "(gnus)Customizing Articles")
1389   :type gnus-article-treat-custom)
1390
1391 (defcustom gnus-treat-fold-newsgroups 'head
1392   "Fold the Newsgroups and Followup-To headers.
1393 Valid values are nil, t, `head', `first', `last', an integer or a
1394 predicate.  See Info node `(gnus)Customizing Articles'."
1395   :version "22.1"
1396   :group 'gnus-article-treat
1397   :link '(custom-manual "(gnus)Customizing Articles")
1398   :type gnus-article-treat-custom)
1399
1400 (defcustom gnus-treat-overstrike t
1401   "Treat overstrike highlighting.
1402 Valid values are nil, t, `head', `first', `last', an integer or a
1403 predicate.  See Info node `(gnus)Customizing Articles'."
1404   :group 'gnus-article-treat
1405   :link '(custom-manual "(gnus)Customizing Articles")
1406   :type gnus-article-treat-custom)
1407 (put 'gnus-treat-overstrike 'highlight t)
1408
1409 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1410   "Treat ANSI SGR control sequences.
1411 Valid values are nil, t, `head', `first', `last', an integer or a
1412 predicate.  See Info node `(gnus)Customizing Articles'."
1413   :group 'gnus-article-treat
1414   :link '(custom-manual "(gnus)Customizing Articles")
1415   :type gnus-article-treat-custom)
1416
1417 (make-obsolete-variable 'gnus-treat-display-xface
1418                         'gnus-treat-display-x-face "Emacs 22.1")
1419
1420 (defcustom gnus-treat-display-x-face
1421   (and (not noninteractive)
1422        (gnus-image-type-available-p 'xbm)
1423        (if (featurep 'xemacs)
1424            (featurep 'xface)
1425          (condition-case nil
1426              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1427                   (executable-find "icontopbm"))
1428            ;; shell-command-to-string may signal an error, e.g. if
1429            ;; shell-file-name is not found.
1430            (error nil)))
1431        'head)
1432   "Display X-Face headers.
1433 Valid values are nil and `head'.
1434 See Info node `(gnus)Customizing Articles' and Info node
1435 `(gnus)X-Face' for details."
1436   :group 'gnus-article-treat
1437   :version "21.1"
1438   :link '(custom-manual "(gnus)Customizing Articles")
1439   :link '(custom-manual "(gnus)X-Face")
1440   :type gnus-article-treat-head-custom
1441   :set (lambda (symbol value)
1442          (set-default
1443           symbol
1444           (cond ((or (boundp symbol) (get symbol 'saved-value))
1445                  value)
1446                 ((boundp 'gnus-treat-display-xface)
1447                  (message "\
1448 ** gnus-treat-display-xface is an obsolete variable;\
1449  use gnus-treat-display-x-face instead")
1450                  (default-value 'gnus-treat-display-xface))
1451                 ((get 'gnus-treat-display-xface 'saved-value)
1452                  (message "\
1453 ** gnus-treat-display-xface is an obsolete variable;\
1454  use gnus-treat-display-x-face instead")
1455                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1456                 (t
1457                  value)))))
1458 (put 'gnus-treat-display-x-face 'highlight t)
1459
1460 (defcustom gnus-treat-display-face
1461   (and (not noninteractive)
1462        (gnus-image-type-available-p 'png)
1463        'head)
1464   "Display Face headers.
1465 Valid values are nil, t, `head', `first', `last', an integer or a
1466 predicate.  See Info node `(gnus)Customizing Articles' and Info
1467 node `(gnus)Face' for details."
1468   :group 'gnus-article-treat
1469   :version "22.1"
1470   :link '(custom-manual "(gnus)Customizing Articles")
1471   :link '(custom-manual "(gnus)X-Face")
1472   :type gnus-article-treat-head-custom)
1473 (put 'gnus-treat-display-face 'highlight t)
1474
1475 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1476   "Display smileys.
1477 Valid values are nil, t, `head', `first', `last', an integer or a
1478 predicate.  See Info node `(gnus)Customizing Articles' and Info
1479 node `(gnus)Smileys' for details."
1480   :group 'gnus-article-treat
1481   :version "21.1"
1482   :link '(custom-manual "(gnus)Customizing Articles")
1483   :link '(custom-manual "(gnus)Smileys")
1484   :type gnus-article-treat-custom)
1485 (put 'gnus-treat-display-smileys 'highlight t)
1486
1487 (defcustom gnus-treat-from-picon
1488   (if (and (gnus-image-type-available-p 'xpm)
1489            (gnus-picons-installed-p))
1490       'head nil)
1491   "Display picons in the From header.
1492 Valid values are nil, t, `head', `first', `last', an integer or a
1493 predicate.  See Info node `(gnus)Customizing Articles' and Info
1494 node `(gnus)Picons' for details."
1495   :version "22.1"
1496   :group 'gnus-article-treat
1497   :group 'gnus-picon
1498   :link '(custom-manual "(gnus)Customizing Articles")
1499   :link '(custom-manual "(gnus)Picons")
1500   :type gnus-article-treat-head-custom)
1501 (put 'gnus-treat-from-picon 'highlight t)
1502
1503 (defcustom gnus-treat-mail-picon
1504   (if (and (gnus-image-type-available-p 'xpm)
1505            (gnus-picons-installed-p))
1506       'head nil)
1507   "Display picons in To and Cc headers.
1508 Valid values are nil, t, `head', `first', `last', an integer or a
1509 predicate.  See Info node `(gnus)Customizing Articles' and Info
1510 node `(gnus)Picons' for details."
1511   :version "22.1"
1512   :group 'gnus-article-treat
1513   :group 'gnus-picon
1514   :link '(custom-manual "(gnus)Customizing Articles")
1515   :link '(custom-manual "(gnus)Picons")
1516   :type gnus-article-treat-head-custom)
1517 (put 'gnus-treat-mail-picon 'highlight t)
1518
1519 (defcustom gnus-treat-newsgroups-picon
1520   (if (and (gnus-image-type-available-p 'xpm)
1521            (gnus-picons-installed-p))
1522       'head nil)
1523   "Display picons in the Newsgroups and Followup-To headers.
1524 Valid values are nil, t, `head', `first', `last', an integer or a
1525 predicate.  See Info node `(gnus)Customizing Articles' and Info
1526 node `(gnus)Picons' for details."
1527   :version "22.1"
1528   :group 'gnus-article-treat
1529   :group 'gnus-picon
1530   :link '(custom-manual "(gnus)Customizing Articles")
1531   :link '(custom-manual "(gnus)Picons")
1532   :type gnus-article-treat-head-custom)
1533 (put 'gnus-treat-newsgroups-picon 'highlight t)
1534
1535 (defcustom gnus-treat-from-gravatar nil
1536   "Display gravatars in the From header.
1537 Valid values are nil, t, `head', `first', `last', an integer or a
1538 predicate.  See Info node `(gnus)Customizing Articles' and Info
1539 node `(gnus)Gravatars' for details."
1540   :version "24.1"
1541   :group 'gnus-article-treat
1542   :group 'gnus-gravatar
1543   :link '(custom-manual "(gnus)Customizing Articles")
1544   :link '(custom-manual "(gnus)Gravatars")
1545   :type gnus-article-treat-head-custom)
1546 (put 'gnus-treat-from-gravatar 'highlight t)
1547
1548 (defcustom gnus-treat-mail-gravatar nil
1549   "Display gravatars in To and Cc headers.
1550 Valid values are nil, t, `head', `first', `last', an integer or a
1551 predicate.  See Info node `(gnus)Customizing Articles' and Info
1552 node `(gnus)Gravatars' for details."
1553   :version "24.1"
1554   :group 'gnus-article-treat
1555   :group 'gnus-gravatar
1556   :link '(custom-manual "(gnus)Customizing Articles")
1557   :link '(custom-manual "(gnus)Gravatars")
1558   :type gnus-article-treat-head-custom)
1559 (put 'gnus-treat-mail-gravatar 'highlight t)
1560
1561 (defcustom gnus-treat-body-boundary
1562   (if (or gnus-treat-newsgroups-picon
1563           gnus-treat-mail-picon
1564           gnus-treat-from-picon
1565           gnus-treat-from-gravatar
1566           gnus-treat-mail-gravatar)
1567       ;; If there's much decoration, the user might prefer a boundary.
1568       'head
1569     nil)
1570   "Draw a boundary at the end of the headers.
1571 Valid values are nil and `head'.
1572 See Info node `(gnus)Customizing Articles' for details."
1573   :version "22.1"
1574   :group 'gnus-article-treat
1575   :link '(custom-manual "(gnus)Customizing Articles")
1576   :type gnus-article-treat-head-custom)
1577
1578 (defcustom gnus-treat-capitalize-sentences nil
1579   "Capitalize sentence-starting words.
1580 Valid values are nil, t, `head', `first', `last', an integer or a
1581 predicate.  See Info node `(gnus)Customizing Articles'."
1582   :version "21.1"
1583   :group 'gnus-article-treat
1584   :link '(custom-manual "(gnus)Customizing Articles")
1585   :type gnus-article-treat-custom)
1586
1587 (defcustom gnus-treat-wash-html nil
1588   "Format as HTML.
1589 Valid values are nil, t, `head', `first', `last', an integer or a
1590 predicate.  See Info node `(gnus)Customizing Articles'."
1591   :version "22.1"
1592   :group 'gnus-article-treat
1593   :link '(custom-manual "(gnus)Customizing Articles")
1594   :type gnus-article-treat-custom)
1595
1596 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1597   "Fill long lines.
1598 Valid values are nil, t, `head', `first', `last', an integer or a
1599 predicate.  See Info node `(gnus)Customizing Articles'."
1600   :version "24.1"
1601   :group 'gnus-article-treat
1602   :link '(custom-manual "(gnus)Customizing Articles")
1603   :type gnus-article-treat-custom)
1604
1605 (defcustom gnus-treat-x-pgp-sig nil
1606   "Verify X-PGP-Sig.
1607 To automatically treat X-PGP-Sig, set it to head.
1608 Valid values are nil, t, `head', `first', `last', an integer or a
1609 predicate.  See Info node `(gnus)Customizing Articles'."
1610   :version "22.1"
1611   :group 'gnus-article-treat
1612   :group 'mime-security
1613   :link '(custom-manual "(gnus)Customizing Articles")
1614   :type gnus-article-treat-custom)
1615
1616 (defvar gnus-article-encrypt-protocol-alist
1617   '(("PGP" . mml2015-self-encrypt)))
1618
1619 ;; Set to nil if more than one protocol added to
1620 ;; gnus-article-encrypt-protocol-alist.
1621 (defcustom gnus-article-encrypt-protocol "PGP"
1622   "The protocol used for encrypt articles.
1623 It is a string, such as \"PGP\". If nil, ask user."
1624   :version "22.1"
1625   :type 'string
1626   :group 'mime-security)
1627
1628 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1629                               (mm-coding-system-p 'utf-8)
1630                               (executable-find idna-program))
1631   "Whether IDNA decoding of headers is used when viewing messages.
1632 This requires GNU Libidn, and by default only enabled if it is found."
1633   :version "22.1"
1634   :group 'gnus-article-headers
1635   :type 'boolean)
1636
1637 (defcustom gnus-article-over-scroll nil
1638   "If non-nil, allow scrolling the article buffer even when there no more text."
1639   :version "22.1"
1640   :group 'gnus-article
1641   :type 'boolean)
1642
1643 (defcustom gnus-inhibit-images nil
1644   "Non-nil means inhibit displaying of images inline in the article body."
1645   :version "24.1"
1646   :group 'gnus-article
1647   :type 'boolean)
1648
1649 (defcustom gnus-blocked-images 'gnus-block-private-groups
1650   "Images that have URLs matching this regexp will be blocked.
1651 This can also be a function to be evaluated.  If so, it will be
1652 called with the group name as the parameter, and should return a
1653 regexp."
1654   :version "24.1"
1655   :group 'gnus-art
1656   :type 'regexp)
1657
1658 ;;; Internal variables
1659
1660 (defvar gnus-english-month-names
1661   '("January" "February" "March" "April" "May" "June" "July" "August"
1662     "September" "October" "November" "December"))
1663
1664 (defvar article-goto-body-goes-to-point-min-p nil)
1665 (defvar gnus-article-wash-types nil)
1666 (defvar gnus-article-emphasis-alist nil)
1667 (defvar gnus-article-image-alist nil)
1668
1669 (defvar gnus-article-mime-handle-alist-1 nil)
1670 (defvar gnus-treatment-function-alist
1671   '((gnus-treat-strip-cr gnus-article-remove-cr)
1672     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1673     (gnus-treat-strip-banner gnus-article-strip-banner)
1674     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1675     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1676     (gnus-treat-buttonize gnus-article-add-buttons)
1677     (gnus-treat-fill-article gnus-article-fill-cited-article)
1678     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1679     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1680     (gnus-treat-display-x-face gnus-article-display-x-face)
1681     (gnus-treat-display-face gnus-article-display-face)
1682     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1683     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1684     (gnus-treat-hide-signature gnus-article-hide-signature)
1685     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1686     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1687     (gnus-treat-from-picon gnus-treat-from-picon)
1688     (gnus-treat-mail-picon gnus-treat-mail-picon)
1689     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1690     (gnus-treat-strip-pem gnus-article-hide-pem)
1691     (gnus-treat-date gnus-article-treat-date)
1692     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1693     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1694     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1695     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1696     (gnus-treat-strip-trailing-blank-lines
1697      gnus-article-remove-trailing-blank-lines)
1698     (gnus-treat-strip-leading-blank-lines
1699      gnus-article-strip-leading-blank-lines)
1700     (gnus-treat-strip-multiple-blank-lines
1701      gnus-article-strip-multiple-blank-lines)
1702     (gnus-treat-overstrike gnus-article-treat-overstrike)
1703     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1704     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1705     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1706     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1707     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1708     (gnus-treat-display-smileys gnus-treat-smiley)
1709     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1710     (gnus-treat-wash-html gnus-article-wash-html)
1711     (gnus-treat-emphasize gnus-article-emphasize)
1712     (gnus-treat-hide-citation gnus-article-hide-citation)
1713     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1714     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1715     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1716
1717 (defvar gnus-article-mime-handle-alist nil)
1718 (defvar article-lapsed-timer nil)
1719 (defvar gnus-article-current-summary nil)
1720
1721 (defvar gnus-article-mode-syntax-table
1722   (let ((table (copy-syntax-table text-mode-syntax-table)))
1723     ;; This causes the citation match run O(2^n).
1724     ;; (modify-syntax-entry ?- "w" table)
1725     (modify-syntax-entry ?> ")<" table)
1726     (modify-syntax-entry ?< "(>" table)
1727     ;; make M-. in article buffers work for `foo' strings
1728     (modify-syntax-entry ?' " " table)
1729     (modify-syntax-entry ?` " " table)
1730     table)
1731   "Syntax table used in article mode buffers.
1732 Initialized from `text-mode-syntax-table.")
1733
1734 (defvar gnus-save-article-buffer nil)
1735
1736 (defvar gnus-number-of-articles-to-be-saved nil)
1737
1738 (defvar gnus-inhibit-hiding nil)
1739
1740 (defvar gnus-article-edit-mode nil)
1741
1742 ;;; Macros for dealing with the article buffer.
1743
1744 (defmacro gnus-with-article-headers (&rest forms)
1745   `(with-current-buffer gnus-article-buffer
1746      (save-restriction
1747        (let ((inhibit-read-only t)
1748              (inhibit-point-motion-hooks t)
1749              (case-fold-search t))
1750          (article-narrow-to-head)
1751          ,@forms))))
1752
1753 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1754 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1755
1756 (defmacro gnus-with-article-buffer (&rest forms)
1757   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1758      (with-current-buffer gnus-article-buffer
1759        (let ((inhibit-read-only t))
1760          ,@forms))))
1761
1762 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1763 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1764
1765 (defun gnus-article-goto-header (header)
1766   "Go to HEADER, which is a regular expression."
1767   (re-search-forward (concat "^\\(" header "\\):") nil t))
1768
1769 (defsubst gnus-article-hide-text (b e props)
1770   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1771   (gnus-add-text-properties-when 'article-type nil b e props)
1772   (when (memq 'intangible props)
1773     (put-text-property
1774      (max (1- b) (point-min))
1775      b 'intangible (cddr (memq 'intangible props)))))
1776
1777 (defsubst gnus-article-unhide-text (b e)
1778   "Remove hidden text properties from region between B and E."
1779   (remove-text-properties b e gnus-hidden-properties)
1780   (when (memq 'intangible gnus-hidden-properties)
1781     (put-text-property (max (1- b) (point-min))
1782                        b 'intangible nil)))
1783
1784 (defun gnus-article-hide-text-type (b e type)
1785   "Hide text of TYPE between B and E."
1786   (gnus-add-wash-type type)
1787   (gnus-article-hide-text
1788    b e (cons 'article-type (cons type gnus-hidden-properties))))
1789
1790 (defun gnus-article-unhide-text-type (b e type)
1791   "Unhide text of TYPE between B and E."
1792   (gnus-delete-wash-type type)
1793   (remove-text-properties
1794    b e (cons 'article-type (cons type gnus-hidden-properties)))
1795   (when (memq 'intangible gnus-hidden-properties)
1796     (put-text-property (max (1- b) (point-min))
1797                        b 'intangible nil)))
1798
1799 (defun gnus-article-delete-text-of-type (type)
1800   "Delete text of TYPE in the current buffer."
1801   (save-excursion
1802     (let ((b (point-min)))
1803       (if (eq type 'multipart)
1804           ;; Remove MIME buttons associated with multipart/alternative parts.
1805           (progn
1806             (goto-char b)
1807             (while (if (get-text-property (point) 'gnus-part)
1808                        (setq b (point))
1809                      (when (setq b (next-single-property-change (point)
1810                                                                 'gnus-part))
1811                        (goto-char b)
1812                        t))
1813               (end-of-line)
1814               (skip-chars-forward "\n")
1815               (when (eq (get-text-property b 'article-type) 'multipart)
1816                 (delete-region b (point)))))
1817         (while (setq b (text-property-any b (point-max) 'article-type type))
1818           (delete-region
1819            b (or (text-property-not-all b (point-max) 'article-type type)
1820                  (point-max))))))))
1821
1822 (defun gnus-article-delete-invisible-text ()
1823   "Delete all invisible text in the current buffer."
1824   (save-excursion
1825     (let ((b (point-min)))
1826       (while (setq b (text-property-any b (point-max) 'invisible t))
1827         (delete-region
1828          b (or (text-property-not-all b (point-max) 'invisible t)
1829                (point-max)))))))
1830
1831 (defsubst gnus-article-header-rank ()
1832   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1833   (let ((list gnus-sorted-header-list)
1834         (i 1))
1835     (while list
1836       (if (looking-at (car list))
1837           (setq list nil)
1838         (setq list (cdr list))
1839         (incf i)))
1840       i))
1841
1842 (defun article-hide-headers (&optional arg delete)
1843   "Hide unwanted headers and possibly sort them as well."
1844   (interactive)
1845   ;; This function might be inhibited.
1846   (unless gnus-inhibit-hiding
1847     (let ((inhibit-read-only t)
1848           (case-fold-search t)
1849           (max (1+ (length gnus-sorted-header-list)))
1850           (inhibit-point-motion-hooks t)
1851           (cur (current-buffer))
1852           ignored visible beg)
1853       (save-excursion
1854         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1855         ;; group parameters, so we should go to the summary buffer.
1856         (when (prog1
1857                   (condition-case nil
1858                       (progn (set-buffer gnus-summary-buffer) t)
1859                     (error nil))
1860                 (setq ignored (when (not gnus-visible-headers)
1861                                 (cond ((stringp gnus-ignored-headers)
1862                                        gnus-ignored-headers)
1863                                       ((listp gnus-ignored-headers)
1864                                        (mapconcat 'identity
1865                                                   gnus-ignored-headers
1866                                                   "\\|"))))
1867                       visible (cond ((stringp gnus-visible-headers)
1868                                      gnus-visible-headers)
1869                                     ((and gnus-visible-headers
1870                                           (listp gnus-visible-headers))
1871                                      (mapconcat 'identity
1872                                                 gnus-visible-headers
1873                                                 "\\|")))))
1874           (set-buffer cur))
1875         (save-restriction
1876           ;; First we narrow to just the headers.
1877           (article-narrow-to-head)
1878           ;; Hide any "From " lines at the beginning of (mail) articles.
1879           (while (looking-at "From ")
1880             (forward-line 1))
1881           (unless (bobp)
1882             (delete-region (point-min) (point)))
1883           ;; Then treat the rest of the header lines.
1884           ;; Then we use the two regular expressions
1885           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1886           ;; select which header lines is to remain visible in the
1887           ;; article buffer.
1888           (while (re-search-forward "^[^ \t:]*:" nil t)
1889             (beginning-of-line)
1890             ;; Mark the rank of the header.
1891             (put-text-property
1892              (point) (1+ (point)) 'message-rank
1893              (if (or (and visible (looking-at visible))
1894                      (and ignored
1895                           (not (looking-at ignored))))
1896                  (gnus-article-header-rank)
1897                (+ 2 max)))
1898             (forward-line 1))
1899           (message-sort-headers-1)
1900           (when (setq beg (text-property-any
1901                            (point-min) (point-max) 'message-rank (+ 2 max)))
1902             ;; We delete the unwanted headers.
1903             (gnus-add-wash-type 'headers)
1904             (add-text-properties (point-min) (+ 5 (point-min))
1905                                  '(article-type headers dummy-invisible t))
1906             (delete-region beg (point-max))))))))
1907
1908 (defun article-hide-boring-headers (&optional arg)
1909   "Toggle hiding of headers that aren't very interesting.
1910 If given a negative prefix, always show; if given a positive prefix,
1911 always hide."
1912   (interactive (gnus-article-hidden-arg))
1913   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1914              (not gnus-show-all-headers))
1915     (save-excursion
1916       (save-restriction
1917         (let ((inhibit-read-only t)
1918               (inhibit-point-motion-hooks t))
1919           (article-narrow-to-head)
1920           (dolist (elem gnus-boring-article-headers)
1921             (goto-char (point-min))
1922             (cond
1923              ;; Hide empty headers.
1924              ((eq elem 'empty)
1925               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1926                 (forward-line -1)
1927                 (gnus-article-hide-text-type
1928                  (point-at-bol)
1929                  (progn
1930                    (end-of-line)
1931                    (if (re-search-forward "^[^ \t]" nil t)
1932                        (match-beginning 0)
1933                      (point-max)))
1934                  'boring-headers)))
1935              ;; Hide boring Newsgroups header.
1936              ((eq elem 'newsgroups)
1937               (when (gnus-string-equal
1938                      (gnus-fetch-field "newsgroups")
1939                      (gnus-group-real-name
1940                       (if (boundp 'gnus-newsgroup-name)
1941                           gnus-newsgroup-name
1942                         "")))
1943                 (gnus-article-hide-header "newsgroups")))
1944              ((eq elem 'to-address)
1945               (let ((to (message-fetch-field "to"))
1946                     (to-address
1947                      (gnus-parameter-to-address
1948                       (if (boundp 'gnus-newsgroup-name)
1949                           gnus-newsgroup-name ""))))
1950                 (when (and to to-address
1951                            (ignore-errors
1952                              (gnus-string-equal
1953                               ;; only one address in To
1954                               (nth 1 (mail-extract-address-components to))
1955                               to-address)))
1956                   (gnus-article-hide-header "to"))))
1957              ((eq elem 'to-list)
1958               (let ((to (message-fetch-field "to"))
1959                     (to-list
1960                      (gnus-parameter-to-list
1961                       (if (boundp 'gnus-newsgroup-name)
1962                           gnus-newsgroup-name ""))))
1963                 (when (and to to-list
1964                            (ignore-errors
1965                              (gnus-string-equal
1966                               ;; only one address in To
1967                               (nth 1 (mail-extract-address-components to))
1968                               to-list)))
1969                   (gnus-article-hide-header "to"))))
1970              ((eq elem 'cc-list)
1971               (let ((cc (message-fetch-field "cc"))
1972                     (to-list
1973                      (gnus-parameter-to-list
1974                       (if (boundp 'gnus-newsgroup-name)
1975                           gnus-newsgroup-name ""))))
1976                 (when (and cc to-list
1977                            (ignore-errors
1978                              (gnus-string-equal
1979                               ;; only one address in CC
1980                               (nth 1 (mail-extract-address-components cc))
1981                               to-list)))
1982                   (gnus-article-hide-header "cc"))))
1983              ((eq elem 'followup-to)
1984               (when (gnus-string-equal
1985                      (message-fetch-field "followup-to")
1986                      (message-fetch-field "newsgroups"))
1987                 (gnus-article-hide-header "followup-to")))
1988              ((eq elem 'reply-to)
1989               (if (gnus-group-find-parameter
1990                    gnus-newsgroup-name 'broken-reply-to)
1991                   (gnus-article-hide-header "reply-to")
1992                 (let ((from (message-fetch-field "from"))
1993                       (reply-to (message-fetch-field "reply-to")))
1994                   (when
1995                       (and
1996                        from reply-to
1997                        (ignore-errors
1998                          (equal
1999                           (sort (mapcar
2000                                  (lambda (x) (downcase (cadr x)))
2001                                  (mail-extract-address-components from t))
2002                                 'string<)
2003                           (sort (mapcar
2004                                  (lambda (x) (downcase (cadr x)))
2005                                  (mail-extract-address-components reply-to t))
2006                                 'string<))))
2007                     (gnus-article-hide-header "reply-to")))))
2008              ((eq elem 'date)
2009               (let ((date (with-current-buffer gnus-original-article-buffer
2010                             ;; If date in `gnus-article-buffer' is localized
2011                             ;; (`gnus-treat-date-user-defined'),
2012                             ;; `days-between' might fail.
2013                             (message-fetch-field "date"))))
2014                 (when (and date
2015                            (< (days-between (current-time-string) date)
2016                               4))
2017                   (gnus-article-hide-header "date"))))
2018              ((eq elem 'long-to)
2019               (let ((to (message-fetch-field "to"))
2020                     (cc (message-fetch-field "cc")))
2021                 (when (> (length to) 1024)
2022                   (gnus-article-hide-header "to"))
2023                 (when (> (length cc) 1024)
2024                   (gnus-article-hide-header "cc"))))
2025              ((eq elem 'many-to)
2026               (let ((to-count 0)
2027                     (cc-count 0))
2028                 (goto-char (point-min))
2029                 (while (re-search-forward "^to:" nil t)
2030                   (setq to-count (1+ to-count)))
2031                 (when (> to-count 1)
2032                   (while (> to-count 0)
2033                     (goto-char (point-min))
2034                     (save-restriction
2035                       (re-search-forward "^to:" nil nil to-count)
2036                       (forward-line -1)
2037                       (narrow-to-region (point) (point-max))
2038                       (gnus-article-hide-header "to"))
2039                     (setq to-count (1- to-count))))
2040                 (goto-char (point-min))
2041                 (while (re-search-forward "^cc:" nil t)
2042                   (setq cc-count (1+ cc-count)))
2043                 (when (> cc-count 1)
2044                   (while (> cc-count 0)
2045                     (goto-char (point-min))
2046                     (save-restriction
2047                       (re-search-forward "^cc:" nil nil cc-count)
2048                       (forward-line -1)
2049                       (narrow-to-region (point) (point-max))
2050                       (gnus-article-hide-header "cc"))
2051                     (setq cc-count (1- cc-count)))))))))))))
2052
2053 (defun gnus-article-hide-header (header)
2054   (save-excursion
2055     (goto-char (point-min))
2056     (when (re-search-forward (concat "^" header ":") nil t)
2057       (gnus-article-hide-text-type
2058        (point-at-bol)
2059        (progn
2060          (end-of-line)
2061          (if (re-search-forward "^[^ \t]" nil t)
2062              (match-beginning 0)
2063            (point-max)))
2064        'boring-headers))))
2065
2066 (defvar gnus-article-normalized-header-length 40
2067   "Length of normalized headers.")
2068
2069 (defun article-normalize-headers ()
2070   "Make all header lines 40 characters long."
2071   (interactive)
2072   (let ((inhibit-read-only t)
2073         column)
2074     (save-excursion
2075       (save-restriction
2076         (article-narrow-to-head)
2077         (while (not (eobp))
2078           (cond
2079            ((< (setq column (- (point-at-eol) (point)))
2080                gnus-article-normalized-header-length)
2081             (end-of-line)
2082             (insert (make-string
2083                      (- gnus-article-normalized-header-length column)
2084                      ? )))
2085            ((> column gnus-article-normalized-header-length)
2086             (gnus-put-text-property
2087              (progn
2088                (forward-char gnus-article-normalized-header-length)
2089                (point))
2090              (point-at-eol)
2091              'invisible t))
2092            (t
2093             ;; Do nothing.
2094             ))
2095           (forward-line 1))))))
2096
2097 (defun article-treat-dumbquotes ()
2098   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2099 Note that this function guesses whether a character is a sm*rtq**t* or
2100 not, so it should only be used interactively.
2101
2102 Sm*rtq**t*s are M****s***'s unilateral extension to the
2103 iso-8859-1 character map in an attempt to provide more quoting
2104 characters.  If you see something like \\222 or \\264 where
2105 you're expecting some kind of apostrophe or quotation mark, then
2106 try this wash."
2107   (interactive)
2108   (article-translate-strings gnus-article-dumbquotes-map))
2109
2110 (defvar org-entities)
2111
2112 (defun article-treat-non-ascii ()
2113   "Translate many Unicode characters into their ASCII equivalents."
2114   (interactive)
2115   (require 'org-entities)
2116   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2117     (dolist (elem org-entities)
2118       (when (and (listp elem)
2119                  (= (length (nth 6 elem)) 1))
2120         (if (featurep 'xemacs)
2121             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2122           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2123     (save-excursion
2124       (when (article-goto-body)
2125         (let ((inhibit-read-only t)
2126               replace props)
2127           (while (not (eobp))
2128             (if (not (setq replace (if (featurep 'xemacs)
2129                                        (get-char-table (following-char) table)
2130                                      (aref table (following-char)))))
2131                 (forward-char 1)
2132               (if (prog1
2133                       (setq props (text-properties-at (point)))
2134                     (delete-char 1))
2135                   (add-text-properties (point) (progn (insert replace) (point))
2136                                        props)
2137                 (insert replace)))))))))
2138
2139 (defun article-translate-strings (map)
2140   "Translate all string in the body of the article according to MAP.
2141 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2142   (save-excursion
2143     (when (article-goto-body)
2144       (let ((inhibit-read-only t))
2145         (dolist (elem map)
2146           (let ((from (car elem))
2147                 (to (cadr elem)))
2148             (save-excursion
2149               (if (stringp from)
2150                   (while (search-forward from nil t)
2151                     (replace-match to))
2152                 (while (not (eobp))
2153                   (if (eq (following-char) from)
2154                       (progn
2155                         (delete-char 1)
2156                         (insert to))
2157                     (forward-char 1)))))))))))
2158
2159 (defun article-treat-overstrike ()
2160   "Translate overstrikes into bold text."
2161   (interactive)
2162   (save-excursion
2163     (when (article-goto-body)
2164       (let ((inhibit-read-only t))
2165         (while (search-forward "\b" nil t)
2166           (let ((next (char-after))
2167                 (previous (char-after (- (point) 2))))
2168             ;; We do the boldification/underlining by hiding the
2169             ;; overstrikes and putting the proper text property
2170             ;; on the letters.
2171             (cond
2172              ((eq next previous)
2173               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2174               (put-text-property (point) (1+ (point)) 'face 'bold))
2175              ((eq next ?_)
2176               (gnus-article-hide-text-type
2177                (1- (point)) (1+ (point)) 'overstrike)
2178               (put-text-property
2179                (- (point) 2) (1- (point)) 'face 'underline))
2180              ((eq previous ?_)
2181               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2182               (put-text-property
2183                (point) (1+ (point)) 'face 'underline)))))))))
2184
2185 (defun article-treat-ansi-sequences ()
2186   "Translate ANSI SGR control sequences into overlays or extents."
2187   (interactive)
2188   (save-excursion
2189     (when (article-goto-body)
2190       (let ((inhibit-read-only t))
2191         (ansi-color-apply-on-region (point) (point-max))))))
2192
2193 (defun gnus-article-treat-unfold-headers ()
2194   "Unfold folded message headers.
2195 Only the headers that fit into the current window width will be
2196 unfolded."
2197   (interactive)
2198   (gnus-with-article-headers
2199     (let (length)
2200       (while (not (eobp))
2201         (save-restriction
2202           (mail-header-narrow-to-field)
2203           (let* ((header (buffer-string))
2204                  (unfoldable
2205                   (or (equal gnus-article-unfold-long-headers t)
2206                       (and (stringp gnus-article-unfold-long-headers)
2207                            (string-match gnus-article-unfold-long-headers
2208                                          header)))))
2209             (with-temp-buffer
2210               (insert header)
2211               (goto-char (point-min))
2212               (while (re-search-forward "\n[\t ]" nil t)
2213                 (replace-match " " t t)))
2214             (setq length (- (point-max) (point-min) 1))
2215             (when (or unfoldable
2216                       (< length (window-width)))
2217               (while (re-search-forward "\n[\t ]" nil t)
2218                 (replace-match " " t t))))
2219           (goto-char (point-max)))))))
2220
2221 (defun gnus-article-treat-fold-headers ()
2222   "Fold message headers."
2223   (interactive)
2224   (gnus-with-article-headers
2225     (while (not (eobp))
2226       (save-restriction
2227         (mail-header-narrow-to-field)
2228         (mail-header-fold-field)
2229         (goto-char (point-max))))))
2230
2231 (defun gnus-treat-smiley ()
2232   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2233   (interactive)
2234   (gnus-with-article-buffer
2235     (if (memq 'smiley gnus-article-wash-types)
2236         (gnus-delete-images 'smiley)
2237       (article-goto-body)
2238       (let ((images (smiley-region (point) (point-max))))
2239         (when images
2240           (gnus-add-wash-type 'smiley)
2241           (dolist (image images)
2242             (gnus-add-image 'smiley image)))))))
2243
2244 (defun gnus-article-remove-images ()
2245   "Remove all images from the article buffer."
2246   (interactive)
2247   (gnus-with-article-buffer
2248     (save-restriction
2249       (widen)
2250       (dolist (elem gnus-article-image-alist)
2251         (gnus-delete-images (car elem))))))
2252
2253 (autoload 'w3m-toggle-inline-images "w3m")
2254
2255 (defun gnus-article-show-images ()
2256   "Show any images that are in the HTML-rendered article buffer.
2257 This only works if the article in question is HTML."
2258   (interactive)
2259   (gnus-with-article-buffer
2260     (save-restriction
2261       (widen)
2262       (if (eq mm-text-html-renderer 'w3m)
2263           (let ((mm-inline-text-html-with-images nil))
2264             (w3m-toggle-inline-images))
2265         (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2266                                                         'image-displayer))
2267           (destructuring-bind (start end function) region
2268             (funcall function (get-text-property start 'image-url)
2269                      start end)))))))
2270
2271 (defun gnus-article-treat-fold-newsgroups ()
2272   "Unfold folded message headers.
2273 Only the headers that fit into the current window width will be
2274 unfolded."
2275   (interactive)
2276   (gnus-with-article-headers
2277     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2278       (save-restriction
2279         (mail-header-narrow-to-field)
2280         (while (re-search-forward ", *" nil t)
2281           (replace-match ", " t t))
2282         (mail-header-fold-field)
2283         (goto-char (point-max))))))
2284
2285 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2286   "Value of `truncate-lines' in Gnus Article buffer.
2287 Valid values are nil, t, `head', `first', `last', an integer or a
2288 predicate.  See Info node `(gnus)Customizing Articles'."
2289   :version "23.1" ;; No Gnus
2290   :group 'gnus-article
2291   ;; :link '(custom-manual "(gnus)Customizing Articles")
2292   :type 'boolean)
2293
2294 (defun gnus-article-toggle-truncate-lines (&optional arg)
2295   "Toggle whether to fold or truncate long lines in article the buffer.
2296 If ARG is non-nil and not a number, toggle
2297 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2298 long lines if and only if arg is positive."
2299   (interactive "P")
2300   (cond
2301    ((and (numberp arg) (> arg 0))
2302     (setq gnus-article-truncate-lines t))
2303    ((numberp arg)
2304     (setq gnus-article-truncate-lines nil))
2305    (arg
2306     (setq gnus-article-truncate-lines
2307           (not gnus-article-truncate-lines))))
2308   (gnus-with-article-buffer
2309     (cond
2310      ((and (numberp arg) (> arg 0))
2311       (setq truncate-lines nil))
2312      ((numberp arg)
2313       (setq truncate-lines t)))
2314     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2315     ;; `toggle-truncate-lines' needs an argument.
2316     (toggle-truncate-lines)))
2317
2318 (defun gnus-article-treat-body-boundary ()
2319   "Place a boundary line at the end of the headers."
2320   (interactive)
2321   (when (and gnus-body-boundary-delimiter
2322              (> (length gnus-body-boundary-delimiter) 0))
2323     (gnus-with-article-headers
2324       (goto-char (point-max))
2325       (let ((start (point)))
2326         (insert "X-Boundary: ")
2327         (gnus-add-text-properties start (point) '(invisible t intangible t))
2328        (insert (let (str (max (window-width)))
2329                  (if (featurep 'xemacs)
2330                      (setq max (1- max)))
2331                  (while (>= max (length str))
2332                     (setq str (concat str gnus-body-boundary-delimiter)))
2333                  (substring str 0 max))
2334                 "\n")
2335         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2336
2337 (defun article-fill-long-lines ()
2338   "Fill lines that are wider than the window width."
2339   (interactive)
2340   (save-excursion
2341     (let ((inhibit-read-only t)
2342           (width (window-width (get-buffer-window (current-buffer)))))
2343       (save-restriction
2344         (article-goto-body)
2345         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2346           (while (not (eobp))
2347             (end-of-line)
2348             (when (>= (current-column) (min fill-column width))
2349               (narrow-to-region (min (1+ (point)) (point-max))
2350                                 (point-at-bol))
2351               (let ((goback (point-marker)))
2352                 (fill-paragraph nil)
2353                 (goto-char (marker-position goback)))
2354               (widen))
2355             (forward-line 1)))))))
2356
2357 (defun article-capitalize-sentences ()
2358   "Capitalize the first word in each sentence."
2359   (interactive)
2360   (save-excursion
2361     (let ((inhibit-read-only t)
2362           (paragraph-start "^[\n\^L]"))
2363       (article-goto-body)
2364       (while (not (eobp))
2365         (capitalize-word 1)
2366         (forward-sentence)))))
2367
2368 (defun article-remove-cr ()
2369   "Remove trailing CRs and then translate remaining CRs into LFs."
2370   (interactive)
2371   (save-excursion
2372     (let ((inhibit-read-only t))
2373       (goto-char (point-min))
2374       (while (re-search-forward "\r+$" nil t)
2375         (replace-match "" t t))
2376       (goto-char (point-min))
2377       (while (search-forward "\r" nil t)
2378         (replace-match "\n" t t)))))
2379
2380 (defun article-remove-trailing-blank-lines ()
2381   "Remove all trailing blank lines from the article."
2382   (interactive)
2383   (save-excursion
2384     (let ((inhibit-read-only t))
2385       (goto-char (point-max))
2386       (delete-region
2387        (point)
2388        (progn
2389          (while (and (not (bobp))
2390                      (looking-at "^[ \t]*$")
2391                      (not (gnus-annotation-in-region-p
2392                            (point) (point-at-eol))))
2393            (forward-line -1))
2394          (forward-line 1)
2395          (point))))))
2396
2397 (defvar gnus-face-properties-alist)
2398
2399 (defun article-display-face (&optional force)
2400   "Display any Face headers in the header."
2401   (interactive (list 'force))
2402   (let ((wash-face-p buffer-read-only))
2403     (gnus-with-article-headers
2404       ;; When displaying parts, this function can be called several times on
2405       ;; the same article, without any intended toggle semantic (as typing `W
2406       ;; D d' would have). So face deletion must occur only when we come from
2407       ;; an interactive command, that is when the *Article* buffer is
2408       ;; read-only.
2409       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2410           (gnus-delete-images 'face)
2411         (let ((from (message-fetch-field "from"))
2412               face faces)
2413           (save-current-buffer
2414             (when (and wash-face-p
2415                        (gnus-buffer-live-p gnus-original-article-buffer)
2416                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2417               (set-buffer gnus-original-article-buffer))
2418             (save-restriction
2419               (mail-narrow-to-head)
2420               (when (or force
2421                         ;; Check whether this face is censored.
2422                         (not (and gnus-article-x-face-too-ugly
2423                                   (or from
2424                                       (setq from (message-fetch-field "from")))
2425                                   (string-match gnus-article-x-face-too-ugly
2426                                                 from))))
2427                 (while (gnus-article-goto-header "Face")
2428                   (push (mail-header-field-value) faces)))))
2429           (when faces
2430             (goto-char (point-min))
2431             (let (png image)
2432               (unless (setq from (gnus-article-goto-header "from"))
2433                 (insert "From:")
2434                 (setq from (point))
2435                 (insert " [no `from' set]\n"))
2436               (while faces
2437                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2438                   (setq image
2439                         (apply 'gnus-create-image png 'png t
2440                                (cdr (assq 'png gnus-face-properties-alist))))
2441                   (goto-char from)
2442                   (when image
2443                     (gnus-add-wash-type 'face)
2444                     (gnus-add-image 'face image)
2445                     (gnus-put-image image nil 'face)))))))))))
2446
2447 (defun article-display-x-face (&optional force)
2448   "Look for an X-Face header and display it if present."
2449   (interactive (list 'force))
2450   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2451     (gnus-with-article-headers
2452       ;; Delete the old process, if any.
2453       (when (process-status "article-x-face")
2454         (delete-process "article-x-face"))
2455       ;; See the comment in `article-display-face'.
2456       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2457           ;; We have already displayed X-Faces, so we remove them
2458           ;; instead.
2459           (gnus-delete-images 'xface)
2460         ;; Display X-Faces.
2461         (let ((from (message-fetch-field "from"))
2462               x-faces face)
2463           (save-current-buffer
2464             (when (and wash-face-p
2465                        (gnus-buffer-live-p gnus-original-article-buffer)
2466                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2467               ;; If type `W f', use gnus-original-article-buffer,
2468               ;; otherwise use the current buffer because displaying
2469               ;; RFC822 parts calls this function too.
2470               (set-buffer gnus-original-article-buffer))
2471             (save-restriction
2472               (mail-narrow-to-head)
2473               (and gnus-article-x-face-command
2474                    (or force
2475                        ;; Check whether this face is censored.
2476                        (not (and gnus-article-x-face-too-ugly
2477                                  (or from
2478                                      (setq from (message-fetch-field "from")))
2479                                  (string-match gnus-article-x-face-too-ugly
2480                                                from))))
2481                    (while (gnus-article-goto-header "X-Face")
2482                      (push (mail-header-field-value) x-faces)))))
2483           (when x-faces
2484             ;; We display the face.
2485             (cond ((functionp gnus-article-x-face-command)
2486                    ;; The command is a lisp function, so we call it.
2487                    (mapc gnus-article-x-face-command x-faces))
2488                   ((stringp gnus-article-x-face-command)
2489                    ;; The command is a string, so we interpret the command
2490                    ;; as a, well, command, and fork it off.
2491                    (let ((process-connection-type nil))
2492                      (gnus-set-process-query-on-exit-flag
2493                       (start-process
2494                        "article-x-face" nil shell-file-name
2495                        shell-command-switch gnus-article-x-face-command)
2496                       nil)
2497                      ;; Sending multiple EOFs to xv doesn't work,
2498                      ;; so we only do a single external face.
2499                      (with-temp-buffer
2500                        (insert (car x-faces))
2501                        (process-send-region "article-x-face"
2502                                             (point-min) (point-max)))
2503                      (process-send-eof "article-x-face")))
2504                   (t
2505                    (error "`%s' set to `%s' is not a function"
2506                           gnus-article-x-face-command
2507                           'gnus-article-x-face-command)))))))))
2508
2509 (defun article-decode-mime-words ()
2510   "Decode all MIME-encoded words in the article."
2511   (interactive)
2512   (gnus-with-article-buffer
2513     (let ((inhibit-point-motion-hooks t)
2514           (mail-parse-charset gnus-newsgroup-charset)
2515           (mail-parse-ignored-charsets
2516            (with-current-buffer gnus-summary-buffer
2517              gnus-newsgroup-ignored-charsets)))
2518       (mail-decode-encoded-word-region (point-min) (point-max)))))
2519
2520 (defun article-decode-charset (&optional prompt)
2521   "Decode charset-encoded text in the article.
2522 If PROMPT (the prefix), prompt for a coding system to use."
2523   (interactive "P")
2524   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2525         (inhibit-read-only t)
2526         (mail-parse-charset gnus-newsgroup-charset)
2527         (mail-parse-ignored-charsets
2528          (save-excursion (condition-case nil
2529                              (set-buffer gnus-summary-buffer)
2530                            (error))
2531                          gnus-newsgroup-ignored-charsets))
2532         ct cte ctl charset format)
2533     (save-excursion
2534       (save-restriction
2535         (article-narrow-to-head)
2536         (setq ct (message-fetch-field "Content-Type" t)
2537               cte (message-fetch-field "Content-Transfer-Encoding" t)
2538               ctl (and ct (mail-header-parse-content-type ct))
2539               charset (cond
2540                        (prompt
2541                         (mm-read-coding-system "Charset to decode: "))
2542                        (ctl
2543                         (mail-content-type-get ctl 'charset)))
2544               format (and ctl (mail-content-type-get ctl 'format)))
2545         (when cte
2546           (setq cte (mail-header-strip cte)))
2547         (if (and ctl (not (string-match "/" (car ctl))))
2548             (setq ctl nil))
2549         (goto-char (point-max)))
2550       (forward-line 1)
2551       (save-restriction
2552         (narrow-to-region (point) (point-max))
2553         (when (and (eq mail-parse-charset 'gnus-decoded)
2554                    (eq (mm-body-7-or-8) '8bit))
2555           ;; The text code could have been decoded.
2556           (setq charset mail-parse-charset))
2557         (when (and (or (not ctl)
2558                        (equal (car ctl) "text/plain"))
2559                    (not format)) ;; article with format will decode later.
2560           (mm-decode-body
2561            charset (and cte (intern (downcase
2562                                      (gnus-strip-whitespace cte))))
2563            (car ctl)))))))
2564
2565 (defun article-decode-encoded-words ()
2566   "Remove encoded-word encoding from headers."
2567   (let ((inhibit-point-motion-hooks t)
2568         (mail-parse-charset gnus-newsgroup-charset)
2569         (mail-parse-ignored-charsets
2570          (save-excursion (condition-case nil
2571                              (set-buffer gnus-summary-buffer)
2572                            (error))
2573                          gnus-newsgroup-ignored-charsets))
2574         (inhibit-read-only t)
2575         end start)
2576     (goto-char (point-min))
2577     (when (search-forward "\n\n" nil 'move)
2578       (forward-line -1))
2579     (setq end (point))
2580     (while (not (bobp))
2581       (while (progn
2582                (forward-line -1)
2583                (and (not (bobp))
2584                     (memq (char-after) '(?\t ? )))))
2585       (setq start (point))
2586       (if (looking-at "\
2587 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2588 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2589           (funcall gnus-decode-address-function start end)
2590         (funcall gnus-decode-header-function start end))
2591       (goto-char (setq end start)))))
2592
2593 (defun article-decode-group-name ()
2594   "Decode group names in Newsgroups, Followup-To and Xref headers."
2595   (let ((inhibit-point-motion-hooks t)
2596         (inhibit-read-only t)
2597         (method (gnus-find-method-for-group gnus-newsgroup-name))
2598         regexp)
2599     (when (and (or gnus-group-name-charset-method-alist
2600                    gnus-group-name-charset-group-alist)
2601                (gnus-buffer-live-p gnus-original-article-buffer))
2602       (save-restriction
2603         (article-narrow-to-head)
2604         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2605           (with-current-buffer gnus-original-article-buffer
2606             (goto-char (point-min)))
2607           (setq regexp (concat "^" header
2608                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2609           (while (re-search-forward regexp nil t)
2610             (replace-match (save-match-data
2611                              (gnus-decode-newsgroups
2612                               ;; XXX how to use data in article buffer?
2613                               (with-current-buffer gnus-original-article-buffer
2614                                 (re-search-forward regexp nil t)
2615                                 (match-string 1))
2616                               gnus-newsgroup-name method))
2617                            t t nil 1))
2618           (goto-char (point-min)))))))
2619
2620 (autoload 'idna-to-unicode "idna")
2621
2622 (defun article-decode-idna-rhs ()
2623   "Decode IDNA strings in RHS in various headers in current buffer.
2624 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2625 Mail-Reply-To: and Mail-Followup-To:."
2626   (when gnus-use-idna
2627     (save-restriction
2628       (let ((inhibit-point-motion-hooks t)
2629             (inhibit-read-only t))
2630         (article-narrow-to-head)
2631         (goto-char (point-min))
2632         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2633           (let (ace unicode)
2634             (when (save-match-data
2635                     (and (setq ace (match-string 1))
2636                          (save-excursion
2637                            (and (re-search-backward "^[^ \t]" nil t)
2638                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2639                          (setq unicode (idna-to-unicode ace))))
2640               (unless (string= ace unicode)
2641                 (replace-match unicode nil nil nil 1)))))))))
2642
2643 (defun article-de-quoted-unreadable (&optional force read-charset)
2644   "Translate a quoted-printable-encoded article.
2645 If FORCE, decode the article whether it is marked as quoted-printable
2646 or not.
2647 If READ-CHARSET, ask for a coding system."
2648   (interactive (list 'force current-prefix-arg))
2649   (save-excursion
2650     (let ((inhibit-read-only t) type charset)
2651       (if (gnus-buffer-live-p gnus-original-article-buffer)
2652           (with-current-buffer gnus-original-article-buffer
2653             (setq type
2654                   (gnus-fetch-field "content-transfer-encoding"))
2655             (let* ((ct (gnus-fetch-field "content-type"))
2656                    (ctl (and ct (mail-header-parse-content-type ct))))
2657               (setq charset (and ctl
2658                                  (mail-content-type-get ctl 'charset)))
2659               (if (stringp charset)
2660                   (setq charset (intern (downcase charset)))))))
2661       (if read-charset
2662           (setq charset (mm-read-coding-system "Charset: " charset)))
2663       (unless charset
2664         (setq charset gnus-newsgroup-charset))
2665       (when (or force
2666                 (and type (let ((case-fold-search t))
2667                             (string-match "quoted-printable" type))))
2668         (article-goto-body)
2669         (quoted-printable-decode-region
2670          (point) (point-max) (mm-charset-to-coding-system charset))))))
2671
2672 (defun article-de-base64-unreadable (&optional force read-charset)
2673   "Translate a base64 article.
2674 If FORCE, decode the article whether it is marked as base64 not.
2675 If READ-CHARSET, ask for a coding system."
2676   (interactive (list 'force current-prefix-arg))
2677   (save-excursion
2678     (let ((inhibit-read-only t) type charset)
2679       (if (gnus-buffer-live-p gnus-original-article-buffer)
2680           (with-current-buffer gnus-original-article-buffer
2681             (setq type
2682                   (gnus-fetch-field "content-transfer-encoding"))
2683             (let* ((ct (gnus-fetch-field "content-type"))
2684                    (ctl (and ct (mail-header-parse-content-type ct))))
2685               (setq charset (and ctl
2686                                  (mail-content-type-get ctl 'charset)))
2687               (if (stringp charset)
2688                   (setq charset (intern (downcase charset)))))))
2689       (if read-charset
2690           (setq charset (mm-read-coding-system "Charset: " charset)))
2691       (unless charset
2692         (setq charset gnus-newsgroup-charset))
2693       (when (or force
2694                 (and type (let ((case-fold-search t))
2695                             (string-match "base64" type))))
2696         (article-goto-body)
2697         (save-restriction
2698           (narrow-to-region (point) (point-max))
2699           (base64-decode-region (point-min) (point-max))
2700           (mm-decode-coding-region
2701            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2702
2703 (eval-when-compile
2704   (require 'rfc1843))
2705
2706 (defun article-decode-HZ ()
2707   "Translate a HZ-encoded article."
2708   (interactive)
2709   (require 'rfc1843)
2710   (save-excursion
2711     (let ((inhibit-read-only t))
2712       (rfc1843-decode-region (point-min) (point-max)))))
2713
2714 (defun article-unsplit-urls ()
2715   "Remove the newlines that some other mailers insert into URLs."
2716   (interactive)
2717   (save-excursion
2718     (let ((inhibit-read-only t))
2719       (goto-char (point-min))
2720       (while (re-search-forward
2721               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2722         (replace-match "\\1\\3" t)))
2723     (when (gmm-called-interactively-p 'any)
2724       (gnus-treat-article nil))))
2725
2726 (defun article-wash-html ()
2727   "Format an HTML article."
2728   (interactive)
2729   (let ((handles nil)
2730         (buffer-read-only nil))
2731     (when (gnus-buffer-live-p gnus-original-article-buffer)
2732       (with-current-buffer gnus-original-article-buffer
2733         (setq handles (mm-dissect-buffer t t))))
2734     (article-goto-body)
2735     (delete-region (point) (point-max))
2736     (mm-enable-multibyte)
2737     (mm-inline-text-html handles)))
2738
2739 (defvar gnus-article-browse-html-temp-list nil
2740   "List of temporary files created by `gnus-article-browse-html-parts'.
2741 Internal variable.")
2742
2743 (defcustom gnus-article-browse-delete-temp 'ask
2744   "What to do with temporary files from `gnus-article-browse-html-parts'.
2745 If nil, don't delete temporary files.  If it is t, delete them on
2746 exit from the summary buffer.  If it is the symbol `file', query
2747 on each file, if it is `ask' ask once when exiting from the
2748 summary buffer."
2749   :group 'gnus-article
2750   :version "23.1" ;; No Gnus
2751   :type '(choice (const :tag "Don't delete" nil)
2752                  (const :tag "Don't ask" t)
2753                  (const :tag "Ask" ask)
2754                  (const :tag "Ask for each file" file)))
2755
2756 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2757
2758 (defun gnus-article-browse-delete-temp-files (&optional how)
2759   "Delete temp-files created by `gnus-article-browse-html-parts'."
2760   (when (and gnus-article-browse-html-temp-list
2761              (progn
2762                (or how (setq how gnus-article-browse-delete-temp))
2763                (if (eq how 'ask)
2764                    (let ((files (length gnus-article-browse-html-temp-list)))
2765                      (or (gnus-y-or-n-p
2766                           (if (= files 1)
2767                               "Delete the temporary HTML file? "
2768                             (format "Delete all %s temporary HTML files? "
2769                                     files)))
2770                          (setq gnus-article-browse-html-temp-list nil)))
2771                  how)))
2772     (dolist (file gnus-article-browse-html-temp-list)
2773       (cond ((file-directory-p file)
2774              (when (or (not (eq how 'file))
2775                        (gnus-y-or-n-p
2776                         (format
2777                          "Delete temporary HTML file(s) in directory `%s'? "
2778                          (file-name-as-directory file))))
2779                (gnus-delete-directory file)))
2780             ((file-exists-p file)
2781              (when (or (not (eq how 'file))
2782                        (gnus-y-or-n-p
2783                         (format "Delete temporary HTML file `%s'? " file)))
2784                (delete-file file)))))
2785     ;; Also remove file from the list when not deleted or if file doesn't
2786     ;; exist anymore.
2787     (setq gnus-article-browse-html-temp-list nil))
2788   gnus-article-browse-html-temp-list)
2789
2790 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2791   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2792 Return file name."
2793   (save-match-data
2794     (let (file type)
2795       (catch 'found
2796         (dolist (handle handles)
2797           (cond
2798            ((not (listp handle)))
2799            ((equal (mm-handle-media-supertype handle) "multipart")
2800             (when (setq file (gnus-article-browse-html-save-cid-content
2801                               cid handle directory))
2802               (throw 'found file)))
2803            ((equal (concat "<" cid ">") (mm-handle-id handle))
2804             (setq file
2805                   (expand-file-name
2806                    (or (mm-handle-filename handle)
2807                        (concat
2808                         (make-temp-name "cid")
2809                         (car (rassoc (car (mm-handle-type handle)) mailcap-mime-extensions))))
2810                    directory))
2811             (mm-save-part-to-file handle file)
2812             (throw 'found file))))))))
2813
2814 (defun gnus-article-browse-html-parts (list &optional header)
2815   "View all \"text/html\" parts from LIST.
2816 Recurse into multiparts.  The optional HEADER that should be a decoded
2817 message header will be added to the bodies of the \"text/html\" parts."
2818   ;; Internal function used by `gnus-article-browse-html-article'.
2819   (let (type file charset content cid-dir tmp-file showed)
2820     ;; Find and show the html-parts.
2821     (dolist (handle list)
2822       ;; If HTML, show it:
2823       (cond ((not (listp handle)))
2824             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2825                  (and (equal (car type) "message/external-body")
2826                       (or header
2827                           (setq file (mm-handle-filename handle)))
2828                       (or (mm-handle-cache handle)
2829                           (condition-case code
2830                               (progn (mm-extern-cache-contents handle) t)
2831                             (error
2832                              (gnus-message 3 "%s" (error-message-string code))
2833                              (when (>= gnus-verbose 3) (sit-for 2))
2834                              nil)))
2835                       (progn
2836                         (setq handle (mm-handle-cache handle)
2837                               type (mm-handle-type handle))
2838                         (equal (car type) "text/html"))))
2839              (setq charset (mail-content-type-get type 'charset)
2840                    content (mm-get-part handle))
2841              (with-temp-buffer
2842                (if (eq charset 'gnus-decoded)
2843                    (mm-enable-multibyte)
2844                  (mm-disable-multibyte))
2845                (insert content)
2846                ;; resolve cid contents
2847                (let ((case-fold-search t)
2848                      cid-file)
2849                  (goto-char (point-min))
2850                  (while (re-search-forward "\
2851 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2852                                            nil t)
2853                    (unless cid-dir
2854                      (setq cid-dir (mm-make-temp-file "cid" t))
2855                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2856                    (setq file nil
2857                          content nil)
2858                    (when (setq cid-file
2859                                (gnus-article-browse-html-save-cid-content
2860                                 (match-string 2)
2861                                 (with-current-buffer gnus-article-buffer
2862                                   gnus-article-mime-handles)
2863                                 cid-dir))
2864                      (when (eq system-type 'cygwin)
2865                        (setq cid-file
2866                              (concat "/" (substring
2867                                           (with-output-to-string
2868                                             (call-process "cygpath" nil
2869                                                           standard-output
2870                                                           nil "-m" cid-file))
2871                                           0 -1))))
2872                      (replace-match (concat "file://" cid-file)
2873                                     nil nil nil 1))))
2874                (unless content (setq content (buffer-string))))
2875              (when (or charset header (not file))
2876                (setq tmp-file (mm-make-temp-file
2877                                ;; Do we need to care for 8.3 filenames?
2878                                "mm-" nil ".html")))
2879              ;; Add a meta html tag to specify charset and a header.
2880              (cond
2881               (header
2882                (let (title eheader body hcharset coding)
2883                  (with-temp-buffer
2884                    (mm-enable-multibyte)
2885                    (setq case-fold-search t)
2886                    (insert header "\n")
2887                    (setq title (message-fetch-field "subject"))
2888                    (goto-char (point-min))
2889                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2890                      (replace-match (cond ((match-beginning 1) "&lt;")
2891                                           ((match-beginning 2) "&gt;")
2892                                           (t "&amp;"))))
2893                    (goto-char (point-min))
2894                    (insert "<pre>\n")
2895                    (goto-char (point-max))
2896                    (insert "</pre>\n<hr>\n")
2897                    ;; We have to examine charset one by one since
2898                    ;; charset specified in parts might be different.
2899                    (if (eq charset 'gnus-decoded)
2900                        (setq charset 'utf-8
2901                              eheader (mm-encode-coding-string (buffer-string)
2902                                                               charset)
2903                              title (when title
2904                                      (mm-encode-coding-string title charset))
2905                              body (mm-encode-coding-string content charset))
2906                      (setq hcharset (mm-find-mime-charset-region (point-min)
2907                                                                  (point-max)))
2908                      (cond ((= (length hcharset) 1)
2909                             (setq hcharset (car hcharset)
2910                                   coding (mm-charset-to-coding-system
2911                                           hcharset)))
2912                            ((> (length hcharset) 1)
2913                             (setq hcharset 'utf-8
2914                                   coding hcharset)))
2915                      (if coding
2916                          (if charset
2917                              (progn
2918                                (setq body
2919                                      (mm-charset-to-coding-system charset))
2920                                (if (eq coding body)
2921                                    (setq eheader (mm-encode-coding-string
2922                                                   (buffer-string) coding)
2923                                          title (when title
2924                                                  (mm-encode-coding-string
2925                                                   title coding))
2926                                          body content)
2927                                  (setq charset 'utf-8
2928                                        eheader (mm-encode-coding-string
2929                                                 (buffer-string) charset)
2930                                        title (when title
2931                                                (mm-encode-coding-string
2932                                                 title charset))
2933                                        body (mm-encode-coding-string
2934                                              (mm-decode-coding-string
2935                                               content body)
2936                                              charset))))
2937                            (setq charset hcharset
2938                                  eheader (mm-encode-coding-string
2939                                           (buffer-string) coding)
2940                                  title (when title
2941                                          (mm-encode-coding-string
2942                                           title coding))
2943                                  body content))
2944                        (setq eheader (mm-string-as-unibyte (buffer-string))
2945                              body content)))
2946                    (erase-buffer)
2947                    (mm-disable-multibyte)
2948                    (insert body)
2949                    (when charset
2950                      (mm-add-meta-html-tag handle charset t))
2951                    (when title
2952                      (goto-char (point-min))
2953                      (unless (search-forward "<title>" nil t)
2954                        (re-search-forward "<head>\\s-*" nil t)
2955                        (insert "<title>" title "</title>\n")))
2956                    (goto-char (point-min))
2957                    (or (re-search-forward
2958                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2959                        (re-search-forward
2960                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2961                    (insert eheader)
2962                    (mm-write-region (point-min) (point-max)
2963                                     tmp-file nil nil nil 'binary t))))
2964               (charset
2965                (mm-with-unibyte-buffer
2966                  (insert (if (eq charset 'gnus-decoded)
2967                              (mm-encode-coding-string content
2968                                                       (setq charset 'utf-8))
2969                            content))
2970                  (if (or (mm-add-meta-html-tag handle charset)
2971                          (not file))
2972                      (mm-write-region (point-min) (point-max)
2973                                       tmp-file nil nil nil 'binary t)
2974                    (setq tmp-file nil))))
2975               (tmp-file
2976                (mm-save-part-to-file handle tmp-file)))
2977              (when tmp-file
2978                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2979              (add-hook 'gnus-summary-prepare-exit-hook
2980                        'gnus-article-browse-delete-temp-files)
2981              (add-hook 'gnus-exit-gnus-hook
2982                        (lambda  ()
2983                          (gnus-article-browse-delete-temp-files t)))
2984              ;; FIXME: Warn if there's an <img> tag?
2985              (browse-url-of-file (or tmp-file (expand-file-name file)))
2986              (setq showed t))
2987             ;; If multipart, recurse
2988             ((equal (mm-handle-media-supertype handle) "multipart")
2989              (when (gnus-article-browse-html-parts handle header)
2990                (setq showed t)))
2991             ((equal (mm-handle-media-type handle) "message/rfc822")
2992              (mm-with-multibyte-buffer
2993                (mm-insert-part handle)
2994                (setq handle (mm-dissect-buffer t t))
2995                (when (and (bufferp (car handle))
2996                           (stringp (car (mm-handle-type handle))))
2997                  (setq handle (list handle)))
2998                (when header
2999                  (article-decode-encoded-words)
3000                  (let ((gnus-visible-headers
3001                         (or (get 'gnus-visible-headers 'standard-value)
3002                             gnus-visible-headers)))
3003                    (article-hide-headers))
3004                  (goto-char (point-min))
3005                  (search-forward "\n\n" nil 'move)
3006                  (skip-chars-backward "\t\n ")
3007                  (setq header (buffer-substring (point-min) (point)))))
3008              (when (prog1
3009                        (gnus-article-browse-html-parts handle header)
3010                      (mm-destroy-parts handle))
3011                (setq showed t)))))
3012     showed))
3013
3014 (defun gnus-article-browse-html-article (&optional arg)
3015   "View \"text/html\" parts of the current article with a WWW browser.
3016 Inline images embedded in a message using the cid scheme, as they are
3017 generally considered to be safe, will be processed properly.
3018 The message header is added to the beginning of every html part unless
3019 the prefix argument ARG is given.
3020
3021 Warning: Spammers use links to images (using the http scheme) in HTML
3022 articles to verify whether you have read the message.  As
3023 `gnus-article-browse-html-article' passes the HTML content to the
3024 browser without eliminating these \"web bugs\" you should only
3025 use it for mails from trusted senders.
3026
3027 If you always want to display HTML parts in the browser, set
3028 `mm-text-html-renderer' to nil.
3029
3030 This command creates temporary files to pass HTML contents including
3031 images if any to the browser, and deletes them when exiting the group
3032 \(if you want)."
3033   ;; Cf. `mm-w3m-safe-url-regexp'
3034   (interactive "P")
3035   (if arg
3036       (gnus-summary-show-article)
3037     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3038                                     gnus-visible-headers))
3039           ;; As we insert a <hr>, there's no need for the body boundary.
3040           (gnus-treat-body-boundary nil))
3041       (gnus-summary-show-article)))
3042   (with-current-buffer gnus-article-buffer
3043     (let ((header (unless arg
3044                     (save-restriction
3045                       (widen)
3046                       (buffer-substring-no-properties
3047                        (goto-char (point-min))
3048                        (if (search-forward "\n\n" nil t)
3049                            (match-beginning 0)
3050                          (goto-char (point-max))
3051                          (skip-chars-backward "\t\n ")
3052                          (point))))))
3053           parts)
3054       (set-buffer gnus-original-article-buffer)
3055       (setq parts (mm-dissect-buffer t t))
3056       ;; If singlepart, enforce a list.
3057       (when (and (bufferp (car parts))
3058                  (stringp (car (mm-handle-type parts))))
3059         (setq parts (list parts)))
3060       ;; Process the list
3061       (unless (gnus-article-browse-html-parts parts header)
3062         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3063       (mm-destroy-parts parts)
3064       (unless arg
3065         (gnus-summary-show-article)))))
3066
3067 (defun article-hide-list-identifiers ()
3068   "Remove list identifies from the Subject header.
3069 The `gnus-list-identifiers' variable specifies what to do."
3070   (interactive)
3071   (let ((inhibit-point-motion-hooks t)
3072         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3073         (inhibit-read-only t))
3074     (when regexp
3075       (save-excursion
3076         (save-restriction
3077           (article-narrow-to-head)
3078           (goto-char (point-min))
3079           (while (re-search-forward
3080                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3081                   nil t)
3082             (delete-region (match-beginning 2) (match-end 0))
3083             (beginning-of-line))
3084           (when (re-search-forward
3085                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3086             (delete-region (match-beginning 1) (match-end 1))))))))
3087
3088 (defun article-hide-pem (&optional arg)
3089   "Toggle hiding of any PEM headers and signatures in the current article.
3090 If given a negative prefix, always show; if given a positive prefix,
3091 always hide."
3092   (interactive (gnus-article-hidden-arg))
3093   (unless (gnus-article-check-hidden-text 'pem arg)
3094     (save-excursion
3095       (let ((inhibit-read-only t) end)
3096         (goto-char (point-min))
3097         ;; Hide the horrendously ugly "header".
3098         (when (and (search-forward
3099                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3100                     nil t)
3101                    (setq end (1+ (match-beginning 0))))
3102           (gnus-add-wash-type 'pem)
3103           (gnus-article-hide-text-type
3104            end
3105            (if (search-forward "\n\n" nil t)
3106                (match-end 0)
3107              (point-max))
3108            'pem)
3109           ;; Hide the trailer as well
3110           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3111                                 nil t)
3112             (gnus-article-hide-text-type
3113              (match-beginning 0) (match-end 0) 'pem)))))))
3114
3115 (defun article-strip-banner ()
3116   "Strip the banners specified by the `banner' group parameter and by
3117 `gnus-article-address-banner-alist'."
3118   (interactive)
3119   (save-excursion
3120     (save-restriction
3121       (let ((inhibit-point-motion-hooks t))
3122         (when (gnus-parameter-banner gnus-newsgroup-name)
3123           (article-really-strip-banner
3124            (gnus-parameter-banner gnus-newsgroup-name)))
3125         (when gnus-article-address-banner-alist
3126           ;; Note that the From header is decoded here, so it is
3127           ;; required that the *-extract-address-components function
3128           ;; supports non-ASCII text.
3129           (let ((from (save-restriction
3130                         (widen)
3131                         (article-narrow-to-head)
3132                         (mail-fetch-field "from"))))
3133             (when (and from
3134                        (setq from
3135                              (cadr (funcall gnus-extract-address-components
3136                                             from))))
3137               (catch 'found
3138                 (dolist (pair gnus-article-address-banner-alist)
3139                   (when (string-match (car pair) from)
3140                     (throw 'found
3141                            (article-really-strip-banner (cdr pair)))))))))))))
3142
3143 (defun article-really-strip-banner (banner)
3144   "Strip the banner specified by the argument."
3145   (save-excursion
3146     (save-restriction
3147       (let ((inhibit-point-motion-hooks t)
3148             (gnus-signature-limit nil)
3149             (inhibit-read-only t))
3150         (article-goto-body)
3151         (cond
3152          ((eq banner 'signature)
3153           (when (gnus-article-narrow-to-signature)
3154             (widen)
3155             (forward-line -1)
3156             (delete-region (point) (point-max))))
3157          ((symbolp banner)
3158           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3159               (while (re-search-forward banner nil t)
3160                 (delete-region (match-beginning 0) (match-end 0)))))
3161          ((stringp banner)
3162           (while (re-search-forward banner nil t)
3163             (delete-region (match-beginning 0) (match-end 0)))))))))
3164
3165 (defun article-babel ()
3166   "Translate article using an online translation service."
3167   (interactive)
3168   (require 'babel)
3169   (gnus-with-article-buffer
3170     (when (article-goto-body)
3171       (let* ((start (point))
3172              (end (point-max))
3173              (orig (buffer-substring start end))
3174              (trans (babel-as-string orig)))
3175         (save-restriction
3176           (narrow-to-region start end)
3177           (delete-region start end)
3178           (insert trans))))))
3179
3180 (defun article-hide-signature (&optional arg)
3181   "Hide the signature in the current article.
3182 If given a negative prefix, always show; if given a positive prefix,
3183 always hide."
3184   (interactive (gnus-article-hidden-arg))
3185   (unless (gnus-article-check-hidden-text 'signature arg)
3186     (save-excursion
3187       (save-restriction
3188         (let ((inhibit-read-only t))
3189           (when (gnus-article-narrow-to-signature)
3190             (gnus-article-hide-text-type
3191              (point-min) (point-max) 'signature))))))
3192   (gnus-set-mode-line 'article))
3193
3194 (defun article-strip-headers-in-body ()
3195   "Strip offensive headers from bodies."
3196   (interactive)
3197   (save-excursion
3198     (article-goto-body)
3199     (let ((case-fold-search t))
3200       (when (looking-at "x-no-archive:")
3201         (gnus-delete-line)))))
3202
3203 (defun article-strip-leading-blank-lines ()
3204   "Remove all blank lines from the beginning of the article."
3205   (interactive)
3206   (save-excursion
3207     (let ((inhibit-point-motion-hooks t)
3208           (inhibit-read-only t))
3209       (when (article-goto-body)
3210         (while (and (not (eobp))
3211                     (looking-at "[ \t]*$"))
3212           (gnus-delete-line))))))
3213
3214 (defun article-narrow-to-head ()
3215   "Narrow the buffer to the head of the message.
3216 Point is left at the beginning of the narrowed-to region."
3217   (narrow-to-region
3218    (goto-char (point-min))
3219    (cond
3220     ;; Absolutely no headers displayed.
3221     ((looking-at "\n")
3222      (point))
3223     ;; Normal headers.
3224     ((search-forward "\n\n" nil 1)
3225      (1- (point)))
3226     ;; Nothing but headers.
3227     (t
3228      (point-max))))
3229   (goto-char (point-min)))
3230
3231 (defun article-goto-body ()
3232   "Place point at the start of the body."
3233   (goto-char (point-min))
3234   (cond
3235    ;; This variable is only bound when dealing with separate
3236    ;; MIME body parts.
3237    (article-goto-body-goes-to-point-min-p
3238     t)
3239    ((search-forward "\n\n" nil t)
3240     t)
3241    (t
3242     (goto-char (point-max))
3243     nil)))
3244
3245 (defun article-strip-multiple-blank-lines ()
3246   "Replace consecutive blank lines with one empty line."
3247   (interactive)
3248   (save-excursion
3249     (let ((inhibit-point-motion-hooks t)
3250           (inhibit-read-only t))
3251       ;; First make all blank lines empty.
3252       (article-goto-body)
3253       (while (re-search-forward "^[ \t]+$" nil t)
3254         (unless (gnus-annotation-in-region-p
3255                  (match-beginning 0) (match-end 0))
3256           (replace-match "" nil t)))
3257       ;; Then replace multiple empty lines with a single empty line.
3258       (article-goto-body)
3259       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3260         (unless (gnus-annotation-in-region-p
3261                  (match-beginning 0) (match-end 0))
3262           (delete-region (match-beginning 1) (match-end 1)))))))
3263
3264 (defun article-strip-leading-space ()
3265   "Remove all white space from the beginning of the lines in the article."
3266   (interactive)
3267   (save-excursion
3268     (let ((inhibit-point-motion-hooks t)
3269           (inhibit-read-only t))
3270       (article-goto-body)
3271       (while (re-search-forward "^[ \t]+" nil t)
3272         (replace-match "" t t)))))
3273
3274 (defun article-strip-trailing-space ()
3275   "Remove all white space from the end of the lines in the article."
3276   (interactive)
3277   (save-excursion
3278     (let ((inhibit-point-motion-hooks t)
3279           (inhibit-read-only t))
3280       (article-goto-body)
3281       (while (re-search-forward "[ \t]+$" nil t)
3282         (replace-match "" t t)))))
3283
3284 (defun article-strip-blank-lines ()
3285   "Strip leading, trailing and multiple blank lines."
3286   (interactive)
3287   (article-strip-leading-blank-lines)
3288   (article-remove-trailing-blank-lines)
3289   (article-strip-multiple-blank-lines))
3290
3291 (defun article-strip-all-blank-lines ()
3292   "Strip all blank lines."
3293   (interactive)
3294   (save-excursion
3295     (let ((inhibit-point-motion-hooks t)
3296           (inhibit-read-only t))
3297       (article-goto-body)
3298       (while (re-search-forward "^[ \t]*\n" nil t)
3299         (replace-match "" t t)))))
3300
3301 (defun gnus-article-narrow-to-signature ()
3302   "Narrow to the signature; return t if a signature is found, else nil."
3303   (let ((inhibit-point-motion-hooks t))
3304     (when (gnus-article-search-signature)
3305       (forward-line 1)
3306       ;; Check whether we have some limits to what we consider
3307       ;; to be a signature.
3308       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3309                       (list gnus-signature-limit)))
3310             limit limited)
3311         (while (setq limit (pop limits))
3312           (if (or (and (integerp limit)
3313                        (< (- (point-max) (point)) limit))
3314                   (and (floatp limit)
3315                        (< (count-lines (point) (point-max)) limit))
3316                   (and (functionp limit)
3317                        (funcall limit))
3318                   (and (stringp limit)
3319                        (not (re-search-forward limit nil t))))
3320               ()                        ; This limit did not succeed.
3321             (setq limited t
3322                   limits nil)))
3323         (unless limited
3324           (narrow-to-region (point) (point-max))
3325           t)))))
3326
3327 (defun gnus-article-search-signature ()
3328   "Search the current buffer for the signature separator.
3329 Put point at the beginning of the signature separator."
3330   (let ((cur (point)))
3331     (goto-char (point-max))
3332     (if (if (stringp gnus-signature-separator)
3333             (re-search-backward gnus-signature-separator nil t)
3334           (let ((seps gnus-signature-separator))
3335             (while (and seps
3336                         (not (re-search-backward (car seps) nil t)))
3337               (pop seps))
3338             seps))
3339         t
3340       (goto-char cur)
3341       nil)))
3342
3343 (defun gnus-article-hidden-arg ()
3344   "Return the current prefix arg as a number, or 0 if no prefix."
3345   (list (if current-prefix-arg
3346             (prefix-numeric-value current-prefix-arg)
3347           0)))
3348
3349 (defun gnus-article-check-hidden-text (type arg)
3350   "Return nil if hiding is necessary.
3351 Arg can be nil or a number.  nil and positive means hide, negative
3352 means show, 0 means toggle."
3353   (save-excursion
3354     (save-restriction
3355       (let ((hide (gnus-article-hidden-text-p type)))
3356         (cond
3357          ((or (null arg)
3358               (> arg 0))
3359           nil)
3360          ((< arg 0)
3361           (gnus-article-show-hidden-text type)
3362           t)
3363          (t
3364           (if (eq hide 'hidden)
3365               (progn
3366                 (gnus-article-show-hidden-text type)
3367                 t)
3368             nil)))))))
3369
3370 (defun gnus-article-hidden-text-p (type)
3371   "Say whether the current buffer contains hidden text of type TYPE."
3372   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3373     (while (and pos
3374                 (not (get-text-property pos 'invisible))
3375                 (not (get-text-property pos 'dummy-invisible)))
3376       (setq pos
3377             (text-property-any (1+ pos) (point-max) 'article-type type)))
3378     (if pos
3379         'hidden
3380       nil)))
3381
3382 (defun gnus-article-show-hidden-text (type &optional dummy)
3383   "Show all hidden text of type TYPE.
3384 Originally it is hide instead of DUMMY."
3385   (let ((inhibit-read-only t)
3386         (inhibit-point-motion-hooks t))
3387     (gnus-remove-text-properties-when
3388      'article-type type
3389      (point-min) (point-max)
3390      (cons 'article-type (cons type
3391                                gnus-hidden-properties)))
3392     (gnus-delete-wash-type type)))
3393
3394 (defconst article-time-units
3395   `((year . ,(* 365.25 24 60 60))
3396     (week . ,(* 7 24 60 60))
3397     (day . ,(* 24 60 60))
3398     (hour . ,(* 60 60))
3399     (minute . 60)
3400     (second . 1))
3401   "Mapping from time units to seconds.")
3402
3403 (defun gnus-article-forward-header ()
3404   "Move point to the start of the next header.
3405 If the current header is a continuation header, this can be several
3406 lines forward."
3407   (let ((ended nil))
3408     (while (not ended)
3409       (forward-line 1)
3410       (if (looking-at "[ \t]+[^ \t]")
3411           (forward-line 1)
3412         (setq ended t)))))
3413
3414 (defun article-treat-date ()
3415   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3416                        (with-current-buffer gnus-summary-buffer
3417                          gnus-article-date-headers)
3418                      gnus-article-date-headers)
3419                    t))
3420
3421 (defun article-date-ut (&optional type highlight date-position)
3422   "Convert DATE date to TYPE in the current article.
3423 The default type is `ut'.  See `gnus-article-date-headers' for
3424 possible values."
3425   (interactive (list 'ut t))
3426   (let* ((case-fold-search t)
3427          (inhibit-read-only t)
3428          (inhibit-point-motion-hooks t)
3429          (first t)
3430          (visible-date (mail-fetch-field "Date"))
3431          pos date bface eface)
3432     (save-excursion
3433       (goto-char (point-min))
3434       (when (re-search-forward "^Date:" nil t)
3435         (setq bface (get-text-property (point-at-bol) 'face)
3436               eface (get-text-property (1- (point-at-eol)) 'face)))
3437       ;; Delete any old Date headers.
3438       (if date-position
3439           (progn
3440             (goto-char date-position)
3441             (setq date (get-text-property (point) 'original-date))
3442             (delete-region (point)
3443                            (progn
3444                              (gnus-article-forward-header)
3445                              (point)))
3446             (article-transform-date date type bface eface))
3447         (save-restriction
3448           (widen)
3449           (goto-char (point-min))
3450           (while (or (get-text-property (setq pos (point)) 'original-date)
3451                      (and (setq pos (next-single-property-change
3452                                      (point) 'original-date))
3453                           (goto-char pos)))
3454             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3455                                       (1+ (match-beginning 0))
3456                                     (point-max)))
3457             (goto-char (point-min))
3458             (while (re-search-forward "^Date:" nil t)
3459               (setq date (get-text-property (match-beginning 0) 'original-date))
3460               (delete-region (point-at-bol) (progn
3461                                               (gnus-article-forward-header)
3462                                               (point))))
3463             (when (and (not date)
3464                        visible-date)
3465               (setq date visible-date))
3466             (when date
3467               (article-transform-date date type bface eface))
3468             (goto-char (point-max))
3469             (widen)))))))
3470
3471 (defun article-transform-date (date type bface eface)
3472   (dolist (this-type (cond
3473                       ((null type)
3474                        (list 'ut))
3475                       ((atom type)
3476                        (list type))
3477                       (t
3478                        type)))
3479     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3480     (forward-line -1)
3481     (beginning-of-line)
3482     (put-text-property (point) (1+ (point))
3483                        'original-date date)
3484     (put-text-property (point) (1+ (point))
3485                        'gnus-date-type this-type)
3486     ;; Do highlighting.
3487     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3488       (put-text-property (match-beginning 1) (1+ (match-end 1))
3489                          'face bface)
3490       (put-text-property (match-beginning 2) (match-end 2)
3491                          'face eface))
3492     (forward-line 1)))
3493
3494 (defun article-make-date-line (date type)
3495   "Return a DATE line of TYPE."
3496   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3497                              combined-lapsed))
3498     (error "Unknown conversion type: %s" type))
3499   (condition-case ()
3500       (let ((time (ignore-errors (date-to-time date))))
3501         (cond
3502          ;; Convert to the local timezone.
3503          ((eq type 'local)
3504           (concat "Date: " (message-make-date time)))
3505          ;; Convert to Universal Time.
3506          ((eq type 'ut)
3507           (concat "Date: "
3508                   (substring
3509                    (message-make-date
3510                     (let* ((e (parse-time-string date))
3511                            (tm (apply 'encode-time e))
3512                            (ms (car tm))
3513                            (ls (- (cadr tm) (car (current-time-zone time)))))
3514                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3515                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3516                             (t (list ms ls)))))
3517                    0 -5)
3518                   "UT"))
3519          ;; Get the original date from the article.
3520          ((eq type 'original)
3521           (concat "Date: " (if (string-match "\n+$" date)
3522                                (substring date 0 (match-beginning 0))
3523                              date)))
3524          ;; Let the user define the format.
3525          ((eq type 'user-defined)
3526           (let ((format (or (condition-case nil
3527                                 (with-current-buffer gnus-summary-buffer
3528                                   gnus-article-time-format)
3529                               (error nil))
3530                             gnus-article-time-format)))
3531             (if (functionp format)
3532                 (funcall format time)
3533               (concat "Date: " (format-time-string format time)))))
3534          ;; ISO 8601.
3535          ((eq type 'iso8601)
3536           (let ((tz (car (current-time-zone time))))
3537             (concat
3538              "Date: "
3539              (format-time-string "%Y%m%dT%H%M%S" time)
3540              (format "%s%02d%02d"
3541                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3542                      (/ (% (abs tz) 3600) 60)))))
3543          ;; Do a lapsed format.
3544          ((eq type 'lapsed)
3545           (concat "Date: " (article-lapsed-string time)))
3546          ;; A combined date/lapsed format.
3547          ((eq type 'combined-lapsed)
3548           (let ((date-string (article-make-date-line date 'original))
3549                 (segments 3)
3550                 lapsed-string)
3551             (while (and
3552                     time
3553                     (setq lapsed-string
3554                           (concat " (" (article-lapsed-string time segments) ")"))
3555                     (> (+ (length date-string)
3556                           (length lapsed-string))
3557                        (+ fill-column 6))
3558                     (> segments 0))
3559               (setq segments (1- segments)))
3560             (if (> segments 0)
3561                 (concat date-string lapsed-string)
3562               date-string)))
3563          ;; Display the date in proper English
3564          ((eq type 'english)
3565           (let ((dtime (decode-time time)))
3566             (concat
3567              "Date: the "
3568              (number-to-string (nth 3 dtime))
3569              (let ((digit (% (nth 3 dtime) 10)))
3570                (cond
3571                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3572                 ((= digit 1) "st")
3573                 ((= digit 2) "nd")
3574                 ((= digit 3) "rd")
3575                 (t "th")))
3576              " of "
3577              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3578              " "
3579              (number-to-string (nth 5 dtime))
3580              " at "
3581              (format "%02d" (nth 2 dtime))
3582              ":"
3583              (format "%02d" (nth 1 dtime)))))))
3584     (foo
3585      (format "Date: %s (from Gnus)" date))))
3586
3587 (defun article-lapsed-string (time &optional max-segments)
3588   ;; If the date is seriously mangled, the timezone functions are
3589   ;; liable to bug out, so we ignore all errors.
3590   (let* ((now (current-time))
3591          (real-time (subtract-time now time))
3592          (real-sec (and real-time
3593                         (+ (* (float (car real-time)) 65536)
3594                            (cadr real-time))))
3595          (sec (and real-time (abs real-sec)))
3596          (segments 0)
3597          num prev)
3598     (unless max-segments
3599       (setq max-segments (length article-time-units)))
3600     (cond
3601      ((null real-time)
3602       "Unknown")
3603      ((zerop sec)
3604       "Now")
3605      (t
3606       (concat
3607        ;; This is a bit convoluted, but basically we go
3608        ;; through the time units for years, weeks, etc,
3609        ;; and divide things to see whether that results
3610        ;; in positive answers.
3611        (mapconcat
3612         (lambda (unit)
3613           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3614                   (>= segments max-segments))
3615               ;; The (remaining) seconds are too few to
3616               ;; be divided into this time unit.
3617               ""
3618             ;; It's big enough, so we output it.
3619             (setq sec (- sec (* num (cdr unit))))
3620             (prog1
3621                 (concat (if prev ", " "") (int-to-string
3622                                            (floor num))
3623                         " " (symbol-name (car unit))
3624                         (if (> num 1) "s" ""))
3625               (setq prev t
3626                     segments (1+ segments)))))
3627         article-time-units "")
3628        ;; If dates are odd, then it might appear like the
3629        ;; article was sent in the future.
3630        (if (> real-sec 0)
3631            " ago"
3632          " in the future"))))))
3633
3634 (defun article-date-local (&optional highlight)
3635   "Convert the current article date to the local timezone."
3636   (interactive (list t))
3637   (article-date-ut 'local highlight))
3638
3639 (defun article-date-english (&optional highlight)
3640   "Convert the current article date to something that is proper English."
3641   (interactive (list t))
3642   (article-date-ut 'english highlight))
3643
3644 (defun article-date-original (&optional highlight)
3645   "Convert the current article date to what it was originally.
3646 This is only useful if you have used some other date conversion
3647 function and want to see what the date was before converting."
3648   (interactive (list t))
3649   (article-date-ut 'original highlight))
3650
3651 (defun article-date-lapsed (&optional highlight)
3652   "Convert the current article date to time lapsed since it was sent."
3653   (interactive (list t))
3654   (article-date-ut 'lapsed highlight))
3655
3656 (defun article-date-combined-lapsed (&optional highlight)
3657   "Convert the current article date to time lapsed since it was sent."
3658   (interactive (list t))
3659   (article-date-ut 'combined-lapsed highlight))
3660
3661 (defun article-update-date-lapsed ()
3662   "Function to be run from a timer to update the lapsed time line."
3663   (save-match-data
3664     (let ((buffer (current-buffer)))
3665       (ignore-errors
3666         (walk-windows
3667          (lambda (w)
3668            (set-buffer (window-buffer w))
3669            (when (eq major-mode 'gnus-article-mode)
3670              (let ((old-line (count-lines (point-min) (point)))
3671                    (old-column (- (point) (line-beginning-position)))
3672                    (window-start
3673                     (window-start (get-buffer-window (current-buffer)))))
3674                (goto-char (point-min))
3675                (while (re-search-forward "^Date:" nil t)
3676                  (let ((type (get-text-property (match-beginning 0)
3677                                                 'gnus-date-type)))
3678                    (when (memq type '(lapsed combined-lapsed user-format))
3679                      (when (and window-start
3680                                 (not (= window-start
3681                                         (save-excursion
3682                                           (forward-line 1)
3683                                           (point)))))
3684                        (setq window-start nil))
3685                      (save-excursion
3686                        (article-date-ut type t (match-beginning 0)))
3687                      (forward-line 1)
3688                      (when window-start
3689                        (set-window-start (get-buffer-window (current-buffer))
3690                                          (point))))))
3691                (goto-char (point-min))
3692                (when (> old-column 0)
3693                  (setq old-line (1- old-line)))
3694                (forward-line old-line)
3695                (end-of-line)
3696                (when (> (current-column) old-column)
3697                  (beginning-of-line)
3698                  (forward-char old-column)))))
3699          nil 'visible))
3700       (set-buffer buffer))))
3701
3702 (defun gnus-start-date-timer (&optional n)
3703   "Start a timer to update the Date headers in the article buffers.
3704 The numerical prefix says how frequently (in seconds) the function
3705 is to run."
3706   (interactive "p")
3707   (unless n
3708     (setq n 1))
3709   (gnus-stop-date-timer)
3710   (setq article-lapsed-timer
3711         (run-at-time 1 n 'article-update-date-lapsed)))
3712
3713 (defun gnus-stop-date-timer ()
3714   "Stop the Date timer."
3715   (interactive)
3716   (when article-lapsed-timer
3717     (nnheader-cancel-timer article-lapsed-timer)
3718     (setq article-lapsed-timer nil)))
3719
3720 (defun article-date-user (&optional highlight)
3721   "Convert the current article date to the user-defined format.
3722 This format is defined by the `gnus-article-time-format' variable."
3723   (interactive (list t))
3724   (article-date-ut 'user highlight))
3725
3726 (defun article-date-iso8601 (&optional highlight)
3727   "Convert the current article date to ISO8601."
3728   (interactive (list t))
3729   (article-date-ut 'iso8601 highlight))
3730
3731 (defmacro gnus-article-save-original-date (&rest forms)
3732   "Save the original date as a text property and evaluate FORMS."
3733   `(let* ((case-fold-search t)
3734           (start (progn
3735                    (goto-char (point-min))
3736                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3737                               (not (bolp)))
3738                      (match-end 0))))
3739           (date (when (and start
3740                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3741                                               nil t))
3742                   (buffer-substring-no-properties start
3743                                                   (match-beginning 0)))))
3744      (goto-char (point-max))
3745      (skip-chars-backward "\n")
3746      (put-text-property (point-min) (point) 'original-date date)
3747      ,@forms
3748      (goto-char (point-max))
3749      (skip-chars-backward "\n")
3750      (put-text-property (point-min) (point) 'original-date date)))
3751
3752 ;; (defun article-show-all ()
3753 ;;   "Show all hidden text in the article buffer."
3754 ;;   (interactive)
3755 ;;   (save-excursion
3756 ;;     (let ((inhibit-read-only t))
3757 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3758
3759 (defun article-remove-leading-whitespace ()
3760   "Remove excessive whitespace from all headers."
3761   (interactive)
3762   (save-excursion
3763     (save-restriction
3764       (let ((inhibit-read-only t))
3765         (article-narrow-to-head)
3766         (goto-char (point-min))
3767         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3768           (delete-region (match-beginning 1) (match-end 1)))))))
3769
3770 (defun article-emphasize (&optional arg)
3771   "Emphasize text according to `gnus-emphasis-alist'."
3772   (interactive (gnus-article-hidden-arg))
3773   (unless (gnus-article-check-hidden-text 'emphasis arg)
3774     (save-excursion
3775       (let ((alist (or
3776                     (condition-case nil
3777                         (with-current-buffer gnus-summary-buffer
3778                           gnus-article-emphasis-alist)
3779                       (error))
3780                     gnus-emphasis-alist))
3781             (inhibit-read-only t)
3782             (props (append '(article-type emphasis)
3783                            gnus-hidden-properties))
3784             regexp elem beg invisible visible face)
3785         (article-goto-body)
3786         (setq beg (point))
3787         (while (setq elem (pop alist))
3788           (goto-char beg)
3789           (setq regexp (car elem)
3790                 invisible (nth 1 elem)
3791                 visible (nth 2 elem)
3792                 face (nth 3 elem))
3793           (while (re-search-forward regexp nil t)
3794             (when (and (match-beginning visible) (match-beginning invisible))
3795               (gnus-article-hide-text
3796                (match-beginning invisible) (match-end invisible) props)
3797               (gnus-article-unhide-text-type
3798                (match-beginning visible) (match-end visible) 'emphasis)
3799               (gnus-put-overlay-excluding-newlines
3800                (match-beginning visible) (match-end visible) 'face face)
3801               (gnus-add-wash-type 'emphasis)
3802               (goto-char (match-end invisible)))))))))
3803
3804 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3805   "Setup newsgroup emphasis alist."
3806   (unless gnus-article-emphasis-alist
3807     (let ((name (and gnus-newsgroup-name
3808                      (gnus-group-real-name gnus-newsgroup-name))))
3809       (make-local-variable 'gnus-article-emphasis-alist)
3810       (setq gnus-article-emphasis-alist
3811             (nconc
3812              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3813                (while (setq elem (pop alist))
3814                  (when (and name (string-match (car elem) name))
3815                    (setq alist nil
3816                          highlight (copy-sequence (cdr elem)))))
3817                highlight)
3818              (copy-sequence highlight-words)
3819              (if gnus-newsgroup-name
3820                  (copy-sequence (gnus-group-find-parameter
3821                                  gnus-newsgroup-name 'highlight-words t)))
3822              gnus-emphasis-alist)))))
3823
3824 (defvar gnus-summary-article-menu)
3825 (defvar gnus-summary-post-menu)
3826
3827 ;;; Saving functions.
3828
3829 (defun gnus-article-save (save-buffer file &optional num)
3830   "Save the currently selected article."
3831   (when (or (get gnus-default-article-saver :headers)
3832             (not gnus-save-all-headers))
3833     ;; Remove headers according to `gnus-saved-headers' or the value
3834     ;; of the `:headers' property that the saver function might have.
3835     (let ((gnus-visible-headers
3836            (or (symbol-value (get gnus-default-article-saver :headers))
3837                gnus-saved-headers gnus-visible-headers))
3838           ;; Ignore group parameter.  See `article-hide-headers'.
3839           (gnus-summary-buffer nil))
3840       (with-current-buffer save-buffer
3841         (article-hide-headers 1 t))))
3842   (save-window-excursion
3843     (if (not gnus-default-article-saver)
3844         (error "No default saver is defined")
3845       ;; !!! Magic!  The saving functions all save
3846       ;; `gnus-save-article-buffer' (or so they think), but we
3847       ;; bind that variable to our save-buffer.
3848       (set-buffer gnus-article-buffer)
3849       (let* ((gnus-save-article-buffer save-buffer)
3850              (filename
3851               (cond
3852                ((not gnus-prompt-before-saving) 'default)
3853                ((eq gnus-prompt-before-saving 'always) nil)
3854                (t file)))
3855              (gnus-number-of-articles-to-be-saved
3856               (when (eq gnus-prompt-before-saving t)
3857                 num)))                  ; Magic
3858         (set-buffer gnus-article-current-summary)
3859         (funcall gnus-default-article-saver filename)))))
3860
3861 (defun gnus-read-save-file-name (prompt &optional filename
3862                                         function group headers variable
3863                                         dir-var)
3864   (let ((default-name
3865           (funcall function group headers (symbol-value variable)))
3866         result)
3867     (setq result
3868           (expand-file-name
3869            (cond
3870             ((eq filename 'default)
3871              default-name)
3872             ((eq filename t)
3873              default-name)
3874             (filename filename)
3875             (t
3876              (when (symbol-value dir-var)
3877                (setq default-name (expand-file-name
3878                                    (file-name-nondirectory default-name)
3879                                    (symbol-value dir-var))))
3880              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3881                     (prompt
3882                      (format prompt
3883                              (if (and gnus-number-of-articles-to-be-saved
3884                                       (> gnus-number-of-articles-to-be-saved 1))
3885                                  (format "these %d articles"
3886                                          gnus-number-of-articles-to-be-saved)
3887                                "this article")))
3888                     (file
3889                      ;; Let the split methods have their say.
3890                      (cond
3891                       ;; No split name was found.
3892                       ((null split-name)
3893                        (read-file-name
3894                         (concat prompt " (default "
3895                                 (file-name-nondirectory default-name) "): ")
3896                         (file-name-directory default-name)
3897                         default-name))
3898                       ;; A single group name is returned.
3899                       ((stringp split-name)
3900                        (setq default-name
3901                              (funcall function split-name headers
3902                                       (symbol-value variable)))
3903                        (read-file-name
3904                         (concat prompt " (default "
3905                                 (file-name-nondirectory default-name) "): ")
3906                         (file-name-directory default-name)
3907                         default-name))
3908                       ;; A single split name was found
3909                       ((= 1 (length split-name))
3910                        (let* ((name (expand-file-name
3911                                      (car split-name)
3912                                      gnus-article-save-directory))
3913                               (dir (cond ((file-directory-p name)
3914                                           (file-name-as-directory name))
3915                                          ((file-exists-p name) name)
3916                                          (t gnus-article-save-directory))))
3917                          (read-file-name
3918                           (concat prompt " (default " name "): ")
3919                           dir name)))
3920                       ;; A list of splits was found.
3921                       (t
3922                        (setq split-name (nreverse split-name))
3923                        (let (result)
3924                          (let ((file-name-history
3925                                 (nconc split-name file-name-history)))
3926                            (setq result
3927                                  (expand-file-name
3928                                   (read-file-name
3929                                    (concat prompt " (`M-p' for defaults): ")
3930                                    gnus-article-save-directory
3931                                    (car split-name))
3932                                   gnus-article-save-directory)))
3933                          (car (push result file-name-history)))))))
3934                ;; Create the directory.
3935                (gnus-make-directory (file-name-directory file))
3936                ;; If we have read a directory, we append the default file name.
3937                (when (file-directory-p file)
3938                  (setq file (expand-file-name (file-name-nondirectory
3939                                                default-name)
3940                                               (file-name-as-directory file))))
3941                ;; Possibly translate some characters.
3942                (nnheader-translate-file-chars file))))))
3943     (gnus-make-directory (file-name-directory result))
3944     (when variable
3945       (set variable result))
3946     (when dir-var
3947       (set dir-var (file-name-directory result)))
3948     result))
3949
3950 (defun gnus-article-archive-name (group)
3951   "Return the first instance of an \"Archive-name\" in the current buffer."
3952   (let ((case-fold-search t))
3953     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3954       (nnheader-concat gnus-article-save-directory
3955                        (match-string 1)))))
3956
3957 (defun gnus-article-nndoc-name (group)
3958   "If GROUP is an nndoc group, return the name of the parent group."
3959   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3960     (gnus-group-get-parameter group 'save-article-group)))
3961
3962 (defun gnus-summary-save-in-rmail (&optional filename)
3963   "Append this article to Rmail file.
3964 Optional argument FILENAME specifies file name.
3965 Directory to save to is default to `gnus-article-save-directory'."
3966   (setq filename (gnus-read-save-file-name
3967                   "Save %s in rmail file" filename
3968                   gnus-rmail-save-name gnus-newsgroup-name
3969                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3970   (with-current-buffer gnus-save-article-buffer
3971     (save-excursion
3972       (save-restriction
3973         (widen)
3974         ;; Note that unlike gnus-summary-save-in-mail, there is no
3975         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3976         ;; not use Babyl.
3977         (gnus-output-to-rmail filename))))
3978   filename)
3979
3980 (defun gnus-summary-save-in-mail (&optional filename)
3981   "Append this article to Unix mail file.
3982 Optional argument FILENAME specifies file name.
3983 Directory to save to is default to `gnus-article-save-directory'."
3984   (setq filename (gnus-read-save-file-name
3985                   "Save %s in Unix mail file" filename
3986                   gnus-mail-save-name gnus-newsgroup-name
3987                   gnus-current-headers 'gnus-newsgroup-last-mail))
3988   (with-current-buffer gnus-save-article-buffer
3989     (save-excursion
3990       (save-restriction
3991         (widen)
3992         (if (and (file-readable-p filename)
3993                  (file-regular-p filename)
3994                  (mail-file-babyl-p filename))
3995             (gnus-output-to-rmail filename)
3996           (gnus-output-to-mail filename)))))
3997   filename)
3998
3999 (put 'gnus-summary-save-in-file :decode t)
4000 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4001 (defun gnus-summary-save-in-file (&optional filename overwrite)
4002   "Append this article to file.
4003 Optional argument FILENAME specifies file name.
4004 Directory to save to is default to `gnus-article-save-directory'."
4005   (setq filename (gnus-read-save-file-name
4006                   "Save %s in file" filename
4007                   gnus-file-save-name gnus-newsgroup-name
4008                   gnus-current-headers 'gnus-newsgroup-last-file))
4009   (with-current-buffer gnus-save-article-buffer
4010     (save-excursion
4011       (save-restriction
4012         (widen)
4013         (when (and overwrite
4014                    (file-exists-p filename))
4015           (delete-file filename))
4016         (gnus-output-to-file filename))))
4017   filename)
4018
4019 (put 'gnus-summary-write-to-file :decode t)
4020 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4021 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4022 (defun gnus-summary-write-to-file (&optional filename)
4023   "Write this article to a file, overwriting it if the file exists.
4024 Optional argument FILENAME specifies file name.
4025 The directory to save in defaults to `gnus-article-save-directory'."
4026   (setq filename (gnus-read-save-file-name
4027                   "Save %s in file" filename
4028                   gnus-file-save-name gnus-newsgroup-name
4029                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4030   (gnus-summary-save-in-file filename t))
4031
4032 (put 'gnus-summary-save-body-in-file :decode t)
4033 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4034   "Append this article body to a file.
4035 Optional argument FILENAME specifies file name.
4036 The directory to save in defaults to `gnus-article-save-directory'."
4037   (setq filename (gnus-read-save-file-name
4038                   "Save %s body in file" filename
4039                   gnus-file-save-name gnus-newsgroup-name
4040                   gnus-current-headers 'gnus-newsgroup-last-file))
4041   (with-current-buffer gnus-save-article-buffer
4042     (save-excursion
4043       (save-restriction
4044         (widen)
4045         (when (article-goto-body)
4046           (narrow-to-region (point) (point-max)))
4047         (when (and overwrite
4048                    (file-exists-p filename))
4049           (delete-file filename))
4050         (gnus-output-to-file filename))))
4051   filename)
4052
4053 (put 'gnus-summary-write-body-to-file :decode t)
4054 (put 'gnus-summary-write-body-to-file
4055      :function 'gnus-summary-save-body-in-file)
4056 (defun gnus-summary-write-body-to-file (&optional filename)
4057   "Write this article body to a file, overwriting it if the file exists.
4058 Optional argument FILENAME specifies file name.
4059 The directory to save in defaults to `gnus-article-save-directory'."
4060   (setq filename (gnus-read-save-file-name
4061                   "Save %s body in file" filename
4062                   gnus-file-save-name gnus-newsgroup-name
4063                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4064   (gnus-summary-save-body-in-file filename t))
4065
4066 (put 'gnus-summary-save-in-pipe :decode t)
4067 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4068 (defun gnus-summary-save-in-pipe (&optional command raw)
4069   "Pipe this article to subprocess COMMAND.
4070 Valid values for COMMAND include:
4071   a string
4072     The executable command name and possibly arguments.
4073   nil
4074     You will be prompted for the command in the minibuffer.
4075   the symbol `default'
4076     It will be replaced with the command which the variable
4077     `gnus-summary-pipe-output-default-command' holds or the command
4078     last used for saving.
4079 Non-nil value for RAW overrides `:decode' and `:headers' properties
4080 and the raw article including all headers will be piped."
4081   (let ((article (gnus-summary-article-number))
4082         (decode (unless raw
4083                   (get 'gnus-summary-save-in-pipe :decode)))
4084         save-buffer default)
4085     (if article
4086         (if (vectorp (gnus-summary-article-header article))
4087             (save-current-buffer
4088               (gnus-summary-select-article decode decode nil article)
4089               (insert-buffer-substring
4090                (prog1
4091                    (if decode
4092                        gnus-article-buffer
4093                      gnus-original-article-buffer)
4094                  (setq save-buffer
4095                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4096               ;; Remove unwanted headers.
4097               (when (and (not raw)
4098                          (or (get 'gnus-summary-save-in-pipe :headers)
4099                              (not gnus-save-all-headers)))
4100                 (let ((gnus-visible-headers
4101                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4102                                               :headers))
4103                            gnus-saved-headers gnus-visible-headers))
4104                       (gnus-summary-buffer nil))
4105                   (article-hide-headers 1 t))))
4106           (error "%d is not a real article" article))
4107       (error "No article to pipe"))
4108     (setq default (or gnus-summary-pipe-output-default-command
4109                       gnus-last-shell-command))
4110     (unless (stringp command)
4111       (setq command
4112             (if (and (eq command 'default) default)
4113                 default
4114               (gnus-read-shell-command "Shell command on this article: "
4115                                        default))))
4116     (when (string-equal command "")
4117       (if default
4118           (setq command default)
4119         (error "A command is required")))
4120     (with-current-buffer save-buffer
4121       (save-restriction
4122         (widen)
4123         (shell-command-on-region (point-min) (point-max) command nil)))
4124     (gnus-kill-buffer save-buffer))
4125   (setq gnus-summary-pipe-output-default-command command))
4126
4127 (defun gnus-summary-pipe-to-muttprint (&optional command)
4128   "Pipe this article to muttprint."
4129   (unless (stringp command)
4130     (setq command (read-string
4131                    "Print using command: " gnus-summary-muttprint-program
4132                    nil gnus-summary-muttprint-program)))
4133   (let ((gnus-summary-pipe-output-default-command
4134          gnus-summary-pipe-output-default-command))
4135     (gnus-summary-save-in-pipe command))
4136   (setq gnus-summary-muttprint-program command))
4137
4138 ;;; Article file names when saving.
4139
4140 (defun gnus-capitalize-newsgroup (newsgroup)
4141   "Capitalize NEWSGROUP name."
4142   (when (not (zerop (length newsgroup)))
4143     (concat (char-to-string (upcase (aref newsgroup 0)))
4144             (substring newsgroup 1))))
4145
4146 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4147   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4148 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4149 Otherwise, it is like ~/News/news/group/num."
4150   (let ((default
4151           (expand-file-name
4152            (concat (if (gnus-use-long-file-name 'not-save)
4153                        (gnus-capitalize-newsgroup newsgroup)
4154                      (gnus-newsgroup-directory-form newsgroup))
4155                    "/" (int-to-string (mail-header-number headers)))
4156            gnus-article-save-directory)))
4157     (if (and last-file
4158              (string-equal (file-name-directory default)
4159                            (file-name-directory last-file))
4160              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4161         default
4162       (or last-file default))))
4163
4164 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4165   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4166 If variable `gnus-use-long-file-name' is non-nil, it is
4167 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4168   (let ((default
4169           (expand-file-name
4170            (concat (if (gnus-use-long-file-name 'not-save)
4171                        newsgroup
4172                      (gnus-newsgroup-directory-form newsgroup))
4173                    "/" (int-to-string (mail-header-number headers)))
4174            gnus-article-save-directory)))
4175     (if (and last-file
4176              (string-equal (file-name-directory default)
4177                            (file-name-directory last-file))
4178              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4179         default
4180       (or last-file default))))
4181
4182 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4183   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4184 If variable `gnus-use-long-file-name' is non-nil, it is
4185 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4186   (or last-file
4187       (expand-file-name
4188        (if (gnus-use-long-file-name 'not-save)
4189            newsgroup
4190          (file-relative-name
4191           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4192           default-directory))
4193        gnus-article-save-directory)))
4194
4195 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4196   "Generate file name from sender."
4197   (let ((from (mail-header-from headers)))
4198     (expand-file-name
4199      (if (and from (string-match "\\([^ <]+\\)@" from))
4200          (match-string 1 from)
4201        "nobody")
4202      gnus-article-save-directory)))
4203
4204 (defun article-verify-x-pgp-sig ()
4205   "Verify X-PGP-Sig."
4206   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4207   (interactive)
4208   (if (gnus-buffer-live-p gnus-original-article-buffer)
4209       (let ((sig (with-current-buffer gnus-original-article-buffer
4210                    (gnus-fetch-field "X-PGP-Sig")))
4211             items info headers)
4212         (when (and sig
4213                    mml2015-use
4214                    (mml2015-clear-verify-function))
4215           (with-temp-buffer
4216             (insert-buffer-substring gnus-original-article-buffer)
4217             (setq items (split-string sig))
4218             (message-narrow-to-head)
4219             (let ((inhibit-point-motion-hooks t)
4220                   (case-fold-search t))
4221               ;; Don't verify multiple headers.
4222               (setq headers (mapconcat (lambda (header)
4223                                          (concat header ": "
4224                                                  (mail-fetch-field header)
4225                                                  "\n"))
4226                                        (split-string (nth 1 items) ",") "")))
4227             (delete-region (point-min) (point-max))
4228             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4229             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4230             (insert headers)
4231             (widen)
4232             (forward-line)
4233             (while (not (eobp))
4234               (if (looking-at "^-")
4235                   (insert "- "))
4236               (forward-line))
4237             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4238             (insert "Version: " (car items) "\n\n")
4239             (insert (mapconcat 'identity (cddr items) "\n"))
4240             (insert "\n-----END PGP SIGNATURE-----\n")
4241             (let ((mm-security-handle (list (format "multipart/signed"))))
4242               (mml2015-clean-buffer)
4243               (let ((coding-system-for-write (or gnus-newsgroup-charset
4244                                                  'iso-8859-1)))
4245                 (funcall (mml2015-clear-verify-function)))
4246               (setq info
4247                     (or (mm-handle-multipart-ctl-parameter
4248                          mm-security-handle 'gnus-details)
4249                         (mm-handle-multipart-ctl-parameter
4250                          mm-security-handle 'gnus-info)))))
4251           (when info
4252             (let ((inhibit-read-only t) bface eface)
4253               (save-restriction
4254                 (message-narrow-to-head)
4255                 (goto-char (point-max))
4256                 (forward-line -1)
4257                 (setq bface (get-text-property (point-at-bol) 'face)
4258                       eface (get-text-property (1- (point-at-eol)) 'face))
4259                 (message-remove-header "X-Gnus-PGP-Verify")
4260                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4261                     (forward-line)
4262                   (goto-char (point-max)))
4263                 (narrow-to-region (point) (point))
4264                 (insert "X-Gnus-PGP-Verify: " info "\n")
4265                 (goto-char (point-min))
4266                 (forward-line)
4267                 (while (not (eobp))
4268                   (if (not (looking-at "^[ \t]"))
4269                       (insert " "))
4270                   (forward-line))
4271                 ;; Do highlighting.
4272                 (goto-char (point-min))
4273                 (when (looking-at "\\([^:]+\\): *")
4274                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4275                                      'face bface)
4276                   (put-text-property (match-end 0) (point-max)
4277                                      'face eface)))))))))
4278
4279 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4280
4281 (defun article-verify-cancel-lock ()
4282   "Verify Cancel-Lock header."
4283   (interactive)
4284   (if (gnus-buffer-live-p gnus-original-article-buffer)
4285       (canlock-verify gnus-original-article-buffer)))
4286
4287 (eval-and-compile
4288   (mapc
4289    (lambda (func)
4290      (let (afunc gfunc)
4291        (if (consp func)
4292            (setq afunc (car func)
4293                  gfunc (cdr func))
4294          (setq afunc func
4295                gfunc (intern (format "gnus-%s" func))))
4296        (defalias gfunc
4297          (when (fboundp afunc)
4298            `(lambda (&optional interactive &rest args)
4299               ,(documentation afunc t)
4300               (interactive (list t))
4301               (with-current-buffer gnus-article-buffer
4302                 (if interactive
4303                     (call-interactively ',afunc)
4304                   (apply ',afunc args))))))))
4305    '(article-hide-headers
4306      article-verify-x-pgp-sig
4307      article-verify-cancel-lock
4308      article-hide-boring-headers
4309      article-treat-overstrike
4310      article-treat-ansi-sequences
4311      article-fill-long-lines
4312      article-capitalize-sentences
4313      article-remove-cr
4314      article-remove-leading-whitespace
4315      article-display-x-face
4316      article-display-face
4317      article-de-quoted-unreadable
4318      article-de-base64-unreadable
4319      article-decode-HZ
4320      article-wash-html
4321      article-unsplit-urls
4322      article-hide-list-identifiers
4323      article-strip-banner
4324      article-babel
4325      article-hide-pem
4326      article-hide-signature
4327      article-strip-headers-in-body
4328      article-remove-trailing-blank-lines
4329      article-strip-leading-blank-lines
4330      article-strip-multiple-blank-lines
4331      article-strip-leading-space
4332      article-strip-trailing-space
4333      article-strip-blank-lines
4334      article-strip-all-blank-lines
4335      article-date-local
4336      article-date-english
4337      article-date-iso8601
4338      article-date-original
4339      article-treat-date
4340      article-date-ut
4341      article-decode-mime-words
4342      article-decode-charset
4343      article-decode-encoded-words
4344      article-date-user
4345      article-date-lapsed
4346      article-date-combined-lapsed
4347      article-emphasize
4348      article-treat-dumbquotes
4349      article-treat-non-ascii
4350      article-normalize-headers
4351      ;;(article-show-all . gnus-article-show-all-headers)
4352      )))
4353 \f
4354 ;;;
4355 ;;; Gnus article mode
4356 ;;;
4357
4358 (put 'gnus-article-mode 'mode-class 'special)
4359
4360 (set-keymap-parent gnus-article-mode-map widget-keymap)
4361
4362 (gnus-define-keys gnus-article-mode-map
4363   " " gnus-article-goto-next-page
4364   "\177" gnus-article-goto-prev-page
4365   [delete] gnus-article-goto-prev-page
4366   [backspace] gnus-article-goto-prev-page
4367   "\C-c^" gnus-article-refer-article
4368   "h" gnus-article-show-summary
4369   "s" gnus-article-show-summary
4370   "\C-c\C-m" gnus-article-mail
4371   "?" gnus-article-describe-briefly
4372   "<" beginning-of-buffer
4373   ">" end-of-buffer
4374   "\C-c\C-i" gnus-info-find-node
4375   "\C-c\C-b" gnus-bug
4376   "R" gnus-article-reply-with-original
4377   "F" gnus-article-followup-with-original
4378   "\C-hk" gnus-article-describe-key
4379   "\C-hc" gnus-article-describe-key-briefly
4380   "\C-hb" gnus-article-describe-bindings
4381
4382   "e" gnus-article-read-summary-keys
4383   "\C-d" gnus-article-read-summary-keys
4384   "\M-*" gnus-article-read-summary-keys
4385   "\M-#" gnus-article-read-summary-keys
4386   "\M-^" gnus-article-read-summary-keys
4387   "\M-g" gnus-article-read-summary-keys)
4388
4389 (substitute-key-definition
4390  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4391
4392 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4393   "W" gnus-article-wide-reply-with-original)
4394 (if (featurep 'xemacs)
4395     (set-keymap-default-binding gnus-article-send-map
4396                                 'gnus-article-read-summary-send-keys)
4397   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4398
4399 (defun gnus-article-make-menu-bar ()
4400   (unless (boundp 'gnus-article-commands-menu)
4401     (gnus-summary-make-menu-bar))
4402   (unless (boundp 'gnus-article-article-menu)
4403     (easy-menu-define
4404      gnus-article-article-menu gnus-article-mode-map ""
4405      '("Article"
4406        ["Scroll forwards" gnus-article-goto-next-page t]
4407        ["Scroll backwards" gnus-article-goto-prev-page t]
4408        ["Show summary" gnus-article-show-summary t]
4409        ["Fetch Message-ID at point" gnus-article-refer-article t]
4410        ["Mail to address at point" gnus-article-mail t]
4411        ["Send a bug report" gnus-bug t]))
4412
4413     (easy-menu-define
4414      gnus-article-treatment-menu gnus-article-mode-map ""
4415      ;; Fixme: this should use :active (and maybe :visible).
4416      '("Treatment"
4417        ["Hide headers" gnus-article-hide-headers t]
4418        ["Hide signature" gnus-article-hide-signature t]
4419        ["Hide citation" gnus-article-hide-citation t]
4420        ["Treat overstrike" gnus-article-treat-overstrike t]
4421        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4422        ["Remove carriage return" gnus-article-remove-cr t]
4423        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4424        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4425        ["Remove base64" gnus-article-de-base64-unreadable t]
4426        ["Treat html" gnus-article-wash-html t]
4427        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4428        ["Decode HZ" gnus-article-decode-HZ t]))
4429
4430     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4431
4432     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4433
4434     (gnus-run-hooks 'gnus-article-menu-hook)))
4435
4436 (defvar bookmark-make-record-function)
4437 (defvar shr-put-image-function)
4438
4439 (defun gnus-article-mode ()
4440   "Major mode for displaying an article.
4441
4442 All normal editing commands are switched off.
4443
4444 The following commands are available in addition to all summary mode
4445 commands:
4446 \\<gnus-article-mode-map>
4447 \\[gnus-article-next-page]\t Scroll the article one page forwards
4448 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4449 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4450 \\[gnus-article-show-summary]\t Display the summary buffer
4451 \\[gnus-article-mail]\t Send a reply to the address near point
4452 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4453 \\[gnus-info-find-node]\t Go to the Gnus info node"
4454   (interactive)
4455   (kill-all-local-variables)
4456   (gnus-simplify-mode-line)
4457   (setq mode-name "Article")
4458   (setq major-mode 'gnus-article-mode)
4459   (make-local-variable 'minor-mode-alist)
4460   (use-local-map gnus-article-mode-map)
4461   (when (gnus-visual-p 'article-menu 'menu)
4462     (gnus-article-make-menu-bar)
4463     (when gnus-summary-tool-bar-map
4464       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4465   (gnus-update-format-specifications nil 'article-mode)
4466   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4467   (set (make-local-variable 'gnus-page-broken) nil)
4468   (make-local-variable 'gnus-article-current-summary)
4469   (make-local-variable 'gnus-article-mime-handles)
4470   (make-local-variable 'gnus-article-decoded-p)
4471   (make-local-variable 'gnus-article-mime-handle-alist)
4472   (make-local-variable 'gnus-article-wash-types)
4473   (make-local-variable 'gnus-article-image-alist)
4474   (make-local-variable 'gnus-article-charset)
4475   (make-local-variable 'gnus-article-ignored-charsets)
4476   (set (make-local-variable 'bookmark-make-record-function)
4477        'gnus-summary-bookmark-make-record)
4478   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4479   ;; face.
4480   (set (make-local-variable 'nobreak-char-display) nil)
4481   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4482   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4483   (setq cursor-in-non-selected-windows nil)
4484   (gnus-set-default-directory)
4485   (buffer-disable-undo)
4486   (setq buffer-read-only t
4487         show-trailing-whitespace nil)
4488   (set-syntax-table gnus-article-mode-syntax-table)
4489   (mm-enable-multibyte)
4490   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4491
4492 (defun gnus-article-setup-buffer ()
4493   "Initialize the article buffer."
4494   (let* ((name (if gnus-single-article-buffer "*Article*"
4495                  (concat "*Article "
4496                          (gnus-group-decoded-name gnus-newsgroup-name)
4497                          "*")))
4498          (original
4499           (progn (string-match "\\*Article" name)
4500                  (concat " *Original Article"
4501                          (substring name (match-end 0))))))
4502     (setq gnus-article-buffer name)
4503     (setq gnus-original-article-buffer original)
4504     (setq gnus-article-mime-handle-alist nil)
4505     (with-current-buffer gnus-summary-buffer
4506       ;; This might be a variable local to the summary buffer.
4507       (unless gnus-single-article-buffer
4508         (setq gnus-article-buffer name)
4509         (setq gnus-original-article-buffer original)
4510         (gnus-set-global-variables)))
4511     (gnus-article-setup-highlight-words)
4512     ;; Init original article buffer.
4513     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4514       (mm-enable-multibyte)
4515       (setq major-mode 'gnus-original-article-mode)
4516       (make-local-variable 'gnus-original-article))
4517     (if (and (get-buffer name)
4518              (with-current-buffer name
4519                (if gnus-article-edit-mode
4520                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4521                        (progn
4522                          (set-buffer-modified-p nil)
4523                          (gnus-kill-buffer name)
4524                          (message "")
4525                          nil)
4526                      (error "Action aborted"))
4527                  t)))
4528         (with-current-buffer name
4529           (set (make-local-variable 'gnus-article-edit-mode) nil)
4530           (gnus-article-stop-animations)
4531           (when gnus-article-mime-handles
4532             (mm-destroy-parts gnus-article-mime-handles)
4533             (setq gnus-article-mime-handles nil))
4534           ;; Set it to nil in article-buffer!
4535           (setq gnus-article-mime-handle-alist nil)
4536           (buffer-disable-undo)
4537           (setq buffer-read-only t)
4538           (unless (eq major-mode 'gnus-article-mode)
4539             (gnus-article-mode))
4540           (setq truncate-lines gnus-article-truncate-lines)
4541           (current-buffer))
4542       (with-current-buffer (gnus-get-buffer-create name)
4543         (gnus-article-mode)
4544         (setq truncate-lines gnus-article-truncate-lines)
4545         (make-local-variable 'gnus-summary-buffer)
4546         (setq gnus-summary-buffer
4547               (gnus-summary-buffer-name gnus-newsgroup-name))
4548         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4549         (when article-lapsed-timer
4550           (gnus-stop-date-timer))
4551         (when gnus-article-update-date-headers
4552           (gnus-start-date-timer gnus-article-update-date-headers))
4553         (current-buffer)))))
4554
4555 (defun gnus-article-stop-animations ()
4556   (dolist (timer (and (boundp 'timer-list)
4557                       timer-list))
4558     (when (eq (gnus-timer--function timer) 'image-animate-timeout)
4559       (cancel-timer timer))))
4560
4561 (defun gnus-stop-downloads ()
4562   (when (boundp 'url-queue)
4563     (set (intern "url-queue" obarray) nil)))
4564
4565 ;; Set article window start at LINE, where LINE is the number of lines
4566 ;; from the head of the article.
4567 (defun gnus-article-set-window-start (&optional line)
4568   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4569     (when article-window
4570       (set-window-start
4571        article-window
4572        (with-current-buffer gnus-article-buffer
4573          (goto-char (point-min))
4574          (if (not line)
4575              (point-min)
4576            (gnus-message 6 "Moved to bookmark")
4577            (search-forward "\n\n" nil t)
4578            (forward-line line)
4579            (point)))))))
4580
4581 (defun gnus-article-prepare (article &optional all-headers header)
4582   "Prepare ARTICLE in article mode buffer.
4583 ARTICLE should either be an article number or a Message-ID.
4584 If ARTICLE is an id, HEADER should be the article headers.
4585 If ALL-HEADERS is non-nil, no headers are hidden."
4586   (save-excursion
4587     ;; Make sure we start in a summary buffer.
4588     (unless (eq major-mode 'gnus-summary-mode)
4589       (set-buffer gnus-summary-buffer))
4590     (setq gnus-summary-buffer (current-buffer))
4591     (let* ((gnus-article (if header (mail-header-number header) article))
4592            (summary-buffer (current-buffer))
4593            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4594            (group gnus-newsgroup-name)
4595            result)
4596       (save-excursion
4597         (gnus-article-setup-buffer)
4598         (set-buffer gnus-article-buffer)
4599         ;; Deactivate active regions.
4600         (when (and (boundp 'transient-mark-mode)
4601                    transient-mark-mode)
4602           (setq mark-active nil))
4603         (if (not (setq result (let ((inhibit-read-only t))
4604                                 (gnus-request-article-this-buffer
4605                                  article group))))
4606             ;; There is no such article.
4607             (save-excursion
4608               (when (and (numberp article)
4609                          (not (memq article gnus-newsgroup-sparse)))
4610                 (setq gnus-article-current
4611                       (cons gnus-newsgroup-name article))
4612                 (set-buffer gnus-summary-buffer)
4613                 (setq gnus-current-article article)
4614                 (if (and (memq article gnus-newsgroup-undownloaded)
4615                          (not (gnus-online (gnus-find-method-for-group
4616                                             gnus-newsgroup-name))))
4617                     (progn
4618                       (gnus-summary-set-agent-mark article)
4619                       (message "Message marked for downloading"))
4620                   (gnus-summary-mark-article article gnus-canceled-mark)
4621                   (unless (memq article gnus-newsgroup-sparse)
4622                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4623           (if (or (eq result 'pseudo)
4624                   (eq result 'nneething))
4625               (progn
4626                 (with-current-buffer summary-buffer
4627                   (push article gnus-newsgroup-history)
4628                   (setq gnus-last-article gnus-current-article
4629                         gnus-current-article 0
4630                         gnus-current-headers nil
4631                         gnus-article-current nil)
4632                   (if (eq result 'nneething)
4633                       (gnus-configure-windows 'summary)
4634                     (gnus-configure-windows 'article))
4635                   (gnus-set-global-variables))
4636                 (let ((gnus-article-mime-handle-alist-1
4637                        gnus-article-mime-handle-alist))
4638                   (gnus-set-mode-line 'article)))
4639             ;; The result from the `request' was an actual article -
4640             ;; or at least some text that is now displayed in the
4641             ;; article buffer.
4642             (when (and (numberp article)
4643                        (not (eq article gnus-current-article)))
4644               ;; Seems like a new article has been selected.
4645               ;; `gnus-current-article' must be an article number.
4646               (with-current-buffer summary-buffer
4647                 (push article gnus-newsgroup-history)
4648                 (setq gnus-last-article gnus-current-article
4649                       gnus-current-article article
4650                       gnus-current-headers
4651                       (gnus-summary-article-header gnus-current-article)
4652                       gnus-article-current
4653                       (cons gnus-newsgroup-name gnus-current-article))
4654                 (unless (vectorp gnus-current-headers)
4655                   (setq gnus-current-headers nil))
4656                 (gnus-summary-goto-subject gnus-current-article)
4657                 (when (gnus-summary-show-thread)
4658                   ;; If the summary buffer really was folded, the
4659                   ;; previous goto may not actually have gone to
4660                   ;; the right article, but the thread root instead.
4661                   ;; So we go again.
4662                   (gnus-summary-goto-subject gnus-current-article))
4663                 (gnus-run-hooks 'gnus-mark-article-hook)
4664                 (gnus-set-mode-line 'summary)
4665                 (when (gnus-visual-p 'article-highlight 'highlight)
4666                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4667                 ;; Set the global newsgroup variables here.
4668                 (gnus-set-global-variables)
4669                 (setq gnus-have-all-headers
4670                       (or all-headers gnus-show-all-headers))))
4671             (save-excursion
4672               (gnus-configure-windows 'article))
4673             (when (or (numberp article)
4674                       (stringp article))
4675               (gnus-article-prepare-display)
4676               ;; Do page break.
4677               (goto-char (point-min))
4678               (when gnus-break-pages
4679                 (gnus-narrow-to-page)))
4680             (let ((gnus-article-mime-handle-alist-1
4681                    gnus-article-mime-handle-alist))
4682               (gnus-set-mode-line 'article))
4683             (article-goto-body)
4684             (unless (bobp)
4685               (forward-line -1))
4686             (set-window-point (get-buffer-window (current-buffer)) (point))
4687             (gnus-configure-windows 'article)
4688             (gnus-run-hooks 'gnus-article-prepare-hook)
4689             t))))))
4690
4691 ;;;###autoload
4692 (defun gnus-article-prepare-display ()
4693   "Make the current buffer look like a nice article."
4694   ;; Hooks for getting information from the article.
4695   ;; This hook must be called before being narrowed.
4696   (let ((gnus-article-buffer (current-buffer))
4697         buffer-read-only
4698         (inhibit-read-only t))
4699     (unless (eq major-mode 'gnus-article-mode)
4700       (gnus-article-mode))
4701     (setq buffer-read-only nil
4702           gnus-article-wash-types nil
4703           gnus-article-image-alist nil)
4704     (gnus-run-hooks 'gnus-tmp-internal-hook)
4705     (when gnus-display-mime-function
4706       (funcall gnus-display-mime-function))))
4707
4708 ;;;
4709 ;;; Gnus Sticky Article Mode
4710 ;;;
4711
4712 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4713   "Mode for sticky articles."
4714   ;; Release bindings that won't work.
4715   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4716                              gnus-sticky-article-mode-map)
4717   (substitute-key-definition 'gnus-article-refer-article 'undefined
4718                              gnus-sticky-article-mode-map)
4719   (dolist (k '("e" "h" "s" "F" "R"))
4720     (define-key gnus-sticky-article-mode-map k nil))
4721   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4722   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4723   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4724   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4725
4726 (defun gnus-sticky-article (arg)
4727   "Make the current article sticky.
4728 If a prefix ARG is given, ask for a name for this sticky article buffer."
4729   (interactive "P")
4730   (gnus-summary-show-thread)
4731   (gnus-summary-select-article nil nil 'pseudo)
4732   (let (new-art-buf-name)
4733     (gnus-eval-in-buffer-window gnus-article-buffer
4734       (setq new-art-buf-name
4735             (concat
4736              "*Sticky Article: "
4737              (if arg
4738                  (read-from-minibuffer "Sticky article buffer name: ")
4739                (gnus-with-article-headers
4740                  (gnus-article-goto-header "subject")
4741                  (setq new-art-buf-name
4742                        (buffer-substring-no-properties
4743                         (line-beginning-position) (line-end-position)))
4744                  (goto-char (point-min))
4745                  (gnus-article-goto-header "from")
4746                  (setq new-art-buf-name
4747                        (concat
4748                         new-art-buf-name ", "
4749                         (buffer-substring-no-properties
4750                          (line-beginning-position) (line-end-position))))
4751                  (goto-char (point-min))
4752                  (gnus-article-goto-header "date")
4753                  (setq new-art-buf-name
4754                        (concat
4755                         new-art-buf-name ", "
4756                         (buffer-substring-no-properties
4757                          (line-beginning-position) (line-end-position))))))
4758              "*"))
4759       (if (and (gnus-buffer-live-p new-art-buf-name)
4760                (with-current-buffer new-art-buf-name
4761                  (eq major-mode 'gnus-sticky-article-mode)))
4762           (switch-to-buffer new-art-buf-name)
4763         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4764       (gnus-sticky-article-mode))
4765     (setq gnus-article-buffer new-art-buf-name))
4766   (gnus-summary-recenter)
4767   (gnus-summary-position-point))
4768
4769 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4770   "Kill the given sticky article BUFFER.
4771 If none is given, assume the current buffer and kill it if it has
4772 `gnus-sticky-article-mode'."
4773   (interactive)
4774   (unless buffer
4775     (setq buffer (current-buffer)))
4776   (with-current-buffer buffer
4777     (when (eq major-mode 'gnus-sticky-article-mode)
4778       (gnus-kill-buffer buffer))))
4779
4780 (defun gnus-kill-sticky-article-buffers (arg)
4781   "Kill all sticky article buffers.
4782 If a prefix ARG is given, ask for confirmation."
4783   (interactive "P")
4784   (dolist (buf (gnus-buffers))
4785     (with-current-buffer buf
4786       (when (eq major-mode 'gnus-sticky-article-mode)
4787        (if (not arg)
4788            (gnus-kill-buffer buf)
4789          (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4790            (gnus-kill-buffer buf)))))))
4791
4792 ;;;
4793 ;;; Gnus MIME viewing functions
4794 ;;;
4795
4796 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4797   "Format of the MIME buttons.
4798
4799 Valid specifiers include:
4800 %t  The MIME type
4801 %T  MIME type, along with additional info
4802 %n  The `name' parameter
4803 %d  The description, if any
4804 %l  The length of the encoded part
4805 %p  The part identifier number
4806 %e  Dots if the part isn't displayed
4807
4808 General format specifiers can also be used.  See Info node
4809 `(gnus)Formatting Variables'.")
4810
4811 (defvar gnus-mime-button-line-format-alist
4812   '((?t gnus-tmp-type ?s)
4813     (?T gnus-tmp-type-long ?s)
4814     (?n gnus-tmp-name ?s)
4815     (?d gnus-tmp-description ?s)
4816     (?p gnus-tmp-id ?s)
4817     (?l gnus-tmp-length ?d)
4818     (?e gnus-tmp-dots ?s)))
4819
4820 (defvar gnus-mime-button-commands
4821   '((gnus-article-press-button "\r" "Toggle Display")
4822     (gnus-mime-view-part "v" "View Interactively...")
4823     (gnus-mime-view-part-as-type "t" "View As Type...")
4824     (gnus-mime-view-part-as-charset "C" "View As charset...")
4825     (gnus-mime-save-part "o" "Save...")
4826     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4827     (gnus-mime-replace-part "r" "Replace part")
4828     (gnus-mime-delete-part "d" "Delete part")
4829     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4830     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4831     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4832     (gnus-mime-view-part-externally "e" "View Externally")
4833     (gnus-mime-print-part "p" "Print")
4834     (gnus-mime-pipe-part "|" "Pipe To Command...")
4835     (gnus-mime-action-on-part "." "Take action on the part...")))
4836
4837 (defun gnus-article-mime-part-status ()
4838   (if gnus-article-mime-handle-alist-1
4839       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4840           " (1 part)"
4841         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4842     ""))
4843
4844 (defvar gnus-mime-button-map
4845   (let ((map (make-sparse-keymap)))
4846     (define-key map gnus-mouse-2 'gnus-article-push-button)
4847     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4848     (dolist (c gnus-mime-button-commands)
4849       (define-key map (cadr c) (car c)))
4850     map))
4851
4852 (easy-menu-define
4853   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4854   `("MIME Part"
4855     ,@(mapcar (lambda (c)
4856                 (vector (caddr c) (car c) :active t))
4857               gnus-mime-button-commands)))
4858
4859 (defvar gnus-url-button-commands
4860   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4861
4862 (defvar gnus-url-button-map
4863   (let ((map (make-sparse-keymap)))
4864     (dolist (c gnus-url-button-commands)
4865       (define-key map (cadr c) (car c)))
4866     map))
4867
4868 (easy-menu-define
4869   gnus-url-button-menu gnus-url-button-map "URL button menu."
4870   `("Url Button"
4871     ,@(mapcar (lambda (c)
4872                 (vector (caddr c) (car c) :active t))
4873               gnus-url-button-commands)))
4874
4875 (defmacro gnus-bind-safe-url-regexp (&rest body)
4876   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4877   `(let ((mm-w3m-safe-url-regexp
4878           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4879                                 (gnus-buffer-live-p
4880                                  gnus-article-current-summary))
4881                            (with-current-buffer gnus-article-current-summary
4882                              gnus-newsgroup-name)
4883                          gnus-newsgroup-name)))
4884             (if (cond ((not group)
4885                        ;; Maybe we're in a mml-preview buffer
4886                        ;; and no group is selected.
4887                        t)
4888                       ((stringp gnus-safe-html-newsgroups)
4889                        (string-match gnus-safe-html-newsgroups group))
4890                       ((consp gnus-safe-html-newsgroups)
4891                        (member group gnus-safe-html-newsgroups)))
4892                 nil
4893               mm-w3m-safe-url-regexp))))
4894      ,@body))
4895
4896 (defun gnus-mime-button-menu (event prefix)
4897  "Construct a context-sensitive menu of MIME commands."
4898  (interactive "e\nP")
4899  (save-window-excursion
4900    (let ((pos (event-start event)))
4901      (select-window (posn-window pos))
4902      (goto-char (posn-point pos))
4903      (gnus-article-check-buffer)
4904      (popup-menu gnus-mime-button-menu nil prefix))))
4905
4906 (defun gnus-mime-view-all-parts (&optional handles)
4907   "View all the MIME parts."
4908   (interactive)
4909   (with-current-buffer gnus-article-buffer
4910     (let ((handles (or handles gnus-article-mime-handles))
4911           (mail-parse-charset gnus-newsgroup-charset)
4912           (mail-parse-ignored-charsets
4913            (with-current-buffer gnus-summary-buffer
4914              gnus-newsgroup-ignored-charsets)))
4915       (when handles
4916         (mm-remove-parts handles)
4917         (goto-char (point-min))
4918         (or (search-forward "\n\n") (goto-char (point-max)))
4919         (let ((inhibit-read-only t))
4920           (delete-region (point) (point-max))
4921           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4922
4923 (defun gnus-article-jump-to-part (n)
4924   "Jump to MIME part N."
4925   (interactive "P")
4926   (let ((parts (with-current-buffer gnus-article-buffer
4927                  (length gnus-article-mime-handle-alist))))
4928     (when (zerop parts)
4929       (error "No such part"))
4930     (pop-to-buffer gnus-article-buffer)
4931     (or n
4932         (setq n (if (= parts 1)
4933                     1
4934                   (read-number (format "Jump to part (1..%s): " parts)))))
4935     (unless (and (integerp n) (<= n parts) (>= n 1))
4936       (setq n
4937             (progn
4938               (gnus-message 7 "Invalid part `%s', using %s instead."
4939                             n parts)
4940               parts)))
4941     (gnus-message 9 "Jumping to part %s." n)
4942     (cond ((>= gnus-auto-select-part 1)
4943            (while (and (<= n parts)
4944                        (not (gnus-article-goto-part n)))
4945              (setq n (1+ n))))
4946           ((< gnus-auto-select-part 0)
4947            (while (and (>= n 1)
4948                        (not (gnus-article-goto-part n)))
4949              (setq n (1- n))))
4950           (t
4951            (gnus-article-goto-part n)))))
4952
4953 (defvar gnus-mime-buttonized-part-id nil
4954   "ID of a mime part that should be buttonized.
4955 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4956
4957 (eval-when-compile
4958   (defsubst gnus-article-edit-part (handles &optional current-id)
4959     "Edit an article in order to delete a mime part.
4960 This function is exclusively used by `gnus-mime-save-part-and-strip'
4961 and `gnus-mime-delete-part', and not provided at run-time normally."
4962     (gnus-article-edit-article
4963      `(lambda ()
4964         (buffer-disable-undo)
4965         (erase-buffer)
4966         (let ((mail-parse-charset (or gnus-article-charset
4967                                       ',gnus-newsgroup-charset))
4968               (mail-parse-ignored-charsets
4969                (or gnus-article-ignored-charsets
4970                    ',gnus-newsgroup-ignored-charsets))
4971               (mbl mml-buffer-list))
4972           (setq mml-buffer-list nil)
4973           (insert-buffer-substring gnus-original-article-buffer)
4974           (mime-to-mml ',handles)
4975           (setq gnus-article-mime-handles nil)
4976           (let ((mbl1 mml-buffer-list))
4977             (setq mml-buffer-list mbl)
4978             (set (make-local-variable 'mml-buffer-list) mbl1))
4979           (gnus-make-local-hook 'kill-buffer-hook)
4980           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4981      `(lambda (no-highlight)
4982         (let ((mail-parse-charset (or gnus-article-charset
4983                                       ',gnus-newsgroup-charset))
4984               (message-options message-options)
4985               (message-options-set-recipient)
4986               (mail-parse-ignored-charsets
4987                (or gnus-article-ignored-charsets
4988                    ',gnus-newsgroup-ignored-charsets)))
4989           (mml-to-mime)
4990           (mml-destroy-buffers)
4991           (remove-hook 'kill-buffer-hook
4992                        'mml-destroy-buffers t)
4993           (kill-local-variable 'mml-buffer-list))
4994         (gnus-summary-edit-article-done
4995          ,(or (mail-header-references gnus-current-headers) "")
4996          ,(gnus-group-read-only-p)
4997          ,gnus-summary-buffer no-highlight))
4998      t)
4999     ;; Force buttonizing this part.
5000     (let ((gnus-mime-buttonized-part-id current-id))
5001       (gnus-article-edit-done))
5002     (gnus-configure-windows 'article)
5003     (when (and current-id (integerp gnus-auto-select-part))
5004       (gnus-article-jump-to-part
5005        (min (max (+ current-id gnus-auto-select-part) 1)
5006             (with-current-buffer gnus-article-buffer
5007               (length gnus-article-mime-handle-alist)))))))
5008
5009 (defun gnus-mime-replace-part (file)
5010   "Replace MIME part under point with an external body."
5011   ;; Useful if file has already been saved to disk
5012   (interactive
5013    (list
5014     (read-file-name "Replace MIME part with file: "
5015                     (or mm-default-directory default-directory)
5016                     nil nil)))
5017   (gnus-mime-save-part-and-strip file))
5018
5019 (defun gnus-mime-save-part-and-strip (&optional file)
5020   "Save the MIME part under point then replace it with an external body.
5021 If FILE is given, use it for the external part."
5022   (interactive)
5023   (gnus-article-check-buffer)
5024   (when (gnus-group-read-only-p)
5025     (error "The current group does not support deleting of parts"))
5026   (when (mm-complicated-handles gnus-article-mime-handles)
5027     (error "\
5028 The current article has a complicated MIME structure, giving up..."))
5029   (let* ((data (get-text-property (point) 'gnus-data))
5030          (id (get-text-property (point) 'gnus-part))
5031          param
5032          (handles gnus-article-mime-handles))
5033     (unless file
5034       (setq file
5035             (and data (mm-save-part data "Delete MIME part and save to: "))))
5036     (when file
5037       (with-current-buffer (mm-handle-buffer data)
5038         (erase-buffer)
5039         (insert "Content-Type: " (mm-handle-media-type data))
5040         (mml-insert-parameter-string (cdr (mm-handle-type data))
5041                                      '(charset))
5042         ;; Add a filename for the sake of saving the part again.
5043         (mml-insert-parameter
5044          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5045         (insert "\n")
5046         (insert "Content-ID: " (message-make-message-id) "\n")
5047         (insert "Content-Transfer-Encoding: binary\n")
5048         (insert "\n"))
5049       (setcdr data
5050               (cdr (mm-make-handle nil
5051                                    `("message/external-body"
5052                                      (access-type . "LOCAL-FILE")
5053                                      (name . ,file)))))
5054       ;; (set-buffer gnus-summary-buffer)
5055       (gnus-article-edit-part handles id))))
5056
5057 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5058 ;; parts...>') but with stripping would be nice.
5059
5060 (defun gnus-mime-delete-part ()
5061   "Delete the MIME part under point.
5062 Replace it with some information about the removed part."
5063   (interactive)
5064   (gnus-article-check-buffer)
5065   (when (gnus-group-read-only-p)
5066     (error "The current group does not support deleting of parts"))
5067   (when (mm-complicated-handles gnus-article-mime-handles)
5068     (error "\
5069 The current article has a complicated MIME structure, giving up..."))
5070   (when (or gnus-expert-user
5071             (gnus-yes-or-no-p "\
5072 Deleting parts may malfunction or destroy the article; continue? "))
5073     (let* ((data (get-text-property (point) 'gnus-data))
5074            (id (get-text-property (point) 'gnus-part))
5075            (handles gnus-article-mime-handles)
5076            (description
5077             (let ((desc (mm-handle-description data)))
5078               (when desc
5079                 (mail-decode-encoded-word-string desc))))
5080            (filename (or (mm-handle-filename data) "(none)"))
5081            (type (mm-handle-media-type data)))
5082       (unless data
5083         (error "No MIME part under point"))
5084       (with-current-buffer (mm-handle-buffer data)
5085         (let ((bsize (buffer-size)))
5086           (erase-buffer)
5087           (insert
5088            (concat
5089             ",----\n"
5090             "| The following attachment has been deleted:\n"
5091             "|\n"
5092             "| Type:           " type "\n"
5093             "| Filename:       " filename "\n"
5094             "| Size (encoded): " (format "%s byte%s\n"
5095                                          bsize (if (= bsize 1)
5096                                                    ""
5097                                                  "s"))
5098             (when description
5099               (concat    "| Description:    " description "\n"))
5100             "`----\n"))
5101           (setcdr data
5102                   (cdr (mm-make-handle
5103                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5104                         (list "attachment")
5105                         (format "Deleted attachment (%s bytes)" bsize))))))
5106       ;; (set-buffer gnus-summary-buffer)
5107       (gnus-article-edit-part handles id))))
5108
5109 (defun gnus-mime-save-part ()
5110   "Save the MIME part under point."
5111   (interactive)
5112   (gnus-article-check-buffer)
5113   (let ((data (get-text-property (point) 'gnus-data)))
5114     (when data
5115       (mm-save-part data))))
5116
5117 (defun gnus-mime-pipe-part (&optional cmd)
5118   "Pipe the MIME part under point to a process.
5119 Use CMD as the process."
5120   (interactive)
5121   (gnus-article-check-buffer)
5122   (let ((data (get-text-property (point) 'gnus-data)))
5123     (when data
5124       (mm-pipe-part data cmd))))
5125
5126 (defun gnus-mime-view-part ()
5127   "Interactively choose a viewing method for the MIME part under point."
5128   (interactive)
5129   (gnus-article-check-buffer)
5130   (let ((data (get-text-property (point) 'gnus-data)))
5131     (when data
5132       (setq gnus-article-mime-handles
5133             (mm-merge-handles
5134              gnus-article-mime-handles (setq data (copy-sequence data))))
5135       (mm-interactively-view-part data))))
5136
5137 (defun gnus-mime-view-part-as-type-internal ()
5138   (gnus-article-check-buffer)
5139   (let* ((handle (get-text-property (point) 'gnus-data))
5140          (name (or
5141                 ;; Content-Type: foo/bar; name=...
5142                 (mail-content-type-get (mm-handle-type handle) 'name)
5143                 ;; Content-Disposition: attachment; filename=...
5144                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5145          (def-type (and name (mm-default-file-encoding name))))
5146     (or (and def-type (cons def-type 0))
5147         (and handle
5148              (equal (mm-handle-media-supertype handle) "text")
5149              '("text/plain" . 0))
5150         '("application/octet-stream" . 0))))
5151
5152 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5153   "Choose a MIME media type, and view the part as such.
5154 If non-nil, PRED is a predicate to use during completion to limit the
5155 available media-types."
5156   (interactive)
5157   (unless mime-type
5158     (setq mime-type
5159           (let ((default (gnus-mime-view-part-as-type-internal)))
5160             (gnus-completing-read
5161              "View as MIME type"
5162              (if pred
5163                  (gnus-remove-if-not pred (mailcap-mime-types))
5164                (mailcap-mime-types))
5165              nil nil nil
5166              (car default)))))
5167   (gnus-article-check-buffer)
5168   (let ((handle (get-text-property (point) 'gnus-data)))
5169     (when handle
5170       (when (equal (mm-handle-media-type handle) "message/external-body")
5171         (unless (mm-handle-cache handle)
5172           (mm-extern-cache-contents handle))
5173         (setq handle (mm-handle-cache handle)))
5174       (setq handle
5175             (mm-make-handle (mm-handle-buffer handle)
5176                             (cons mime-type (cdr (mm-handle-type handle)))
5177                             (mm-handle-encoding handle)
5178                             (mm-handle-undisplayer handle)
5179                             (mm-handle-disposition handle)
5180                             (mm-handle-description handle)
5181                             nil
5182                             (mm-handle-id handle)))
5183       (setq gnus-article-mime-handles
5184             (mm-merge-handles gnus-article-mime-handles handle))
5185       (when (mm-handle-displayed-p handle)
5186         (mm-remove-part handle))
5187       (gnus-mm-display-part handle))))
5188
5189 (defun gnus-mime-copy-part (&optional handle arg)
5190   "Put the MIME part under point into a new buffer.
5191 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5192 are decompressed."
5193   (interactive (list nil current-prefix-arg))
5194   (gnus-article-check-buffer)
5195   (unless handle
5196     (setq handle (get-text-property (point) 'gnus-data)))
5197   (when handle
5198     (let ((filename (mm-handle-filename handle))
5199           contents dont-decode charset coding-system)
5200       (mm-with-unibyte-buffer
5201         (mm-insert-part handle)
5202         (setq contents (or (condition-case nil
5203                                (mm-decompress-buffer filename nil 'sig)
5204                              (error
5205                               (setq dont-decode t)
5206                               nil))
5207                            (buffer-string))))
5208       (setq filename (cond (filename (file-name-nondirectory filename))
5209                            (dont-decode "*raw data*")
5210                            (t "*decoded*")))
5211       (cond
5212        (dont-decode)
5213        ((not arg)
5214         (unless (setq charset (mail-content-type-get
5215                                (mm-handle-type handle) 'charset))
5216           (unless (setq coding-system (mm-with-unibyte-buffer
5217                                         (insert contents)
5218                                         (mm-find-buffer-file-coding-system)))
5219             (setq charset gnus-newsgroup-charset))))
5220        ((numberp arg)
5221         (setq charset (or (cdr (assq arg
5222                                      gnus-summary-show-article-charset-alist))
5223                           (mm-read-coding-system "Charset: ")))))
5224       (switch-to-buffer (generate-new-buffer filename))
5225       (if (or coding-system
5226               (and charset
5227                    (setq coding-system (mm-charset-to-coding-system charset))
5228                    (not (eq coding-system 'ascii))))
5229           (progn
5230             (mm-enable-multibyte)
5231             (insert (mm-decode-coding-string contents coding-system))
5232             (setq buffer-file-coding-system
5233                   (if (boundp 'last-coding-system-used)
5234                       (symbol-value 'last-coding-system-used)
5235                     coding-system)))
5236         (mm-disable-multibyte)
5237         (insert contents)
5238         (setq buffer-file-coding-system mm-binary-coding-system))
5239       ;; We do it this way to make `normal-mode' set the appropriate mode.
5240       (unwind-protect
5241           (progn
5242             (setq buffer-file-name (expand-file-name filename))
5243             (normal-mode))
5244         (setq buffer-file-name nil))
5245       (goto-char (point-min)))))
5246
5247 (defun gnus-mime-print-part (&optional handle filename)
5248   "Print the MIME part under point."
5249   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5250   (gnus-article-check-buffer)
5251   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5252          (contents (and handle (mm-get-part handle)))
5253          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5254          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5255     (when contents
5256         (if printer
5257             (unwind-protect
5258                 (progn
5259                   (mm-save-part-to-file handle file)
5260                   (call-process shell-file-name nil
5261                                 (generate-new-buffer " *mm*")
5262                                 nil
5263                                 shell-command-switch
5264                                 (mm-mailcap-command
5265                                  printer file (mm-handle-type handle))))
5266               (delete-file file))
5267           (with-temp-buffer
5268             (insert contents)
5269             (gnus-print-buffer))
5270           (ps-despool filename)))))
5271
5272 (defun gnus-mime-inline-part (&optional handle arg)
5273   "Insert the MIME part under point into the current buffer.
5274 Compressed files like .gz and .bz2 are decompressed."
5275   (interactive (list nil current-prefix-arg))
5276   (gnus-article-check-buffer)
5277   (unless handle
5278     (setq handle (get-text-property (point) 'gnus-data)))
5279   (when handle
5280     (let ((b (point))
5281           (inhibit-read-only t)
5282           contents charset coding-system)
5283       (if (and (not arg) (mm-handle-undisplayer handle))
5284           (mm-remove-part handle)
5285         (mm-with-unibyte-buffer
5286           (mm-insert-part handle)
5287           (setq contents
5288                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5289                     (buffer-string))))
5290         (cond
5291          ((not arg)
5292           (unless (setq charset (mail-content-type-get
5293                                  (mm-handle-type handle) 'charset))
5294             (unless (setq coding-system
5295                           (mm-with-unibyte-buffer
5296                             (insert contents)
5297                             (mm-find-buffer-file-coding-system)))
5298               (setq charset gnus-newsgroup-charset))))
5299          ((numberp arg)
5300           (if (mm-handle-undisplayer handle)
5301               (mm-remove-part handle))
5302           (setq charset
5303                 (or (cdr (assq arg
5304                                gnus-summary-show-article-charset-alist))
5305                     (mm-read-coding-system "Charset: "))))
5306          ((mm-handle-undisplayer handle)
5307           (mm-remove-part handle)))
5308         (forward-line 2)
5309         (mm-display-inline handle)
5310         (goto-char b)))))
5311
5312 (defun gnus-mime-set-charset-parameters (handle charset)
5313   "Set CHARSET to parameters in HANDLE.
5314 CHARSET may either be a string or a symbol."
5315   (unless (stringp charset)
5316     (setq charset (symbol-name charset)))
5317   (if (stringp (car handle))
5318       (dolist (h (cdr handle))
5319         (gnus-mime-set-charset-parameters h charset))
5320     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5321                                             "message/external-body")
5322                                      (progn
5323                                        (unless (mm-handle-cache handle)
5324                                          (mm-extern-cache-contents handle))
5325                                        (mm-handle-cache handle))
5326                                    handle)))
5327            (param (assq 'charset (cdr type))))
5328       (if param
5329           (setcdr param charset)
5330         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5331
5332 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5333   "Insert the MIME part under point into the current buffer using the
5334 specified charset."
5335   (interactive (list nil current-prefix-arg))
5336   (gnus-article-check-buffer)
5337   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5338         (fun (get-text-property (point) 'gnus-callback))
5339         (gnus-newsgroup-ignored-charsets 'gnus-all)
5340         charset form preferred parts)
5341     (when handle
5342       (when (prog1
5343                 (and fun
5344                      (setq charset
5345                            (or (cdr (assq
5346                                      arg
5347                                      gnus-summary-show-article-charset-alist))
5348                                (mm-read-coding-system "Charset: "))))
5349               (if (mm-handle-undisplayer handle)
5350                   (mm-remove-part handle)))
5351         (gnus-mime-set-charset-parameters handle charset)
5352         (when (and (consp (setq form (cdr-safe fun)))
5353                    (setq form (ignore-errors
5354                                 (assq 'gnus-mime-display-alternative form)))
5355                    (setq preferred (caddr form))
5356                    (progn
5357                      (when (eq (car preferred) 'quote)
5358                        (setq preferred (cadr preferred)))
5359                      (not (equal preferred
5360                                  (get-text-property (point) 'gnus-data))))
5361                    (setq parts (get-text-property (point) 'gnus-part))
5362                    (setq parts (cdr (assq parts
5363                                           gnus-article-mime-handle-alist)))
5364                    (equal (mm-handle-media-type parts) "multipart/alternative")
5365                    (setq parts (reverse (cdr parts))))
5366           (setcar (cddr form)
5367                   (list 'quote (or (cadr (member preferred parts))
5368                                    (car parts)))))
5369         (funcall fun handle)))))
5370
5371 (defun gnus-mime-view-part-externally (&optional handle)
5372   "View the MIME part under point with an external viewer."
5373   (interactive)
5374   (gnus-article-check-buffer)
5375   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5376          (mm-user-display-methods nil)
5377          (mm-inlined-types nil)
5378          (mail-parse-charset gnus-newsgroup-charset)
5379          (mail-parse-ignored-charsets
5380           (with-current-buffer gnus-summary-buffer
5381             gnus-newsgroup-ignored-charsets))
5382          (type (mm-handle-media-type handle))
5383          (method (mailcap-mime-info type))
5384          (mm-enable-external t))
5385     (if (not (stringp method))
5386         (gnus-mime-view-part-as-type
5387          nil (lambda (type) (stringp (mailcap-mime-info type))))
5388       (when handle
5389         (mm-display-part handle nil t)))))
5390
5391 (defun gnus-mime-view-part-internally (&optional handle)
5392   "View the MIME part under point with an internal viewer.
5393 If no internal viewer is available, use an external viewer."
5394   (interactive)
5395   (gnus-article-check-buffer)
5396   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5397          (mm-inlined-types '(".*"))
5398          (mm-inline-large-images t)
5399          (mail-parse-charset gnus-newsgroup-charset)
5400          (mail-parse-ignored-charsets
5401           (with-current-buffer gnus-summary-buffer
5402             gnus-newsgroup-ignored-charsets))
5403          (inhibit-read-only t))
5404     (if (not (mm-inlinable-p handle))
5405         (gnus-mime-view-part-as-type
5406          nil (lambda (type) (mm-inlinable-p handle type)))
5407       (when handle
5408         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5409
5410 (defun gnus-mime-action-on-part (&optional action)
5411   "Do something with the MIME attachment at \(point\)."
5412   (interactive
5413    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5414   (gnus-article-check-buffer)
5415   (let ((action-pair (assoc action gnus-mime-action-alist)))
5416     (if action-pair
5417         (funcall (cdr action-pair)))))
5418
5419 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5420   "Call FUNCTION on MIME part N.
5421 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5422 If INTERACTIVE, call FUNCTION interactively."
5423   (let (window frame)
5424     ;; Check whether the article is displayed.
5425     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5426                  (setq window (get-buffer-window gnus-article-buffer t))
5427                  (frame-visible-p (setq frame (window-frame window))))
5428       (error "No article is displayed"))
5429     (with-current-buffer gnus-article-buffer
5430       ;; Check whether the article displays the right contents.
5431       (unless (with-current-buffer gnus-summary-buffer
5432                 (eq gnus-current-article (gnus-summary-article-number)))
5433         (error "You should select the right article first"))
5434       (if n
5435           (setq n (prefix-numeric-value n))
5436         (let ((pt (point)))
5437           (setq n (or (get-text-property pt 'gnus-part)
5438                       (and (not (bobp))
5439                            (get-text-property (1- pt) 'gnus-part))
5440                       (get-text-property (prog2
5441                                              (forward-line 1)
5442                                              (point)
5443                                            (goto-char pt))
5444                                          'gnus-part)
5445                       (get-text-property
5446                        (or (and (setq pt (previous-single-property-change
5447                                           pt 'gnus-part))
5448                                 (1- pt))
5449                            (next-single-property-change (point) 'gnus-part)
5450                            (point))
5451                        'gnus-part)
5452                       1))))
5453       ;; Check whether the specified part exists.
5454       (when (> n (length gnus-article-mime-handle-alist))
5455         (error "No such part")))
5456     (unless
5457         (progn
5458           ;; To select the window is needed so that the cursor
5459           ;; might be visible on the MIME button.
5460           (select-window (prog1
5461                              window
5462                            (setq window (selected-window))
5463                            ;; Article may be displayed in the other frame.
5464                            (gnus-select-frame-set-input-focus
5465                             (prog1
5466                                 frame
5467                               (setq frame (selected-frame))))))
5468           (when (gnus-article-goto-part n)
5469             ;; We point the cursor and the arrow at the MIME button
5470             ;; when the `function' prompt the user for something.
5471             (unless (and (pos-visible-in-window-p)
5472                          (> (count-lines (point) (window-end))
5473                             (/ (1- (window-height)) 3)))
5474               (recenter (/ (1- (window-height)) 3)))
5475             (let ((cursor-in-non-selected-windows t)
5476                   (overlay-arrow-string "=>")
5477                   (overlay-arrow-position (point-marker)))
5478               (unwind-protect
5479                   (cond
5480                    ((and no-handle interactive)
5481                     (call-interactively function))
5482                    (no-handle
5483                     (funcall function))
5484                    (interactive
5485                     (call-interactively
5486                      function (get-text-property (point) 'gnus-data)))
5487                    (t
5488                     (funcall function
5489                              (get-text-property (point) 'gnus-data))))
5490                 (set-marker overlay-arrow-position nil)
5491                 (unless gnus-auto-select-part
5492                   (gnus-select-frame-set-input-focus frame)
5493                   (select-window window))))
5494             t))
5495       (if gnus-inhibit-mime-unbuttonizing
5496           ;; This is the default though the program shouldn't reach here.
5497           (error "No such part")
5498         ;; The part which doesn't have the MIME button is selected.
5499         ;; So, we display all the buttons and redo it.
5500         (let ((gnus-inhibit-mime-unbuttonizing t))
5501           (gnus-summary-show-article)
5502           (gnus-article-part-wrapper n function no-handle))))))
5503
5504 (defun gnus-article-pipe-part (n)
5505   "Pipe MIME part N, which is the numerical prefix."
5506   (interactive "P")
5507   (gnus-article-part-wrapper n 'mm-pipe-part))
5508
5509 (defun gnus-article-save-part (n)
5510   "Save MIME part N, which is the numerical prefix."
5511   (interactive "P")
5512   (gnus-article-part-wrapper n 'mm-save-part))
5513
5514 (defun gnus-article-interactively-view-part (n)
5515   "View MIME part N interactively, which is the numerical prefix."
5516   (interactive "P")
5517   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5518
5519 (defun gnus-article-copy-part (n)
5520   "Copy MIME part N, which is the numerical prefix."
5521   (interactive "P")
5522   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5523
5524 (defun gnus-article-view-part-as-charset (n)
5525   "View MIME part N using a specified charset.
5526 N is the numerical prefix."
5527   (interactive "P")
5528   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5529
5530 (defun gnus-article-view-part-externally (n)
5531   "View MIME part N externally, which is the numerical prefix."
5532   (interactive "P")
5533   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5534
5535 (defun gnus-article-inline-part (n)
5536   "Inline MIME part N, which is the numerical prefix."
5537   (interactive "P")
5538   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5539
5540 (defun gnus-article-save-part-and-strip (n)
5541   "Save MIME part N and replace it with an external body.
5542 N is the numerical prefix."
5543   (interactive "P")
5544   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5545
5546 (defun gnus-article-replace-part (n)
5547   "Replace MIME part N with an external body.
5548 N is the numerical prefix."
5549   (interactive "P")
5550   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5551
5552 (defun gnus-article-delete-part (n)
5553   "Delete MIME part N and add some information about the removed part.
5554 N is the numerical prefix."
5555   (interactive "P")
5556   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5557
5558 (defun gnus-article-view-part-as-type (n)
5559   "Choose a MIME media type, and view part N as such.
5560 N is the numerical prefix."
5561   (interactive "P")
5562   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5563
5564 (defun gnus-article-mime-match-handle-first (condition)
5565   (if condition
5566       (let (n)
5567         (dolist (ihandle gnus-article-mime-handle-alist)
5568           (if (and (cond
5569                     ((functionp condition)
5570                      (funcall condition (cdr ihandle)))
5571                     ((eq condition 'undisplayed)
5572                      (not (or (mm-handle-undisplayer (cdr ihandle))
5573                               (equal (mm-handle-media-type (cdr ihandle))
5574                                      "multipart/alternative"))))
5575                     ((eq condition 'undisplayed-alternative)
5576                      (not (mm-handle-undisplayer (cdr ihandle))))
5577                     (t t))
5578                    (gnus-article-goto-part (car ihandle))
5579                    (or (not n) (< (car ihandle) n)))
5580               (setq n (car ihandle))))
5581         (or n 1))
5582     1))
5583
5584 (defun gnus-article-view-part (&optional n)
5585   "View MIME part N, which is the numerical prefix.
5586 If the part is already shown, hide the part.  If N is nil, view
5587 all parts."
5588   (interactive "P")
5589   (with-current-buffer gnus-article-buffer
5590     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5591                              gnus-article-mime-match-handle-function)))
5592     (when (> n (length gnus-article-mime-handle-alist))
5593       (error "No such part"))
5594     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5595       (when (gnus-article-goto-part n)
5596         (if (equal (car handle) "multipart/alternative")
5597             (progn
5598               (beginning-of-line) ;; Make it toggle subparts
5599               (gnus-article-press-button))
5600           (when (eq (gnus-mm-display-part handle) 'internal)
5601             (gnus-set-window-start)))))))
5602
5603 (defsubst gnus-article-mime-total-parts ()
5604   (if (bufferp (car gnus-article-mime-handles))
5605       1 ;; single part
5606     (1- (length gnus-article-mime-handles))))
5607
5608 (defun gnus-mm-display-part (handle)
5609   "Display HANDLE and fix MIME button."
5610   (let ((id (get-text-property (point) 'gnus-part))
5611         (point (point))
5612         (inhibit-read-only t))
5613     (forward-line 1)
5614     (prog1
5615         (let ((window (selected-window))
5616               (mail-parse-charset gnus-newsgroup-charset)
5617               (mail-parse-ignored-charsets
5618                (if (gnus-buffer-live-p gnus-summary-buffer)
5619                    (with-current-buffer gnus-summary-buffer
5620                      gnus-newsgroup-ignored-charsets)
5621                  nil)))
5622           (save-excursion
5623             (unwind-protect
5624                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5625                       (beg (point)))
5626                   (when win
5627                     (select-window win))
5628                   (goto-char point)
5629                   (forward-line)
5630                   (if (mm-handle-displayed-p handle)
5631                       ;; This will remove the part.
5632                       (mm-display-part handle)
5633                     (save-restriction
5634                       (narrow-to-region (point)
5635                                         (if (eobp) (point) (1+ (point))))
5636                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5637                       ;; We narrow to the part itself and
5638                       ;; then call the treatment functions.
5639                       (goto-char (point-min))
5640                       (forward-line 1)
5641                       (narrow-to-region (point) (point-max))
5642                       (gnus-treat-article
5643                        nil id
5644                        (gnus-article-mime-total-parts)
5645                        (mm-handle-media-type handle)))))
5646               (if (window-live-p window)
5647                   (select-window window)))))
5648       (goto-char point)
5649       (gnus-delete-line)
5650       (gnus-insert-mime-button
5651        handle id (list (mm-handle-displayed-p handle)))
5652       (goto-char point))))
5653
5654 (defun gnus-article-goto-part (n)
5655   "Go to MIME part N."
5656   (when gnus-break-pages
5657     (widen))
5658   (prog1
5659       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5660             part handle end next handles)
5661         (when start
5662           (goto-char start)
5663           (if (setq handle (get-text-property start 'gnus-data))
5664               start
5665             ;; Go to the displayed subpart, assuming this is
5666             ;; multipart/alternative.
5667             (setq part start
5668                   end (point-at-eol))
5669             (while (and (not handle)
5670                         part
5671                         (< part end)
5672                         (setq next (text-property-not-all part end
5673                                                           'gnus-data nil)))
5674               (setq part next
5675                     handle (get-text-property part 'gnus-data))
5676               (push (cons handle part) handles)
5677               (unless (mm-handle-displayed-p handle)
5678                 (setq handle nil
5679                       part (text-property-any part end 'gnus-data nil))))
5680             (unless handle
5681               ;; No subpart is displayed, so we find preferred one.
5682               (setq part
5683                     (cdr (assq (mm-preferred-alternative
5684                                 (nreverse (mapcar 'car handles)))
5685                                handles))))
5686             (if part
5687                 (goto-char (1+ part))
5688               start))))
5689     (when gnus-break-pages
5690       (gnus-narrow-to-page))))
5691
5692 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5693   (let ((gnus-tmp-name
5694          (or (mm-handle-filename handle)
5695              (mail-content-type-get (mm-handle-type handle) 'url)
5696              ""))
5697         (gnus-tmp-type (mm-handle-media-type handle))
5698         (gnus-tmp-description (or (mm-handle-description handle) ""))
5699         (gnus-tmp-dots
5700          (if (if displayed (car displayed)
5701                (mm-handle-displayed-p handle))
5702              "" "..."))
5703         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5704                            (buffer-size)))
5705         gnus-tmp-type-long b e)
5706     (when (string-match ".*/" gnus-tmp-name)
5707       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5708     (setq gnus-tmp-type-long (concat gnus-tmp-type
5709                                      (and (not (equal gnus-tmp-name ""))
5710                                           (concat "; " gnus-tmp-name))))
5711     (unless (equal gnus-tmp-description "")
5712       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5713     (unless (bolp)
5714       (insert "\n"))
5715     (setq b (point))
5716     (gnus-eval-format
5717      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5718      `(keymap ,gnus-mime-button-map
5719               gnus-callback gnus-mm-display-part
5720               gnus-part ,gnus-tmp-id
5721               article-type annotation
5722               gnus-data ,handle
5723               rear-nonsticky t))
5724     (setq e (if (bolp)
5725                 ;; Exclude a newline.
5726                 (1- (point))
5727               (point)))
5728     (when gnus-article-button-face
5729       (gnus-overlay-put (gnus-make-overlay b e nil t)
5730                         'face gnus-article-button-face))
5731     (widget-convert-button
5732      'link b e
5733      :mime-handle handle
5734      :action 'gnus-widget-press-button
5735      :button-keymap gnus-mime-button-map
5736      :help-echo
5737      (lambda (widget)
5738        ;; Needed to properly clear the message due to a bug in
5739        ;; wid-edit (XEmacs only).
5740        (if (boundp 'help-echo-owns-message)
5741            (setq help-echo-owns-message t))
5742        (format
5743         "%S: %s the MIME part; %S: more options"
5744         (aref gnus-mouse-2 0)
5745         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5746             "hide" "show")
5747         (aref gnus-down-mouse-3 0))))))
5748
5749 (defun gnus-widget-press-button (elems el)
5750   (goto-char (widget-get elems :from))
5751   (gnus-article-press-button))
5752
5753 (defvar gnus-displaying-mime nil)
5754
5755 (defun gnus-display-mime (&optional ihandles)
5756   "Display the MIME parts."
5757   (save-excursion
5758     (save-selected-window
5759       (let ((window (get-buffer-window gnus-article-buffer))
5760             (point (point)))
5761         (when window
5762           (select-window window)
5763           ;; We have to do this since selecting the window
5764           ;; may change the point.  So we set the window point.
5765           (set-window-point window point)))
5766       (let ((handles ihandles)
5767             (inhibit-read-only t)
5768             handle)
5769         (cond (handles)
5770               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5771                (when gnus-article-emulate-mime
5772                  (mm-uu-dissect-text-parts handles)))
5773               (gnus-article-emulate-mime
5774                (setq handles (mm-uu-dissect))))
5775         (when (and (not ihandles)
5776                    (not gnus-displaying-mime))
5777           ;; Top-level call; we clean up.
5778           (when gnus-article-mime-handles
5779             (mm-destroy-parts gnus-article-mime-handles)
5780             (setq gnus-article-mime-handle-alist nil));; A trick.
5781           (setq gnus-article-mime-handles handles)
5782           ;; We allow users to glean info from the handles.
5783           (when gnus-article-mime-part-function
5784             (gnus-mime-part-function handles)))
5785         (if (and handles
5786                  (or (not (stringp (car handles)))
5787                      (cdr handles)))
5788             (progn
5789               (when (and (not ihandles)
5790                          (not gnus-displaying-mime))
5791                 ;; Clean up for mime parts.
5792                 (article-goto-body)
5793                 (delete-region (point) (point-max)))
5794               (let ((gnus-displaying-mime t))
5795                 (gnus-mime-display-part handles)))
5796           (save-restriction
5797             (article-goto-body)
5798             (narrow-to-region (point) (point-max))
5799             (gnus-treat-article nil 1 1 "text/plain")
5800             (widen)))
5801         (unless ihandles
5802           ;; Highlight the headers.
5803           (save-excursion
5804             (save-restriction
5805               (article-goto-body)
5806               (narrow-to-region (point-min) (point))
5807               (gnus-article-save-original-date
5808                (gnus-treat-article 'head)))))))
5809     ;; Cope with broken MIME messages.
5810     (goto-char (point-max))
5811     (unless (bolp)
5812       (insert "\n"))))
5813
5814 (defcustom gnus-mime-display-multipart-as-mixed nil
5815   "Display \"multipart\" parts as  \"multipart/mixed\".
5816
5817 If t, it overrides nil values of
5818 `gnus-mime-display-multipart-alternative-as-mixed' and
5819 `gnus-mime-display-multipart-related-as-mixed'."
5820   :group 'gnus-article-mime
5821   :type 'boolean)
5822
5823 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5824   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5825   :version "22.1"
5826   :group 'gnus-article-mime
5827   :type 'boolean)
5828
5829 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5830   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5831
5832 If displaying \"text/html\" is discouraged \(see
5833 `mm-discouraged-alternatives'\) images or other material inside a
5834 \"multipart/related\" part might be overlooked when this variable is nil."
5835   :version "22.1"
5836   :group 'gnus-article-mime
5837   :type 'boolean)
5838
5839 (defun gnus-mime-display-part (handle)
5840   (cond
5841    ;; Maybe a broken MIME message.
5842    ((null handle))
5843    ;; Single part.
5844    ((not (stringp (car handle)))
5845     (gnus-mime-display-single handle))
5846    ;; User-defined multipart
5847    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5848     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5849              handle))
5850    ;; multipart/alternative
5851    ((and (equal (car handle) "multipart/alternative")
5852          (not (or gnus-mime-display-multipart-as-mixed
5853                   gnus-mime-display-multipart-alternative-as-mixed)))
5854     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5855       (push (cons id handle) gnus-article-mime-handle-alist)
5856       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5857    ;; multipart/related
5858    ((and (equal (car handle) "multipart/related")
5859          (not (or gnus-mime-display-multipart-as-mixed
5860                   gnus-mime-display-multipart-related-as-mixed)))
5861     ;;;!!!We should find the start part, but we just default
5862     ;;;!!!to the first part.
5863     ;;(gnus-mime-display-part (cadr handle))
5864     ;;;!!! Most multipart/related is an HTML message plus images.
5865     ;;;!!! Unfortunately we are unable to let W3 display those
5866     ;;;!!! included images, so we just display it as a mixed multipart.
5867     ;;(gnus-mime-display-mixed (cdr handle))
5868     ;;;!!! No, w3 can display everything just fine.
5869     (gnus-mime-display-part (cadr handle)))
5870    ((equal (car handle) "multipart/signed")
5871     (gnus-add-wash-type 'signed)
5872     (gnus-mime-display-security handle))
5873    ((equal (car handle) "multipart/encrypted")
5874     (gnus-add-wash-type 'encrypted)
5875     (gnus-mime-display-security handle))
5876    ;; Other multiparts are handled like multipart/mixed.
5877    (t
5878     (gnus-mime-display-mixed (cdr handle)))))
5879
5880 (defun gnus-mime-part-function (handles)
5881   (if (stringp (car handles))
5882       (mapcar 'gnus-mime-part-function (cdr handles))
5883     (funcall gnus-article-mime-part-function handles)))
5884
5885 (defun gnus-mime-display-mixed (handles)
5886   (mapcar 'gnus-mime-display-part handles))
5887
5888 (defun gnus-mime-display-single (handle)
5889   (let ((type (mm-handle-media-type handle))
5890         (ignored gnus-ignored-mime-types)
5891         (not-attachment t)
5892         (move nil)
5893         display text)
5894     (catch 'ignored
5895       (progn
5896         (while ignored
5897           (when (string-match (pop ignored) type)
5898             (throw 'ignored nil)))
5899         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5900                                (with-current-buffer gnus-summary-buffer
5901                                  gnus-inhibit-images)
5902                              gnus-inhibit-images)
5903                            (string-match "\\`image/" type)))
5904                  (setq not-attachment
5905                        (and (not (mm-inline-override-p handle))
5906                             (or (not (mm-handle-disposition handle))
5907                                 (equal (car (mm-handle-disposition handle))
5908                                        "inline")
5909                                 (mm-attachment-override-p handle))))
5910                  (mm-automatic-display-p handle)
5911                  (or (and
5912                       (mm-inlinable-p handle)
5913                       (mm-inlined-p handle))
5914                      (mm-automatic-external-display-p type)))
5915             (setq display t)
5916           (when (equal (mm-handle-media-supertype handle) "text")
5917             (setq text t)))
5918         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5919               beg)
5920           (push (cons id handle) gnus-article-mime-handle-alist)
5921           (when (and display
5922                      (equal (mm-handle-media-supertype handle) "message"))
5923             (insert-char
5924              ?\n
5925              (cond ((not (bolp)) 2)
5926                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5927                    (t 1))))
5928           (when (or (not display)
5929                     (not (gnus-unbuttonized-mime-type-p type))
5930                     (eq id gnus-mime-buttonized-part-id))
5931             (gnus-insert-mime-button
5932              handle id (list (or display (and not-attachment text))))
5933             (gnus-article-insert-newline)
5934             ;; Remember modify the number of forward lines.
5935             (setq move t))
5936           (setq beg (point))
5937           (cond
5938            (display
5939             (when move
5940               (forward-line -1)
5941               (setq beg (point)))
5942             (let ((mail-parse-charset gnus-newsgroup-charset)
5943                   (mail-parse-ignored-charsets
5944                    (save-excursion (condition-case ()
5945                                        (set-buffer gnus-summary-buffer)
5946                                      (error))
5947                                    gnus-newsgroup-ignored-charsets)))
5948               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5949             (goto-char (point-max)))
5950            ((and text not-attachment)
5951             (when move
5952               (forward-line -1)
5953               (setq beg (point)))
5954             (gnus-article-insert-newline)
5955             (mm-display-inline handle)
5956             (goto-char (point-max))))
5957           ;; Do highlighting.
5958           (save-excursion
5959             (save-restriction
5960               (narrow-to-region beg (point))
5961               (if (eq handle gnus-article-mime-handles)
5962                   ;; The format=flowed case.
5963                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5964                 ;; Don't count signature parts that are never displayed.
5965                 ;; The part number should be re-calculated supposing this
5966                 ;; might be a message/rfc822 part.
5967                 (let (handles)
5968                   (dolist (part gnus-article-mime-handles)
5969                     (unless (or (stringp part)
5970                                 (equal (car (mm-handle-type part))
5971                                        "application/pgp-signature"))
5972                       (push part handles)))
5973                   (gnus-treat-article
5974                    nil (length (memq handle handles)) (length handles)
5975                    (mm-handle-media-type handle)))))))))))
5976
5977 (defun gnus-unbuttonized-mime-type-p (type)
5978   "Say whether TYPE is to be unbuttonized."
5979   (unless gnus-inhibit-mime-unbuttonizing
5980     (when (catch 'found
5981             (let ((types gnus-unbuttonized-mime-types))
5982               (while types
5983                 (when (string-match (pop types) type)
5984                   (throw 'found t)))))
5985       (not (catch 'found
5986              (let ((types gnus-buttonized-mime-types))
5987                (while types
5988                  (when (string-match (pop types) type)
5989                    (throw 'found t)))))))))
5990
5991 (defun gnus-article-insert-newline ()
5992   "Insert a newline, but mark it as undeletable."
5993   (gnus-put-text-property
5994    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5995
5996 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5997   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5998          (ihandles handles)
5999          (point (point))
6000          handle (inhibit-read-only t) from props begend not-pref)
6001     (save-window-excursion
6002       (save-restriction
6003         (when ibegend
6004           (narrow-to-region (car ibegend)
6005                             (or (cdr ibegend)
6006                                 (progn
6007                                   (goto-char (car ibegend))
6008                                   (forward-line 2)
6009                                   (point))))
6010           (delete-region (point-min) (point-max))
6011           (mm-remove-parts handles))
6012         (setq begend (list (point-marker)))
6013         ;; Do the toggle.
6014         (unless (setq not-pref (cadr (member preferred ihandles)))
6015           (setq not-pref (car ihandles)))
6016         (when (or ibegend
6017                   (not preferred)
6018                   (not (gnus-unbuttonized-mime-type-p
6019                         "multipart/alternative")))
6020           (gnus-add-text-properties
6021            (setq from (point))
6022            (progn
6023              (insert (format "%d.  " id))
6024              (point))
6025            `(gnus-callback
6026              (lambda (handles)
6027                (unless ,(not ibegend)
6028                  (setq gnus-article-mime-handle-alist
6029                        ',gnus-article-mime-handle-alist))
6030                (gnus-mime-display-alternative
6031                 ',ihandles ',not-pref ',begend ,id))
6032              keymap ,gnus-mime-button-map
6033              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6034              face ,gnus-article-button-face
6035              gnus-part ,id
6036              article-type multipart
6037              rear-nonsticky t))
6038           (widget-convert-button 'link from (point)
6039                                  :action 'gnus-widget-press-button
6040                                  :button-keymap gnus-widget-button-keymap)
6041           ;; Do the handles
6042           (while (setq handle (pop handles))
6043             (gnus-add-text-properties
6044              (setq from (point))
6045              (progn
6046                (insert (format "(%c) %-18s"
6047                                (if (equal handle preferred) ?* ? )
6048                                (mm-handle-media-type handle)))
6049                (point))
6050              `(gnus-callback
6051                (lambda (handles)
6052                  (unless ,(not ibegend)
6053                    (setq gnus-article-mime-handle-alist
6054                          ',gnus-article-mime-handle-alist))
6055                  (gnus-mime-display-alternative
6056                   ',ihandles ',handle ',begend ,id))
6057                keymap ,gnus-mime-button-map
6058                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6059                face ,gnus-article-button-face
6060                gnus-part ,id
6061                gnus-data ,handle
6062                rear-nonsticky t))
6063             (widget-convert-button 'link from (point)
6064                                    :action 'gnus-widget-press-button
6065                                    :button-keymap gnus-widget-button-keymap)
6066             (insert "  "))
6067           (insert "\n\n"))
6068         (when preferred
6069           (if (stringp (car preferred))
6070               (gnus-display-mime preferred)
6071             (let ((mail-parse-charset gnus-newsgroup-charset)
6072                   (mail-parse-ignored-charsets
6073                    (with-current-buffer gnus-summary-buffer
6074                      gnus-newsgroup-ignored-charsets)))
6075               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6076               ;; Do highlighting.
6077               (save-excursion
6078                 (save-restriction
6079                   (narrow-to-region (car begend) (point-max))
6080                   (gnus-treat-article
6081                    nil (length gnus-article-mime-handle-alist)
6082                    (gnus-article-mime-total-parts)
6083                    (mm-handle-media-type preferred))))))
6084           (goto-char (point-max))
6085           (setcdr begend (point-marker)))))
6086     (when ibegend
6087       (goto-char point))))
6088
6089 (defconst gnus-article-wash-status-strings
6090   (let ((alist '((cite "c" "Possible hidden citation text"
6091                        " " "All citation text visible")
6092                  (headers "h" "Hidden headers"
6093                           " " "All headers visible.")
6094                  (pgp "p" "Encrypted or signed message status hidden"
6095                       " " "No hidden encryption nor digital signature status")
6096                  (signature "s" "Signature has been hidden"
6097                             " " "Signature is visible")
6098                  (overstrike "o" "Overstrike (^H) characters applied"
6099                              " " "No overstrike characters applied")
6100                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6101                            " " "No /*_emphasis_*/ characters applied")))
6102         result)
6103     (dolist (entry alist result)
6104       (let ((key (nth 0 entry))
6105             (on (copy-sequence (nth 1 entry)))
6106             (on-help (nth 2 entry))
6107             (off (copy-sequence (nth 3 entry)))
6108             (off-help (nth 4 entry)))
6109         (put-text-property 0 1 'help-echo on-help on)
6110         (put-text-property 0 1 'help-echo off-help off)
6111         (push (list key on off) result))))
6112   "Alist of strings describing wash status in the mode line.
6113 Each entry has the form (KEY ON OF), where the KEY is a symbol
6114 representing the particular washing function, ON is the string to use
6115 in the article mode line when the washing function is active, and OFF
6116 is the string to use when it is inactive.")
6117
6118 (defun gnus-article-wash-status-entry (key value)
6119   (let ((entry (assoc key gnus-article-wash-status-strings)))
6120     (if value (nth 1 entry) (nth 2 entry))))
6121
6122 (defun gnus-article-wash-status ()
6123   "Return a string which display status of article washing."
6124   (with-current-buffer gnus-article-buffer
6125     (let ((cite (memq 'cite gnus-article-wash-types))
6126           (headers (memq 'headers gnus-article-wash-types))
6127           (boring (memq 'boring-headers gnus-article-wash-types))
6128           (pgp (memq 'pgp gnus-article-wash-types))
6129           (pem (memq 'pem gnus-article-wash-types))
6130           (signed (memq 'signed gnus-article-wash-types))
6131           (encrypted (memq 'encrypted gnus-article-wash-types))
6132           (signature (memq 'signature gnus-article-wash-types))
6133           (overstrike (memq 'overstrike gnus-article-wash-types))
6134           (emphasis (memq 'emphasis gnus-article-wash-types)))
6135       (concat
6136        (gnus-article-wash-status-entry 'cite cite)
6137        (gnus-article-wash-status-entry 'headers (or headers boring))
6138        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6139        (gnus-article-wash-status-entry 'signature signature)
6140        (gnus-article-wash-status-entry 'overstrike overstrike)
6141        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6142
6143 (defun gnus-add-wash-type (type)
6144   "Add a washing of TYPE to the current status."
6145   (add-to-list 'gnus-article-wash-types type))
6146
6147 (defun gnus-delete-wash-type (type)
6148   "Add a washing of TYPE to the current status."
6149   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6150
6151 (defun gnus-add-image (category image)
6152   "Add IMAGE of CATEGORY to the list of displayed images."
6153   (let ((entry (assq category gnus-article-image-alist)))
6154     (unless entry
6155       (setq entry (list category))
6156       (push entry gnus-article-image-alist))
6157     (nconc entry (list image))))
6158
6159 (defun gnus-delete-images (category)
6160   "Delete all images in CATEGORY."
6161   (let ((entry (assq category gnus-article-image-alist)))
6162     (dolist (image (cdr entry))
6163       (gnus-remove-image image category))
6164     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6165     (gnus-delete-wash-type category)))
6166
6167 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6168
6169 (defun gnus-article-maybe-hide-headers ()
6170   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6171 Provided for backwards compatibility."
6172   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6173                  (not (with-current-buffer gnus-summary-buffer
6174                         gnus-have-all-headers)))
6175              (not gnus-inhibit-hiding))
6176     (gnus-article-hide-headers)))
6177
6178 (declare-function shr-put-image "shr" (data alt &optional flags))
6179
6180 (defun gnus-shr-put-image (data alt &optional flags)
6181   "Put image DATA with a string ALT.  Enable image to be deleted."
6182   (let ((image (shr-put-image data (propertize (or alt "*")
6183                                                'gnus-image-category 'shr)
6184                               flags)))
6185     (when image
6186       (gnus-add-image 'shr image))))
6187
6188 ;;; Article savers.
6189
6190 (defun gnus-output-to-file (file-name)
6191   "Append the current article to a file named FILE-NAME.
6192 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6193 text and used as the value of the coding cookie which is added to the
6194 top of a file.  Otherwise, this function saves a raw article without
6195 the coding cookie."
6196   (let* ((artbuf (current-buffer))
6197          (file-name-coding-system nnmail-pathname-coding-system)
6198          (coding gnus-article-save-coding-system)
6199          (coding-system-for-read (if coding
6200                                      nil ;; Rely on the coding cookie.
6201                                    mm-text-coding-system))
6202          (coding-system-for-write (or coding
6203                                       mm-text-coding-system-for-write
6204                                       mm-text-coding-system))
6205          (exists (file-exists-p file-name)))
6206     (with-temp-buffer
6207       (when exists
6208         (insert-file-contents file-name)
6209         (goto-char (point-min))
6210         ;; Remove the existing coding cookie.
6211         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6212           (delete-region (match-beginning 0) (match-end 0))))
6213       (goto-char (point-max))
6214       (insert-buffer-substring artbuf)
6215       ;; Append newline at end of the buffer as separator, and then
6216       ;; save it to file.
6217       (goto-char (point-max))
6218       (insert "\n")
6219       (when coding
6220         ;; If the coding system is not suitable to encode the text,
6221         ;; ask a user for a proper one.
6222         (when (fboundp 'select-safe-coding-system)
6223           (setq coding (coding-system-base
6224                         (save-window-excursion
6225                           (select-safe-coding-system (point-min) (point-max)
6226                                                      coding))))
6227           (setq coding-system-for-write
6228                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6229                     coding)))
6230         (goto-char (point-min))
6231         ;; Add the coding cookie.
6232         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6233                         coding-system-for-write)))
6234       (if exists
6235           (progn
6236             (write-region (point-min) (point-max) file-name nil 'no-message)
6237             (message "Appended to %s" file-name))
6238         (write-region (point-min) (point-max) file-name))))
6239   t)
6240
6241 (defun gnus-narrow-to-page (&optional arg)
6242   "Narrow the article buffer to a page.
6243 If given a numerical ARG, move forward ARG pages."
6244   (interactive "P")
6245   (setq arg (if arg (prefix-numeric-value arg) 0))
6246   (with-current-buffer gnus-article-buffer
6247     (widen)
6248     ;; Remove any old next/prev buttons.
6249     (when (gnus-visual-p 'page-marker)
6250       (let ((inhibit-read-only t))
6251         (gnus-remove-text-with-property 'gnus-prev)
6252         (gnus-remove-text-with-property 'gnus-next)))
6253     (let (st nd pt)
6254       (when (save-excursion
6255               (cond ((< arg 0)
6256                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6257                          (prog1
6258                              (setq nd (match-beginning 0)
6259                                    pt nd)
6260                            (when (re-search-backward page-delimiter nil t)
6261                              (setq st (match-end 0))))
6262                        (when (re-search-forward page-delimiter nil t)
6263                          (setq nd (match-beginning 0)
6264                                pt (point-min)))))
6265                     ((> arg 0)
6266                      (if (re-search-forward page-delimiter nil 'move arg)
6267                          (prog1
6268                              (setq st (match-end 0)
6269                                    pt st)
6270                            (when (re-search-forward page-delimiter nil t)
6271                              (setq nd (match-beginning 0))))
6272                        (when (re-search-backward page-delimiter nil t)
6273                          (setq st (match-end 0)
6274                                pt (point-max)))))
6275                     (t
6276                      (when (re-search-backward page-delimiter nil t)
6277                        (goto-char (setq st (match-end 0))))
6278                      (when (re-search-forward page-delimiter nil t)
6279                        (setq nd (match-beginning 0)))
6280                      (or st nd))))
6281         (setq gnus-page-broken t)
6282         (when pt (goto-char pt))
6283         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6284         (when (gnus-visual-p 'page-marker)
6285           (save-excursion
6286             (when nd
6287               (goto-char nd)
6288               (gnus-insert-next-page-button))
6289             (when st
6290               (goto-char st)
6291               (gnus-insert-prev-page-button))))))))
6292
6293 ;; Article mode commands
6294
6295 (defun gnus-article-goto-next-page ()
6296   "Show the next page of the article."
6297   (interactive)
6298   (when (gnus-article-next-page)
6299     (goto-char (point-min))
6300     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6301
6302
6303 (defun gnus-article-goto-prev-page ()
6304   "Show the previous page of the article."
6305   (interactive)
6306   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6307       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6308     (gnus-article-prev-page nil)))
6309
6310 ;; This is cleaner but currently breaks `gnus-pick-mode':
6311 ;;
6312 ;; (defun gnus-article-goto-next-page ()
6313 ;;   "Show the next page of the article."
6314 ;;   (interactive)
6315 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6316 ;;     (gnus-summary-next-page)))
6317 ;;
6318 ;; (defun gnus-article-goto-prev-page ()
6319 ;;   "Show the next page of the article."
6320 ;;   (interactive)
6321 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6322 ;;     (gnus-summary-prev-page)))
6323
6324 (defun gnus-article-next-page (&optional lines)
6325   "Show the next page of the current article.
6326 If end of article, return non-nil.  Otherwise return nil.
6327 Argument LINES specifies lines to be scrolled up."
6328   (interactive "p")
6329   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6330   (if (and (not (and gnus-article-over-scroll
6331                      (> (count-lines (window-start) (point-max))
6332                         (if (featurep 'xemacs)
6333                             (or lines (1- (window-height)))
6334                           (+ (or lines (1- (window-height))) scroll-margin)))))
6335            (save-excursion
6336              (end-of-line)
6337              (and (pos-visible-in-window-p)     ;Not continuation line.
6338                   (>= (point) (point-max)))))
6339       ;; Nothing in this page.
6340       (if (or (not gnus-page-broken)
6341               (save-excursion
6342                 (save-restriction
6343                   (widen)
6344                   (forward-line)
6345                   (eobp)))) ;Real end-of-buffer?
6346           (progn
6347             (when gnus-article-over-scroll
6348               (gnus-article-next-page-1 lines))
6349             t)                  ;Nothing more.
6350         (gnus-narrow-to-page 1)         ;Go to next page.
6351         nil)
6352     ;; More in this page.
6353     (gnus-article-next-page-1 lines)
6354     nil))
6355
6356 (defun gnus-article-beginning-of-window ()
6357   "Move point to the beginning of the window.
6358 In Emacs, the point is placed at the line number which `scroll-margin'
6359 specifies."
6360   (if (featurep 'xemacs)
6361       (move-to-window-line 0)
6362     ;; There is an obscure bug in Emacs that makes it impossible to
6363     ;; scroll past big pictures in the article buffer.  Try to fix
6364     ;; this by adding a sanity check by counting the lines visible.
6365     (when (> (count-lines (window-start) (window-end)) 30)
6366       (move-to-window-line
6367        (min (max 0 scroll-margin)
6368             (max 1 (- (window-height)
6369                       (if mode-line-format 1 0)
6370                       (if header-line-format 1 0)
6371                       2)))))))
6372
6373 (defun gnus-article-next-page-1 (lines)
6374   (condition-case ()
6375       (let ((scroll-in-place nil)
6376             (auto-window-vscroll nil))
6377         (scroll-up lines))
6378     (end-of-buffer
6379      ;; Long lines may cause an end-of-buffer error.
6380      (goto-char (point-max))))
6381   (gnus-article-beginning-of-window))
6382
6383 (defun gnus-article-prev-page (&optional lines)
6384   "Show previous page of current article.
6385 Argument LINES specifies lines to be scrolled down."
6386   (interactive "p")
6387   (move-to-window-line 0)
6388   (if (and gnus-page-broken
6389            (bobp)
6390            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6391       (progn
6392         (gnus-narrow-to-page -1)        ;Go to previous page.
6393         (goto-char (point-max))
6394         (recenter (if gnus-article-over-scroll
6395                       (if lines
6396                           (max (if (featurep 'xemacs)
6397                                    lines
6398                                  (+ lines scroll-margin))
6399                                3)
6400                         (- (window-height) 2))
6401                     -1)))
6402     (prog1
6403         (condition-case ()
6404             (let ((scroll-in-place nil))
6405               (scroll-down lines))
6406           (beginning-of-buffer
6407            (goto-char (point-min))))
6408       (gnus-article-beginning-of-window))))
6409
6410 (defun gnus-article-only-boring-p ()
6411   "Decide whether there is only boring text remaining in the article.
6412 Something \"interesting\" is a word of at least two letters that does
6413 not have a face in `gnus-article-boring-faces'."
6414   (when (and gnus-article-skip-boring
6415              (boundp 'gnus-article-boring-faces)
6416              (symbol-value 'gnus-article-boring-faces))
6417     (save-excursion
6418       (let ((inhibit-point-motion-hooks t))
6419         (catch 'only-boring
6420           (while (re-search-forward "\\b\\w\\w" nil t)
6421             (forward-char -1)
6422             (when (not (gnus-intersection
6423                         (gnus-faces-at (point))
6424                         (symbol-value 'gnus-article-boring-faces)))
6425               (throw 'only-boring nil)))
6426           (throw 'only-boring t))))))
6427
6428 (defun gnus-article-refer-article ()
6429   "Read article specified by message-id around point."
6430   (interactive)
6431   (save-excursion
6432     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6433     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6434     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6435         (let ((msg-id (concat "<" (match-string 0) ">")))
6436           (set-buffer gnus-summary-buffer)
6437           (gnus-summary-refer-article msg-id))
6438       (error "No references around point"))))
6439
6440 (defun gnus-article-show-summary ()
6441   "Reconfigure windows to show summary buffer."
6442   (interactive)
6443   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6444       (error "There is no summary buffer for this article buffer")
6445     (gnus-article-set-globals)
6446     (gnus-configure-windows 'article)
6447     (gnus-summary-goto-subject gnus-current-article)
6448     (gnus-summary-position-point)))
6449
6450 (defun gnus-article-describe-briefly ()
6451   "Describe article mode commands briefly."
6452   (interactive)
6453   (gnus-message 6 "%s" (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-goto-next-page]:Next page      \\[gnus-article-goto-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
6454
6455 (defun gnus-article-check-buffer ()
6456   "Beep if not in an article buffer."
6457   (unless (equal major-mode 'gnus-article-mode)
6458     (error "Command invoked outside of a Gnus article buffer")))
6459
6460 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6461   "Read a summary buffer key sequence and execute it from the article buffer."
6462   (interactive "P")
6463   (gnus-article-check-buffer)
6464   (let ((nosaves
6465          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6466            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6467            "=" "^" "\M-^" "|"))
6468         (nosave-but-article
6469          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6470            "An" "Ap" [?A (meta return)] [?A delete]))
6471         (nosave-in-article
6472          '("AS" "\C-d"))
6473         (up-to-top
6474          '("n" "Gn" "p" "Gp"))
6475         keys new-sum-point)
6476     (with-current-buffer gnus-article-current-summary
6477       (let (gnus-pick-mode)
6478         (setq unread-command-events (nconc unread-command-events
6479                                            (list (or key last-command-event)))
6480               keys (if (featurep 'xemacs)
6481                        (events-to-keys (read-key-sequence nil t))
6482                      (read-key-sequence nil t)))))
6483
6484     (message "")
6485
6486     (cond
6487      ((eq (aref keys (1- (length keys))) ?\C-h)
6488       (gnus-article-describe-bindings (substring keys 0 -1)))
6489      ((or (member keys nosaves)
6490           (member keys nosave-but-article)
6491           (member keys nosave-in-article))
6492       (let (func)
6493         (save-window-excursion
6494           (pop-to-buffer gnus-article-current-summary)
6495           ;; We disable the pick minor mode commands.
6496           (let (gnus-pick-mode)
6497             (setq func (key-binding keys t))))
6498         (if (or (not func)
6499                 (numberp func))
6500             (ding)
6501           (unless (member keys nosave-in-article)
6502             (set-buffer gnus-article-current-summary))
6503           (when (and (symbolp func)
6504                      (get func 'disabled))
6505             (error "Function %s disabled" func))
6506           (call-interactively func)
6507           (setq new-sum-point (point)))
6508         (when (member keys nosave-but-article)
6509           (pop-to-buffer gnus-article-buffer))))
6510      (t
6511       ;; These commands should restore window configuration.
6512       (let ((obuf (current-buffer))
6513             (owin (current-window-configuration))
6514             win func in-buffer selected new-sum-start new-sum-hscroll err)
6515         (cond (not-restore-window
6516                (pop-to-buffer gnus-article-current-summary)
6517                (setq win (selected-window)))
6518               ((setq win (get-buffer-window gnus-article-current-summary))
6519                (select-window win))
6520               (t
6521                (let ((summary-buffer gnus-article-current-summary))
6522                  (gnus-configure-windows 'article)
6523                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6524                    (let ((gnus-buffer-configuration
6525                           '((article ((vertical 1.0
6526                                                 (summary 0.25 point)
6527                                                 (article 1.0)))))))
6528                      (gnus-configure-windows 'article))
6529                    (setq win (get-buffer-window summary-buffer 'visible)))
6530                  (gnus-select-frame-set-input-focus (window-frame win))
6531                  (select-window win))))
6532         (setq in-buffer (current-buffer))
6533         ;; We disable the pick minor mode commands.
6534         (setq func (let (gnus-pick-mode)
6535                      (key-binding keys t)))
6536         (when (get func 'disabled)
6537           (error "Function %s disabled" func))
6538         (if (and func
6539                  (functionp func)
6540                  (condition-case code
6541                      (progn
6542                        (call-interactively func)
6543                        t)
6544                    (error
6545                     (setq err code)
6546                     nil)))
6547             (progn
6548               (when (eq win (selected-window))
6549                 (setq new-sum-point (point)
6550                       new-sum-start (window-start win)
6551                       new-sum-hscroll (window-hscroll win)))
6552               (when (or (eq in-buffer (current-buffer))
6553                         (when (eq obuf (current-buffer))
6554                           (set-buffer in-buffer)
6555                           t))
6556                 (setq selected (gnus-summary-select-article))
6557                 (set-buffer obuf)
6558                 (unless not-restore-window
6559                   (set-window-configuration owin))
6560                 (when (and (eq selected 'old)
6561                            new-sum-point)
6562                   (set-window-start (get-buffer-window (current-buffer))
6563                                     1)
6564                   (set-window-point (get-buffer-window (current-buffer))
6565                                     (if (article-goto-body)
6566                                         (1- (point))
6567                                       (point))))
6568                 (when (and (not not-restore-window)
6569                            new-sum-point
6570                            (window-live-p win)
6571                            (with-current-buffer (window-buffer win)
6572                              (eq major-mode 'gnus-summary-mode)))
6573                   (set-window-point win new-sum-point)
6574                   (set-window-start win new-sum-start)
6575                   (set-window-hscroll win new-sum-hscroll))))
6576           (set-window-configuration owin)
6577           (if err
6578               (signal (car err) (cdr err))
6579             (ding))))))))
6580
6581 (defun gnus-article-read-summary-send-keys ()
6582   (interactive)
6583   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6584     (gnus-article-read-summary-keys)))
6585
6586 (defun gnus-article-describe-key (key)
6587   "Display documentation of the function invoked by KEY.
6588 KEY is a string or a vector."
6589   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6590                        (read-key-sequence "Describe key: "))))
6591   (gnus-article-check-buffer)
6592   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6593                                   gnus-article-read-summary-send-keys))
6594       (with-current-buffer gnus-article-current-summary
6595         (setq unread-command-events
6596               (if (featurep 'xemacs)
6597                   (append key nil)
6598                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6599                                         (list 'meta (- x 128))
6600                                       x))
6601                         key)))
6602         (let ((cursor-in-echo-area t)
6603               gnus-pick-mode)
6604           (describe-key (read-key-sequence nil t))))
6605     (describe-key key)))
6606
6607 (defun gnus-article-describe-key-briefly (key &optional insert)
6608   "Display documentation of the function invoked by KEY.
6609 KEY is a string or a vector."
6610   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6611                        (read-key-sequence "Describe key: "))
6612                      current-prefix-arg))
6613   (gnus-article-check-buffer)
6614   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6615                                   gnus-article-read-summary-send-keys))
6616       (with-current-buffer gnus-article-current-summary
6617         (setq unread-command-events
6618               (if (featurep 'xemacs)
6619                   (append key nil)
6620                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6621                                         (list 'meta (- x 128))
6622                                       x))
6623                         key)))
6624         (let ((cursor-in-echo-area t)
6625               gnus-pick-mode)
6626           (describe-key-briefly (read-key-sequence nil t) insert)))
6627     (describe-key-briefly key insert)))
6628
6629 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6630 (defvar gnus-agent-summary-mode)
6631 (defvar gnus-draft-mode)
6632 ;; Calling help-buffer will autoload help-mode.
6633 (defvar help-xref-stack-item)
6634 ;; Emacs 22 doesn't load it in the batch mode.
6635 (eval-when-compile
6636   (autoload 'help-buffer "help-mode"))
6637
6638 (defun gnus-article-describe-bindings (&optional prefix)
6639   "Show a list of all defined keys, and their definitions.
6640 The optional argument PREFIX, if non-nil, should be a key sequence;
6641 then we display only bindings that start with that prefix."
6642   (interactive)
6643   (gnus-article-check-buffer)
6644   (let ((keymap (copy-keymap gnus-article-mode-map))
6645         (map (copy-keymap gnus-article-send-map))
6646         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6647         parent agent draft)
6648     (define-key keymap "S" map)
6649     (define-key map [t] nil)
6650     (with-current-buffer gnus-article-current-summary
6651       (set-keymap-parent
6652        keymap
6653        (if (setq parent (keymap-parent gnus-article-mode-map))
6654            (prog1
6655                (setq parent (copy-keymap parent))
6656              (set-keymap-parent parent (current-local-map)))
6657          (current-local-map)))
6658       (set-keymap-parent map (key-binding "S"))
6659       (let (key def gnus-pick-mode)
6660         (while sumkeys
6661           (setq key (pop sumkeys))
6662           (cond ((and (vectorp key) (= (length key) 1)
6663                       (consp (setq def (aref key 0)))
6664                       (numberp (car def)) (numberp (cdr def)))
6665                  (when (< (max (car def) (cdr def)) 128)
6666                    (setq sumkeys
6667                          (append (mapcar
6668                                   #'vector
6669                                   (nreverse (gnus-uncompress-range def)))
6670                                  sumkeys))))
6671                 ((setq def (key-binding key))
6672                  (unless (eq def 'undefined)
6673                    (define-key keymap key def))))))
6674       (when (boundp 'gnus-agent-summary-mode)
6675         (setq agent gnus-agent-summary-mode))
6676       (when (boundp 'gnus-draft-mode)
6677         (setq draft gnus-draft-mode)))
6678     (with-temp-buffer
6679       (use-local-map keymap)
6680       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6681       (set (make-local-variable 'gnus-draft-mode) draft)
6682       (describe-bindings prefix))
6683     (let ((item `((lambda (prefix)
6684                     (with-current-buffer ,(current-buffer)
6685                       (gnus-article-describe-bindings prefix)))
6686                   ,prefix)))
6687       (with-current-buffer (let (help-xref-following) (help-buffer))
6688         (setq help-xref-stack-item item)))))
6689
6690 (defun gnus-article-reply-with-original (&optional wide)
6691   "Start composing a reply mail to the current message.
6692 The text in the region will be yanked.  If the region isn't active,
6693 the entire article will be yanked."
6694   (interactive)
6695   (let ((article (cdr gnus-article-current))
6696         contents)
6697     (if (not (gnus-region-active-p))
6698         (with-current-buffer gnus-summary-buffer
6699           (gnus-summary-reply (list (list article)) wide))
6700       (setq contents (buffer-substring (point) (mark t)))
6701       ;; Deactivate active regions.
6702       (when (and (boundp 'transient-mark-mode)
6703                  transient-mark-mode)
6704         (setq mark-active nil))
6705       (with-current-buffer gnus-summary-buffer
6706         (gnus-summary-reply
6707          (list (list article contents)) wide)))))
6708
6709 (defun gnus-article-wide-reply-with-original ()
6710   "Start composing a wide reply mail to the current message.
6711 The text in the region will be yanked.  If the region isn't active,
6712 the entire article will be yanked."
6713   (interactive)
6714   (gnus-article-reply-with-original t))
6715
6716 (defun gnus-article-followup-with-original ()
6717   "Compose a followup to the current article.
6718 The text in the region will be yanked.  If the region isn't active,
6719 the entire article will be yanked."
6720   (interactive)
6721   (let ((article (cdr gnus-article-current))
6722         contents)
6723       (if (not (gnus-region-active-p))
6724           (with-current-buffer gnus-summary-buffer
6725             (gnus-summary-followup (list (list article))))
6726         (setq contents (buffer-substring (point) (mark t)))
6727         ;; Deactivate active regions.
6728         (when (and (boundp 'transient-mark-mode)
6729                    transient-mark-mode)
6730           (setq mark-active nil))
6731         (with-current-buffer gnus-summary-buffer
6732           (gnus-summary-followup
6733            (list (list article contents)))))))
6734
6735 (defun gnus-article-hide (&optional arg force)
6736   "Hide all the gruft in the current article.
6737 This means that signatures, cited text and (some) headers will be
6738 hidden.
6739 If given a prefix, show the hidden text instead."
6740   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6741   (gnus-article-hide-headers arg)
6742   (gnus-article-hide-list-identifiers arg)
6743   (gnus-article-hide-citation-maybe arg force)
6744   (gnus-article-hide-signature arg))
6745
6746 (defun gnus-check-group-server ()
6747   ;; Make sure the connection to the server is alive.
6748   (unless (gnus-server-opened
6749            (gnus-find-method-for-group gnus-newsgroup-name))
6750     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6751     (gnus-request-group gnus-newsgroup-name t)))
6752
6753 (eval-when-compile
6754   (autoload 'nneething-get-file-name "nneething"))
6755
6756 (defun gnus-request-article-this-buffer (article group)
6757   "Get an article and insert it into this buffer."
6758   (let (do-update-line sparse-header)
6759     (prog1
6760         (save-excursion
6761           (erase-buffer)
6762           (gnus-kill-all-overlays)
6763           (setq group (or group gnus-newsgroup-name))
6764
6765           ;; Using `gnus-request-article' directly will insert the article into
6766           ;; `nntp-server-buffer' - so we'll save some time by not having to
6767           ;; copy it from the server buffer into the article buffer.
6768
6769           ;; We only request an article by message-id when we do not have the
6770           ;; headers for it, so we'll have to get those.
6771           (when (stringp article)
6772             (gnus-read-header article))
6773
6774           ;; If the article number is negative, that means that this article
6775           ;; doesn't belong in this newsgroup (possibly), so we find its
6776           ;; message-id and request it by id instead of number.
6777           (when (and (numberp article)
6778                      gnus-summary-buffer
6779                      (get-buffer gnus-summary-buffer)
6780                      (gnus-buffer-exists-p gnus-summary-buffer))
6781             (with-current-buffer gnus-summary-buffer
6782               (let ((header (gnus-summary-article-header article)))
6783                 (when (< article 0)
6784                   (cond
6785                    ((memq article gnus-newsgroup-sparse)
6786                     ;; This is a sparse gap article.
6787                     (setq do-update-line article)
6788                     (setq article (mail-header-id header))
6789                     (setq sparse-header (gnus-read-header article))
6790                     (setq gnus-newsgroup-sparse
6791                           (delq article gnus-newsgroup-sparse)))
6792                    ((vectorp header)
6793                     ;; It's a real article.
6794                     (setq article (mail-header-id header)))
6795                    (t
6796                     ;; It is an extracted pseudo-article.
6797                     (setq article 'pseudo)
6798                     (gnus-request-pseudo-article header))))
6799
6800                 (let ((method (gnus-find-method-for-group
6801                                gnus-newsgroup-name)))
6802                   (when (and (eq (car method) 'nneething)
6803                              (vectorp header))
6804                     (let ((dir (nneething-get-file-name
6805                                 (mail-header-id header))))
6806                       (when (and (stringp dir)
6807                                  (file-directory-p dir))
6808                         (setq article 'nneething)
6809                         (gnus-group-enter-directory dir))))))))
6810
6811           (cond
6812            ;; Refuse to select canceled articles.
6813            ((and (numberp article)
6814                  gnus-summary-buffer
6815                  (get-buffer gnus-summary-buffer)
6816                  (gnus-buffer-exists-p gnus-summary-buffer)
6817                  (eq (cdr (with-current-buffer gnus-summary-buffer
6818                             (assq article gnus-newsgroup-reads)))
6819                      gnus-canceled-mark))
6820             nil)
6821            ;; We first check `gnus-original-article-buffer'.
6822            ((and (get-buffer gnus-original-article-buffer)
6823                  (numberp article)
6824                  (with-current-buffer gnus-original-article-buffer
6825                    (and (equal (car gnus-original-article) group)
6826                         (eq (cdr gnus-original-article) article))))
6827             ;; `insert-buffer-substring' would incorrectly use the
6828             ;; equivalent of string-make-multibyte which amount to decoding
6829             ;; with locale-coding-system, causing failure of
6830             ;; subsequent decoding.
6831             (insert (mm-string-to-multibyte
6832                      (with-current-buffer gnus-original-article-buffer
6833                        (buffer-substring (point-min) (point-max)))))
6834             'article)
6835            ;; Check the backlog.
6836            ((and gnus-keep-backlog
6837                  (gnus-backlog-request-article group article (current-buffer)))
6838             'article)
6839            ;; Check asynchronous pre-fetch.
6840            ((gnus-async-request-fetched-article group article (current-buffer))
6841             (gnus-async-prefetch-next group article gnus-summary-buffer)
6842             (when (and (numberp article) gnus-keep-backlog)
6843               (gnus-backlog-enter-article group article (current-buffer)))
6844             'article)
6845            ;; Check the cache.
6846            ((and gnus-use-cache
6847                  (numberp article)
6848                  (gnus-cache-request-article article group))
6849             'article)
6850            ;; Check the agent cache.
6851            ((gnus-agent-request-article article group)
6852             'article)
6853            ;; Get the article and put into the article buffer.
6854            ((or (stringp article)
6855                 (numberp article))
6856             (let ((gnus-override-method gnus-override-method)
6857                   (methods (and (stringp article)
6858                                 (with-current-buffer gnus-summary-buffer
6859                                   (gnus-refer-article-methods))))
6860                   (backend (car (gnus-find-method-for-group
6861                                  gnus-newsgroup-name)))
6862                   result
6863                   (inhibit-read-only t))
6864               (when (and (null gnus-override-method)
6865                          methods)
6866                 (setq gnus-override-method (pop methods)))
6867               (while (not result)
6868                 (erase-buffer)
6869                 (gnus-kill-all-overlays)
6870                 (let ((gnus-newsgroup-name group))
6871                   (gnus-check-group-server))
6872                 (cond
6873                  ((gnus-request-article article group (current-buffer))
6874                   (when (numberp article)
6875                     (gnus-async-prefetch-next group article
6876                                               gnus-summary-buffer)
6877                     (when gnus-keep-backlog
6878                       (gnus-backlog-enter-article
6879                        group article (current-buffer)))
6880                     (when (and gnus-agent
6881                                (gnus-agent-group-covered-p group))
6882                       (gnus-agent-store-article article group)))
6883                   (setq result 'article))
6884                  (methods
6885                   (setq gnus-override-method (pop methods)))
6886                  ((not (string-match "^400 "
6887                                      (nnheader-get-report backend)))
6888                   ;; If we get 400 server disconnect, reconnect and
6889                   ;; retry; otherwise, assume the article has expired.
6890                   (setq result 'done))))
6891               (and (eq result 'article) 'article)))
6892            ;; It was a pseudo.
6893            (t article)))
6894
6895       ;; Associate this article with the current summary buffer.
6896       (setq gnus-article-current-summary gnus-summary-buffer)
6897
6898       ;; Take the article from the original article buffer
6899       ;; and place it in the buffer it's supposed to be in.
6900       (when (and (get-buffer gnus-article-buffer)
6901                  (equal (buffer-name (current-buffer))
6902                         (buffer-name (get-buffer gnus-article-buffer))))
6903         (save-excursion
6904           (if (get-buffer gnus-original-article-buffer)
6905               (set-buffer gnus-original-article-buffer)
6906             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6907             (buffer-disable-undo)
6908             (setq major-mode 'gnus-original-article-mode)
6909             (setq buffer-read-only t))
6910           (let ((inhibit-read-only t))
6911             (erase-buffer)
6912             (insert-buffer-substring gnus-article-buffer))
6913           (setq gnus-original-article (cons group article)))
6914
6915         ;; Decode charsets.
6916         (run-hooks 'gnus-article-decode-hook)
6917         ;; Mark article as decoded or not.
6918         (setq gnus-article-decoded-p gnus-article-decode-hook))
6919
6920       ;; Update sparse articles.
6921       (when (and do-update-line
6922                  (or (numberp article)
6923                      (stringp article)))
6924         (let ((buf (current-buffer)))
6925           (set-buffer gnus-summary-buffer)
6926           (gnus-summary-update-article do-update-line sparse-header)
6927           (gnus-summary-goto-subject do-update-line nil t)
6928           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6929                             (point))
6930           (set-buffer buf))))))
6931
6932 (defun gnus-block-private-groups (group)
6933   (if (gnus-news-group-p group)
6934       ;; Block nothing in news groups.
6935       nil
6936     ;; Block everything anywhere else.
6937     "."))
6938
6939 (defun gnus-blocked-images ()
6940   (if (functionp gnus-blocked-images)
6941       (funcall gnus-blocked-images gnus-newsgroup-name)
6942     gnus-blocked-images))
6943
6944 ;;;
6945 ;;; Article editing
6946 ;;;
6947
6948 (defcustom gnus-article-edit-mode-hook nil
6949   "Hook run in article edit mode buffers."
6950   :group 'gnus-article-various
6951   :type 'hook)
6952
6953 (defvar gnus-article-edit-done-function nil)
6954
6955 (defvar gnus-article-edit-mode-map nil)
6956
6957 ;; Should we be using derived.el for this?
6958 (unless gnus-article-edit-mode-map
6959   (setq gnus-article-edit-mode-map (make-keymap))
6960   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6961
6962   (gnus-define-keys gnus-article-edit-mode-map
6963     "\C-c?"    describe-mode
6964     "\C-c\C-c" gnus-article-edit-done
6965     "\C-c\C-k" gnus-article-edit-exit
6966     "\C-c\C-f\C-t" message-goto-to
6967     "\C-c\C-f\C-o" message-goto-from
6968     "\C-c\C-f\C-b" message-goto-bcc
6969     ;;"\C-c\C-f\C-w" message-goto-fcc
6970     "\C-c\C-f\C-c" message-goto-cc
6971     "\C-c\C-f\C-s" message-goto-subject
6972     "\C-c\C-f\C-r" message-goto-reply-to
6973     "\C-c\C-f\C-n" message-goto-newsgroups
6974     "\C-c\C-f\C-d" message-goto-distribution
6975     "\C-c\C-f\C-f" message-goto-followup-to
6976     "\C-c\C-f\C-m" message-goto-mail-followup-to
6977     "\C-c\C-f\C-k" message-goto-keywords
6978     "\C-c\C-f\C-u" message-goto-summary
6979     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6980     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6981     "\C-c\C-b" message-goto-body
6982     "\C-c\C-i" message-goto-signature
6983
6984     "\C-c\C-t" message-insert-to
6985     "\C-c\C-n" message-insert-newsgroups
6986     "\C-c\C-o" message-sort-headers
6987     "\C-c\C-e" message-elide-region
6988     "\C-c\C-v" message-delete-not-region
6989     "\C-c\C-z" message-kill-to-signature
6990     "\M-\r" message-newline-and-reformat
6991     "\C-c\C-a" mml-attach-file
6992     "\C-a" message-beginning-of-line
6993     "\t" message-tab
6994     "\M-;" comment-region)
6995
6996   (gnus-define-keys (gnus-article-edit-wash-map
6997                      "\C-c\C-w" gnus-article-edit-mode-map)
6998     "f" gnus-article-edit-full-stops))
6999
7000 (easy-menu-define
7001   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7002   '("Field"
7003     ["Fetch To" message-insert-to t]
7004     ["Fetch Newsgroups" message-insert-newsgroups t]
7005     "----"
7006     ["To" message-goto-to t]
7007     ["From" message-goto-from t]
7008     ["Subject" message-goto-subject t]
7009     ["Cc" message-goto-cc t]
7010     ["Reply-To" message-goto-reply-to t]
7011     ["Summary" message-goto-summary t]
7012     ["Keywords" message-goto-keywords t]
7013     ["Newsgroups" message-goto-newsgroups t]
7014     ["Followup-To" message-goto-followup-to t]
7015     ["Mail-Followup-To" message-goto-mail-followup-to t]
7016     ["Distribution" message-goto-distribution t]
7017     ["Body" message-goto-body t]
7018     ["Signature" message-goto-signature t]))
7019
7020 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7021   "Major mode for editing articles.
7022 This is an extended text-mode.
7023
7024 \\{gnus-article-edit-mode-map}"
7025   (make-local-variable 'gnus-article-edit-done-function)
7026   (make-local-variable 'gnus-prev-winconf)
7027   (set (make-local-variable 'font-lock-defaults)
7028        '(message-font-lock-keywords t))
7029   (set (make-local-variable 'mail-header-separator) "")
7030   (set (make-local-variable 'gnus-article-edit-mode) t)
7031   (easy-menu-add message-mode-field-menu message-mode-map)
7032   (mml-mode)
7033   (setq buffer-read-only nil)
7034   (buffer-enable-undo)
7035   (widen))
7036
7037 (defun gnus-article-edit (&optional force)
7038   "Edit the current article.
7039 This will have permanent effect only in mail groups.
7040 If FORCE is non-nil, allow editing of articles even in read-only
7041 groups."
7042   (interactive "P")
7043   (when (and (not force)
7044              (gnus-group-read-only-p))
7045     (error "The current newsgroup does not support article editing"))
7046   (gnus-article-date-original)
7047   (gnus-article-edit-article
7048    'ignore
7049    `(lambda (no-highlight)
7050       'ignore
7051       (gnus-summary-edit-article-done
7052        ,(or (mail-header-references gnus-current-headers) "")
7053        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7054
7055 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7056   "Start editing the contents of the current article buffer."
7057   (let ((winconf (current-window-configuration)))
7058     (set-buffer gnus-article-buffer)
7059     (let ((message-auto-save-directory
7060            ;; Don't associate the article buffer with a draft file.
7061            nil))
7062       (gnus-article-edit-mode))
7063     (funcall start-func)
7064     (set-buffer-modified-p nil)
7065     (gnus-configure-windows 'edit-article)
7066     (setq gnus-article-edit-done-function exit-func)
7067     (setq gnus-prev-winconf winconf)
7068     (unless quiet
7069       (gnus-message 6 "C-c C-c to end edits"))))
7070
7071 (defun gnus-article-edit-done (&optional arg)
7072   "Update the article edits and exit."
7073   (interactive "P")
7074   (let ((func gnus-article-edit-done-function)
7075         (buf (current-buffer))
7076         (start (window-start))
7077         (p (point))
7078         (winconf gnus-prev-winconf))
7079     (widen) ;; Widen it in case that users narrowed the buffer.
7080     (funcall func arg)
7081     (set-buffer buf)
7082     ;; The cache and backlog have to be flushed somewhat.
7083     (when gnus-keep-backlog
7084       (gnus-backlog-remove-article
7085        (car gnus-article-current) (cdr gnus-article-current)))
7086     ;; Flush original article as well.
7087     (gnus-flush-original-article-buffer)
7088     (when gnus-use-cache
7089       (gnus-cache-update-article
7090        (car gnus-article-current) (cdr gnus-article-current)))
7091     ;; We remove all text props from the article buffer.
7092     (kill-all-local-variables)
7093     (set-text-properties (point-min) (point-max) nil)
7094     (gnus-article-mode)
7095     (set-window-configuration winconf)
7096     (set-buffer buf)
7097     (set-window-start (get-buffer-window buf) start)
7098     (set-window-point (get-buffer-window buf) (point)))
7099   (gnus-summary-show-article))
7100
7101 (defun gnus-flush-original-article-buffer ()
7102   (when (get-buffer gnus-original-article-buffer)
7103     (with-current-buffer gnus-original-article-buffer
7104       (setq gnus-original-article nil))))
7105
7106 (defun gnus-article-edit-exit ()
7107   "Exit the article editing without updating."
7108   (interactive)
7109   (when (or (not (buffer-modified-p))
7110             (yes-or-no-p "Article modified; kill anyway? "))
7111     (let ((curbuf (current-buffer))
7112           (p (point))
7113           (window-start (window-start)))
7114       (erase-buffer)
7115       (if (gnus-buffer-live-p gnus-original-article-buffer)
7116           (insert-buffer-substring gnus-original-article-buffer))
7117       (let ((winconf gnus-prev-winconf))
7118         (kill-all-local-variables)
7119         (gnus-article-mode)
7120         (set-window-configuration winconf)
7121         ;; Tippy-toe some to make sure that point remains where it was.
7122         (save-current-buffer
7123           (set-buffer curbuf)
7124           (set-window-start (get-buffer-window (current-buffer)) window-start)
7125           (goto-char p))))
7126     (gnus-summary-show-article)))
7127
7128 (defun gnus-article-edit-full-stops ()
7129   "Interactively repair spacing at end of sentences."
7130   (interactive)
7131   (save-excursion
7132     (goto-char (point-min))
7133     (search-forward-regexp "^$" nil t)
7134     (let ((case-fold-search nil))
7135       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7136
7137 ;;;
7138 ;;; Article highlights
7139 ;;;
7140
7141 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7142
7143 ;;; Internal Variables:
7144
7145 (defcustom gnus-button-url-regexp
7146   (concat
7147    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7148    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7149    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7150    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7151        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7152              (punct "!?:;.,"))
7153          (concat
7154           "\\(?:"
7155           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7156           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7157           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7158           "\\|"
7159           "[" chars punct     "]+" "[" chars "]"
7160           "\\)"))
7161      (concat ;; XEmacs 21.4 doesn't support POSIX.
7162       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7163       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7164    "\\)")
7165   "Regular expression that matches URLs."
7166   :group 'gnus-article-buttons
7167   :type 'regexp)
7168
7169 (defcustom gnus-button-valid-fqdn-regexp
7170   message-valid-fqdn-regexp
7171   "Regular expression that matches a valid FQDN."
7172   :version "22.1"
7173   :group 'gnus-article-buttons
7174   :type 'regexp)
7175
7176 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7177 (defcustom gnus-button-valid-localpart-regexp
7178   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7179   "Regular expression that matches a localpart of mail addresses or MIDs."
7180   :version "22.1"
7181   :group 'gnus-article-buttons
7182   :type 'regexp)
7183
7184 (defcustom gnus-button-man-handler 'manual-entry
7185   "Function to use for displaying man pages.
7186 The function must take at least one argument with a string naming the
7187 man page."
7188   :version "22.1"
7189   :type '(choice (function-item :tag "Man" manual-entry)
7190                  (function-item :tag "Woman" woman)
7191                  (function :tag "Other"))
7192   :group 'gnus-article-buttons)
7193
7194 (defcustom gnus-button-mid-or-mail-regexp
7195   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7196           gnus-button-valid-fqdn-regexp
7197           ">?\\)\\b")
7198   "Regular expression that matches a message ID or a mail address."
7199   :version "22.1"
7200   :group 'gnus-article-buttons
7201   :type 'regexp)
7202
7203 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7204   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7205 Strings like this can be either a message ID or a mail address.  If it is one
7206 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7207 message ID or a mail address, respectively.  If this variable is set to the
7208 symbol `ask', always query the user what do do.  If it is a function, this
7209 function will be called with the string as its only argument.  The function
7210 must return `mid', `mail', `invalid' or `ask'."
7211   :version "22.1"
7212   :group 'gnus-article-buttons
7213   :type '(choice (function-item :tag "Heuristic function"
7214                                 gnus-button-mid-or-mail-heuristic)
7215                  (const ask)
7216                  (const mid)
7217                  (const mail)))
7218
7219 (defcustom gnus-button-mid-or-mail-heuristic-alist
7220   '((-10.0 . ".+\\$.+@")
7221     (-10.0 . "#")
7222     (-10.0 . "\\*")
7223     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7224     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7225     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7226     (-1.0  . "^[^a-z]+@")
7227     ;;
7228     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7229     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7230     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7231     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7232     ;;
7233     (-2.0  . "^[0-9]")
7234     (-1.0  . "^[0-9][0-9]")
7235     ;;
7236     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7237     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7238     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7239     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7240     ;;
7241     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7242     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7243     ;;       "[0-9]{8,}.*\@"
7244     (-3.0
7245      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7246     ;; "[0-9]{12,}.*\@"
7247     ;; compensation for TDMA dated mail addresses:
7248     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7249     ;;
7250     (-20.0 . "\\.fsf@") ;; Gnus
7251     (-20.0 . "^slrn")
7252     (-20.0 . "^Pine")
7253     (-20.0 . "^alpine\\.")
7254     (-20.0 . "_-_") ;; Subject change in thread
7255     ;;
7256     (-20.0 . "\\.ln@") ;; leafnode
7257     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7258     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7259     ;;
7260     ;; (5.0 . "") ;; $local_part_len <= 7
7261     (10.0  . "^[^0-9]+@")
7262     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7263     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7264     (3.0   . "\@stud")
7265     ;;
7266     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7267     ;;
7268     (0.5   . "^[A-Z][a-z]")
7269     (0.5   . "^[A-Z][a-z][a-z]")
7270     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7271     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7272   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7273
7274 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7275 address.  The REGEXP is processed with `case-fold-search' set to nil."
7276   :version "22.1"
7277   :group 'gnus-article-buttons
7278   :type '(repeat (cons (number :tag "Rate")
7279                        (regexp :tag "Regexp"))))
7280
7281 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7282   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7283 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7284 address, `ask' if unsure and `invalid' if the string is invalid."
7285   (let ((case-fold-search nil)
7286         (list gnus-button-mid-or-mail-heuristic-alist)
7287         (result 0) rate regexp lpartlen elem)
7288     (setq lpartlen
7289           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7290     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7291     ;; Certain special cases...
7292     (when (string-match
7293            (concat
7294             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7295             "^[0-9]+\\.[0-9]+@compuserve\\|"
7296             "@public\\.gmane\\.org")
7297            mid-or-mail)
7298       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7299       (setq result 'mail))
7300     (when (string-match "@.*@\\| " mid-or-mail)
7301       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7302       (setq result 'invalid))
7303     ;; Nothing more to do, if result is not a number here...
7304     (when (numberp result)
7305       (while list
7306         (setq elem (car list)
7307               rate (car elem)
7308               regexp (cdr elem)
7309               list (cdr list))
7310         (when (string-match regexp mid-or-mail)
7311           (setq result (+ result rate))
7312           (gnus-message
7313            9 "`%s' matched `%s', rate `%s', result `%s'."
7314            mid-or-mail regexp rate result)))
7315       (when (<= lpartlen 7)
7316         (setq result (+ result 5.0))
7317         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7318                       mid-or-mail result))
7319       (when (>= lpartlen 12)
7320         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7321         (cond
7322          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7323           ;; Long local part should contain realname if e-mail address,
7324           ;; too many digits: message-id.
7325           ;; $score -= 5.0 + 0.1 * $local_part_len;
7326           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7327           (setq result (+ result rate))
7328           (gnus-message
7329            9 "Many digits in `%s', rate `%s', result `%s'."
7330            mid-or-mail rate result))
7331          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7332                         mid-or-mail)
7333           ;; Too few vowels [^aeiouy]{4,}.*\@
7334           (setq result (+ result -5.0))
7335           (gnus-message
7336            9 "Few vowels in `%s', rate `%s', result `%s'."
7337            mid-or-mail -5.0 result))
7338          (t
7339           (setq result (+ result 5.0))
7340           (gnus-message
7341            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7342     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7343     ;; Maybe we should make this a customizable alist: (condition . 'result)
7344     (cond
7345      ((symbolp result) result)
7346      ;; Now convert number into proper results:
7347      ((< result -10.0) 'mid)
7348      ((> result  10.0) 'mail)
7349      (t 'ask))))
7350
7351 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7352   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7353          (url-mid (concat "news" ":" mid-or-mail))
7354          (url-mailto (concat "mailto" ":" mid-or-mail)))
7355     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7356     (when (fboundp pref)
7357       (setq guessed
7358             ;; get rid of surrounding angles...
7359             (funcall pref
7360                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7361       (if (or (eq 'mid guessed) (eq 'mail guessed))
7362           (setq pref guessed)
7363         (setq pref 'ask)))
7364     (if (eq pref 'ask)
7365         (save-window-excursion
7366           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7367               (setq pref 'mail)
7368             (setq pref 'mid))))
7369     (cond ((eq pref 'mid)
7370            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7371            (gnus-button-handle-news url-mid))
7372           ((eq pref 'mail)
7373            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7374            (gnus-url-mailto url-mailto))
7375           (t (gnus-message 3 "Invalid string.")))))
7376
7377 (defun gnus-button-handle-custom (fun arg)
7378   "Call function FUN on argument ARG.
7379 Both FUN and ARG are supposed to be strings.  ARG will be passed
7380 as a symbol to FUN."
7381   (funcall (intern fun)
7382            (if (string-match "^customize-apropos" fun)
7383                arg
7384              (intern arg))))
7385
7386 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7387
7388 (defun gnus-button-handle-describe-function (url)
7389   "Call `describe-function' when pushing the corresponding URL button."
7390   (describe-function
7391    (intern
7392     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7393
7394 (defun gnus-button-handle-describe-variable (url)
7395   "Call `describe-variable' when pushing the corresponding URL button."
7396   (describe-variable
7397    (intern
7398     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7399
7400 (defun gnus-button-handle-symbol (url)
7401 "Display help on variable or function.
7402 Calls `describe-variable' or `describe-function'."
7403   (let ((sym (intern url)))
7404     (cond
7405      ((fboundp sym) (describe-function sym))
7406      ((boundp sym) (describe-variable sym))
7407      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7408
7409 (defun gnus-button-handle-describe-key (url)
7410   "Call `describe-key' when pushing the corresponding URL button."
7411   (let* ((key-string
7412           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7413          (keys (ignore-errors (eval `(kbd ,key-string)))))
7414     (if keys
7415         (describe-key keys)
7416       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7417
7418 (defun gnus-button-handle-apropos (url)
7419   "Call `apropos' when pushing the corresponding URL button."
7420   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7421
7422 (defun gnus-button-handle-apropos-command (url)
7423   "Call `apropos' when pushing the corresponding URL button."
7424   (apropos-command
7425    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7426
7427 (defun gnus-button-handle-apropos-variable (url)
7428   "Call `apropos' when pushing the corresponding URL button."
7429   (funcall
7430    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7431    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7432
7433 (defun gnus-button-handle-apropos-documentation (url)
7434   "Call `apropos' when pushing the corresponding URL button."
7435   (funcall
7436    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7437    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7438
7439 (defun gnus-button-handle-library (url)
7440   "Call `locate-library' when pushing the corresponding URL button."
7441   (gnus-message 9 "url=`%s'" url)
7442   (let* ((lib (locate-library url))
7443          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7444     (if (not lib)
7445         (gnus-message 1 "Cannot locale library `%s'." url)
7446       (find-file-read-only file))))
7447
7448 (defcustom gnus-button-man-level 5
7449   "*Integer that says how many man-related buttons Gnus will show.
7450 The higher the number, the more buttons will appear and the more false
7451 positives are possible.  Note that you can set this variable local to
7452 specific groups.  Setting it higher in Unix groups is probably a good idea.
7453 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7454 how to set variables in specific groups."
7455   :version "22.1"
7456   :group 'gnus-article-buttons
7457   :link '(custom-manual "(gnus)Group Parameters")
7458   :type 'integer)
7459
7460 (defcustom gnus-button-emacs-level 5
7461   "*Integer that says how many emacs-related buttons Gnus will show.
7462 The higher the number, the more buttons will appear and the more false
7463 positives are possible.  Note that you can set this variable local to
7464 specific groups.  Setting it higher in Emacs or Gnus related groups is
7465 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7466 `gnus-parameters' on how to set variables in specific groups."
7467   :version "22.1"
7468   :group 'gnus-article-buttons
7469   :link '(custom-manual "(gnus)Group Parameters")
7470   :type 'integer)
7471
7472 (defcustom gnus-button-message-level 5
7473   "*Integer that says how many buttons for news or mail messages will appear.
7474 The higher the number, the more buttons will appear and the more false
7475 positives are possible."
7476   ;; mail addresses, MIDs, URLs for news, ...
7477   :version "22.1"
7478   :group 'gnus-article-buttons
7479   :type 'integer)
7480
7481 (defcustom gnus-button-browse-level 5
7482   "*Integer that says how many buttons for browsing will appear.
7483 The higher the number, the more buttons will appear and the more false
7484 positives are possible."
7485   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7486   :version "22.1"
7487   :group 'gnus-article-buttons
7488   :type 'integer)
7489
7490 (defcustom gnus-button-alist
7491   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7492      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7493     ((concat "\\b\\(nntp\\|news\\):\\("
7494              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7495      0 t gnus-button-handle-news 2)
7496     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7497      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7498     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7499      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7500     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7501     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7502      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7503     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7504      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7505     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7506      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7507     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7508      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7509     ;; RFC 2368 (The mailto URL scheme)
7510     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7511      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7512     ("\\bmailto:\\([^ \n\t]+\\)"
7513      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7514     ;; Info Konqueror style <info:/foo/bar baz>.
7515     ;; Must come before " Gnus home-grown style".
7516     ("\\binfo://?\\([^'\">\n\t]+\\)"
7517      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7518    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7519     ("\\binfo://\\([^'\">\n\t ]+\\)"
7520      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7521     ;; Info GNOME style <info:foo#bar_baz>
7522     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7523      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7524     ;; Info KDE style <info:(foo)bar baz>
7525     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7526      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7527     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7528      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7529     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n,]*\\)\\)?"
7530      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7531      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7532     ;; This is custom
7533     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7534      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7535     ;; Emacs help commands
7536     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7537      ;; regexp doesn't match arguments containing ` '.
7538      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7539     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7540      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7541     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7542      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7543     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7544      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7545     ;; The following entries may lead to many false positives so don't enable
7546     ;; them by default (use a high button level).
7547     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7548      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7549      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7550     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7551      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7552     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7553      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7554     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7555      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7556     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7557      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7558     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7559      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7560     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7561      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7562     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7563      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7564     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7565      ;; Unlike the other regexps we really have to require quoting
7566      ;; here to determine where it ends.
7567      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7568     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7569     ("<URL: *\\([^\n<>]*\\)>"
7570      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7571     ;; RFC 2396 (2.4.3., delims) ...
7572     ("\"URL: *\\([^\n\"]*\\)\""
7573      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7574     ;; Raw URLs.
7575     (gnus-button-url-regexp
7576      0 (>= gnus-button-browse-level 0) browse-url 0)
7577     ;; man pages
7578     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7579      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7580      gnus-button-handle-man 1)
7581     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7582     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7583      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7584      gnus-button-handle-man 1)
7585     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7586     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7587     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7588      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7589     ;; Recognizing patches to .el files.  This is somewhat obscure,
7590     ;; but considering the percentage of Gnus users who hack Emacs
7591     ;; Lisp files...
7592     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7593      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7594     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7595      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7596     ;; MID or mail: To avoid too many false positives we don't try to catch
7597     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7598     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7599     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7600     ;; so that non-ambiguous entries (see above) match first.
7601     (gnus-button-mid-or-mail-regexp
7602      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7603   "*Alist of regexps matching buttons in article bodies.
7604
7605 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7606 REGEXP: is the string (case insensitive) matching text around the button (can
7607 also be Lisp expression evaluating to a string),
7608 BUTTON: is the number of the regexp grouping actually matching the button,
7609 FORM: is a Lisp expression which must eval to true for the button to
7610 be added,
7611 CALLBACK: is the function to call when the user push this button, and each
7612 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7613
7614 CALLBACK can also be a variable, in that case the value of that
7615 variable it the real callback function."
7616   :group 'gnus-article-buttons
7617   :type '(repeat (list (choice regexp variable sexp)
7618                        (integer :tag "Button")
7619                        (sexp :tag "Form")
7620                        (function :tag "Callback")
7621                        (repeat :tag "Par"
7622                                :inline t
7623                                (integer :tag "Regexp group")))))
7624 (put 'gnus-button-alist 'risky-local-variable t)
7625
7626 (defcustom gnus-header-button-alist
7627   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7628      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7629     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7630      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7631     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7632      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7633     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7634      0 (>= gnus-button-browse-level 0) browse-url 0)
7635     ("^Subject:" gnus-button-url-regexp
7636      0 (>= gnus-button-browse-level 0) browse-url 0)
7637     ("^[^:]+:" gnus-button-url-regexp
7638      0 (>= gnus-button-browse-level 0) browse-url 0)
7639     ("^OpenPGP:.*url=" gnus-button-url-regexp
7640      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7641     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7642      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7643     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7644      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7645   "*Alist of headers and regexps to match buttons in article heads.
7646
7647 This alist is very similar to `gnus-button-alist', except that each
7648 alist has an additional HEADER element first in each entry:
7649
7650 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7651
7652 HEADER is a regexp to match a header.  For a fuller explanation, see
7653 `gnus-button-alist'."
7654   :group 'gnus-article-buttons
7655   :group 'gnus-article-headers
7656   :type '(repeat (list (regexp :tag "Header")
7657                        (choice regexp variable)
7658                        (integer :tag "Button")
7659                        (sexp :tag "Form")
7660                        (function :tag "Callback")
7661                        (repeat :tag "Par"
7662                                :inline t
7663                                (integer :tag "Regexp group")))))
7664 (put 'gnus-header-button-alist 'risky-local-variable t)
7665
7666 ;;; Commands:
7667
7668 (defun gnus-article-push-button (event)
7669   "Check text under the mouse pointer for a callback function.
7670 If the text under the mouse pointer has a `gnus-callback' property,
7671 call it with the value of the `gnus-data' text property."
7672   (interactive "e")
7673   (set-buffer (window-buffer (posn-window (event-start event))))
7674   (let* ((pos (posn-point (event-start event)))
7675          (data (get-text-property pos 'gnus-data))
7676          (fun (get-text-property pos 'gnus-callback)))
7677     (goto-char pos)
7678     (when fun
7679       (funcall fun data))))
7680
7681 (defun gnus-article-press-button ()
7682   "Check text at point for a callback function.
7683 If the text at point has a `gnus-callback' property,
7684 call it with the value of the `gnus-data' text property."
7685   (interactive)
7686   (let ((data (get-text-property (point) 'gnus-data))
7687         (fun (get-text-property (point) 'gnus-callback)))
7688     (when fun
7689       (funcall fun data))))
7690
7691 (defun gnus-article-highlight (&optional force)
7692   "Highlight current article.
7693 This function calls `gnus-article-highlight-headers',
7694 `gnus-article-highlight-citation',
7695 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7696 do the highlighting.  See the documentation for those functions."
7697   (interactive (list 'force))
7698   (gnus-article-highlight-headers)
7699   (gnus-article-highlight-citation force)
7700   (gnus-article-highlight-signature)
7701   (gnus-article-add-buttons)
7702   (gnus-article-add-buttons-to-head))
7703
7704 (defun gnus-article-highlight-some (&optional force)
7705   "Highlight current article.
7706 This function calls `gnus-article-highlight-headers',
7707 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7708 do the highlighting.  See the documentation for those functions."
7709   (interactive (list 'force))
7710   (gnus-article-highlight-headers)
7711   (gnus-article-highlight-signature)
7712   (gnus-article-add-buttons))
7713
7714 (defun gnus-article-highlight-headers ()
7715   "Highlight article headers as specified by `gnus-header-face-alist'."
7716   (interactive)
7717   (gnus-with-article-headers
7718     (let (regexp header-face field-face from hpoints fpoints)
7719       (dolist (entry gnus-header-face-alist)
7720         (goto-char (point-min))
7721         (setq regexp (concat "^\\("
7722                              (if (string-equal "" (nth 0 entry))
7723                                  "[^\t ]"
7724                                (nth 0 entry))
7725                              "\\)")
7726               header-face (nth 1 entry)
7727               field-face (nth 2 entry))
7728         (while (and (re-search-forward regexp nil t)
7729                     (not (eobp)))
7730           (beginning-of-line)
7731           (setq from (point))
7732           (unless (search-forward ":" nil t)
7733             (forward-char 1))
7734           (when (and header-face
7735                      (not (memq (point) hpoints)))
7736             (push (point) hpoints)
7737             (gnus-put-text-property from (point) 'face header-face))
7738           (when (and field-face
7739                      (not (memq (setq from (point)) fpoints)))
7740             (push from fpoints)
7741             (if (re-search-forward "^[^ \t]" nil t)
7742                 (forward-char -2)
7743               (goto-char (point-max)))
7744             (gnus-put-text-property from (point) 'face field-face)))))))
7745
7746 (defun gnus-article-highlight-signature ()
7747   "Highlight the signature in an article.
7748 It does this by highlighting everything after
7749 `gnus-signature-separator' using the face `gnus-signature'."
7750   (interactive)
7751   (gnus-with-article-buffer
7752     (let ((inhibit-point-motion-hooks t))
7753       (save-restriction
7754         (when (and gnus-signature-face
7755                    (gnus-article-narrow-to-signature))
7756           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7757                             'face gnus-signature-face)
7758           (widen)
7759           (gnus-article-search-signature)
7760           (let ((start (match-beginning 0))
7761                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7762             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7763                                      end)))))))
7764
7765 (defun gnus-button-in-region-p (b e prop)
7766   "Say whether PROP exists in the region."
7767   (text-property-not-all b e prop nil))
7768
7769 (defun gnus-article-add-buttons ()
7770   "Find external references in the article and make buttons of them.
7771 \"External references\" are things like Message-IDs and URLs, as
7772 specified by `gnus-button-alist'."
7773   (interactive)
7774   (gnus-with-article-buffer
7775     (let ((inhibit-point-motion-hooks t)
7776           (case-fold-search t)
7777           (alist gnus-button-alist)
7778           beg entry regexp)
7779       ;; We skip the headers.
7780       (article-goto-body)
7781       (setq beg (point))
7782       (while (setq entry (pop alist))
7783         (setq regexp (eval (car entry)))
7784         (goto-char beg)
7785         (while (re-search-forward regexp nil t)
7786           (let ((start (match-beginning (nth 1 entry)))
7787                 (end (match-end (nth 1 entry)))
7788                 (from (match-beginning 0)))
7789             (when (and (eval (nth 2 entry))
7790                        (not (gnus-button-in-region-p
7791                              start end 'gnus-callback)))
7792               ;; That optional form returned non-nil, so we add the
7793               ;; button.
7794               (setq from (set-marker (make-marker) from))
7795               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7796                            (gnus-article-extend-url-button from start end))
7797                 (gnus-article-add-button start end
7798                                          'gnus-button-push (list from entry))
7799                 (gnus-put-text-property
7800                  start end
7801                  'gnus-string (buffer-substring-no-properties
7802                                start end))))))))))
7803
7804 (defun gnus-article-extend-url-button (beg start end)
7805   "Extend url button if url is folded into two or more lines.
7806 Return non-nil if button is extended.  BEG is a marker that points to
7807 the beginning position of a text containing url.  START and END are
7808 the endpoints of a url button before it is extended.  The concatenated
7809 url is put as the `gnus-button-url' overlay property on the button."
7810   (let ((opoint (point))
7811         (points (list start end))
7812         url delim regexp)
7813     (prog1
7814         (when (and (progn
7815                      (goto-char end)
7816                      (not (looking-at "[\t ]*[\">]")))
7817                    (progn
7818                      (goto-char start)
7819                      (string-match
7820                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7821                       (buffer-substring (point-at-bol) start)))
7822                    (progn
7823                      (setq url (list (buffer-substring start end))
7824                            delim (if (match-beginning 1) ">" "\""))
7825                      (beginning-of-line)
7826                      (setq regexp (concat
7827                                    (when (and (looking-at
7828                                                message-cite-prefix-regexp)
7829                                               (< (match-end 0) start))
7830                                      (regexp-quote (match-string 0)))
7831                                    "\
7832 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7833                                    delim "\\)"))
7834                      (while (progn
7835                               (forward-line 1)
7836                               (and (looking-at regexp)
7837                                    (prog1
7838                                        (match-beginning 1)
7839                                      (push (or (match-string 2)
7840                                                (match-string 1))
7841                                            url)
7842                                      (push (setq end (or (match-end 2)
7843                                                          (match-end 1)))
7844                                            points)
7845                                      (push (or (match-beginning 2)
7846                                                (match-beginning 1))
7847                                            points)))))
7848                      (match-beginning 2)))
7849           (let (gnus-article-mouse-face widget-mouse-face)
7850             (while points
7851               (gnus-article-add-button (pop points) (pop points)
7852                                        'gnus-button-push beg)))
7853           (let ((overlay (gnus-make-overlay start end)))
7854             (gnus-overlay-put overlay 'evaporate t)
7855             (gnus-overlay-put overlay 'gnus-button-url
7856                               (list (mapconcat 'identity (nreverse url) "")))
7857             (when gnus-article-mouse-face
7858               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7859           t)
7860       (goto-char opoint))))
7861
7862 ;; Add buttons to the head of an article.
7863 (defun gnus-article-add-buttons-to-head ()
7864   "Add buttons to the head of the article."
7865   (interactive)
7866   (gnus-with-article-headers
7867     (let (beg end)
7868       (dolist (entry gnus-header-button-alist)
7869         ;; Each alist entry.
7870         (goto-char (point-min))
7871         (while (re-search-forward (car entry) nil t)
7872           ;; Each header matching the entry.
7873           (setq beg (match-beginning 0))
7874           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7875                              (match-beginning 0))
7876                         (point-max)))
7877           (goto-char beg)
7878           (while (re-search-forward (eval (nth 1 entry)) end t)
7879             ;; Each match within a header.
7880             (let* ((entry (cdr entry))
7881                    (start (match-beginning (nth 1 entry)))
7882                    (end (match-end (nth 1 entry)))
7883                    (form (nth 2 entry)))
7884               (goto-char (match-end 0))
7885               (when (eval form)
7886                 (gnus-article-add-button
7887                  start end (nth 3 entry)
7888                  (buffer-substring (match-beginning (nth 4 entry))
7889                                    (match-end (nth 4 entry)))))))
7890           (goto-char end))))))
7891
7892 ;;; External functions:
7893
7894 (defun gnus-article-add-button (from to fun &optional data text)
7895   "Create a button between FROM and TO with callback FUN and data DATA."
7896   (when gnus-article-button-face
7897     (gnus-overlay-put (gnus-make-overlay from to nil t)
7898                       'face gnus-article-button-face))
7899   (gnus-add-text-properties
7900    from to
7901    (nconc (and gnus-article-mouse-face
7902                (list gnus-mouse-face-prop gnus-article-mouse-face))
7903           (list 'gnus-callback fun)
7904           (and data (list 'gnus-data data))))
7905   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7906                          :help-echo (or text "Follow the link")
7907                          :keymap gnus-url-button-map
7908                          :button-keymap gnus-widget-button-keymap))
7909
7910 (defun gnus-article-copy-string ()
7911   "Copy the string in the button to the kill ring."
7912   (interactive)
7913   (gnus-article-check-buffer)
7914   (let ((data (get-text-property (point) 'gnus-string)))
7915     (when data
7916       (with-temp-buffer
7917         (insert data)
7918         (copy-region-as-kill (point-min) (point-max))
7919         (message "Copied %s" data)))))
7920
7921 ;;; Internal functions:
7922
7923 (defun gnus-article-set-globals ()
7924   (with-current-buffer gnus-summary-buffer
7925     (gnus-set-global-variables)))
7926
7927 (defun gnus-signature-toggle (end)
7928   (gnus-with-article-buffer
7929     (let ((inhibit-point-motion-hooks t))
7930       (if (text-property-any end (point-max) 'article-type 'signature)
7931           (progn
7932             (gnus-delete-wash-type 'signature)
7933             (gnus-remove-text-properties-when
7934              'article-type 'signature end (point-max)
7935              (cons 'article-type (cons 'signature
7936                                        gnus-hidden-properties))))
7937         (gnus-add-wash-type 'signature)
7938         (gnus-add-text-properties-when
7939          'article-type nil end (point-max)
7940          (cons 'article-type (cons 'signature
7941                                    gnus-hidden-properties)))))
7942     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7943       (gnus-set-mode-line 'article))))
7944
7945 (defun gnus-button-push (marker-and-entry)
7946   ;; Push button starting at MARKER.
7947   (save-excursion
7948     (let* ((marker (car marker-and-entry))
7949            (entry (cadr marker-and-entry))
7950            (regexp (car entry))
7951            (inhibit-point-motion-hooks t))
7952       (goto-char marker)
7953       ;; This is obviously true, or something bad is happening :)
7954       ;; But we need it to have the match-data
7955       (when (looking-at (or (if (symbolp regexp)
7956                                 (symbol-value regexp)
7957                               regexp)))
7958         (let ((fun (nth 3 entry))
7959               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7960                              (get-char-property marker 'gnus-button-url))
7961                         (mapcar (lambda (group)
7962                                   (let ((string (match-string group)))
7963                                     (set-text-properties
7964                                      0 (length string) nil string)
7965                                     string))
7966                                 (nthcdr 4 entry)))))
7967
7968           (cond
7969            ((fboundp fun)
7970             (apply fun args))
7971            ((and (boundp fun)
7972                  (fboundp (symbol-value fun)))
7973             (apply (symbol-value fun) args))
7974            (t
7975             (gnus-message 1 "You must define `%S' to use this button"
7976                           (cons fun args)))))))))
7977
7978 (defun gnus-parse-news-url (url)
7979   (let (scheme server port group message-id articles)
7980     (with-temp-buffer
7981       (insert url)
7982       (goto-char (point-min))
7983       (when (looking-at "\\([A-Za-z]+\\):")
7984         (setq scheme (match-string 1))
7985         (goto-char (match-end 0)))
7986       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7987         (setq server (match-string 1))
7988         (setq port (if (stringp (match-string 3))
7989                        (string-to-number (match-string 3))
7990                      (match-string 3)))
7991         (goto-char (match-end 0)))
7992
7993       (cond
7994        ((looking-at "\\(.*@.*\\)")
7995         (setq message-id (match-string 1)))
7996        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7997         (setq group (match-string 1)
7998               articles (split-string (match-string 2) "-")))
7999        ((looking-at "\\([^/]+\\)/?")
8000         (setq group (match-string 1)))
8001        (t
8002         (error "Unknown news URL syntax"))))
8003     (list scheme server port group message-id articles)))
8004
8005 (defun gnus-button-handle-news (url)
8006   "Fetch a news URL."
8007   (destructuring-bind (scheme server port group message-id articles)
8008       (gnus-parse-news-url url)
8009     (cond
8010      (message-id
8011       (with-current-buffer gnus-summary-buffer
8012         (if server
8013             (let ((gnus-refer-article-method
8014                    (nconc (list (list 'nntp server))
8015                           gnus-refer-article-method))
8016                   (nntp-port-number (or port "nntp")))
8017               (gnus-message 7 "Fetching %s with %s"
8018                             message-id gnus-refer-article-method)
8019               (gnus-summary-refer-article message-id))
8020           (gnus-summary-refer-article message-id))))
8021      (group
8022       (gnus-button-fetch-group url)))))
8023
8024 (defun gnus-button-patch (library line)
8025   "Visit an Emacs Lisp library LIBRARY on line LINE."
8026   (interactive)
8027   (let ((file (locate-library (file-name-nondirectory library))))
8028     (unless file
8029       (error "Couldn't find library %s" library))
8030     (find-file file)
8031     (goto-char (point-min))
8032     (forward-line (1- (string-to-number line)))))
8033
8034 (defun gnus-button-handle-man (url)
8035   "Fetch a man page."
8036   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8037   (when (eq gnus-button-man-handler 'woman)
8038     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8039   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8040   (funcall gnus-button-man-handler url))
8041
8042 (defun gnus-button-handle-info-url (url)
8043   "Fetch an info URL."
8044   (setq url (mm-subst-char-in-string ?+ ?\  url))
8045   (cond
8046    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8047     (gnus-info-find-node
8048      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8049                      "Gnus")
8050              ")" (gnus-url-unhex-string (match-string 2 url)))))
8051    ((string-match "([^)\"]+)[^\"]+" url)
8052     (setq url
8053           (gnus-replace-in-string
8054            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8055     (gnus-info-find-node url))
8056    (t (error "Can't parse %s" url))))
8057
8058 (defun gnus-button-handle-info-url-gnome (url)
8059   "Fetch GNOME style info URL."
8060   (setq url (mm-subst-char-in-string ?_ ?\  url))
8061   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8062       (gnus-info-find-node
8063        (concat "("
8064                (gnus-url-unhex-string
8065                  (match-string 1 url))
8066                ")"
8067                (or (gnus-url-unhex-string
8068                     (match-string 2 url))
8069                    "Top")))
8070     (error "Can't parse %s" url)))
8071
8072 (defun gnus-button-handle-info-url-kde (url)
8073   "Fetch KDE style info URL."
8074   (gnus-info-find-node (gnus-url-unhex-string url)))
8075
8076 ;; (info) will autoload info.el
8077 (declare-function Info-menu "info" (menu-item &optional fork))
8078 (declare-function Info-index-next "info" (num))
8079
8080 (defun gnus-button-handle-info-keystrokes (url)
8081   "Call `info' when pushing the corresponding URL button."
8082   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8083   (let (node indx comma)
8084     (if (string-match
8085          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8086                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8087                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8088                  "\\(?:[ \t\n,]*\\)\\)?")
8089          url)
8090         (setq node (match-string 2 url)
8091               indx (match-string 3 url))
8092       (error "Can't parse %s" url))
8093     (info)
8094     (Info-directory)
8095     (Info-menu node)
8096     (when (> (length indx) 0)
8097       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8098                             "\\([ \t\n,]*\\)")
8099                     indx)
8100       (setq comma (match-string 2 indx))
8101       (setq indx  (match-string 1 indx))
8102       (Info-index indx)
8103       (when comma
8104         (dotimes (i (with-temp-buffer
8105                       (insert comma)
8106                       ;; Note: the XEmacs version of `how-many' takes
8107                       ;; no optional argument.
8108                       (goto-char (point-min))
8109                       (how-many ",")))
8110           (Info-index-next 1)))
8111       nil)))
8112
8113 (autoload 'pgg-snarf-keys-region "pgg")
8114 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8115 (declare-function pgg-display-output-buffer "pgg" (start end status))
8116
8117 (defun gnus-button-openpgp (url)
8118   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8119   (with-temp-buffer
8120     (mm-url-insert-file-contents-external url)
8121     (pgg-snarf-keys-region (point-min) (point-max))
8122     (pgg-display-output-buffer nil nil nil)))
8123
8124 (defun gnus-button-message-id (message-id)
8125   "Fetch MESSAGE-ID."
8126   (with-current-buffer gnus-summary-buffer
8127     (gnus-summary-refer-article message-id)))
8128
8129 (defun gnus-button-fetch-group (address &rest ignore)
8130   "Fetch GROUP specified by ADDRESS."
8131   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8132                       address)
8133     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8134     ;; for nntp:// and news://
8135     (setq address (match-string 3 address)))
8136   (if (not (string-match "[:/]" address))
8137       ;; This is just a simple group url.
8138       (gnus-group-read-ephemeral-group address gnus-select-method)
8139     (if (not
8140          (string-match
8141           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8142           address))
8143         (error "Can't parse %s" address)
8144       (gnus-group-read-ephemeral-group
8145        (match-string 4 address)
8146        `(nntp ,(match-string 1 address)
8147               (nntp-address ,(match-string 1 address))
8148               (nntp-port-number ,(if (match-end 3)
8149                                      (match-string 3 address)
8150                                    "nntp")))
8151        nil nil nil
8152        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8153
8154 (defun gnus-url-parse-query-string (query &optional downcase)
8155   (let (retval pairs cur key val)
8156     (setq pairs (split-string query "&"))
8157     (while pairs
8158       (setq cur (car pairs)
8159             pairs (cdr pairs))
8160       (if (not (string-match "=" cur))
8161           nil                           ; Grace
8162         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8163               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8164         (if downcase
8165             (setq key (downcase key)))
8166         (setq cur (assoc key retval))
8167         (if cur
8168             (setcdr cur (cons val (cdr cur)))
8169           (setq retval (cons (list key val) retval)))))
8170     retval))
8171
8172 (defun gnus-url-mailto (url)
8173   ;; Send mail to someone
8174   (setq url (replace-regexp-in-string "\n" " " url))
8175   (when (string-match "mailto:/*\\(.*\\)" url)
8176     (setq url (substring url (match-beginning 1) nil)))
8177   (let (to args subject func)
8178     (setq args (gnus-url-parse-query-string
8179                 (if (string-match "^\\?" url)
8180                     (substring url 1)
8181                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8182                       (concat "to=" (match-string 1 url) "&"
8183                               (match-string 2 url))
8184                     (concat "to=" url))))
8185           subject (cdr-safe (assoc "subject" args)))
8186     (gnus-msg-mail)
8187     (while args
8188       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8189       (if (fboundp func)
8190           (funcall func)
8191         (message-position-on-field (caar args)))
8192       (insert (gnus-replace-in-string
8193                (mapconcat 'identity (reverse (cdar args)) ", ")
8194                "\r\n" "\n" t))
8195       (setq args (cdr args)))
8196     (if subject
8197         (message-goto-body)
8198       (message-goto-subject))))
8199
8200 (defun gnus-button-embedded-url (address)
8201   "Activate ADDRESS with `browse-url'."
8202   (browse-url (gnus-strip-whitespace address)))
8203
8204 ;;; Next/prev buttons in the article buffer.
8205
8206 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8207 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8208
8209 (defvar gnus-prev-page-map
8210   (let ((map (make-sparse-keymap)))
8211     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8212     (define-key map "\r" 'gnus-button-prev-page)
8213     map))
8214
8215 (defvar gnus-next-page-map
8216   (let ((map (make-sparse-keymap)))
8217     (define-key map gnus-mouse-2 'gnus-button-next-page)
8218     (define-key map "\r" 'gnus-button-next-page)
8219     map))
8220
8221 (defun gnus-insert-prev-page-button ()
8222   (let ((b (point)) e
8223         (inhibit-read-only t))
8224     (gnus-eval-format
8225      gnus-prev-page-line-format nil
8226      `(keymap ,gnus-prev-page-map
8227               gnus-prev t
8228               gnus-callback gnus-article-button-prev-page
8229               article-type annotation))
8230     (setq e (if (bolp)
8231                 ;; Exclude a newline.
8232                 (1- (point))
8233               (point)))
8234     (when gnus-article-button-face
8235       (gnus-overlay-put (gnus-make-overlay b e nil t)
8236                         'face gnus-article-button-face))
8237     (widget-convert-button
8238      'link b e
8239      :action 'gnus-button-prev-page
8240      :button-keymap gnus-prev-page-map)))
8241
8242 (defun gnus-button-next-page (&optional args more-args)
8243   "Go to the next page."
8244   (interactive)
8245   (let ((win (selected-window)))
8246     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8247     (gnus-article-next-page)
8248     (select-window win)))
8249
8250 (defun gnus-button-prev-page (&optional args more-args)
8251   "Go to the prev page."
8252   (interactive)
8253   (let ((win (selected-window)))
8254     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8255     (gnus-article-prev-page)
8256     (select-window win)))
8257
8258 (defun gnus-insert-next-page-button ()
8259   (let ((b (point)) e
8260         (inhibit-read-only t))
8261     (gnus-eval-format gnus-next-page-line-format nil
8262                       `(keymap ,gnus-next-page-map
8263                                gnus-next t
8264                                gnus-callback gnus-article-button-next-page
8265                                article-type annotation))
8266     (setq e (if (bolp)
8267                 ;; Exclude a newline.
8268                 (1- (point))
8269               (point)))
8270     (when gnus-article-button-face
8271       (gnus-overlay-put (gnus-make-overlay b e nil t)
8272                         'face gnus-article-button-face))
8273     (widget-convert-button
8274      'link b e
8275      :action 'gnus-button-next-page
8276      :button-keymap gnus-next-page-map)))
8277
8278 (defun gnus-article-button-next-page (arg)
8279   "Go to the next page."
8280   (interactive "P")
8281   (let ((win (selected-window)))
8282     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8283     (gnus-article-next-page)
8284     (select-window win)))
8285
8286 (defun gnus-article-button-prev-page (arg)
8287   "Go to the prev page."
8288   (interactive "P")
8289   (let ((win (selected-window)))
8290     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8291     (gnus-article-prev-page)
8292     (select-window win)))
8293
8294 (defvar gnus-decode-header-methods
8295   '(mail-decode-encoded-word-region)
8296   "List of methods used to decode headers.
8297
8298 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8299 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8300 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8301 whose names match REGEXP.
8302
8303 For example:
8304 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8305  mail-decode-encoded-word-region
8306  (\"chinese\" . rfc1843-decode-region))
8307 ")
8308
8309 (defvar gnus-decode-header-methods-cache nil)
8310
8311 (defun gnus-multi-decode-header (start end)
8312   "Apply the functions from `gnus-encoded-word-methods' that match."
8313   (unless (and gnus-decode-header-methods-cache
8314                (eq gnus-newsgroup-name
8315                    (car gnus-decode-header-methods-cache)))
8316     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8317     (dolist (x gnus-decode-header-methods)
8318       (if (symbolp x)
8319           (nconc gnus-decode-header-methods-cache (list x))
8320         (if (and gnus-newsgroup-name
8321                  (string-match (car x) gnus-newsgroup-name))
8322             (nconc gnus-decode-header-methods-cache
8323                    (list (cdr x)))))))
8324   (let ((xlist gnus-decode-header-methods-cache))
8325     (pop xlist)
8326     (save-restriction
8327       (narrow-to-region start end)
8328       (while xlist
8329         (funcall (pop xlist) (point-min) (point-max))))))
8330
8331 ;;;
8332 ;;; Treatment top-level handling.
8333 ;;;
8334
8335 (defvar gnus-inhibit-article-treatments nil)
8336
8337 (defun gnus-treat-article (gnus-treat-condition
8338                            &optional part-number total-parts gnus-treat-type)
8339   (let ((gnus-treat-length (- (point-max) (point-min)))
8340         (alist gnus-treatment-function-alist)
8341         (article-goto-body-goes-to-point-min-p t)
8342         (treated-type
8343          (or (not gnus-treat-type)
8344              (catch 'found
8345                (let ((list gnus-article-treat-types))
8346                  (while list
8347                    (when (string-match (pop list) gnus-treat-type)
8348                      (throw 'found t)))))))
8349         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8350         val elem)
8351     (gnus-run-hooks 'gnus-part-display-hook)
8352     (dolist (elem alist)
8353       (setq val
8354             (save-excursion
8355               (when (gnus-buffer-live-p gnus-summary-buffer)
8356                 (set-buffer gnus-summary-buffer))
8357               (symbol-value (car elem))))
8358       (when (and (or (consp val)
8359                      treated-type)
8360                  (or (not gnus-inhibit-article-treatments)
8361                      (eq gnus-treat-condition 'head))
8362                  (gnus-treat-predicate val)
8363                  (or (not (get (car elem) 'highlight))
8364                      highlightp))
8365         (save-restriction
8366           (funcall (cadr elem)))))))
8367
8368 ;; Dynamic variables.
8369 (defvar part-number)
8370 (defvar total-parts)
8371 (defvar gnus-treat-type)
8372 (defvar gnus-treat-condition)
8373 (defvar gnus-treat-length)
8374
8375 (defun gnus-treat-predicate (val)
8376   (cond
8377    ((null val)
8378     nil)
8379    (gnus-treat-condition
8380     (eq gnus-treat-condition val))
8381    ((and (listp val)
8382          (stringp (car val)))
8383     (apply 'gnus-or (mapcar `(lambda (s)
8384                                (string-match s ,(or gnus-newsgroup-name "")))
8385                             val)))
8386    ((listp val)
8387     (let ((pred (pop val)))
8388       (cond
8389        ((eq pred 'or)
8390         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8391        ((eq pred 'and)
8392         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8393        ((eq pred 'not)
8394         (not (gnus-treat-predicate (car val))))
8395        ((eq pred 'typep)
8396         (equal (car val) gnus-treat-type))
8397        (t
8398         (error "%S is not a valid predicate" pred)))))
8399    ((eq val t)
8400     t)
8401    ((eq val 'head)
8402     nil)
8403    ((eq val 'first)
8404     (eq part-number 1))
8405    ((eq val 'last)
8406     (eq part-number total-parts))
8407    ((numberp val)
8408     (< gnus-treat-length val))
8409    (t
8410     (error "%S is not a valid value" val))))
8411
8412 (defun gnus-article-encrypt-body (protocol &optional n)
8413   "Encrypt the article body."
8414   (interactive
8415    (list
8416     (or gnus-article-encrypt-protocol
8417         (gnus-completing-read "Encrypt protocol"
8418                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8419                               t))
8420     current-prefix-arg))
8421   ;; User might hit `K E' instead of `K e', so prompt once.
8422   (when (and gnus-article-encrypt-protocol
8423              gnus-novice-user)
8424     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8425       (error "Encrypt aborted")))
8426   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8427     (unless func
8428       (error "Can't find the encrypt protocol %s" protocol))
8429     (if (member gnus-newsgroup-name '("nndraft:delayed"
8430                                       "nndraft:drafts"
8431                                       "nndraft:queue"))
8432         (error "Can't encrypt the article in group %s"
8433                gnus-newsgroup-name))
8434     (gnus-summary-iterate n
8435       (with-current-buffer gnus-summary-buffer
8436         (let ((mail-parse-charset gnus-newsgroup-charset)
8437               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8438               (summary-buffer gnus-summary-buffer)
8439               references point)
8440           (gnus-set-global-variables)
8441           (when (gnus-group-read-only-p)
8442             (error "The current newsgroup does not support article encrypt"))
8443           (gnus-summary-show-article t)
8444           (setq references
8445               (or (mail-header-references gnus-current-headers) ""))
8446           (set-buffer gnus-article-buffer)
8447           (let* ((inhibit-read-only t)
8448                  (headers
8449                   (mapcar (lambda (field)
8450                             (and (save-restriction
8451                                    (message-narrow-to-head)
8452                                    (goto-char (point-min))
8453                                    (search-forward field nil t))
8454                                  (prog2
8455                                      (message-narrow-to-field)
8456                                      (buffer-string)
8457                                    (delete-region (point-min) (point-max))
8458                                    (widen))))
8459                           '("Content-Type:" "Content-Transfer-Encoding:"
8460                             "Content-Disposition:"))))
8461             (message-narrow-to-head)
8462             (message-remove-header "MIME-Version")
8463             (goto-char (point-max))
8464             (setq point (point))
8465             (insert (apply 'concat headers))
8466             (widen)
8467             (narrow-to-region point (point-max))
8468             (let ((message-options message-options))
8469               (message-options-set 'message-sender user-mail-address)
8470               (message-options-set 'message-recipients user-mail-address)
8471               (message-options-set 'message-sign-encrypt 'not)
8472               (funcall func))
8473             (goto-char (point-min))
8474             (insert "MIME-Version: 1.0\n")
8475             (widen)
8476             (gnus-summary-edit-article-done
8477              references nil summary-buffer t))
8478           (when gnus-keep-backlog
8479             (gnus-backlog-remove-article
8480              (car gnus-article-current) (cdr gnus-article-current)))
8481           (gnus-flush-original-article-buffer)
8482           (when gnus-use-cache
8483             (gnus-cache-update-article
8484              (car gnus-article-current) (cdr gnus-article-current))))))))
8485
8486 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8487   "The following specs can be used:
8488 %t  The security MIME type
8489 %i  Additional info
8490 %d  Details
8491 %D  Details if button is pressed")
8492
8493 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8494   "The following specs can be used:
8495 %t  The security MIME type
8496 %i  Additional info
8497 %d  Details
8498 %D  Details if button is pressed")
8499
8500 (defvar gnus-mime-security-button-line-format-alist
8501   '((?t gnus-tmp-type ?s)
8502     (?i gnus-tmp-info ?s)
8503     (?d gnus-tmp-details ?s)
8504     (?D gnus-tmp-pressed-details ?s)))
8505
8506 (defvar gnus-mime-security-button-commands
8507   '((gnus-article-press-button "\r" "Show Detail")
8508     (undefined "v")
8509     (undefined "t")
8510     (undefined "C")
8511     (gnus-mime-security-save-part "o" "Save...")
8512     (undefined "\C-o")
8513     (undefined "r")
8514     (undefined "d")
8515     (undefined "c")
8516     (undefined "i")
8517     (undefined "E")
8518     (undefined "e")
8519     (undefined "p")
8520     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8521     (undefined ".")))
8522
8523 (defvar gnus-mime-security-button-map
8524   (let ((map (make-sparse-keymap)))
8525     (define-key map gnus-mouse-2 'gnus-article-push-button)
8526     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8527     (dolist (c gnus-mime-security-button-commands)
8528       (define-key map (cadr c) (car c)))
8529     map))
8530
8531 (easy-menu-define
8532   gnus-mime-security-button-menu gnus-mime-security-button-map
8533   "Security button menu."
8534   `("Security Part"
8535     ,@(delq nil
8536             (mapcar (lambda (c)
8537                       (unless (eq (car c) 'undefined)
8538                         (vector (caddr c) (car c) :active t)))
8539                     gnus-mime-security-button-commands))))
8540
8541 (defun gnus-mime-security-button-menu (event prefix)
8542   "Construct a context-sensitive menu of security commands."
8543   (interactive "e\nP")
8544   (save-window-excursion
8545     (let ((pos (event-start event)))
8546       (select-window (posn-window pos))
8547       (goto-char (posn-point pos))
8548       (gnus-article-check-buffer)
8549       (popup-menu gnus-mime-security-button-menu nil prefix))))
8550
8551 (defvar gnus-mime-security-details-buffer nil)
8552
8553 (defvar gnus-mime-security-button-pressed nil)
8554
8555 (defvar gnus-mime-security-show-details-inline t
8556   "If non-nil, show details in the article buffer.")
8557
8558 (defun gnus-mime-security-verify-or-decrypt (handle)
8559   (mm-remove-parts (cdr handle))
8560   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8561         point (inhibit-read-only t))
8562     (if region
8563         (goto-char (car region)))
8564     (setq point (point))
8565     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8566       (let* ((mm-verify-option 'known)
8567              (mm-decrypt-option 'known)
8568              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8569         (unless (eq nparts (cdr handle))
8570           (mm-destroy-parts (cdr handle))
8571           (setcdr handle nparts))))
8572     (gnus-mime-display-security handle)
8573     (when region
8574       (delete-region (point) (cdr region))
8575       (set-marker (car region) nil)
8576       (set-marker (cdr region) nil))
8577     (goto-char point)))
8578
8579 (defun gnus-mime-security-show-details (handle)
8580   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8581     (if (not details)
8582         (gnus-message 5 "No details.")
8583       (if gnus-mime-security-show-details-inline
8584           (let ((gnus-mime-security-button-pressed
8585                  (not (get-text-property (point) 'gnus-mime-details)))
8586                 (gnus-mime-security-button-line-format
8587                  (get-text-property (point) 'gnus-line-format))
8588                 (inhibit-read-only t))
8589             (forward-char -1)
8590             (while (eq (get-text-property (point) 'gnus-line-format)
8591                        gnus-mime-security-button-line-format)
8592               (forward-char -1))
8593             (forward-char)
8594             (save-restriction
8595               (narrow-to-region (point) (point))
8596               (gnus-insert-mime-security-button handle))
8597             (delete-region (point)
8598                            (or (text-property-not-all
8599                                 (point) (point-max)
8600                                 'gnus-line-format
8601                                 gnus-mime-security-button-line-format)
8602                                (point-max))))
8603         ;; Not inlined.
8604         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8605             (with-current-buffer gnus-mime-security-details-buffer
8606               (erase-buffer)
8607               t)
8608           (setq gnus-mime-security-details-buffer
8609                 (gnus-get-buffer-create "*MIME Security Details*")))
8610         (with-current-buffer gnus-mime-security-details-buffer
8611           (insert details)
8612           (goto-char (point-min)))
8613         (pop-to-buffer gnus-mime-security-details-buffer)))))
8614
8615 (defun gnus-mime-security-press-button (handle)
8616   (save-excursion
8617     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8618         (gnus-mime-security-show-details handle)
8619       (gnus-mime-security-verify-or-decrypt handle))))
8620
8621 (defun gnus-insert-mime-security-button (handle &optional displayed)
8622   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8623          (gnus-tmp-type
8624           (concat
8625            (or (nth 2 (assoc protocol mm-verify-function-alist))
8626                (nth 2 (assoc protocol mm-decrypt-function-alist))
8627                "Unknown")
8628            (if (equal (car handle) "multipart/signed")
8629                " Signed" " Encrypted")
8630            " Part"))
8631          (gnus-tmp-info
8632           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8633               "Undecided"))
8634          (gnus-tmp-details
8635           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8636          gnus-tmp-pressed-details
8637          b e)
8638     (setq gnus-tmp-details
8639           (if gnus-tmp-details
8640               (concat "\n" gnus-tmp-details)
8641             ""))
8642     (setq gnus-tmp-pressed-details
8643           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8644     (unless (bolp)
8645       (insert "\n"))
8646     (setq b (point))
8647     (gnus-eval-format
8648      gnus-mime-security-button-line-format
8649      gnus-mime-security-button-line-format-alist
8650      `(keymap ,gnus-mime-security-button-map
8651          gnus-callback gnus-mime-security-press-button
8652          gnus-line-format ,gnus-mime-security-button-line-format
8653          gnus-mime-details ,gnus-mime-security-button-pressed
8654          article-type annotation
8655          gnus-data ,handle))
8656     (setq e (if (bolp)
8657                 ;; Exclude a newline.
8658                 (1- (point))
8659               (point)))
8660     (when gnus-article-button-face
8661       (gnus-overlay-put (gnus-make-overlay b e nil t)
8662                         'face gnus-article-button-face))
8663     (widget-convert-button
8664      'link b e
8665      :mime-handle handle
8666      :action 'gnus-widget-press-button
8667      :button-keymap gnus-mime-security-button-map
8668      :help-echo
8669      (lambda (widget)
8670        ;; Needed to properly clear the message due to a bug in
8671        ;; wid-edit (XEmacs only).
8672        (when (boundp 'help-echo-owns-message)
8673          (setq help-echo-owns-message t))
8674        (format
8675         "%S: show detail; %S: more options"
8676         (aref gnus-mouse-2 0)
8677         (aref gnus-down-mouse-3 0))))))
8678
8679 (defun gnus-mime-display-security (handle)
8680   (save-restriction
8681     (narrow-to-region (point) (point))
8682     (unless (gnus-unbuttonized-mime-type-p (car handle))
8683       (gnus-insert-mime-security-button handle))
8684     (gnus-mime-display-part (cadr handle))
8685     (unless (bolp)
8686       (insert "\n"))
8687     (unless (gnus-unbuttonized-mime-type-p (car handle))
8688       (let ((gnus-mime-security-button-line-format
8689              gnus-mime-security-button-end-line-format))
8690         (gnus-insert-mime-security-button handle)))
8691     (mm-set-handle-multipart-parameter
8692      handle 'gnus-region
8693      (cons (set-marker (make-marker) (point-min))
8694            (set-marker (make-marker) (point-max))))
8695     (goto-char (point-max))))
8696
8697 (defun gnus-mime-security-run-function (function)
8698   "Run FUNCTION with the security part under point."
8699   (gnus-article-check-buffer)
8700   (let ((data (get-text-property (point) 'gnus-data))
8701         buffer handle)
8702     (when (and (stringp (car-safe data))
8703                (setq buffer (mm-handle-multipart-original-buffer data))
8704                (setq handle (cadr data)))
8705       (if (bufferp (mm-handle-buffer handle))
8706           (progn
8707             (setq handle (cons buffer (copy-sequence (cdr handle))))
8708             (mm-handle-set-undisplayer handle nil))
8709         (setq handle (mm-make-handle
8710                       buffer
8711                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8712                       nil nil nil nil nil nil)))
8713       (funcall function handle))))
8714
8715 (defun gnus-mime-security-save-part ()
8716   "Save the security part under point."
8717   (interactive)
8718   (gnus-mime-security-run-function 'mm-save-part))
8719
8720 (defun gnus-mime-security-pipe-part ()
8721   "Pipe the security part under point to a process."
8722   (interactive)
8723   (gnus-mime-security-run-function 'mm-pipe-part))
8724
8725 (gnus-ems-redefine)
8726
8727 (provide 'gnus-art)
8728
8729 (run-hooks 'gnus-art-load-hook)
8730
8731 ;;; gnus-art.el ends here