* gnus-art.el (gnus-ignored-headers): Removed obsolete and non-working use of :custom...
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2011 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 '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
272     (if (gnus-image-type-available-p 'pbm)
273         'gnus-display-x-face-in-from
274       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
275 display -"))
276   "*String or function to be executed to display an X-Face header.
277 If it is a string, the command will be executed in a sub-shell
278 asynchronously.  The compressed face will be piped to this command."
279   :type `(choice string
280                  (function-item gnus-display-x-face-in-from)
281                  function)
282   :version "21.1"
283   :group 'gnus-picon
284   :group 'gnus-article-washing)
285
286 (defcustom gnus-article-x-face-too-ugly nil
287   "Regexp matching posters whose face shouldn't be shown automatically."
288   :type '(choice regexp (const nil))
289   :group 'gnus-article-washing)
290
291 (defcustom gnus-article-banner-alist nil
292   "Banner alist for stripping.
293 For example,
294      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
295   :version "21.1"
296   :type '(repeat (cons symbol regexp))
297   :group 'gnus-article-washing)
298
299 (gnus-define-group-parameter
300  banner
301  :variable-document
302  "Alist of regexps (to match group names) and banner."
303  :variable-group gnus-article-washing
304  :parameter-type
305  '(choice :tag "Banner"
306           :value nil
307           (const :tag "Remove signature" signature)
308           (symbol :tag "Item in `gnus-article-banner-alist'" none)
309           regexp
310           (const :tag "None" nil))
311  :parameter-document
312  "If non-nil, specify how to remove `banners' from articles.
313
314 Symbol `signature' means to remove signatures delimited by
315 `gnus-signature-separator'.  Any other symbol is used to look up a
316 regular expression to match the banner in `gnus-article-banner-alist'.
317 A string is used as a regular expression to match the banner
318 directly.")
319
320 (defcustom gnus-article-address-banner-alist nil
321   "Alist of mail addresses and banners.
322 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
323 to match a mail address in the From: header, BANNER is one of a symbol
324 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
325 If ADDRESS matches author's mail address, it will remove things like
326 advertisements.  For example:
327
328 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
329 "
330   :type '(repeat
331           (cons
332            (regexp :tag "Address")
333            (choice :tag "Banner" :value nil
334                    (const :tag "Remove signature" signature)
335                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
336                    regexp
337                    (const :tag "None" nil))))
338   :version "22.1"
339   :group 'gnus-article-washing)
340
341 (defmacro gnus-emphasis-custom-with-format (&rest body)
342   `(let ((format "\
343 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
344 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
345      ,@body))
346
347 (defun gnus-emphasis-custom-value-to-external (value)
348   (gnus-emphasis-custom-with-format
349    (if (consp (car value))
350        (list (format format (car (car value)) (cdr (car value)))
351              2
352              (if (nth 1 value) 2 3)
353              (nth 2 value))
354      value)))
355
356 (defun gnus-emphasis-custom-value-to-internal (value)
357   (gnus-emphasis-custom-with-format
358    (let ((regexp (concat "\\`"
359                          (format (regexp-quote format)
360                                  "\\([^()]+\\)" "\\([^()]+\\)")
361                          "\\'"))
362          pattern)
363      (if (string-match regexp (setq pattern (car value)))
364          (list (cons (match-string 1 pattern) (match-string 2 pattern))
365                (= (nth 2 value) 2)
366                (nth 3 value))
367        value))))
368
369 (defcustom gnus-emphasis-alist
370   (let ((types
371          '(("\\*" "\\*" bold nil 2)
372            ("_" "_" underline)
373            ("/" "/" italic)
374            ("_/" "/_" underline-italic)
375            ("_\\*" "\\*_" underline-bold)
376            ("\\*/" "/\\*" bold-italic)
377            ("_\\*/" "/\\*_" underline-bold-italic))))
378     (nconc
379      (gnus-emphasis-custom-with-format
380       (mapcar (lambda (spec)
381                 (list (format format (car spec) (cadr spec))
382                       (or (nth 3 spec) 2)
383                       (or (nth 4 spec) 3)
384                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
385               types))
386      '(;; I've never seen anyone use this strikethru convention whereas I've
387        ;; several times seen it triggered by normal text.  --Stef
388        ;; Miles suggests that this form is sometimes used but for italics,
389        ;; so maybe we should map it to `italic'.
390        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
391        ;; 2 3 gnus-emphasis-strikethru)
392        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
393         2 3 gnus-emphasis-underline))))
394   "*Alist that says how to fontify certain phrases.
395 Each item looks like this:
396
397   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
398
399 The first element is a regular expression to be matched.  The second
400 is a number that says what regular expression grouping used to find
401 the entire emphasized word.  The third is a number that says what
402 regexp grouping should be displayed and highlighted.  The fourth
403 is the face used for highlighting."
404   :type
405   '(repeat
406     (menu-choice
407      :format "%[Customizing Style%]\n%v"
408      :indent 2
409      (group :tag "Default"
410             :value ("" 0 0 default)
411             :value-create
412             (lambda (widget)
413               (let ((value (widget-get
414                             (cadr (widget-get (widget-get widget :parent)
415                                               :args))
416                             :value)))
417                 (if (not (eq (nth 2 value) 'default))
418                     (widget-put
419                      widget
420                      :value
421                      (gnus-emphasis-custom-value-to-external value))))
422               (widget-group-value-create widget))
423             regexp
424             (integer :format "Match group: %v")
425             (integer :format "Emphasize group: %v")
426             face)
427      (group :tag "Simple"
428             :value (("_" . "_") nil default)
429             (cons :format "%v"
430                   (regexp :format "Start regexp: %v")
431                   (regexp :format "End regexp: %v"))
432             (boolean :format "Show start and end patterns: %[%v%]\n"
433                      :on " On " :off " Off ")
434             face)))
435   :get (lambda (symbol)
436          (mapcar 'gnus-emphasis-custom-value-to-internal
437                  (default-value symbol)))
438   :set (lambda (symbol value)
439          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
440                                      value)))
441   :group 'gnus-article-emphasis)
442
443 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
444   "A regexp to describe whitespace which should not be emphasized.
445 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
446 The former avoids underlining of leading and trailing whitespace,
447 and the latter avoids underlining any whitespace at all."
448   :version "21.1"
449   :group 'gnus-article-emphasis
450   :type 'regexp)
451
452 (defface gnus-emphasis-bold '((t (:bold t)))
453   "Face used for displaying strong emphasized text (*word*)."
454   :group 'gnus-article-emphasis)
455
456 (defface gnus-emphasis-italic '((t (:italic t)))
457   "Face used for displaying italic emphasized text (/word/)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-underline '((t (:underline t)))
461   "Face used for displaying underlined emphasized text (_word_)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
465   "Face used for displaying underlined bold emphasized text (_*word*_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
469   "Face used for displaying underlined italic emphasized text (_/word/_)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
473   "Face used for displaying bold italic emphasized text (/*word*/)."
474   :group 'gnus-article-emphasis)
475
476 (defface gnus-emphasis-underline-bold-italic
477   '((t (:bold t :italic t :underline t)))
478   "Face used for displaying underlined bold italic emphasized text.
479 Example: (_/*word*/_)."
480   :group 'gnus-article-emphasis)
481
482 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
483                                       '((t (:strikethru t)))
484                                     '((t (:strike-through t))))
485   "Face used for displaying strike-through text (-word-)."
486   :group 'gnus-article-emphasis)
487
488 (defface gnus-emphasis-highlight-words
489   '((t (:background "black" :foreground "yellow")))
490   "Face used for displaying highlighted words."
491   :group 'gnus-article-emphasis)
492
493 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
494   "Format for display of Date headers in article bodies.
495 See `format-time-string' for the possible values.
496
497 The variable can also be function, which should return a complete Date
498 header.  The function is called with one argument, the time, which can
499 be fed to `format-time-string'."
500   :type '(choice string function)
501   :link '(custom-manual "(gnus)Article Date")
502   :group 'gnus-article-washing)
503
504 (defcustom gnus-save-all-headers t
505   "*If non-nil, don't remove any headers before saving.
506 This will be overridden by the `:headers' property that the symbol of
507 the saver function, which is specified by `gnus-default-article-saver',
508 might have."
509   :group 'gnus-article-saving
510   :type 'boolean)
511
512 (defcustom gnus-prompt-before-saving 'always
513   "*This variable says how much prompting is to be done when saving articles.
514 If it is nil, no prompting will be done, and the articles will be
515 saved to the default files.  If this variable is `always', each and
516 every article that is saved will be preceded by a prompt, even when
517 saving large batches of articles.  If this variable is neither nil not
518 `always', there the user will be prompted once for a file name for
519 each invocation of the saving commands."
520   :group 'gnus-article-saving
521   :type '(choice (item always)
522                  (item :tag "never" nil)
523                  (sexp :tag "once" :format "%t\n" :value t)))
524
525 (defcustom gnus-saved-headers gnus-visible-headers
526   "Headers to keep if `gnus-save-all-headers' is nil.
527 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
528 If that variable is nil, however, all headers that match this regexp
529 will be kept while the rest will be deleted before saving.  This and
530 `gnus-save-all-headers' will be overridden by the `:headers' property
531 that the symbol of the saver function, which is specified by
532 `gnus-default-article-saver', might have."
533   :group 'gnus-article-saving
534   :type 'regexp)
535
536 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
537 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
538   "A function to save articles in your favourite format.
539 The function will be called by way of the `gnus-summary-save-article'
540 command, and friends such as `gnus-summary-save-article-rmail'.
541
542 Gnus provides the following functions:
543
544 * gnus-summary-save-in-rmail (Rmail format)
545 * gnus-summary-save-in-mail (Unix mail format)
546 * gnus-summary-save-in-folder (MH folder)
547 * gnus-summary-save-in-file (article format)
548 * gnus-summary-save-body-in-file (article body)
549 * gnus-summary-save-in-vm (use VM's folder format)
550 * gnus-summary-write-to-file (article format -- overwrite)
551 * gnus-summary-write-body-to-file (article body -- overwrite)
552 * gnus-summary-save-in-pipe (article format)
553
554 The symbol of each function may have the following properties:
555
556 * :decode
557 The value non-nil means save decoded articles.  This is meaningful
558 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
559 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
560 `gnus-summary-save-in-pipe'.
561
562 * :function
563 The value specifies an alternative function which appends, not
564 overwrites, articles to a file.  This implies that when saving many
565 articles at a time, `gnus-prompt-before-saving' is bound to t and all
566 articles are saved in a single file.  This is meaningful only with
567 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
568
569 * :headers
570 The value specifies the symbol of a variable of which the value
571 specifies headers to be saved.  If it is omitted,
572 `gnus-save-all-headers' and `gnus-saved-headers' control what
573 headers should be saved."
574   :group 'gnus-article-saving
575   :type '(radio (function-item gnus-summary-save-in-rmail)
576                 (function-item gnus-summary-save-in-mail)
577                 (function-item gnus-summary-save-in-folder)
578                 (function-item gnus-summary-save-in-file)
579                 (function-item gnus-summary-save-body-in-file)
580                 (function-item gnus-summary-save-in-vm)
581                 (function-item gnus-summary-write-to-file)
582                 (function-item gnus-summary-write-body-to-file)
583                 (function-item gnus-summary-save-in-pipe)
584                 (function)))
585
586 (defcustom gnus-article-save-coding-system
587   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
588       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
589       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
590       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
591   "Coding system used to save decoded articles to a file.
592
593 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
594 which can safely encode any characters in text.  This is used by the
595 commands including:
596
597 * gnus-summary-save-article-file
598 * gnus-summary-save-article-body-file
599 * gnus-summary-write-article-file
600 * gnus-summary-write-article-body-file
601
602 and the functions to which you may set `gnus-default-article-saver':
603
604 * gnus-summary-save-in-file
605 * gnus-summary-save-body-in-file
606 * gnus-summary-write-to-file
607 * gnus-summary-write-body-to-file
608
609 Those commands and functions save just text displayed in the article
610 buffer to a file if the value of this variable is non-nil.  Note that
611 buttonized MIME parts will be lost in a saved file in that case.
612 Otherwise, raw articles will be saved."
613   :group 'gnus-article-saving
614   :type `(choice
615           :format "%{%t%}:\n %[Value Menu%] %v"
616           (const :tag "Save raw articles" nil)
617           ,@(delq nil
618                   (mapcar
619                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
620                    '((const :tag "UTF-8" utf-8)
621                      (const :tag "iso-2022-7bit" iso-2022-7bit)
622                      (const :tag "Emacs internal" emacs-mule)
623                      (const :tag "escape-quoted" escape-quoted))))
624           (symbol :tag "Coding system")))
625
626 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
627   "A function generating a file name to save articles in Rmail format.
628 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
629   :group 'gnus-article-saving
630   :type 'function)
631
632 (defcustom gnus-mail-save-name 'gnus-plain-save-name
633   "A function generating a file name to save articles in Unix mail format.
634 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
635   :group 'gnus-article-saving
636   :type 'function)
637
638 (defcustom gnus-folder-save-name 'gnus-folder-save-name
639   "A function generating a file name to save articles in MH folder.
640 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
641   :group 'gnus-article-saving
642   :type 'function)
643
644 (defcustom gnus-file-save-name 'gnus-numeric-save-name
645   "A function generating a file name to save articles in article format.
646 The function is called with NEWSGROUP, HEADERS, and optional
647 LAST-FILE."
648   :group 'gnus-article-saving
649   :type 'function)
650
651 (defcustom gnus-split-methods
652   '((gnus-article-archive-name)
653     (gnus-article-nndoc-name))
654   "*Variable used to suggest where articles are to be saved.
655 For instance, if you would like to save articles related to Gnus in
656 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
657 you could set this variable to something like:
658
659  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
660    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
661
662 This variable is an alist where the key is the match and the
663 value is a list of possible files to save in if the match is
664 non-nil.
665
666 If the match is a string, it is used as a regexp match on the
667 article.  If the match is a symbol, that symbol will be funcalled
668 from the buffer of the article to be saved with the newsgroup as the
669 parameter.  If it is a list, it will be evaled in the same buffer.
670
671 If this form or function returns a string, this string will be used as a
672 possible file name; and if it returns a non-nil list, that list will be
673 used as possible file names."
674   :group 'gnus-article-saving
675   :type '(repeat (choice (list :value (fun) function)
676                          (cons :value ("" "") regexp (repeat string))
677                          (sexp :value nil))))
678
679 (defcustom gnus-page-delimiter "^\^L"
680   "*Regexp describing what to use as article page delimiters.
681 The default value is \"^\^L\", which is a form linefeed at the
682 beginning of a line."
683   :type 'regexp
684   :group 'gnus-article-various)
685
686 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
687   "*The format specification for the article mode line.
688 See `gnus-summary-mode-line-format' for a closer description.
689
690 The following additional specs are available:
691
692 %w  The article washing status.
693 %m  The number of MIME parts in the article."
694   :version "24.1"
695   :type 'string
696   :group 'gnus-article-various)
697
698 (defcustom gnus-article-mode-hook nil
699   "*A hook for Gnus article mode."
700   :type 'hook
701   :group 'gnus-article-various)
702
703 (when (featurep 'xemacs)
704   ;; Extracted from gnus-xmas-define in order to preserve user settings
705   (when (fboundp 'turn-off-scroll-in-place)
706     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
707   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
708   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
709
710 (defcustom gnus-article-menu-hook nil
711   "*Hook run after the creation of the article mode menu."
712   :type 'hook
713   :group 'gnus-article-various)
714
715 (defcustom gnus-article-prepare-hook nil
716   "*A hook called after an article has been prepared in the article buffer."
717   :type 'hook
718   :group 'gnus-article-various)
719
720 (defcustom gnus-copy-article-ignored-headers nil
721   "List of headers to be removed when copying an article.
722 Each element is a regular expression."
723   :version "23.1" ;; No Gnus
724   :type '(repeat regexp)
725   :group 'gnus-article-various)
726
727 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
728                         "Gnus 5.10 (Emacs 22.1)")
729
730 (defface gnus-button
731   '((t (:weight bold)))
732   "Face used for highlighting a button in the article buffer."
733   :group 'gnus-article-buttons)
734
735 (defcustom gnus-article-button-face 'gnus-button
736   "Face used for highlighting buttons in the article buffer.
737
738 An article button is a piece of text that you can activate by pressing
739 `RET' or `mouse-2' above it."
740   :type 'face
741   :group 'gnus-article-buttons)
742
743 (defcustom gnus-article-mouse-face 'highlight
744   "Face used for mouse highlighting in the article buffer.
745
746 Article buttons will be displayed in this face when the cursor is
747 above them."
748   :type 'face
749   :group 'gnus-article-buttons)
750
751 (defcustom gnus-signature-face 'gnus-signature
752   "Face used for highlighting a signature in the article buffer.
753 Obsolete; use the face `gnus-signature' for customizations instead."
754   :type 'face
755   :group 'gnus-article-highlight
756   :group 'gnus-article-signature)
757
758 (defface gnus-signature
759   '((t
760      (:italic t)))
761   "Face used for highlighting a signature in the article buffer."
762   :group 'gnus-article-highlight
763   :group 'gnus-article-signature)
764 ;; backward-compatibility alias
765 (put 'gnus-signature-face 'face-alias 'gnus-signature)
766 (put 'gnus-signature-face 'obsolete-face "22.1")
767
768 (defface gnus-header-from
769   '((((class color)
770       (background dark))
771      (:foreground "PaleGreen1"))
772     (((class color)
773       (background light))
774      (:foreground "red3"))
775     (t
776      (:italic t)))
777   "Face used for displaying from headers."
778   :group 'gnus-article-headers
779   :group 'gnus-article-highlight)
780 ;; backward-compatibility alias
781 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
782 (put 'gnus-header-from-face 'obsolete-face "22.1")
783
784 (defface gnus-header-subject
785   '((((class color)
786       (background dark))
787      (:foreground "SeaGreen1"))
788     (((class color)
789       (background light))
790      (:foreground "red4"))
791     (t
792      (:bold t :italic t)))
793   "Face used for displaying subject headers."
794   :group 'gnus-article-headers
795   :group 'gnus-article-highlight)
796 ;; backward-compatibility alias
797 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
798 (put 'gnus-header-subject-face 'obsolete-face "22.1")
799
800 (defface gnus-header-newsgroups
801   '((((class color)
802       (background dark))
803      (:foreground "yellow" :italic t))
804     (((class color)
805       (background light))
806      (:foreground "MidnightBlue" :italic t))
807     (t
808      (:italic t)))
809   "Face used for displaying newsgroups headers.
810 In the default setup this face is only used for crossposted
811 articles."
812   :group 'gnus-article-headers
813   :group 'gnus-article-highlight)
814 ;; backward-compatibility alias
815 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
816 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
817
818 (defface gnus-header-name
819   '((((class color)
820       (background dark))
821      (:foreground "SpringGreen2"))
822     (((class color)
823       (background light))
824      (:foreground "maroon"))
825     (t
826      (:bold t)))
827   "Face used for displaying header names."
828   :group 'gnus-article-headers
829   :group 'gnus-article-highlight)
830 ;; backward-compatibility alias
831 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
832 (put 'gnus-header-name-face 'obsolete-face "22.1")
833
834 (defface gnus-header-content
835   '((((class color)
836       (background dark))
837      (:foreground "SpringGreen1" :italic t))
838     (((class color)
839       (background light))
840      (:foreground "indianred4" :italic t))
841     (t
842      (:italic t)))  "Face used for displaying header content."
843   :group 'gnus-article-headers
844   :group 'gnus-article-highlight)
845 ;; backward-compatibility alias
846 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
847 (put 'gnus-header-content-face 'obsolete-face "22.1")
848
849 (defcustom gnus-header-face-alist
850   '(("From" nil gnus-header-from)
851     ("Subject" nil gnus-header-subject)
852     ("Newsgroups:.*," nil gnus-header-newsgroups)
853     ("" gnus-header-name gnus-header-content))
854   "*Controls highlighting of article headers.
855
856 An alist of the form (HEADER NAME CONTENT).
857
858 HEADER is a regular expression which should match the name of a
859 header and NAME and CONTENT are either face names or nil.
860
861 The name of each header field will be displayed using the face
862 specified by the first element in the list where HEADER matches
863 the header name and NAME is non-nil.  Similarly, the content will
864 be displayed by the first non-nil matching CONTENT face."
865   :group 'gnus-article-headers
866   :group 'gnus-article-highlight
867   :type '(repeat (list (regexp :tag "Header")
868                        (choice :tag "Name"
869                                (item :tag "skip" nil)
870                                (face :value default))
871                        (choice :tag "Content"
872                                (item :tag "skip" nil)
873                                (face :value default)))))
874
875 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
876                                           '((xface . (:face gnus-x-face)))
877                                         '((pbm . (:face gnus-x-face))
878                                           (png . nil)))
879   "Alist of image types and properties applied to Face and X-Face images.
880 Here are examples:
881
882 ;; Specify the altitude of Face images in the From header.
883 \(setq gnus-face-properties-alist
884       '((pbm . (:face gnus-x-face :ascent 80))
885         (png . (:ascent 80))))
886
887 ;; Show Face images as pressed buttons.
888 \(setq gnus-face-properties-alist
889       '((pbm . (:face gnus-x-face :relief -2))
890         (png . (:relief -2))))
891
892 See the manual for the valid properties for various image types.
893 Currently, `pbm' is used for X-Face images and `png' is used for Face
894 images in Emacs.  Only the `:face' property is effective on the `xface'
895 image type in XEmacs if it is built with the libcompface library."
896   :version "23.1" ;; No Gnus
897   :group 'gnus-article-headers
898   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
899
900 (defcustom gnus-article-decode-hook
901   '(article-decode-charset article-decode-encoded-words
902                            article-decode-group-name article-decode-idna-rhs)
903   "*Hook run to decode charsets in articles."
904   :group 'gnus-article-headers
905   :type 'hook)
906
907 (defcustom gnus-display-mime-function 'gnus-display-mime
908   "Function to display MIME articles."
909   :group 'gnus-article-mime
910   :type 'function)
911
912 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
913   "Function used to decode headers.")
914
915 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
916   "Function used to decode addresses.")
917
918 (defvar gnus-article-dumbquotes-map
919   '((?\200 "EUR")
920     (?\202 ",")
921     (?\203 "f")
922     (?\204 ",,")
923     (?\205 "...")
924     (?\213 "<")
925     (?\214 "OE")
926     (?\221 "`")
927     (?\222 "'")
928     (?\223 "``")
929     (?\224 "\"")
930     (?\225 "*")
931     (?\226 "-")
932     (?\227 "--")
933     (?\230 "~")
934     (?\231 "(TM)")
935     (?\233 ">")
936     (?\234 "oe")
937     (?\264 "'"))
938   "Table for MS-to-Latin1 translation.")
939
940 (defcustom gnus-ignored-mime-types nil
941   "List of MIME types that should be ignored by Gnus."
942   :version "21.1"
943   :group 'gnus-article-mime
944   :type '(repeat regexp))
945
946 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
947   "List of MIME types that should not be given buttons when rendered inline.
948 See also `gnus-buttonized-mime-types' which may override this variable.
949 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
950   :version "21.1"
951   :group 'gnus-article-mime
952   :type '(repeat regexp))
953
954 (defcustom gnus-buttonized-mime-types nil
955   "List of MIME types that should be given buttons when rendered inline.
956 If set, this variable overrides `gnus-unbuttonized-mime-types'.
957 To see e.g. security buttons you could set this to
958 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
959 this list to display radio buttons that allow you to choose one of two
960 media types those mails include.  See also `mm-discouraged-alternatives'.
961 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
962   :version "22.1"
963   :group 'gnus-article-mime
964   :type '(repeat regexp))
965
966 (defcustom gnus-inhibit-mime-unbuttonizing nil
967   "If non-nil, all MIME parts get buttons.
968 When nil (the default value), then some MIME parts do not get buttons,
969 as described by the variables `gnus-buttonized-mime-types' and
970 `gnus-unbuttonized-mime-types'."
971   :version "22.1"
972   :group 'gnus-article-mime
973   :type 'boolean)
974
975 (defcustom gnus-body-boundary-delimiter "_"
976   "String used to delimit header and body.
977 This variable is used by `gnus-article-treat-body-boundary' which can
978 be controlled by `gnus-treat-body-boundary'."
979   :version "22.1"
980   :group 'gnus-article-various
981   :type '(choice (item :tag "None" :value nil)
982                  string))
983
984 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
985                                   "/usr/share/picons")
986   "Defines the location of the faces database.
987 For information on obtaining this database of pretty pictures, please
988 see http://www.cs.indiana.edu/picons/ftp/index.html"
989   :version "22.1"
990   :type '(repeat directory)
991   :link '(url-link :tag "download"
992                    "http://www.cs.indiana.edu/picons/ftp/index.html")
993   :link '(custom-manual "(gnus)Picons")
994   :group 'gnus-picon)
995
996 (defun gnus-picons-installed-p ()
997   "Say whether picons are installed on your machine."
998   (let ((installed nil))
999     (dolist (database gnus-picon-databases)
1000       (when (file-exists-p database)
1001         (setq installed t)))
1002     installed))
1003
1004 (defcustom gnus-article-mime-part-function nil
1005   "Function called with a MIME handle as the argument.
1006 This is meant for people who want to do something automatic based
1007 on parts -- for instance, adding Vcard info to a database."
1008   :group 'gnus-article-mime
1009   :type '(choice (const nil)
1010                  function))
1011
1012 (defcustom gnus-mime-multipart-functions nil
1013   "An alist of MIME types to functions to display them."
1014   :version "21.1"
1015   :group 'gnus-article-mime
1016   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1017
1018 (defcustom gnus-article-date-headers '(combined-lapsed)
1019   "A list of Date header formats to display.
1020 Valid formats are `ut' (universal time), `local' (local time
1021 zone), `english' (readable English), `lapsed' (elapsed time),
1022 `combined-lapsed' (both the original date and the elapsed time),
1023 `original' (the original date header), `iso8601' (ISO8601
1024 format), and `user-defined' (a user-defined format defined by the
1025 `gnus-article-time-format' variable).
1026
1027 You have as many date headers as you want in the article buffer.
1028 Some of these headers are updated automatically.  See
1029 `gnus-article-update-date-headers' for details."
1030   :version "24.1"
1031   :group 'gnus-article-headers
1032   :type '(repeat
1033           (item :tag "Universal time (UT)" :value 'ut)
1034           (item :tag "Local time zone" :value 'local)
1035           (item :tag "Readable English" :value 'english)
1036           (item :tag "Elapsed time" :value 'lapsed)
1037           (item :tag "Original and elapsed time" :value 'combined-lapsed)
1038           (item :tag "Original date header" :value 'original)
1039           (item :tag "ISO8601 format" :value 'iso8601)
1040           (item :tag "User-defined" :value 'user-defined)))
1041
1042 (defcustom gnus-article-update-date-headers 1
1043   "A number that says how often to update the date header (in seconds).
1044 If nil, don't update it at all."
1045   :version "24.1"
1046   :group 'gnus-article-headers
1047   :type '(choice
1048           (item :tag "Don't update" :value nil)
1049           integer))
1050
1051 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1052   "Function called with a MIME handle as the argument.
1053 This is meant for people who want to view first matched part.
1054 For `undisplayed-alternative' (default), the first undisplayed
1055 part or alternative part is used.  For `undisplayed', the first
1056 undisplayed part is used.  For a function, the first part which
1057 the function return t is used.  For nil, the first part is
1058 used."
1059   :version "21.1"
1060   :group 'gnus-article-mime
1061   :type '(choice
1062           (item :tag "first" :value nil)
1063           (item :tag "undisplayed" :value undisplayed)
1064           (item :tag "undisplayed or alternative"
1065                 :value undisplayed-alternative)
1066           (function)))
1067
1068 (defcustom gnus-mime-action-alist
1069   '(("save to file" . gnus-mime-save-part)
1070     ("save and strip" . gnus-mime-save-part-and-strip)
1071     ("replace with file" . gnus-mime-replace-part)
1072     ("delete part" . gnus-mime-delete-part)
1073     ("display as text" . gnus-mime-inline-part)
1074     ("view the part" . gnus-mime-view-part)
1075     ("pipe to command" . gnus-mime-pipe-part)
1076     ("toggle display" . gnus-article-press-button)
1077     ("toggle display" . gnus-article-view-part-as-charset)
1078     ("view as type" . gnus-mime-view-part-as-type)
1079     ("view internally" . gnus-mime-view-part-internally)
1080     ("view externally" . gnus-mime-view-part-externally))
1081   "An alist of actions that run on the MIME attachment."
1082   :group 'gnus-article-mime
1083   :type '(repeat (cons (string :tag "name")
1084                        (function))))
1085
1086 (defcustom gnus-auto-select-part 1
1087   "Advance to next MIME part when deleting or stripping parts.
1088
1089 When 0, point will be placed on the same part as before.  When
1090 positive (negative), move point forward (backwards) this many
1091 parts.  When nil, redisplay article."
1092   :version "23.1" ;; No Gnus
1093   :group 'gnus-article-mime
1094   :type '(choice (const nil :tag "Redisplay article.")
1095                  (const 1 :tag "Next part.")
1096                  (const 0 :tag "Current part.")
1097                  integer))
1098
1099 ;;;
1100 ;;; The treatment variables
1101 ;;;
1102
1103 (defvar gnus-part-display-hook nil
1104   "Hook called on parts that are to receive treatment.")
1105
1106 (defvar gnus-article-treat-custom
1107   '(choice (const :tag "Off" nil)
1108            (const :tag "On" t)
1109            (const :tag "Header" head)
1110            (const :tag "First" first)
1111            (const :tag "Last" last)
1112            (integer :tag "Less")
1113            (repeat :tag "Groups" regexp)
1114            (sexp :tag "Predicate")))
1115
1116 (defvar gnus-article-treat-head-custom
1117   '(choice (const :tag "Off" nil)
1118            (const :tag "Header" head)))
1119
1120 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1121                                    "text/x-patch")
1122   "Parts to treat.")
1123
1124 (defvar gnus-inhibit-treatment nil
1125   "Whether to inhibit treatment.")
1126
1127 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1128   "Highlight the signature.
1129 Valid values are nil, t, `head', `first', `last', an integer or a
1130 predicate.  See Info node `(gnus)Customizing Articles'."
1131   :group 'gnus-article-treat
1132   :link '(custom-manual "(gnus)Customizing Articles")
1133   :type gnus-article-treat-custom)
1134 (put 'gnus-treat-highlight-signature 'highlight t)
1135
1136 (defcustom gnus-treat-buttonize 100000
1137   "Add buttons.
1138 Valid values are nil, t, `head', `first', `last', an integer or a
1139 predicate.  See Info node `(gnus)Customizing Articles'."
1140   :group 'gnus-article-treat
1141   :link '(custom-manual "(gnus)Customizing Articles")
1142   :type gnus-article-treat-custom)
1143 (put 'gnus-treat-buttonize 'highlight t)
1144
1145 (defcustom gnus-treat-buttonize-head 'head
1146   "Add buttons to the head.
1147 Valid values are nil, t, `head', `first', `last', an integer or a
1148 predicate.  See Info node `(gnus)Customizing Articles'."
1149   :group 'gnus-article-treat
1150   :link '(custom-manual "(gnus)Customizing Articles")
1151   :type gnus-article-treat-head-custom)
1152 (put 'gnus-treat-buttonize-head 'highlight t)
1153
1154 (defcustom gnus-treat-date 'head
1155   "Display dates according to the `gnus-article-date-headers' variable.
1156 Valid values are nil, t, `head', `first', `last', an integer or a
1157 predicate.  See Info node `(gnus)Customizing Articles'."
1158   :version "24.1"
1159   :group 'gnus-article-treat
1160   :link '(custom-manual "(gnus)Customizing Articles")
1161   :type gnus-article-treat-head-custom)
1162
1163 (defcustom gnus-treat-emphasize 50000
1164   "Emphasize text.
1165 Valid values are nil, t, `head', `first', `last', an integer or a
1166 predicate.  See Info node `(gnus)Customizing Articles'."
1167   :group 'gnus-article-treat
1168   :link '(custom-manual "(gnus)Customizing Articles")
1169   :type gnus-article-treat-custom)
1170 (put 'gnus-treat-emphasize 'highlight t)
1171
1172 (defcustom gnus-treat-strip-cr nil
1173   "Remove carriage returns.
1174 Valid values are nil, t, `head', `first', `last', an integer or a
1175 predicate.  See Info node `(gnus)Customizing Articles'."
1176   :version "22.1"
1177   :group 'gnus-article-treat
1178   :link '(custom-manual "(gnus)Customizing Articles")
1179   :type gnus-article-treat-custom)
1180
1181 (defcustom gnus-treat-unsplit-urls nil
1182   "Remove newlines from within URLs.
1183 Valid values are nil, t, `head', `first', `last', an integer or a
1184 predicate.  See Info node `(gnus)Customizing Articles'."
1185   :version "22.1"
1186   :group 'gnus-article-treat
1187   :link '(custom-manual "(gnus)Customizing Articles")
1188   :type gnus-article-treat-custom)
1189
1190 (defcustom gnus-treat-leading-whitespace nil
1191   "Remove leading whitespace in headers.
1192 Valid values are nil, t, `head', `first', `last', an integer or a
1193 predicate.  See Info node `(gnus)Customizing Articles'."
1194   :version "22.1"
1195   :group 'gnus-article-treat
1196   :link '(custom-manual "(gnus)Customizing Articles")
1197   :type gnus-article-treat-custom)
1198
1199 (defcustom gnus-treat-hide-headers 'head
1200   "Hide headers.
1201 Valid values are nil, t, `head', `first', `last', an integer or a
1202 predicate.  See Info node `(gnus)Customizing Articles'."
1203   :group 'gnus-article-treat
1204   :link '(custom-manual "(gnus)Customizing Articles")
1205   :type gnus-article-treat-head-custom)
1206
1207 (defcustom gnus-treat-hide-boring-headers nil
1208   "Hide boring headers.
1209 Valid values are nil, t, `head', `first', `last', an integer or a
1210 predicate.  See Info node `(gnus)Customizing Articles'."
1211   :group 'gnus-article-treat
1212   :link '(custom-manual "(gnus)Customizing Articles")
1213   :type gnus-article-treat-head-custom)
1214
1215 (defcustom gnus-treat-hide-signature nil
1216   "Hide the signature.
1217 Valid values are nil, t, `head', `first', `last', an integer or a
1218 predicate.  See Info node `(gnus)Customizing Articles'."
1219   :group 'gnus-article-treat
1220   :link '(custom-manual "(gnus)Customizing Articles")
1221   :type gnus-article-treat-custom)
1222
1223 (defcustom gnus-treat-fill-article nil
1224   "Fill the article.
1225 Valid values are nil, t, `head', `first', `last', an integer or a
1226 predicate.  See Info node `(gnus)Customizing Articles'."
1227   :group 'gnus-article-treat
1228   :link '(custom-manual "(gnus)Customizing Articles")
1229   :type gnus-article-treat-custom)
1230
1231 (defcustom gnus-treat-hide-citation nil
1232   "Hide cited text.
1233 Valid values are nil, t, `head', `first', `last', an integer or a
1234 predicate.  See Info node `(gnus)Customizing Articles'."
1235   :group 'gnus-article-treat
1236   :link '(custom-manual "(gnus)Customizing Articles")
1237   :type gnus-article-treat-custom)
1238
1239 (defcustom gnus-treat-hide-citation-maybe nil
1240   "Hide cited text.
1241 Valid values are nil, t, `head', `first', `last', an integer or a
1242 predicate.  See Info node `(gnus)Customizing Articles'."
1243   :group 'gnus-article-treat
1244   :link '(custom-manual "(gnus)Customizing Articles")
1245   :type gnus-article-treat-custom)
1246
1247 (defcustom gnus-treat-strip-list-identifiers 'head
1248   "Strip list identifiers from `gnus-list-identifiers`.
1249 Valid values are nil, t, `head', `first', `last', an integer or a
1250 predicate.  See Info node `(gnus)Customizing Articles'."
1251   :version "21.1"
1252   :group 'gnus-article-treat
1253   :link '(custom-manual "(gnus)Customizing Articles")
1254   :type gnus-article-treat-custom)
1255
1256 (gnus-define-group-parameter
1257  list-identifier
1258  :variable-document
1259  "Alist of regexps and correspondent identifiers."
1260  :variable-group gnus-article-washing
1261  :parameter-type
1262  '(choice :tag "Identifier"
1263           :value nil
1264           (symbol :tag "Item in `gnus-list-identifiers'" none)
1265           regexp
1266           (const :tag "None" nil))
1267  :parameter-document
1268  "If non-nil, specify how to remove `identifiers' from articles' subject.
1269
1270 Any symbol is used to look up a regular expression to match the
1271 banner in `gnus-list-identifiers'.  A string is used as a regular
1272 expression to match the identifier directly.")
1273
1274 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1275                         "Gnus 5.10 (Emacs 22.1)")
1276
1277 (defcustom gnus-treat-strip-pem nil
1278   "Strip PEM signatures.
1279 Valid values are nil, t, `head', `first', `last', an integer or a
1280 predicate.  See Info node `(gnus)Customizing Articles'."
1281   :group 'gnus-article-treat
1282   :link '(custom-manual "(gnus)Customizing Articles")
1283   :type gnus-article-treat-custom)
1284
1285 (defcustom gnus-treat-strip-banner t
1286   "Strip banners from articles.
1287 The banner to be stripped is specified in the `banner' group parameter.
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-highlight-headers 'head
1295   "Highlight the headers.
1296 Valid values are nil, t, `head', `first', `last', an integer or a
1297 predicate.  See Info node `(gnus)Customizing Articles'."
1298   :group 'gnus-article-treat
1299   :link '(custom-manual "(gnus)Customizing Articles")
1300   :type gnus-article-treat-head-custom)
1301 (put 'gnus-treat-highlight-headers 'highlight t)
1302
1303 (defcustom gnus-treat-highlight-citation t
1304   "Highlight cited text.
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-custom)
1310 (put 'gnus-treat-highlight-citation 'highlight t)
1311
1312 (defcustom gnus-treat-strip-headers-in-body t
1313   "Strip the X-No-Archive header line from the beginning of the body.
1314 Valid values are nil, t, `head', `first', `last', an integer or a
1315 predicate.  See Info node `(gnus)Customizing Articles'."
1316   :version "21.1"
1317   :group 'gnus-article-treat
1318   :link '(custom-manual "(gnus)Customizing Articles")
1319   :type gnus-article-treat-custom)
1320
1321 (defcustom gnus-treat-strip-trailing-blank-lines nil
1322   "Strip trailing blank lines.
1323 Valid values are nil, t, `head', `first', `last', an integer or a
1324 predicate.  See Info node `(gnus)Customizing Articles'.
1325
1326 When set to t, it also strips trailing blanks in all MIME parts.
1327 Consider to use `last' instead."
1328   :group 'gnus-article-treat
1329   :link '(custom-manual "(gnus)Customizing Articles")
1330   :type gnus-article-treat-custom)
1331
1332 (defcustom gnus-treat-strip-leading-blank-lines nil
1333   "Strip leading blank lines.
1334 Valid values are nil, t, `head', `first', `last', an integer or a
1335 predicate.  See Info node `(gnus)Customizing Articles'.
1336
1337 When set to t, it also strips trailing blanks in all MIME parts."
1338   :group 'gnus-article-treat
1339   :link '(custom-manual "(gnus)Customizing Articles")
1340   :type gnus-article-treat-custom)
1341
1342 (defcustom gnus-treat-strip-multiple-blank-lines nil
1343   "Strip multiple blank lines.
1344 Valid values are nil, t, `head', `first', `last', an integer or a
1345 predicate.  See Info node `(gnus)Customizing Articles'."
1346   :group 'gnus-article-treat
1347   :link '(custom-manual "(gnus)Customizing Articles")
1348   :type gnus-article-treat-custom)
1349
1350 (defcustom gnus-treat-unfold-headers 'head
1351   "Unfold folded header lines.
1352 Valid values are nil, t, `head', `first', `last', an integer or a
1353 predicate.  See Info node `(gnus)Customizing Articles'."
1354   :version "22.1"
1355   :group 'gnus-article-treat
1356   :link '(custom-manual "(gnus)Customizing Articles")
1357   :type gnus-article-treat-custom)
1358
1359 (defcustom gnus-article-unfold-long-headers nil
1360   "If non-nil, allow unfolding headers even if the header is long.
1361 If it is a regexp, only long headers matching this regexp are unfolded.
1362 If it is t, all long headers are unfolded.
1363
1364 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1365   :version "23.1" ;; No Gnus
1366   :group 'gnus-article-treat
1367   :type '(choice (const nil)
1368                  (const :tag "all" t)
1369                  (regexp)))
1370
1371 (defcustom gnus-treat-fold-headers nil
1372   "Fold headers.
1373 Valid values are nil, t, `head', `first', `last', an integer or a
1374 predicate.  See Info node `(gnus)Customizing Articles'."
1375   :version "22.1"
1376   :group 'gnus-article-treat
1377   :link '(custom-manual "(gnus)Customizing Articles")
1378   :type gnus-article-treat-custom)
1379
1380 (defcustom gnus-treat-fold-newsgroups 'head
1381   "Fold the Newsgroups and Followup-To 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-overstrike t
1390   "Treat overstrike highlighting.
1391 Valid values are nil, t, `head', `first', `last', an integer or a
1392 predicate.  See Info node `(gnus)Customizing Articles'."
1393   :group 'gnus-article-treat
1394   :link '(custom-manual "(gnus)Customizing Articles")
1395   :type gnus-article-treat-custom)
1396 (put 'gnus-treat-overstrike 'highlight t)
1397
1398 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1399   "Treat ANSI SGR control sequences.
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
1406 (make-obsolete-variable 'gnus-treat-display-xface
1407                         'gnus-treat-display-x-face "Emacs 22.1")
1408
1409 (defcustom gnus-treat-display-x-face
1410   (and (not noninteractive)
1411        (gnus-image-type-available-p 'xbm)
1412        (if (featurep 'xemacs)
1413            (featurep 'xface)
1414          (condition-case nil
1415              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1416                   (executable-find "icontopbm"))
1417            ;; shell-command-to-string may signal an error, e.g. if
1418            ;; shell-file-name is not found.
1419            (error nil)))
1420        'head)
1421   "Display X-Face headers.
1422 Valid values are nil and `head'.
1423 See Info node `(gnus)Customizing Articles' and Info node
1424 `(gnus)X-Face' for details."
1425   :group 'gnus-article-treat
1426   :version "21.1"
1427   :link '(custom-manual "(gnus)Customizing Articles")
1428   :link '(custom-manual "(gnus)X-Face")
1429   :type gnus-article-treat-head-custom
1430   :set (lambda (symbol value)
1431          (set-default
1432           symbol
1433           (cond ((or (boundp symbol) (get symbol 'saved-value))
1434                  value)
1435                 ((boundp 'gnus-treat-display-xface)
1436                  (message "\
1437 ** gnus-treat-display-xface is an obsolete variable;\
1438  use gnus-treat-display-x-face instead")
1439                  (default-value 'gnus-treat-display-xface))
1440                 ((get 'gnus-treat-display-xface 'saved-value)
1441                  (message "\
1442 ** gnus-treat-display-xface is an obsolete variable;\
1443  use gnus-treat-display-x-face instead")
1444                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1445                 (t
1446                  value)))))
1447 (put 'gnus-treat-display-x-face 'highlight t)
1448
1449 (defcustom gnus-treat-display-face
1450   (and (not noninteractive)
1451        (gnus-image-type-available-p 'png)
1452        'head)
1453   "Display Face headers.
1454 Valid values are nil, t, `head', `first', `last', an integer or a
1455 predicate.  See Info node `(gnus)Customizing Articles' and Info
1456 node `(gnus)Face' for details."
1457   :group 'gnus-article-treat
1458   :version "22.1"
1459   :link '(custom-manual "(gnus)Customizing Articles")
1460   :link '(custom-manual "(gnus)X-Face")
1461   :type gnus-article-treat-head-custom)
1462 (put 'gnus-treat-display-face 'highlight t)
1463
1464 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1465   "Display smileys.
1466 Valid values are nil, t, `head', `first', `last', an integer or a
1467 predicate.  See Info node `(gnus)Customizing Articles' and Info
1468 node `(gnus)Smileys' for details."
1469   :group 'gnus-article-treat
1470   :version "21.1"
1471   :link '(custom-manual "(gnus)Customizing Articles")
1472   :link '(custom-manual "(gnus)Smileys")
1473   :type gnus-article-treat-custom)
1474 (put 'gnus-treat-display-smileys 'highlight t)
1475
1476 (defcustom gnus-treat-from-picon
1477   (if (and (gnus-image-type-available-p 'xpm)
1478            (gnus-picons-installed-p))
1479       'head nil)
1480   "Display picons in the From header.
1481 Valid values are nil, t, `head', `first', `last', an integer or a
1482 predicate.  See Info node `(gnus)Customizing Articles' and Info
1483 node `(gnus)Picons' for details."
1484   :version "22.1"
1485   :group 'gnus-article-treat
1486   :group 'gnus-picon
1487   :link '(custom-manual "(gnus)Customizing Articles")
1488   :link '(custom-manual "(gnus)Picons")
1489   :type gnus-article-treat-head-custom)
1490 (put 'gnus-treat-from-picon 'highlight t)
1491
1492 (defcustom gnus-treat-mail-picon
1493   (if (and (gnus-image-type-available-p 'xpm)
1494            (gnus-picons-installed-p))
1495       'head nil)
1496   "Display picons in To and Cc headers.
1497 Valid values are nil, t, `head', `first', `last', an integer or a
1498 predicate.  See Info node `(gnus)Customizing Articles' and Info
1499 node `(gnus)Picons' for details."
1500   :version "22.1"
1501   :group 'gnus-article-treat
1502   :group 'gnus-picon
1503   :link '(custom-manual "(gnus)Customizing Articles")
1504   :link '(custom-manual "(gnus)Picons")
1505   :type gnus-article-treat-head-custom)
1506 (put 'gnus-treat-mail-picon 'highlight t)
1507
1508 (defcustom gnus-treat-newsgroups-picon
1509   (if (and (gnus-image-type-available-p 'xpm)
1510            (gnus-picons-installed-p))
1511       'head nil)
1512   "Display picons in the Newsgroups and Followup-To headers.
1513 Valid values are nil, t, `head', `first', `last', an integer or a
1514 predicate.  See Info node `(gnus)Customizing Articles' and Info
1515 node `(gnus)Picons' for details."
1516   :version "22.1"
1517   :group 'gnus-article-treat
1518   :group 'gnus-picon
1519   :link '(custom-manual "(gnus)Customizing Articles")
1520   :link '(custom-manual "(gnus)Picons")
1521   :type gnus-article-treat-head-custom)
1522 (put 'gnus-treat-newsgroups-picon 'highlight t)
1523
1524 (defcustom gnus-treat-from-gravatar nil
1525   "Display gravatars in the From header.
1526 Valid values are nil, t, `head', `first', `last', an integer or a
1527 predicate.  See Info node `(gnus)Customizing Articles' and Info
1528 node `(gnus)Gravatars' for details."
1529   :version "24.1"
1530   :group 'gnus-article-treat
1531   :group 'gnus-gravatar
1532   :link '(custom-manual "(gnus)Customizing Articles")
1533   :link '(custom-manual "(gnus)Gravatars")
1534   :type gnus-article-treat-head-custom)
1535 (put 'gnus-treat-from-gravatar 'highlight t)
1536
1537 (defcustom gnus-treat-mail-gravatar nil
1538   "Display gravatars in To and Cc headers.
1539 Valid values are nil, t, `head', `first', `last', an integer or a
1540 predicate.  See Info node `(gnus)Customizing Articles' and Info
1541 node `(gnus)Gravatars' for details."
1542   :version "24.1"
1543   :group 'gnus-article-treat
1544   :group 'gnus-gravatar
1545   :link '(custom-manual "(gnus)Customizing Articles")
1546   :link '(custom-manual "(gnus)Gravatars")
1547   :type gnus-article-treat-head-custom)
1548 (put 'gnus-treat-mail-gravatar 'highlight t)
1549
1550 (defcustom gnus-treat-body-boundary
1551   (if (or gnus-treat-newsgroups-picon
1552           gnus-treat-mail-picon
1553           gnus-treat-from-picon
1554           gnus-treat-from-gravatar
1555           gnus-treat-mail-gravatar)
1556       ;; If there's much decoration, the user might prefer a boundery.
1557       'head
1558     nil)
1559   "Draw a boundary at the end of the headers.
1560 Valid values are nil and `head'.
1561 See Info node `(gnus)Customizing Articles' for details."
1562   :version "22.1"
1563   :group 'gnus-article-treat
1564   :link '(custom-manual "(gnus)Customizing Articles")
1565   :type gnus-article-treat-head-custom)
1566
1567 (defcustom gnus-treat-capitalize-sentences nil
1568   "Capitalize sentence-starting words.
1569 Valid values are nil, t, `head', `first', `last', an integer or a
1570 predicate.  See Info node `(gnus)Customizing Articles'."
1571   :version "21.1"
1572   :group 'gnus-article-treat
1573   :link '(custom-manual "(gnus)Customizing Articles")
1574   :type gnus-article-treat-custom)
1575
1576 (defcustom gnus-treat-wash-html nil
1577   "Format as HTML.
1578 Valid values are nil, t, `head', `first', `last', an integer or a
1579 predicate.  See Info node `(gnus)Customizing Articles'."
1580   :version "22.1"
1581   :group 'gnus-article-treat
1582   :link '(custom-manual "(gnus)Customizing Articles")
1583   :type gnus-article-treat-custom)
1584
1585 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1586   "Fill long lines.
1587 Valid values are nil, t, `head', `first', `last', an integer or a
1588 predicate.  See Info node `(gnus)Customizing Articles'."
1589   :version "24.1"
1590   :group 'gnus-article-treat
1591   :link '(custom-manual "(gnus)Customizing Articles")
1592   :type gnus-article-treat-custom)
1593
1594 (defcustom gnus-treat-x-pgp-sig nil
1595   "Verify X-PGP-Sig.
1596 To automatically treat X-PGP-Sig, set it to head.
1597 Valid values are nil, t, `head', `first', `last', an integer or a
1598 predicate.  See Info node `(gnus)Customizing Articles'."
1599   :version "22.1"
1600   :group 'gnus-article-treat
1601   :group 'mime-security
1602   :link '(custom-manual "(gnus)Customizing Articles")
1603   :type gnus-article-treat-custom)
1604
1605 (defvar gnus-article-encrypt-protocol-alist
1606   '(("PGP" . mml2015-self-encrypt)))
1607
1608 ;; Set to nil if more than one protocol added to
1609 ;; gnus-article-encrypt-protocol-alist.
1610 (defcustom gnus-article-encrypt-protocol "PGP"
1611   "The protocol used for encrypt articles.
1612 It is a string, such as \"PGP\". If nil, ask user."
1613   :version "22.1"
1614   :type 'string
1615   :group 'mime-security)
1616
1617 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1618                               (mm-coding-system-p 'utf-8)
1619                               (executable-find idna-program))
1620   "Whether IDNA decoding of headers is used when viewing messages.
1621 This requires GNU Libidn, and by default only enabled if it is found."
1622   :version "22.1"
1623   :group 'gnus-article-headers
1624   :type 'boolean)
1625
1626 (defcustom gnus-article-over-scroll nil
1627   "If non-nil, allow scrolling the article buffer even when there no more text."
1628   :version "22.1"
1629   :group 'gnus-article
1630   :type 'boolean)
1631
1632 (defcustom gnus-inhibit-images nil
1633   "Non-nil means inhibit displaying of images inline in the article body."
1634   :version "24.1"
1635   :group 'gnus-article
1636   :type 'boolean)
1637
1638 (defcustom gnus-blocked-images 'gnus-block-private-groups
1639   "Images that have URLs matching this regexp will be blocked.
1640 This can also be a function to be evaluated.  If so, it will be
1641 called with the group name as the parameter, and should return a
1642 regexp."
1643   :version "24.1"
1644   :group 'gnus-art
1645   :type 'regexp)
1646
1647 ;;; Internal variables
1648
1649 (defvar gnus-english-month-names
1650   '("January" "February" "March" "April" "May" "June" "July" "August"
1651     "September" "October" "November" "December"))
1652
1653 (defvar article-goto-body-goes-to-point-min-p nil)
1654 (defvar gnus-article-wash-types nil)
1655 (defvar gnus-article-emphasis-alist nil)
1656 (defvar gnus-article-image-alist nil)
1657
1658 (defvar gnus-article-mime-handle-alist-1 nil)
1659 (defvar gnus-treatment-function-alist
1660   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1661     (gnus-treat-strip-banner gnus-article-strip-banner)
1662     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1663     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1664     (gnus-treat-buttonize gnus-article-add-buttons)
1665     (gnus-treat-fill-article gnus-article-fill-cited-article)
1666     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1667     (gnus-treat-strip-cr gnus-article-remove-cr)
1668     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1669     (gnus-treat-display-x-face gnus-article-display-x-face)
1670     (gnus-treat-display-face gnus-article-display-face)
1671     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1672     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1673     (gnus-treat-hide-signature gnus-article-hide-signature)
1674     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1675     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1676     (gnus-treat-from-picon gnus-treat-from-picon)
1677     (gnus-treat-mail-picon gnus-treat-mail-picon)
1678     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1679     (gnus-treat-strip-pem gnus-article-hide-pem)
1680     (gnus-treat-date gnus-article-treat-date)
1681     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1682     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1683     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1684     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1685     (gnus-treat-strip-trailing-blank-lines
1686      gnus-article-remove-trailing-blank-lines)
1687     (gnus-treat-strip-leading-blank-lines
1688      gnus-article-strip-leading-blank-lines)
1689     (gnus-treat-strip-multiple-blank-lines
1690      gnus-article-strip-multiple-blank-lines)
1691     (gnus-treat-overstrike gnus-article-treat-overstrike)
1692     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1693     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1694     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1695     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1696     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1697     (gnus-treat-display-smileys gnus-treat-smiley)
1698     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1699     (gnus-treat-wash-html gnus-article-wash-html)
1700     (gnus-treat-emphasize gnus-article-emphasize)
1701     (gnus-treat-hide-citation gnus-article-hide-citation)
1702     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1703     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1704     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1705
1706 (defvar gnus-article-mime-handle-alist nil)
1707 (defvar article-lapsed-timer nil)
1708 (defvar gnus-article-current-summary nil)
1709
1710 (defvar gnus-article-mode-syntax-table
1711   (let ((table (copy-syntax-table text-mode-syntax-table)))
1712     ;; This causes the citation match run O(2^n).
1713     ;; (modify-syntax-entry ?- "w" table)
1714     (modify-syntax-entry ?> ")<" table)
1715     (modify-syntax-entry ?< "(>" table)
1716     ;; make M-. in article buffers work for `foo' strings
1717     (modify-syntax-entry ?' " " table)
1718     (modify-syntax-entry ?` " " table)
1719     table)
1720   "Syntax table used in article mode buffers.
1721 Initialized from `text-mode-syntax-table.")
1722
1723 (defvar gnus-save-article-buffer nil)
1724
1725 (defvar gnus-number-of-articles-to-be-saved nil)
1726
1727 (defvar gnus-inhibit-hiding nil)
1728
1729 (defvar gnus-article-edit-mode nil)
1730
1731 ;;; Macros for dealing with the article buffer.
1732
1733 (defmacro gnus-with-article-headers (&rest forms)
1734   `(with-current-buffer gnus-article-buffer
1735      (save-restriction
1736        (let ((inhibit-read-only t)
1737              (inhibit-point-motion-hooks t)
1738              (case-fold-search t))
1739          (article-narrow-to-head)
1740          ,@forms))))
1741
1742 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1743 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1744
1745 (defmacro gnus-with-article-buffer (&rest forms)
1746   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1747      (with-current-buffer gnus-article-buffer
1748        (let ((inhibit-read-only t))
1749          ,@forms))))
1750
1751 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1752 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1753
1754 (defun gnus-article-goto-header (header)
1755   "Go to HEADER, which is a regular expression."
1756   (re-search-forward (concat "^\\(" header "\\):") nil t))
1757
1758 (defsubst gnus-article-hide-text (b e props)
1759   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1760   (gnus-add-text-properties-when 'article-type nil b e props)
1761   (when (memq 'intangible props)
1762     (put-text-property
1763      (max (1- b) (point-min))
1764      b 'intangible (cddr (memq 'intangible props)))))
1765
1766 (defsubst gnus-article-unhide-text (b e)
1767   "Remove hidden text properties from region between B and E."
1768   (remove-text-properties b e gnus-hidden-properties)
1769   (when (memq 'intangible gnus-hidden-properties)
1770     (put-text-property (max (1- b) (point-min))
1771                        b 'intangible nil)))
1772
1773 (defun gnus-article-hide-text-type (b e type)
1774   "Hide text of TYPE between B and E."
1775   (gnus-add-wash-type type)
1776   (gnus-article-hide-text
1777    b e (cons 'article-type (cons type gnus-hidden-properties))))
1778
1779 (defun gnus-article-unhide-text-type (b e type)
1780   "Unhide text of TYPE between B and E."
1781   (gnus-delete-wash-type type)
1782   (remove-text-properties
1783    b e (cons 'article-type (cons type gnus-hidden-properties)))
1784   (when (memq 'intangible gnus-hidden-properties)
1785     (put-text-property (max (1- b) (point-min))
1786                        b 'intangible nil)))
1787
1788 (defun gnus-article-hide-text-of-type (type)
1789   "Hide text of TYPE in the current buffer."
1790   (save-excursion
1791     (let ((b (point-min))
1792           (e (point-max)))
1793       (while (setq b (text-property-any b e 'article-type type))
1794         (add-text-properties b (incf b) gnus-hidden-properties)))))
1795
1796 (defun gnus-article-delete-text-of-type (type)
1797   "Delete text of TYPE in the current buffer."
1798   (save-excursion
1799     (let ((b (point-min)))
1800       (if (eq type 'multipart)
1801           ;; Remove MIME buttons associated with multipart/alternative parts.
1802           (progn
1803             (goto-char b)
1804             (while (if (get-text-property (point) 'gnus-part)
1805                        (setq b (point))
1806                      (when (setq b (next-single-property-change (point)
1807                                                                 'gnus-part))
1808                        (goto-char b)
1809                        t))
1810               (end-of-line)
1811               (skip-chars-forward "\n")
1812               (when (eq (get-text-property b 'article-type) 'multipart)
1813                 (delete-region b (point)))))
1814         (while (setq b (text-property-any b (point-max) 'article-type type))
1815           (delete-region
1816            b (or (text-property-not-all b (point-max) 'article-type type)
1817                  (point-max))))))))
1818
1819 (defun gnus-article-delete-invisible-text ()
1820   "Delete all invisible text in the current buffer."
1821   (save-excursion
1822     (let ((b (point-min)))
1823       (while (setq b (text-property-any b (point-max) 'invisible t))
1824         (delete-region
1825          b (or (text-property-not-all b (point-max) 'invisible t)
1826                (point-max)))))))
1827
1828 (defun gnus-article-text-type-exists-p (type)
1829   "Say whether any text of type TYPE exists in the buffer."
1830   (text-property-any (point-min) (point-max) 'article-type type))
1831
1832 (defsubst gnus-article-header-rank ()
1833   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1834   (let ((list gnus-sorted-header-list)
1835         (i 1))
1836     (while list
1837       (if (looking-at (car list))
1838           (setq list nil)
1839         (setq list (cdr list))
1840         (incf i)))
1841       i))
1842
1843 (defun article-hide-headers (&optional arg delete)
1844   "Hide unwanted headers and possibly sort them as well."
1845   (interactive)
1846   ;; This function might be inhibited.
1847   (unless gnus-inhibit-hiding
1848     (let ((inhibit-read-only t)
1849           (case-fold-search t)
1850           (max (1+ (length gnus-sorted-header-list)))
1851           (inhibit-point-motion-hooks t)
1852           (cur (current-buffer))
1853           ignored visible beg)
1854       (save-excursion
1855         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1856         ;; group parameters, so we should go to the summary buffer.
1857         (when (prog1
1858                   (condition-case nil
1859                       (progn (set-buffer gnus-summary-buffer) t)
1860                     (error nil))
1861                 (setq ignored (when (not gnus-visible-headers)
1862                                 (cond ((stringp gnus-ignored-headers)
1863                                        gnus-ignored-headers)
1864                                       ((listp gnus-ignored-headers)
1865                                        (mapconcat 'identity
1866                                                   gnus-ignored-headers
1867                                                   "\\|"))))
1868                       visible (cond ((stringp gnus-visible-headers)
1869                                      gnus-visible-headers)
1870                                     ((and gnus-visible-headers
1871                                           (listp gnus-visible-headers))
1872                                      (mapconcat 'identity
1873                                                 gnus-visible-headers
1874                                                 "\\|")))))
1875           (set-buffer cur))
1876         (save-restriction
1877           ;; First we narrow to just the headers.
1878           (article-narrow-to-head)
1879           ;; Hide any "From " lines at the beginning of (mail) articles.
1880           (while (looking-at "From ")
1881             (forward-line 1))
1882           (unless (bobp)
1883             (delete-region (point-min) (point)))
1884           ;; Then treat the rest of the header lines.
1885           ;; Then we use the two regular expressions
1886           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1887           ;; select which header lines is to remain visible in the
1888           ;; article buffer.
1889           (while (re-search-forward "^[^ \t:]*:" nil t)
1890             (beginning-of-line)
1891             ;; Mark the rank of the header.
1892             (put-text-property
1893              (point) (1+ (point)) 'message-rank
1894              (if (or (and visible (looking-at visible))
1895                      (and ignored
1896                           (not (looking-at ignored))))
1897                  (gnus-article-header-rank)
1898                (+ 2 max)))
1899             (forward-line 1))
1900           (message-sort-headers-1)
1901           (when (setq beg (text-property-any
1902                            (point-min) (point-max) 'message-rank (+ 2 max)))
1903             ;; We delete the unwanted headers.
1904             (gnus-add-wash-type 'headers)
1905             (add-text-properties (point-min) (+ 5 (point-min))
1906                                  '(article-type headers dummy-invisible t))
1907             (delete-region beg (point-max))))))))
1908
1909 (defun article-hide-boring-headers (&optional arg)
1910   "Toggle hiding of headers that aren't very interesting.
1911 If given a negative prefix, always show; if given a positive prefix,
1912 always hide."
1913   (interactive (gnus-article-hidden-arg))
1914   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1915              (not gnus-show-all-headers))
1916     (save-excursion
1917       (save-restriction
1918         (let ((inhibit-read-only t)
1919               (inhibit-point-motion-hooks t))
1920           (article-narrow-to-head)
1921           (dolist (elem gnus-boring-article-headers)
1922             (goto-char (point-min))
1923             (cond
1924              ;; Hide empty headers.
1925              ((eq elem 'empty)
1926               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1927                 (forward-line -1)
1928                 (gnus-article-hide-text-type
1929                  (point-at-bol)
1930                  (progn
1931                    (end-of-line)
1932                    (if (re-search-forward "^[^ \t]" nil t)
1933                        (match-beginning 0)
1934                      (point-max)))
1935                  'boring-headers)))
1936              ;; Hide boring Newsgroups header.
1937              ((eq elem 'newsgroups)
1938               (when (gnus-string-equal
1939                      (gnus-fetch-field "newsgroups")
1940                      (gnus-group-real-name
1941                       (if (boundp 'gnus-newsgroup-name)
1942                           gnus-newsgroup-name
1943                         "")))
1944                 (gnus-article-hide-header "newsgroups")))
1945              ((eq elem 'to-address)
1946               (let ((to (message-fetch-field "to"))
1947                     (to-address
1948                      (gnus-parameter-to-address
1949                       (if (boundp 'gnus-newsgroup-name)
1950                           gnus-newsgroup-name ""))))
1951                 (when (and to to-address
1952                            (ignore-errors
1953                              (gnus-string-equal
1954                               ;; only one address in To
1955                               (nth 1 (mail-extract-address-components to))
1956                               to-address)))
1957                   (gnus-article-hide-header "to"))))
1958              ((eq elem 'to-list)
1959               (let ((to (message-fetch-field "to"))
1960                     (to-list
1961                      (gnus-parameter-to-list
1962                       (if (boundp 'gnus-newsgroup-name)
1963                           gnus-newsgroup-name ""))))
1964                 (when (and to to-list
1965                            (ignore-errors
1966                              (gnus-string-equal
1967                               ;; only one address in To
1968                               (nth 1 (mail-extract-address-components to))
1969                               to-list)))
1970                   (gnus-article-hide-header "to"))))
1971              ((eq elem 'cc-list)
1972               (let ((cc (message-fetch-field "cc"))
1973                     (to-list
1974                      (gnus-parameter-to-list
1975                       (if (boundp 'gnus-newsgroup-name)
1976                           gnus-newsgroup-name ""))))
1977                 (when (and cc to-list
1978                            (ignore-errors
1979                              (gnus-string-equal
1980                               ;; only one address in CC
1981                               (nth 1 (mail-extract-address-components cc))
1982                               to-list)))
1983                   (gnus-article-hide-header "cc"))))
1984              ((eq elem 'followup-to)
1985               (when (gnus-string-equal
1986                      (message-fetch-field "followup-to")
1987                      (message-fetch-field "newsgroups"))
1988                 (gnus-article-hide-header "followup-to")))
1989              ((eq elem 'reply-to)
1990               (if (gnus-group-find-parameter
1991                    gnus-newsgroup-name 'broken-reply-to)
1992                   (gnus-article-hide-header "reply-to")
1993                 (let ((from (message-fetch-field "from"))
1994                       (reply-to (message-fetch-field "reply-to")))
1995                   (when
1996                       (and
1997                        from reply-to
1998                        (ignore-errors
1999                          (equal
2000                           (sort (mapcar
2001                                  (lambda (x) (downcase (cadr x)))
2002                                  (mail-extract-address-components from t))
2003                                 'string<)
2004                           (sort (mapcar
2005                                  (lambda (x) (downcase (cadr x)))
2006                                  (mail-extract-address-components reply-to t))
2007                                 'string<))))
2008                     (gnus-article-hide-header "reply-to")))))
2009              ((eq elem 'date)
2010               (let ((date (with-current-buffer gnus-original-article-buffer
2011                             ;; If date in `gnus-article-buffer' is localized
2012                             ;; (`gnus-treat-date-user-defined'),
2013                             ;; `days-between' might fail.
2014                             (message-fetch-field "date"))))
2015                 (when (and date
2016                            (< (days-between (current-time-string) date)
2017                               4))
2018                   (gnus-article-hide-header "date"))))
2019              ((eq elem 'long-to)
2020               (let ((to (message-fetch-field "to"))
2021                     (cc (message-fetch-field "cc")))
2022                 (when (> (length to) 1024)
2023                   (gnus-article-hide-header "to"))
2024                 (when (> (length cc) 1024)
2025                   (gnus-article-hide-header "cc"))))
2026              ((eq elem 'many-to)
2027               (let ((to-count 0)
2028                     (cc-count 0))
2029                 (goto-char (point-min))
2030                 (while (re-search-forward "^to:" nil t)
2031                   (setq to-count (1+ to-count)))
2032                 (when (> to-count 1)
2033                   (while (> to-count 0)
2034                     (goto-char (point-min))
2035                     (save-restriction
2036                       (re-search-forward "^to:" nil nil to-count)
2037                       (forward-line -1)
2038                       (narrow-to-region (point) (point-max))
2039                       (gnus-article-hide-header "to"))
2040                     (setq to-count (1- to-count))))
2041                 (goto-char (point-min))
2042                 (while (re-search-forward "^cc:" nil t)
2043                   (setq cc-count (1+ cc-count)))
2044                 (when (> cc-count 1)
2045                   (while (> cc-count 0)
2046                     (goto-char (point-min))
2047                     (save-restriction
2048                       (re-search-forward "^cc:" nil nil cc-count)
2049                       (forward-line -1)
2050                       (narrow-to-region (point) (point-max))
2051                       (gnus-article-hide-header "cc"))
2052                     (setq cc-count (1- cc-count)))))))))))))
2053
2054 (defun gnus-article-hide-header (header)
2055   (save-excursion
2056     (goto-char (point-min))
2057     (when (re-search-forward (concat "^" header ":") nil t)
2058       (gnus-article-hide-text-type
2059        (point-at-bol)
2060        (progn
2061          (end-of-line)
2062          (if (re-search-forward "^[^ \t]" nil t)
2063              (match-beginning 0)
2064            (point-max)))
2065        'boring-headers))))
2066
2067 (defvar gnus-article-normalized-header-length 40
2068   "Length of normalized headers.")
2069
2070 (defun article-normalize-headers ()
2071   "Make all header lines 40 characters long."
2072   (interactive)
2073   (let ((inhibit-read-only t)
2074         column)
2075     (save-excursion
2076       (save-restriction
2077         (article-narrow-to-head)
2078         (while (not (eobp))
2079           (cond
2080            ((< (setq column (- (point-at-eol) (point)))
2081                gnus-article-normalized-header-length)
2082             (end-of-line)
2083             (insert (make-string
2084                      (- gnus-article-normalized-header-length column)
2085                      ? )))
2086            ((> column gnus-article-normalized-header-length)
2087             (gnus-put-text-property
2088              (progn
2089                (forward-char gnus-article-normalized-header-length)
2090                (point))
2091              (point-at-eol)
2092              'invisible t))
2093            (t
2094             ;; Do nothing.
2095             ))
2096           (forward-line 1))))))
2097
2098 (defun article-treat-dumbquotes ()
2099   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2100 Note that this function guesses whether a character is a sm*rtq**t* or
2101 not, so it should only be used interactively.
2102
2103 Sm*rtq**t*s are M****s***'s unilateral extension to the
2104 iso-8859-1 character map in an attempt to provide more quoting
2105 characters.  If you see something like \\222 or \\264 where
2106 you're expecting some kind of apostrophe or quotation mark, then
2107 try this wash."
2108   (interactive)
2109   (article-translate-strings gnus-article-dumbquotes-map))
2110
2111 (defvar org-entities)
2112
2113 (defun article-treat-non-ascii ()
2114   "Translate many Unicode characters into their ASCII equivalents."
2115   (interactive)
2116   (require 'org-entities)
2117   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2118     (dolist (elem org-entities)
2119       (when (and (listp elem)
2120                  (= (length (nth 6 elem)) 1))
2121         (if (featurep 'xemacs)
2122             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2123           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2124     (save-excursion
2125       (when (article-goto-body)
2126         (let ((inhibit-read-only t)
2127               replace props)
2128           (while (not (eobp))
2129             (if (not (setq replace (if (featurep 'xemacs)
2130                                        (get-char-table (following-char) table)
2131                                      (aref table (following-char)))))
2132                 (forward-char 1)
2133               (if (prog1
2134                       (setq props (text-properties-at (point)))
2135                     (delete-char 1))
2136                   (add-text-properties (point) (progn (insert replace) (point))
2137                                        props)
2138                 (insert replace)))))))))
2139
2140 (defun article-translate-characters (from to)
2141   "Translate all characters in the body of the article according to FROM and TO.
2142 FROM is a string of characters to translate from; to is a string of
2143 characters to translate to."
2144   (save-excursion
2145     (when (article-goto-body)
2146       (let ((inhibit-read-only t)
2147             (x (make-string 225 ?x))
2148             (i -1))
2149         (while (< (incf i) (length x))
2150           (aset x i i))
2151         (setq i 0)
2152         (while (< i (length from))
2153           (aset x (aref from i) (aref to i))
2154           (incf i))
2155         (translate-region (point) (point-max) x)))))
2156
2157 (defun article-translate-strings (map)
2158   "Translate all string in the body of the article according to MAP.
2159 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2160   (save-excursion
2161     (when (article-goto-body)
2162       (let ((inhibit-read-only t))
2163         (dolist (elem map)
2164           (let ((from (car elem))
2165                 (to (cadr elem)))
2166             (save-excursion
2167               (if (stringp from)
2168                   (while (search-forward from nil t)
2169                     (replace-match to))
2170                 (while (not (eobp))
2171                   (if (eq (following-char) from)
2172                       (progn
2173                         (delete-char 1)
2174                         (insert to))
2175                     (forward-char 1)))))))))))
2176
2177 (defun article-treat-overstrike ()
2178   "Translate overstrikes into bold text."
2179   (interactive)
2180   (save-excursion
2181     (when (article-goto-body)
2182       (let ((inhibit-read-only t))
2183         (while (search-forward "\b" nil t)
2184           (let ((next (char-after))
2185                 (previous (char-after (- (point) 2))))
2186             ;; We do the boldification/underlining by hiding the
2187             ;; overstrikes and putting the proper text property
2188             ;; on the letters.
2189             (cond
2190              ((eq next previous)
2191               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2192               (put-text-property (point) (1+ (point)) 'face 'bold))
2193              ((eq next ?_)
2194               (gnus-article-hide-text-type
2195                (1- (point)) (1+ (point)) 'overstrike)
2196               (put-text-property
2197                (- (point) 2) (1- (point)) 'face 'underline))
2198              ((eq previous ?_)
2199               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2200               (put-text-property
2201                (point) (1+ (point)) 'face 'underline)))))))))
2202
2203 (defun article-treat-ansi-sequences ()
2204   "Translate ANSI SGR control sequences into overlays or extents."
2205   (interactive)
2206   (save-excursion
2207     (when (article-goto-body)
2208       (let ((inhibit-read-only t))
2209         (ansi-color-apply-on-region (point) (point-max))))))
2210
2211 (defun gnus-article-treat-unfold-headers ()
2212   "Unfold folded message headers.
2213 Only the headers that fit into the current window width will be
2214 unfolded."
2215   (interactive)
2216   (gnus-with-article-headers
2217     (let (length)
2218       (while (not (eobp))
2219         (save-restriction
2220           (mail-header-narrow-to-field)
2221           (let* ((header (buffer-string))
2222                  (unfoldable
2223                   (or (equal gnus-article-unfold-long-headers t)
2224                       (and (stringp gnus-article-unfold-long-headers)
2225                            (string-match gnus-article-unfold-long-headers header)))))
2226             (with-temp-buffer
2227               (insert header)
2228               (goto-char (point-min))
2229               (while (re-search-forward "\n[\t ]" nil t)
2230                 (replace-match " " t t)))
2231             (setq length (- (point-max) (point-min) 1))
2232             (when (or unfoldable
2233                       (< length (window-width)))
2234               (while (re-search-forward "\n[\t ]" nil t)
2235                 (replace-match " " t t))))
2236           (goto-char (point-max)))))))
2237
2238 (defun gnus-article-treat-fold-headers ()
2239   "Fold message headers."
2240   (interactive)
2241   (gnus-with-article-headers
2242     (while (not (eobp))
2243       (save-restriction
2244         (mail-header-narrow-to-field)
2245         (mail-header-fold-field)
2246         (goto-char (point-max))))))
2247
2248 (defun gnus-treat-smiley ()
2249   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2250   (interactive)
2251   (gnus-with-article-buffer
2252     (if (memq 'smiley gnus-article-wash-types)
2253         (gnus-delete-images 'smiley)
2254       (article-goto-body)
2255       (let ((images (smiley-region (point) (point-max))))
2256         (when images
2257           (gnus-add-wash-type 'smiley)
2258           (dolist (image images)
2259             (gnus-add-image 'smiley image)))))))
2260
2261 (defun gnus-article-remove-images ()
2262   "Remove all images from the article buffer."
2263   (interactive)
2264   (gnus-with-article-buffer
2265     (save-restriction
2266       (widen)
2267       (dolist (elem gnus-article-image-alist)
2268         (gnus-delete-images (car elem))))))
2269
2270 (defun gnus-article-show-images ()
2271   "Show any images that are in the HTML-rendered article buffer.
2272 This only works if the article in question is HTML."
2273   (interactive)
2274   (gnus-with-article-buffer
2275     (save-restriction
2276       (widen)
2277       (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2278                                                       'image-displayer))
2279         (destructuring-bind (start end function) region
2280           (funcall function (get-text-property start 'image-url)
2281                    start end))))))
2282
2283 (defun gnus-article-treat-fold-newsgroups ()
2284   "Unfold folded message headers.
2285 Only the headers that fit into the current window width will be
2286 unfolded."
2287   (interactive)
2288   (gnus-with-article-headers
2289     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2290       (save-restriction
2291         (mail-header-narrow-to-field)
2292         (while (re-search-forward ", *" nil t)
2293           (replace-match ", " t t))
2294         (mail-header-fold-field)
2295         (goto-char (point-max))))))
2296
2297 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2298   "Value of `truncate-lines' in Gnus Article buffer.
2299 Valid values are nil, t, `head', `first', `last', an integer or a
2300 predicate.  See Info node `(gnus)Customizing Articles'."
2301   :version "23.1" ;; No Gnus
2302   :group 'gnus-article
2303   ;; :link '(custom-manual "(gnus)Customizing Articles")
2304   :type 'boolean)
2305
2306 (defun gnus-article-toggle-truncate-lines (&optional arg)
2307   "Toggle whether to fold or truncate long lines in article the buffer.
2308 If ARG is non-nil and not a number, toggle
2309 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2310 long lines if and only if arg is positive."
2311   (interactive "P")
2312   (cond
2313    ((and (numberp arg) (> arg 0))
2314     (setq gnus-article-truncate-lines t))
2315    ((numberp arg)
2316     (setq gnus-article-truncate-lines nil))
2317    (arg
2318     (setq gnus-article-truncate-lines
2319           (not gnus-article-truncate-lines))))
2320   (gnus-with-article-buffer
2321     (cond
2322      ((and (numberp arg) (> arg 0))
2323       (setq truncate-lines nil))
2324      ((numberp arg)
2325       (setq truncate-lines t)))
2326     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2327     ;; `toggle-truncate-lines' needs an argument.
2328     (toggle-truncate-lines)))
2329
2330 (defun gnus-article-treat-body-boundary ()
2331   "Place a boundary line at the end of the headers."
2332   (interactive)
2333   (when (and gnus-body-boundary-delimiter
2334              (> (length gnus-body-boundary-delimiter) 0))
2335     (gnus-with-article-headers
2336       (goto-char (point-max))
2337       (let ((start (point)))
2338         (insert "X-Boundary: ")
2339         (gnus-add-text-properties start (point) '(invisible t intangible t))
2340        (insert (let (str (max (window-width)))
2341                  (if (featurep 'xemacs)
2342                      (setq max (1- max)))
2343                  (while (>= max (length str))
2344                     (setq str (concat str gnus-body-boundary-delimiter)))
2345                  (substring str 0 max))
2346                 "\n")
2347         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2348
2349 (defun article-fill-long-lines ()
2350   "Fill lines that are wider than the window width."
2351   (interactive)
2352   (save-excursion
2353     (let ((inhibit-read-only t)
2354           (width (window-width (get-buffer-window (current-buffer)))))
2355       (save-restriction
2356         (article-goto-body)
2357         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2358           (while (not (eobp))
2359             (end-of-line)
2360             (when (>= (current-column) (min fill-column width))
2361               (narrow-to-region (min (1+ (point)) (point-max))
2362                                 (point-at-bol))
2363               (let ((goback (point-marker)))
2364                 (fill-paragraph nil)
2365                 (goto-char (marker-position goback)))
2366               (widen))
2367             (forward-line 1)))))))
2368
2369 (defun article-capitalize-sentences ()
2370   "Capitalize the first word in each sentence."
2371   (interactive)
2372   (save-excursion
2373     (let ((inhibit-read-only t)
2374           (paragraph-start "^[\n\^L]"))
2375       (article-goto-body)
2376       (while (not (eobp))
2377         (capitalize-word 1)
2378         (forward-sentence)))))
2379
2380 (defun article-remove-cr ()
2381   "Remove trailing CRs and then translate remaining CRs into LFs."
2382   (interactive)
2383   (save-excursion
2384     (let ((inhibit-read-only t))
2385       (goto-char (point-min))
2386       (while (re-search-forward "\r+$" nil t)
2387         (replace-match "" t t))
2388       (goto-char (point-min))
2389       (while (search-forward "\r" nil t)
2390         (replace-match "\n" t t)))))
2391
2392 (defun article-remove-trailing-blank-lines ()
2393   "Remove all trailing blank lines from the article."
2394   (interactive)
2395   (save-excursion
2396     (let ((inhibit-read-only t))
2397       (goto-char (point-max))
2398       (delete-region
2399        (point)
2400        (progn
2401          (while (and (not (bobp))
2402                      (looking-at "^[ \t]*$")
2403                      (not (gnus-annotation-in-region-p
2404                            (point) (point-at-eol))))
2405            (forward-line -1))
2406          (forward-line 1)
2407          (point))))))
2408
2409 (defvar gnus-face-properties-alist)
2410
2411 (defun article-display-face (&optional force)
2412   "Display any Face headers in the header."
2413   (interactive (list 'force))
2414   (let ((wash-face-p buffer-read-only))
2415     (gnus-with-article-headers
2416       ;; When displaying parts, this function can be called several times on
2417       ;; the same article, without any intended toggle semantic (as typing `W
2418       ;; D d' would have). So face deletion must occur only when we come from
2419       ;; an interactive command, that is when the *Article* buffer is
2420       ;; read-only.
2421       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2422           (gnus-delete-images 'face)
2423         (let ((from (message-fetch-field "from"))
2424               face faces)
2425           (save-current-buffer
2426             (when (and wash-face-p
2427                        (gnus-buffer-live-p gnus-original-article-buffer)
2428                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2429               (set-buffer gnus-original-article-buffer))
2430             (save-restriction
2431               (mail-narrow-to-head)
2432               (when (or force
2433                         ;; Check whether this face is censored.
2434                         (not (and gnus-article-x-face-too-ugly
2435                                   (or from
2436                                       (setq from (message-fetch-field "from")))
2437                                   (string-match gnus-article-x-face-too-ugly
2438                                                 from))))
2439                 (while (gnus-article-goto-header "Face")
2440                   (push (mail-header-field-value) faces)))))
2441           (when faces
2442             (goto-char (point-min))
2443             (let (png image)
2444               (unless (setq from (gnus-article-goto-header "from"))
2445                 (insert "From:")
2446                 (setq from (point))
2447                 (insert " [no `from' set]\n"))
2448               (while faces
2449                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2450                   (setq image
2451                         (apply 'gnus-create-image png 'png t
2452                                (cdr (assq 'png gnus-face-properties-alist))))
2453                   (goto-char from)
2454                   (gnus-add-wash-type 'face)
2455                   (gnus-add-image 'face image)
2456                   (gnus-put-image image nil 'face))))))))))
2457
2458 (defun article-display-x-face (&optional force)
2459   "Look for an X-Face header and display it if present."
2460   (interactive (list 'force))
2461   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2462     (gnus-with-article-headers
2463       ;; Delete the old process, if any.
2464       (when (process-status "article-x-face")
2465         (delete-process "article-x-face"))
2466       ;; See the comment in `article-display-face'.
2467       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2468           ;; We have already displayed X-Faces, so we remove them
2469           ;; instead.
2470           (gnus-delete-images 'xface)
2471         ;; Display X-Faces.
2472         (let ((from (message-fetch-field "from"))
2473               x-faces face)
2474           (save-current-buffer
2475             (when (and wash-face-p
2476                        (gnus-buffer-live-p gnus-original-article-buffer)
2477                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2478               ;; If type `W f', use gnus-original-article-buffer,
2479               ;; otherwise use the current buffer because displaying
2480               ;; RFC822 parts calls this function too.
2481               (set-buffer gnus-original-article-buffer))
2482             (save-restriction
2483               (mail-narrow-to-head)
2484               (and gnus-article-x-face-command
2485                    (or force
2486                        ;; Check whether this face is censored.
2487                        (not (and gnus-article-x-face-too-ugly
2488                                  (or from
2489                                      (setq from (message-fetch-field "from")))
2490                                  (string-match gnus-article-x-face-too-ugly
2491                                                from))))
2492                    (while (gnus-article-goto-header "X-Face")
2493                      (push (mail-header-field-value) x-faces)))))
2494           (when x-faces
2495             ;; We display the face.
2496             (cond ((functionp gnus-article-x-face-command)
2497                    ;; The command is a lisp function, so we call it.
2498                    (mapc gnus-article-x-face-command x-faces))
2499                   ((stringp gnus-article-x-face-command)
2500                    ;; The command is a string, so we interpret the command
2501                    ;; as a, well, command, and fork it off.
2502                    (let ((process-connection-type nil))
2503                      (gnus-set-process-query-on-exit-flag
2504                       (start-process
2505                        "article-x-face" nil shell-file-name
2506                        shell-command-switch gnus-article-x-face-command)
2507                       nil)
2508                      ;; Sending multiple EOFs to xv doesn't work,
2509                      ;; so we only do a single external face.
2510                      (with-temp-buffer
2511                        (insert (car x-faces))
2512                        (process-send-region "article-x-face"
2513                                             (point-min) (point-max)))
2514                      (process-send-eof "article-x-face")))
2515                   (t
2516                    (error "`%s' set to `%s' is not a function"
2517                           gnus-article-x-face-command
2518                           'gnus-article-x-face-command)))))))))
2519
2520 (defun article-decode-mime-words ()
2521   "Decode all MIME-encoded words in the article."
2522   (interactive)
2523   (gnus-with-article-buffer
2524     (let ((inhibit-point-motion-hooks t)
2525           (mail-parse-charset gnus-newsgroup-charset)
2526           (mail-parse-ignored-charsets
2527            (with-current-buffer gnus-summary-buffer
2528              gnus-newsgroup-ignored-charsets)))
2529       (mail-decode-encoded-word-region (point-min) (point-max)))))
2530
2531 (defun article-decode-charset (&optional prompt)
2532   "Decode charset-encoded text in the article.
2533 If PROMPT (the prefix), prompt for a coding system to use."
2534   (interactive "P")
2535   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2536         (inhibit-read-only t)
2537         (mail-parse-charset gnus-newsgroup-charset)
2538         (mail-parse-ignored-charsets
2539          (save-excursion (condition-case nil
2540                              (set-buffer gnus-summary-buffer)
2541                            (error))
2542                          gnus-newsgroup-ignored-charsets))
2543         ct cte ctl charset format)
2544     (save-excursion
2545       (save-restriction
2546         (article-narrow-to-head)
2547         (setq ct (message-fetch-field "Content-Type" t)
2548               cte (message-fetch-field "Content-Transfer-Encoding" t)
2549               ctl (and ct (mail-header-parse-content-type ct))
2550               charset (cond
2551                        (prompt
2552                         (mm-read-coding-system "Charset to decode: "))
2553                        (ctl
2554                         (mail-content-type-get ctl 'charset)))
2555               format (and ctl (mail-content-type-get ctl 'format)))
2556         (when cte
2557           (setq cte (mail-header-strip cte)))
2558         (if (and ctl (not (string-match "/" (car ctl))))
2559             (setq ctl nil))
2560         (goto-char (point-max)))
2561       (forward-line 1)
2562       (save-restriction
2563         (narrow-to-region (point) (point-max))
2564         (when (and (eq mail-parse-charset 'gnus-decoded)
2565                    (eq (mm-body-7-or-8) '8bit))
2566           ;; The text code could have been decoded.
2567           (setq charset mail-parse-charset))
2568         (when (and (or (not ctl)
2569                        (equal (car ctl) "text/plain"))
2570                    (not format)) ;; article with format will decode later.
2571           (mm-decode-body
2572            charset (and cte (intern (downcase
2573                                      (gnus-strip-whitespace cte))))
2574            (car ctl)))))))
2575
2576 (defun article-decode-encoded-words ()
2577   "Remove encoded-word encoding from headers."
2578   (let ((inhibit-point-motion-hooks t)
2579         (mail-parse-charset gnus-newsgroup-charset)
2580         (mail-parse-ignored-charsets
2581          (save-excursion (condition-case nil
2582                              (set-buffer gnus-summary-buffer)
2583                            (error))
2584                          gnus-newsgroup-ignored-charsets))
2585         (inhibit-read-only t)
2586         end start)
2587     (goto-char (point-min))
2588     (when (search-forward "\n\n" nil 'move)
2589       (forward-line -1))
2590     (setq end (point))
2591     (while (not (bobp))
2592       (while (progn
2593                (forward-line -1)
2594                (and (not (bobp))
2595                     (memq (char-after) '(?\t ? )))))
2596       (setq start (point))
2597       (if (looking-at "\
2598 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2599 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2600           (funcall gnus-decode-address-function start end)
2601         (funcall gnus-decode-header-function start end))
2602       (goto-char (setq end start)))))
2603
2604 (defun article-decode-group-name ()
2605   "Decode group names in Newsgroups, Followup-To and Xref headers."
2606   (let ((inhibit-point-motion-hooks t)
2607         (inhibit-read-only t)
2608         (method (gnus-find-method-for-group gnus-newsgroup-name))
2609         regexp)
2610     (when (and (or gnus-group-name-charset-method-alist
2611                    gnus-group-name-charset-group-alist)
2612                (gnus-buffer-live-p gnus-original-article-buffer))
2613       (save-restriction
2614         (article-narrow-to-head)
2615         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2616           (with-current-buffer gnus-original-article-buffer
2617             (goto-char (point-min)))
2618           (setq regexp (concat "^" header
2619                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2620           (while (re-search-forward regexp nil t)
2621             (replace-match (save-match-data
2622                              (gnus-decode-newsgroups
2623                               ;; XXX how to use data in article buffer?
2624                               (with-current-buffer gnus-original-article-buffer
2625                                 (re-search-forward regexp nil t)
2626                                 (match-string 1))
2627                               gnus-newsgroup-name method))
2628                            t t nil 1))
2629           (goto-char (point-min)))))))
2630
2631 (autoload 'idna-to-unicode "idna")
2632
2633 (defun article-decode-idna-rhs ()
2634   "Decode IDNA strings in RHS in various headers in current buffer.
2635 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2636 Mail-Reply-To: and Mail-Followup-To:."
2637   (when gnus-use-idna
2638     (save-restriction
2639       (let ((inhibit-point-motion-hooks t)
2640             (inhibit-read-only t))
2641         (article-narrow-to-head)
2642         (goto-char (point-min))
2643         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2644           (let (ace unicode)
2645             (when (save-match-data
2646                     (and (setq ace (match-string 1))
2647                          (save-excursion
2648                            (and (re-search-backward "^[^ \t]" nil t)
2649                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2650                          (setq unicode (idna-to-unicode ace))))
2651               (unless (string= ace unicode)
2652                 (replace-match unicode nil nil nil 1)))))))))
2653
2654 (defun article-de-quoted-unreadable (&optional force read-charset)
2655   "Translate a quoted-printable-encoded article.
2656 If FORCE, decode the article whether it is marked as quoted-printable
2657 or not.
2658 If READ-CHARSET, ask for a coding system."
2659   (interactive (list 'force current-prefix-arg))
2660   (save-excursion
2661     (let ((inhibit-read-only t) type charset)
2662       (if (gnus-buffer-live-p gnus-original-article-buffer)
2663           (with-current-buffer gnus-original-article-buffer
2664             (setq type
2665                   (gnus-fetch-field "content-transfer-encoding"))
2666             (let* ((ct (gnus-fetch-field "content-type"))
2667                    (ctl (and ct (mail-header-parse-content-type ct))))
2668               (setq charset (and ctl
2669                                  (mail-content-type-get ctl 'charset)))
2670               (if (stringp charset)
2671                   (setq charset (intern (downcase charset)))))))
2672       (if read-charset
2673           (setq charset (mm-read-coding-system "Charset: " charset)))
2674       (unless charset
2675         (setq charset gnus-newsgroup-charset))
2676       (when (or force
2677                 (and type (let ((case-fold-search t))
2678                             (string-match "quoted-printable" type))))
2679         (article-goto-body)
2680         (quoted-printable-decode-region
2681          (point) (point-max) (mm-charset-to-coding-system charset))))))
2682
2683 (defun article-de-base64-unreadable (&optional force read-charset)
2684   "Translate a base64 article.
2685 If FORCE, decode the article whether it is marked as base64 not.
2686 If READ-CHARSET, ask for a coding system."
2687   (interactive (list 'force current-prefix-arg))
2688   (save-excursion
2689     (let ((inhibit-read-only t) type charset)
2690       (if (gnus-buffer-live-p gnus-original-article-buffer)
2691           (with-current-buffer gnus-original-article-buffer
2692             (setq type
2693                   (gnus-fetch-field "content-transfer-encoding"))
2694             (let* ((ct (gnus-fetch-field "content-type"))
2695                    (ctl (and ct (mail-header-parse-content-type ct))))
2696               (setq charset (and ctl
2697                                  (mail-content-type-get ctl 'charset)))
2698               (if (stringp charset)
2699                   (setq charset (intern (downcase charset)))))))
2700       (if read-charset
2701           (setq charset (mm-read-coding-system "Charset: " charset)))
2702       (unless charset
2703         (setq charset gnus-newsgroup-charset))
2704       (when (or force
2705                 (and type (let ((case-fold-search t))
2706                             (string-match "base64" type))))
2707         (article-goto-body)
2708         (save-restriction
2709           (narrow-to-region (point) (point-max))
2710           (base64-decode-region (point-min) (point-max))
2711           (mm-decode-coding-region
2712            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2713
2714 (eval-when-compile
2715   (require 'rfc1843))
2716
2717 (defun article-decode-HZ ()
2718   "Translate a HZ-encoded article."
2719   (interactive)
2720   (require 'rfc1843)
2721   (save-excursion
2722     (let ((inhibit-read-only t))
2723       (rfc1843-decode-region (point-min) (point-max)))))
2724
2725 (defun article-unsplit-urls ()
2726   "Remove the newlines that some other mailers insert into URLs."
2727   (interactive)
2728   (save-excursion
2729     (let ((inhibit-read-only t))
2730       (goto-char (point-min))
2731       (while (re-search-forward
2732               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2733         (replace-match "\\1\\3" t)))
2734     (when (interactive-p)
2735       (gnus-treat-article nil))))
2736
2737 (defun article-wash-html ()
2738   "Format an HTML article."
2739   (interactive)
2740   (let ((handles nil)
2741         (buffer-read-only nil))
2742     (when (gnus-buffer-live-p gnus-original-article-buffer)
2743       (setq handles (mm-dissect-buffer t t)))
2744     (article-goto-body)
2745     (delete-region (point) (point-max))
2746     (mm-inline-text-html handles)))
2747
2748 (defvar gnus-article-browse-html-temp-list nil
2749   "List of temporary files created by `gnus-article-browse-html-parts'.
2750 Internal variable.")
2751
2752 (defcustom gnus-article-browse-delete-temp 'ask
2753   "What to do with temporary files from `gnus-article-browse-html-parts'.
2754 If nil, don't delete temporary files.  If it is t, delete them on
2755 exit from the summary buffer.  If it is the symbol `file', query
2756 on each file, if it is `ask' ask once when exiting from the
2757 summary buffer."
2758   :group 'gnus-article
2759   :version "23.1" ;; No Gnus
2760   :type '(choice (const :tag "Don't delete" nil)
2761                  (const :tag "Don't ask" t)
2762                  (const :tag "Ask" ask)
2763                  (const :tag "Ask for each file" file)))
2764
2765 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2766
2767 (defun gnus-article-browse-delete-temp-files (&optional how)
2768   "Delete temp-files created by `gnus-article-browse-html-parts'."
2769   (when (and gnus-article-browse-html-temp-list
2770              (progn
2771                (or how (setq how gnus-article-browse-delete-temp))
2772                (if (eq how 'ask)
2773                    (let ((files (length gnus-article-browse-html-temp-list)))
2774                      (gnus-y-or-n-p (format
2775                                      "Delete all %s temporary HTML file%s? "
2776                                      files
2777                                      (if (> files 1) "s" ""))))
2778                  how)))
2779     (dolist (file gnus-article-browse-html-temp-list)
2780       (cond ((file-directory-p file)
2781              (when (or (not (eq how 'file))
2782                        (gnus-y-or-n-p
2783                         (format
2784                          "Delete temporary HTML file(s) in directory `%s'? "
2785                          (file-name-as-directory file))))
2786                (gnus-delete-directory file)))
2787             ((file-exists-p file)
2788              (when (or (not (eq how 'file))
2789                        (gnus-y-or-n-p
2790                         (format "Delete temporary HTML file `%s'? " file)))
2791                (delete-file file)))))
2792     ;; Also remove file from the list when not deleted or if file doesn't
2793     ;; exist anymore.
2794     (setq gnus-article-browse-html-temp-list nil))
2795   gnus-article-browse-html-temp-list)
2796
2797 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2798   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2799 Return file name."
2800   (save-match-data
2801     (let (file type)
2802       (catch 'found
2803         (dolist (handle handles)
2804           (cond
2805            ((not (listp handle)))
2806            ((equal (mm-handle-media-supertype handle) "multipart")
2807             (when (setq file (gnus-article-browse-html-save-cid-content
2808                               cid handle directory))
2809               (throw 'found file)))
2810            ((equal (concat "<" cid ">") (mm-handle-id handle))
2811             (setq file
2812                   (expand-file-name
2813                    (or (mm-handle-filename handle)
2814                        (concat
2815                         (make-temp-name "cid")
2816                         (car (rassoc (car (mm-handle-type handle)) mailcap-mime-extensions))))
2817                    directory))
2818             (mm-save-part-to-file handle file)
2819             (throw 'found file))))))))
2820
2821 (defun gnus-article-browse-html-parts (list &optional header)
2822   "View all \"text/html\" parts from LIST.
2823 Recurse into multiparts.  The optional HEADER that should be a decoded
2824 message header will be added to the bodies of the \"text/html\" parts."
2825   ;; Internal function used by `gnus-article-browse-html-article'.
2826   (let (type file charset content cid-dir tmp-file showed)
2827     ;; Find and show the html-parts.
2828     (dolist (handle list)
2829       ;; If HTML, show it:
2830       (cond ((not (listp handle)))
2831             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2832                  (and (equal (car type) "message/external-body")
2833                       (or header
2834                           (setq file (mm-handle-filename handle)))
2835                       (or (mm-handle-cache handle)
2836                           (condition-case code
2837                               (progn (mm-extern-cache-contents handle) t)
2838                             (error
2839                              (gnus-message 3 "%s" (error-message-string code))
2840                              (when (>= gnus-verbose 3) (sit-for 2))
2841                              nil)))
2842                       (progn
2843                         (setq handle (mm-handle-cache handle)
2844                               type (mm-handle-type handle))
2845                         (equal (car type) "text/html"))))
2846              (setq charset (mail-content-type-get type 'charset)
2847                    content (mm-get-part handle))
2848              (with-temp-buffer
2849                (if (eq charset 'gnus-decoded)
2850                    (mm-enable-multibyte)
2851                  (mm-disable-multibyte))
2852                (insert content)
2853                ;; resolve cid contents
2854                (let ((case-fold-search t)
2855                      cid-file)
2856                  (goto-char (point-min))
2857                  (while (re-search-forward "\
2858 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2859                                            nil t)
2860                    (unless cid-dir
2861                      (setq cid-dir (mm-make-temp-file "cid" t))
2862                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2863                    (setq file nil
2864                          content nil)
2865                    (when (setq cid-file
2866                                (gnus-article-browse-html-save-cid-content
2867                                 (match-string 2)
2868                                 (with-current-buffer gnus-article-buffer
2869                                   gnus-article-mime-handles)
2870                                 cid-dir))
2871                      (replace-match (concat "file://" cid-file)
2872                                     nil nil nil 1))))
2873                (unless content (setq content (buffer-string))))
2874              (when (or charset header (not file))
2875                (setq tmp-file (mm-make-temp-file
2876                                ;; Do we need to care for 8.3 filenames?
2877                                "mm-" nil ".html")))
2878              ;; Add a meta html tag to specify charset and a header.
2879              (cond
2880               (header
2881                (let (title eheader body hcharset coding force-charset)
2882                  (with-temp-buffer
2883                    (mm-enable-multibyte)
2884                    (setq case-fold-search t)
2885                    (insert header "\n")
2886                    (setq title (message-fetch-field "subject"))
2887                    (goto-char (point-min))
2888                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2889                      (replace-match (cond ((match-beginning 1) "&lt;")
2890                                           ((match-beginning 2) "&gt;")
2891                                           (t "&amp;"))))
2892                    (goto-char (point-min))
2893                    (insert "<pre>\n")
2894                    (goto-char (point-max))
2895                    (insert "</pre>\n<hr>\n")
2896                    ;; We have to examine charset one by one since
2897                    ;; charset specified in parts might be different.
2898                    (if (eq charset 'gnus-decoded)
2899                        (setq charset 'utf-8
2900                              eheader (mm-encode-coding-string (buffer-string)
2901                                                               charset)
2902                              title (when title
2903                                      (mm-encode-coding-string title charset))
2904                              body (mm-encode-coding-string content charset)
2905                              force-charset t)
2906                      (setq hcharset (mm-find-mime-charset-region (point-min)
2907                                                                  (point-max)))
2908                      (cond ((= (length hcharset) 1)
2909                             (setq hcharset (car hcharset)
2910                                   coding (mm-charset-to-coding-system
2911                                           hcharset)))
2912                            ((> (length hcharset) 1)
2913                             (setq hcharset 'utf-8
2914                                   coding hcharset)))
2915                      (if coding
2916                          (if charset
2917                              (progn
2918                                (setq body
2919                                      (mm-charset-to-coding-system charset))
2920                                (if (eq coding body)
2921                                    (setq eheader (mm-encode-coding-string
2922                                                   (buffer-string) coding)
2923                                          title (when title
2924                                                  (mm-encode-coding-string
2925                                                   title coding))
2926                                          body content)
2927                                  (setq charset 'utf-8
2928                                        eheader (mm-encode-coding-string
2929                                                 (buffer-string) charset)
2930                                        title (when title
2931                                                (mm-encode-coding-string
2932                                                 title charset))
2933                                        body (mm-encode-coding-string
2934                                              (mm-decode-coding-string
2935                                               content body)
2936                                              charset)
2937                                        force-charset t)))
2938                            (setq charset hcharset
2939                                  eheader (mm-encode-coding-string
2940                                           (buffer-string) coding)
2941                                  title (when title
2942                                          (mm-encode-coding-string
2943                                           title coding))
2944                                  body content))
2945                        (setq eheader (mm-string-as-unibyte (buffer-string))
2946                              body content)))
2947                    (erase-buffer)
2948                    (mm-disable-multibyte)
2949                    (insert body)
2950                    (when charset
2951                      (mm-add-meta-html-tag handle charset force-charset))
2952                    (when title
2953                      (goto-char (point-min))
2954                      (unless (search-forward "<title>" nil t)
2955                        (re-search-forward "<head>\\s-*" nil t)
2956                        (insert "<title>" title "</title>\n")))
2957                    (goto-char (point-min))
2958                    (or (re-search-forward
2959                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2960                        (re-search-forward
2961                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2962                    (insert eheader)
2963                    (mm-write-region (point-min) (point-max)
2964                                     tmp-file nil nil nil 'binary t))))
2965               (charset
2966                (mm-with-unibyte-buffer
2967                  (insert (if (eq charset 'gnus-decoded)
2968                              (mm-encode-coding-string content
2969                                                       (setq charset 'utf-8))
2970                            content))
2971                  (if (or (mm-add-meta-html-tag handle charset)
2972                          (not file))
2973                      (mm-write-region (point-min) (point-max)
2974                                       tmp-file nil nil nil 'binary t)
2975                    (setq tmp-file nil))))
2976               (tmp-file
2977                (mm-save-part-to-file handle tmp-file)))
2978              (when tmp-file
2979                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2980              (add-hook 'gnus-summary-prepare-exit-hook
2981                        'gnus-article-browse-delete-temp-files)
2982              (add-hook 'gnus-exit-gnus-hook
2983                        (lambda  ()
2984                          (gnus-article-browse-delete-temp-files t)))
2985              ;; FIXME: Warn if there's an <img> tag?
2986              (browse-url-of-file (or tmp-file (expand-file-name file)))
2987              (setq showed t))
2988             ;; If multipart, recurse
2989             ((equal (mm-handle-media-supertype handle) "multipart")
2990              (when (gnus-article-browse-html-parts handle header)
2991                (setq showed t)))
2992             ((equal (mm-handle-media-type handle) "message/rfc822")
2993              (mm-with-multibyte-buffer
2994                (mm-insert-part handle)
2995                (setq handle (mm-dissect-buffer t t))
2996                (when (and (bufferp (car handle))
2997                           (stringp (car (mm-handle-type handle))))
2998                  (setq handle (list handle)))
2999                (when header
3000                  (article-decode-encoded-words)
3001                  (let ((gnus-visible-headers
3002                         (or (get 'gnus-visible-headers 'standard-value)
3003                             gnus-visible-headers)))
3004                    (article-hide-headers))
3005                  (goto-char (point-min))
3006                  (search-forward "\n\n" nil 'move)
3007                  (skip-chars-backward "\t\n ")
3008                  (setq header (buffer-substring (point-min) (point)))))
3009              (when (prog1
3010                        (gnus-article-browse-html-parts handle header)
3011                      (mm-destroy-parts handle))
3012                (setq showed t)))))
3013     showed))
3014
3015 (defun gnus-article-browse-html-article (&optional arg)
3016   "View \"text/html\" parts of the current article with a WWW browser.
3017 Inline images embedded in a message using the cid scheme, as they are
3018 generally considered to be safe, will be processed properly.
3019 The message header is added to the beginning of every html part unless
3020 the prefix argument ARG is given.
3021
3022 Warning: Spammers use links to images (using the http scheme) in HTML
3023 articles to verify whether you have read the message.  As
3024 `gnus-article-browse-html-article' passes the HTML content to the
3025 browser without eliminating these \"web bugs\" you should only
3026 use it for mails from trusted senders.
3027
3028 If you always want to display HTML parts in the browser, set
3029 `mm-text-html-renderer' to nil.
3030
3031 This command creates temporary files to pass HTML contents including
3032 images if any to the browser, and deletes them when exiting the group
3033 \(if you want)."
3034   ;; Cf. `mm-w3m-safe-url-regexp'
3035   (interactive "P")
3036   (if arg
3037       (gnus-summary-show-article)
3038     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3039                                     gnus-visible-headers))
3040           ;; As we insert a <hr>, there's no need for the body boundary.
3041           (gnus-treat-body-boundary nil))
3042       (gnus-summary-show-article)))
3043   (with-current-buffer gnus-article-buffer
3044     (let ((header (unless arg
3045                     (save-restriction
3046                       (widen)
3047                       (buffer-substring-no-properties
3048                        (goto-char (point-min))
3049                        (if (search-forward "\n\n" nil t)
3050                            (match-beginning 0)
3051                          (goto-char (point-max))
3052                          (skip-chars-backward "\t\n ")
3053                          (point))))))
3054           parts)
3055       (set-buffer gnus-original-article-buffer)
3056       (setq parts (mm-dissect-buffer t t))
3057       ;; If singlepart, enforce a list.
3058       (when (and (bufferp (car parts))
3059                  (stringp (car (mm-handle-type parts))))
3060         (setq parts (list parts)))
3061       ;; Process the list
3062       (unless (gnus-article-browse-html-parts parts header)
3063         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3064       (mm-destroy-parts parts)
3065       (unless arg
3066         (gnus-summary-show-article)))))
3067
3068 (defun article-hide-list-identifiers ()
3069   "Remove list identifies from the Subject header.
3070 The `gnus-list-identifiers' variable specifies what to do."
3071   (interactive)
3072   (let ((inhibit-point-motion-hooks t)
3073         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3074         (inhibit-read-only t))
3075     (when regexp
3076       (save-excursion
3077         (save-restriction
3078           (article-narrow-to-head)
3079           (goto-char (point-min))
3080           (while (re-search-forward
3081                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3082                   nil t)
3083             (delete-region (match-beginning 2) (match-end 0))
3084             (beginning-of-line))
3085           (when (re-search-forward
3086                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3087             (delete-region (match-beginning 1) (match-end 1))))))))
3088
3089 (defun article-hide-pem (&optional arg)
3090   "Toggle hiding of any PEM headers and signatures in the current article.
3091 If given a negative prefix, always show; if given a positive prefix,
3092 always hide."
3093   (interactive (gnus-article-hidden-arg))
3094   (unless (gnus-article-check-hidden-text 'pem arg)
3095     (save-excursion
3096       (let ((inhibit-read-only t) end)
3097         (goto-char (point-min))
3098         ;; Hide the horrendously ugly "header".
3099         (when (and (search-forward
3100                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3101                     nil t)
3102                    (setq end (1+ (match-beginning 0))))
3103           (gnus-add-wash-type 'pem)
3104           (gnus-article-hide-text-type
3105            end
3106            (if (search-forward "\n\n" nil t)
3107                (match-end 0)
3108              (point-max))
3109            'pem)
3110           ;; Hide the trailer as well
3111           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3112                                 nil t)
3113             (gnus-article-hide-text-type
3114              (match-beginning 0) (match-end 0) 'pem)))))))
3115
3116 (defun article-strip-banner ()
3117   "Strip the banners specified by the `banner' group parameter and by
3118 `gnus-article-address-banner-alist'."
3119   (interactive)
3120   (save-excursion
3121     (save-restriction
3122       (let ((inhibit-point-motion-hooks t))
3123         (when (gnus-parameter-banner gnus-newsgroup-name)
3124           (article-really-strip-banner
3125            (gnus-parameter-banner gnus-newsgroup-name)))
3126         (when gnus-article-address-banner-alist
3127           ;; Note that the From header is decoded here, so it is
3128           ;; required that the *-extract-address-components function
3129           ;; supports non-ASCII text.
3130           (let ((from (save-restriction
3131                         (widen)
3132                         (article-narrow-to-head)
3133                         (mail-fetch-field "from"))))
3134             (when (and from
3135                        (setq from
3136                              (cadr (funcall gnus-extract-address-components
3137                                             from))))
3138               (catch 'found
3139                 (dolist (pair gnus-article-address-banner-alist)
3140                   (when (string-match (car pair) from)
3141                     (throw 'found
3142                            (article-really-strip-banner (cdr pair)))))))))))))
3143
3144 (defun article-really-strip-banner (banner)
3145   "Strip the banner specified by the argument."
3146   (save-excursion
3147     (save-restriction
3148       (let ((inhibit-point-motion-hooks t)
3149             (gnus-signature-limit nil)
3150             (inhibit-read-only t))
3151         (article-goto-body)
3152         (cond
3153          ((eq banner 'signature)
3154           (when (gnus-article-narrow-to-signature)
3155             (widen)
3156             (forward-line -1)
3157             (delete-region (point) (point-max))))
3158          ((symbolp banner)
3159           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3160               (while (re-search-forward banner nil t)
3161                 (delete-region (match-beginning 0) (match-end 0)))))
3162          ((stringp banner)
3163           (while (re-search-forward banner nil t)
3164             (delete-region (match-beginning 0) (match-end 0)))))))))
3165
3166 (defun article-babel ()
3167   "Translate article using an online translation service."
3168   (interactive)
3169   (require 'babel)
3170   (gnus-with-article-buffer
3171     (when (article-goto-body)
3172       (let* ((start (point))
3173              (end (point-max))
3174              (orig (buffer-substring start end))
3175              (trans (babel-as-string orig)))
3176         (save-restriction
3177           (narrow-to-region start end)
3178           (delete-region start end)
3179           (insert trans))))))
3180
3181 (defun article-hide-signature (&optional arg)
3182   "Hide the signature in the current article.
3183 If given a negative prefix, always show; if given a positive prefix,
3184 always hide."
3185   (interactive (gnus-article-hidden-arg))
3186   (unless (gnus-article-check-hidden-text 'signature arg)
3187     (save-excursion
3188       (save-restriction
3189         (let ((inhibit-read-only t))
3190           (when (gnus-article-narrow-to-signature)
3191             (gnus-article-hide-text-type
3192              (point-min) (point-max) 'signature))))))
3193   (gnus-set-mode-line 'article))
3194
3195 (defun article-strip-headers-in-body ()
3196   "Strip offensive headers from bodies."
3197   (interactive)
3198   (save-excursion
3199     (article-goto-body)
3200     (let ((case-fold-search t))
3201       (when (looking-at "x-no-archive:")
3202         (gnus-delete-line)))))
3203
3204 (defun article-strip-leading-blank-lines ()
3205   "Remove all blank lines from the beginning of the article."
3206   (interactive)
3207   (save-excursion
3208     (let ((inhibit-point-motion-hooks t)
3209           (inhibit-read-only t))
3210       (when (article-goto-body)
3211         (while (and (not (eobp))
3212                     (looking-at "[ \t]*$"))
3213           (gnus-delete-line))))))
3214
3215 (defun article-narrow-to-head ()
3216   "Narrow the buffer to the head of the message.
3217 Point is left at the beginning of the narrowed-to region."
3218   (narrow-to-region
3219    (goto-char (point-min))
3220    (if (search-forward "\n\n" nil 1)
3221        (1- (point))
3222      (point-max)))
3223   (goto-char (point-min)))
3224
3225 (defun article-goto-body ()
3226   "Place point at the start of the body."
3227   (goto-char (point-min))
3228   (cond
3229    ;; This variable is only bound when dealing with separate
3230    ;; MIME body parts.
3231    (article-goto-body-goes-to-point-min-p
3232     t)
3233    ((search-forward "\n\n" nil t)
3234     t)
3235    (t
3236     (goto-char (point-max))
3237     nil)))
3238
3239 (defun article-strip-multiple-blank-lines ()
3240   "Replace consecutive blank lines with one empty line."
3241   (interactive)
3242   (save-excursion
3243     (let ((inhibit-point-motion-hooks t)
3244           (inhibit-read-only t))
3245       ;; First make all blank lines empty.
3246       (article-goto-body)
3247       (while (re-search-forward "^[ \t]+$" nil t)
3248         (unless (gnus-annotation-in-region-p
3249                  (match-beginning 0) (match-end 0))
3250           (replace-match "" nil t)))
3251       ;; Then replace multiple empty lines with a single empty line.
3252       (article-goto-body)
3253       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3254         (unless (gnus-annotation-in-region-p
3255                  (match-beginning 0) (match-end 0))
3256           (delete-region (match-beginning 1) (match-end 1)))))))
3257
3258 (defun article-strip-leading-space ()
3259   "Remove all white space from the beginning of the lines in the article."
3260   (interactive)
3261   (save-excursion
3262     (let ((inhibit-point-motion-hooks t)
3263           (inhibit-read-only t))
3264       (article-goto-body)
3265       (while (re-search-forward "^[ \t]+" nil t)
3266         (replace-match "" t t)))))
3267
3268 (defun article-strip-trailing-space ()
3269   "Remove all white space from the end of the lines in the article."
3270   (interactive)
3271   (save-excursion
3272     (let ((inhibit-point-motion-hooks t)
3273           (inhibit-read-only t))
3274       (article-goto-body)
3275       (while (re-search-forward "[ \t]+$" nil t)
3276         (replace-match "" t t)))))
3277
3278 (defun article-strip-blank-lines ()
3279   "Strip leading, trailing and multiple blank lines."
3280   (interactive)
3281   (article-strip-leading-blank-lines)
3282   (article-remove-trailing-blank-lines)
3283   (article-strip-multiple-blank-lines))
3284
3285 (defun article-strip-all-blank-lines ()
3286   "Strip all blank lines."
3287   (interactive)
3288   (save-excursion
3289     (let ((inhibit-point-motion-hooks t)
3290           (inhibit-read-only t))
3291       (article-goto-body)
3292       (while (re-search-forward "^[ \t]*\n" nil t)
3293         (replace-match "" t t)))))
3294
3295 (defun gnus-article-narrow-to-signature ()
3296   "Narrow to the signature; return t if a signature is found, else nil."
3297   (let ((inhibit-point-motion-hooks t))
3298     (when (gnus-article-search-signature)
3299       (forward-line 1)
3300       ;; Check whether we have some limits to what we consider
3301       ;; to be a signature.
3302       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3303                       (list gnus-signature-limit)))
3304             limit limited)
3305         (while (setq limit (pop limits))
3306           (if (or (and (integerp limit)
3307                        (< (- (point-max) (point)) limit))
3308                   (and (floatp limit)
3309                        (< (count-lines (point) (point-max)) limit))
3310                   (and (functionp limit)
3311                        (funcall limit))
3312                   (and (stringp limit)
3313                        (not (re-search-forward limit nil t))))
3314               ()                        ; This limit did not succeed.
3315             (setq limited t
3316                   limits nil)))
3317         (unless limited
3318           (narrow-to-region (point) (point-max))
3319           t)))))
3320
3321 (defun gnus-article-search-signature ()
3322   "Search the current buffer for the signature separator.
3323 Put point at the beginning of the signature separator."
3324   (let ((cur (point)))
3325     (goto-char (point-max))
3326     (if (if (stringp gnus-signature-separator)
3327             (re-search-backward gnus-signature-separator nil t)
3328           (let ((seps gnus-signature-separator))
3329             (while (and seps
3330                         (not (re-search-backward (car seps) nil t)))
3331               (pop seps))
3332             seps))
3333         t
3334       (goto-char cur)
3335       nil)))
3336
3337 (defun gnus-article-hidden-arg ()
3338   "Return the current prefix arg as a number, or 0 if no prefix."
3339   (list (if current-prefix-arg
3340             (prefix-numeric-value current-prefix-arg)
3341           0)))
3342
3343 (defun gnus-article-check-hidden-text (type arg)
3344   "Return nil if hiding is necessary.
3345 Arg can be nil or a number.  nil and positive means hide, negative
3346 means show, 0 means toggle."
3347   (save-excursion
3348     (save-restriction
3349       (let ((hide (gnus-article-hidden-text-p type)))
3350         (cond
3351          ((or (null arg)
3352               (> arg 0))
3353           nil)
3354          ((< arg 0)
3355           (gnus-article-show-hidden-text type)
3356           t)
3357          (t
3358           (if (eq hide 'hidden)
3359               (progn
3360                 (gnus-article-show-hidden-text type)
3361                 t)
3362             nil)))))))
3363
3364 (defun gnus-article-hidden-text-p (type)
3365   "Say whether the current buffer contains hidden text of type TYPE."
3366   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3367     (while (and pos
3368                 (not (get-text-property pos 'invisible))
3369                 (not (get-text-property pos 'dummy-invisible)))
3370       (setq pos
3371             (text-property-any (1+ pos) (point-max) 'article-type type)))
3372     (if pos
3373         'hidden
3374       nil)))
3375
3376 (defun gnus-article-show-hidden-text (type &optional dummy)
3377   "Show all hidden text of type TYPE.
3378 Originally it is hide instead of DUMMY."
3379   (let ((inhibit-read-only t)
3380         (inhibit-point-motion-hooks t))
3381     (gnus-remove-text-properties-when
3382      'article-type type
3383      (point-min) (point-max)
3384      (cons 'article-type (cons type
3385                                gnus-hidden-properties)))
3386     (gnus-delete-wash-type type)))
3387
3388 (defconst article-time-units
3389   `((year . ,(* 365.25 24 60 60))
3390     (week . ,(* 7 24 60 60))
3391     (day . ,(* 24 60 60))
3392     (hour . ,(* 60 60))
3393     (minute . 60)
3394     (second . 1))
3395   "Mapping from time units to seconds.")
3396
3397 (defun gnus-article-forward-header ()
3398   "Move point to the start of the next header.
3399 If the current header is a continuation header, this can be several
3400 lines forward."
3401   (let ((ended nil))
3402     (while (not ended)
3403       (forward-line 1)
3404       (if (looking-at "[ \t]+[^ \t]")
3405           (forward-line 1)
3406         (setq ended t)))))
3407
3408 (defun article-treat-date ()
3409   (article-date-ut (if (gnus-buffer-live-p gnus-summary-buffer)
3410                        (with-current-buffer gnus-summary-buffer
3411                          gnus-article-date-headers)
3412                      gnus-article-date-headers)
3413                    t))
3414
3415 (defun article-date-ut (&optional type highlight date-position)
3416   "Convert DATE date to TYPE in the current article.
3417 The default type is `ut'.  See `gnus-article-date-headers' for
3418 possible values."
3419   (interactive (list 'ut t))
3420   (let* ((case-fold-search t)
3421          (inhibit-read-only t)
3422          (inhibit-point-motion-hooks t)
3423          (first t)
3424          (visible-date (mail-fetch-field "Date"))
3425          pos date bface eface)
3426     (save-excursion
3427       (save-restriction
3428         (goto-char (point-min))
3429         (when (re-search-forward "^Date:" nil t)
3430           (setq bface (get-text-property (point-at-bol) 'face)
3431                 eface (get-text-property (1- (point-at-eol)) 'face)))
3432         (goto-char (point-min))
3433         ;; Delete any old Date headers.
3434         (if date-position
3435             (progn
3436               (goto-char date-position)
3437               (setq date (get-text-property (point) 'original-date))
3438               (delete-region (point)
3439                              (progn
3440                                (gnus-article-forward-header)
3441                                (point)))
3442               (article-transform-date date type bface eface))
3443           (while (re-search-forward "^Date:" nil t)
3444             (setq date (get-text-property (match-beginning 0) 'original-date))
3445             (delete-region (point-at-bol) (progn
3446                                             (gnus-article-forward-header)
3447                                             (point))))
3448           (when (and (not date)
3449                      visible-date)
3450             (setq date visible-date))
3451           (when date
3452             (article-transform-date date type bface eface)))))))
3453
3454 (defun article-transform-date (date type bface eface)
3455   (dolist (this-type (cond
3456                       ((null type)
3457                        (list 'ut))
3458                       ((atom type)
3459                        (list type))
3460                       (t
3461                        type)))
3462     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3463     (forward-line -1)
3464     (beginning-of-line)
3465     (put-text-property (point) (1+ (point))
3466                        'original-date date)
3467     (put-text-property (point) (1+ (point))
3468                        'gnus-date-type this-type)
3469     ;; Do highlighting.
3470     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3471       (put-text-property (match-beginning 1) (1+ (match-end 1))
3472                          'face bface)
3473       (put-text-property (match-beginning 2) (match-end 2)
3474                          'face eface))
3475     (forward-line 1)))
3476
3477 (defun article-make-date-line (date type)
3478   "Return a DATE line of TYPE."
3479   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3480                              combined-lapsed))
3481     (error "Unknown conversion type: %s" type))
3482   (condition-case ()
3483       (let ((time (ignore-errors (date-to-time date))))
3484         (cond
3485          ;; Convert to the local timezone.
3486          ((eq type 'local)
3487           (concat "Date: " (message-make-date time)))
3488          ;; Convert to Universal Time.
3489          ((eq type 'ut)
3490           (concat "Date: "
3491                   (substring
3492                    (message-make-date
3493                     (let* ((e (parse-time-string date))
3494                            (tm (apply 'encode-time e))
3495                            (ms (car tm))
3496                            (ls (- (cadr tm) (car (current-time-zone time)))))
3497                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3498                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3499                             (t (list ms ls)))))
3500                    0 -5)
3501                   "UT"))
3502          ;; Get the original date from the article.
3503          ((eq type 'original)
3504           (concat "Date: " (if (string-match "\n+$" date)
3505                                (substring date 0 (match-beginning 0))
3506                              date)))
3507          ;; Let the user define the format.
3508          ((eq type 'user-defined)
3509           (let ((format (or (condition-case nil
3510                                 (with-current-buffer gnus-summary-buffer
3511                                   gnus-article-time-format)
3512                               (error nil))
3513                             gnus-article-time-format)))
3514             (if (functionp format)
3515                 (funcall format time)
3516               (concat "Date: " (format-time-string format time)))))
3517          ;; ISO 8601.
3518          ((eq type 'iso8601)
3519           (let ((tz (car (current-time-zone time))))
3520             (concat
3521              "Date: "
3522              (format-time-string "%Y%m%dT%H%M%S" time)
3523              (format "%s%02d%02d"
3524                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3525                      (/ (% (abs tz) 3600) 60)))))
3526          ;; Do a lapsed format.
3527          ((eq type 'lapsed)
3528           (concat "Date: " (article-lapsed-string time)))
3529          ;; A combined date/lapsed format.
3530          ((eq type 'combined-lapsed)
3531           (let ((date-string (article-make-date-line date 'original))
3532                 (segments 3)
3533                 lapsed-string)
3534             (while (and
3535                     time
3536                     (setq lapsed-string
3537                           (concat " (" (article-lapsed-string time segments) ")"))
3538                     (> (+ (length date-string)
3539                           (length lapsed-string))
3540                        (+ fill-column 6))
3541                     (> segments 0))
3542               (setq segments (1- segments)))
3543             (if (> segments 0)
3544                 (concat date-string lapsed-string)
3545               date-string)))
3546          ;; Display the date in proper English
3547          ((eq type 'english)
3548           (let ((dtime (decode-time time)))
3549             (concat
3550              "Date: the "
3551              (number-to-string (nth 3 dtime))
3552              (let ((digit (% (nth 3 dtime) 10)))
3553                (cond
3554                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3555                 ((= digit 1) "st")
3556                 ((= digit 2) "nd")
3557                 ((= digit 3) "rd")
3558                 (t "th")))
3559              " of "
3560              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3561              " "
3562              (number-to-string (nth 5 dtime))
3563              " at "
3564              (format "%02d" (nth 2 dtime))
3565              ":"
3566              (format "%02d" (nth 1 dtime)))))))
3567     (foo
3568      (format "Date: %s (from Gnus)" date))))
3569
3570 (defun article-lapsed-string (time &optional max-segments)
3571   ;; If the date is seriously mangled, the timezone functions are
3572   ;; liable to bug out, so we ignore all errors.
3573   (let* ((now (current-time))
3574          (real-time (subtract-time now time))
3575          (real-sec (and real-time
3576                         (+ (* (float (car real-time)) 65536)
3577                            (cadr real-time))))
3578          (sec (and real-time (abs real-sec)))
3579          (segments 0)
3580          num prev)
3581     (unless max-segments
3582       (setq max-segments (length article-time-units)))
3583     (cond
3584      ((null real-time)
3585       "Unknown")
3586      ((zerop sec)
3587       "Now")
3588      (t
3589       (concat
3590        ;; This is a bit convoluted, but basically we go
3591        ;; through the time units for years, weeks, etc,
3592        ;; and divide things to see whether that results
3593        ;; in positive answers.
3594        (mapconcat
3595         (lambda (unit)
3596           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3597                   (>= segments max-segments))
3598               ;; The (remaining) seconds are too few to
3599               ;; be divided into this time unit.
3600               ""
3601             ;; It's big enough, so we output it.
3602             (setq sec (- sec (* num (cdr unit))))
3603             (prog1
3604                 (concat (if prev ", " "") (int-to-string
3605                                            (floor num))
3606                         " " (symbol-name (car unit))
3607                         (if (> num 1) "s" ""))
3608               (setq prev t
3609                     segments (1+ segments)))))
3610         article-time-units "")
3611        ;; If dates are odd, then it might appear like the
3612        ;; article was sent in the future.
3613        (if (> real-sec 0)
3614            " ago"
3615          " in the future"))))))
3616
3617 (defun article-date-local (&optional highlight)
3618   "Convert the current article date to the local timezone."
3619   (interactive (list t))
3620   (article-date-ut 'local highlight))
3621
3622 (defun article-date-english (&optional highlight)
3623   "Convert the current article date to something that is proper English."
3624   (interactive (list t))
3625   (article-date-ut 'english highlight))
3626
3627 (defun article-date-original (&optional highlight)
3628   "Convert the current article date to what it was originally.
3629 This is only useful if you have used some other date conversion
3630 function and want to see what the date was before converting."
3631   (interactive (list t))
3632   (article-date-ut 'original highlight))
3633
3634 (defun article-date-lapsed (&optional highlight)
3635   "Convert the current article date to time lapsed since it was sent."
3636   (interactive (list t))
3637   (article-date-ut 'lapsed highlight))
3638
3639 (defun article-date-combined-lapsed (&optional highlight)
3640   "Convert the current article date to time lapsed since it was sent."
3641   (interactive (list t))
3642   (article-date-ut 'combined-lapsed highlight))
3643
3644 (defun article-update-date-lapsed ()
3645   "Function to be run from a timer to update the lapsed time line."
3646   (save-match-data
3647     (let ((buffer (current-buffer)))
3648       (ignore-errors
3649         (walk-windows
3650          (lambda (w)
3651            (set-buffer (window-buffer w))
3652            (when (eq major-mode 'gnus-article-mode)
3653              (let ((old-line (count-lines (point-min) (point)))
3654                    (old-column (- (point) (line-beginning-position)))
3655                    (window-start
3656                     (window-start (get-buffer-window (current-buffer)))))
3657                (goto-char (point-min))
3658                (while (re-search-forward "^Date:" nil t)
3659                  (let ((type (get-text-property (match-beginning 0)
3660                                                 'gnus-date-type)))
3661                    (when (memq type '(lapsed combined-lapsed user-format))
3662                      (when (and window-start
3663                                 (not (= window-start
3664                                         (save-excursion
3665                                           (forward-line 1)
3666                                           (point)))))
3667                        (setq window-start nil))
3668                      (save-excursion
3669                        (article-date-ut type t (match-beginning 0)))
3670                      (forward-line 1)
3671                      (when window-start
3672                        (set-window-start (get-buffer-window (current-buffer))
3673                                          (point))))))
3674                (goto-char (point-min))
3675                (when (> old-column 0)
3676                  (setq old-line (1- old-line)))
3677                (forward-line old-line)
3678                (end-of-line)
3679                (when (> (current-column) old-column)
3680                  (beginning-of-line)
3681                  (forward-char old-column)))))
3682          nil 'visible))
3683       (set-buffer buffer))))
3684
3685 (defun gnus-start-date-timer (&optional n)
3686   "Start a timer to update the Date headers in the article buffers.
3687 The numerical prefix says how frequently (in seconds) the function
3688 is to run."
3689   (interactive "p")
3690   (unless n
3691     (setq n 1))
3692   (gnus-stop-date-timer)
3693   (setq article-lapsed-timer
3694         (run-at-time 1 n 'article-update-date-lapsed)))
3695
3696 (defun gnus-stop-date-timer ()
3697   "Stop the Date timer."
3698   (interactive)
3699   (when article-lapsed-timer
3700     (nnheader-cancel-timer article-lapsed-timer)
3701     (setq article-lapsed-timer nil)))
3702
3703 (defun article-date-user (&optional highlight)
3704   "Convert the current article date to the user-defined format.
3705 This format is defined by the `gnus-article-time-format' variable."
3706   (interactive (list t))
3707   (article-date-ut 'user highlight))
3708
3709 (defun article-date-iso8601 (&optional highlight)
3710   "Convert the current article date to ISO8601."
3711   (interactive (list t))
3712   (article-date-ut 'iso8601 highlight))
3713
3714 (defmacro gnus-article-save-original-date (&rest forms)
3715   "Save the original date as a text property and evaluate FORMS."
3716   `(let* ((case-fold-search t)
3717           (start (progn
3718                    (goto-char (point-min))
3719                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3720                               (not (bolp)))
3721                      (match-end 0))))
3722           (date (when (and start
3723                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3724                                               nil t))
3725                   (buffer-substring-no-properties start
3726                                                   (match-beginning 0)))))
3727      (goto-char (point-max))
3728      (skip-chars-backward "\n")
3729      (put-text-property (point-min) (point) 'original-date date)
3730      ,@forms
3731      (goto-char (point-max))
3732      (skip-chars-backward "\n")
3733      (put-text-property (point-min) (point) 'original-date date)))
3734
3735 ;; (defun article-show-all ()
3736 ;;   "Show all hidden text in the article buffer."
3737 ;;   (interactive)
3738 ;;   (save-excursion
3739 ;;     (let ((inhibit-read-only t))
3740 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3741
3742 (defun article-remove-leading-whitespace ()
3743   "Remove excessive whitespace from all headers."
3744   (interactive)
3745   (save-excursion
3746     (save-restriction
3747       (let ((inhibit-read-only t))
3748         (article-narrow-to-head)
3749         (goto-char (point-min))
3750         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3751           (delete-region (match-beginning 1) (match-end 1)))))))
3752
3753 (defun article-emphasize (&optional arg)
3754   "Emphasize text according to `gnus-emphasis-alist'."
3755   (interactive (gnus-article-hidden-arg))
3756   (unless (gnus-article-check-hidden-text 'emphasis arg)
3757     (save-excursion
3758       (let ((alist (or
3759                     (condition-case nil
3760                         (with-current-buffer gnus-summary-buffer
3761                           gnus-article-emphasis-alist)
3762                       (error))
3763                     gnus-emphasis-alist))
3764             (inhibit-read-only t)
3765             (props (append '(article-type emphasis)
3766                            gnus-hidden-properties))
3767             regexp elem beg invisible visible face)
3768         (article-goto-body)
3769         (setq beg (point))
3770         (while (setq elem (pop alist))
3771           (goto-char beg)
3772           (setq regexp (car elem)
3773                 invisible (nth 1 elem)
3774                 visible (nth 2 elem)
3775                 face (nth 3 elem))
3776           (while (re-search-forward regexp nil t)
3777             (when (and (match-beginning visible) (match-beginning invisible))
3778               (gnus-article-hide-text
3779                (match-beginning invisible) (match-end invisible) props)
3780               (gnus-article-unhide-text-type
3781                (match-beginning visible) (match-end visible) 'emphasis)
3782               (gnus-put-overlay-excluding-newlines
3783                (match-beginning visible) (match-end visible) 'face face)
3784               (gnus-add-wash-type 'emphasis)
3785               (goto-char (match-end invisible)))))))))
3786
3787 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3788   "Setup newsgroup emphasis alist."
3789   (unless gnus-article-emphasis-alist
3790     (let ((name (and gnus-newsgroup-name
3791                      (gnus-group-real-name gnus-newsgroup-name))))
3792       (make-local-variable 'gnus-article-emphasis-alist)
3793       (setq gnus-article-emphasis-alist
3794             (nconc
3795              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3796                (while (setq elem (pop alist))
3797                  (when (and name (string-match (car elem) name))
3798                    (setq alist nil
3799                          highlight (copy-sequence (cdr elem)))))
3800                highlight)
3801              (copy-sequence highlight-words)
3802              (if gnus-newsgroup-name
3803                  (copy-sequence (gnus-group-find-parameter
3804                                  gnus-newsgroup-name 'highlight-words t)))
3805              gnus-emphasis-alist)))))
3806
3807 (defvar gnus-summary-article-menu)
3808 (defvar gnus-summary-post-menu)
3809
3810 ;;; Saving functions.
3811
3812 (defun gnus-article-save (save-buffer file &optional num)
3813   "Save the currently selected article."
3814   (when (or (get gnus-default-article-saver :headers)
3815             (not gnus-save-all-headers))
3816     ;; Remove headers according to `gnus-saved-headers' or the value
3817     ;; of the `:headers' property that the saver function might have.
3818     (let ((gnus-visible-headers
3819            (or (symbol-value (get gnus-default-article-saver :headers))
3820                gnus-saved-headers gnus-visible-headers))
3821           ;; Ignore group parameter.  See `article-hide-headers'.
3822           (gnus-summary-buffer nil))
3823       (with-current-buffer save-buffer
3824         (article-hide-headers 1 t))))
3825   (save-window-excursion
3826     (if (not gnus-default-article-saver)
3827         (error "No default saver is defined")
3828       ;; !!! Magic!  The saving functions all save
3829       ;; `gnus-save-article-buffer' (or so they think), but we
3830       ;; bind that variable to our save-buffer.
3831       (set-buffer gnus-article-buffer)
3832       (let* ((gnus-save-article-buffer save-buffer)
3833              (filename
3834               (cond
3835                ((not gnus-prompt-before-saving) 'default)
3836                ((eq gnus-prompt-before-saving 'always) nil)
3837                (t file)))
3838              (gnus-number-of-articles-to-be-saved
3839               (when (eq gnus-prompt-before-saving t)
3840                 num)))                  ; Magic
3841         (set-buffer gnus-article-current-summary)
3842         (funcall gnus-default-article-saver filename)))))
3843
3844 (defun gnus-read-save-file-name (prompt &optional filename
3845                                         function group headers variable
3846                                         dir-var)
3847   (let ((default-name
3848           (funcall function group headers (symbol-value variable)))
3849         result)
3850     (setq result
3851           (expand-file-name
3852            (cond
3853             ((eq filename 'default)
3854              default-name)
3855             ((eq filename t)
3856              default-name)
3857             (filename filename)
3858             (t
3859              (when (symbol-value dir-var)
3860                (setq default-name (expand-file-name
3861                                    (file-name-nondirectory default-name)
3862                                    (symbol-value dir-var))))
3863              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3864                     (prompt
3865                      (format prompt
3866                              (if (and gnus-number-of-articles-to-be-saved
3867                                       (> gnus-number-of-articles-to-be-saved 1))
3868                                  (format "these %d articles"
3869                                          gnus-number-of-articles-to-be-saved)
3870                                "this article")))
3871                     (file
3872                      ;; Let the split methods have their say.
3873                      (cond
3874                       ;; No split name was found.
3875                       ((null split-name)
3876                        (read-file-name
3877                         (concat prompt " (default "
3878                                 (file-name-nondirectory default-name) "): ")
3879                         (file-name-directory default-name)
3880                         default-name))
3881                       ;; A single group name is returned.
3882                       ((stringp split-name)
3883                        (setq default-name
3884                              (funcall function split-name headers
3885                                       (symbol-value variable)))
3886                        (read-file-name
3887                         (concat prompt " (default "
3888                                 (file-name-nondirectory default-name) "): ")
3889                         (file-name-directory default-name)
3890                         default-name))
3891                       ;; A single split name was found
3892                       ((= 1 (length split-name))
3893                        (let* ((name (expand-file-name
3894                                      (car split-name)
3895                                      gnus-article-save-directory))
3896                               (dir (cond ((file-directory-p name)
3897                                           (file-name-as-directory name))
3898                                          ((file-exists-p name) name)
3899                                          (t gnus-article-save-directory))))
3900                          (read-file-name
3901                           (concat prompt " (default " name "): ")
3902                           dir name)))
3903                       ;; A list of splits was found.
3904                       (t
3905                        (setq split-name (nreverse split-name))
3906                        (let (result)
3907                          (let ((file-name-history
3908                                 (nconc split-name file-name-history)))
3909                            (setq result
3910                                  (expand-file-name
3911                                   (read-file-name
3912                                    (concat prompt " (`M-p' for defaults): ")
3913                                    gnus-article-save-directory
3914                                    (car split-name))
3915                                   gnus-article-save-directory)))
3916                          (car (push result file-name-history)))))))
3917                ;; Create the directory.
3918                (gnus-make-directory (file-name-directory file))
3919                ;; If we have read a directory, we append the default file name.
3920                (when (file-directory-p file)
3921                  (setq file (expand-file-name (file-name-nondirectory
3922                                                default-name)
3923                                               (file-name-as-directory file))))
3924                ;; Possibly translate some characters.
3925                (nnheader-translate-file-chars file))))))
3926     (gnus-make-directory (file-name-directory result))
3927     (when variable
3928       (set variable result))
3929     (when dir-var
3930       (set dir-var (file-name-directory result)))
3931     result))
3932
3933 (defun gnus-article-archive-name (group)
3934   "Return the first instance of an \"Archive-name\" in the current buffer."
3935   (let ((case-fold-search t))
3936     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3937       (nnheader-concat gnus-article-save-directory
3938                        (match-string 1)))))
3939
3940 (defun gnus-article-nndoc-name (group)
3941   "If GROUP is an nndoc group, return the name of the parent group."
3942   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3943     (gnus-group-get-parameter group 'save-article-group)))
3944
3945 (defun gnus-summary-save-in-rmail (&optional filename)
3946   "Append this article to Rmail file.
3947 Optional argument FILENAME specifies file name.
3948 Directory to save to is default to `gnus-article-save-directory'."
3949   (setq filename (gnus-read-save-file-name
3950                   "Save %s in rmail file" filename
3951                   gnus-rmail-save-name gnus-newsgroup-name
3952                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3953   (with-current-buffer gnus-save-article-buffer
3954     (save-excursion
3955       (save-restriction
3956         (widen)
3957         ;; Note that unlike gnus-summary-save-in-mail, there is no
3958         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3959         ;; not use Babyl.
3960         (gnus-output-to-rmail filename))))
3961   filename)
3962
3963 (defun gnus-summary-save-in-mail (&optional filename)
3964   "Append this article to Unix mail file.
3965 Optional argument FILENAME specifies file name.
3966 Directory to save to is default to `gnus-article-save-directory'."
3967   (setq filename (gnus-read-save-file-name
3968                   "Save %s in Unix mail file" filename
3969                   gnus-mail-save-name gnus-newsgroup-name
3970                   gnus-current-headers 'gnus-newsgroup-last-mail))
3971   (with-current-buffer gnus-save-article-buffer
3972     (save-excursion
3973       (save-restriction
3974         (widen)
3975         (if (and (file-readable-p filename)
3976                  (file-regular-p filename)
3977                  (mail-file-babyl-p filename))
3978             (gnus-output-to-rmail filename)
3979           (gnus-output-to-mail filename)))))
3980   filename)
3981
3982 (put 'gnus-summary-save-in-file :decode t)
3983 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3984 (defun gnus-summary-save-in-file (&optional filename overwrite)
3985   "Append this article to file.
3986 Optional argument FILENAME specifies file name.
3987 Directory to save to is default to `gnus-article-save-directory'."
3988   (setq filename (gnus-read-save-file-name
3989                   "Save %s in file" filename
3990                   gnus-file-save-name gnus-newsgroup-name
3991                   gnus-current-headers 'gnus-newsgroup-last-file))
3992   (with-current-buffer gnus-save-article-buffer
3993     (save-excursion
3994       (save-restriction
3995         (widen)
3996         (when (and overwrite
3997                    (file-exists-p filename))
3998           (delete-file filename))
3999         (gnus-output-to-file filename))))
4000   filename)
4001
4002 (put 'gnus-summary-write-to-file :decode t)
4003 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4004 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4005 (defun gnus-summary-write-to-file (&optional filename)
4006   "Write this article to a file, overwriting it if the file exists.
4007 Optional argument FILENAME specifies file name.
4008 The directory to save in defaults to `gnus-article-save-directory'."
4009   (setq filename (gnus-read-save-file-name
4010                   "Save %s in file" filename
4011                   gnus-file-save-name gnus-newsgroup-name
4012                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4013   (gnus-summary-save-in-file filename t))
4014
4015 (put 'gnus-summary-save-body-in-file :decode t)
4016 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4017   "Append this article body to a file.
4018 Optional argument FILENAME specifies file name.
4019 The directory to save in defaults to `gnus-article-save-directory'."
4020   (setq filename (gnus-read-save-file-name
4021                   "Save %s body in file" filename
4022                   gnus-file-save-name gnus-newsgroup-name
4023                   gnus-current-headers 'gnus-newsgroup-last-file))
4024   (with-current-buffer gnus-save-article-buffer
4025     (save-excursion
4026       (save-restriction
4027         (widen)
4028         (when (article-goto-body)
4029           (narrow-to-region (point) (point-max)))
4030         (when (and overwrite
4031                    (file-exists-p filename))
4032           (delete-file filename))
4033         (gnus-output-to-file filename))))
4034   filename)
4035
4036 (put 'gnus-summary-write-body-to-file :decode t)
4037 (put 'gnus-summary-write-body-to-file
4038      :function 'gnus-summary-save-body-in-file)
4039 (defun gnus-summary-write-body-to-file (&optional filename)
4040   "Write this article body to a file, overwriting it if the file exists.
4041 Optional argument FILENAME specifies file name.
4042 The directory to save in defaults to `gnus-article-save-directory'."
4043   (setq filename (gnus-read-save-file-name
4044                   "Save %s body in file" filename
4045                   gnus-file-save-name gnus-newsgroup-name
4046                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4047   (gnus-summary-save-body-in-file filename t))
4048
4049 (put 'gnus-summary-save-in-pipe :decode t)
4050 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4051 (defun gnus-summary-save-in-pipe (&optional command raw)
4052   "Pipe this article to subprocess COMMAND.
4053 Valid values for COMMAND include:
4054   a string
4055     The executable command name and possibly arguments.
4056   nil
4057     You will be prompted for the command in the minibuffer.
4058   the symbol `default'
4059     It will be replaced with the command which the variable
4060     `gnus-summary-pipe-output-default-command' holds or the command
4061     last used for saving.
4062 Non-nil value for RAW overrides `:decode' and `:headers' properties
4063 and the raw article including all headers will be piped."
4064   (let ((article (gnus-summary-article-number))
4065         (decode (unless raw
4066                   (get 'gnus-summary-save-in-pipe :decode)))
4067         save-buffer default)
4068     (if article
4069         (if (vectorp (gnus-summary-article-header article))
4070             (save-current-buffer
4071               (gnus-summary-select-article decode decode nil article)
4072               (insert-buffer-substring
4073                (prog1
4074                    (if decode
4075                        gnus-article-buffer
4076                      gnus-original-article-buffer)
4077                  (setq save-buffer
4078                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4079               ;; Remove unwanted headers.
4080               (when (and (not raw)
4081                          (or (get 'gnus-summary-save-in-pipe :headers)
4082                              (not gnus-save-all-headers)))
4083                 (let ((gnus-visible-headers
4084                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4085                                               :headers))
4086                            gnus-saved-headers gnus-visible-headers))
4087                       (gnus-summary-buffer nil))
4088                   (article-hide-headers 1 t))))
4089           (error "%d is not a real article" article))
4090       (error "No article to pipe"))
4091     (setq default (or gnus-summary-pipe-output-default-command
4092                       gnus-last-shell-command))
4093     (unless (stringp command)
4094       (setq command
4095             (if (and (eq command 'default) default)
4096                 default
4097               (gnus-read-shell-command "Shell command on this article: "
4098                                        default))))
4099     (when (string-equal command "")
4100       (if default
4101           (setq command default)
4102         (error "A command is required")))
4103     (with-current-buffer save-buffer
4104       (save-restriction
4105         (widen)
4106         (shell-command-on-region (point-min) (point-max) command nil)))
4107     (gnus-kill-buffer save-buffer))
4108   (setq gnus-summary-pipe-output-default-command command))
4109
4110 (defun gnus-summary-pipe-to-muttprint (&optional command)
4111   "Pipe this article to muttprint."
4112   (unless (stringp command)
4113     (setq command (read-string
4114                    "Print using command: " gnus-summary-muttprint-program
4115                    nil gnus-summary-muttprint-program)))
4116   (let ((gnus-summary-pipe-output-default-command
4117          gnus-summary-pipe-output-default-command))
4118     (gnus-summary-save-in-pipe command))
4119   (setq gnus-summary-muttprint-program command))
4120
4121 ;;; Article file names when saving.
4122
4123 (defun gnus-capitalize-newsgroup (newsgroup)
4124   "Capitalize NEWSGROUP name."
4125   (when (not (zerop (length newsgroup)))
4126     (concat (char-to-string (upcase (aref newsgroup 0)))
4127             (substring newsgroup 1))))
4128
4129 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4130   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4131 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4132 Otherwise, it is like ~/News/news/group/num."
4133   (let ((default
4134           (expand-file-name
4135            (concat (if (gnus-use-long-file-name 'not-save)
4136                        (gnus-capitalize-newsgroup newsgroup)
4137                      (gnus-newsgroup-directory-form newsgroup))
4138                    "/" (int-to-string (mail-header-number headers)))
4139            gnus-article-save-directory)))
4140     (if (and last-file
4141              (string-equal (file-name-directory default)
4142                            (file-name-directory last-file))
4143              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4144         default
4145       (or last-file default))))
4146
4147 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4148   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4149 If variable `gnus-use-long-file-name' is non-nil, it is
4150 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4151   (let ((default
4152           (expand-file-name
4153            (concat (if (gnus-use-long-file-name 'not-save)
4154                        newsgroup
4155                      (gnus-newsgroup-directory-form newsgroup))
4156                    "/" (int-to-string (mail-header-number headers)))
4157            gnus-article-save-directory)))
4158     (if (and last-file
4159              (string-equal (file-name-directory default)
4160                            (file-name-directory last-file))
4161              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4162         default
4163       (or last-file default))))
4164
4165 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4166   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4167 If variable `gnus-use-long-file-name' is non-nil, it is
4168 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4169   (or last-file
4170       (expand-file-name
4171        (if (gnus-use-long-file-name 'not-save)
4172            newsgroup
4173          (file-relative-name
4174           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4175           default-directory))
4176        gnus-article-save-directory)))
4177
4178 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4179   "Generate file name from sender."
4180   (let ((from (mail-header-from headers)))
4181     (expand-file-name
4182      (if (and from (string-match "\\([^ <]+\\)@" from))
4183          (match-string 1 from)
4184        "nobody")
4185      gnus-article-save-directory)))
4186
4187 (defun article-verify-x-pgp-sig ()
4188   "Verify X-PGP-Sig."
4189   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4190   (interactive)
4191   (if (gnus-buffer-live-p gnus-original-article-buffer)
4192       (let ((sig (with-current-buffer gnus-original-article-buffer
4193                    (gnus-fetch-field "X-PGP-Sig")))
4194             items info headers)
4195         (when (and sig
4196                    mml2015-use
4197                    (mml2015-clear-verify-function))
4198           (with-temp-buffer
4199             (insert-buffer-substring gnus-original-article-buffer)
4200             (setq items (split-string sig))
4201             (message-narrow-to-head)
4202             (let ((inhibit-point-motion-hooks t)
4203                   (case-fold-search t))
4204               ;; Don't verify multiple headers.
4205               (setq headers (mapconcat (lambda (header)
4206                                          (concat header ": "
4207                                                  (mail-fetch-field header)
4208                                                  "\n"))
4209                                        (split-string (nth 1 items) ",") "")))
4210             (delete-region (point-min) (point-max))
4211             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4212             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4213             (insert headers)
4214             (widen)
4215             (forward-line)
4216             (while (not (eobp))
4217               (if (looking-at "^-")
4218                   (insert "- "))
4219               (forward-line))
4220             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4221             (insert "Version: " (car items) "\n\n")
4222             (insert (mapconcat 'identity (cddr items) "\n"))
4223             (insert "\n-----END PGP SIGNATURE-----\n")
4224             (let ((mm-security-handle (list (format "multipart/signed"))))
4225               (mml2015-clean-buffer)
4226               (let ((coding-system-for-write (or gnus-newsgroup-charset
4227                                                  'iso-8859-1)))
4228                 (funcall (mml2015-clear-verify-function)))
4229               (setq info
4230                     (or (mm-handle-multipart-ctl-parameter
4231                          mm-security-handle 'gnus-details)
4232                         (mm-handle-multipart-ctl-parameter
4233                          mm-security-handle 'gnus-info)))))
4234           (when info
4235             (let ((inhibit-read-only t) bface eface)
4236               (save-restriction
4237                 (message-narrow-to-head)
4238                 (goto-char (point-max))
4239                 (forward-line -1)
4240                 (setq bface (get-text-property (point-at-bol) 'face)
4241                       eface (get-text-property (1- (point-at-eol)) 'face))
4242                 (message-remove-header "X-Gnus-PGP-Verify")
4243                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4244                     (forward-line)
4245                   (goto-char (point-max)))
4246                 (narrow-to-region (point) (point))
4247                 (insert "X-Gnus-PGP-Verify: " info "\n")
4248                 (goto-char (point-min))
4249                 (forward-line)
4250                 (while (not (eobp))
4251                   (if (not (looking-at "^[ \t]"))
4252                       (insert " "))
4253                   (forward-line))
4254                 ;; Do highlighting.
4255                 (goto-char (point-min))
4256                 (when (looking-at "\\([^:]+\\): *")
4257                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4258                                      'face bface)
4259                   (put-text-property (match-end 0) (point-max)
4260                                      'face eface)))))))))
4261
4262 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4263
4264 (defun article-verify-cancel-lock ()
4265   "Verify Cancel-Lock header."
4266   (interactive)
4267   (if (gnus-buffer-live-p gnus-original-article-buffer)
4268       (canlock-verify gnus-original-article-buffer)))
4269
4270 (eval-and-compile
4271   (mapc
4272    (lambda (func)
4273      (let (afunc gfunc)
4274        (if (consp func)
4275            (setq afunc (car func)
4276                  gfunc (cdr func))
4277          (setq afunc func
4278                gfunc (intern (format "gnus-%s" func))))
4279        (defalias gfunc
4280          (when (fboundp afunc)
4281            `(lambda (&optional interactive &rest args)
4282               ,(documentation afunc t)
4283               (interactive (list t))
4284               (with-current-buffer gnus-article-buffer
4285                 (if interactive
4286                     (call-interactively ',afunc)
4287                   (apply ',afunc args))))))))
4288    '(article-hide-headers
4289      article-verify-x-pgp-sig
4290      article-verify-cancel-lock
4291      article-hide-boring-headers
4292      article-treat-overstrike
4293      article-treat-ansi-sequences
4294      article-fill-long-lines
4295      article-capitalize-sentences
4296      article-remove-cr
4297      article-remove-leading-whitespace
4298      article-display-x-face
4299      article-display-face
4300      article-de-quoted-unreadable
4301      article-de-base64-unreadable
4302      article-decode-HZ
4303      article-wash-html
4304      article-unsplit-urls
4305      article-hide-list-identifiers
4306      article-strip-banner
4307      article-babel
4308      article-hide-pem
4309      article-hide-signature
4310      article-strip-headers-in-body
4311      article-remove-trailing-blank-lines
4312      article-strip-leading-blank-lines
4313      article-strip-multiple-blank-lines
4314      article-strip-leading-space
4315      article-strip-trailing-space
4316      article-strip-blank-lines
4317      article-strip-all-blank-lines
4318      article-date-local
4319      article-date-english
4320      article-date-iso8601
4321      article-date-original
4322      article-treat-date
4323      article-date-ut
4324      article-decode-mime-words
4325      article-decode-charset
4326      article-decode-encoded-words
4327      article-date-user
4328      article-date-lapsed
4329      article-date-combined-lapsed
4330      article-emphasize
4331      article-treat-dumbquotes
4332      article-treat-non-ascii
4333      article-normalize-headers
4334      ;;(article-show-all . gnus-article-show-all-headers)
4335      )))
4336 \f
4337 ;;;
4338 ;;; Gnus article mode
4339 ;;;
4340
4341 (put 'gnus-article-mode 'mode-class 'special)
4342
4343 (set-keymap-parent gnus-article-mode-map widget-keymap)
4344
4345 (gnus-define-keys gnus-article-mode-map
4346   " " gnus-article-goto-next-page
4347   "\177" gnus-article-goto-prev-page
4348   [delete] gnus-article-goto-prev-page
4349   [backspace] gnus-article-goto-prev-page
4350   "\C-c^" gnus-article-refer-article
4351   "h" gnus-article-show-summary
4352   "s" gnus-article-show-summary
4353   "\C-c\C-m" gnus-article-mail
4354   "?" gnus-article-describe-briefly
4355   "<" beginning-of-buffer
4356   ">" end-of-buffer
4357   "\C-c\C-i" gnus-info-find-node
4358   "\C-c\C-b" gnus-bug
4359   "R" gnus-article-reply-with-original
4360   "F" gnus-article-followup-with-original
4361   "\C-hk" gnus-article-describe-key
4362   "\C-hc" gnus-article-describe-key-briefly
4363   "\C-hb" gnus-article-describe-bindings
4364
4365   "e" gnus-article-read-summary-keys
4366   "\C-d" gnus-article-read-summary-keys
4367   "\M-*" gnus-article-read-summary-keys
4368   "\M-#" gnus-article-read-summary-keys
4369   "\M-^" gnus-article-read-summary-keys
4370   "\M-g" gnus-article-read-summary-keys)
4371
4372 (substitute-key-definition
4373  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4374
4375 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4376   "W" gnus-article-wide-reply-with-original)
4377 (if (featurep 'xemacs)
4378     (set-keymap-default-binding gnus-article-send-map
4379                                 'gnus-article-read-summary-send-keys)
4380   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4381
4382 (defun gnus-article-make-menu-bar ()
4383   (unless (boundp 'gnus-article-commands-menu)
4384     (gnus-summary-make-menu-bar))
4385   (unless (boundp 'gnus-article-article-menu)
4386     (easy-menu-define
4387      gnus-article-article-menu gnus-article-mode-map ""
4388      '("Article"
4389        ["Scroll forwards" gnus-article-goto-next-page t]
4390        ["Scroll backwards" gnus-article-goto-prev-page t]
4391        ["Show summary" gnus-article-show-summary t]
4392        ["Fetch Message-ID at point" gnus-article-refer-article t]
4393        ["Mail to address at point" gnus-article-mail t]
4394        ["Send a bug report" gnus-bug t]))
4395
4396     (easy-menu-define
4397      gnus-article-treatment-menu gnus-article-mode-map ""
4398      ;; Fixme: this should use :active (and maybe :visible).
4399      '("Treatment"
4400        ["Hide headers" gnus-article-hide-headers t]
4401        ["Hide signature" gnus-article-hide-signature t]
4402        ["Hide citation" gnus-article-hide-citation t]
4403        ["Treat overstrike" gnus-article-treat-overstrike t]
4404        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4405        ["Remove carriage return" gnus-article-remove-cr t]
4406        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4407        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4408        ["Remove base64" gnus-article-de-base64-unreadable t]
4409        ["Treat html" gnus-article-wash-html t]
4410        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4411        ["Decode HZ" gnus-article-decode-HZ t]))
4412
4413     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4414
4415     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4416
4417     (gnus-run-hooks 'gnus-article-menu-hook)))
4418
4419 (defvar bookmark-make-record-function)
4420 (defvar shr-put-image-function)
4421
4422 (defun gnus-article-mode ()
4423   "Major mode for displaying an article.
4424
4425 All normal editing commands are switched off.
4426
4427 The following commands are available in addition to all summary mode
4428 commands:
4429 \\<gnus-article-mode-map>
4430 \\[gnus-article-next-page]\t Scroll the article one page forwards
4431 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4432 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4433 \\[gnus-article-show-summary]\t Display the summary buffer
4434 \\[gnus-article-mail]\t Send a reply to the address near point
4435 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4436 \\[gnus-info-find-node]\t Go to the Gnus info node"
4437   (interactive)
4438   (kill-all-local-variables)
4439   (gnus-simplify-mode-line)
4440   (setq mode-name "Article")
4441   (setq major-mode 'gnus-article-mode)
4442   (make-local-variable 'minor-mode-alist)
4443   (use-local-map gnus-article-mode-map)
4444   (when (gnus-visual-p 'article-menu 'menu)
4445     (gnus-article-make-menu-bar)
4446     (when gnus-summary-tool-bar-map
4447       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4448   (gnus-update-format-specifications nil 'article-mode)
4449   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4450   (set (make-local-variable 'gnus-page-broken) nil)
4451   (make-local-variable 'gnus-article-current-summary)
4452   (make-local-variable 'gnus-article-mime-handles)
4453   (make-local-variable 'gnus-article-decoded-p)
4454   (make-local-variable 'gnus-article-mime-handle-alist)
4455   (make-local-variable 'gnus-article-wash-types)
4456   (make-local-variable 'gnus-article-image-alist)
4457   (make-local-variable 'gnus-article-charset)
4458   (make-local-variable 'gnus-article-ignored-charsets)
4459   (set (make-local-variable 'bookmark-make-record-function)
4460        'gnus-summary-bookmark-make-record)
4461   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4462   ;; face.
4463   (set (make-local-variable 'nobreak-char-display) nil)
4464   ;; Enable `gnus-article-remove-images' to delete images shr.el renders.
4465   (set (make-local-variable 'shr-put-image-function) 'gnus-shr-put-image)
4466   (setq cursor-in-non-selected-windows nil)
4467   (gnus-set-default-directory)
4468   (buffer-disable-undo)
4469   (setq buffer-read-only t
4470         show-trailing-whitespace nil)
4471   (set-syntax-table gnus-article-mode-syntax-table)
4472   (mm-enable-multibyte)
4473   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4474
4475 (defun gnus-article-setup-buffer ()
4476   "Initialize the article buffer."
4477   (let* ((name (if gnus-single-article-buffer "*Article*"
4478                  (concat "*Article " gnus-newsgroup-name "*")))
4479          (original
4480           (progn (string-match "\\*Article" name)
4481                  (concat " *Original Article"
4482                          (substring name (match-end 0))))))
4483     (setq gnus-article-buffer name)
4484     (setq gnus-original-article-buffer original)
4485     (setq gnus-article-mime-handle-alist nil)
4486     (with-current-buffer gnus-summary-buffer
4487       ;; This might be a variable local to the summary buffer.
4488       (unless gnus-single-article-buffer
4489         (setq gnus-article-buffer name)
4490         (setq gnus-original-article-buffer original)
4491         (gnus-set-global-variables)))
4492     (gnus-article-setup-highlight-words)
4493     ;; Init original article buffer.
4494     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4495       (mm-enable-multibyte)
4496       (setq major-mode 'gnus-original-article-mode)
4497       (make-local-variable 'gnus-original-article))
4498     (if (and (get-buffer name)
4499              (with-current-buffer name
4500                (if gnus-article-edit-mode
4501                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4502                        (progn
4503                          (set-buffer-modified-p nil)
4504                          (gnus-kill-buffer name)
4505                          (message "")
4506                          nil)
4507                      (error "Action aborted"))
4508                  t)))
4509         (with-current-buffer name
4510           (set (make-local-variable 'gnus-article-edit-mode) nil)
4511           (gnus-article-stop-animations)
4512           (when gnus-article-mime-handles
4513             (mm-destroy-parts gnus-article-mime-handles)
4514             (setq gnus-article-mime-handles nil))
4515           ;; Set it to nil in article-buffer!
4516           (setq gnus-article-mime-handle-alist nil)
4517           (buffer-disable-undo)
4518           (setq buffer-read-only t)
4519           (unless (eq major-mode 'gnus-article-mode)
4520             (gnus-article-mode))
4521           (setq truncate-lines gnus-article-truncate-lines)
4522           (current-buffer))
4523       (with-current-buffer (gnus-get-buffer-create name)
4524         (gnus-article-mode)
4525         (setq truncate-lines gnus-article-truncate-lines)
4526         (make-local-variable 'gnus-summary-buffer)
4527         (setq gnus-summary-buffer
4528               (gnus-summary-buffer-name gnus-newsgroup-name))
4529         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4530         (when article-lapsed-timer
4531           (gnus-stop-date-timer))
4532         (when gnus-article-update-date-headers
4533           (gnus-start-date-timer gnus-article-update-date-headers))
4534         (current-buffer)))))
4535
4536 (defun gnus-article-stop-animations ()
4537   (dolist (timer (and (boundp 'timer-list)
4538                       timer-list))
4539     (when (eq (aref timer 5) 'image-animate-timeout)
4540       (cancel-timer timer))))
4541
4542 ;; Set article window start at LINE, where LINE is the number of lines
4543 ;; from the head of the article.
4544 (defun gnus-article-set-window-start (&optional line)
4545   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4546     (when article-window
4547       (set-window-start
4548        article-window
4549        (with-current-buffer gnus-article-buffer
4550          (goto-char (point-min))
4551          (if (not line)
4552              (point-min)
4553            (gnus-message 6 "Moved to bookmark")
4554            (search-forward "\n\n" nil t)
4555            (forward-line line)
4556            (point)))))))
4557
4558 (defun gnus-article-prepare (article &optional all-headers header)
4559   "Prepare ARTICLE in article mode buffer.
4560 ARTICLE should either be an article number or a Message-ID.
4561 If ARTICLE is an id, HEADER should be the article headers.
4562 If ALL-HEADERS is non-nil, no headers are hidden."
4563   (save-excursion
4564     ;; Make sure we start in a summary buffer.
4565     (unless (eq major-mode 'gnus-summary-mode)
4566       (set-buffer gnus-summary-buffer))
4567     (setq gnus-summary-buffer (current-buffer))
4568     (let* ((gnus-article (if header (mail-header-number header) article))
4569            (summary-buffer (current-buffer))
4570            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4571            (group gnus-newsgroup-name)
4572            result)
4573       (save-excursion
4574         (gnus-article-setup-buffer)
4575         (set-buffer gnus-article-buffer)
4576         ;; Deactivate active regions.
4577         (when (and (boundp 'transient-mark-mode)
4578                    transient-mark-mode)
4579           (setq mark-active nil))
4580         (if (not (setq result (let ((inhibit-read-only t))
4581                                 (gnus-request-article-this-buffer
4582                                  article group))))
4583             ;; There is no such article.
4584             (save-excursion
4585               (when (and (numberp article)
4586                          (not (memq article gnus-newsgroup-sparse)))
4587                 (setq gnus-article-current
4588                       (cons gnus-newsgroup-name article))
4589                 (set-buffer gnus-summary-buffer)
4590                 (setq gnus-current-article article)
4591                 (if (and (memq article gnus-newsgroup-undownloaded)
4592                          (not (gnus-online (gnus-find-method-for-group
4593                                             gnus-newsgroup-name))))
4594                     (progn
4595                       (gnus-summary-set-agent-mark article)
4596                       (message "Message marked for downloading"))
4597                   (gnus-summary-mark-article article gnus-canceled-mark)
4598                   (unless (memq article gnus-newsgroup-sparse)
4599                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4600           (if (or (eq result 'pseudo)
4601                   (eq result 'nneething))
4602               (progn
4603                 (with-current-buffer summary-buffer
4604                   (push article gnus-newsgroup-history)
4605                   (setq gnus-last-article gnus-current-article
4606                         gnus-current-article 0
4607                         gnus-current-headers nil
4608                         gnus-article-current nil)
4609                   (if (eq result 'nneething)
4610                       (gnus-configure-windows 'summary)
4611                     (gnus-configure-windows 'article))
4612                   (gnus-set-global-variables))
4613                 (let ((gnus-article-mime-handle-alist-1
4614                        gnus-article-mime-handle-alist))
4615                   (gnus-set-mode-line 'article)))
4616             ;; The result from the `request' was an actual article -
4617             ;; or at least some text that is now displayed in the
4618             ;; article buffer.
4619             (when (and (numberp article)
4620                        (not (eq article gnus-current-article)))
4621               ;; Seems like a new article has been selected.
4622               ;; `gnus-current-article' must be an article number.
4623               (with-current-buffer summary-buffer
4624                 (push article gnus-newsgroup-history)
4625                 (setq gnus-last-article gnus-current-article
4626                       gnus-current-article article
4627                       gnus-current-headers
4628                       (gnus-summary-article-header gnus-current-article)
4629                       gnus-article-current
4630                       (cons gnus-newsgroup-name gnus-current-article))
4631                 (unless (vectorp gnus-current-headers)
4632                   (setq gnus-current-headers nil))
4633                 (gnus-summary-goto-subject gnus-current-article)
4634                 (when (gnus-summary-show-thread)
4635                   ;; If the summary buffer really was folded, the
4636                   ;; previous goto may not actually have gone to
4637                   ;; the right article, but the thread root instead.
4638                   ;; So we go again.
4639                   (gnus-summary-goto-subject gnus-current-article))
4640                 (gnus-run-hooks 'gnus-mark-article-hook)
4641                 (gnus-set-mode-line 'summary)
4642                 (when (gnus-visual-p 'article-highlight 'highlight)
4643                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4644                 ;; Set the global newsgroup variables here.
4645                 (gnus-set-global-variables)
4646                 (setq gnus-have-all-headers
4647                       (or all-headers gnus-show-all-headers))))
4648             (save-excursion
4649               (gnus-configure-windows 'article))
4650             (when (or (numberp article)
4651                       (stringp article))
4652               (gnus-article-prepare-display)
4653               ;; Do page break.
4654               (goto-char (point-min))
4655               (when gnus-break-pages
4656                 (gnus-narrow-to-page)))
4657             (let ((gnus-article-mime-handle-alist-1
4658                    gnus-article-mime-handle-alist))
4659               (gnus-set-mode-line 'article))
4660             (article-goto-body)
4661             (unless (bobp)
4662               (forward-line -1))
4663             (set-window-point (get-buffer-window (current-buffer)) (point))
4664             (gnus-configure-windows 'article)
4665             (gnus-run-hooks 'gnus-article-prepare-hook)
4666             t))))))
4667
4668 ;;;###autoload
4669 (defun gnus-article-prepare-display ()
4670   "Make the current buffer look like a nice article."
4671   ;; Hooks for getting information from the article.
4672   ;; This hook must be called before being narrowed.
4673   (let ((gnus-article-buffer (current-buffer))
4674         buffer-read-only
4675         (inhibit-read-only t))
4676     (unless (eq major-mode 'gnus-article-mode)
4677       (gnus-article-mode))
4678     (setq buffer-read-only nil
4679           gnus-article-wash-types nil
4680           gnus-article-image-alist nil)
4681     (gnus-run-hooks 'gnus-tmp-internal-hook)
4682     (when gnus-display-mime-function
4683       (funcall gnus-display-mime-function))))
4684
4685 ;;;
4686 ;;; Gnus Sticky Article Mode
4687 ;;;
4688
4689 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4690   "Mode for sticky articles."
4691   ;; Release bindings that won't work.
4692   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4693                              gnus-sticky-article-mode-map)
4694   (substitute-key-definition 'gnus-article-refer-article 'undefined
4695                              gnus-sticky-article-mode-map)
4696   (dolist (k '("e" "h" "s" "F" "R"))
4697     (define-key gnus-sticky-article-mode-map k nil))
4698   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4699   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4700   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4701   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4702
4703 (defun gnus-sticky-article (arg)
4704   "Make the current article sticky.
4705 If a prefix ARG is given, ask for a name for this sticky article buffer."
4706   (interactive "P")
4707   (gnus-summary-show-thread)
4708   (gnus-summary-select-article nil nil 'pseudo)
4709   (let (new-art-buf-name)
4710     (gnus-eval-in-buffer-window gnus-article-buffer
4711       (setq new-art-buf-name
4712             (concat
4713              "*Sticky Article: "
4714              (if arg
4715                  (read-from-minibuffer "Sticky article buffer name: ")
4716                (gnus-with-article-headers
4717                  (gnus-article-goto-header "subject")
4718                  (setq new-art-buf-name
4719                        (buffer-substring-no-properties
4720                         (line-beginning-position) (line-end-position)))
4721                  (goto-char (point-min))
4722                  (gnus-article-goto-header "from")
4723                  (setq new-art-buf-name
4724                        (concat
4725                         new-art-buf-name ", "
4726                         (buffer-substring-no-properties
4727                          (line-beginning-position) (line-end-position))))
4728                  (goto-char (point-min))
4729                  (gnus-article-goto-header "date")
4730                  (setq new-art-buf-name
4731                        (concat
4732                         new-art-buf-name ", "
4733                         (buffer-substring-no-properties
4734                          (line-beginning-position) (line-end-position))))))
4735              "*"))
4736       (if (and (gnus-buffer-live-p new-art-buf-name)
4737                (with-current-buffer new-art-buf-name
4738                  (eq major-mode 'gnus-sticky-article-mode)))
4739           (switch-to-buffer new-art-buf-name)
4740         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4741       (gnus-sticky-article-mode))
4742     (setq gnus-article-buffer new-art-buf-name))
4743   (gnus-summary-recenter)
4744   (gnus-summary-position-point))
4745
4746 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4747   "Kill the given sticky article BUFFER.
4748 If none is given, assume the current buffer and kill it if it has
4749 `gnus-sticky-article-mode'."
4750   (interactive)
4751   (unless buffer
4752     (setq buffer (current-buffer)))
4753   (with-current-buffer buffer
4754     (when (eq major-mode 'gnus-sticky-article-mode)
4755       (gnus-kill-buffer buffer))))
4756
4757 (defun gnus-kill-sticky-article-buffers (arg)
4758   "Kill all sticky article buffers.
4759 If a prefix ARG is given, ask for confirmation."
4760   (interactive "P")
4761   (dolist (buf (gnus-buffers))
4762     (with-current-buffer buf
4763       (when (eq major-mode 'gnus-sticky-article-mode)
4764         (if (not arg)
4765             (gnus-kill-buffer buf)
4766           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4767             (gnus-kill-buffer buf)))))))
4768
4769 ;;;
4770 ;;; Gnus MIME viewing functions
4771 ;;;
4772
4773 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4774   "Format of the MIME buttons.
4775
4776 Valid specifiers include:
4777 %t  The MIME type
4778 %T  MIME type, along with additional info
4779 %n  The `name' parameter
4780 %d  The description, if any
4781 %l  The length of the encoded part
4782 %p  The part identifier number
4783 %e  Dots if the part isn't displayed
4784
4785 General format specifiers can also be used.  See Info node
4786 `(gnus)Formatting Variables'.")
4787
4788 (defvar gnus-mime-button-line-format-alist
4789   '((?t gnus-tmp-type ?s)
4790     (?T gnus-tmp-type-long ?s)
4791     (?n gnus-tmp-name ?s)
4792     (?d gnus-tmp-description ?s)
4793     (?p gnus-tmp-id ?s)
4794     (?l gnus-tmp-length ?d)
4795     (?e gnus-tmp-dots ?s)))
4796
4797 (defvar gnus-mime-button-commands
4798   '((gnus-article-press-button "\r" "Toggle Display")
4799     (gnus-mime-view-part "v" "View Interactively...")
4800     (gnus-mime-view-part-as-type "t" "View As Type...")
4801     (gnus-mime-view-part-as-charset "C" "View As charset...")
4802     (gnus-mime-save-part "o" "Save...")
4803     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4804     (gnus-mime-replace-part "r" "Replace part")
4805     (gnus-mime-delete-part "d" "Delete part")
4806     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4807     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4808     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4809     (gnus-mime-view-part-externally "e" "View Externally")
4810     (gnus-mime-print-part "p" "Print")
4811     (gnus-mime-pipe-part "|" "Pipe To Command...")
4812     (gnus-mime-action-on-part "." "Take action on the part...")))
4813
4814 (defun gnus-article-mime-part-status ()
4815   (if gnus-article-mime-handle-alist-1
4816       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4817           " (1 part)"
4818         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4819     ""))
4820
4821 (defvar gnus-mime-button-map
4822   (let ((map (make-sparse-keymap)))
4823     (define-key map gnus-mouse-2 'gnus-article-push-button)
4824     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4825     (dolist (c gnus-mime-button-commands)
4826       (define-key map (cadr c) (car c)))
4827     map))
4828
4829 (easy-menu-define
4830   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4831   `("MIME Part"
4832     ,@(mapcar (lambda (c)
4833                 (vector (caddr c) (car c) :active t))
4834               gnus-mime-button-commands)))
4835
4836 (defvar gnus-url-button-commands
4837   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4838
4839 (defvar gnus-url-button-map
4840   (let ((map (make-sparse-keymap)))
4841     (dolist (c gnus-url-button-commands)
4842       (define-key map (cadr c) (car c)))
4843     map))
4844
4845 (easy-menu-define
4846   gnus-url-button-menu gnus-url-button-map "URL button menu."
4847   `("Url Button"
4848     ,@(mapcar (lambda (c)
4849                 (vector (caddr c) (car c) :active t))
4850               gnus-url-button-commands)))
4851
4852 (defmacro gnus-bind-safe-url-regexp (&rest body)
4853   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4854   `(let ((mm-w3m-safe-url-regexp
4855           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4856                                 (gnus-buffer-live-p
4857                                  gnus-article-current-summary))
4858                            (with-current-buffer gnus-article-current-summary
4859                              gnus-newsgroup-name)
4860                          gnus-newsgroup-name)))
4861             (if (cond ((not group)
4862                        ;; Maybe we're in a mml-preview buffer
4863                        ;; and no group is selected.
4864                        t)
4865                       ((stringp gnus-safe-html-newsgroups)
4866                        (string-match gnus-safe-html-newsgroups group))
4867                       ((consp gnus-safe-html-newsgroups)
4868                        (member group gnus-safe-html-newsgroups)))
4869                 nil
4870               mm-w3m-safe-url-regexp))))
4871      ,@body))
4872
4873 (defun gnus-mime-button-menu (event prefix)
4874  "Construct a context-sensitive menu of MIME commands."
4875  (interactive "e\nP")
4876  (save-window-excursion
4877    (let ((pos (event-start event)))
4878      (select-window (posn-window pos))
4879      (goto-char (posn-point pos))
4880      (gnus-article-check-buffer)
4881      (popup-menu gnus-mime-button-menu nil prefix))))
4882
4883 (defun gnus-mime-view-all-parts (&optional handles)
4884   "View all the MIME parts."
4885   (interactive)
4886   (with-current-buffer gnus-article-buffer
4887     (let ((handles (or handles gnus-article-mime-handles))
4888           (mail-parse-charset gnus-newsgroup-charset)
4889           (mail-parse-ignored-charsets
4890            (with-current-buffer gnus-summary-buffer
4891              gnus-newsgroup-ignored-charsets)))
4892       (when handles
4893         (mm-remove-parts handles)
4894         (goto-char (point-min))
4895         (or (search-forward "\n\n") (goto-char (point-max)))
4896         (let ((inhibit-read-only t))
4897           (delete-region (point) (point-max))
4898           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4899
4900 (defun gnus-article-jump-to-part (n)
4901   "Jump to MIME part N."
4902   (interactive "P")
4903   (let ((parts (with-current-buffer gnus-article-buffer
4904                  (length gnus-article-mime-handle-alist))))
4905     (when (zerop parts)
4906       (error "No such part"))
4907     (pop-to-buffer gnus-article-buffer)
4908     (or n
4909         (setq n (if (= parts 1)
4910                     1
4911                   (read-number (format "Jump to part (1..%s): " parts)))))
4912     (unless (and (integerp n) (<= n parts) (>= n 1))
4913       (setq n
4914             (progn
4915               (gnus-message 7 "Invalid part `%s', using %s instead."
4916                             n parts)
4917               parts)))
4918     (gnus-message 9 "Jumping to part %s." n)
4919     (cond ((>= gnus-auto-select-part 1)
4920            (while (and (<= n parts)
4921                        (not (gnus-article-goto-part n)))
4922              (setq n (1+ n))))
4923           ((< gnus-auto-select-part 0)
4924            (while (and (>= n 1)
4925                        (not (gnus-article-goto-part n)))
4926              (setq n (1- n))))
4927           (t
4928            (gnus-article-goto-part n)))))
4929
4930 (defvar gnus-mime-buttonized-part-id nil
4931   "ID of a mime part that should be buttonized.
4932 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4933
4934 (eval-when-compile
4935   (defsubst gnus-article-edit-part (handles &optional current-id)
4936     "Edit an article in order to delete a mime part.
4937 This function is exclusively used by `gnus-mime-save-part-and-strip'
4938 and `gnus-mime-delete-part', and not provided at run-time normally."
4939     (gnus-article-edit-article
4940      `(lambda ()
4941         (buffer-disable-undo)
4942         (erase-buffer)
4943         (let ((mail-parse-charset (or gnus-article-charset
4944                                       ',gnus-newsgroup-charset))
4945               (mail-parse-ignored-charsets
4946                (or gnus-article-ignored-charsets
4947                    ',gnus-newsgroup-ignored-charsets))
4948               (mbl mml-buffer-list))
4949           (setq mml-buffer-list nil)
4950           (insert-buffer-substring gnus-original-article-buffer)
4951           (mime-to-mml ',handles)
4952           (setq gnus-article-mime-handles nil)
4953           (let ((mbl1 mml-buffer-list))
4954             (setq mml-buffer-list mbl)
4955             (set (make-local-variable 'mml-buffer-list) mbl1))
4956           (gnus-make-local-hook 'kill-buffer-hook)
4957           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4958      `(lambda (no-highlight)
4959         (let ((mail-parse-charset (or gnus-article-charset
4960                                       ',gnus-newsgroup-charset))
4961               (message-options message-options)
4962               (message-options-set-recipient)
4963               (mail-parse-ignored-charsets
4964                (or gnus-article-ignored-charsets
4965                    ',gnus-newsgroup-ignored-charsets)))
4966           (mml-to-mime)
4967           (mml-destroy-buffers)
4968           (remove-hook 'kill-buffer-hook
4969                        'mml-destroy-buffers t)
4970           (kill-local-variable 'mml-buffer-list))
4971         (gnus-summary-edit-article-done
4972          ,(or (mail-header-references gnus-current-headers) "")
4973          ,(gnus-group-read-only-p)
4974          ,gnus-summary-buffer no-highlight))
4975      t)
4976     ;; Force buttonizing this part.
4977     (let ((gnus-mime-buttonized-part-id current-id))
4978       (gnus-article-edit-done))
4979     (gnus-configure-windows 'article)
4980     (when (and current-id (integerp gnus-auto-select-part))
4981       (gnus-article-jump-to-part
4982        (min (max (+ current-id gnus-auto-select-part) 1)
4983             (with-current-buffer gnus-article-buffer
4984               (length gnus-article-mime-handle-alist)))))))
4985
4986 (defun gnus-mime-replace-part (file)
4987   "Replace MIME part under point with an external body."
4988   ;; Useful if file has already been saved to disk
4989   (interactive
4990    (list
4991     (read-file-name "Replace MIME part with file: "
4992                     (or mm-default-directory default-directory)
4993                     nil nil)))
4994   (gnus-mime-save-part-and-strip file))
4995
4996 (defun gnus-mime-save-part-and-strip (&optional file)
4997   "Save the MIME part under point then replace it with an external body.
4998 If FILE is given, use it for the external part."
4999   (interactive)
5000   (gnus-article-check-buffer)
5001   (when (gnus-group-read-only-p)
5002     (error "The current group does not support deleting of parts"))
5003   (when (mm-complicated-handles gnus-article-mime-handles)
5004     (error "\
5005 The current article has a complicated MIME structure, giving up..."))
5006   (let* ((data (get-text-property (point) 'gnus-data))
5007          (id (get-text-property (point) 'gnus-part))
5008          param
5009          (handles gnus-article-mime-handles))
5010     (unless file
5011       (setq file
5012             (and data (mm-save-part data "Delete MIME part and save to: "))))
5013     (when file
5014       (with-current-buffer (mm-handle-buffer data)
5015         (erase-buffer)
5016         (insert "Content-Type: " (mm-handle-media-type data))
5017         (mml-insert-parameter-string (cdr (mm-handle-type data))
5018                                      '(charset))
5019         ;; Add a filename for the sake of saving the part again.
5020         (mml-insert-parameter
5021          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5022         (insert "\n")
5023         (insert "Content-ID: " (message-make-message-id) "\n")
5024         (insert "Content-Transfer-Encoding: binary\n")
5025         (insert "\n"))
5026       (setcdr data
5027               (cdr (mm-make-handle nil
5028                                    `("message/external-body"
5029                                      (access-type . "LOCAL-FILE")
5030                                      (name . ,file)))))
5031       ;; (set-buffer gnus-summary-buffer)
5032       (gnus-article-edit-part handles id))))
5033
5034 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5035 ;; parts...>') but with stripping would be nice.
5036
5037 (defun gnus-mime-delete-part ()
5038   "Delete the MIME part under point.
5039 Replace it with some information about the removed part."
5040   (interactive)
5041   (gnus-article-check-buffer)
5042   (when (gnus-group-read-only-p)
5043     (error "The current group does not support deleting of parts"))
5044   (when (mm-complicated-handles gnus-article-mime-handles)
5045     (error "\
5046 The current article has a complicated MIME structure, giving up..."))
5047   (when (or gnus-expert-user
5048             (gnus-yes-or-no-p "\
5049 Deleting parts may malfunction or destroy the article; continue? "))
5050     (let* ((data (get-text-property (point) 'gnus-data))
5051            (id (get-text-property (point) 'gnus-part))
5052            (handles gnus-article-mime-handles)
5053            (description
5054             (let ((desc (mm-handle-description data)))
5055               (when desc
5056                 (mail-decode-encoded-word-string desc))))
5057            (filename (or (mm-handle-filename data) "(none)"))
5058            (type (mm-handle-media-type data)))
5059       (unless data
5060         (error "No MIME part under point"))
5061       (with-current-buffer (mm-handle-buffer data)
5062         (let ((bsize (buffer-size)))
5063           (erase-buffer)
5064           (insert
5065            (concat
5066             ",----\n"
5067             "| The following attachment has been deleted:\n"
5068             "|\n"
5069             "| Type:           " type "\n"
5070             "| Filename:       " filename "\n"
5071             "| Size (encoded): " (format "%s byte%s\n"
5072                                          bsize (if (= bsize 1)
5073                                                    ""
5074                                                  "s"))
5075             (when description
5076               (concat    "| Description:    " description "\n"))
5077             "`----\n"))
5078           (setcdr data
5079                   (cdr (mm-make-handle
5080                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5081                         (list "attachment")
5082                         (format "Deleted attachment (%s bytes)" bsize))))))
5083       ;; (set-buffer gnus-summary-buffer)
5084       (gnus-article-edit-part handles id))))
5085
5086 (defun gnus-mime-save-part ()
5087   "Save the MIME part under point."
5088   (interactive)
5089   (gnus-article-check-buffer)
5090   (let ((data (get-text-property (point) 'gnus-data)))
5091     (when data
5092       (mm-save-part data))))
5093
5094 (defun gnus-mime-pipe-part (&optional cmd)
5095   "Pipe the MIME part under point to a process.
5096 Use CMD as the process."
5097   (interactive)
5098   (gnus-article-check-buffer)
5099   (let ((data (get-text-property (point) 'gnus-data)))
5100     (when data
5101       (mm-pipe-part data cmd))))
5102
5103 (defun gnus-mime-view-part ()
5104   "Interactively choose a viewing method for the MIME part under point."
5105   (interactive)
5106   (gnus-article-check-buffer)
5107   (let ((data (get-text-property (point) 'gnus-data)))
5108     (when data
5109       (setq gnus-article-mime-handles
5110             (mm-merge-handles
5111              gnus-article-mime-handles (setq data (copy-sequence data))))
5112       (mm-interactively-view-part data))))
5113
5114 (defun gnus-mime-view-part-as-type-internal ()
5115   (gnus-article-check-buffer)
5116   (let* ((handle (get-text-property (point) 'gnus-data))
5117          (name (or
5118                 ;; Content-Type: foo/bar; name=...
5119                 (mail-content-type-get (mm-handle-type handle) 'name)
5120                 ;; Content-Disposition: attachment; filename=...
5121                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5122          (def-type (and name (mm-default-file-encoding name))))
5123     (or (and def-type (cons def-type 0))
5124         (and handle
5125              (equal (mm-handle-media-supertype handle) "text")
5126              '("text/plain" . 0))
5127         '("application/octet-stream" . 0))))
5128
5129 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5130   "Choose a MIME media type, and view the part as such.
5131 If non-nil, PRED is a predicate to use during completion to limit the
5132 available media-types."
5133   (interactive)
5134   (unless mime-type
5135     (setq mime-type
5136           (let ((default (gnus-mime-view-part-as-type-internal)))
5137             (gnus-completing-read
5138              "View as MIME type"
5139              (if pred
5140                  (gnus-remove-if-not pred (mailcap-mime-types))
5141                (mailcap-mime-types))
5142              nil nil nil
5143              (car default)))))
5144   (gnus-article-check-buffer)
5145   (let ((handle (get-text-property (point) 'gnus-data)))
5146     (when handle
5147       (when (equal (mm-handle-media-type handle) "message/external-body")
5148         (unless (mm-handle-cache handle)
5149           (mm-extern-cache-contents handle))
5150         (setq handle (mm-handle-cache handle)))
5151       (setq handle
5152             (mm-make-handle (mm-handle-buffer handle)
5153                             (cons mime-type (cdr (mm-handle-type handle)))
5154                             (mm-handle-encoding handle)
5155                             (mm-handle-undisplayer handle)
5156                             (mm-handle-disposition handle)
5157                             (mm-handle-description handle)
5158                             nil
5159                             (mm-handle-id handle)))
5160       (setq gnus-article-mime-handles
5161             (mm-merge-handles gnus-article-mime-handles handle))
5162       (when (mm-handle-displayed-p handle)
5163         (mm-remove-part handle))
5164       (gnus-mm-display-part handle))))
5165
5166 (defun gnus-mime-copy-part (&optional handle arg)
5167   "Put the MIME part under point into a new buffer.
5168 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5169 are decompressed."
5170   (interactive (list nil current-prefix-arg))
5171   (gnus-article-check-buffer)
5172   (unless handle
5173     (setq handle (get-text-property (point) 'gnus-data)))
5174   (when handle
5175     (let ((filename (mm-handle-filename handle))
5176           contents dont-decode charset coding-system)
5177       (mm-with-unibyte-buffer
5178         (mm-insert-part handle)
5179         (setq contents (or (condition-case nil
5180                                (mm-decompress-buffer filename nil 'sig)
5181                              (error
5182                               (setq dont-decode t)
5183                               nil))
5184                            (buffer-string))))
5185       (setq filename (cond (filename (file-name-nondirectory filename))
5186                            (dont-decode "*raw data*")
5187                            (t "*decoded*")))
5188       (cond
5189        (dont-decode)
5190        ((not arg)
5191         (unless (setq charset (mail-content-type-get
5192                                (mm-handle-type handle) 'charset))
5193           (unless (setq coding-system (mm-with-unibyte-buffer
5194                                         (insert contents)
5195                                         (mm-find-buffer-file-coding-system)))
5196             (setq charset gnus-newsgroup-charset))))
5197        ((numberp arg)
5198         (setq charset (or (cdr (assq arg
5199                                      gnus-summary-show-article-charset-alist))
5200                           (mm-read-coding-system "Charset: ")))))
5201       (switch-to-buffer (generate-new-buffer filename))
5202       (if (or coding-system
5203               (and charset
5204                    (setq coding-system (mm-charset-to-coding-system charset))
5205                    (not (eq coding-system 'ascii))))
5206           (progn
5207             (mm-enable-multibyte)
5208             (insert (mm-decode-coding-string contents coding-system))
5209             (setq buffer-file-coding-system
5210                   (if (boundp 'last-coding-system-used)
5211                       (symbol-value 'last-coding-system-used)
5212                     coding-system)))
5213         (mm-disable-multibyte)
5214         (insert contents)
5215         (setq buffer-file-coding-system mm-binary-coding-system))
5216       ;; We do it this way to make `normal-mode' set the appropriate mode.
5217       (unwind-protect
5218           (progn
5219             (setq buffer-file-name (expand-file-name filename))
5220             (normal-mode))
5221         (setq buffer-file-name nil))
5222       (goto-char (point-min)))))
5223
5224 (defun gnus-mime-print-part (&optional handle filename)
5225   "Print the MIME part under point."
5226   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5227   (gnus-article-check-buffer)
5228   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5229          (contents (and handle (mm-get-part handle)))
5230          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5231          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5232     (when contents
5233         (if printer
5234             (unwind-protect
5235                 (progn
5236                   (mm-save-part-to-file handle file)
5237                   (call-process shell-file-name nil
5238                                 (generate-new-buffer " *mm*")
5239                                 nil
5240                                 shell-command-switch
5241                                 (mm-mailcap-command
5242                                  printer file (mm-handle-type handle))))
5243               (delete-file file))
5244           (with-temp-buffer
5245             (insert contents)
5246             (gnus-print-buffer))
5247           (ps-despool filename)))))
5248
5249 (defun gnus-mime-inline-part (&optional handle arg)
5250   "Insert the MIME part under point into the current buffer.
5251 Compressed files like .gz and .bz2 are decompressed."
5252   (interactive (list nil current-prefix-arg))
5253   (gnus-article-check-buffer)
5254   (unless handle
5255     (setq handle (get-text-property (point) 'gnus-data)))
5256   (when handle
5257     (let ((b (point))
5258           (inhibit-read-only t)
5259           contents charset coding-system)
5260       (if (and (not arg) (mm-handle-undisplayer handle))
5261           (mm-remove-part handle)
5262         (mm-with-unibyte-buffer
5263           (mm-insert-part handle)
5264           (setq contents
5265                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5266                     (buffer-string))))
5267         (cond
5268          ((not arg)
5269           (unless (setq charset (mail-content-type-get
5270                                  (mm-handle-type handle) 'charset))
5271             (unless (setq coding-system
5272                           (mm-with-unibyte-buffer
5273                             (insert contents)
5274                             (mm-find-buffer-file-coding-system)))
5275               (setq charset gnus-newsgroup-charset))))
5276          ((numberp arg)
5277           (if (mm-handle-undisplayer handle)
5278               (mm-remove-part handle))
5279           (setq charset
5280                 (or (cdr (assq arg
5281                                gnus-summary-show-article-charset-alist))
5282                     (mm-read-coding-system "Charset: "))))
5283          (t
5284           (if (mm-handle-undisplayer handle)
5285               (mm-remove-part handle))))
5286         (forward-line 2)
5287         (mm-display-inline handle)
5288         (goto-char b)))))
5289
5290 (defun gnus-mime-set-charset-parameters (handle charset)
5291   "Set CHARSET to parameters in HANDLE.
5292 CHARSET may either be a string or a symbol."
5293   (unless (stringp charset)
5294     (setq charset (symbol-name charset)))
5295   (if (stringp (car handle))
5296       (dolist (h (cdr handle))
5297         (gnus-mime-set-charset-parameters h charset))
5298     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5299                                             "message/external-body")
5300                                      (progn
5301                                        (unless (mm-handle-cache handle)
5302                                          (mm-extern-cache-contents handle))
5303                                        (mm-handle-cache handle))
5304                                    handle)))
5305            (param (assq 'charset (cdr type))))
5306       (if param
5307           (setcdr param charset)
5308         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5309
5310 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5311   "Insert the MIME part under point into the current buffer using the
5312 specified charset."
5313   (interactive (list nil current-prefix-arg))
5314   (gnus-article-check-buffer)
5315   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5316         (fun (get-text-property (point) 'gnus-callback))
5317         (gnus-newsgroup-ignored-charsets 'gnus-all)
5318         charset form preferred parts)
5319     (when handle
5320       (when (prog1
5321                 (and fun
5322                      (setq charset
5323                            (or (cdr (assq
5324                                      arg
5325                                      gnus-summary-show-article-charset-alist))
5326                                (mm-read-coding-system "Charset: "))))
5327               (if (mm-handle-undisplayer handle)
5328                   (mm-remove-part handle)))
5329         (gnus-mime-set-charset-parameters handle charset)
5330         (when (and (consp (setq form (cdr-safe fun)))
5331                    (setq form (ignore-errors
5332                                 (assq 'gnus-mime-display-alternative form)))
5333                    (setq preferred (caddr form))
5334                    (progn
5335                      (when (eq (car preferred) 'quote)
5336                        (setq preferred (cadr preferred)))
5337                      (not (equal preferred
5338                                  (get-text-property (point) 'gnus-data))))
5339                    (setq parts (get-text-property (point) 'gnus-part))
5340                    (setq parts (cdr (assq parts
5341                                           gnus-article-mime-handle-alist)))
5342                    (equal (mm-handle-media-type parts) "multipart/alternative")
5343                    (setq parts (reverse (cdr parts))))
5344           (setcar (cddr form)
5345                   (list 'quote (or (cadr (member preferred parts))
5346                                    (car parts)))))
5347         (funcall fun handle)))))
5348
5349 (defun gnus-mime-view-part-externally (&optional handle)
5350   "View the MIME part under point with an external viewer."
5351   (interactive)
5352   (gnus-article-check-buffer)
5353   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5354          (mm-user-display-methods nil)
5355          (mm-inlined-types nil)
5356          (mail-parse-charset gnus-newsgroup-charset)
5357          (mail-parse-ignored-charsets
5358           (with-current-buffer gnus-summary-buffer
5359             gnus-newsgroup-ignored-charsets))
5360          (type (mm-handle-media-type handle))
5361          (method (mailcap-mime-info type))
5362          (mm-enable-external t))
5363     (if (not (stringp method))
5364         (gnus-mime-view-part-as-type
5365          nil (lambda (type) (stringp (mailcap-mime-info type))))
5366       (when handle
5367         (mm-display-part handle nil t)))))
5368
5369 (defun gnus-mime-view-part-internally (&optional handle)
5370   "View the MIME part under point with an internal viewer.
5371 If no internal viewer is available, use an external viewer."
5372   (interactive)
5373   (gnus-article-check-buffer)
5374   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5375          (mm-inlined-types '(".*"))
5376          (mm-inline-large-images t)
5377          (mail-parse-charset gnus-newsgroup-charset)
5378          (mail-parse-ignored-charsets
5379           (with-current-buffer gnus-summary-buffer
5380             gnus-newsgroup-ignored-charsets))
5381          (inhibit-read-only t))
5382     (if (not (mm-inlinable-p handle))
5383         (gnus-mime-view-part-as-type
5384          nil (lambda (type) (mm-inlinable-p handle type)))
5385       (when handle
5386         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5387
5388 (defun gnus-mime-action-on-part (&optional action)
5389   "Do something with the MIME attachment at \(point\)."
5390   (interactive
5391    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5392   (gnus-article-check-buffer)
5393   (let ((action-pair (assoc action gnus-mime-action-alist)))
5394     (if action-pair
5395         (funcall (cdr action-pair)))))
5396
5397 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5398   "Call FUNCTION on MIME part N.
5399 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5400 If INTERACTIVE, call FUNCTION interactivly."
5401   (let (window frame)
5402     ;; Check whether the article is displayed.
5403     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5404                  (setq window (get-buffer-window gnus-article-buffer t))
5405                  (frame-visible-p (setq frame (window-frame window))))
5406       (error "No article is displayed"))
5407     (with-current-buffer gnus-article-buffer
5408       ;; Check whether the article displays the right contents.
5409       (unless (with-current-buffer gnus-summary-buffer
5410                 (eq gnus-current-article (gnus-summary-article-number)))
5411         (error "You should select the right article first"))
5412       (if n
5413           (setq n (prefix-numeric-value n))
5414         (let ((pt (point)))
5415           (setq n (or (get-text-property pt 'gnus-part)
5416                       (and (not (bobp))
5417                            (get-text-property (1- pt) 'gnus-part))
5418                       (get-text-property (prog2
5419                                              (forward-line 1)
5420                                              (point)
5421                                            (goto-char pt))
5422                                          'gnus-part)
5423                       (get-text-property
5424                        (or (and (setq pt (previous-single-property-change
5425                                           pt 'gnus-part))
5426                                 (1- pt))
5427                            (next-single-property-change (point) 'gnus-part)
5428                            (point))
5429                        'gnus-part)
5430                       1))))
5431       ;; Check whether the specified part exists.
5432       (when (> n (length gnus-article-mime-handle-alist))
5433         (error "No such part")))
5434     (unless
5435         (progn
5436           ;; To select the window is needed so that the cursor
5437           ;; might be visible on the MIME button.
5438           (select-window (prog1
5439                              window
5440                            (setq window (selected-window))
5441                            ;; Article may be displayed in the other frame.
5442                            (gnus-select-frame-set-input-focus
5443                             (prog1
5444                                 frame
5445                               (setq frame (selected-frame))))))
5446           (when (gnus-article-goto-part n)
5447             ;; We point the cursor and the arrow at the MIME button
5448             ;; when the `function' prompt the user for something.
5449             (unless (and (pos-visible-in-window-p)
5450                          (> (count-lines (point) (window-end))
5451                             (/ (1- (window-height)) 3)))
5452               (recenter (/ (1- (window-height)) 3)))
5453             (let ((cursor-in-non-selected-windows t)
5454                   (overlay-arrow-string "=>")
5455                   (overlay-arrow-position (point-marker)))
5456               (unwind-protect
5457                   (cond
5458                    ((and no-handle interactive)
5459                     (call-interactively function))
5460                    (no-handle
5461                     (funcall function))
5462                    (interactive
5463                     (call-interactively
5464                      function (get-text-property (point) 'gnus-data)))
5465                    (t
5466                     (funcall function
5467                              (get-text-property (point) 'gnus-data))))
5468                 (set-marker overlay-arrow-position nil)
5469                 (unless gnus-auto-select-part
5470                   (gnus-select-frame-set-input-focus frame)
5471                   (select-window window))))
5472             t))
5473       (if gnus-inhibit-mime-unbuttonizing
5474           ;; This is the default though the program shouldn't reach here.
5475           (error "No such part")
5476         ;; The part which doesn't have the MIME button is selected.
5477         ;; So, we display all the buttons and redo it.
5478         (let ((gnus-inhibit-mime-unbuttonizing t))
5479           (gnus-summary-show-article)
5480           (gnus-article-part-wrapper n function no-handle))))))
5481
5482 (defun gnus-article-pipe-part (n)
5483   "Pipe MIME part N, which is the numerical prefix."
5484   (interactive "P")
5485   (gnus-article-part-wrapper n 'mm-pipe-part))
5486
5487 (defun gnus-article-save-part (n)
5488   "Save MIME part N, which is the numerical prefix."
5489   (interactive "P")
5490   (gnus-article-part-wrapper n 'mm-save-part))
5491
5492 (defun gnus-article-interactively-view-part (n)
5493   "View MIME part N interactively, which is the numerical prefix."
5494   (interactive "P")
5495   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5496
5497 (defun gnus-article-copy-part (n)
5498   "Copy MIME part N, which is the numerical prefix."
5499   (interactive "P")
5500   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5501
5502 (defun gnus-article-view-part-as-charset (n)
5503   "View MIME part N using a specified charset.
5504 N is the numerical prefix."
5505   (interactive "P")
5506   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5507
5508 (defun gnus-article-view-part-externally (n)
5509   "View MIME part N externally, which is the numerical prefix."
5510   (interactive "P")
5511   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5512
5513 (defun gnus-article-inline-part (n)
5514   "Inline MIME part N, which is the numerical prefix."
5515   (interactive "P")
5516   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5517
5518 (defun gnus-article-save-part-and-strip (n)
5519   "Save MIME part N and replace it with an external body.
5520 N is the numerical prefix."
5521   (interactive "P")
5522   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5523
5524 (defun gnus-article-replace-part (n)
5525   "Replace MIME part N with an external body.
5526 N is the numerical prefix."
5527   (interactive "P")
5528   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5529
5530 (defun gnus-article-delete-part (n)
5531   "Delete MIME part N and add some information about the removed part.
5532 N is the numerical prefix."
5533   (interactive "P")
5534   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5535
5536 (defun gnus-article-view-part-as-type (n)
5537   "Choose a MIME media type, and view part N as such.
5538 N is the numerical prefix."
5539   (interactive "P")
5540   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5541
5542 (defun gnus-article-mime-match-handle-first (condition)
5543   (if condition
5544       (let (n)
5545         (dolist (ihandle gnus-article-mime-handle-alist)
5546           (if (and (cond
5547                     ((functionp condition)
5548                      (funcall condition (cdr ihandle)))
5549                     ((eq condition 'undisplayed)
5550                      (not (or (mm-handle-undisplayer (cdr ihandle))
5551                               (equal (mm-handle-media-type (cdr ihandle))
5552                                      "multipart/alternative"))))
5553                     ((eq condition 'undisplayed-alternative)
5554                      (not (mm-handle-undisplayer (cdr ihandle))))
5555                     (t t))
5556                    (gnus-article-goto-part (car ihandle))
5557                    (or (not n) (< (car ihandle) n)))
5558               (setq n (car ihandle))))
5559         (or n 1))
5560     1))
5561
5562 (defun gnus-article-view-part (&optional n)
5563   "View MIME part N, which is the numerical prefix.
5564 If the part is already shown, hide the part.  If N is nil, view
5565 all parts."
5566   (interactive "P")
5567   (with-current-buffer gnus-article-buffer
5568     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5569                              gnus-article-mime-match-handle-function)))
5570     (when (> n (length gnus-article-mime-handle-alist))
5571       (error "No such part"))
5572     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5573       (when (gnus-article-goto-part n)
5574         (if (equal (car handle) "multipart/alternative")
5575             (gnus-article-press-button)
5576           (when (eq (gnus-mm-display-part handle) 'internal)
5577             (gnus-set-window-start)))))))
5578
5579 (defsubst gnus-article-mime-total-parts ()
5580   (if (bufferp (car gnus-article-mime-handles))
5581       1 ;; single part
5582     (1- (length gnus-article-mime-handles))))
5583
5584 (defun gnus-mm-display-part (handle)
5585   "Display HANDLE and fix MIME button."
5586   (let ((id (get-text-property (point) 'gnus-part))
5587         (point (point))
5588         (inhibit-read-only t))
5589     (forward-line 1)
5590     (prog1
5591         (let ((window (selected-window))
5592               (mail-parse-charset gnus-newsgroup-charset)
5593               (mail-parse-ignored-charsets
5594                (if (gnus-buffer-live-p gnus-summary-buffer)
5595                    (with-current-buffer gnus-summary-buffer
5596                      gnus-newsgroup-ignored-charsets)
5597                  nil)))
5598           (save-excursion
5599             (unwind-protect
5600                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5601                       (beg (point)))
5602                   (when win
5603                     (select-window win))
5604                   (goto-char point)
5605                   (forward-line)
5606                   (if (mm-handle-displayed-p handle)
5607                       ;; This will remove the part.
5608                       (mm-display-part handle)
5609                     (save-restriction
5610                       (narrow-to-region (point)
5611                                         (if (eobp) (point) (1+ (point))))
5612                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5613                       ;; We narrow to the part itself and
5614                       ;; then call the treatment functions.
5615                       (goto-char (point-min))
5616                       (forward-line 1)
5617                       (narrow-to-region (point) (point-max))
5618                       (gnus-treat-article
5619                        nil id
5620                        (gnus-article-mime-total-parts)
5621                        (mm-handle-media-type handle)))))
5622               (if (window-live-p window)
5623                   (select-window window)))))
5624       (goto-char point)
5625       (gnus-delete-line)
5626       (gnus-insert-mime-button
5627        handle id (list (mm-handle-displayed-p handle)))
5628       (goto-char point))))
5629
5630 (defun gnus-article-goto-part (n)
5631   "Go to MIME part N."
5632   (when gnus-break-pages
5633     (widen))
5634   (prog1
5635       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5636             part handle end next handles)
5637         (when start
5638           (goto-char start)
5639           (if (setq handle (get-text-property start 'gnus-data))
5640               start
5641             ;; Go to the displayed subpart, assuming this is
5642             ;; multipart/alternative.
5643             (setq part start
5644                   end (point-at-eol))
5645             (while (and (not handle)
5646                         part
5647                         (< part end)
5648                         (setq next (text-property-not-all part end
5649                                                           'gnus-data nil)))
5650               (setq part next
5651                     handle (get-text-property part 'gnus-data))
5652               (push (cons handle part) handles)
5653               (unless (mm-handle-displayed-p handle)
5654                 (setq handle nil
5655                       part (text-property-any part end 'gnus-data nil))))
5656             (unless handle
5657               ;; No subpart is displayed, so we find preferred one.
5658               (setq part
5659                     (cdr (assq (mm-preferred-alternative
5660                                 (nreverse (mapcar 'car handles)))
5661                                handles))))
5662             (if part
5663                 (goto-char (1+ part))
5664               start))))
5665     (when gnus-break-pages
5666       (gnus-narrow-to-page))))
5667
5668 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5669   (let ((gnus-tmp-name
5670          (or (mm-handle-filename handle)
5671              (mail-content-type-get (mm-handle-type handle) 'url)
5672              ""))
5673         (gnus-tmp-type (mm-handle-media-type handle))
5674         (gnus-tmp-description (or (mm-handle-description handle) ""))
5675         (gnus-tmp-dots
5676          (if (if displayed (car displayed)
5677                (mm-handle-displayed-p handle))
5678              "" "..."))
5679         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5680                            (buffer-size)))
5681         gnus-tmp-type-long b e)
5682     (when (string-match ".*/" gnus-tmp-name)
5683       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5684     (setq gnus-tmp-type-long (concat gnus-tmp-type
5685                                      (and (not (equal gnus-tmp-name ""))
5686                                           (concat "; " gnus-tmp-name))))
5687     (unless (equal gnus-tmp-description "")
5688       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5689     (unless (bolp)
5690       (insert "\n"))
5691     (setq b (point))
5692     (gnus-eval-format
5693      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5694      `(keymap ,gnus-mime-button-map
5695               gnus-callback gnus-mm-display-part
5696               gnus-part ,gnus-tmp-id
5697               article-type annotation
5698               gnus-data ,handle))
5699     (setq e (if (bolp)
5700                 ;; Exclude a newline.
5701                 (1- (point))
5702               (point)))
5703     (when gnus-article-button-face
5704       (gnus-overlay-put (gnus-make-overlay b e nil t)
5705                         'face gnus-article-button-face))
5706     (widget-convert-button
5707      'link b e
5708      :mime-handle handle
5709      :action 'gnus-widget-press-button
5710      :button-keymap gnus-mime-button-map
5711      :help-echo
5712      (lambda (widget)
5713        ;; Needed to properly clear the message due to a bug in
5714        ;; wid-edit (XEmacs only).
5715        (if (boundp 'help-echo-owns-message)
5716            (setq help-echo-owns-message t))
5717        (format
5718         "%S: %s the MIME part; %S: more options"
5719         (aref gnus-mouse-2 0)
5720         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5721             "hide" "show")
5722         (aref gnus-down-mouse-3 0))))))
5723
5724 (defun gnus-widget-press-button (elems el)
5725   (goto-char (widget-get elems :from))
5726   (gnus-article-press-button))
5727
5728 (defvar gnus-displaying-mime nil)
5729
5730 (defun gnus-display-mime (&optional ihandles)
5731   "Display the MIME parts."
5732   (save-excursion
5733     (save-selected-window
5734       (let ((window (get-buffer-window gnus-article-buffer))
5735             (point (point)))
5736         (when window
5737           (select-window window)
5738           ;; We have to do this since selecting the window
5739           ;; may change the point.  So we set the window point.
5740           (set-window-point window point)))
5741       (let ((handles ihandles)
5742             (inhibit-read-only t)
5743             handle)
5744         (cond (handles)
5745               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5746                (when gnus-article-emulate-mime
5747                  (mm-uu-dissect-text-parts handles)))
5748               (gnus-article-emulate-mime
5749                (setq handles (mm-uu-dissect))))
5750         (when (and (not ihandles)
5751                    (not gnus-displaying-mime))
5752           ;; Top-level call; we clean up.
5753           (when gnus-article-mime-handles
5754             (mm-destroy-parts gnus-article-mime-handles)
5755             (setq gnus-article-mime-handle-alist nil));; A trick.
5756           (setq gnus-article-mime-handles handles)
5757           ;; We allow users to glean info from the handles.
5758           (when gnus-article-mime-part-function
5759             (gnus-mime-part-function handles)))
5760         (if (and handles
5761                  (or (not (stringp (car handles)))
5762                      (cdr handles)))
5763             (progn
5764               (when (and (not ihandles)
5765                          (not gnus-displaying-mime))
5766                 ;; Clean up for mime parts.
5767                 (article-goto-body)
5768                 (delete-region (point) (point-max)))
5769               (let ((gnus-displaying-mime t))
5770                 (gnus-mime-display-part handles)))
5771           (save-restriction
5772             (article-goto-body)
5773             (narrow-to-region (point) (point-max))
5774             (gnus-treat-article nil 1 1 "text/plain")
5775             (widen)))
5776         (unless ihandles
5777           ;; Highlight the headers.
5778           (save-excursion
5779             (save-restriction
5780               (article-goto-body)
5781               (narrow-to-region (point-min) (point))
5782               (gnus-article-save-original-date
5783                (gnus-treat-article 'head)))))))
5784     ;; Cope with broken MIME messages.
5785     (goto-char (point-max))
5786     (unless (bolp)
5787       (insert "\n"))))
5788
5789 (defcustom gnus-mime-display-multipart-as-mixed nil
5790   "Display \"multipart\" parts as  \"multipart/mixed\".
5791
5792 If t, it overrides nil values of
5793 `gnus-mime-display-multipart-alternative-as-mixed' and
5794 `gnus-mime-display-multipart-related-as-mixed'."
5795   :group 'gnus-article-mime
5796   :type 'boolean)
5797
5798 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5799   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5800   :version "22.1"
5801   :group 'gnus-article-mime
5802   :type 'boolean)
5803
5804 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5805   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5806
5807 If displaying \"text/html\" is discouraged \(see
5808 `mm-discouraged-alternatives'\) images or other material inside a
5809 \"multipart/related\" part might be overlooked when this variable is nil."
5810   :version "22.1"
5811   :group 'gnus-article-mime
5812   :type 'boolean)
5813
5814 (defun gnus-mime-display-part (handle)
5815   (cond
5816    ;; Maybe a broken MIME message.
5817    ((null handle))
5818    ;; Single part.
5819    ((not (stringp (car handle)))
5820     (gnus-mime-display-single handle))
5821    ;; User-defined multipart
5822    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5823     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5824              handle))
5825    ;; multipart/alternative
5826    ((and (equal (car handle) "multipart/alternative")
5827          (not (or gnus-mime-display-multipart-as-mixed
5828                   gnus-mime-display-multipart-alternative-as-mixed)))
5829     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5830       (push (cons id handle) gnus-article-mime-handle-alist)
5831       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5832    ;; multipart/related
5833    ((and (equal (car handle) "multipart/related")
5834          (not (or gnus-mime-display-multipart-as-mixed
5835                   gnus-mime-display-multipart-related-as-mixed)))
5836     ;;;!!!We should find the start part, but we just default
5837     ;;;!!!to the first part.
5838     ;;(gnus-mime-display-part (cadr handle))
5839     ;;;!!! Most multipart/related is an HTML message plus images.
5840     ;;;!!! Unfortunately we are unable to let W3 display those
5841     ;;;!!! included images, so we just display it as a mixed multipart.
5842     ;;(gnus-mime-display-mixed (cdr handle))
5843     ;;;!!! No, w3 can display everything just fine.
5844     (gnus-mime-display-part (cadr handle)))
5845    ((equal (car handle) "multipart/signed")
5846     (gnus-add-wash-type 'signed)
5847     (gnus-mime-display-security handle))
5848    ((equal (car handle) "multipart/encrypted")
5849     (gnus-add-wash-type 'encrypted)
5850     (gnus-mime-display-security handle))
5851    ;; Other multiparts are handled like multipart/mixed.
5852    (t
5853     (gnus-mime-display-mixed (cdr handle)))))
5854
5855 (defun gnus-mime-part-function (handles)
5856   (if (stringp (car handles))
5857       (mapcar 'gnus-mime-part-function (cdr handles))
5858     (funcall gnus-article-mime-part-function handles)))
5859
5860 (defun gnus-mime-display-mixed (handles)
5861   (mapcar 'gnus-mime-display-part handles))
5862
5863 (defun gnus-mime-display-single (handle)
5864   (let ((type (mm-handle-media-type handle))
5865         (ignored gnus-ignored-mime-types)
5866         (not-attachment t)
5867         (move nil)
5868         display text)
5869     (catch 'ignored
5870       (progn
5871         (while ignored
5872           (when (string-match (pop ignored) type)
5873             (throw 'ignored nil)))
5874         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5875                                (with-current-buffer gnus-summary-buffer
5876                                  gnus-inhibit-images)
5877                              gnus-inhibit-images)
5878                            (string-match "\\`image/" type)))
5879                  (setq not-attachment
5880                        (and (not (mm-inline-override-p handle))
5881                             (or (not (mm-handle-disposition handle))
5882                                 (equal (car (mm-handle-disposition handle))
5883                                        "inline")
5884                                 (mm-attachment-override-p handle))))
5885                  (mm-automatic-display-p handle)
5886                  (or (and
5887                       (mm-inlinable-p handle)
5888                       (mm-inlined-p handle))
5889                      (mm-automatic-external-display-p type)))
5890             (setq display t)
5891           (when (equal (mm-handle-media-supertype handle) "text")
5892             (setq text t)))
5893         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5894               beg)
5895           (push (cons id handle) gnus-article-mime-handle-alist)
5896           (when (and display
5897                      (equal (mm-handle-media-supertype handle) "message"))
5898             (insert-char
5899              ?\n
5900              (cond ((not (bolp)) 2)
5901                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5902                    (t 1))))
5903           (when (or (not display)
5904                     (not (gnus-unbuttonized-mime-type-p type))
5905                     (eq id gnus-mime-buttonized-part-id))
5906             (gnus-insert-mime-button
5907              handle id (list (or display (and not-attachment text))))
5908             (gnus-article-insert-newline)
5909             ;; Remember modify the number of forward lines.
5910             (setq move t))
5911           (setq beg (point))
5912           (cond
5913            (display
5914             (when move
5915               (forward-line -1)
5916               (setq beg (point)))
5917             (let ((mail-parse-charset gnus-newsgroup-charset)
5918                   (mail-parse-ignored-charsets
5919                    (save-excursion (condition-case ()
5920                                        (set-buffer gnus-summary-buffer)
5921                                      (error))
5922                                    gnus-newsgroup-ignored-charsets)))
5923               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5924             (goto-char (point-max)))
5925            ((and text not-attachment)
5926             (when move
5927               (forward-line -1)
5928               (setq beg (point)))
5929             (gnus-article-insert-newline)
5930             (mm-display-inline handle)
5931             (goto-char (point-max))))
5932           ;; Do highlighting.
5933           (save-excursion
5934             (save-restriction
5935               (narrow-to-region beg (point))
5936               (if (eq handle gnus-article-mime-handles)
5937                   ;; The format=flowed case.
5938                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5939                 ;; Don't count signature parts that are never displayed.
5940                 ;; The part number should be re-calculated supposing this
5941                 ;; might be a message/rfc822 part.
5942                 (let (handles)
5943                   (dolist (part gnus-article-mime-handles)
5944                     (unless (or (stringp part)
5945                                 (equal (car (mm-handle-type part))
5946                                        "application/pgp-signature"))
5947                       (push part handles)))
5948                   (gnus-treat-article
5949                    nil (length (memq handle handles)) (length handles)
5950                    (mm-handle-media-type handle)))))))))))
5951
5952 (defun gnus-unbuttonized-mime-type-p (type)
5953   "Say whether TYPE is to be unbuttonized."
5954   (unless gnus-inhibit-mime-unbuttonizing
5955     (when (catch 'found
5956             (let ((types gnus-unbuttonized-mime-types))
5957               (while types
5958                 (when (string-match (pop types) type)
5959                   (throw 'found t)))))
5960       (not (catch 'found
5961              (let ((types gnus-buttonized-mime-types))
5962                (while types
5963                  (when (string-match (pop types) type)
5964                    (throw 'found t)))))))))
5965
5966 (defun gnus-article-insert-newline ()
5967   "Insert a newline, but mark it as undeletable."
5968   (gnus-put-text-property
5969    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5970
5971 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5972   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5973          (ihandles handles)
5974          (point (point))
5975          handle (inhibit-read-only t) from props begend not-pref)
5976     (save-window-excursion
5977       (save-restriction
5978         (when ibegend
5979           (narrow-to-region (car ibegend)
5980                             (or (cdr ibegend)
5981                                 (progn
5982                                   (goto-char (car ibegend))
5983                                   (forward-line 2)
5984                                   (point))))
5985           (delete-region (point-min) (point-max))
5986           (mm-remove-parts handles))
5987         (setq begend (list (point-marker)))
5988         ;; Do the toggle.
5989         (unless (setq not-pref (cadr (member preferred ihandles)))
5990           (setq not-pref (car ihandles)))
5991         (when (or ibegend
5992                   (not preferred)
5993                   (not (gnus-unbuttonized-mime-type-p
5994                         "multipart/alternative")))
5995           (gnus-add-text-properties
5996            (setq from (point))
5997            (progn
5998              (insert (format "%d.  " id))
5999              (point))
6000            `(gnus-callback
6001              (lambda (handles)
6002                (unless ,(not ibegend)
6003                  (setq gnus-article-mime-handle-alist
6004                        ',gnus-article-mime-handle-alist))
6005                (gnus-mime-display-alternative
6006                 ',ihandles ',not-pref ',begend ,id))
6007              keymap ,gnus-mime-button-map
6008              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6009              face ,gnus-article-button-face
6010              gnus-part ,id
6011              article-type multipart))
6012           (widget-convert-button 'link from (point)
6013                                  :action 'gnus-widget-press-button
6014                                  :button-keymap gnus-widget-button-keymap)
6015           ;; Do the handles
6016           (while (setq handle (pop handles))
6017             (gnus-add-text-properties
6018              (setq from (point))
6019              (progn
6020                (insert (format "(%c) %-18s"
6021                                (if (equal handle preferred) ?* ? )
6022                                (mm-handle-media-type handle)))
6023                (point))
6024              `(gnus-callback
6025                (lambda (handles)
6026                  (unless ,(not ibegend)
6027                    (setq gnus-article-mime-handle-alist
6028                          ',gnus-article-mime-handle-alist))
6029                  (gnus-mime-display-alternative
6030                   ',ihandles ',handle ',begend ,id))
6031                keymap ,gnus-mime-button-map
6032                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6033                face ,gnus-article-button-face
6034                gnus-part ,id
6035                gnus-data ,handle))
6036             (widget-convert-button 'link from (point)
6037                                    :action 'gnus-widget-press-button
6038                                    :button-keymap gnus-widget-button-keymap)
6039             (insert "  "))
6040           (insert "\n\n"))
6041         (when preferred
6042           (if (stringp (car preferred))
6043               (gnus-display-mime preferred)
6044             (let ((mail-parse-charset gnus-newsgroup-charset)
6045                   (mail-parse-ignored-charsets
6046                    (with-current-buffer gnus-summary-buffer
6047                      gnus-newsgroup-ignored-charsets)))
6048               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6049               ;; Do highlighting.
6050               (save-excursion
6051                 (save-restriction
6052                   (narrow-to-region (car begend) (point-max))
6053                   (gnus-treat-article
6054                    nil (length gnus-article-mime-handle-alist)
6055                    (gnus-article-mime-total-parts)
6056                    (mm-handle-media-type preferred))))))
6057           (goto-char (point-max))
6058           (setcdr begend (point-marker)))))
6059     (when ibegend
6060       (goto-char point))))
6061
6062 (defconst gnus-article-wash-status-strings
6063   (let ((alist '((cite "c" "Possible hidden citation text"
6064                        " " "All citation text visible")
6065                  (headers "h" "Hidden headers"
6066                           " " "All headers visible.")
6067                  (pgp "p" "Encrypted or signed message status hidden"
6068                       " " "No hidden encryption nor digital signature status")
6069                  (signature "s" "Signature has been hidden"
6070                             " " "Signature is visible")
6071                  (overstrike "o" "Overstrike (^H) characters applied"
6072                              " " "No overstrike characters applied")
6073                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6074                            " " "No /*_emphasis_*/ characters applied")))
6075         result)
6076     (dolist (entry alist result)
6077       (let ((key (nth 0 entry))
6078             (on (copy-sequence (nth 1 entry)))
6079             (on-help (nth 2 entry))
6080             (off (copy-sequence (nth 3 entry)))
6081             (off-help (nth 4 entry)))
6082         (put-text-property 0 1 'help-echo on-help on)
6083         (put-text-property 0 1 'help-echo off-help off)
6084         (push (list key on off) result))))
6085   "Alist of strings describing wash status in the mode line.
6086 Each entry has the form (KEY ON OF), where the KEY is a symbol
6087 representing the particular washing function, ON is the string to use
6088 in the article mode line when the washing function is active, and OFF
6089 is the string to use when it is inactive.")
6090
6091 (defun gnus-article-wash-status-entry (key value)
6092   (let ((entry (assoc key gnus-article-wash-status-strings)))
6093     (if value (nth 1 entry) (nth 2 entry))))
6094
6095 (defun gnus-article-wash-status ()
6096   "Return a string which display status of article washing."
6097   (with-current-buffer gnus-article-buffer
6098     (let ((cite (memq 'cite gnus-article-wash-types))
6099           (headers (memq 'headers gnus-article-wash-types))
6100           (boring (memq 'boring-headers gnus-article-wash-types))
6101           (pgp (memq 'pgp gnus-article-wash-types))
6102           (pem (memq 'pem gnus-article-wash-types))
6103           (signed (memq 'signed gnus-article-wash-types))
6104           (encrypted (memq 'encrypted gnus-article-wash-types))
6105           (signature (memq 'signature gnus-article-wash-types))
6106           (overstrike (memq 'overstrike gnus-article-wash-types))
6107           (emphasis (memq 'emphasis gnus-article-wash-types)))
6108       (concat
6109        (gnus-article-wash-status-entry 'cite cite)
6110        (gnus-article-wash-status-entry 'headers (or headers boring))
6111        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6112        (gnus-article-wash-status-entry 'signature signature)
6113        (gnus-article-wash-status-entry 'overstrike overstrike)
6114        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6115
6116 (defun gnus-add-wash-type (type)
6117   "Add a washing of TYPE to the current status."
6118   (add-to-list 'gnus-article-wash-types type))
6119
6120 (defun gnus-delete-wash-type (type)
6121   "Add a washing of TYPE to the current status."
6122   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6123
6124 (defun gnus-add-image (category image)
6125   "Add IMAGE of CATEGORY to the list of displayed images."
6126   (let ((entry (assq category gnus-article-image-alist)))
6127     (unless entry
6128       (setq entry (list category))
6129       (push entry gnus-article-image-alist))
6130     (nconc entry (list image))))
6131
6132 (defun gnus-delete-images (category)
6133   "Delete all images in CATEGORY."
6134   (let ((entry (assq category gnus-article-image-alist)))
6135     (dolist (image (cdr entry))
6136       (gnus-remove-image image category))
6137     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6138     (gnus-delete-wash-type category)))
6139
6140 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6141
6142 (defun gnus-article-maybe-hide-headers ()
6143   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6144 Provided for backwards compatibility."
6145   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6146                  (not (with-current-buffer gnus-summary-buffer
6147                         gnus-have-all-headers)))
6148              (not gnus-inhibit-hiding))
6149     (gnus-article-hide-headers)))
6150
6151 (declare-function shr-put-image "shr" (data alt))
6152
6153 (defun gnus-shr-put-image (data alt)
6154   "Put image DATA with a string ALT.  Enable image to be deleted."
6155   (let ((image (shr-put-image data (propertize (or alt "*")
6156                                                'gnus-image-category 'shr))))
6157     (when image
6158       (gnus-add-image 'shr image))))
6159
6160 ;;; Article savers.
6161
6162 (defun gnus-output-to-file (file-name)
6163   "Append the current article to a file named FILE-NAME.
6164 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6165 text and used as the value of the coding cookie which is added to the
6166 top of a file.  Otherwise, this function saves a raw article without
6167 the coding cookie."
6168   (let* ((artbuf (current-buffer))
6169          (file-name-coding-system nnmail-pathname-coding-system)
6170          (coding gnus-article-save-coding-system)
6171          (coding-system-for-read (if coding
6172                                      nil ;; Rely on the coding cookie.
6173                                    mm-text-coding-system))
6174          (coding-system-for-write (or coding
6175                                       mm-text-coding-system-for-write
6176                                       mm-text-coding-system))
6177          (exists (file-exists-p file-name)))
6178     (with-temp-buffer
6179       (when exists
6180         (insert-file-contents file-name)
6181         (goto-char (point-min))
6182         ;; Remove the existing coding cookie.
6183         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6184           (delete-region (match-beginning 0) (match-end 0))))
6185       (goto-char (point-max))
6186       (insert-buffer-substring artbuf)
6187       ;; Append newline at end of the buffer as separator, and then
6188       ;; save it to file.
6189       (goto-char (point-max))
6190       (insert "\n")
6191       (when coding
6192         ;; If the coding system is not suitable to encode the text,
6193         ;; ask a user for a proper one.
6194         (when (fboundp 'select-safe-coding-system)
6195           (setq coding (coding-system-base
6196                         (save-window-excursion
6197                           (select-safe-coding-system (point-min) (point-max)
6198                                                      coding))))
6199           (setq coding-system-for-write
6200                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6201                     coding)))
6202         (goto-char (point-min))
6203         ;; Add the coding cookie.
6204         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6205                         coding-system-for-write)))
6206       (if exists
6207           (progn
6208             (write-region (point-min) (point-max) file-name nil 'no-message)
6209             (message "Appended to %s" file-name))
6210         (write-region (point-min) (point-max) file-name))))
6211   t)
6212
6213 (defun gnus-narrow-to-page (&optional arg)
6214   "Narrow the article buffer to a page.
6215 If given a numerical ARG, move forward ARG pages."
6216   (interactive "P")
6217   (setq arg (if arg (prefix-numeric-value arg) 0))
6218   (with-current-buffer gnus-article-buffer
6219     (widen)
6220     ;; Remove any old next/prev buttons.
6221     (when (gnus-visual-p 'page-marker)
6222       (let ((inhibit-read-only t))
6223         (gnus-remove-text-with-property 'gnus-prev)
6224         (gnus-remove-text-with-property 'gnus-next)))
6225     (let (st nd pt)
6226       (when (save-excursion
6227               (cond ((< arg 0)
6228                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6229                          (prog1
6230                              (setq nd (match-beginning 0)
6231                                    pt nd)
6232                            (when (re-search-backward page-delimiter nil t)
6233                              (setq st (match-end 0))))
6234                        (when (re-search-forward page-delimiter nil t)
6235                          (setq nd (match-beginning 0)
6236                                pt (point-min)))))
6237                     ((> arg 0)
6238                      (if (re-search-forward page-delimiter nil 'move arg)
6239                          (prog1
6240                              (setq st (match-end 0)
6241                                    pt st)
6242                            (when (re-search-forward page-delimiter nil t)
6243                              (setq nd (match-beginning 0))))
6244                        (when (re-search-backward page-delimiter nil t)
6245                          (setq st (match-end 0)
6246                                pt (point-max)))))
6247                     (t
6248                      (when (re-search-backward page-delimiter nil t)
6249                        (goto-char (setq st (match-end 0))))
6250                      (when (re-search-forward page-delimiter nil t)
6251                        (setq nd (match-beginning 0)))
6252                      (or st nd))))
6253         (setq gnus-page-broken t)
6254         (when pt (goto-char pt))
6255         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6256         (when (gnus-visual-p 'page-marker)
6257           (save-excursion
6258             (when nd
6259               (goto-char nd)
6260               (gnus-insert-next-page-button))
6261             (when st
6262               (goto-char st)
6263               (gnus-insert-prev-page-button))))))))
6264
6265 ;; Article mode commands
6266
6267 (defun gnus-article-goto-next-page ()
6268   "Show the next page of the article."
6269   (interactive)
6270   (when (gnus-article-next-page)
6271     (goto-char (point-min))
6272     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6273
6274
6275 (defun gnus-article-goto-prev-page ()
6276   "Show the previous page of the article."
6277   (interactive)
6278   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6279       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6280     (gnus-article-prev-page nil)))
6281
6282 ;; This is cleaner but currently breaks `gnus-pick-mode':
6283 ;;
6284 ;; (defun gnus-article-goto-next-page ()
6285 ;;   "Show the next page of the article."
6286 ;;   (interactive)
6287 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6288 ;;     (gnus-summary-next-page)))
6289 ;;
6290 ;; (defun gnus-article-goto-prev-page ()
6291 ;;   "Show the next page of the article."
6292 ;;   (interactive)
6293 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6294 ;;     (gnus-summary-prev-page)))
6295
6296 (defun gnus-article-next-page (&optional lines)
6297   "Show the next page of the current article.
6298 If end of article, return non-nil.  Otherwise return nil.
6299 Argument LINES specifies lines to be scrolled up."
6300   (interactive "p")
6301   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6302   (if (and (not (and gnus-article-over-scroll
6303                      (> (count-lines (window-start) (point-max))
6304                         (if (featurep 'xemacs)
6305                             (or lines (1- (window-height)))
6306                           (+ (or lines (1- (window-height))) scroll-margin)))))
6307            (save-excursion
6308              (end-of-line)
6309              (and (pos-visible-in-window-p)     ;Not continuation line.
6310                   (>= (point) (point-max)))))
6311       ;; Nothing in this page.
6312       (if (or (not gnus-page-broken)
6313               (save-excursion
6314                 (save-restriction
6315                   (widen)
6316                   (forward-line)
6317                   (eobp)))) ;Real end-of-buffer?
6318           (progn
6319             (when gnus-article-over-scroll
6320               (gnus-article-next-page-1 lines))
6321             t)                  ;Nothing more.
6322         (gnus-narrow-to-page 1)         ;Go to next page.
6323         nil)
6324     ;; More in this page.
6325     (gnus-article-next-page-1 lines)
6326     nil))
6327
6328 (defun gnus-article-beginning-of-window ()
6329   "Move point to the beginning of the window.
6330 In Emacs, the point is placed at the line number which `scroll-margin'
6331 specifies."
6332   (if (featurep 'xemacs)
6333       (move-to-window-line 0)
6334     ;; There is an obscure bug in Emacs that makes it impossible to
6335     ;; scroll past big pictures in the article buffer.  Try to fix
6336     ;; this by adding a sanity check by counting the lines visible.
6337     (when (> (count-lines (window-start) (window-end)) 30)
6338       (move-to-window-line
6339        (min (max 0 scroll-margin)
6340             (max 1 (- (window-height)
6341                       (if mode-line-format 1 0)
6342                       (if header-line-format 1 0)
6343                       2)))))))
6344
6345 (defun gnus-article-next-page-1 (lines)
6346   (condition-case ()
6347       (let ((scroll-in-place nil)
6348             (auto-window-vscroll nil))
6349         (scroll-up lines))
6350     (end-of-buffer
6351      ;; Long lines may cause an end-of-buffer error.
6352      (goto-char (point-max))))
6353   (gnus-article-beginning-of-window))
6354
6355 (defun gnus-article-prev-page (&optional lines)
6356   "Show previous page of current article.
6357 Argument LINES specifies lines to be scrolled down."
6358   (interactive "p")
6359   (move-to-window-line 0)
6360   (if (and gnus-page-broken
6361            (bobp)
6362            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6363       (progn
6364         (gnus-narrow-to-page -1)        ;Go to previous page.
6365         (goto-char (point-max))
6366         (recenter (if gnus-article-over-scroll
6367                       (if lines
6368                           (max (if (featurep 'xemacs)
6369                                    lines
6370                                  (+ lines scroll-margin))
6371                                3)
6372                         (- (window-height) 2))
6373                     -1)))
6374     (prog1
6375         (condition-case ()
6376             (let ((scroll-in-place nil))
6377               (scroll-down lines))
6378           (beginning-of-buffer
6379            (goto-char (point-min))))
6380       (gnus-article-beginning-of-window))))
6381
6382 (defun gnus-article-only-boring-p ()
6383   "Decide whether there is only boring text remaining in the article.
6384 Something \"interesting\" is a word of at least two letters that does
6385 not have a face in `gnus-article-boring-faces'."
6386   (when (and gnus-article-skip-boring
6387              (boundp 'gnus-article-boring-faces)
6388              (symbol-value 'gnus-article-boring-faces))
6389     (save-excursion
6390       (let ((inhibit-point-motion-hooks t))
6391         (catch 'only-boring
6392           (while (re-search-forward "\\b\\w\\w" nil t)
6393             (forward-char -1)
6394             (when (not (gnus-intersection
6395                         (gnus-faces-at (point))
6396                         (symbol-value 'gnus-article-boring-faces)))
6397               (throw 'only-boring nil)))
6398           (throw 'only-boring t))))))
6399
6400 (defun gnus-article-refer-article ()
6401   "Read article specified by message-id around point."
6402   (interactive)
6403   (save-excursion
6404     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6405     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6406     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6407         (let ((msg-id (concat "<" (match-string 0) ">")))
6408           (set-buffer gnus-summary-buffer)
6409           (gnus-summary-refer-article msg-id))
6410       (error "No references around point"))))
6411
6412 (defun gnus-article-show-summary ()
6413   "Reconfigure windows to show summary buffer."
6414   (interactive)
6415   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6416       (error "There is no summary buffer for this article buffer")
6417     (gnus-article-set-globals)
6418     (gnus-configure-windows 'article)
6419     (gnus-summary-goto-subject gnus-current-article)
6420     (gnus-summary-position-point)))
6421
6422 (defun gnus-article-describe-briefly ()
6423   "Describe article mode commands briefly."
6424   (interactive)
6425   (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")))
6426
6427 (defun gnus-article-check-buffer ()
6428   "Beep if not in an article buffer."
6429   (unless (equal major-mode 'gnus-article-mode)
6430     (error "Command invoked outside of a Gnus article buffer")))
6431
6432 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6433   "Read a summary buffer key sequence and execute it from the article buffer."
6434   (interactive "P")
6435   (gnus-article-check-buffer)
6436   (let ((nosaves
6437          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6438            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6439            "=" "^" "\M-^" "|"))
6440         (nosave-but-article
6441          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6442            "An" "Ap" [?A (meta return)] [?A delete]))
6443         (nosave-in-article
6444          '("AS" "\C-d"))
6445         (up-to-top
6446          '("n" "Gn" "p" "Gp"))
6447         keys new-sum-point)
6448     (with-current-buffer gnus-article-current-summary
6449       (let (gnus-pick-mode)
6450         (setq unread-command-events (nconc unread-command-events
6451                                            (list (or key last-command-event)))
6452               keys (if (featurep 'xemacs)
6453                        (events-to-keys (read-key-sequence nil t))
6454                      (read-key-sequence nil t)))))
6455
6456     (message "")
6457
6458     (cond
6459      ((eq (aref keys (1- (length keys))) ?\C-h)
6460       (gnus-article-describe-bindings (substring keys 0 -1)))
6461      ((or (member keys nosaves)
6462           (member keys nosave-but-article)
6463           (member keys nosave-in-article))
6464       (let (func)
6465         (save-window-excursion
6466           (pop-to-buffer gnus-article-current-summary)
6467           ;; We disable the pick minor mode commands.
6468           (let (gnus-pick-mode)
6469             (setq func (key-binding keys t))))
6470         (if (or (not func)
6471                 (numberp func))
6472             (ding)
6473           (unless (member keys nosave-in-article)
6474             (set-buffer gnus-article-current-summary))
6475           (when (get func 'disabled)
6476             (error "Function %s disabled" func))
6477           (call-interactively func)
6478           (setq new-sum-point (point)))
6479         (when (member keys nosave-but-article)
6480           (pop-to-buffer gnus-article-buffer))))
6481      (t
6482       ;; These commands should restore window configuration.
6483       (let ((obuf (current-buffer))
6484             (owin (current-window-configuration))
6485             win func in-buffer selected new-sum-start new-sum-hscroll err)
6486         (cond (not-restore-window
6487                (pop-to-buffer gnus-article-current-summary)
6488                (setq win (selected-window)))
6489               ((setq win (get-buffer-window gnus-article-current-summary))
6490                (select-window win))
6491               (t
6492                (let ((summary-buffer gnus-article-current-summary))
6493                  (gnus-configure-windows 'article)
6494                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6495                    (let ((gnus-buffer-configuration
6496                           '((article ((vertical 1.0
6497                                                 (summary 0.25 point)
6498                                                 (article 1.0)))))))
6499                      (gnus-configure-windows 'article))
6500                    (setq win (get-buffer-window summary-buffer 'visible)))
6501                  (gnus-select-frame-set-input-focus (window-frame win))
6502                  (select-window win))))
6503         (setq in-buffer (current-buffer))
6504         ;; We disable the pick minor mode commands.
6505         (setq func (let (gnus-pick-mode)
6506                      (key-binding keys t)))
6507         (when (get func 'disabled)
6508           (error "Function %s disabled" func))
6509         (if (and func
6510                  (functionp func)
6511                  (condition-case code
6512                      (progn
6513                        (call-interactively func)
6514                        t)
6515                    (error
6516                     (setq err code)
6517                     nil)))
6518             (progn
6519               (when (eq win (selected-window))
6520                 (setq new-sum-point (point)
6521                       new-sum-start (window-start win)
6522                       new-sum-hscroll (window-hscroll win)))
6523               (when (or (eq in-buffer (current-buffer))
6524                         (when (eq obuf (current-buffer))
6525                           (set-buffer in-buffer)
6526                           t))
6527                 (setq selected (gnus-summary-select-article))
6528                 (set-buffer obuf)
6529                 (unless not-restore-window
6530                   (set-window-configuration owin))
6531                 (when (and (eq selected 'old)
6532                            new-sum-point)
6533                   (set-window-start (get-buffer-window (current-buffer))
6534                                     1)
6535                   (set-window-point (get-buffer-window (current-buffer))
6536                                     (if (article-goto-body)
6537                                         (1- (point))
6538                                       (point))))
6539                 (when (and (not not-restore-window)
6540                            new-sum-point
6541                            (window-live-p win)
6542                            (with-current-buffer (window-buffer win)
6543                              (eq major-mode 'gnus-summary-mode)))
6544                   (set-window-point win new-sum-point)
6545                   (set-window-start win new-sum-start)
6546                   (set-window-hscroll win new-sum-hscroll))))
6547           (set-window-configuration owin)
6548           (if err
6549               (signal (car err) (cdr err))
6550             (ding))))))))
6551
6552 (defun gnus-article-read-summary-send-keys ()
6553   (interactive)
6554   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6555     (gnus-article-read-summary-keys)))
6556
6557 (defun gnus-article-describe-key (key)
6558   "Display documentation of the function invoked by KEY.
6559 KEY is a string or a vector."
6560   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6561                        (read-key-sequence "Describe key: "))))
6562   (gnus-article-check-buffer)
6563   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6564                                   gnus-article-read-summary-send-keys))
6565       (with-current-buffer gnus-article-current-summary
6566         (setq unread-command-events
6567               (if (featurep 'xemacs)
6568                   (append key nil)
6569                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6570                                         (list 'meta (- x 128))
6571                                       x))
6572                         key)))
6573         (let ((cursor-in-echo-area t)
6574               gnus-pick-mode)
6575           (describe-key (read-key-sequence nil t))))
6576     (describe-key key)))
6577
6578 (defun gnus-article-describe-key-briefly (key &optional insert)
6579   "Display documentation of the function invoked by KEY.
6580 KEY is a string or a vector."
6581   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6582                        (read-key-sequence "Describe key: "))
6583                      current-prefix-arg))
6584   (gnus-article-check-buffer)
6585   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6586                                   gnus-article-read-summary-send-keys))
6587       (with-current-buffer gnus-article-current-summary
6588         (setq unread-command-events
6589               (if (featurep 'xemacs)
6590                   (append key nil)
6591                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6592                                         (list 'meta (- x 128))
6593                                       x))
6594                         key)))
6595         (let ((cursor-in-echo-area t)
6596               gnus-pick-mode)
6597           (describe-key-briefly (read-key-sequence nil t) insert)))
6598     (describe-key-briefly key insert)))
6599
6600 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6601 (defvar gnus-agent-summary-mode)
6602 (defvar gnus-draft-mode)
6603 ;; Calling help-buffer will autoload help-mode.
6604 (defvar help-xref-stack-item)
6605 ;; Emacs 22 doesn't load it in the batch mode.
6606 (eval-when-compile
6607   (autoload 'help-buffer "help-mode"))
6608
6609 (defun gnus-article-describe-bindings (&optional prefix)
6610   "Show a list of all defined keys, and their definitions.
6611 The optional argument PREFIX, if non-nil, should be a key sequence;
6612 then we display only bindings that start with that prefix."
6613   (interactive)
6614   (gnus-article-check-buffer)
6615   (let ((keymap (copy-keymap gnus-article-mode-map))
6616         (map (copy-keymap gnus-article-send-map))
6617         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6618         parent agent draft)
6619     (define-key keymap "S" map)
6620     (define-key map [t] nil)
6621     (with-current-buffer gnus-article-current-summary
6622       (set-keymap-parent
6623        keymap
6624        (if (setq parent (keymap-parent gnus-article-mode-map))
6625            (prog1
6626                (setq parent (copy-keymap parent))
6627              (set-keymap-parent parent (current-local-map)))
6628          (current-local-map)))
6629       (set-keymap-parent map (key-binding "S"))
6630       (let (key def gnus-pick-mode)
6631         (while sumkeys
6632           (setq key (pop sumkeys))
6633           (cond ((and (vectorp key) (= (length key) 1)
6634                       (consp (setq def (aref key 0)))
6635                       (numberp (car def)) (numberp (cdr def)))
6636                  (when (< (max (car def) (cdr def)) 128)
6637                    (setq sumkeys
6638                          (append (mapcar
6639                                   #'vector
6640                                   (nreverse (gnus-uncompress-range def)))
6641                                  sumkeys))))
6642                 ((setq def (key-binding key))
6643                  (unless (eq def 'undefined)
6644                    (define-key keymap key def))))))
6645       (when (boundp 'gnus-agent-summary-mode)
6646         (setq agent gnus-agent-summary-mode))
6647       (when (boundp 'gnus-draft-mode)
6648         (setq draft gnus-draft-mode)))
6649     (with-temp-buffer
6650       (use-local-map keymap)
6651       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6652       (set (make-local-variable 'gnus-draft-mode) draft)
6653       (describe-bindings prefix))
6654     (let ((item `((lambda (prefix)
6655                     (with-current-buffer ,(current-buffer)
6656                       (gnus-article-describe-bindings prefix)))
6657                   ,prefix)))
6658       (with-current-buffer (let (help-xref-following) (help-buffer))
6659         (setq help-xref-stack-item item)))))
6660
6661 (defun gnus-article-reply-with-original (&optional wide)
6662   "Start composing a reply mail to the current message.
6663 The text in the region will be yanked.  If the region isn't active,
6664 the entire article will be yanked."
6665   (interactive)
6666   (let ((article (cdr gnus-article-current))
6667         contents)
6668     (if (not (gnus-region-active-p))
6669         (with-current-buffer gnus-summary-buffer
6670           (gnus-summary-reply (list (list article)) wide))
6671       (setq contents (buffer-substring (point) (mark t)))
6672       ;; Deactivate active regions.
6673       (when (and (boundp 'transient-mark-mode)
6674                  transient-mark-mode)
6675         (setq mark-active nil))
6676       (with-current-buffer gnus-summary-buffer
6677         (gnus-summary-reply
6678          (list (list article contents)) wide)))))
6679
6680 (defun gnus-article-wide-reply-with-original ()
6681   "Start composing a wide reply mail to the current message.
6682 The text in the region will be yanked.  If the region isn't active,
6683 the entire article will be yanked."
6684   (interactive)
6685   (gnus-article-reply-with-original t))
6686
6687 (defun gnus-article-followup-with-original ()
6688   "Compose a followup to the current article.
6689 The text in the region will be yanked.  If the region isn't active,
6690 the entire article will be yanked."
6691   (interactive)
6692   (let ((article (cdr gnus-article-current))
6693         contents)
6694       (if (not (gnus-region-active-p))
6695           (with-current-buffer gnus-summary-buffer
6696             (gnus-summary-followup (list (list article))))
6697         (setq contents (buffer-substring (point) (mark t)))
6698         ;; Deactivate active regions.
6699         (when (and (boundp 'transient-mark-mode)
6700                    transient-mark-mode)
6701           (setq mark-active nil))
6702         (with-current-buffer gnus-summary-buffer
6703           (gnus-summary-followup
6704            (list (list article contents)))))))
6705
6706 (defun gnus-article-hide (&optional arg force)
6707   "Hide all the gruft in the current article.
6708 This means that signatures, cited text and (some) headers will be
6709 hidden.
6710 If given a prefix, show the hidden text instead."
6711   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6712   (gnus-article-hide-headers arg)
6713   (gnus-article-hide-list-identifiers arg)
6714   (gnus-article-hide-citation-maybe arg force)
6715   (gnus-article-hide-signature arg))
6716
6717 (defun gnus-article-maybe-highlight ()
6718   "Do some article highlighting if article highlighting is requested."
6719   (when (gnus-visual-p 'article-highlight 'highlight)
6720     (gnus-article-highlight-some)))
6721
6722 (defun gnus-check-group-server ()
6723   ;; Make sure the connection to the server is alive.
6724   (unless (gnus-server-opened
6725            (gnus-find-method-for-group gnus-newsgroup-name))
6726     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6727     (gnus-request-group gnus-newsgroup-name t)))
6728
6729 (eval-when-compile
6730   (autoload 'nneething-get-file-name "nneething"))
6731
6732 (defun gnus-request-article-this-buffer (article group)
6733   "Get an article and insert it into this buffer."
6734   (let (do-update-line sparse-header)
6735     (prog1
6736         (save-excursion
6737           (erase-buffer)
6738           (gnus-kill-all-overlays)
6739           (setq group (or group gnus-newsgroup-name))
6740
6741           ;; Using `gnus-request-article' directly will insert the article into
6742           ;; `nntp-server-buffer' - so we'll save some time by not having to
6743           ;; copy it from the server buffer into the article buffer.
6744
6745           ;; We only request an article by message-id when we do not have the
6746           ;; headers for it, so we'll have to get those.
6747           (when (stringp article)
6748             (gnus-read-header article))
6749
6750           ;; If the article number is negative, that means that this article
6751           ;; doesn't belong in this newsgroup (possibly), so we find its
6752           ;; message-id and request it by id instead of number.
6753           (when (and (numberp article)
6754                      gnus-summary-buffer
6755                      (get-buffer gnus-summary-buffer)
6756                      (gnus-buffer-exists-p gnus-summary-buffer))
6757             (with-current-buffer gnus-summary-buffer
6758               (let ((header (gnus-summary-article-header article)))
6759                 (when (< article 0)
6760                   (cond
6761                    ((memq article gnus-newsgroup-sparse)
6762                     ;; This is a sparse gap article.
6763                     (setq do-update-line article)
6764                     (setq article (mail-header-id header))
6765                     (setq sparse-header (gnus-read-header article))
6766                     (setq gnus-newsgroup-sparse
6767                           (delq article gnus-newsgroup-sparse)))
6768                    ((vectorp header)
6769                     ;; It's a real article.
6770                     (setq article (mail-header-id header)))
6771                    (t
6772                     ;; It is an extracted pseudo-article.
6773                     (setq article 'pseudo)
6774                     (gnus-request-pseudo-article header))))
6775
6776                 (let ((method (gnus-find-method-for-group
6777                                gnus-newsgroup-name)))
6778                   (when (and (eq (car method) 'nneething)
6779                              (vectorp header))
6780                     (let ((dir (nneething-get-file-name
6781                                 (mail-header-id header))))
6782                       (when (and (stringp dir)
6783                                  (file-directory-p dir))
6784                         (setq article 'nneething)
6785                         (gnus-group-enter-directory dir))))))))
6786
6787           (cond
6788            ;; Refuse to select canceled articles.
6789            ((and (numberp article)
6790                  gnus-summary-buffer
6791                  (get-buffer gnus-summary-buffer)
6792                  (gnus-buffer-exists-p gnus-summary-buffer)
6793                  (eq (cdr (with-current-buffer gnus-summary-buffer
6794                             (assq article gnus-newsgroup-reads)))
6795                      gnus-canceled-mark))
6796             nil)
6797            ;; We first check `gnus-original-article-buffer'.
6798            ((and (get-buffer gnus-original-article-buffer)
6799                  (numberp article)
6800                  (with-current-buffer gnus-original-article-buffer
6801                    (and (equal (car gnus-original-article) group)
6802                         (eq (cdr gnus-original-article) article))))
6803             ;; `insert-buffer-substring' would incorrectly use the
6804             ;; equivalent of string-make-multibyte which amount to decoding
6805             ;; with locale-coding-system, causing failure of
6806             ;; subsequent decoding.
6807             (insert (mm-string-to-multibyte
6808                      (with-current-buffer gnus-original-article-buffer
6809                        (buffer-substring (point-min) (point-max)))))
6810             'article)
6811            ;; Check the backlog.
6812            ((and gnus-keep-backlog
6813                  (gnus-backlog-request-article group article (current-buffer)))
6814             'article)
6815            ;; Check asynchronous pre-fetch.
6816            ((gnus-async-request-fetched-article group article (current-buffer))
6817             (gnus-async-prefetch-next group article gnus-summary-buffer)
6818             (when (and (numberp article) gnus-keep-backlog)
6819               (gnus-backlog-enter-article group article (current-buffer)))
6820             'article)
6821            ;; Check the cache.
6822            ((and gnus-use-cache
6823                  (numberp article)
6824                  (gnus-cache-request-article article group))
6825             'article)
6826            ;; Check the agent cache.
6827            ((gnus-agent-request-article article group)
6828             'article)
6829            ;; Get the article and put into the article buffer.
6830            ((or (stringp article)
6831                 (numberp article))
6832             (let ((gnus-override-method gnus-override-method)
6833                   (methods (and (stringp article)
6834                                 (with-current-buffer gnus-summary-buffer
6835                                   (gnus-refer-article-methods))))
6836                   (backend (car (gnus-find-method-for-group
6837                                  gnus-newsgroup-name)))
6838                   result
6839                   (inhibit-read-only t))
6840               (when (and (null gnus-override-method)
6841                          methods)
6842                 (setq gnus-override-method (pop methods)))
6843               (while (not result)
6844                 (erase-buffer)
6845                 (gnus-kill-all-overlays)
6846                 (let ((gnus-newsgroup-name group))
6847                   (gnus-check-group-server))
6848                 (cond
6849                  ((gnus-request-article article group (current-buffer))
6850                   (when (numberp article)
6851                     (gnus-async-prefetch-next group article
6852                                               gnus-summary-buffer)
6853                     (when gnus-keep-backlog
6854                       (gnus-backlog-enter-article
6855                        group article (current-buffer)))
6856                     (when (and gnus-agent
6857                                (gnus-agent-group-covered-p group))
6858                       (gnus-agent-store-article article group)))
6859                   (setq result 'article))
6860                  (methods
6861                   (setq gnus-override-method (pop methods)))
6862                  ((not (string-match "^400 "
6863                                      (nnheader-get-report backend)))
6864                   ;; If we get 400 server disconnect, reconnect and
6865                   ;; retry; otherwise, assume the article has expired.
6866                   (setq result 'done))))
6867               (and (eq result 'article) 'article)))
6868            ;; It was a pseudo.
6869            (t article)))
6870
6871       ;; Associate this article with the current summary buffer.
6872       (setq gnus-article-current-summary gnus-summary-buffer)
6873
6874       ;; Take the article from the original article buffer
6875       ;; and place it in the buffer it's supposed to be in.
6876       (when (and (get-buffer gnus-article-buffer)
6877                  (equal (buffer-name (current-buffer))
6878                         (buffer-name (get-buffer gnus-article-buffer))))
6879         (save-excursion
6880           (if (get-buffer gnus-original-article-buffer)
6881               (set-buffer gnus-original-article-buffer)
6882             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6883             (buffer-disable-undo)
6884             (setq major-mode 'gnus-original-article-mode)
6885             (setq buffer-read-only t))
6886           (let ((inhibit-read-only t))
6887             (erase-buffer)
6888             (insert-buffer-substring gnus-article-buffer))
6889           (setq gnus-original-article (cons group article)))
6890
6891         ;; Decode charsets.
6892         (run-hooks 'gnus-article-decode-hook)
6893         ;; Mark article as decoded or not.
6894         (setq gnus-article-decoded-p gnus-article-decode-hook))
6895
6896       ;; Update sparse articles.
6897       (when (and do-update-line
6898                  (or (numberp article)
6899                      (stringp article)))
6900         (let ((buf (current-buffer)))
6901           (set-buffer gnus-summary-buffer)
6902           (gnus-summary-update-article do-update-line sparse-header)
6903           (gnus-summary-goto-subject do-update-line nil t)
6904           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6905                             (point))
6906           (set-buffer buf))))))
6907
6908 (defun gnus-block-private-groups (group)
6909   (if (gnus-news-group-p group)
6910       ;; Block nothing in news groups.
6911       nil
6912     ;; Block everything anywhere else.
6913     "."))
6914
6915 (defun gnus-blocked-images ()
6916   (if (functionp gnus-blocked-images)
6917       (funcall gnus-blocked-images gnus-newsgroup-name)
6918     gnus-blocked-images))
6919
6920 ;;;
6921 ;;; Article editing
6922 ;;;
6923
6924 (defcustom gnus-article-edit-mode-hook nil
6925   "Hook run in article edit mode buffers."
6926   :group 'gnus-article-various
6927   :type 'hook)
6928
6929 (defvar gnus-article-edit-done-function nil)
6930
6931 (defvar gnus-article-edit-mode-map nil)
6932
6933 ;; Should we be using derived.el for this?
6934 (unless gnus-article-edit-mode-map
6935   (setq gnus-article-edit-mode-map (make-keymap))
6936   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6937
6938   (gnus-define-keys gnus-article-edit-mode-map
6939     "\C-c?"    describe-mode
6940     "\C-c\C-c" gnus-article-edit-done
6941     "\C-c\C-k" gnus-article-edit-exit
6942     "\C-c\C-f\C-t" message-goto-to
6943     "\C-c\C-f\C-o" message-goto-from
6944     "\C-c\C-f\C-b" message-goto-bcc
6945     ;;"\C-c\C-f\C-w" message-goto-fcc
6946     "\C-c\C-f\C-c" message-goto-cc
6947     "\C-c\C-f\C-s" message-goto-subject
6948     "\C-c\C-f\C-r" message-goto-reply-to
6949     "\C-c\C-f\C-n" message-goto-newsgroups
6950     "\C-c\C-f\C-d" message-goto-distribution
6951     "\C-c\C-f\C-f" message-goto-followup-to
6952     "\C-c\C-f\C-m" message-goto-mail-followup-to
6953     "\C-c\C-f\C-k" message-goto-keywords
6954     "\C-c\C-f\C-u" message-goto-summary
6955     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6956     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6957     "\C-c\C-b" message-goto-body
6958     "\C-c\C-i" message-goto-signature
6959
6960     "\C-c\C-t" message-insert-to
6961     "\C-c\C-n" message-insert-newsgroups
6962     "\C-c\C-o" message-sort-headers
6963     "\C-c\C-e" message-elide-region
6964     "\C-c\C-v" message-delete-not-region
6965     "\C-c\C-z" message-kill-to-signature
6966     "\M-\r" message-newline-and-reformat
6967     "\C-c\C-a" mml-attach-file
6968     "\C-a" message-beginning-of-line
6969     "\t" message-tab
6970     "\M-;" comment-region)
6971
6972   (gnus-define-keys (gnus-article-edit-wash-map
6973                      "\C-c\C-w" gnus-article-edit-mode-map)
6974     "f" gnus-article-edit-full-stops))
6975
6976 (easy-menu-define
6977   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6978   '("Field"
6979     ["Fetch To" message-insert-to t]
6980     ["Fetch Newsgroups" message-insert-newsgroups t]
6981     "----"
6982     ["To" message-goto-to t]
6983     ["From" message-goto-from t]
6984     ["Subject" message-goto-subject t]
6985     ["Cc" message-goto-cc t]
6986     ["Reply-To" message-goto-reply-to t]
6987     ["Summary" message-goto-summary t]
6988     ["Keywords" message-goto-keywords t]
6989     ["Newsgroups" message-goto-newsgroups t]
6990     ["Followup-To" message-goto-followup-to t]
6991     ["Mail-Followup-To" message-goto-mail-followup-to t]
6992     ["Distribution" message-goto-distribution t]
6993     ["Body" message-goto-body t]
6994     ["Signature" message-goto-signature t]))
6995
6996 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6997   "Major mode for editing articles.
6998 This is an extended text-mode.
6999
7000 \\{gnus-article-edit-mode-map}"
7001   (make-local-variable 'gnus-article-edit-done-function)
7002   (make-local-variable 'gnus-prev-winconf)
7003   (set (make-local-variable 'font-lock-defaults)
7004        '(message-font-lock-keywords t))
7005   (set (make-local-variable 'mail-header-separator) "")
7006   (set (make-local-variable 'gnus-article-edit-mode) t)
7007   (easy-menu-add message-mode-field-menu message-mode-map)
7008   (mml-mode)
7009   (setq buffer-read-only nil)
7010   (buffer-enable-undo)
7011   (widen))
7012
7013 (defun gnus-article-edit (&optional force)
7014   "Edit the current article.
7015 This will have permanent effect only in mail groups.
7016 If FORCE is non-nil, allow editing of articles even in read-only
7017 groups."
7018   (interactive "P")
7019   (when (and (not force)
7020              (gnus-group-read-only-p))
7021     (error "The current newsgroup does not support article editing"))
7022   (gnus-article-date-original)
7023   (gnus-article-edit-article
7024    'ignore
7025    `(lambda (no-highlight)
7026       'ignore
7027       (gnus-summary-edit-article-done
7028        ,(or (mail-header-references gnus-current-headers) "")
7029        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7030
7031 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7032   "Start editing the contents of the current article buffer."
7033   (let ((winconf (current-window-configuration)))
7034     (set-buffer gnus-article-buffer)
7035     (let ((message-auto-save-directory
7036            ;; Don't associate the article buffer with a draft file.
7037            nil))
7038       (gnus-article-edit-mode))
7039     (funcall start-func)
7040     (set-buffer-modified-p nil)
7041     (gnus-configure-windows 'edit-article)
7042     (setq gnus-article-edit-done-function exit-func)
7043     (setq gnus-prev-winconf winconf)
7044     (unless quiet
7045       (gnus-message 6 "C-c C-c to end edits"))))
7046
7047 (defun gnus-article-edit-done (&optional arg)
7048   "Update the article edits and exit."
7049   (interactive "P")
7050   (let ((func gnus-article-edit-done-function)
7051         (buf (current-buffer))
7052         (start (window-start))
7053         (p (point))
7054         (winconf gnus-prev-winconf))
7055     (widen) ;; Widen it in case that users narrowed the buffer.
7056     (funcall func arg)
7057     (set-buffer buf)
7058     ;; The cache and backlog have to be flushed somewhat.
7059     (when gnus-keep-backlog
7060       (gnus-backlog-remove-article
7061        (car gnus-article-current) (cdr gnus-article-current)))
7062     ;; Flush original article as well.
7063     (gnus-flush-original-article-buffer)
7064     (when gnus-use-cache
7065       (gnus-cache-update-article
7066        (car gnus-article-current) (cdr gnus-article-current)))
7067     ;; We remove all text props from the article buffer.
7068     (kill-all-local-variables)
7069     (set-text-properties (point-min) (point-max) nil)
7070     (gnus-article-mode)
7071     (set-window-configuration winconf)
7072     (set-buffer buf)
7073     (set-window-start (get-buffer-window buf) start)
7074     (set-window-point (get-buffer-window buf) (point)))
7075   (gnus-summary-show-article))
7076
7077 (defun gnus-flush-original-article-buffer ()
7078   (when (get-buffer gnus-original-article-buffer)
7079     (with-current-buffer gnus-original-article-buffer
7080       (setq gnus-original-article nil))))
7081
7082 (defun gnus-article-edit-exit ()
7083   "Exit the article editing without updating."
7084   (interactive)
7085   (when (or (not (buffer-modified-p))
7086             (yes-or-no-p "Article modified; kill anyway? "))
7087     (let ((curbuf (current-buffer))
7088           (p (point))
7089           (window-start (window-start)))
7090       (erase-buffer)
7091       (if (gnus-buffer-live-p gnus-original-article-buffer)
7092           (insert-buffer-substring gnus-original-article-buffer))
7093       (let ((winconf gnus-prev-winconf))
7094         (kill-all-local-variables)
7095         (gnus-article-mode)
7096         (set-window-configuration winconf)
7097         ;; Tippy-toe some to make sure that point remains where it was.
7098         (save-current-buffer
7099           (set-buffer curbuf)
7100           (set-window-start (get-buffer-window (current-buffer)) window-start)
7101           (goto-char p))))
7102     (gnus-summary-show-article)))
7103
7104 (defun gnus-article-edit-full-stops ()
7105   "Interactively repair spacing at end of sentences."
7106   (interactive)
7107   (save-excursion
7108     (goto-char (point-min))
7109     (search-forward-regexp "^$" nil t)
7110     (let ((case-fold-search nil))
7111       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7112
7113 ;;;
7114 ;;; Article highlights
7115 ;;;
7116
7117 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7118
7119 ;;; Internal Variables:
7120
7121 (defcustom gnus-button-url-regexp
7122   (concat
7123    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7124    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7125    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7126    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7127        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7128              (punct "!?:;.,"))
7129          (concat
7130           "\\(?:"
7131           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7132           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7133           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7134           "\\|"
7135           "[" chars punct     "]+" "[" chars "]"
7136           "\\)"))
7137      (concat ;; XEmacs 21.4 doesn't support POSIX.
7138       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7139       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7140    "\\)")
7141   "Regular expression that matches URLs."
7142   :group 'gnus-article-buttons
7143   :type 'regexp)
7144
7145 (defcustom gnus-button-valid-fqdn-regexp
7146   message-valid-fqdn-regexp
7147   "Regular expression that matches a valid FQDN."
7148   :version "22.1"
7149   :group 'gnus-article-buttons
7150   :type 'regexp)
7151
7152 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7153 (defcustom gnus-button-valid-localpart-regexp
7154   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7155   "Regular expression that matches a localpart of mail addresses or MIDs."
7156   :version "22.1"
7157   :group 'gnus-article-buttons
7158   :type 'regexp)
7159
7160 (defcustom gnus-button-man-handler 'manual-entry
7161   "Function to use for displaying man pages.
7162 The function must take at least one argument with a string naming the
7163 man page."
7164   :version "22.1"
7165   :type '(choice (function-item :tag "Man" manual-entry)
7166                  (function-item :tag "Woman" woman)
7167                  (function :tag "Other"))
7168   :group 'gnus-article-buttons)
7169
7170 (defcustom gnus-button-mid-or-mail-regexp
7171   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7172           gnus-button-valid-fqdn-regexp
7173           ">?\\)\\b")
7174   "Regular expression that matches a message ID or a mail address."
7175   :version "22.1"
7176   :group 'gnus-article-buttons
7177   :type 'regexp)
7178
7179 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7180   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7181 Strings like this can be either a message ID or a mail address.  If it is one
7182 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7183 message ID or a mail address, respectively.  If this variable is set to the
7184 symbol `ask', always query the user what do do.  If it is a function, this
7185 function will be called with the string as its only argument.  The function
7186 must return `mid', `mail', `invalid' or `ask'."
7187   :version "22.1"
7188   :group 'gnus-article-buttons
7189   :type '(choice (function-item :tag "Heuristic function"
7190                                 gnus-button-mid-or-mail-heuristic)
7191                  (const ask)
7192                  (const mid)
7193                  (const mail)))
7194
7195 (defcustom gnus-button-mid-or-mail-heuristic-alist
7196   '((-10.0 . ".+\\$.+@")
7197     (-10.0 . "#")
7198     (-10.0 . "\\*")
7199     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7200     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7201     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7202     (-1.0  . "^[^a-z]+@")
7203     ;;
7204     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7205     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7206     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7207     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7208     ;;
7209     (-2.0  . "^[0-9]")
7210     (-1.0  . "^[0-9][0-9]")
7211     ;;
7212     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7213     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7214     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7215     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7216     ;;
7217     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7218     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7219     ;;       "[0-9]{8,}.*\@"
7220     (-3.0
7221      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7222     ;; "[0-9]{12,}.*\@"
7223     ;; compensation for TDMA dated mail addresses:
7224     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7225     ;;
7226     (-20.0 . "\\.fsf@") ;; Gnus
7227     (-20.0 . "^slrn")
7228     (-20.0 . "^Pine")
7229     (-20.0 . "^alpine\\.")
7230     (-20.0 . "_-_") ;; Subject change in thread
7231     ;;
7232     (-20.0 . "\\.ln@") ;; leafnode
7233     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7234     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7235     ;;
7236     ;; (5.0 . "") ;; $local_part_len <= 7
7237     (10.0  . "^[^0-9]+@")
7238     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7239     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7240     (3.0   . "\@stud")
7241     ;;
7242     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7243     ;;
7244     (0.5   . "^[A-Z][a-z]")
7245     (0.5   . "^[A-Z][a-z][a-z]")
7246     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7247     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7248   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7249
7250 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7251 address.  The REGEXP is processed with `case-fold-search' set to nil."
7252   :version "22.1"
7253   :group 'gnus-article-buttons
7254   :type '(repeat (cons (number :tag "Rate")
7255                        (regexp :tag "Regexp"))))
7256
7257 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7258   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7259 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7260 address, `ask' if unsure and `invalid' if the string is invalid."
7261   (let ((case-fold-search nil)
7262         (list gnus-button-mid-or-mail-heuristic-alist)
7263         (result 0) rate regexp lpartlen elem)
7264     (setq lpartlen
7265           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7266     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7267     ;; Certain special cases...
7268     (when (string-match
7269            (concat
7270             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7271             "^[0-9]+\\.[0-9]+@compuserve\\|"
7272             "@public\\.gmane\\.org")
7273            mid-or-mail)
7274       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7275       (setq result 'mail))
7276     (when (string-match "@.*@\\| " mid-or-mail)
7277       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7278       (setq result 'invalid))
7279     ;; Nothing more to do, if result is not a number here...
7280     (when (numberp result)
7281       (while list
7282         (setq elem (car list)
7283               rate (car elem)
7284               regexp (cdr elem)
7285               list (cdr list))
7286         (when (string-match regexp mid-or-mail)
7287           (setq result (+ result rate))
7288           (gnus-message
7289            9 "`%s' matched `%s', rate `%s', result `%s'."
7290            mid-or-mail regexp rate result)))
7291       (when (<= lpartlen 7)
7292         (setq result (+ result 5.0))
7293         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7294                       mid-or-mail result))
7295       (when (>= lpartlen 12)
7296         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7297         (cond
7298          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7299           ;; Long local part should contain realname if e-mail address,
7300           ;; too many digits: message-id.
7301           ;; $score -= 5.0 + 0.1 * $local_part_len;
7302           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7303           (setq result (+ result rate))
7304           (gnus-message
7305            9 "Many digits in `%s', rate `%s', result `%s'."
7306            mid-or-mail rate result))
7307          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7308                         mid-or-mail)
7309           ;; Too few vowels [^aeiouy]{4,}.*\@
7310           (setq result (+ result -5.0))
7311           (gnus-message
7312            9 "Few vowels in `%s', rate `%s', result `%s'."
7313            mid-or-mail -5.0 result))
7314          (t
7315           (setq result (+ result 5.0))
7316           (gnus-message
7317            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7318     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7319     ;; Maybe we should make this a customizable alist: (condition . 'result)
7320     (cond
7321      ((symbolp result) result)
7322      ;; Now convert number into proper results:
7323      ((< result -10.0) 'mid)
7324      ((> result  10.0) 'mail)
7325      (t 'ask))))
7326
7327 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7328   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7329          (url-mid (concat "news" ":" mid-or-mail))
7330          (url-mailto (concat "mailto" ":" mid-or-mail)))
7331     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7332     (when (fboundp pref)
7333       (setq guessed
7334             ;; get rid of surrounding angles...
7335             (funcall pref
7336                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7337       (if (or (eq 'mid guessed) (eq 'mail guessed))
7338           (setq pref guessed)
7339         (setq pref 'ask)))
7340     (if (eq pref 'ask)
7341         (save-window-excursion
7342           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7343               (setq pref 'mail)
7344             (setq pref 'mid))))
7345     (cond ((eq pref 'mid)
7346            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7347            (gnus-button-handle-news url-mid))
7348           ((eq pref 'mail)
7349            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7350            (gnus-url-mailto url-mailto))
7351           (t (gnus-message 3 "Invalid string.")))))
7352
7353 (defun gnus-button-handle-custom (fun arg)
7354   "Call function FUN on argument ARG.
7355 Both FUN and ARG are supposed to be strings.  ARG will be passed
7356 as a symbol to FUN."
7357   (funcall (intern fun)
7358            (if (string-match "^customize-apropos" fun)
7359                arg
7360              (intern arg))))
7361
7362 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7363
7364 (defun gnus-button-handle-describe-function (url)
7365   "Call `describe-function' when pushing the corresponding URL button."
7366   (describe-function
7367    (intern
7368     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7369
7370 (defun gnus-button-handle-describe-variable (url)
7371   "Call `describe-variable' when pushing the corresponding URL button."
7372   (describe-variable
7373    (intern
7374     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7375
7376 (defun gnus-button-handle-symbol (url)
7377 "Display help on variable or function.
7378 Calls `describe-variable' or `describe-function'."
7379   (let ((sym (intern url)))
7380     (cond
7381      ((fboundp sym) (describe-function sym))
7382      ((boundp sym) (describe-variable sym))
7383      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7384
7385 (defun gnus-button-handle-describe-key (url)
7386   "Call `describe-key' when pushing the corresponding URL button."
7387   (let* ((key-string
7388           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7389          (keys (ignore-errors (eval `(kbd ,key-string)))))
7390     (if keys
7391         (describe-key keys)
7392       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7393
7394 (defun gnus-button-handle-apropos (url)
7395   "Call `apropos' when pushing the corresponding URL button."
7396   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7397
7398 (defun gnus-button-handle-apropos-command (url)
7399   "Call `apropos' when pushing the corresponding URL button."
7400   (apropos-command
7401    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7402
7403 (defun gnus-button-handle-apropos-variable (url)
7404   "Call `apropos' when pushing the corresponding URL button."
7405   (funcall
7406    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7407    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7408
7409 (defun gnus-button-handle-apropos-documentation (url)
7410   "Call `apropos' when pushing the corresponding URL button."
7411   (funcall
7412    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7413    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7414
7415 (defun gnus-button-handle-library (url)
7416   "Call `locate-library' when pushing the corresponding URL button."
7417   (gnus-message 9 "url=`%s'" url)
7418   (let* ((lib (locate-library url))
7419          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7420     (if (not lib)
7421         (gnus-message 1 "Cannot locale library `%s'." url)
7422       (find-file-read-only file))))
7423
7424 (defcustom gnus-button-man-level 5
7425   "*Integer that says how many man-related buttons Gnus will show.
7426 The higher the number, the more buttons will appear and the more false
7427 positives are possible.  Note that you can set this variable local to
7428 specific groups.  Setting it higher in Unix groups is probably a good idea.
7429 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7430 how to set variables in specific groups."
7431   :version "22.1"
7432   :group 'gnus-article-buttons
7433   :link '(custom-manual "(gnus)Group Parameters")
7434   :type 'integer)
7435
7436 (defcustom gnus-button-emacs-level 5
7437   "*Integer that says how many emacs-related buttons Gnus will show.
7438 The higher the number, the more buttons will appear and the more false
7439 positives are possible.  Note that you can set this variable local to
7440 specific groups.  Setting it higher in Emacs or Gnus related groups is
7441 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7442 `gnus-parameters' on how to set variables in specific groups."
7443   :version "22.1"
7444   :group 'gnus-article-buttons
7445   :link '(custom-manual "(gnus)Group Parameters")
7446   :type 'integer)
7447
7448 (defcustom gnus-button-message-level 5
7449   "*Integer that says how many buttons for news or mail messages will appear.
7450 The higher the number, the more buttons will appear and the more false
7451 positives are possible."
7452   ;; mail addresses, MIDs, URLs for news, ...
7453   :version "22.1"
7454   :group 'gnus-article-buttons
7455   :type 'integer)
7456
7457 (defcustom gnus-button-browse-level 5
7458   "*Integer that says how many buttons for browsing will appear.
7459 The higher the number, the more buttons will appear and the more false
7460 positives are possible."
7461   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7462   :version "22.1"
7463   :group 'gnus-article-buttons
7464   :type 'integer)
7465
7466 (defcustom gnus-button-alist
7467   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7468      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7469     ((concat "\\b\\(nntp\\|news\\):\\("
7470              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7471      0 t gnus-button-handle-news 2)
7472     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7473      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7474     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7475      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7476     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7477     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7478      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7479     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7480      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7481     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7482      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7483     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7484      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7485     ;; RFC 2368 (The mailto URL scheme)
7486     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7487      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7488     ("\\bmailto:\\([^ \n\t]+\\)"
7489      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7490     ;; Info Konqueror style <info:/foo/bar baz>.
7491     ;; Must come before " Gnus home-grown style".
7492     ("\\binfo://?\\([^'\">\n\t]+\\)"
7493      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7494    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7495     ("\\binfo://\\([^'\">\n\t ]+\\)"
7496      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7497     ;; Info GNOME style <info:foo#bar_baz>
7498     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7499      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7500     ;; Info KDE style <info:(foo)bar baz>
7501     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7502      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7503     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7504      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7505     ("\\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,]*\\)\\)?"
7506      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7507      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7508     ;; This is custom
7509     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7510      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7511     ;; Emacs help commands
7512     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7513      ;; regexp doesn't match arguments containing ` '.
7514      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7515     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7516      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7517     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7518      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7519     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7520      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7521     ;; The following entries may lead to many false positives so don't enable
7522     ;; them by default (use a high button level).
7523     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7524      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7525      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7526     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7527      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7528     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7529      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7530     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7531      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7532     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7533      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7534     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7535      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7536     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7537      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7538     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7539      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7540     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7541      ;; Unlike the other regexps we really have to require quoting
7542      ;; here to determine where it ends.
7543      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7544     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7545     ("<URL: *\\([^\n<>]*\\)>"
7546      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7547     ;; RFC 2396 (2.4.3., delims) ...
7548     ("\"URL: *\\([^\n\"]*\\)\""
7549      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7550     ;; Raw URLs.
7551     (gnus-button-url-regexp
7552      0 (>= gnus-button-browse-level 0) browse-url 0)
7553     ;; man pages
7554     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7555      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7556      gnus-button-handle-man 1)
7557     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7558     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7559      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7560      gnus-button-handle-man 1)
7561     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7562     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7563     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7564      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7565     ;; Recognizing patches to .el files.  This is somewhat obscure,
7566     ;; but considering the percentage of Gnus users who hack Emacs
7567     ;; Lisp files...
7568     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7569      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7570     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7571      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7572     ;; MID or mail: To avoid too many false positives we don't try to catch
7573     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7574     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7575     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7576     ;; so that non-ambiguous entries (see above) match first.
7577     (gnus-button-mid-or-mail-regexp
7578      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7579   "*Alist of regexps matching buttons in article bodies.
7580
7581 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7582 REGEXP: is the string (case insensitive) matching text around the button (can
7583 also be Lisp expression evaluating to a string),
7584 BUTTON: is the number of the regexp grouping actually matching the button,
7585 FORM: is a Lisp expression which must eval to true for the button to
7586 be added,
7587 CALLBACK: is the function to call when the user push this button, and each
7588 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7589
7590 CALLBACK can also be a variable, in that case the value of that
7591 variable it the real callback function."
7592   :group 'gnus-article-buttons
7593   :type '(repeat (list (choice regexp variable sexp)
7594                        (integer :tag "Button")
7595                        (sexp :tag "Form")
7596                        (function :tag "Callback")
7597                        (repeat :tag "Par"
7598                                :inline t
7599                                (integer :tag "Regexp group")))))
7600 (put 'gnus-button-alist 'risky-local-variable t)
7601
7602 (defcustom gnus-header-button-alist
7603   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7604      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7605     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7606      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7607     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7608      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7609     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7610      0 (>= gnus-button-browse-level 0) browse-url 0)
7611     ("^Subject:" gnus-button-url-regexp
7612      0 (>= gnus-button-browse-level 0) browse-url 0)
7613     ("^[^:]+:" gnus-button-url-regexp
7614      0 (>= gnus-button-browse-level 0) browse-url 0)
7615     ("^OpenPGP:.*url=" gnus-button-url-regexp
7616      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7617     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7618      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7619     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7620      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7621   "*Alist of headers and regexps to match buttons in article heads.
7622
7623 This alist is very similar to `gnus-button-alist', except that each
7624 alist has an additional HEADER element first in each entry:
7625
7626 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7627
7628 HEADER is a regexp to match a header.  For a fuller explanation, see
7629 `gnus-button-alist'."
7630   :group 'gnus-article-buttons
7631   :group 'gnus-article-headers
7632   :type '(repeat (list (regexp :tag "Header")
7633                        (choice regexp variable)
7634                        (integer :tag "Button")
7635                        (sexp :tag "Form")
7636                        (function :tag "Callback")
7637                        (repeat :tag "Par"
7638                                :inline t
7639                                (integer :tag "Regexp group")))))
7640 (put 'gnus-header-button-alist 'risky-local-variable t)
7641
7642 ;;; Commands:
7643
7644 (defun gnus-article-push-button (event)
7645   "Check text under the mouse pointer for a callback function.
7646 If the text under the mouse pointer has a `gnus-callback' property,
7647 call it with the value of the `gnus-data' text property."
7648   (interactive "e")
7649   (set-buffer (window-buffer (posn-window (event-start event))))
7650   (let* ((pos (posn-point (event-start event)))
7651          (data (get-text-property pos 'gnus-data))
7652          (fun (get-text-property pos 'gnus-callback)))
7653     (goto-char pos)
7654     (when fun
7655       (funcall fun data))))
7656
7657 (defun gnus-article-press-button ()
7658   "Check text at point for a callback function.
7659 If the text at point has a `gnus-callback' property,
7660 call it with the value of the `gnus-data' text property."
7661   (interactive)
7662   (let ((data (get-text-property (point) 'gnus-data))
7663         (fun (get-text-property (point) 'gnus-callback)))
7664     (when fun
7665       (funcall fun data))))
7666
7667 (defun gnus-article-highlight (&optional force)
7668   "Highlight current article.
7669 This function calls `gnus-article-highlight-headers',
7670 `gnus-article-highlight-citation',
7671 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7672 do the highlighting.  See the documentation for those functions."
7673   (interactive (list 'force))
7674   (gnus-article-highlight-headers)
7675   (gnus-article-highlight-citation force)
7676   (gnus-article-highlight-signature)
7677   (gnus-article-add-buttons)
7678   (gnus-article-add-buttons-to-head))
7679
7680 (defun gnus-article-highlight-some (&optional force)
7681   "Highlight current article.
7682 This function calls `gnus-article-highlight-headers',
7683 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7684 do the highlighting.  See the documentation for those functions."
7685   (interactive (list 'force))
7686   (gnus-article-highlight-headers)
7687   (gnus-article-highlight-signature)
7688   (gnus-article-add-buttons))
7689
7690 (defun gnus-article-highlight-headers ()
7691   "Highlight article headers as specified by `gnus-header-face-alist'."
7692   (interactive)
7693   (gnus-with-article-headers
7694     (let (regexp header-face field-face from hpoints fpoints)
7695       (dolist (entry gnus-header-face-alist)
7696         (goto-char (point-min))
7697         (setq regexp (concat "^\\("
7698                              (if (string-equal "" (nth 0 entry))
7699                                  "[^\t ]"
7700                                (nth 0 entry))
7701                              "\\)")
7702               header-face (nth 1 entry)
7703               field-face (nth 2 entry))
7704         (while (and (re-search-forward regexp nil t)
7705                     (not (eobp)))
7706           (beginning-of-line)
7707           (setq from (point))
7708           (unless (search-forward ":" nil t)
7709             (forward-char 1))
7710           (when (and header-face
7711                      (not (memq (point) hpoints)))
7712             (push (point) hpoints)
7713             (gnus-put-text-property from (point) 'face header-face))
7714           (when (and field-face
7715                      (not (memq (setq from (point)) fpoints)))
7716             (push from fpoints)
7717             (if (re-search-forward "^[^ \t]" nil t)
7718                 (forward-char -2)
7719               (goto-char (point-max)))
7720             (gnus-put-text-property from (point) 'face field-face)))))))
7721
7722 (defun gnus-article-highlight-signature ()
7723   "Highlight the signature in an article.
7724 It does this by highlighting everything after
7725 `gnus-signature-separator' using the face `gnus-signature'."
7726   (interactive)
7727   (gnus-with-article-buffer
7728     (let ((inhibit-point-motion-hooks t))
7729       (save-restriction
7730         (when (and gnus-signature-face
7731                    (gnus-article-narrow-to-signature))
7732           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7733                             'face gnus-signature-face)
7734           (widen)
7735           (gnus-article-search-signature)
7736           (let ((start (match-beginning 0))
7737                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7738             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7739                                      end)))))))
7740
7741 (defun gnus-button-in-region-p (b e prop)
7742   "Say whether PROP exists in the region."
7743   (text-property-not-all b e prop nil))
7744
7745 (defun gnus-article-add-buttons ()
7746   "Find external references in the article and make buttons of them.
7747 \"External references\" are things like Message-IDs and URLs, as
7748 specified by `gnus-button-alist'."
7749   (interactive)
7750   (gnus-with-article-buffer
7751     (let ((inhibit-point-motion-hooks t)
7752           (case-fold-search t)
7753           (alist gnus-button-alist)
7754           beg entry regexp)
7755       ;; We skip the headers.
7756       (article-goto-body)
7757       (setq beg (point))
7758       (while (setq entry (pop alist))
7759         (setq regexp (eval (car entry)))
7760         (goto-char beg)
7761         (while (re-search-forward regexp nil t)
7762           (let ((start (match-beginning (nth 1 entry)))
7763                 (end (match-end (nth 1 entry)))
7764                 (from (match-beginning 0)))
7765             (when (and (eval (nth 2 entry))
7766                        (not (gnus-button-in-region-p
7767                              start end 'gnus-callback)))
7768               ;; That optional form returned non-nil, so we add the
7769               ;; button.
7770               (setq from (set-marker (make-marker) from))
7771               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7772                            (gnus-article-extend-url-button from start end))
7773                 (gnus-article-add-button start end
7774                                          'gnus-button-push (list from entry))
7775                 (gnus-put-text-property
7776                  start end
7777                  'gnus-string (buffer-substring-no-properties
7778                                start end))))))))))
7779
7780 (defun gnus-article-extend-url-button (beg start end)
7781   "Extend url button if url is folded into two or more lines.
7782 Return non-nil if button is extended.  BEG is a marker that points to
7783 the beginning position of a text containing url.  START and END are
7784 the endpoints of a url button before it is extended.  The concatenated
7785 url is put as the `gnus-button-url' overlay property on the button."
7786   (let ((opoint (point))
7787         (points (list start end))
7788         url delim regexp)
7789     (prog1
7790         (when (and (progn
7791                      (goto-char end)
7792                      (not (looking-at "[\t ]*[\">]")))
7793                    (progn
7794                      (goto-char start)
7795                      (string-match
7796                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7797                       (buffer-substring (point-at-bol) start)))
7798                    (progn
7799                      (setq url (list (buffer-substring start end))
7800                            delim (if (match-beginning 1) ">" "\""))
7801                      (beginning-of-line)
7802                      (setq regexp (concat
7803                                    (when (and (looking-at
7804                                                message-cite-prefix-regexp)
7805                                               (< (match-end 0) start))
7806                                      (regexp-quote (match-string 0)))
7807                                    "\
7808 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7809                                    delim "\\)"))
7810                      (while (progn
7811                               (forward-line 1)
7812                               (and (looking-at regexp)
7813                                    (prog1
7814                                        (match-beginning 1)
7815                                      (push (or (match-string 2)
7816                                                (match-string 1))
7817                                            url)
7818                                      (push (setq end (or (match-end 2)
7819                                                          (match-end 1)))
7820                                            points)
7821                                      (push (or (match-beginning 2)
7822                                                (match-beginning 1))
7823                                            points)))))
7824                      (match-beginning 2)))
7825           (let (gnus-article-mouse-face widget-mouse-face)
7826             (while points
7827               (gnus-article-add-button (pop points) (pop points)
7828                                        'gnus-button-push beg)))
7829           (let ((overlay (gnus-make-overlay start end)))
7830             (gnus-overlay-put overlay 'evaporate t)
7831             (gnus-overlay-put overlay 'gnus-button-url
7832                               (list (mapconcat 'identity (nreverse url) "")))
7833             (when gnus-article-mouse-face
7834               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7835           t)
7836       (goto-char opoint))))
7837
7838 ;; Add buttons to the head of an article.
7839 (defun gnus-article-add-buttons-to-head ()
7840   "Add buttons to the head of the article."
7841   (interactive)
7842   (gnus-with-article-headers
7843     (let (beg end)
7844       (dolist (entry gnus-header-button-alist)
7845         ;; Each alist entry.
7846         (goto-char (point-min))
7847         (while (re-search-forward (car entry) nil t)
7848           ;; Each header matching the entry.
7849           (setq beg (match-beginning 0))
7850           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7851                              (match-beginning 0))
7852                         (point-max)))
7853           (goto-char beg)
7854           (while (re-search-forward (eval (nth 1 entry)) end t)
7855             ;; Each match within a header.
7856             (let* ((entry (cdr entry))
7857                    (start (match-beginning (nth 1 entry)))
7858                    (end (match-end (nth 1 entry)))
7859                    (form (nth 2 entry)))
7860               (goto-char (match-end 0))
7861               (when (eval form)
7862                 (gnus-article-add-button
7863                  start end (nth 3 entry)
7864                  (buffer-substring (match-beginning (nth 4 entry))
7865                                    (match-end (nth 4 entry)))))))
7866           (goto-char end))))))
7867
7868 ;;; External functions:
7869
7870 (defun gnus-article-add-button (from to fun &optional data text)
7871   "Create a button between FROM and TO with callback FUN and data DATA."
7872   (when gnus-article-button-face
7873     (gnus-overlay-put (gnus-make-overlay from to nil t)
7874                       'face gnus-article-button-face))
7875   (gnus-add-text-properties
7876    from to
7877    (nconc (and gnus-article-mouse-face
7878                (list gnus-mouse-face-prop gnus-article-mouse-face))
7879           (list 'gnus-callback fun)
7880           (and data (list 'gnus-data data))))
7881   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7882                          :help-echo (or text "Follow the link")
7883                          :keymap gnus-url-button-map
7884                          :button-keymap gnus-widget-button-keymap))
7885
7886 (defun gnus-article-copy-string ()
7887   "Copy the string in the button to the kill ring."
7888   (interactive)
7889   (gnus-article-check-buffer)
7890   (let ((data (get-text-property (point) 'gnus-string)))
7891     (when data
7892       (with-temp-buffer
7893         (insert data)
7894         (copy-region-as-kill (point-min) (point-max))
7895         (message "Copied %s" data)))))
7896
7897 ;;; Internal functions:
7898
7899 (defun gnus-article-set-globals ()
7900   (with-current-buffer gnus-summary-buffer
7901     (gnus-set-global-variables)))
7902
7903 (defun gnus-signature-toggle (end)
7904   (gnus-with-article-buffer
7905     (let ((inhibit-point-motion-hooks t))
7906       (if (text-property-any end (point-max) 'article-type 'signature)
7907           (progn
7908             (gnus-delete-wash-type 'signature)
7909             (gnus-remove-text-properties-when
7910              'article-type 'signature end (point-max)
7911              (cons 'article-type (cons 'signature
7912                                        gnus-hidden-properties))))
7913         (gnus-add-wash-type 'signature)
7914         (gnus-add-text-properties-when
7915          'article-type nil end (point-max)
7916          (cons 'article-type (cons 'signature
7917                                    gnus-hidden-properties)))))
7918     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7919       (gnus-set-mode-line 'article))))
7920
7921 (defun gnus-button-push (marker-and-entry)
7922   ;; Push button starting at MARKER.
7923   (save-excursion
7924     (let* ((marker (car marker-and-entry))
7925            (entry (cadr marker-and-entry))
7926            (regexp (car entry))
7927            (inhibit-point-motion-hooks t))
7928       (goto-char marker)
7929       ;; This is obviously true, or something bad is happening :)
7930       ;; But we need it to have the match-data
7931       (when (looking-at (or (if (symbolp regexp)
7932                                 (symbol-value regexp)
7933                               regexp)))
7934         (let ((fun (nth 3 entry))
7935               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7936                              (get-char-property marker 'gnus-button-url))
7937                         (mapcar (lambda (group)
7938                                   (let ((string (match-string group)))
7939                                     (set-text-properties
7940                                      0 (length string) nil string)
7941                                     string))
7942                                 (nthcdr 4 entry)))))
7943
7944           (cond
7945            ((fboundp fun)
7946             (apply fun args))
7947            ((and (boundp fun)
7948                  (fboundp (symbol-value fun)))
7949             (apply (symbol-value fun) args))
7950            (t
7951             (gnus-message 1 "You must define `%S' to use this button"
7952                           (cons fun args)))))))))
7953
7954 (defun gnus-parse-news-url (url)
7955   (let (scheme server port group message-id articles)
7956     (with-temp-buffer
7957       (insert url)
7958       (goto-char (point-min))
7959       (when (looking-at "\\([A-Za-z]+\\):")
7960         (setq scheme (match-string 1))
7961         (goto-char (match-end 0)))
7962       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7963         (setq server (match-string 1))
7964         (setq port (if (stringp (match-string 3))
7965                        (string-to-number (match-string 3))
7966                      (match-string 3)))
7967         (goto-char (match-end 0)))
7968
7969       (cond
7970        ((looking-at "\\(.*@.*\\)")
7971         (setq message-id (match-string 1)))
7972        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7973         (setq group (match-string 1)
7974               articles (split-string (match-string 2) "-")))
7975        ((looking-at "\\([^/]+\\)/?")
7976         (setq group (match-string 1)))
7977        (t
7978         (error "Unknown news URL syntax"))))
7979     (list scheme server port group message-id articles)))
7980
7981 (defun gnus-button-handle-news (url)
7982   "Fetch a news URL."
7983   (destructuring-bind (scheme server port group message-id articles)
7984       (gnus-parse-news-url url)
7985     (cond
7986      (message-id
7987       (with-current-buffer gnus-summary-buffer
7988         (if server
7989             (let ((gnus-refer-article-method
7990                    (nconc (list (list 'nntp server))
7991                           gnus-refer-article-method))
7992                   (nntp-port-number (or port "nntp")))
7993               (gnus-message 7 "Fetching %s with %s"
7994                             message-id gnus-refer-article-method)
7995               (gnus-summary-refer-article message-id))
7996           (gnus-summary-refer-article message-id))))
7997      (group
7998       (gnus-button-fetch-group url)))))
7999
8000 (defun gnus-button-patch (library line)
8001   "Visit an Emacs Lisp library LIBRARY on line LINE."
8002   (interactive)
8003   (let ((file (locate-library (file-name-nondirectory library))))
8004     (unless file
8005       (error "Couldn't find library %s" library))
8006     (find-file file)
8007     (goto-char (point-min))
8008     (forward-line (1- (string-to-number line)))))
8009
8010 (defun gnus-button-handle-man (url)
8011   "Fetch a man page."
8012   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8013   (when (eq gnus-button-man-handler 'woman)
8014     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8015   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8016   (funcall gnus-button-man-handler url))
8017
8018 (defun gnus-button-handle-info-url (url)
8019   "Fetch an info URL."
8020   (setq url (mm-subst-char-in-string ?+ ?\  url))
8021   (cond
8022    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8023     (gnus-info-find-node
8024      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8025                      "Gnus")
8026              ")" (gnus-url-unhex-string (match-string 2 url)))))
8027    ((string-match "([^)\"]+)[^\"]+" url)
8028     (setq url
8029           (gnus-replace-in-string
8030            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8031     (gnus-info-find-node url))
8032    (t (error "Can't parse %s" url))))
8033
8034 (defun gnus-button-handle-info-url-gnome (url)
8035   "Fetch GNOME style info URL."
8036   (setq url (mm-subst-char-in-string ?_ ?\  url))
8037   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8038       (gnus-info-find-node
8039        (concat "("
8040                (gnus-url-unhex-string
8041                  (match-string 1 url))
8042                ")"
8043                (or (gnus-url-unhex-string
8044                     (match-string 2 url))
8045                    "Top")))
8046     (error "Can't parse %s" url)))
8047
8048 (defun gnus-button-handle-info-url-kde (url)
8049   "Fetch KDE style info URL."
8050   (gnus-info-find-node (gnus-url-unhex-string url)))
8051
8052 ;; (info) will autoload info.el
8053 (declare-function Info-menu "info" (menu-item &optional fork))
8054 (declare-function Info-index-next "info" (num))
8055
8056 (defun gnus-button-handle-info-keystrokes (url)
8057   "Call `info' when pushing the corresponding URL button."
8058   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8059   (let (node indx comma)
8060     (if (string-match
8061          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8062                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8063                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8064                  "\\(?:[ \t\n,]*\\)\\)?")
8065          url)
8066         (setq node (match-string 2 url)
8067               indx (match-string 3 url))
8068       (error "Can't parse %s" url))
8069     (info)
8070     (Info-directory)
8071     (Info-menu node)
8072     (when (> (length indx) 0)
8073       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8074                             "\\([ \t\n,]*\\)")
8075                     indx)
8076       (setq comma (match-string 2 indx))
8077       (setq indx  (match-string 1 indx))
8078       (Info-index indx)
8079       (when comma
8080         (dotimes (i (with-temp-buffer
8081                       (insert comma)
8082                       ;; Note: the XEmacs version of `how-many' takes
8083                       ;; no optional argument.
8084                       (goto-char (point-min))
8085                       (how-many ",")))
8086           (Info-index-next 1)))
8087       nil)))
8088
8089 (autoload 'pgg-snarf-keys-region "pgg")
8090 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8091 (declare-function pgg-display-output-buffer "pgg" (start end status))
8092
8093 (defun gnus-button-openpgp (url)
8094   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8095   (with-temp-buffer
8096     (mm-url-insert-file-contents-external url)
8097     (pgg-snarf-keys-region (point-min) (point-max))
8098     (pgg-display-output-buffer nil nil nil)))
8099
8100 (defun gnus-button-message-id (message-id)
8101   "Fetch MESSAGE-ID."
8102   (with-current-buffer gnus-summary-buffer
8103     (gnus-summary-refer-article message-id)))
8104
8105 (defun gnus-button-fetch-group (address &rest ignore)
8106   "Fetch GROUP specified by ADDRESS."
8107   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8108                       address)
8109     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8110     ;; for nntp:// and news://
8111     (setq address (match-string 3 address)))
8112   (if (not (string-match "[:/]" address))
8113       ;; This is just a simple group url.
8114       (gnus-group-read-ephemeral-group address gnus-select-method)
8115     (if (not
8116          (string-match
8117           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8118           address))
8119         (error "Can't parse %s" address)
8120       (gnus-group-read-ephemeral-group
8121        (match-string 4 address)
8122        `(nntp ,(match-string 1 address)
8123               (nntp-address ,(match-string 1 address))
8124               (nntp-port-number ,(if (match-end 3)
8125                                      (match-string 3 address)
8126                                    "nntp")))
8127        nil nil nil
8128        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8129
8130 (defun gnus-url-parse-query-string (query &optional downcase)
8131   (let (retval pairs cur key val)
8132     (setq pairs (split-string query "&"))
8133     (while pairs
8134       (setq cur (car pairs)
8135             pairs (cdr pairs))
8136       (if (not (string-match "=" cur))
8137           nil                           ; Grace
8138         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8139               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8140         (if downcase
8141             (setq key (downcase key)))
8142         (setq cur (assoc key retval))
8143         (if cur
8144             (setcdr cur (cons val (cdr cur)))
8145           (setq retval (cons (list key val) retval)))))
8146     retval))
8147
8148 (defun gnus-url-mailto (url)
8149   ;; Send mail to someone
8150   (setq url (replace-regexp-in-string "\n" " " url))
8151   (when (string-match "mailto:/*\\(.*\\)" url)
8152     (setq url (substring url (match-beginning 1) nil)))
8153   (let (to args subject func)
8154     (setq args (gnus-url-parse-query-string
8155                 (if (string-match "^\\?" url)
8156                     (substring url 1)
8157                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8158                       (concat "to=" (match-string 1 url) "&"
8159                               (match-string 2 url))
8160                     (concat "to=" url))))
8161           subject (cdr-safe (assoc "subject" args)))
8162     (gnus-msg-mail)
8163     (while args
8164       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8165       (if (fboundp func)
8166           (funcall func)
8167         (message-position-on-field (caar args)))
8168       (insert (gnus-replace-in-string
8169                (mapconcat 'identity (reverse (cdar args)) ", ")
8170                "\r\n" "\n" t))
8171       (setq args (cdr args)))
8172     (if subject
8173         (message-goto-body)
8174       (message-goto-subject))))
8175
8176 (defun gnus-button-embedded-url (address)
8177   "Activate ADDRESS with `browse-url'."
8178   (browse-url (gnus-strip-whitespace address)))
8179
8180 ;;; Next/prev buttons in the article buffer.
8181
8182 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8183 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8184
8185 (defvar gnus-prev-page-map
8186   (let ((map (make-sparse-keymap)))
8187     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8188     (define-key map "\r" 'gnus-button-prev-page)
8189     map))
8190
8191 (defvar gnus-next-page-map
8192   (let ((map (make-sparse-keymap)))
8193     (define-key map gnus-mouse-2 'gnus-button-next-page)
8194     (define-key map "\r" 'gnus-button-next-page)
8195     map))
8196
8197 (defun gnus-insert-prev-page-button ()
8198   (let ((b (point)) e
8199         (inhibit-read-only t))
8200     (gnus-eval-format
8201      gnus-prev-page-line-format nil
8202      `(keymap ,gnus-prev-page-map
8203               gnus-prev t
8204               gnus-callback gnus-article-button-prev-page
8205               article-type annotation))
8206     (setq e (if (bolp)
8207                 ;; Exclude a newline.
8208                 (1- (point))
8209               (point)))
8210     (when gnus-article-button-face
8211       (gnus-overlay-put (gnus-make-overlay b e nil t)
8212                         'face gnus-article-button-face))
8213     (widget-convert-button
8214      'link b e
8215      :action 'gnus-button-prev-page
8216      :button-keymap gnus-prev-page-map)))
8217
8218 (defun gnus-button-next-page (&optional args more-args)
8219   "Go to the next page."
8220   (interactive)
8221   (let ((win (selected-window)))
8222     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8223     (gnus-article-next-page)
8224     (select-window win)))
8225
8226 (defun gnus-button-prev-page (&optional args more-args)
8227   "Go to the prev page."
8228   (interactive)
8229   (let ((win (selected-window)))
8230     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8231     (gnus-article-prev-page)
8232     (select-window win)))
8233
8234 (defun gnus-insert-next-page-button ()
8235   (let ((b (point)) e
8236         (inhibit-read-only t))
8237     (gnus-eval-format gnus-next-page-line-format nil
8238                       `(keymap ,gnus-next-page-map
8239                                gnus-next t
8240                                gnus-callback gnus-article-button-next-page
8241                                article-type annotation))
8242     (setq e (if (bolp)
8243                 ;; Exclude a newline.
8244                 (1- (point))
8245               (point)))
8246     (when gnus-article-button-face
8247       (gnus-overlay-put (gnus-make-overlay b e nil t)
8248                         'face gnus-article-button-face))
8249     (widget-convert-button
8250      'link b e
8251      :action 'gnus-button-next-page
8252      :button-keymap gnus-next-page-map)))
8253
8254 (defun gnus-article-button-next-page (arg)
8255   "Go to the next page."
8256   (interactive "P")
8257   (let ((win (selected-window)))
8258     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8259     (gnus-article-next-page)
8260     (select-window win)))
8261
8262 (defun gnus-article-button-prev-page (arg)
8263   "Go to the prev page."
8264   (interactive "P")
8265   (let ((win (selected-window)))
8266     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8267     (gnus-article-prev-page)
8268     (select-window win)))
8269
8270 (defvar gnus-decode-header-methods
8271   '(mail-decode-encoded-word-region)
8272   "List of methods used to decode headers.
8273
8274 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8275 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8276 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8277 whose names match REGEXP.
8278
8279 For example:
8280 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8281  mail-decode-encoded-word-region
8282  (\"chinese\" . rfc1843-decode-region))
8283 ")
8284
8285 (defvar gnus-decode-header-methods-cache nil)
8286
8287 (defun gnus-multi-decode-header (start end)
8288   "Apply the functions from `gnus-encoded-word-methods' that match."
8289   (unless (and gnus-decode-header-methods-cache
8290                (eq gnus-newsgroup-name
8291                    (car gnus-decode-header-methods-cache)))
8292     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8293     (dolist (x gnus-decode-header-methods)
8294       (if (symbolp x)
8295           (nconc gnus-decode-header-methods-cache (list x))
8296         (if (and gnus-newsgroup-name
8297                  (string-match (car x) gnus-newsgroup-name))
8298             (nconc gnus-decode-header-methods-cache
8299                    (list (cdr x)))))))
8300   (let ((xlist gnus-decode-header-methods-cache))
8301     (pop xlist)
8302     (save-restriction
8303       (narrow-to-region start end)
8304       (while xlist
8305         (funcall (pop xlist) (point-min) (point-max))))))
8306
8307 ;;;
8308 ;;; Treatment top-level handling.
8309 ;;;
8310
8311 (defvar gnus-inhibit-article-treatments nil)
8312
8313 (defun gnus-treat-article (gnus-treat-condition
8314                            &optional part-number total-parts gnus-treat-type)
8315   (let ((gnus-treat-length (- (point-max) (point-min)))
8316         (alist gnus-treatment-function-alist)
8317         (article-goto-body-goes-to-point-min-p t)
8318         (treated-type
8319          (or (not gnus-treat-type)
8320              (catch 'found
8321                (let ((list gnus-article-treat-types))
8322                  (while list
8323                    (when (string-match (pop list) gnus-treat-type)
8324                      (throw 'found t)))))))
8325         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8326         val elem)
8327     (gnus-run-hooks 'gnus-part-display-hook)
8328     (dolist (elem alist)
8329       (setq val
8330             (save-excursion
8331               (when (gnus-buffer-live-p gnus-summary-buffer)
8332                 (set-buffer gnus-summary-buffer))
8333               (symbol-value (car elem))))
8334       (when (and (or (consp val)
8335                      treated-type)
8336                  (or (not gnus-inhibit-article-treatments)
8337                      (eq gnus-treat-condition 'head))
8338                  (gnus-treat-predicate val)
8339                  (or (not (get (car elem) 'highlight))
8340                      highlightp))
8341         (save-restriction
8342           (funcall (cadr elem)))))))
8343
8344 ;; Dynamic variables.
8345 (defvar part-number)
8346 (defvar total-parts)
8347 (defvar gnus-treat-type)
8348 (defvar gnus-treat-condition)
8349 (defvar gnus-treat-length)
8350
8351 (defun gnus-treat-predicate (val)
8352   (cond
8353    ((null val)
8354     nil)
8355    (gnus-treat-condition
8356     (eq gnus-treat-condition val))
8357    ((and (listp val)
8358          (stringp (car val)))
8359     (apply 'gnus-or (mapcar `(lambda (s)
8360                                (string-match s ,(or gnus-newsgroup-name "")))
8361                             val)))
8362    ((listp val)
8363     (let ((pred (pop val)))
8364       (cond
8365        ((eq pred 'or)
8366         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8367        ((eq pred 'and)
8368         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8369        ((eq pred 'not)
8370         (not (gnus-treat-predicate (car val))))
8371        ((eq pred 'typep)
8372         (equal (car val) gnus-treat-type))
8373        (t
8374         (error "%S is not a valid predicate" pred)))))
8375    ((eq val t)
8376     t)
8377    ((eq val 'head)
8378     nil)
8379    ((eq val 'first)
8380     (eq part-number 1))
8381    ((eq val 'last)
8382     (eq part-number total-parts))
8383    ((numberp val)
8384     (< gnus-treat-length val))
8385    (t
8386     (error "%S is not a valid value" val))))
8387
8388 (defun gnus-article-encrypt-body (protocol &optional n)
8389   "Encrypt the article body."
8390   (interactive
8391    (list
8392     (or gnus-article-encrypt-protocol
8393         (gnus-completing-read "Encrypt protocol"
8394                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8395                               t))
8396     current-prefix-arg))
8397   ;; User might hit `K E' instead of `K e', so prompt once.
8398   (when (and gnus-article-encrypt-protocol
8399              gnus-novice-user)
8400     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8401       (error "Encrypt aborted")))
8402   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8403     (unless func
8404       (error "Can't find the encrypt protocol %s" protocol))
8405     (if (member gnus-newsgroup-name '("nndraft:delayed"
8406                                       "nndraft:drafts"
8407                                       "nndraft:queue"))
8408         (error "Can't encrypt the article in group %s"
8409                gnus-newsgroup-name))
8410     (gnus-summary-iterate n
8411       (with-current-buffer gnus-summary-buffer
8412         (let ((mail-parse-charset gnus-newsgroup-charset)
8413               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8414               (summary-buffer gnus-summary-buffer)
8415               references point)
8416           (gnus-set-global-variables)
8417           (when (gnus-group-read-only-p)
8418             (error "The current newsgroup does not support article encrypt"))
8419           (gnus-summary-show-article t)
8420           (setq references
8421               (or (mail-header-references gnus-current-headers) ""))
8422           (set-buffer gnus-article-buffer)
8423           (let* ((inhibit-read-only t)
8424                  (headers
8425                   (mapcar (lambda (field)
8426                             (and (save-restriction
8427                                    (message-narrow-to-head)
8428                                    (goto-char (point-min))
8429                                    (search-forward field nil t))
8430                                  (prog2
8431                                      (message-narrow-to-field)
8432                                      (buffer-string)
8433                                    (delete-region (point-min) (point-max))
8434                                    (widen))))
8435                           '("Content-Type:" "Content-Transfer-Encoding:"
8436                             "Content-Disposition:"))))
8437             (message-narrow-to-head)
8438             (message-remove-header "MIME-Version")
8439             (goto-char (point-max))
8440             (setq point (point))
8441             (insert (apply 'concat headers))
8442             (widen)
8443             (narrow-to-region point (point-max))
8444             (let ((message-options message-options))
8445               (message-options-set 'message-sender user-mail-address)
8446               (message-options-set 'message-recipients user-mail-address)
8447               (message-options-set 'message-sign-encrypt 'not)
8448               (funcall func))
8449             (goto-char (point-min))
8450             (insert "MIME-Version: 1.0\n")
8451             (widen)
8452             (gnus-summary-edit-article-done
8453              references nil summary-buffer t))
8454           (when gnus-keep-backlog
8455             (gnus-backlog-remove-article
8456              (car gnus-article-current) (cdr gnus-article-current)))
8457           (gnus-flush-original-article-buffer)
8458           (when gnus-use-cache
8459             (gnus-cache-update-article
8460              (car gnus-article-current) (cdr gnus-article-current))))))))
8461
8462 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8463   "The following specs can be used:
8464 %t  The security MIME type
8465 %i  Additional info
8466 %d  Details
8467 %D  Details if button is pressed")
8468
8469 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8470   "The following specs can be used:
8471 %t  The security MIME type
8472 %i  Additional info
8473 %d  Details
8474 %D  Details if button is pressed")
8475
8476 (defvar gnus-mime-security-button-line-format-alist
8477   '((?t gnus-tmp-type ?s)
8478     (?i gnus-tmp-info ?s)
8479     (?d gnus-tmp-details ?s)
8480     (?D gnus-tmp-pressed-details ?s)))
8481
8482 (defvar gnus-mime-security-button-commands
8483   '((gnus-article-press-button "\r" "Show Detail")
8484     (undefined "v")
8485     (undefined "t")
8486     (undefined "C")
8487     (gnus-mime-security-save-part "o" "Save...")
8488     (undefined "\C-o")
8489     (undefined "r")
8490     (undefined "d")
8491     (undefined "c")
8492     (undefined "i")
8493     (undefined "E")
8494     (undefined "e")
8495     (undefined "p")
8496     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8497     (undefined ".")))
8498
8499 (defvar gnus-mime-security-button-map
8500   (let ((map (make-sparse-keymap)))
8501     (define-key map gnus-mouse-2 'gnus-article-push-button)
8502     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8503     (dolist (c gnus-mime-security-button-commands)
8504       (define-key map (cadr c) (car c)))
8505     map))
8506
8507 (easy-menu-define
8508   gnus-mime-security-button-menu gnus-mime-security-button-map
8509   "Security button menu."
8510   `("Security Part"
8511     ,@(delq nil
8512             (mapcar (lambda (c)
8513                       (unless (eq (car c) 'undefined)
8514                         (vector (caddr c) (car c) :active t)))
8515                     gnus-mime-security-button-commands))))
8516
8517 (defun gnus-mime-security-button-menu (event prefix)
8518   "Construct a context-sensitive menu of security commands."
8519   (interactive "e\nP")
8520   (save-window-excursion
8521     (let ((pos (event-start event)))
8522       (select-window (posn-window pos))
8523       (goto-char (posn-point pos))
8524       (gnus-article-check-buffer)
8525       (popup-menu gnus-mime-security-button-menu nil prefix))))
8526
8527 (defvar gnus-mime-security-details-buffer nil)
8528
8529 (defvar gnus-mime-security-button-pressed nil)
8530
8531 (defvar gnus-mime-security-show-details-inline t
8532   "If non-nil, show details in the article buffer.")
8533
8534 (defun gnus-mime-security-verify-or-decrypt (handle)
8535   (mm-remove-parts (cdr handle))
8536   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8537         point (inhibit-read-only t))
8538     (if region
8539         (goto-char (car region)))
8540     (setq point (point))
8541     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8542       (let* ((mm-verify-option 'known)
8543              (mm-decrypt-option 'known)
8544              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8545         (unless (eq nparts (cdr handle))
8546           (mm-destroy-parts (cdr handle))
8547           (setcdr handle nparts))))
8548     (gnus-mime-display-security handle)
8549     (when region
8550       (delete-region (point) (cdr region))
8551       (set-marker (car region) nil)
8552       (set-marker (cdr region) nil))
8553     (goto-char point)))
8554
8555 (defun gnus-mime-security-show-details (handle)
8556   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8557     (if (not details)
8558         (gnus-message 5 "No details.")
8559       (if gnus-mime-security-show-details-inline
8560           (let ((gnus-mime-security-button-pressed
8561                  (not (get-text-property (point) 'gnus-mime-details)))
8562                 (gnus-mime-security-button-line-format
8563                  (get-text-property (point) 'gnus-line-format))
8564                 (inhibit-read-only t))
8565             (forward-char -1)
8566             (while (eq (get-text-property (point) 'gnus-line-format)
8567                        gnus-mime-security-button-line-format)
8568               (forward-char -1))
8569             (forward-char)
8570             (save-restriction
8571               (narrow-to-region (point) (point))
8572               (gnus-insert-mime-security-button handle))
8573             (delete-region (point)
8574                            (or (text-property-not-all
8575                                 (point) (point-max)
8576                                 'gnus-line-format
8577                                 gnus-mime-security-button-line-format)
8578                                (point-max))))
8579         ;; Not inlined.
8580         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8581             (with-current-buffer gnus-mime-security-details-buffer
8582               (erase-buffer)
8583               t)
8584           (setq gnus-mime-security-details-buffer
8585                 (gnus-get-buffer-create "*MIME Security Details*")))
8586         (with-current-buffer gnus-mime-security-details-buffer
8587           (insert details)
8588           (goto-char (point-min)))
8589         (pop-to-buffer gnus-mime-security-details-buffer)))))
8590
8591 (defun gnus-mime-security-press-button (handle)
8592   (save-excursion
8593     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8594         (gnus-mime-security-show-details handle)
8595       (gnus-mime-security-verify-or-decrypt handle))))
8596
8597 (defun gnus-insert-mime-security-button (handle &optional displayed)
8598   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8599          (gnus-tmp-type
8600           (concat
8601            (or (nth 2 (assoc protocol mm-verify-function-alist))
8602                (nth 2 (assoc protocol mm-decrypt-function-alist))
8603                "Unknown")
8604            (if (equal (car handle) "multipart/signed")
8605                " Signed" " Encrypted")
8606            " Part"))
8607          (gnus-tmp-info
8608           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8609               "Undecided"))
8610          (gnus-tmp-details
8611           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8612          gnus-tmp-pressed-details
8613          b e)
8614     (setq gnus-tmp-details
8615           (if gnus-tmp-details
8616               (concat "\n" gnus-tmp-details)
8617             ""))
8618     (setq gnus-tmp-pressed-details
8619           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8620     (unless (bolp)
8621       (insert "\n"))
8622     (setq b (point))
8623     (gnus-eval-format
8624      gnus-mime-security-button-line-format
8625      gnus-mime-security-button-line-format-alist
8626      `(keymap ,gnus-mime-security-button-map
8627          gnus-callback gnus-mime-security-press-button
8628          gnus-line-format ,gnus-mime-security-button-line-format
8629          gnus-mime-details ,gnus-mime-security-button-pressed
8630          article-type annotation
8631          gnus-data ,handle))
8632     (setq e (if (bolp)
8633                 ;; Exclude a newline.
8634                 (1- (point))
8635               (point)))
8636     (when gnus-article-button-face
8637       (gnus-overlay-put (gnus-make-overlay b e nil t)
8638                         'face gnus-article-button-face))
8639     (widget-convert-button
8640      'link b e
8641      :mime-handle handle
8642      :action 'gnus-widget-press-button
8643      :button-keymap gnus-mime-security-button-map
8644      :help-echo
8645      (lambda (widget)
8646        ;; Needed to properly clear the message due to a bug in
8647        ;; wid-edit (XEmacs only).
8648        (when (boundp 'help-echo-owns-message)
8649          (setq help-echo-owns-message t))
8650        (format
8651         "%S: show detail; %S: more options"
8652         (aref gnus-mouse-2 0)
8653         (aref gnus-down-mouse-3 0))))))
8654
8655 (defun gnus-mime-display-security (handle)
8656   (save-restriction
8657     (narrow-to-region (point) (point))
8658     (unless (gnus-unbuttonized-mime-type-p (car handle))
8659       (gnus-insert-mime-security-button handle))
8660     (gnus-mime-display-part (cadr handle))
8661     (unless (bolp)
8662       (insert "\n"))
8663     (unless (gnus-unbuttonized-mime-type-p (car handle))
8664       (let ((gnus-mime-security-button-line-format
8665              gnus-mime-security-button-end-line-format))
8666         (gnus-insert-mime-security-button handle)))
8667     (mm-set-handle-multipart-parameter
8668      handle 'gnus-region
8669      (cons (set-marker (make-marker) (point-min))
8670            (set-marker (make-marker) (point-max))))
8671     (goto-char (point-max))))
8672
8673 (defun gnus-mime-security-run-function (function)
8674   "Run FUNCTION with the security part under point."
8675   (gnus-article-check-buffer)
8676   (let ((data (get-text-property (point) 'gnus-data))
8677         buffer handle)
8678     (when (and (stringp (car-safe data))
8679                (setq buffer (mm-handle-multipart-original-buffer data))
8680                (setq handle (cadr data)))
8681       (if (bufferp (mm-handle-buffer handle))
8682           (progn
8683             (setq handle (cons buffer (copy-sequence (cdr handle))))
8684             (mm-handle-set-undisplayer handle nil))
8685         (setq handle (mm-make-handle
8686                       buffer
8687                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8688                       nil nil nil nil nil nil)))
8689       (funcall function handle))))
8690
8691 (defun gnus-mime-security-save-part ()
8692   "Save the security part under point."
8693   (interactive)
8694   (gnus-mime-security-run-function 'mm-save-part))
8695
8696 (defun gnus-mime-security-pipe-part ()
8697   "Pipe the security part under point to a process."
8698   (interactive)
8699   (gnus-mime-security-run-function 'mm-pipe-part))
8700
8701 (gnus-ems-redefine)
8702
8703 (provide 'gnus-art)
8704
8705 (run-hooks 'gnus-art-load-hook)
8706
8707 ;;; gnus-art.el ends here