Merge remote-tracking 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 header)))))
2235             (with-temp-buffer
2236               (insert header)
2237               (goto-char (point-min))
2238               (while (re-search-forward "\n[\t ]" nil t)
2239                 (replace-match " " t t)))
2240             (setq length (- (point-max) (point-min) 1))
2241             (when (or unfoldable
2242                       (< length (window-width)))
2243               (while (re-search-forward "\n[\t ]" nil t)
2244                 (replace-match " " t t))))
2245           (goto-char (point-max)))))))
2246
2247 (defun gnus-article-treat-fold-headers ()
2248   "Fold message headers."
2249   (interactive)
2250   (gnus-with-article-headers
2251     (while (not (eobp))
2252       (save-restriction
2253         (mail-header-narrow-to-field)
2254         (mail-header-fold-field)
2255         (goto-char (point-max))))))
2256
2257 (defun gnus-treat-smiley ()
2258   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2259   (interactive)
2260   (gnus-with-article-buffer
2261     (if (memq 'smiley gnus-article-wash-types)
2262         (gnus-delete-images 'smiley)
2263       (article-goto-body)
2264       (let ((images (smiley-region (point) (point-max))))
2265         (when images
2266           (gnus-add-wash-type 'smiley)
2267           (dolist (image images)
2268             (gnus-add-image 'smiley image)))))))
2269
2270 (defun gnus-article-remove-images ()
2271   "Remove all images from the article buffer."
2272   (interactive)
2273   (gnus-with-article-buffer
2274     (save-restriction
2275       (widen)
2276       (dolist (elem gnus-article-image-alist)
2277         (gnus-delete-images (car elem))))))
2278
2279 (autoload 'w3m-toggle-inline-images "w3m")
2280
2281 (defun gnus-article-show-images ()
2282   "Show any images that are in the HTML-rendered article buffer.
2283 This only works if the article in question is HTML."
2284   (interactive)
2285   (gnus-with-article-buffer
2286     (save-restriction
2287       (widen)
2288       (if (eq mm-text-html-renderer 'w3m)
2289           (let ((mm-inline-text-html-with-images nil))
2290             (w3m-toggle-inline-images))
2291         (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2292                                                         'image-displayer))
2293           (destructuring-bind (start end function) region
2294             (funcall function (get-text-property start 'image-url)
2295                      start end)))))))
2296
2297 (defun gnus-article-treat-fold-newsgroups ()
2298   "Unfold folded message headers.
2299 Only the headers that fit into the current window width will be
2300 unfolded."
2301   (interactive)
2302   (gnus-with-article-headers
2303     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2304       (save-restriction
2305         (mail-header-narrow-to-field)
2306         (while (re-search-forward ", *" nil t)
2307           (replace-match ", " t t))
2308         (mail-header-fold-field)
2309         (goto-char (point-max))))))
2310
2311 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2312   "Value of `truncate-lines' in Gnus Article buffer.
2313 Valid values are nil, t, `head', `first', `last', an integer or a
2314 predicate.  See Info node `(gnus)Customizing Articles'."
2315   :version "23.1" ;; No Gnus
2316   :group 'gnus-article
2317   ;; :link '(custom-manual "(gnus)Customizing Articles")
2318   :type 'boolean)
2319
2320 (defun gnus-article-toggle-truncate-lines (&optional arg)
2321   "Toggle whether to fold or truncate long lines in article the buffer.
2322 If ARG is non-nil and not a number, toggle
2323 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2324 long lines if and only if arg is positive."
2325   (interactive "P")
2326   (cond
2327    ((and (numberp arg) (> arg 0))
2328     (setq gnus-article-truncate-lines t))
2329    ((numberp arg)
2330     (setq gnus-article-truncate-lines nil))
2331    (arg
2332     (setq gnus-article-truncate-lines
2333           (not gnus-article-truncate-lines))))
2334   (gnus-with-article-buffer
2335     (cond
2336      ((and (numberp arg) (> arg 0))
2337       (setq truncate-lines nil))
2338      ((numberp arg)
2339       (setq truncate-lines t)))
2340     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2341     ;; `toggle-truncate-lines' needs an argument.
2342     (toggle-truncate-lines)))
2343
2344 (defun gnus-article-treat-body-boundary ()
2345   "Place a boundary line at the end of the headers."
2346   (interactive)
2347   (when (and gnus-body-boundary-delimiter
2348              (> (length gnus-body-boundary-delimiter) 0))
2349     (gnus-with-article-headers
2350       (goto-char (point-max))
2351       (let ((start (point)))
2352         (insert "X-Boundary: ")
2353         (gnus-add-text-properties start (point) '(invisible t intangible t))
2354        (insert (let (str (max (window-width)))
2355                  (if (featurep 'xemacs)
2356                      (setq max (1- max)))
2357                  (while (>= max (length str))
2358                     (setq str (concat str gnus-body-boundary-delimiter)))
2359                  (substring str 0 max))
2360                 "\n")
2361         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2362
2363 (defun article-fill-long-lines ()
2364   "Fill lines that are wider than the window width."
2365   (interactive)
2366   (save-excursion
2367     (let ((inhibit-read-only t)
2368           (width (window-width (get-buffer-window (current-buffer)))))
2369       (save-restriction
2370         (article-goto-body)
2371         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2372           (while (not (eobp))
2373             (end-of-line)
2374             (when (>= (current-column) (min fill-column width))
2375               (narrow-to-region (min (1+ (point)) (point-max))
2376                                 (point-at-bol))
2377               (let ((goback (point-marker)))
2378                 (fill-paragraph nil)
2379                 (goto-char (marker-position goback)))
2380               (widen))
2381             (forward-line 1)))))))
2382
2383 (defun article-capitalize-sentences ()
2384   "Capitalize the first word in each sentence."
2385   (interactive)
2386   (save-excursion
2387     (let ((inhibit-read-only t)
2388           (paragraph-start "^[\n\^L]"))
2389       (article-goto-body)
2390       (while (not (eobp))
2391         (capitalize-word 1)
2392         (forward-sentence)))))
2393
2394 (defun article-remove-cr ()
2395   "Remove trailing CRs and then translate remaining CRs into LFs."
2396   (interactive)
2397   (save-excursion
2398     (let ((inhibit-read-only t))
2399       (goto-char (point-min))
2400       (while (re-search-forward "\r+$" nil t)
2401         (replace-match "" t t))
2402       (goto-char (point-min))
2403       (while (search-forward "\r" nil t)
2404         (replace-match "\n" t t)))))
2405
2406 (defun article-remove-trailing-blank-lines ()
2407   "Remove all trailing blank lines from the article."
2408   (interactive)
2409   (save-excursion
2410     (let ((inhibit-read-only t))
2411       (goto-char (point-max))
2412       (delete-region
2413        (point)
2414        (progn
2415          (while (and (not (bobp))
2416                      (looking-at "^[ \t]*$")
2417                      (not (gnus-annotation-in-region-p
2418                            (point) (point-at-eol))))
2419            (forward-line -1))
2420          (forward-line 1)
2421          (point))))))
2422
2423 (defvar gnus-face-properties-alist)
2424
2425 (defun article-display-face (&optional force)
2426   "Display any Face headers in the header."
2427   (interactive (list 'force))
2428   (let ((wash-face-p buffer-read-only))
2429     (gnus-with-article-headers
2430       ;; When displaying parts, this function can be called several times on
2431       ;; the same article, without any intended toggle semantic (as typing `W
2432       ;; D d' would have). So face deletion must occur only when we come from
2433       ;; an interactive command, that is when the *Article* buffer is
2434       ;; read-only.
2435       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2436           (gnus-delete-images 'face)
2437         (let ((from (message-fetch-field "from"))
2438               face faces)
2439           (save-current-buffer
2440             (when (and wash-face-p
2441                        (gnus-buffer-live-p gnus-original-article-buffer)
2442                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2443               (set-buffer gnus-original-article-buffer))
2444             (save-restriction
2445               (mail-narrow-to-head)
2446               (when (or force
2447                         ;; Check whether this face is censored.
2448                         (not (and gnus-article-x-face-too-ugly
2449                                   (or from
2450                                       (setq from (message-fetch-field "from")))
2451                                   (string-match gnus-article-x-face-too-ugly
2452                                                 from))))
2453                 (while (gnus-article-goto-header "Face")
2454                   (push (mail-header-field-value) faces)))))
2455           (when faces
2456             (goto-char (point-min))
2457             (let (png image)
2458               (unless (setq from (gnus-article-goto-header "from"))
2459                 (insert "From:")
2460                 (setq from (point))
2461                 (insert " [no `from' set]\n"))
2462               (while faces
2463                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2464                   (setq image
2465                         (apply 'gnus-create-image png 'png t
2466                                (cdr (assq 'png gnus-face-properties-alist))))
2467                   (goto-char from)
2468                   (gnus-add-wash-type 'face)
2469                   (gnus-add-image 'face image)
2470                   (gnus-put-image image nil 'face))))))))))
2471
2472 (defun article-display-x-face (&optional force)
2473   "Look for an X-Face header and display it if present."
2474   (interactive (list 'force))
2475   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2476     (gnus-with-article-headers
2477       ;; Delete the old process, if any.
2478       (when (process-status "article-x-face")
2479         (delete-process "article-x-face"))
2480       ;; See the comment in `article-display-face'.
2481       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2482           ;; We have already displayed X-Faces, so we remove them
2483           ;; instead.
2484           (gnus-delete-images 'xface)
2485         ;; Display X-Faces.
2486         (let ((from (message-fetch-field "from"))
2487               x-faces face)
2488           (save-current-buffer
2489             (when (and wash-face-p
2490                        (gnus-buffer-live-p gnus-original-article-buffer)
2491                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2492               ;; If type `W f', use gnus-original-article-buffer,
2493               ;; otherwise use the current buffer because displaying
2494               ;; RFC822 parts calls this function too.
2495               (set-buffer gnus-original-article-buffer))
2496             (save-restriction
2497               (mail-narrow-to-head)
2498               (and gnus-article-x-face-command
2499                    (or force
2500                        ;; Check whether this face is censored.
2501                        (not (and gnus-article-x-face-too-ugly
2502                                  (or from
2503                                      (setq from (message-fetch-field "from")))
2504                                  (string-match gnus-article-x-face-too-ugly
2505                                                from))))
2506                    (while (gnus-article-goto-header "X-Face")
2507                      (push (mail-header-field-value) x-faces)))))
2508           (when x-faces
2509             ;; We display the face.
2510             (cond ((functionp gnus-article-x-face-command)
2511                    ;; The command is a lisp function, so we call it.
2512                    (mapc gnus-article-x-face-command x-faces))
2513                   ((stringp gnus-article-x-face-command)
2514                    ;; The command is a string, so we interpret the command
2515                    ;; as a, well, command, and fork it off.
2516                    (let ((process-connection-type nil))
2517                      (gnus-set-process-query-on-exit-flag
2518                       (start-process
2519                        "article-x-face" nil shell-file-name
2520                        shell-command-switch gnus-article-x-face-command)
2521                       nil)
2522                      ;; Sending multiple EOFs to xv doesn't work,
2523                      ;; so we only do a single external face.
2524                      (with-temp-buffer
2525                        (insert (car x-faces))
2526                        (process-send-region "article-x-face"
2527                                             (point-min) (point-max)))
2528                      (process-send-eof "article-x-face")))
2529                   (t
2530                    (error "`%s' set to `%s' is not a function"
2531                           gnus-article-x-face-command
2532                           'gnus-article-x-face-command)))))))))
2533
2534 (defun article-decode-mime-words ()
2535   "Decode all MIME-encoded words in the article."
2536   (interactive)
2537   (gnus-with-article-buffer
2538     (let ((inhibit-point-motion-hooks t)
2539           (mail-parse-charset gnus-newsgroup-charset)
2540           (mail-parse-ignored-charsets
2541            (with-current-buffer gnus-summary-buffer
2542              gnus-newsgroup-ignored-charsets)))
2543       (mail-decode-encoded-word-region (point-min) (point-max)))))
2544
2545 (defun article-decode-charset (&optional prompt)
2546   "Decode charset-encoded text in the article.
2547 If PROMPT (the prefix), prompt for a coding system to use."
2548   (interactive "P")
2549   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2550         (inhibit-read-only t)
2551         (mail-parse-charset gnus-newsgroup-charset)
2552         (mail-parse-ignored-charsets
2553          (save-excursion (condition-case nil
2554                              (set-buffer gnus-summary-buffer)
2555                            (error))
2556                          gnus-newsgroup-ignored-charsets))
2557         ct cte ctl charset format)
2558     (save-excursion
2559       (save-restriction
2560         (article-narrow-to-head)
2561         (setq ct (message-fetch-field "Content-Type" t)
2562               cte (message-fetch-field "Content-Transfer-Encoding" t)
2563               ctl (and ct (mail-header-parse-content-type ct))
2564               charset (cond
2565                        (prompt
2566                         (mm-read-coding-system "Charset to decode: "))
2567                        (ctl
2568                         (mail-content-type-get ctl 'charset)))
2569               format (and ctl (mail-content-type-get ctl 'format)))
2570         (when cte
2571           (setq cte (mail-header-strip cte)))
2572         (if (and ctl (not (string-match "/" (car ctl))))
2573             (setq ctl nil))
2574         (goto-char (point-max)))
2575       (forward-line 1)
2576       (save-restriction
2577         (narrow-to-region (point) (point-max))
2578         (when (and (eq mail-parse-charset 'gnus-decoded)
2579                    (eq (mm-body-7-or-8) '8bit))
2580           ;; The text code could have been decoded.
2581           (setq charset mail-parse-charset))
2582         (when (and (or (not ctl)
2583                        (equal (car ctl) "text/plain"))
2584                    (not format)) ;; article with format will decode later.
2585           (mm-decode-body
2586            charset (and cte (intern (downcase
2587                                      (gnus-strip-whitespace cte))))
2588            (car ctl)))))))
2589
2590 (defun article-decode-encoded-words ()
2591   "Remove encoded-word encoding from headers."
2592   (let ((inhibit-point-motion-hooks t)
2593         (mail-parse-charset gnus-newsgroup-charset)
2594         (mail-parse-ignored-charsets
2595          (save-excursion (condition-case nil
2596                              (set-buffer gnus-summary-buffer)
2597                            (error))
2598                          gnus-newsgroup-ignored-charsets))
2599         (inhibit-read-only t)
2600         end start)
2601     (goto-char (point-min))
2602     (when (search-forward "\n\n" nil 'move)
2603       (forward-line -1))
2604     (setq end (point))
2605     (while (not (bobp))
2606       (while (progn
2607                (forward-line -1)
2608                (and (not (bobp))
2609                     (memq (char-after) '(?\t ? )))))
2610       (setq start (point))
2611       (if (looking-at "\
2612 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2613 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2614           (funcall gnus-decode-address-function start end)
2615         (funcall gnus-decode-header-function start end))
2616       (goto-char (setq end start)))))
2617
2618 (defun article-decode-group-name ()
2619   "Decode group names in Newsgroups, Followup-To and Xref headers."
2620   (let ((inhibit-point-motion-hooks t)
2621         (inhibit-read-only t)
2622         (method (gnus-find-method-for-group gnus-newsgroup-name))
2623         regexp)
2624     (when (and (or gnus-group-name-charset-method-alist
2625                    gnus-group-name-charset-group-alist)
2626                (gnus-buffer-live-p gnus-original-article-buffer))
2627       (save-restriction
2628         (article-narrow-to-head)
2629         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2630           (with-current-buffer gnus-original-article-buffer
2631             (goto-char (point-min)))
2632           (setq regexp (concat "^" header
2633                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2634           (while (re-search-forward regexp nil t)
2635             (replace-match (save-match-data
2636                              (gnus-decode-newsgroups
2637                               ;; XXX how to use data in article buffer?
2638                               (with-current-buffer gnus-original-article-buffer
2639                                 (re-search-forward regexp nil t)
2640                                 (match-string 1))
2641                               gnus-newsgroup-name method))
2642                            t t nil 1))
2643           (goto-char (point-min)))))))
2644
2645 (autoload 'idna-to-unicode "idna")
2646
2647 (defun article-decode-idna-rhs ()
2648   "Decode IDNA strings in RHS in various headers in current buffer.
2649 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2650 Mail-Reply-To: and Mail-Followup-To:."
2651   (when gnus-use-idna
2652     (save-restriction
2653       (let ((inhibit-point-motion-hooks t)
2654             (inhibit-read-only t))
2655         (article-narrow-to-head)
2656         (goto-char (point-min))
2657         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2658           (let (ace unicode)
2659             (when (save-match-data
2660                     (and (setq ace (match-string 1))
2661                          (save-excursion
2662                            (and (re-search-backward "^[^ \t]" nil t)
2663                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2664                          (setq unicode (idna-to-unicode ace))))
2665               (unless (string= ace unicode)
2666                 (replace-match unicode nil nil nil 1)))))))))
2667
2668 (defun article-de-quoted-unreadable (&optional force read-charset)
2669   "Translate a quoted-printable-encoded article.
2670 If FORCE, decode the article whether it is marked as quoted-printable
2671 or not.
2672 If READ-CHARSET, ask for a coding system."
2673   (interactive (list 'force current-prefix-arg))
2674   (save-excursion
2675     (let ((inhibit-read-only t) type charset)
2676       (if (gnus-buffer-live-p gnus-original-article-buffer)
2677           (with-current-buffer gnus-original-article-buffer
2678             (setq type
2679                   (gnus-fetch-field "content-transfer-encoding"))
2680             (let* ((ct (gnus-fetch-field "content-type"))
2681                    (ctl (and ct (mail-header-parse-content-type ct))))
2682               (setq charset (and ctl
2683                                  (mail-content-type-get ctl 'charset)))
2684               (if (stringp charset)
2685                   (setq charset (intern (downcase charset)))))))
2686       (if read-charset
2687           (setq charset (mm-read-coding-system "Charset: " charset)))
2688       (unless charset
2689         (setq charset gnus-newsgroup-charset))
2690       (when (or force
2691                 (and type (let ((case-fold-search t))
2692                             (string-match "quoted-printable" type))))
2693         (article-goto-body)
2694         (quoted-printable-decode-region
2695          (point) (point-max) (mm-charset-to-coding-system charset))))))
2696
2697 (defun article-de-base64-unreadable (&optional force read-charset)
2698   "Translate a base64 article.
2699 If FORCE, decode the article whether it is marked as base64 not.
2700 If READ-CHARSET, ask for a coding system."
2701   (interactive (list 'force current-prefix-arg))
2702   (save-excursion
2703     (let ((inhibit-read-only t) type charset)
2704       (if (gnus-buffer-live-p gnus-original-article-buffer)
2705           (with-current-buffer gnus-original-article-buffer
2706             (setq type
2707                   (gnus-fetch-field "content-transfer-encoding"))
2708             (let* ((ct (gnus-fetch-field "content-type"))
2709                    (ctl (and ct (mail-header-parse-content-type ct))))
2710               (setq charset (and ctl
2711                                  (mail-content-type-get ctl 'charset)))
2712               (if (stringp charset)
2713                   (setq charset (intern (downcase charset)))))))
2714       (if read-charset
2715           (setq charset (mm-read-coding-system "Charset: " charset)))
2716       (unless charset
2717         (setq charset gnus-newsgroup-charset))
2718       (when (or force
2719                 (and type (let ((case-fold-search t))
2720                             (string-match "base64" type))))
2721         (article-goto-body)
2722         (save-restriction
2723           (narrow-to-region (point) (point-max))
2724           (base64-decode-region (point-min) (point-max))
2725           (mm-decode-coding-region
2726            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2727
2728 (eval-when-compile
2729   (require 'rfc1843))
2730
2731 (defun article-decode-HZ ()
2732   "Translate a HZ-encoded article."
2733   (interactive)
2734   (require 'rfc1843)
2735   (save-excursion
2736     (let ((inhibit-read-only t))
2737       (rfc1843-decode-region (point-min) (point-max)))))
2738
2739 (defun article-unsplit-urls ()
2740   "Remove the newlines that some other mailers insert into URLs."
2741   (interactive)
2742   (save-excursion
2743     (let ((inhibit-read-only t))
2744       (goto-char (point-min))
2745       (while (re-search-forward
2746               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2747         (replace-match "\\1\\3" t)))
2748     (when (interactive-p)
2749       (gnus-treat-article nil))))
2750
2751 (defun article-wash-html ()
2752   "Format an HTML article."
2753   (interactive)
2754   (let ((handles nil)
2755         (buffer-read-only nil))
2756     (when (gnus-buffer-live-p gnus-original-article-buffer)
2757       (setq handles (mm-dissect-buffer t t)))
2758     (article-goto-body)
2759     (delete-region (point) (point-max))
2760     (mm-inline-text-html handles)))
2761
2762 (defvar gnus-article-browse-html-temp-list nil
2763   "List of temporary files created by `gnus-article-browse-html-parts'.
2764 Internal variable.")
2765
2766 (defcustom gnus-article-browse-delete-temp 'ask
2767   "What to do with temporary files from `gnus-article-browse-html-parts'.
2768 If nil, don't delete temporary files.  If it is t, delete them on
2769 exit from the summary buffer.  If it is the symbol `file', query
2770 on each file, if it is `ask' ask once when exiting from the
2771 summary buffer."
2772   :group 'gnus-article
2773   :version "23.1" ;; No Gnus
2774   :type '(choice (const :tag "Don't delete" nil)
2775                  (const :tag "Don't ask" t)
2776                  (const :tag "Ask" ask)
2777                  (const :tag "Ask for each file" file)))
2778
2779 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2780
2781 (defun gnus-article-browse-delete-temp-files (&optional how)
2782   "Delete temp-files created by `gnus-article-browse-html-parts'."
2783   (when (and gnus-article-browse-html-temp-list
2784              (progn
2785                (or how (setq how gnus-article-browse-delete-temp))
2786                (if (eq how 'ask)
2787                    (let ((files (length gnus-article-browse-html-temp-list)))
2788                      (gnus-y-or-n-p
2789                       (if (= files 1)
2790                           "Delete the temporary HTML file? "
2791                         (format "Delete all %s temporary HTML files? "
2792                                 files))))
2793                  how)))
2794     (dolist (file gnus-article-browse-html-temp-list)
2795       (cond ((file-directory-p file)
2796              (when (or (not (eq how 'file))
2797                        (gnus-y-or-n-p
2798                         (format
2799                          "Delete temporary HTML file(s) in directory `%s'? "
2800                          (file-name-as-directory file))))
2801                (gnus-delete-directory file)))
2802             ((file-exists-p file)
2803              (when (or (not (eq how 'file))
2804                        (gnus-y-or-n-p
2805                         (format "Delete temporary HTML file `%s'? " file)))
2806                (delete-file file)))))
2807     ;; Also remove file from the list when not deleted or if file doesn't
2808     ;; exist anymore.
2809     (setq gnus-article-browse-html-temp-list nil))
2810   gnus-article-browse-html-temp-list)
2811
2812 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2813   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2814 Return file name."
2815   (save-match-data
2816     (let (file type)
2817       (catch 'found
2818         (dolist (handle handles)
2819           (cond
2820            ((not (listp handle)))
2821            ((equal (mm-handle-media-supertype handle) "multipart")
2822             (when (setq file (gnus-article-browse-html-save-cid-content
2823                               cid handle directory))
2824               (throw 'found file)))
2825            ((equal (concat "<" cid ">") (mm-handle-id handle))
2826             (setq file
2827                   (expand-file-name
2828                    (or (mm-handle-filename handle)
2829                        (concat
2830                         (make-temp-name "cid")
2831                         (car (rassoc (car (mm-handle-type handle)) mailcap-mime-extensions))))
2832                    directory))
2833             (mm-save-part-to-file handle file)
2834             (throw 'found file))))))))
2835
2836 (defun gnus-article-browse-html-parts (list &optional header)
2837   "View all \"text/html\" parts from LIST.
2838 Recurse into multiparts.  The optional HEADER that should be a decoded
2839 message header will be added to the bodies of the \"text/html\" parts."
2840   ;; Internal function used by `gnus-article-browse-html-article'.
2841   (let (type file charset content cid-dir tmp-file showed)
2842     ;; Find and show the html-parts.
2843     (dolist (handle list)
2844       ;; If HTML, show it:
2845       (cond ((not (listp handle)))
2846             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2847                  (and (equal (car type) "message/external-body")
2848                       (or header
2849                           (setq file (mm-handle-filename handle)))
2850                       (or (mm-handle-cache handle)
2851                           (condition-case code
2852                               (progn (mm-extern-cache-contents handle) t)
2853                             (error
2854                              (gnus-message 3 "%s" (error-message-string code))
2855                              (when (>= gnus-verbose 3) (sit-for 2))
2856                              nil)))
2857                       (progn
2858                         (setq handle (mm-handle-cache handle)
2859                               type (mm-handle-type handle))
2860                         (equal (car type) "text/html"))))
2861              (setq charset (mail-content-type-get type 'charset)
2862                    content (mm-get-part handle))
2863              (with-temp-buffer
2864                (if (eq charset 'gnus-decoded)
2865                    (mm-enable-multibyte)
2866                  (mm-disable-multibyte))
2867                (insert content)
2868                ;; resolve cid contents
2869                (let ((case-fold-search t)
2870                      cid-file)
2871                  (goto-char (point-min))
2872                  (while (re-search-forward "\
2873 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2874                                            nil t)
2875                    (unless cid-dir
2876                      (setq cid-dir (mm-make-temp-file "cid" t))
2877                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2878                    (setq file nil
2879                          content nil)
2880                    (when (setq cid-file
2881                                (gnus-article-browse-html-save-cid-content
2882                                 (match-string 2)
2883                                 (with-current-buffer gnus-article-buffer
2884                                   gnus-article-mime-handles)
2885                                 cid-dir))
2886                      (when (eq system-type 'cygwin)
2887                        (setq cid-file
2888                              (concat "/" (substring
2889                                           (with-output-to-string
2890                                             (call-process "cygpath" nil
2891                                                           standard-output
2892                                                           nil "-m" cid-file))
2893                                           0 -1))))
2894                      (replace-match (concat "file://" cid-file)
2895                                     nil nil nil 1))))
2896                (unless content (setq content (buffer-string))))
2897              (when (or charset header (not file))
2898                (setq tmp-file (mm-make-temp-file
2899                                ;; Do we need to care for 8.3 filenames?
2900                                "mm-" nil ".html")))
2901              ;; Add a meta html tag to specify charset and a header.
2902              (cond
2903               (header
2904                (let (title eheader body hcharset coding force-charset)
2905                  (with-temp-buffer
2906                    (mm-enable-multibyte)
2907                    (setq case-fold-search t)
2908                    (insert header "\n")
2909                    (setq title (message-fetch-field "subject"))
2910                    (goto-char (point-min))
2911                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2912                      (replace-match (cond ((match-beginning 1) "&lt;")
2913                                           ((match-beginning 2) "&gt;")
2914                                           (t "&amp;"))))
2915                    (goto-char (point-min))
2916                    (insert "<pre>\n")
2917                    (goto-char (point-max))
2918                    (insert "</pre>\n<hr>\n")
2919                    ;; We have to examine charset one by one since
2920                    ;; charset specified in parts might be different.
2921                    (if (eq charset 'gnus-decoded)
2922                        (setq charset 'utf-8
2923                              eheader (mm-encode-coding-string (buffer-string)
2924                                                               charset)
2925                              title (when title
2926                                      (mm-encode-coding-string title charset))
2927                              body (mm-encode-coding-string content charset)
2928                              force-charset t)
2929                      (setq hcharset (mm-find-mime-charset-region (point-min)
2930                                                                  (point-max)))
2931                      (cond ((= (length hcharset) 1)
2932                             (setq hcharset (car hcharset)
2933                                   coding (mm-charset-to-coding-system
2934                                           hcharset)))
2935                            ((> (length hcharset) 1)
2936                             (setq hcharset 'utf-8
2937                                   coding hcharset)))
2938                      (if coding
2939                          (if charset
2940                              (progn
2941                                (setq body
2942                                      (mm-charset-to-coding-system charset))
2943                                (if (eq coding body)
2944                                    (setq eheader (mm-encode-coding-string
2945                                                   (buffer-string) coding)
2946                                          title (when title
2947                                                  (mm-encode-coding-string
2948                                                   title coding))
2949                                          body content)
2950                                  (setq charset 'utf-8
2951                                        eheader (mm-encode-coding-string
2952                                                 (buffer-string) charset)
2953                                        title (when title
2954                                                (mm-encode-coding-string
2955                                                 title charset))
2956                                        body (mm-encode-coding-string
2957                                              (mm-decode-coding-string
2958                                               content body)
2959                                              charset)
2960                                        force-charset t)))
2961                            (setq charset hcharset
2962                                  eheader (mm-encode-coding-string
2963                                           (buffer-string) coding)
2964                                  title (when title
2965                                          (mm-encode-coding-string
2966                                           title coding))
2967                                  body content))
2968                        (setq eheader (mm-string-as-unibyte (buffer-string))
2969                              body content)))
2970                    (erase-buffer)
2971                    (mm-disable-multibyte)
2972                    (insert body)
2973                    (when charset
2974                      (mm-add-meta-html-tag handle charset force-charset))
2975                    (when title
2976                      (goto-char (point-min))
2977                      (unless (search-forward "<title>" nil t)
2978                        (re-search-forward "<head>\\s-*" nil t)
2979                        (insert "<title>" title "</title>\n")))
2980                    (goto-char (point-min))
2981                    (or (re-search-forward
2982                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2983                        (re-search-forward
2984                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2985                    (insert eheader)
2986                    (mm-write-region (point-min) (point-max)
2987                                     tmp-file nil nil nil 'binary t))))
2988               (charset
2989                (mm-with-unibyte-buffer
2990                  (insert (if (eq charset 'gnus-decoded)
2991                              (mm-encode-coding-string content
2992                                                       (setq charset 'utf-8))
2993                            content))
2994                  (if (or (mm-add-meta-html-tag handle charset)
2995                          (not file))
2996                      (mm-write-region (point-min) (point-max)
2997                                       tmp-file nil nil nil 'binary t)
2998                    (setq tmp-file nil))))
2999               (tmp-file
3000                (mm-save-part-to-file handle tmp-file)))
3001              (when tmp-file
3002                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3003              (add-hook 'gnus-summary-prepare-exit-hook
3004                        'gnus-article-browse-delete-temp-files)
3005              (add-hook 'gnus-exit-gnus-hook
3006                        (lambda  ()
3007                          (gnus-article-browse-delete-temp-files t)))
3008              ;; FIXME: Warn if there's an <img> tag?
3009              (browse-url-of-file (or tmp-file (expand-file-name file)))
3010              (setq showed t))
3011             ;; If multipart, recurse
3012             ((equal (mm-handle-media-supertype handle) "multipart")
3013              (when (gnus-article-browse-html-parts handle header)
3014                (setq showed t)))
3015             ((equal (mm-handle-media-type handle) "message/rfc822")
3016              (mm-with-multibyte-buffer
3017                (mm-insert-part handle)
3018                (setq handle (mm-dissect-buffer t t))
3019                (when (and (bufferp (car handle))
3020                           (stringp (car (mm-handle-type handle))))
3021                  (setq handle (list handle)))
3022                (when header
3023                  (article-decode-encoded-words)
3024                  (let ((gnus-visible-headers
3025                         (or (get 'gnus-visible-headers 'standard-value)
3026                             gnus-visible-headers)))
3027                    (article-hide-headers))
3028                  (goto-char (point-min))
3029                  (search-forward "\n\n" nil 'move)
3030                  (skip-chars-backward "\t\n ")
3031                  (setq header (buffer-substring (point-min) (point)))))
3032              (when (prog1
3033                        (gnus-article-browse-html-parts handle header)
3034                      (mm-destroy-parts handle))
3035                (setq showed t)))))
3036     showed))
3037
3038 (defun gnus-article-browse-html-article (&optional arg)
3039   "View \"text/html\" parts of the current article with a WWW browser.
3040 Inline images embedded in a message using the cid scheme, as they are
3041 generally considered to be safe, will be processed properly.
3042 The message header is added to the beginning of every html part unless
3043 the prefix argument ARG is given.
3044
3045 Warning: Spammers use links to images (using the http scheme) in HTML
3046 articles to verify whether you have read the message.  As
3047 `gnus-article-browse-html-article' passes the HTML content to the
3048 browser without eliminating these \"web bugs\" you should only
3049 use it for mails from trusted senders.
3050
3051 If you always want to display HTML parts in the browser, set
3052 `mm-text-html-renderer' to nil.
3053
3054 This command creates temporary files to pass HTML contents including
3055 images if any to the browser, and deletes them when exiting the group
3056 \(if you want)."
3057   ;; Cf. `mm-w3m-safe-url-regexp'
3058   (interactive "P")
3059   (if arg
3060       (gnus-summary-show-article)
3061     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3062                                     gnus-visible-headers))
3063           ;; As we insert a <hr>, there's no need for the body boundary.
3064           (gnus-treat-body-boundary nil))
3065       (gnus-summary-show-article)))
3066   (with-current-buffer gnus-article-buffer
3067     (let ((header (unless arg
3068                     (save-restriction
3069                       (widen)
3070                       (buffer-substring-no-properties
3071                        (goto-char (point-min))
3072                        (if (search-forward "\n\n" nil t)
3073                            (match-beginning 0)
3074                          (goto-char (point-max))
3075                          (skip-chars-backward "\t\n ")
3076                          (point))))))
3077           parts)
3078       (set-buffer gnus-original-article-buffer)
3079       (setq parts (mm-dissect-buffer t t))
3080       ;; If singlepart, enforce a list.
3081       (when (and (bufferp (car parts))
3082                  (stringp (car (mm-handle-type parts))))
3083         (setq parts (list parts)))
3084       ;; Process the list
3085       (unless (gnus-article-browse-html-parts parts header)
3086         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3087       (mm-destroy-parts parts)
3088       (unless arg
3089         (gnus-summary-show-article)))))
3090
3091 (defun article-hide-list-identifiers ()
3092   "Remove list identifies from the Subject header.
3093 The `gnus-list-identifiers' variable specifies what to do."
3094   (interactive)
3095   (let ((inhibit-point-motion-hooks t)
3096         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3097         (inhibit-read-only t))
3098     (when regexp
3099       (save-excursion
3100         (save-restriction
3101           (article-narrow-to-head)
3102           (goto-char (point-min))
3103           (while (re-search-forward
3104                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3105                   nil t)
3106             (delete-region (match-beginning 2) (match-end 0))
3107             (beginning-of-line))
3108           (when (re-search-forward
3109                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3110             (delete-region (match-beginning 1) (match-end 1))))))))
3111
3112 (defun article-hide-pem (&optional arg)
3113   "Toggle hiding of any PEM headers and signatures in the current article.
3114 If given a negative prefix, always show; if given a positive prefix,
3115 always hide."
3116   (interactive (gnus-article-hidden-arg))
3117   (unless (gnus-article-check-hidden-text 'pem arg)
3118     (save-excursion
3119       (let ((inhibit-read-only t) end)
3120         (goto-char (point-min))
3121         ;; Hide the horrendously ugly "header".
3122         (when (and (search-forward
3123                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3124                     nil t)
3125                    (setq end (1+ (match-beginning 0))))
3126           (gnus-add-wash-type 'pem)
3127           (gnus-article-hide-text-type
3128            end
3129            (if (search-forward "\n\n" nil t)
3130                (match-end 0)
3131              (point-max))
3132            'pem)
3133           ;; Hide the trailer as well
3134           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3135                                 nil t)
3136             (gnus-article-hide-text-type
3137              (match-beginning 0) (match-end 0) 'pem)))))))
3138
3139 (defun article-strip-banner ()
3140   "Strip the banners specified by the `banner' group parameter and by
3141 `gnus-article-address-banner-alist'."
3142   (interactive)
3143   (save-excursion
3144     (save-restriction
3145       (let ((inhibit-point-motion-hooks t))
3146         (when (gnus-parameter-banner gnus-newsgroup-name)
3147           (article-really-strip-banner
3148            (gnus-parameter-banner gnus-newsgroup-name)))
3149         (when gnus-article-address-banner-alist
3150           ;; Note that the From header is decoded here, so it is
3151           ;; required that the *-extract-address-components function
3152           ;; supports non-ASCII text.
3153           (let ((from (save-restriction
3154                         (widen)
3155                         (article-narrow-to-head)
3156                         (mail-fetch-field "from"))))
3157             (when (and from
3158                        (setq from
3159                              (cadr (funcall gnus-extract-address-components
3160                                             from))))
3161               (catch 'found
3162                 (dolist (pair gnus-article-address-banner-alist)
3163                   (when (string-match (car pair) from)
3164                     (throw 'found
3165                            (article-really-strip-banner (cdr pair)))))))))))))
3166
3167 (defun article-really-strip-banner (banner)
3168   "Strip the banner specified by the argument."
3169   (save-excursion
3170     (save-restriction
3171       (let ((inhibit-point-motion-hooks t)
3172             (gnus-signature-limit nil)
3173             (inhibit-read-only t))
3174         (article-goto-body)
3175         (cond
3176          ((eq banner 'signature)
3177           (when (gnus-article-narrow-to-signature)
3178             (widen)
3179             (forward-line -1)
3180             (delete-region (point) (point-max))))
3181          ((symbolp banner)
3182           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3183               (while (re-search-forward banner nil t)
3184                 (delete-region (match-beginning 0) (match-end 0)))))
3185          ((stringp banner)
3186           (while (re-search-forward banner nil t)
3187             (delete-region (match-beginning 0) (match-end 0)))))))))
3188
3189 (defun article-babel ()
3190   "Translate article using an online translation service."
3191   (interactive)
3192   (require 'babel)
3193   (gnus-with-article-buffer
3194     (when (article-goto-body)
3195       (let* ((start (point))
3196              (end (point-max))
3197              (orig (buffer-substring start end))
3198              (trans (babel-as-string orig)))
3199         (save-restriction
3200           (narrow-to-region start end)
3201           (delete-region start end)
3202           (insert trans))))))
3203
3204 (defun article-hide-signature (&optional arg)
3205   "Hide the signature in the current article.
3206 If given a negative prefix, always show; if given a positive prefix,
3207 always hide."
3208   (interactive (gnus-article-hidden-arg))
3209   (unless (gnus-article-check-hidden-text 'signature arg)
3210     (save-excursion
3211       (save-restriction
3212         (let ((inhibit-read-only t))
3213           (when (gnus-article-narrow-to-signature)
3214             (gnus-article-hide-text-type
3215              (point-min) (point-max) 'signature))))))
3216   (gnus-set-mode-line 'article))
3217
3218 (defun article-strip-headers-in-body ()
3219   "Strip offensive headers from bodies."
3220   (interactive)
3221   (save-excursion
3222     (article-goto-body)
3223     (let ((case-fold-search t))
3224       (when (looking-at "x-no-archive:")
3225         (gnus-delete-line)))))
3226
3227 (defun article-strip-leading-blank-lines ()
3228   "Remove all blank lines from the beginning of the article."
3229   (interactive)
3230   (save-excursion
3231     (let ((inhibit-point-motion-hooks t)
3232           (inhibit-read-only t))
3233       (when (article-goto-body)
3234         (while (and (not (eobp))
3235                     (looking-at "[ \t]*$"))
3236           (gnus-delete-line))))))
3237
3238 (defun article-narrow-to-head ()
3239   "Narrow the buffer to the head of the message.
3240 Point is left at the beginning of the narrowed-to region."
3241   (narrow-to-region
3242    (goto-char (point-min))
3243    (cond
3244     ;; Absolutely no headers displayed.
3245     ((looking-at "\n")
3246      (point))
3247     ;; Normal headers.
3248     ((search-forward "\n\n" nil 1)
3249      (1- (point)))
3250     ;; Nothing but headers.
3251     (t
3252      (point-max))))
3253   (goto-char (point-min)))
3254
3255 (defun article-goto-body ()
3256   "Place point at the start of the body."
3257   (goto-char (point-min))
3258   (cond
3259    ;; This variable is only bound when dealing with separate
3260    ;; MIME body parts.
3261    (article-goto-body-goes-to-point-min-p
3262     t)
3263    ((search-forward "\n\n" nil t)
3264     t)
3265    (t
3266     (goto-char (point-max))
3267     nil)))
3268
3269 (defun article-strip-multiple-blank-lines ()
3270   "Replace consecutive blank lines with one empty line."
3271   (interactive)
3272   (save-excursion
3273     (let ((inhibit-point-motion-hooks t)
3274           (inhibit-read-only t))
3275       ;; First make all blank lines empty.
3276       (article-goto-body)
3277       (while (re-search-forward "^[ \t]+$" nil t)
3278         (unless (gnus-annotation-in-region-p
3279                  (match-beginning 0) (match-end 0))
3280           (replace-match "" nil t)))
3281       ;; Then replace multiple empty lines with a single empty line.
3282       (article-goto-body)
3283       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3284         (unless (gnus-annotation-in-region-p
3285                  (match-beginning 0) (match-end 0))
3286           (delete-region (match-beginning 1) (match-end 1)))))))
3287
3288 (defun article-strip-leading-space ()
3289   "Remove all white space from the beginning of the lines in the article."
3290   (interactive)
3291   (save-excursion
3292     (let ((inhibit-point-motion-hooks t)
3293           (inhibit-read-only t))
3294       (article-goto-body)
3295       (while (re-search-forward "^[ \t]+" nil t)
3296         (replace-match "" t t)))))
3297
3298 (defun article-strip-trailing-space ()
3299   "Remove all white space from the end of the lines in the article."
3300   (interactive)
3301   (save-excursion
3302     (let ((inhibit-point-motion-hooks t)
3303           (inhibit-read-only t))
3304       (article-goto-body)
3305       (while (re-search-forward "[ \t]+$" nil t)
3306         (replace-match "" t t)))))
3307
3308 (defun article-strip-blank-lines ()
3309   "Strip leading, trailing and multiple blank lines."
3310   (interactive)
3311   (article-strip-leading-blank-lines)
3312   (article-remove-trailing-blank-lines)
3313   (article-strip-multiple-blank-lines))
3314
3315 (defun article-strip-all-blank-lines ()
3316   "Strip all blank lines."
3317   (interactive)
3318   (save-excursion
3319     (let ((inhibit-point-motion-hooks t)
3320           (inhibit-read-only t))
3321       (article-goto-body)
3322       (while (re-search-forward "^[ \t]*\n" nil t)
3323         (replace-match "" t t)))))
3324
3325 (defun gnus-article-narrow-to-signature ()
3326   "Narrow to the signature; return t if a signature is found, else nil."
3327   (let ((inhibit-point-motion-hooks t))
3328     (when (gnus-article-search-signature)
3329       (forward-line 1)
3330       ;; Check whether we have some limits to what we consider
3331       ;; to be a signature.
3332       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3333                       (list gnus-signature-limit)))
3334             limit limited)
3335         (while (setq limit (pop limits))
3336           (if (or (and (integerp limit)
3337                        (< (- (point-max) (point)) limit))
3338                   (and (floatp limit)
3339                        (< (count-lines (point) (point-max)) limit))
3340                   (and (functionp limit)
3341                        (funcall limit))
3342                   (and (stringp limit)
3343                        (not (re-search-forward limit nil t))))
3344               ()                        ; This limit did not succeed.
3345             (setq limited t
3346                   limits nil)))
3347         (unless limited
3348           (narrow-to-region (point) (point-max))
3349           t)))))
3350
3351 (defun gnus-article-search-signature ()
3352   "Search the current buffer for the signature separator.
3353 Put point at the beginning of the signature separator."
3354   (let ((cur (point)))
3355     (goto-char (point-max))
3356     (if (if (stringp gnus-signature-separator)
3357             (re-search-backward gnus-signature-separator nil t)
3358           (let ((seps gnus-signature-separator))
3359             (while (and seps
3360                         (not (re-search-backward (car seps) nil t)))
3361               (pop seps))
3362             seps))
3363         t
3364       (goto-char cur)
3365       nil)))
3366
3367 (defun gnus-article-hidden-arg ()
3368   "Return the current prefix arg as a number, or 0 if no prefix."
3369   (list (if current-prefix-arg
3370             (prefix-numeric-value current-prefix-arg)
3371           0)))
3372
3373 (defun gnus-article-check-hidden-text (type arg)
3374   "Return nil if hiding is necessary.
3375 Arg can be nil or a number.  nil and positive means hide, negative
3376 means show, 0 means toggle."
3377   (save-excursion
3378     (save-restriction
3379       (let ((hide (gnus-article-hidden-text-p type)))
3380         (cond
3381          ((or (null arg)
3382               (> arg 0))
3383           nil)
3384          ((< arg 0)
3385           (gnus-article-show-hidden-text type)
3386           t)
3387          (t
3388           (if (eq hide 'hidden)
3389               (progn
3390                 (gnus-article-show-hidden-text type)
3391                 t)
3392             nil)))))))
3393
3394 (defun gnus-article-hidden-text-p (type)
3395   "Say whether the current buffer contains hidden text of type TYPE."
3396   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3397     (while (and pos
3398                 (not (get-text-property pos 'invisible))
3399                 (not (get-text-property pos 'dummy-invisible)))
3400       (setq pos
3401             (text-property-any (1+ pos) (point-max) 'article-type type)))
3402     (if pos
3403         'hidden
3404       nil)))
3405
3406 (defun gnus-article-show-hidden-text (type &optional dummy)
3407   "Show all hidden text of type TYPE.
3408 Originally it is hide instead of DUMMY."
3409   (let ((inhibit-read-only t)
3410         (inhibit-point-motion-hooks t))
3411     (gnus-remove-text-properties-when
3412      'article-type type
3413      (point-min) (point-max)
3414      (cons 'article-type (cons type
3415                                gnus-hidden-properties)))
3416     (gnus-delete-wash-type type)))
3417
3418 (defconst article-time-units
3419   `((year . ,(* 365.25 24 60 60))
3420     (week . ,(* 7 24 60 60))
3421     (day . ,(* 24 60 60))
3422     (hour . ,(* 60 60))
3423     (minute . 60)
3424     (second . 1))
3425   "Mapping from time units to seconds.")
3426
3427 (defun gnus-article-forward-header ()
3428   "Move point to the start of the next header.
3429 If the current header is a continuation header, this can be several
3430 lines forward."
3431   (let ((ended nil))
3432     (while (not ended)
3433       (forward-line 1)
3434       (if (looking-at "[ \t]+[^ \t]")
3435           (forward-line 1)
3436         (setq ended t)))))
3437
3438 (defun article-treat-date ()
3439   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3440                        (with-current-buffer gnus-summary-buffer
3441                          gnus-article-date-headers)
3442                      gnus-article-date-headers)
3443                    t))
3444
3445 (defun article-date-ut (&optional type highlight date-position)
3446   "Convert DATE date to TYPE in the current article.
3447 The default type is `ut'.  See `gnus-article-date-headers' for
3448 possible values."
3449   (interactive (list 'ut t))
3450   (let* ((case-fold-search t)
3451          (inhibit-read-only t)
3452          (inhibit-point-motion-hooks t)
3453          (first t)
3454          (visible-date (mail-fetch-field "Date"))
3455          pos date bface eface)
3456     (save-excursion
3457       (goto-char (point-min))
3458       (when (re-search-forward "^Date:" nil t)
3459         (setq bface (get-text-property (point-at-bol) 'face)
3460               eface (get-text-property (1- (point-at-eol)) 'face)))
3461       ;; Delete any old Date headers.
3462       (if date-position
3463           (progn
3464             (goto-char date-position)
3465             (setq date (get-text-property (point) 'original-date))
3466             (delete-region (point)
3467                            (progn
3468                              (gnus-article-forward-header)
3469                              (point)))
3470             (article-transform-date date type bface eface))
3471         (save-restriction
3472           (widen)
3473           (goto-char (point-min))
3474           (while (or (get-text-property (setq pos (point)) 'original-date)
3475                      (and (setq pos (next-single-property-change
3476                                      (point) 'original-date))
3477                           (goto-char pos)))
3478             (narrow-to-region pos (if (search-forward "\n\n" nil t)
3479                                       (1+ (match-beginning 0))
3480                                     (point-max)))
3481             (goto-char (point-min))
3482             (while (re-search-forward "^Date:" nil t)
3483               (setq date (get-text-property (match-beginning 0) 'original-date))
3484               (delete-region (point-at-bol) (progn
3485                                               (gnus-article-forward-header)
3486                                               (point))))
3487             (when (and (not date)
3488                        visible-date)
3489               (setq date visible-date))
3490             (when date
3491               (article-transform-date date type bface eface))
3492             (goto-char (point-max))
3493             (widen)))))))
3494
3495 (defun article-transform-date (date type bface eface)
3496   (dolist (this-type (cond
3497                       ((null type)
3498                        (list 'ut))
3499                       ((atom type)
3500                        (list type))
3501                       (t
3502                        type)))
3503     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3504     (forward-line -1)
3505     (beginning-of-line)
3506     (put-text-property (point) (1+ (point))
3507                        'original-date date)
3508     (put-text-property (point) (1+ (point))
3509                        'gnus-date-type this-type)
3510     ;; Do highlighting.
3511     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3512       (put-text-property (match-beginning 1) (1+ (match-end 1))
3513                          'face bface)
3514       (put-text-property (match-beginning 2) (match-end 2)
3515                          'face eface))
3516     (forward-line 1)))
3517
3518 (defun article-make-date-line (date type)
3519   "Return a DATE line of TYPE."
3520   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3521                              combined-lapsed))
3522     (error "Unknown conversion type: %s" type))
3523   (condition-case ()
3524       (let ((time (ignore-errors (date-to-time date))))
3525         (cond
3526          ;; Convert to the local timezone.
3527          ((eq type 'local)
3528           (concat "Date: " (message-make-date time)))
3529          ;; Convert to Universal Time.
3530          ((eq type 'ut)
3531           (concat "Date: "
3532                   (substring
3533                    (message-make-date
3534                     (let* ((e (parse-time-string date))
3535                            (tm (apply 'encode-time e))
3536                            (ms (car tm))
3537                            (ls (- (cadr tm) (car (current-time-zone time)))))
3538                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3539                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3540                             (t (list ms ls)))))
3541                    0 -5)
3542                   "UT"))
3543          ;; Get the original date from the article.
3544          ((eq type 'original)
3545           (concat "Date: " (if (string-match "\n+$" date)
3546                                (substring date 0 (match-beginning 0))
3547                              date)))
3548          ;; Let the user define the format.
3549          ((eq type 'user-defined)
3550           (let ((format (or (condition-case nil
3551                                 (with-current-buffer gnus-summary-buffer
3552                                   gnus-article-time-format)
3553                               (error nil))
3554                             gnus-article-time-format)))
3555             (if (functionp format)
3556                 (funcall format time)
3557               (concat "Date: " (format-time-string format time)))))
3558          ;; ISO 8601.
3559          ((eq type 'iso8601)
3560           (let ((tz (car (current-time-zone time))))
3561             (concat
3562              "Date: "
3563              (format-time-string "%Y%m%dT%H%M%S" time)
3564              (format "%s%02d%02d"
3565                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3566                      (/ (% (abs tz) 3600) 60)))))
3567          ;; Do a lapsed format.
3568          ((eq type 'lapsed)
3569           (concat "Date: " (article-lapsed-string time)))
3570          ;; A combined date/lapsed format.
3571          ((eq type 'combined-lapsed)
3572           (let ((date-string (article-make-date-line date 'original))
3573                 (segments 3)
3574                 lapsed-string)
3575             (while (and
3576                     time
3577                     (setq lapsed-string
3578                           (concat " (" (article-lapsed-string time segments) ")"))
3579                     (> (+ (length date-string)
3580                           (length lapsed-string))
3581                        (+ fill-column 6))
3582                     (> segments 0))
3583               (setq segments (1- segments)))
3584             (if (> segments 0)
3585                 (concat date-string lapsed-string)
3586               date-string)))
3587          ;; Display the date in proper English
3588          ((eq type 'english)
3589           (let ((dtime (decode-time time)))
3590             (concat
3591              "Date: the "
3592              (number-to-string (nth 3 dtime))
3593              (let ((digit (% (nth 3 dtime) 10)))
3594                (cond
3595                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3596                 ((= digit 1) "st")
3597                 ((= digit 2) "nd")
3598                 ((= digit 3) "rd")
3599                 (t "th")))
3600              " of "
3601              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3602              " "
3603              (number-to-string (nth 5 dtime))
3604              " at "
3605              (format "%02d" (nth 2 dtime))
3606              ":"
3607              (format "%02d" (nth 1 dtime)))))))
3608     (foo
3609      (format "Date: %s (from Gnus)" date))))
3610
3611 (defun article-lapsed-string (time &optional max-segments)
3612   ;; If the date is seriously mangled, the timezone functions are
3613   ;; liable to bug out, so we ignore all errors.
3614   (let* ((now (current-time))
3615          (real-time (subtract-time now time))
3616          (real-sec (and real-time
3617                         (+ (* (float (car real-time)) 65536)
3618                            (cadr real-time))))
3619          (sec (and real-time (abs real-sec)))
3620          (segments 0)
3621          num prev)
3622     (unless max-segments
3623       (setq max-segments (length article-time-units)))
3624     (cond
3625      ((null real-time)
3626       "Unknown")
3627      ((zerop sec)
3628       "Now")
3629      (t
3630       (concat
3631        ;; This is a bit convoluted, but basically we go
3632        ;; through the time units for years, weeks, etc,
3633        ;; and divide things to see whether that results
3634        ;; in positive answers.
3635        (mapconcat
3636         (lambda (unit)
3637           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3638                   (>= segments max-segments))
3639               ;; The (remaining) seconds are too few to
3640               ;; be divided into this time unit.
3641               ""
3642             ;; It's big enough, so we output it.
3643             (setq sec (- sec (* num (cdr unit))))
3644             (prog1
3645                 (concat (if prev ", " "") (int-to-string
3646                                            (floor num))
3647                         " " (symbol-name (car unit))
3648                         (if (> num 1) "s" ""))
3649               (setq prev t
3650                     segments (1+ segments)))))
3651         article-time-units "")
3652        ;; If dates are odd, then it might appear like the
3653        ;; article was sent in the future.
3654        (if (> real-sec 0)
3655            " ago"
3656          " in the future"))))))
3657
3658 (defun article-date-local (&optional highlight)
3659   "Convert the current article date to the local timezone."
3660   (interactive (list t))
3661   (article-date-ut 'local highlight))
3662
3663 (defun article-date-english (&optional highlight)
3664   "Convert the current article date to something that is proper English."
3665   (interactive (list t))
3666   (article-date-ut 'english highlight))
3667
3668 (defun article-date-original (&optional highlight)
3669   "Convert the current article date to what it was originally.
3670 This is only useful if you have used some other date conversion
3671 function and want to see what the date was before converting."
3672   (interactive (list t))
3673   (article-date-ut 'original highlight))
3674
3675 (defun article-date-lapsed (&optional highlight)
3676   "Convert the current article date to time lapsed since it was sent."
3677   (interactive (list t))
3678   (article-date-ut 'lapsed highlight))
3679
3680 (defun article-date-combined-lapsed (&optional highlight)
3681   "Convert the current article date to time lapsed since it was sent."
3682   (interactive (list t))
3683   (article-date-ut 'combined-lapsed highlight))
3684
3685 (defun article-update-date-lapsed ()
3686   "Function to be run from a timer to update the lapsed time line."
3687   (save-match-data
3688     (let ((buffer (current-buffer)))
3689       (ignore-errors
3690         (walk-windows
3691          (lambda (w)
3692            (set-buffer (window-buffer w))
3693            (when (eq major-mode 'gnus-article-mode)
3694              (let ((old-line (count-lines (point-min) (point)))
3695                    (old-column (- (point) (line-beginning-position)))
3696                    (window-start
3697                     (window-start (get-buffer-window (current-buffer)))))
3698                (goto-char (point-min))
3699                (while (re-search-forward "^Date:" nil t)
3700                  (let ((type (get-text-property (match-beginning 0)
3701                                                 'gnus-date-type)))
3702                    (when (memq type '(lapsed combined-lapsed user-format))
3703                      (when (and window-start
3704                                 (not (= window-start
3705                                         (save-excursion
3706                                           (forward-line 1)
3707                                           (point)))))
3708                        (setq window-start nil))
3709                      (save-excursion
3710                        (article-date-ut type t (match-beginning 0)))
3711                      (forward-line 1)
3712                      (when window-start
3713                        (set-window-start (get-buffer-window (current-buffer))
3714                                          (point))))))
3715                (goto-char (point-min))
3716                (when (> old-column 0)
3717                  (setq old-line (1- old-line)))
3718                (forward-line old-line)
3719                (end-of-line)
3720                (when (> (current-column) old-column)
3721                  (beginning-of-line)
3722                  (forward-char old-column)))))
3723          nil 'visible))
3724       (set-buffer buffer))))
3725
3726 (defun gnus-start-date-timer (&optional n)
3727   "Start a timer to update the Date headers in the article buffers.
3728 The numerical prefix says how frequently (in seconds) the function
3729 is to run."
3730   (interactive "p")
3731   (unless n
3732     (setq n 1))
3733   (gnus-stop-date-timer)
3734   (setq article-lapsed-timer
3735         (run-at-time 1 n 'article-update-date-lapsed)))
3736
3737 (defun gnus-stop-date-timer ()
3738   "Stop the Date timer."
3739   (interactive)
3740   (when article-lapsed-timer
3741     (nnheader-cancel-timer article-lapsed-timer)
3742     (setq article-lapsed-timer nil)))
3743
3744 (defun article-date-user (&optional highlight)
3745   "Convert the current article date to the user-defined format.
3746 This format is defined by the `gnus-article-time-format' variable."
3747   (interactive (list t))
3748   (article-date-ut 'user highlight))
3749
3750 (defun article-date-iso8601 (&optional highlight)
3751   "Convert the current article date to ISO8601."
3752   (interactive (list t))
3753   (article-date-ut 'iso8601 highlight))
3754
3755 (defmacro gnus-article-save-original-date (&rest forms)
3756   "Save the original date as a text property and evaluate FORMS."
3757   `(let* ((case-fold-search t)
3758           (start (progn
3759                    (goto-char (point-min))
3760                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3761                               (not (bolp)))
3762                      (match-end 0))))
3763           (date (when (and start
3764                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3765                                               nil t))
3766                   (buffer-substring-no-properties start
3767                                                   (match-beginning 0)))))
3768      (goto-char (point-max))
3769      (skip-chars-backward "\n")
3770      (put-text-property (point-min) (point) 'original-date date)
3771      ,@forms
3772      (goto-char (point-max))
3773      (skip-chars-backward "\n")
3774      (put-text-property (point-min) (point) 'original-date date)))
3775
3776 ;; (defun article-show-all ()
3777 ;;   "Show all hidden text in the article buffer."
3778 ;;   (interactive)
3779 ;;   (save-excursion
3780 ;;     (let ((inhibit-read-only t))
3781 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3782
3783 (defun article-remove-leading-whitespace ()
3784   "Remove excessive whitespace from all headers."
3785   (interactive)
3786   (save-excursion
3787     (save-restriction
3788       (let ((inhibit-read-only t))
3789         (article-narrow-to-head)
3790         (goto-char (point-min))
3791         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3792           (delete-region (match-beginning 1) (match-end 1)))))))
3793
3794 (defun article-emphasize (&optional arg)
3795   "Emphasize text according to `gnus-emphasis-alist'."
3796   (interactive (gnus-article-hidden-arg))
3797   (unless (gnus-article-check-hidden-text 'emphasis arg)
3798     (save-excursion
3799       (let ((alist (or
3800                     (condition-case nil
3801                         (with-current-buffer gnus-summary-buffer
3802                           gnus-article-emphasis-alist)
3803                       (error))
3804                     gnus-emphasis-alist))
3805             (inhibit-read-only t)
3806             (props (append '(article-type emphasis)
3807                            gnus-hidden-properties))
3808             regexp elem beg invisible visible face)
3809         (article-goto-body)
3810         (setq beg (point))
3811         (while (setq elem (pop alist))
3812           (goto-char beg)
3813           (setq regexp (car elem)
3814                 invisible (nth 1 elem)
3815                 visible (nth 2 elem)
3816                 face (nth 3 elem))
3817           (while (re-search-forward regexp nil t)
3818             (when (and (match-beginning visible) (match-beginning invisible))
3819               (gnus-article-hide-text
3820                (match-beginning invisible) (match-end invisible) props)
3821               (gnus-article-unhide-text-type
3822                (match-beginning visible) (match-end visible) 'emphasis)
3823               (gnus-put-overlay-excluding-newlines
3824                (match-beginning visible) (match-end visible) 'face face)
3825               (gnus-add-wash-type 'emphasis)
3826               (goto-char (match-end invisible)))))))))
3827
3828 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3829   "Setup newsgroup emphasis alist."
3830   (unless gnus-article-emphasis-alist
3831     (let ((name (and gnus-newsgroup-name
3832                      (gnus-group-real-name gnus-newsgroup-name))))
3833       (make-local-variable 'gnus-article-emphasis-alist)
3834       (setq gnus-article-emphasis-alist
3835             (nconc
3836              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3837                (while (setq elem (pop alist))
3838                  (when (and name (string-match (car elem) name))
3839                    (setq alist nil
3840                          highlight (copy-sequence (cdr elem)))))
3841                highlight)
3842              (copy-sequence highlight-words)
3843              (if gnus-newsgroup-name
3844                  (copy-sequence (gnus-group-find-parameter
3845                                  gnus-newsgroup-name 'highlight-words t)))
3846              gnus-emphasis-alist)))))
3847
3848 (defvar gnus-summary-article-menu)
3849 (defvar gnus-summary-post-menu)
3850
3851 ;;; Saving functions.
3852
3853 (defun gnus-article-save (save-buffer file &optional num)
3854   "Save the currently selected article."
3855   (when (or (get gnus-default-article-saver :headers)
3856             (not gnus-save-all-headers))
3857     ;; Remove headers according to `gnus-saved-headers' or the value
3858     ;; of the `:headers' property that the saver function might have.
3859     (let ((gnus-visible-headers
3860            (or (symbol-value (get gnus-default-article-saver :headers))
3861                gnus-saved-headers gnus-visible-headers))
3862           ;; Ignore group parameter.  See `article-hide-headers'.
3863           (gnus-summary-buffer nil))
3864       (with-current-buffer save-buffer
3865         (article-hide-headers 1 t))))
3866   (save-window-excursion
3867     (if (not gnus-default-article-saver)
3868         (error "No default saver is defined")
3869       ;; !!! Magic!  The saving functions all save
3870       ;; `gnus-save-article-buffer' (or so they think), but we
3871       ;; bind that variable to our save-buffer.
3872       (set-buffer gnus-article-buffer)
3873       (let* ((gnus-save-article-buffer save-buffer)
3874              (filename
3875               (cond
3876                ((not gnus-prompt-before-saving) 'default)
3877                ((eq gnus-prompt-before-saving 'always) nil)
3878                (t file)))
3879              (gnus-number-of-articles-to-be-saved
3880               (when (eq gnus-prompt-before-saving t)
3881                 num)))                  ; Magic
3882         (set-buffer gnus-article-current-summary)
3883         (funcall gnus-default-article-saver filename)))))
3884
3885 (defun gnus-read-save-file-name (prompt &optional filename
3886                                         function group headers variable
3887                                         dir-var)
3888   (let ((default-name
3889           (funcall function group headers (symbol-value variable)))
3890         result)
3891     (setq result
3892           (expand-file-name
3893            (cond
3894             ((eq filename 'default)
3895              default-name)
3896             ((eq filename t)
3897              default-name)
3898             (filename filename)
3899             (t
3900              (when (symbol-value dir-var)
3901                (setq default-name (expand-file-name
3902                                    (file-name-nondirectory default-name)
3903                                    (symbol-value dir-var))))
3904              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3905                     (prompt
3906                      (format prompt
3907                              (if (and gnus-number-of-articles-to-be-saved
3908                                       (> gnus-number-of-articles-to-be-saved 1))
3909                                  (format "these %d articles"
3910                                          gnus-number-of-articles-to-be-saved)
3911                                "this article")))
3912                     (file
3913                      ;; Let the split methods have their say.
3914                      (cond
3915                       ;; No split name was found.
3916                       ((null split-name)
3917                        (read-file-name
3918                         (concat prompt " (default "
3919                                 (file-name-nondirectory default-name) "): ")
3920                         (file-name-directory default-name)
3921                         default-name))
3922                       ;; A single group name is returned.
3923                       ((stringp split-name)
3924                        (setq default-name
3925                              (funcall function split-name headers
3926                                       (symbol-value variable)))
3927                        (read-file-name
3928                         (concat prompt " (default "
3929                                 (file-name-nondirectory default-name) "): ")
3930                         (file-name-directory default-name)
3931                         default-name))
3932                       ;; A single split name was found
3933                       ((= 1 (length split-name))
3934                        (let* ((name (expand-file-name
3935                                      (car split-name)
3936                                      gnus-article-save-directory))
3937                               (dir (cond ((file-directory-p name)
3938                                           (file-name-as-directory name))
3939                                          ((file-exists-p name) name)
3940                                          (t gnus-article-save-directory))))
3941                          (read-file-name
3942                           (concat prompt " (default " name "): ")
3943                           dir name)))
3944                       ;; A list of splits was found.
3945                       (t
3946                        (setq split-name (nreverse split-name))
3947                        (let (result)
3948                          (let ((file-name-history
3949                                 (nconc split-name file-name-history)))
3950                            (setq result
3951                                  (expand-file-name
3952                                   (read-file-name
3953                                    (concat prompt " (`M-p' for defaults): ")
3954                                    gnus-article-save-directory
3955                                    (car split-name))
3956                                   gnus-article-save-directory)))
3957                          (car (push result file-name-history)))))))
3958                ;; Create the directory.
3959                (gnus-make-directory (file-name-directory file))
3960                ;; If we have read a directory, we append the default file name.
3961                (when (file-directory-p file)
3962                  (setq file (expand-file-name (file-name-nondirectory
3963                                                default-name)
3964                                               (file-name-as-directory file))))
3965                ;; Possibly translate some characters.
3966                (nnheader-translate-file-chars file))))))
3967     (gnus-make-directory (file-name-directory result))
3968     (when variable
3969       (set variable result))
3970     (when dir-var
3971       (set dir-var (file-name-directory result)))
3972     result))
3973
3974 (defun gnus-article-archive-name (group)
3975   "Return the first instance of an \"Archive-name\" in the current buffer."
3976   (let ((case-fold-search t))
3977     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3978       (nnheader-concat gnus-article-save-directory
3979                        (match-string 1)))))
3980
3981 (defun gnus-article-nndoc-name (group)
3982   "If GROUP is an nndoc group, return the name of the parent group."
3983   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3984     (gnus-group-get-parameter group 'save-article-group)))
3985
3986 (defun gnus-summary-save-in-rmail (&optional filename)
3987   "Append this article to Rmail file.
3988 Optional argument FILENAME specifies file name.
3989 Directory to save to is default to `gnus-article-save-directory'."
3990   (setq filename (gnus-read-save-file-name
3991                   "Save %s in rmail file" filename
3992                   gnus-rmail-save-name gnus-newsgroup-name
3993                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3994   (with-current-buffer gnus-save-article-buffer
3995     (save-excursion
3996       (save-restriction
3997         (widen)
3998         ;; Note that unlike gnus-summary-save-in-mail, there is no
3999         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
4000         ;; not use Babyl.
4001         (gnus-output-to-rmail filename))))
4002   filename)
4003
4004 (defun gnus-summary-save-in-mail (&optional filename)
4005   "Append this article to Unix mail file.
4006 Optional argument FILENAME specifies file name.
4007 Directory to save to is default to `gnus-article-save-directory'."
4008   (setq filename (gnus-read-save-file-name
4009                   "Save %s in Unix mail file" filename
4010                   gnus-mail-save-name gnus-newsgroup-name
4011                   gnus-current-headers 'gnus-newsgroup-last-mail))
4012   (with-current-buffer gnus-save-article-buffer
4013     (save-excursion
4014       (save-restriction
4015         (widen)
4016         (if (and (file-readable-p filename)
4017                  (file-regular-p filename)
4018                  (mail-file-babyl-p filename))
4019             (gnus-output-to-rmail filename)
4020           (gnus-output-to-mail filename)))))
4021   filename)
4022
4023 (put 'gnus-summary-save-in-file :decode t)
4024 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4025 (defun gnus-summary-save-in-file (&optional filename overwrite)
4026   "Append this article to file.
4027 Optional argument FILENAME specifies file name.
4028 Directory to save to is default to `gnus-article-save-directory'."
4029   (setq filename (gnus-read-save-file-name
4030                   "Save %s in file" filename
4031                   gnus-file-save-name gnus-newsgroup-name
4032                   gnus-current-headers 'gnus-newsgroup-last-file))
4033   (with-current-buffer gnus-save-article-buffer
4034     (save-excursion
4035       (save-restriction
4036         (widen)
4037         (when (and overwrite
4038                    (file-exists-p filename))
4039           (delete-file filename))
4040         (gnus-output-to-file filename))))
4041   filename)
4042
4043 (put 'gnus-summary-write-to-file :decode t)
4044 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4045 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4046 (defun gnus-summary-write-to-file (&optional filename)
4047   "Write this article to a file, overwriting it if the file exists.
4048 Optional argument FILENAME specifies file name.
4049 The directory to save in defaults to `gnus-article-save-directory'."
4050   (setq filename (gnus-read-save-file-name
4051                   "Save %s in file" filename
4052                   gnus-file-save-name gnus-newsgroup-name
4053                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4054   (gnus-summary-save-in-file filename t))
4055
4056 (put 'gnus-summary-save-body-in-file :decode t)
4057 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4058   "Append this article body to a file.
4059 Optional argument FILENAME specifies file name.
4060 The directory to save in defaults to `gnus-article-save-directory'."
4061   (setq filename (gnus-read-save-file-name
4062                   "Save %s body in file" filename
4063                   gnus-file-save-name gnus-newsgroup-name
4064                   gnus-current-headers 'gnus-newsgroup-last-file))
4065   (with-current-buffer gnus-save-article-buffer
4066     (save-excursion
4067       (save-restriction
4068         (widen)
4069         (when (article-goto-body)
4070           (narrow-to-region (point) (point-max)))
4071         (when (and overwrite
4072                    (file-exists-p filename))
4073           (delete-file filename))
4074         (gnus-output-to-file filename))))
4075   filename)
4076
4077 (put 'gnus-summary-write-body-to-file :decode t)
4078 (put 'gnus-summary-write-body-to-file
4079      :function 'gnus-summary-save-body-in-file)
4080 (defun gnus-summary-write-body-to-file (&optional filename)
4081   "Write this article body to a file, overwriting it if the file exists.
4082 Optional argument FILENAME specifies file name.
4083 The directory to save in defaults to `gnus-article-save-directory'."
4084   (setq filename (gnus-read-save-file-name
4085                   "Save %s body in file" filename
4086                   gnus-file-save-name gnus-newsgroup-name
4087                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4088   (gnus-summary-save-body-in-file filename t))
4089
4090 (put 'gnus-summary-save-in-pipe :decode t)
4091 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4092 (defun gnus-summary-save-in-pipe (&optional command raw)
4093   "Pipe this article to subprocess COMMAND.
4094 Valid values for COMMAND include:
4095   a string
4096     The executable command name and possibly arguments.
4097   nil
4098     You will be prompted for the command in the minibuffer.
4099   the symbol `default'
4100     It will be replaced with the command which the variable
4101     `gnus-summary-pipe-output-default-command' holds or the command
4102     last used for saving.
4103 Non-nil value for RAW overrides `:decode' and `:headers' properties
4104 and the raw article including all headers will be piped."
4105   (let ((article (gnus-summary-article-number))
4106         (decode (unless raw
4107                   (get 'gnus-summary-save-in-pipe :decode)))
4108         save-buffer default)
4109     (if article
4110         (if (vectorp (gnus-summary-article-header article))
4111             (save-current-buffer
4112               (gnus-summary-select-article decode decode nil article)
4113               (insert-buffer-substring
4114                (prog1
4115                    (if decode
4116                        gnus-article-buffer
4117                      gnus-original-article-buffer)
4118                  (setq save-buffer
4119                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4120               ;; Remove unwanted headers.
4121               (when (and (not raw)
4122                          (or (get 'gnus-summary-save-in-pipe :headers)
4123                              (not gnus-save-all-headers)))
4124                 (let ((gnus-visible-headers
4125                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4126                                               :headers))
4127                            gnus-saved-headers gnus-visible-headers))
4128                       (gnus-summary-buffer nil))
4129                   (article-hide-headers 1 t))))
4130           (error "%d is not a real article" article))
4131       (error "No article to pipe"))
4132     (setq default (or gnus-summary-pipe-output-default-command
4133                       gnus-last-shell-command))
4134     (unless (stringp command)
4135       (setq command
4136             (if (and (eq command 'default) default)
4137                 default
4138               (gnus-read-shell-command "Shell command on this article: "
4139                                        default))))
4140     (when (string-equal command "")
4141       (if default
4142           (setq command default)
4143         (error "A command is required")))
4144     (with-current-buffer save-buffer
4145       (save-restriction
4146         (widen)
4147         (shell-command-on-region (point-min) (point-max) command nil)))
4148     (gnus-kill-buffer save-buffer))
4149   (setq gnus-summary-pipe-output-default-command command))
4150
4151 (defun gnus-summary-pipe-to-muttprint (&optional command)
4152   "Pipe this article to muttprint."
4153   (unless (stringp command)
4154     (setq command (read-string
4155                    "Print using command: " gnus-summary-muttprint-program
4156                    nil gnus-summary-muttprint-program)))
4157   (let ((gnus-summary-pipe-output-default-command
4158          gnus-summary-pipe-output-default-command))
4159     (gnus-summary-save-in-pipe command))
4160   (setq gnus-summary-muttprint-program command))
4161
4162 ;;; Article file names when saving.
4163
4164 (defun gnus-capitalize-newsgroup (newsgroup)
4165   "Capitalize NEWSGROUP name."
4166   (when (not (zerop (length newsgroup)))
4167     (concat (char-to-string (upcase (aref newsgroup 0)))
4168             (substring newsgroup 1))))
4169
4170 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4171   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4172 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4173 Otherwise, it is like ~/News/news/group/num."
4174   (let ((default
4175           (expand-file-name
4176            (concat (if (gnus-use-long-file-name 'not-save)
4177                        (gnus-capitalize-newsgroup newsgroup)
4178                      (gnus-newsgroup-directory-form newsgroup))
4179                    "/" (int-to-string (mail-header-number headers)))
4180            gnus-article-save-directory)))
4181     (if (and last-file
4182              (string-equal (file-name-directory default)
4183                            (file-name-directory last-file))
4184              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4185         default
4186       (or last-file default))))
4187
4188 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4189   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4190 If variable `gnus-use-long-file-name' is non-nil, it is
4191 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4192   (let ((default
4193           (expand-file-name
4194            (concat (if (gnus-use-long-file-name 'not-save)
4195                        newsgroup
4196                      (gnus-newsgroup-directory-form newsgroup))
4197                    "/" (int-to-string (mail-header-number headers)))
4198            gnus-article-save-directory)))
4199     (if (and last-file
4200              (string-equal (file-name-directory default)
4201                            (file-name-directory last-file))
4202              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4203         default
4204       (or last-file default))))
4205
4206 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4207   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4208 If variable `gnus-use-long-file-name' is non-nil, it is
4209 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4210   (or last-file
4211       (expand-file-name
4212        (if (gnus-use-long-file-name 'not-save)
4213            newsgroup
4214          (file-relative-name
4215           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4216           default-directory))
4217        gnus-article-save-directory)))
4218
4219 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4220   "Generate file name from sender."
4221   (let ((from (mail-header-from headers)))
4222     (expand-file-name
4223      (if (and from (string-match "\\([^ <]+\\)@" from))
4224          (match-string 1 from)
4225        "nobody")
4226      gnus-article-save-directory)))
4227
4228 (defun article-verify-x-pgp-sig ()
4229   "Verify X-PGP-Sig."
4230   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4231   (interactive)
4232   (if (gnus-buffer-live-p gnus-original-article-buffer)
4233       (let ((sig (with-current-buffer gnus-original-article-buffer
4234                    (gnus-fetch-field "X-PGP-Sig")))
4235             items info headers)
4236         (when (and sig
4237                    mml2015-use
4238                    (mml2015-clear-verify-function))
4239           (with-temp-buffer
4240             (insert-buffer-substring gnus-original-article-buffer)
4241             (setq items (split-string sig))
4242             (message-narrow-to-head)
4243             (let ((inhibit-point-motion-hooks t)
4244                   (case-fold-search t))
4245               ;; Don't verify multiple headers.
4246               (setq headers (mapconcat (lambda (header)
4247                                          (concat header ": "
4248                                                  (mail-fetch-field header)
4249                                                  "\n"))
4250                                        (split-string (nth 1 items) ",") "")))
4251             (delete-region (point-min) (point-max))
4252             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4253             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4254             (insert headers)
4255             (widen)
4256             (forward-line)
4257             (while (not (eobp))
4258               (if (looking-at "^-")
4259                   (insert "- "))
4260               (forward-line))
4261             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4262             (insert "Version: " (car items) "\n\n")
4263             (insert (mapconcat 'identity (cddr items) "\n"))
4264             (insert "\n-----END PGP SIGNATURE-----\n")
4265             (let ((mm-security-handle (list (format "multipart/signed"))))
4266               (mml2015-clean-buffer)
4267               (let ((coding-system-for-write (or gnus-newsgroup-charset
4268                                                  'iso-8859-1)))
4269                 (funcall (mml2015-clear-verify-function)))
4270               (setq info
4271                     (or (mm-handle-multipart-ctl-parameter
4272                          mm-security-handle 'gnus-details)
4273                         (mm-handle-multipart-ctl-parameter
4274                          mm-security-handle 'gnus-info)))))
4275           (when info
4276             (let ((inhibit-read-only t) bface eface)
4277               (save-restriction
4278                 (message-narrow-to-head)
4279                 (goto-char (point-max))
4280                 (forward-line -1)
4281                 (setq bface (get-text-property (point-at-bol) 'face)
4282                       eface (get-text-property (1- (point-at-eol)) 'face))
4283                 (message-remove-header "X-Gnus-PGP-Verify")
4284                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4285                     (forward-line)
4286                   (goto-char (point-max)))
4287                 (narrow-to-region (point) (point))
4288                 (insert "X-Gnus-PGP-Verify: " info "\n")
4289                 (goto-char (point-min))
4290                 (forward-line)
4291                 (while (not (eobp))
4292                   (if (not (looking-at "^[ \t]"))
4293                       (insert " "))
4294                   (forward-line))
4295                 ;; Do highlighting.
4296                 (goto-char (point-min))
4297                 (when (looking-at "\\([^:]+\\): *")
4298                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4299                                      'face bface)
4300                   (put-text-property (match-end 0) (point-max)
4301                                      'face eface)))))))))
4302
4303 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4304
4305 (defun article-verify-cancel-lock ()
4306   "Verify Cancel-Lock header."
4307   (interactive)
4308   (if (gnus-buffer-live-p gnus-original-article-buffer)
4309       (canlock-verify gnus-original-article-buffer)))
4310
4311 (eval-and-compile
4312   (mapc
4313    (lambda (func)
4314      (let (afunc gfunc)
4315        (if (consp func)
4316            (setq afunc (car func)
4317                  gfunc (cdr func))
4318          (setq afunc func
4319                gfunc (intern (format "gnus-%s" func))))
4320        (defalias gfunc
4321          (when (fboundp afunc)
4322            `(lambda (&optional interactive &rest args)
4323               ,(documentation afunc t)
4324               (interactive (list t))
4325               (with-current-buffer gnus-article-buffer
4326                 (if interactive
4327                     (call-interactively ',afunc)
4328                   (apply ',afunc args))))))))
4329    '(article-hide-headers
4330      article-verify-x-pgp-sig
4331      article-verify-cancel-lock
4332      article-hide-boring-headers
4333      article-treat-overstrike
4334      article-treat-ansi-sequences
4335      article-fill-long-lines
4336      article-capitalize-sentences
4337      article-remove-cr
4338      article-remove-leading-whitespace
4339      article-display-x-face
4340      article-display-face
4341      article-de-quoted-unreadable
4342      article-de-base64-unreadable
4343      article-decode-HZ
4344      article-wash-html
4345      article-unsplit-urls
4346      article-hide-list-identifiers
4347      article-strip-banner
4348      article-babel
4349      article-hide-pem
4350      article-hide-signature
4351      article-strip-headers-in-body
4352      article-remove-trailing-blank-lines
4353      article-strip-leading-blank-lines
4354      article-strip-multiple-blank-lines
4355      article-strip-leading-space
4356      article-strip-trailing-space
4357      article-strip-blank-lines
4358      article-strip-all-blank-lines
4359      article-date-local
4360      article-date-english
4361      article-date-iso8601
4362      article-date-original
4363      article-treat-date
4364      article-date-ut
4365      article-decode-mime-words
4366      article-decode-charset
4367      article-decode-encoded-words
4368      article-date-user
4369      article-date-lapsed
4370      article-date-combined-lapsed
4371      article-emphasize
4372      article-treat-dumbquotes
4373      article-treat-non-ascii
4374      article-normalize-headers
4375      ;;(article-show-all . gnus-article-show-all-headers)
4376      )))
4377 \f
4378 ;;;
4379 ;;; Gnus article mode
4380 ;;;
4381
4382 (put 'gnus-article-mode 'mode-class 'special)
4383
4384 (set-keymap-parent gnus-article-mode-map widget-keymap)
4385
4386 (gnus-define-keys gnus-article-mode-map
4387   " " gnus-article-goto-next-page
4388   "\177" gnus-article-goto-prev-page
4389   [delete] gnus-article-goto-prev-page
4390   [backspace] gnus-article-goto-prev-page
4391   "\C-c^" gnus-article-refer-article
4392   "h" gnus-article-show-summary
4393   "s" gnus-article-show-summary
4394   "\C-c\C-m" gnus-article-mail
4395   "?" gnus-article-describe-briefly
4396   "<" beginning-of-buffer
4397   ">" end-of-buffer
4398   "\C-c\C-i" gnus-info-find-node
4399   "\C-c\C-b" gnus-bug
4400   "R" gnus-article-reply-with-original
4401   "F" gnus-article-followup-with-original
4402   "\C-hk" gnus-article-describe-key
4403   "\C-hc" gnus-article-describe-key-briefly
4404   "\C-hb" gnus-article-describe-bindings
4405
4406   "e" gnus-article-read-summary-keys
4407   "\C-d" gnus-article-read-summary-keys
4408   "\M-*" gnus-article-read-summary-keys
4409   "\M-#" gnus-article-read-summary-keys
4410   "\M-^" gnus-article-read-summary-keys
4411   "\M-g" gnus-article-read-summary-keys)
4412
4413 (substitute-key-definition
4414  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4415
4416 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4417   "W" gnus-article-wide-reply-with-original)
4418 (if (featurep 'xemacs)
4419     (set-keymap-default-binding gnus-article-send-map
4420                                 'gnus-article-read-summary-send-keys)
4421   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4422
4423 (defun gnus-article-make-menu-bar ()
4424   (unless (boundp 'gnus-article-commands-menu)
4425     (gnus-summary-make-menu-bar))
4426   (unless (boundp 'gnus-article-article-menu)
4427     (easy-menu-define
4428      gnus-article-article-menu gnus-article-mode-map ""
4429      '("Article"
4430        ["Scroll forwards" gnus-article-goto-next-page t]
4431        ["Scroll backwards" gnus-article-goto-prev-page t]
4432        ["Show summary" gnus-article-show-summary t]
4433        ["Fetch Message-ID at point" gnus-article-refer-article t]
4434        ["Mail to address at point" gnus-article-mail t]
4435        ["Send a bug report" gnus-bug t]))
4436
4437     (easy-menu-define
4438      gnus-article-treatment-menu gnus-article-mode-map ""
4439      ;; Fixme: this should use :active (and maybe :visible).
4440      '("Treatment"
4441        ["Hide headers" gnus-article-hide-headers t]
4442        ["Hide signature" gnus-article-hide-signature t]
4443        ["Hide citation" gnus-article-hide-citation t]
4444        ["Treat overstrike" gnus-article-treat-overstrike t]
4445        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4446        ["Remove carriage return" gnus-article-remove-cr t]
4447        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4448        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4449        ["Remove base64" gnus-article-de-base64-unreadable t]
4450        ["Treat html" gnus-article-wash-html t]
4451        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4452        ["Decode HZ" gnus-article-decode-HZ t]))
4453
4454     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4455
4456     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4457
4458     (gnus-run-hooks 'gnus-article-menu-hook)))
4459
4460 (defvar bookmark-make-record-function)
4461 (defvar shr-put-image-function)
4462
4463 (defun gnus-article-mode ()
4464   "Major mode for displaying an article.
4465
4466 All normal editing commands are switched off.
4467
4468 The following commands are available in addition to all summary mode
4469 commands:
4470 \\<gnus-article-mode-map>
4471 \\[gnus-article-next-page]\t Scroll the article one page forwards
4472 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4473 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4474 \\[gnus-article-show-summary]\t Display the summary buffer
4475 \\[gnus-article-mail]\t Send a reply to the address near point
4476 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4477 \\[gnus-info-find-node]\t Go to the Gnus info node"
4478   (interactive)
4479   (kill-all-local-variables)
4480   (gnus-simplify-mode-line)
4481   (setq mode-name "Article")
4482   (setq major-mode 'gnus-article-mode)
4483   (make-local-variable 'minor-mode-alist)
4484   (use-local-map gnus-article-mode-map)
4485   (when (gnus-visual-p 'article-menu 'menu)
4486     (gnus-article-make-menu-bar)
4487     (when gnus-summary-tool-bar-map
4488       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4489   (gnus-update-format-specifications nil 'article-mode)
4490   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4491   (set (make-local-variable 'gnus-page-broken) nil)
4492   (make-local-variable 'gnus-article-current-summary)
4493   (make-local-variable 'gnus-article-mime-handles)
4494   (make-local-variable 'gnus-article-decoded-p)
4495   (make-local-variable 'gnus-article-mime-handle-alist)
4496   (make-local-variable 'gnus-article-wash-types)
4497   (make-local-variable 'gnus-article-image-alist)
4498   (make-local-variable 'gnus-article-charset)
4499   (make-local-variable 'gnus-article-ignored-charsets)
4500   (set (make-local-variable 'bookmark-make-record-function)
4501        'gnus-summary-bookmark-make-record)
4502   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4503   ;; face.
4504   (set (make-local-variable 'nobreak-char-display) nil)
4505   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4506   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4507   (setq cursor-in-non-selected-windows nil)
4508   (gnus-set-default-directory)
4509   (buffer-disable-undo)
4510   (setq buffer-read-only t
4511         show-trailing-whitespace nil)
4512   (set-syntax-table gnus-article-mode-syntax-table)
4513   (mm-enable-multibyte)
4514   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4515
4516 (defun gnus-article-setup-buffer ()
4517   "Initialize the article buffer."
4518   (let* ((name (if gnus-single-article-buffer "*Article*"
4519                  (concat "*Article "
4520                          (gnus-group-decoded-name gnus-newsgroup-name)
4521                          "*")))
4522          (original
4523           (progn (string-match "\\*Article" name)
4524                  (concat " *Original Article"
4525                          (substring name (match-end 0))))))
4526     (setq gnus-article-buffer name)
4527     (setq gnus-original-article-buffer original)
4528     (setq gnus-article-mime-handle-alist nil)
4529     (with-current-buffer gnus-summary-buffer
4530       ;; This might be a variable local to the summary buffer.
4531       (unless gnus-single-article-buffer
4532         (setq gnus-article-buffer name)
4533         (setq gnus-original-article-buffer original)
4534         (gnus-set-global-variables)))
4535     (gnus-article-setup-highlight-words)
4536     ;; Init original article buffer.
4537     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4538       (mm-enable-multibyte)
4539       (setq major-mode 'gnus-original-article-mode)
4540       (make-local-variable 'gnus-original-article))
4541     (if (and (get-buffer name)
4542              (with-current-buffer name
4543                (if gnus-article-edit-mode
4544                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4545                        (progn
4546                          (set-buffer-modified-p nil)
4547                          (gnus-kill-buffer name)
4548                          (message "")
4549                          nil)
4550                      (error "Action aborted"))
4551                  t)))
4552         (with-current-buffer name
4553           (set (make-local-variable 'gnus-article-edit-mode) nil)
4554           (gnus-article-stop-animations)
4555           (when gnus-article-mime-handles
4556             (mm-destroy-parts gnus-article-mime-handles)
4557             (setq gnus-article-mime-handles nil))
4558           ;; Set it to nil in article-buffer!
4559           (setq gnus-article-mime-handle-alist nil)
4560           (buffer-disable-undo)
4561           (setq buffer-read-only t)
4562           (unless (eq major-mode 'gnus-article-mode)
4563             (gnus-article-mode))
4564           (setq truncate-lines gnus-article-truncate-lines)
4565           (current-buffer))
4566       (with-current-buffer (gnus-get-buffer-create name)
4567         (gnus-article-mode)
4568         (setq truncate-lines gnus-article-truncate-lines)
4569         (make-local-variable 'gnus-summary-buffer)
4570         (setq gnus-summary-buffer
4571               (gnus-summary-buffer-name gnus-newsgroup-name))
4572         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4573         (when article-lapsed-timer
4574           (gnus-stop-date-timer))
4575         (when gnus-article-update-date-headers
4576           (gnus-start-date-timer gnus-article-update-date-headers))
4577         (current-buffer)))))
4578
4579 (defun gnus-article-stop-animations ()
4580   (dolist (timer (and (boundp 'timer-list)
4581                       timer-list))
4582     (when (eq (elt timer 5) 'image-animate-timeout)
4583       (cancel-timer timer))))
4584
4585 ;; Set article window start at LINE, where LINE is the number of lines
4586 ;; from the head of the article.
4587 (defun gnus-article-set-window-start (&optional line)
4588   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4589     (when article-window
4590       (set-window-start
4591        article-window
4592        (with-current-buffer gnus-article-buffer
4593          (goto-char (point-min))
4594          (if (not line)
4595              (point-min)
4596            (gnus-message 6 "Moved to bookmark")
4597            (search-forward "\n\n" nil t)
4598            (forward-line line)
4599            (point)))))))
4600
4601 (defun gnus-article-prepare (article &optional all-headers header)
4602   "Prepare ARTICLE in article mode buffer.
4603 ARTICLE should either be an article number or a Message-ID.
4604 If ARTICLE is an id, HEADER should be the article headers.
4605 If ALL-HEADERS is non-nil, no headers are hidden."
4606   (save-excursion
4607     ;; Make sure we start in a summary buffer.
4608     (unless (eq major-mode 'gnus-summary-mode)
4609       (set-buffer gnus-summary-buffer))
4610     (setq gnus-summary-buffer (current-buffer))
4611     (let* ((gnus-article (if header (mail-header-number header) article))
4612            (summary-buffer (current-buffer))
4613            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4614            (group gnus-newsgroup-name)
4615            result)
4616       (save-excursion
4617         (gnus-article-setup-buffer)
4618         (set-buffer gnus-article-buffer)
4619         ;; Deactivate active regions.
4620         (when (and (boundp 'transient-mark-mode)
4621                    transient-mark-mode)
4622           (setq mark-active nil))
4623         (if (not (setq result (let ((inhibit-read-only t))
4624                                 (gnus-request-article-this-buffer
4625                                  article group))))
4626             ;; There is no such article.
4627             (save-excursion
4628               (when (and (numberp article)
4629                          (not (memq article gnus-newsgroup-sparse)))
4630                 (setq gnus-article-current
4631                       (cons gnus-newsgroup-name article))
4632                 (set-buffer gnus-summary-buffer)
4633                 (setq gnus-current-article article)
4634                 (if (and (memq article gnus-newsgroup-undownloaded)
4635                          (not (gnus-online (gnus-find-method-for-group
4636                                             gnus-newsgroup-name))))
4637                     (progn
4638                       (gnus-summary-set-agent-mark article)
4639                       (message "Message marked for downloading"))
4640                   (gnus-summary-mark-article article gnus-canceled-mark)
4641                   (unless (memq article gnus-newsgroup-sparse)
4642                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4643           (if (or (eq result 'pseudo)
4644                   (eq result 'nneething))
4645               (progn
4646                 (with-current-buffer summary-buffer
4647                   (push article gnus-newsgroup-history)
4648                   (setq gnus-last-article gnus-current-article
4649                         gnus-current-article 0
4650                         gnus-current-headers nil
4651                         gnus-article-current nil)
4652                   (if (eq result 'nneething)
4653                       (gnus-configure-windows 'summary)
4654                     (gnus-configure-windows 'article))
4655                   (gnus-set-global-variables))
4656                 (let ((gnus-article-mime-handle-alist-1
4657                        gnus-article-mime-handle-alist))
4658                   (gnus-set-mode-line 'article)))
4659             ;; The result from the `request' was an actual article -
4660             ;; or at least some text that is now displayed in the
4661             ;; article buffer.
4662             (when (and (numberp article)
4663                        (not (eq article gnus-current-article)))
4664               ;; Seems like a new article has been selected.
4665               ;; `gnus-current-article' must be an article number.
4666               (with-current-buffer summary-buffer
4667                 (push article gnus-newsgroup-history)
4668                 (setq gnus-last-article gnus-current-article
4669                       gnus-current-article article
4670                       gnus-current-headers
4671                       (gnus-summary-article-header gnus-current-article)
4672                       gnus-article-current
4673                       (cons gnus-newsgroup-name gnus-current-article))
4674                 (unless (vectorp gnus-current-headers)
4675                   (setq gnus-current-headers nil))
4676                 (gnus-summary-goto-subject gnus-current-article)
4677                 (when (gnus-summary-show-thread)
4678                   ;; If the summary buffer really was folded, the
4679                   ;; previous goto may not actually have gone to
4680                   ;; the right article, but the thread root instead.
4681                   ;; So we go again.
4682                   (gnus-summary-goto-subject gnus-current-article))
4683                 (gnus-run-hooks 'gnus-mark-article-hook)
4684                 (gnus-set-mode-line 'summary)
4685                 (when (gnus-visual-p 'article-highlight 'highlight)
4686                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4687                 ;; Set the global newsgroup variables here.
4688                 (gnus-set-global-variables)
4689                 (setq gnus-have-all-headers
4690                       (or all-headers gnus-show-all-headers))))
4691             (save-excursion
4692               (gnus-configure-windows 'article))
4693             (when (or (numberp article)
4694                       (stringp article))
4695               (gnus-article-prepare-display)
4696               ;; Do page break.
4697               (goto-char (point-min))
4698               (when gnus-break-pages
4699                 (gnus-narrow-to-page)))
4700             (let ((gnus-article-mime-handle-alist-1
4701                    gnus-article-mime-handle-alist))
4702               (gnus-set-mode-line 'article))
4703             (article-goto-body)
4704             (unless (bobp)
4705               (forward-line -1))
4706             (set-window-point (get-buffer-window (current-buffer)) (point))
4707             (gnus-configure-windows 'article)
4708             (gnus-run-hooks 'gnus-article-prepare-hook)
4709             t))))))
4710
4711 ;;;###autoload
4712 (defun gnus-article-prepare-display ()
4713   "Make the current buffer look like a nice article."
4714   ;; Hooks for getting information from the article.
4715   ;; This hook must be called before being narrowed.
4716   (let ((gnus-article-buffer (current-buffer))
4717         buffer-read-only
4718         (inhibit-read-only t))
4719     (unless (eq major-mode 'gnus-article-mode)
4720       (gnus-article-mode))
4721     (setq buffer-read-only nil
4722           gnus-article-wash-types nil
4723           gnus-article-image-alist nil)
4724     (gnus-run-hooks 'gnus-tmp-internal-hook)
4725     (when gnus-display-mime-function
4726       (funcall gnus-display-mime-function))))
4727
4728 ;;;
4729 ;;; Gnus Sticky Article Mode
4730 ;;;
4731
4732 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4733   "Mode for sticky articles."
4734   ;; Release bindings that won't work.
4735   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4736                              gnus-sticky-article-mode-map)
4737   (substitute-key-definition 'gnus-article-refer-article 'undefined
4738                              gnus-sticky-article-mode-map)
4739   (dolist (k '("e" "h" "s" "F" "R"))
4740     (define-key gnus-sticky-article-mode-map k nil))
4741   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4742   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4743   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4744   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4745
4746 (defun gnus-sticky-article (arg)
4747   "Make the current article sticky.
4748 If a prefix ARG is given, ask for a name for this sticky article buffer."
4749   (interactive "P")
4750   (gnus-summary-show-thread)
4751   (gnus-summary-select-article nil nil 'pseudo)
4752   (let (new-art-buf-name)
4753     (gnus-eval-in-buffer-window gnus-article-buffer
4754       (setq new-art-buf-name
4755             (concat
4756              "*Sticky Article: "
4757              (if arg
4758                  (read-from-minibuffer "Sticky article buffer name: ")
4759                (gnus-with-article-headers
4760                  (gnus-article-goto-header "subject")
4761                  (setq new-art-buf-name
4762                        (buffer-substring-no-properties
4763                         (line-beginning-position) (line-end-position)))
4764                  (goto-char (point-min))
4765                  (gnus-article-goto-header "from")
4766                  (setq new-art-buf-name
4767                        (concat
4768                         new-art-buf-name ", "
4769                         (buffer-substring-no-properties
4770                          (line-beginning-position) (line-end-position))))
4771                  (goto-char (point-min))
4772                  (gnus-article-goto-header "date")
4773                  (setq new-art-buf-name
4774                        (concat
4775                         new-art-buf-name ", "
4776                         (buffer-substring-no-properties
4777                          (line-beginning-position) (line-end-position))))))
4778              "*"))
4779       (if (and (gnus-buffer-live-p new-art-buf-name)
4780                (with-current-buffer new-art-buf-name
4781                  (eq major-mode 'gnus-sticky-article-mode)))
4782           (switch-to-buffer new-art-buf-name)
4783         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4784       (gnus-sticky-article-mode))
4785     (setq gnus-article-buffer new-art-buf-name))
4786   (gnus-summary-recenter)
4787   (gnus-summary-position-point))
4788
4789 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4790   "Kill the given sticky article BUFFER.
4791 If none is given, assume the current buffer and kill it if it has
4792 `gnus-sticky-article-mode'."
4793   (interactive)
4794   (unless buffer
4795     (setq buffer (current-buffer)))
4796   (with-current-buffer buffer
4797     (when (eq major-mode 'gnus-sticky-article-mode)
4798       (gnus-kill-buffer buffer))))
4799
4800 (defun gnus-kill-sticky-article-buffers (arg)
4801   "Kill all sticky article buffers.
4802 If a prefix ARG is given, ask for confirmation."
4803   (interactive "P")
4804   (dolist (buf (gnus-buffers))
4805     (with-current-buffer buf
4806       (when (eq major-mode 'gnus-sticky-article-mode)
4807         (if (not arg)
4808             (gnus-kill-buffer buf)
4809           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4810             (gnus-kill-buffer buf)))))))
4811
4812 ;;;
4813 ;;; Gnus MIME viewing functions
4814 ;;;
4815
4816 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4817   "Format of the MIME buttons.
4818
4819 Valid specifiers include:
4820 %t  The MIME type
4821 %T  MIME type, along with additional info
4822 %n  The `name' parameter
4823 %d  The description, if any
4824 %l  The length of the encoded part
4825 %p  The part identifier number
4826 %e  Dots if the part isn't displayed
4827
4828 General format specifiers can also be used.  See Info node
4829 `(gnus)Formatting Variables'.")
4830
4831 (defvar gnus-mime-button-line-format-alist
4832   '((?t gnus-tmp-type ?s)
4833     (?T gnus-tmp-type-long ?s)
4834     (?n gnus-tmp-name ?s)
4835     (?d gnus-tmp-description ?s)
4836     (?p gnus-tmp-id ?s)
4837     (?l gnus-tmp-length ?d)
4838     (?e gnus-tmp-dots ?s)))
4839
4840 (defvar gnus-mime-button-commands
4841   '((gnus-article-press-button "\r" "Toggle Display")
4842     (gnus-mime-view-part "v" "View Interactively...")
4843     (gnus-mime-view-part-as-type "t" "View As Type...")
4844     (gnus-mime-view-part-as-charset "C" "View As charset...")
4845     (gnus-mime-save-part "o" "Save...")
4846     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4847     (gnus-mime-replace-part "r" "Replace part")
4848     (gnus-mime-delete-part "d" "Delete part")
4849     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4850     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4851     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4852     (gnus-mime-view-part-externally "e" "View Externally")
4853     (gnus-mime-print-part "p" "Print")
4854     (gnus-mime-pipe-part "|" "Pipe To Command...")
4855     (gnus-mime-action-on-part "." "Take action on the part...")))
4856
4857 (defun gnus-article-mime-part-status ()
4858   (if gnus-article-mime-handle-alist-1
4859       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4860           " (1 part)"
4861         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4862     ""))
4863
4864 (defvar gnus-mime-button-map
4865   (let ((map (make-sparse-keymap)))
4866     (define-key map gnus-mouse-2 'gnus-article-push-button)
4867     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4868     (dolist (c gnus-mime-button-commands)
4869       (define-key map (cadr c) (car c)))
4870     map))
4871
4872 (easy-menu-define
4873   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4874   `("MIME Part"
4875     ,@(mapcar (lambda (c)
4876                 (vector (caddr c) (car c) :active t))
4877               gnus-mime-button-commands)))
4878
4879 (defvar gnus-url-button-commands
4880   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4881
4882 (defvar gnus-url-button-map
4883   (let ((map (make-sparse-keymap)))
4884     (dolist (c gnus-url-button-commands)
4885       (define-key map (cadr c) (car c)))
4886     map))
4887
4888 (easy-menu-define
4889   gnus-url-button-menu gnus-url-button-map "URL button menu."
4890   `("Url Button"
4891     ,@(mapcar (lambda (c)
4892                 (vector (caddr c) (car c) :active t))
4893               gnus-url-button-commands)))
4894
4895 (defmacro gnus-bind-safe-url-regexp (&rest body)
4896   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4897   `(let ((mm-w3m-safe-url-regexp
4898           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4899                                 (gnus-buffer-live-p
4900                                  gnus-article-current-summary))
4901                            (with-current-buffer gnus-article-current-summary
4902                              gnus-newsgroup-name)
4903                          gnus-newsgroup-name)))
4904             (if (cond ((not group)
4905                        ;; Maybe we're in a mml-preview buffer
4906                        ;; and no group is selected.
4907                        t)
4908                       ((stringp gnus-safe-html-newsgroups)
4909                        (string-match gnus-safe-html-newsgroups group))
4910                       ((consp gnus-safe-html-newsgroups)
4911                        (member group gnus-safe-html-newsgroups)))
4912                 nil
4913               mm-w3m-safe-url-regexp))))
4914      ,@body))
4915
4916 (defun gnus-mime-button-menu (event prefix)
4917  "Construct a context-sensitive menu of MIME commands."
4918  (interactive "e\nP")
4919  (save-window-excursion
4920    (let ((pos (event-start event)))
4921      (select-window (posn-window pos))
4922      (goto-char (posn-point pos))
4923      (gnus-article-check-buffer)
4924      (popup-menu gnus-mime-button-menu nil prefix))))
4925
4926 (defun gnus-mime-view-all-parts (&optional handles)
4927   "View all the MIME parts."
4928   (interactive)
4929   (with-current-buffer gnus-article-buffer
4930     (let ((handles (or handles gnus-article-mime-handles))
4931           (mail-parse-charset gnus-newsgroup-charset)
4932           (mail-parse-ignored-charsets
4933            (with-current-buffer gnus-summary-buffer
4934              gnus-newsgroup-ignored-charsets)))
4935       (when handles
4936         (mm-remove-parts handles)
4937         (goto-char (point-min))
4938         (or (search-forward "\n\n") (goto-char (point-max)))
4939         (let ((inhibit-read-only t))
4940           (delete-region (point) (point-max))
4941           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4942
4943 (defun gnus-article-jump-to-part (n)
4944   "Jump to MIME part N."
4945   (interactive "P")
4946   (let ((parts (with-current-buffer gnus-article-buffer
4947                  (length gnus-article-mime-handle-alist))))
4948     (when (zerop parts)
4949       (error "No such part"))
4950     (pop-to-buffer gnus-article-buffer)
4951     (or n
4952         (setq n (if (= parts 1)
4953                     1
4954                   (read-number (format "Jump to part (1..%s): " parts)))))
4955     (unless (and (integerp n) (<= n parts) (>= n 1))
4956       (setq n
4957             (progn
4958               (gnus-message 7 "Invalid part `%s', using %s instead."
4959                             n parts)
4960               parts)))
4961     (gnus-message 9 "Jumping to part %s." n)
4962     (cond ((>= gnus-auto-select-part 1)
4963            (while (and (<= n parts)
4964                        (not (gnus-article-goto-part n)))
4965              (setq n (1+ n))))
4966           ((< gnus-auto-select-part 0)
4967            (while (and (>= n 1)
4968                        (not (gnus-article-goto-part n)))
4969              (setq n (1- n))))
4970           (t
4971            (gnus-article-goto-part n)))))
4972
4973 (defvar gnus-mime-buttonized-part-id nil
4974   "ID of a mime part that should be buttonized.
4975 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4976
4977 (eval-when-compile
4978   (defsubst gnus-article-edit-part (handles &optional current-id)
4979     "Edit an article in order to delete a mime part.
4980 This function is exclusively used by `gnus-mime-save-part-and-strip'
4981 and `gnus-mime-delete-part', and not provided at run-time normally."
4982     (gnus-article-edit-article
4983      `(lambda ()
4984         (buffer-disable-undo)
4985         (erase-buffer)
4986         (let ((mail-parse-charset (or gnus-article-charset
4987                                       ',gnus-newsgroup-charset))
4988               (mail-parse-ignored-charsets
4989                (or gnus-article-ignored-charsets
4990                    ',gnus-newsgroup-ignored-charsets))
4991               (mbl mml-buffer-list))
4992           (setq mml-buffer-list nil)
4993           (insert-buffer-substring gnus-original-article-buffer)
4994           (mime-to-mml ',handles)
4995           (setq gnus-article-mime-handles nil)
4996           (let ((mbl1 mml-buffer-list))
4997             (setq mml-buffer-list mbl)
4998             (set (make-local-variable 'mml-buffer-list) mbl1))
4999           (gnus-make-local-hook 'kill-buffer-hook)
5000           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
5001      `(lambda (no-highlight)
5002         (let ((mail-parse-charset (or gnus-article-charset
5003                                       ',gnus-newsgroup-charset))
5004               (message-options message-options)
5005               (message-options-set-recipient)
5006               (mail-parse-ignored-charsets
5007                (or gnus-article-ignored-charsets
5008                    ',gnus-newsgroup-ignored-charsets)))
5009           (mml-to-mime)
5010           (mml-destroy-buffers)
5011           (remove-hook 'kill-buffer-hook
5012                        'mml-destroy-buffers t)
5013           (kill-local-variable 'mml-buffer-list))
5014         (gnus-summary-edit-article-done
5015          ,(or (mail-header-references gnus-current-headers) "")
5016          ,(gnus-group-read-only-p)
5017          ,gnus-summary-buffer no-highlight))
5018      t)
5019     ;; Force buttonizing this part.
5020     (let ((gnus-mime-buttonized-part-id current-id))
5021       (gnus-article-edit-done))
5022     (gnus-configure-windows 'article)
5023     (when (and current-id (integerp gnus-auto-select-part))
5024       (gnus-article-jump-to-part
5025        (min (max (+ current-id gnus-auto-select-part) 1)
5026             (with-current-buffer gnus-article-buffer
5027               (length gnus-article-mime-handle-alist)))))))
5028
5029 (defun gnus-mime-replace-part (file)
5030   "Replace MIME part under point with an external body."
5031   ;; Useful if file has already been saved to disk
5032   (interactive
5033    (list
5034     (read-file-name "Replace MIME part with file: "
5035                     (or mm-default-directory default-directory)
5036                     nil nil)))
5037   (gnus-mime-save-part-and-strip file))
5038
5039 (defun gnus-mime-save-part-and-strip (&optional file)
5040   "Save the MIME part under point then replace it with an external body.
5041 If FILE is given, use it for the external part."
5042   (interactive)
5043   (gnus-article-check-buffer)
5044   (when (gnus-group-read-only-p)
5045     (error "The current group does not support deleting of parts"))
5046   (when (mm-complicated-handles gnus-article-mime-handles)
5047     (error "\
5048 The current article has a complicated MIME structure, giving up..."))
5049   (let* ((data (get-text-property (point) 'gnus-data))
5050          (id (get-text-property (point) 'gnus-part))
5051          param
5052          (handles gnus-article-mime-handles))
5053     (unless file
5054       (setq file
5055             (and data (mm-save-part data "Delete MIME part and save to: "))))
5056     (when file
5057       (with-current-buffer (mm-handle-buffer data)
5058         (erase-buffer)
5059         (insert "Content-Type: " (mm-handle-media-type data))
5060         (mml-insert-parameter-string (cdr (mm-handle-type data))
5061                                      '(charset))
5062         ;; Add a filename for the sake of saving the part again.
5063         (mml-insert-parameter
5064          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5065         (insert "\n")
5066         (insert "Content-ID: " (message-make-message-id) "\n")
5067         (insert "Content-Transfer-Encoding: binary\n")
5068         (insert "\n"))
5069       (setcdr data
5070               (cdr (mm-make-handle nil
5071                                    `("message/external-body"
5072                                      (access-type . "LOCAL-FILE")
5073                                      (name . ,file)))))
5074       ;; (set-buffer gnus-summary-buffer)
5075       (gnus-article-edit-part handles id))))
5076
5077 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5078 ;; parts...>') but with stripping would be nice.
5079
5080 (defun gnus-mime-delete-part ()
5081   "Delete the MIME part under point.
5082 Replace it with some information about the removed part."
5083   (interactive)
5084   (gnus-article-check-buffer)
5085   (when (gnus-group-read-only-p)
5086     (error "The current group does not support deleting of parts"))
5087   (when (mm-complicated-handles gnus-article-mime-handles)
5088     (error "\
5089 The current article has a complicated MIME structure, giving up..."))
5090   (when (or gnus-expert-user
5091             (gnus-yes-or-no-p "\
5092 Deleting parts may malfunction or destroy the article; continue? "))
5093     (let* ((data (get-text-property (point) 'gnus-data))
5094            (id (get-text-property (point) 'gnus-part))
5095            (handles gnus-article-mime-handles)
5096            (description
5097             (let ((desc (mm-handle-description data)))
5098               (when desc
5099                 (mail-decode-encoded-word-string desc))))
5100            (filename (or (mm-handle-filename data) "(none)"))
5101            (type (mm-handle-media-type data)))
5102       (unless data
5103         (error "No MIME part under point"))
5104       (with-current-buffer (mm-handle-buffer data)
5105         (let ((bsize (buffer-size)))
5106           (erase-buffer)
5107           (insert
5108            (concat
5109             ",----\n"
5110             "| The following attachment has been deleted:\n"
5111             "|\n"
5112             "| Type:           " type "\n"
5113             "| Filename:       " filename "\n"
5114             "| Size (encoded): " (format "%s byte%s\n"
5115                                          bsize (if (= bsize 1)
5116                                                    ""
5117                                                  "s"))
5118             (when description
5119               (concat    "| Description:    " description "\n"))
5120             "`----\n"))
5121           (setcdr data
5122                   (cdr (mm-make-handle
5123                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5124                         (list "attachment")
5125                         (format "Deleted attachment (%s bytes)" bsize))))))
5126       ;; (set-buffer gnus-summary-buffer)
5127       (gnus-article-edit-part handles id))))
5128
5129 (defun gnus-mime-save-part ()
5130   "Save the MIME part under point."
5131   (interactive)
5132   (gnus-article-check-buffer)
5133   (let ((data (get-text-property (point) 'gnus-data)))
5134     (when data
5135       (mm-save-part data))))
5136
5137 (defun gnus-mime-pipe-part (&optional cmd)
5138   "Pipe the MIME part under point to a process.
5139 Use CMD as the process."
5140   (interactive)
5141   (gnus-article-check-buffer)
5142   (let ((data (get-text-property (point) 'gnus-data)))
5143     (when data
5144       (mm-pipe-part data cmd))))
5145
5146 (defun gnus-mime-view-part ()
5147   "Interactively choose a viewing method for the MIME part under point."
5148   (interactive)
5149   (gnus-article-check-buffer)
5150   (let ((data (get-text-property (point) 'gnus-data)))
5151     (when data
5152       (setq gnus-article-mime-handles
5153             (mm-merge-handles
5154              gnus-article-mime-handles (setq data (copy-sequence data))))
5155       (mm-interactively-view-part data))))
5156
5157 (defun gnus-mime-view-part-as-type-internal ()
5158   (gnus-article-check-buffer)
5159   (let* ((handle (get-text-property (point) 'gnus-data))
5160          (name (or
5161                 ;; Content-Type: foo/bar; name=...
5162                 (mail-content-type-get (mm-handle-type handle) 'name)
5163                 ;; Content-Disposition: attachment; filename=...
5164                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5165          (def-type (and name (mm-default-file-encoding name))))
5166     (or (and def-type (cons def-type 0))
5167         (and handle
5168              (equal (mm-handle-media-supertype handle) "text")
5169              '("text/plain" . 0))
5170         '("application/octet-stream" . 0))))
5171
5172 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5173   "Choose a MIME media type, and view the part as such.
5174 If non-nil, PRED is a predicate to use during completion to limit the
5175 available media-types."
5176   (interactive)
5177   (unless mime-type
5178     (setq mime-type
5179           (let ((default (gnus-mime-view-part-as-type-internal)))
5180             (gnus-completing-read
5181              "View as MIME type"
5182              (if pred
5183                  (gnus-remove-if-not pred (mailcap-mime-types))
5184                (mailcap-mime-types))
5185              nil nil nil
5186              (car default)))))
5187   (gnus-article-check-buffer)
5188   (let ((handle (get-text-property (point) 'gnus-data)))
5189     (when handle
5190       (when (equal (mm-handle-media-type handle) "message/external-body")
5191         (unless (mm-handle-cache handle)
5192           (mm-extern-cache-contents handle))
5193         (setq handle (mm-handle-cache handle)))
5194       (setq handle
5195             (mm-make-handle (mm-handle-buffer handle)
5196                             (cons mime-type (cdr (mm-handle-type handle)))
5197                             (mm-handle-encoding handle)
5198                             (mm-handle-undisplayer handle)
5199                             (mm-handle-disposition handle)
5200                             (mm-handle-description handle)
5201                             nil
5202                             (mm-handle-id handle)))
5203       (setq gnus-article-mime-handles
5204             (mm-merge-handles gnus-article-mime-handles handle))
5205       (when (mm-handle-displayed-p handle)
5206         (mm-remove-part handle))
5207       (gnus-mm-display-part handle))))
5208
5209 (defun gnus-mime-copy-part (&optional handle arg)
5210   "Put the MIME part under point into a new buffer.
5211 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5212 are decompressed."
5213   (interactive (list nil current-prefix-arg))
5214   (gnus-article-check-buffer)
5215   (unless handle
5216     (setq handle (get-text-property (point) 'gnus-data)))
5217   (when handle
5218     (let ((filename (mm-handle-filename handle))
5219           contents dont-decode charset coding-system)
5220       (mm-with-unibyte-buffer
5221         (mm-insert-part handle)
5222         (setq contents (or (condition-case nil
5223                                (mm-decompress-buffer filename nil 'sig)
5224                              (error
5225                               (setq dont-decode t)
5226                               nil))
5227                            (buffer-string))))
5228       (setq filename (cond (filename (file-name-nondirectory filename))
5229                            (dont-decode "*raw data*")
5230                            (t "*decoded*")))
5231       (cond
5232        (dont-decode)
5233        ((not arg)
5234         (unless (setq charset (mail-content-type-get
5235                                (mm-handle-type handle) 'charset))
5236           (unless (setq coding-system (mm-with-unibyte-buffer
5237                                         (insert contents)
5238                                         (mm-find-buffer-file-coding-system)))
5239             (setq charset gnus-newsgroup-charset))))
5240        ((numberp arg)
5241         (setq charset (or (cdr (assq arg
5242                                      gnus-summary-show-article-charset-alist))
5243                           (mm-read-coding-system "Charset: ")))))
5244       (switch-to-buffer (generate-new-buffer filename))
5245       (if (or coding-system
5246               (and charset
5247                    (setq coding-system (mm-charset-to-coding-system charset))
5248                    (not (eq coding-system 'ascii))))
5249           (progn
5250             (mm-enable-multibyte)
5251             (insert (mm-decode-coding-string contents coding-system))
5252             (setq buffer-file-coding-system
5253                   (if (boundp 'last-coding-system-used)
5254                       (symbol-value 'last-coding-system-used)
5255                     coding-system)))
5256         (mm-disable-multibyte)
5257         (insert contents)
5258         (setq buffer-file-coding-system mm-binary-coding-system))
5259       ;; We do it this way to make `normal-mode' set the appropriate mode.
5260       (unwind-protect
5261           (progn
5262             (setq buffer-file-name (expand-file-name filename))
5263             (normal-mode))
5264         (setq buffer-file-name nil))
5265       (goto-char (point-min)))))
5266
5267 (defun gnus-mime-print-part (&optional handle filename)
5268   "Print the MIME part under point."
5269   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5270   (gnus-article-check-buffer)
5271   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5272          (contents (and handle (mm-get-part handle)))
5273          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5274          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5275     (when contents
5276         (if printer
5277             (unwind-protect
5278                 (progn
5279                   (mm-save-part-to-file handle file)
5280                   (call-process shell-file-name nil
5281                                 (generate-new-buffer " *mm*")
5282                                 nil
5283                                 shell-command-switch
5284                                 (mm-mailcap-command
5285                                  printer file (mm-handle-type handle))))
5286               (delete-file file))
5287           (with-temp-buffer
5288             (insert contents)
5289             (gnus-print-buffer))
5290           (ps-despool filename)))))
5291
5292 (defun gnus-mime-inline-part (&optional handle arg)
5293   "Insert the MIME part under point into the current buffer.
5294 Compressed files like .gz and .bz2 are decompressed."
5295   (interactive (list nil current-prefix-arg))
5296   (gnus-article-check-buffer)
5297   (unless handle
5298     (setq handle (get-text-property (point) 'gnus-data)))
5299   (when handle
5300     (let ((b (point))
5301           (inhibit-read-only t)
5302           contents charset coding-system)
5303       (if (and (not arg) (mm-handle-undisplayer handle))
5304           (mm-remove-part handle)
5305         (mm-with-unibyte-buffer
5306           (mm-insert-part handle)
5307           (setq contents
5308                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5309                     (buffer-string))))
5310         (cond
5311          ((not arg)
5312           (unless (setq charset (mail-content-type-get
5313                                  (mm-handle-type handle) 'charset))
5314             (unless (setq coding-system
5315                           (mm-with-unibyte-buffer
5316                             (insert contents)
5317                             (mm-find-buffer-file-coding-system)))
5318               (setq charset gnus-newsgroup-charset))))
5319          ((numberp arg)
5320           (if (mm-handle-undisplayer handle)
5321               (mm-remove-part handle))
5322           (setq charset
5323                 (or (cdr (assq arg
5324                                gnus-summary-show-article-charset-alist))
5325                     (mm-read-coding-system "Charset: "))))
5326          ((mm-handle-undisplayer handle)
5327           (mm-remove-part handle)))
5328         (forward-line 2)
5329         (mm-display-inline handle)
5330         (goto-char b)))))
5331
5332 (defun gnus-mime-set-charset-parameters (handle charset)
5333   "Set CHARSET to parameters in HANDLE.
5334 CHARSET may either be a string or a symbol."
5335   (unless (stringp charset)
5336     (setq charset (symbol-name charset)))
5337   (if (stringp (car handle))
5338       (dolist (h (cdr handle))
5339         (gnus-mime-set-charset-parameters h charset))
5340     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5341                                             "message/external-body")
5342                                      (progn
5343                                        (unless (mm-handle-cache handle)
5344                                          (mm-extern-cache-contents handle))
5345                                        (mm-handle-cache handle))
5346                                    handle)))
5347            (param (assq 'charset (cdr type))))
5348       (if param
5349           (setcdr param charset)
5350         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5351
5352 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5353   "Insert the MIME part under point into the current buffer using the
5354 specified charset."
5355   (interactive (list nil current-prefix-arg))
5356   (gnus-article-check-buffer)
5357   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5358         (fun (get-text-property (point) 'gnus-callback))
5359         (gnus-newsgroup-ignored-charsets 'gnus-all)
5360         charset form preferred parts)
5361     (when handle
5362       (when (prog1
5363                 (and fun
5364                      (setq charset
5365                            (or (cdr (assq
5366                                      arg
5367                                      gnus-summary-show-article-charset-alist))
5368                                (mm-read-coding-system "Charset: "))))
5369               (if (mm-handle-undisplayer handle)
5370                   (mm-remove-part handle)))
5371         (gnus-mime-set-charset-parameters handle charset)
5372         (when (and (consp (setq form (cdr-safe fun)))
5373                    (setq form (ignore-errors
5374                                 (assq 'gnus-mime-display-alternative form)))
5375                    (setq preferred (caddr form))
5376                    (progn
5377                      (when (eq (car preferred) 'quote)
5378                        (setq preferred (cadr preferred)))
5379                      (not (equal preferred
5380                                  (get-text-property (point) 'gnus-data))))
5381                    (setq parts (get-text-property (point) 'gnus-part))
5382                    (setq parts (cdr (assq parts
5383                                           gnus-article-mime-handle-alist)))
5384                    (equal (mm-handle-media-type parts) "multipart/alternative")
5385                    (setq parts (reverse (cdr parts))))
5386           (setcar (cddr form)
5387                   (list 'quote (or (cadr (member preferred parts))
5388                                    (car parts)))))
5389         (funcall fun handle)))))
5390
5391 (defun gnus-mime-view-part-externally (&optional handle)
5392   "View the MIME part under point with an external viewer."
5393   (interactive)
5394   (gnus-article-check-buffer)
5395   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5396          (mm-user-display-methods nil)
5397          (mm-inlined-types nil)
5398          (mail-parse-charset gnus-newsgroup-charset)
5399          (mail-parse-ignored-charsets
5400           (with-current-buffer gnus-summary-buffer
5401             gnus-newsgroup-ignored-charsets))
5402          (type (mm-handle-media-type handle))
5403          (method (mailcap-mime-info type))
5404          (mm-enable-external t))
5405     (if (not (stringp method))
5406         (gnus-mime-view-part-as-type
5407          nil (lambda (type) (stringp (mailcap-mime-info type))))
5408       (when handle
5409         (mm-display-part handle nil t)))))
5410
5411 (defun gnus-mime-view-part-internally (&optional handle)
5412   "View the MIME part under point with an internal viewer.
5413 If no internal viewer is available, use an external viewer."
5414   (interactive)
5415   (gnus-article-check-buffer)
5416   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5417          (mm-inlined-types '(".*"))
5418          (mm-inline-large-images t)
5419          (mail-parse-charset gnus-newsgroup-charset)
5420          (mail-parse-ignored-charsets
5421           (with-current-buffer gnus-summary-buffer
5422             gnus-newsgroup-ignored-charsets))
5423          (inhibit-read-only t))
5424     (if (not (mm-inlinable-p handle))
5425         (gnus-mime-view-part-as-type
5426          nil (lambda (type) (mm-inlinable-p handle type)))
5427       (when handle
5428         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5429
5430 (defun gnus-mime-action-on-part (&optional action)
5431   "Do something with the MIME attachment at \(point\)."
5432   (interactive
5433    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5434   (gnus-article-check-buffer)
5435   (let ((action-pair (assoc action gnus-mime-action-alist)))
5436     (if action-pair
5437         (funcall (cdr action-pair)))))
5438
5439 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5440   "Call FUNCTION on MIME part N.
5441 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as its only argument.
5442 If INTERACTIVE, call FUNCTION interactively."
5443   (let (window frame)
5444     ;; Check whether the article is displayed.
5445     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5446                  (setq window (get-buffer-window gnus-article-buffer t))
5447                  (frame-visible-p (setq frame (window-frame window))))
5448       (error "No article is displayed"))
5449     (with-current-buffer gnus-article-buffer
5450       ;; Check whether the article displays the right contents.
5451       (unless (with-current-buffer gnus-summary-buffer
5452                 (eq gnus-current-article (gnus-summary-article-number)))
5453         (error "You should select the right article first"))
5454       (if n
5455           (setq n (prefix-numeric-value n))
5456         (let ((pt (point)))
5457           (setq n (or (get-text-property pt 'gnus-part)
5458                       (and (not (bobp))
5459                            (get-text-property (1- pt) 'gnus-part))
5460                       (get-text-property (prog2
5461                                              (forward-line 1)
5462                                              (point)
5463                                            (goto-char pt))
5464                                          'gnus-part)
5465                       (get-text-property
5466                        (or (and (setq pt (previous-single-property-change
5467                                           pt 'gnus-part))
5468                                 (1- pt))
5469                            (next-single-property-change (point) 'gnus-part)
5470                            (point))
5471                        'gnus-part)
5472                       1))))
5473       ;; Check whether the specified part exists.
5474       (when (> n (length gnus-article-mime-handle-alist))
5475         (error "No such part")))
5476     (unless
5477         (progn
5478           ;; To select the window is needed so that the cursor
5479           ;; might be visible on the MIME button.
5480           (select-window (prog1
5481                              window
5482                            (setq window (selected-window))
5483                            ;; Article may be displayed in the other frame.
5484                            (gnus-select-frame-set-input-focus
5485                             (prog1
5486                                 frame
5487                               (setq frame (selected-frame))))))
5488           (when (gnus-article-goto-part n)
5489             ;; We point the cursor and the arrow at the MIME button
5490             ;; when the `function' prompt the user for something.
5491             (unless (and (pos-visible-in-window-p)
5492                          (> (count-lines (point) (window-end))
5493                             (/ (1- (window-height)) 3)))
5494               (recenter (/ (1- (window-height)) 3)))
5495             (let ((cursor-in-non-selected-windows t)
5496                   (overlay-arrow-string "=>")
5497                   (overlay-arrow-position (point-marker)))
5498               (unwind-protect
5499                   (cond
5500                    ((and no-handle interactive)
5501                     (call-interactively function))
5502                    (no-handle
5503                     (funcall function))
5504                    (interactive
5505                     (call-interactively
5506                      function (get-text-property (point) 'gnus-data)))
5507                    (t
5508                     (funcall function
5509                              (get-text-property (point) 'gnus-data))))
5510                 (set-marker overlay-arrow-position nil)
5511                 (unless gnus-auto-select-part
5512                   (gnus-select-frame-set-input-focus frame)
5513                   (select-window window))))
5514             t))
5515       (if gnus-inhibit-mime-unbuttonizing
5516           ;; This is the default though the program shouldn't reach here.
5517           (error "No such part")
5518         ;; The part which doesn't have the MIME button is selected.
5519         ;; So, we display all the buttons and redo it.
5520         (let ((gnus-inhibit-mime-unbuttonizing t))
5521           (gnus-summary-show-article)
5522           (gnus-article-part-wrapper n function no-handle))))))
5523
5524 (defun gnus-article-pipe-part (n)
5525   "Pipe MIME part N, which is the numerical prefix."
5526   (interactive "P")
5527   (gnus-article-part-wrapper n 'mm-pipe-part))
5528
5529 (defun gnus-article-save-part (n)
5530   "Save MIME part N, which is the numerical prefix."
5531   (interactive "P")
5532   (gnus-article-part-wrapper n 'mm-save-part))
5533
5534 (defun gnus-article-interactively-view-part (n)
5535   "View MIME part N interactively, which is the numerical prefix."
5536   (interactive "P")
5537   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5538
5539 (defun gnus-article-copy-part (n)
5540   "Copy MIME part N, which is the numerical prefix."
5541   (interactive "P")
5542   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5543
5544 (defun gnus-article-view-part-as-charset (n)
5545   "View MIME part N using a specified charset.
5546 N is the numerical prefix."
5547   (interactive "P")
5548   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5549
5550 (defun gnus-article-view-part-externally (n)
5551   "View MIME part N externally, which is the numerical prefix."
5552   (interactive "P")
5553   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5554
5555 (defun gnus-article-inline-part (n)
5556   "Inline MIME part N, which is the numerical prefix."
5557   (interactive "P")
5558   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5559
5560 (defun gnus-article-save-part-and-strip (n)
5561   "Save MIME part N and replace it with an external body.
5562 N is the numerical prefix."
5563   (interactive "P")
5564   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5565
5566 (defun gnus-article-replace-part (n)
5567   "Replace MIME part N with an external body.
5568 N is the numerical prefix."
5569   (interactive "P")
5570   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5571
5572 (defun gnus-article-delete-part (n)
5573   "Delete MIME part N and add some information about the removed part.
5574 N is the numerical prefix."
5575   (interactive "P")
5576   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5577
5578 (defun gnus-article-view-part-as-type (n)
5579   "Choose a MIME media type, and view part N as such.
5580 N is the numerical prefix."
5581   (interactive "P")
5582   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5583
5584 (defun gnus-article-mime-match-handle-first (condition)
5585   (if condition
5586       (let (n)
5587         (dolist (ihandle gnus-article-mime-handle-alist)
5588           (if (and (cond
5589                     ((functionp condition)
5590                      (funcall condition (cdr ihandle)))
5591                     ((eq condition 'undisplayed)
5592                      (not (or (mm-handle-undisplayer (cdr ihandle))
5593                               (equal (mm-handle-media-type (cdr ihandle))
5594                                      "multipart/alternative"))))
5595                     ((eq condition 'undisplayed-alternative)
5596                      (not (mm-handle-undisplayer (cdr ihandle))))
5597                     (t t))
5598                    (gnus-article-goto-part (car ihandle))
5599                    (or (not n) (< (car ihandle) n)))
5600               (setq n (car ihandle))))
5601         (or n 1))
5602     1))
5603
5604 (defun gnus-article-view-part (&optional n)
5605   "View MIME part N, which is the numerical prefix.
5606 If the part is already shown, hide the part.  If N is nil, view
5607 all parts."
5608   (interactive "P")
5609   (with-current-buffer gnus-article-buffer
5610     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5611                              gnus-article-mime-match-handle-function)))
5612     (when (> n (length gnus-article-mime-handle-alist))
5613       (error "No such part"))
5614     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5615       (when (gnus-article-goto-part n)
5616         (if (equal (car handle) "multipart/alternative")
5617             (gnus-article-press-button)
5618           (when (eq (gnus-mm-display-part handle) 'internal)
5619             (gnus-set-window-start)))))))
5620
5621 (defsubst gnus-article-mime-total-parts ()
5622   (if (bufferp (car gnus-article-mime-handles))
5623       1 ;; single part
5624     (1- (length gnus-article-mime-handles))))
5625
5626 (defun gnus-mm-display-part (handle)
5627   "Display HANDLE and fix MIME button."
5628   (let ((id (get-text-property (point) 'gnus-part))
5629         (point (point))
5630         (inhibit-read-only t))
5631     (forward-line 1)
5632     (prog1
5633         (let ((window (selected-window))
5634               (mail-parse-charset gnus-newsgroup-charset)
5635               (mail-parse-ignored-charsets
5636                (if (gnus-buffer-live-p gnus-summary-buffer)
5637                    (with-current-buffer gnus-summary-buffer
5638                      gnus-newsgroup-ignored-charsets)
5639                  nil)))
5640           (save-excursion
5641             (unwind-protect
5642                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5643                       (beg (point)))
5644                   (when win
5645                     (select-window win))
5646                   (goto-char point)
5647                   (forward-line)
5648                   (if (mm-handle-displayed-p handle)
5649                       ;; This will remove the part.
5650                       (mm-display-part handle)
5651                     (save-restriction
5652                       (narrow-to-region (point)
5653                                         (if (eobp) (point) (1+ (point))))
5654                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5655                       ;; We narrow to the part itself and
5656                       ;; then call the treatment functions.
5657                       (goto-char (point-min))
5658                       (forward-line 1)
5659                       (narrow-to-region (point) (point-max))
5660                       (gnus-treat-article
5661                        nil id
5662                        (gnus-article-mime-total-parts)
5663                        (mm-handle-media-type handle)))))
5664               (if (window-live-p window)
5665                   (select-window window)))))
5666       (goto-char point)
5667       (gnus-delete-line)
5668       (gnus-insert-mime-button
5669        handle id (list (mm-handle-displayed-p handle)))
5670       (goto-char point))))
5671
5672 (defun gnus-article-goto-part (n)
5673   "Go to MIME part N."
5674   (when gnus-break-pages
5675     (widen))
5676   (prog1
5677       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5678             part handle end next handles)
5679         (when start
5680           (goto-char start)
5681           (if (setq handle (get-text-property start 'gnus-data))
5682               start
5683             ;; Go to the displayed subpart, assuming this is
5684             ;; multipart/alternative.
5685             (setq part start
5686                   end (point-at-eol))
5687             (while (and (not handle)
5688                         part
5689                         (< part end)
5690                         (setq next (text-property-not-all part end
5691                                                           'gnus-data nil)))
5692               (setq part next
5693                     handle (get-text-property part 'gnus-data))
5694               (push (cons handle part) handles)
5695               (unless (mm-handle-displayed-p handle)
5696                 (setq handle nil
5697                       part (text-property-any part end 'gnus-data nil))))
5698             (unless handle
5699               ;; No subpart is displayed, so we find preferred one.
5700               (setq part
5701                     (cdr (assq (mm-preferred-alternative
5702                                 (nreverse (mapcar 'car handles)))
5703                                handles))))
5704             (if part
5705                 (goto-char (1+ part))
5706               start))))
5707     (when gnus-break-pages
5708       (gnus-narrow-to-page))))
5709
5710 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5711   (let ((gnus-tmp-name
5712          (or (mm-handle-filename handle)
5713              (mail-content-type-get (mm-handle-type handle) 'url)
5714              ""))
5715         (gnus-tmp-type (mm-handle-media-type handle))
5716         (gnus-tmp-description (or (mm-handle-description handle) ""))
5717         (gnus-tmp-dots
5718          (if (if displayed (car displayed)
5719                (mm-handle-displayed-p handle))
5720              "" "..."))
5721         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5722                            (buffer-size)))
5723         gnus-tmp-type-long b e)
5724     (when (string-match ".*/" gnus-tmp-name)
5725       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5726     (setq gnus-tmp-type-long (concat gnus-tmp-type
5727                                      (and (not (equal gnus-tmp-name ""))
5728                                           (concat "; " gnus-tmp-name))))
5729     (unless (equal gnus-tmp-description "")
5730       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5731     (unless (bolp)
5732       (insert "\n"))
5733     (setq b (point))
5734     (gnus-eval-format
5735      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5736      `(keymap ,gnus-mime-button-map
5737               gnus-callback gnus-mm-display-part
5738               gnus-part ,gnus-tmp-id
5739               article-type annotation
5740               gnus-data ,handle
5741               rear-nonsticky t))
5742     (setq e (if (bolp)
5743                 ;; Exclude a newline.
5744                 (1- (point))
5745               (point)))
5746     (when gnus-article-button-face
5747       (gnus-overlay-put (gnus-make-overlay b e nil t)
5748                         'face gnus-article-button-face))
5749     (widget-convert-button
5750      'link b e
5751      :mime-handle handle
5752      :action 'gnus-widget-press-button
5753      :button-keymap gnus-mime-button-map
5754      :help-echo
5755      (lambda (widget)
5756        ;; Needed to properly clear the message due to a bug in
5757        ;; wid-edit (XEmacs only).
5758        (if (boundp 'help-echo-owns-message)
5759            (setq help-echo-owns-message t))
5760        (format
5761         "%S: %s the MIME part; %S: more options"
5762         (aref gnus-mouse-2 0)
5763         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5764             "hide" "show")
5765         (aref gnus-down-mouse-3 0))))))
5766
5767 (defun gnus-widget-press-button (elems el)
5768   (goto-char (widget-get elems :from))
5769   (gnus-article-press-button))
5770
5771 (defvar gnus-displaying-mime nil)
5772
5773 (defun gnus-display-mime (&optional ihandles)
5774   "Display the MIME parts."
5775   (save-excursion
5776     (save-selected-window
5777       (let ((window (get-buffer-window gnus-article-buffer))
5778             (point (point)))
5779         (when window
5780           (select-window window)
5781           ;; We have to do this since selecting the window
5782           ;; may change the point.  So we set the window point.
5783           (set-window-point window point)))
5784       (let ((handles ihandles)
5785             (inhibit-read-only t)
5786             handle)
5787         (cond (handles)
5788               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5789                (when gnus-article-emulate-mime
5790                  (mm-uu-dissect-text-parts handles)))
5791               (gnus-article-emulate-mime
5792                (setq handles (mm-uu-dissect))))
5793         (when (and (not ihandles)
5794                    (not gnus-displaying-mime))
5795           ;; Top-level call; we clean up.
5796           (when gnus-article-mime-handles
5797             (mm-destroy-parts gnus-article-mime-handles)
5798             (setq gnus-article-mime-handle-alist nil));; A trick.
5799           (setq gnus-article-mime-handles handles)
5800           ;; We allow users to glean info from the handles.
5801           (when gnus-article-mime-part-function
5802             (gnus-mime-part-function handles)))
5803         (if (and handles
5804                  (or (not (stringp (car handles)))
5805                      (cdr handles)))
5806             (progn
5807               (when (and (not ihandles)
5808                          (not gnus-displaying-mime))
5809                 ;; Clean up for mime parts.
5810                 (article-goto-body)
5811                 (delete-region (point) (point-max)))
5812               (let ((gnus-displaying-mime t))
5813                 (gnus-mime-display-part handles)))
5814           (save-restriction
5815             (article-goto-body)
5816             (narrow-to-region (point) (point-max))
5817             (gnus-treat-article nil 1 1 "text/plain")
5818             (widen)))
5819         (unless ihandles
5820           ;; Highlight the headers.
5821           (save-excursion
5822             (save-restriction
5823               (article-goto-body)
5824               (narrow-to-region (point-min) (point))
5825               (gnus-article-save-original-date
5826                (gnus-treat-article 'head)))))))
5827     ;; Cope with broken MIME messages.
5828     (goto-char (point-max))
5829     (unless (bolp)
5830       (insert "\n"))))
5831
5832 (defcustom gnus-mime-display-multipart-as-mixed nil
5833   "Display \"multipart\" parts as  \"multipart/mixed\".
5834
5835 If t, it overrides nil values of
5836 `gnus-mime-display-multipart-alternative-as-mixed' and
5837 `gnus-mime-display-multipart-related-as-mixed'."
5838   :group 'gnus-article-mime
5839   :type 'boolean)
5840
5841 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5842   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5843   :version "22.1"
5844   :group 'gnus-article-mime
5845   :type 'boolean)
5846
5847 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5848   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5849
5850 If displaying \"text/html\" is discouraged \(see
5851 `mm-discouraged-alternatives'\) images or other material inside a
5852 \"multipart/related\" part might be overlooked when this variable is nil."
5853   :version "22.1"
5854   :group 'gnus-article-mime
5855   :type 'boolean)
5856
5857 (defun gnus-mime-display-part (handle)
5858   (cond
5859    ;; Maybe a broken MIME message.
5860    ((null handle))
5861    ;; Single part.
5862    ((not (stringp (car handle)))
5863     (gnus-mime-display-single handle))
5864    ;; User-defined multipart
5865    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5866     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5867              handle))
5868    ;; multipart/alternative
5869    ((and (equal (car handle) "multipart/alternative")
5870          (not (or gnus-mime-display-multipart-as-mixed
5871                   gnus-mime-display-multipart-alternative-as-mixed)))
5872     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5873       (push (cons id handle) gnus-article-mime-handle-alist)
5874       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5875    ;; multipart/related
5876    ((and (equal (car handle) "multipart/related")
5877          (not (or gnus-mime-display-multipart-as-mixed
5878                   gnus-mime-display-multipart-related-as-mixed)))
5879     ;;;!!!We should find the start part, but we just default
5880     ;;;!!!to the first part.
5881     ;;(gnus-mime-display-part (cadr handle))
5882     ;;;!!! Most multipart/related is an HTML message plus images.
5883     ;;;!!! Unfortunately we are unable to let W3 display those
5884     ;;;!!! included images, so we just display it as a mixed multipart.
5885     ;;(gnus-mime-display-mixed (cdr handle))
5886     ;;;!!! No, w3 can display everything just fine.
5887     (gnus-mime-display-part (cadr handle)))
5888    ((equal (car handle) "multipart/signed")
5889     (gnus-add-wash-type 'signed)
5890     (gnus-mime-display-security handle))
5891    ((equal (car handle) "multipart/encrypted")
5892     (gnus-add-wash-type 'encrypted)
5893     (gnus-mime-display-security handle))
5894    ;; Other multiparts are handled like multipart/mixed.
5895    (t
5896     (gnus-mime-display-mixed (cdr handle)))))
5897
5898 (defun gnus-mime-part-function (handles)
5899   (if (stringp (car handles))
5900       (mapcar 'gnus-mime-part-function (cdr handles))
5901     (funcall gnus-article-mime-part-function handles)))
5902
5903 (defun gnus-mime-display-mixed (handles)
5904   (mapcar 'gnus-mime-display-part handles))
5905
5906 (defun gnus-mime-display-single (handle)
5907   (let ((type (mm-handle-media-type handle))
5908         (ignored gnus-ignored-mime-types)
5909         (not-attachment t)
5910         (move nil)
5911         display text)
5912     (catch 'ignored
5913       (progn
5914         (while ignored
5915           (when (string-match (pop ignored) type)
5916             (throw 'ignored nil)))
5917         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5918                                (with-current-buffer gnus-summary-buffer
5919                                  gnus-inhibit-images)
5920                              gnus-inhibit-images)
5921                            (string-match "\\`image/" type)))
5922                  (setq not-attachment
5923                        (and (not (mm-inline-override-p handle))
5924                             (or (not (mm-handle-disposition handle))
5925                                 (equal (car (mm-handle-disposition handle))
5926                                        "inline")
5927                                 (mm-attachment-override-p handle))))
5928                  (mm-automatic-display-p handle)
5929                  (or (and
5930                       (mm-inlinable-p handle)
5931                       (mm-inlined-p handle))
5932                      (mm-automatic-external-display-p type)))
5933             (setq display t)
5934           (when (equal (mm-handle-media-supertype handle) "text")
5935             (setq text t)))
5936         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5937               beg)
5938           (push (cons id handle) gnus-article-mime-handle-alist)
5939           (when (and display
5940                      (equal (mm-handle-media-supertype handle) "message"))
5941             (insert-char
5942              ?\n
5943              (cond ((not (bolp)) 2)
5944                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5945                    (t 1))))
5946           (when (or (not display)
5947                     (not (gnus-unbuttonized-mime-type-p type))
5948                     (eq id gnus-mime-buttonized-part-id))
5949             (gnus-insert-mime-button
5950              handle id (list (or display (and not-attachment text))))
5951             (gnus-article-insert-newline)
5952             ;; Remember modify the number of forward lines.
5953             (setq move t))
5954           (setq beg (point))
5955           (cond
5956            (display
5957             (when move
5958               (forward-line -1)
5959               (setq beg (point)))
5960             (let ((mail-parse-charset gnus-newsgroup-charset)
5961                   (mail-parse-ignored-charsets
5962                    (save-excursion (condition-case ()
5963                                        (set-buffer gnus-summary-buffer)
5964                                      (error))
5965                                    gnus-newsgroup-ignored-charsets)))
5966               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5967             (goto-char (point-max)))
5968            ((and text not-attachment)
5969             (when move
5970               (forward-line -1)
5971               (setq beg (point)))
5972             (gnus-article-insert-newline)
5973             (mm-display-inline handle)
5974             (goto-char (point-max))))
5975           ;; Do highlighting.
5976           (save-excursion
5977             (save-restriction
5978               (narrow-to-region beg (point))
5979               (if (eq handle gnus-article-mime-handles)
5980                   ;; The format=flowed case.
5981                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5982                 ;; Don't count signature parts that are never displayed.
5983                 ;; The part number should be re-calculated supposing this
5984                 ;; might be a message/rfc822 part.
5985                 (let (handles)
5986                   (dolist (part gnus-article-mime-handles)
5987                     (unless (or (stringp part)
5988                                 (equal (car (mm-handle-type part))
5989                                        "application/pgp-signature"))
5990                       (push part handles)))
5991                   (gnus-treat-article
5992                    nil (length (memq handle handles)) (length handles)
5993                    (mm-handle-media-type handle)))))))))))
5994
5995 (defun gnus-unbuttonized-mime-type-p (type)
5996   "Say whether TYPE is to be unbuttonized."
5997   (unless gnus-inhibit-mime-unbuttonizing
5998     (when (catch 'found
5999             (let ((types gnus-unbuttonized-mime-types))
6000               (while types
6001                 (when (string-match (pop types) type)
6002                   (throw 'found t)))))
6003       (not (catch 'found
6004              (let ((types gnus-buttonized-mime-types))
6005                (while types
6006                  (when (string-match (pop types) type)
6007                    (throw 'found t)))))))))
6008
6009 (defun gnus-article-insert-newline ()
6010   "Insert a newline, but mark it as undeletable."
6011   (gnus-put-text-property
6012    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
6013
6014 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
6015   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
6016          (ihandles handles)
6017          (point (point))
6018          handle (inhibit-read-only t) from props begend not-pref)
6019     (save-window-excursion
6020       (save-restriction
6021         (when ibegend
6022           (narrow-to-region (car ibegend)
6023                             (or (cdr ibegend)
6024                                 (progn
6025                                   (goto-char (car ibegend))
6026                                   (forward-line 2)
6027                                   (point))))
6028           (delete-region (point-min) (point-max))
6029           (mm-remove-parts handles))
6030         (setq begend (list (point-marker)))
6031         ;; Do the toggle.
6032         (unless (setq not-pref (cadr (member preferred ihandles)))
6033           (setq not-pref (car ihandles)))
6034         (when (or ibegend
6035                   (not preferred)
6036                   (not (gnus-unbuttonized-mime-type-p
6037                         "multipart/alternative")))
6038           (gnus-add-text-properties
6039            (setq from (point))
6040            (progn
6041              (insert (format "%d.  " id))
6042              (point))
6043            `(gnus-callback
6044              (lambda (handles)
6045                (unless ,(not ibegend)
6046                  (setq gnus-article-mime-handle-alist
6047                        ',gnus-article-mime-handle-alist))
6048                (gnus-mime-display-alternative
6049                 ',ihandles ',not-pref ',begend ,id))
6050              keymap ,gnus-mime-button-map
6051              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6052              face ,gnus-article-button-face
6053              gnus-part ,id
6054              article-type multipart
6055              rear-nonsticky t))
6056           (widget-convert-button 'link from (point)
6057                                  :action 'gnus-widget-press-button
6058                                  :button-keymap gnus-widget-button-keymap)
6059           ;; Do the handles
6060           (while (setq handle (pop handles))
6061             (gnus-add-text-properties
6062              (setq from (point))
6063              (progn
6064                (insert (format "(%c) %-18s"
6065                                (if (equal handle preferred) ?* ? )
6066                                (mm-handle-media-type handle)))
6067                (point))
6068              `(gnus-callback
6069                (lambda (handles)
6070                  (unless ,(not ibegend)
6071                    (setq gnus-article-mime-handle-alist
6072                          ',gnus-article-mime-handle-alist))
6073                  (gnus-mime-display-alternative
6074                   ',ihandles ',handle ',begend ,id))
6075                keymap ,gnus-mime-button-map
6076                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6077                face ,gnus-article-button-face
6078                gnus-part ,id
6079                gnus-data ,handle
6080                rear-nonsticky t))
6081             (widget-convert-button 'link from (point)
6082                                    :action 'gnus-widget-press-button
6083                                    :button-keymap gnus-widget-button-keymap)
6084             (insert "  "))
6085           (insert "\n\n"))
6086         (when preferred
6087           (if (stringp (car preferred))
6088               (gnus-display-mime preferred)
6089             (let ((mail-parse-charset gnus-newsgroup-charset)
6090                   (mail-parse-ignored-charsets
6091                    (with-current-buffer gnus-summary-buffer
6092                      gnus-newsgroup-ignored-charsets)))
6093               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6094               ;; Do highlighting.
6095               (save-excursion
6096                 (save-restriction
6097                   (narrow-to-region (car begend) (point-max))
6098                   (gnus-treat-article
6099                    nil (length gnus-article-mime-handle-alist)
6100                    (gnus-article-mime-total-parts)
6101                    (mm-handle-media-type preferred))))))
6102           (goto-char (point-max))
6103           (setcdr begend (point-marker)))))
6104     (when ibegend
6105       (goto-char point))))
6106
6107 (defconst gnus-article-wash-status-strings
6108   (let ((alist '((cite "c" "Possible hidden citation text"
6109                        " " "All citation text visible")
6110                  (headers "h" "Hidden headers"
6111                           " " "All headers visible.")
6112                  (pgp "p" "Encrypted or signed message status hidden"
6113                       " " "No hidden encryption nor digital signature status")
6114                  (signature "s" "Signature has been hidden"
6115                             " " "Signature is visible")
6116                  (overstrike "o" "Overstrike (^H) characters applied"
6117                              " " "No overstrike characters applied")
6118                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6119                            " " "No /*_emphasis_*/ characters applied")))
6120         result)
6121     (dolist (entry alist result)
6122       (let ((key (nth 0 entry))
6123             (on (copy-sequence (nth 1 entry)))
6124             (on-help (nth 2 entry))
6125             (off (copy-sequence (nth 3 entry)))
6126             (off-help (nth 4 entry)))
6127         (put-text-property 0 1 'help-echo on-help on)
6128         (put-text-property 0 1 'help-echo off-help off)
6129         (push (list key on off) result))))
6130   "Alist of strings describing wash status in the mode line.
6131 Each entry has the form (KEY ON OF), where the KEY is a symbol
6132 representing the particular washing function, ON is the string to use
6133 in the article mode line when the washing function is active, and OFF
6134 is the string to use when it is inactive.")
6135
6136 (defun gnus-article-wash-status-entry (key value)
6137   (let ((entry (assoc key gnus-article-wash-status-strings)))
6138     (if value (nth 1 entry) (nth 2 entry))))
6139
6140 (defun gnus-article-wash-status ()
6141   "Return a string which display status of article washing."
6142   (with-current-buffer gnus-article-buffer
6143     (let ((cite (memq 'cite gnus-article-wash-types))
6144           (headers (memq 'headers gnus-article-wash-types))
6145           (boring (memq 'boring-headers gnus-article-wash-types))
6146           (pgp (memq 'pgp gnus-article-wash-types))
6147           (pem (memq 'pem gnus-article-wash-types))
6148           (signed (memq 'signed gnus-article-wash-types))
6149           (encrypted (memq 'encrypted gnus-article-wash-types))
6150           (signature (memq 'signature gnus-article-wash-types))
6151           (overstrike (memq 'overstrike gnus-article-wash-types))
6152           (emphasis (memq 'emphasis gnus-article-wash-types)))
6153       (concat
6154        (gnus-article-wash-status-entry 'cite cite)
6155        (gnus-article-wash-status-entry 'headers (or headers boring))
6156        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6157        (gnus-article-wash-status-entry 'signature signature)
6158        (gnus-article-wash-status-entry 'overstrike overstrike)
6159        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6160
6161 (defun gnus-add-wash-type (type)
6162   "Add a washing of TYPE to the current status."
6163   (add-to-list 'gnus-article-wash-types type))
6164
6165 (defun gnus-delete-wash-type (type)
6166   "Add a washing of TYPE to the current status."
6167   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6168
6169 (defun gnus-add-image (category image)
6170   "Add IMAGE of CATEGORY to the list of displayed images."
6171   (let ((entry (assq category gnus-article-image-alist)))
6172     (unless entry
6173       (setq entry (list category))
6174       (push entry gnus-article-image-alist))
6175     (nconc entry (list image))))
6176
6177 (defun gnus-delete-images (category)
6178   "Delete all images in CATEGORY."
6179   (let ((entry (assq category gnus-article-image-alist)))
6180     (dolist (image (cdr entry))
6181       (gnus-remove-image image category))
6182     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6183     (gnus-delete-wash-type category)))
6184
6185 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6186
6187 (defun gnus-article-maybe-hide-headers ()
6188   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6189 Provided for backwards compatibility."
6190   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6191                  (not (with-current-buffer gnus-summary-buffer
6192                         gnus-have-all-headers)))
6193              (not gnus-inhibit-hiding))
6194     (gnus-article-hide-headers)))
6195
6196 (declare-function shr-put-image "shr" (data alt &optional flags))
6197
6198 (defun gnus-shr-put-image (data alt &optional flags)
6199   "Put image DATA with a string ALT.  Enable image to be deleted."
6200   (let ((image (shr-put-image data (propertize (or alt "*")
6201                                                'gnus-image-category 'shr)
6202                               flags)))
6203     (when image
6204       (gnus-add-image 'shr image))))
6205
6206 ;;; Article savers.
6207
6208 (defun gnus-output-to-file (file-name)
6209   "Append the current article to a file named FILE-NAME.
6210 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6211 text and used as the value of the coding cookie which is added to the
6212 top of a file.  Otherwise, this function saves a raw article without
6213 the coding cookie."
6214   (let* ((artbuf (current-buffer))
6215          (file-name-coding-system nnmail-pathname-coding-system)
6216          (coding gnus-article-save-coding-system)
6217          (coding-system-for-read (if coding
6218                                      nil ;; Rely on the coding cookie.
6219                                    mm-text-coding-system))
6220          (coding-system-for-write (or coding
6221                                       mm-text-coding-system-for-write
6222                                       mm-text-coding-system))
6223          (exists (file-exists-p file-name)))
6224     (with-temp-buffer
6225       (when exists
6226         (insert-file-contents file-name)
6227         (goto-char (point-min))
6228         ;; Remove the existing coding cookie.
6229         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6230           (delete-region (match-beginning 0) (match-end 0))))
6231       (goto-char (point-max))
6232       (insert-buffer-substring artbuf)
6233       ;; Append newline at end of the buffer as separator, and then
6234       ;; save it to file.
6235       (goto-char (point-max))
6236       (insert "\n")
6237       (when coding
6238         ;; If the coding system is not suitable to encode the text,
6239         ;; ask a user for a proper one.
6240         (when (fboundp 'select-safe-coding-system)
6241           (setq coding (coding-system-base
6242                         (save-window-excursion
6243                           (select-safe-coding-system (point-min) (point-max)
6244                                                      coding))))
6245           (setq coding-system-for-write
6246                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6247                     coding)))
6248         (goto-char (point-min))
6249         ;; Add the coding cookie.
6250         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6251                         coding-system-for-write)))
6252       (if exists
6253           (progn
6254             (write-region (point-min) (point-max) file-name nil 'no-message)
6255             (message "Appended to %s" file-name))
6256         (write-region (point-min) (point-max) file-name))))
6257   t)
6258
6259 (defun gnus-narrow-to-page (&optional arg)
6260   "Narrow the article buffer to a page.
6261 If given a numerical ARG, move forward ARG pages."
6262   (interactive "P")
6263   (setq arg (if arg (prefix-numeric-value arg) 0))
6264   (with-current-buffer gnus-article-buffer
6265     (widen)
6266     ;; Remove any old next/prev buttons.
6267     (when (gnus-visual-p 'page-marker)
6268       (let ((inhibit-read-only t))
6269         (gnus-remove-text-with-property 'gnus-prev)
6270         (gnus-remove-text-with-property 'gnus-next)))
6271     (let (st nd pt)
6272       (when (save-excursion
6273               (cond ((< arg 0)
6274                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6275                          (prog1
6276                              (setq nd (match-beginning 0)
6277                                    pt nd)
6278                            (when (re-search-backward page-delimiter nil t)
6279                              (setq st (match-end 0))))
6280                        (when (re-search-forward page-delimiter nil t)
6281                          (setq nd (match-beginning 0)
6282                                pt (point-min)))))
6283                     ((> arg 0)
6284                      (if (re-search-forward page-delimiter nil 'move arg)
6285                          (prog1
6286                              (setq st (match-end 0)
6287                                    pt st)
6288                            (when (re-search-forward page-delimiter nil t)
6289                              (setq nd (match-beginning 0))))
6290                        (when (re-search-backward page-delimiter nil t)
6291                          (setq st (match-end 0)
6292                                pt (point-max)))))
6293                     (t
6294                      (when (re-search-backward page-delimiter nil t)
6295                        (goto-char (setq st (match-end 0))))
6296                      (when (re-search-forward page-delimiter nil t)
6297                        (setq nd (match-beginning 0)))
6298                      (or st nd))))
6299         (setq gnus-page-broken t)
6300         (when pt (goto-char pt))
6301         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6302         (when (gnus-visual-p 'page-marker)
6303           (save-excursion
6304             (when nd
6305               (goto-char nd)
6306               (gnus-insert-next-page-button))
6307             (when st
6308               (goto-char st)
6309               (gnus-insert-prev-page-button))))))))
6310
6311 ;; Article mode commands
6312
6313 (defun gnus-article-goto-next-page ()
6314   "Show the next page of the article."
6315   (interactive)
6316   (when (gnus-article-next-page)
6317     (goto-char (point-min))
6318     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6319
6320
6321 (defun gnus-article-goto-prev-page ()
6322   "Show the previous page of the article."
6323   (interactive)
6324   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6325       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6326     (gnus-article-prev-page nil)))
6327
6328 ;; This is cleaner but currently breaks `gnus-pick-mode':
6329 ;;
6330 ;; (defun gnus-article-goto-next-page ()
6331 ;;   "Show the next page of the article."
6332 ;;   (interactive)
6333 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6334 ;;     (gnus-summary-next-page)))
6335 ;;
6336 ;; (defun gnus-article-goto-prev-page ()
6337 ;;   "Show the next page of the article."
6338 ;;   (interactive)
6339 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6340 ;;     (gnus-summary-prev-page)))
6341
6342 (defun gnus-article-next-page (&optional lines)
6343   "Show the next page of the current article.
6344 If end of article, return non-nil.  Otherwise return nil.
6345 Argument LINES specifies lines to be scrolled up."
6346   (interactive "p")
6347   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6348   (if (and (not (and gnus-article-over-scroll
6349                      (> (count-lines (window-start) (point-max))
6350                         (if (featurep 'xemacs)
6351                             (or lines (1- (window-height)))
6352                           (+ (or lines (1- (window-height))) scroll-margin)))))
6353            (save-excursion
6354              (end-of-line)
6355              (and (pos-visible-in-window-p)     ;Not continuation line.
6356                   (>= (point) (point-max)))))
6357       ;; Nothing in this page.
6358       (if (or (not gnus-page-broken)
6359               (save-excursion
6360                 (save-restriction
6361                   (widen)
6362                   (forward-line)
6363                   (eobp)))) ;Real end-of-buffer?
6364           (progn
6365             (when gnus-article-over-scroll
6366               (gnus-article-next-page-1 lines))
6367             t)                  ;Nothing more.
6368         (gnus-narrow-to-page 1)         ;Go to next page.
6369         nil)
6370     ;; More in this page.
6371     (gnus-article-next-page-1 lines)
6372     nil))
6373
6374 (defun gnus-article-beginning-of-window ()
6375   "Move point to the beginning of the window.
6376 In Emacs, the point is placed at the line number which `scroll-margin'
6377 specifies."
6378   (if (featurep 'xemacs)
6379       (move-to-window-line 0)
6380     ;; There is an obscure bug in Emacs that makes it impossible to
6381     ;; scroll past big pictures in the article buffer.  Try to fix
6382     ;; this by adding a sanity check by counting the lines visible.
6383     (when (> (count-lines (window-start) (window-end)) 30)
6384       (move-to-window-line
6385        (min (max 0 scroll-margin)
6386             (max 1 (- (window-height)
6387                       (if mode-line-format 1 0)
6388                       (if header-line-format 1 0)
6389                       2)))))))
6390
6391 (defun gnus-article-next-page-1 (lines)
6392   (condition-case ()
6393       (let ((scroll-in-place nil)
6394             (auto-window-vscroll nil))
6395         (scroll-up lines))
6396     (end-of-buffer
6397      ;; Long lines may cause an end-of-buffer error.
6398      (goto-char (point-max))))
6399   (gnus-article-beginning-of-window))
6400
6401 (defun gnus-article-prev-page (&optional lines)
6402   "Show previous page of current article.
6403 Argument LINES specifies lines to be scrolled down."
6404   (interactive "p")
6405   (move-to-window-line 0)
6406   (if (and gnus-page-broken
6407            (bobp)
6408            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6409       (progn
6410         (gnus-narrow-to-page -1)        ;Go to previous page.
6411         (goto-char (point-max))
6412         (recenter (if gnus-article-over-scroll
6413                       (if lines
6414                           (max (if (featurep 'xemacs)
6415                                    lines
6416                                  (+ lines scroll-margin))
6417                                3)
6418                         (- (window-height) 2))
6419                     -1)))
6420     (prog1
6421         (condition-case ()
6422             (let ((scroll-in-place nil))
6423               (scroll-down lines))
6424           (beginning-of-buffer
6425            (goto-char (point-min))))
6426       (gnus-article-beginning-of-window))))
6427
6428 (defun gnus-article-only-boring-p ()
6429   "Decide whether there is only boring text remaining in the article.
6430 Something \"interesting\" is a word of at least two letters that does
6431 not have a face in `gnus-article-boring-faces'."
6432   (when (and gnus-article-skip-boring
6433              (boundp 'gnus-article-boring-faces)
6434              (symbol-value 'gnus-article-boring-faces))
6435     (save-excursion
6436       (let ((inhibit-point-motion-hooks t))
6437         (catch 'only-boring
6438           (while (re-search-forward "\\b\\w\\w" nil t)
6439             (forward-char -1)
6440             (when (not (gnus-intersection
6441                         (gnus-faces-at (point))
6442                         (symbol-value 'gnus-article-boring-faces)))
6443               (throw 'only-boring nil)))
6444           (throw 'only-boring t))))))
6445
6446 (defun gnus-article-refer-article ()
6447   "Read article specified by message-id around point."
6448   (interactive)
6449   (save-excursion
6450     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6451     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6452     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6453         (let ((msg-id (concat "<" (match-string 0) ">")))
6454           (set-buffer gnus-summary-buffer)
6455           (gnus-summary-refer-article msg-id))
6456       (error "No references around point"))))
6457
6458 (defun gnus-article-show-summary ()
6459   "Reconfigure windows to show summary buffer."
6460   (interactive)
6461   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6462       (error "There is no summary buffer for this article buffer")
6463     (gnus-article-set-globals)
6464     (gnus-configure-windows 'article)
6465     (gnus-summary-goto-subject gnus-current-article)
6466     (gnus-summary-position-point)))
6467
6468 (defun gnus-article-describe-briefly ()
6469   "Describe article mode commands briefly."
6470   (interactive)
6471   (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")))
6472
6473 (defun gnus-article-check-buffer ()
6474   "Beep if not in an article buffer."
6475   (unless (equal major-mode 'gnus-article-mode)
6476     (error "Command invoked outside of a Gnus article buffer")))
6477
6478 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6479   "Read a summary buffer key sequence and execute it from the article buffer."
6480   (interactive "P")
6481   (gnus-article-check-buffer)
6482   (let ((nosaves
6483          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6484            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6485            "=" "^" "\M-^" "|"))
6486         (nosave-but-article
6487          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6488            "An" "Ap" [?A (meta return)] [?A delete]))
6489         (nosave-in-article
6490          '("AS" "\C-d"))
6491         (up-to-top
6492          '("n" "Gn" "p" "Gp"))
6493         keys new-sum-point)
6494     (with-current-buffer gnus-article-current-summary
6495       (let (gnus-pick-mode)
6496         (setq unread-command-events (nconc unread-command-events
6497                                            (list (or key last-command-event)))
6498               keys (if (featurep 'xemacs)
6499                        (events-to-keys (read-key-sequence nil t))
6500                      (read-key-sequence nil t)))))
6501
6502     (message "")
6503
6504     (cond
6505      ((eq (aref keys (1- (length keys))) ?\C-h)
6506       (gnus-article-describe-bindings (substring keys 0 -1)))
6507      ((or (member keys nosaves)
6508           (member keys nosave-but-article)
6509           (member keys nosave-in-article))
6510       (let (func)
6511         (save-window-excursion
6512           (pop-to-buffer gnus-article-current-summary)
6513           ;; We disable the pick minor mode commands.
6514           (let (gnus-pick-mode)
6515             (setq func (key-binding keys t))))
6516         (if (or (not func)
6517                 (numberp func))
6518             (ding)
6519           (unless (member keys nosave-in-article)
6520             (set-buffer gnus-article-current-summary))
6521           (when (get func 'disabled)
6522             (error "Function %s disabled" func))
6523           (call-interactively func)
6524           (setq new-sum-point (point)))
6525         (when (member keys nosave-but-article)
6526           (pop-to-buffer gnus-article-buffer))))
6527      (t
6528       ;; These commands should restore window configuration.
6529       (let ((obuf (current-buffer))
6530             (owin (current-window-configuration))
6531             win func in-buffer selected new-sum-start new-sum-hscroll err)
6532         (cond (not-restore-window
6533                (pop-to-buffer gnus-article-current-summary)
6534                (setq win (selected-window)))
6535               ((setq win (get-buffer-window gnus-article-current-summary))
6536                (select-window win))
6537               (t
6538                (let ((summary-buffer gnus-article-current-summary))
6539                  (gnus-configure-windows 'article)
6540                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6541                    (let ((gnus-buffer-configuration
6542                           '((article ((vertical 1.0
6543                                                 (summary 0.25 point)
6544                                                 (article 1.0)))))))
6545                      (gnus-configure-windows 'article))
6546                    (setq win (get-buffer-window summary-buffer 'visible)))
6547                  (gnus-select-frame-set-input-focus (window-frame win))
6548                  (select-window win))))
6549         (setq in-buffer (current-buffer))
6550         ;; We disable the pick minor mode commands.
6551         (setq func (let (gnus-pick-mode)
6552                      (key-binding keys t)))
6553         (when (get func 'disabled)
6554           (error "Function %s disabled" func))
6555         (if (and func
6556                  (functionp func)
6557                  (condition-case code
6558                      (progn
6559                        (call-interactively func)
6560                        t)
6561                    (error
6562                     (setq err code)
6563                     nil)))
6564             (progn
6565               (when (eq win (selected-window))
6566                 (setq new-sum-point (point)
6567                       new-sum-start (window-start win)
6568                       new-sum-hscroll (window-hscroll win)))
6569               (when (or (eq in-buffer (current-buffer))
6570                         (when (eq obuf (current-buffer))
6571                           (set-buffer in-buffer)
6572                           t))
6573                 (setq selected (gnus-summary-select-article))
6574                 (set-buffer obuf)
6575                 (unless not-restore-window
6576                   (set-window-configuration owin))
6577                 (when (and (eq selected 'old)
6578                            new-sum-point)
6579                   (set-window-start (get-buffer-window (current-buffer))
6580                                     1)
6581                   (set-window-point (get-buffer-window (current-buffer))
6582                                     (if (article-goto-body)
6583                                         (1- (point))
6584                                       (point))))
6585                 (when (and (not not-restore-window)
6586                            new-sum-point
6587                            (window-live-p win)
6588                            (with-current-buffer (window-buffer win)
6589                              (eq major-mode 'gnus-summary-mode)))
6590                   (set-window-point win new-sum-point)
6591                   (set-window-start win new-sum-start)
6592                   (set-window-hscroll win new-sum-hscroll))))
6593           (set-window-configuration owin)
6594           (if err
6595               (signal (car err) (cdr err))
6596             (ding))))))))
6597
6598 (defun gnus-article-read-summary-send-keys ()
6599   (interactive)
6600   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6601     (gnus-article-read-summary-keys)))
6602
6603 (defun gnus-article-describe-key (key)
6604   "Display documentation of the function invoked by KEY.
6605 KEY is a string or a vector."
6606   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6607                        (read-key-sequence "Describe key: "))))
6608   (gnus-article-check-buffer)
6609   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6610                                   gnus-article-read-summary-send-keys))
6611       (with-current-buffer gnus-article-current-summary
6612         (setq unread-command-events
6613               (if (featurep 'xemacs)
6614                   (append key nil)
6615                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6616                                         (list 'meta (- x 128))
6617                                       x))
6618                         key)))
6619         (let ((cursor-in-echo-area t)
6620               gnus-pick-mode)
6621           (describe-key (read-key-sequence nil t))))
6622     (describe-key key)))
6623
6624 (defun gnus-article-describe-key-briefly (key &optional insert)
6625   "Display documentation of the function invoked by KEY.
6626 KEY is a string or a vector."
6627   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6628                        (read-key-sequence "Describe key: "))
6629                      current-prefix-arg))
6630   (gnus-article-check-buffer)
6631   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6632                                   gnus-article-read-summary-send-keys))
6633       (with-current-buffer gnus-article-current-summary
6634         (setq unread-command-events
6635               (if (featurep 'xemacs)
6636                   (append key nil)
6637                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6638                                         (list 'meta (- x 128))
6639                                       x))
6640                         key)))
6641         (let ((cursor-in-echo-area t)
6642               gnus-pick-mode)
6643           (describe-key-briefly (read-key-sequence nil t) insert)))
6644     (describe-key-briefly key insert)))
6645
6646 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6647 (defvar gnus-agent-summary-mode)
6648 (defvar gnus-draft-mode)
6649 ;; Calling help-buffer will autoload help-mode.
6650 (defvar help-xref-stack-item)
6651 ;; Emacs 22 doesn't load it in the batch mode.
6652 (eval-when-compile
6653   (autoload 'help-buffer "help-mode"))
6654
6655 (defun gnus-article-describe-bindings (&optional prefix)
6656   "Show a list of all defined keys, and their definitions.
6657 The optional argument PREFIX, if non-nil, should be a key sequence;
6658 then we display only bindings that start with that prefix."
6659   (interactive)
6660   (gnus-article-check-buffer)
6661   (let ((keymap (copy-keymap gnus-article-mode-map))
6662         (map (copy-keymap gnus-article-send-map))
6663         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6664         parent agent draft)
6665     (define-key keymap "S" map)
6666     (define-key map [t] nil)
6667     (with-current-buffer gnus-article-current-summary
6668       (set-keymap-parent
6669        keymap
6670        (if (setq parent (keymap-parent gnus-article-mode-map))
6671            (prog1
6672                (setq parent (copy-keymap parent))
6673              (set-keymap-parent parent (current-local-map)))
6674          (current-local-map)))
6675       (set-keymap-parent map (key-binding "S"))
6676       (let (key def gnus-pick-mode)
6677         (while sumkeys
6678           (setq key (pop sumkeys))
6679           (cond ((and (vectorp key) (= (length key) 1)
6680                       (consp (setq def (aref key 0)))
6681                       (numberp (car def)) (numberp (cdr def)))
6682                  (when (< (max (car def) (cdr def)) 128)
6683                    (setq sumkeys
6684                          (append (mapcar
6685                                   #'vector
6686                                   (nreverse (gnus-uncompress-range def)))
6687                                  sumkeys))))
6688                 ((setq def (key-binding key))
6689                  (unless (eq def 'undefined)
6690                    (define-key keymap key def))))))
6691       (when (boundp 'gnus-agent-summary-mode)
6692         (setq agent gnus-agent-summary-mode))
6693       (when (boundp 'gnus-draft-mode)
6694         (setq draft gnus-draft-mode)))
6695     (with-temp-buffer
6696       (use-local-map keymap)
6697       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6698       (set (make-local-variable 'gnus-draft-mode) draft)
6699       (describe-bindings prefix))
6700     (let ((item `((lambda (prefix)
6701                     (with-current-buffer ,(current-buffer)
6702                       (gnus-article-describe-bindings prefix)))
6703                   ,prefix)))
6704       (with-current-buffer (let (help-xref-following) (help-buffer))
6705         (setq help-xref-stack-item item)))))
6706
6707 (defun gnus-article-reply-with-original (&optional wide)
6708   "Start composing a reply mail to the current message.
6709 The text in the region will be yanked.  If the region isn't active,
6710 the entire article will be yanked."
6711   (interactive)
6712   (let ((article (cdr gnus-article-current))
6713         contents)
6714     (if (not (gnus-region-active-p))
6715         (with-current-buffer gnus-summary-buffer
6716           (gnus-summary-reply (list (list article)) wide))
6717       (setq contents (buffer-substring (point) (mark t)))
6718       ;; Deactivate active regions.
6719       (when (and (boundp 'transient-mark-mode)
6720                  transient-mark-mode)
6721         (setq mark-active nil))
6722       (with-current-buffer gnus-summary-buffer
6723         (gnus-summary-reply
6724          (list (list article contents)) wide)))))
6725
6726 (defun gnus-article-wide-reply-with-original ()
6727   "Start composing a wide reply mail to the current message.
6728 The text in the region will be yanked.  If the region isn't active,
6729 the entire article will be yanked."
6730   (interactive)
6731   (gnus-article-reply-with-original t))
6732
6733 (defun gnus-article-followup-with-original ()
6734   "Compose a followup to the current article.
6735 The text in the region will be yanked.  If the region isn't active,
6736 the entire article will be yanked."
6737   (interactive)
6738   (let ((article (cdr gnus-article-current))
6739         contents)
6740       (if (not (gnus-region-active-p))
6741           (with-current-buffer gnus-summary-buffer
6742             (gnus-summary-followup (list (list article))))
6743         (setq contents (buffer-substring (point) (mark t)))
6744         ;; Deactivate active regions.
6745         (when (and (boundp 'transient-mark-mode)
6746                    transient-mark-mode)
6747           (setq mark-active nil))
6748         (with-current-buffer gnus-summary-buffer
6749           (gnus-summary-followup
6750            (list (list article contents)))))))
6751
6752 (defun gnus-article-hide (&optional arg force)
6753   "Hide all the gruft in the current article.
6754 This means that signatures, cited text and (some) headers will be
6755 hidden.
6756 If given a prefix, show the hidden text instead."
6757   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6758   (gnus-article-hide-headers arg)
6759   (gnus-article-hide-list-identifiers arg)
6760   (gnus-article-hide-citation-maybe arg force)
6761   (gnus-article-hide-signature arg))
6762
6763 (defun gnus-article-maybe-highlight ()
6764   "Do some article highlighting if article highlighting is requested."
6765   (when (gnus-visual-p 'article-highlight 'highlight)
6766     (gnus-article-highlight-some)))
6767
6768 (defun gnus-check-group-server ()
6769   ;; Make sure the connection to the server is alive.
6770   (unless (gnus-server-opened
6771            (gnus-find-method-for-group gnus-newsgroup-name))
6772     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6773     (gnus-request-group gnus-newsgroup-name t)))
6774
6775 (eval-when-compile
6776   (autoload 'nneething-get-file-name "nneething"))
6777
6778 (defun gnus-request-article-this-buffer (article group)
6779   "Get an article and insert it into this buffer."
6780   (let (do-update-line sparse-header)
6781     (prog1
6782         (save-excursion
6783           (erase-buffer)
6784           (gnus-kill-all-overlays)
6785           (setq group (or group gnus-newsgroup-name))
6786
6787           ;; Using `gnus-request-article' directly will insert the article into
6788           ;; `nntp-server-buffer' - so we'll save some time by not having to
6789           ;; copy it from the server buffer into the article buffer.
6790
6791           ;; We only request an article by message-id when we do not have the
6792           ;; headers for it, so we'll have to get those.
6793           (when (stringp article)
6794             (gnus-read-header article))
6795
6796           ;; If the article number is negative, that means that this article
6797           ;; doesn't belong in this newsgroup (possibly), so we find its
6798           ;; message-id and request it by id instead of number.
6799           (when (and (numberp article)
6800                      gnus-summary-buffer
6801                      (get-buffer gnus-summary-buffer)
6802                      (gnus-buffer-exists-p gnus-summary-buffer))
6803             (with-current-buffer gnus-summary-buffer
6804               (let ((header (gnus-summary-article-header article)))
6805                 (when (< article 0)
6806                   (cond
6807                    ((memq article gnus-newsgroup-sparse)
6808                     ;; This is a sparse gap article.
6809                     (setq do-update-line article)
6810                     (setq article (mail-header-id header))
6811                     (setq sparse-header (gnus-read-header article))
6812                     (setq gnus-newsgroup-sparse
6813                           (delq article gnus-newsgroup-sparse)))
6814                    ((vectorp header)
6815                     ;; It's a real article.
6816                     (setq article (mail-header-id header)))
6817                    (t
6818                     ;; It is an extracted pseudo-article.
6819                     (setq article 'pseudo)
6820                     (gnus-request-pseudo-article header))))
6821
6822                 (let ((method (gnus-find-method-for-group
6823                                gnus-newsgroup-name)))
6824                   (when (and (eq (car method) 'nneething)
6825                              (vectorp header))
6826                     (let ((dir (nneething-get-file-name
6827                                 (mail-header-id header))))
6828                       (when (and (stringp dir)
6829                                  (file-directory-p dir))
6830                         (setq article 'nneething)
6831                         (gnus-group-enter-directory dir))))))))
6832
6833           (cond
6834            ;; Refuse to select canceled articles.
6835            ((and (numberp article)
6836                  gnus-summary-buffer
6837                  (get-buffer gnus-summary-buffer)
6838                  (gnus-buffer-exists-p gnus-summary-buffer)
6839                  (eq (cdr (with-current-buffer gnus-summary-buffer
6840                             (assq article gnus-newsgroup-reads)))
6841                      gnus-canceled-mark))
6842             nil)
6843            ;; We first check `gnus-original-article-buffer'.
6844            ((and (get-buffer gnus-original-article-buffer)
6845                  (numberp article)
6846                  (with-current-buffer gnus-original-article-buffer
6847                    (and (equal (car gnus-original-article) group)
6848                         (eq (cdr gnus-original-article) article))))
6849             ;; `insert-buffer-substring' would incorrectly use the
6850             ;; equivalent of string-make-multibyte which amount to decoding
6851             ;; with locale-coding-system, causing failure of
6852             ;; subsequent decoding.
6853             (insert (mm-string-to-multibyte
6854                      (with-current-buffer gnus-original-article-buffer
6855                        (buffer-substring (point-min) (point-max)))))
6856             'article)
6857            ;; Check the backlog.
6858            ((and gnus-keep-backlog
6859                  (gnus-backlog-request-article group article (current-buffer)))
6860             'article)
6861            ;; Check asynchronous pre-fetch.
6862            ((gnus-async-request-fetched-article group article (current-buffer))
6863             (gnus-async-prefetch-next group article gnus-summary-buffer)
6864             (when (and (numberp article) gnus-keep-backlog)
6865               (gnus-backlog-enter-article group article (current-buffer)))
6866             'article)
6867            ;; Check the cache.
6868            ((and gnus-use-cache
6869                  (numberp article)
6870                  (gnus-cache-request-article article group))
6871             'article)
6872            ;; Check the agent cache.
6873            ((gnus-agent-request-article article group)
6874             'article)
6875            ;; Get the article and put into the article buffer.
6876            ((or (stringp article)
6877                 (numberp article))
6878             (let ((gnus-override-method gnus-override-method)
6879                   (methods (and (stringp article)
6880                                 (with-current-buffer gnus-summary-buffer
6881                                   (gnus-refer-article-methods))))
6882                   (backend (car (gnus-find-method-for-group
6883                                  gnus-newsgroup-name)))
6884                   result
6885                   (inhibit-read-only t))
6886               (when (and (null gnus-override-method)
6887                          methods)
6888                 (setq gnus-override-method (pop methods)))
6889               (while (not result)
6890                 (erase-buffer)
6891                 (gnus-kill-all-overlays)
6892                 (let ((gnus-newsgroup-name group))
6893                   (gnus-check-group-server))
6894                 (cond
6895                  ((gnus-request-article article group (current-buffer))
6896                   (when (numberp article)
6897                     (gnus-async-prefetch-next group article
6898                                               gnus-summary-buffer)
6899                     (when gnus-keep-backlog
6900                       (gnus-backlog-enter-article
6901                        group article (current-buffer)))
6902                     (when (and gnus-agent
6903                                (gnus-agent-group-covered-p group))
6904                       (gnus-agent-store-article article group)))
6905                   (setq result 'article))
6906                  (methods
6907                   (setq gnus-override-method (pop methods)))
6908                  ((not (string-match "^400 "
6909                                      (nnheader-get-report backend)))
6910                   ;; If we get 400 server disconnect, reconnect and
6911                   ;; retry; otherwise, assume the article has expired.
6912                   (setq result 'done))))
6913               (and (eq result 'article) 'article)))
6914            ;; It was a pseudo.
6915            (t article)))
6916
6917       ;; Associate this article with the current summary buffer.
6918       (setq gnus-article-current-summary gnus-summary-buffer)
6919
6920       ;; Take the article from the original article buffer
6921       ;; and place it in the buffer it's supposed to be in.
6922       (when (and (get-buffer gnus-article-buffer)
6923                  (equal (buffer-name (current-buffer))
6924                         (buffer-name (get-buffer gnus-article-buffer))))
6925         (save-excursion
6926           (if (get-buffer gnus-original-article-buffer)
6927               (set-buffer gnus-original-article-buffer)
6928             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6929             (buffer-disable-undo)
6930             (setq major-mode 'gnus-original-article-mode)
6931             (setq buffer-read-only t))
6932           (let ((inhibit-read-only t))
6933             (erase-buffer)
6934             (insert-buffer-substring gnus-article-buffer))
6935           (setq gnus-original-article (cons group article)))
6936
6937         ;; Decode charsets.
6938         (run-hooks 'gnus-article-decode-hook)
6939         ;; Mark article as decoded or not.
6940         (setq gnus-article-decoded-p gnus-article-decode-hook))
6941
6942       ;; Update sparse articles.
6943       (when (and do-update-line
6944                  (or (numberp article)
6945                      (stringp article)))
6946         (let ((buf (current-buffer)))
6947           (set-buffer gnus-summary-buffer)
6948           (gnus-summary-update-article do-update-line sparse-header)
6949           (gnus-summary-goto-subject do-update-line nil t)
6950           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6951                             (point))
6952           (set-buffer buf))))))
6953
6954 (defun gnus-block-private-groups (group)
6955   (if (gnus-news-group-p group)
6956       ;; Block nothing in news groups.
6957       nil
6958     ;; Block everything anywhere else.
6959     "."))
6960
6961 (defun gnus-blocked-images ()
6962   (if (functionp gnus-blocked-images)
6963       (funcall gnus-blocked-images gnus-newsgroup-name)
6964     gnus-blocked-images))
6965
6966 ;;;
6967 ;;; Article editing
6968 ;;;
6969
6970 (defcustom gnus-article-edit-mode-hook nil
6971   "Hook run in article edit mode buffers."
6972   :group 'gnus-article-various
6973   :type 'hook)
6974
6975 (defvar gnus-article-edit-done-function nil)
6976
6977 (defvar gnus-article-edit-mode-map nil)
6978
6979 ;; Should we be using derived.el for this?
6980 (unless gnus-article-edit-mode-map
6981   (setq gnus-article-edit-mode-map (make-keymap))
6982   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6983
6984   (gnus-define-keys gnus-article-edit-mode-map
6985     "\C-c?"    describe-mode
6986     "\C-c\C-c" gnus-article-edit-done
6987     "\C-c\C-k" gnus-article-edit-exit
6988     "\C-c\C-f\C-t" message-goto-to
6989     "\C-c\C-f\C-o" message-goto-from
6990     "\C-c\C-f\C-b" message-goto-bcc
6991     ;;"\C-c\C-f\C-w" message-goto-fcc
6992     "\C-c\C-f\C-c" message-goto-cc
6993     "\C-c\C-f\C-s" message-goto-subject
6994     "\C-c\C-f\C-r" message-goto-reply-to
6995     "\C-c\C-f\C-n" message-goto-newsgroups
6996     "\C-c\C-f\C-d" message-goto-distribution
6997     "\C-c\C-f\C-f" message-goto-followup-to
6998     "\C-c\C-f\C-m" message-goto-mail-followup-to
6999     "\C-c\C-f\C-k" message-goto-keywords
7000     "\C-c\C-f\C-u" message-goto-summary
7001     "\C-c\C-f\C-i" message-insert-or-toggle-importance
7002     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
7003     "\C-c\C-b" message-goto-body
7004     "\C-c\C-i" message-goto-signature
7005
7006     "\C-c\C-t" message-insert-to
7007     "\C-c\C-n" message-insert-newsgroups
7008     "\C-c\C-o" message-sort-headers
7009     "\C-c\C-e" message-elide-region
7010     "\C-c\C-v" message-delete-not-region
7011     "\C-c\C-z" message-kill-to-signature
7012     "\M-\r" message-newline-and-reformat
7013     "\C-c\C-a" mml-attach-file
7014     "\C-a" message-beginning-of-line
7015     "\t" message-tab
7016     "\M-;" comment-region)
7017
7018   (gnus-define-keys (gnus-article-edit-wash-map
7019                      "\C-c\C-w" gnus-article-edit-mode-map)
7020     "f" gnus-article-edit-full-stops))
7021
7022 (easy-menu-define
7023   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
7024   '("Field"
7025     ["Fetch To" message-insert-to t]
7026     ["Fetch Newsgroups" message-insert-newsgroups t]
7027     "----"
7028     ["To" message-goto-to t]
7029     ["From" message-goto-from t]
7030     ["Subject" message-goto-subject t]
7031     ["Cc" message-goto-cc t]
7032     ["Reply-To" message-goto-reply-to t]
7033     ["Summary" message-goto-summary t]
7034     ["Keywords" message-goto-keywords t]
7035     ["Newsgroups" message-goto-newsgroups t]
7036     ["Followup-To" message-goto-followup-to t]
7037     ["Mail-Followup-To" message-goto-mail-followup-to t]
7038     ["Distribution" message-goto-distribution t]
7039     ["Body" message-goto-body t]
7040     ["Signature" message-goto-signature t]))
7041
7042 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7043   "Major mode for editing articles.
7044 This is an extended text-mode.
7045
7046 \\{gnus-article-edit-mode-map}"
7047   (make-local-variable 'gnus-article-edit-done-function)
7048   (make-local-variable 'gnus-prev-winconf)
7049   (set (make-local-variable 'font-lock-defaults)
7050        '(message-font-lock-keywords t))
7051   (set (make-local-variable 'mail-header-separator) "")
7052   (set (make-local-variable 'gnus-article-edit-mode) t)
7053   (easy-menu-add message-mode-field-menu message-mode-map)
7054   (mml-mode)
7055   (setq buffer-read-only nil)
7056   (buffer-enable-undo)
7057   (widen))
7058
7059 (defun gnus-article-edit (&optional force)
7060   "Edit the current article.
7061 This will have permanent effect only in mail groups.
7062 If FORCE is non-nil, allow editing of articles even in read-only
7063 groups."
7064   (interactive "P")
7065   (when (and (not force)
7066              (gnus-group-read-only-p))
7067     (error "The current newsgroup does not support article editing"))
7068   (gnus-article-date-original)
7069   (gnus-article-edit-article
7070    'ignore
7071    `(lambda (no-highlight)
7072       'ignore
7073       (gnus-summary-edit-article-done
7074        ,(or (mail-header-references gnus-current-headers) "")
7075        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7076
7077 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7078   "Start editing the contents of the current article buffer."
7079   (let ((winconf (current-window-configuration)))
7080     (set-buffer gnus-article-buffer)
7081     (let ((message-auto-save-directory
7082            ;; Don't associate the article buffer with a draft file.
7083            nil))
7084       (gnus-article-edit-mode))
7085     (funcall start-func)
7086     (set-buffer-modified-p nil)
7087     (gnus-configure-windows 'edit-article)
7088     (setq gnus-article-edit-done-function exit-func)
7089     (setq gnus-prev-winconf winconf)
7090     (unless quiet
7091       (gnus-message 6 "C-c C-c to end edits"))))
7092
7093 (defun gnus-article-edit-done (&optional arg)
7094   "Update the article edits and exit."
7095   (interactive "P")
7096   (let ((func gnus-article-edit-done-function)
7097         (buf (current-buffer))
7098         (start (window-start))
7099         (p (point))
7100         (winconf gnus-prev-winconf))
7101     (widen) ;; Widen it in case that users narrowed the buffer.
7102     (funcall func arg)
7103     (set-buffer buf)
7104     ;; The cache and backlog have to be flushed somewhat.
7105     (when gnus-keep-backlog
7106       (gnus-backlog-remove-article
7107        (car gnus-article-current) (cdr gnus-article-current)))
7108     ;; Flush original article as well.
7109     (gnus-flush-original-article-buffer)
7110     (when gnus-use-cache
7111       (gnus-cache-update-article
7112        (car gnus-article-current) (cdr gnus-article-current)))
7113     ;; We remove all text props from the article buffer.
7114     (kill-all-local-variables)
7115     (set-text-properties (point-min) (point-max) nil)
7116     (gnus-article-mode)
7117     (set-window-configuration winconf)
7118     (set-buffer buf)
7119     (set-window-start (get-buffer-window buf) start)
7120     (set-window-point (get-buffer-window buf) (point)))
7121   (gnus-summary-show-article))
7122
7123 (defun gnus-flush-original-article-buffer ()
7124   (when (get-buffer gnus-original-article-buffer)
7125     (with-current-buffer gnus-original-article-buffer
7126       (setq gnus-original-article nil))))
7127
7128 (defun gnus-article-edit-exit ()
7129   "Exit the article editing without updating."
7130   (interactive)
7131   (when (or (not (buffer-modified-p))
7132             (yes-or-no-p "Article modified; kill anyway? "))
7133     (let ((curbuf (current-buffer))
7134           (p (point))
7135           (window-start (window-start)))
7136       (erase-buffer)
7137       (if (gnus-buffer-live-p gnus-original-article-buffer)
7138           (insert-buffer-substring gnus-original-article-buffer))
7139       (let ((winconf gnus-prev-winconf))
7140         (kill-all-local-variables)
7141         (gnus-article-mode)
7142         (set-window-configuration winconf)
7143         ;; Tippy-toe some to make sure that point remains where it was.
7144         (save-current-buffer
7145           (set-buffer curbuf)
7146           (set-window-start (get-buffer-window (current-buffer)) window-start)
7147           (goto-char p))))
7148     (gnus-summary-show-article)))
7149
7150 (defun gnus-article-edit-full-stops ()
7151   "Interactively repair spacing at end of sentences."
7152   (interactive)
7153   (save-excursion
7154     (goto-char (point-min))
7155     (search-forward-regexp "^$" nil t)
7156     (let ((case-fold-search nil))
7157       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7158
7159 ;;;
7160 ;;; Article highlights
7161 ;;;
7162
7163 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7164
7165 ;;; Internal Variables:
7166
7167 (defcustom gnus-button-url-regexp
7168   (concat
7169    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7170    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7171    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7172    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7173        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7174              (punct "!?:;.,"))
7175          (concat
7176           "\\(?:"
7177           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7178           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7179           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7180           "\\|"
7181           "[" chars punct     "]+" "[" chars "]"
7182           "\\)"))
7183      (concat ;; XEmacs 21.4 doesn't support POSIX.
7184       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7185       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7186    "\\)")
7187   "Regular expression that matches URLs."
7188   :group 'gnus-article-buttons
7189   :type 'regexp)
7190
7191 (defcustom gnus-button-valid-fqdn-regexp
7192   message-valid-fqdn-regexp
7193   "Regular expression that matches a valid FQDN."
7194   :version "22.1"
7195   :group 'gnus-article-buttons
7196   :type 'regexp)
7197
7198 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7199 (defcustom gnus-button-valid-localpart-regexp
7200   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7201   "Regular expression that matches a localpart of mail addresses or MIDs."
7202   :version "22.1"
7203   :group 'gnus-article-buttons
7204   :type 'regexp)
7205
7206 (defcustom gnus-button-man-handler 'manual-entry
7207   "Function to use for displaying man pages.
7208 The function must take at least one argument with a string naming the
7209 man page."
7210   :version "22.1"
7211   :type '(choice (function-item :tag "Man" manual-entry)
7212                  (function-item :tag "Woman" woman)
7213                  (function :tag "Other"))
7214   :group 'gnus-article-buttons)
7215
7216 (defcustom gnus-button-mid-or-mail-regexp
7217   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7218           gnus-button-valid-fqdn-regexp
7219           ">?\\)\\b")
7220   "Regular expression that matches a message ID or a mail address."
7221   :version "22.1"
7222   :group 'gnus-article-buttons
7223   :type 'regexp)
7224
7225 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7226   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7227 Strings like this can be either a message ID or a mail address.  If it is one
7228 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7229 message ID or a mail address, respectively.  If this variable is set to the
7230 symbol `ask', always query the user what do do.  If it is a function, this
7231 function will be called with the string as its only argument.  The function
7232 must return `mid', `mail', `invalid' or `ask'."
7233   :version "22.1"
7234   :group 'gnus-article-buttons
7235   :type '(choice (function-item :tag "Heuristic function"
7236                                 gnus-button-mid-or-mail-heuristic)
7237                  (const ask)
7238                  (const mid)
7239                  (const mail)))
7240
7241 (defcustom gnus-button-mid-or-mail-heuristic-alist
7242   '((-10.0 . ".+\\$.+@")
7243     (-10.0 . "#")
7244     (-10.0 . "\\*")
7245     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7246     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7247     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7248     (-1.0  . "^[^a-z]+@")
7249     ;;
7250     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7251     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7252     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7253     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7254     ;;
7255     (-2.0  . "^[0-9]")
7256     (-1.0  . "^[0-9][0-9]")
7257     ;;
7258     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7259     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7260     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7261     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7262     ;;
7263     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7264     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7265     ;;       "[0-9]{8,}.*\@"
7266     (-3.0
7267      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7268     ;; "[0-9]{12,}.*\@"
7269     ;; compensation for TDMA dated mail addresses:
7270     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7271     ;;
7272     (-20.0 . "\\.fsf@") ;; Gnus
7273     (-20.0 . "^slrn")
7274     (-20.0 . "^Pine")
7275     (-20.0 . "^alpine\\.")
7276     (-20.0 . "_-_") ;; Subject change in thread
7277     ;;
7278     (-20.0 . "\\.ln@") ;; leafnode
7279     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7280     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7281     ;;
7282     ;; (5.0 . "") ;; $local_part_len <= 7
7283     (10.0  . "^[^0-9]+@")
7284     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7285     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7286     (3.0   . "\@stud")
7287     ;;
7288     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7289     ;;
7290     (0.5   . "^[A-Z][a-z]")
7291     (0.5   . "^[A-Z][a-z][a-z]")
7292     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7293     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7294   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7295
7296 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7297 address.  The REGEXP is processed with `case-fold-search' set to nil."
7298   :version "22.1"
7299   :group 'gnus-article-buttons
7300   :type '(repeat (cons (number :tag "Rate")
7301                        (regexp :tag "Regexp"))))
7302
7303 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7304   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7305 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7306 address, `ask' if unsure and `invalid' if the string is invalid."
7307   (let ((case-fold-search nil)
7308         (list gnus-button-mid-or-mail-heuristic-alist)
7309         (result 0) rate regexp lpartlen elem)
7310     (setq lpartlen
7311           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7312     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7313     ;; Certain special cases...
7314     (when (string-match
7315            (concat
7316             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7317             "^[0-9]+\\.[0-9]+@compuserve\\|"
7318             "@public\\.gmane\\.org")
7319            mid-or-mail)
7320       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7321       (setq result 'mail))
7322     (when (string-match "@.*@\\| " mid-or-mail)
7323       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7324       (setq result 'invalid))
7325     ;; Nothing more to do, if result is not a number here...
7326     (when (numberp result)
7327       (while list
7328         (setq elem (car list)
7329               rate (car elem)
7330               regexp (cdr elem)
7331               list (cdr list))
7332         (when (string-match regexp mid-or-mail)
7333           (setq result (+ result rate))
7334           (gnus-message
7335            9 "`%s' matched `%s', rate `%s', result `%s'."
7336            mid-or-mail regexp rate result)))
7337       (when (<= lpartlen 7)
7338         (setq result (+ result 5.0))
7339         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7340                       mid-or-mail result))
7341       (when (>= lpartlen 12)
7342         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7343         (cond
7344          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7345           ;; Long local part should contain realname if e-mail address,
7346           ;; too many digits: message-id.
7347           ;; $score -= 5.0 + 0.1 * $local_part_len;
7348           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7349           (setq result (+ result rate))
7350           (gnus-message
7351            9 "Many digits in `%s', rate `%s', result `%s'."
7352            mid-or-mail rate result))
7353          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7354                         mid-or-mail)
7355           ;; Too few vowels [^aeiouy]{4,}.*\@
7356           (setq result (+ result -5.0))
7357           (gnus-message
7358            9 "Few vowels in `%s', rate `%s', result `%s'."
7359            mid-or-mail -5.0 result))
7360          (t
7361           (setq result (+ result 5.0))
7362           (gnus-message
7363            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7364     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7365     ;; Maybe we should make this a customizable alist: (condition . 'result)
7366     (cond
7367      ((symbolp result) result)
7368      ;; Now convert number into proper results:
7369      ((< result -10.0) 'mid)
7370      ((> result  10.0) 'mail)
7371      (t 'ask))))
7372
7373 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7374   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7375          (url-mid (concat "news" ":" mid-or-mail))
7376          (url-mailto (concat "mailto" ":" mid-or-mail)))
7377     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7378     (when (fboundp pref)
7379       (setq guessed
7380             ;; get rid of surrounding angles...
7381             (funcall pref
7382                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7383       (if (or (eq 'mid guessed) (eq 'mail guessed))
7384           (setq pref guessed)
7385         (setq pref 'ask)))
7386     (if (eq pref 'ask)
7387         (save-window-excursion
7388           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7389               (setq pref 'mail)
7390             (setq pref 'mid))))
7391     (cond ((eq pref 'mid)
7392            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7393            (gnus-button-handle-news url-mid))
7394           ((eq pref 'mail)
7395            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7396            (gnus-url-mailto url-mailto))
7397           (t (gnus-message 3 "Invalid string.")))))
7398
7399 (defun gnus-button-handle-custom (fun arg)
7400   "Call function FUN on argument ARG.
7401 Both FUN and ARG are supposed to be strings.  ARG will be passed
7402 as a symbol to FUN."
7403   (funcall (intern fun)
7404            (if (string-match "^customize-apropos" fun)
7405                arg
7406              (intern arg))))
7407
7408 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7409
7410 (defun gnus-button-handle-describe-function (url)
7411   "Call `describe-function' when pushing the corresponding URL button."
7412   (describe-function
7413    (intern
7414     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7415
7416 (defun gnus-button-handle-describe-variable (url)
7417   "Call `describe-variable' when pushing the corresponding URL button."
7418   (describe-variable
7419    (intern
7420     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7421
7422 (defun gnus-button-handle-symbol (url)
7423 "Display help on variable or function.
7424 Calls `describe-variable' or `describe-function'."
7425   (let ((sym (intern url)))
7426     (cond
7427      ((fboundp sym) (describe-function sym))
7428      ((boundp sym) (describe-variable sym))
7429      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7430
7431 (defun gnus-button-handle-describe-key (url)
7432   "Call `describe-key' when pushing the corresponding URL button."
7433   (let* ((key-string
7434           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7435          (keys (ignore-errors (eval `(kbd ,key-string)))))
7436     (if keys
7437         (describe-key keys)
7438       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7439
7440 (defun gnus-button-handle-apropos (url)
7441   "Call `apropos' when pushing the corresponding URL button."
7442   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7443
7444 (defun gnus-button-handle-apropos-command (url)
7445   "Call `apropos' when pushing the corresponding URL button."
7446   (apropos-command
7447    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7448
7449 (defun gnus-button-handle-apropos-variable (url)
7450   "Call `apropos' when pushing the corresponding URL button."
7451   (funcall
7452    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7453    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7454
7455 (defun gnus-button-handle-apropos-documentation (url)
7456   "Call `apropos' when pushing the corresponding URL button."
7457   (funcall
7458    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7459    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7460
7461 (defun gnus-button-handle-library (url)
7462   "Call `locate-library' when pushing the corresponding URL button."
7463   (gnus-message 9 "url=`%s'" url)
7464   (let* ((lib (locate-library url))
7465          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7466     (if (not lib)
7467         (gnus-message 1 "Cannot locale library `%s'." url)
7468       (find-file-read-only file))))
7469
7470 (defcustom gnus-button-man-level 5
7471   "*Integer that says how many man-related buttons Gnus will show.
7472 The higher the number, the more buttons will appear and the more false
7473 positives are possible.  Note that you can set this variable local to
7474 specific groups.  Setting it higher in Unix groups is probably a good idea.
7475 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7476 how to set variables in specific groups."
7477   :version "22.1"
7478   :group 'gnus-article-buttons
7479   :link '(custom-manual "(gnus)Group Parameters")
7480   :type 'integer)
7481
7482 (defcustom gnus-button-emacs-level 5
7483   "*Integer that says how many emacs-related buttons Gnus will show.
7484 The higher the number, the more buttons will appear and the more false
7485 positives are possible.  Note that you can set this variable local to
7486 specific groups.  Setting it higher in Emacs or Gnus related groups is
7487 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7488 `gnus-parameters' on how to set variables in specific groups."
7489   :version "22.1"
7490   :group 'gnus-article-buttons
7491   :link '(custom-manual "(gnus)Group Parameters")
7492   :type 'integer)
7493
7494 (defcustom gnus-button-message-level 5
7495   "*Integer that says how many buttons for news or mail messages will appear.
7496 The higher the number, the more buttons will appear and the more false
7497 positives are possible."
7498   ;; mail addresses, MIDs, URLs for news, ...
7499   :version "22.1"
7500   :group 'gnus-article-buttons
7501   :type 'integer)
7502
7503 (defcustom gnus-button-browse-level 5
7504   "*Integer that says how many buttons for browsing will appear.
7505 The higher the number, the more buttons will appear and the more false
7506 positives are possible."
7507   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7508   :version "22.1"
7509   :group 'gnus-article-buttons
7510   :type 'integer)
7511
7512 (defcustom gnus-button-alist
7513   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7514      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7515     ((concat "\\b\\(nntp\\|news\\):\\("
7516              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7517      0 t gnus-button-handle-news 2)
7518     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7519      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7520     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7521      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7522     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7523     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7524      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7525     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7526      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7527     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7528      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7529     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7530      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7531     ;; RFC 2368 (The mailto URL scheme)
7532     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7533      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7534     ("\\bmailto:\\([^ \n\t]+\\)"
7535      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7536     ;; Info Konqueror style <info:/foo/bar baz>.
7537     ;; Must come before " Gnus home-grown style".
7538     ("\\binfo://?\\([^'\">\n\t]+\\)"
7539      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7540    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7541     ("\\binfo://\\([^'\">\n\t ]+\\)"
7542      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7543     ;; Info GNOME style <info:foo#bar_baz>
7544     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7545      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7546     ;; Info KDE style <info:(foo)bar baz>
7547     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7548      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7549     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7550      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7551     ("\\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,]*\\)\\)?"
7552      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7553      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7554     ;; This is custom
7555     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7556      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7557     ;; Emacs help commands
7558     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7559      ;; regexp doesn't match arguments containing ` '.
7560      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7561     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7562      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7563     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7564      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7565     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7566      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7567     ;; The following entries may lead to many false positives so don't enable
7568     ;; them by default (use a high button level).
7569     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7570      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7571      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7572     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7573      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7574     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7575      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7576     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7577      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7578     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7579      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7580     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7581      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7582     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7583      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7584     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7585      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7586     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7587      ;; Unlike the other regexps we really have to require quoting
7588      ;; here to determine where it ends.
7589      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7590     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7591     ("<URL: *\\([^\n<>]*\\)>"
7592      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7593     ;; RFC 2396 (2.4.3., delims) ...
7594     ("\"URL: *\\([^\n\"]*\\)\""
7595      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7596     ;; Raw URLs.
7597     (gnus-button-url-regexp
7598      0 (>= gnus-button-browse-level 0) browse-url 0)
7599     ;; man pages
7600     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7601      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7602      gnus-button-handle-man 1)
7603     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7604     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7605      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7606      gnus-button-handle-man 1)
7607     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7608     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7609     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7610      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7611     ;; Recognizing patches to .el files.  This is somewhat obscure,
7612     ;; but considering the percentage of Gnus users who hack Emacs
7613     ;; Lisp files...
7614     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7615      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7616     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7617      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7618     ;; MID or mail: To avoid too many false positives we don't try to catch
7619     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7620     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7621     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7622     ;; so that non-ambiguous entries (see above) match first.
7623     (gnus-button-mid-or-mail-regexp
7624      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7625   "*Alist of regexps matching buttons in article bodies.
7626
7627 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7628 REGEXP: is the string (case insensitive) matching text around the button (can
7629 also be Lisp expression evaluating to a string),
7630 BUTTON: is the number of the regexp grouping actually matching the button,
7631 FORM: is a Lisp expression which must eval to true for the button to
7632 be added,
7633 CALLBACK: is the function to call when the user push this button, and each
7634 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7635
7636 CALLBACK can also be a variable, in that case the value of that
7637 variable it the real callback function."
7638   :group 'gnus-article-buttons
7639   :type '(repeat (list (choice regexp variable sexp)
7640                        (integer :tag "Button")
7641                        (sexp :tag "Form")
7642                        (function :tag "Callback")
7643                        (repeat :tag "Par"
7644                                :inline t
7645                                (integer :tag "Regexp group")))))
7646 (put 'gnus-button-alist 'risky-local-variable t)
7647
7648 (defcustom gnus-header-button-alist
7649   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7650      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7651     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7652      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7653     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7654      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7655     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7656      0 (>= gnus-button-browse-level 0) browse-url 0)
7657     ("^Subject:" gnus-button-url-regexp
7658      0 (>= gnus-button-browse-level 0) browse-url 0)
7659     ("^[^:]+:" gnus-button-url-regexp
7660      0 (>= gnus-button-browse-level 0) browse-url 0)
7661     ("^OpenPGP:.*url=" gnus-button-url-regexp
7662      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7663     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7664      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7665     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7666      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7667   "*Alist of headers and regexps to match buttons in article heads.
7668
7669 This alist is very similar to `gnus-button-alist', except that each
7670 alist has an additional HEADER element first in each entry:
7671
7672 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7673
7674 HEADER is a regexp to match a header.  For a fuller explanation, see
7675 `gnus-button-alist'."
7676   :group 'gnus-article-buttons
7677   :group 'gnus-article-headers
7678   :type '(repeat (list (regexp :tag "Header")
7679                        (choice regexp variable)
7680                        (integer :tag "Button")
7681                        (sexp :tag "Form")
7682                        (function :tag "Callback")
7683                        (repeat :tag "Par"
7684                                :inline t
7685                                (integer :tag "Regexp group")))))
7686 (put 'gnus-header-button-alist 'risky-local-variable t)
7687
7688 ;;; Commands:
7689
7690 (defun gnus-article-push-button (event)
7691   "Check text under the mouse pointer for a callback function.
7692 If the text under the mouse pointer has a `gnus-callback' property,
7693 call it with the value of the `gnus-data' text property."
7694   (interactive "e")
7695   (set-buffer (window-buffer (posn-window (event-start event))))
7696   (let* ((pos (posn-point (event-start event)))
7697          (data (get-text-property pos 'gnus-data))
7698          (fun (get-text-property pos 'gnus-callback)))
7699     (goto-char pos)
7700     (when fun
7701       (funcall fun data))))
7702
7703 (defun gnus-article-press-button ()
7704   "Check text at point for a callback function.
7705 If the text at point has a `gnus-callback' property,
7706 call it with the value of the `gnus-data' text property."
7707   (interactive)
7708   (let ((data (get-text-property (point) 'gnus-data))
7709         (fun (get-text-property (point) 'gnus-callback)))
7710     (when fun
7711       (funcall fun data))))
7712
7713 (defun gnus-article-highlight (&optional force)
7714   "Highlight current article.
7715 This function calls `gnus-article-highlight-headers',
7716 `gnus-article-highlight-citation',
7717 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7718 do the highlighting.  See the documentation for those functions."
7719   (interactive (list 'force))
7720   (gnus-article-highlight-headers)
7721   (gnus-article-highlight-citation force)
7722   (gnus-article-highlight-signature)
7723   (gnus-article-add-buttons)
7724   (gnus-article-add-buttons-to-head))
7725
7726 (defun gnus-article-highlight-some (&optional force)
7727   "Highlight current article.
7728 This function calls `gnus-article-highlight-headers',
7729 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7730 do the highlighting.  See the documentation for those functions."
7731   (interactive (list 'force))
7732   (gnus-article-highlight-headers)
7733   (gnus-article-highlight-signature)
7734   (gnus-article-add-buttons))
7735
7736 (defun gnus-article-highlight-headers ()
7737   "Highlight article headers as specified by `gnus-header-face-alist'."
7738   (interactive)
7739   (gnus-with-article-headers
7740     (let (regexp header-face field-face from hpoints fpoints)
7741       (dolist (entry gnus-header-face-alist)
7742         (goto-char (point-min))
7743         (setq regexp (concat "^\\("
7744                              (if (string-equal "" (nth 0 entry))
7745                                  "[^\t ]"
7746                                (nth 0 entry))
7747                              "\\)")
7748               header-face (nth 1 entry)
7749               field-face (nth 2 entry))
7750         (while (and (re-search-forward regexp nil t)
7751                     (not (eobp)))
7752           (beginning-of-line)
7753           (setq from (point))
7754           (unless (search-forward ":" nil t)
7755             (forward-char 1))
7756           (when (and header-face
7757                      (not (memq (point) hpoints)))
7758             (push (point) hpoints)
7759             (gnus-put-text-property from (point) 'face header-face))
7760           (when (and field-face
7761                      (not (memq (setq from (point)) fpoints)))
7762             (push from fpoints)
7763             (if (re-search-forward "^[^ \t]" nil t)
7764                 (forward-char -2)
7765               (goto-char (point-max)))
7766             (gnus-put-text-property from (point) 'face field-face)))))))
7767
7768 (defun gnus-article-highlight-signature ()
7769   "Highlight the signature in an article.
7770 It does this by highlighting everything after
7771 `gnus-signature-separator' using the face `gnus-signature'."
7772   (interactive)
7773   (gnus-with-article-buffer
7774     (let ((inhibit-point-motion-hooks t))
7775       (save-restriction
7776         (when (and gnus-signature-face
7777                    (gnus-article-narrow-to-signature))
7778           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7779                             'face gnus-signature-face)
7780           (widen)
7781           (gnus-article-search-signature)
7782           (let ((start (match-beginning 0))
7783                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7784             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7785                                      end)))))))
7786
7787 (defun gnus-button-in-region-p (b e prop)
7788   "Say whether PROP exists in the region."
7789   (text-property-not-all b e prop nil))
7790
7791 (defun gnus-article-add-buttons ()
7792   "Find external references in the article and make buttons of them.
7793 \"External references\" are things like Message-IDs and URLs, as
7794 specified by `gnus-button-alist'."
7795   (interactive)
7796   (gnus-with-article-buffer
7797     (let ((inhibit-point-motion-hooks t)
7798           (case-fold-search t)
7799           (alist gnus-button-alist)
7800           beg entry regexp)
7801       ;; We skip the headers.
7802       (article-goto-body)
7803       (setq beg (point))
7804       (while (setq entry (pop alist))
7805         (setq regexp (eval (car entry)))
7806         (goto-char beg)
7807         (while (re-search-forward regexp nil t)
7808           (let ((start (match-beginning (nth 1 entry)))
7809                 (end (match-end (nth 1 entry)))
7810                 (from (match-beginning 0)))
7811             (when (and (eval (nth 2 entry))
7812                        (not (gnus-button-in-region-p
7813                              start end 'gnus-callback)))
7814               ;; That optional form returned non-nil, so we add the
7815               ;; button.
7816               (setq from (set-marker (make-marker) from))
7817               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7818                            (gnus-article-extend-url-button from start end))
7819                 (gnus-article-add-button start end
7820                                          'gnus-button-push (list from entry))
7821                 (gnus-put-text-property
7822                  start end
7823                  'gnus-string (buffer-substring-no-properties
7824                                start end))))))))))
7825
7826 (defun gnus-article-extend-url-button (beg start end)
7827   "Extend url button if url is folded into two or more lines.
7828 Return non-nil if button is extended.  BEG is a marker that points to
7829 the beginning position of a text containing url.  START and END are
7830 the endpoints of a url button before it is extended.  The concatenated
7831 url is put as the `gnus-button-url' overlay property on the button."
7832   (let ((opoint (point))
7833         (points (list start end))
7834         url delim regexp)
7835     (prog1
7836         (when (and (progn
7837                      (goto-char end)
7838                      (not (looking-at "[\t ]*[\">]")))
7839                    (progn
7840                      (goto-char start)
7841                      (string-match
7842                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7843                       (buffer-substring (point-at-bol) start)))
7844                    (progn
7845                      (setq url (list (buffer-substring start end))
7846                            delim (if (match-beginning 1) ">" "\""))
7847                      (beginning-of-line)
7848                      (setq regexp (concat
7849                                    (when (and (looking-at
7850                                                message-cite-prefix-regexp)
7851                                               (< (match-end 0) start))
7852                                      (regexp-quote (match-string 0)))
7853                                    "\
7854 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7855                                    delim "\\)"))
7856                      (while (progn
7857                               (forward-line 1)
7858                               (and (looking-at regexp)
7859                                    (prog1
7860                                        (match-beginning 1)
7861                                      (push (or (match-string 2)
7862                                                (match-string 1))
7863                                            url)
7864                                      (push (setq end (or (match-end 2)
7865                                                          (match-end 1)))
7866                                            points)
7867                                      (push (or (match-beginning 2)
7868                                                (match-beginning 1))
7869                                            points)))))
7870                      (match-beginning 2)))
7871           (let (gnus-article-mouse-face widget-mouse-face)
7872             (while points
7873               (gnus-article-add-button (pop points) (pop points)
7874                                        'gnus-button-push beg)))
7875           (let ((overlay (gnus-make-overlay start end)))
7876             (gnus-overlay-put overlay 'evaporate t)
7877             (gnus-overlay-put overlay 'gnus-button-url
7878                               (list (mapconcat 'identity (nreverse url) "")))
7879             (when gnus-article-mouse-face
7880               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7881           t)
7882       (goto-char opoint))))
7883
7884 ;; Add buttons to the head of an article.
7885 (defun gnus-article-add-buttons-to-head ()
7886   "Add buttons to the head of the article."
7887   (interactive)
7888   (gnus-with-article-headers
7889     (let (beg end)
7890       (dolist (entry gnus-header-button-alist)
7891         ;; Each alist entry.
7892         (goto-char (point-min))
7893         (while (re-search-forward (car entry) nil t)
7894           ;; Each header matching the entry.
7895           (setq beg (match-beginning 0))
7896           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7897                              (match-beginning 0))
7898                         (point-max)))
7899           (goto-char beg)
7900           (while (re-search-forward (eval (nth 1 entry)) end t)
7901             ;; Each match within a header.
7902             (let* ((entry (cdr entry))
7903                    (start (match-beginning (nth 1 entry)))
7904                    (end (match-end (nth 1 entry)))
7905                    (form (nth 2 entry)))
7906               (goto-char (match-end 0))
7907               (when (eval form)
7908                 (gnus-article-add-button
7909                  start end (nth 3 entry)
7910                  (buffer-substring (match-beginning (nth 4 entry))
7911                                    (match-end (nth 4 entry)))))))
7912           (goto-char end))))))
7913
7914 ;;; External functions:
7915
7916 (defun gnus-article-add-button (from to fun &optional data text)
7917   "Create a button between FROM and TO with callback FUN and data DATA."
7918   (when gnus-article-button-face
7919     (gnus-overlay-put (gnus-make-overlay from to nil t)
7920                       'face gnus-article-button-face))
7921   (gnus-add-text-properties
7922    from to
7923    (nconc (and gnus-article-mouse-face
7924                (list gnus-mouse-face-prop gnus-article-mouse-face))
7925           (list 'gnus-callback fun)
7926           (and data (list 'gnus-data data))))
7927   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7928                          :help-echo (or text "Follow the link")
7929                          :keymap gnus-url-button-map
7930                          :button-keymap gnus-widget-button-keymap))
7931
7932 (defun gnus-article-copy-string ()
7933   "Copy the string in the button to the kill ring."
7934   (interactive)
7935   (gnus-article-check-buffer)
7936   (let ((data (get-text-property (point) 'gnus-string)))
7937     (when data
7938       (with-temp-buffer
7939         (insert data)
7940         (copy-region-as-kill (point-min) (point-max))
7941         (message "Copied %s" data)))))
7942
7943 ;;; Internal functions:
7944
7945 (defun gnus-article-set-globals ()
7946   (with-current-buffer gnus-summary-buffer
7947     (gnus-set-global-variables)))
7948
7949 (defun gnus-signature-toggle (end)
7950   (gnus-with-article-buffer
7951     (let ((inhibit-point-motion-hooks t))
7952       (if (text-property-any end (point-max) 'article-type 'signature)
7953           (progn
7954             (gnus-delete-wash-type 'signature)
7955             (gnus-remove-text-properties-when
7956              'article-type 'signature end (point-max)
7957              (cons 'article-type (cons 'signature
7958                                        gnus-hidden-properties))))
7959         (gnus-add-wash-type 'signature)
7960         (gnus-add-text-properties-when
7961          'article-type nil end (point-max)
7962          (cons 'article-type (cons 'signature
7963                                    gnus-hidden-properties)))))
7964     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7965       (gnus-set-mode-line 'article))))
7966
7967 (defun gnus-button-push (marker-and-entry)
7968   ;; Push button starting at MARKER.
7969   (save-excursion
7970     (let* ((marker (car marker-and-entry))
7971            (entry (cadr marker-and-entry))
7972            (regexp (car entry))
7973            (inhibit-point-motion-hooks t))
7974       (goto-char marker)
7975       ;; This is obviously true, or something bad is happening :)
7976       ;; But we need it to have the match-data
7977       (when (looking-at (or (if (symbolp regexp)
7978                                 (symbol-value regexp)
7979                               regexp)))
7980         (let ((fun (nth 3 entry))
7981               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7982                              (get-char-property marker 'gnus-button-url))
7983                         (mapcar (lambda (group)
7984                                   (let ((string (match-string group)))
7985                                     (set-text-properties
7986                                      0 (length string) nil string)
7987                                     string))
7988                                 (nthcdr 4 entry)))))
7989
7990           (cond
7991            ((fboundp fun)
7992             (apply fun args))
7993            ((and (boundp fun)
7994                  (fboundp (symbol-value fun)))
7995             (apply (symbol-value fun) args))
7996            (t
7997             (gnus-message 1 "You must define `%S' to use this button"
7998                           (cons fun args)))))))))
7999
8000 (defun gnus-parse-news-url (url)
8001   (let (scheme server port group message-id articles)
8002     (with-temp-buffer
8003       (insert url)
8004       (goto-char (point-min))
8005       (when (looking-at "\\([A-Za-z]+\\):")
8006         (setq scheme (match-string 1))
8007         (goto-char (match-end 0)))
8008       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8009         (setq server (match-string 1))
8010         (setq port (if (stringp (match-string 3))
8011                        (string-to-number (match-string 3))
8012                      (match-string 3)))
8013         (goto-char (match-end 0)))
8014
8015       (cond
8016        ((looking-at "\\(.*@.*\\)")
8017         (setq message-id (match-string 1)))
8018        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8019         (setq group (match-string 1)
8020               articles (split-string (match-string 2) "-")))
8021        ((looking-at "\\([^/]+\\)/?")
8022         (setq group (match-string 1)))
8023        (t
8024         (error "Unknown news URL syntax"))))
8025     (list scheme server port group message-id articles)))
8026
8027 (defun gnus-button-handle-news (url)
8028   "Fetch a news URL."
8029   (destructuring-bind (scheme server port group message-id articles)
8030       (gnus-parse-news-url url)
8031     (cond
8032      (message-id
8033       (with-current-buffer gnus-summary-buffer
8034         (if server
8035             (let ((gnus-refer-article-method
8036                    (nconc (list (list 'nntp server))
8037                           gnus-refer-article-method))
8038                   (nntp-port-number (or port "nntp")))
8039               (gnus-message 7 "Fetching %s with %s"
8040                             message-id gnus-refer-article-method)
8041               (gnus-summary-refer-article message-id))
8042           (gnus-summary-refer-article message-id))))
8043      (group
8044       (gnus-button-fetch-group url)))))
8045
8046 (defun gnus-button-patch (library line)
8047   "Visit an Emacs Lisp library LIBRARY on line LINE."
8048   (interactive)
8049   (let ((file (locate-library (file-name-nondirectory library))))
8050     (unless file
8051       (error "Couldn't find library %s" library))
8052     (find-file file)
8053     (goto-char (point-min))
8054     (forward-line (1- (string-to-number line)))))
8055
8056 (defun gnus-button-handle-man (url)
8057   "Fetch a man page."
8058   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8059   (when (eq gnus-button-man-handler 'woman)
8060     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8061   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8062   (funcall gnus-button-man-handler url))
8063
8064 (defun gnus-button-handle-info-url (url)
8065   "Fetch an info URL."
8066   (setq url (mm-subst-char-in-string ?+ ?\  url))
8067   (cond
8068    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8069     (gnus-info-find-node
8070      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8071                      "Gnus")
8072              ")" (gnus-url-unhex-string (match-string 2 url)))))
8073    ((string-match "([^)\"]+)[^\"]+" url)
8074     (setq url
8075           (gnus-replace-in-string
8076            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8077     (gnus-info-find-node url))
8078    (t (error "Can't parse %s" url))))
8079
8080 (defun gnus-button-handle-info-url-gnome (url)
8081   "Fetch GNOME style info URL."
8082   (setq url (mm-subst-char-in-string ?_ ?\  url))
8083   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8084       (gnus-info-find-node
8085        (concat "("
8086                (gnus-url-unhex-string
8087                  (match-string 1 url))
8088                ")"
8089                (or (gnus-url-unhex-string
8090                     (match-string 2 url))
8091                    "Top")))
8092     (error "Can't parse %s" url)))
8093
8094 (defun gnus-button-handle-info-url-kde (url)
8095   "Fetch KDE style info URL."
8096   (gnus-info-find-node (gnus-url-unhex-string url)))
8097
8098 ;; (info) will autoload info.el
8099 (declare-function Info-menu "info" (menu-item &optional fork))
8100 (declare-function Info-index-next "info" (num))
8101
8102 (defun gnus-button-handle-info-keystrokes (url)
8103   "Call `info' when pushing the corresponding URL button."
8104   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8105   (let (node indx comma)
8106     (if (string-match
8107          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8108                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8109                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8110                  "\\(?:[ \t\n,]*\\)\\)?")
8111          url)
8112         (setq node (match-string 2 url)
8113               indx (match-string 3 url))
8114       (error "Can't parse %s" url))
8115     (info)
8116     (Info-directory)
8117     (Info-menu node)
8118     (when (> (length indx) 0)
8119       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8120                             "\\([ \t\n,]*\\)")
8121                     indx)
8122       (setq comma (match-string 2 indx))
8123       (setq indx  (match-string 1 indx))
8124       (Info-index indx)
8125       (when comma
8126         (dotimes (i (with-temp-buffer
8127                       (insert comma)
8128                       ;; Note: the XEmacs version of `how-many' takes
8129                       ;; no optional argument.
8130                       (goto-char (point-min))
8131                       (how-many ",")))
8132           (Info-index-next 1)))
8133       nil)))
8134
8135 (autoload 'pgg-snarf-keys-region "pgg")
8136 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8137 (declare-function pgg-display-output-buffer "pgg" (start end status))
8138
8139 (defun gnus-button-openpgp (url)
8140   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8141   (with-temp-buffer
8142     (mm-url-insert-file-contents-external url)
8143     (pgg-snarf-keys-region (point-min) (point-max))
8144     (pgg-display-output-buffer nil nil nil)))
8145
8146 (defun gnus-button-message-id (message-id)
8147   "Fetch MESSAGE-ID."
8148   (with-current-buffer gnus-summary-buffer
8149     (gnus-summary-refer-article message-id)))
8150
8151 (defun gnus-button-fetch-group (address &rest ignore)
8152   "Fetch GROUP specified by ADDRESS."
8153   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8154                       address)
8155     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8156     ;; for nntp:// and news://
8157     (setq address (match-string 3 address)))
8158   (if (not (string-match "[:/]" address))
8159       ;; This is just a simple group url.
8160       (gnus-group-read-ephemeral-group address gnus-select-method)
8161     (if (not
8162          (string-match
8163           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8164           address))
8165         (error "Can't parse %s" address)
8166       (gnus-group-read-ephemeral-group
8167        (match-string 4 address)
8168        `(nntp ,(match-string 1 address)
8169               (nntp-address ,(match-string 1 address))
8170               (nntp-port-number ,(if (match-end 3)
8171                                      (match-string 3 address)
8172                                    "nntp")))
8173        nil nil nil
8174        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8175
8176 (defun gnus-url-parse-query-string (query &optional downcase)
8177   (let (retval pairs cur key val)
8178     (setq pairs (split-string query "&"))
8179     (while pairs
8180       (setq cur (car pairs)
8181             pairs (cdr pairs))
8182       (if (not (string-match "=" cur))
8183           nil                           ; Grace
8184         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8185               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8186         (if downcase
8187             (setq key (downcase key)))
8188         (setq cur (assoc key retval))
8189         (if cur
8190             (setcdr cur (cons val (cdr cur)))
8191           (setq retval (cons (list key val) retval)))))
8192     retval))
8193
8194 (defun gnus-url-mailto (url)
8195   ;; Send mail to someone
8196   (setq url (replace-regexp-in-string "\n" " " url))
8197   (when (string-match "mailto:/*\\(.*\\)" url)
8198     (setq url (substring url (match-beginning 1) nil)))
8199   (let (to args subject func)
8200     (setq args (gnus-url-parse-query-string
8201                 (if (string-match "^\\?" url)
8202                     (substring url 1)
8203                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8204                       (concat "to=" (match-string 1 url) "&"
8205                               (match-string 2 url))
8206                     (concat "to=" url))))
8207           subject (cdr-safe (assoc "subject" args)))
8208     (gnus-msg-mail)
8209     (while args
8210       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8211       (if (fboundp func)
8212           (funcall func)
8213         (message-position-on-field (caar args)))
8214       (insert (gnus-replace-in-string
8215                (mapconcat 'identity (reverse (cdar args)) ", ")
8216                "\r\n" "\n" t))
8217       (setq args (cdr args)))
8218     (if subject
8219         (message-goto-body)
8220       (message-goto-subject))))
8221
8222 (defun gnus-button-embedded-url (address)
8223   "Activate ADDRESS with `browse-url'."
8224   (browse-url (gnus-strip-whitespace address)))
8225
8226 ;;; Next/prev buttons in the article buffer.
8227
8228 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8229 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8230
8231 (defvar gnus-prev-page-map
8232   (let ((map (make-sparse-keymap)))
8233     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8234     (define-key map "\r" 'gnus-button-prev-page)
8235     map))
8236
8237 (defvar gnus-next-page-map
8238   (let ((map (make-sparse-keymap)))
8239     (define-key map gnus-mouse-2 'gnus-button-next-page)
8240     (define-key map "\r" 'gnus-button-next-page)
8241     map))
8242
8243 (defun gnus-insert-prev-page-button ()
8244   (let ((b (point)) e
8245         (inhibit-read-only t))
8246     (gnus-eval-format
8247      gnus-prev-page-line-format nil
8248      `(keymap ,gnus-prev-page-map
8249               gnus-prev t
8250               gnus-callback gnus-article-button-prev-page
8251               article-type annotation))
8252     (setq e (if (bolp)
8253                 ;; Exclude a newline.
8254                 (1- (point))
8255               (point)))
8256     (when gnus-article-button-face
8257       (gnus-overlay-put (gnus-make-overlay b e nil t)
8258                         'face gnus-article-button-face))
8259     (widget-convert-button
8260      'link b e
8261      :action 'gnus-button-prev-page
8262      :button-keymap gnus-prev-page-map)))
8263
8264 (defun gnus-button-next-page (&optional args more-args)
8265   "Go to the next page."
8266   (interactive)
8267   (let ((win (selected-window)))
8268     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8269     (gnus-article-next-page)
8270     (select-window win)))
8271
8272 (defun gnus-button-prev-page (&optional args more-args)
8273   "Go to the prev page."
8274   (interactive)
8275   (let ((win (selected-window)))
8276     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8277     (gnus-article-prev-page)
8278     (select-window win)))
8279
8280 (defun gnus-insert-next-page-button ()
8281   (let ((b (point)) e
8282         (inhibit-read-only t))
8283     (gnus-eval-format gnus-next-page-line-format nil
8284                       `(keymap ,gnus-next-page-map
8285                                gnus-next t
8286                                gnus-callback gnus-article-button-next-page
8287                                article-type annotation))
8288     (setq e (if (bolp)
8289                 ;; Exclude a newline.
8290                 (1- (point))
8291               (point)))
8292     (when gnus-article-button-face
8293       (gnus-overlay-put (gnus-make-overlay b e nil t)
8294                         'face gnus-article-button-face))
8295     (widget-convert-button
8296      'link b e
8297      :action 'gnus-button-next-page
8298      :button-keymap gnus-next-page-map)))
8299
8300 (defun gnus-article-button-next-page (arg)
8301   "Go to the next page."
8302   (interactive "P")
8303   (let ((win (selected-window)))
8304     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8305     (gnus-article-next-page)
8306     (select-window win)))
8307
8308 (defun gnus-article-button-prev-page (arg)
8309   "Go to the prev page."
8310   (interactive "P")
8311   (let ((win (selected-window)))
8312     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8313     (gnus-article-prev-page)
8314     (select-window win)))
8315
8316 (defvar gnus-decode-header-methods
8317   '(mail-decode-encoded-word-region)
8318   "List of methods used to decode headers.
8319
8320 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8321 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8322 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8323 whose names match REGEXP.
8324
8325 For example:
8326 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8327  mail-decode-encoded-word-region
8328  (\"chinese\" . rfc1843-decode-region))
8329 ")
8330
8331 (defvar gnus-decode-header-methods-cache nil)
8332
8333 (defun gnus-multi-decode-header (start end)
8334   "Apply the functions from `gnus-encoded-word-methods' that match."
8335   (unless (and gnus-decode-header-methods-cache
8336                (eq gnus-newsgroup-name
8337                    (car gnus-decode-header-methods-cache)))
8338     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8339     (dolist (x gnus-decode-header-methods)
8340       (if (symbolp x)
8341           (nconc gnus-decode-header-methods-cache (list x))
8342         (if (and gnus-newsgroup-name
8343                  (string-match (car x) gnus-newsgroup-name))
8344             (nconc gnus-decode-header-methods-cache
8345                    (list (cdr x)))))))
8346   (let ((xlist gnus-decode-header-methods-cache))
8347     (pop xlist)
8348     (save-restriction
8349       (narrow-to-region start end)
8350       (while xlist
8351         (funcall (pop xlist) (point-min) (point-max))))))
8352
8353 ;;;
8354 ;;; Treatment top-level handling.
8355 ;;;
8356
8357 (defvar gnus-inhibit-article-treatments nil)
8358
8359 (defun gnus-treat-article (gnus-treat-condition
8360                            &optional part-number total-parts gnus-treat-type)
8361   (let ((gnus-treat-length (- (point-max) (point-min)))
8362         (alist gnus-treatment-function-alist)
8363         (article-goto-body-goes-to-point-min-p t)
8364         (treated-type
8365          (or (not gnus-treat-type)
8366              (catch 'found
8367                (let ((list gnus-article-treat-types))
8368                  (while list
8369                    (when (string-match (pop list) gnus-treat-type)
8370                      (throw 'found t)))))))
8371         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8372         val elem)
8373     (gnus-run-hooks 'gnus-part-display-hook)
8374     (dolist (elem alist)
8375       (setq val
8376             (save-excursion
8377               (when (gnus-buffer-live-p gnus-summary-buffer)
8378                 (set-buffer gnus-summary-buffer))
8379               (symbol-value (car elem))))
8380       (when (and (or (consp val)
8381                      treated-type)
8382                  (or (not gnus-inhibit-article-treatments)
8383                      (eq gnus-treat-condition 'head))
8384                  (gnus-treat-predicate val)
8385                  (or (not (get (car elem) 'highlight))
8386                      highlightp))
8387         (save-restriction
8388           (funcall (cadr elem)))))))
8389
8390 ;; Dynamic variables.
8391 (defvar part-number)
8392 (defvar total-parts)
8393 (defvar gnus-treat-type)
8394 (defvar gnus-treat-condition)
8395 (defvar gnus-treat-length)
8396
8397 (defun gnus-treat-predicate (val)
8398   (cond
8399    ((null val)
8400     nil)
8401    (gnus-treat-condition
8402     (eq gnus-treat-condition val))
8403    ((and (listp val)
8404          (stringp (car val)))
8405     (apply 'gnus-or (mapcar `(lambda (s)
8406                                (string-match s ,(or gnus-newsgroup-name "")))
8407                             val)))
8408    ((listp val)
8409     (let ((pred (pop val)))
8410       (cond
8411        ((eq pred 'or)
8412         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8413        ((eq pred 'and)
8414         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8415        ((eq pred 'not)
8416         (not (gnus-treat-predicate (car val))))
8417        ((eq pred 'typep)
8418         (equal (car val) gnus-treat-type))
8419        (t
8420         (error "%S is not a valid predicate" pred)))))
8421    ((eq val t)
8422     t)
8423    ((eq val 'head)
8424     nil)
8425    ((eq val 'first)
8426     (eq part-number 1))
8427    ((eq val 'last)
8428     (eq part-number total-parts))
8429    ((numberp val)
8430     (< gnus-treat-length val))
8431    (t
8432     (error "%S is not a valid value" val))))
8433
8434 (defun gnus-article-encrypt-body (protocol &optional n)
8435   "Encrypt the article body."
8436   (interactive
8437    (list
8438     (or gnus-article-encrypt-protocol
8439         (gnus-completing-read "Encrypt protocol"
8440                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8441                               t))
8442     current-prefix-arg))
8443   ;; User might hit `K E' instead of `K e', so prompt once.
8444   (when (and gnus-article-encrypt-protocol
8445              gnus-novice-user)
8446     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8447       (error "Encrypt aborted")))
8448   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8449     (unless func
8450       (error "Can't find the encrypt protocol %s" protocol))
8451     (if (member gnus-newsgroup-name '("nndraft:delayed"
8452                                       "nndraft:drafts"
8453                                       "nndraft:queue"))
8454         (error "Can't encrypt the article in group %s"
8455                gnus-newsgroup-name))
8456     (gnus-summary-iterate n
8457       (with-current-buffer gnus-summary-buffer
8458         (let ((mail-parse-charset gnus-newsgroup-charset)
8459               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8460               (summary-buffer gnus-summary-buffer)
8461               references point)
8462           (gnus-set-global-variables)
8463           (when (gnus-group-read-only-p)
8464             (error "The current newsgroup does not support article encrypt"))
8465           (gnus-summary-show-article t)
8466           (setq references
8467               (or (mail-header-references gnus-current-headers) ""))
8468           (set-buffer gnus-article-buffer)
8469           (let* ((inhibit-read-only t)
8470                  (headers
8471                   (mapcar (lambda (field)
8472                             (and (save-restriction
8473                                    (message-narrow-to-head)
8474                                    (goto-char (point-min))
8475                                    (search-forward field nil t))
8476                                  (prog2
8477                                      (message-narrow-to-field)
8478                                      (buffer-string)
8479                                    (delete-region (point-min) (point-max))
8480                                    (widen))))
8481                           '("Content-Type:" "Content-Transfer-Encoding:"
8482                             "Content-Disposition:"))))
8483             (message-narrow-to-head)
8484             (message-remove-header "MIME-Version")
8485             (goto-char (point-max))
8486             (setq point (point))
8487             (insert (apply 'concat headers))
8488             (widen)
8489             (narrow-to-region point (point-max))
8490             (let ((message-options message-options))
8491               (message-options-set 'message-sender user-mail-address)
8492               (message-options-set 'message-recipients user-mail-address)
8493               (message-options-set 'message-sign-encrypt 'not)
8494               (funcall func))
8495             (goto-char (point-min))
8496             (insert "MIME-Version: 1.0\n")
8497             (widen)
8498             (gnus-summary-edit-article-done
8499              references nil summary-buffer t))
8500           (when gnus-keep-backlog
8501             (gnus-backlog-remove-article
8502              (car gnus-article-current) (cdr gnus-article-current)))
8503           (gnus-flush-original-article-buffer)
8504           (when gnus-use-cache
8505             (gnus-cache-update-article
8506              (car gnus-article-current) (cdr gnus-article-current))))))))
8507
8508 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8509   "The following specs can be used:
8510 %t  The security MIME type
8511 %i  Additional info
8512 %d  Details
8513 %D  Details if button is pressed")
8514
8515 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8516   "The following specs can be used:
8517 %t  The security MIME type
8518 %i  Additional info
8519 %d  Details
8520 %D  Details if button is pressed")
8521
8522 (defvar gnus-mime-security-button-line-format-alist
8523   '((?t gnus-tmp-type ?s)
8524     (?i gnus-tmp-info ?s)
8525     (?d gnus-tmp-details ?s)
8526     (?D gnus-tmp-pressed-details ?s)))
8527
8528 (defvar gnus-mime-security-button-commands
8529   '((gnus-article-press-button "\r" "Show Detail")
8530     (undefined "v")
8531     (undefined "t")
8532     (undefined "C")
8533     (gnus-mime-security-save-part "o" "Save...")
8534     (undefined "\C-o")
8535     (undefined "r")
8536     (undefined "d")
8537     (undefined "c")
8538     (undefined "i")
8539     (undefined "E")
8540     (undefined "e")
8541     (undefined "p")
8542     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8543     (undefined ".")))
8544
8545 (defvar gnus-mime-security-button-map
8546   (let ((map (make-sparse-keymap)))
8547     (define-key map gnus-mouse-2 'gnus-article-push-button)
8548     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8549     (dolist (c gnus-mime-security-button-commands)
8550       (define-key map (cadr c) (car c)))
8551     map))
8552
8553 (easy-menu-define
8554   gnus-mime-security-button-menu gnus-mime-security-button-map
8555   "Security button menu."
8556   `("Security Part"
8557     ,@(delq nil
8558             (mapcar (lambda (c)
8559                       (unless (eq (car c) 'undefined)
8560                         (vector (caddr c) (car c) :active t)))
8561                     gnus-mime-security-button-commands))))
8562
8563 (defun gnus-mime-security-button-menu (event prefix)
8564   "Construct a context-sensitive menu of security commands."
8565   (interactive "e\nP")
8566   (save-window-excursion
8567     (let ((pos (event-start event)))
8568       (select-window (posn-window pos))
8569       (goto-char (posn-point pos))
8570       (gnus-article-check-buffer)
8571       (popup-menu gnus-mime-security-button-menu nil prefix))))
8572
8573 (defvar gnus-mime-security-details-buffer nil)
8574
8575 (defvar gnus-mime-security-button-pressed nil)
8576
8577 (defvar gnus-mime-security-show-details-inline t
8578   "If non-nil, show details in the article buffer.")
8579
8580 (defun gnus-mime-security-verify-or-decrypt (handle)
8581   (mm-remove-parts (cdr handle))
8582   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8583         point (inhibit-read-only t))
8584     (if region
8585         (goto-char (car region)))
8586     (setq point (point))
8587     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8588       (let* ((mm-verify-option 'known)
8589              (mm-decrypt-option 'known)
8590              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8591         (unless (eq nparts (cdr handle))
8592           (mm-destroy-parts (cdr handle))
8593           (setcdr handle nparts))))
8594     (gnus-mime-display-security handle)
8595     (when region
8596       (delete-region (point) (cdr region))
8597       (set-marker (car region) nil)
8598       (set-marker (cdr region) nil))
8599     (goto-char point)))
8600
8601 (defun gnus-mime-security-show-details (handle)
8602   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8603     (if (not details)
8604         (gnus-message 5 "No details.")
8605       (if gnus-mime-security-show-details-inline
8606           (let ((gnus-mime-security-button-pressed
8607                  (not (get-text-property (point) 'gnus-mime-details)))
8608                 (gnus-mime-security-button-line-format
8609                  (get-text-property (point) 'gnus-line-format))
8610                 (inhibit-read-only t))
8611             (forward-char -1)
8612             (while (eq (get-text-property (point) 'gnus-line-format)
8613                        gnus-mime-security-button-line-format)
8614               (forward-char -1))
8615             (forward-char)
8616             (save-restriction
8617               (narrow-to-region (point) (point))
8618               (gnus-insert-mime-security-button handle))
8619             (delete-region (point)
8620                            (or (text-property-not-all
8621                                 (point) (point-max)
8622                                 'gnus-line-format
8623                                 gnus-mime-security-button-line-format)
8624                                (point-max))))
8625         ;; Not inlined.
8626         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8627             (with-current-buffer gnus-mime-security-details-buffer
8628               (erase-buffer)
8629               t)
8630           (setq gnus-mime-security-details-buffer
8631                 (gnus-get-buffer-create "*MIME Security Details*")))
8632         (with-current-buffer gnus-mime-security-details-buffer
8633           (insert details)
8634           (goto-char (point-min)))
8635         (pop-to-buffer gnus-mime-security-details-buffer)))))
8636
8637 (defun gnus-mime-security-press-button (handle)
8638   (save-excursion
8639     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8640         (gnus-mime-security-show-details handle)
8641       (gnus-mime-security-verify-or-decrypt handle))))
8642
8643 (defun gnus-insert-mime-security-button (handle &optional displayed)
8644   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8645          (gnus-tmp-type
8646           (concat
8647            (or (nth 2 (assoc protocol mm-verify-function-alist))
8648                (nth 2 (assoc protocol mm-decrypt-function-alist))
8649                "Unknown")
8650            (if (equal (car handle) "multipart/signed")
8651                " Signed" " Encrypted")
8652            " Part"))
8653          (gnus-tmp-info
8654           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8655               "Undecided"))
8656          (gnus-tmp-details
8657           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8658          gnus-tmp-pressed-details
8659          b e)
8660     (setq gnus-tmp-details
8661           (if gnus-tmp-details
8662               (concat "\n" gnus-tmp-details)
8663             ""))
8664     (setq gnus-tmp-pressed-details
8665           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8666     (unless (bolp)
8667       (insert "\n"))
8668     (setq b (point))
8669     (gnus-eval-format
8670      gnus-mime-security-button-line-format
8671      gnus-mime-security-button-line-format-alist
8672      `(keymap ,gnus-mime-security-button-map
8673          gnus-callback gnus-mime-security-press-button
8674          gnus-line-format ,gnus-mime-security-button-line-format
8675          gnus-mime-details ,gnus-mime-security-button-pressed
8676          article-type annotation
8677          gnus-data ,handle))
8678     (setq e (if (bolp)
8679                 ;; Exclude a newline.
8680                 (1- (point))
8681               (point)))
8682     (when gnus-article-button-face
8683       (gnus-overlay-put (gnus-make-overlay b e nil t)
8684                         'face gnus-article-button-face))
8685     (widget-convert-button
8686      'link b e
8687      :mime-handle handle
8688      :action 'gnus-widget-press-button
8689      :button-keymap gnus-mime-security-button-map
8690      :help-echo
8691      (lambda (widget)
8692        ;; Needed to properly clear the message due to a bug in
8693        ;; wid-edit (XEmacs only).
8694        (when (boundp 'help-echo-owns-message)
8695          (setq help-echo-owns-message t))
8696        (format
8697         "%S: show detail; %S: more options"
8698         (aref gnus-mouse-2 0)
8699         (aref gnus-down-mouse-3 0))))))
8700
8701 (defun gnus-mime-display-security (handle)
8702   (save-restriction
8703     (narrow-to-region (point) (point))
8704     (unless (gnus-unbuttonized-mime-type-p (car handle))
8705       (gnus-insert-mime-security-button handle))
8706     (gnus-mime-display-part (cadr handle))
8707     (unless (bolp)
8708       (insert "\n"))
8709     (unless (gnus-unbuttonized-mime-type-p (car handle))
8710       (let ((gnus-mime-security-button-line-format
8711              gnus-mime-security-button-end-line-format))
8712         (gnus-insert-mime-security-button handle)))
8713     (mm-set-handle-multipart-parameter
8714      handle 'gnus-region
8715      (cons (set-marker (make-marker) (point-min))
8716            (set-marker (make-marker) (point-max))))
8717     (goto-char (point-max))))
8718
8719 (defun gnus-mime-security-run-function (function)
8720   "Run FUNCTION with the security part under point."
8721   (gnus-article-check-buffer)
8722   (let ((data (get-text-property (point) 'gnus-data))
8723         buffer handle)
8724     (when (and (stringp (car-safe data))
8725                (setq buffer (mm-handle-multipart-original-buffer data))
8726                (setq handle (cadr data)))
8727       (if (bufferp (mm-handle-buffer handle))
8728           (progn
8729             (setq handle (cons buffer (copy-sequence (cdr handle))))
8730             (mm-handle-set-undisplayer handle nil))
8731         (setq handle (mm-make-handle
8732                       buffer
8733                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8734                       nil nil nil nil nil nil)))
8735       (funcall function handle))))
8736
8737 (defun gnus-mime-security-save-part ()
8738   "Save the security part under point."
8739   (interactive)
8740   (gnus-mime-security-run-function 'mm-save-part))
8741
8742 (defun gnus-mime-security-pipe-part ()
8743   "Pipe the security part under point to a process."
8744   (interactive)
8745   (gnus-mime-security-run-function 'mm-pipe-part))
8746
8747 (gnus-ems-redefine)
8748
8749 (provide 'gnus-art)
8750
8751 (run-hooks 'gnus-art-load-hook)
8752
8753 ;;; gnus-art.el ends here