Merge remote branch 'origin/no-gnus'
[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-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'gnus-win)
40 (require 'mm-bodies)
41 (require 'mail-parse)
42 (require 'mm-decode)
43 (require 'mm-view)
44 (require 'wid-edit)
45 (require 'mm-uu)
46 (require 'message)
47 (require 'mouse)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54 (autoload 'mm-url-insert-file-contents-external "mm-url")
55 (autoload 'mm-extern-cache-contents "mm-extern")
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   (mapcar
120    (lambda (header)
121      (concat "^" header ":"))
122    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
123      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
124      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
125      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
126      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
127      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
128      "X-Attribution" "X-Originating-IP" "Delivered-To"
129      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
130      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
131      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
132      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
133      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
134      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
135      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
136      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
137      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
138      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
139      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
140      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
141      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
142      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
143      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
144      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
145      "List-[A-Za-z]+" "X-Listprocessor-Version"
146      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
147      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
148      "X-Received" "Content-length" "X-precedence"
149      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
150      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
151      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
152      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
153      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
154      "X-Content-length" "X-Posting-Agent" "Original-Received"
155      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
156      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
157      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
158      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
159      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
160      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
161      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
162      "Envelope-Sender" "Envelope-Recipients"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice regexp
167                  (repeat regexp))
168   :group 'gnus-article-hiding)
169
170 (defcustom gnus-visible-headers
171   "^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:"
172   "*All headers that do not match this regexp will be hidden.
173 This variable can also be a list of regexp of headers to remain visible.
174 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
175   :type '(choice
176           (repeat :value-to-internal (lambda (widget value)
177                                        (custom-split-regexp-maybe value))
178                   :match (lambda (widget value)
179                            (or (stringp value)
180                                (widget-editable-list-match widget value)))
181                   regexp)
182           (const :tag "Use gnus-ignored-headers" nil)
183           regexp)
184   :group 'gnus-article-hiding)
185
186 (defcustom gnus-sorted-header-list
187   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
188     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
189   "*This variable is a list of regular expressions.
190 If it is non-nil, headers that match the regular expressions will
191 be placed first in the article buffer in the sequence specified by
192 this list."
193   :type '(repeat regexp)
194   :group 'gnus-article-hiding)
195
196 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
197   "Headers that are only to be displayed if they have interesting data.
198 Possible values in this list are:
199
200   'empty       Headers with no content.
201   'newsgroups  Newsgroup identical to Gnus group.
202   'to-address  To identical to To-address.
203   'to-list     To identical to To-list.
204   'cc-list     CC identical to To-list.
205   'followup-to Followup-to identical to Newsgroups.
206   'reply-to    Reply-to identical to From.
207   'date        Date less than four days old.
208   'long-to     To and/or Cc longer than 1024 characters.
209   'many-to     Multiple To and/or Cc."
210   :type '(set (const :tag "Headers with no content." empty)
211               (const :tag "Newsgroups identical to Gnus group." newsgroups)
212               (const :tag "To identical to To-address." to-address)
213               (const :tag "To identical to To-list." to-list)
214               (const :tag "CC identical to To-list." cc-list)
215               (const :tag "Followup-to identical to Newsgroups." followup-to)
216               (const :tag "Reply-to identical to From." reply-to)
217               (const :tag "Date less than four days old." date)
218               (const :tag "To and/or Cc longer than 1024 characters." long-to)
219               (const :tag "Multiple To and/or Cc headers." many-to))
220   :group 'gnus-article-hiding)
221
222 (defcustom gnus-article-skip-boring nil
223   "Skip over text that is not worth reading.
224 By default, if you set this t, then Gnus will display citations and
225 signatures, but will never scroll down to show you a page consisting
226 only of boring text.  Boring text is controlled by
227 `gnus-article-boring-faces'."
228   :version "22.1"
229   :type 'boolean
230   :group 'gnus-article-hiding)
231
232 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
233   "Regexp matching signature separator.
234 This can also be a list of regexps.  In that case, it will be checked
235 from head to tail looking for a separator.  Searches will be done from
236 the end of the buffer."
237   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
238                  (regexp)
239                  (repeat :tag "List of regexp" regexp))
240   :group 'gnus-article-signature)
241
242 (defcustom gnus-signature-limit nil
243   "Provide a limit to what is considered a signature.
244 If it is a number, no signature may not be longer (in characters) than
245 that number.  If it is a floating point number, no signature may be
246 longer (in lines) than that number.  If it is a function, the function
247 will be called without any parameters, and if it returns nil, there is
248 no signature in the buffer.  If it is a string, it will be used as a
249 regexp.  If it matches, the text in question is not a signature.
250
251 This can also be a list of the above values."
252   :type '(choice (const nil)
253                  (integer :value 200)
254                  (number :value 4.0)
255                  function
256                  (regexp :value ".*"))
257   :group 'gnus-article-signature)
258
259 (defcustom gnus-hidden-properties '(invisible t intangible t)
260   "Property list to use for hiding text."
261   :type 'sexp
262   :group 'gnus-article-hiding)
263
264 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
265 ;; frames in a session.
266 (defcustom gnus-article-x-face-command
267   (if (featurep 'xemacs)
268       (if (or (gnus-image-type-available-p 'xface)
269               (gnus-image-type-available-p 'pbm))
270           'gnus-display-x-face-in-from
271         "{ echo \
272 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
273 ; uncompface; } | icontopbm | ee -")
274     (if (gnus-image-type-available-p 'pbm)
275         'gnus-display-x-face-in-from
276       "{ echo \
277 '/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */'\
278 ; uncompface; } | icontopbm | display -"))
279   "*String or function to be executed to display an X-Face header.
280 If it is a string, the command will be executed in a sub-shell
281 asynchronously.  The compressed face will be piped to this command."
282   :type `(choice string
283                  (function-item gnus-display-x-face-in-from)
284                  function)
285   :version "21.1"
286   :group 'gnus-picon
287   :group 'gnus-article-washing)
288
289 (defcustom gnus-article-x-face-too-ugly nil
290   "Regexp matching posters whose face shouldn't be shown automatically."
291   :type '(choice regexp (const nil))
292   :group 'gnus-article-washing)
293
294 (defcustom gnus-article-banner-alist nil
295   "Banner alist for stripping.
296 For example,
297      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
298   :version "21.1"
299   :type '(repeat (cons symbol regexp))
300   :group 'gnus-article-washing)
301
302 (gnus-define-group-parameter
303  banner
304  :variable-document
305  "Alist of regexps (to match group names) and banner."
306  :variable-group gnus-article-washing
307  :parameter-type
308  '(choice :tag "Banner"
309           :value nil
310           (const :tag "Remove signature" signature)
311           (symbol :tag "Item in `gnus-article-banner-alist'" none)
312           regexp
313           (const :tag "None" nil))
314  :parameter-document
315  "If non-nil, specify how to remove `banners' from articles.
316
317 Symbol `signature' means to remove signatures delimited by
318 `gnus-signature-separator'.  Any other symbol is used to look up a
319 regular expression to match the banner in `gnus-article-banner-alist'.
320 A string is used as a regular expression to match the banner
321 directly.")
322
323 (defcustom gnus-article-address-banner-alist nil
324   "Alist of mail addresses and banners.
325 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
326 to match a mail address in the From: header, BANNER is one of a symbol
327 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
328 If ADDRESS matches author's mail address, it will remove things like
329 advertisements.  For example:
330
331 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
332 "
333   :type '(repeat
334           (cons
335            (regexp :tag "Address")
336            (choice :tag "Banner" :value nil
337                    (const :tag "Remove signature" signature)
338                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
339                    regexp
340                    (const :tag "None" nil))))
341   :version "22.1"
342   :group 'gnus-article-washing)
343
344 (defmacro gnus-emphasis-custom-with-format (&rest body)
345   `(let ((format "\
346 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
347 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
348      ,@body))
349
350 (defun gnus-emphasis-custom-value-to-external (value)
351   (gnus-emphasis-custom-with-format
352    (if (consp (car value))
353        (list (format format (car (car value)) (cdr (car value)))
354              2
355              (if (nth 1 value) 2 3)
356              (nth 2 value))
357      value)))
358
359 (defun gnus-emphasis-custom-value-to-internal (value)
360   (gnus-emphasis-custom-with-format
361    (let ((regexp (concat "\\`"
362                          (format (regexp-quote format)
363                                  "\\([^()]+\\)" "\\([^()]+\\)")
364                          "\\'"))
365          pattern)
366      (if (string-match regexp (setq pattern (car value)))
367          (list (cons (match-string 1 pattern) (match-string 2 pattern))
368                (= (nth 2 value) 2)
369                (nth 3 value))
370        value))))
371
372 (defcustom gnus-emphasis-alist
373   (let ((types
374          '(("\\*" "\\*" bold nil 2)
375            ("_" "_" underline)
376            ("/" "/" italic)
377            ("_/" "/_" underline-italic)
378            ("_\\*" "\\*_" underline-bold)
379            ("\\*/" "/\\*" bold-italic)
380            ("_\\*/" "/\\*_" underline-bold-italic))))
381     (nconc
382      (gnus-emphasis-custom-with-format
383       (mapcar (lambda (spec)
384                 (list (format format (car spec) (cadr spec))
385                       (or (nth 3 spec) 2)
386                       (or (nth 4 spec) 3)
387                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
388               types))
389      '(;; I've never seen anyone use this strikethru convention whereas I've
390        ;; several times seen it triggered by normal text.  --Stef
391        ;; Miles suggests that this form is sometimes used but for italics,
392        ;; so maybe we should map it to `italic'.
393        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
394        ;; 2 3 gnus-emphasis-strikethru)
395        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
396         2 3 gnus-emphasis-underline))))
397   "*Alist that says how to fontify certain phrases.
398 Each item looks like this:
399
400   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
401
402 The first element is a regular expression to be matched.  The second
403 is a number that says what regular expression grouping used to find
404 the entire emphasized word.  The third is a number that says what
405 regexp grouping should be displayed and highlighted.  The fourth
406 is the face used for highlighting."
407   :type
408   '(repeat
409     (menu-choice
410      :format "%[Customizing Style%]\n%v"
411      :indent 2
412      (group :tag "Default"
413             :value ("" 0 0 default)
414             :value-create
415             (lambda (widget)
416               (let ((value (widget-get
417                             (cadr (widget-get (widget-get widget :parent)
418                                               :args))
419                             :value)))
420                 (if (not (eq (nth 2 value) 'default))
421                     (widget-put
422                      widget
423                      :value
424                      (gnus-emphasis-custom-value-to-external value))))
425               (widget-group-value-create widget))
426             regexp
427             (integer :format "Match group: %v")
428             (integer :format "Emphasize group: %v")
429             face)
430      (group :tag "Simple"
431             :value (("_" . "_") nil default)
432             (cons :format "%v"
433                   (regexp :format "Start regexp: %v")
434                   (regexp :format "End regexp: %v"))
435             (boolean :format "Show start and end patterns: %[%v%]\n"
436                      :on " On " :off " Off ")
437             face)))
438   :get (lambda (symbol)
439          (mapcar 'gnus-emphasis-custom-value-to-internal
440                  (default-value symbol)))
441   :set (lambda (symbol value)
442          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
443                                      value)))
444   :group 'gnus-article-emphasis)
445
446 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
447   "A regexp to describe whitespace which should not be emphasized.
448 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
449 The former avoids underlining of leading and trailing whitespace,
450 and the latter avoids underlining any whitespace at all."
451   :version "21.1"
452   :group 'gnus-article-emphasis
453   :type 'regexp)
454
455 (defface gnus-emphasis-bold '((t (:bold t)))
456   "Face used for displaying strong emphasized text (*word*)."
457   :group 'gnus-article-emphasis)
458
459 (defface gnus-emphasis-italic '((t (:italic t)))
460   "Face used for displaying italic emphasized text (/word/)."
461   :group 'gnus-article-emphasis)
462
463 (defface gnus-emphasis-underline '((t (:underline t)))
464   "Face used for displaying underlined emphasized text (_word_)."
465   :group 'gnus-article-emphasis)
466
467 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
468   "Face used for displaying underlined bold emphasized text (_*word*_)."
469   :group 'gnus-article-emphasis)
470
471 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
472   "Face used for displaying underlined italic emphasized text (_/word/_)."
473   :group 'gnus-article-emphasis)
474
475 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
476   "Face used for displaying bold italic emphasized text (/*word*/)."
477   :group 'gnus-article-emphasis)
478
479 (defface gnus-emphasis-underline-bold-italic
480   '((t (:bold t :italic t :underline t)))
481   "Face used for displaying underlined bold italic emphasized text.
482 Example: (_/*word*/_)."
483   :group 'gnus-article-emphasis)
484
485 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
486                                       '((t (:strikethru t)))
487                                     '((t (:strike-through t))))
488   "Face used for displaying strike-through text (-word-)."
489   :group 'gnus-article-emphasis)
490
491 (defface gnus-emphasis-highlight-words
492   '((t (:background "black" :foreground "yellow")))
493   "Face used for displaying highlighted words."
494   :group 'gnus-article-emphasis)
495
496 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
497   "Format for display of Date headers in article bodies.
498 See `format-time-string' for the possible values.
499
500 The variable can also be function, which should return a complete Date
501 header.  The function is called with one argument, the time, which can
502 be fed to `format-time-string'."
503   :type '(choice string function)
504   :link '(custom-manual "(gnus)Article Date")
505   :group 'gnus-article-washing)
506
507 (defcustom gnus-save-all-headers t
508   "*If non-nil, don't remove any headers before saving.
509 This will be overridden by the `:headers' property that the symbol of
510 the saver function, which is specified by `gnus-default-article-saver',
511 might have."
512   :group 'gnus-article-saving
513   :type 'boolean)
514
515 (defcustom gnus-prompt-before-saving 'always
516   "*This variable says how much prompting is to be done when saving articles.
517 If it is nil, no prompting will be done, and the articles will be
518 saved to the default files.  If this variable is `always', each and
519 every article that is saved will be preceded by a prompt, even when
520 saving large batches of articles.  If this variable is neither nil not
521 `always', there the user will be prompted once for a file name for
522 each invocation of the saving commands."
523   :group 'gnus-article-saving
524   :type '(choice (item always)
525                  (item :tag "never" nil)
526                  (sexp :tag "once" :format "%t\n" :value t)))
527
528 (defcustom gnus-saved-headers gnus-visible-headers
529   "Headers to keep if `gnus-save-all-headers' is nil.
530 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
531 If that variable is nil, however, all headers that match this regexp
532 will be kept while the rest will be deleted before saving.  This and
533 `gnus-save-all-headers' will be overridden by the `:headers' property
534 that the symbol of the saver function, which is specified by
535 `gnus-default-article-saver', might have."
536   :group 'gnus-article-saving
537   :type 'regexp)
538
539 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
540 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
541   "A function to save articles in your favorite format.
542 The function will be called by way of the `gnus-summary-save-article'
543 command, and friends such as `gnus-summary-save-article-rmail'.
544
545 Gnus provides the following functions:
546
547 * gnus-summary-save-in-rmail (Rmail format)
548 * gnus-summary-save-in-mail (Unix mail format)
549 * gnus-summary-save-in-folder (MH folder)
550 * gnus-summary-save-in-file (article format)
551 * gnus-summary-save-body-in-file (article body)
552 * gnus-summary-save-in-vm (use VM's folder format)
553 * gnus-summary-write-to-file (article format -- overwrite)
554 * gnus-summary-write-body-to-file (article body -- overwrite)
555 * gnus-summary-save-in-pipe (article format)
556
557 The symbol of each function may have the following properties:
558
559 * :decode
560 The value non-nil means save decoded articles.  This is meaningful
561 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
562 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
563 `gnus-summary-save-in-pipe'.
564
565 * :function
566 The value specifies an alternative function which appends, not
567 overwrites, articles to a file.  This implies that when saving many
568 articles at a time, `gnus-prompt-before-saving' is bound to t and all
569 articles are saved in a single file.  This is meaningful only with
570 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
571
572 * :headers
573 The value specifies the symbol of a variable of which the value
574 specifies headers to be saved.  If it is omitted,
575 `gnus-save-all-headers' and `gnus-saved-headers' control what
576 headers should be saved."
577   :group 'gnus-article-saving
578   :type '(radio (function-item gnus-summary-save-in-rmail)
579                 (function-item gnus-summary-save-in-mail)
580                 (function-item gnus-summary-save-in-folder)
581                 (function-item gnus-summary-save-in-file)
582                 (function-item gnus-summary-save-body-in-file)
583                 (function-item gnus-summary-save-in-vm)
584                 (function-item gnus-summary-write-to-file)
585                 (function-item gnus-summary-write-body-to-file)
586                 (function-item gnus-summary-save-in-pipe)
587                 (function)))
588
589 (defcustom gnus-article-save-coding-system
590   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
591       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
592       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
593       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
594   "Coding system used to save decoded articles to a file.
595
596 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
597 which can safely encode any characters in text.  This is used by the
598 commands including:
599
600 * gnus-summary-save-article-file
601 * gnus-summary-save-article-body-file
602 * gnus-summary-write-article-file
603 * gnus-summary-write-article-body-file
604
605 and the functions to which you may set `gnus-default-article-saver':
606
607 * gnus-summary-save-in-file
608 * gnus-summary-save-body-in-file
609 * gnus-summary-write-to-file
610 * gnus-summary-write-body-to-file
611
612 Those commands and functions save just text displayed in the article
613 buffer to a file if the value of this variable is non-nil.  Note that
614 buttonized MIME parts will be lost in a saved file in that case.
615 Otherwise, raw articles will be saved."
616   :group 'gnus-article-saving
617   :type `(choice
618           :format "%{%t%}:\n %[Value Menu%] %v"
619           (const :tag "Save raw articles" nil)
620           ,@(delq nil
621                   (mapcar
622                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
623                    '((const :tag "UTF-8" utf-8)
624                      (const :tag "iso-2022-7bit" iso-2022-7bit)
625                      (const :tag "Emacs internal" emacs-mule)
626                      (const :tag "escape-quoted" escape-quoted))))
627           (symbol :tag "Coding system")))
628
629 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
630   "A function generating a file name to save articles in Rmail format.
631 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
632   :group 'gnus-article-saving
633   :type 'function)
634
635 (defcustom gnus-mail-save-name 'gnus-plain-save-name
636   "A function generating a file name to save articles in Unix mail format.
637 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
638   :group 'gnus-article-saving
639   :type 'function)
640
641 (defcustom gnus-folder-save-name 'gnus-folder-save-name
642   "A function generating a file name to save articles in MH folder.
643 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
644   :group 'gnus-article-saving
645   :type 'function)
646
647 (defcustom gnus-file-save-name 'gnus-numeric-save-name
648   "A function generating a file name to save articles in article format.
649 The function is called with NEWSGROUP, HEADERS, and optional
650 LAST-FILE."
651   :group 'gnus-article-saving
652   :type 'function)
653
654 (defcustom gnus-split-methods
655   '((gnus-article-archive-name)
656     (gnus-article-nndoc-name))
657   "*Variable used to suggest where articles are to be saved.
658 For instance, if you would like to save articles related to Gnus in
659 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
660 you could set this variable to something like:
661
662  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
663    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
664
665 This variable is an alist where the key is the match and the
666 value is a list of possible files to save in if the match is
667 non-nil.
668
669 If the match is a string, it is used as a regexp match on the
670 article.  If the match is a symbol, that symbol will be funcalled
671 from the buffer of the article to be saved with the newsgroup as the
672 parameter.  If it is a list, it will be evalled in the same buffer.
673
674 If this form or function returns a string, this string will be used as a
675 possible file name; and if it returns a non-nil list, that list will be
676 used as possible file names."
677   :group 'gnus-article-saving
678   :type '(repeat (choice (list :value (fun) function)
679                          (cons :value ("" "") regexp (repeat string))
680                          (sexp :value nil))))
681
682 (defcustom gnus-page-delimiter "^\^L"
683   "*Regexp describing what to use as article page delimiters.
684 The default value is \"^\^L\", which is a form linefeed at the
685 beginning of a line."
686   :type 'regexp
687   :group 'gnus-article-various)
688
689 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
690   "*The format specification for the article mode line.
691 See `gnus-summary-mode-line-format' for a closer description.
692
693 The following additional specs are available:
694
695 %w  The article washing status.
696 %m  The number of MIME parts in the article."
697   :version "24.1"
698   :type 'string
699   :group 'gnus-article-various)
700
701 (defcustom gnus-article-mode-hook nil
702   "*A hook for Gnus article mode."
703   :type 'hook
704   :group 'gnus-article-various)
705
706 (when (featurep 'xemacs)
707   ;; Extracted from gnus-xmas-define in order to preserve user settings
708   (when (fboundp 'turn-off-scroll-in-place)
709     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
710   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
711   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
712
713 (defcustom gnus-article-menu-hook nil
714   "*Hook run after the creation of the article mode menu."
715   :type 'hook
716   :group 'gnus-article-various)
717
718 (defcustom gnus-article-prepare-hook nil
719   "*A hook called after an article has been prepared in the article buffer."
720   :type 'hook
721   :group 'gnus-article-various)
722
723 (defcustom gnus-copy-article-ignored-headers nil
724   "List of headers to be removed when copying an article.
725 Each element is a regular expression."
726   :version "23.1" ;; No Gnus
727   :type '(repeat regexp)
728   :group 'gnus-article-various)
729
730 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
731                         "Gnus 5.10 (Emacs 22.1)")
732
733 (defface gnus-button
734   '((t (:weight bold)))
735   "Face used for highlighting a button in the article buffer."
736   :group 'gnus-article-buttons)
737
738 (defcustom gnus-article-button-face 'gnus-button
739   "Face used for highlighting buttons in the article buffer.
740
741 An article button is a piece of text that you can activate by pressing
742 `RET' or `mouse-2' above it."
743   :type 'face
744   :group 'gnus-article-buttons)
745
746 (defcustom gnus-article-mouse-face 'highlight
747   "Face used for mouse highlighting in the article buffer.
748
749 Article buttons will be displayed in this face when the cursor is
750 above them."
751   :type 'face
752   :group 'gnus-article-buttons)
753
754 (defcustom gnus-signature-face 'gnus-signature
755   "Face used for highlighting a signature in the article buffer.
756 Obsolete; use the face `gnus-signature' for customizations instead."
757   :type 'face
758   :group 'gnus-article-highlight
759   :group 'gnus-article-signature)
760
761 (defface gnus-signature
762   '((t
763      (:italic t)))
764   "Face used for highlighting a signature in the article buffer."
765   :group 'gnus-article-highlight
766   :group 'gnus-article-signature)
767 ;; backward-compatibility alias
768 (put 'gnus-signature-face 'face-alias 'gnus-signature)
769 (put 'gnus-signature-face 'obsolete-face "22.1")
770
771 (defface gnus-header-from
772   '((((class color)
773       (background dark))
774      (:foreground "PaleGreen1"))
775     (((class color)
776       (background light))
777      (:foreground "red3"))
778     (t
779      (:italic t)))
780   "Face used for displaying from headers."
781   :group 'gnus-article-headers
782   :group 'gnus-article-highlight)
783 ;; backward-compatibility alias
784 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
785 (put 'gnus-header-from-face 'obsolete-face "22.1")
786
787 (defface gnus-header-subject
788   '((((class color)
789       (background dark))
790      (:foreground "SeaGreen1"))
791     (((class color)
792       (background light))
793      (:foreground "red4"))
794     (t
795      (:bold t :italic t)))
796   "Face used for displaying subject headers."
797   :group 'gnus-article-headers
798   :group 'gnus-article-highlight)
799 ;; backward-compatibility alias
800 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
801 (put 'gnus-header-subject-face 'obsolete-face "22.1")
802
803 (defface gnus-header-newsgroups
804   '((((class color)
805       (background dark))
806      (:foreground "yellow" :italic t))
807     (((class color)
808       (background light))
809      (:foreground "MidnightBlue" :italic t))
810     (t
811      (:italic t)))
812   "Face used for displaying newsgroups headers.
813 In the default setup this face is only used for crossposted
814 articles."
815   :group 'gnus-article-headers
816   :group 'gnus-article-highlight)
817 ;; backward-compatibility alias
818 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
819 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
820
821 (defface gnus-header-name
822   '((((class color)
823       (background dark))
824      (:foreground "SpringGreen2"))
825     (((class color)
826       (background light))
827      (:foreground "maroon"))
828     (t
829      (:bold t)))
830   "Face used for displaying header names."
831   :group 'gnus-article-headers
832   :group 'gnus-article-highlight)
833 ;; backward-compatibility alias
834 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
835 (put 'gnus-header-name-face 'obsolete-face "22.1")
836
837 (defface gnus-header-content
838   '((((class color)
839       (background dark))
840      (:foreground "SpringGreen1" :italic t))
841     (((class color)
842       (background light))
843      (:foreground "indianred4" :italic t))
844     (t
845      (:italic t)))  "Face used for displaying header content."
846   :group 'gnus-article-headers
847   :group 'gnus-article-highlight)
848 ;; backward-compatibility alias
849 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
850 (put 'gnus-header-content-face 'obsolete-face "22.1")
851
852 (defcustom gnus-header-face-alist
853   '(("From" nil gnus-header-from)
854     ("Subject" nil gnus-header-subject)
855     ("Newsgroups:.*," nil gnus-header-newsgroups)
856     ("" gnus-header-name gnus-header-content))
857   "*Controls highlighting of article headers.
858
859 An alist of the form (HEADER NAME CONTENT).
860
861 HEADER is a regular expression which should match the name of a
862 header and NAME and CONTENT are either face names or nil.
863
864 The name of each header field will be displayed using the face
865 specified by the first element in the list where HEADER matches
866 the header name and NAME is non-nil.  Similarly, the content will
867 be displayed by the first non-nil matching CONTENT face."
868   :group 'gnus-article-headers
869   :group 'gnus-article-highlight
870   :type '(repeat (list (regexp :tag "Header")
871                        (choice :tag "Name"
872                                (item :tag "skip" nil)
873                                (face :value default))
874                        (choice :tag "Content"
875                                (item :tag "skip" nil)
876                                (face :value default)))))
877
878 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
879                                           '((xface . (:face gnus-x-face)))
880                                         '((pbm . (:face gnus-x-face))
881                                           (png . nil)))
882   "Alist of image types and properties applied to Face and X-Face images.
883 Here are examples:
884
885 ;; Specify the altitude of Face images in the From header.
886 \(setq gnus-face-properties-alist
887       '((pbm . (:face gnus-x-face :ascent 80))
888         (png . (:ascent 80))))
889
890 ;; Show Face images as pressed buttons.
891 \(setq gnus-face-properties-alist
892       '((pbm . (:face gnus-x-face :relief -2))
893         (png . (:relief -2))))
894
895 See the manual for the valid properties for various image types.
896 Currently, `pbm' is used for X-Face images and `png' is used for Face
897 images in Emacs.  Only the `:face' property is effective on the `xface'
898 image type in XEmacs if it is built with the libcompface library."
899   :version "23.1" ;; No Gnus
900   :group 'gnus-article-headers
901   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
902
903 (defcustom gnus-article-decode-hook
904   '(article-decode-charset article-decode-encoded-words
905                            article-decode-group-name article-decode-idna-rhs)
906   "*Hook run to decode charsets in articles."
907   :group 'gnus-article-headers
908   :type 'hook)
909
910 (defcustom gnus-display-mime-function 'gnus-display-mime
911   "Function to display MIME articles."
912   :group 'gnus-article-mime
913   :type 'function)
914
915 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
916   "Function used to decode headers.")
917
918 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
919   "Function used to decode addresses.")
920
921 (defvar gnus-article-dumbquotes-map
922   '((?\200 "EUR")
923     (?\202 ",")
924     (?\203 "f")
925     (?\204 ",,")
926     (?\205 "...")
927     (?\213 "<")
928     (?\214 "OE")
929     (?\221 "`")
930     (?\222 "'")
931     (?\223 "``")
932     (?\224 "\"")
933     (?\225 "*")
934     (?\226 "-")
935     (?\227 "--")
936     (?\230 "~")
937     (?\231 "(TM)")
938     (?\233 ">")
939     (?\234 "oe")
940     (?\264 "'"))
941   "Table for MS-to-Latin1 translation.")
942
943 (defcustom gnus-ignored-mime-types nil
944   "List of MIME types that should be ignored by Gnus."
945   :version "21.1"
946   :group 'gnus-article-mime
947   :type '(repeat regexp))
948
949 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
950   "List of MIME types that should not be given buttons when rendered inline.
951 See also `gnus-buttonized-mime-types' which may override this variable.
952 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
953   :version "21.1"
954   :group 'gnus-article-mime
955   :type '(repeat regexp))
956
957 (defcustom gnus-buttonized-mime-types nil
958   "List of MIME types that should be given buttons when rendered inline.
959 If set, this variable overrides `gnus-unbuttonized-mime-types'.
960 To see e.g. security buttons you could set this to
961 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
962 this list to display radio buttons that allow you to choose one of two
963 media types those mails include.  See also `mm-discouraged-alternatives'.
964 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
965   :version "22.1"
966   :group 'gnus-article-mime
967   :type '(repeat regexp))
968
969 (defcustom gnus-inhibit-mime-unbuttonizing nil
970   "If non-nil, all MIME parts get buttons.
971 When nil (the default value), then some MIME parts do not get buttons,
972 as described by the variables `gnus-buttonized-mime-types' and
973 `gnus-unbuttonized-mime-types'."
974   :version "22.1"
975   :group 'gnus-article-mime
976   :type 'boolean)
977
978 (defcustom gnus-body-boundary-delimiter "_"
979   "String used to delimit header and body.
980 This variable is used by `gnus-article-treat-body-boundary' which can
981 be controlled by `gnus-treat-body-boundary'."
982   :version "22.1"
983   :group 'gnus-article-various
984   :type '(choice (item :tag "None" :value nil)
985                  string))
986
987 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
988                                   "/usr/share/picons")
989   "Defines the location of the faces database.
990 For information on obtaining this database of pretty pictures, please
991 see http://www.cs.indiana.edu/picons/ftp/index.html"
992   :version "22.1"
993   :type '(repeat directory)
994   :link '(url-link :tag "download"
995                    "http://www.cs.indiana.edu/picons/ftp/index.html")
996   :link '(custom-manual "(gnus)Picons")
997   :group 'gnus-picon)
998
999 (defun gnus-picons-installed-p ()
1000   "Say whether picons are installed on your machine."
1001   (let ((installed nil))
1002     (dolist (database gnus-picon-databases)
1003       (when (file-exists-p database)
1004         (setq installed t)))
1005     installed))
1006
1007 (defcustom gnus-article-mime-part-function nil
1008   "Function called with a MIME handle as the argument.
1009 This is meant for people who want to do something automatic based
1010 on parts -- for instance, adding Vcard info to a database."
1011   :group 'gnus-article-mime
1012   :type '(choice (const nil)
1013                  function))
1014
1015 (defcustom gnus-mime-multipart-functions nil
1016   "An alist of MIME types to functions to display them."
1017   :version "21.1"
1018   :group 'gnus-article-mime
1019   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1020
1021 (defcustom gnus-article-date-headers '(combined-lapsed)
1022   "A list of Date header formats to display.
1023 Valid formats are `ut' (universal time), `local' (local time
1024 zone), `english' (readable English), `lapsed' (elapsed time),
1025 `combined-lapsed' (both the original date and the elapsed time),
1026 `original' (the original date header), `iso8601' (ISO8601
1027 format), and `user-defined' (a user-defined format defined by the
1028 `gnus-article-time-format' variable).
1029
1030 You have as many date headers as you want in the article buffer.
1031 Some of these headers are updated automatically.  See
1032 `gnus-article-update-date-headers' for details."
1033   :version "24.1"
1034   :group 'gnus-article-headers
1035   :type '(repeat
1036           (item :tag "Universal time (UT)" :value 'ut)
1037           (item :tag "Local time zone" :value 'local)
1038           (item :tag "Readable English" :value 'english)
1039           (item :tag "Elapsed time" :value 'lapsed)
1040           (item :tag "Original and elapsed time" :value 'combined-lapsed)
1041           (item :tag "Original date header" :value 'original)
1042           (item :tag "ISO8601 format" :value 'iso8601)
1043           (item :tag "User-defined" :value 'user-defined)))
1044
1045 (defcustom gnus-article-update-date-headers nil
1046   "A number that says how often to update the date header (in seconds).
1047 If nil, don't update it at all."
1048   :version "24.1"
1049   :group 'gnus-article-headers
1050   :type '(choice
1051           (item :tag "Don't update" :value nil)
1052           integer))
1053
1054 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1055   "Function called with a MIME handle as the argument.
1056 This is meant for people who want to view first matched part.
1057 For `undisplayed-alternative' (default), the first undisplayed
1058 part or alternative part is used.  For `undisplayed', the first
1059 undisplayed part is used.  For a function, the first part which
1060 the function return t is used.  For nil, the first part is
1061 used."
1062   :version "21.1"
1063   :group 'gnus-article-mime
1064   :type '(choice
1065           (item :tag "first" :value nil)
1066           (item :tag "undisplayed" :value undisplayed)
1067           (item :tag "undisplayed or alternative"
1068                 :value undisplayed-alternative)
1069           (function)))
1070
1071 (defcustom gnus-mime-action-alist
1072   '(("save to file" . gnus-mime-save-part)
1073     ("save and strip" . gnus-mime-save-part-and-strip)
1074     ("replace with file" . gnus-mime-replace-part)
1075     ("delete part" . gnus-mime-delete-part)
1076     ("display as text" . gnus-mime-inline-part)
1077     ("view the part" . gnus-mime-view-part)
1078     ("pipe to command" . gnus-mime-pipe-part)
1079     ("toggle display" . gnus-article-press-button)
1080     ("toggle display" . gnus-article-view-part-as-charset)
1081     ("view as type" . gnus-mime-view-part-as-type)
1082     ("view internally" . gnus-mime-view-part-internally)
1083     ("view externally" . gnus-mime-view-part-externally))
1084   "An alist of actions that run on the MIME attachment."
1085   :group 'gnus-article-mime
1086   :type '(repeat (cons (string :tag "name")
1087                        (function))))
1088
1089 (defcustom gnus-auto-select-part 1
1090   "Advance to next MIME part when deleting or stripping parts.
1091
1092 When 0, point will be placed on the same part as before.  When
1093 positive (negative), move point forward (backwards) this many
1094 parts.  When nil, redisplay article."
1095   :version "23.1" ;; No Gnus
1096   :group 'gnus-article-mime
1097   :type '(choice (const nil :tag "Redisplay article.")
1098                  (const 1 :tag "Next part.")
1099                  (const 0 :tag "Current part.")
1100                  integer))
1101
1102 ;;;
1103 ;;; The treatment variables
1104 ;;;
1105
1106 (defvar gnus-part-display-hook nil
1107   "Hook called on parts that are to receive treatment.")
1108
1109 (defvar gnus-article-treat-custom
1110   '(choice (const :tag "Off" nil)
1111            (const :tag "On" t)
1112            (const :tag "Header" head)
1113            (const :tag "First" first)
1114            (const :tag "Last" last)
1115            (integer :tag "Less")
1116            (repeat :tag "Groups" regexp)
1117            (sexp :tag "Predicate")))
1118
1119 (defvar gnus-article-treat-head-custom
1120   '(choice (const :tag "Off" nil)
1121            (const :tag "Header" head)))
1122
1123 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1124                                    "text/x-patch")
1125   "Parts to treat.")
1126
1127 (defvar gnus-inhibit-treatment nil
1128   "Whether to inhibit treatment.")
1129
1130 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1131   "Highlight the signature.
1132 Valid values are nil, t, `head', `first', `last', an integer or a
1133 predicate.  See Info node `(gnus)Customizing Articles'."
1134   :group 'gnus-article-treat
1135   :link '(custom-manual "(gnus)Customizing Articles")
1136   :type gnus-article-treat-custom)
1137 (put 'gnus-treat-highlight-signature 'highlight t)
1138
1139 (defcustom gnus-treat-buttonize 100000
1140   "Add buttons.
1141 Valid values are nil, t, `head', `first', `last', an integer or a
1142 predicate.  See Info node `(gnus)Customizing Articles'."
1143   :group 'gnus-article-treat
1144   :link '(custom-manual "(gnus)Customizing Articles")
1145   :type gnus-article-treat-custom)
1146 (put 'gnus-treat-buttonize 'highlight t)
1147
1148 (defcustom gnus-treat-buttonize-head 'head
1149   "Add buttons to the head.
1150 Valid values are nil, t, `head', `first', `last', an integer or a
1151 predicate.  See Info node `(gnus)Customizing Articles'."
1152   :group 'gnus-article-treat
1153   :link '(custom-manual "(gnus)Customizing Articles")
1154   :type gnus-article-treat-head-custom)
1155 (put 'gnus-treat-buttonize-head 'highlight t)
1156
1157 (defcustom gnus-treat-date 'head
1158   "Display dates according to the `gnus-article-date-headers' variable.
1159 Valid values are nil, t, `head', `first', `last', an integer or a
1160 predicate.  See Info node `(gnus)Customizing Articles'."
1161   :version "24.1"
1162   :group 'gnus-article-treat
1163   :link '(custom-manual "(gnus)Customizing Articles")
1164   :type gnus-article-treat-head-custom)
1165
1166 (defcustom gnus-treat-emphasize 50000
1167   "Emphasize text.
1168 Valid values are nil, t, `head', `first', `last', an integer or a
1169 predicate.  See Info node `(gnus)Customizing Articles'."
1170   :group 'gnus-article-treat
1171   :link '(custom-manual "(gnus)Customizing Articles")
1172   :type gnus-article-treat-custom)
1173 (put 'gnus-treat-emphasize 'highlight t)
1174
1175 (defcustom gnus-treat-strip-cr nil
1176   "Remove carriage returns.
1177 Valid values are nil, t, `head', `first', `last', an integer or a
1178 predicate.  See Info node `(gnus)Customizing Articles'."
1179   :version "22.1"
1180   :group 'gnus-article-treat
1181   :link '(custom-manual "(gnus)Customizing Articles")
1182   :type gnus-article-treat-custom)
1183
1184 (defcustom gnus-treat-unsplit-urls nil
1185   "Remove newlines from within URLs.
1186 Valid values are nil, t, `head', `first', `last', an integer or a
1187 predicate.  See Info node `(gnus)Customizing Articles'."
1188   :version "22.1"
1189   :group 'gnus-article-treat
1190   :link '(custom-manual "(gnus)Customizing Articles")
1191   :type gnus-article-treat-custom)
1192
1193 (defcustom gnus-treat-leading-whitespace nil
1194   "Remove leading whitespace in headers.
1195 Valid values are nil, t, `head', `first', `last', an integer or a
1196 predicate.  See Info node `(gnus)Customizing Articles'."
1197   :version "22.1"
1198   :group 'gnus-article-treat
1199   :link '(custom-manual "(gnus)Customizing Articles")
1200   :type gnus-article-treat-custom)
1201
1202 (defcustom gnus-treat-hide-headers 'head
1203   "Hide headers.
1204 Valid values are nil, t, `head', `first', `last', an integer or a
1205 predicate.  See Info node `(gnus)Customizing Articles'."
1206   :group 'gnus-article-treat
1207   :link '(custom-manual "(gnus)Customizing Articles")
1208   :type gnus-article-treat-head-custom)
1209
1210 (defcustom gnus-treat-hide-boring-headers nil
1211   "Hide boring headers.
1212 Valid values are nil, t, `head', `first', `last', an integer or a
1213 predicate.  See Info node `(gnus)Customizing Articles'."
1214   :group 'gnus-article-treat
1215   :link '(custom-manual "(gnus)Customizing Articles")
1216   :type gnus-article-treat-head-custom)
1217
1218 (defcustom gnus-treat-hide-signature nil
1219   "Hide the signature.
1220 Valid values are nil, t, `head', `first', `last', an integer or a
1221 predicate.  See Info node `(gnus)Customizing Articles'."
1222   :group 'gnus-article-treat
1223   :link '(custom-manual "(gnus)Customizing Articles")
1224   :type gnus-article-treat-custom)
1225
1226 (defcustom gnus-treat-fill-article nil
1227   "Fill the article.
1228 Valid values are nil, t, `head', `first', `last', an integer or a
1229 predicate.  See Info node `(gnus)Customizing Articles'."
1230   :group 'gnus-article-treat
1231   :link '(custom-manual "(gnus)Customizing Articles")
1232   :type gnus-article-treat-custom)
1233
1234 (defcustom gnus-treat-hide-citation nil
1235   "Hide cited text.
1236 Valid values are nil, t, `head', `first', `last', an integer or a
1237 predicate.  See Info node `(gnus)Customizing Articles'.
1238
1239 See `gnus-article-highlight-citation' for variables used to
1240 control what it hides."
1241   :group 'gnus-article-treat
1242   :link '(custom-manual "(gnus)Customizing Articles")
1243   :type gnus-article-treat-custom)
1244
1245 (defcustom gnus-treat-hide-citation-maybe nil
1246   "Hide cited text according to certain conditions.
1247 Valid values are nil, t, `head', `first', `last', an integer or a
1248 predicate.  See Info node `(gnus)Customizing Articles'.
1249
1250 See `gnus-cite-hide-percentage' and `gnus-cite-hide-absolute' for
1251 how to control what it hides."
1252   :group 'gnus-article-treat
1253   :link '(custom-manual "(gnus)Customizing Articles")
1254   :type gnus-article-treat-custom)
1255
1256 (defcustom gnus-treat-strip-list-identifiers 'head
1257   "Strip list identifiers from `gnus-list-identifiers`.
1258 Valid values are nil, t, `head', `first', `last', an integer or a
1259 predicate.  See Info node `(gnus)Customizing Articles'."
1260   :version "21.1"
1261   :group 'gnus-article-treat
1262   :link '(custom-manual "(gnus)Customizing Articles")
1263   :type gnus-article-treat-custom)
1264
1265 (gnus-define-group-parameter
1266  list-identifier
1267  :variable-document
1268  "Alist of regexps and correspondent identifiers."
1269  :variable-group gnus-article-washing
1270  :parameter-type
1271  '(choice :tag "Identifier"
1272           :value nil
1273           (symbol :tag "Item in `gnus-list-identifiers'" none)
1274           regexp
1275           (const :tag "None" nil))
1276  :parameter-document
1277  "If non-nil, specify how to remove `identifiers' from articles' subject.
1278
1279 Any symbol is used to look up a regular expression to match the
1280 banner in `gnus-list-identifiers'.  A string is used as a regular
1281 expression to match the identifier directly.")
1282
1283 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1284                         "Gnus 5.10 (Emacs 22.1)")
1285
1286 (defcustom gnus-treat-strip-pem nil
1287   "Strip PEM signatures.
1288 Valid values are nil, t, `head', `first', `last', an integer or a
1289 predicate.  See Info node `(gnus)Customizing Articles'."
1290   :group 'gnus-article-treat
1291   :link '(custom-manual "(gnus)Customizing Articles")
1292   :type gnus-article-treat-custom)
1293
1294 (defcustom gnus-treat-strip-banner t
1295   "Strip banners from articles.
1296 The banner to be stripped is specified in the `banner' group parameter.
1297 Valid values are nil, t, `head', `first', `last', an integer or a
1298 predicate.  See Info node `(gnus)Customizing Articles'."
1299   :group 'gnus-article-treat
1300   :link '(custom-manual "(gnus)Customizing Articles")
1301   :type gnus-article-treat-custom)
1302
1303 (defcustom gnus-treat-highlight-headers 'head
1304   "Highlight the headers.
1305 Valid values are nil, t, `head', `first', `last', an integer or a
1306 predicate.  See Info node `(gnus)Customizing Articles'."
1307   :group 'gnus-article-treat
1308   :link '(custom-manual "(gnus)Customizing Articles")
1309   :type gnus-article-treat-head-custom)
1310 (put 'gnus-treat-highlight-headers 'highlight t)
1311
1312 (defcustom gnus-treat-highlight-citation t
1313   "Highlight cited text.
1314 Valid values are nil, t, `head', `first', `last', an integer or a
1315 predicate.  See Info node `(gnus)Customizing Articles'."
1316   :group 'gnus-article-treat
1317   :link '(custom-manual "(gnus)Customizing Articles")
1318   :type gnus-article-treat-custom)
1319 (put 'gnus-treat-highlight-citation 'highlight t)
1320
1321 (defcustom gnus-treat-strip-headers-in-body t
1322   "Strip the X-No-Archive header line from the beginning of the body.
1323 Valid values are nil, t, `head', `first', `last', an integer or a
1324 predicate.  See Info node `(gnus)Customizing Articles'."
1325   :version "21.1"
1326   :group 'gnus-article-treat
1327   :link '(custom-manual "(gnus)Customizing Articles")
1328   :type gnus-article-treat-custom)
1329
1330 (defcustom gnus-treat-strip-trailing-blank-lines nil
1331   "Strip trailing blank lines.
1332 Valid values are nil, t, `head', `first', `last', an integer or a
1333 predicate.  See Info node `(gnus)Customizing Articles'.
1334
1335 When set to t, it also strips trailing blanks in all MIME parts.
1336 Consider to use `last' instead."
1337   :group 'gnus-article-treat
1338   :link '(custom-manual "(gnus)Customizing Articles")
1339   :type gnus-article-treat-custom)
1340
1341 (defcustom gnus-treat-strip-leading-blank-lines nil
1342   "Strip leading blank lines.
1343 Valid values are nil, t, `head', `first', `last', an integer or a
1344 predicate.  See Info node `(gnus)Customizing Articles'.
1345
1346 When set to t, it also strips trailing blanks in all MIME parts."
1347   :group 'gnus-article-treat
1348   :link '(custom-manual "(gnus)Customizing Articles")
1349   :type gnus-article-treat-custom)
1350
1351 (defcustom gnus-treat-strip-multiple-blank-lines nil
1352   "Strip multiple blank lines.
1353 Valid values are nil, t, `head', `first', `last', an integer or a
1354 predicate.  See Info node `(gnus)Customizing Articles'."
1355   :group 'gnus-article-treat
1356   :link '(custom-manual "(gnus)Customizing Articles")
1357   :type gnus-article-treat-custom)
1358
1359 (defcustom gnus-treat-unfold-headers 'head
1360   "Unfold folded header lines.
1361 Valid values are nil, t, `head', `first', `last', an integer or a
1362 predicate.  See Info node `(gnus)Customizing Articles'."
1363   :version "22.1"
1364   :group 'gnus-article-treat
1365   :link '(custom-manual "(gnus)Customizing Articles")
1366   :type gnus-article-treat-custom)
1367
1368 (defcustom gnus-article-unfold-long-headers nil
1369   "If non-nil, allow unfolding headers even if the header is long.
1370 If it is a regexp, only long headers matching this regexp are unfolded.
1371 If it is t, all long headers are unfolded.
1372
1373 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1374   :version "23.1" ;; No Gnus
1375   :group 'gnus-article-treat
1376   :type '(choice (const nil)
1377                  (const :tag "all" t)
1378                  (regexp)))
1379
1380 (defcustom gnus-treat-fold-headers nil
1381   "Fold headers.
1382 Valid values are nil, t, `head', `first', `last', an integer or a
1383 predicate.  See Info node `(gnus)Customizing Articles'."
1384   :version "22.1"
1385   :group 'gnus-article-treat
1386   :link '(custom-manual "(gnus)Customizing Articles")
1387   :type gnus-article-treat-custom)
1388
1389 (defcustom gnus-treat-fold-newsgroups 'head
1390   "Fold the Newsgroups and Followup-To headers.
1391 Valid values are nil, t, `head', `first', `last', an integer or a
1392 predicate.  See Info node `(gnus)Customizing Articles'."
1393   :version "22.1"
1394   :group 'gnus-article-treat
1395   :link '(custom-manual "(gnus)Customizing Articles")
1396   :type gnus-article-treat-custom)
1397
1398 (defcustom gnus-treat-overstrike t
1399   "Treat overstrike highlighting.
1400 Valid values are nil, t, `head', `first', `last', an integer or a
1401 predicate.  See Info node `(gnus)Customizing Articles'."
1402   :group 'gnus-article-treat
1403   :link '(custom-manual "(gnus)Customizing Articles")
1404   :type gnus-article-treat-custom)
1405 (put 'gnus-treat-overstrike 'highlight t)
1406
1407 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1408   "Treat ANSI SGR control sequences.
1409 Valid values are nil, t, `head', `first', `last', an integer or a
1410 predicate.  See Info node `(gnus)Customizing Articles'."
1411   :group 'gnus-article-treat
1412   :link '(custom-manual "(gnus)Customizing Articles")
1413   :type gnus-article-treat-custom)
1414
1415 (make-obsolete-variable 'gnus-treat-display-xface
1416                         'gnus-treat-display-x-face "Emacs 22.1")
1417
1418 (defcustom gnus-treat-display-x-face
1419   (and (not noninteractive)
1420        (gnus-image-type-available-p 'xbm)
1421        (if (featurep 'xemacs)
1422            (featurep 'xface)
1423          (condition-case nil
1424              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1425                   (executable-find "icontopbm"))
1426            ;; shell-command-to-string may signal an error, e.g. if
1427            ;; shell-file-name is not found.
1428            (error nil)))
1429        'head)
1430   "Display X-Face headers.
1431 Valid values are nil and `head'.
1432 See Info node `(gnus)Customizing Articles' and Info node
1433 `(gnus)X-Face' for details."
1434   :group 'gnus-article-treat
1435   :version "21.1"
1436   :link '(custom-manual "(gnus)Customizing Articles")
1437   :link '(custom-manual "(gnus)X-Face")
1438   :type gnus-article-treat-head-custom
1439   :set (lambda (symbol value)
1440          (set-default
1441           symbol
1442           (cond ((or (boundp symbol) (get symbol 'saved-value))
1443                  value)
1444                 ((boundp 'gnus-treat-display-xface)
1445                  (message "\
1446 ** gnus-treat-display-xface is an obsolete variable;\
1447  use gnus-treat-display-x-face instead")
1448                  (default-value 'gnus-treat-display-xface))
1449                 ((get 'gnus-treat-display-xface 'saved-value)
1450                  (message "\
1451 ** gnus-treat-display-xface is an obsolete variable;\
1452  use gnus-treat-display-x-face instead")
1453                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1454                 (t
1455                  value)))))
1456 (put 'gnus-treat-display-x-face 'highlight t)
1457
1458 (defcustom gnus-treat-display-face
1459   (and (not noninteractive)
1460        (gnus-image-type-available-p 'png)
1461        'head)
1462   "Display Face headers.
1463 Valid values are nil, t, `head', `first', `last', an integer or a
1464 predicate.  See Info node `(gnus)Customizing Articles' and Info
1465 node `(gnus)Face' for details."
1466   :group 'gnus-article-treat
1467   :version "22.1"
1468   :link '(custom-manual "(gnus)Customizing Articles")
1469   :link '(custom-manual "(gnus)X-Face")
1470   :type gnus-article-treat-head-custom)
1471 (put 'gnus-treat-display-face 'highlight t)
1472
1473 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1474   "Display smileys.
1475 Valid values are nil, t, `head', `first', `last', an integer or a
1476 predicate.  See Info node `(gnus)Customizing Articles' and Info
1477 node `(gnus)Smileys' for details."
1478   :group 'gnus-article-treat
1479   :version "21.1"
1480   :link '(custom-manual "(gnus)Customizing Articles")
1481   :link '(custom-manual "(gnus)Smileys")
1482   :type gnus-article-treat-custom)
1483 (put 'gnus-treat-display-smileys 'highlight t)
1484
1485 (defcustom gnus-treat-from-picon
1486   (if (and (gnus-image-type-available-p 'xpm)
1487            (gnus-picons-installed-p))
1488       'head nil)
1489   "Display picons in the From header.
1490 Valid values are nil, t, `head', `first', `last', an integer or a
1491 predicate.  See Info node `(gnus)Customizing Articles' and Info
1492 node `(gnus)Picons' for details."
1493   :version "22.1"
1494   :group 'gnus-article-treat
1495   :group 'gnus-picon
1496   :link '(custom-manual "(gnus)Customizing Articles")
1497   :link '(custom-manual "(gnus)Picons")
1498   :type gnus-article-treat-head-custom)
1499 (put 'gnus-treat-from-picon 'highlight t)
1500
1501 (defcustom gnus-treat-mail-picon
1502   (if (and (gnus-image-type-available-p 'xpm)
1503            (gnus-picons-installed-p))
1504       'head nil)
1505   "Display picons in To and Cc headers.
1506 Valid values are nil, t, `head', `first', `last', an integer or a
1507 predicate.  See Info node `(gnus)Customizing Articles' and Info
1508 node `(gnus)Picons' for details."
1509   :version "22.1"
1510   :group 'gnus-article-treat
1511   :group 'gnus-picon
1512   :link '(custom-manual "(gnus)Customizing Articles")
1513   :link '(custom-manual "(gnus)Picons")
1514   :type gnus-article-treat-head-custom)
1515 (put 'gnus-treat-mail-picon 'highlight t)
1516
1517 (defcustom gnus-treat-newsgroups-picon
1518   (if (and (gnus-image-type-available-p 'xpm)
1519            (gnus-picons-installed-p))
1520       'head nil)
1521   "Display picons in the Newsgroups and Followup-To headers.
1522 Valid values are nil, t, `head', `first', `last', an integer or a
1523 predicate.  See Info node `(gnus)Customizing Articles' and Info
1524 node `(gnus)Picons' for details."
1525   :version "22.1"
1526   :group 'gnus-article-treat
1527   :group 'gnus-picon
1528   :link '(custom-manual "(gnus)Customizing Articles")
1529   :link '(custom-manual "(gnus)Picons")
1530   :type gnus-article-treat-head-custom)
1531 (put 'gnus-treat-newsgroups-picon 'highlight t)
1532
1533 (defcustom gnus-treat-from-gravatar nil
1534   "Display gravatars in the From header.
1535 Valid values are nil, t, `head', `first', `last', an integer or a
1536 predicate.  See Info node `(gnus)Customizing Articles' and Info
1537 node `(gnus)Gravatars' for details."
1538   :version "24.1"
1539   :group 'gnus-article-treat
1540   :group 'gnus-gravatar
1541   :link '(custom-manual "(gnus)Customizing Articles")
1542   :link '(custom-manual "(gnus)Gravatars")
1543   :type gnus-article-treat-head-custom)
1544 (put 'gnus-treat-from-gravatar 'highlight t)
1545
1546 (defcustom gnus-treat-mail-gravatar nil
1547   "Display gravatars in To and Cc headers.
1548 Valid values are nil, t, `head', `first', `last', an integer or a
1549 predicate.  See Info node `(gnus)Customizing Articles' and Info
1550 node `(gnus)Gravatars' for details."
1551   :version "24.1"
1552   :group 'gnus-article-treat
1553   :group 'gnus-gravatar
1554   :link '(custom-manual "(gnus)Customizing Articles")
1555   :link '(custom-manual "(gnus)Gravatars")
1556   :type gnus-article-treat-head-custom)
1557 (put 'gnus-treat-mail-gravatar 'highlight t)
1558
1559 (defcustom gnus-treat-body-boundary
1560   (if (or gnus-treat-newsgroups-picon
1561           gnus-treat-mail-picon
1562           gnus-treat-from-picon
1563           gnus-treat-from-gravatar
1564           gnus-treat-mail-gravatar)
1565       ;; If there's much decoration, the user might prefer a boundary.
1566       'head
1567     nil)
1568   "Draw a boundary at the end of the headers.
1569 Valid values are nil and `head'.
1570 See Info node `(gnus)Customizing Articles' for details."
1571   :version "22.1"
1572   :group 'gnus-article-treat
1573   :link '(custom-manual "(gnus)Customizing Articles")
1574   :type gnus-article-treat-head-custom)
1575
1576 (defcustom gnus-treat-capitalize-sentences nil
1577   "Capitalize sentence-starting words.
1578 Valid values are nil, t, `head', `first', `last', an integer or a
1579 predicate.  See Info node `(gnus)Customizing Articles'."
1580   :version "21.1"
1581   :group 'gnus-article-treat
1582   :link '(custom-manual "(gnus)Customizing Articles")
1583   :type gnus-article-treat-custom)
1584
1585 (defcustom gnus-treat-wash-html nil
1586   "Format as HTML.
1587 Valid values are nil, t, `head', `first', `last', an integer or a
1588 predicate.  See Info node `(gnus)Customizing Articles'."
1589   :version "22.1"
1590   :group 'gnus-article-treat
1591   :link '(custom-manual "(gnus)Customizing Articles")
1592   :type gnus-article-treat-custom)
1593
1594 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1595   "Fill long lines.
1596 Valid values are nil, t, `head', `first', `last', an integer or a
1597 predicate.  See Info node `(gnus)Customizing Articles'."
1598   :version "24.1"
1599   :group 'gnus-article-treat
1600   :link '(custom-manual "(gnus)Customizing Articles")
1601   :type gnus-article-treat-custom)
1602
1603 (defcustom gnus-treat-x-pgp-sig nil
1604   "Verify X-PGP-Sig.
1605 To automatically treat X-PGP-Sig, set it to head.
1606 Valid values are nil, t, `head', `first', `last', an integer or a
1607 predicate.  See Info node `(gnus)Customizing Articles'."
1608   :version "22.1"
1609   :group 'gnus-article-treat
1610   :group 'mime-security
1611   :link '(custom-manual "(gnus)Customizing Articles")
1612   :type gnus-article-treat-custom)
1613
1614 (defvar gnus-article-encrypt-protocol-alist
1615   '(("PGP" . mml2015-self-encrypt)))
1616
1617 ;; Set to nil if more than one protocol added to
1618 ;; gnus-article-encrypt-protocol-alist.
1619 (defcustom gnus-article-encrypt-protocol "PGP"
1620   "The protocol used for encrypt articles.
1621 It is a string, such as \"PGP\". If nil, ask user."
1622   :version "22.1"
1623   :type 'string
1624   :group 'mime-security)
1625
1626 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1627                               (mm-coding-system-p 'utf-8)
1628                               (executable-find idna-program))
1629   "Whether IDNA decoding of headers is used when viewing messages.
1630 This requires GNU Libidn, and by default only enabled if it is found."
1631   :version "22.1"
1632   :group 'gnus-article-headers
1633   :type 'boolean)
1634
1635 (defcustom gnus-article-over-scroll nil
1636   "If non-nil, allow scrolling the article buffer even when there no more text."
1637   :version "22.1"
1638   :group 'gnus-article
1639   :type 'boolean)
1640
1641 (defcustom gnus-inhibit-images nil
1642   "Non-nil means inhibit displaying of images inline in the article body."
1643   :version "24.1"
1644   :group 'gnus-article
1645   :type 'boolean)
1646
1647 (defcustom gnus-blocked-images 'gnus-block-private-groups
1648   "Images that have URLs matching this regexp will be blocked.
1649 This can also be a function to be evaluated.  If so, it will be
1650 called with the group name as the parameter, and should return a
1651 regexp."
1652   :version "24.1"
1653   :group 'gnus-art
1654   :type 'regexp)
1655
1656 ;;; Internal variables
1657
1658 (defvar gnus-english-month-names
1659   '("January" "February" "March" "April" "May" "June" "July" "August"
1660     "September" "October" "November" "December"))
1661
1662 (defvar article-goto-body-goes-to-point-min-p nil)
1663 (defvar gnus-article-wash-types nil)
1664 (defvar gnus-article-emphasis-alist nil)
1665 (defvar gnus-article-image-alist nil)
1666
1667 (defvar gnus-article-mime-handle-alist-1 nil)
1668 (defvar gnus-treatment-function-alist
1669   '((gnus-treat-strip-cr gnus-article-remove-cr)
1670     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1671     (gnus-treat-strip-banner gnus-article-strip-banner)
1672     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1673     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1674     (gnus-treat-buttonize gnus-article-add-buttons)
1675     (gnus-treat-fill-article gnus-article-fill-cited-article)
1676     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1677     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1678     (gnus-treat-display-x-face gnus-article-display-x-face)
1679     (gnus-treat-display-face gnus-article-display-face)
1680     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1681     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1682     (gnus-treat-hide-signature gnus-article-hide-signature)
1683     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1684     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1685     (gnus-treat-from-picon gnus-treat-from-picon)
1686     (gnus-treat-mail-picon gnus-treat-mail-picon)
1687     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1688     (gnus-treat-strip-pem gnus-article-hide-pem)
1689     (gnus-treat-date gnus-article-treat-date)
1690     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1691     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1692     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1693     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1694     (gnus-treat-strip-trailing-blank-lines
1695      gnus-article-remove-trailing-blank-lines)
1696     (gnus-treat-strip-leading-blank-lines
1697      gnus-article-strip-leading-blank-lines)
1698     (gnus-treat-strip-multiple-blank-lines
1699      gnus-article-strip-multiple-blank-lines)
1700     (gnus-treat-overstrike gnus-article-treat-overstrike)
1701     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1702     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1703     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1704     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1705     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1706     (gnus-treat-display-smileys gnus-treat-smiley)
1707     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1708     (gnus-treat-wash-html gnus-article-wash-html)
1709     (gnus-treat-emphasize gnus-article-emphasize)
1710     (gnus-treat-hide-citation gnus-article-hide-citation)
1711     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1712     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1713     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1714
1715 (defvar gnus-article-mime-handle-alist nil)
1716 (defvar article-lapsed-timer nil)
1717 (defvar gnus-article-current-summary nil)
1718
1719 (defvar gnus-article-mode-syntax-table
1720   (let ((table (copy-syntax-table text-mode-syntax-table)))
1721     ;; This causes the citation match run O(2^n).
1722     ;; (modify-syntax-entry ?- "w" table)
1723     (modify-syntax-entry ?> ")<" table)
1724     (modify-syntax-entry ?< "(>" table)
1725     ;; make M-. in article buffers work for `foo' strings
1726     (modify-syntax-entry ?' " " table)
1727     (modify-syntax-entry ?` " " table)
1728     table)
1729   "Syntax table used in article mode buffers.
1730 Initialized from `text-mode-syntax-table.")
1731
1732 (defvar gnus-save-article-buffer nil)
1733
1734 (defvar gnus-number-of-articles-to-be-saved nil)
1735
1736 (defvar gnus-inhibit-hiding nil)
1737
1738 (defvar gnus-article-edit-mode nil)
1739
1740 ;;; Macros for dealing with the article buffer.
1741
1742 (defmacro gnus-with-article-headers (&rest forms)
1743   `(with-current-buffer gnus-article-buffer
1744      (save-restriction
1745        (let ((inhibit-read-only t)
1746              (inhibit-point-motion-hooks t)
1747              (case-fold-search t))
1748          (article-narrow-to-head)
1749          ,@forms))))
1750
1751 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1752 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1753
1754 (defmacro gnus-with-article-buffer (&rest forms)
1755   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1756      (with-current-buffer gnus-article-buffer
1757        (let ((inhibit-read-only t))
1758          ,@forms))))
1759
1760 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1761 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1762
1763 (defun gnus-article-goto-header (header)
1764   "Go to HEADER, which is a regular expression."
1765   (re-search-forward (concat "^\\(" header "\\):") nil t))
1766
1767 (defsubst gnus-article-hide-text (b e props)
1768   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1769   (gnus-add-text-properties-when 'article-type nil b e props)
1770   (when (memq 'intangible props)
1771     (put-text-property
1772      (max (1- b) (point-min))
1773      b 'intangible (cddr (memq 'intangible props)))))
1774
1775 (defsubst gnus-article-unhide-text (b e)
1776   "Remove hidden text properties from region between B and E."
1777   (remove-text-properties b e gnus-hidden-properties)
1778   (when (memq 'intangible gnus-hidden-properties)
1779     (put-text-property (max (1- b) (point-min))
1780                        b 'intangible nil)))
1781
1782 (defun gnus-article-hide-text-type (b e type)
1783   "Hide text of TYPE between B and E."
1784   (gnus-add-wash-type type)
1785   (gnus-article-hide-text
1786    b e (cons 'article-type (cons type gnus-hidden-properties))))
1787
1788 (defun gnus-article-unhide-text-type (b e type)
1789   "Unhide text of TYPE between B and E."
1790   (gnus-delete-wash-type type)
1791   (remove-text-properties
1792    b e (cons 'article-type (cons type gnus-hidden-properties)))
1793   (when (memq 'intangible gnus-hidden-properties)
1794     (put-text-property (max (1- b) (point-min))
1795                        b 'intangible nil)))
1796
1797 (defun gnus-article-hide-text-of-type (type)
1798   "Hide text of TYPE in the current buffer."
1799   (save-excursion
1800     (let ((b (point-min))
1801           (e (point-max)))
1802       (while (setq b (text-property-any b e 'article-type type))
1803         (add-text-properties b (incf b) gnus-hidden-properties)))))
1804
1805 (defun gnus-article-delete-text-of-type (type)
1806   "Delete text of TYPE in the current buffer."
1807   (save-excursion
1808     (let ((b (point-min)))
1809       (if (eq type 'multipart)
1810           ;; Remove MIME buttons associated with multipart/alternative parts.
1811           (progn
1812             (goto-char b)
1813             (while (if (get-text-property (point) 'gnus-part)
1814                        (setq b (point))
1815                      (when (setq b (next-single-property-change (point)
1816                                                                 'gnus-part))
1817                        (goto-char b)
1818                        t))
1819               (end-of-line)
1820               (skip-chars-forward "\n")
1821               (when (eq (get-text-property b 'article-type) 'multipart)
1822                 (delete-region b (point)))))
1823         (while (setq b (text-property-any b (point-max) 'article-type type))
1824           (delete-region
1825            b (or (text-property-not-all b (point-max) 'article-type type)
1826                  (point-max))))))))
1827
1828 (defun gnus-article-delete-invisible-text ()
1829   "Delete all invisible text in the current buffer."
1830   (save-excursion
1831     (let ((b (point-min)))
1832       (while (setq b (text-property-any b (point-max) 'invisible t))
1833         (delete-region
1834          b (or (text-property-not-all b (point-max) 'invisible t)
1835                (point-max)))))))
1836
1837 (defun gnus-article-text-type-exists-p (type)
1838   "Say whether any text of type TYPE exists in the buffer."
1839   (text-property-any (point-min) (point-max) 'article-type type))
1840
1841 (defsubst gnus-article-header-rank ()
1842   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1843   (let ((list gnus-sorted-header-list)
1844         (i 1))
1845     (while list
1846       (if (looking-at (car list))
1847           (setq list nil)
1848         (setq list (cdr list))
1849         (incf i)))
1850       i))
1851
1852 (defun article-hide-headers (&optional arg delete)
1853   "Hide unwanted headers and possibly sort them as well."
1854   (interactive)
1855   ;; This function might be inhibited.
1856   (unless gnus-inhibit-hiding
1857     (let ((inhibit-read-only t)
1858           (case-fold-search t)
1859           (max (1+ (length gnus-sorted-header-list)))
1860           (inhibit-point-motion-hooks t)
1861           (cur (current-buffer))
1862           ignored visible beg)
1863       (save-excursion
1864         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1865         ;; group parameters, so we should go to the summary buffer.
1866         (when (prog1
1867                   (condition-case nil
1868                       (progn (set-buffer gnus-summary-buffer) t)
1869                     (error nil))
1870                 (setq ignored (when (not gnus-visible-headers)
1871                                 (cond ((stringp gnus-ignored-headers)
1872                                        gnus-ignored-headers)
1873                                       ((listp gnus-ignored-headers)
1874                                        (mapconcat 'identity
1875                                                   gnus-ignored-headers
1876                                                   "\\|"))))
1877                       visible (cond ((stringp gnus-visible-headers)
1878                                      gnus-visible-headers)
1879                                     ((and gnus-visible-headers
1880                                           (listp gnus-visible-headers))
1881                                      (mapconcat 'identity
1882                                                 gnus-visible-headers
1883                                                 "\\|")))))
1884           (set-buffer cur))
1885         (save-restriction
1886           ;; First we narrow to just the headers.
1887           (article-narrow-to-head)
1888           ;; Hide any "From " lines at the beginning of (mail) articles.
1889           (while (looking-at "From ")
1890             (forward-line 1))
1891           (unless (bobp)
1892             (delete-region (point-min) (point)))
1893           ;; Then treat the rest of the header lines.
1894           ;; Then we use the two regular expressions
1895           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1896           ;; select which header lines is to remain visible in the
1897           ;; article buffer.
1898           (while (re-search-forward "^[^ \t:]*:" nil t)
1899             (beginning-of-line)
1900             ;; Mark the rank of the header.
1901             (put-text-property
1902              (point) (1+ (point)) 'message-rank
1903              (if (or (and visible (looking-at visible))
1904                      (and ignored
1905                           (not (looking-at ignored))))
1906                  (gnus-article-header-rank)
1907                (+ 2 max)))
1908             (forward-line 1))
1909           (message-sort-headers-1)
1910           (when (setq beg (text-property-any
1911                            (point-min) (point-max) 'message-rank (+ 2 max)))
1912             ;; We delete the unwanted headers.
1913             (gnus-add-wash-type 'headers)
1914             (add-text-properties (point-min) (+ 5 (point-min))
1915                                  '(article-type headers dummy-invisible t))
1916             (delete-region beg (point-max))))))))
1917
1918 (defun article-hide-boring-headers (&optional arg)
1919   "Toggle hiding of headers that aren't very interesting.
1920 If given a negative prefix, always show; if given a positive prefix,
1921 always hide."
1922   (interactive (gnus-article-hidden-arg))
1923   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1924              (not gnus-show-all-headers))
1925     (save-excursion
1926       (save-restriction
1927         (let ((inhibit-read-only t)
1928               (inhibit-point-motion-hooks t))
1929           (article-narrow-to-head)
1930           (dolist (elem gnus-boring-article-headers)
1931             (goto-char (point-min))
1932             (cond
1933              ;; Hide empty headers.
1934              ((eq elem 'empty)
1935               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1936                 (forward-line -1)
1937                 (gnus-article-hide-text-type
1938                  (point-at-bol)
1939                  (progn
1940                    (end-of-line)
1941                    (if (re-search-forward "^[^ \t]" nil t)
1942                        (match-beginning 0)
1943                      (point-max)))
1944                  'boring-headers)))
1945              ;; Hide boring Newsgroups header.
1946              ((eq elem 'newsgroups)
1947               (when (gnus-string-equal
1948                      (gnus-fetch-field "newsgroups")
1949                      (gnus-group-real-name
1950                       (if (boundp 'gnus-newsgroup-name)
1951                           gnus-newsgroup-name
1952                         "")))
1953                 (gnus-article-hide-header "newsgroups")))
1954              ((eq elem 'to-address)
1955               (let ((to (message-fetch-field "to"))
1956                     (to-address
1957                      (gnus-parameter-to-address
1958                       (if (boundp 'gnus-newsgroup-name)
1959                           gnus-newsgroup-name ""))))
1960                 (when (and to to-address
1961                            (ignore-errors
1962                              (gnus-string-equal
1963                               ;; only one address in To
1964                               (nth 1 (mail-extract-address-components to))
1965                               to-address)))
1966                   (gnus-article-hide-header "to"))))
1967              ((eq elem 'to-list)
1968               (let ((to (message-fetch-field "to"))
1969                     (to-list
1970                      (gnus-parameter-to-list
1971                       (if (boundp 'gnus-newsgroup-name)
1972                           gnus-newsgroup-name ""))))
1973                 (when (and to to-list
1974                            (ignore-errors
1975                              (gnus-string-equal
1976                               ;; only one address in To
1977                               (nth 1 (mail-extract-address-components to))
1978                               to-list)))
1979                   (gnus-article-hide-header "to"))))
1980              ((eq elem 'cc-list)
1981               (let ((cc (message-fetch-field "cc"))
1982                     (to-list
1983                      (gnus-parameter-to-list
1984                       (if (boundp 'gnus-newsgroup-name)
1985                           gnus-newsgroup-name ""))))
1986                 (when (and cc to-list
1987                            (ignore-errors
1988                              (gnus-string-equal
1989                               ;; only one address in CC
1990                               (nth 1 (mail-extract-address-components cc))
1991                               to-list)))
1992                   (gnus-article-hide-header "cc"))))
1993              ((eq elem 'followup-to)
1994               (when (gnus-string-equal
1995                      (message-fetch-field "followup-to")
1996                      (message-fetch-field "newsgroups"))
1997                 (gnus-article-hide-header "followup-to")))
1998              ((eq elem 'reply-to)
1999               (if (gnus-group-find-parameter
2000                    gnus-newsgroup-name 'broken-reply-to)
2001                   (gnus-article-hide-header "reply-to")
2002                 (let ((from (message-fetch-field "from"))
2003                       (reply-to (message-fetch-field "reply-to")))
2004                   (when
2005                       (and
2006                        from reply-to
2007                        (ignore-errors
2008                          (equal
2009                           (sort (mapcar
2010                                  (lambda (x) (downcase (cadr x)))
2011                                  (mail-extract-address-components from t))
2012                                 'string<)
2013                           (sort (mapcar
2014                                  (lambda (x) (downcase (cadr x)))
2015                                  (mail-extract-address-components reply-to t))
2016                                 'string<))))
2017                     (gnus-article-hide-header "reply-to")))))
2018              ((eq elem 'date)
2019               (let ((date (with-current-buffer gnus-original-article-buffer
2020                             ;; If date in `gnus-article-buffer' is localized
2021                             ;; (`gnus-treat-date-user-defined'),
2022                             ;; `days-between' might fail.
2023                             (message-fetch-field "date"))))
2024                 (when (and date
2025                            (< (days-between (current-time-string) date)
2026                               4))
2027                   (gnus-article-hide-header "date"))))
2028              ((eq elem 'long-to)
2029               (let ((to (message-fetch-field "to"))
2030                     (cc (message-fetch-field "cc")))
2031                 (when (> (length to) 1024)
2032                   (gnus-article-hide-header "to"))
2033                 (when (> (length cc) 1024)
2034                   (gnus-article-hide-header "cc"))))
2035              ((eq elem 'many-to)
2036               (let ((to-count 0)
2037                     (cc-count 0))
2038                 (goto-char (point-min))
2039                 (while (re-search-forward "^to:" nil t)
2040                   (setq to-count (1+ to-count)))
2041                 (when (> to-count 1)
2042                   (while (> to-count 0)
2043                     (goto-char (point-min))
2044                     (save-restriction
2045                       (re-search-forward "^to:" nil nil to-count)
2046                       (forward-line -1)
2047                       (narrow-to-region (point) (point-max))
2048                       (gnus-article-hide-header "to"))
2049                     (setq to-count (1- to-count))))
2050                 (goto-char (point-min))
2051                 (while (re-search-forward "^cc:" nil t)
2052                   (setq cc-count (1+ cc-count)))
2053                 (when (> cc-count 1)
2054                   (while (> cc-count 0)
2055                     (goto-char (point-min))
2056                     (save-restriction
2057                       (re-search-forward "^cc:" nil nil cc-count)
2058                       (forward-line -1)
2059                       (narrow-to-region (point) (point-max))
2060                       (gnus-article-hide-header "cc"))
2061                     (setq cc-count (1- cc-count)))))))))))))
2062
2063 (defun gnus-article-hide-header (header)
2064   (save-excursion
2065     (goto-char (point-min))
2066     (when (re-search-forward (concat "^" header ":") nil t)
2067       (gnus-article-hide-text-type
2068        (point-at-bol)
2069        (progn
2070          (end-of-line)
2071          (if (re-search-forward "^[^ \t]" nil t)
2072              (match-beginning 0)
2073            (point-max)))
2074        'boring-headers))))
2075
2076 (defvar gnus-article-normalized-header-length 40
2077   "Length of normalized headers.")
2078
2079 (defun article-normalize-headers ()
2080   "Make all header lines 40 characters long."
2081   (interactive)
2082   (let ((inhibit-read-only t)
2083         column)
2084     (save-excursion
2085       (save-restriction
2086         (article-narrow-to-head)
2087         (while (not (eobp))
2088           (cond
2089            ((< (setq column (- (point-at-eol) (point)))
2090                gnus-article-normalized-header-length)
2091             (end-of-line)
2092             (insert (make-string
2093                      (- gnus-article-normalized-header-length column)
2094                      ? )))
2095            ((> column gnus-article-normalized-header-length)
2096             (gnus-put-text-property
2097              (progn
2098                (forward-char gnus-article-normalized-header-length)
2099                (point))
2100              (point-at-eol)
2101              'invisible t))
2102            (t
2103             ;; Do nothing.
2104             ))
2105           (forward-line 1))))))
2106
2107 (defun article-treat-dumbquotes ()
2108   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2109 Note that this function guesses whether a character is a sm*rtq**t* or
2110 not, so it should only be used interactively.
2111
2112 Sm*rtq**t*s are M****s***'s unilateral extension to the
2113 iso-8859-1 character map in an attempt to provide more quoting
2114 characters.  If you see something like \\222 or \\264 where
2115 you're expecting some kind of apostrophe or quotation mark, then
2116 try this wash."
2117   (interactive)
2118   (article-translate-strings gnus-article-dumbquotes-map))
2119
2120 (defvar org-entities)
2121
2122 (defun article-treat-non-ascii ()
2123   "Translate many Unicode characters into their ASCII equivalents."
2124   (interactive)
2125   (require 'org-entities)
2126   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2127     (dolist (elem org-entities)
2128       (when (and (listp elem)
2129                  (= (length (nth 6 elem)) 1))
2130         (if (featurep 'xemacs)
2131             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2132           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2133     (save-excursion
2134       (when (article-goto-body)
2135         (let ((inhibit-read-only t)
2136               replace props)
2137           (while (not (eobp))
2138             (if (not (setq replace (if (featurep 'xemacs)
2139                                        (get-char-table (following-char) table)
2140                                      (aref table (following-char)))))
2141                 (forward-char 1)
2142               (if (prog1
2143                       (setq props (text-properties-at (point)))
2144                     (delete-char 1))
2145                   (add-text-properties (point) (progn (insert replace) (point))
2146                                        props)
2147                 (insert replace)))))))))
2148
2149 (defun article-translate-characters (from to)
2150   "Translate all characters in the body of the article according to FROM and TO.
2151 FROM is a string of characters to translate from; to is a string of
2152 characters to translate to."
2153   (save-excursion
2154     (when (article-goto-body)
2155       (let ((inhibit-read-only t)
2156             (x (make-string 225 ?x))
2157             (i -1))
2158         (while (< (incf i) (length x))
2159           (aset x i i))
2160         (setq i 0)
2161         (while (< i (length from))
2162           (aset x (aref from i) (aref to i))
2163           (incf i))
2164         (translate-region (point) (point-max) x)))))
2165
2166 (defun article-translate-strings (map)
2167   "Translate all string in the body of the article according to MAP.
2168 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2169   (save-excursion
2170     (when (article-goto-body)
2171       (let ((inhibit-read-only t))
2172         (dolist (elem map)
2173           (let ((from (car elem))
2174                 (to (cadr elem)))
2175             (save-excursion
2176               (if (stringp from)
2177                   (while (search-forward from nil t)
2178                     (replace-match to))
2179                 (while (not (eobp))
2180                   (if (eq (following-char) from)
2181                       (progn
2182                         (delete-char 1)
2183                         (insert to))
2184                     (forward-char 1)))))))))))
2185
2186 (defun article-treat-overstrike ()
2187   "Translate overstrikes into bold text."
2188   (interactive)
2189   (save-excursion
2190     (when (article-goto-body)
2191       (let ((inhibit-read-only t))
2192         (while (search-forward "\b" nil t)
2193           (let ((next (char-after))
2194                 (previous (char-after (- (point) 2))))
2195             ;; We do the boldification/underlining by hiding the
2196             ;; overstrikes and putting the proper text property
2197             ;; on the letters.
2198             (cond
2199              ((eq next previous)
2200               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2201               (put-text-property (point) (1+ (point)) 'face 'bold))
2202              ((eq next ?_)
2203               (gnus-article-hide-text-type
2204                (1- (point)) (1+ (point)) 'overstrike)
2205               (put-text-property
2206                (- (point) 2) (1- (point)) 'face 'underline))
2207              ((eq previous ?_)
2208               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2209               (put-text-property
2210                (point) (1+ (point)) 'face 'underline)))))))))
2211
2212 (defun article-treat-ansi-sequences ()
2213   "Translate ANSI SGR control sequences into overlays or extents."
2214   (interactive)
2215   (save-excursion
2216     (when (article-goto-body)
2217       (let ((inhibit-read-only t))
2218         (ansi-color-apply-on-region (point) (point-max))))))
2219
2220 (defun gnus-article-treat-unfold-headers ()
2221   "Unfold folded message headers.
2222 Only the headers that fit into the current window width will be
2223 unfolded."
2224   (interactive)
2225   (gnus-with-article-headers
2226     (let (length)
2227       (while (not (eobp))
2228         (save-restriction
2229           (mail-header-narrow-to-field)
2230           (let* ((header (buffer-string))
2231                  (unfoldable
2232                   (or (equal gnus-article-unfold-long-headers t)
2233                       (and (stringp gnus-article-unfold-long-headers)
2234                            (string-match gnus-article-unfold-long-headers
2235                                          header)))))
2236             (with-temp-buffer
2237               (insert header)
2238               (goto-char (point-min))
2239               (while (re-search-forward "\n[\t ]" nil t)
2240                 (replace-match " " t t)))
2241             (setq length (- (point-max) (point-min) 1))
2242             (when (or unfoldable
2243                       (< length (window-width)))
2244               (while (re-search-forward "\n[\t ]" nil t)
2245                 (replace-match " " t t))))
2246           (goto-char (point-max)))))))
2247
2248 (defun gnus-article-treat-fold-headers ()
2249   "Fold message headers."
2250   (interactive)
2251   (gnus-with-article-headers
2252     (while (not (eobp))
2253       (save-restriction
2254         (mail-header-narrow-to-field)
2255         (mail-header-fold-field)
2256         (goto-char (point-max))))))
2257
2258 (defun gnus-treat-smiley ()
2259   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2260   (interactive)
2261   (gnus-with-article-buffer
2262     (if (memq 'smiley gnus-article-wash-types)
2263         (gnus-delete-images 'smiley)
2264       (article-goto-body)
2265       (let ((images (smiley-region (point) (point-max))))
2266         (when images
2267           (gnus-add-wash-type 'smiley)
2268           (dolist (image images)
2269             (gnus-add-image 'smiley image)))))))
2270
2271 (defun gnus-article-remove-images ()
2272   "Remove all images from the article buffer."
2273   (interactive)
2274   (gnus-with-article-buffer
2275     (save-restriction
2276       (widen)
2277       (dolist (elem gnus-article-image-alist)
2278         (gnus-delete-images (car elem))))))
2279
2280 (autoload 'w3m-toggle-inline-images "w3m")
2281
2282 (defun gnus-article-show-images ()
2283   "Show any images that are in the HTML-rendered article buffer.
2284 This only works if the article in question is HTML."
2285   (interactive)
2286   (gnus-with-article-buffer
2287     (save-restriction
2288       (widen)
2289       (if (eq mm-text-html-renderer 'w3m)
2290           (let ((mm-inline-text-html-with-images nil))
2291             (w3m-toggle-inline-images))
2292         (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2293                                                         'image-displayer))
2294           (destructuring-bind (start end function) region
2295             (funcall function (get-text-property start 'image-url)
2296                      start end)))))))
2297
2298 (defun gnus-article-treat-fold-newsgroups ()
2299   "Unfold folded message headers.
2300 Only the headers that fit into the current window width will be
2301 unfolded."
2302   (interactive)
2303   (gnus-with-article-headers
2304     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2305       (save-restriction
2306         (mail-header-narrow-to-field)
2307         (while (re-search-forward ", *" nil t)
2308           (replace-match ", " t t))
2309         (mail-header-fold-field)
2310         (goto-char (point-max))))))
2311
2312 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2313   "Value of `truncate-lines' in Gnus Article buffer.
2314 Valid values are nil, t, `head', `first', `last', an integer or a
2315 predicate.  See Info node `(gnus)Customizing Articles'."
2316   :version "23.1" ;; No Gnus
2317   :group 'gnus-article
2318   ;; :link '(custom-manual "(gnus)Customizing Articles")
2319   :type 'boolean)
2320
2321 (defun gnus-article-toggle-truncate-lines (&optional arg)
2322   "Toggle whether to fold or truncate long lines in article the buffer.
2323 If ARG is non-nil and not a number, toggle
2324 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2325 long lines if and only if arg is positive."
2326   (interactive "P")
2327   (cond
2328    ((and (numberp arg) (> arg 0))
2329     (setq gnus-article-truncate-lines t))
2330    ((numberp arg)
2331     (setq gnus-article-truncate-lines nil))
2332    (arg
2333     (setq gnus-article-truncate-lines
2334           (not gnus-article-truncate-lines))))
2335   (gnus-with-article-buffer
2336     (cond
2337      ((and (numberp arg) (> arg 0))
2338       (setq truncate-lines nil))
2339      ((numberp arg)
2340       (setq truncate-lines t)))
2341     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2342     ;; `toggle-truncate-lines' needs an argument.
2343     (toggle-truncate-lines)))
2344
2345 (defun gnus-article-treat-body-boundary ()
2346   "Place a boundary line at the end of the headers."
2347   (interactive)
2348   (when (and gnus-body-boundary-delimiter
2349              (> (length gnus-body-boundary-delimiter) 0))
2350     (gnus-with-article-headers
2351       (goto-char (point-max))
2352       (let ((start (point)))
2353         (insert "X-Boundary: ")
2354         (gnus-add-text-properties start (point) '(invisible t intangible t))
2355        (insert (let (str (max (window-width)))
2356                  (if (featurep 'xemacs)
2357                      (setq max (1- max)))
2358                  (while (>= max (length str))
2359                     (setq str (concat str gnus-body-boundary-delimiter)))
2360                  (substring str 0 max))
2361                 "\n")
2362         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2363
2364 (defun article-fill-long-lines ()
2365   "Fill lines that are wider than the window width."
2366   (interactive)
2367   (save-excursion
2368     (let ((inhibit-read-only t)
2369           (width (window-width (get-buffer-window (current-buffer)))))
2370       (save-restriction
2371         (article-goto-body)
2372         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2373           (while (not (eobp))
2374             (end-of-line)
2375             (when (>= (current-column) (min fill-column width))
2376               (narrow-to-region (min (1+ (point)) (point-max))
2377                                 (point-at-bol))
2378               (let ((goback (point-marker)))
2379                 (fill-paragraph nil)
2380                 (goto-char (marker-position goback)))
2381               (widen))
2382             (forward-line 1)))))))
2383
2384 (defun article-capitalize-sentences ()
2385   "Capitalize the first word in each sentence."
2386   (interactive)
2387   (save-excursion
2388     (let ((inhibit-read-only t)
2389           (paragraph-start "^[\n\^L]"))
2390       (article-goto-body)
2391       (while (not (eobp))
2392         (capitalize-word 1)
2393         (forward-sentence)))))
2394
2395 (defun article-remove-cr ()
2396   "Remove trailing CRs and then translate remaining CRs into LFs."
2397   (interactive)
2398   (save-excursion
2399     (let ((inhibit-read-only t))
2400       (goto-char (point-min))
2401       (while (re-search-forward "\r+$" nil t)
2402         (replace-match "" t t))
2403       (goto-char (point-min))
2404       (while (search-forward "\r" nil t)
2405         (replace-match "\n" t t)))))
2406
2407 (defun article-remove-trailing-blank-lines ()
2408   "Remove all trailing blank lines from the article."
2409   (interactive)
2410   (save-excursion
2411     (let ((inhibit-read-only t))
2412       (goto-char (point-max))
2413       (delete-region
2414        (point)
2415        (progn
2416          (while (and (not (bobp))
2417                      (looking-at "^[ \t]*$")
2418                      (not (gnus-annotation-in-region-p
2419                            (point) (point-at-eol))))
2420            (forward-line -1))
2421          (forward-line 1)
2422          (point))))))
2423
2424 (defvar gnus-face-properties-alist)
2425
2426 (defun article-display-face (&optional force)
2427   "Display any Face headers in the header."
2428   (interactive (list 'force))
2429   (let ((wash-face-p buffer-read-only))
2430     (gnus-with-article-headers
2431       ;; When displaying parts, this function can be called several times on
2432       ;; the same article, without any intended toggle semantic (as typing `W
2433       ;; D d' would have). So face deletion must occur only when we come from
2434       ;; an interactive command, that is when the *Article* buffer is
2435       ;; read-only.
2436       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2437           (gnus-delete-images 'face)
2438         (let ((from (message-fetch-field "from"))
2439               face faces)
2440           (save-current-buffer
2441             (when (and wash-face-p
2442                        (gnus-buffer-live-p gnus-original-article-buffer)
2443                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2444               (set-buffer gnus-original-article-buffer))
2445             (save-restriction
2446               (mail-narrow-to-head)
2447               (when (or force
2448                         ;; Check whether this face is censored.
2449                         (not (and gnus-article-x-face-too-ugly
2450                                   (or from
2451                                       (setq from (message-fetch-field "from")))
2452                                   (string-match gnus-article-x-face-too-ugly
2453                                                 from))))
2454                 (while (gnus-article-goto-header "Face")
2455                   (push (mail-header-field-value) faces)))))
2456           (when faces
2457             (goto-char (point-min))
2458             (let (png image)
2459               (unless (setq from (gnus-article-goto-header "from"))
2460                 (insert "From:")
2461                 (setq from (point))
2462                 (insert " [no `from' set]\n"))
2463               (while faces
2464                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2465                   (setq image
2466                         (apply 'gnus-create-image png 'png t
2467                                (cdr (assq 'png gnus-face-properties-alist))))
2468                   (goto-char from)
2469                   (gnus-add-wash-type 'face)
2470                   (gnus-add-image 'face image)
2471                   (gnus-put-image image nil 'face))))))))))
2472
2473 (defun article-display-x-face (&optional force)
2474   "Look for an X-Face header and display it if present."
2475   (interactive (list 'force))
2476   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2477     (gnus-with-article-headers
2478       ;; Delete the old process, if any.
2479       (when (process-status "article-x-face")
2480         (delete-process "article-x-face"))
2481       ;; See the comment in `article-display-face'.
2482       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2483           ;; We have already displayed X-Faces, so we remove them
2484           ;; instead.
2485           (gnus-delete-images 'xface)
2486         ;; Display X-Faces.
2487         (let ((from (message-fetch-field "from"))
2488               x-faces face)
2489           (save-current-buffer
2490             (when (and wash-face-p
2491                        (gnus-buffer-live-p gnus-original-article-buffer)
2492                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2493               ;; If type `W f', use gnus-original-article-buffer,
2494               ;; otherwise use the current buffer because displaying
2495               ;; RFC822 parts calls this function too.
2496               (set-buffer gnus-original-article-buffer))
2497             (save-restriction
2498               (mail-narrow-to-head)
2499               (and gnus-article-x-face-command
2500                    (or force
2501                        ;; Check whether this face is censored.
2502                        (not (and gnus-article-x-face-too-ugly
2503                                  (or from
2504                                      (setq from (message-fetch-field "from")))
2505                                  (string-match gnus-article-x-face-too-ugly
2506                                                from))))
2507                    (while (gnus-article-goto-header "X-Face")
2508                      (push (mail-header-field-value) x-faces)))))
2509           (when x-faces
2510             ;; We display the face.
2511             (cond ((functionp gnus-article-x-face-command)
2512                    ;; The command is a lisp function, so we call it.
2513                    (mapc gnus-article-x-face-command x-faces))
2514                   ((stringp gnus-article-x-face-command)
2515                    ;; The command is a string, so we interpret the command
2516                    ;; as a, well, command, and fork it off.
2517                    (let ((process-connection-type nil))
2518                      (gnus-set-process-query-on-exit-flag
2519                       (start-process
2520                        "article-x-face" nil shell-file-name
2521                        shell-command-switch gnus-article-x-face-command)
2522                       nil)
2523                      ;; Sending multiple EOFs to xv doesn't work,
2524                      ;; so we only do a single external face.
2525                      (with-temp-buffer
2526                        (insert (car x-faces))
2527                        (process-send-region "article-x-face"
2528                                             (point-min) (point-max)))
2529                      (process-send-eof "article-x-face")))
2530                   (t
2531                    (error "`%s' set to `%s' is not a function"
2532                           gnus-article-x-face-command
2533                           'gnus-article-x-face-command)))))))))
2534
2535 (defun article-decode-mime-words ()
2536   "Decode all MIME-encoded words in the article."
2537   (interactive)
2538   (gnus-with-article-buffer
2539     (let ((inhibit-point-motion-hooks t)
2540           (mail-parse-charset gnus-newsgroup-charset)
2541           (mail-parse-ignored-charsets
2542            (with-current-buffer gnus-summary-buffer
2543              gnus-newsgroup-ignored-charsets)))
2544       (mail-decode-encoded-word-region (point-min) (point-max)))))
2545
2546 (defun article-decode-charset (&optional prompt)
2547   "Decode charset-encoded text in the article.
2548 If PROMPT (the prefix), prompt for a coding system to use."
2549   (interactive "P")
2550   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2551         (inhibit-read-only t)
2552         (mail-parse-charset gnus-newsgroup-charset)
2553         (mail-parse-ignored-charsets
2554          (save-excursion (condition-case nil
2555                              (set-buffer gnus-summary-buffer)
2556                            (error))
2557                          gnus-newsgroup-ignored-charsets))
2558         ct cte ctl charset format)
2559     (save-excursion
2560       (save-restriction
2561         (article-narrow-to-head)
2562         (setq ct (message-fetch-field "Content-Type" t)
2563               cte (message-fetch-field "Content-Transfer-Encoding" t)
2564               ctl (and ct (mail-header-parse-content-type ct))
2565               charset (cond
2566                        (prompt
2567                         (mm-read-coding-system "Charset to decode: "))
2568                        (ctl
2569                         (mail-content-type-get ctl 'charset)))
2570               format (and ctl (mail-content-type-get ctl 'format)))
2571         (when cte
2572           (setq cte (mail-header-strip cte)))
2573         (if (and ctl (not (string-match "/" (car ctl))))
2574             (setq ctl nil))
2575         (goto-char (point-max)))
2576       (forward-line 1)
2577       (save-restriction
2578         (narrow-to-region (point) (point-max))
2579         (when (and (eq mail-parse-charset 'gnus-decoded)
2580                    (eq (mm-body-7-or-8) '8bit))
2581           ;; The text code could have been decoded.
2582           (setq charset mail-parse-charset))
2583         (when (and (or (not ctl)
2584                        (equal (car ctl) "text/plain"))
2585                    (not format)) ;; article with format will decode later.
2586           (mm-decode-body
2587            charset (and cte (intern (downcase
2588                                      (gnus-strip-whitespace cte))))
2589            (car ctl)))))))
2590
2591 (defun article-decode-encoded-words ()
2592   "Remove encoded-word encoding from headers."
2593   (let ((inhibit-point-motion-hooks t)
2594         (mail-parse-charset gnus-newsgroup-charset)
2595         (mail-parse-ignored-charsets
2596          (save-excursion (condition-case nil
2597                              (set-buffer gnus-summary-buffer)
2598                            (error))
2599                          gnus-newsgroup-ignored-charsets))
2600         (inhibit-read-only t)
2601         end start)
2602     (goto-char (point-min))
2603     (when (search-forward "\n\n" nil 'move)
2604       (forward-line -1))
2605     (setq end (point))
2606     (while (not (bobp))
2607       (while (progn
2608                (forward-line -1)
2609                (and (not (bobp))
2610                     (memq (char-after) '(?\t ? )))))
2611       (setq start (point))
2612       (if (looking-at "\
2613 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2614 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2615           (funcall gnus-decode-address-function start end)
2616         (funcall gnus-decode-header-function start end))
2617       (goto-char (setq end start)))))
2618
2619 (defun article-decode-group-name ()
2620   "Decode group names in Newsgroups, Followup-To and Xref headers."
2621   (let ((inhibit-point-motion-hooks t)
2622         (inhibit-read-only t)
2623         (method (gnus-find-method-for-group gnus-newsgroup-name))
2624         regexp)
2625     (when (and (or gnus-group-name-charset-method-alist
2626                    gnus-group-name-charset-group-alist)
2627                (gnus-buffer-live-p gnus-original-article-buffer))
2628       (save-restriction
2629         (article-narrow-to-head)
2630         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2631           (with-current-buffer gnus-original-article-buffer
2632             (goto-char (point-min)))
2633           (setq regexp (concat "^" header
2634                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2635           (while (re-search-forward regexp nil t)
2636             (replace-match (save-match-data
2637                              (gnus-decode-newsgroups
2638                               ;; XXX how to use data in article buffer?
2639                               (with-current-buffer gnus-original-article-buffer
2640                                 (re-search-forward regexp nil t)
2641                                 (match-string 1))
2642                               gnus-newsgroup-name method))
2643                            t t nil 1))
2644           (goto-char (point-min)))))))
2645
2646 (autoload 'idna-to-unicode "idna")
2647
2648 (defun article-decode-idna-rhs ()
2649   "Decode IDNA strings in RHS in various headers in current buffer.
2650 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2651 Mail-Reply-To: and Mail-Followup-To:."
2652   (when gnus-use-idna
2653     (save-restriction
2654       (let ((inhibit-point-motion-hooks t)
2655             (inhibit-read-only t))
2656         (article-narrow-to-head)
2657         (goto-char (point-min))
2658         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2659           (let (ace unicode)
2660             (when (save-match-data
2661                     (and (setq ace (match-string 1))
2662                          (save-excursion
2663                            (and (re-search-backward "^[^ \t]" nil t)
2664                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2665                          (setq unicode (idna-to-unicode ace))))
2666               (unless (string= ace unicode)
2667                 (replace-match unicode nil nil nil 1)))))))))
2668
2669 (defun article-de-quoted-unreadable (&optional force read-charset)
2670   "Translate a quoted-printable-encoded article.
2671 If FORCE, decode the article whether it is marked as quoted-printable
2672 or not.
2673 If READ-CHARSET, ask for a coding system."
2674   (interactive (list 'force current-prefix-arg))
2675   (save-excursion
2676     (let ((inhibit-read-only t) type charset)
2677       (if (gnus-buffer-live-p gnus-original-article-buffer)
2678           (with-current-buffer gnus-original-article-buffer
2679             (setq type
2680                   (gnus-fetch-field "content-transfer-encoding"))
2681             (let* ((ct (gnus-fetch-field "content-type"))
2682                    (ctl (and ct (mail-header-parse-content-type ct))))
2683               (setq charset (and ctl
2684                                  (mail-content-type-get ctl 'charset)))
2685               (if (stringp charset)
2686                   (setq charset (intern (downcase charset)))))))
2687       (if read-charset
2688           (setq charset (mm-read-coding-system "Charset: " charset)))
2689       (unless charset
2690         (setq charset gnus-newsgroup-charset))
2691       (when (or force
2692                 (and type (let ((case-fold-search t))
2693                             (string-match "quoted-printable" type))))
2694         (article-goto-body)
2695         (quoted-printable-decode-region
2696          (point) (point-max) (mm-charset-to-coding-system charset))))))
2697
2698 (defun article-de-base64-unreadable (&optional force read-charset)
2699   "Translate a base64 article.
2700 If FORCE, decode the article whether it is marked as base64 not.
2701 If READ-CHARSET, ask for a coding system."
2702   (interactive (list 'force current-prefix-arg))
2703   (save-excursion
2704     (let ((inhibit-read-only t) type charset)
2705       (if (gnus-buffer-live-p gnus-original-article-buffer)
2706           (with-current-buffer gnus-original-article-buffer
2707             (setq type
2708                   (gnus-fetch-field "content-transfer-encoding"))
2709             (let* ((ct (gnus-fetch-field "content-type"))
2710                    (ctl (and ct (mail-header-parse-content-type ct))))
2711               (setq charset (and ctl
2712                                  (mail-content-type-get ctl 'charset)))
2713               (if (stringp charset)
2714                   (setq charset (intern (downcase charset)))))))
2715       (if read-charset
2716           (setq charset (mm-read-coding-system "Charset: " charset)))
2717       (unless charset
2718         (setq charset gnus-newsgroup-charset))
2719       (when (or force
2720                 (and type (let ((case-fold-search t))
2721                             (string-match "base64" type))))
2722         (article-goto-body)
2723         (save-restriction
2724           (narrow-to-region (point) (point-max))
2725           (base64-decode-region (point-min) (point-max))
2726           (mm-decode-coding-region
2727            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2728
2729 (eval-when-compile
2730   (require 'rfc1843))
2731
2732 (defun article-decode-HZ ()
2733   "Translate a HZ-encoded article."
2734   (interactive)
2735   (require 'rfc1843)
2736   (save-excursion
2737     (let ((inhibit-read-only t))
2738       (rfc1843-decode-region (point-min) (point-max)))))
2739
2740 (defun article-unsplit-urls ()
2741   "Remove the newlines that some other mailers insert into URLs."
2742   (interactive)
2743   (save-excursion
2744     (let ((inhibit-read-only t))
2745       (goto-char (point-min))
2746       (while (re-search-forward
2747               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2748         (replace-match "\\1\\3" t)))
2749     (when (interactive-p)
2750       (gnus-treat-article nil))))
2751
2752 (defun article-wash-html ()
2753   "Format an HTML article."
2754   (interactive)
2755   (let ((handles nil)
2756         (buffer-read-only nil))
2757     (when (gnus-buffer-live-p gnus-original-article-buffer)
2758       (setq handles (mm-dissect-buffer t t)))
2759     (article-goto-body)
2760     (delete-region (point) (point-max))
2761     (mm-inline-text-html handles)))
2762
2763 (defvar gnus-article-browse-html-temp-list nil
2764   "List of temporary files created by `gnus-article-browse-html-parts'.
2765 Internal variable.")
2766
2767 (defcustom gnus-article-browse-delete-temp 'ask
2768   "What to do with temporary files from `gnus-article-browse-html-parts'.
2769 If nil, don't delete temporary files.  If it is t, delete them on
2770 exit from the summary buffer.  If it is the symbol `file', query
2771 on each file, if it is `ask' ask once when exiting from the
2772 summary buffer."
2773   :group 'gnus-article
2774   :version "23.1" ;; No Gnus
2775   :type '(choice (const :tag "Don't delete" nil)
2776                  (const :tag "Don't ask" t)
2777                  (const :tag "Ask" ask)
2778                  (const :tag "Ask for each file" file)))
2779
2780 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2781
2782 (defun gnus-article-browse-delete-temp-files (&optional how)
2783   "Delete temp-files created by `gnus-article-browse-html-parts'."
2784   (when (and gnus-article-browse-html-temp-list
2785              (progn
2786                (or how (setq how gnus-article-browse-delete-temp))
2787                (if (eq how 'ask)
2788                    (let ((files (length gnus-article-browse-html-temp-list)))
2789                      (gnus-y-or-n-p
2790                       (if (= files 1)
2791                           "Delete the temporary HTML file? "
2792                         (format "Delete all %s temporary HTML files? "
2793                                 files))))
2794                  how)))
2795     (dolist (file gnus-article-browse-html-temp-list)
2796       (cond ((file-directory-p file)
2797              (when (or (not (eq how 'file))
2798                        (gnus-y-or-n-p
2799                         (format
2800                          "Delete temporary HTML file(s) in directory `%s'? "
2801                          (file-name-as-directory file))))
2802                (gnus-delete-directory file)))
2803             ((file-exists-p file)
2804              (when (or (not (eq how 'file))
2805                        (gnus-y-or-n-p
2806                         (format "Delete temporary HTML file `%s'? " file)))
2807                (delete-file file)))))
2808     ;; Also remove file from the list when not deleted or if file doesn't
2809     ;; exist anymore.
2810     (setq gnus-article-browse-html-temp-list nil))
2811   gnus-article-browse-html-temp-list)
2812
2813 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2814   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2815 Return file name."
2816   (save-match-data
2817     (let (file type)
2818       (catch 'found
2819         (dolist (handle handles)
2820           (cond
2821            ((not (listp handle)))
2822            ((equal (mm-handle-media-supertype handle) "multipart")
2823             (when (setq file (gnus-article-browse-html-save-cid-content
2824                               cid handle directory))
2825               (throw 'found file)))
2826            ((equal (concat "<" cid ">") (mm-handle-id handle))
2827             (setq file
2828                   (expand-file-name
2829                    (or (mm-handle-filename handle)
2830                        (concat
2831                         (make-temp-name "cid")
2832                         (car (rassoc (car (mm-handle-type handle)) mailcap-mime-extensions))))
2833                    directory))
2834             (mm-save-part-to-file handle file)
2835             (throw 'found file))))))))
2836
2837 (defun gnus-article-browse-html-parts (list &optional header)
2838   "View all \"text/html\" parts from LIST.
2839 Recurse into multiparts.  The optional HEADER that should be a decoded
2840 message header will be added to the bodies of the \"text/html\" parts."
2841   ;; Internal function used by `gnus-article-browse-html-article'.
2842   (let (type file charset content cid-dir tmp-file showed)
2843     ;; Find and show the html-parts.
2844     (dolist (handle list)
2845       ;; If HTML, show it:
2846       (cond ((not (listp handle)))
2847             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2848                  (and (equal (car type) "message/external-body")
2849                       (or header
2850                           (setq file (mm-handle-filename handle)))
2851                       (or (mm-handle-cache handle)
2852                           (condition-case code
2853                               (progn (mm-extern-cache-contents handle) t)
2854                             (error
2855                              (gnus-message 3 "%s" (error-message-string code))
2856                              (when (>= gnus-verbose 3) (sit-for 2))
2857                              nil)))
2858                       (progn
2859                         (setq handle (mm-handle-cache handle)
2860                               type (mm-handle-type handle))
2861                         (equal (car type) "text/html"))))
2862              (setq charset (mail-content-type-get type 'charset)
2863                    content (mm-get-part handle))
2864              (with-temp-buffer
2865                (if (eq charset 'gnus-decoded)
2866                    (mm-enable-multibyte)
2867                  (mm-disable-multibyte))
2868                (insert content)
2869                ;; resolve cid contents
2870                (let ((case-fold-search t)
2871                      cid-file)
2872                  (goto-char (point-min))
2873                  (while (re-search-forward "\
2874 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2875                                            nil t)
2876                    (unless cid-dir
2877                      (setq cid-dir (mm-make-temp-file "cid" t))
2878                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2879                    (setq file nil
2880                          content nil)
2881                    (when (setq cid-file
2882                                (gnus-article-browse-html-save-cid-content
2883                                 (match-string 2)
2884                                 (with-current-buffer gnus-article-buffer
2885                                   gnus-article-mime-handles)
2886                                 cid-dir))
2887                      (when (eq system-type 'cygwin)
2888                        (setq cid-file
2889                              (concat "/" (substring
2890                                           (with-output-to-string
2891                                             (call-process "cygpath" nil
2892                                                           standard-output
2893                                                           nil "-m" cid-file))
2894                                           0 -1))))
2895                      (replace-match (concat "file://" cid-file)
2896                                     nil nil nil 1))))
2897                (unless content (setq content (buffer-string))))
2898              (when (or charset header (not file))
2899                (setq tmp-file (mm-make-temp-file
2900                                ;; Do we need to care for 8.3 filenames?
2901                                "mm-" nil ".html")))
2902              ;; Add a meta html tag to specify charset and a header.
2903              (cond
2904               (header
2905                (let (title eheader body hcharset coding force-charset)
2906                  (with-temp-buffer
2907                    (mm-enable-multibyte)
2908                    (setq case-fold-search t)
2909                    (insert header "\n")
2910                    (setq title (message-fetch-field "subject"))
2911                    (goto-char (point-min))
2912                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2913                      (replace-match (cond ((match-beginning 1) "&lt;")
2914                                           ((match-beginning 2) "&gt;")
2915                                           (t "&amp;"))))
2916                    (goto-char (point-min))
2917                    (insert "<pre>\n")
2918                    (goto-char (point-max))
2919                    (insert "</pre>\n<hr>\n")
2920                    ;; We have to examine charset one by one since
2921                    ;; charset specified in parts might be different.
2922                    (if (eq charset 'gnus-decoded)
2923                        (setq charset 'utf-8
2924                              eheader (mm-encode-coding-string (buffer-string)
2925                                                               charset)
2926                              title (when title
2927                                      (mm-encode-coding-string title charset))
2928                              body (mm-encode-coding-string content charset)
2929                              force-charset t)
2930                      (setq hcharset (mm-find-mime-charset-region (point-min)
2931                                                                  (point-max)))
2932                      (cond ((= (length hcharset) 1)
2933                             (setq hcharset (car hcharset)
2934                                   coding (mm-charset-to-coding-system
2935                                           hcharset)))
2936                            ((> (length hcharset) 1)
2937                             (setq hcharset 'utf-8
2938                                   coding hcharset)))
2939                      (if coding
2940                          (if charset
2941                              (progn
2942                                (setq body
2943                                      (mm-charset-to-coding-system charset))
2944                                (if (eq coding body)
2945                                    (setq eheader (mm-encode-coding-string
2946                                                   (buffer-string) coding)
2947                                          title (when title
2948                                                  (mm-encode-coding-string
2949                                                   title coding))
2950                                          body content)
2951                                  (setq charset 'utf-8
2952                                        eheader (mm-encode-coding-string
2953                                                 (buffer-string) charset)
2954                                        title (when title
2955                                                (mm-encode-coding-string
2956                                                 title charset))
2957                                        body (mm-encode-coding-string
2958                                              (mm-decode-coding-string
2959                                               content body)
2960                                              charset)
2961                                        force-charset t)))
2962                            (setq charset hcharset
2963                                  eheader (mm-encode-coding-string
2964                                           (buffer-string) coding)
2965                                  title (when title
2966                                          (mm-encode-coding-string
2967                                           title coding))
2968                                  body content))
2969                        (setq eheader (mm-string-as-unibyte (buffer-string))
2970                              body content)))
2971                    (erase-buffer)
2972                    (mm-disable-multibyte)
2973                    (insert body)
2974                    (when charset
2975                      (mm-add-meta-html-tag handle charset force-charset))
2976                    (when title
2977                      (goto-char (point-min))
2978                      (unless (search-forward "<title>" nil t)
2979                        (re-search-forward "<head>\\s-*" nil t)
2980                        (insert "<title>" title "</title>\n")))
2981                    (goto-char (point-min))
2982                    (or (re-search-forward
2983                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2984                        (re-search-forward
2985                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2986                    (insert eheader)
2987                    (mm-write-region (point-min) (point-max)
2988                                     tmp-file nil nil nil 'binary t))))
2989               (charset
2990                (mm-with-unibyte-buffer
2991                  (insert (if (eq charset 'gnus-decoded)
2992                              (mm-encode-coding-string content
2993                                                       (setq charset 'utf-8))
2994                            content))
2995                  (if (or (mm-add-meta-html-tag handle charset)
2996                          (not file))
2997                      (mm-write-region (point-min) (point-max)
2998                                       tmp-file nil nil nil 'binary t)
2999                    (setq tmp-file nil))))
3000               (tmp-file
3001                (mm-save-part-to-file handle tmp-file)))
3002              (when tmp-file
3003                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3004              (add-hook 'gnus-summary-prepare-exit-hook
3005                        'gnus-article-browse-delete-temp-files)
3006              (add-hook 'gnus-exit-gnus-hook
3007                        (lambda  ()
3008                          (gnus-article-browse-delete-temp-files t)))
3009              ;; FIXME: Warn if there's an <img> tag?
3010              (browse-url-of-file (or tmp-file (expand-file-name file)))
3011              (setq showed t))
3012             ;; If multipart, recurse
3013             ((equal (mm-handle-media-supertype handle) "multipart")
3014              (when (gnus-article-browse-html-parts handle header)
3015                (setq showed t)))
3016             ((equal (mm-handle-media-type handle) "message/rfc822")
3017              (mm-with-multibyte-buffer
3018                (mm-insert-part handle)
3019                (setq handle (mm-dissect-buffer t t))
3020                (when (and (bufferp (car handle))
3021                           (stringp (car (mm-handle-type handle))))
3022                  (setq handle (list handle)))
3023                (when header
3024                  (article-decode-encoded-words)
3025                  (let ((gnus-visible-headers
3026                         (or (get 'gnus-visible-headers 'standard-value)
3027                             gnus-visible-headers)))
3028                    (article-hide-headers))
3029                  (goto-char (point-min))
3030                  (search-forward "\n\n" nil 'move)
3031                  (skip-chars-backward "\t\n ")
3032                  (setq header (buffer-substring (point-min) (point)))))
3033              (when (prog1
3034                        (gnus-article-browse-html-parts handle header)
3035                      (mm-destroy-parts handle))
3036                (setq showed t)))))
3037     showed))
3038
3039 (defun gnus-article-browse-html-article (&optional arg)
3040   "View \"text/html\" parts of the current article with a WWW browser.
3041 Inline images embedded in a message using the cid scheme, as they are
3042 generally considered to be safe, will be processed properly.
3043 The message header is added to the beginning of every html part unless
3044 the prefix argument ARG is given.
3045
3046 Warning: Spammers use links to images (using the http scheme) in HTML
3047 articles to verify whether you have read the message.  As
3048 `gnus-article-browse-html-article' passes the HTML content to the
3049 browser without eliminating these \"web bugs\" you should only
3050 use it for mails from trusted senders.
3051
3052 If you always want to display HTML parts in the browser, set
3053 `mm-text-html-renderer' to nil.
3054
3055 This command creates temporary files to pass HTML contents including
3056 images if any to the browser, and deletes them when exiting the group
3057 \(if you want)."
3058   ;; Cf. `mm-w3m-safe-url-regexp'
3059   (interactive "P")
3060   (if arg
3061       (gnus-summary-show-article)
3062     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3063                                     gnus-visible-headers))
3064           ;; As we insert a <hr>, there's no need for the body boundary.
3065           (gnus-treat-body-boundary nil))
3066       (gnus-summary-show-article)))
3067   (with-current-buffer gnus-article-buffer
3068     (let ((header (unless arg
3069                     (save-restriction
3070                       (widen)
3071                       (buffer-substring-no-properties
3072                        (goto-char (point-min))
3073                        (if (search-forward "\n\n" nil t)
3074                            (match-beginning 0)
3075                          (goto-char (point-max))
3076                          (skip-chars-backward "\t\n ")
3077                          (point))))))
3078           parts)
3079       (set-buffer gnus-original-article-buffer)
3080       (setq parts (mm-dissect-buffer t t))
3081       ;; If singlepart, enforce a list.
3082       (when (and (bufferp (car parts))
3083                  (stringp (car (mm-handle-type parts))))
3084         (setq parts (list parts)))
3085       ;; Process the list
3086       (unless (gnus-article-browse-html-parts parts header)
3087         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3088       (mm-destroy-parts parts)
3089       (unless arg
3090         (gnus-summary-show-article)))))
3091
3092 (defun article-hide-list-identifiers ()
3093   "Remove list identifies from the Subject header.
3094 The `gnus-list-identifiers' variable specifies what to do."
3095   (interactive)
3096   (let ((inhibit-point-motion-hooks t)
3097         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3098         (inhibit-read-only t))
3099     (when regexp
3100       (save-excursion
3101         (save-restriction
3102           (article-narrow-to-head)
3103           (goto-char (point-min))
3104           (while (re-search-forward
3105                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3106                   nil t)
3107             (delete-region (match-beginning 2) (match-end 0))
3108             (beginning-of-line))
3109           (when (re-search-forward
3110                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3111             (delete-region (match-beginning 1) (match-end 1))))))))
3112
3113 (defun article-hide-pem (&optional arg)
3114   "Toggle hiding of any PEM headers and signatures in the current article.
3115 If given a negative prefix, always show; if given a positive prefix,
3116 always hide."
3117   (interactive (gnus-article-hidden-arg))
3118   (unless (gnus-article-check-hidden-text 'pem arg)
3119     (save-excursion
3120       (let ((inhibit-read-only t) end)
3121         (goto-char (point-min))
3122         ;; Hide the horrendously ugly "header".
3123         (when (and (search-forward
3124                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3125                     nil t)
3126                    (setq end (1+ (match-beginning 0))))
3127           (gnus-add-wash-type 'pem)
3128           (gnus-article-hide-text-type
3129            end
3130            (if (search-forward "\n\n" nil t)
3131                (match-end 0)
3132              (point-max))
3133            'pem)
3134           ;; Hide the trailer as well
3135           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3136                                 nil t)
3137             (gnus-article-hide-text-type
3138              (match-beginning 0) (match-end 0) 'pem)))))))
3139
3140 (defun article-strip-banner ()
3141   "Strip the banners specified by the `banner' group parameter and by
3142 `gnus-article-address-banner-alist'."
3143   (interactive)
3144   (save-excursion
3145     (save-restriction
3146       (let ((inhibit-point-motion-hooks t))
3147         (when (gnus-parameter-banner gnus-newsgroup-name)
3148           (article-really-strip-banner
3149            (gnus-parameter-banner gnus-newsgroup-name)))
3150         (when gnus-article-address-banner-alist
3151           ;; Note that the From header is decoded here, so it is
3152           ;; required that the *-extract-address-components function
3153           ;; supports non-ASCII text.
3154           (let ((from (save-restriction
3155                         (widen)
3156                         (article-narrow-to-head)
3157                         (mail-fetch-field "from"))))
3158             (when (and from
3159                        (setq from
3160                              (cadr (funcall gnus-extract-address-components
3161                                             from))))
3162               (catch 'found
3163                 (dolist (pair gnus-article-address-banner-alist)
3164                   (when (string-match (car pair) from)
3165                     (throw 'found
3166                            (article-really-strip-banner (cdr pair)))))))))))))
3167
3168 (defun article-really-strip-banner (banner)
3169   "Strip the banner specified by the argument."
3170   (save-excursion
3171     (save-restriction
3172       (let ((inhibit-point-motion-hooks t)
3173             (gnus-signature-limit nil)
3174             (inhibit-read-only t))
3175         (article-goto-body)
3176         (cond
3177          ((eq banner 'signature)
3178           (when (gnus-article-narrow-to-signature)
3179             (widen)
3180             (forward-line -1)
3181             (delete-region (point) (point-max))))
3182          ((symbolp banner)
3183           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3184               (while (re-search-forward banner nil t)
3185                 (delete-region (match-beginning 0) (match-end 0)))))
3186          ((stringp banner)
3187           (while (re-search-forward banner nil t)
3188             (delete-region (match-beginning 0) (match-end 0)))))))))
3189
3190 (defun article-babel ()
3191   "Translate article using an online translation service."
3192   (interactive)
3193   (require 'babel)
3194   (gnus-with-article-buffer
3195     (when (article-goto-body)
3196       (let* ((start (point))
3197              (end (point-max))
3198              (orig (buffer-substring start end))
3199              (trans (babel-as-string orig)))
3200         (save-restriction
3201           (narrow-to-region start end)
3202           (delete-region start end)
3203           (insert trans))))))
3204
3205 (defun article-hide-signature (&optional arg)
3206   "Hide the signature in the current article.
3207 If given a negative prefix, always show; if given a positive prefix,
3208 always hide."
3209   (interactive (gnus-article-hidden-arg))
3210   (unless (gnus-article-check-hidden-text 'signature arg)
3211     (save-excursion
3212       (save-restriction
3213         (let ((inhibit-read-only t))
3214           (when (gnus-article-narrow-to-signature)
3215             (gnus-article-hide-text-type
3216              (point-min) (point-max) 'signature))))))
3217   (gnus-set-mode-line 'article))
3218
3219 (defun article-strip-headers-in-body ()
3220   "Strip offensive headers from bodies."
3221   (interactive)
3222   (save-excursion
3223     (article-goto-body)
3224     (let ((case-fold-search t))
3225       (when (looking-at "x-no-archive:")
3226         (gnus-delete-line)))))
3227
3228 (defun article-strip-leading-blank-lines ()
3229   "Remove all blank lines from the beginning of the article."
3230   (interactive)
3231   (save-excursion
3232     (let ((inhibit-point-motion-hooks t)
3233           (inhibit-read-only t))
3234       (when (article-goto-body)
3235         (while (and (not (eobp))
3236                     (looking-at "[ \t]*$"))
3237           (gnus-delete-line))))))
3238
3239 (defun article-narrow-to-head ()
3240   "Narrow the buffer to the head of the message.
3241 Point is left at the beginning of the narrowed-to region."
3242   (narrow-to-region
3243    (goto-char (point-min))
3244    (cond
3245     ;; Absolutely no headers displayed.
3246     ((looking-at "\n")
3247      (point))
3248     ;; Normal headers.
3249     ((search-forward "\n\n" nil 1)
3250      (1- (point)))
3251     ;; Nothing but headers.
3252     (t
3253      (point-max))))
3254   (goto-char (point-min)))
3255
3256 (defun article-goto-body ()
3257   "Place point at the start of the body."
3258   (goto-char (point-min))
3259   (cond
3260    ;; This variable is only bound when dealing with separate
3261    ;; MIME body parts.
3262    (article-goto-body-goes-to-point-min-p
3263     t)
3264    ((search-forward "\n\n" nil t)
3265     t)
3266    (t
3267     (goto-char (point-max))
3268     nil)))
3269
3270 (defun article-strip-multiple-blank-lines ()
3271   "Replace consecutive blank lines with one empty line."
3272   (interactive)
3273   (save-excursion
3274     (let ((inhibit-point-motion-hooks t)
3275           (inhibit-read-only t))
3276       ;; First make all blank lines empty.
3277       (article-goto-body)
3278       (while (re-search-forward "^[ \t]+$" nil t)
3279         (unless (gnus-annotation-in-region-p
3280                  (match-beginning 0) (match-end 0))
3281           (replace-match "" nil t)))
3282       ;; Then replace multiple empty lines with a single empty line.
3283       (article-goto-body)
3284       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3285         (unless (gnus-annotation-in-region-p
3286                  (match-beginning 0) (match-end 0))
3287           (delete-region (match-beginning 1) (match-end 1)))))))
3288
3289 (defun article-strip-leading-space ()
3290   "Remove all white space from the beginning of the lines in the article."
3291   (interactive)
3292   (save-excursion
3293     (let ((inhibit-point-motion-hooks t)
3294           (inhibit-read-only t))
3295       (article-goto-body)
3296       (while (re-search-forward "^[ \t]+" nil t)
3297         (replace-match "" t t)))))
3298
3299 (defun article-strip-trailing-space ()
3300   "Remove all white space from the end of the lines in the article."
3301   (interactive)
3302   (save-excursion
3303     (let ((inhibit-point-motion-hooks t)
3304           (inhibit-read-only t))
3305       (article-goto-body)
3306       (while (re-search-forward "[ \t]+$" nil t)
3307         (replace-match "" t t)))))
3308
3309 (defun article-strip-blank-lines ()
3310   "Strip leading, trailing and multiple blank lines."
3311   (interactive)
3312   (article-strip-leading-blank-lines)
3313   (article-remove-trailing-blank-lines)
3314   (article-strip-multiple-blank-lines))
3315
3316 (defun article-strip-all-blank-lines ()
3317   "Strip all blank lines."
3318   (interactive)
3319   (save-excursion
3320     (let ((inhibit-point-motion-hooks t)
3321           (inhibit-read-only t))
3322       (article-goto-body)
3323       (while (re-search-forward "^[ \t]*\n" nil t)
3324         (replace-match "" t t)))))
3325
3326 (defun gnus-article-narrow-to-signature ()
3327   "Narrow to the signature; return t if a signature is found, else nil."
3328   (let ((inhibit-point-motion-hooks t))
3329     (when (gnus-article-search-signature)
3330       (forward-line 1)
3331       ;; Check whether we have some limits to what we consider
3332       ;; to be a signature.
3333       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3334                       (list gnus-signature-limit)))
3335             limit limited)
3336         (while (setq limit (pop limits))
3337           (if (or (and (integerp limit)
3338                        (< (- (point-max) (point)) limit))
3339                   (and (floatp limit)
3340                        (< (count-lines (point) (point-max)) limit))
3341                   (and (functionp limit)
3342                        (funcall limit))
3343                   (and (stringp limit)
3344                        (not (re-search-forward limit nil t))))
3345               ()                        ; This limit did not succeed.
3346             (setq limited t
3347                   limits nil)))
3348         (unless limited
3349           (narrow-to-region (point) (point-max))
3350           t)))))
3351
3352 (defun gnus-article-search-signature ()
3353   "Search the current buffer for the signature separator.
3354 Put point at the beginning of the signature separator."
3355   (let ((cur (point)))
3356     (goto-char (point-max))
3357     (if (if (stringp gnus-signature-separator)
3358             (re-search-backward gnus-signature-separator nil t)
3359           (let ((seps gnus-signature-separator))
3360             (while (and seps
3361                         (not (re-search-backward (car seps) nil t)))
3362               (pop seps))
3363             seps))
3364         t
3365       (goto-char cur)
3366       nil)))
3367
3368 (defun gnus-article-hidden-arg ()
3369   "Return the current prefix arg as a number, or 0 if no prefix."
3370   (list (if current-prefix-arg
3371             (prefix-numeric-value current-prefix-arg)
3372           0)))
3373
3374 (defun gnus-article-check-hidden-text (type arg)
3375   "Return nil if hiding is necessary.
3376 Arg can be nil or a number.  nil and positive means hide, negative
3377 means show, 0 means toggle."
3378   (save-excursion
3379     (save-restriction
3380       (let ((hide (gnus-article-hidden-text-p type)))
3381         (cond
3382          ((or (null arg)
3383               (> arg 0))
3384           nil)
3385          ((< arg 0)
3386           (gnus-article-show-hidden-text type)
3387           t)
3388          (t
3389           (if (eq hide 'hidden)
3390               (progn
3391                 (gnus-article-show-hidden-text type)
3392                 t)
3393             nil)))))))
3394
3395 (defun gnus-article-hidden-text-p (type)
3396   "Say whether the current buffer contains hidden text of type TYPE."
3397   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3398     (while (and pos
3399                 (not (get-text-property pos 'invisible))
3400                 (not (get-text-property pos 'dummy-invisible)))
3401       (setq pos
3402             (text-property-any (1+ pos) (point-max) 'article-type type)))
3403     (if pos
3404         'hidden
3405       nil)))
3406
3407 (defun gnus-article-show-hidden-text (type &optional dummy)
3408   "Show all hidden text of type TYPE.
3409 Originally it is hide instead of DUMMY."
3410   (let ((inhibit-read-only t)
3411         (inhibit-point-motion-hooks t))
3412     (gnus-remove-text-properties-when
3413      'article-type type
3414      (point-min) (point-max)
3415      (cons 'article-type (cons type
3416                                gnus-hidden-properties)))
3417     (gnus-delete-wash-type type)))
3418
3419 (defconst article-time-units
3420   `((year . ,(* 365.25 24 60 60))
3421     (week . ,(* 7 24 60 60))
3422     (day . ,(* 24 60 60))
3423     (hour . ,(* 60 60))
3424     (minute . 60)
3425     (second . 1))
3426   "Mapping from time units to seconds.")
3427
3428 (defun gnus-article-forward-header ()
3429   "Move point to the start of the next header.
3430 If the current header is a continuation header, this can be several
3431 lines forward."
3432   (let ((ended nil))
3433     (while (not ended)
3434       (forward-line 1)
3435       (if (looking-at "[ \t]+[^ \t]")
3436           (forward-line 1)
3437         (setq ended t)))))
3438
3439 (defun article-treat-date ()
3440   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3441                        (with-current-buffer gnus-summary-buffer
3442                          gnus-article-date-headers)
3443                      gnus-article-date-headers)
3444                    t))
3445
3446 (defun article-date-ut (&optional type highlight date-position)
3447   "Convert DATE date to TYPE in the current article.
3448 The default type is `ut'.  See `gnus-article-date-headers' for
3449 possible values."
3450   (interactive (list 'ut t))
3451   (let* ((case-fold-search t)
3452          (inhibit-read-only t)
3453          (inhibit-point-motion-hooks t)
3454          (first t)
3455          (visible-date (mail-fetch-field "Date"))
3456          pos date bface eface)
3457     (save-excursion
3458       (goto-char (point-min))
3459       (when (re-search-forward "^Date:" nil t)
3460         (setq bface (get-text-property (point-at-bol) 'face)
3461               eface (get-text-property (1- (point-at-eol)) 'face)))
3462       ;; Delete any old Date headers.
3463       (if date-position
3464           (progn
3465             (goto-char date-position)
3466             (setq date (get-text-property (point) 'original-date))
3467             (delete-region (point)
3468                            (progn
3469                              (gnus-article-forward-header)
3470                              (point)))
3471             (article-transform-date date type bface eface))
3472         (save-restriction
3473           (widen)
3474           (goto-char (point-min))
3475           (while (or (get-text-property (setq pos (point)) 'original-date)
3476                      (and (setq pos (next-single-property-change
3477                                      (point) 'original-date))
3478                           (goto-char pos)))
3479             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3480                                       (1+ (match-beginning 0))
3481                                     (point-max)))
3482             (goto-char (point-min))
3483             (while (re-search-forward "^Date:" nil t)
3484               (setq date (get-text-property (match-beginning 0) 'original-date))
3485               (delete-region (point-at-bol) (progn
3486                                               (gnus-article-forward-header)
3487                                               (point))))
3488             (when (and (not date)
3489                        visible-date)
3490               (setq date visible-date))
3491             (when date
3492               (article-transform-date date type bface eface))
3493             (goto-char (point-max))
3494             (widen)))))))
3495
3496 (defun article-transform-date (date type bface eface)
3497   (dolist (this-type (cond
3498                       ((null type)
3499                        (list 'ut))
3500                       ((atom type)
3501                        (list type))
3502                       (t
3503                        type)))
3504     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3505     (forward-line -1)
3506     (beginning-of-line)
3507     (put-text-property (point) (1+ (point))
3508                        'original-date date)
3509     (put-text-property (point) (1+ (point))
3510                        'gnus-date-type this-type)
3511     ;; Do highlighting.
3512     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3513       (put-text-property (match-beginning 1) (1+ (match-end 1))
3514                          'face bface)
3515       (put-text-property (match-beginning 2) (match-end 2)
3516                          'face eface))
3517     (forward-line 1)))
3518
3519 (defun article-make-date-line (date type)
3520   "Return a DATE line of TYPE."
3521   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3522                              combined-lapsed))
3523     (error "Unknown conversion type: %s" type))
3524   (condition-case ()
3525       (let ((time (ignore-errors (date-to-time date))))
3526         (cond
3527          ;; Convert to the local timezone.
3528          ((eq type 'local)
3529           (concat "Date: " (message-make-date time)))
3530          ;; Convert to Universal Time.
3531          ((eq type 'ut)
3532           (concat "Date: "
3533                   (substring
3534                    (message-make-date
3535                     (let* ((e (parse-time-string date))
3536                            (tm (apply 'encode-time e))
3537                            (ms (car tm))
3538                            (ls (- (cadr tm) (car (current-time-zone time)))))
3539                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3540                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3541                             (t (list ms ls)))))
3542                    0 -5)
3543                   "UT"))
3544          ;; Get the original date from the article.
3545          ((eq type 'original)
3546           (concat "Date: " (if (string-match "\n+$" date)
3547                                (substring date 0 (match-beginning 0))
3548                              date)))
3549          ;; Let the user define the format.
3550          ((eq type 'user-defined)
3551           (let ((format (or (condition-case nil
3552                                 (with-current-buffer gnus-summary-buffer
3553                                   gnus-article-time-format)
3554                               (error nil))
3555                             gnus-article-time-format)))
3556             (if (functionp format)
3557                 (funcall format time)
3558               (concat "Date: " (format-time-string format time)))))
3559          ;; ISO 8601.
3560          ((eq type 'iso8601)
3561           (let ((tz (car (current-time-zone time))))
3562             (concat
3563              "Date: "
3564              (format-time-string "%Y%m%dT%H%M%S" time)
3565              (format "%s%02d%02d"
3566                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3567                      (/ (% (abs tz) 3600) 60)))))
3568          ;; Do a lapsed format.
3569          ((eq type 'lapsed)
3570           (concat "Date: " (article-lapsed-string time)))
3571          ;; A combined date/lapsed format.
3572          ((eq type 'combined-lapsed)
3573           (let ((date-string (article-make-date-line date 'original))
3574                 (segments 3)
3575                 lapsed-string)
3576             (while (and
3577                     time
3578                     (setq lapsed-string
3579                           (concat " (" (article-lapsed-string time segments) ")"))
3580                     (> (+ (length date-string)
3581                           (length lapsed-string))
3582                        (+ fill-column 6))
3583                     (> segments 0))
3584               (setq segments (1- segments)))
3585             (if (> segments 0)
3586                 (concat date-string lapsed-string)
3587               date-string)))
3588          ;; Display the date in proper English
3589          ((eq type 'english)
3590           (let ((dtime (decode-time time)))
3591             (concat
3592              "Date: the "
3593              (number-to-string (nth 3 dtime))
3594              (let ((digit (% (nth 3 dtime) 10)))
3595                (cond
3596                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3597                 ((= digit 1) "st")
3598                 ((= digit 2) "nd")
3599                 ((= digit 3) "rd")
3600                 (t "th")))
3601              " of "
3602              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3603              " "
3604              (number-to-string (nth 5 dtime))
3605              " at "
3606              (format "%02d" (nth 2 dtime))
3607              ":"
3608              (format "%02d" (nth 1 dtime)))))))
3609     (foo
3610      (format "Date: %s (from Gnus)" date))))
3611
3612 (defun article-lapsed-string (time &optional max-segments)
3613   ;; If the date is seriously mangled, the timezone functions are
3614   ;; liable to bug out, so we ignore all errors.
3615   (let* ((now (current-time))
3616          (real-time (subtract-time now time))
3617          (real-sec (and real-time
3618                         (+ (* (float (car real-time)) 65536)
3619                            (cadr real-time))))
3620          (sec (and real-time (abs real-sec)))
3621          (segments 0)
3622          num prev)
3623     (unless max-segments
3624       (setq max-segments (length article-time-units)))
3625     (cond
3626      ((null real-time)
3627       "Unknown")
3628      ((zerop sec)
3629       "Now")
3630      (t
3631       (concat
3632        ;; This is a bit convoluted, but basically we go
3633        ;; through the time units for years, weeks, etc,
3634        ;; and divide things to see whether that results
3635        ;; in positive answers.
3636        (mapconcat
3637         (lambda (unit)
3638           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3639                   (>= segments max-segments))
3640               ;; The (remaining) seconds are too few to
3641               ;; be divided into this time unit.
3642               ""
3643             ;; It's big enough, so we output it.
3644             (setq sec (- sec (* num (cdr unit))))
3645             (prog1
3646                 (concat (if prev ", " "") (int-to-string
3647                                            (floor num))
3648                         " " (symbol-name (car unit))
3649                         (if (> num 1) "s" ""))
3650               (setq prev t
3651                     segments (1+ segments)))))
3652         article-time-units "")
3653        ;; If dates are odd, then it might appear like the
3654        ;; article was sent in the future.
3655        (if (> real-sec 0)
3656            " ago"
3657          " in the future"))))))
3658
3659 (defun article-date-local (&optional highlight)
3660   "Convert the current article date to the local timezone."
3661   (interactive (list t))
3662   (article-date-ut 'local highlight))
3663
3664 (defun article-date-english (&optional highlight)
3665   "Convert the current article date to something that is proper English."
3666   (interactive (list t))
3667   (article-date-ut 'english highlight))
3668
3669 (defun article-date-original (&optional highlight)
3670   "Convert the current article date to what it was originally.
3671 This is only useful if you have used some other date conversion
3672 function and want to see what the date was before converting."
3673   (interactive (list t))
3674   (article-date-ut 'original highlight))
3675
3676 (defun article-date-lapsed (&optional highlight)
3677   "Convert the current article date to time lapsed since it was sent."
3678   (interactive (list t))
3679   (article-date-ut 'lapsed highlight))
3680
3681 (defun article-date-combined-lapsed (&optional highlight)
3682   "Convert the current article date to time lapsed since it was sent."
3683   (interactive (list t))
3684   (article-date-ut 'combined-lapsed highlight))
3685
3686 (defun article-update-date-lapsed ()
3687   "Function to be run from a timer to update the lapsed time line."
3688   (save-match-data
3689     (let ((buffer (current-buffer)))
3690       (ignore-errors
3691         (walk-windows
3692          (lambda (w)
3693            (set-buffer (window-buffer w))
3694            (when (eq major-mode 'gnus-article-mode)
3695              (let ((old-line (count-lines (point-min) (point)))
3696                    (old-column (- (point) (line-beginning-position)))
3697                    (window-start
3698                     (window-start (get-buffer-window (current-buffer)))))
3699                (goto-char (point-min))
3700                (while (re-search-forward "^Date:" nil t)
3701                  (let ((type (get-text-property (match-beginning 0)
3702                                                 'gnus-date-type)))
3703                    (when (memq type '(lapsed combined-lapsed user-format))
3704                      (when (and window-start
3705                                 (not (= window-start
3706                                         (save-excursion
3707                                           (forward-line 1)
3708                                           (point)))))
3709                        (setq window-start nil))
3710                      (save-excursion
3711                        (article-date-ut type t (match-beginning 0)))
3712                      (forward-line 1)
3713                      (when window-start
3714                        (set-window-start (get-buffer-window (current-buffer))
3715                                          (point))))))
3716                (goto-char (point-min))
3717                (when (> old-column 0)
3718                  (setq old-line (1- old-line)))
3719                (forward-line old-line)
3720                (end-of-line)
3721                (when (> (current-column) old-column)
3722                  (beginning-of-line)
3723                  (forward-char old-column)))))
3724          nil 'visible))
3725       (set-buffer buffer))))
3726
3727 (defun gnus-start-date-timer (&optional n)
3728   "Start a timer to update the Date headers in the article buffers.
3729 The numerical prefix says how frequently (in seconds) the function
3730 is to run."
3731   (interactive "p")
3732   (unless n
3733     (setq n 1))
3734   (gnus-stop-date-timer)
3735   (setq article-lapsed-timer
3736         (run-at-time 1 n 'article-update-date-lapsed)))
3737
3738 (defun gnus-stop-date-timer ()
3739   "Stop the Date timer."
3740   (interactive)
3741   (when article-lapsed-timer
3742     (nnheader-cancel-timer article-lapsed-timer)
3743     (setq article-lapsed-timer nil)))
3744
3745 (defun article-date-user (&optional highlight)
3746   "Convert the current article date to the user-defined format.
3747 This format is defined by the `gnus-article-time-format' variable."
3748   (interactive (list t))
3749   (article-date-ut 'user highlight))
3750
3751 (defun article-date-iso8601 (&optional highlight)
3752   "Convert the current article date to ISO8601."
3753   (interactive (list t))
3754   (article-date-ut 'iso8601 highlight))
3755
3756 (defmacro gnus-article-save-original-date (&rest forms)
3757   "Save the original date as a text property and evaluate FORMS."
3758   `(let* ((case-fold-search t)
3759           (start (progn
3760                    (goto-char (point-min))
3761                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3762                               (not (bolp)))
3763                      (match-end 0))))
3764           (date (when (and start
3765                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3766                                               nil t))
3767                   (buffer-substring-no-properties start
3768                                                   (match-beginning 0)))))
3769      (goto-char (point-max))
3770      (skip-chars-backward "\n")
3771      (put-text-property (point-min) (point) 'original-date date)
3772      ,@forms
3773      (goto-char (point-max))
3774      (skip-chars-backward "\n")
3775      (put-text-property (point-min) (point) 'original-date date)))
3776
3777 ;; (defun article-show-all ()
3778 ;;   "Show all hidden text in the article buffer."
3779 ;;   (interactive)
3780 ;;   (save-excursion
3781 ;;     (let ((inhibit-read-only t))
3782 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3783
3784 (defun article-remove-leading-whitespace ()
3785   "Remove excessive whitespace from all headers."
3786   (interactive)
3787   (save-excursion
3788     (save-restriction
3789       (let ((inhibit-read-only t))
3790         (article-narrow-to-head)
3791         (goto-char (point-min))
3792         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3793           (delete-region (match-beginning 1) (match-end 1)))))))
3794
3795 (defun article-emphasize (&optional arg)
3796   "Emphasize text according to `gnus-emphasis-alist'."
3797   (interactive (gnus-article-hidden-arg))
3798   (unless (gnus-article-check-hidden-text 'emphasis arg)
3799     (save-excursion
3800       (let ((alist (or
3801                     (condition-case nil
3802                         (with-current-buffer gnus-summary-buffer
3803                           gnus-article-emphasis-alist)
3804                       (error))
3805                     gnus-emphasis-alist))
3806             (inhibit-read-only t)
3807             (props (append '(article-type emphasis)
3808                            gnus-hidden-properties))
3809             regexp elem beg invisible visible face)
3810         (article-goto-body)
3811         (setq beg (point))
3812         (while (setq elem (pop alist))
3813           (goto-char beg)
3814           (setq regexp (car elem)
3815                 invisible (nth 1 elem)
3816                 visible (nth 2 elem)
3817                 face (nth 3 elem))
3818           (while (re-search-forward regexp nil t)
3819             (when (and (match-beginning visible) (match-beginning invisible))
3820               (gnus-article-hide-text
3821                (match-beginning invisible) (match-end invisible) props)
3822               (gnus-article-unhide-text-type
3823                (match-beginning visible) (match-end visible) 'emphasis)
3824               (gnus-put-overlay-excluding-newlines
3825                (match-beginning visible) (match-end visible) 'face face)
3826               (gnus-add-wash-type 'emphasis)
3827               (goto-char (match-end invisible)))))))))
3828
3829 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3830   "Setup newsgroup emphasis alist."
3831   (unless gnus-article-emphasis-alist
3832     (let ((name (and gnus-newsgroup-name
3833                      (gnus-group-real-name gnus-newsgroup-name))))
3834       (make-local-variable 'gnus-article-emphasis-alist)
3835       (setq gnus-article-emphasis-alist
3836             (nconc
3837              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3838                (while (setq elem (pop alist))
3839                  (when (and name (string-match (car elem) name))
3840                    (setq alist nil
3841                          highlight (copy-sequence (cdr elem)))))
3842                highlight)
3843              (copy-sequence highlight-words)
3844              (if gnus-newsgroup-name
3845                  (copy-sequence (gnus-group-find-parameter
3846                                  gnus-newsgroup-name 'highlight-words t)))
3847              gnus-emphasis-alist)))))
3848
3849 (defvar gnus-summary-article-menu)
3850 (defvar gnus-summary-post-menu)
3851
3852 ;;; Saving functions.
3853
3854 (defun gnus-article-save (save-buffer file &optional num)
3855   "Save the currently selected article."
3856   (when (or (get gnus-default-article-saver :headers)
3857             (not gnus-save-all-headers))
3858     ;; Remove headers according to `gnus-saved-headers' or the value
3859     ;; of the `:headers' property that the saver function might have.
3860     (let ((gnus-visible-headers
3861            (or (symbol-value (get gnus-default-article-saver :headers))
3862                gnus-saved-headers gnus-visible-headers))
3863           ;; Ignore group parameter.  See `article-hide-headers'.
3864           (gnus-summary-buffer nil))
3865       (with-current-buffer save-buffer
3866         (article-hide-headers 1 t))))
3867   (save-window-excursion
3868     (if (not gnus-default-article-saver)
3869         (error "No default saver is defined")
3870       ;; !!! Magic!  The saving functions all save
3871       ;; `gnus-save-article-buffer' (or so they think), but we
3872       ;; bind that variable to our save-buffer.
3873       (set-buffer gnus-article-buffer)
3874       (let* ((gnus-save-article-buffer save-buffer)
3875              (filename
3876               (cond
3877                ((not gnus-prompt-before-saving) 'default)
3878                ((eq gnus-prompt-before-saving 'always) nil)
3879                (t file)))
3880              (gnus-number-of-articles-to-be-saved
3881               (when (eq gnus-prompt-before-saving t)
3882                 num)))                  ; Magic
3883         (set-buffer gnus-article-current-summary)
3884         (funcall gnus-default-article-saver filename)))))
3885
3886 (defun gnus-read-save-file-name (prompt &optional filename
3887                                         function group headers variable
3888                                         dir-var)
3889   (let ((default-name
3890           (funcall function group headers (symbol-value variable)))
3891         result)
3892     (setq result
3893           (expand-file-name
3894            (cond
3895             ((eq filename 'default)
3896              default-name)
3897             ((eq filename t)
3898              default-name)
3899             (filename filename)
3900             (t
3901              (when (symbol-value dir-var)
3902                (setq default-name (expand-file-name
3903                                    (file-name-nondirectory default-name)
3904                                    (symbol-value dir-var))))
3905              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3906                     (prompt
3907                      (format prompt
3908                              (if (and gnus-number-of-articles-to-be-saved
3909                                       (> gnus-number-of-articles-to-be-saved 1))
3910                                  (format "these %d articles"
3911                                          gnus-number-of-articles-to-be-saved)
3912                                "this article")))
3913                     (file
3914                      ;; Let the split methods have their say.
3915                      (cond
3916                       ;; No split name was found.
3917                       ((null split-name)
3918                        (read-file-name
3919                         (concat prompt " (default "
3920                                 (file-name-nondirectory default-name) "): ")
3921                         (file-name-directory default-name)
3922                         default-name))
3923                       ;; A single group name is returned.
3924                       ((stringp split-name)
3925                        (setq default-name
3926                              (funcall function split-name headers
3927                                       (symbol-value variable)))
3928                        (read-file-name
3929                         (concat prompt " (default "
3930                                 (file-name-nondirectory default-name) "): ")
3931                         (file-name-directory default-name)
3932                         default-name))
3933                       ;; A single split name was found
3934                       ((= 1 (length split-name))
3935                        (let* ((name (expand-file-name
3936                                      (car split-name)
3937                                      gnus-article-save-directory))
3938                               (dir (cond ((file-directory-p name)
3939                                           (file-name-as-directory name))
3940                                          ((file-exists-p name) name)
3941                                          (t gnus-article-save-directory))))
3942                          (read-file-name
3943                           (concat prompt " (default " name "): ")
3944                           dir name)))
3945                       ;; A list of splits was found.
3946                       (t
3947                        (setq split-name (nreverse split-name))
3948                        (let (result)
3949                          (let ((file-name-history
3950                                 (nconc split-name file-name-history)))
3951                            (setq result
3952                                  (expand-file-name
3953                                   (read-file-name
3954                                    (concat prompt " (`M-p' for defaults): ")
3955                                    gnus-article-save-directory
3956                                    (car split-name))
3957                                   gnus-article-save-directory)))
3958                          (car (push result file-name-history)))))))
3959                ;; Create the directory.
3960                (gnus-make-directory (file-name-directory file))
3961                ;; If we have read a directory, we append the default file name.
3962                (when (file-directory-p file)
3963                  (setq file (expand-file-name (file-name-nondirectory
3964                                                default-name)
3965                                               (file-name-as-directory file))))
3966                ;; Possibly translate some characters.
3967                (nnheader-translate-file-chars file))))))
3968     (gnus-make-directory (file-name-directory result))
3969     (when variable
3970       (set variable result))
3971     (when dir-var
3972       (set dir-var (file-name-directory result)))
3973     result))
3974
3975 (defun gnus-article-archive-name (group)
3976   "Return the first instance of an \"Archive-name\" in the current buffer."
3977   (let ((case-fold-search t))
3978     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3979       (nnheader-concat gnus-article-save-directory
3980                        (match-string 1)))))
3981
3982 (defun gnus-article-nndoc-name (group)
3983   "If GROUP is an nndoc group, return the name of the parent group."
3984   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3985     (gnus-group-get-parameter group 'save-article-group)))
3986
3987 (defun gnus-summary-save-in-rmail (&optional filename)
3988   "Append this article to Rmail file.
3989 Optional argument FILENAME specifies file name.
3990 Directory to save to is default to `gnus-article-save-directory'."
3991   (setq filename (gnus-read-save-file-name
3992                   "Save %s in rmail file" filename
3993                   gnus-rmail-save-name gnus-newsgroup-name
3994                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3995   (with-current-buffer gnus-save-article-buffer
3996     (save-excursion
3997       (save-restriction
3998         (widen)
3999         ;; Note that unlike gnus-summary-save-in-mail, there is no
4000         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
4001         ;; not use Babyl.
4002         (gnus-output-to-rmail filename))))
4003   filename)
4004
4005 (defun gnus-summary-save-in-mail (&optional filename)
4006   "Append this article to Unix mail file.
4007 Optional argument FILENAME specifies file name.
4008 Directory to save to is default to `gnus-article-save-directory'."
4009   (setq filename (gnus-read-save-file-name
4010                   "Save %s in Unix mail file" filename
4011                   gnus-mail-save-name gnus-newsgroup-name
4012                   gnus-current-headers 'gnus-newsgroup-last-mail))
4013   (with-current-buffer gnus-save-article-buffer
4014     (save-excursion
4015       (save-restriction
4016         (widen)
4017         (if (and (file-readable-p filename)
4018                  (file-regular-p filename)
4019                  (mail-file-babyl-p filename))
4020             (gnus-output-to-rmail filename)
4021           (gnus-output-to-mail filename)))))
4022   filename)
4023
4024 (put 'gnus-summary-save-in-file :decode t)
4025 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4026 (defun gnus-summary-save-in-file (&optional filename overwrite)
4027   "Append this article to file.
4028 Optional argument FILENAME specifies file name.
4029 Directory to save to is default to `gnus-article-save-directory'."
4030   (setq filename (gnus-read-save-file-name
4031                   "Save %s in file" filename
4032                   gnus-file-save-name gnus-newsgroup-name
4033                   gnus-current-headers 'gnus-newsgroup-last-file))
4034   (with-current-buffer gnus-save-article-buffer
4035     (save-excursion
4036       (save-restriction
4037         (widen)
4038         (when (and overwrite
4039                    (file-exists-p filename))
4040           (delete-file filename))
4041         (gnus-output-to-file filename))))
4042   filename)
4043
4044 (put 'gnus-summary-write-to-file :decode t)
4045 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4046 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4047 (defun gnus-summary-write-to-file (&optional filename)
4048   "Write this article to a file, overwriting it if the file exists.
4049 Optional argument FILENAME specifies file name.
4050 The directory to save in defaults to `gnus-article-save-directory'."
4051   (setq filename (gnus-read-save-file-name
4052                   "Save %s in file" filename
4053                   gnus-file-save-name gnus-newsgroup-name
4054                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4055   (gnus-summary-save-in-file filename t))
4056
4057 (put 'gnus-summary-save-body-in-file :decode t)
4058 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4059   "Append this article body to a file.
4060 Optional argument FILENAME specifies file name.
4061 The directory to save in defaults to `gnus-article-save-directory'."
4062   (setq filename (gnus-read-save-file-name
4063                   "Save %s body in file" filename
4064                   gnus-file-save-name gnus-newsgroup-name
4065                   gnus-current-headers 'gnus-newsgroup-last-file))
4066   (with-current-buffer gnus-save-article-buffer
4067     (save-excursion
4068       (save-restriction
4069         (widen)
4070         (when (article-goto-body)
4071           (narrow-to-region (point) (point-max)))
4072         (when (and overwrite
4073                    (file-exists-p filename))
4074           (delete-file filename))
4075         (gnus-output-to-file filename))))
4076   filename)
4077
4078 (put 'gnus-summary-write-body-to-file :decode t)
4079 (put 'gnus-summary-write-body-to-file
4080      :function 'gnus-summary-save-body-in-file)
4081 (defun gnus-summary-write-body-to-file (&optional filename)
4082   "Write this article body to a file, overwriting it if the file exists.
4083 Optional argument FILENAME specifies file name.
4084 The directory to save in defaults to `gnus-article-save-directory'."
4085   (setq filename (gnus-read-save-file-name
4086                   "Save %s body in file" filename
4087                   gnus-file-save-name gnus-newsgroup-name
4088                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4089   (gnus-summary-save-body-in-file filename t))
4090
4091 (put 'gnus-summary-save-in-pipe :decode t)
4092 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4093 (defun gnus-summary-save-in-pipe (&optional command raw)
4094   "Pipe this article to subprocess COMMAND.
4095 Valid values for COMMAND include:
4096   a string
4097     The executable command name and possibly arguments.
4098   nil
4099     You will be prompted for the command in the minibuffer.
4100   the symbol `default'
4101     It will be replaced with the command which the variable
4102     `gnus-summary-pipe-output-default-command' holds or the command
4103     last used for saving.
4104 Non-nil value for RAW overrides `:decode' and `:headers' properties
4105 and the raw article including all headers will be piped."
4106   (let ((article (gnus-summary-article-number))
4107         (decode (unless raw
4108                   (get 'gnus-summary-save-in-pipe :decode)))
4109         save-buffer default)
4110     (if article
4111         (if (vectorp (gnus-summary-article-header article))
4112             (save-current-buffer
4113               (gnus-summary-select-article decode decode nil article)
4114               (insert-buffer-substring
4115                (prog1
4116                    (if decode
4117                        gnus-article-buffer
4118                      gnus-original-article-buffer)
4119                  (setq save-buffer
4120                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4121               ;; Remove unwanted headers.
4122               (when (and (not raw)
4123                          (or (get 'gnus-summary-save-in-pipe :headers)
4124                              (not gnus-save-all-headers)))
4125                 (let ((gnus-visible-headers
4126                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4127                                               :headers))
4128                            gnus-saved-headers gnus-visible-headers))
4129                       (gnus-summary-buffer nil))
4130                   (article-hide-headers 1 t))))
4131           (error "%d is not a real article" article))
4132       (error "No article to pipe"))
4133     (setq default (or gnus-summary-pipe-output-default-command
4134                       gnus-last-shell-command))
4135     (unless (stringp command)
4136       (setq command
4137             (if (and (eq command 'default) default)
4138                 default
4139               (gnus-read-shell-command "Shell command on this article: "
4140                                        default))))
4141     (when (string-equal command "")
4142       (if default
4143           (setq command default)
4144         (error "A command is required")))
4145     (with-current-buffer save-buffer
4146       (save-restriction
4147         (widen)
4148         (shell-command-on-region (point-min) (point-max) command nil)))
4149     (gnus-kill-buffer save-buffer))
4150   (setq gnus-summary-pipe-output-default-command command))
4151
4152 (defun gnus-summary-pipe-to-muttprint (&optional command)
4153   "Pipe this article to muttprint."
4154   (unless (stringp command)
4155     (setq command (read-string
4156                    "Print using command: " gnus-summary-muttprint-program
4157                    nil gnus-summary-muttprint-program)))
4158   (let ((gnus-summary-pipe-output-default-command
4159          gnus-summary-pipe-output-default-command))
4160     (gnus-summary-save-in-pipe command))
4161   (setq gnus-summary-muttprint-program command))
4162
4163 ;;; Article file names when saving.
4164
4165 (defun gnus-capitalize-newsgroup (newsgroup)
4166   "Capitalize NEWSGROUP name."
4167   (when (not (zerop (length newsgroup)))
4168     (concat (char-to-string (upcase (aref newsgroup 0)))
4169             (substring newsgroup 1))))
4170
4171 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4172   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4173 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4174 Otherwise, it is like ~/News/news/group/num."
4175   (let ((default
4176           (expand-file-name
4177            (concat (if (gnus-use-long-file-name 'not-save)
4178                        (gnus-capitalize-newsgroup newsgroup)
4179                      (gnus-newsgroup-directory-form newsgroup))
4180                    "/" (int-to-string (mail-header-number headers)))
4181            gnus-article-save-directory)))
4182     (if (and last-file
4183              (string-equal (file-name-directory default)
4184                            (file-name-directory last-file))
4185              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4186         default
4187       (or last-file default))))
4188
4189 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4190   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4191 If variable `gnus-use-long-file-name' is non-nil, it is
4192 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4193   (let ((default
4194           (expand-file-name
4195            (concat (if (gnus-use-long-file-name 'not-save)
4196                        newsgroup
4197                      (gnus-newsgroup-directory-form newsgroup))
4198                    "/" (int-to-string (mail-header-number headers)))
4199            gnus-article-save-directory)))
4200     (if (and last-file
4201              (string-equal (file-name-directory default)
4202                            (file-name-directory last-file))
4203              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4204         default
4205       (or last-file default))))
4206
4207 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4208   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4209 If variable `gnus-use-long-file-name' is non-nil, it is
4210 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4211   (or last-file
4212       (expand-file-name
4213        (if (gnus-use-long-file-name 'not-save)
4214            newsgroup
4215          (file-relative-name
4216           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4217           default-directory))
4218        gnus-article-save-directory)))
4219
4220 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4221   "Generate file name from sender."
4222   (let ((from (mail-header-from headers)))
4223     (expand-file-name
4224      (if (and from (string-match "\\([^ <]+\\)@" from))
4225          (match-string 1 from)
4226        "nobody")
4227      gnus-article-save-directory)))
4228
4229 (defun article-verify-x-pgp-sig ()
4230   "Verify X-PGP-Sig."
4231   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4232   (interactive)
4233   (if (gnus-buffer-live-p gnus-original-article-buffer)
4234       (let ((sig (with-current-buffer gnus-original-article-buffer
4235                    (gnus-fetch-field "X-PGP-Sig")))
4236             items info headers)
4237         (when (and sig
4238                    mml2015-use
4239                    (mml2015-clear-verify-function))
4240           (with-temp-buffer
4241             (insert-buffer-substring gnus-original-article-buffer)
4242             (setq items (split-string sig))
4243             (message-narrow-to-head)
4244             (let ((inhibit-point-motion-hooks t)
4245                   (case-fold-search t))
4246               ;; Don't verify multiple headers.
4247               (setq headers (mapconcat (lambda (header)
4248                                          (concat header ": "
4249                                                  (mail-fetch-field header)
4250                                                  "\n"))
4251                                        (split-string (nth 1 items) ",") "")))
4252             (delete-region (point-min) (point-max))
4253             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4254             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4255             (insert headers)
4256             (widen)
4257             (forward-line)
4258             (while (not (eobp))
4259               (if (looking-at "^-")
4260                   (insert "- "))
4261               (forward-line))
4262             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4263             (insert "Version: " (car items) "\n\n")
4264             (insert (mapconcat 'identity (cddr items) "\n"))
4265             (insert "\n-----END PGP SIGNATURE-----\n")
4266             (let ((mm-security-handle (list (format "multipart/signed"))))
4267               (mml2015-clean-buffer)
4268               (let ((coding-system-for-write (or gnus-newsgroup-charset
4269                                                  'iso-8859-1)))
4270                 (funcall (mml2015-clear-verify-function)))
4271               (setq info
4272                     (or (mm-handle-multipart-ctl-parameter
4273                          mm-security-handle 'gnus-details)
4274                         (mm-handle-multipart-ctl-parameter
4275                          mm-security-handle 'gnus-info)))))
4276           (when info
4277             (let ((inhibit-read-only t) bface eface)
4278               (save-restriction
4279                 (message-narrow-to-head)
4280                 (goto-char (point-max))
4281                 (forward-line -1)
4282                 (setq bface (get-text-property (point-at-bol) 'face)
4283                       eface (get-text-property (1- (point-at-eol)) 'face))
4284                 (message-remove-header "X-Gnus-PGP-Verify")
4285                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4286                     (forward-line)
4287                   (goto-char (point-max)))
4288                 (narrow-to-region (point) (point))
4289                 (insert "X-Gnus-PGP-Verify: " info "\n")
4290                 (goto-char (point-min))
4291                 (forward-line)
4292                 (while (not (eobp))
4293                   (if (not (looking-at "^[ \t]"))
4294                       (insert " "))
4295                   (forward-line))
4296                 ;; Do highlighting.
4297                 (goto-char (point-min))
4298                 (when (looking-at "\\([^:]+\\): *")
4299                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4300                                      'face bface)
4301                   (put-text-property (match-end 0) (point-max)
4302                                      'face eface)))))))))
4303
4304 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4305
4306 (defun article-verify-cancel-lock ()
4307   "Verify Cancel-Lock header."
4308   (interactive)
4309   (if (gnus-buffer-live-p gnus-original-article-buffer)
4310       (canlock-verify gnus-original-article-buffer)))
4311
4312 (eval-and-compile
4313   (mapc
4314    (lambda (func)
4315      (let (afunc gfunc)
4316        (if (consp func)
4317            (setq afunc (car func)
4318                  gfunc (cdr func))
4319          (setq afunc func
4320                gfunc (intern (format "gnus-%s" func))))
4321        (defalias gfunc
4322          (when (fboundp afunc)
4323            `(lambda (&optional interactive &rest args)
4324               ,(documentation afunc t)
4325               (interactive (list t))
4326               (with-current-buffer gnus-article-buffer
4327                 (if interactive
4328                     (call-interactively ',afunc)
4329                   (apply ',afunc args))))))))
4330    '(article-hide-headers
4331      article-verify-x-pgp-sig
4332      article-verify-cancel-lock
4333      article-hide-boring-headers
4334      article-treat-overstrike
4335      article-treat-ansi-sequences
4336      article-fill-long-lines
4337      article-capitalize-sentences
4338      article-remove-cr
4339      article-remove-leading-whitespace
4340      article-display-x-face
4341      article-display-face
4342      article-de-quoted-unreadable
4343      article-de-base64-unreadable
4344      article-decode-HZ
4345      article-wash-html
4346      article-unsplit-urls
4347      article-hide-list-identifiers
4348      article-strip-banner
4349      article-babel
4350      article-hide-pem
4351      article-hide-signature
4352      article-strip-headers-in-body
4353      article-remove-trailing-blank-lines
4354      article-strip-leading-blank-lines
4355      article-strip-multiple-blank-lines
4356      article-strip-leading-space
4357      article-strip-trailing-space
4358      article-strip-blank-lines
4359      article-strip-all-blank-lines
4360      article-date-local
4361      article-date-english
4362      article-date-iso8601
4363      article-date-original
4364      article-treat-date
4365      article-date-ut
4366      article-decode-mime-words
4367      article-decode-charset
4368      article-decode-encoded-words
4369      article-date-user
4370      article-date-lapsed
4371      article-date-combined-lapsed
4372      article-emphasize
4373      article-treat-dumbquotes
4374      article-treat-non-ascii
4375      article-normalize-headers
4376      ;;(article-show-all . gnus-article-show-all-headers)
4377      )))
4378 \f
4379 ;;;
4380 ;;; Gnus article mode
4381 ;;;
4382
4383 (put 'gnus-article-mode 'mode-class 'special)
4384
4385 (set-keymap-parent gnus-article-mode-map widget-keymap)
4386
4387 (gnus-define-keys gnus-article-mode-map
4388   " " gnus-article-goto-next-page
4389   "\177" gnus-article-goto-prev-page
4390   [delete] gnus-article-goto-prev-page
4391   [backspace] gnus-article-goto-prev-page
4392   "\C-c^" gnus-article-refer-article
4393   "h" gnus-article-show-summary
4394   "s" gnus-article-show-summary
4395   "\C-c\C-m" gnus-article-mail
4396   "?" gnus-article-describe-briefly
4397   "<" beginning-of-buffer
4398   ">" end-of-buffer
4399   "\C-c\C-i" gnus-info-find-node
4400   "\C-c\C-b" gnus-bug
4401   "R" gnus-article-reply-with-original
4402   "F" gnus-article-followup-with-original
4403   "\C-hk" gnus-article-describe-key
4404   "\C-hc" gnus-article-describe-key-briefly
4405   "\C-hb" gnus-article-describe-bindings
4406
4407   "e" gnus-article-read-summary-keys
4408   "\C-d" gnus-article-read-summary-keys
4409   "\M-*" gnus-article-read-summary-keys
4410   "\M-#" gnus-article-read-summary-keys
4411   "\M-^" gnus-article-read-summary-keys
4412   "\M-g" gnus-article-read-summary-keys)
4413
4414 (substitute-key-definition
4415  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4416
4417 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4418   "W" gnus-article-wide-reply-with-original)
4419 (if (featurep 'xemacs)
4420     (set-keymap-default-binding gnus-article-send-map
4421                                 'gnus-article-read-summary-send-keys)
4422   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4423
4424 (defun gnus-article-make-menu-bar ()
4425   (unless (boundp 'gnus-article-commands-menu)
4426     (gnus-summary-make-menu-bar))
4427   (unless (boundp 'gnus-article-article-menu)
4428     (easy-menu-define
4429      gnus-article-article-menu gnus-article-mode-map ""
4430      '("Article"
4431        ["Scroll forwards" gnus-article-goto-next-page t]
4432        ["Scroll backwards" gnus-article-goto-prev-page t]
4433        ["Show summary" gnus-article-show-summary t]
4434        ["Fetch Message-ID at point" gnus-article-refer-article t]
4435        ["Mail to address at point" gnus-article-mail t]
4436        ["Send a bug report" gnus-bug t]))
4437
4438     (easy-menu-define
4439      gnus-article-treatment-menu gnus-article-mode-map ""
4440      ;; Fixme: this should use :active (and maybe :visible).
4441      '("Treatment"
4442        ["Hide headers" gnus-article-hide-headers t]
4443        ["Hide signature" gnus-article-hide-signature t]
4444        ["Hide citation" gnus-article-hide-citation t]
4445        ["Treat overstrike" gnus-article-treat-overstrike t]
4446        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4447        ["Remove carriage return" gnus-article-remove-cr t]
4448        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4449        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4450        ["Remove base64" gnus-article-de-base64-unreadable t]
4451        ["Treat html" gnus-article-wash-html t]
4452        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4453        ["Decode HZ" gnus-article-decode-HZ t]))
4454
4455     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4456
4457     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4458
4459     (gnus-run-hooks 'gnus-article-menu-hook)))
4460
4461 (defvar bookmark-make-record-function)
4462 (defvar shr-put-image-function)
4463
4464 (defun gnus-article-mode ()
4465   "Major mode for displaying an article.
4466
4467 All normal editing commands are switched off.
4468
4469 The following commands are available in addition to all summary mode
4470 commands:
4471 \\<gnus-article-mode-map>
4472 \\[gnus-article-next-page]\t Scroll the article one page forwards
4473 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4474 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4475 \\[gnus-article-show-summary]\t Display the summary buffer
4476 \\[gnus-article-mail]\t Send a reply to the address near point
4477 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4478 \\[gnus-info-find-node]\t Go to the Gnus info node"
4479   (interactive)
4480   (kill-all-local-variables)
4481   (gnus-simplify-mode-line)
4482   (setq mode-name "Article")
4483   (setq major-mode 'gnus-article-mode)
4484   (make-local-variable 'minor-mode-alist)
4485   (use-local-map gnus-article-mode-map)
4486   (when (gnus-visual-p 'article-menu 'menu)
4487     (gnus-article-make-menu-bar)
4488     (when gnus-summary-tool-bar-map
4489       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4490   (gnus-update-format-specifications nil 'article-mode)
4491   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4492   (set (make-local-variable 'gnus-page-broken) nil)
4493   (make-local-variable 'gnus-article-current-summary)
4494   (make-local-variable 'gnus-article-mime-handles)
4495   (make-local-variable 'gnus-article-decoded-p)
4496   (make-local-variable 'gnus-article-mime-handle-alist)
4497   (make-local-variable 'gnus-article-wash-types)
4498   (make-local-variable 'gnus-article-image-alist)
4499   (make-local-variable 'gnus-article-charset)
4500   (make-local-variable 'gnus-article-ignored-charsets)
4501   (set (make-local-variable 'bookmark-make-record-function)
4502        'gnus-summary-bookmark-make-record)
4503   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4504   ;; face.
4505   (set (make-local-variable 'nobreak-char-display) nil)
4506   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4507   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4508   (setq cursor-in-non-selected-windows nil)
4509   (gnus-set-default-directory)
4510   (buffer-disable-undo)
4511   (setq buffer-read-only t
4512         show-trailing-whitespace nil)
4513   (set-syntax-table gnus-article-mode-syntax-table)
4514   (mm-enable-multibyte)
4515   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4516
4517 (defun gnus-article-setup-buffer ()
4518   "Initialize the article buffer."
4519   (let* ((name (if gnus-single-article-buffer "*Article*"
4520                  (concat "*Article "
4521                          (gnus-group-decoded-name gnus-newsgroup-name)
4522                          "*")))
4523          (original
4524           (progn (string-match "\\*Article" name)
4525                  (concat " *Original Article"
4526                          (substring name (match-end 0))))))
4527     (setq gnus-article-buffer name)
4528     (setq gnus-original-article-buffer original)
4529     (setq gnus-article-mime-handle-alist nil)
4530     (with-current-buffer gnus-summary-buffer
4531       ;; This might be a variable local to the summary buffer.
4532       (unless gnus-single-article-buffer
4533         (setq gnus-article-buffer name)
4534         (setq gnus-original-article-buffer original)
4535         (gnus-set-global-variables)))
4536     (gnus-article-setup-highlight-words)
4537     ;; Init original article buffer.
4538     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4539       (mm-enable-multibyte)
4540       (setq major-mode 'gnus-original-article-mode)
4541       (make-local-variable 'gnus-original-article))
4542     (if (and (get-buffer name)
4543              (with-current-buffer name
4544                (if gnus-article-edit-mode
4545                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4546                        (progn
4547                          (set-buffer-modified-p nil)
4548                          (gnus-kill-buffer name)
4549                          (message "")
4550                          nil)
4551                      (error "Action aborted"))
4552                  t)))
4553         (with-current-buffer name
4554           (set (make-local-variable 'gnus-article-edit-mode) nil)
4555           (gnus-article-stop-animations)
4556           (when gnus-article-mime-handles
4557             (mm-destroy-parts gnus-article-mime-handles)
4558             (setq gnus-article-mime-handles nil))
4559           ;; Set it to nil in article-buffer!
4560           (setq gnus-article-mime-handle-alist nil)
4561           (buffer-disable-undo)
4562           (setq buffer-read-only t)
4563           (unless (eq major-mode 'gnus-article-mode)
4564             (gnus-article-mode))
4565           (setq truncate-lines gnus-article-truncate-lines)
4566           (current-buffer))
4567       (with-current-buffer (gnus-get-buffer-create name)
4568         (gnus-article-mode)
4569         (setq truncate-lines gnus-article-truncate-lines)
4570         (make-local-variable 'gnus-summary-buffer)
4571         (setq gnus-summary-buffer
4572               (gnus-summary-buffer-name gnus-newsgroup-name))
4573         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4574         (when article-lapsed-timer
4575           (gnus-stop-date-timer))
4576         (when gnus-article-update-date-headers
4577           (gnus-start-date-timer gnus-article-update-date-headers))
4578         (current-buffer)))))
4579
4580 (defun gnus-article-stop-animations ()
4581   (dolist (timer (and (boundp 'timer-list)
4582                       timer-list))
4583     (when (eq (elt timer 5) 'image-animate-timeout)
4584       (cancel-timer timer))))
4585
4586 (defun gnus-stop-downloads ()
4587   (when (boundp 'url-queue)
4588     (set (intern "url-queue" obarray) nil)))
4589
4590 ;; Set article window start at LINE, where LINE is the number of lines
4591 ;; from the head of the article.
4592 (defun gnus-article-set-window-start (&optional line)
4593   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4594     (when article-window
4595       (set-window-start
4596        article-window
4597        (with-current-buffer gnus-article-buffer
4598          (goto-char (point-min))
4599          (if (not line)
4600              (point-min)
4601            (gnus-message 6 "Moved to bookmark")
4602            (search-forward "\n\n" nil t)
4603            (forward-line line)
4604            (point)))))))
4605
4606 (defun gnus-article-prepare (article &optional all-headers header)
4607   "Prepare ARTICLE in article mode buffer.
4608 ARTICLE should either be an article number or a Message-ID.
4609 If ARTICLE is an id, HEADER should be the article headers.
4610 If ALL-HEADERS is non-nil, no headers are hidden."
4611   (save-excursion
4612     ;; Make sure we start in a summary buffer.
4613     (unless (eq major-mode 'gnus-summary-mode)
4614       (set-buffer gnus-summary-buffer))
4615     (setq gnus-summary-buffer (current-buffer))
4616     (let* ((gnus-article (if header (mail-header-number header) article))
4617            (summary-buffer (current-buffer))
4618            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4619            (group gnus-newsgroup-name)
4620            result)
4621       (save-excursion
4622         (gnus-article-setup-buffer)
4623         (set-buffer gnus-article-buffer)
4624         ;; Deactivate active regions.
4625         (when (and (boundp 'transient-mark-mode)
4626                    transient-mark-mode)
4627           (setq mark-active nil))
4628         (if (not (setq result (let ((inhibit-read-only t))
4629                                 (gnus-request-article-this-buffer
4630                                  article group))))
4631             ;; There is no such article.
4632             (save-excursion
4633               (when (and (numberp article)
4634                          (not (memq article gnus-newsgroup-sparse)))
4635                 (setq gnus-article-current
4636                       (cons gnus-newsgroup-name article))
4637                 (set-buffer gnus-summary-buffer)
4638                 (setq gnus-current-article article)
4639                 (if (and (memq article gnus-newsgroup-undownloaded)
4640                          (not (gnus-online (gnus-find-method-for-group
4641                                             gnus-newsgroup-name))))
4642                     (progn
4643                       (gnus-summary-set-agent-mark article)
4644                       (message "Message marked for downloading"))
4645                   (gnus-summary-mark-article article gnus-canceled-mark)
4646                   (unless (memq article gnus-newsgroup-sparse)
4647                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4648           (if (or (eq result 'pseudo)
4649                   (eq result 'nneething))
4650               (progn
4651                 (with-current-buffer summary-buffer
4652                   (push article gnus-newsgroup-history)
4653                   (setq gnus-last-article gnus-current-article
4654                         gnus-current-article 0
4655                         gnus-current-headers nil
4656                         gnus-article-current nil)
4657                   (if (eq result 'nneething)
4658                       (gnus-configure-windows 'summary)
4659                     (gnus-configure-windows 'article))
4660                   (gnus-set-global-variables))
4661                 (let ((gnus-article-mime-handle-alist-1
4662                        gnus-article-mime-handle-alist))
4663                   (gnus-set-mode-line 'article)))
4664             ;; The result from the `request' was an actual article -
4665             ;; or at least some text that is now displayed in the
4666             ;; article buffer.
4667             (when (and (numberp article)
4668                        (not (eq article gnus-current-article)))
4669               ;; Seems like a new article has been selected.
4670               ;; `gnus-current-article' must be an article number.
4671               (with-current-buffer summary-buffer
4672                 (push article gnus-newsgroup-history)
4673                 (setq gnus-last-article gnus-current-article
4674                       gnus-current-article article
4675                       gnus-current-headers
4676                       (gnus-summary-article-header gnus-current-article)
4677                       gnus-article-current
4678                       (cons gnus-newsgroup-name gnus-current-article))
4679                 (unless (vectorp gnus-current-headers)
4680                   (setq gnus-current-headers nil))
4681                 (gnus-summary-goto-subject gnus-current-article)
4682                 (when (gnus-summary-show-thread)
4683                   ;; If the summary buffer really was folded, the
4684                   ;; previous goto may not actually have gone to
4685                   ;; the right article, but the thread root instead.
4686                   ;; So we go again.
4687                   (gnus-summary-goto-subject gnus-current-article))
4688                 (gnus-run-hooks 'gnus-mark-article-hook)
4689                 (gnus-set-mode-line 'summary)
4690                 (when (gnus-visual-p 'article-highlight 'highlight)
4691                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4692                 ;; Set the global newsgroup variables here.
4693                 (gnus-set-global-variables)
4694                 (setq gnus-have-all-headers
4695                       (or all-headers gnus-show-all-headers))))
4696             (save-excursion
4697               (gnus-configure-windows 'article))
4698             (when (or (numberp article)
4699                       (stringp article))
4700               (gnus-article-prepare-display)
4701               ;; Do page break.
4702               (goto-char (point-min))
4703               (when gnus-break-pages
4704                 (gnus-narrow-to-page)))
4705             (let ((gnus-article-mime-handle-alist-1
4706                    gnus-article-mime-handle-alist))
4707               (gnus-set-mode-line 'article))
4708             (article-goto-body)
4709             (unless (bobp)
4710               (forward-line -1))
4711             (set-window-point (get-buffer-window (current-buffer)) (point))
4712             (gnus-configure-windows 'article)
4713             (gnus-run-hooks 'gnus-article-prepare-hook)
4714             t))))))
4715
4716 ;;;###autoload
4717 (defun gnus-article-prepare-display ()
4718   "Make the current buffer look like a nice article."
4719   ;; Hooks for getting information from the article.
4720   ;; This hook must be called before being narrowed.
4721   (let ((gnus-article-buffer (current-buffer))
4722         buffer-read-only
4723         (inhibit-read-only t))
4724     (unless (eq major-mode 'gnus-article-mode)
4725       (gnus-article-mode))
4726     (setq buffer-read-only nil
4727           gnus-article-wash-types nil
4728           gnus-article-image-alist nil)
4729     (gnus-run-hooks 'gnus-tmp-internal-hook)
4730     (when gnus-display-mime-function
4731       (funcall gnus-display-mime-function))))
4732
4733 ;;;
4734 ;;; Gnus Sticky Article Mode
4735 ;;;
4736
4737 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4738   "Mode for sticky articles."
4739   ;; Release bindings that won't work.
4740   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4741                              gnus-sticky-article-mode-map)
4742   (substitute-key-definition 'gnus-article-refer-article 'undefined
4743                              gnus-sticky-article-mode-map)
4744   (dolist (k '("e" "h" "s" "F" "R"))
4745     (define-key gnus-sticky-article-mode-map k nil))
4746   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4747   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4748   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4749   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4750
4751 (defun gnus-sticky-article (arg)
4752   "Make the current article sticky.
4753 If a prefix ARG is given, ask for a name for this sticky article buffer."
4754   (interactive "P")
4755   (gnus-summary-show-thread)
4756   (gnus-summary-select-article nil nil 'pseudo)
4757   (let (new-art-buf-name)
4758     (gnus-eval-in-buffer-window gnus-article-buffer
4759       (setq new-art-buf-name
4760             (concat
4761              "*Sticky Article: "
4762              (if arg
4763                  (read-from-minibuffer "Sticky article buffer name: ")
4764                (gnus-with-article-headers
4765                  (gnus-article-goto-header "subject")
4766                  (setq new-art-buf-name
4767                        (buffer-substring-no-properties
4768                         (line-beginning-position) (line-end-position)))
4769                  (goto-char (point-min))
4770                  (gnus-article-goto-header "from")
4771                  (setq new-art-buf-name
4772                        (concat
4773                         new-art-buf-name ", "
4774                         (buffer-substring-no-properties
4775                          (line-beginning-position) (line-end-position))))
4776                  (goto-char (point-min))
4777                  (gnus-article-goto-header "date")
4778                  (setq new-art-buf-name
4779                        (concat
4780                         new-art-buf-name ", "
4781                         (buffer-substring-no-properties
4782                          (line-beginning-position) (line-end-position))))))
4783              "*"))
4784       (if (and (gnus-buffer-live-p new-art-buf-name)
4785                (with-current-buffer new-art-buf-name
4786                  (eq major-mode 'gnus-sticky-article-mode)))
4787           (switch-to-buffer new-art-buf-name)
4788         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4789       (gnus-sticky-article-mode))
4790     (setq gnus-article-buffer new-art-buf-name))
4791   (gnus-summary-recenter)
4792   (gnus-summary-position-point))
4793
4794 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4795   "Kill the given sticky article BUFFER.
4796 If none is given, assume the current buffer and kill it if it has
4797 `gnus-sticky-article-mode'."
4798   (interactive)
4799   (unless buffer
4800     (setq buffer (current-buffer)))
4801   (with-current-buffer buffer
4802     (when (eq major-mode 'gnus-sticky-article-mode)
4803       (gnus-kill-buffer buffer))))
4804
4805 (defun gnus-kill-sticky-article-buffers (arg)
4806   "Kill all sticky article buffers.
4807 If a prefix ARG is given, ask for confirmation."
4808   (interactive "P")
4809   (dolist (buf (gnus-buffers))
4810     (with-current-buffer buf
4811       (when (eq major-mode 'gnus-sticky-article-mode)
4812         (if (not arg)
4813             (gnus-kill-buffer buf)
4814           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4815             (gnus-kill-buffer buf)))))))
4816
4817 ;;;
4818 ;;; Gnus MIME viewing functions
4819 ;;;
4820
4821 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4822   "Format of the MIME buttons.
4823
4824 Valid specifiers include:
4825 %t  The MIME type
4826 %T  MIME type, along with additional info
4827 %n  The `name' parameter
4828 %d  The description, if any
4829 %l  The length of the encoded part
4830 %p  The part identifier number
4831 %e  Dots if the part isn't displayed
4832
4833 General format specifiers can also be used.  See Info node
4834 `(gnus)Formatting Variables'.")
4835
4836 (defvar gnus-mime-button-line-format-alist
4837   '((?t gnus-tmp-type ?s)
4838     (?T gnus-tmp-type-long ?s)
4839     (?n gnus-tmp-name ?s)
4840     (?d gnus-tmp-description ?s)
4841     (?p gnus-tmp-id ?s)
4842     (?l gnus-tmp-length ?d)
4843     (?e gnus-tmp-dots ?s)))
4844
4845 (defvar gnus-mime-button-commands
4846   '((gnus-article-press-button "\r" "Toggle Display")
4847     (gnus-mime-view-part "v" "View Interactively...")
4848     (gnus-mime-view-part-as-type "t" "View As Type...")
4849     (gnus-mime-view-part-as-charset "C" "View As charset...")
4850     (gnus-mime-save-part "o" "Save...")
4851     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4852     (gnus-mime-replace-part "r" "Replace part")
4853     (gnus-mime-delete-part "d" "Delete part")
4854     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4855     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4856     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4857     (gnus-mime-view-part-externally "e" "View Externally")
4858     (gnus-mime-print-part "p" "Print")
4859     (gnus-mime-pipe-part "|" "Pipe To Command...")
4860     (gnus-mime-action-on-part "." "Take action on the part...")))
4861
4862 (defun gnus-article-mime-part-status ()
4863   (if gnus-article-mime-handle-alist-1
4864       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4865           " (1 part)"
4866         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4867     ""))
4868
4869 (defvar gnus-mime-button-map
4870   (let ((map (make-sparse-keymap)))
4871     (define-key map gnus-mouse-2 'gnus-article-push-button)
4872     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4873     (dolist (c gnus-mime-button-commands)
4874       (define-key map (cadr c) (car c)))
4875     map))
4876
4877 (easy-menu-define
4878   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4879   `("MIME Part"
4880     ,@(mapcar (lambda (c)
4881                 (vector (caddr c) (car c) :active t))
4882               gnus-mime-button-commands)))
4883
4884 (defvar gnus-url-button-commands
4885   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4886
4887 (defvar gnus-url-button-map
4888   (let ((map (make-sparse-keymap)))
4889     (dolist (c gnus-url-button-commands)
4890       (define-key map (cadr c) (car c)))
4891     map))
4892
4893 (easy-menu-define
4894   gnus-url-button-menu gnus-url-button-map "URL button menu."
4895   `("Url Button"
4896     ,@(mapcar (lambda (c)
4897                 (vector (caddr c) (car c) :active t))
4898               gnus-url-button-commands)))
4899
4900 (defmacro gnus-bind-safe-url-regexp (&rest body)
4901   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4902   `(let ((mm-w3m-safe-url-regexp
4903           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4904                                 (gnus-buffer-live-p
4905                                  gnus-article-current-summary))
4906                            (with-current-buffer gnus-article-current-summary
4907                              gnus-newsgroup-name)
4908                          gnus-newsgroup-name)))
4909             (if (cond ((not group)
4910                        ;; Maybe we're in a mml-preview buffer
4911                        ;; and no group is selected.
4912                        t)
4913                       ((stringp gnus-safe-html-newsgroups)
4914                        (string-match gnus-safe-html-newsgroups group))
4915                       ((consp gnus-safe-html-newsgroups)
4916                        (member group gnus-safe-html-newsgroups)))
4917                 nil
4918               mm-w3m-safe-url-regexp))))
4919      ,@body))
4920
4921 (defun gnus-mime-button-menu (event prefix)
4922  "Construct a context-sensitive menu of MIME commands."
4923  (interactive "e\nP")
4924  (save-window-excursion
4925    (let ((pos (event-start event)))
4926      (select-window (posn-window pos))
4927      (goto-char (posn-point pos))
4928      (gnus-article-check-buffer)
4929      (popup-menu gnus-mime-button-menu nil prefix))))
4930
4931 (defun gnus-mime-view-all-parts (&optional handles)
4932   "View all the MIME parts."
4933   (interactive)
4934   (with-current-buffer gnus-article-buffer
4935     (let ((handles (or handles gnus-article-mime-handles))
4936           (mail-parse-charset gnus-newsgroup-charset)
4937           (mail-parse-ignored-charsets
4938            (with-current-buffer gnus-summary-buffer
4939              gnus-newsgroup-ignored-charsets)))
4940       (when handles
4941         (mm-remove-parts handles)
4942         (goto-char (point-min))
4943         (or (search-forward "\n\n") (goto-char (point-max)))
4944         (let ((inhibit-read-only t))
4945           (delete-region (point) (point-max))
4946           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4947
4948 (defun gnus-article-jump-to-part (n)
4949   "Jump to MIME part N."
4950   (interactive "P")
4951   (let ((parts (with-current-buffer gnus-article-buffer
4952                  (length gnus-article-mime-handle-alist))))
4953     (when (zerop parts)
4954       (error "No such part"))
4955     (pop-to-buffer gnus-article-buffer)
4956     (or n
4957         (setq n (if (= parts 1)
4958                     1
4959                   (read-number (format "Jump to part (1..%s): " parts)))))
4960     (unless (and (integerp n) (<= n parts) (>= n 1))
4961       (setq n
4962             (progn
4963               (gnus-message 7 "Invalid part `%s', using %s instead."
4964                             n parts)
4965               parts)))
4966     (gnus-message 9 "Jumping to part %s." n)
4967     (cond ((>= gnus-auto-select-part 1)
4968            (while (and (<= n parts)
4969                        (not (gnus-article-goto-part n)))
4970              (setq n (1+ n))))
4971           ((< gnus-auto-select-part 0)
4972            (while (and (>= n 1)
4973                        (not (gnus-article-goto-part n)))
4974              (setq n (1- n))))
4975           (t
4976            (gnus-article-goto-part n)))))
4977
4978 (defvar gnus-mime-buttonized-part-id nil
4979   "ID of a mime part that should be buttonized.
4980 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4981
4982 (eval-when-compile
4983   (defsubst gnus-article-edit-part (handles &optional current-id)
4984     "Edit an article in order to delete a mime part.
4985 This function is exclusively used by `gnus-mime-save-part-and-strip'
4986 and `gnus-mime-delete-part', and not provided at run-time normally."
4987     (gnus-article-edit-article
4988      `(lambda ()
4989         (buffer-disable-undo)
4990         (erase-buffer)
4991         (let ((mail-parse-charset (or gnus-article-charset
4992                                       ',gnus-newsgroup-charset))
4993               (mail-parse-ignored-charsets
4994                (or gnus-article-ignored-charsets
4995                    ',gnus-newsgroup-ignored-charsets))
4996               (mbl mml-buffer-list))
4997           (setq mml-buffer-list nil)
4998           (insert-buffer-substring gnus-original-article-buffer)
4999           (mime-to-mml ',handles)
5000           (setq gnus-article-mime-handles nil)
5001           (let ((mbl1 mml-buffer-list))
5002             (setq mml-buffer-list mbl)
5003             (set (make-local-variable 'mml-buffer-list) mbl1))
5004           (gnus-make-local-hook 'kill-buffer-hook)
5005           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
5006      `(lambda (no-highlight)
5007         (let ((mail-parse-charset (or gnus-article-charset
5008                                       ',gnus-newsgroup-charset))
5009               (message-options message-options)
5010               (message-options-set-recipient)
5011               (mail-parse-ignored-charsets
5012                (or gnus-article-ignored-charsets
5013                    ',gnus-newsgroup-ignored-charsets)))
5014           (mml-to-mime)
5015           (mml-destroy-buffers)
5016           (remove-hook 'kill-buffer-hook
5017                        'mml-destroy-buffers t)
5018           (kill-local-variable 'mml-buffer-list))
5019         (gnus-summary-edit-article-done
5020          ,(or (mail-header-references gnus-current-headers) "")
5021          ,(gnus-group-read-only-p)
5022          ,gnus-summary-buffer no-highlight))
5023      t)
5024     ;; Force buttonizing this part.
5025     (let ((gnus-mime-buttonized-part-id current-id))
5026       (gnus-article-edit-done))
5027     (gnus-configure-windows 'article)
5028     (when (and current-id (integerp gnus-auto-select-part))
5029       (gnus-article-jump-to-part
5030        (min (max (+ current-id gnus-auto-select-part) 1)
5031             (with-current-buffer gnus-article-buffer
5032               (length gnus-article-mime-handle-alist)))))))
5033
5034 (defun gnus-mime-replace-part (file)
5035   "Replace MIME part under point with an external body."
5036   ;; Useful if file has already been saved to disk
5037   (interactive
5038    (list
5039     (read-file-name "Replace MIME part with file: "
5040                     (or mm-default-directory default-directory)
5041                     nil nil)))
5042   (gnus-mime-save-part-and-strip file))
5043
5044 (defun gnus-mime-save-part-and-strip (&optional file)
5045   "Save the MIME part under point then replace it with an external body.
5046 If FILE is given, use it for the external part."
5047   (interactive)
5048   (gnus-article-check-buffer)
5049   (when (gnus-group-read-only-p)
5050     (error "The current group does not support deleting of parts"))
5051   (when (mm-complicated-handles gnus-article-mime-handles)
5052     (error "\
5053 The current article has a complicated MIME structure, giving up..."))
5054   (let* ((data (get-text-property (point) 'gnus-data))
5055          (id (get-text-property (point) 'gnus-part))
5056          param
5057          (handles gnus-article-mime-handles))
5058     (unless file
5059       (setq file
5060             (and data (mm-save-part data "Delete MIME part and save to: "))))
5061     (when file
5062       (with-current-buffer (mm-handle-buffer data)
5063         (erase-buffer)
5064         (insert "Content-Type: " (mm-handle-media-type data))
5065         (mml-insert-parameter-string (cdr (mm-handle-type data))
5066                                      '(charset))
5067         ;; Add a filename for the sake of saving the part again.
5068         (mml-insert-parameter
5069          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5070         (insert "\n")
5071         (insert "Content-ID: " (message-make-message-id) "\n")
5072         (insert "Content-Transfer-Encoding: binary\n")
5073         (insert "\n"))
5074       (setcdr data
5075               (cdr (mm-make-handle nil
5076                                    `("message/external-body"
5077                                      (access-type . "LOCAL-FILE")
5078                                      (name . ,file)))))
5079       ;; (set-buffer gnus-summary-buffer)
5080       (gnus-article-edit-part handles id))))
5081
5082 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5083 ;; parts...>') but with stripping would be nice.
5084
5085 (defun gnus-mime-delete-part ()
5086   "Delete the MIME part under point.
5087 Replace it with some information about the removed part."
5088   (interactive)
5089   (gnus-article-check-buffer)
5090   (when (gnus-group-read-only-p)
5091     (error "The current group does not support deleting of parts"))
5092   (when (mm-complicated-handles gnus-article-mime-handles)
5093     (error "\
5094 The current article has a complicated MIME structure, giving up..."))
5095   (when (or gnus-expert-user
5096             (gnus-yes-or-no-p "\
5097 Deleting parts may malfunction or destroy the article; continue? "))
5098     (let* ((data (get-text-property (point) 'gnus-data))
5099            (id (get-text-property (point) 'gnus-part))
5100            (handles gnus-article-mime-handles)
5101            (description
5102             (let ((desc (mm-handle-description data)))
5103               (when desc
5104                 (mail-decode-encoded-word-string desc))))
5105            (filename (or (mm-handle-filename data) "(none)"))
5106            (type (mm-handle-media-type data)))
5107       (unless data
5108         (error "No MIME part under point"))
5109       (with-current-buffer (mm-handle-buffer data)
5110         (let ((bsize (buffer-size)))
5111           (erase-buffer)
5112           (insert
5113            (concat
5114             ",----\n"
5115             "| The following attachment has been deleted:\n"
5116             "|\n"
5117             "| Type:           " type "\n"
5118             "| Filename:       " filename "\n"
5119             "| Size (encoded): " (format "%s byte%s\n"
5120                                          bsize (if (= bsize 1)
5121                                                    ""
5122                                                  "s"))
5123             (when description
5124               (concat    "| Description:    " description "\n"))
5125             "`----\n"))
5126           (setcdr data
5127                   (cdr (mm-make-handle
5128                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5129                         (list "attachment")
5130                         (format "Deleted attachment (%s bytes)" bsize))))))
5131       ;; (set-buffer gnus-summary-buffer)
5132       (gnus-article-edit-part handles id))))
5133
5134 (defun gnus-mime-save-part ()
5135   "Save the MIME part under point."
5136   (interactive)
5137   (gnus-article-check-buffer)
5138   (let ((data (get-text-property (point) 'gnus-data)))
5139     (when data
5140       (mm-save-part data))))
5141
5142 (defun gnus-mime-pipe-part (&optional cmd)
5143   "Pipe the MIME part under point to a process.
5144 Use CMD as the process."
5145   (interactive)
5146   (gnus-article-check-buffer)
5147   (let ((data (get-text-property (point) 'gnus-data)))
5148     (when data
5149       (mm-pipe-part data cmd))))
5150
5151 (defun gnus-mime-view-part ()
5152   "Interactively choose a viewing method for the MIME part under point."
5153   (interactive)
5154   (gnus-article-check-buffer)
5155   (let ((data (get-text-property (point) 'gnus-data)))
5156     (when data
5157       (setq gnus-article-mime-handles
5158             (mm-merge-handles
5159              gnus-article-mime-handles (setq data (copy-sequence data))))
5160       (mm-interactively-view-part data))))
5161
5162 (defun gnus-mime-view-part-as-type-internal ()
5163   (gnus-article-check-buffer)
5164   (let* ((handle (get-text-property (point) 'gnus-data))
5165          (name (or
5166                 ;; Content-Type: foo/bar; name=...
5167                 (mail-content-type-get (mm-handle-type handle) 'name)
5168                 ;; Content-Disposition: attachment; filename=...
5169                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5170          (def-type (and name (mm-default-file-encoding name))))
5171     (or (and def-type (cons def-type 0))
5172         (and handle
5173              (equal (mm-handle-media-supertype handle) "text")
5174              '("text/plain" . 0))
5175         '("application/octet-stream" . 0))))
5176
5177 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5178   "Choose a MIME media type, and view the part as such.
5179 If non-nil, PRED is a predicate to use during completion to limit the
5180 available media-types."
5181   (interactive)
5182   (unless mime-type
5183     (setq mime-type
5184           (let ((default (gnus-mime-view-part-as-type-internal)))
5185             (gnus-completing-read
5186              "View as MIME type"
5187              (if pred
5188                  (gnus-remove-if-not pred (mailcap-mime-types))
5189                (mailcap-mime-types))
5190              nil nil nil
5191              (car default)))))
5192   (gnus-article-check-buffer)
5193   (let ((handle (get-text-property (point) 'gnus-data)))
5194     (when handle
5195       (when (equal (mm-handle-media-type handle) "message/external-body")
5196         (unless (mm-handle-cache handle)
5197           (mm-extern-cache-contents handle))
5198         (setq handle (mm-handle-cache handle)))
5199       (setq handle
5200             (mm-make-handle (mm-handle-buffer handle)
5201                             (cons mime-type (cdr (mm-handle-type handle)))
5202                             (mm-handle-encoding handle)
5203                             (mm-handle-undisplayer handle)
5204                             (mm-handle-disposition handle)
5205                             (mm-handle-description handle)
5206                             nil
5207                             (mm-handle-id handle)))
5208       (setq gnus-article-mime-handles
5209             (mm-merge-handles gnus-article-mime-handles handle))
5210       (when (mm-handle-displayed-p handle)
5211         (mm-remove-part handle))
5212       (gnus-mm-display-part handle))))
5213
5214 (defun gnus-mime-copy-part (&optional handle arg)
5215   "Put the MIME part under point into a new buffer.
5216 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5217 are decompressed."
5218   (interactive (list nil current-prefix-arg))
5219   (gnus-article-check-buffer)
5220   (unless handle
5221     (setq handle (get-text-property (point) 'gnus-data)))
5222   (when handle
5223     (let ((filename (mm-handle-filename handle))
5224           contents dont-decode charset coding-system)
5225       (mm-with-unibyte-buffer
5226         (mm-insert-part handle)
5227         (setq contents (or (condition-case nil
5228                                (mm-decompress-buffer filename nil 'sig)
5229                              (error
5230                               (setq dont-decode t)
5231                               nil))
5232                            (buffer-string))))
5233       (setq filename (cond (filename (file-name-nondirectory filename))
5234                            (dont-decode "*raw data*")
5235                            (t "*decoded*")))
5236       (cond
5237        (dont-decode)
5238        ((not arg)
5239         (unless (setq charset (mail-content-type-get
5240                                (mm-handle-type handle) 'charset))
5241           (unless (setq coding-system (mm-with-unibyte-buffer
5242                                         (insert contents)
5243                                         (mm-find-buffer-file-coding-system)))
5244             (setq charset gnus-newsgroup-charset))))
5245        ((numberp arg)
5246         (setq charset (or (cdr (assq arg
5247                                      gnus-summary-show-article-charset-alist))
5248                           (mm-read-coding-system "Charset: ")))))
5249       (switch-to-buffer (generate-new-buffer filename))
5250       (if (or coding-system
5251               (and charset
5252                    (setq coding-system (mm-charset-to-coding-system charset))
5253                    (not (eq coding-system 'ascii))))
5254           (progn
5255             (mm-enable-multibyte)
5256             (insert (mm-decode-coding-string contents coding-system))
5257             (setq buffer-file-coding-system
5258                   (if (boundp 'last-coding-system-used)
5259                       (symbol-value 'last-coding-system-used)
5260                     coding-system)))
5261         (mm-disable-multibyte)
5262         (insert contents)
5263         (setq buffer-file-coding-system mm-binary-coding-system))
5264       ;; We do it this way to make `normal-mode' set the appropriate mode.
5265       (unwind-protect
5266           (progn
5267             (setq buffer-file-name (expand-file-name filename))
5268             (normal-mode))
5269         (setq buffer-file-name nil))
5270       (goto-char (point-min)))))
5271
5272 (defun gnus-mime-print-part (&optional handle filename)
5273   "Print the MIME part under point."
5274   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5275   (gnus-article-check-buffer)
5276   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5277          (contents (and handle (mm-get-part handle)))
5278          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5279          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5280     (when contents
5281         (if printer
5282             (unwind-protect
5283                 (progn
5284                   (mm-save-part-to-file handle file)
5285                   (call-process shell-file-name nil
5286                                 (generate-new-buffer " *mm*")
5287                                 nil
5288                                 shell-command-switch
5289                                 (mm-mailcap-command
5290                                  printer file (mm-handle-type handle))))
5291               (delete-file file))
5292           (with-temp-buffer
5293             (insert contents)
5294             (gnus-print-buffer))
5295           (ps-despool filename)))))
5296
5297 (defun gnus-mime-inline-part (&optional handle arg)
5298   "Insert the MIME part under point into the current buffer.
5299 Compressed files like .gz and .bz2 are decompressed."
5300   (interactive (list nil current-prefix-arg))
5301   (gnus-article-check-buffer)
5302   (unless handle
5303     (setq handle (get-text-property (point) 'gnus-data)))
5304   (when handle
5305     (let ((b (point))
5306           (inhibit-read-only t)
5307           contents charset coding-system)
5308       (if (and (not arg) (mm-handle-undisplayer handle))
5309           (mm-remove-part handle)
5310         (mm-with-unibyte-buffer
5311           (mm-insert-part handle)
5312           (setq contents
5313                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5314                     (buffer-string))))
5315         (cond
5316          ((not arg)
5317           (unless (setq charset (mail-content-type-get
5318                                  (mm-handle-type handle) 'charset))
5319             (unless (setq coding-system
5320                           (mm-with-unibyte-buffer
5321                             (insert contents)
5322                             (mm-find-buffer-file-coding-system)))
5323               (setq charset gnus-newsgroup-charset))))
5324          ((numberp arg)
5325           (if (mm-handle-undisplayer handle)
5326               (mm-remove-part handle))
5327           (setq charset
5328                 (or (cdr (assq arg
5329                                gnus-summary-show-article-charset-alist))
5330                     (mm-read-coding-system "Charset: "))))
5331          ((mm-handle-undisplayer handle)
5332           (mm-remove-part handle)))
5333         (forward-line 2)
5334         (mm-display-inline handle)
5335         (goto-char b)))))
5336
5337 (defun gnus-mime-set-charset-parameters (handle charset)
5338   "Set CHARSET to parameters in HANDLE.
5339 CHARSET may either be a string or a symbol."
5340   (unless (stringp charset)
5341     (setq charset (symbol-name charset)))
5342   (if (stringp (car handle))
5343       (dolist (h (cdr handle))
5344         (gnus-mime-set-charset-parameters h charset))
5345     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5346                                             "message/external-body")
5347                                      (progn
5348                                        (unless (mm-handle-cache handle)
5349                                          (mm-extern-cache-contents handle))
5350                                        (mm-handle-cache handle))
5351                                    handle)))
5352            (param (assq 'charset (cdr type))))
5353       (if param
5354           (setcdr param charset)
5355         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5356
5357 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5358   "Insert the MIME part under point into the current buffer using the
5359 specified charset."
5360   (interactive (list nil current-prefix-arg))
5361   (gnus-article-check-buffer)
5362   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5363         (fun (get-text-property (point) 'gnus-callback))
5364         (gnus-newsgroup-ignored-charsets 'gnus-all)
5365         charset form preferred parts)
5366     (when handle
5367       (when (prog1
5368                 (and fun
5369                      (setq charset
5370                            (or (cdr (assq
5371                                      arg
5372                                      gnus-summary-show-article-charset-alist))
5373                                (mm-read-coding-system "Charset: "))))
5374               (if (mm-handle-undisplayer handle)
5375                   (mm-remove-part handle)))
5376         (gnus-mime-set-charset-parameters handle charset)
5377         (when (and (consp (setq form (cdr-safe fun)))
5378                    (setq form (ignore-errors
5379                                 (assq 'gnus-mime-display-alternative form)))
5380                    (setq preferred (caddr form))
5381                    (progn
5382                      (when (eq (car preferred) 'quote)
5383                        (setq preferred (cadr preferred)))
5384                      (not (equal preferred
5385                                  (get-text-property (point) 'gnus-data))))
5386                    (setq parts (get-text-property (point) 'gnus-part))
5387                    (setq parts (cdr (assq parts
5388                                           gnus-article-mime-handle-alist)))
5389                    (equal (mm-handle-media-type parts) "multipart/alternative")
5390                    (setq parts (reverse (cdr parts))))
5391           (setcar (cddr form)
5392                   (list 'quote (or (cadr (member preferred parts))
5393                                    (car parts)))))
5394         (funcall fun handle)))))
5395
5396 (defun gnus-mime-view-part-externally (&optional handle)
5397   "View the MIME part under point with an external viewer."
5398   (interactive)
5399   (gnus-article-check-buffer)
5400   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5401          (mm-user-display-methods nil)
5402          (mm-inlined-types nil)
5403          (mail-parse-charset gnus-newsgroup-charset)
5404          (mail-parse-ignored-charsets
5405           (with-current-buffer gnus-summary-buffer
5406             gnus-newsgroup-ignored-charsets))
5407          (type (mm-handle-media-type handle))
5408          (method (mailcap-mime-info type))
5409          (mm-enable-external t))
5410     (if (not (stringp method))
5411         (gnus-mime-view-part-as-type
5412          nil (lambda (type) (stringp (mailcap-mime-info type))))
5413       (when handle
5414         (mm-display-part handle nil t)))))
5415
5416 (defun gnus-mime-view-part-internally (&optional handle)
5417   "View the MIME part under point with an internal viewer.
5418 If no internal viewer is available, use an external viewer."
5419   (interactive)
5420   (gnus-article-check-buffer)
5421   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5422          (mm-inlined-types '(".*"))
5423          (mm-inline-large-images t)
5424          (mail-parse-charset gnus-newsgroup-charset)
5425          (mail-parse-ignored-charsets
5426           (with-current-buffer gnus-summary-buffer
5427             gnus-newsgroup-ignored-charsets))
5428          (inhibit-read-only t))
5429     (if (not (mm-inlinable-p handle))
5430         (gnus-mime-view-part-as-type
5431          nil (lambda (type) (mm-inlinable-p handle type)))
5432       (when handle
5433         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5434
5435 (defun gnus-mime-action-on-part (&optional action)
5436   "Do something with the MIME attachment at \(point\)."
5437   (interactive
5438    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5439   (gnus-article-check-buffer)
5440   (let ((action-pair (assoc action gnus-mime-action-alist)))
5441     (if action-pair
5442         (funcall (cdr action-pair)))))
5443
5444 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5445   "Call FUNCTION on MIME part N.
5446 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5447 If INTERACTIVE, call FUNCTION interactively."
5448   (let (window frame)
5449     ;; Check whether the article is displayed.
5450     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5451                  (setq window (get-buffer-window gnus-article-buffer t))
5452                  (frame-visible-p (setq frame (window-frame window))))
5453       (error "No article is displayed"))
5454     (with-current-buffer gnus-article-buffer
5455       ;; Check whether the article displays the right contents.
5456       (unless (with-current-buffer gnus-summary-buffer
5457                 (eq gnus-current-article (gnus-summary-article-number)))
5458         (error "You should select the right article first"))
5459       (if n
5460           (setq n (prefix-numeric-value n))
5461         (let ((pt (point)))
5462           (setq n (or (get-text-property pt 'gnus-part)
5463                       (and (not (bobp))
5464                            (get-text-property (1- pt) 'gnus-part))
5465                       (get-text-property (prog2
5466                                              (forward-line 1)
5467                                              (point)
5468                                            (goto-char pt))
5469                                          'gnus-part)
5470                       (get-text-property
5471                        (or (and (setq pt (previous-single-property-change
5472                                           pt 'gnus-part))
5473                                 (1- pt))
5474                            (next-single-property-change (point) 'gnus-part)
5475                            (point))
5476                        'gnus-part)
5477                       1))))
5478       ;; Check whether the specified part exists.
5479       (when (> n (length gnus-article-mime-handle-alist))
5480         (error "No such part")))
5481     (unless
5482         (progn
5483           ;; To select the window is needed so that the cursor
5484           ;; might be visible on the MIME button.
5485           (select-window (prog1
5486                              window
5487                            (setq window (selected-window))
5488                            ;; Article may be displayed in the other frame.
5489                            (gnus-select-frame-set-input-focus
5490                             (prog1
5491                                 frame
5492                               (setq frame (selected-frame))))))
5493           (when (gnus-article-goto-part n)
5494             ;; We point the cursor and the arrow at the MIME button
5495             ;; when the `function' prompt the user for something.
5496             (unless (and (pos-visible-in-window-p)
5497                          (> (count-lines (point) (window-end))
5498                             (/ (1- (window-height)) 3)))
5499               (recenter (/ (1- (window-height)) 3)))
5500             (let ((cursor-in-non-selected-windows t)
5501                   (overlay-arrow-string "=>")
5502                   (overlay-arrow-position (point-marker)))
5503               (unwind-protect
5504                   (cond
5505                    ((and no-handle interactive)
5506                     (call-interactively function))
5507                    (no-handle
5508                     (funcall function))
5509                    (interactive
5510                     (call-interactively
5511                      function (get-text-property (point) 'gnus-data)))
5512                    (t
5513                     (funcall function
5514                              (get-text-property (point) 'gnus-data))))
5515                 (set-marker overlay-arrow-position nil)
5516                 (unless gnus-auto-select-part
5517                   (gnus-select-frame-set-input-focus frame)
5518                   (select-window window))))
5519             t))
5520       (if gnus-inhibit-mime-unbuttonizing
5521           ;; This is the default though the program shouldn't reach here.
5522           (error "No such part")
5523         ;; The part which doesn't have the MIME button is selected.
5524         ;; So, we display all the buttons and redo it.
5525         (let ((gnus-inhibit-mime-unbuttonizing t))
5526           (gnus-summary-show-article)
5527           (gnus-article-part-wrapper n function no-handle))))))
5528
5529 (defun gnus-article-pipe-part (n)
5530   "Pipe MIME part N, which is the numerical prefix."
5531   (interactive "P")
5532   (gnus-article-part-wrapper n 'mm-pipe-part))
5533
5534 (defun gnus-article-save-part (n)
5535   "Save MIME part N, which is the numerical prefix."
5536   (interactive "P")
5537   (gnus-article-part-wrapper n 'mm-save-part))
5538
5539 (defun gnus-article-interactively-view-part (n)
5540   "View MIME part N interactively, which is the numerical prefix."
5541   (interactive "P")
5542   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5543
5544 (defun gnus-article-copy-part (n)
5545   "Copy MIME part N, which is the numerical prefix."
5546   (interactive "P")
5547   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5548
5549 (defun gnus-article-view-part-as-charset (n)
5550   "View MIME part N using a specified charset.
5551 N is the numerical prefix."
5552   (interactive "P")
5553   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5554
5555 (defun gnus-article-view-part-externally (n)
5556   "View MIME part N externally, which is the numerical prefix."
5557   (interactive "P")
5558   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5559
5560 (defun gnus-article-inline-part (n)
5561   "Inline MIME part N, which is the numerical prefix."
5562   (interactive "P")
5563   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5564
5565 (defun gnus-article-save-part-and-strip (n)
5566   "Save MIME part N and replace it with an external body.
5567 N is the numerical prefix."
5568   (interactive "P")
5569   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5570
5571 (defun gnus-article-replace-part (n)
5572   "Replace MIME part N with an external body.
5573 N is the numerical prefix."
5574   (interactive "P")
5575   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5576
5577 (defun gnus-article-delete-part (n)
5578   "Delete MIME part N and add some information about the removed part.
5579 N is the numerical prefix."
5580   (interactive "P")
5581   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5582
5583 (defun gnus-article-view-part-as-type (n)
5584   "Choose a MIME media type, and view part N as such.
5585 N is the numerical prefix."
5586   (interactive "P")
5587   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5588
5589 (defun gnus-article-mime-match-handle-first (condition)
5590   (if condition
5591       (let (n)
5592         (dolist (ihandle gnus-article-mime-handle-alist)
5593           (if (and (cond
5594                     ((functionp condition)
5595                      (funcall condition (cdr ihandle)))
5596                     ((eq condition 'undisplayed)
5597                      (not (or (mm-handle-undisplayer (cdr ihandle))
5598                               (equal (mm-handle-media-type (cdr ihandle))
5599                                      "multipart/alternative"))))
5600                     ((eq condition 'undisplayed-alternative)
5601                      (not (mm-handle-undisplayer (cdr ihandle))))
5602                     (t t))
5603                    (gnus-article-goto-part (car ihandle))
5604                    (or (not n) (< (car ihandle) n)))
5605               (setq n (car ihandle))))
5606         (or n 1))
5607     1))
5608
5609 (defun gnus-article-view-part (&optional n)
5610   "View MIME part N, which is the numerical prefix.
5611 If the part is already shown, hide the part.  If N is nil, view
5612 all parts."
5613   (interactive "P")
5614   (with-current-buffer gnus-article-buffer
5615     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5616                              gnus-article-mime-match-handle-function)))
5617     (when (> n (length gnus-article-mime-handle-alist))
5618       (error "No such part"))
5619     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5620       (when (gnus-article-goto-part n)
5621         (if (equal (car handle) "multipart/alternative")
5622             (gnus-article-press-button)
5623           (when (eq (gnus-mm-display-part handle) 'internal)
5624             (gnus-set-window-start)))))))
5625
5626 (defsubst gnus-article-mime-total-parts ()
5627   (if (bufferp (car gnus-article-mime-handles))
5628       1 ;; single part
5629     (1- (length gnus-article-mime-handles))))
5630
5631 (defun gnus-mm-display-part (handle)
5632   "Display HANDLE and fix MIME button."
5633   (let ((id (get-text-property (point) 'gnus-part))
5634         (point (point))
5635         (inhibit-read-only t))
5636     (forward-line 1)
5637     (prog1
5638         (let ((window (selected-window))
5639               (mail-parse-charset gnus-newsgroup-charset)
5640               (mail-parse-ignored-charsets
5641                (if (gnus-buffer-live-p gnus-summary-buffer)
5642                    (with-current-buffer gnus-summary-buffer
5643                      gnus-newsgroup-ignored-charsets)
5644                  nil)))
5645           (save-excursion
5646             (unwind-protect
5647                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5648                       (beg (point)))
5649                   (when win
5650                     (select-window win))
5651                   (goto-char point)
5652                   (forward-line)
5653                   (if (mm-handle-displayed-p handle)
5654                       ;; This will remove the part.
5655                       (mm-display-part handle)
5656                     (save-restriction
5657                       (narrow-to-region (point)
5658                                         (if (eobp) (point) (1+ (point))))
5659                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5660                       ;; We narrow to the part itself and
5661                       ;; then call the treatment functions.
5662                       (goto-char (point-min))
5663                       (forward-line 1)
5664                       (narrow-to-region (point) (point-max))
5665                       (gnus-treat-article
5666                        nil id
5667                        (gnus-article-mime-total-parts)
5668                        (mm-handle-media-type handle)))))
5669               (if (window-live-p window)
5670                   (select-window window)))))
5671       (goto-char point)
5672       (gnus-delete-line)
5673       (gnus-insert-mime-button
5674        handle id (list (mm-handle-displayed-p handle)))
5675       (goto-char point))))
5676
5677 (defun gnus-article-goto-part (n)
5678   "Go to MIME part N."
5679   (when gnus-break-pages
5680     (widen))
5681   (prog1
5682       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5683             part handle end next handles)
5684         (when start
5685           (goto-char start)
5686           (if (setq handle (get-text-property start 'gnus-data))
5687               start
5688             ;; Go to the displayed subpart, assuming this is
5689             ;; multipart/alternative.
5690             (setq part start
5691                   end (point-at-eol))
5692             (while (and (not handle)
5693                         part
5694                         (< part end)
5695                         (setq next (text-property-not-all part end
5696                                                           'gnus-data nil)))
5697               (setq part next
5698                     handle (get-text-property part 'gnus-data))
5699               (push (cons handle part) handles)
5700               (unless (mm-handle-displayed-p handle)
5701                 (setq handle nil
5702                       part (text-property-any part end 'gnus-data nil))))
5703             (unless handle
5704               ;; No subpart is displayed, so we find preferred one.
5705               (setq part
5706                     (cdr (assq (mm-preferred-alternative
5707                                 (nreverse (mapcar 'car handles)))
5708                                handles))))
5709             (if part
5710                 (goto-char (1+ part))
5711               start))))
5712     (when gnus-break-pages
5713       (gnus-narrow-to-page))))
5714
5715 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5716   (let ((gnus-tmp-name
5717          (or (mm-handle-filename handle)
5718              (mail-content-type-get (mm-handle-type handle) 'url)
5719              ""))
5720         (gnus-tmp-type (mm-handle-media-type handle))
5721         (gnus-tmp-description (or (mm-handle-description handle) ""))
5722         (gnus-tmp-dots
5723          (if (if displayed (car displayed)
5724                (mm-handle-displayed-p handle))
5725              "" "..."))
5726         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5727                            (buffer-size)))
5728         gnus-tmp-type-long b e)
5729     (when (string-match ".*/" gnus-tmp-name)
5730       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5731     (setq gnus-tmp-type-long (concat gnus-tmp-type
5732                                      (and (not (equal gnus-tmp-name ""))
5733                                           (concat "; " gnus-tmp-name))))
5734     (unless (equal gnus-tmp-description "")
5735       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5736     (unless (bolp)
5737       (insert "\n"))
5738     (setq b (point))
5739     (gnus-eval-format
5740      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5741      `(keymap ,gnus-mime-button-map
5742               gnus-callback gnus-mm-display-part
5743               gnus-part ,gnus-tmp-id
5744               article-type annotation
5745               gnus-data ,handle
5746               rear-nonsticky t))
5747     (setq e (if (bolp)
5748                 ;; Exclude a newline.
5749                 (1- (point))
5750               (point)))
5751     (when gnus-article-button-face
5752       (gnus-overlay-put (gnus-make-overlay b e nil t)
5753                         'face gnus-article-button-face))
5754     (widget-convert-button
5755      'link b e
5756      :mime-handle handle
5757      :action 'gnus-widget-press-button
5758      :button-keymap gnus-mime-button-map
5759      :help-echo
5760      (lambda (widget)
5761        ;; Needed to properly clear the message due to a bug in
5762        ;; wid-edit (XEmacs only).
5763        (if (boundp 'help-echo-owns-message)
5764            (setq help-echo-owns-message t))
5765        (format
5766         "%S: %s the MIME part; %S: more options"
5767         (aref gnus-mouse-2 0)
5768         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5769             "hide" "show")
5770         (aref gnus-down-mouse-3 0))))))
5771
5772 (defun gnus-widget-press-button (elems el)
5773   (goto-char (widget-get elems :from))
5774   (gnus-article-press-button))
5775
5776 (defvar gnus-displaying-mime nil)
5777
5778 (defun gnus-display-mime (&optional ihandles)
5779   "Display the MIME parts."
5780   (save-excursion
5781     (save-selected-window
5782       (let ((window (get-buffer-window gnus-article-buffer))
5783             (point (point)))
5784         (when window
5785           (select-window window)
5786           ;; We have to do this since selecting the window
5787           ;; may change the point.  So we set the window point.
5788           (set-window-point window point)))
5789       (let ((handles ihandles)
5790             (inhibit-read-only t)
5791             handle)
5792         (cond (handles)
5793               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5794                (when gnus-article-emulate-mime
5795                  (mm-uu-dissect-text-parts handles)))
5796               (gnus-article-emulate-mime
5797                (setq handles (mm-uu-dissect))))
5798         (when (and (not ihandles)
5799                    (not gnus-displaying-mime))
5800           ;; Top-level call; we clean up.
5801           (when gnus-article-mime-handles
5802             (mm-destroy-parts gnus-article-mime-handles)
5803             (setq gnus-article-mime-handle-alist nil));; A trick.
5804           (setq gnus-article-mime-handles handles)
5805           ;; We allow users to glean info from the handles.
5806           (when gnus-article-mime-part-function
5807             (gnus-mime-part-function handles)))
5808         (if (and handles
5809                  (or (not (stringp (car handles)))
5810                      (cdr handles)))
5811             (progn
5812               (when (and (not ihandles)
5813                          (not gnus-displaying-mime))
5814                 ;; Clean up for mime parts.
5815                 (article-goto-body)
5816                 (delete-region (point) (point-max)))
5817               (let ((gnus-displaying-mime t))
5818                 (gnus-mime-display-part handles)))
5819           (save-restriction
5820             (article-goto-body)
5821             (narrow-to-region (point) (point-max))
5822             (gnus-treat-article nil 1 1 "text/plain")
5823             (widen)))
5824         (unless ihandles
5825           ;; Highlight the headers.
5826           (save-excursion
5827             (save-restriction
5828               (article-goto-body)
5829               (narrow-to-region (point-min) (point))
5830               (gnus-article-save-original-date
5831                (gnus-treat-article 'head)))))))
5832     ;; Cope with broken MIME messages.
5833     (goto-char (point-max))
5834     (unless (bolp)
5835       (insert "\n"))))
5836
5837 (defcustom gnus-mime-display-multipart-as-mixed nil
5838   "Display \"multipart\" parts as  \"multipart/mixed\".
5839
5840 If t, it overrides nil values of
5841 `gnus-mime-display-multipart-alternative-as-mixed' and
5842 `gnus-mime-display-multipart-related-as-mixed'."
5843   :group 'gnus-article-mime
5844   :type 'boolean)
5845
5846 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5847   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5848   :version "22.1"
5849   :group 'gnus-article-mime
5850   :type 'boolean)
5851
5852 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5853   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5854
5855 If displaying \"text/html\" is discouraged \(see
5856 `mm-discouraged-alternatives'\) images or other material inside a
5857 \"multipart/related\" part might be overlooked when this variable is nil."
5858   :version "22.1"
5859   :group 'gnus-article-mime
5860   :type 'boolean)
5861
5862 (defun gnus-mime-display-part (handle)
5863   (cond
5864    ;; Maybe a broken MIME message.
5865    ((null handle))
5866    ;; Single part.
5867    ((not (stringp (car handle)))
5868     (gnus-mime-display-single handle))
5869    ;; User-defined multipart
5870    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5871     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5872              handle))
5873    ;; multipart/alternative
5874    ((and (equal (car handle) "multipart/alternative")
5875          (not (or gnus-mime-display-multipart-as-mixed
5876                   gnus-mime-display-multipart-alternative-as-mixed)))
5877     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5878       (push (cons id handle) gnus-article-mime-handle-alist)
5879       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5880    ;; multipart/related
5881    ((and (equal (car handle) "multipart/related")
5882          (not (or gnus-mime-display-multipart-as-mixed
5883                   gnus-mime-display-multipart-related-as-mixed)))
5884     ;;;!!!We should find the start part, but we just default
5885     ;;;!!!to the first part.
5886     ;;(gnus-mime-display-part (cadr handle))
5887     ;;;!!! Most multipart/related is an HTML message plus images.
5888     ;;;!!! Unfortunately we are unable to let W3 display those
5889     ;;;!!! included images, so we just display it as a mixed multipart.
5890     ;;(gnus-mime-display-mixed (cdr handle))
5891     ;;;!!! No, w3 can display everything just fine.
5892     (gnus-mime-display-part (cadr handle)))
5893    ((equal (car handle) "multipart/signed")
5894     (gnus-add-wash-type 'signed)
5895     (gnus-mime-display-security handle))
5896    ((equal (car handle) "multipart/encrypted")
5897     (gnus-add-wash-type 'encrypted)
5898     (gnus-mime-display-security handle))
5899    ;; Other multiparts are handled like multipart/mixed.
5900    (t
5901     (gnus-mime-display-mixed (cdr handle)))))
5902
5903 (defun gnus-mime-part-function (handles)
5904   (if (stringp (car handles))
5905       (mapcar 'gnus-mime-part-function (cdr handles))
5906     (funcall gnus-article-mime-part-function handles)))
5907
5908 (defun gnus-mime-display-mixed (handles)
5909   (mapcar 'gnus-mime-display-part handles))
5910
5911 (defun gnus-mime-display-single (handle)
5912   (let ((type (mm-handle-media-type handle))
5913         (ignored gnus-ignored-mime-types)
5914         (not-attachment t)
5915         (move nil)
5916         display text)
5917     (catch 'ignored
5918       (progn
5919         (while ignored
5920           (when (string-match (pop ignored) type)
5921             (throw 'ignored nil)))
5922         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5923                                (with-current-buffer gnus-summary-buffer
5924                                  gnus-inhibit-images)
5925                              gnus-inhibit-images)
5926                            (string-match "\\`image/" type)))
5927                  (setq not-attachment
5928                        (and (not (mm-inline-override-p handle))
5929                             (or (not (mm-handle-disposition handle))
5930                                 (equal (car (mm-handle-disposition handle))
5931                                        "inline")
5932                                 (mm-attachment-override-p handle))))
5933                  (mm-automatic-display-p handle)
5934                  (or (and
5935                       (mm-inlinable-p handle)
5936                       (mm-inlined-p handle))
5937                      (mm-automatic-external-display-p type)))
5938             (setq display t)
5939           (when (equal (mm-handle-media-supertype handle) "text")
5940             (setq text t)))
5941         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5942               beg)
5943           (push (cons id handle) gnus-article-mime-handle-alist)
5944           (when (and display
5945                      (equal (mm-handle-media-supertype handle) "message"))
5946             (insert-char
5947              ?\n
5948              (cond ((not (bolp)) 2)
5949                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5950                    (t 1))))
5951           (when (or (not display)
5952                     (not (gnus-unbuttonized-mime-type-p type))
5953                     (eq id gnus-mime-buttonized-part-id))
5954             (gnus-insert-mime-button
5955              handle id (list (or display (and not-attachment text))))
5956             (gnus-article-insert-newline)
5957             ;; Remember modify the number of forward lines.
5958             (setq move t))
5959           (setq beg (point))
5960           (cond
5961            (display
5962             (when move
5963               (forward-line -1)
5964               (setq beg (point)))
5965             (let ((mail-parse-charset gnus-newsgroup-charset)
5966                   (mail-parse-ignored-charsets
5967                    (save-excursion (condition-case ()
5968                                        (set-buffer gnus-summary-buffer)
5969                                      (error))
5970                                    gnus-newsgroup-ignored-charsets)))
5971               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5972             (goto-char (point-max)))
5973            ((and text not-attachment)
5974             (when move
5975               (forward-line -1)
5976               (setq beg (point)))
5977             (gnus-article-insert-newline)
5978             (mm-display-inline handle)
5979             (goto-char (point-max))))
5980           ;; Do highlighting.
5981           (save-excursion
5982             (save-restriction
5983               (narrow-to-region beg (point))
5984               (if (eq handle gnus-article-mime-handles)
5985                   ;; The format=flowed case.
5986                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5987                 ;; Don't count signature parts that are never displayed.
5988                 ;; The part number should be re-calculated supposing this
5989                 ;; might be a message/rfc822 part.
5990                 (let (handles)
5991                   (dolist (part gnus-article-mime-handles)
5992                     (unless (or (stringp part)
5993                                 (equal (car (mm-handle-type part))
5994                                        "application/pgp-signature"))
5995                       (push part handles)))
5996                   (gnus-treat-article
5997                    nil (length (memq handle handles)) (length handles)
5998                    (mm-handle-media-type handle)))))))))))
5999
6000 (defun gnus-unbuttonized-mime-type-p (type)
6001   "Say whether TYPE is to be unbuttonized."
6002   (unless gnus-inhibit-mime-unbuttonizing
6003     (when (catch 'found
6004             (let ((types gnus-unbuttonized-mime-types))
6005               (while types
6006                 (when (string-match (pop types) type)
6007                   (throw 'found t)))))
6008       (not (catch 'found
6009              (let ((types gnus-buttonized-mime-types))
6010                (while types
6011                  (when (string-match (pop types) type)
6012                    (throw 'found t)))))))))
6013
6014 (defun gnus-article-insert-newline ()
6015   "Insert a newline, but mark it as undeletable."
6016   (gnus-put-text-property
6017    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
6018
6019 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
6020   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
6021          (ihandles handles)
6022          (point (point))
6023          handle (inhibit-read-only t) from props begend not-pref)
6024     (save-window-excursion
6025       (save-restriction
6026         (when ibegend
6027           (narrow-to-region (car ibegend)
6028                             (or (cdr ibegend)
6029                                 (progn
6030                                   (goto-char (car ibegend))
6031                                   (forward-line 2)
6032                                   (point))))
6033           (delete-region (point-min) (point-max))
6034           (mm-remove-parts handles))
6035         (setq begend (list (point-marker)))
6036         ;; Do the toggle.
6037         (unless (setq not-pref (cadr (member preferred ihandles)))
6038           (setq not-pref (car ihandles)))
6039         (when (or ibegend
6040                   (not preferred)
6041                   (not (gnus-unbuttonized-mime-type-p
6042                         "multipart/alternative")))
6043           (gnus-add-text-properties
6044            (setq from (point))
6045            (progn
6046              (insert (format "%d.  " id))
6047              (point))
6048            `(gnus-callback
6049              (lambda (handles)
6050                (unless ,(not ibegend)
6051                  (setq gnus-article-mime-handle-alist
6052                        ',gnus-article-mime-handle-alist))
6053                (gnus-mime-display-alternative
6054                 ',ihandles ',not-pref ',begend ,id))
6055              keymap ,gnus-mime-button-map
6056              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6057              face ,gnus-article-button-face
6058              gnus-part ,id
6059              article-type multipart
6060              rear-nonsticky t))
6061           (widget-convert-button 'link from (point)
6062                                  :action 'gnus-widget-press-button
6063                                  :button-keymap gnus-widget-button-keymap)
6064           ;; Do the handles
6065           (while (setq handle (pop handles))
6066             (gnus-add-text-properties
6067              (setq from (point))
6068              (progn
6069                (insert (format "(%c) %-18s"
6070                                (if (equal handle preferred) ?* ? )
6071                                (mm-handle-media-type handle)))
6072                (point))
6073              `(gnus-callback
6074                (lambda (handles)
6075                  (unless ,(not ibegend)
6076                    (setq gnus-article-mime-handle-alist
6077                          ',gnus-article-mime-handle-alist))
6078                  (gnus-mime-display-alternative
6079                   ',ihandles ',handle ',begend ,id))
6080                keymap ,gnus-mime-button-map
6081                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6082                face ,gnus-article-button-face
6083                gnus-part ,id
6084                gnus-data ,handle
6085                rear-nonsticky t))
6086             (widget-convert-button 'link from (point)
6087                                    :action 'gnus-widget-press-button
6088                                    :button-keymap gnus-widget-button-keymap)
6089             (insert "  "))
6090           (insert "\n\n"))
6091         (when preferred
6092           (if (stringp (car preferred))
6093               (gnus-display-mime preferred)
6094             (let ((mail-parse-charset gnus-newsgroup-charset)
6095                   (mail-parse-ignored-charsets
6096                    (with-current-buffer gnus-summary-buffer
6097                      gnus-newsgroup-ignored-charsets)))
6098               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6099               ;; Do highlighting.
6100               (save-excursion
6101                 (save-restriction
6102                   (narrow-to-region (car begend) (point-max))
6103                   (gnus-treat-article
6104                    nil (length gnus-article-mime-handle-alist)
6105                    (gnus-article-mime-total-parts)
6106                    (mm-handle-media-type preferred))))))
6107           (goto-char (point-max))
6108           (setcdr begend (point-marker)))))
6109     (when ibegend
6110       (goto-char point))))
6111
6112 (defconst gnus-article-wash-status-strings
6113   (let ((alist '((cite "c" "Possible hidden citation text"
6114                        " " "All citation text visible")
6115                  (headers "h" "Hidden headers"
6116                           " " "All headers visible.")
6117                  (pgp "p" "Encrypted or signed message status hidden"
6118                       " " "No hidden encryption nor digital signature status")
6119                  (signature "s" "Signature has been hidden"
6120                             " " "Signature is visible")
6121                  (overstrike "o" "Overstrike (^H) characters applied"
6122                              " " "No overstrike characters applied")
6123                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6124                            " " "No /*_emphasis_*/ characters applied")))
6125         result)
6126     (dolist (entry alist result)
6127       (let ((key (nth 0 entry))
6128             (on (copy-sequence (nth 1 entry)))
6129             (on-help (nth 2 entry))
6130             (off (copy-sequence (nth 3 entry)))
6131             (off-help (nth 4 entry)))
6132         (put-text-property 0 1 'help-echo on-help on)
6133         (put-text-property 0 1 'help-echo off-help off)
6134         (push (list key on off) result))))
6135   "Alist of strings describing wash status in the mode line.
6136 Each entry has the form (KEY ON OF), where the KEY is a symbol
6137 representing the particular washing function, ON is the string to use
6138 in the article mode line when the washing function is active, and OFF
6139 is the string to use when it is inactive.")
6140
6141 (defun gnus-article-wash-status-entry (key value)
6142   (let ((entry (assoc key gnus-article-wash-status-strings)))
6143     (if value (nth 1 entry) (nth 2 entry))))
6144
6145 (defun gnus-article-wash-status ()
6146   "Return a string which display status of article washing."
6147   (with-current-buffer gnus-article-buffer
6148     (let ((cite (memq 'cite gnus-article-wash-types))
6149           (headers (memq 'headers gnus-article-wash-types))
6150           (boring (memq 'boring-headers gnus-article-wash-types))
6151           (pgp (memq 'pgp gnus-article-wash-types))
6152           (pem (memq 'pem gnus-article-wash-types))
6153           (signed (memq 'signed gnus-article-wash-types))
6154           (encrypted (memq 'encrypted gnus-article-wash-types))
6155           (signature (memq 'signature gnus-article-wash-types))
6156           (overstrike (memq 'overstrike gnus-article-wash-types))
6157           (emphasis (memq 'emphasis gnus-article-wash-types)))
6158       (concat
6159        (gnus-article-wash-status-entry 'cite cite)
6160        (gnus-article-wash-status-entry 'headers (or headers boring))
6161        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6162        (gnus-article-wash-status-entry 'signature signature)
6163        (gnus-article-wash-status-entry 'overstrike overstrike)
6164        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6165
6166 (defun gnus-add-wash-type (type)
6167   "Add a washing of TYPE to the current status."
6168   (add-to-list 'gnus-article-wash-types type))
6169
6170 (defun gnus-delete-wash-type (type)
6171   "Add a washing of TYPE to the current status."
6172   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6173
6174 (defun gnus-add-image (category image)
6175   "Add IMAGE of CATEGORY to the list of displayed images."
6176   (let ((entry (assq category gnus-article-image-alist)))
6177     (unless entry
6178       (setq entry (list category))
6179       (push entry gnus-article-image-alist))
6180     (nconc entry (list image))))
6181
6182 (defun gnus-delete-images (category)
6183   "Delete all images in CATEGORY."
6184   (let ((entry (assq category gnus-article-image-alist)))
6185     (dolist (image (cdr entry))
6186       (gnus-remove-image image category))
6187     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6188     (gnus-delete-wash-type category)))
6189
6190 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6191
6192 (defun gnus-article-maybe-hide-headers ()
6193   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6194 Provided for backwards compatibility."
6195   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6196                  (not (with-current-buffer gnus-summary-buffer
6197                         gnus-have-all-headers)))
6198              (not gnus-inhibit-hiding))
6199     (gnus-article-hide-headers)))
6200
6201 (declare-function shr-put-image "shr" (data alt &optional flags))
6202
6203 (defun gnus-shr-put-image (data alt &optional flags)
6204   "Put image DATA with a string ALT.  Enable image to be deleted."
6205   (let ((image (shr-put-image data (propertize (or alt "*")
6206                                                'gnus-image-category 'shr)
6207                               flags)))
6208     (when image
6209       (gnus-add-image 'shr image))))
6210
6211 ;;; Article savers.
6212
6213 (defun gnus-output-to-file (file-name)
6214   "Append the current article to a file named FILE-NAME.
6215 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6216 text and used as the value of the coding cookie which is added to the
6217 top of a file.  Otherwise, this function saves a raw article without
6218 the coding cookie."
6219   (let* ((artbuf (current-buffer))
6220          (file-name-coding-system nnmail-pathname-coding-system)
6221          (coding gnus-article-save-coding-system)
6222          (coding-system-for-read (if coding
6223                                      nil ;; Rely on the coding cookie.
6224                                    mm-text-coding-system))
6225          (coding-system-for-write (or coding
6226                                       mm-text-coding-system-for-write
6227                                       mm-text-coding-system))
6228          (exists (file-exists-p file-name)))
6229     (with-temp-buffer
6230       (when exists
6231         (insert-file-contents file-name)
6232         (goto-char (point-min))
6233         ;; Remove the existing coding cookie.
6234         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6235           (delete-region (match-beginning 0) (match-end 0))))
6236       (goto-char (point-max))
6237       (insert-buffer-substring artbuf)
6238       ;; Append newline at end of the buffer as separator, and then
6239       ;; save it to file.
6240       (goto-char (point-max))
6241       (insert "\n")
6242       (when coding
6243         ;; If the coding system is not suitable to encode the text,
6244         ;; ask a user for a proper one.
6245         (when (fboundp 'select-safe-coding-system)
6246           (setq coding (coding-system-base
6247                         (save-window-excursion
6248                           (select-safe-coding-system (point-min) (point-max)
6249                                                      coding))))
6250           (setq coding-system-for-write
6251                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6252                     coding)))
6253         (goto-char (point-min))
6254         ;; Add the coding cookie.
6255         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6256                         coding-system-for-write)))
6257       (if exists
6258           (progn
6259             (write-region (point-min) (point-max) file-name nil 'no-message)
6260             (message "Appended to %s" file-name))
6261         (write-region (point-min) (point-max) file-name))))
6262   t)
6263
6264 (defun gnus-narrow-to-page (&optional arg)
6265   "Narrow the article buffer to a page.
6266 If given a numerical ARG, move forward ARG pages."
6267   (interactive "P")
6268   (setq arg (if arg (prefix-numeric-value arg) 0))
6269   (with-current-buffer gnus-article-buffer
6270     (widen)
6271     ;; Remove any old next/prev buttons.
6272     (when (gnus-visual-p 'page-marker)
6273       (let ((inhibit-read-only t))
6274         (gnus-remove-text-with-property 'gnus-prev)
6275         (gnus-remove-text-with-property 'gnus-next)))
6276     (let (st nd pt)
6277       (when (save-excursion
6278               (cond ((< arg 0)
6279                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6280                          (prog1
6281                              (setq nd (match-beginning 0)
6282                                    pt nd)
6283                            (when (re-search-backward page-delimiter nil t)
6284                              (setq st (match-end 0))))
6285                        (when (re-search-forward page-delimiter nil t)
6286                          (setq nd (match-beginning 0)
6287                                pt (point-min)))))
6288                     ((> arg 0)
6289                      (if (re-search-forward page-delimiter nil 'move arg)
6290                          (prog1
6291                              (setq st (match-end 0)
6292                                    pt st)
6293                            (when (re-search-forward page-delimiter nil t)
6294                              (setq nd (match-beginning 0))))
6295                        (when (re-search-backward page-delimiter nil t)
6296                          (setq st (match-end 0)
6297                                pt (point-max)))))
6298                     (t
6299                      (when (re-search-backward page-delimiter nil t)
6300                        (goto-char (setq st (match-end 0))))
6301                      (when (re-search-forward page-delimiter nil t)
6302                        (setq nd (match-beginning 0)))
6303                      (or st nd))))
6304         (setq gnus-page-broken t)
6305         (when pt (goto-char pt))
6306         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6307         (when (gnus-visual-p 'page-marker)
6308           (save-excursion
6309             (when nd
6310               (goto-char nd)
6311               (gnus-insert-next-page-button))
6312             (when st
6313               (goto-char st)
6314               (gnus-insert-prev-page-button))))))))
6315
6316 ;; Article mode commands
6317
6318 (defun gnus-article-goto-next-page ()
6319   "Show the next page of the article."
6320   (interactive)
6321   (when (gnus-article-next-page)
6322     (goto-char (point-min))
6323     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6324
6325
6326 (defun gnus-article-goto-prev-page ()
6327   "Show the previous page of the article."
6328   (interactive)
6329   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6330       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6331     (gnus-article-prev-page nil)))
6332
6333 ;; This is cleaner but currently breaks `gnus-pick-mode':
6334 ;;
6335 ;; (defun gnus-article-goto-next-page ()
6336 ;;   "Show the next page of the article."
6337 ;;   (interactive)
6338 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6339 ;;     (gnus-summary-next-page)))
6340 ;;
6341 ;; (defun gnus-article-goto-prev-page ()
6342 ;;   "Show the next page of the article."
6343 ;;   (interactive)
6344 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6345 ;;     (gnus-summary-prev-page)))
6346
6347 (defun gnus-article-next-page (&optional lines)
6348   "Show the next page of the current article.
6349 If end of article, return non-nil.  Otherwise return nil.
6350 Argument LINES specifies lines to be scrolled up."
6351   (interactive "p")
6352   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6353   (if (and (not (and gnus-article-over-scroll
6354                      (> (count-lines (window-start) (point-max))
6355                         (if (featurep 'xemacs)
6356                             (or lines (1- (window-height)))
6357                           (+ (or lines (1- (window-height))) scroll-margin)))))
6358            (save-excursion
6359              (end-of-line)
6360              (and (pos-visible-in-window-p)     ;Not continuation line.
6361                   (>= (point) (point-max)))))
6362       ;; Nothing in this page.
6363       (if (or (not gnus-page-broken)
6364               (save-excursion
6365                 (save-restriction
6366                   (widen)
6367                   (forward-line)
6368                   (eobp)))) ;Real end-of-buffer?
6369           (progn
6370             (when gnus-article-over-scroll
6371               (gnus-article-next-page-1 lines))
6372             t)                  ;Nothing more.
6373         (gnus-narrow-to-page 1)         ;Go to next page.
6374         nil)
6375     ;; More in this page.
6376     (gnus-article-next-page-1 lines)
6377     nil))
6378
6379 (defun gnus-article-beginning-of-window ()
6380   "Move point to the beginning of the window.
6381 In Emacs, the point is placed at the line number which `scroll-margin'
6382 specifies."
6383   (if (featurep 'xemacs)
6384       (move-to-window-line 0)
6385     ;; There is an obscure bug in Emacs that makes it impossible to
6386     ;; scroll past big pictures in the article buffer.  Try to fix
6387     ;; this by adding a sanity check by counting the lines visible.
6388     (when (> (count-lines (window-start) (window-end)) 30)
6389       (move-to-window-line
6390        (min (max 0 scroll-margin)
6391             (max 1 (- (window-height)
6392                       (if mode-line-format 1 0)
6393                       (if header-line-format 1 0)
6394                       2)))))))
6395
6396 (defun gnus-article-next-page-1 (lines)
6397   (condition-case ()
6398       (let ((scroll-in-place nil)
6399             (auto-window-vscroll nil))
6400         (scroll-up lines))
6401     (end-of-buffer
6402      ;; Long lines may cause an end-of-buffer error.
6403      (goto-char (point-max))))
6404   (gnus-article-beginning-of-window))
6405
6406 (defun gnus-article-prev-page (&optional lines)
6407   "Show previous page of current article.
6408 Argument LINES specifies lines to be scrolled down."
6409   (interactive "p")
6410   (move-to-window-line 0)
6411   (if (and gnus-page-broken
6412            (bobp)
6413            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6414       (progn
6415         (gnus-narrow-to-page -1)        ;Go to previous page.
6416         (goto-char (point-max))
6417         (recenter (if gnus-article-over-scroll
6418                       (if lines
6419                           (max (if (featurep 'xemacs)
6420                                    lines
6421                                  (+ lines scroll-margin))
6422                                3)
6423                         (- (window-height) 2))
6424                     -1)))
6425     (prog1
6426         (condition-case ()
6427             (let ((scroll-in-place nil))
6428               (scroll-down lines))
6429           (beginning-of-buffer
6430            (goto-char (point-min))))
6431       (gnus-article-beginning-of-window))))
6432
6433 (defun gnus-article-only-boring-p ()
6434   "Decide whether there is only boring text remaining in the article.
6435 Something \"interesting\" is a word of at least two letters that does
6436 not have a face in `gnus-article-boring-faces'."
6437   (when (and gnus-article-skip-boring
6438              (boundp 'gnus-article-boring-faces)
6439              (symbol-value 'gnus-article-boring-faces))
6440     (save-excursion
6441       (let ((inhibit-point-motion-hooks t))
6442         (catch 'only-boring
6443           (while (re-search-forward "\\b\\w\\w" nil t)
6444             (forward-char -1)
6445             (when (not (gnus-intersection
6446                         (gnus-faces-at (point))
6447                         (symbol-value 'gnus-article-boring-faces)))
6448               (throw 'only-boring nil)))
6449           (throw 'only-boring t))))))
6450
6451 (defun gnus-article-refer-article ()
6452   "Read article specified by message-id around point."
6453   (interactive)
6454   (save-excursion
6455     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6456     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6457     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6458         (let ((msg-id (concat "<" (match-string 0) ">")))
6459           (set-buffer gnus-summary-buffer)
6460           (gnus-summary-refer-article msg-id))
6461       (error "No references around point"))))
6462
6463 (defun gnus-article-show-summary ()
6464   "Reconfigure windows to show summary buffer."
6465   (interactive)
6466   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6467       (error "There is no summary buffer for this article buffer")
6468     (gnus-article-set-globals)
6469     (gnus-configure-windows 'article)
6470     (gnus-summary-goto-subject gnus-current-article)
6471     (gnus-summary-position-point)))
6472
6473 (defun gnus-article-describe-briefly ()
6474   "Describe article mode commands briefly."
6475   (interactive)
6476   (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")))
6477
6478 (defun gnus-article-check-buffer ()
6479   "Beep if not in an article buffer."
6480   (unless (equal major-mode 'gnus-article-mode)
6481     (error "Command invoked outside of a Gnus article buffer")))
6482
6483 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6484   "Read a summary buffer key sequence and execute it from the article buffer."
6485   (interactive "P")
6486   (gnus-article-check-buffer)
6487   (let ((nosaves
6488          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6489            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6490            "=" "^" "\M-^" "|"))
6491         (nosave-but-article
6492          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6493            "An" "Ap" [?A (meta return)] [?A delete]))
6494         (nosave-in-article
6495          '("AS" "\C-d"))
6496         (up-to-top
6497          '("n" "Gn" "p" "Gp"))
6498         keys new-sum-point)
6499     (with-current-buffer gnus-article-current-summary
6500       (let (gnus-pick-mode)
6501         (setq unread-command-events (nconc unread-command-events
6502                                            (list (or key last-command-event)))
6503               keys (if (featurep 'xemacs)
6504                        (events-to-keys (read-key-sequence nil t))
6505                      (read-key-sequence nil t)))))
6506
6507     (message "")
6508
6509     (cond
6510      ((eq (aref keys (1- (length keys))) ?\C-h)
6511       (gnus-article-describe-bindings (substring keys 0 -1)))
6512      ((or (member keys nosaves)
6513           (member keys nosave-but-article)
6514           (member keys nosave-in-article))
6515       (let (func)
6516         (save-window-excursion
6517           (pop-to-buffer gnus-article-current-summary)
6518           ;; We disable the pick minor mode commands.
6519           (let (gnus-pick-mode)
6520             (setq func (key-binding keys t))))
6521         (if (or (not func)
6522                 (numberp func))
6523             (ding)
6524           (unless (member keys nosave-in-article)
6525             (set-buffer gnus-article-current-summary))
6526           (when (get func 'disabled)
6527             (error "Function %s disabled" func))
6528           (call-interactively func)
6529           (setq new-sum-point (point)))
6530         (when (member keys nosave-but-article)
6531           (pop-to-buffer gnus-article-buffer))))
6532      (t
6533       ;; These commands should restore window configuration.
6534       (let ((obuf (current-buffer))
6535             (owin (current-window-configuration))
6536             win func in-buffer selected new-sum-start new-sum-hscroll err)
6537         (cond (not-restore-window
6538                (pop-to-buffer gnus-article-current-summary)
6539                (setq win (selected-window)))
6540               ((setq win (get-buffer-window gnus-article-current-summary))
6541                (select-window win))
6542               (t
6543                (let ((summary-buffer gnus-article-current-summary))
6544                  (gnus-configure-windows 'article)
6545                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6546                    (let ((gnus-buffer-configuration
6547                           '((article ((vertical 1.0
6548                                                 (summary 0.25 point)
6549                                                 (article 1.0)))))))
6550                      (gnus-configure-windows 'article))
6551                    (setq win (get-buffer-window summary-buffer 'visible)))
6552                  (gnus-select-frame-set-input-focus (window-frame win))
6553                  (select-window win))))
6554         (setq in-buffer (current-buffer))
6555         ;; We disable the pick minor mode commands.
6556         (setq func (let (gnus-pick-mode)
6557                      (key-binding keys t)))
6558         (when (get func 'disabled)
6559           (error "Function %s disabled" func))
6560         (if (and func
6561                  (functionp func)
6562                  (condition-case code
6563                      (progn
6564                        (call-interactively func)
6565                        t)
6566                    (error
6567                     (setq err code)
6568                     nil)))
6569             (progn
6570               (when (eq win (selected-window))
6571                 (setq new-sum-point (point)
6572                       new-sum-start (window-start win)
6573                       new-sum-hscroll (window-hscroll win)))
6574               (when (or (eq in-buffer (current-buffer))
6575                         (when (eq obuf (current-buffer))
6576                           (set-buffer in-buffer)
6577                           t))
6578                 (setq selected (gnus-summary-select-article))
6579                 (set-buffer obuf)
6580                 (unless not-restore-window
6581                   (set-window-configuration owin))
6582                 (when (and (eq selected 'old)
6583                            new-sum-point)
6584                   (set-window-start (get-buffer-window (current-buffer))
6585                                     1)
6586                   (set-window-point (get-buffer-window (current-buffer))
6587                                     (if (article-goto-body)
6588                                         (1- (point))
6589                                       (point))))
6590                 (when (and (not not-restore-window)
6591                            new-sum-point
6592                            (window-live-p win)
6593                            (with-current-buffer (window-buffer win)
6594                              (eq major-mode 'gnus-summary-mode)))
6595                   (set-window-point win new-sum-point)
6596                   (set-window-start win new-sum-start)
6597                   (set-window-hscroll win new-sum-hscroll))))
6598           (set-window-configuration owin)
6599           (if err
6600               (signal (car err) (cdr err))
6601             (ding))))))))
6602
6603 (defun gnus-article-read-summary-send-keys ()
6604   (interactive)
6605   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6606     (gnus-article-read-summary-keys)))
6607
6608 (defun gnus-article-describe-key (key)
6609   "Display documentation of the function invoked by KEY.
6610 KEY is a string or a vector."
6611   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6612                        (read-key-sequence "Describe key: "))))
6613   (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 (read-key-sequence nil t))))
6627     (describe-key key)))
6628
6629 (defun gnus-article-describe-key-briefly (key &optional insert)
6630   "Display documentation of the function invoked by KEY.
6631 KEY is a string or a vector."
6632   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6633                        (read-key-sequence "Describe key: "))
6634                      current-prefix-arg))
6635   (gnus-article-check-buffer)
6636   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6637                                   gnus-article-read-summary-send-keys))
6638       (with-current-buffer gnus-article-current-summary
6639         (setq unread-command-events
6640               (if (featurep 'xemacs)
6641                   (append key nil)
6642                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6643                                         (list 'meta (- x 128))
6644                                       x))
6645                         key)))
6646         (let ((cursor-in-echo-area t)
6647               gnus-pick-mode)
6648           (describe-key-briefly (read-key-sequence nil t) insert)))
6649     (describe-key-briefly key insert)))
6650
6651 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6652 (defvar gnus-agent-summary-mode)
6653 (defvar gnus-draft-mode)
6654 ;; Calling help-buffer will autoload help-mode.
6655 (defvar help-xref-stack-item)
6656 ;; Emacs 22 doesn't load it in the batch mode.
6657 (eval-when-compile
6658   (autoload 'help-buffer "help-mode"))
6659
6660 (defun gnus-article-describe-bindings (&optional prefix)
6661   "Show a list of all defined keys, and their definitions.
6662 The optional argument PREFIX, if non-nil, should be a key sequence;
6663 then we display only bindings that start with that prefix."
6664   (interactive)
6665   (gnus-article-check-buffer)
6666   (let ((keymap (copy-keymap gnus-article-mode-map))
6667         (map (copy-keymap gnus-article-send-map))
6668         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6669         parent agent draft)
6670     (define-key keymap "S" map)
6671     (define-key map [t] nil)
6672     (with-current-buffer gnus-article-current-summary
6673       (set-keymap-parent
6674        keymap
6675        (if (setq parent (keymap-parent gnus-article-mode-map))
6676            (prog1
6677                (setq parent (copy-keymap parent))
6678              (set-keymap-parent parent (current-local-map)))
6679          (current-local-map)))
6680       (set-keymap-parent map (key-binding "S"))
6681       (let (key def gnus-pick-mode)
6682         (while sumkeys
6683           (setq key (pop sumkeys))
6684           (cond ((and (vectorp key) (= (length key) 1)
6685                       (consp (setq def (aref key 0)))
6686                       (numberp (car def)) (numberp (cdr def)))
6687                  (when (< (max (car def) (cdr def)) 128)
6688                    (setq sumkeys
6689                          (append (mapcar
6690                                   #'vector
6691                                   (nreverse (gnus-uncompress-range def)))
6692                                  sumkeys))))
6693                 ((setq def (key-binding key))
6694                  (unless (eq def 'undefined)
6695                    (define-key keymap key def))))))
6696       (when (boundp 'gnus-agent-summary-mode)
6697         (setq agent gnus-agent-summary-mode))
6698       (when (boundp 'gnus-draft-mode)
6699         (setq draft gnus-draft-mode)))
6700     (with-temp-buffer
6701       (use-local-map keymap)
6702       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6703       (set (make-local-variable 'gnus-draft-mode) draft)
6704       (describe-bindings prefix))
6705     (let ((item `((lambda (prefix)
6706                     (with-current-buffer ,(current-buffer)
6707                       (gnus-article-describe-bindings prefix)))
6708                   ,prefix)))
6709       (with-current-buffer (let (help-xref-following) (help-buffer))
6710         (setq help-xref-stack-item item)))))
6711
6712 (defun gnus-article-reply-with-original (&optional wide)
6713   "Start composing a reply mail to the current message.
6714 The text in the region will be yanked.  If the region isn't active,
6715 the entire article will be yanked."
6716   (interactive)
6717   (let ((article (cdr gnus-article-current))
6718         contents)
6719     (if (not (gnus-region-active-p))
6720         (with-current-buffer gnus-summary-buffer
6721           (gnus-summary-reply (list (list article)) wide))
6722       (setq contents (buffer-substring (point) (mark t)))
6723       ;; Deactivate active regions.
6724       (when (and (boundp 'transient-mark-mode)
6725                  transient-mark-mode)
6726         (setq mark-active nil))
6727       (with-current-buffer gnus-summary-buffer
6728         (gnus-summary-reply
6729          (list (list article contents)) wide)))))
6730
6731 (defun gnus-article-wide-reply-with-original ()
6732   "Start composing a wide reply mail to the current message.
6733 The text in the region will be yanked.  If the region isn't active,
6734 the entire article will be yanked."
6735   (interactive)
6736   (gnus-article-reply-with-original t))
6737
6738 (defun gnus-article-followup-with-original ()
6739   "Compose a followup to the current article.
6740 The text in the region will be yanked.  If the region isn't active,
6741 the entire article will be yanked."
6742   (interactive)
6743   (let ((article (cdr gnus-article-current))
6744         contents)
6745       (if (not (gnus-region-active-p))
6746           (with-current-buffer gnus-summary-buffer
6747             (gnus-summary-followup (list (list article))))
6748         (setq contents (buffer-substring (point) (mark t)))
6749         ;; Deactivate active regions.
6750         (when (and (boundp 'transient-mark-mode)
6751                    transient-mark-mode)
6752           (setq mark-active nil))
6753         (with-current-buffer gnus-summary-buffer
6754           (gnus-summary-followup
6755            (list (list article contents)))))))
6756
6757 (defun gnus-article-hide (&optional arg force)
6758   "Hide all the gruft in the current article.
6759 This means that signatures, cited text and (some) headers will be
6760 hidden.
6761 If given a prefix, show the hidden text instead."
6762   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6763   (gnus-article-hide-headers arg)
6764   (gnus-article-hide-list-identifiers arg)
6765   (gnus-article-hide-citation-maybe arg force)
6766   (gnus-article-hide-signature arg))
6767
6768 (defun gnus-article-maybe-highlight ()
6769   "Do some article highlighting if article highlighting is requested."
6770   (when (gnus-visual-p 'article-highlight 'highlight)
6771     (gnus-article-highlight-some)))
6772
6773 (defun gnus-check-group-server ()
6774   ;; Make sure the connection to the server is alive.
6775   (unless (gnus-server-opened
6776            (gnus-find-method-for-group gnus-newsgroup-name))
6777     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6778     (gnus-request-group gnus-newsgroup-name t)))
6779
6780 (eval-when-compile
6781   (autoload 'nneething-get-file-name "nneething"))
6782
6783 (defun gnus-request-article-this-buffer (article group)
6784   "Get an article and insert it into this buffer."
6785   (let (do-update-line sparse-header)
6786     (prog1
6787         (save-excursion
6788           (erase-buffer)
6789           (gnus-kill-all-overlays)
6790           (setq group (or group gnus-newsgroup-name))
6791
6792           ;; Using `gnus-request-article' directly will insert the article into
6793           ;; `nntp-server-buffer' - so we'll save some time by not having to
6794           ;; copy it from the server buffer into the article buffer.
6795
6796           ;; We only request an article by message-id when we do not have the
6797           ;; headers for it, so we'll have to get those.
6798           (when (stringp article)
6799             (gnus-read-header article))
6800
6801           ;; If the article number is negative, that means that this article
6802           ;; doesn't belong in this newsgroup (possibly), so we find its
6803           ;; message-id and request it by id instead of number.
6804           (when (and (numberp article)
6805                      gnus-summary-buffer
6806                      (get-buffer gnus-summary-buffer)
6807                      (gnus-buffer-exists-p gnus-summary-buffer))
6808             (with-current-buffer gnus-summary-buffer
6809               (let ((header (gnus-summary-article-header article)))
6810                 (when (< article 0)
6811                   (cond
6812                    ((memq article gnus-newsgroup-sparse)
6813                     ;; This is a sparse gap article.
6814                     (setq do-update-line article)
6815                     (setq article (mail-header-id header))
6816                     (setq sparse-header (gnus-read-header article))
6817                     (setq gnus-newsgroup-sparse
6818                           (delq article gnus-newsgroup-sparse)))
6819                    ((vectorp header)
6820                     ;; It's a real article.
6821                     (setq article (mail-header-id header)))
6822                    (t
6823                     ;; It is an extracted pseudo-article.
6824                     (setq article 'pseudo)
6825                     (gnus-request-pseudo-article header))))
6826
6827                 (let ((method (gnus-find-method-for-group
6828                                gnus-newsgroup-name)))
6829                   (when (and (eq (car method) 'nneething)
6830                              (vectorp header))
6831                     (let ((dir (nneething-get-file-name
6832                                 (mail-header-id header))))
6833                       (when (and (stringp dir)
6834                                  (file-directory-p dir))
6835                         (setq article 'nneething)
6836                         (gnus-group-enter-directory dir))))))))
6837
6838           (cond
6839            ;; Refuse to select canceled articles.
6840            ((and (numberp article)
6841                  gnus-summary-buffer
6842                  (get-buffer gnus-summary-buffer)
6843                  (gnus-buffer-exists-p gnus-summary-buffer)
6844                  (eq (cdr (with-current-buffer gnus-summary-buffer
6845                             (assq article gnus-newsgroup-reads)))
6846                      gnus-canceled-mark))
6847             nil)
6848            ;; We first check `gnus-original-article-buffer'.
6849            ((and (get-buffer gnus-original-article-buffer)
6850                  (numberp article)
6851                  (with-current-buffer gnus-original-article-buffer
6852                    (and (equal (car gnus-original-article) group)
6853                         (eq (cdr gnus-original-article) article))))
6854             ;; `insert-buffer-substring' would incorrectly use the
6855             ;; equivalent of string-make-multibyte which amount to decoding
6856             ;; with locale-coding-system, causing failure of
6857             ;; subsequent decoding.
6858             (insert (mm-string-to-multibyte
6859                      (with-current-buffer gnus-original-article-buffer
6860                        (buffer-substring (point-min) (point-max)))))
6861             'article)
6862            ;; Check the backlog.
6863            ((and gnus-keep-backlog
6864                  (gnus-backlog-request-article group article (current-buffer)))
6865             'article)
6866            ;; Check asynchronous pre-fetch.
6867            ((gnus-async-request-fetched-article group article (current-buffer))
6868             (gnus-async-prefetch-next group article gnus-summary-buffer)
6869             (when (and (numberp article) gnus-keep-backlog)
6870               (gnus-backlog-enter-article group article (current-buffer)))
6871             'article)
6872            ;; Check the cache.
6873            ((and gnus-use-cache
6874                  (numberp article)
6875                  (gnus-cache-request-article article group))
6876             'article)
6877            ;; Check the agent cache.
6878            ((gnus-agent-request-article article group)
6879             'article)
6880            ;; Get the article and put into the article buffer.
6881            ((or (stringp article)
6882                 (numberp article))
6883             (let ((gnus-override-method gnus-override-method)
6884                   (methods (and (stringp article)
6885                                 (with-current-buffer gnus-summary-buffer
6886                                   (gnus-refer-article-methods))))
6887                   (backend (car (gnus-find-method-for-group
6888                                  gnus-newsgroup-name)))
6889                   result
6890                   (inhibit-read-only t))
6891               (when (and (null gnus-override-method)
6892                          methods)
6893                 (setq gnus-override-method (pop methods)))
6894               (while (not result)
6895                 (erase-buffer)
6896                 (gnus-kill-all-overlays)
6897                 (let ((gnus-newsgroup-name group))
6898                   (gnus-check-group-server))
6899                 (cond
6900                  ((gnus-request-article article group (current-buffer))
6901                   (when (numberp article)
6902                     (gnus-async-prefetch-next group article
6903                                               gnus-summary-buffer)
6904                     (when gnus-keep-backlog
6905                       (gnus-backlog-enter-article
6906                        group article (current-buffer)))
6907                     (when (and gnus-agent
6908                                (gnus-agent-group-covered-p group))
6909                       (gnus-agent-store-article article group)))
6910                   (setq result 'article))
6911                  (methods
6912                   (setq gnus-override-method (pop methods)))
6913                  ((not (string-match "^400 "
6914                                      (nnheader-get-report backend)))
6915                   ;; If we get 400 server disconnect, reconnect and
6916                   ;; retry; otherwise, assume the article has expired.
6917                   (setq result 'done))))
6918               (and (eq result 'article) 'article)))
6919            ;; It was a pseudo.
6920            (t article)))
6921
6922       ;; Associate this article with the current summary buffer.
6923       (setq gnus-article-current-summary gnus-summary-buffer)
6924
6925       ;; Take the article from the original article buffer
6926       ;; and place it in the buffer it's supposed to be in.
6927       (when (and (get-buffer gnus-article-buffer)
6928                  (equal (buffer-name (current-buffer))
6929                         (buffer-name (get-buffer gnus-article-buffer))))
6930         (save-excursion
6931           (if (get-buffer gnus-original-article-buffer)
6932               (set-buffer gnus-original-article-buffer)
6933             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6934             (buffer-disable-undo)
6935             (setq major-mode 'gnus-original-article-mode)
6936             (setq buffer-read-only t))
6937           (let ((inhibit-read-only t))
6938             (erase-buffer)
6939             (insert-buffer-substring gnus-article-buffer))
6940           (setq gnus-original-article (cons group article)))
6941
6942         ;; Decode charsets.
6943         (run-hooks 'gnus-article-decode-hook)
6944         ;; Mark article as decoded or not.
6945         (setq gnus-article-decoded-p gnus-article-decode-hook))
6946
6947       ;; Update sparse articles.
6948       (when (and do-update-line
6949                  (or (numberp article)
6950                      (stringp article)))
6951         (let ((buf (current-buffer)))
6952           (set-buffer gnus-summary-buffer)
6953           (gnus-summary-update-article do-update-line sparse-header)
6954           (gnus-summary-goto-subject do-update-line nil t)
6955           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6956                             (point))
6957           (set-buffer buf))))))
6958
6959 (defun gnus-block-private-groups (group)
6960   (if (gnus-news-group-p group)
6961       ;; Block nothing in news groups.
6962       nil
6963     ;; Block everything anywhere else.
6964     "."))
6965
6966 (defun gnus-blocked-images ()
6967   (if (functionp gnus-blocked-images)
6968       (funcall gnus-blocked-images gnus-newsgroup-name)
6969     gnus-blocked-images))
6970
6971 ;;;
6972 ;;; Article editing
6973 ;;;
6974
6975 (defcustom gnus-article-edit-mode-hook nil
6976   "Hook run in article edit mode buffers."
6977   :group 'gnus-article-various
6978   :type 'hook)
6979
6980 (defvar gnus-article-edit-done-function nil)
6981
6982 (defvar gnus-article-edit-mode-map nil)
6983
6984 ;; Should we be using derived.el for this?
6985 (unless gnus-article-edit-mode-map
6986   (setq gnus-article-edit-mode-map (make-keymap))
6987   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6988
6989   (gnus-define-keys gnus-article-edit-mode-map
6990     "\C-c?"    describe-mode
6991     "\C-c\C-c" gnus-article-edit-done
6992     "\C-c\C-k" gnus-article-edit-exit
6993     "\C-c\C-f\C-t" message-goto-to
6994     "\C-c\C-f\C-o" message-goto-from
6995     "\C-c\C-f\C-b" message-goto-bcc
6996     ;;"\C-c\C-f\C-w" message-goto-fcc
6997     "\C-c\C-f\C-c" message-goto-cc
6998     "\C-c\C-f\C-s" message-goto-subject
6999     "\C-c\C-f\C-r" message-goto-reply-to
7000     "\C-c\C-f\C-n" message-goto-newsgroups
7001     "\C-c\C-f\C-d" message-goto-distribution
7002     "\C-c\C-f\C-f" message-goto-followup-to
7003     "\C-c\C-f\C-m" message-goto-mail-followup-to
7004     "\C-c\C-f\C-k" message-goto-keywords
7005     "\C-c\C-f\C-u" message-goto-summary
7006     "\C-c\C-f\C-i" message-insert-or-toggle-importance
7007     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
7008     "\C-c\C-b" message-goto-body
7009     "\C-c\C-i" message-goto-signature
7010
7011     "\C-c\C-t" message-insert-to
7012     "\C-c\C-n" message-insert-newsgroups
7013     "\C-c\C-o" message-sort-headers
7014     "\C-c\C-e" message-elide-region
7015     "\C-c\C-v" message-delete-not-region
7016     "\C-c\C-z" message-kill-to-signature
7017     "\M-\r" message-newline-and-reformat
7018     "\C-c\C-a" mml-attach-file
7019     "\C-a" message-beginning-of-line
7020     "\t" message-tab
7021     "\M-;" comment-region)
7022
7023   (gnus-define-keys (gnus-article-edit-wash-map
7024                      "\C-c\C-w" gnus-article-edit-mode-map)
7025     "f" gnus-article-edit-full-stops))
7026
7027 (easy-menu-define
7028   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7029   '("Field"
7030     ["Fetch To" message-insert-to t]
7031     ["Fetch Newsgroups" message-insert-newsgroups t]
7032     "----"
7033     ["To" message-goto-to t]
7034     ["From" message-goto-from t]
7035     ["Subject" message-goto-subject t]
7036     ["Cc" message-goto-cc t]
7037     ["Reply-To" message-goto-reply-to t]
7038     ["Summary" message-goto-summary t]
7039     ["Keywords" message-goto-keywords t]
7040     ["Newsgroups" message-goto-newsgroups t]
7041     ["Followup-To" message-goto-followup-to t]
7042     ["Mail-Followup-To" message-goto-mail-followup-to t]
7043     ["Distribution" message-goto-distribution t]
7044     ["Body" message-goto-body t]
7045     ["Signature" message-goto-signature t]))
7046
7047 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7048   "Major mode for editing articles.
7049 This is an extended text-mode.
7050
7051 \\{gnus-article-edit-mode-map}"
7052   (make-local-variable 'gnus-article-edit-done-function)
7053   (make-local-variable 'gnus-prev-winconf)
7054   (set (make-local-variable 'font-lock-defaults)
7055        '(message-font-lock-keywords t))
7056   (set (make-local-variable 'mail-header-separator) "")
7057   (set (make-local-variable 'gnus-article-edit-mode) t)
7058   (easy-menu-add message-mode-field-menu message-mode-map)
7059   (mml-mode)
7060   (setq buffer-read-only nil)
7061   (buffer-enable-undo)
7062   (widen))
7063
7064 (defun gnus-article-edit (&optional force)
7065   "Edit the current article.
7066 This will have permanent effect only in mail groups.
7067 If FORCE is non-nil, allow editing of articles even in read-only
7068 groups."
7069   (interactive "P")
7070   (when (and (not force)
7071              (gnus-group-read-only-p))
7072     (error "The current newsgroup does not support article editing"))
7073   (gnus-article-date-original)
7074   (gnus-article-edit-article
7075    'ignore
7076    `(lambda (no-highlight)
7077       'ignore
7078       (gnus-summary-edit-article-done
7079        ,(or (mail-header-references gnus-current-headers) "")
7080        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7081
7082 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7083   "Start editing the contents of the current article buffer."
7084   (let ((winconf (current-window-configuration)))
7085     (set-buffer gnus-article-buffer)
7086     (let ((message-auto-save-directory
7087            ;; Don't associate the article buffer with a draft file.
7088            nil))
7089       (gnus-article-edit-mode))
7090     (funcall start-func)
7091     (set-buffer-modified-p nil)
7092     (gnus-configure-windows 'edit-article)
7093     (setq gnus-article-edit-done-function exit-func)
7094     (setq gnus-prev-winconf winconf)
7095     (unless quiet
7096       (gnus-message 6 "C-c C-c to end edits"))))
7097
7098 (defun gnus-article-edit-done (&optional arg)
7099   "Update the article edits and exit."
7100   (interactive "P")
7101   (let ((func gnus-article-edit-done-function)
7102         (buf (current-buffer))
7103         (start (window-start))
7104         (p (point))
7105         (winconf gnus-prev-winconf))
7106     (widen) ;; Widen it in case that users narrowed the buffer.
7107     (funcall func arg)
7108     (set-buffer buf)
7109     ;; The cache and backlog have to be flushed somewhat.
7110     (when gnus-keep-backlog
7111       (gnus-backlog-remove-article
7112        (car gnus-article-current) (cdr gnus-article-current)))
7113     ;; Flush original article as well.
7114     (gnus-flush-original-article-buffer)
7115     (when gnus-use-cache
7116       (gnus-cache-update-article
7117        (car gnus-article-current) (cdr gnus-article-current)))
7118     ;; We remove all text props from the article buffer.
7119     (kill-all-local-variables)
7120     (set-text-properties (point-min) (point-max) nil)
7121     (gnus-article-mode)
7122     (set-window-configuration winconf)
7123     (set-buffer buf)
7124     (set-window-start (get-buffer-window buf) start)
7125     (set-window-point (get-buffer-window buf) (point)))
7126   (gnus-summary-show-article))
7127
7128 (defun gnus-flush-original-article-buffer ()
7129   (when (get-buffer gnus-original-article-buffer)
7130     (with-current-buffer gnus-original-article-buffer
7131       (setq gnus-original-article nil))))
7132
7133 (defun gnus-article-edit-exit ()
7134   "Exit the article editing without updating."
7135   (interactive)
7136   (when (or (not (buffer-modified-p))
7137             (yes-or-no-p "Article modified; kill anyway? "))
7138     (let ((curbuf (current-buffer))
7139           (p (point))
7140           (window-start (window-start)))
7141       (erase-buffer)
7142       (if (gnus-buffer-live-p gnus-original-article-buffer)
7143           (insert-buffer-substring gnus-original-article-buffer))
7144       (let ((winconf gnus-prev-winconf))
7145         (kill-all-local-variables)
7146         (gnus-article-mode)
7147         (set-window-configuration winconf)
7148         ;; Tippy-toe some to make sure that point remains where it was.
7149         (save-current-buffer
7150           (set-buffer curbuf)
7151           (set-window-start (get-buffer-window (current-buffer)) window-start)
7152           (goto-char p))))
7153     (gnus-summary-show-article)))
7154
7155 (defun gnus-article-edit-full-stops ()
7156   "Interactively repair spacing at end of sentences."
7157   (interactive)
7158   (save-excursion
7159     (goto-char (point-min))
7160     (search-forward-regexp "^$" nil t)
7161     (let ((case-fold-search nil))
7162       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7163
7164 ;;;
7165 ;;; Article highlights
7166 ;;;
7167
7168 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7169
7170 ;;; Internal Variables:
7171
7172 (defcustom gnus-button-url-regexp
7173   (concat
7174    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7175    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7176    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7177    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7178        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7179              (punct "!?:;.,"))
7180          (concat
7181           "\\(?:"
7182           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7183           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7184           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7185           "\\|"
7186           "[" chars punct     "]+" "[" chars "]"
7187           "\\)"))
7188      (concat ;; XEmacs 21.4 doesn't support POSIX.
7189       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7190       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7191    "\\)")
7192   "Regular expression that matches URLs."
7193   :group 'gnus-article-buttons
7194   :type 'regexp)
7195
7196 (defcustom gnus-button-valid-fqdn-regexp
7197   message-valid-fqdn-regexp
7198   "Regular expression that matches a valid FQDN."
7199   :version "22.1"
7200   :group 'gnus-article-buttons
7201   :type 'regexp)
7202
7203 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7204 (defcustom gnus-button-valid-localpart-regexp
7205   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7206   "Regular expression that matches a localpart of mail addresses or MIDs."
7207   :version "22.1"
7208   :group 'gnus-article-buttons
7209   :type 'regexp)
7210
7211 (defcustom gnus-button-man-handler 'manual-entry
7212   "Function to use for displaying man pages.
7213 The function must take at least one argument with a string naming the
7214 man page."
7215   :version "22.1"
7216   :type '(choice (function-item :tag "Man" manual-entry)
7217                  (function-item :tag "Woman" woman)
7218                  (function :tag "Other"))
7219   :group 'gnus-article-buttons)
7220
7221 (defcustom gnus-button-mid-or-mail-regexp
7222   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7223           gnus-button-valid-fqdn-regexp
7224           ">?\\)\\b")
7225   "Regular expression that matches a message ID or a mail address."
7226   :version "22.1"
7227   :group 'gnus-article-buttons
7228   :type 'regexp)
7229
7230 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7231   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7232 Strings like this can be either a message ID or a mail address.  If it is one
7233 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7234 message ID or a mail address, respectively.  If this variable is set to the
7235 symbol `ask', always query the user what do do.  If it is a function, this
7236 function will be called with the string as its only argument.  The function
7237 must return `mid', `mail', `invalid' or `ask'."
7238   :version "22.1"
7239   :group 'gnus-article-buttons
7240   :type '(choice (function-item :tag "Heuristic function"
7241                                 gnus-button-mid-or-mail-heuristic)
7242                  (const ask)
7243                  (const mid)
7244                  (const mail)))
7245
7246 (defcustom gnus-button-mid-or-mail-heuristic-alist
7247   '((-10.0 . ".+\\$.+@")
7248     (-10.0 . "#")
7249     (-10.0 . "\\*")
7250     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7251     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7252     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7253     (-1.0  . "^[^a-z]+@")
7254     ;;
7255     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7256     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7257     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7258     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7259     ;;
7260     (-2.0  . "^[0-9]")
7261     (-1.0  . "^[0-9][0-9]")
7262     ;;
7263     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7264     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7265     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7266     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7267     ;;
7268     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7269     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7270     ;;       "[0-9]{8,}.*\@"
7271     (-3.0
7272      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7273     ;; "[0-9]{12,}.*\@"
7274     ;; compensation for TDMA dated mail addresses:
7275     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7276     ;;
7277     (-20.0 . "\\.fsf@") ;; Gnus
7278     (-20.0 . "^slrn")
7279     (-20.0 . "^Pine")
7280     (-20.0 . "^alpine\\.")
7281     (-20.0 . "_-_") ;; Subject change in thread
7282     ;;
7283     (-20.0 . "\\.ln@") ;; leafnode
7284     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7285     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7286     ;;
7287     ;; (5.0 . "") ;; $local_part_len <= 7
7288     (10.0  . "^[^0-9]+@")
7289     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7290     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7291     (3.0   . "\@stud")
7292     ;;
7293     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7294     ;;
7295     (0.5   . "^[A-Z][a-z]")
7296     (0.5   . "^[A-Z][a-z][a-z]")
7297     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7298     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7299   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7300
7301 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7302 address.  The REGEXP is processed with `case-fold-search' set to nil."
7303   :version "22.1"
7304   :group 'gnus-article-buttons
7305   :type '(repeat (cons (number :tag "Rate")
7306                        (regexp :tag "Regexp"))))
7307
7308 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7309   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7310 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7311 address, `ask' if unsure and `invalid' if the string is invalid."
7312   (let ((case-fold-search nil)
7313         (list gnus-button-mid-or-mail-heuristic-alist)
7314         (result 0) rate regexp lpartlen elem)
7315     (setq lpartlen
7316           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7317     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7318     ;; Certain special cases...
7319     (when (string-match
7320            (concat
7321             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7322             "^[0-9]+\\.[0-9]+@compuserve\\|"
7323             "@public\\.gmane\\.org")
7324            mid-or-mail)
7325       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7326       (setq result 'mail))
7327     (when (string-match "@.*@\\| " mid-or-mail)
7328       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7329       (setq result 'invalid))
7330     ;; Nothing more to do, if result is not a number here...
7331     (when (numberp result)
7332       (while list
7333         (setq elem (car list)
7334               rate (car elem)
7335               regexp (cdr elem)
7336               list (cdr list))
7337         (when (string-match regexp mid-or-mail)
7338           (setq result (+ result rate))
7339           (gnus-message
7340            9 "`%s' matched `%s', rate `%s', result `%s'."
7341            mid-or-mail regexp rate result)))
7342       (when (<= lpartlen 7)
7343         (setq result (+ result 5.0))
7344         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7345                       mid-or-mail result))
7346       (when (>= lpartlen 12)
7347         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7348         (cond
7349          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7350           ;; Long local part should contain realname if e-mail address,
7351           ;; too many digits: message-id.
7352           ;; $score -= 5.0 + 0.1 * $local_part_len;
7353           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7354           (setq result (+ result rate))
7355           (gnus-message
7356            9 "Many digits in `%s', rate `%s', result `%s'."
7357            mid-or-mail rate result))
7358          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7359                         mid-or-mail)
7360           ;; Too few vowels [^aeiouy]{4,}.*\@
7361           (setq result (+ result -5.0))
7362           (gnus-message
7363            9 "Few vowels in `%s', rate `%s', result `%s'."
7364            mid-or-mail -5.0 result))
7365          (t
7366           (setq result (+ result 5.0))
7367           (gnus-message
7368            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7369     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7370     ;; Maybe we should make this a customizable alist: (condition . 'result)
7371     (cond
7372      ((symbolp result) result)
7373      ;; Now convert number into proper results:
7374      ((< result -10.0) 'mid)
7375      ((> result  10.0) 'mail)
7376      (t 'ask))))
7377
7378 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7379   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7380          (url-mid (concat "news" ":" mid-or-mail))
7381          (url-mailto (concat "mailto" ":" mid-or-mail)))
7382     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7383     (when (fboundp pref)
7384       (setq guessed
7385             ;; get rid of surrounding angles...
7386             (funcall pref
7387                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7388       (if (or (eq 'mid guessed) (eq 'mail guessed))
7389           (setq pref guessed)
7390         (setq pref 'ask)))
7391     (if (eq pref 'ask)
7392         (save-window-excursion
7393           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7394               (setq pref 'mail)
7395             (setq pref 'mid))))
7396     (cond ((eq pref 'mid)
7397            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7398            (gnus-button-handle-news url-mid))
7399           ((eq pref 'mail)
7400            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7401            (gnus-url-mailto url-mailto))
7402           (t (gnus-message 3 "Invalid string.")))))
7403
7404 (defun gnus-button-handle-custom (fun arg)
7405   "Call function FUN on argument ARG.
7406 Both FUN and ARG are supposed to be strings.  ARG will be passed
7407 as a symbol to FUN."
7408   (funcall (intern fun)
7409            (if (string-match "^customize-apropos" fun)
7410                arg
7411              (intern arg))))
7412
7413 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7414
7415 (defun gnus-button-handle-describe-function (url)
7416   "Call `describe-function' when pushing the corresponding URL button."
7417   (describe-function
7418    (intern
7419     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7420
7421 (defun gnus-button-handle-describe-variable (url)
7422   "Call `describe-variable' when pushing the corresponding URL button."
7423   (describe-variable
7424    (intern
7425     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7426
7427 (defun gnus-button-handle-symbol (url)
7428 "Display help on variable or function.
7429 Calls `describe-variable' or `describe-function'."
7430   (let ((sym (intern url)))
7431     (cond
7432      ((fboundp sym) (describe-function sym))
7433      ((boundp sym) (describe-variable sym))
7434      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7435
7436 (defun gnus-button-handle-describe-key (url)
7437   "Call `describe-key' when pushing the corresponding URL button."
7438   (let* ((key-string
7439           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7440          (keys (ignore-errors (eval `(kbd ,key-string)))))
7441     (if keys
7442         (describe-key keys)
7443       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7444
7445 (defun gnus-button-handle-apropos (url)
7446   "Call `apropos' when pushing the corresponding URL button."
7447   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7448
7449 (defun gnus-button-handle-apropos-command (url)
7450   "Call `apropos' when pushing the corresponding URL button."
7451   (apropos-command
7452    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7453
7454 (defun gnus-button-handle-apropos-variable (url)
7455   "Call `apropos' when pushing the corresponding URL button."
7456   (funcall
7457    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7458    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7459
7460 (defun gnus-button-handle-apropos-documentation (url)
7461   "Call `apropos' when pushing the corresponding URL button."
7462   (funcall
7463    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7464    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7465
7466 (defun gnus-button-handle-library (url)
7467   "Call `locate-library' when pushing the corresponding URL button."
7468   (gnus-message 9 "url=`%s'" url)
7469   (let* ((lib (locate-library url))
7470          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7471     (if (not lib)
7472         (gnus-message 1 "Cannot locale library `%s'." url)
7473       (find-file-read-only file))))
7474
7475 (defcustom gnus-button-man-level 5
7476   "*Integer that says how many man-related buttons Gnus will show.
7477 The higher the number, the more buttons will appear and the more false
7478 positives are possible.  Note that you can set this variable local to
7479 specific groups.  Setting it higher in Unix groups is probably a good idea.
7480 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7481 how to set variables in specific groups."
7482   :version "22.1"
7483   :group 'gnus-article-buttons
7484   :link '(custom-manual "(gnus)Group Parameters")
7485   :type 'integer)
7486
7487 (defcustom gnus-button-emacs-level 5
7488   "*Integer that says how many emacs-related buttons Gnus will show.
7489 The higher the number, the more buttons will appear and the more false
7490 positives are possible.  Note that you can set this variable local to
7491 specific groups.  Setting it higher in Emacs or Gnus related groups is
7492 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7493 `gnus-parameters' on how to set variables in specific groups."
7494   :version "22.1"
7495   :group 'gnus-article-buttons
7496   :link '(custom-manual "(gnus)Group Parameters")
7497   :type 'integer)
7498
7499 (defcustom gnus-button-message-level 5
7500   "*Integer that says how many buttons for news or mail messages will appear.
7501 The higher the number, the more buttons will appear and the more false
7502 positives are possible."
7503   ;; mail addresses, MIDs, URLs for news, ...
7504   :version "22.1"
7505   :group 'gnus-article-buttons
7506   :type 'integer)
7507
7508 (defcustom gnus-button-browse-level 5
7509   "*Integer that says how many buttons for browsing will appear.
7510 The higher the number, the more buttons will appear and the more false
7511 positives are possible."
7512   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7513   :version "22.1"
7514   :group 'gnus-article-buttons
7515   :type 'integer)
7516
7517 (defcustom gnus-button-alist
7518   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7519      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7520     ((concat "\\b\\(nntp\\|news\\):\\("
7521              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7522      0 t gnus-button-handle-news 2)
7523     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7524      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7525     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7526      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7527     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7528     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7529      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7530     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7531      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7532     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7533      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7534     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7535      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7536     ;; RFC 2368 (The mailto URL scheme)
7537     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7538      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7539     ("\\bmailto:\\([^ \n\t]+\\)"
7540      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7541     ;; Info Konqueror style <info:/foo/bar baz>.
7542     ;; Must come before " Gnus home-grown style".
7543     ("\\binfo://?\\([^'\">\n\t]+\\)"
7544      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7545    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7546     ("\\binfo://\\([^'\">\n\t ]+\\)"
7547      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7548     ;; Info GNOME style <info:foo#bar_baz>
7549     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7550      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7551     ;; Info KDE style <info:(foo)bar baz>
7552     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7553      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7554     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7555      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7556     ("\\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,]*\\)\\)?"
7557      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7558      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7559     ;; This is custom
7560     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7561      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7562     ;; Emacs help commands
7563     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7564      ;; regexp doesn't match arguments containing ` '.
7565      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7566     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7567      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7568     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7569      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7570     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7571      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7572     ;; The following entries may lead to many false positives so don't enable
7573     ;; them by default (use a high button level).
7574     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7575      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7576      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7577     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7578      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7579     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7580      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7581     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7582      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7583     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7584      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7585     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7586      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7587     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7588      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7589     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7590      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7591     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7592      ;; Unlike the other regexps we really have to require quoting
7593      ;; here to determine where it ends.
7594      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7595     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7596     ("<URL: *\\([^\n<>]*\\)>"
7597      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7598     ;; RFC 2396 (2.4.3., delims) ...
7599     ("\"URL: *\\([^\n\"]*\\)\""
7600      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7601     ;; Raw URLs.
7602     (gnus-button-url-regexp
7603      0 (>= gnus-button-browse-level 0) browse-url 0)
7604     ;; man pages
7605     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7606      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7607      gnus-button-handle-man 1)
7608     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7609     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7610      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7611      gnus-button-handle-man 1)
7612     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7613     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7614     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7615      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7616     ;; Recognizing patches to .el files.  This is somewhat obscure,
7617     ;; but considering the percentage of Gnus users who hack Emacs
7618     ;; Lisp files...
7619     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7620      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7621     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7622      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7623     ;; MID or mail: To avoid too many false positives we don't try to catch
7624     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7625     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7626     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7627     ;; so that non-ambiguous entries (see above) match first.
7628     (gnus-button-mid-or-mail-regexp
7629      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7630   "*Alist of regexps matching buttons in article bodies.
7631
7632 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7633 REGEXP: is the string (case insensitive) matching text around the button (can
7634 also be Lisp expression evaluating to a string),
7635 BUTTON: is the number of the regexp grouping actually matching the button,
7636 FORM: is a Lisp expression which must eval to true for the button to
7637 be added,
7638 CALLBACK: is the function to call when the user push this button, and each
7639 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7640
7641 CALLBACK can also be a variable, in that case the value of that
7642 variable it the real callback function."
7643   :group 'gnus-article-buttons
7644   :type '(repeat (list (choice regexp variable sexp)
7645                        (integer :tag "Button")
7646                        (sexp :tag "Form")
7647                        (function :tag "Callback")
7648                        (repeat :tag "Par"
7649                                :inline t
7650                                (integer :tag "Regexp group")))))
7651 (put 'gnus-button-alist 'risky-local-variable t)
7652
7653 (defcustom gnus-header-button-alist
7654   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7655      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7656     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7657      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7658     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7659      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7660     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7661      0 (>= gnus-button-browse-level 0) browse-url 0)
7662     ("^Subject:" gnus-button-url-regexp
7663      0 (>= gnus-button-browse-level 0) browse-url 0)
7664     ("^[^:]+:" gnus-button-url-regexp
7665      0 (>= gnus-button-browse-level 0) browse-url 0)
7666     ("^OpenPGP:.*url=" gnus-button-url-regexp
7667      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7668     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7669      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7670     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7671      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7672   "*Alist of headers and regexps to match buttons in article heads.
7673
7674 This alist is very similar to `gnus-button-alist', except that each
7675 alist has an additional HEADER element first in each entry:
7676
7677 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7678
7679 HEADER is a regexp to match a header.  For a fuller explanation, see
7680 `gnus-button-alist'."
7681   :group 'gnus-article-buttons
7682   :group 'gnus-article-headers
7683   :type '(repeat (list (regexp :tag "Header")
7684                        (choice regexp variable)
7685                        (integer :tag "Button")
7686                        (sexp :tag "Form")
7687                        (function :tag "Callback")
7688                        (repeat :tag "Par"
7689                                :inline t
7690                                (integer :tag "Regexp group")))))
7691 (put 'gnus-header-button-alist 'risky-local-variable t)
7692
7693 ;;; Commands:
7694
7695 (defun gnus-article-push-button (event)
7696   "Check text under the mouse pointer for a callback function.
7697 If the text under the mouse pointer has a `gnus-callback' property,
7698 call it with the value of the `gnus-data' text property."
7699   (interactive "e")
7700   (set-buffer (window-buffer (posn-window (event-start event))))
7701   (let* ((pos (posn-point (event-start event)))
7702          (data (get-text-property pos 'gnus-data))
7703          (fun (get-text-property pos 'gnus-callback)))
7704     (goto-char pos)
7705     (when fun
7706       (funcall fun data))))
7707
7708 (defun gnus-article-press-button ()
7709   "Check text at point for a callback function.
7710 If the text at point has a `gnus-callback' property,
7711 call it with the value of the `gnus-data' text property."
7712   (interactive)
7713   (let ((data (get-text-property (point) 'gnus-data))
7714         (fun (get-text-property (point) 'gnus-callback)))
7715     (when fun
7716       (funcall fun data))))
7717
7718 (defun gnus-article-highlight (&optional force)
7719   "Highlight current article.
7720 This function calls `gnus-article-highlight-headers',
7721 `gnus-article-highlight-citation',
7722 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7723 do the highlighting.  See the documentation for those functions."
7724   (interactive (list 'force))
7725   (gnus-article-highlight-headers)
7726   (gnus-article-highlight-citation force)
7727   (gnus-article-highlight-signature)
7728   (gnus-article-add-buttons)
7729   (gnus-article-add-buttons-to-head))
7730
7731 (defun gnus-article-highlight-some (&optional force)
7732   "Highlight current article.
7733 This function calls `gnus-article-highlight-headers',
7734 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7735 do the highlighting.  See the documentation for those functions."
7736   (interactive (list 'force))
7737   (gnus-article-highlight-headers)
7738   (gnus-article-highlight-signature)
7739   (gnus-article-add-buttons))
7740
7741 (defun gnus-article-highlight-headers ()
7742   "Highlight article headers as specified by `gnus-header-face-alist'."
7743   (interactive)
7744   (gnus-with-article-headers
7745     (let (regexp header-face field-face from hpoints fpoints)
7746       (dolist (entry gnus-header-face-alist)
7747         (goto-char (point-min))
7748         (setq regexp (concat "^\\("
7749                              (if (string-equal "" (nth 0 entry))
7750                                  "[^\t ]"
7751                                (nth 0 entry))
7752                              "\\)")
7753               header-face (nth 1 entry)
7754               field-face (nth 2 entry))
7755         (while (and (re-search-forward regexp nil t)
7756                     (not (eobp)))
7757           (beginning-of-line)
7758           (setq from (point))
7759           (unless (search-forward ":" nil t)
7760             (forward-char 1))
7761           (when (and header-face
7762                      (not (memq (point) hpoints)))
7763             (push (point) hpoints)
7764             (gnus-put-text-property from (point) 'face header-face))
7765           (when (and field-face
7766                      (not (memq (setq from (point)) fpoints)))
7767             (push from fpoints)
7768             (if (re-search-forward "^[^ \t]" nil t)
7769                 (forward-char -2)
7770               (goto-char (point-max)))
7771             (gnus-put-text-property from (point) 'face field-face)))))))
7772
7773 (defun gnus-article-highlight-signature ()
7774   "Highlight the signature in an article.
7775 It does this by highlighting everything after
7776 `gnus-signature-separator' using the face `gnus-signature'."
7777   (interactive)
7778   (gnus-with-article-buffer
7779     (let ((inhibit-point-motion-hooks t))
7780       (save-restriction
7781         (when (and gnus-signature-face
7782                    (gnus-article-narrow-to-signature))
7783           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7784                             'face gnus-signature-face)
7785           (widen)
7786           (gnus-article-search-signature)
7787           (let ((start (match-beginning 0))
7788                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7789             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7790                                      end)))))))
7791
7792 (defun gnus-button-in-region-p (b e prop)
7793   "Say whether PROP exists in the region."
7794   (text-property-not-all b e prop nil))
7795
7796 (defun gnus-article-add-buttons ()
7797   "Find external references in the article and make buttons of them.
7798 \"External references\" are things like Message-IDs and URLs, as
7799 specified by `gnus-button-alist'."
7800   (interactive)
7801   (gnus-with-article-buffer
7802     (let ((inhibit-point-motion-hooks t)
7803           (case-fold-search t)
7804           (alist gnus-button-alist)
7805           beg entry regexp)
7806       ;; We skip the headers.
7807       (article-goto-body)
7808       (setq beg (point))
7809       (while (setq entry (pop alist))
7810         (setq regexp (eval (car entry)))
7811         (goto-char beg)
7812         (while (re-search-forward regexp nil t)
7813           (let ((start (match-beginning (nth 1 entry)))
7814                 (end (match-end (nth 1 entry)))
7815                 (from (match-beginning 0)))
7816             (when (and (eval (nth 2 entry))
7817                        (not (gnus-button-in-region-p
7818                              start end 'gnus-callback)))
7819               ;; That optional form returned non-nil, so we add the
7820               ;; button.
7821               (setq from (set-marker (make-marker) from))
7822               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7823                            (gnus-article-extend-url-button from start end))
7824                 (gnus-article-add-button start end
7825                                          'gnus-button-push (list from entry))
7826                 (gnus-put-text-property
7827                  start end
7828                  'gnus-string (buffer-substring-no-properties
7829                                start end))))))))))
7830
7831 (defun gnus-article-extend-url-button (beg start end)
7832   "Extend url button if url is folded into two or more lines.
7833 Return non-nil if button is extended.  BEG is a marker that points to
7834 the beginning position of a text containing url.  START and END are
7835 the endpoints of a url button before it is extended.  The concatenated
7836 url is put as the `gnus-button-url' overlay property on the button."
7837   (let ((opoint (point))
7838         (points (list start end))
7839         url delim regexp)
7840     (prog1
7841         (when (and (progn
7842                      (goto-char end)
7843                      (not (looking-at "[\t ]*[\">]")))
7844                    (progn
7845                      (goto-char start)
7846                      (string-match
7847                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7848                       (buffer-substring (point-at-bol) start)))
7849                    (progn
7850                      (setq url (list (buffer-substring start end))
7851                            delim (if (match-beginning 1) ">" "\""))
7852                      (beginning-of-line)
7853                      (setq regexp (concat
7854                                    (when (and (looking-at
7855                                                message-cite-prefix-regexp)
7856                                               (< (match-end 0) start))
7857                                      (regexp-quote (match-string 0)))
7858                                    "\
7859 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7860                                    delim "\\)"))
7861                      (while (progn
7862                               (forward-line 1)
7863                               (and (looking-at regexp)
7864                                    (prog1
7865                                        (match-beginning 1)
7866                                      (push (or (match-string 2)
7867                                                (match-string 1))
7868                                            url)
7869                                      (push (setq end (or (match-end 2)
7870                                                          (match-end 1)))
7871                                            points)
7872                                      (push (or (match-beginning 2)
7873                                                (match-beginning 1))
7874                                            points)))))
7875                      (match-beginning 2)))
7876           (let (gnus-article-mouse-face widget-mouse-face)
7877             (while points
7878               (gnus-article-add-button (pop points) (pop points)
7879                                        'gnus-button-push beg)))
7880           (let ((overlay (gnus-make-overlay start end)))
7881             (gnus-overlay-put overlay 'evaporate t)
7882             (gnus-overlay-put overlay 'gnus-button-url
7883                               (list (mapconcat 'identity (nreverse url) "")))
7884             (when gnus-article-mouse-face
7885               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7886           t)
7887       (goto-char opoint))))
7888
7889 ;; Add buttons to the head of an article.
7890 (defun gnus-article-add-buttons-to-head ()
7891   "Add buttons to the head of the article."
7892   (interactive)
7893   (gnus-with-article-headers
7894     (let (beg end)
7895       (dolist (entry gnus-header-button-alist)
7896         ;; Each alist entry.
7897         (goto-char (point-min))
7898         (while (re-search-forward (car entry) nil t)
7899           ;; Each header matching the entry.
7900           (setq beg (match-beginning 0))
7901           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7902                              (match-beginning 0))
7903                         (point-max)))
7904           (goto-char beg)
7905           (while (re-search-forward (eval (nth 1 entry)) end t)
7906             ;; Each match within a header.
7907             (let* ((entry (cdr entry))
7908                    (start (match-beginning (nth 1 entry)))
7909                    (end (match-end (nth 1 entry)))
7910                    (form (nth 2 entry)))
7911               (goto-char (match-end 0))
7912               (when (eval form)
7913                 (gnus-article-add-button
7914                  start end (nth 3 entry)
7915                  (buffer-substring (match-beginning (nth 4 entry))
7916                                    (match-end (nth 4 entry)))))))
7917           (goto-char end))))))
7918
7919 ;;; External functions:
7920
7921 (defun gnus-article-add-button (from to fun &optional data text)
7922   "Create a button between FROM and TO with callback FUN and data DATA."
7923   (when gnus-article-button-face
7924     (gnus-overlay-put (gnus-make-overlay from to nil t)
7925                       'face gnus-article-button-face))
7926   (gnus-add-text-properties
7927    from to
7928    (nconc (and gnus-article-mouse-face
7929                (list gnus-mouse-face-prop gnus-article-mouse-face))
7930           (list 'gnus-callback fun)
7931           (and data (list 'gnus-data data))))
7932   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7933                          :help-echo (or text "Follow the link")
7934                          :keymap gnus-url-button-map
7935                          :button-keymap gnus-widget-button-keymap))
7936
7937 (defun gnus-article-copy-string ()
7938   "Copy the string in the button to the kill ring."
7939   (interactive)
7940   (gnus-article-check-buffer)
7941   (let ((data (get-text-property (point) 'gnus-string)))
7942     (when data
7943       (with-temp-buffer
7944         (insert data)
7945         (copy-region-as-kill (point-min) (point-max))
7946         (message "Copied %s" data)))))
7947
7948 ;;; Internal functions:
7949
7950 (defun gnus-article-set-globals ()
7951   (with-current-buffer gnus-summary-buffer
7952     (gnus-set-global-variables)))
7953
7954 (defun gnus-signature-toggle (end)
7955   (gnus-with-article-buffer
7956     (let ((inhibit-point-motion-hooks t))
7957       (if (text-property-any end (point-max) 'article-type 'signature)
7958           (progn
7959             (gnus-delete-wash-type 'signature)
7960             (gnus-remove-text-properties-when
7961              'article-type 'signature end (point-max)
7962              (cons 'article-type (cons 'signature
7963                                        gnus-hidden-properties))))
7964         (gnus-add-wash-type 'signature)
7965         (gnus-add-text-properties-when
7966          'article-type nil end (point-max)
7967          (cons 'article-type (cons 'signature
7968                                    gnus-hidden-properties)))))
7969     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7970       (gnus-set-mode-line 'article))))
7971
7972 (defun gnus-button-push (marker-and-entry)
7973   ;; Push button starting at MARKER.
7974   (save-excursion
7975     (let* ((marker (car marker-and-entry))
7976            (entry (cadr marker-and-entry))
7977            (regexp (car entry))
7978            (inhibit-point-motion-hooks t))
7979       (goto-char marker)
7980       ;; This is obviously true, or something bad is happening :)
7981       ;; But we need it to have the match-data
7982       (when (looking-at (or (if (symbolp regexp)
7983                                 (symbol-value regexp)
7984                               regexp)))
7985         (let ((fun (nth 3 entry))
7986               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7987                              (get-char-property marker 'gnus-button-url))
7988                         (mapcar (lambda (group)
7989                                   (let ((string (match-string group)))
7990                                     (set-text-properties
7991                                      0 (length string) nil string)
7992                                     string))
7993                                 (nthcdr 4 entry)))))
7994
7995           (cond
7996            ((fboundp fun)
7997             (apply fun args))
7998            ((and (boundp fun)
7999                  (fboundp (symbol-value fun)))
8000             (apply (symbol-value fun) args))
8001            (t
8002             (gnus-message 1 "You must define `%S' to use this button"
8003                           (cons fun args)))))))))
8004
8005 (defun gnus-parse-news-url (url)
8006   (let (scheme server port group message-id articles)
8007     (with-temp-buffer
8008       (insert url)
8009       (goto-char (point-min))
8010       (when (looking-at "\\([A-Za-z]+\\):")
8011         (setq scheme (match-string 1))
8012         (goto-char (match-end 0)))
8013       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8014         (setq server (match-string 1))
8015         (setq port (if (stringp (match-string 3))
8016                        (string-to-number (match-string 3))
8017                      (match-string 3)))
8018         (goto-char (match-end 0)))
8019
8020       (cond
8021        ((looking-at "\\(.*@.*\\)")
8022         (setq message-id (match-string 1)))
8023        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8024         (setq group (match-string 1)
8025               articles (split-string (match-string 2) "-")))
8026        ((looking-at "\\([^/]+\\)/?")
8027         (setq group (match-string 1)))
8028        (t
8029         (error "Unknown news URL syntax"))))
8030     (list scheme server port group message-id articles)))
8031
8032 (defun gnus-button-handle-news (url)
8033   "Fetch a news URL."
8034   (destructuring-bind (scheme server port group message-id articles)
8035       (gnus-parse-news-url url)
8036     (cond
8037      (message-id
8038       (with-current-buffer gnus-summary-buffer
8039         (if server
8040             (let ((gnus-refer-article-method
8041                    (nconc (list (list 'nntp server))
8042                           gnus-refer-article-method))
8043                   (nntp-port-number (or port "nntp")))
8044               (gnus-message 7 "Fetching %s with %s"
8045                             message-id gnus-refer-article-method)
8046               (gnus-summary-refer-article message-id))
8047           (gnus-summary-refer-article message-id))))
8048      (group
8049       (gnus-button-fetch-group url)))))
8050
8051 (defun gnus-button-patch (library line)
8052   "Visit an Emacs Lisp library LIBRARY on line LINE."
8053   (interactive)
8054   (let ((file (locate-library (file-name-nondirectory library))))
8055     (unless file
8056       (error "Couldn't find library %s" library))
8057     (find-file file)
8058     (goto-char (point-min))
8059     (forward-line (1- (string-to-number line)))))
8060
8061 (defun gnus-button-handle-man (url)
8062   "Fetch a man page."
8063   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8064   (when (eq gnus-button-man-handler 'woman)
8065     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8066   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8067   (funcall gnus-button-man-handler url))
8068
8069 (defun gnus-button-handle-info-url (url)
8070   "Fetch an info URL."
8071   (setq url (mm-subst-char-in-string ?+ ?\  url))
8072   (cond
8073    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8074     (gnus-info-find-node
8075      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8076                      "Gnus")
8077              ")" (gnus-url-unhex-string (match-string 2 url)))))
8078    ((string-match "([^)\"]+)[^\"]+" url)
8079     (setq url
8080           (gnus-replace-in-string
8081            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8082     (gnus-info-find-node url))
8083    (t (error "Can't parse %s" url))))
8084
8085 (defun gnus-button-handle-info-url-gnome (url)
8086   "Fetch GNOME style info URL."
8087   (setq url (mm-subst-char-in-string ?_ ?\  url))
8088   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8089       (gnus-info-find-node
8090        (concat "("
8091                (gnus-url-unhex-string
8092                  (match-string 1 url))
8093                ")"
8094                (or (gnus-url-unhex-string
8095                     (match-string 2 url))
8096                    "Top")))
8097     (error "Can't parse %s" url)))
8098
8099 (defun gnus-button-handle-info-url-kde (url)
8100   "Fetch KDE style info URL."
8101   (gnus-info-find-node (gnus-url-unhex-string url)))
8102
8103 ;; (info) will autoload info.el
8104 (declare-function Info-menu "info" (menu-item &optional fork))
8105 (declare-function Info-index-next "info" (num))
8106
8107 (defun gnus-button-handle-info-keystrokes (url)
8108   "Call `info' when pushing the corresponding URL button."
8109   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8110   (let (node indx comma)
8111     (if (string-match
8112          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8113                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8114                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8115                  "\\(?:[ \t\n,]*\\)\\)?")
8116          url)
8117         (setq node (match-string 2 url)
8118               indx (match-string 3 url))
8119       (error "Can't parse %s" url))
8120     (info)
8121     (Info-directory)
8122     (Info-menu node)
8123     (when (> (length indx) 0)
8124       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8125                             "\\([ \t\n,]*\\)")
8126                     indx)
8127       (setq comma (match-string 2 indx))
8128       (setq indx  (match-string 1 indx))
8129       (Info-index indx)
8130       (when comma
8131         (dotimes (i (with-temp-buffer
8132                       (insert comma)
8133                       ;; Note: the XEmacs version of `how-many' takes
8134                       ;; no optional argument.
8135                       (goto-char (point-min))
8136                       (how-many ",")))
8137           (Info-index-next 1)))
8138       nil)))
8139
8140 (autoload 'pgg-snarf-keys-region "pgg")
8141 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8142 (declare-function pgg-display-output-buffer "pgg" (start end status))
8143
8144 (defun gnus-button-openpgp (url)
8145   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8146   (with-temp-buffer
8147     (mm-url-insert-file-contents-external url)
8148     (pgg-snarf-keys-region (point-min) (point-max))
8149     (pgg-display-output-buffer nil nil nil)))
8150
8151 (defun gnus-button-message-id (message-id)
8152   "Fetch MESSAGE-ID."
8153   (with-current-buffer gnus-summary-buffer
8154     (gnus-summary-refer-article message-id)))
8155
8156 (defun gnus-button-fetch-group (address &rest ignore)
8157   "Fetch GROUP specified by ADDRESS."
8158   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8159                       address)
8160     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8161     ;; for nntp:// and news://
8162     (setq address (match-string 3 address)))
8163   (if (not (string-match "[:/]" address))
8164       ;; This is just a simple group url.
8165       (gnus-group-read-ephemeral-group address gnus-select-method)
8166     (if (not
8167          (string-match
8168           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8169           address))
8170         (error "Can't parse %s" address)
8171       (gnus-group-read-ephemeral-group
8172        (match-string 4 address)
8173        `(nntp ,(match-string 1 address)
8174               (nntp-address ,(match-string 1 address))
8175               (nntp-port-number ,(if (match-end 3)
8176                                      (match-string 3 address)
8177                                    "nntp")))
8178        nil nil nil
8179        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8180
8181 (defun gnus-url-parse-query-string (query &optional downcase)
8182   (let (retval pairs cur key val)
8183     (setq pairs (split-string query "&"))
8184     (while pairs
8185       (setq cur (car pairs)
8186             pairs (cdr pairs))
8187       (if (not (string-match "=" cur))
8188           nil                           ; Grace
8189         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8190               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8191         (if downcase
8192             (setq key (downcase key)))
8193         (setq cur (assoc key retval))
8194         (if cur
8195             (setcdr cur (cons val (cdr cur)))
8196           (setq retval (cons (list key val) retval)))))
8197     retval))
8198
8199 (defun gnus-url-mailto (url)
8200   ;; Send mail to someone
8201   (setq url (replace-regexp-in-string "\n" " " url))
8202   (when (string-match "mailto:/*\\(.*\\)" url)
8203     (setq url (substring url (match-beginning 1) nil)))
8204   (let (to args subject func)
8205     (setq args (gnus-url-parse-query-string
8206                 (if (string-match "^\\?" url)
8207                     (substring url 1)
8208                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8209                       (concat "to=" (match-string 1 url) "&"
8210                               (match-string 2 url))
8211                     (concat "to=" url))))
8212           subject (cdr-safe (assoc "subject" args)))
8213     (gnus-msg-mail)
8214     (while args
8215       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8216       (if (fboundp func)
8217           (funcall func)
8218         (message-position-on-field (caar args)))
8219       (insert (gnus-replace-in-string
8220                (mapconcat 'identity (reverse (cdar args)) ", ")
8221                "\r\n" "\n" t))
8222       (setq args (cdr args)))
8223     (if subject
8224         (message-goto-body)
8225       (message-goto-subject))))
8226
8227 (defun gnus-button-embedded-url (address)
8228   "Activate ADDRESS with `browse-url'."
8229   (browse-url (gnus-strip-whitespace address)))
8230
8231 ;;; Next/prev buttons in the article buffer.
8232
8233 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8234 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8235
8236 (defvar gnus-prev-page-map
8237   (let ((map (make-sparse-keymap)))
8238     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8239     (define-key map "\r" 'gnus-button-prev-page)
8240     map))
8241
8242 (defvar gnus-next-page-map
8243   (let ((map (make-sparse-keymap)))
8244     (define-key map gnus-mouse-2 'gnus-button-next-page)
8245     (define-key map "\r" 'gnus-button-next-page)
8246     map))
8247
8248 (defun gnus-insert-prev-page-button ()
8249   (let ((b (point)) e
8250         (inhibit-read-only t))
8251     (gnus-eval-format
8252      gnus-prev-page-line-format nil
8253      `(keymap ,gnus-prev-page-map
8254               gnus-prev t
8255               gnus-callback gnus-article-button-prev-page
8256               article-type annotation))
8257     (setq e (if (bolp)
8258                 ;; Exclude a newline.
8259                 (1- (point))
8260               (point)))
8261     (when gnus-article-button-face
8262       (gnus-overlay-put (gnus-make-overlay b e nil t)
8263                         'face gnus-article-button-face))
8264     (widget-convert-button
8265      'link b e
8266      :action 'gnus-button-prev-page
8267      :button-keymap gnus-prev-page-map)))
8268
8269 (defun gnus-button-next-page (&optional args more-args)
8270   "Go to the next page."
8271   (interactive)
8272   (let ((win (selected-window)))
8273     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8274     (gnus-article-next-page)
8275     (select-window win)))
8276
8277 (defun gnus-button-prev-page (&optional args more-args)
8278   "Go to the prev page."
8279   (interactive)
8280   (let ((win (selected-window)))
8281     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8282     (gnus-article-prev-page)
8283     (select-window win)))
8284
8285 (defun gnus-insert-next-page-button ()
8286   (let ((b (point)) e
8287         (inhibit-read-only t))
8288     (gnus-eval-format gnus-next-page-line-format nil
8289                       `(keymap ,gnus-next-page-map
8290                                gnus-next t
8291                                gnus-callback gnus-article-button-next-page
8292                                article-type annotation))
8293     (setq e (if (bolp)
8294                 ;; Exclude a newline.
8295                 (1- (point))
8296               (point)))
8297     (when gnus-article-button-face
8298       (gnus-overlay-put (gnus-make-overlay b e nil t)
8299                         'face gnus-article-button-face))
8300     (widget-convert-button
8301      'link b e
8302      :action 'gnus-button-next-page
8303      :button-keymap gnus-next-page-map)))
8304
8305 (defun gnus-article-button-next-page (arg)
8306   "Go to the next page."
8307   (interactive "P")
8308   (let ((win (selected-window)))
8309     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8310     (gnus-article-next-page)
8311     (select-window win)))
8312
8313 (defun gnus-article-button-prev-page (arg)
8314   "Go to the prev page."
8315   (interactive "P")
8316   (let ((win (selected-window)))
8317     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8318     (gnus-article-prev-page)
8319     (select-window win)))
8320
8321 (defvar gnus-decode-header-methods
8322   '(mail-decode-encoded-word-region)
8323   "List of methods used to decode headers.
8324
8325 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8326 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8327 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8328 whose names match REGEXP.
8329
8330 For example:
8331 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8332  mail-decode-encoded-word-region
8333  (\"chinese\" . rfc1843-decode-region))
8334 ")
8335
8336 (defvar gnus-decode-header-methods-cache nil)
8337
8338 (defun gnus-multi-decode-header (start end)
8339   "Apply the functions from `gnus-encoded-word-methods' that match."
8340   (unless (and gnus-decode-header-methods-cache
8341                (eq gnus-newsgroup-name
8342                    (car gnus-decode-header-methods-cache)))
8343     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8344     (dolist (x gnus-decode-header-methods)
8345       (if (symbolp x)
8346           (nconc gnus-decode-header-methods-cache (list x))
8347         (if (and gnus-newsgroup-name
8348                  (string-match (car x) gnus-newsgroup-name))
8349             (nconc gnus-decode-header-methods-cache
8350                    (list (cdr x)))))))
8351   (let ((xlist gnus-decode-header-methods-cache))
8352     (pop xlist)
8353     (save-restriction
8354       (narrow-to-region start end)
8355       (while xlist
8356         (funcall (pop xlist) (point-min) (point-max))))))
8357
8358 ;;;
8359 ;;; Treatment top-level handling.
8360 ;;;
8361
8362 (defvar gnus-inhibit-article-treatments nil)
8363
8364 (defun gnus-treat-article (gnus-treat-condition
8365                            &optional part-number total-parts gnus-treat-type)
8366   (let ((gnus-treat-length (- (point-max) (point-min)))
8367         (alist gnus-treatment-function-alist)
8368         (article-goto-body-goes-to-point-min-p t)
8369         (treated-type
8370          (or (not gnus-treat-type)
8371              (catch 'found
8372                (let ((list gnus-article-treat-types))
8373                  (while list
8374                    (when (string-match (pop list) gnus-treat-type)
8375                      (throw 'found t)))))))
8376         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8377         val elem)
8378     (gnus-run-hooks 'gnus-part-display-hook)
8379     (dolist (elem alist)
8380       (setq val
8381             (save-excursion
8382               (when (gnus-buffer-live-p gnus-summary-buffer)
8383                 (set-buffer gnus-summary-buffer))
8384               (symbol-value (car elem))))
8385       (when (and (or (consp val)
8386                      treated-type)
8387                  (or (not gnus-inhibit-article-treatments)
8388                      (eq gnus-treat-condition 'head))
8389                  (gnus-treat-predicate val)
8390                  (or (not (get (car elem) 'highlight))
8391                      highlightp))
8392         (save-restriction
8393           (funcall (cadr elem)))))))
8394
8395 ;; Dynamic variables.
8396 (defvar part-number)
8397 (defvar total-parts)
8398 (defvar gnus-treat-type)
8399 (defvar gnus-treat-condition)
8400 (defvar gnus-treat-length)
8401
8402 (defun gnus-treat-predicate (val)
8403   (cond
8404    ((null val)
8405     nil)
8406    (gnus-treat-condition
8407     (eq gnus-treat-condition val))
8408    ((and (listp val)
8409          (stringp (car val)))
8410     (apply 'gnus-or (mapcar `(lambda (s)
8411                                (string-match s ,(or gnus-newsgroup-name "")))
8412                             val)))
8413    ((listp val)
8414     (let ((pred (pop val)))
8415       (cond
8416        ((eq pred 'or)
8417         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8418        ((eq pred 'and)
8419         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8420        ((eq pred 'not)
8421         (not (gnus-treat-predicate (car val))))
8422        ((eq pred 'typep)
8423         (equal (car val) gnus-treat-type))
8424        (t
8425         (error "%S is not a valid predicate" pred)))))
8426    ((eq val t)
8427     t)
8428    ((eq val 'head)
8429     nil)
8430    ((eq val 'first)
8431     (eq part-number 1))
8432    ((eq val 'last)
8433     (eq part-number total-parts))
8434    ((numberp val)
8435     (< gnus-treat-length val))
8436    (t
8437     (error "%S is not a valid value" val))))
8438
8439 (defun gnus-article-encrypt-body (protocol &optional n)
8440   "Encrypt the article body."
8441   (interactive
8442    (list
8443     (or gnus-article-encrypt-protocol
8444         (gnus-completing-read "Encrypt protocol"
8445                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8446                               t))
8447     current-prefix-arg))
8448   ;; User might hit `K E' instead of `K e', so prompt once.
8449   (when (and gnus-article-encrypt-protocol
8450              gnus-novice-user)
8451     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8452       (error "Encrypt aborted")))
8453   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8454     (unless func
8455       (error "Can't find the encrypt protocol %s" protocol))
8456     (if (member gnus-newsgroup-name '("nndraft:delayed"
8457                                       "nndraft:drafts"
8458                                       "nndraft:queue"))
8459         (error "Can't encrypt the article in group %s"
8460                gnus-newsgroup-name))
8461     (gnus-summary-iterate n
8462       (with-current-buffer gnus-summary-buffer
8463         (let ((mail-parse-charset gnus-newsgroup-charset)
8464               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8465               (summary-buffer gnus-summary-buffer)
8466               references point)
8467           (gnus-set-global-variables)
8468           (when (gnus-group-read-only-p)
8469             (error "The current newsgroup does not support article encrypt"))
8470           (gnus-summary-show-article t)
8471           (setq references
8472               (or (mail-header-references gnus-current-headers) ""))
8473           (set-buffer gnus-article-buffer)
8474           (let* ((inhibit-read-only t)
8475                  (headers
8476                   (mapcar (lambda (field)
8477                             (and (save-restriction
8478                                    (message-narrow-to-head)
8479                                    (goto-char (point-min))
8480                                    (search-forward field nil t))
8481                                  (prog2
8482                                      (message-narrow-to-field)
8483                                      (buffer-string)
8484                                    (delete-region (point-min) (point-max))
8485                                    (widen))))
8486                           '("Content-Type:" "Content-Transfer-Encoding:"
8487                             "Content-Disposition:"))))
8488             (message-narrow-to-head)
8489             (message-remove-header "MIME-Version")
8490             (goto-char (point-max))
8491             (setq point (point))
8492             (insert (apply 'concat headers))
8493             (widen)
8494             (narrow-to-region point (point-max))
8495             (let ((message-options message-options))
8496               (message-options-set 'message-sender user-mail-address)
8497               (message-options-set 'message-recipients user-mail-address)
8498               (message-options-set 'message-sign-encrypt 'not)
8499               (funcall func))
8500             (goto-char (point-min))
8501             (insert "MIME-Version: 1.0\n")
8502             (widen)
8503             (gnus-summary-edit-article-done
8504              references nil summary-buffer t))
8505           (when gnus-keep-backlog
8506             (gnus-backlog-remove-article
8507              (car gnus-article-current) (cdr gnus-article-current)))
8508           (gnus-flush-original-article-buffer)
8509           (when gnus-use-cache
8510             (gnus-cache-update-article
8511              (car gnus-article-current) (cdr gnus-article-current))))))))
8512
8513 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8514   "The following specs can be used:
8515 %t  The security MIME type
8516 %i  Additional info
8517 %d  Details
8518 %D  Details if button is pressed")
8519
8520 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8521   "The following specs can be used:
8522 %t  The security MIME type
8523 %i  Additional info
8524 %d  Details
8525 %D  Details if button is pressed")
8526
8527 (defvar gnus-mime-security-button-line-format-alist
8528   '((?t gnus-tmp-type ?s)
8529     (?i gnus-tmp-info ?s)
8530     (?d gnus-tmp-details ?s)
8531     (?D gnus-tmp-pressed-details ?s)))
8532
8533 (defvar gnus-mime-security-button-commands
8534   '((gnus-article-press-button "\r" "Show Detail")
8535     (undefined "v")
8536     (undefined "t")
8537     (undefined "C")
8538     (gnus-mime-security-save-part "o" "Save...")
8539     (undefined "\C-o")
8540     (undefined "r")
8541     (undefined "d")
8542     (undefined "c")
8543     (undefined "i")
8544     (undefined "E")
8545     (undefined "e")
8546     (undefined "p")
8547     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8548     (undefined ".")))
8549
8550 (defvar gnus-mime-security-button-map
8551   (let ((map (make-sparse-keymap)))
8552     (define-key map gnus-mouse-2 'gnus-article-push-button)
8553     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8554     (dolist (c gnus-mime-security-button-commands)
8555       (define-key map (cadr c) (car c)))
8556     map))
8557
8558 (easy-menu-define
8559   gnus-mime-security-button-menu gnus-mime-security-button-map
8560   "Security button menu."
8561   `("Security Part"
8562     ,@(delq nil
8563             (mapcar (lambda (c)
8564                       (unless (eq (car c) 'undefined)
8565                         (vector (caddr c) (car c) :active t)))
8566                     gnus-mime-security-button-commands))))
8567
8568 (defun gnus-mime-security-button-menu (event prefix)
8569   "Construct a context-sensitive menu of security commands."
8570   (interactive "e\nP")
8571   (save-window-excursion
8572     (let ((pos (event-start event)))
8573       (select-window (posn-window pos))
8574       (goto-char (posn-point pos))
8575       (gnus-article-check-buffer)
8576       (popup-menu gnus-mime-security-button-menu nil prefix))))
8577
8578 (defvar gnus-mime-security-details-buffer nil)
8579
8580 (defvar gnus-mime-security-button-pressed nil)
8581
8582 (defvar gnus-mime-security-show-details-inline t
8583   "If non-nil, show details in the article buffer.")
8584
8585 (defun gnus-mime-security-verify-or-decrypt (handle)
8586   (mm-remove-parts (cdr handle))
8587   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8588         point (inhibit-read-only t))
8589     (if region
8590         (goto-char (car region)))
8591     (setq point (point))
8592     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8593       (let* ((mm-verify-option 'known)
8594              (mm-decrypt-option 'known)
8595              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8596         (unless (eq nparts (cdr handle))
8597           (mm-destroy-parts (cdr handle))
8598           (setcdr handle nparts))))
8599     (gnus-mime-display-security handle)
8600     (when region
8601       (delete-region (point) (cdr region))
8602       (set-marker (car region) nil)
8603       (set-marker (cdr region) nil))
8604     (goto-char point)))
8605
8606 (defun gnus-mime-security-show-details (handle)
8607   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8608     (if (not details)
8609         (gnus-message 5 "No details.")
8610       (if gnus-mime-security-show-details-inline
8611           (let ((gnus-mime-security-button-pressed
8612                  (not (get-text-property (point) 'gnus-mime-details)))
8613                 (gnus-mime-security-button-line-format
8614                  (get-text-property (point) 'gnus-line-format))
8615                 (inhibit-read-only t))
8616             (forward-char -1)
8617             (while (eq (get-text-property (point) 'gnus-line-format)
8618                        gnus-mime-security-button-line-format)
8619               (forward-char -1))
8620             (forward-char)
8621             (save-restriction
8622               (narrow-to-region (point) (point))
8623               (gnus-insert-mime-security-button handle))
8624             (delete-region (point)
8625                            (or (text-property-not-all
8626                                 (point) (point-max)
8627                                 'gnus-line-format
8628                                 gnus-mime-security-button-line-format)
8629                                (point-max))))
8630         ;; Not inlined.
8631         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8632             (with-current-buffer gnus-mime-security-details-buffer
8633               (erase-buffer)
8634               t)
8635           (setq gnus-mime-security-details-buffer
8636                 (gnus-get-buffer-create "*MIME Security Details*")))
8637         (with-current-buffer gnus-mime-security-details-buffer
8638           (insert details)
8639           (goto-char (point-min)))
8640         (pop-to-buffer gnus-mime-security-details-buffer)))))
8641
8642 (defun gnus-mime-security-press-button (handle)
8643   (save-excursion
8644     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8645         (gnus-mime-security-show-details handle)
8646       (gnus-mime-security-verify-or-decrypt handle))))
8647
8648 (defun gnus-insert-mime-security-button (handle &optional displayed)
8649   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8650          (gnus-tmp-type
8651           (concat
8652            (or (nth 2 (assoc protocol mm-verify-function-alist))
8653                (nth 2 (assoc protocol mm-decrypt-function-alist))
8654                "Unknown")
8655            (if (equal (car handle) "multipart/signed")
8656                " Signed" " Encrypted")
8657            " Part"))
8658          (gnus-tmp-info
8659           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8660               "Undecided"))
8661          (gnus-tmp-details
8662           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8663          gnus-tmp-pressed-details
8664          b e)
8665     (setq gnus-tmp-details
8666           (if gnus-tmp-details
8667               (concat "\n" gnus-tmp-details)
8668             ""))
8669     (setq gnus-tmp-pressed-details
8670           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8671     (unless (bolp)
8672       (insert "\n"))
8673     (setq b (point))
8674     (gnus-eval-format
8675      gnus-mime-security-button-line-format
8676      gnus-mime-security-button-line-format-alist
8677      `(keymap ,gnus-mime-security-button-map
8678          gnus-callback gnus-mime-security-press-button
8679          gnus-line-format ,gnus-mime-security-button-line-format
8680          gnus-mime-details ,gnus-mime-security-button-pressed
8681          article-type annotation
8682          gnus-data ,handle))
8683     (setq e (if (bolp)
8684                 ;; Exclude a newline.
8685                 (1- (point))
8686               (point)))
8687     (when gnus-article-button-face
8688       (gnus-overlay-put (gnus-make-overlay b e nil t)
8689                         'face gnus-article-button-face))
8690     (widget-convert-button
8691      'link b e
8692      :mime-handle handle
8693      :action 'gnus-widget-press-button
8694      :button-keymap gnus-mime-security-button-map
8695      :help-echo
8696      (lambda (widget)
8697        ;; Needed to properly clear the message due to a bug in
8698        ;; wid-edit (XEmacs only).
8699        (when (boundp 'help-echo-owns-message)
8700          (setq help-echo-owns-message t))
8701        (format
8702         "%S: show detail; %S: more options"
8703         (aref gnus-mouse-2 0)
8704         (aref gnus-down-mouse-3 0))))))
8705
8706 (defun gnus-mime-display-security (handle)
8707   (save-restriction
8708     (narrow-to-region (point) (point))
8709     (unless (gnus-unbuttonized-mime-type-p (car handle))
8710       (gnus-insert-mime-security-button handle))
8711     (gnus-mime-display-part (cadr handle))
8712     (unless (bolp)
8713       (insert "\n"))
8714     (unless (gnus-unbuttonized-mime-type-p (car handle))
8715       (let ((gnus-mime-security-button-line-format
8716              gnus-mime-security-button-end-line-format))
8717         (gnus-insert-mime-security-button handle)))
8718     (mm-set-handle-multipart-parameter
8719      handle 'gnus-region
8720      (cons (set-marker (make-marker) (point-min))
8721            (set-marker (make-marker) (point-max))))
8722     (goto-char (point-max))))
8723
8724 (defun gnus-mime-security-run-function (function)
8725   "Run FUNCTION with the security part under point."
8726   (gnus-article-check-buffer)
8727   (let ((data (get-text-property (point) 'gnus-data))
8728         buffer handle)
8729     (when (and (stringp (car-safe data))
8730                (setq buffer (mm-handle-multipart-original-buffer data))
8731                (setq handle (cadr data)))
8732       (if (bufferp (mm-handle-buffer handle))
8733           (progn
8734             (setq handle (cons buffer (copy-sequence (cdr handle))))
8735             (mm-handle-set-undisplayer handle nil))
8736         (setq handle (mm-make-handle
8737                       buffer
8738                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8739                       nil nil nil nil nil nil)))
8740       (funcall function handle))))
8741
8742 (defun gnus-mime-security-save-part ()
8743   "Save the security part under point."
8744   (interactive)
8745   (gnus-mime-security-run-function 'mm-save-part))
8746
8747 (defun gnus-mime-security-pipe-part ()
8748   "Pipe the security part under point to a process."
8749   (interactive)
8750   (gnus-mime-security-run-function 'mm-pipe-part))
8751
8752 (gnus-ems-redefine)
8753
8754 (provide 'gnus-art)
8755
8756 (run-hooks 'gnus-art-load-hook)
8757
8758 ;;; gnus-art.el ends here