Require mouse, which the build bot seems to say is needed.
[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 :custom-show nil
167                  regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-hidden-properties '(invisible t intangible t)
261   "Property list to use for hiding text."
262   :type 'sexp
263   :group 'gnus-article-hiding)
264
265 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
266 ;; frames in a session.
267 (defcustom gnus-article-x-face-command
268   (if (featurep 'xemacs)
269       (if (or (gnus-image-type-available-p 'xface)
270               (gnus-image-type-available-p 'pbm))
271           'gnus-display-x-face-in-from
272         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
273     (if (gnus-image-type-available-p 'pbm)
274         'gnus-display-x-face-in-from
275       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
276 display -"))
277   "*String or function to be executed to display an X-Face header.
278 If it is a string, the command will be executed in a sub-shell
279 asynchronously.  The compressed face will be piped to this command."
280   :type `(choice string
281                  (function-item gnus-display-x-face-in-from)
282                  function)
283   :version "21.1"
284   :group 'gnus-picon
285   :group 'gnus-article-washing)
286
287 (defcustom gnus-article-x-face-too-ugly nil
288   "Regexp matching posters whose face shouldn't be shown automatically."
289   :type '(choice regexp (const nil))
290   :group 'gnus-article-washing)
291
292 (defcustom gnus-article-banner-alist nil
293   "Banner alist for stripping.
294 For example,
295      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
296   :version "21.1"
297   :type '(repeat (cons symbol regexp))
298   :group 'gnus-article-washing)
299
300 (gnus-define-group-parameter
301  banner
302  :variable-document
303  "Alist of regexps (to match group names) and banner."
304  :variable-group gnus-article-washing
305  :parameter-type
306  '(choice :tag "Banner"
307           :value nil
308           (const :tag "Remove signature" signature)
309           (symbol :tag "Item in `gnus-article-banner-alist'" none)
310           regexp
311           (const :tag "None" nil))
312  :parameter-document
313  "If non-nil, specify how to remove `banners' from articles.
314
315 Symbol `signature' means to remove signatures delimited by
316 `gnus-signature-separator'.  Any other symbol is used to look up a
317 regular expression to match the banner in `gnus-article-banner-alist'.
318 A string is used as a regular expression to match the banner
319 directly.")
320
321 (defcustom gnus-article-address-banner-alist nil
322   "Alist of mail addresses and banners.
323 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
324 to match a mail address in the From: header, BANNER is one of a symbol
325 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
326 If ADDRESS matches author's mail address, it will remove things like
327 advertisements.  For example:
328
329 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
330 "
331   :type '(repeat
332           (cons
333            (regexp :tag "Address")
334            (choice :tag "Banner" :value nil
335                    (const :tag "Remove signature" signature)
336                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
337                    regexp
338                    (const :tag "None" nil))))
339   :version "22.1"
340   :group 'gnus-article-washing)
341
342 (defmacro gnus-emphasis-custom-with-format (&rest body)
343   `(let ((format "\
344 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
345 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
346      ,@body))
347
348 (defun gnus-emphasis-custom-value-to-external (value)
349   (gnus-emphasis-custom-with-format
350    (if (consp (car value))
351        (list (format format (car (car value)) (cdr (car value)))
352              2
353              (if (nth 1 value) 2 3)
354              (nth 2 value))
355      value)))
356
357 (defun gnus-emphasis-custom-value-to-internal (value)
358   (gnus-emphasis-custom-with-format
359    (let ((regexp (concat "\\`"
360                          (format (regexp-quote format)
361                                  "\\([^()]+\\)" "\\([^()]+\\)")
362                          "\\'"))
363          pattern)
364      (if (string-match regexp (setq pattern (car value)))
365          (list (cons (match-string 1 pattern) (match-string 2 pattern))
366                (= (nth 2 value) 2)
367                (nth 3 value))
368        value))))
369
370 (defcustom gnus-emphasis-alist
371   (let ((types
372          '(("\\*" "\\*" bold nil 2)
373            ("_" "_" underline)
374            ("/" "/" italic)
375            ("_/" "/_" underline-italic)
376            ("_\\*" "\\*_" underline-bold)
377            ("\\*/" "/\\*" bold-italic)
378            ("_\\*/" "/\\*_" underline-bold-italic))))
379     (nconc
380      (gnus-emphasis-custom-with-format
381       (mapcar (lambda (spec)
382                 (list (format format (car spec) (cadr spec))
383                       (or (nth 3 spec) 2)
384                       (or (nth 4 spec) 3)
385                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
386               types))
387      '(;; I've never seen anyone use this strikethru convention whereas I've
388        ;; several times seen it triggered by normal text.  --Stef
389        ;; Miles suggests that this form is sometimes used but for italics,
390        ;; so maybe we should map it to `italic'.
391        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
392        ;; 2 3 gnus-emphasis-strikethru)
393        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
394         2 3 gnus-emphasis-underline))))
395   "*Alist that says how to fontify certain phrases.
396 Each item looks like this:
397
398   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
399
400 The first element is a regular expression to be matched.  The second
401 is a number that says what regular expression grouping used to find
402 the entire emphasized word.  The third is a number that says what
403 regexp grouping should be displayed and highlighted.  The fourth
404 is the face used for highlighting."
405   :type
406   '(repeat
407     (menu-choice
408      :format "%[Customizing Style%]\n%v"
409      :indent 2
410      (group :tag "Default"
411             :value ("" 0 0 default)
412             :value-create
413             (lambda (widget)
414               (let ((value (widget-get
415                             (cadr (widget-get (widget-get widget :parent)
416                                               :args))
417                             :value)))
418                 (if (not (eq (nth 2 value) 'default))
419                     (widget-put
420                      widget
421                      :value
422                      (gnus-emphasis-custom-value-to-external value))))
423               (widget-group-value-create widget))
424             regexp
425             (integer :format "Match group: %v")
426             (integer :format "Emphasize group: %v")
427             face)
428      (group :tag "Simple"
429             :value (("_" . "_") nil default)
430             (cons :format "%v"
431                   (regexp :format "Start regexp: %v")
432                   (regexp :format "End regexp: %v"))
433             (boolean :format "Show start and end patterns: %[%v%]\n"
434                      :on " On " :off " Off ")
435             face)))
436   :get (lambda (symbol)
437          (mapcar 'gnus-emphasis-custom-value-to-internal
438                  (default-value symbol)))
439   :set (lambda (symbol value)
440          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
441                                      value)))
442   :group 'gnus-article-emphasis)
443
444 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
445   "A regexp to describe whitespace which should not be emphasized.
446 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
447 The former avoids underlining of leading and trailing whitespace,
448 and the latter avoids underlining any whitespace at all."
449   :version "21.1"
450   :group 'gnus-article-emphasis
451   :type 'regexp)
452
453 (defface gnus-emphasis-bold '((t (:bold t)))
454   "Face used for displaying strong emphasized text (*word*)."
455   :group 'gnus-article-emphasis)
456
457 (defface gnus-emphasis-italic '((t (:italic t)))
458   "Face used for displaying italic emphasized text (/word/)."
459   :group 'gnus-article-emphasis)
460
461 (defface gnus-emphasis-underline '((t (:underline t)))
462   "Face used for displaying underlined emphasized text (_word_)."
463   :group 'gnus-article-emphasis)
464
465 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
466   "Face used for displaying underlined bold emphasized text (_*word*_)."
467   :group 'gnus-article-emphasis)
468
469 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
470   "Face used for displaying underlined italic emphasized text (_/word/_)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
474   "Face used for displaying bold italic emphasized text (/*word*/)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-underline-bold-italic
478   '((t (:bold t :italic t :underline t)))
479   "Face used for displaying underlined bold italic emphasized text.
480 Example: (_/*word*/_)."
481   :group 'gnus-article-emphasis)
482
483 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
484                                       '((t (:strikethru t)))
485                                     '((t (:strike-through t))))
486   "Face used for displaying strike-through text (-word-)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-highlight-words
490   '((t (:background "black" :foreground "yellow")))
491   "Face used for displaying highlighted words."
492   :group 'gnus-article-emphasis)
493
494 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
495   "Format for display of Date headers in article bodies.
496 See `format-time-string' for the possible values.
497
498 The variable can also be function, which should return a complete Date
499 header.  The function is called with one argument, the time, which can
500 be fed to `format-time-string'."
501   :type '(choice string function)
502   :link '(custom-manual "(gnus)Article Date")
503   :group 'gnus-article-washing)
504
505 (defcustom gnus-save-all-headers t
506   "*If non-nil, don't remove any headers before saving.
507 This will be overridden by the `:headers' property that the symbol of
508 the saver function, which is specified by `gnus-default-article-saver',
509 might have."
510   :group 'gnus-article-saving
511   :type 'boolean)
512
513 (defcustom gnus-prompt-before-saving 'always
514   "*This variable says how much prompting is to be done when saving articles.
515 If it is nil, no prompting will be done, and the articles will be
516 saved to the default files.  If this variable is `always', each and
517 every article that is saved will be preceded by a prompt, even when
518 saving large batches of articles.  If this variable is neither nil not
519 `always', there the user will be prompted once for a file name for
520 each invocation of the saving commands."
521   :group 'gnus-article-saving
522   :type '(choice (item always)
523                  (item :tag "never" nil)
524                  (sexp :tag "once" :format "%t\n" :value t)))
525
526 (defcustom gnus-saved-headers gnus-visible-headers
527   "Headers to keep if `gnus-save-all-headers' is nil.
528 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
529 If that variable is nil, however, all headers that match this regexp
530 will be kept while the rest will be deleted before saving.  This and
531 `gnus-save-all-headers' will be overridden by the `:headers' property
532 that the symbol of the saver function, which is specified by
533 `gnus-default-article-saver', might have."
534   :group 'gnus-article-saving
535   :type 'regexp)
536
537 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
538 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
539   "A function to save articles in your favourite format.
540 The function will be called by way of the `gnus-summary-save-article'
541 command, and friends such as `gnus-summary-save-article-rmail'.
542
543 Gnus provides the following functions:
544
545 * gnus-summary-save-in-rmail (Rmail format)
546 * gnus-summary-save-in-mail (Unix mail format)
547 * gnus-summary-save-in-folder (MH folder)
548 * gnus-summary-save-in-file (article format)
549 * gnus-summary-save-body-in-file (article body)
550 * gnus-summary-save-in-vm (use VM's folder format)
551 * gnus-summary-write-to-file (article format -- overwrite)
552 * gnus-summary-write-body-to-file (article body -- overwrite)
553 * gnus-summary-save-in-pipe (article format)
554
555 The symbol of each function may have the following properties:
556
557 * :decode
558 The value non-nil means save decoded articles.  This is meaningful
559 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
560 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
561 `gnus-summary-save-in-pipe'.
562
563 * :function
564 The value specifies an alternative function which appends, not
565 overwrites, articles to a file.  This implies that when saving many
566 articles at a time, `gnus-prompt-before-saving' is bound to t and all
567 articles are saved in a single file.  This is meaningful only with
568 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
569
570 * :headers
571 The value specifies the symbol of a variable of which the value
572 specifies headers to be saved.  If it is omitted,
573 `gnus-save-all-headers' and `gnus-saved-headers' control what
574 headers should be saved."
575   :group 'gnus-article-saving
576   :type '(radio (function-item gnus-summary-save-in-rmail)
577                 (function-item gnus-summary-save-in-mail)
578                 (function-item gnus-summary-save-in-folder)
579                 (function-item gnus-summary-save-in-file)
580                 (function-item gnus-summary-save-body-in-file)
581                 (function-item gnus-summary-save-in-vm)
582                 (function-item gnus-summary-write-to-file)
583                 (function-item gnus-summary-write-body-to-file)
584                 (function-item gnus-summary-save-in-pipe)
585                 (function)))
586
587 (defcustom gnus-article-save-coding-system
588   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
589       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
590       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
591       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
592   "Coding system used to save decoded articles to a file.
593
594 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
595 which can safely encode any characters in text.  This is used by the
596 commands including:
597
598 * gnus-summary-save-article-file
599 * gnus-summary-save-article-body-file
600 * gnus-summary-write-article-file
601 * gnus-summary-write-article-body-file
602
603 and the functions to which you may set `gnus-default-article-saver':
604
605 * gnus-summary-save-in-file
606 * gnus-summary-save-body-in-file
607 * gnus-summary-write-to-file
608 * gnus-summary-write-body-to-file
609
610 Those commands and functions save just text displayed in the article
611 buffer to a file if the value of this variable is non-nil.  Note that
612 buttonized MIME parts will be lost in a saved file in that case.
613 Otherwise, raw articles will be saved."
614   :group 'gnus-article-saving
615   :type `(choice
616           :format "%{%t%}:\n %[Value Menu%] %v"
617           (const :tag "Save raw articles" nil)
618           ,@(delq nil
619                   (mapcar
620                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
621                    '((const :tag "UTF-8" utf-8)
622                      (const :tag "iso-2022-7bit" iso-2022-7bit)
623                      (const :tag "Emacs internal" emacs-mule)
624                      (const :tag "escape-quoted" escape-quoted))))
625           (symbol :tag "Coding system")))
626
627 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
628   "A function generating a file name to save articles in Rmail format.
629 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
630   :group 'gnus-article-saving
631   :type 'function)
632
633 (defcustom gnus-mail-save-name 'gnus-plain-save-name
634   "A function generating a file name to save articles in Unix mail format.
635 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
636   :group 'gnus-article-saving
637   :type 'function)
638
639 (defcustom gnus-folder-save-name 'gnus-folder-save-name
640   "A function generating a file name to save articles in MH folder.
641 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
642   :group 'gnus-article-saving
643   :type 'function)
644
645 (defcustom gnus-file-save-name 'gnus-numeric-save-name
646   "A function generating a file name to save articles in article format.
647 The function is called with NEWSGROUP, HEADERS, and optional
648 LAST-FILE."
649   :group 'gnus-article-saving
650   :type 'function)
651
652 (defcustom gnus-split-methods
653   '((gnus-article-archive-name)
654     (gnus-article-nndoc-name))
655   "*Variable used to suggest where articles are to be saved.
656 For instance, if you would like to save articles related to Gnus in
657 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
658 you could set this variable to something like:
659
660  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
661    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
662
663 This variable is an alist where the key is the match and the
664 value is a list of possible files to save in if the match is
665 non-nil.
666
667 If the match is a string, it is used as a regexp match on the
668 article.  If the match is a symbol, that symbol will be funcalled
669 from the buffer of the article to be saved with the newsgroup as the
670 parameter.  If it is a list, it will be evaled in the same buffer.
671
672 If this form or function returns a string, this string will be used as a
673 possible file name; and if it returns a non-nil list, that list will be
674 used as possible file names."
675   :group 'gnus-article-saving
676   :type '(repeat (choice (list :value (fun) function)
677                          (cons :value ("" "") regexp (repeat string))
678                          (sexp :value nil))))
679
680 (defcustom gnus-page-delimiter "^\^L"
681   "*Regexp describing what to use as article page delimiters.
682 The default value is \"^\^L\", which is a form linefeed at the
683 beginning of a line."
684   :type 'regexp
685   :group 'gnus-article-various)
686
687 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
688   "*The format specification for the article mode line.
689 See `gnus-summary-mode-line-format' for a closer description.
690
691 The following additional specs are available:
692
693 %w  The article washing status.
694 %m  The number of MIME parts in the article."
695   :version "24.1"
696   :type 'string
697   :group 'gnus-article-various)
698
699 (defcustom gnus-article-mode-hook nil
700   "*A hook for Gnus article mode."
701   :type 'hook
702   :group 'gnus-article-various)
703
704 (when (featurep 'xemacs)
705   ;; Extracted from gnus-xmas-define in order to preserve user settings
706   (when (fboundp 'turn-off-scroll-in-place)
707     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
708   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
709   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
710
711 (defcustom gnus-article-menu-hook nil
712   "*Hook run after the creation of the article mode menu."
713   :type 'hook
714   :group 'gnus-article-various)
715
716 (defcustom gnus-article-prepare-hook nil
717   "*A hook called after an article has been prepared in the article buffer."
718   :type 'hook
719   :group 'gnus-article-various)
720
721 (defcustom gnus-copy-article-ignored-headers nil
722   "List of headers to be removed when copying an article.
723 Each element is a regular expression."
724   :version "23.1" ;; No Gnus
725   :type '(repeat regexp)
726   :group 'gnus-article-various)
727
728 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
729                         "Gnus 5.10 (Emacs 22.1)")
730
731 (defface gnus-button
732   '((t (:weight bold)))
733   "Face used for highlighting a button in the article buffer."
734   :group 'gnus-article-buttons)
735
736 (defcustom gnus-article-button-face 'gnus-button
737   "Face used for highlighting buttons in the article buffer.
738
739 An article button is a piece of text that you can activate by pressing
740 `RET' or `mouse-2' above it."
741   :type 'face
742   :group 'gnus-article-buttons)
743
744 (defcustom gnus-article-mouse-face 'highlight
745   "Face used for mouse highlighting in the article buffer.
746
747 Article buttons will be displayed in this face when the cursor is
748 above them."
749   :type 'face
750   :group 'gnus-article-buttons)
751
752 (defcustom gnus-signature-face 'gnus-signature
753   "Face used for highlighting a signature in the article buffer.
754 Obsolete; use the face `gnus-signature' for customizations instead."
755   :type 'face
756   :group 'gnus-article-highlight
757   :group 'gnus-article-signature)
758
759 (defface gnus-signature
760   '((t
761      (:italic t)))
762   "Face used for highlighting a signature in the article buffer."
763   :group 'gnus-article-highlight
764   :group 'gnus-article-signature)
765 ;; backward-compatibility alias
766 (put 'gnus-signature-face 'face-alias 'gnus-signature)
767 (put 'gnus-signature-face 'obsolete-face "22.1")
768
769 (defface gnus-header-from
770   '((((class color)
771       (background dark))
772      (:foreground "PaleGreen1"))
773     (((class color)
774       (background light))
775      (:foreground "red3"))
776     (t
777      (:italic t)))
778   "Face used for displaying from headers."
779   :group 'gnus-article-headers
780   :group 'gnus-article-highlight)
781 ;; backward-compatibility alias
782 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
783 (put 'gnus-header-from-face 'obsolete-face "22.1")
784
785 (defface gnus-header-subject
786   '((((class color)
787       (background dark))
788      (:foreground "SeaGreen1"))
789     (((class color)
790       (background light))
791      (:foreground "red4"))
792     (t
793      (:bold t :italic t)))
794   "Face used for displaying subject headers."
795   :group 'gnus-article-headers
796   :group 'gnus-article-highlight)
797 ;; backward-compatibility alias
798 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
799 (put 'gnus-header-subject-face 'obsolete-face "22.1")
800
801 (defface gnus-header-newsgroups
802   '((((class color)
803       (background dark))
804      (:foreground "yellow" :italic t))
805     (((class color)
806       (background light))
807      (:foreground "MidnightBlue" :italic t))
808     (t
809      (:italic t)))
810   "Face used for displaying newsgroups headers.
811 In the default setup this face is only used for crossposted
812 articles."
813   :group 'gnus-article-headers
814   :group 'gnus-article-highlight)
815 ;; backward-compatibility alias
816 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
817 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
818
819 (defface gnus-header-name
820   '((((class color)
821       (background dark))
822      (:foreground "SpringGreen2"))
823     (((class color)
824       (background light))
825      (:foreground "maroon"))
826     (t
827      (:bold t)))
828   "Face used for displaying header names."
829   :group 'gnus-article-headers
830   :group 'gnus-article-highlight)
831 ;; backward-compatibility alias
832 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
833 (put 'gnus-header-name-face 'obsolete-face "22.1")
834
835 (defface gnus-header-content
836   '((((class color)
837       (background dark))
838      (:foreground "SpringGreen1" :italic t))
839     (((class color)
840       (background light))
841      (:foreground "indianred4" :italic t))
842     (t
843      (:italic t)))  "Face used for displaying header content."
844   :group 'gnus-article-headers
845   :group 'gnus-article-highlight)
846 ;; backward-compatibility alias
847 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
848 (put 'gnus-header-content-face 'obsolete-face "22.1")
849
850 (defcustom gnus-header-face-alist
851   '(("From" nil gnus-header-from)
852     ("Subject" nil gnus-header-subject)
853     ("Newsgroups:.*," nil gnus-header-newsgroups)
854     ("" gnus-header-name gnus-header-content))
855   "*Controls highlighting of article headers.
856
857 An alist of the form (HEADER NAME CONTENT).
858
859 HEADER is a regular expression which should match the name of a
860 header and NAME and CONTENT are either face names or nil.
861
862 The name of each header field will be displayed using the face
863 specified by the first element in the list where HEADER matches
864 the header name and NAME is non-nil.  Similarly, the content will
865 be displayed by the first non-nil matching CONTENT face."
866   :group 'gnus-article-headers
867   :group 'gnus-article-highlight
868   :type '(repeat (list (regexp :tag "Header")
869                        (choice :tag "Name"
870                                (item :tag "skip" nil)
871                                (face :value default))
872                        (choice :tag "Content"
873                                (item :tag "skip" nil)
874                                (face :value default)))))
875
876 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
877                                           '((xface . (:face gnus-x-face)))
878                                         '((pbm . (:face gnus-x-face))
879                                           (png . nil)))
880   "Alist of image types and properties applied to Face and X-Face images.
881 Here are examples:
882
883 ;; Specify the altitude of Face images in the From header.
884 \(setq gnus-face-properties-alist
885       '((pbm . (:face gnus-x-face :ascent 80))
886         (png . (:ascent 80))))
887
888 ;; Show Face images as pressed buttons.
889 \(setq gnus-face-properties-alist
890       '((pbm . (:face gnus-x-face :relief -2))
891         (png . (:relief -2))))
892
893 See the manual for the valid properties for various image types.
894 Currently, `pbm' is used for X-Face images and `png' is used for Face
895 images in Emacs.  Only the `:face' property is effective on the `xface'
896 image type in XEmacs if it is built with the libcompface library."
897   :version "23.1" ;; No Gnus
898   :group 'gnus-article-headers
899   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
900
901 (defcustom gnus-article-decode-hook
902   '(article-decode-charset article-decode-encoded-words
903                            article-decode-group-name article-decode-idna-rhs)
904   "*Hook run to decode charsets in articles."
905   :group 'gnus-article-headers
906   :type 'hook)
907
908 (defcustom gnus-display-mime-function 'gnus-display-mime
909   "Function to display MIME articles."
910   :group 'gnus-article-mime
911   :type 'function)
912
913 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
914   "Function used to decode headers.")
915
916 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
917   "Function used to decode addresses.")
918
919 (defvar gnus-article-dumbquotes-map
920   '((?\200 "EUR")
921     (?\202 ",")
922     (?\203 "f")
923     (?\204 ",,")
924     (?\205 "...")
925     (?\213 "<")
926     (?\214 "OE")
927     (?\221 "`")
928     (?\222 "'")
929     (?\223 "``")
930     (?\224 "\"")
931     (?\225 "*")
932     (?\226 "-")
933     (?\227 "--")
934     (?\230 "~")
935     (?\231 "(TM)")
936     (?\233 ">")
937     (?\234 "oe")
938     (?\264 "'"))
939   "Table for MS-to-Latin1 translation.")
940
941 (defcustom gnus-ignored-mime-types nil
942   "List of MIME types that should be ignored by Gnus."
943   :version "21.1"
944   :group 'gnus-article-mime
945   :type '(repeat regexp))
946
947 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
948   "List of MIME types that should not be given buttons when rendered inline.
949 See also `gnus-buttonized-mime-types' which may override this variable.
950 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
951   :version "21.1"
952   :group 'gnus-article-mime
953   :type '(repeat regexp))
954
955 (defcustom gnus-buttonized-mime-types nil
956   "List of MIME types that should be given buttons when rendered inline.
957 If set, this variable overrides `gnus-unbuttonized-mime-types'.
958 To see e.g. security buttons you could set this to
959 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
960 this list to display radio buttons that allow you to choose one of two
961 media types those mails include.  See also `mm-discouraged-alternatives'.
962 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
963   :version "22.1"
964   :group 'gnus-article-mime
965   :type '(repeat regexp))
966
967 (defcustom gnus-inhibit-mime-unbuttonizing nil
968   "If non-nil, all MIME parts get buttons.
969 When nil (the default value), then some MIME parts do not get buttons,
970 as described by the variables `gnus-buttonized-mime-types' and
971 `gnus-unbuttonized-mime-types'."
972   :version "22.1"
973   :group 'gnus-article-mime
974   :type 'boolean)
975
976 (defcustom gnus-body-boundary-delimiter "_"
977   "String used to delimit header and body.
978 This variable is used by `gnus-article-treat-body-boundary' which can
979 be controlled by `gnus-treat-body-boundary'."
980   :version "22.1"
981   :group 'gnus-article-various
982   :type '(choice (item :tag "None" :value nil)
983                  string))
984
985 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
986                                   "/usr/share/picons")
987   "Defines the location of the faces database.
988 For information on obtaining this database of pretty pictures, please
989 see http://www.cs.indiana.edu/picons/ftp/index.html"
990   :version "22.1"
991   :type '(repeat directory)
992   :link '(url-link :tag "download"
993                    "http://www.cs.indiana.edu/picons/ftp/index.html")
994   :link '(custom-manual "(gnus)Picons")
995   :group 'gnus-picon)
996
997 (defun gnus-picons-installed-p ()
998   "Say whether picons are installed on your machine."
999   (let ((installed nil))
1000     (dolist (database gnus-picon-databases)
1001       (when (file-exists-p database)
1002         (setq installed t)))
1003     installed))
1004
1005 (defcustom gnus-article-mime-part-function nil
1006   "Function called with a MIME handle as the argument.
1007 This is meant for people who want to do something automatic based
1008 on parts -- for instance, adding Vcard info to a database."
1009   :group 'gnus-article-mime
1010   :type '(choice (const nil)
1011                  function))
1012
1013 (defcustom gnus-mime-multipart-functions nil
1014   "An alist of MIME types to functions to display them."
1015   :version "21.1"
1016   :group 'gnus-article-mime
1017   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1018
1019 (defcustom gnus-article-date-headers '(combined-lapsed)
1020   "A list of Date header formats to display.
1021 Valid formats are `ut' (universal time), `local' (local time
1022 zone), `english' (readable English), `lapsed' (elapsed time),
1023 `combined-lapsed' (both the original date and the elapsed time),
1024 `original' (the original date header), `iso8601' (ISO8601
1025 format), and `user-defined' (a user-defined format defined by the
1026 `gnus-article-time-format' variable).
1027
1028 You have as many date headers as you want in the article buffer.
1029 Some of these headers are updated automatically.  See
1030 `gnus-article-update-date-headers' for details."
1031   :version "24.1"
1032   :group 'gnus-article-headers
1033   :type '(repeat
1034           (item :tag "Universal time (UT)" :value 'ut)
1035           (item :tag "Local time zone" :value 'local)
1036           (item :tag "Readable English" :value 'english)
1037           (item :tag "Elapsed time" :value 'lapsed)
1038           (item :tag "Original and elapsed time" :value 'combined-lapsed)
1039           (item :tag "Original date header" :value 'original)
1040           (item :tag "ISO8601 format" :value 'iso8601)
1041           (item :tag "User-defined" :value 'user-defined)))
1042
1043 (defcustom gnus-article-update-date-headers 1
1044   "A number that says how often to update the date header (in seconds).
1045 If nil, don't update it at all."
1046   :version "24.1"
1047   :group 'gnus-article-headers
1048   :type '(choice
1049           (item :tag "Don't update" :value nil)
1050           integer))
1051
1052 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1053   "Function called with a MIME handle as the argument.
1054 This is meant for people who want to view first matched part.
1055 For `undisplayed-alternative' (default), the first undisplayed
1056 part or alternative part is used.  For `undisplayed', the first
1057 undisplayed part is used.  For a function, the first part which
1058 the function return t is used.  For nil, the first part is
1059 used."
1060   :version "21.1"
1061   :group 'gnus-article-mime
1062   :type '(choice
1063           (item :tag "first" :value nil)
1064           (item :tag "undisplayed" :value undisplayed)
1065           (item :tag "undisplayed or alternative"
1066                 :value undisplayed-alternative)
1067           (function)))
1068
1069 (defcustom gnus-mime-action-alist
1070   '(("save to file" . gnus-mime-save-part)
1071     ("save and strip" . gnus-mime-save-part-and-strip)
1072     ("replace with file" . gnus-mime-replace-part)
1073     ("delete part" . gnus-mime-delete-part)
1074     ("display as text" . gnus-mime-inline-part)
1075     ("view the part" . gnus-mime-view-part)
1076     ("pipe to command" . gnus-mime-pipe-part)
1077     ("toggle display" . gnus-article-press-button)
1078     ("toggle display" . gnus-article-view-part-as-charset)
1079     ("view as type" . gnus-mime-view-part-as-type)
1080     ("view internally" . gnus-mime-view-part-internally)
1081     ("view externally" . gnus-mime-view-part-externally))
1082   "An alist of actions that run on the MIME attachment."
1083   :group 'gnus-article-mime
1084   :type '(repeat (cons (string :tag "name")
1085                        (function))))
1086
1087 (defcustom gnus-auto-select-part 1
1088   "Advance to next MIME part when deleting or stripping parts.
1089
1090 When 0, point will be placed on the same part as before.  When
1091 positive (negative), move point forward (backwards) this many
1092 parts.  When nil, redisplay article."
1093   :version "23.1" ;; No Gnus
1094   :group 'gnus-article-mime
1095   :type '(choice (const nil :tag "Redisplay article.")
1096                  (const 1 :tag "Next part.")
1097                  (const 0 :tag "Current part.")
1098                  integer))
1099
1100 ;;;
1101 ;;; The treatment variables
1102 ;;;
1103
1104 (defvar gnus-part-display-hook nil
1105   "Hook called on parts that are to receive treatment.")
1106
1107 (defvar gnus-article-treat-custom
1108   '(choice (const :tag "Off" nil)
1109            (const :tag "On" t)
1110            (const :tag "Header" head)
1111            (const :tag "First" first)
1112            (const :tag "Last" last)
1113            (integer :tag "Less")
1114            (repeat :tag "Groups" regexp)
1115            (sexp :tag "Predicate")))
1116
1117 (defvar gnus-article-treat-head-custom
1118   '(choice (const :tag "Off" nil)
1119            (const :tag "Header" head)))
1120
1121 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1122                                    "text/x-patch")
1123   "Parts to treat.")
1124
1125 (defvar gnus-inhibit-treatment nil
1126   "Whether to inhibit treatment.")
1127
1128 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1129   "Highlight the signature.
1130 Valid values are nil, t, `head', `first', `last', an integer or a
1131 predicate.  See Info node `(gnus)Customizing Articles'."
1132   :group 'gnus-article-treat
1133   :link '(custom-manual "(gnus)Customizing Articles")
1134   :type gnus-article-treat-custom)
1135 (put 'gnus-treat-highlight-signature 'highlight t)
1136
1137 (defcustom gnus-treat-buttonize 100000
1138   "Add buttons.
1139 Valid values are nil, t, `head', `first', `last', an integer or a
1140 predicate.  See Info node `(gnus)Customizing Articles'."
1141   :group 'gnus-article-treat
1142   :link '(custom-manual "(gnus)Customizing Articles")
1143   :type gnus-article-treat-custom)
1144 (put 'gnus-treat-buttonize 'highlight t)
1145
1146 (defcustom gnus-treat-buttonize-head 'head
1147   "Add buttons to the head.
1148 Valid values are nil, t, `head', `first', `last', an integer or a
1149 predicate.  See Info node `(gnus)Customizing Articles'."
1150   :group 'gnus-article-treat
1151   :link '(custom-manual "(gnus)Customizing Articles")
1152   :type gnus-article-treat-head-custom)
1153 (put 'gnus-treat-buttonize-head 'highlight t)
1154
1155 (defcustom gnus-treat-date 'head
1156   "Display dates according to the `gnus-article-date-headers' variable.
1157 Valid values are nil, t, `head', `first', `last', an integer or a
1158 predicate.  See Info node `(gnus)Customizing Articles'."
1159   :version "24.1"
1160   :group 'gnus-article-treat
1161   :link '(custom-manual "(gnus)Customizing Articles")
1162   :type gnus-article-treat-head-custom)
1163
1164 (defcustom gnus-treat-emphasize 50000
1165   "Emphasize text.
1166 Valid values are nil, t, `head', `first', `last', an integer or a
1167 predicate.  See Info node `(gnus)Customizing Articles'."
1168   :group 'gnus-article-treat
1169   :link '(custom-manual "(gnus)Customizing Articles")
1170   :type gnus-article-treat-custom)
1171 (put 'gnus-treat-emphasize 'highlight t)
1172
1173 (defcustom gnus-treat-strip-cr nil
1174   "Remove carriage returns.
1175 Valid values are nil, t, `head', `first', `last', an integer or a
1176 predicate.  See Info node `(gnus)Customizing Articles'."
1177   :version "22.1"
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-custom)
1181
1182 (defcustom gnus-treat-unsplit-urls nil
1183   "Remove newlines from within URLs.
1184 Valid values are nil, t, `head', `first', `last', an integer or a
1185 predicate.  See Info node `(gnus)Customizing Articles'."
1186   :version "22.1"
1187   :group 'gnus-article-treat
1188   :link '(custom-manual "(gnus)Customizing Articles")
1189   :type gnus-article-treat-custom)
1190
1191 (defcustom gnus-treat-leading-whitespace nil
1192   "Remove leading whitespace in headers.
1193 Valid values are nil, t, `head', `first', `last', an integer or a
1194 predicate.  See Info node `(gnus)Customizing Articles'."
1195   :version "22.1"
1196   :group 'gnus-article-treat
1197   :link '(custom-manual "(gnus)Customizing Articles")
1198   :type gnus-article-treat-custom)
1199
1200 (defcustom gnus-treat-hide-headers 'head
1201   "Hide headers.
1202 Valid values are nil, t, `head', `first', `last', an integer or a
1203 predicate.  See Info node `(gnus)Customizing Articles'."
1204   :group 'gnus-article-treat
1205   :link '(custom-manual "(gnus)Customizing Articles")
1206   :type gnus-article-treat-head-custom)
1207
1208 (defcustom gnus-treat-hide-boring-headers nil
1209   "Hide boring headers.
1210 Valid values are nil, t, `head', `first', `last', an integer or a
1211 predicate.  See Info node `(gnus)Customizing Articles'."
1212   :group 'gnus-article-treat
1213   :link '(custom-manual "(gnus)Customizing Articles")
1214   :type gnus-article-treat-head-custom)
1215
1216 (defcustom gnus-treat-hide-signature nil
1217   "Hide the signature.
1218 Valid values are nil, t, `head', `first', `last', an integer or a
1219 predicate.  See Info node `(gnus)Customizing Articles'."
1220   :group 'gnus-article-treat
1221   :link '(custom-manual "(gnus)Customizing Articles")
1222   :type gnus-article-treat-custom)
1223
1224 (defcustom gnus-treat-fill-article nil
1225   "Fill the article.
1226 Valid values are nil, t, `head', `first', `last', an integer or a
1227 predicate.  See Info node `(gnus)Customizing Articles'."
1228   :group 'gnus-article-treat
1229   :link '(custom-manual "(gnus)Customizing Articles")
1230   :type gnus-article-treat-custom)
1231
1232 (defcustom gnus-treat-hide-citation nil
1233   "Hide cited text.
1234 Valid values are nil, t, `head', `first', `last', an integer or a
1235 predicate.  See Info node `(gnus)Customizing Articles'."
1236   :group 'gnus-article-treat
1237   :link '(custom-manual "(gnus)Customizing Articles")
1238   :type gnus-article-treat-custom)
1239
1240 (defcustom gnus-treat-hide-citation-maybe nil
1241   "Hide cited text.
1242 Valid values are nil, t, `head', `first', `last', an integer or a
1243 predicate.  See Info node `(gnus)Customizing Articles'."
1244   :group 'gnus-article-treat
1245   :link '(custom-manual "(gnus)Customizing Articles")
1246   :type gnus-article-treat-custom)
1247
1248 (defcustom gnus-treat-strip-list-identifiers 'head
1249   "Strip list identifiers from `gnus-list-identifiers`.
1250 Valid values are nil, t, `head', `first', `last', an integer or a
1251 predicate.  See Info node `(gnus)Customizing Articles'."
1252   :version "21.1"
1253   :group 'gnus-article-treat
1254   :link '(custom-manual "(gnus)Customizing Articles")
1255   :type gnus-article-treat-custom)
1256
1257 (gnus-define-group-parameter
1258  list-identifier
1259  :variable-document
1260  "Alist of regexps and correspondent identifiers."
1261  :variable-group gnus-article-washing
1262  :parameter-type
1263  '(choice :tag "Identifier"
1264           :value nil
1265           (symbol :tag "Item in `gnus-list-identifiers'" none)
1266           regexp
1267           (const :tag "None" nil))
1268  :parameter-document
1269  "If non-nil, specify how to remove `identifiers' from articles' subject.
1270
1271 Any symbol is used to look up a regular expression to match the
1272 banner in `gnus-list-identifiers'.  A string is used as a regular
1273 expression to match the identifier directly.")
1274
1275 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1276                         "Gnus 5.10 (Emacs 22.1)")
1277
1278 (defcustom gnus-treat-strip-pem nil
1279   "Strip PEM signatures.
1280 Valid values are nil, t, `head', `first', `last', an integer or a
1281 predicate.  See Info node `(gnus)Customizing Articles'."
1282   :group 'gnus-article-treat
1283   :link '(custom-manual "(gnus)Customizing Articles")
1284   :type gnus-article-treat-custom)
1285
1286 (defcustom gnus-treat-strip-banner t
1287   "Strip banners from articles.
1288 The banner to be stripped is specified in the `banner' group parameter.
1289 Valid values are nil, t, `head', `first', `last', an integer or a
1290 predicate.  See Info node `(gnus)Customizing Articles'."
1291   :group 'gnus-article-treat
1292   :link '(custom-manual "(gnus)Customizing Articles")
1293   :type gnus-article-treat-custom)
1294
1295 (defcustom gnus-treat-highlight-headers 'head
1296   "Highlight the headers.
1297 Valid values are nil, t, `head', `first', `last', an integer or a
1298 predicate.  See Info node `(gnus)Customizing Articles'."
1299   :group 'gnus-article-treat
1300   :link '(custom-manual "(gnus)Customizing Articles")
1301   :type gnus-article-treat-head-custom)
1302 (put 'gnus-treat-highlight-headers 'highlight t)
1303
1304 (defcustom gnus-treat-highlight-citation t
1305   "Highlight cited text.
1306 Valid values are nil, t, `head', `first', `last', an integer or a
1307 predicate.  See Info node `(gnus)Customizing Articles'."
1308   :group 'gnus-article-treat
1309   :link '(custom-manual "(gnus)Customizing Articles")
1310   :type gnus-article-treat-custom)
1311 (put 'gnus-treat-highlight-citation 'highlight t)
1312
1313 (defcustom gnus-treat-strip-headers-in-body t
1314   "Strip the X-No-Archive header line from the beginning of the body.
1315 Valid values are nil, t, `head', `first', `last', an integer or a
1316 predicate.  See Info node `(gnus)Customizing Articles'."
1317   :version "21.1"
1318   :group 'gnus-article-treat
1319   :link '(custom-manual "(gnus)Customizing Articles")
1320   :type gnus-article-treat-custom)
1321
1322 (defcustom gnus-treat-strip-trailing-blank-lines nil
1323   "Strip trailing blank lines.
1324 Valid values are nil, t, `head', `first', `last', an integer or a
1325 predicate.  See Info node `(gnus)Customizing Articles'.
1326
1327 When set to t, it also strips trailing blanks in all MIME parts.
1328 Consider to use `last' instead."
1329   :group 'gnus-article-treat
1330   :link '(custom-manual "(gnus)Customizing Articles")
1331   :type gnus-article-treat-custom)
1332
1333 (defcustom gnus-treat-strip-leading-blank-lines nil
1334   "Strip leading blank lines.
1335 Valid values are nil, t, `head', `first', `last', an integer or a
1336 predicate.  See Info node `(gnus)Customizing Articles'.
1337
1338 When set to t, it also strips trailing blanks in all MIME parts."
1339   :group 'gnus-article-treat
1340   :link '(custom-manual "(gnus)Customizing Articles")
1341   :type gnus-article-treat-custom)
1342
1343 (defcustom gnus-treat-strip-multiple-blank-lines nil
1344   "Strip multiple blank lines.
1345 Valid values are nil, t, `head', `first', `last', an integer or a
1346 predicate.  See Info node `(gnus)Customizing Articles'."
1347   :group 'gnus-article-treat
1348   :link '(custom-manual "(gnus)Customizing Articles")
1349   :type gnus-article-treat-custom)
1350
1351 (defcustom gnus-treat-unfold-headers 'head
1352   "Unfold folded header lines.
1353 Valid values are nil, t, `head', `first', `last', an integer or a
1354 predicate.  See Info node `(gnus)Customizing Articles'."
1355   :version "22.1"
1356   :group 'gnus-article-treat
1357   :link '(custom-manual "(gnus)Customizing Articles")
1358   :type gnus-article-treat-custom)
1359
1360 (defcustom gnus-article-unfold-long-headers nil
1361   "If non-nil, allow unfolding headers even if the header is long.
1362 If it is a regexp, only long headers matching this regexp are unfolded.
1363 If it is t, all long headers are unfolded.
1364
1365 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1366   :version "23.1" ;; No Gnus
1367   :group 'gnus-article-treat
1368   :type '(choice (const nil)
1369                  (const :tag "all" t)
1370                  (regexp)))
1371
1372 (defcustom gnus-treat-fold-headers nil
1373   "Fold headers.
1374 Valid values are nil, t, `head', `first', `last', an integer or a
1375 predicate.  See Info node `(gnus)Customizing Articles'."
1376   :version "22.1"
1377   :group 'gnus-article-treat
1378   :link '(custom-manual "(gnus)Customizing Articles")
1379   :type gnus-article-treat-custom)
1380
1381 (defcustom gnus-treat-fold-newsgroups 'head
1382   "Fold the Newsgroups and Followup-To headers.
1383 Valid values are nil, t, `head', `first', `last', an integer or a
1384 predicate.  See Info node `(gnus)Customizing Articles'."
1385   :version "22.1"
1386   :group 'gnus-article-treat
1387   :link '(custom-manual "(gnus)Customizing Articles")
1388   :type gnus-article-treat-custom)
1389
1390 (defcustom gnus-treat-overstrike t
1391   "Treat overstrike highlighting.
1392 Valid values are nil, t, `head', `first', `last', an integer or a
1393 predicate.  See Info node `(gnus)Customizing Articles'."
1394   :group 'gnus-article-treat
1395   :link '(custom-manual "(gnus)Customizing Articles")
1396   :type gnus-article-treat-custom)
1397 (put 'gnus-treat-overstrike 'highlight t)
1398
1399 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1400   "Treat ANSI SGR control sequences.
1401 Valid values are nil, t, `head', `first', `last', an integer or a
1402 predicate.  See Info node `(gnus)Customizing Articles'."
1403   :group 'gnus-article-treat
1404   :link '(custom-manual "(gnus)Customizing Articles")
1405   :type gnus-article-treat-custom)
1406
1407 (make-obsolete-variable 'gnus-treat-display-xface
1408                         'gnus-treat-display-x-face "Emacs 22.1")
1409
1410 (defcustom gnus-treat-display-x-face
1411   (and (not noninteractive)
1412        (gnus-image-type-available-p 'xbm)
1413        (if (featurep 'xemacs)
1414            (featurep 'xface)
1415          (condition-case nil
1416              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1417                   (executable-find "icontopbm"))
1418            ;; shell-command-to-string may signal an error, e.g. if
1419            ;; shell-file-name is not found.
1420            (error nil)))
1421        'head)
1422   "Display X-Face headers.
1423 Valid values are nil and `head'.
1424 See Info node `(gnus)Customizing Articles' and Info node
1425 `(gnus)X-Face' for details."
1426   :group 'gnus-article-treat
1427   :version "21.1"
1428   :link '(custom-manual "(gnus)Customizing Articles")
1429   :link '(custom-manual "(gnus)X-Face")
1430   :type gnus-article-treat-head-custom
1431   :set (lambda (symbol value)
1432          (set-default
1433           symbol
1434           (cond ((or (boundp symbol) (get symbol 'saved-value))
1435                  value)
1436                 ((boundp 'gnus-treat-display-xface)
1437                  (message "\
1438 ** gnus-treat-display-xface is an obsolete variable;\
1439  use gnus-treat-display-x-face instead")
1440                  (default-value 'gnus-treat-display-xface))
1441                 ((get 'gnus-treat-display-xface 'saved-value)
1442                  (message "\
1443 ** gnus-treat-display-xface is an obsolete variable;\
1444  use gnus-treat-display-x-face instead")
1445                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1446                 (t
1447                  value)))))
1448 (put 'gnus-treat-display-x-face 'highlight t)
1449
1450 (defcustom gnus-treat-display-face
1451   (and (not noninteractive)
1452        (gnus-image-type-available-p 'png)
1453        'head)
1454   "Display Face headers.
1455 Valid values are nil, t, `head', `first', `last', an integer or a
1456 predicate.  See Info node `(gnus)Customizing Articles' and Info
1457 node `(gnus)Face' for details."
1458   :group 'gnus-article-treat
1459   :version "22.1"
1460   :link '(custom-manual "(gnus)Customizing Articles")
1461   :link '(custom-manual "(gnus)X-Face")
1462   :type gnus-article-treat-head-custom)
1463 (put 'gnus-treat-display-face 'highlight t)
1464
1465 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1466   "Display smileys.
1467 Valid values are nil, t, `head', `first', `last', an integer or a
1468 predicate.  See Info node `(gnus)Customizing Articles' and Info
1469 node `(gnus)Smileys' for details."
1470   :group 'gnus-article-treat
1471   :version "21.1"
1472   :link '(custom-manual "(gnus)Customizing Articles")
1473   :link '(custom-manual "(gnus)Smileys")
1474   :type gnus-article-treat-custom)
1475 (put 'gnus-treat-display-smileys 'highlight t)
1476
1477 (defcustom gnus-treat-from-picon
1478   (if (and (gnus-image-type-available-p 'xpm)
1479            (gnus-picons-installed-p))
1480       'head nil)
1481   "Display picons in the From header.
1482 Valid values are nil, t, `head', `first', `last', an integer or a
1483 predicate.  See Info node `(gnus)Customizing Articles' and Info
1484 node `(gnus)Picons' for details."
1485   :version "22.1"
1486   :group 'gnus-article-treat
1487   :group 'gnus-picon
1488   :link '(custom-manual "(gnus)Customizing Articles")
1489   :link '(custom-manual "(gnus)Picons")
1490   :type gnus-article-treat-head-custom)
1491 (put 'gnus-treat-from-picon 'highlight t)
1492
1493 (defcustom gnus-treat-mail-picon
1494   (if (and (gnus-image-type-available-p 'xpm)
1495            (gnus-picons-installed-p))
1496       'head nil)
1497   "Display picons in To and Cc headers.
1498 Valid values are nil, t, `head', `first', `last', an integer or a
1499 predicate.  See Info node `(gnus)Customizing Articles' and Info
1500 node `(gnus)Picons' for details."
1501   :version "22.1"
1502   :group 'gnus-article-treat
1503   :group 'gnus-picon
1504   :link '(custom-manual "(gnus)Customizing Articles")
1505   :link '(custom-manual "(gnus)Picons")
1506   :type gnus-article-treat-head-custom)
1507 (put 'gnus-treat-mail-picon 'highlight t)
1508
1509 (defcustom gnus-treat-newsgroups-picon
1510   (if (and (gnus-image-type-available-p 'xpm)
1511            (gnus-picons-installed-p))
1512       'head nil)
1513   "Display picons in the Newsgroups and Followup-To headers.
1514 Valid values are nil, t, `head', `first', `last', an integer or a
1515 predicate.  See Info node `(gnus)Customizing Articles' and Info
1516 node `(gnus)Picons' for details."
1517   :version "22.1"
1518   :group 'gnus-article-treat
1519   :group 'gnus-picon
1520   :link '(custom-manual "(gnus)Customizing Articles")
1521   :link '(custom-manual "(gnus)Picons")
1522   :type gnus-article-treat-head-custom)
1523 (put 'gnus-treat-newsgroups-picon 'highlight t)
1524
1525 (defcustom gnus-treat-from-gravatar nil
1526   "Display gravatars in the From header.
1527 Valid values are nil, t, `head', `first', `last', an integer or a
1528 predicate.  See Info node `(gnus)Customizing Articles' and Info
1529 node `(gnus)Gravatars' for details."
1530   :version "24.1"
1531   :group 'gnus-article-treat
1532   :group 'gnus-gravatar
1533   :link '(custom-manual "(gnus)Customizing Articles")
1534   :link '(custom-manual "(gnus)Gravatars")
1535   :type gnus-article-treat-head-custom)
1536 (put 'gnus-treat-from-gravatar 'highlight t)
1537
1538 (defcustom gnus-treat-mail-gravatar nil
1539   "Display gravatars in To and Cc headers.
1540 Valid values are nil, t, `head', `first', `last', an integer or a
1541 predicate.  See Info node `(gnus)Customizing Articles' and Info
1542 node `(gnus)Gravatars' for details."
1543   :version "24.1"
1544   :group 'gnus-article-treat
1545   :group 'gnus-gravatar
1546   :link '(custom-manual "(gnus)Customizing Articles")
1547   :link '(custom-manual "(gnus)Gravatars")
1548   :type gnus-article-treat-head-custom)
1549 (put 'gnus-treat-mail-gravatar 'highlight t)
1550
1551 (defcustom gnus-treat-body-boundary
1552   (if (or gnus-treat-newsgroups-picon
1553           gnus-treat-mail-picon
1554           gnus-treat-from-picon
1555           gnus-treat-from-gravatar
1556           gnus-treat-mail-gravatar)
1557       ;; If there's much decoration, the user might prefer a boundery.
1558       'head
1559     nil)
1560   "Draw a boundary at the end of the headers.
1561 Valid values are nil and `head'.
1562 See Info node `(gnus)Customizing Articles' for details."
1563   :version "22.1"
1564   :group 'gnus-article-treat
1565   :link '(custom-manual "(gnus)Customizing Articles")
1566   :type gnus-article-treat-head-custom)
1567
1568 (defcustom gnus-treat-capitalize-sentences nil
1569   "Capitalize sentence-starting words.
1570 Valid values are nil, t, `head', `first', `last', an integer or a
1571 predicate.  See Info node `(gnus)Customizing Articles'."
1572   :version "21.1"
1573   :group 'gnus-article-treat
1574   :link '(custom-manual "(gnus)Customizing Articles")
1575   :type gnus-article-treat-custom)
1576
1577 (defcustom gnus-treat-wash-html nil
1578   "Format as HTML.
1579 Valid values are nil, t, `head', `first', `last', an integer or a
1580 predicate.  See Info node `(gnus)Customizing Articles'."
1581   :version "22.1"
1582   :group 'gnus-article-treat
1583   :link '(custom-manual "(gnus)Customizing Articles")
1584   :type gnus-article-treat-custom)
1585
1586 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1587   "Fill long lines.
1588 Valid values are nil, t, `head', `first', `last', an integer or a
1589 predicate.  See Info node `(gnus)Customizing Articles'."
1590   :version "24.1"
1591   :group 'gnus-article-treat
1592   :link '(custom-manual "(gnus)Customizing Articles")
1593   :type gnus-article-treat-custom)
1594
1595 (defcustom gnus-treat-x-pgp-sig nil
1596   "Verify X-PGP-Sig.
1597 To automatically treat X-PGP-Sig, set it to head.
1598 Valid values are nil, t, `head', `first', `last', an integer or a
1599 predicate.  See Info node `(gnus)Customizing Articles'."
1600   :version "22.1"
1601   :group 'gnus-article-treat
1602   :group 'mime-security
1603   :link '(custom-manual "(gnus)Customizing Articles")
1604   :type gnus-article-treat-custom)
1605
1606 (defvar gnus-article-encrypt-protocol-alist
1607   '(("PGP" . mml2015-self-encrypt)))
1608
1609 ;; Set to nil if more than one protocol added to
1610 ;; gnus-article-encrypt-protocol-alist.
1611 (defcustom gnus-article-encrypt-protocol "PGP"
1612   "The protocol used for encrypt articles.
1613 It is a string, such as \"PGP\". If nil, ask user."
1614   :version "22.1"
1615   :type 'string
1616   :group 'mime-security)
1617
1618 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1619                               (mm-coding-system-p 'utf-8)
1620                               (executable-find idna-program))
1621   "Whether IDNA decoding of headers is used when viewing messages.
1622 This requires GNU Libidn, and by default only enabled if it is found."
1623   :version "22.1"
1624   :group 'gnus-article-headers
1625   :type 'boolean)
1626
1627 (defcustom gnus-article-over-scroll nil
1628   "If non-nil, allow scrolling the article buffer even when there no more text."
1629   :version "22.1"
1630   :group 'gnus-article
1631   :type 'boolean)
1632
1633 (defcustom gnus-inhibit-images nil
1634   "Non-nil means inhibit displaying of images inline in the article body."
1635   :version "24.1"
1636   :group 'gnus-article
1637   :type 'boolean)
1638
1639 (defcustom gnus-blocked-images 'gnus-block-private-groups
1640   "Images that have URLs matching this regexp will be blocked.
1641 This can also be a function to be evaluated.  If so, it will be
1642 called with the group name as the parameter, and should return a
1643 regexp."
1644   :version "24.1"
1645   :group 'gnus-art
1646   :type 'regexp)
1647
1648 ;;; Internal variables
1649
1650 (defvar gnus-english-month-names
1651   '("January" "February" "March" "April" "May" "June" "July" "August"
1652     "September" "October" "November" "December"))
1653
1654 (defvar article-goto-body-goes-to-point-min-p nil)
1655 (defvar gnus-article-wash-types nil)
1656 (defvar gnus-article-emphasis-alist nil)
1657 (defvar gnus-article-image-alist nil)
1658
1659 (defvar gnus-article-mime-handle-alist-1 nil)
1660 (defvar gnus-treatment-function-alist
1661   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1662     (gnus-treat-strip-banner gnus-article-strip-banner)
1663     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1664     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1665     (gnus-treat-buttonize gnus-article-add-buttons)
1666     (gnus-treat-fill-article gnus-article-fill-cited-article)
1667     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1668     (gnus-treat-strip-cr gnus-article-remove-cr)
1669     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1670     (gnus-treat-display-x-face gnus-article-display-x-face)
1671     (gnus-treat-display-face gnus-article-display-face)
1672     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1673     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1674     (gnus-treat-hide-signature gnus-article-hide-signature)
1675     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1676     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1677     (gnus-treat-from-picon gnus-treat-from-picon)
1678     (gnus-treat-mail-picon gnus-treat-mail-picon)
1679     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1680     (gnus-treat-strip-pem gnus-article-hide-pem)
1681     (gnus-treat-date gnus-article-treat-date)
1682     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1683     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1684     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1685     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1686     (gnus-treat-strip-trailing-blank-lines
1687      gnus-article-remove-trailing-blank-lines)
1688     (gnus-treat-strip-leading-blank-lines
1689      gnus-article-strip-leading-blank-lines)
1690     (gnus-treat-strip-multiple-blank-lines
1691      gnus-article-strip-multiple-blank-lines)
1692     (gnus-treat-overstrike gnus-article-treat-overstrike)
1693     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1694     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1695     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1696     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1697     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1698     (gnus-treat-display-smileys gnus-treat-smiley)
1699     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1700     (gnus-treat-wash-html gnus-article-wash-html)
1701     (gnus-treat-emphasize gnus-article-emphasize)
1702     (gnus-treat-hide-citation gnus-article-hide-citation)
1703     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1704     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1705     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1706
1707 (defvar gnus-article-mime-handle-alist nil)
1708 (defvar article-lapsed-timer nil)
1709 (defvar gnus-article-current-summary nil)
1710
1711 (defvar gnus-article-mode-syntax-table
1712   (let ((table (copy-syntax-table text-mode-syntax-table)))
1713     ;; This causes the citation match run O(2^n).
1714     ;; (modify-syntax-entry ?- "w" table)
1715     (modify-syntax-entry ?> ")<" table)
1716     (modify-syntax-entry ?< "(>" table)
1717     ;; make M-. in article buffers work for `foo' strings
1718     (modify-syntax-entry ?' " " table)
1719     (modify-syntax-entry ?` " " table)
1720     table)
1721   "Syntax table used in article mode buffers.
1722 Initialized from `text-mode-syntax-table.")
1723
1724 (defvar gnus-save-article-buffer nil)
1725
1726 (defvar gnus-number-of-articles-to-be-saved nil)
1727
1728 (defvar gnus-inhibit-hiding nil)
1729
1730 (defvar gnus-article-edit-mode nil)
1731
1732 ;;; Macros for dealing with the article buffer.
1733
1734 (defmacro gnus-with-article-headers (&rest forms)
1735   `(with-current-buffer gnus-article-buffer
1736      (save-restriction
1737        (let ((inhibit-read-only t)
1738              (inhibit-point-motion-hooks t)
1739              (case-fold-search t))
1740          (article-narrow-to-head)
1741          ,@forms))))
1742
1743 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1744 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1745
1746 (defmacro gnus-with-article-buffer (&rest forms)
1747   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1748      (with-current-buffer gnus-article-buffer
1749        (let ((inhibit-read-only t))
1750          ,@forms))))
1751
1752 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1753 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1754
1755 (defun gnus-article-goto-header (header)
1756   "Go to HEADER, which is a regular expression."
1757   (re-search-forward (concat "^\\(" header "\\):") nil t))
1758
1759 (defsubst gnus-article-hide-text (b e props)
1760   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1761   (gnus-add-text-properties-when 'article-type nil b e props)
1762   (when (memq 'intangible props)
1763     (put-text-property
1764      (max (1- b) (point-min))
1765      b 'intangible (cddr (memq 'intangible props)))))
1766
1767 (defsubst gnus-article-unhide-text (b e)
1768   "Remove hidden text properties from region between B and E."
1769   (remove-text-properties b e gnus-hidden-properties)
1770   (when (memq 'intangible gnus-hidden-properties)
1771     (put-text-property (max (1- b) (point-min))
1772                        b 'intangible nil)))
1773
1774 (defun gnus-article-hide-text-type (b e type)
1775   "Hide text of TYPE between B and E."
1776   (gnus-add-wash-type type)
1777   (gnus-article-hide-text
1778    b e (cons 'article-type (cons type gnus-hidden-properties))))
1779
1780 (defun gnus-article-unhide-text-type (b e type)
1781   "Unhide text of TYPE between B and E."
1782   (gnus-delete-wash-type type)
1783   (remove-text-properties
1784    b e (cons 'article-type (cons type gnus-hidden-properties)))
1785   (when (memq 'intangible gnus-hidden-properties)
1786     (put-text-property (max (1- b) (point-min))
1787                        b 'intangible nil)))
1788
1789 (defun gnus-article-hide-text-of-type (type)
1790   "Hide text of TYPE in the current buffer."
1791   (save-excursion
1792     (let ((b (point-min))
1793           (e (point-max)))
1794       (while (setq b (text-property-any b e 'article-type type))
1795         (add-text-properties b (incf b) gnus-hidden-properties)))))
1796
1797 (defun gnus-article-delete-text-of-type (type)
1798   "Delete text of TYPE in the current buffer."
1799   (save-excursion
1800     (let ((b (point-min)))
1801       (if (eq type 'multipart)
1802           ;; Remove MIME buttons associated with multipart/alternative parts.
1803           (progn
1804             (goto-char b)
1805             (while (if (get-text-property (point) 'gnus-part)
1806                        (setq b (point))
1807                      (when (setq b (next-single-property-change (point)
1808                                                                 'gnus-part))
1809                        (goto-char b)
1810                        t))
1811               (end-of-line)
1812               (skip-chars-forward "\n")
1813               (when (eq (get-text-property b 'article-type) 'multipart)
1814                 (delete-region b (point)))))
1815         (while (setq b (text-property-any b (point-max) 'article-type type))
1816           (delete-region
1817            b (or (text-property-not-all b (point-max) 'article-type type)
1818                  (point-max))))))))
1819
1820 (defun gnus-article-delete-invisible-text ()
1821   "Delete all invisible text in the current buffer."
1822   (save-excursion
1823     (let ((b (point-min)))
1824       (while (setq b (text-property-any b (point-max) 'invisible t))
1825         (delete-region
1826          b (or (text-property-not-all b (point-max) 'invisible t)
1827                (point-max)))))))
1828
1829 (defun gnus-article-text-type-exists-p (type)
1830   "Say whether any text of type TYPE exists in the buffer."
1831   (text-property-any (point-min) (point-max) 'article-type type))
1832
1833 (defsubst gnus-article-header-rank ()
1834   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1835   (let ((list gnus-sorted-header-list)
1836         (i 1))
1837     (while list
1838       (if (looking-at (car list))
1839           (setq list nil)
1840         (setq list (cdr list))
1841         (incf i)))
1842       i))
1843
1844 (defun article-hide-headers (&optional arg delete)
1845   "Hide unwanted headers and possibly sort them as well."
1846   (interactive)
1847   ;; This function might be inhibited.
1848   (unless gnus-inhibit-hiding
1849     (let ((inhibit-read-only t)
1850           (case-fold-search t)
1851           (max (1+ (length gnus-sorted-header-list)))
1852           (inhibit-point-motion-hooks t)
1853           (cur (current-buffer))
1854           ignored visible beg)
1855       (save-excursion
1856         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1857         ;; group parameters, so we should go to the summary buffer.
1858         (when (prog1
1859                   (condition-case nil
1860                       (progn (set-buffer gnus-summary-buffer) t)
1861                     (error nil))
1862                 (setq ignored (when (not gnus-visible-headers)
1863                                 (cond ((stringp gnus-ignored-headers)
1864                                        gnus-ignored-headers)
1865                                       ((listp gnus-ignored-headers)
1866                                        (mapconcat 'identity
1867                                                   gnus-ignored-headers
1868                                                   "\\|"))))
1869                       visible (cond ((stringp gnus-visible-headers)
1870                                      gnus-visible-headers)
1871                                     ((and gnus-visible-headers
1872                                           (listp gnus-visible-headers))
1873                                      (mapconcat 'identity
1874                                                 gnus-visible-headers
1875                                                 "\\|")))))
1876           (set-buffer cur))
1877         (save-restriction
1878           ;; First we narrow to just the headers.
1879           (article-narrow-to-head)
1880           ;; Hide any "From " lines at the beginning of (mail) articles.
1881           (while (looking-at "From ")
1882             (forward-line 1))
1883           (unless (bobp)
1884             (delete-region (point-min) (point)))
1885           ;; Then treat the rest of the header lines.
1886           ;; Then we use the two regular expressions
1887           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1888           ;; select which header lines is to remain visible in the
1889           ;; article buffer.
1890           (while (re-search-forward "^[^ \t:]*:" nil t)
1891             (beginning-of-line)
1892             ;; Mark the rank of the header.
1893             (put-text-property
1894              (point) (1+ (point)) 'message-rank
1895              (if (or (and visible (looking-at visible))
1896                      (and ignored
1897                           (not (looking-at ignored))))
1898                  (gnus-article-header-rank)
1899                (+ 2 max)))
1900             (forward-line 1))
1901           (message-sort-headers-1)
1902           (when (setq beg (text-property-any
1903                            (point-min) (point-max) 'message-rank (+ 2 max)))
1904             ;; We delete the unwanted headers.
1905             (gnus-add-wash-type 'headers)
1906             (add-text-properties (point-min) (+ 5 (point-min))
1907                                  '(article-type headers dummy-invisible t))
1908             (delete-region beg (point-max))))))))
1909
1910 (defun article-hide-boring-headers (&optional arg)
1911   "Toggle hiding of headers that aren't very interesting.
1912 If given a negative prefix, always show; if given a positive prefix,
1913 always hide."
1914   (interactive (gnus-article-hidden-arg))
1915   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1916              (not gnus-show-all-headers))
1917     (save-excursion
1918       (save-restriction
1919         (let ((inhibit-read-only t)
1920               (inhibit-point-motion-hooks t))
1921           (article-narrow-to-head)
1922           (dolist (elem gnus-boring-article-headers)
1923             (goto-char (point-min))
1924             (cond
1925              ;; Hide empty headers.
1926              ((eq elem 'empty)
1927               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1928                 (forward-line -1)
1929                 (gnus-article-hide-text-type
1930                  (point-at-bol)
1931                  (progn
1932                    (end-of-line)
1933                    (if (re-search-forward "^[^ \t]" nil t)
1934                        (match-beginning 0)
1935                      (point-max)))
1936                  'boring-headers)))
1937              ;; Hide boring Newsgroups header.
1938              ((eq elem 'newsgroups)
1939               (when (gnus-string-equal
1940                      (gnus-fetch-field "newsgroups")
1941                      (gnus-group-real-name
1942                       (if (boundp 'gnus-newsgroup-name)
1943                           gnus-newsgroup-name
1944                         "")))
1945                 (gnus-article-hide-header "newsgroups")))
1946              ((eq elem 'to-address)
1947               (let ((to (message-fetch-field "to"))
1948                     (to-address
1949                      (gnus-parameter-to-address
1950                       (if (boundp 'gnus-newsgroup-name)
1951                           gnus-newsgroup-name ""))))
1952                 (when (and to to-address
1953                            (ignore-errors
1954                              (gnus-string-equal
1955                               ;; only one address in To
1956                               (nth 1 (mail-extract-address-components to))
1957                               to-address)))
1958                   (gnus-article-hide-header "to"))))
1959              ((eq elem 'to-list)
1960               (let ((to (message-fetch-field "to"))
1961                     (to-list
1962                      (gnus-parameter-to-list
1963                       (if (boundp 'gnus-newsgroup-name)
1964                           gnus-newsgroup-name ""))))
1965                 (when (and to to-list
1966                            (ignore-errors
1967                              (gnus-string-equal
1968                               ;; only one address in To
1969                               (nth 1 (mail-extract-address-components to))
1970                               to-list)))
1971                   (gnus-article-hide-header "to"))))
1972              ((eq elem 'cc-list)
1973               (let ((cc (message-fetch-field "cc"))
1974                     (to-list
1975                      (gnus-parameter-to-list
1976                       (if (boundp 'gnus-newsgroup-name)
1977                           gnus-newsgroup-name ""))))
1978                 (when (and cc to-list
1979                            (ignore-errors
1980                              (gnus-string-equal
1981                               ;; only one address in CC
1982                               (nth 1 (mail-extract-address-components cc))
1983                               to-list)))
1984                   (gnus-article-hide-header "cc"))))
1985              ((eq elem 'followup-to)
1986               (when (gnus-string-equal
1987                      (message-fetch-field "followup-to")
1988                      (message-fetch-field "newsgroups"))
1989                 (gnus-article-hide-header "followup-to")))
1990              ((eq elem 'reply-to)
1991               (if (gnus-group-find-parameter
1992                    gnus-newsgroup-name 'broken-reply-to)
1993                   (gnus-article-hide-header "reply-to")
1994                 (let ((from (message-fetch-field "from"))
1995                       (reply-to (message-fetch-field "reply-to")))
1996                   (when
1997                       (and
1998                        from reply-to
1999                        (ignore-errors
2000                          (equal
2001                           (sort (mapcar
2002                                  (lambda (x) (downcase (cadr x)))
2003                                  (mail-extract-address-components from t))
2004                                 'string<)
2005                           (sort (mapcar
2006                                  (lambda (x) (downcase (cadr x)))
2007                                  (mail-extract-address-components reply-to t))
2008                                 'string<))))
2009                     (gnus-article-hide-header "reply-to")))))
2010              ((eq elem 'date)
2011               (let ((date (with-current-buffer gnus-original-article-buffer
2012                             ;; If date in `gnus-article-buffer' is localized
2013                             ;; (`gnus-treat-date-user-defined'),
2014                             ;; `days-between' might fail.
2015                             (message-fetch-field "date"))))
2016                 (when (and date
2017                            (< (days-between (current-time-string) date)
2018                               4))
2019                   (gnus-article-hide-header "date"))))
2020              ((eq elem 'long-to)
2021               (let ((to (message-fetch-field "to"))
2022                     (cc (message-fetch-field "cc")))
2023                 (when (> (length to) 1024)
2024                   (gnus-article-hide-header "to"))
2025                 (when (> (length cc) 1024)
2026                   (gnus-article-hide-header "cc"))))
2027              ((eq elem 'many-to)
2028               (let ((to-count 0)
2029                     (cc-count 0))
2030                 (goto-char (point-min))
2031                 (while (re-search-forward "^to:" nil t)
2032                   (setq to-count (1+ to-count)))
2033                 (when (> to-count 1)
2034                   (while (> to-count 0)
2035                     (goto-char (point-min))
2036                     (save-restriction
2037                       (re-search-forward "^to:" nil nil to-count)
2038                       (forward-line -1)
2039                       (narrow-to-region (point) (point-max))
2040                       (gnus-article-hide-header "to"))
2041                     (setq to-count (1- to-count))))
2042                 (goto-char (point-min))
2043                 (while (re-search-forward "^cc:" nil t)
2044                   (setq cc-count (1+ cc-count)))
2045                 (when (> cc-count 1)
2046                   (while (> cc-count 0)
2047                     (goto-char (point-min))
2048                     (save-restriction
2049                       (re-search-forward "^cc:" nil nil cc-count)
2050                       (forward-line -1)
2051                       (narrow-to-region (point) (point-max))
2052                       (gnus-article-hide-header "cc"))
2053                     (setq cc-count (1- cc-count)))))))))))))
2054
2055 (defun gnus-article-hide-header (header)
2056   (save-excursion
2057     (goto-char (point-min))
2058     (when (re-search-forward (concat "^" header ":") nil t)
2059       (gnus-article-hide-text-type
2060        (point-at-bol)
2061        (progn
2062          (end-of-line)
2063          (if (re-search-forward "^[^ \t]" nil t)
2064              (match-beginning 0)
2065            (point-max)))
2066        'boring-headers))))
2067
2068 (defvar gnus-article-normalized-header-length 40
2069   "Length of normalized headers.")
2070
2071 (defun article-normalize-headers ()
2072   "Make all header lines 40 characters long."
2073   (interactive)
2074   (let ((inhibit-read-only t)
2075         column)
2076     (save-excursion
2077       (save-restriction
2078         (article-narrow-to-head)
2079         (while (not (eobp))
2080           (cond
2081            ((< (setq column (- (point-at-eol) (point)))
2082                gnus-article-normalized-header-length)
2083             (end-of-line)
2084             (insert (make-string
2085                      (- gnus-article-normalized-header-length column)
2086                      ? )))
2087            ((> column gnus-article-normalized-header-length)
2088             (gnus-put-text-property
2089              (progn
2090                (forward-char gnus-article-normalized-header-length)
2091                (point))
2092              (point-at-eol)
2093              'invisible t))
2094            (t
2095             ;; Do nothing.
2096             ))
2097           (forward-line 1))))))
2098
2099 (defun article-treat-dumbquotes ()
2100   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2101 Note that this function guesses whether a character is a sm*rtq**t* or
2102 not, so it should only be used interactively.
2103
2104 Sm*rtq**t*s are M****s***'s unilateral extension to the
2105 iso-8859-1 character map in an attempt to provide more quoting
2106 characters.  If you see something like \\222 or \\264 where
2107 you're expecting some kind of apostrophe or quotation mark, then
2108 try this wash."
2109   (interactive)
2110   (article-translate-strings gnus-article-dumbquotes-map))
2111
2112 (defvar org-entities)
2113
2114 (defun article-treat-non-ascii ()
2115   "Translate many Unicode characters into their ASCII equivalents."
2116   (interactive)
2117   (require 'org-entities)
2118   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2119     (dolist (elem org-entities)
2120       (when (and (listp elem)
2121                  (= (length (nth 6 elem)) 1))
2122         (if (featurep 'xemacs)
2123             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2124           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2125     (save-excursion
2126       (when (article-goto-body)
2127         (let ((inhibit-read-only t)
2128               replace props)
2129           (while (not (eobp))
2130             (if (not (setq replace (if (featurep 'xemacs)
2131                                        (get-char-table (following-char) table)
2132                                      (aref table (following-char)))))
2133                 (forward-char 1)
2134               (if (prog1
2135                       (setq props (text-properties-at (point)))
2136                     (delete-char 1))
2137                   (add-text-properties (point) (progn (insert replace) (point))
2138                                        props)
2139                 (insert replace)))))))))
2140
2141 (defun article-translate-characters (from to)
2142   "Translate all characters in the body of the article according to FROM and TO.
2143 FROM is a string of characters to translate from; to is a string of
2144 characters to translate to."
2145   (save-excursion
2146     (when (article-goto-body)
2147       (let ((inhibit-read-only t)
2148             (x (make-string 225 ?x))
2149             (i -1))
2150         (while (< (incf i) (length x))
2151           (aset x i i))
2152         (setq i 0)
2153         (while (< i (length from))
2154           (aset x (aref from i) (aref to i))
2155           (incf i))
2156         (translate-region (point) (point-max) x)))))
2157
2158 (defun article-translate-strings (map)
2159   "Translate all string in the body of the article according to MAP.
2160 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2161   (save-excursion
2162     (when (article-goto-body)
2163       (let ((inhibit-read-only t))
2164         (dolist (elem map)
2165           (let ((from (car elem))
2166                 (to (cadr elem)))
2167             (save-excursion
2168               (if (stringp from)
2169                   (while (search-forward from nil t)
2170                     (replace-match to))
2171                 (while (not (eobp))
2172                   (if (eq (following-char) from)
2173                       (progn
2174                         (delete-char 1)
2175                         (insert to))
2176                     (forward-char 1)))))))))))
2177
2178 (defun article-treat-overstrike ()
2179   "Translate overstrikes into bold text."
2180   (interactive)
2181   (save-excursion
2182     (when (article-goto-body)
2183       (let ((inhibit-read-only t))
2184         (while (search-forward "\b" nil t)
2185           (let ((next (char-after))
2186                 (previous (char-after (- (point) 2))))
2187             ;; We do the boldification/underlining by hiding the
2188             ;; overstrikes and putting the proper text property
2189             ;; on the letters.
2190             (cond
2191              ((eq next previous)
2192               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2193               (put-text-property (point) (1+ (point)) 'face 'bold))
2194              ((eq next ?_)
2195               (gnus-article-hide-text-type
2196                (1- (point)) (1+ (point)) 'overstrike)
2197               (put-text-property
2198                (- (point) 2) (1- (point)) 'face 'underline))
2199              ((eq previous ?_)
2200               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2201               (put-text-property
2202                (point) (1+ (point)) 'face 'underline)))))))))
2203
2204 (defun article-treat-ansi-sequences ()
2205   "Translate ANSI SGR control sequences into overlays or extents."
2206   (interactive)
2207   (save-excursion
2208     (when (article-goto-body)
2209       (let ((inhibit-read-only t))
2210         (ansi-color-apply-on-region (point) (point-max))))))
2211
2212 (defun gnus-article-treat-unfold-headers ()
2213   "Unfold folded message headers.
2214 Only the headers that fit into the current window width will be
2215 unfolded."
2216   (interactive)
2217   (gnus-with-article-headers
2218     (let (length)
2219       (while (not (eobp))
2220         (save-restriction
2221           (mail-header-narrow-to-field)
2222           (let* ((header (buffer-string))
2223                  (unfoldable
2224                   (or (equal gnus-article-unfold-long-headers t)
2225                       (and (stringp gnus-article-unfold-long-headers)
2226                            (string-match gnus-article-unfold-long-headers header)))))
2227             (with-temp-buffer
2228               (insert header)
2229               (goto-char (point-min))
2230               (while (re-search-forward "\n[\t ]" nil t)
2231                 (replace-match " " t t)))
2232             (setq length (- (point-max) (point-min) 1))
2233             (when (or unfoldable
2234                       (< length (window-width)))
2235               (while (re-search-forward "\n[\t ]" nil t)
2236                 (replace-match " " t t))))
2237           (goto-char (point-max)))))))
2238
2239 (defun gnus-article-treat-fold-headers ()
2240   "Fold message headers."
2241   (interactive)
2242   (gnus-with-article-headers
2243     (while (not (eobp))
2244       (save-restriction
2245         (mail-header-narrow-to-field)
2246         (mail-header-fold-field)
2247         (goto-char (point-max))))))
2248
2249 (defun gnus-treat-smiley ()
2250   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2251   (interactive)
2252   (gnus-with-article-buffer
2253     (if (memq 'smiley gnus-article-wash-types)
2254         (gnus-delete-images 'smiley)
2255       (article-goto-body)
2256       (let ((images (smiley-region (point) (point-max))))
2257         (when images
2258           (gnus-add-wash-type 'smiley)
2259           (dolist (image images)
2260             (gnus-add-image 'smiley image)))))))
2261
2262 (defun gnus-article-remove-images ()
2263   "Remove all images from the article buffer."
2264   (interactive)
2265   (gnus-with-article-buffer
2266     (save-restriction
2267       (widen)
2268       (dolist (elem gnus-article-image-alist)
2269         (gnus-delete-images (car elem))))))
2270
2271 (defun gnus-article-show-images ()
2272   "Show any images that are in the HTML-rendered article buffer.
2273 This only works if the article in question is HTML."
2274   (interactive)
2275   (gnus-with-article-buffer
2276     (save-restriction
2277       (widen)
2278       (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2279                                                       'image-displayer))
2280         (destructuring-bind (start end function) region
2281           (funcall function (get-text-property start 'image-url)
2282                    start end))))))
2283
2284 (defun gnus-article-treat-fold-newsgroups ()
2285   "Unfold folded message headers.
2286 Only the headers that fit into the current window width will be
2287 unfolded."
2288   (interactive)
2289   (gnus-with-article-headers
2290     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2291       (save-restriction
2292         (mail-header-narrow-to-field)
2293         (while (re-search-forward ", *" nil t)
2294           (replace-match ", " t t))
2295         (mail-header-fold-field)
2296         (goto-char (point-max))))))
2297
2298 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2299   "Value of `truncate-lines' in Gnus Article buffer.
2300 Valid values are nil, t, `head', `first', `last', an integer or a
2301 predicate.  See Info node `(gnus)Customizing Articles'."
2302   :version "23.1" ;; No Gnus
2303   :group 'gnus-article
2304   ;; :link '(custom-manual "(gnus)Customizing Articles")
2305   :type 'boolean)
2306
2307 (defun gnus-article-toggle-truncate-lines (&optional arg)
2308   "Toggle whether to fold or truncate long lines in article the buffer.
2309 If ARG is non-nil and not a number, toggle
2310 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2311 long lines if and only if arg is positive."
2312   (interactive "P")
2313   (cond
2314    ((and (numberp arg) (> arg 0))
2315     (setq gnus-article-truncate-lines t))
2316    ((numberp arg)
2317     (setq gnus-article-truncate-lines nil))
2318    (arg
2319     (setq gnus-article-truncate-lines
2320           (not gnus-article-truncate-lines))))
2321   (gnus-with-article-buffer
2322     (cond
2323      ((and (numberp arg) (> arg 0))
2324       (setq truncate-lines nil))
2325      ((numberp arg)
2326       (setq truncate-lines t)))
2327     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2328     ;; `toggle-truncate-lines' needs an argument.
2329     (toggle-truncate-lines)))
2330
2331 (defun gnus-article-treat-body-boundary ()
2332   "Place a boundary line at the end of the headers."
2333   (interactive)
2334   (when (and gnus-body-boundary-delimiter
2335              (> (length gnus-body-boundary-delimiter) 0))
2336     (gnus-with-article-headers
2337       (goto-char (point-max))
2338       (let ((start (point)))
2339         (insert "X-Boundary: ")
2340         (gnus-add-text-properties start (point) '(invisible t intangible t))
2341        (insert (let (str (max (window-width)))
2342                  (if (featurep 'xemacs)
2343                      (setq max (1- max)))
2344                  (while (>= max (length str))
2345                     (setq str (concat str gnus-body-boundary-delimiter)))
2346                  (substring str 0 max))
2347                 "\n")
2348         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2349
2350 (defun article-fill-long-lines ()
2351   "Fill lines that are wider than the window width."
2352   (interactive)
2353   (save-excursion
2354     (let ((inhibit-read-only t)
2355           (width (window-width (get-buffer-window (current-buffer)))))
2356       (save-restriction
2357         (article-goto-body)
2358         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2359           (while (not (eobp))
2360             (end-of-line)
2361             (when (>= (current-column) (min fill-column width))
2362               (narrow-to-region (min (1+ (point)) (point-max))
2363                                 (point-at-bol))
2364               (let ((goback (point-marker)))
2365                 (fill-paragraph nil)
2366                 (goto-char (marker-position goback)))
2367               (widen))
2368             (forward-line 1)))))))
2369
2370 (defun article-capitalize-sentences ()
2371   "Capitalize the first word in each sentence."
2372   (interactive)
2373   (save-excursion
2374     (let ((inhibit-read-only t)
2375           (paragraph-start "^[\n\^L]"))
2376       (article-goto-body)
2377       (while (not (eobp))
2378         (capitalize-word 1)
2379         (forward-sentence)))))
2380
2381 (defun article-remove-cr ()
2382   "Remove trailing CRs and then translate remaining CRs into LFs."
2383   (interactive)
2384   (save-excursion
2385     (let ((inhibit-read-only t))
2386       (goto-char (point-min))
2387       (while (re-search-forward "\r+$" nil t)
2388         (replace-match "" t t))
2389       (goto-char (point-min))
2390       (while (search-forward "\r" nil t)
2391         (replace-match "\n" t t)))))
2392
2393 (defun article-remove-trailing-blank-lines ()
2394   "Remove all trailing blank lines from the article."
2395   (interactive)
2396   (save-excursion
2397     (let ((inhibit-read-only t))
2398       (goto-char (point-max))
2399       (delete-region
2400        (point)
2401        (progn
2402          (while (and (not (bobp))
2403                      (looking-at "^[ \t]*$")
2404                      (not (gnus-annotation-in-region-p
2405                            (point) (point-at-eol))))
2406            (forward-line -1))
2407          (forward-line 1)
2408          (point))))))
2409
2410 (defvar gnus-face-properties-alist)
2411
2412 (defun article-display-face (&optional force)
2413   "Display any Face headers in the header."
2414   (interactive (list 'force))
2415   (let ((wash-face-p buffer-read-only))
2416     (gnus-with-article-headers
2417       ;; When displaying parts, this function can be called several times on
2418       ;; the same article, without any intended toggle semantic (as typing `W
2419       ;; D d' would have). So face deletion must occur only when we come from
2420       ;; an interactive command, that is when the *Article* buffer is
2421       ;; read-only.
2422       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2423           (gnus-delete-images 'face)
2424         (let ((from (message-fetch-field "from"))
2425               face faces)
2426           (save-current-buffer
2427             (when (and wash-face-p
2428                        (gnus-buffer-live-p gnus-original-article-buffer)
2429                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2430               (set-buffer gnus-original-article-buffer))
2431             (save-restriction
2432               (mail-narrow-to-head)
2433               (when (or force
2434                         ;; Check whether this face is censored.
2435                         (not (and gnus-article-x-face-too-ugly
2436                                   (or from
2437                                       (setq from (message-fetch-field "from")))
2438                                   (string-match gnus-article-x-face-too-ugly
2439                                                 from))))
2440                 (while (gnus-article-goto-header "Face")
2441                   (push (mail-header-field-value) faces)))))
2442           (when faces
2443             (goto-char (point-min))
2444             (let (png image)
2445               (unless (setq from (gnus-article-goto-header "from"))
2446                 (insert "From:")
2447                 (setq from (point))
2448                 (insert " [no `from' set]\n"))
2449               (while faces
2450                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2451                   (setq image
2452                         (apply 'gnus-create-image png 'png t
2453                                (cdr (assq 'png gnus-face-properties-alist))))
2454                   (goto-char from)
2455                   (gnus-add-wash-type 'face)
2456                   (gnus-add-image 'face image)
2457                   (gnus-put-image image nil 'face))))))))))
2458
2459 (defun article-display-x-face (&optional force)
2460   "Look for an X-Face header and display it if present."
2461   (interactive (list 'force))
2462   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2463     (gnus-with-article-headers
2464       ;; Delete the old process, if any.
2465       (when (process-status "article-x-face")
2466         (delete-process "article-x-face"))
2467       ;; See the comment in `article-display-face'.
2468       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2469           ;; We have already displayed X-Faces, so we remove them
2470           ;; instead.
2471           (gnus-delete-images 'xface)
2472         ;; Display X-Faces.
2473         (let ((from (message-fetch-field "from"))
2474               x-faces face)
2475           (save-current-buffer
2476             (when (and wash-face-p
2477                        (gnus-buffer-live-p gnus-original-article-buffer)
2478                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2479               ;; If type `W f', use gnus-original-article-buffer,
2480               ;; otherwise use the current buffer because displaying
2481               ;; RFC822 parts calls this function too.
2482               (set-buffer gnus-original-article-buffer))
2483             (save-restriction
2484               (mail-narrow-to-head)
2485               (and gnus-article-x-face-command
2486                    (or force
2487                        ;; Check whether this face is censored.
2488                        (not (and gnus-article-x-face-too-ugly
2489                                  (or from
2490                                      (setq from (message-fetch-field "from")))
2491                                  (string-match gnus-article-x-face-too-ugly
2492                                                from))))
2493                    (while (gnus-article-goto-header "X-Face")
2494                      (push (mail-header-field-value) x-faces)))))
2495           (when x-faces
2496             ;; We display the face.
2497             (cond ((functionp gnus-article-x-face-command)
2498                    ;; The command is a lisp function, so we call it.
2499                    (mapc gnus-article-x-face-command x-faces))
2500                   ((stringp gnus-article-x-face-command)
2501                    ;; The command is a string, so we interpret the command
2502                    ;; as a, well, command, and fork it off.
2503                    (let ((process-connection-type nil))
2504                      (gnus-set-process-query-on-exit-flag
2505                       (start-process
2506                        "article-x-face" nil shell-file-name
2507                        shell-command-switch gnus-article-x-face-command)
2508                       nil)
2509                      ;; Sending multiple EOFs to xv doesn't work,
2510                      ;; so we only do a single external face.
2511                      (with-temp-buffer
2512                        (insert (car x-faces))
2513                        (process-send-region "article-x-face"
2514                                             (point-min) (point-max)))
2515                      (process-send-eof "article-x-face")))
2516                   (t
2517                    (error "`%s' set to `%s' is not a function"
2518                           gnus-article-x-face-command
2519                           'gnus-article-x-face-command)))))))))
2520
2521 (defun article-decode-mime-words ()
2522   "Decode all MIME-encoded words in the article."
2523   (interactive)
2524   (gnus-with-article-buffer
2525     (let ((inhibit-point-motion-hooks t)
2526           (mail-parse-charset gnus-newsgroup-charset)
2527           (mail-parse-ignored-charsets
2528            (with-current-buffer gnus-summary-buffer
2529              gnus-newsgroup-ignored-charsets)))
2530       (mail-decode-encoded-word-region (point-min) (point-max)))))
2531
2532 (defun article-decode-charset (&optional prompt)
2533   "Decode charset-encoded text in the article.
2534 If PROMPT (the prefix), prompt for a coding system to use."
2535   (interactive "P")
2536   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2537         (inhibit-read-only t)
2538         (mail-parse-charset gnus-newsgroup-charset)
2539         (mail-parse-ignored-charsets
2540          (save-excursion (condition-case nil
2541                              (set-buffer gnus-summary-buffer)
2542                            (error))
2543                          gnus-newsgroup-ignored-charsets))
2544         ct cte ctl charset format)
2545     (save-excursion
2546       (save-restriction
2547         (article-narrow-to-head)
2548         (setq ct (message-fetch-field "Content-Type" t)
2549               cte (message-fetch-field "Content-Transfer-Encoding" t)
2550               ctl (and ct (mail-header-parse-content-type ct))
2551               charset (cond
2552                        (prompt
2553                         (mm-read-coding-system "Charset to decode: "))
2554                        (ctl
2555                         (mail-content-type-get ctl 'charset)))
2556               format (and ctl (mail-content-type-get ctl 'format)))
2557         (when cte
2558           (setq cte (mail-header-strip cte)))
2559         (if (and ctl (not (string-match "/" (car ctl))))
2560             (setq ctl nil))
2561         (goto-char (point-max)))
2562       (forward-line 1)
2563       (save-restriction
2564         (narrow-to-region (point) (point-max))
2565         (when (and (eq mail-parse-charset 'gnus-decoded)
2566                    (eq (mm-body-7-or-8) '8bit))
2567           ;; The text code could have been decoded.
2568           (setq charset mail-parse-charset))
2569         (when (and (or (not ctl)
2570                        (equal (car ctl) "text/plain"))
2571                    (not format)) ;; article with format will decode later.
2572           (mm-decode-body
2573            charset (and cte (intern (downcase
2574                                      (gnus-strip-whitespace cte))))
2575            (car ctl)))))))
2576
2577 (defun article-decode-encoded-words ()
2578   "Remove encoded-word encoding from headers."
2579   (let ((inhibit-point-motion-hooks t)
2580         (mail-parse-charset gnus-newsgroup-charset)
2581         (mail-parse-ignored-charsets
2582          (save-excursion (condition-case nil
2583                              (set-buffer gnus-summary-buffer)
2584                            (error))
2585                          gnus-newsgroup-ignored-charsets))
2586         (inhibit-read-only t)
2587         end start)
2588     (goto-char (point-min))
2589     (when (search-forward "\n\n" nil 'move)
2590       (forward-line -1))
2591     (setq end (point))
2592     (while (not (bobp))
2593       (while (progn
2594                (forward-line -1)
2595                (and (not (bobp))
2596                     (memq (char-after) '(?\t ? )))))
2597       (setq start (point))
2598       (if (looking-at "\
2599 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2600 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2601           (funcall gnus-decode-address-function start end)
2602         (funcall gnus-decode-header-function start end))
2603       (goto-char (setq end start)))))
2604
2605 (defun article-decode-group-name ()
2606   "Decode group names in Newsgroups, Followup-To and Xref headers."
2607   (let ((inhibit-point-motion-hooks t)
2608         (inhibit-read-only t)
2609         (method (gnus-find-method-for-group gnus-newsgroup-name))
2610         regexp)
2611     (when (and (or gnus-group-name-charset-method-alist
2612                    gnus-group-name-charset-group-alist)
2613                (gnus-buffer-live-p gnus-original-article-buffer))
2614       (save-restriction
2615         (article-narrow-to-head)
2616         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2617           (with-current-buffer gnus-original-article-buffer
2618             (goto-char (point-min)))
2619           (setq regexp (concat "^" header
2620                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2621           (while (re-search-forward regexp nil t)
2622             (replace-match (save-match-data
2623                              (gnus-decode-newsgroups
2624                               ;; XXX how to use data in article buffer?
2625                               (with-current-buffer gnus-original-article-buffer
2626                                 (re-search-forward regexp nil t)
2627                                 (match-string 1))
2628                               gnus-newsgroup-name method))
2629                            t t nil 1))
2630           (goto-char (point-min)))))))
2631
2632 (autoload 'idna-to-unicode "idna")
2633
2634 (defun article-decode-idna-rhs ()
2635   "Decode IDNA strings in RHS in various headers in current buffer.
2636 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2637 Mail-Reply-To: and Mail-Followup-To:."
2638   (when gnus-use-idna
2639     (save-restriction
2640       (let ((inhibit-point-motion-hooks t)
2641             (inhibit-read-only t))
2642         (article-narrow-to-head)
2643         (goto-char (point-min))
2644         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2645           (let (ace unicode)
2646             (when (save-match-data
2647                     (and (setq ace (match-string 1))
2648                          (save-excursion
2649                            (and (re-search-backward "^[^ \t]" nil t)
2650                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2651                          (setq unicode (idna-to-unicode ace))))
2652               (unless (string= ace unicode)
2653                 (replace-match unicode nil nil nil 1)))))))))
2654
2655 (defun article-de-quoted-unreadable (&optional force read-charset)
2656   "Translate a quoted-printable-encoded article.
2657 If FORCE, decode the article whether it is marked as quoted-printable
2658 or not.
2659 If READ-CHARSET, ask for a coding system."
2660   (interactive (list 'force current-prefix-arg))
2661   (save-excursion
2662     (let ((inhibit-read-only t) type charset)
2663       (if (gnus-buffer-live-p gnus-original-article-buffer)
2664           (with-current-buffer gnus-original-article-buffer
2665             (setq type
2666                   (gnus-fetch-field "content-transfer-encoding"))
2667             (let* ((ct (gnus-fetch-field "content-type"))
2668                    (ctl (and ct (mail-header-parse-content-type ct))))
2669               (setq charset (and ctl
2670                                  (mail-content-type-get ctl 'charset)))
2671               (if (stringp charset)
2672                   (setq charset (intern (downcase charset)))))))
2673       (if read-charset
2674           (setq charset (mm-read-coding-system "Charset: " charset)))
2675       (unless charset
2676         (setq charset gnus-newsgroup-charset))
2677       (when (or force
2678                 (and type (let ((case-fold-search t))
2679                             (string-match "quoted-printable" type))))
2680         (article-goto-body)
2681         (quoted-printable-decode-region
2682          (point) (point-max) (mm-charset-to-coding-system charset))))))
2683
2684 (defun article-de-base64-unreadable (&optional force read-charset)
2685   "Translate a base64 article.
2686 If FORCE, decode the article whether it is marked as base64 not.
2687 If READ-CHARSET, ask for a coding system."
2688   (interactive (list 'force current-prefix-arg))
2689   (save-excursion
2690     (let ((inhibit-read-only t) type charset)
2691       (if (gnus-buffer-live-p gnus-original-article-buffer)
2692           (with-current-buffer gnus-original-article-buffer
2693             (setq type
2694                   (gnus-fetch-field "content-transfer-encoding"))
2695             (let* ((ct (gnus-fetch-field "content-type"))
2696                    (ctl (and ct (mail-header-parse-content-type ct))))
2697               (setq charset (and ctl
2698                                  (mail-content-type-get ctl 'charset)))
2699               (if (stringp charset)
2700                   (setq charset (intern (downcase charset)))))))
2701       (if read-charset
2702           (setq charset (mm-read-coding-system "Charset: " charset)))
2703       (unless charset
2704         (setq charset gnus-newsgroup-charset))
2705       (when (or force
2706                 (and type (let ((case-fold-search t))
2707                             (string-match "base64" type))))
2708         (article-goto-body)
2709         (save-restriction
2710           (narrow-to-region (point) (point-max))
2711           (base64-decode-region (point-min) (point-max))
2712           (mm-decode-coding-region
2713            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2714
2715 (eval-when-compile
2716   (require 'rfc1843))
2717
2718 (defun article-decode-HZ ()
2719   "Translate a HZ-encoded article."
2720   (interactive)
2721   (require 'rfc1843)
2722   (save-excursion
2723     (let ((inhibit-read-only t))
2724       (rfc1843-decode-region (point-min) (point-max)))))
2725
2726 (defun article-unsplit-urls ()
2727   "Remove the newlines that some other mailers insert into URLs."
2728   (interactive)
2729   (save-excursion
2730     (let ((inhibit-read-only t))
2731       (goto-char (point-min))
2732       (while (re-search-forward
2733               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2734         (replace-match "\\1\\3" t)))
2735     (when (interactive-p)
2736       (gnus-treat-article nil))))
2737
2738 (defun article-wash-html ()
2739   "Format an HTML article."
2740   (interactive)
2741   (let ((handles nil)
2742         (buffer-read-only nil))
2743     (when (gnus-buffer-live-p gnus-original-article-buffer)
2744       (setq handles (mm-dissect-buffer t t)))
2745     (article-goto-body)
2746     (delete-region (point) (point-max))
2747     (mm-inline-text-html handles)))
2748
2749 (defvar gnus-article-browse-html-temp-list nil
2750   "List of temporary files created by `gnus-article-browse-html-parts'.
2751 Internal variable.")
2752
2753 (defcustom gnus-article-browse-delete-temp 'ask
2754   "What to do with temporary files from `gnus-article-browse-html-parts'.
2755 If nil, don't delete temporary files.  If it is t, delete them on
2756 exit from the summary buffer.  If it is the symbol `file', query
2757 on each file, if it is `ask' ask once when exiting from the
2758 summary buffer."
2759   :group 'gnus-article
2760   :version "23.1" ;; No Gnus
2761   :type '(choice (const :tag "Don't delete" nil)
2762                  (const :tag "Don't ask" t)
2763                  (const :tag "Ask" ask)
2764                  (const :tag "Ask for each file" file)))
2765
2766 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2767
2768 (defun gnus-article-browse-delete-temp-files (&optional how)
2769   "Delete temp-files created by `gnus-article-browse-html-parts'."
2770   (when (and gnus-article-browse-html-temp-list
2771              (progn
2772                (or how (setq how gnus-article-browse-delete-temp))
2773                (if (eq how 'ask)
2774                    (let ((files (length gnus-article-browse-html-temp-list)))
2775                      (gnus-y-or-n-p (format
2776                                      "Delete all %s temporary HTML file%s? "
2777                                      files
2778                                      (if (> files 1) "s" ""))))
2779                  how)))
2780     (dolist (file gnus-article-browse-html-temp-list)
2781       (cond ((file-directory-p file)
2782              (when (or (not (eq how 'file))
2783                        (gnus-y-or-n-p
2784                         (format
2785                          "Delete temporary HTML file(s) in directory `%s'? "
2786                          (file-name-as-directory file))))
2787                (gnus-delete-directory file)))
2788             ((file-exists-p file)
2789              (when (or (not (eq how 'file))
2790                        (gnus-y-or-n-p
2791                         (format "Delete temporary HTML file `%s'? " file)))
2792                (delete-file file)))))
2793     ;; Also remove file from the list when not deleted or if file doesn't
2794     ;; exist anymore.
2795     (setq gnus-article-browse-html-temp-list nil))
2796   gnus-article-browse-html-temp-list)
2797
2798 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2799   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2800 Return file name."
2801   (save-match-data
2802     (let (file type)
2803       (catch 'found
2804         (dolist (handle handles)
2805           (cond
2806            ((not (listp handle)))
2807            ((equal (mm-handle-media-supertype handle) "multipart")
2808             (when (setq file (gnus-article-browse-html-save-cid-content
2809                               cid handle directory))
2810               (throw 'found file)))
2811            ((equal (concat "<" cid ">") (mm-handle-id handle))
2812             (setq file
2813                   (expand-file-name
2814                    (or (mail-content-type-get
2815                         (mm-handle-disposition handle) 'filename)
2816                        (mail-content-type-get
2817                         (setq type (mm-handle-type handle)) 'name)
2818                        (concat
2819                         (make-temp-name "cid")
2820                         (car (rassoc (car type) mailcap-mime-extensions))))
2821                    directory))
2822             (mm-save-part-to-file handle file)
2823             (throw 'found file))))))))
2824
2825 (defun gnus-article-browse-html-parts (list &optional header)
2826   "View all \"text/html\" parts from LIST.
2827 Recurse into multiparts.  The optional HEADER that should be a decoded
2828 message header will be added to the bodies of the \"text/html\" parts."
2829   ;; Internal function used by `gnus-article-browse-html-article'.
2830   (let (type file charset content cid-dir tmp-file showed)
2831     ;; Find and show the html-parts.
2832     (dolist (handle list)
2833       ;; If HTML, show it:
2834       (cond ((not (listp handle)))
2835             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2836                  (and (equal (car type) "message/external-body")
2837                       (or header
2838                           (setq file (or (mail-content-type-get type 'name)
2839                                          (mail-content-type-get
2840                                           (mm-handle-disposition handle)
2841                                           'filename))))
2842                       (or (mm-handle-cache handle)
2843                           (condition-case code
2844                               (progn (mm-extern-cache-contents handle) t)
2845                             (error
2846                              (gnus-message 3 "%s" (error-message-string code))
2847                              (when (>= gnus-verbose 3) (sit-for 2))
2848                              nil)))
2849                       (progn
2850                         (setq handle (mm-handle-cache handle)
2851                               type (mm-handle-type handle))
2852                         (equal (car type) "text/html"))))
2853              (setq charset (mail-content-type-get type 'charset)
2854                    content (mm-get-part handle))
2855              (with-temp-buffer
2856                (if (eq charset 'gnus-decoded)
2857                    (mm-enable-multibyte)
2858                  (mm-disable-multibyte))
2859                (insert content)
2860                ;; resolve cid contents
2861                (let ((case-fold-search t)
2862                      cid-file)
2863                  (goto-char (point-min))
2864                  (while (re-search-forward "\
2865 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2866                                            nil t)
2867                    (unless cid-dir
2868                      (setq cid-dir (mm-make-temp-file "cid" t))
2869                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2870                    (setq file nil
2871                          content nil)
2872                    (when (setq cid-file
2873                                (gnus-article-browse-html-save-cid-content
2874                                 (match-string 2)
2875                                 (with-current-buffer gnus-article-buffer
2876                                   gnus-article-mime-handles)
2877                                 cid-dir))
2878                      (replace-match (concat "file://" cid-file)
2879                                     nil nil nil 1))))
2880                (unless content (setq content (buffer-string))))
2881              (when (or charset header (not file))
2882                (setq tmp-file (mm-make-temp-file
2883                                ;; Do we need to care for 8.3 filenames?
2884                                "mm-" nil ".html")))
2885              ;; Add a meta html tag to specify charset and a header.
2886              (cond
2887               (header
2888                (let (title eheader body hcharset coding force-charset)
2889                  (with-temp-buffer
2890                    (mm-enable-multibyte)
2891                    (setq case-fold-search t)
2892                    (insert header "\n")
2893                    (setq title (message-fetch-field "subject"))
2894                    (goto-char (point-min))
2895                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2896                      (replace-match (cond ((match-beginning 1) "&lt;")
2897                                           ((match-beginning 2) "&gt;")
2898                                           (t "&amp;"))))
2899                    (goto-char (point-min))
2900                    (insert "<pre>\n")
2901                    (goto-char (point-max))
2902                    (insert "</pre>\n<hr>\n")
2903                    ;; We have to examine charset one by one since
2904                    ;; charset specified in parts might be different.
2905                    (if (eq charset 'gnus-decoded)
2906                        (setq charset 'utf-8
2907                              eheader (mm-encode-coding-string (buffer-string)
2908                                                               charset)
2909                              title (when title
2910                                      (mm-encode-coding-string title charset))
2911                              body (mm-encode-coding-string content charset)
2912                              force-charset t)
2913                      (setq hcharset (mm-find-mime-charset-region (point-min)
2914                                                                  (point-max)))
2915                      (cond ((= (length hcharset) 1)
2916                             (setq hcharset (car hcharset)
2917                                   coding (mm-charset-to-coding-system
2918                                           hcharset)))
2919                            ((> (length hcharset) 1)
2920                             (setq hcharset 'utf-8
2921                                   coding hcharset)))
2922                      (if coding
2923                          (if charset
2924                              (progn
2925                                (setq body
2926                                      (mm-charset-to-coding-system charset))
2927                                (if (eq coding body)
2928                                    (setq eheader (mm-encode-coding-string
2929                                                   (buffer-string) coding)
2930                                          title (when title
2931                                                  (mm-encode-coding-string
2932                                                   title coding))
2933                                          body content)
2934                                  (setq charset 'utf-8
2935                                        eheader (mm-encode-coding-string
2936                                                 (buffer-string) charset)
2937                                        title (when title
2938                                                (mm-encode-coding-string
2939                                                 title charset))
2940                                        body (mm-encode-coding-string
2941                                              (mm-decode-coding-string
2942                                               content body)
2943                                              charset)
2944                                        force-charset t)))
2945                            (setq charset hcharset
2946                                  eheader (mm-encode-coding-string
2947                                           (buffer-string) coding)
2948                                  title (when title
2949                                          (mm-encode-coding-string
2950                                           title coding))
2951                                  body content))
2952                        (setq eheader (mm-string-as-unibyte (buffer-string))
2953                              body content)))
2954                    (erase-buffer)
2955                    (mm-disable-multibyte)
2956                    (insert body)
2957                    (when charset
2958                      (mm-add-meta-html-tag handle charset force-charset))
2959                    (when title
2960                      (goto-char (point-min))
2961                      (unless (search-forward "<title>" nil t)
2962                        (re-search-forward "<head>\\s-*" nil t)
2963                        (insert "<title>" title "</title>\n")))
2964                    (goto-char (point-min))
2965                    (or (re-search-forward
2966                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2967                        (re-search-forward
2968                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2969                    (insert eheader)
2970                    (mm-write-region (point-min) (point-max)
2971                                     tmp-file nil nil nil 'binary t))))
2972               (charset
2973                (mm-with-unibyte-buffer
2974                  (insert (if (eq charset 'gnus-decoded)
2975                              (mm-encode-coding-string content
2976                                                       (setq charset 'utf-8))
2977                            content))
2978                  (if (or (mm-add-meta-html-tag handle charset)
2979                          (not file))
2980                      (mm-write-region (point-min) (point-max)
2981                                       tmp-file nil nil nil 'binary t)
2982                    (setq tmp-file nil))))
2983               (tmp-file
2984                (mm-save-part-to-file handle tmp-file)))
2985              (when tmp-file
2986                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2987              (add-hook 'gnus-summary-prepare-exit-hook
2988                        'gnus-article-browse-delete-temp-files)
2989              (add-hook 'gnus-exit-gnus-hook
2990                        (lambda  ()
2991                          (gnus-article-browse-delete-temp-files t)))
2992              ;; FIXME: Warn if there's an <img> tag?
2993              (browse-url-of-file (or tmp-file (expand-file-name file)))
2994              (setq showed t))
2995             ;; If multipart, recurse
2996             ((equal (mm-handle-media-supertype handle) "multipart")
2997              (when (gnus-article-browse-html-parts handle header)
2998                (setq showed t)))
2999             ((equal (mm-handle-media-type handle) "message/rfc822")
3000              (mm-with-multibyte-buffer
3001                (mm-insert-part handle)
3002                (setq handle (mm-dissect-buffer t t))
3003                (when (and (bufferp (car handle))
3004                           (stringp (car (mm-handle-type handle))))
3005                  (setq handle (list handle)))
3006                (when header
3007                  (article-decode-encoded-words)
3008                  (let ((gnus-visible-headers
3009                         (or (get 'gnus-visible-headers 'standard-value)
3010                             gnus-visible-headers)))
3011                    (article-hide-headers))
3012                  (goto-char (point-min))
3013                  (search-forward "\n\n" nil 'move)
3014                  (skip-chars-backward "\t\n ")
3015                  (setq header (buffer-substring (point-min) (point)))))
3016              (when (prog1
3017                        (gnus-article-browse-html-parts handle header)
3018                      (mm-destroy-parts handle))
3019                (setq showed t)))))
3020     showed))
3021
3022 (defun gnus-article-browse-html-article (&optional arg)
3023   "View \"text/html\" parts of the current article with a WWW browser.
3024 Inline images embedded in a message using the cid scheme, as they are
3025 generally considered to be safe, will be processed properly.
3026 The message header is added to the beginning of every html part unless
3027 the prefix argument ARG is given.
3028
3029 Warning: Spammers use links to images (using the http scheme) in HTML
3030 articles to verify whether you have read the message.  As
3031 `gnus-article-browse-html-article' passes the HTML content to the
3032 browser without eliminating these \"web bugs\" you should only
3033 use it for mails from trusted senders.
3034
3035 If you always want to display HTML parts in the browser, set
3036 `mm-text-html-renderer' to nil.
3037
3038 This command creates temporary files to pass HTML contents including
3039 images if any to the browser, and deletes them when exiting the group
3040 \(if you want)."
3041   ;; Cf. `mm-w3m-safe-url-regexp'
3042   (interactive "P")
3043   (if arg
3044       (gnus-summary-show-article)
3045     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3046                                     gnus-visible-headers))
3047           ;; As we insert a <hr>, there's no need for the body boundary.
3048           (gnus-treat-body-boundary nil))
3049       (gnus-summary-show-article)))
3050   (with-current-buffer gnus-article-buffer
3051     (let ((header (unless arg
3052                     (save-restriction
3053                       (widen)
3054                       (buffer-substring-no-properties
3055                        (goto-char (point-min))
3056                        (if (search-forward "\n\n" nil t)
3057                            (match-beginning 0)
3058                          (goto-char (point-max))
3059                          (skip-chars-backward "\t\n ")
3060                          (point))))))
3061           parts)
3062       (set-buffer gnus-original-article-buffer)
3063       (setq parts (mm-dissect-buffer t t))
3064       ;; If singlepart, enforce a list.
3065       (when (and (bufferp (car parts))
3066                  (stringp (car (mm-handle-type parts))))
3067         (setq parts (list parts)))
3068       ;; Process the list
3069       (unless (gnus-article-browse-html-parts parts header)
3070         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3071       (mm-destroy-parts parts)
3072       (unless arg
3073         (gnus-summary-show-article)))))
3074
3075 (defun article-hide-list-identifiers ()
3076   "Remove list identifies from the Subject header.
3077 The `gnus-list-identifiers' variable specifies what to do."
3078   (interactive)
3079   (let ((inhibit-point-motion-hooks t)
3080         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3081         (inhibit-read-only t))
3082     (when regexp
3083       (save-excursion
3084         (save-restriction
3085           (article-narrow-to-head)
3086           (goto-char (point-min))
3087           (while (re-search-forward
3088                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3089                   nil t)
3090             (delete-region (match-beginning 2) (match-end 0))
3091             (beginning-of-line))
3092           (when (re-search-forward
3093                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3094             (delete-region (match-beginning 1) (match-end 1))))))))
3095
3096 (defun article-hide-pem (&optional arg)
3097   "Toggle hiding of any PEM headers and signatures in the current article.
3098 If given a negative prefix, always show; if given a positive prefix,
3099 always hide."
3100   (interactive (gnus-article-hidden-arg))
3101   (unless (gnus-article-check-hidden-text 'pem arg)
3102     (save-excursion
3103       (let ((inhibit-read-only t) end)
3104         (goto-char (point-min))
3105         ;; Hide the horrendously ugly "header".
3106         (when (and (search-forward
3107                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3108                     nil t)
3109                    (setq end (1+ (match-beginning 0))))
3110           (gnus-add-wash-type 'pem)
3111           (gnus-article-hide-text-type
3112            end
3113            (if (search-forward "\n\n" nil t)
3114                (match-end 0)
3115              (point-max))
3116            'pem)
3117           ;; Hide the trailer as well
3118           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3119                                 nil t)
3120             (gnus-article-hide-text-type
3121              (match-beginning 0) (match-end 0) 'pem)))))))
3122
3123 (defun article-strip-banner ()
3124   "Strip the banners specified by the `banner' group parameter and by
3125 `gnus-article-address-banner-alist'."
3126   (interactive)
3127   (save-excursion
3128     (save-restriction
3129       (let ((inhibit-point-motion-hooks t))
3130         (when (gnus-parameter-banner gnus-newsgroup-name)
3131           (article-really-strip-banner
3132            (gnus-parameter-banner gnus-newsgroup-name)))
3133         (when gnus-article-address-banner-alist
3134           ;; Note that the From header is decoded here, so it is
3135           ;; required that the *-extract-address-components function
3136           ;; supports non-ASCII text.
3137           (let ((from (save-restriction
3138                         (widen)
3139                         (article-narrow-to-head)
3140                         (mail-fetch-field "from"))))
3141             (when (and from
3142                        (setq from
3143                              (cadr (funcall gnus-extract-address-components
3144                                             from))))
3145               (catch 'found
3146                 (dolist (pair gnus-article-address-banner-alist)
3147                   (when (string-match (car pair) from)
3148                     (throw 'found
3149                            (article-really-strip-banner (cdr pair)))))))))))))
3150
3151 (defun article-really-strip-banner (banner)
3152   "Strip the banner specified by the argument."
3153   (save-excursion
3154     (save-restriction
3155       (let ((inhibit-point-motion-hooks t)
3156             (gnus-signature-limit nil)
3157             (inhibit-read-only t))
3158         (article-goto-body)
3159         (cond
3160          ((eq banner 'signature)
3161           (when (gnus-article-narrow-to-signature)
3162             (widen)
3163             (forward-line -1)
3164             (delete-region (point) (point-max))))
3165          ((symbolp banner)
3166           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3167               (while (re-search-forward banner nil t)
3168                 (delete-region (match-beginning 0) (match-end 0)))))
3169          ((stringp banner)
3170           (while (re-search-forward banner nil t)
3171             (delete-region (match-beginning 0) (match-end 0)))))))))
3172
3173 (defun article-babel ()
3174   "Translate article using an online translation service."
3175   (interactive)
3176   (require 'babel)
3177   (gnus-with-article-buffer
3178     (when (article-goto-body)
3179       (let* ((start (point))
3180              (end (point-max))
3181              (orig (buffer-substring start end))
3182              (trans (babel-as-string orig)))
3183         (save-restriction
3184           (narrow-to-region start end)
3185           (delete-region start end)
3186           (insert trans))))))
3187
3188 (defun article-hide-signature (&optional arg)
3189   "Hide the signature in the current article.
3190 If given a negative prefix, always show; if given a positive prefix,
3191 always hide."
3192   (interactive (gnus-article-hidden-arg))
3193   (unless (gnus-article-check-hidden-text 'signature arg)
3194     (save-excursion
3195       (save-restriction
3196         (let ((inhibit-read-only t))
3197           (when (gnus-article-narrow-to-signature)
3198             (gnus-article-hide-text-type
3199              (point-min) (point-max) 'signature))))))
3200   (gnus-set-mode-line 'article))
3201
3202 (defun article-strip-headers-in-body ()
3203   "Strip offensive headers from bodies."
3204   (interactive)
3205   (save-excursion
3206     (article-goto-body)
3207     (let ((case-fold-search t))
3208       (when (looking-at "x-no-archive:")
3209         (gnus-delete-line)))))
3210
3211 (defun article-strip-leading-blank-lines ()
3212   "Remove all blank lines from the beginning of the article."
3213   (interactive)
3214   (save-excursion
3215     (let ((inhibit-point-motion-hooks t)
3216           (inhibit-read-only t))
3217       (when (article-goto-body)
3218         (while (and (not (eobp))
3219                     (looking-at "[ \t]*$"))
3220           (gnus-delete-line))))))
3221
3222 (defun article-narrow-to-head ()
3223   "Narrow the buffer to the head of the message.
3224 Point is left at the beginning of the narrowed-to region."
3225   (narrow-to-region
3226    (goto-char (point-min))
3227    (if (search-forward "\n\n" nil 1)
3228        (1- (point))
3229      (point-max)))
3230   (goto-char (point-min)))
3231
3232 (defun article-goto-body ()
3233   "Place point at the start of the body."
3234   (goto-char (point-min))
3235   (cond
3236    ;; This variable is only bound when dealing with separate
3237    ;; MIME body parts.
3238    (article-goto-body-goes-to-point-min-p
3239     t)
3240    ((search-forward "\n\n" nil t)
3241     t)
3242    (t
3243     (goto-char (point-max))
3244     nil)))
3245
3246 (defun article-strip-multiple-blank-lines ()
3247   "Replace consecutive blank lines with one empty line."
3248   (interactive)
3249   (save-excursion
3250     (let ((inhibit-point-motion-hooks t)
3251           (inhibit-read-only t))
3252       ;; First make all blank lines empty.
3253       (article-goto-body)
3254       (while (re-search-forward "^[ \t]+$" nil t)
3255         (unless (gnus-annotation-in-region-p
3256                  (match-beginning 0) (match-end 0))
3257           (replace-match "" nil t)))
3258       ;; Then replace multiple empty lines with a single empty line.
3259       (article-goto-body)
3260       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3261         (unless (gnus-annotation-in-region-p
3262                  (match-beginning 0) (match-end 0))
3263           (delete-region (match-beginning 1) (match-end 1)))))))
3264
3265 (defun article-strip-leading-space ()
3266   "Remove all white space from the beginning of the lines in the article."
3267   (interactive)
3268   (save-excursion
3269     (let ((inhibit-point-motion-hooks t)
3270           (inhibit-read-only t))
3271       (article-goto-body)
3272       (while (re-search-forward "^[ \t]+" nil t)
3273         (replace-match "" t t)))))
3274
3275 (defun article-strip-trailing-space ()
3276   "Remove all white space from the end of the lines in the article."
3277   (interactive)
3278   (save-excursion
3279     (let ((inhibit-point-motion-hooks t)
3280           (inhibit-read-only t))
3281       (article-goto-body)
3282       (while (re-search-forward "[ \t]+$" nil t)
3283         (replace-match "" t t)))))
3284
3285 (defun article-strip-blank-lines ()
3286   "Strip leading, trailing and multiple blank lines."
3287   (interactive)
3288   (article-strip-leading-blank-lines)
3289   (article-remove-trailing-blank-lines)
3290   (article-strip-multiple-blank-lines))
3291
3292 (defun article-strip-all-blank-lines ()
3293   "Strip all blank lines."
3294   (interactive)
3295   (save-excursion
3296     (let ((inhibit-point-motion-hooks t)
3297           (inhibit-read-only t))
3298       (article-goto-body)
3299       (while (re-search-forward "^[ \t]*\n" nil t)
3300         (replace-match "" t t)))))
3301
3302 (defun gnus-article-narrow-to-signature ()
3303   "Narrow to the signature; return t if a signature is found, else nil."
3304   (let ((inhibit-point-motion-hooks t))
3305     (when (gnus-article-search-signature)
3306       (forward-line 1)
3307       ;; Check whether we have some limits to what we consider
3308       ;; to be a signature.
3309       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3310                       (list gnus-signature-limit)))
3311             limit limited)
3312         (while (setq limit (pop limits))
3313           (if (or (and (integerp limit)
3314                        (< (- (point-max) (point)) limit))
3315                   (and (floatp limit)
3316                        (< (count-lines (point) (point-max)) limit))
3317                   (and (functionp limit)
3318                        (funcall limit))
3319                   (and (stringp limit)
3320                        (not (re-search-forward limit nil t))))
3321               ()                        ; This limit did not succeed.
3322             (setq limited t
3323                   limits nil)))
3324         (unless limited
3325           (narrow-to-region (point) (point-max))
3326           t)))))
3327
3328 (defun gnus-article-search-signature ()
3329   "Search the current buffer for the signature separator.
3330 Put point at the beginning of the signature separator."
3331   (let ((cur (point)))
3332     (goto-char (point-max))
3333     (if (if (stringp gnus-signature-separator)
3334             (re-search-backward gnus-signature-separator nil t)
3335           (let ((seps gnus-signature-separator))
3336             (while (and seps
3337                         (not (re-search-backward (car seps) nil t)))
3338               (pop seps))
3339             seps))
3340         t
3341       (goto-char cur)
3342       nil)))
3343
3344 (defun gnus-article-hidden-arg ()
3345   "Return the current prefix arg as a number, or 0 if no prefix."
3346   (list (if current-prefix-arg
3347             (prefix-numeric-value current-prefix-arg)
3348           0)))
3349
3350 (defun gnus-article-check-hidden-text (type arg)
3351   "Return nil if hiding is necessary.
3352 Arg can be nil or a number.  nil and positive means hide, negative
3353 means show, 0 means toggle."
3354   (save-excursion
3355     (save-restriction
3356       (let ((hide (gnus-article-hidden-text-p type)))
3357         (cond
3358          ((or (null arg)
3359               (> arg 0))
3360           nil)
3361          ((< arg 0)
3362           (gnus-article-show-hidden-text type)
3363           t)
3364          (t
3365           (if (eq hide 'hidden)
3366               (progn
3367                 (gnus-article-show-hidden-text type)
3368                 t)
3369             nil)))))))
3370
3371 (defun gnus-article-hidden-text-p (type)
3372   "Say whether the current buffer contains hidden text of type TYPE."
3373   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3374     (while (and pos
3375                 (not (get-text-property pos 'invisible))
3376                 (not (get-text-property pos 'dummy-invisible)))
3377       (setq pos
3378             (text-property-any (1+ pos) (point-max) 'article-type type)))
3379     (if pos
3380         'hidden
3381       nil)))
3382
3383 (defun gnus-article-show-hidden-text (type &optional dummy)
3384   "Show all hidden text of type TYPE.
3385 Originally it is hide instead of DUMMY."
3386   (let ((inhibit-read-only t)
3387         (inhibit-point-motion-hooks t))
3388     (gnus-remove-text-properties-when
3389      'article-type type
3390      (point-min) (point-max)
3391      (cons 'article-type (cons type
3392                                gnus-hidden-properties)))
3393     (gnus-delete-wash-type type)))
3394
3395 (defconst article-time-units
3396   `((year . ,(* 365.25 24 60 60))
3397     (week . ,(* 7 24 60 60))
3398     (day . ,(* 24 60 60))
3399     (hour . ,(* 60 60))
3400     (minute . 60)
3401     (second . 1))
3402   "Mapping from time units to seconds.")
3403
3404 (defun gnus-article-forward-header ()
3405   "Move point to the start of the next header.
3406 If the current header is a continuation header, this can be several
3407 lines forward."
3408   (let ((ended nil))
3409     (while (not ended)
3410       (forward-line 1)
3411       (if (looking-at "[ \t]+[^ \t]")
3412           (forward-line 1)
3413         (setq ended t)))))
3414
3415 (defun article-treat-date ()
3416   (article-date-ut gnus-article-date-headers t))
3417
3418 (defun article-date-ut (&optional type highlight date-position)
3419   "Convert DATE date to TYPE in the current article.
3420 The default type is `ut'.  See `gnus-article-date-headers' for
3421 possible values."
3422   (interactive (list 'ut t))
3423   (let* ((case-fold-search t)
3424          (inhibit-read-only t)
3425          (inhibit-point-motion-hooks t)
3426          (first t)
3427          (visible-date (mail-fetch-field "Date"))
3428          pos date bface eface)
3429     (save-excursion
3430       (save-restriction
3431         (goto-char (point-min))
3432         (when (re-search-forward "^Date:" nil t)
3433           (setq bface (get-text-property (point-at-bol) 'face)
3434                 eface (get-text-property (1- (point-at-eol)) 'face)))
3435         (goto-char (point-min))
3436         ;; Delete any old Date headers.
3437         (if date-position
3438             (progn
3439               (goto-char date-position)
3440               (setq date (get-text-property (point) 'original-date))
3441               (delete-region (point)
3442                              (progn
3443                                (gnus-article-forward-header)
3444                                (point)))
3445               (article-transform-date date type bface eface))
3446           (while (re-search-forward "^Date:" nil t)
3447             (setq date (get-text-property (match-beginning 0) 'original-date))
3448             (delete-region (point-at-bol) (progn
3449                                             (gnus-article-forward-header)
3450                                             (point))))
3451           (when (and (not date)
3452                      visible-date)
3453             (setq date visible-date))
3454           (when date
3455             (article-transform-date date type bface eface)))))))
3456
3457 (defun article-transform-date (date type bface eface)
3458   (dolist (this-type (cond
3459                       ((null type)
3460                        (list 'ut))
3461                       ((atom type)
3462                        (list type))
3463                       (t
3464                        type)))
3465     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3466     (forward-line -1)
3467     (beginning-of-line)
3468     (put-text-property (point) (1+ (point))
3469                        'original-date date)
3470     (put-text-property (point) (1+ (point))
3471                        'gnus-date-type this-type)
3472     ;; Do highlighting.
3473     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3474       (put-text-property (match-beginning 1) (1+ (match-end 1))
3475                          'face bface)
3476       (put-text-property (match-beginning 2) (match-end 2)
3477                          'face eface))
3478     (forward-line 1)))
3479
3480 (defun article-make-date-line (date type)
3481   "Return a DATE line of TYPE."
3482   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3483                              combined-lapsed))
3484     (error "Unknown conversion type: %s" type))
3485   (condition-case ()
3486       (let ((time (ignore-errors (date-to-time date))))
3487         (cond
3488          ;; Convert to the local timezone.
3489          ((eq type 'local)
3490           (concat "Date: " (message-make-date time)))
3491          ;; Convert to Universal Time.
3492          ((eq type 'ut)
3493           (concat "Date: "
3494                   (substring
3495                    (message-make-date
3496                     (let* ((e (parse-time-string date))
3497                            (tm (apply 'encode-time e))
3498                            (ms (car tm))
3499                            (ls (- (cadr tm) (car (current-time-zone time)))))
3500                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3501                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3502                             (t (list ms ls)))))
3503                    0 -5)
3504                   "UT"))
3505          ;; Get the original date from the article.
3506          ((eq type 'original)
3507           (concat "Date: " (if (string-match "\n+$" date)
3508                                (substring date 0 (match-beginning 0))
3509                              date)))
3510          ;; Let the user define the format.
3511          ((eq type 'user-defined)
3512           (let ((format (or (condition-case nil
3513                                 (with-current-buffer gnus-summary-buffer
3514                                   gnus-article-time-format)
3515                               (error nil))
3516                             gnus-article-time-format)))
3517             (if (functionp format)
3518                 (funcall format time)
3519               (concat "Date: " (format-time-string format time)))))
3520          ;; ISO 8601.
3521          ((eq type 'iso8601)
3522           (let ((tz (car (current-time-zone time))))
3523             (concat
3524              "Date: "
3525              (format-time-string "%Y%m%dT%H%M%S" time)
3526              (format "%s%02d%02d"
3527                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3528                      (/ (% (abs tz) 3600) 60)))))
3529          ;; Do a lapsed format.
3530          ((eq type 'lapsed)
3531           (concat "Date: " (article-lapsed-string time)))
3532          ;; A combined date/lapsed format.
3533          ((eq type 'combined-lapsed)
3534           (let ((date-string (article-make-date-line date 'original))
3535                 (segments 3)
3536                 lapsed-string)
3537             (while (and
3538                     time
3539                     (setq lapsed-string
3540                           (concat " (" (article-lapsed-string time segments) ")"))
3541                     (> (+ (length date-string)
3542                           (length lapsed-string))
3543                        (+ fill-column 6))
3544                     (> segments 0))
3545               (setq segments (1- segments)))
3546             (if (> segments 0)
3547                 (concat date-string lapsed-string)
3548               date-string)))
3549          ;; Display the date in proper English
3550          ((eq type 'english)
3551           (let ((dtime (decode-time time)))
3552             (concat
3553              "Date: the "
3554              (number-to-string (nth 3 dtime))
3555              (let ((digit (% (nth 3 dtime) 10)))
3556                (cond
3557                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3558                 ((= digit 1) "st")
3559                 ((= digit 2) "nd")
3560                 ((= digit 3) "rd")
3561                 (t "th")))
3562              " of "
3563              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3564              " "
3565              (number-to-string (nth 5 dtime))
3566              " at "
3567              (format "%02d" (nth 2 dtime))
3568              ":"
3569              (format "%02d" (nth 1 dtime)))))))
3570     (foo
3571      (format "Date: %s (from Gnus)" date))))
3572
3573 (defun article-lapsed-string (time &optional max-segments)
3574   ;; If the date is seriously mangled, the timezone functions are
3575   ;; liable to bug out, so we ignore all errors.
3576   (let* ((now (current-time))
3577          (real-time (subtract-time now time))
3578          (real-sec (and real-time
3579                         (+ (* (float (car real-time)) 65536)
3580                            (cadr real-time))))
3581          (sec (and real-time (abs real-sec)))
3582          (segments 0)
3583          num prev)
3584     (unless max-segments
3585       (setq max-segments (length article-time-units)))
3586     (cond
3587      ((null real-time)
3588       "Unknown")
3589      ((zerop sec)
3590       "Now")
3591      (t
3592       (concat
3593        ;; This is a bit convoluted, but basically we go
3594        ;; through the time units for years, weeks, etc,
3595        ;; and divide things to see whether that results
3596        ;; in positive answers.
3597        (mapconcat
3598         (lambda (unit)
3599           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3600                   (>= segments max-segments))
3601               ;; The (remaining) seconds are too few to
3602               ;; be divided into this time unit.
3603               ""
3604             ;; It's big enough, so we output it.
3605             (setq sec (- sec (* num (cdr unit))))
3606             (prog1
3607                 (concat (if prev ", " "") (int-to-string
3608                                            (floor num))
3609                         " " (symbol-name (car unit))
3610                         (if (> num 1) "s" ""))
3611               (setq prev t
3612                     segments (1+ segments)))))
3613         article-time-units "")
3614        ;; If dates are odd, then it might appear like the
3615        ;; article was sent in the future.
3616        (if (> real-sec 0)
3617            " ago"
3618          " in the future"))))))
3619
3620 (defun article-date-local (&optional highlight)
3621   "Convert the current article date to the local timezone."
3622   (interactive (list t))
3623   (article-date-ut 'local highlight))
3624
3625 (defun article-date-english (&optional highlight)
3626   "Convert the current article date to something that is proper English."
3627   (interactive (list t))
3628   (article-date-ut 'english highlight))
3629
3630 (defun article-date-original (&optional highlight)
3631   "Convert the current article date to what it was originally.
3632 This is only useful if you have used some other date conversion
3633 function and want to see what the date was before converting."
3634   (interactive (list t))
3635   (article-date-ut 'original highlight))
3636
3637 (defun article-date-lapsed (&optional highlight)
3638   "Convert the current article date to time lapsed since it was sent."
3639   (interactive (list t))
3640   (article-date-ut 'lapsed highlight))
3641
3642 (defun article-date-combined-lapsed (&optional highlight)
3643   "Convert the current article date to time lapsed since it was sent."
3644   (interactive (list t))
3645   (article-date-ut 'combined-lapsed highlight))
3646
3647 (defun article-update-date-lapsed ()
3648   "Function to be run from a timer to update the lapsed time line."
3649   (save-match-data
3650     (let ((buffer (current-buffer)))
3651       (ignore-errors
3652         (walk-windows
3653          (lambda (w)
3654            (set-buffer (window-buffer w))
3655            (when (eq major-mode 'gnus-article-mode)
3656              (let ((old-line (count-lines (point-min) (point)))
3657                    (old-column (- (point) (line-beginning-position)))
3658                    (window-start
3659                     (window-start (get-buffer-window (current-buffer)))))
3660                (goto-char (point-min))
3661                (while (re-search-forward "^Date:" nil t)
3662                  (let ((type (get-text-property (match-beginning 0)
3663                                                 'gnus-date-type)))
3664                    (when (memq type '(lapsed combined-lapsed user-format))
3665                      (when (and window-start
3666                                 (not (= window-start
3667                                         (save-excursion
3668                                           (forward-line 1)
3669                                           (point)))))
3670                        (setq window-start nil))
3671                      (save-excursion
3672                        (article-date-ut type t (match-beginning 0)))
3673                      (forward-line 1)
3674                      (when window-start
3675                        (set-window-start (get-buffer-window (current-buffer))
3676                                          (point))))))
3677                (goto-char (point-min))
3678                (when (> old-column 0)
3679                  (setq old-line (1- old-line)))
3680                (forward-line old-line)
3681                (end-of-line)
3682                (when (> (current-column) old-column)
3683                  (beginning-of-line)
3684                  (forward-char old-column)))))
3685          nil 'visible))
3686       (set-buffer buffer))))
3687
3688 (defun gnus-start-date-timer (&optional n)
3689   "Start a timer to update the Date headers in the article buffers.
3690 The numerical prefix says how frequently (in seconds) the function
3691 is to run."
3692   (interactive "p")
3693   (unless n
3694     (setq n 1))
3695   (gnus-stop-date-timer)
3696   (setq article-lapsed-timer
3697         (run-at-time 1 n 'article-update-date-lapsed)))
3698
3699 (defun gnus-stop-date-timer ()
3700   "Stop the Date timer."
3701   (interactive)
3702   (when article-lapsed-timer
3703     (nnheader-cancel-timer article-lapsed-timer)
3704     (setq article-lapsed-timer nil)))
3705
3706 (defun article-date-user (&optional highlight)
3707   "Convert the current article date to the user-defined format.
3708 This format is defined by the `gnus-article-time-format' variable."
3709   (interactive (list t))
3710   (article-date-ut 'user highlight))
3711
3712 (defun article-date-iso8601 (&optional highlight)
3713   "Convert the current article date to ISO8601."
3714   (interactive (list t))
3715   (article-date-ut 'iso8601 highlight))
3716
3717 (defmacro gnus-article-save-original-date (&rest forms)
3718   "Save the original date as a text property and evaluate FORMS."
3719   `(let* ((case-fold-search t)
3720           (start (progn
3721                    (goto-char (point-min))
3722                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3723                               (not (bolp)))
3724                      (match-end 0))))
3725           (date (when (and start
3726                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3727                                               nil t))
3728                   (buffer-substring-no-properties start
3729                                                   (match-beginning 0)))))
3730      (goto-char (point-max))
3731      (skip-chars-backward "\n")
3732      (put-text-property (point-min) (point) 'original-date date)
3733      ,@forms
3734      (goto-char (point-max))
3735      (skip-chars-backward "\n")
3736      (put-text-property (point-min) (point) 'original-date date)))
3737
3738 ;; (defun article-show-all ()
3739 ;;   "Show all hidden text in the article buffer."
3740 ;;   (interactive)
3741 ;;   (save-excursion
3742 ;;     (let ((inhibit-read-only t))
3743 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3744
3745 (defun article-remove-leading-whitespace ()
3746   "Remove excessive whitespace from all headers."
3747   (interactive)
3748   (save-excursion
3749     (save-restriction
3750       (let ((inhibit-read-only t))
3751         (article-narrow-to-head)
3752         (goto-char (point-min))
3753         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3754           (delete-region (match-beginning 1) (match-end 1)))))))
3755
3756 (defun article-emphasize (&optional arg)
3757   "Emphasize text according to `gnus-emphasis-alist'."
3758   (interactive (gnus-article-hidden-arg))
3759   (unless (gnus-article-check-hidden-text 'emphasis arg)
3760     (save-excursion
3761       (let ((alist (or
3762                     (condition-case nil
3763                         (with-current-buffer gnus-summary-buffer
3764                           gnus-article-emphasis-alist)
3765                       (error))
3766                     gnus-emphasis-alist))
3767             (inhibit-read-only t)
3768             (props (append '(article-type emphasis)
3769                            gnus-hidden-properties))
3770             regexp elem beg invisible visible face)
3771         (article-goto-body)
3772         (setq beg (point))
3773         (while (setq elem (pop alist))
3774           (goto-char beg)
3775           (setq regexp (car elem)
3776                 invisible (nth 1 elem)
3777                 visible (nth 2 elem)
3778                 face (nth 3 elem))
3779           (while (re-search-forward regexp nil t)
3780             (when (and (match-beginning visible) (match-beginning invisible))
3781               (gnus-article-hide-text
3782                (match-beginning invisible) (match-end invisible) props)
3783               (gnus-article-unhide-text-type
3784                (match-beginning visible) (match-end visible) 'emphasis)
3785               (gnus-put-overlay-excluding-newlines
3786                (match-beginning visible) (match-end visible) 'face face)
3787               (gnus-add-wash-type 'emphasis)
3788               (goto-char (match-end invisible)))))))))
3789
3790 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3791   "Setup newsgroup emphasis alist."
3792   (unless gnus-article-emphasis-alist
3793     (let ((name (and gnus-newsgroup-name
3794                      (gnus-group-real-name gnus-newsgroup-name))))
3795       (make-local-variable 'gnus-article-emphasis-alist)
3796       (setq gnus-article-emphasis-alist
3797             (nconc
3798              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3799                (while (setq elem (pop alist))
3800                  (when (and name (string-match (car elem) name))
3801                    (setq alist nil
3802                          highlight (copy-sequence (cdr elem)))))
3803                highlight)
3804              (copy-sequence highlight-words)
3805              (if gnus-newsgroup-name
3806                  (copy-sequence (gnus-group-find-parameter
3807                                  gnus-newsgroup-name 'highlight-words t)))
3808              gnus-emphasis-alist)))))
3809
3810 (defvar gnus-summary-article-menu)
3811 (defvar gnus-summary-post-menu)
3812
3813 ;;; Saving functions.
3814
3815 (defun gnus-article-save (save-buffer file &optional num)
3816   "Save the currently selected article."
3817   (when (or (get gnus-default-article-saver :headers)
3818             (not gnus-save-all-headers))
3819     ;; Remove headers according to `gnus-saved-headers' or the value
3820     ;; of the `:headers' property that the saver function might have.
3821     (let ((gnus-visible-headers
3822            (or (symbol-value (get gnus-default-article-saver :headers))
3823                gnus-saved-headers gnus-visible-headers))
3824           ;; Ignore group parameter.  See `article-hide-headers'.
3825           (gnus-summary-buffer nil))
3826       (with-current-buffer save-buffer
3827         (article-hide-headers 1 t))))
3828   (save-window-excursion
3829     (if (not gnus-default-article-saver)
3830         (error "No default saver is defined")
3831       ;; !!! Magic!  The saving functions all save
3832       ;; `gnus-save-article-buffer' (or so they think), but we
3833       ;; bind that variable to our save-buffer.
3834       (set-buffer gnus-article-buffer)
3835       (let* ((gnus-save-article-buffer save-buffer)
3836              (filename
3837               (cond
3838                ((not gnus-prompt-before-saving) 'default)
3839                ((eq gnus-prompt-before-saving 'always) nil)
3840                (t file)))
3841              (gnus-number-of-articles-to-be-saved
3842               (when (eq gnus-prompt-before-saving t)
3843                 num)))                  ; Magic
3844         (set-buffer gnus-article-current-summary)
3845         (funcall gnus-default-article-saver filename)))))
3846
3847 (defun gnus-read-save-file-name (prompt &optional filename
3848                                         function group headers variable
3849                                         dir-var)
3850   (let ((default-name
3851           (funcall function group headers (symbol-value variable)))
3852         result)
3853     (setq result
3854           (expand-file-name
3855            (cond
3856             ((eq filename 'default)
3857              default-name)
3858             ((eq filename t)
3859              default-name)
3860             (filename filename)
3861             (t
3862              (when (symbol-value dir-var)
3863                (setq default-name (expand-file-name
3864                                    (file-name-nondirectory default-name)
3865                                    (symbol-value dir-var))))
3866              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3867                     (prompt
3868                      (format prompt
3869                              (if (and gnus-number-of-articles-to-be-saved
3870                                       (> gnus-number-of-articles-to-be-saved 1))
3871                                  (format "these %d articles"
3872                                          gnus-number-of-articles-to-be-saved)
3873                                "this article")))
3874                     (file
3875                      ;; Let the split methods have their say.
3876                      (cond
3877                       ;; No split name was found.
3878                       ((null split-name)
3879                        (read-file-name
3880                         (concat prompt " (default "
3881                                 (file-name-nondirectory default-name) "): ")
3882                         (file-name-directory default-name)
3883                         default-name))
3884                       ;; A single group name is returned.
3885                       ((stringp split-name)
3886                        (setq default-name
3887                              (funcall function split-name headers
3888                                       (symbol-value variable)))
3889                        (read-file-name
3890                         (concat prompt " (default "
3891                                 (file-name-nondirectory default-name) "): ")
3892                         (file-name-directory default-name)
3893                         default-name))
3894                       ;; A single split name was found
3895                       ((= 1 (length split-name))
3896                        (let* ((name (expand-file-name
3897                                      (car split-name)
3898                                      gnus-article-save-directory))
3899                               (dir (cond ((file-directory-p name)
3900                                           (file-name-as-directory name))
3901                                          ((file-exists-p name) name)
3902                                          (t gnus-article-save-directory))))
3903                          (read-file-name
3904                           (concat prompt " (default " name "): ")
3905                           dir name)))
3906                       ;; A list of splits was found.
3907                       (t
3908                        (setq split-name (nreverse split-name))
3909                        (let (result)
3910                          (let ((file-name-history
3911                                 (nconc split-name file-name-history)))
3912                            (setq result
3913                                  (expand-file-name
3914                                   (read-file-name
3915                                    (concat prompt " (`M-p' for defaults): ")
3916                                    gnus-article-save-directory
3917                                    (car split-name))
3918                                   gnus-article-save-directory)))
3919                          (car (push result file-name-history)))))))
3920                ;; Create the directory.
3921                (gnus-make-directory (file-name-directory file))
3922                ;; If we have read a directory, we append the default file name.
3923                (when (file-directory-p file)
3924                  (setq file (expand-file-name (file-name-nondirectory
3925                                                default-name)
3926                                               (file-name-as-directory file))))
3927                ;; Possibly translate some characters.
3928                (nnheader-translate-file-chars file))))))
3929     (gnus-make-directory (file-name-directory result))
3930     (when variable
3931       (set variable result))
3932     (when dir-var
3933       (set dir-var (file-name-directory result)))
3934     result))
3935
3936 (defun gnus-article-archive-name (group)
3937   "Return the first instance of an \"Archive-name\" in the current buffer."
3938   (let ((case-fold-search t))
3939     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3940       (nnheader-concat gnus-article-save-directory
3941                        (match-string 1)))))
3942
3943 (defun gnus-article-nndoc-name (group)
3944   "If GROUP is an nndoc group, return the name of the parent group."
3945   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3946     (gnus-group-get-parameter group 'save-article-group)))
3947
3948 (defun gnus-summary-save-in-rmail (&optional filename)
3949   "Append this article to Rmail file.
3950 Optional argument FILENAME specifies file name.
3951 Directory to save to is default to `gnus-article-save-directory'."
3952   (setq filename (gnus-read-save-file-name
3953                   "Save %s in rmail file" filename
3954                   gnus-rmail-save-name gnus-newsgroup-name
3955                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3956   (with-current-buffer gnus-save-article-buffer
3957     (save-excursion
3958       (save-restriction
3959         (widen)
3960         ;; Note that unlike gnus-summary-save-in-mail, there is no
3961         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3962         ;; not use Babyl.
3963         (gnus-output-to-rmail filename))))
3964   filename)
3965
3966 (defun gnus-summary-save-in-mail (&optional filename)
3967   "Append this article to Unix mail file.
3968 Optional argument FILENAME specifies file name.
3969 Directory to save to is default to `gnus-article-save-directory'."
3970   (setq filename (gnus-read-save-file-name
3971                   "Save %s in Unix mail file" filename
3972                   gnus-mail-save-name gnus-newsgroup-name
3973                   gnus-current-headers 'gnus-newsgroup-last-mail))
3974   (with-current-buffer gnus-save-article-buffer
3975     (save-excursion
3976       (save-restriction
3977         (widen)
3978         (if (and (file-readable-p filename)
3979                  (file-regular-p filename)
3980                  (mail-file-babyl-p filename))
3981             (gnus-output-to-rmail filename)
3982           (gnus-output-to-mail filename)))))
3983   filename)
3984
3985 (put 'gnus-summary-save-in-file :decode t)
3986 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3987 (defun gnus-summary-save-in-file (&optional filename overwrite)
3988   "Append this article to file.
3989 Optional argument FILENAME specifies file name.
3990 Directory to save to is default to `gnus-article-save-directory'."
3991   (setq filename (gnus-read-save-file-name
3992                   "Save %s in file" filename
3993                   gnus-file-save-name gnus-newsgroup-name
3994                   gnus-current-headers 'gnus-newsgroup-last-file))
3995   (with-current-buffer gnus-save-article-buffer
3996     (save-excursion
3997       (save-restriction
3998         (widen)
3999         (when (and overwrite
4000                    (file-exists-p filename))
4001           (delete-file filename))
4002         (gnus-output-to-file filename))))
4003   filename)
4004
4005 (put 'gnus-summary-write-to-file :decode t)
4006 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4007 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4008 (defun gnus-summary-write-to-file (&optional filename)
4009   "Write this article to a file, overwriting it if the file exists.
4010 Optional argument FILENAME specifies file name.
4011 The directory to save in defaults to `gnus-article-save-directory'."
4012   (setq filename (gnus-read-save-file-name
4013                   "Save %s in file" filename
4014                   gnus-file-save-name gnus-newsgroup-name
4015                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4016   (gnus-summary-save-in-file filename t))
4017
4018 (put 'gnus-summary-save-body-in-file :decode t)
4019 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4020   "Append this article body to a file.
4021 Optional argument FILENAME specifies file name.
4022 The directory to save in defaults to `gnus-article-save-directory'."
4023   (setq filename (gnus-read-save-file-name
4024                   "Save %s body in file" filename
4025                   gnus-file-save-name gnus-newsgroup-name
4026                   gnus-current-headers 'gnus-newsgroup-last-file))
4027   (with-current-buffer gnus-save-article-buffer
4028     (save-excursion
4029       (save-restriction
4030         (widen)
4031         (when (article-goto-body)
4032           (narrow-to-region (point) (point-max)))
4033         (when (and overwrite
4034                    (file-exists-p filename))
4035           (delete-file filename))
4036         (gnus-output-to-file filename))))
4037   filename)
4038
4039 (put 'gnus-summary-write-body-to-file :decode t)
4040 (put 'gnus-summary-write-body-to-file
4041      :function 'gnus-summary-save-body-in-file)
4042 (defun gnus-summary-write-body-to-file (&optional filename)
4043   "Write this article body to a file, overwriting it if the file exists.
4044 Optional argument FILENAME specifies file name.
4045 The directory to save in defaults to `gnus-article-save-directory'."
4046   (setq filename (gnus-read-save-file-name
4047                   "Save %s body in file" filename
4048                   gnus-file-save-name gnus-newsgroup-name
4049                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4050   (gnus-summary-save-body-in-file filename t))
4051
4052 (put 'gnus-summary-save-in-pipe :decode t)
4053 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4054 (defun gnus-summary-save-in-pipe (&optional command raw)
4055   "Pipe this article to subprocess COMMAND.
4056 Valid values for COMMAND include:
4057   a string
4058     The executable command name and possibly arguments.
4059   nil
4060     You will be prompted for the command in the minibuffer.
4061   the symbol `default'
4062     It will be replaced with the command which the variable
4063     `gnus-summary-pipe-output-default-command' holds or the command
4064     last used for saving.
4065 Non-nil value for RAW overrides `:decode' and `:headers' properties
4066 and the raw article including all headers will be piped."
4067   (let ((article (gnus-summary-article-number))
4068         (decode (unless raw
4069                   (get 'gnus-summary-save-in-pipe :decode)))
4070         save-buffer default)
4071     (if article
4072         (if (vectorp (gnus-summary-article-header article))
4073             (save-current-buffer
4074               (gnus-summary-select-article decode decode nil article)
4075               (insert-buffer-substring
4076                (prog1
4077                    (if decode
4078                        gnus-article-buffer
4079                      gnus-original-article-buffer)
4080                  (setq save-buffer
4081                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4082               ;; Remove unwanted headers.
4083               (when (and (not raw)
4084                          (or (get 'gnus-summary-save-in-pipe :headers)
4085                              (not gnus-save-all-headers)))
4086                 (let ((gnus-visible-headers
4087                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4088                                               :headers))
4089                            gnus-saved-headers gnus-visible-headers))
4090                       (gnus-summary-buffer nil))
4091                   (article-hide-headers 1 t))))
4092           (error "%d is not a real article" article))
4093       (error "No article to pipe"))
4094     (setq default (or gnus-summary-pipe-output-default-command
4095                       gnus-last-shell-command))
4096     (unless (stringp command)
4097       (setq command
4098             (if (and (eq command 'default) default)
4099                 default
4100               (gnus-read-shell-command "Shell command on this article: "
4101                                        default))))
4102     (when (string-equal command "")
4103       (if default
4104           (setq command default)
4105         (error "A command is required")))
4106     (with-current-buffer save-buffer
4107       (save-restriction
4108         (widen)
4109         (shell-command-on-region (point-min) (point-max) command nil)))
4110     (gnus-kill-buffer save-buffer))
4111   (setq gnus-summary-pipe-output-default-command command))
4112
4113 (defun gnus-summary-pipe-to-muttprint (&optional command)
4114   "Pipe this article to muttprint."
4115   (unless (stringp command)
4116     (setq command (read-string
4117                    "Print using command: " gnus-summary-muttprint-program
4118                    nil gnus-summary-muttprint-program)))
4119   (let ((gnus-summary-pipe-output-default-command
4120          gnus-summary-pipe-output-default-command))
4121     (gnus-summary-save-in-pipe command))
4122   (setq gnus-summary-muttprint-program command))
4123
4124 ;;; Article file names when saving.
4125
4126 (defun gnus-capitalize-newsgroup (newsgroup)
4127   "Capitalize NEWSGROUP name."
4128   (when (not (zerop (length newsgroup)))
4129     (concat (char-to-string (upcase (aref newsgroup 0)))
4130             (substring newsgroup 1))))
4131
4132 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4133   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4134 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4135 Otherwise, it is like ~/News/news/group/num."
4136   (let ((default
4137           (expand-file-name
4138            (concat (if (gnus-use-long-file-name 'not-save)
4139                        (gnus-capitalize-newsgroup newsgroup)
4140                      (gnus-newsgroup-directory-form newsgroup))
4141                    "/" (int-to-string (mail-header-number headers)))
4142            gnus-article-save-directory)))
4143     (if (and last-file
4144              (string-equal (file-name-directory default)
4145                            (file-name-directory last-file))
4146              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4147         default
4148       (or last-file default))))
4149
4150 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4151   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4152 If variable `gnus-use-long-file-name' is non-nil, it is
4153 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4154   (let ((default
4155           (expand-file-name
4156            (concat (if (gnus-use-long-file-name 'not-save)
4157                        newsgroup
4158                      (gnus-newsgroup-directory-form newsgroup))
4159                    "/" (int-to-string (mail-header-number headers)))
4160            gnus-article-save-directory)))
4161     (if (and last-file
4162              (string-equal (file-name-directory default)
4163                            (file-name-directory last-file))
4164              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4165         default
4166       (or last-file default))))
4167
4168 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4169   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4170 If variable `gnus-use-long-file-name' is non-nil, it is
4171 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4172   (or last-file
4173       (expand-file-name
4174        (if (gnus-use-long-file-name 'not-save)
4175            newsgroup
4176          (file-relative-name
4177           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4178           default-directory))
4179        gnus-article-save-directory)))
4180
4181 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4182   "Generate file name from sender."
4183   (let ((from (mail-header-from headers)))
4184     (expand-file-name
4185      (if (and from (string-match "\\([^ <]+\\)@" from))
4186          (match-string 1 from)
4187        "nobody")
4188      gnus-article-save-directory)))
4189
4190 (defun article-verify-x-pgp-sig ()
4191   "Verify X-PGP-Sig."
4192   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4193   (interactive)
4194   (if (gnus-buffer-live-p gnus-original-article-buffer)
4195       (let ((sig (with-current-buffer gnus-original-article-buffer
4196                    (gnus-fetch-field "X-PGP-Sig")))
4197             items info headers)
4198         (when (and sig
4199                    mml2015-use
4200                    (mml2015-clear-verify-function))
4201           (with-temp-buffer
4202             (insert-buffer-substring gnus-original-article-buffer)
4203             (setq items (split-string sig))
4204             (message-narrow-to-head)
4205             (let ((inhibit-point-motion-hooks t)
4206                   (case-fold-search t))
4207               ;; Don't verify multiple headers.
4208               (setq headers (mapconcat (lambda (header)
4209                                          (concat header ": "
4210                                                  (mail-fetch-field header)
4211                                                  "\n"))
4212                                        (split-string (nth 1 items) ",") "")))
4213             (delete-region (point-min) (point-max))
4214             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4215             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4216             (insert headers)
4217             (widen)
4218             (forward-line)
4219             (while (not (eobp))
4220               (if (looking-at "^-")
4221                   (insert "- "))
4222               (forward-line))
4223             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4224             (insert "Version: " (car items) "\n\n")
4225             (insert (mapconcat 'identity (cddr items) "\n"))
4226             (insert "\n-----END PGP SIGNATURE-----\n")
4227             (let ((mm-security-handle (list (format "multipart/signed"))))
4228               (mml2015-clean-buffer)
4229               (let ((coding-system-for-write (or gnus-newsgroup-charset
4230                                                  'iso-8859-1)))
4231                 (funcall (mml2015-clear-verify-function)))
4232               (setq info
4233                     (or (mm-handle-multipart-ctl-parameter
4234                          mm-security-handle 'gnus-details)
4235                         (mm-handle-multipart-ctl-parameter
4236                          mm-security-handle 'gnus-info)))))
4237           (when info
4238             (let ((inhibit-read-only t) bface eface)
4239               (save-restriction
4240                 (message-narrow-to-head)
4241                 (goto-char (point-max))
4242                 (forward-line -1)
4243                 (setq bface (get-text-property (point-at-bol) 'face)
4244                       eface (get-text-property (1- (point-at-eol)) 'face))
4245                 (message-remove-header "X-Gnus-PGP-Verify")
4246                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4247                     (forward-line)
4248                   (goto-char (point-max)))
4249                 (narrow-to-region (point) (point))
4250                 (insert "X-Gnus-PGP-Verify: " info "\n")
4251                 (goto-char (point-min))
4252                 (forward-line)
4253                 (while (not (eobp))
4254                   (if (not (looking-at "^[ \t]"))
4255                       (insert " "))
4256                   (forward-line))
4257                 ;; Do highlighting.
4258                 (goto-char (point-min))
4259                 (when (looking-at "\\([^:]+\\): *")
4260                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4261                                      'face bface)
4262                   (put-text-property (match-end 0) (point-max)
4263                                      'face eface)))))))))
4264
4265 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4266
4267 (defun article-verify-cancel-lock ()
4268   "Verify Cancel-Lock header."
4269   (interactive)
4270   (if (gnus-buffer-live-p gnus-original-article-buffer)
4271       (canlock-verify gnus-original-article-buffer)))
4272
4273 (eval-and-compile
4274   (mapc
4275    (lambda (func)
4276      (let (afunc gfunc)
4277        (if (consp func)
4278            (setq afunc (car func)
4279                  gfunc (cdr func))
4280          (setq afunc func
4281                gfunc (intern (format "gnus-%s" func))))
4282        (defalias gfunc
4283          (when (fboundp afunc)
4284            `(lambda (&optional interactive &rest args)
4285               ,(documentation afunc t)
4286               (interactive (list t))
4287               (with-current-buffer gnus-article-buffer
4288                 (if interactive
4289                     (call-interactively ',afunc)
4290                   (apply ',afunc args))))))))
4291    '(article-hide-headers
4292      article-verify-x-pgp-sig
4293      article-verify-cancel-lock
4294      article-hide-boring-headers
4295      article-treat-overstrike
4296      article-treat-ansi-sequences
4297      article-fill-long-lines
4298      article-capitalize-sentences
4299      article-remove-cr
4300      article-remove-leading-whitespace
4301      article-display-x-face
4302      article-display-face
4303      article-de-quoted-unreadable
4304      article-de-base64-unreadable
4305      article-decode-HZ
4306      article-wash-html
4307      article-unsplit-urls
4308      article-hide-list-identifiers
4309      article-strip-banner
4310      article-babel
4311      article-hide-pem
4312      article-hide-signature
4313      article-strip-headers-in-body
4314      article-remove-trailing-blank-lines
4315      article-strip-leading-blank-lines
4316      article-strip-multiple-blank-lines
4317      article-strip-leading-space
4318      article-strip-trailing-space
4319      article-strip-blank-lines
4320      article-strip-all-blank-lines
4321      article-date-local
4322      article-date-english
4323      article-date-iso8601
4324      article-date-original
4325      article-treat-date
4326      article-date-ut
4327      article-decode-mime-words
4328      article-decode-charset
4329      article-decode-encoded-words
4330      article-date-user
4331      article-date-lapsed
4332      article-date-combined-lapsed
4333      article-emphasize
4334      article-treat-dumbquotes
4335      article-treat-non-ascii
4336      article-normalize-headers
4337      ;;(article-show-all . gnus-article-show-all-headers)
4338      )))
4339 \f
4340 ;;;
4341 ;;; Gnus article mode
4342 ;;;
4343
4344 (put 'gnus-article-mode 'mode-class 'special)
4345
4346 (set-keymap-parent gnus-article-mode-map widget-keymap)
4347
4348 (gnus-define-keys gnus-article-mode-map
4349   " " gnus-article-goto-next-page
4350   "\177" gnus-article-goto-prev-page
4351   [delete] gnus-article-goto-prev-page
4352   [backspace] gnus-article-goto-prev-page
4353   "\C-c^" gnus-article-refer-article
4354   "h" gnus-article-show-summary
4355   "s" gnus-article-show-summary
4356   "\C-c\C-m" gnus-article-mail
4357   "?" gnus-article-describe-briefly
4358   "<" beginning-of-buffer
4359   ">" end-of-buffer
4360   "\C-c\C-i" gnus-info-find-node
4361   "\C-c\C-b" gnus-bug
4362   "R" gnus-article-reply-with-original
4363   "F" gnus-article-followup-with-original
4364   "\C-hk" gnus-article-describe-key
4365   "\C-hc" gnus-article-describe-key-briefly
4366   "\C-hb" gnus-article-describe-bindings
4367
4368   "e" gnus-article-read-summary-keys
4369   "\C-d" gnus-article-read-summary-keys
4370   "\M-*" gnus-article-read-summary-keys
4371   "\M-#" gnus-article-read-summary-keys
4372   "\M-^" gnus-article-read-summary-keys
4373   "\M-g" gnus-article-read-summary-keys)
4374
4375 (substitute-key-definition
4376  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4377
4378 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4379   "W" gnus-article-wide-reply-with-original)
4380 (if (featurep 'xemacs)
4381     (set-keymap-default-binding gnus-article-send-map
4382                                 'gnus-article-read-summary-send-keys)
4383   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4384
4385 (defun gnus-article-make-menu-bar ()
4386   (unless (boundp 'gnus-article-commands-menu)
4387     (gnus-summary-make-menu-bar))
4388   (unless (boundp 'gnus-article-article-menu)
4389     (easy-menu-define
4390      gnus-article-article-menu gnus-article-mode-map ""
4391      '("Article"
4392        ["Scroll forwards" gnus-article-goto-next-page t]
4393        ["Scroll backwards" gnus-article-goto-prev-page t]
4394        ["Show summary" gnus-article-show-summary t]
4395        ["Fetch Message-ID at point" gnus-article-refer-article t]
4396        ["Mail to address at point" gnus-article-mail t]
4397        ["Send a bug report" gnus-bug t]))
4398
4399     (easy-menu-define
4400      gnus-article-treatment-menu gnus-article-mode-map ""
4401      ;; Fixme: this should use :active (and maybe :visible).
4402      '("Treatment"
4403        ["Hide headers" gnus-article-hide-headers t]
4404        ["Hide signature" gnus-article-hide-signature t]
4405        ["Hide citation" gnus-article-hide-citation t]
4406        ["Treat overstrike" gnus-article-treat-overstrike t]
4407        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4408        ["Remove carriage return" gnus-article-remove-cr t]
4409        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4410        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4411        ["Remove base64" gnus-article-de-base64-unreadable t]
4412        ["Treat html" gnus-article-wash-html t]
4413        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4414        ["Decode HZ" gnus-article-decode-HZ t]))
4415
4416     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4417
4418     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4419
4420     (gnus-run-hooks 'gnus-article-menu-hook)))
4421
4422 (defvar bookmark-make-record-function)
4423
4424 (defun gnus-article-mode ()
4425   "Major mode for displaying an article.
4426
4427 All normal editing commands are switched off.
4428
4429 The following commands are available in addition to all summary mode
4430 commands:
4431 \\<gnus-article-mode-map>
4432 \\[gnus-article-next-page]\t Scroll the article one page forwards
4433 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4434 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4435 \\[gnus-article-show-summary]\t Display the summary buffer
4436 \\[gnus-article-mail]\t Send a reply to the address near point
4437 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4438 \\[gnus-info-find-node]\t Go to the Gnus info node"
4439   (interactive)
4440   (kill-all-local-variables)
4441   (gnus-simplify-mode-line)
4442   (setq mode-name "Article")
4443   (setq major-mode 'gnus-article-mode)
4444   (make-local-variable 'minor-mode-alist)
4445   (use-local-map gnus-article-mode-map)
4446   (when (gnus-visual-p 'article-menu 'menu)
4447     (gnus-article-make-menu-bar)
4448     (when gnus-summary-tool-bar-map
4449       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4450   (gnus-update-format-specifications nil 'article-mode)
4451   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4452   (set (make-local-variable 'gnus-page-broken) nil)
4453   (make-local-variable 'gnus-article-current-summary)
4454   (make-local-variable 'gnus-article-mime-handles)
4455   (make-local-variable 'gnus-article-decoded-p)
4456   (make-local-variable 'gnus-article-mime-handle-alist)
4457   (make-local-variable 'gnus-article-wash-types)
4458   (make-local-variable 'gnus-article-image-alist)
4459   (make-local-variable 'gnus-article-charset)
4460   (make-local-variable 'gnus-article-ignored-charsets)
4461   (set (make-local-variable 'bookmark-make-record-function)
4462        'gnus-summary-bookmark-make-record)
4463   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4464   ;; face.
4465   (set (make-local-variable 'nobreak-char-display) nil)
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           (when gnus-article-mime-handles
4512             (mm-destroy-parts gnus-article-mime-handles)
4513             (setq gnus-article-mime-handles nil))
4514           ;; Set it to nil in article-buffer!
4515           (setq gnus-article-mime-handle-alist nil)
4516           (buffer-disable-undo)
4517           (setq buffer-read-only t)
4518           (unless (eq major-mode 'gnus-article-mode)
4519             (gnus-article-mode))
4520           (setq truncate-lines gnus-article-truncate-lines)
4521           (current-buffer))
4522       (with-current-buffer (gnus-get-buffer-create name)
4523         (gnus-article-mode)
4524         (setq truncate-lines gnus-article-truncate-lines)
4525         (make-local-variable 'gnus-summary-buffer)
4526         (setq gnus-summary-buffer
4527               (gnus-summary-buffer-name gnus-newsgroup-name))
4528         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4529         (when article-lapsed-timer
4530           (gnus-stop-date-timer))
4531         (when gnus-article-update-date-headers
4532           (gnus-start-date-timer gnus-article-update-date-headers))
4533         (current-buffer)))))
4534
4535 ;; Set article window start at LINE, where LINE is the number of lines
4536 ;; from the head of the article.
4537 (defun gnus-article-set-window-start (&optional line)
4538   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4539     (when article-window
4540       (set-window-start
4541        article-window
4542        (with-current-buffer gnus-article-buffer
4543          (goto-char (point-min))
4544          (if (not line)
4545              (point-min)
4546            (gnus-message 6 "Moved to bookmark")
4547            (search-forward "\n\n" nil t)
4548            (forward-line line)
4549            (point)))))))
4550
4551 (defun gnus-article-prepare (article &optional all-headers header)
4552   "Prepare ARTICLE in article mode buffer.
4553 ARTICLE should either be an article number or a Message-ID.
4554 If ARTICLE is an id, HEADER should be the article headers.
4555 If ALL-HEADERS is non-nil, no headers are hidden."
4556   (save-excursion
4557     ;; Make sure we start in a summary buffer.
4558     (unless (eq major-mode 'gnus-summary-mode)
4559       (set-buffer gnus-summary-buffer))
4560     (setq gnus-summary-buffer (current-buffer))
4561     (let* ((gnus-article (if header (mail-header-number header) article))
4562            (summary-buffer (current-buffer))
4563            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4564            (group gnus-newsgroup-name)
4565            result)
4566       (save-excursion
4567         (gnus-article-setup-buffer)
4568         (set-buffer gnus-article-buffer)
4569         ;; Deactivate active regions.
4570         (when (and (boundp 'transient-mark-mode)
4571                    transient-mark-mode)
4572           (setq mark-active nil))
4573         (if (not (setq result (let ((inhibit-read-only t))
4574                                 (gnus-request-article-this-buffer
4575                                  article group))))
4576             ;; There is no such article.
4577             (save-excursion
4578               (when (and (numberp article)
4579                          (not (memq article gnus-newsgroup-sparse)))
4580                 (setq gnus-article-current
4581                       (cons gnus-newsgroup-name article))
4582                 (set-buffer gnus-summary-buffer)
4583                 (setq gnus-current-article article)
4584                 (if (and (memq article gnus-newsgroup-undownloaded)
4585                          (not (gnus-online (gnus-find-method-for-group
4586                                             gnus-newsgroup-name))))
4587                     (progn
4588                       (gnus-summary-set-agent-mark article)
4589                       (message "Message marked for downloading"))
4590                   (gnus-summary-mark-article article gnus-canceled-mark)
4591                   (unless (memq article gnus-newsgroup-sparse)
4592                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4593           (if (or (eq result 'pseudo)
4594                   (eq result 'nneething))
4595               (progn
4596                 (with-current-buffer summary-buffer
4597                   (push article gnus-newsgroup-history)
4598                   (setq gnus-last-article gnus-current-article
4599                         gnus-current-article 0
4600                         gnus-current-headers nil
4601                         gnus-article-current nil)
4602                   (if (eq result 'nneething)
4603                       (gnus-configure-windows 'summary)
4604                     (gnus-configure-windows 'article))
4605                   (gnus-set-global-variables))
4606                 (let ((gnus-article-mime-handle-alist-1
4607                        gnus-article-mime-handle-alist))
4608                   (gnus-set-mode-line 'article)))
4609             ;; The result from the `request' was an actual article -
4610             ;; or at least some text that is now displayed in the
4611             ;; article buffer.
4612             (when (and (numberp article)
4613                        (not (eq article gnus-current-article)))
4614               ;; Seems like a new article has been selected.
4615               ;; `gnus-current-article' must be an article number.
4616               (with-current-buffer summary-buffer
4617                 (push article gnus-newsgroup-history)
4618                 (setq gnus-last-article gnus-current-article
4619                       gnus-current-article article
4620                       gnus-current-headers
4621                       (gnus-summary-article-header gnus-current-article)
4622                       gnus-article-current
4623                       (cons gnus-newsgroup-name gnus-current-article))
4624                 (unless (vectorp gnus-current-headers)
4625                   (setq gnus-current-headers nil))
4626                 (gnus-summary-goto-subject gnus-current-article)
4627                 (when (gnus-summary-show-thread)
4628                   ;; If the summary buffer really was folded, the
4629                   ;; previous goto may not actually have gone to
4630                   ;; the right article, but the thread root instead.
4631                   ;; So we go again.
4632                   (gnus-summary-goto-subject gnus-current-article))
4633                 (gnus-run-hooks 'gnus-mark-article-hook)
4634                 (gnus-set-mode-line 'summary)
4635                 (when (gnus-visual-p 'article-highlight 'highlight)
4636                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4637                 ;; Set the global newsgroup variables here.
4638                 (gnus-set-global-variables)
4639                 (setq gnus-have-all-headers
4640                       (or all-headers gnus-show-all-headers))))
4641             (save-excursion
4642               (gnus-configure-windows 'article))
4643             (when (or (numberp article)
4644                       (stringp article))
4645               (gnus-article-prepare-display)
4646               ;; Do page break.
4647               (goto-char (point-min))
4648               (when gnus-break-pages
4649                 (gnus-narrow-to-page)))
4650             (let ((gnus-article-mime-handle-alist-1
4651                    gnus-article-mime-handle-alist))
4652               (gnus-set-mode-line 'article))
4653             (article-goto-body)
4654             (unless (bobp)
4655               (forward-line -1))
4656             (set-window-point (get-buffer-window (current-buffer)) (point))
4657             (gnus-configure-windows 'article)
4658             (gnus-run-hooks 'gnus-article-prepare-hook)
4659             t))))))
4660
4661 ;;;###autoload
4662 (defun gnus-article-prepare-display ()
4663   "Make the current buffer look like a nice article."
4664   ;; Hooks for getting information from the article.
4665   ;; This hook must be called before being narrowed.
4666   (let ((gnus-article-buffer (current-buffer))
4667         buffer-read-only
4668         (inhibit-read-only t))
4669     (unless (eq major-mode 'gnus-article-mode)
4670       (gnus-article-mode))
4671     (setq buffer-read-only nil
4672           gnus-article-wash-types nil
4673           gnus-article-image-alist nil)
4674     (gnus-run-hooks 'gnus-tmp-internal-hook)
4675     (when gnus-display-mime-function
4676       (funcall gnus-display-mime-function))))
4677
4678 ;;;
4679 ;;; Gnus Sticky Article Mode
4680 ;;;
4681
4682 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4683   "Mode for sticky articles."
4684   ;; Release bindings that won't work.
4685   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4686                              gnus-sticky-article-mode-map)
4687   (substitute-key-definition 'gnus-article-refer-article 'undefined
4688                              gnus-sticky-article-mode-map)
4689   (dolist (k '("e" "h" "s" "F" "R"))
4690     (define-key gnus-sticky-article-mode-map k nil))
4691   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4692   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4693   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4694   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4695
4696 (defun gnus-sticky-article (arg)
4697   "Make the current article sticky.
4698 If a prefix ARG is given, ask for a name for this sticky article buffer."
4699   (interactive "P")
4700   (gnus-summary-show-thread)
4701   (gnus-summary-select-article nil nil 'pseudo)
4702   (let (new-art-buf-name)
4703     (gnus-eval-in-buffer-window gnus-article-buffer
4704       (setq new-art-buf-name
4705             (concat
4706              "*Sticky Article: "
4707              (if arg
4708                  (read-from-minibuffer "Sticky article buffer name: ")
4709                (gnus-with-article-headers
4710                  (gnus-article-goto-header "subject")
4711                  (setq new-art-buf-name
4712                        (buffer-substring-no-properties
4713                         (line-beginning-position) (line-end-position)))
4714                  (goto-char (point-min))
4715                  (gnus-article-goto-header "from")
4716                  (setq new-art-buf-name
4717                        (concat
4718                         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 "date")
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              "*"))
4729       (if (and (gnus-buffer-live-p new-art-buf-name)
4730                (with-current-buffer new-art-buf-name
4731                  (eq major-mode 'gnus-sticky-article-mode)))
4732           (switch-to-buffer new-art-buf-name)
4733         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4734       (gnus-sticky-article-mode))
4735     (setq gnus-article-buffer new-art-buf-name))
4736   (gnus-summary-recenter)
4737   (gnus-summary-position-point))
4738
4739 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4740   "Kill the given sticky article BUFFER.
4741 If none is given, assume the current buffer and kill it if it has
4742 `gnus-sticky-article-mode'."
4743   (interactive)
4744   (unless buffer
4745     (setq buffer (current-buffer)))
4746   (with-current-buffer buffer
4747     (when (eq major-mode 'gnus-sticky-article-mode)
4748       (gnus-kill-buffer buffer))))
4749
4750 (defun gnus-kill-sticky-article-buffers (arg)
4751   "Kill all sticky article buffers.
4752 If a prefix ARG is given, ask for confirmation."
4753   (interactive "P")
4754   (dolist (buf (gnus-buffers))
4755     (with-current-buffer buf
4756       (when (eq major-mode 'gnus-sticky-article-mode)
4757         (if (not arg)
4758             (gnus-kill-buffer buf)
4759           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4760             (gnus-kill-buffer buf)))))))
4761
4762 ;;;
4763 ;;; Gnus MIME viewing functions
4764 ;;;
4765
4766 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4767   "Format of the MIME buttons.
4768
4769 Valid specifiers include:
4770 %t  The MIME type
4771 %T  MIME type, along with additional info
4772 %n  The `name' parameter
4773 %d  The description, if any
4774 %l  The length of the encoded part
4775 %p  The part identifier number
4776 %e  Dots if the part isn't displayed
4777
4778 General format specifiers can also be used.  See Info node
4779 `(gnus)Formatting Variables'.")
4780
4781 (defvar gnus-mime-button-line-format-alist
4782   '((?t gnus-tmp-type ?s)
4783     (?T gnus-tmp-type-long ?s)
4784     (?n gnus-tmp-name ?s)
4785     (?d gnus-tmp-description ?s)
4786     (?p gnus-tmp-id ?s)
4787     (?l gnus-tmp-length ?d)
4788     (?e gnus-tmp-dots ?s)))
4789
4790 (defvar gnus-mime-button-commands
4791   '((gnus-article-press-button "\r" "Toggle Display")
4792     (gnus-mime-view-part "v" "View Interactively...")
4793     (gnus-mime-view-part-as-type "t" "View As Type...")
4794     (gnus-mime-view-part-as-charset "C" "View As charset...")
4795     (gnus-mime-save-part "o" "Save...")
4796     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4797     (gnus-mime-replace-part "r" "Replace part")
4798     (gnus-mime-delete-part "d" "Delete part")
4799     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4800     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4801     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4802     (gnus-mime-view-part-externally "e" "View Externally")
4803     (gnus-mime-print-part "p" "Print")
4804     (gnus-mime-pipe-part "|" "Pipe To Command...")
4805     (gnus-mime-action-on-part "." "Take action on the part...")))
4806
4807 (defun gnus-article-mime-part-status ()
4808   (if gnus-article-mime-handle-alist-1
4809       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4810           " (1 part)"
4811         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4812     ""))
4813
4814 (defvar gnus-mime-button-map
4815   (let ((map (make-sparse-keymap)))
4816     (define-key map gnus-mouse-2 'gnus-article-push-button)
4817     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4818     (dolist (c gnus-mime-button-commands)
4819       (define-key map (cadr c) (car c)))
4820     map))
4821
4822 (easy-menu-define
4823   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4824   `("MIME Part"
4825     ,@(mapcar (lambda (c)
4826                 (vector (caddr c) (car c) :active t))
4827               gnus-mime-button-commands)))
4828
4829 (defvar gnus-url-button-commands
4830   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4831
4832 (defvar gnus-url-button-map
4833   (let ((map (make-sparse-keymap)))
4834     (dolist (c gnus-url-button-commands)
4835       (define-key map (cadr c) (car c)))
4836     map))
4837
4838 (easy-menu-define
4839   gnus-url-button-menu gnus-url-button-map "URL button menu."
4840   `("Url Button"
4841     ,@(mapcar (lambda (c)
4842                 (vector (caddr c) (car c) :active t))
4843               gnus-url-button-commands)))
4844
4845 (defmacro gnus-bind-safe-url-regexp (&rest body)
4846   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4847   `(let ((mm-w3m-safe-url-regexp
4848           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4849                                 (gnus-buffer-live-p
4850                                  gnus-article-current-summary))
4851                            (with-current-buffer gnus-article-current-summary
4852                              gnus-newsgroup-name)
4853                          gnus-newsgroup-name)))
4854             (if (cond ((not group)
4855                        ;; Maybe we're in a mml-preview buffer
4856                        ;; and no group is selected.
4857                        t)
4858                       ((stringp gnus-safe-html-newsgroups)
4859                        (string-match gnus-safe-html-newsgroups group))
4860                       ((consp gnus-safe-html-newsgroups)
4861                        (member group gnus-safe-html-newsgroups)))
4862                 nil
4863               mm-w3m-safe-url-regexp))))
4864      ,@body))
4865
4866 (defun gnus-mime-button-menu (event prefix)
4867  "Construct a context-sensitive menu of MIME commands."
4868  (interactive "e\nP")
4869  (save-window-excursion
4870    (let ((pos (event-start event)))
4871      (select-window (posn-window pos))
4872      (goto-char (posn-point pos))
4873      (gnus-article-check-buffer)
4874      (popup-menu gnus-mime-button-menu nil prefix))))
4875
4876 (defun gnus-mime-view-all-parts (&optional handles)
4877   "View all the MIME parts."
4878   (interactive)
4879   (with-current-buffer gnus-article-buffer
4880     (let ((handles (or handles gnus-article-mime-handles))
4881           (mail-parse-charset gnus-newsgroup-charset)
4882           (mail-parse-ignored-charsets
4883            (with-current-buffer gnus-summary-buffer
4884              gnus-newsgroup-ignored-charsets)))
4885       (when handles
4886         (mm-remove-parts handles)
4887         (goto-char (point-min))
4888         (or (search-forward "\n\n") (goto-char (point-max)))
4889         (let ((inhibit-read-only t))
4890           (delete-region (point) (point-max))
4891           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4892
4893 (defun gnus-article-jump-to-part (n)
4894   "Jump to MIME part N."
4895   (interactive "P")
4896   (let ((parts (with-current-buffer gnus-article-buffer
4897                  (length gnus-article-mime-handle-alist))))
4898     (when (zerop parts)
4899       (error "No such part"))
4900     (pop-to-buffer gnus-article-buffer)
4901     (or n
4902         (setq n (if (= parts 1)
4903                     1
4904                   (read-number (format "Jump to part (1..%s): " parts)))))
4905     (unless (and (integerp n) (<= n parts) (>= n 1))
4906       (setq n
4907             (progn
4908               (gnus-message 7 "Invalid part `%s', using %s instead."
4909                             n parts)
4910               parts)))
4911     (gnus-message 9 "Jumping to part %s." n)
4912     (cond ((>= gnus-auto-select-part 1)
4913            (while (and (<= n parts)
4914                        (not (gnus-article-goto-part n)))
4915              (setq n (1+ n))))
4916           ((< gnus-auto-select-part 0)
4917            (while (and (>= n 1)
4918                        (not (gnus-article-goto-part n)))
4919              (setq n (1- n))))
4920           (t
4921            (gnus-article-goto-part n)))))
4922
4923 (defvar gnus-mime-buttonized-part-id nil
4924   "ID of a mime part that should be buttonized.
4925 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4926
4927 (eval-when-compile
4928   (defsubst gnus-article-edit-part (handles &optional current-id)
4929     "Edit an article in order to delete a mime part.
4930 This function is exclusively used by `gnus-mime-save-part-and-strip'
4931 and `gnus-mime-delete-part', and not provided at run-time normally."
4932     (gnus-article-edit-article
4933      `(lambda ()
4934         (buffer-disable-undo)
4935         (erase-buffer)
4936         (let ((mail-parse-charset (or gnus-article-charset
4937                                       ',gnus-newsgroup-charset))
4938               (mail-parse-ignored-charsets
4939                (or gnus-article-ignored-charsets
4940                    ',gnus-newsgroup-ignored-charsets))
4941               (mbl mml-buffer-list))
4942           (setq mml-buffer-list nil)
4943           (insert-buffer-substring gnus-original-article-buffer)
4944           (mime-to-mml ',handles)
4945           (setq gnus-article-mime-handles nil)
4946           (let ((mbl1 mml-buffer-list))
4947             (setq mml-buffer-list mbl)
4948             (set (make-local-variable 'mml-buffer-list) mbl1))
4949           (gnus-make-local-hook 'kill-buffer-hook)
4950           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4951      `(lambda (no-highlight)
4952         (let ((mail-parse-charset (or gnus-article-charset
4953                                       ',gnus-newsgroup-charset))
4954               (message-options message-options)
4955               (message-options-set-recipient)
4956               (mail-parse-ignored-charsets
4957                (or gnus-article-ignored-charsets
4958                    ',gnus-newsgroup-ignored-charsets)))
4959           (mml-to-mime)
4960           (mml-destroy-buffers)
4961           (remove-hook 'kill-buffer-hook
4962                        'mml-destroy-buffers t)
4963           (kill-local-variable 'mml-buffer-list))
4964         (gnus-summary-edit-article-done
4965          ,(or (mail-header-references gnus-current-headers) "")
4966          ,(gnus-group-read-only-p)
4967          ,gnus-summary-buffer no-highlight))
4968      t)
4969     ;; Force buttonizing this part.
4970     (let ((gnus-mime-buttonized-part-id current-id))
4971       (gnus-article-edit-done))
4972     (gnus-configure-windows 'article)
4973     (when (and current-id (integerp gnus-auto-select-part))
4974       (gnus-article-jump-to-part
4975        (min (max (+ current-id gnus-auto-select-part) 1)
4976             (with-current-buffer gnus-article-buffer
4977               (length gnus-article-mime-handle-alist)))))))
4978
4979 (defun gnus-mime-replace-part (file)
4980   "Replace MIME part under point with an external body."
4981   ;; Useful if file has already been saved to disk
4982   (interactive
4983    (list
4984     (read-file-name "Replace MIME part with file: "
4985                     (or mm-default-directory default-directory)
4986                     nil nil)))
4987   (gnus-mime-save-part-and-strip file))
4988
4989 (defun gnus-mime-save-part-and-strip (&optional file)
4990   "Save the MIME part under point then replace it with an external body.
4991 If FILE is given, use it for the external part."
4992   (interactive)
4993   (gnus-article-check-buffer)
4994   (when (gnus-group-read-only-p)
4995     (error "The current group does not support deleting of parts"))
4996   (when (mm-complicated-handles gnus-article-mime-handles)
4997     (error "\
4998 The current article has a complicated MIME structure, giving up..."))
4999   (let* ((data (get-text-property (point) 'gnus-data))
5000          (id (get-text-property (point) 'gnus-part))
5001          param
5002          (handles gnus-article-mime-handles))
5003     (unless file
5004       (setq file
5005             (and data (mm-save-part data "Delete MIME part and save to: "))))
5006     (when file
5007       (with-current-buffer (mm-handle-buffer data)
5008         (erase-buffer)
5009         (insert "Content-Type: " (mm-handle-media-type data))
5010         (mml-insert-parameter-string (cdr (mm-handle-type data))
5011                                      '(charset))
5012         ;; Add a filename for the sake of saving the part again.
5013         (mml-insert-parameter
5014          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5015         (insert "\n")
5016         (insert "Content-ID: " (message-make-message-id) "\n")
5017         (insert "Content-Transfer-Encoding: binary\n")
5018         (insert "\n"))
5019       (setcdr data
5020               (cdr (mm-make-handle nil
5021                                    `("message/external-body"
5022                                      (access-type . "LOCAL-FILE")
5023                                      (name . ,file)))))
5024       ;; (set-buffer gnus-summary-buffer)
5025       (gnus-article-edit-part handles id))))
5026
5027 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5028 ;; parts...>') but with stripping would be nice.
5029
5030 (defun gnus-mime-delete-part ()
5031   "Delete the MIME part under point.
5032 Replace it with some information about the removed part."
5033   (interactive)
5034   (gnus-article-check-buffer)
5035   (when (gnus-group-read-only-p)
5036     (error "The current group does not support deleting of parts"))
5037   (when (mm-complicated-handles gnus-article-mime-handles)
5038     (error "\
5039 The current article has a complicated MIME structure, giving up..."))
5040   (when (or gnus-expert-user
5041             (gnus-yes-or-no-p "\
5042 Deleting parts may malfunction or destroy the article; continue? "))
5043     (let* ((data (get-text-property (point) 'gnus-data))
5044            (id (get-text-property (point) 'gnus-part))
5045            (handles gnus-article-mime-handles)
5046            (none "(none)")
5047            (description
5048             (let ((desc (mm-handle-description data)))
5049               (when desc
5050                 (mail-decode-encoded-word-string desc))))
5051            (filename
5052             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5053                 none))
5054            (type (mm-handle-media-type data)))
5055       (unless data
5056         (error "No MIME part under point"))
5057       (with-current-buffer (mm-handle-buffer data)
5058         (let ((bsize (buffer-size)))
5059           (erase-buffer)
5060           (insert
5061            (concat
5062             ",----\n"
5063             "| The following attachment has been deleted:\n"
5064             "|\n"
5065             "| Type:           " type "\n"
5066             "| Filename:       " filename "\n"
5067             "| Size (encoded): " (format "%s byte%s\n"
5068                                          bsize (if (= bsize 1)
5069                                                    ""
5070                                                  "s"))
5071             (when description
5072               (concat    "| Description:    " description "\n"))
5073             "`----\n"))
5074           (setcdr data
5075                   (cdr (mm-make-handle
5076                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5077                         (list "attachment")
5078                         (format "Deleted attachment (%s bytes)" bsize))))))
5079       ;; (set-buffer gnus-summary-buffer)
5080       (gnus-article-edit-part handles id))))
5081
5082 (defun gnus-mime-save-part ()
5083   "Save the MIME part under point."
5084   (interactive)
5085   (gnus-article-check-buffer)
5086   (let ((data (get-text-property (point) 'gnus-data)))
5087     (when data
5088       (mm-save-part data))))
5089
5090 (defun gnus-mime-pipe-part (&optional cmd)
5091   "Pipe the MIME part under point to a process.
5092 Use CMD as the process."
5093   (interactive)
5094   (gnus-article-check-buffer)
5095   (let ((data (get-text-property (point) 'gnus-data)))
5096     (when data
5097       (mm-pipe-part data cmd))))
5098
5099 (defun gnus-mime-view-part ()
5100   "Interactively choose a viewing method for the MIME part under point."
5101   (interactive)
5102   (gnus-article-check-buffer)
5103   (let ((data (get-text-property (point) 'gnus-data)))
5104     (when data
5105       (setq gnus-article-mime-handles
5106             (mm-merge-handles
5107              gnus-article-mime-handles (setq data (copy-sequence data))))
5108       (mm-interactively-view-part data))))
5109
5110 (defun gnus-mime-view-part-as-type-internal ()
5111   (gnus-article-check-buffer)
5112   (let* ((handle (get-text-property (point) 'gnus-data))
5113          (name (or
5114                 ;; Content-Type: foo/bar; name=...
5115                 (mail-content-type-get (mm-handle-type handle) 'name)
5116                 ;; Content-Disposition: attachment; filename=...
5117                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5118          (def-type (and name (mm-default-file-encoding name))))
5119     (or (and def-type (cons def-type 0))
5120         (and handle
5121              (equal (mm-handle-media-supertype handle) "text")
5122              '("text/plain" . 0))
5123         '("application/octet-stream" . 0))))
5124
5125 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5126   "Choose a MIME media type, and view the part as such.
5127 If non-nil, PRED is a predicate to use during completion to limit the
5128 available media-types."
5129   (interactive)
5130   (unless mime-type
5131     (setq mime-type
5132           (let ((default (gnus-mime-view-part-as-type-internal)))
5133             (gnus-completing-read
5134              "View as MIME type"
5135              (if pred
5136                  (gnus-remove-if-not pred (mailcap-mime-types))
5137                (mailcap-mime-types))
5138              nil nil nil
5139              (car default)))))
5140   (gnus-article-check-buffer)
5141   (let ((handle (get-text-property (point) 'gnus-data)))
5142     (when handle
5143       (when (equal (mm-handle-media-type handle) "message/external-body")
5144         (unless (mm-handle-cache handle)
5145           (mm-extern-cache-contents handle))
5146         (setq handle (mm-handle-cache handle)))
5147       (setq handle
5148             (mm-make-handle (mm-handle-buffer handle)
5149                             (cons mime-type (cdr (mm-handle-type handle)))
5150                             (mm-handle-encoding handle)
5151                             (mm-handle-undisplayer handle)
5152                             (mm-handle-disposition handle)
5153                             (mm-handle-description handle)
5154                             nil
5155                             (mm-handle-id handle)))
5156       (setq gnus-article-mime-handles
5157             (mm-merge-handles gnus-article-mime-handles handle))
5158       (when (mm-handle-displayed-p handle)
5159         (mm-remove-part handle))
5160       (gnus-mm-display-part handle))))
5161
5162 (defun gnus-mime-copy-part (&optional handle arg)
5163   "Put the MIME part under point into a new buffer.
5164 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5165 are decompressed."
5166   (interactive (list nil current-prefix-arg))
5167   (gnus-article-check-buffer)
5168   (unless handle
5169     (setq handle (get-text-property (point) 'gnus-data)))
5170   (when handle
5171     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5172                                                'name)
5173                         (mail-content-type-get (mm-handle-disposition handle)
5174                                                'filename)))
5175           contents dont-decode charset coding-system)
5176       (mm-with-unibyte-buffer
5177         (mm-insert-part handle)
5178         (setq contents (or (condition-case nil
5179                                (mm-decompress-buffer filename nil 'sig)
5180                              (error
5181                               (setq dont-decode t)
5182                               nil))
5183                            (buffer-string))))
5184       (setq filename (cond (filename (file-name-nondirectory filename))
5185                            (dont-decode "*raw data*")
5186                            (t "*decoded*")))
5187       (cond
5188        (dont-decode)
5189        ((not arg)
5190         (unless (setq charset (mail-content-type-get
5191                                (mm-handle-type handle) 'charset))
5192           (unless (setq coding-system (mm-with-unibyte-buffer
5193                                         (insert contents)
5194                                         (mm-find-buffer-file-coding-system)))
5195             (setq charset gnus-newsgroup-charset))))
5196        ((numberp arg)
5197         (setq charset (or (cdr (assq arg
5198                                      gnus-summary-show-article-charset-alist))
5199                           (mm-read-coding-system "Charset: ")))))
5200       (switch-to-buffer (generate-new-buffer filename))
5201       (if (or coding-system
5202               (and charset
5203                    (setq coding-system (mm-charset-to-coding-system charset))
5204                    (not (eq coding-system 'ascii))))
5205           (progn
5206             (mm-enable-multibyte)
5207             (insert (mm-decode-coding-string contents coding-system))
5208             (setq buffer-file-coding-system
5209                   (if (boundp 'last-coding-system-used)
5210                       (symbol-value 'last-coding-system-used)
5211                     coding-system)))
5212         (mm-disable-multibyte)
5213         (insert contents)
5214         (setq buffer-file-coding-system mm-binary-coding-system))
5215       ;; We do it this way to make `normal-mode' set the appropriate mode.
5216       (unwind-protect
5217           (progn
5218             (setq buffer-file-name (expand-file-name filename))
5219             (normal-mode))
5220         (setq buffer-file-name nil))
5221       (goto-char (point-min)))))
5222
5223 (defun gnus-mime-print-part (&optional handle filename)
5224   "Print the MIME part under point."
5225   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5226   (gnus-article-check-buffer)
5227   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5228          (contents (and handle (mm-get-part handle)))
5229          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5230          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5231     (when contents
5232         (if printer
5233             (unwind-protect
5234                 (progn
5235                   (mm-save-part-to-file handle file)
5236                   (call-process shell-file-name nil
5237                                 (generate-new-buffer " *mm*")
5238                                 nil
5239                                 shell-command-switch
5240                                 (mm-mailcap-command
5241                                  printer file (mm-handle-type handle))))
5242               (delete-file file))
5243           (with-temp-buffer
5244             (insert contents)
5245             (gnus-print-buffer))
5246           (ps-despool filename)))))
5247
5248 (defun gnus-mime-inline-part (&optional handle arg)
5249   "Insert the MIME part under point into the current buffer.
5250 Compressed files like .gz and .bz2 are decompressed."
5251   (interactive (list nil current-prefix-arg))
5252   (gnus-article-check-buffer)
5253   (unless handle
5254     (setq handle (get-text-property (point) 'gnus-data)))
5255   (when handle
5256     (let ((b (point))
5257           (inhibit-read-only t)
5258           contents charset coding-system)
5259       (if (and (not arg) (mm-handle-undisplayer handle))
5260           (mm-remove-part handle)
5261         (mm-with-unibyte-buffer
5262           (mm-insert-part handle)
5263           (setq contents
5264                 (or (mm-decompress-buffer
5265                      (or (mail-content-type-get (mm-handle-type handle)
5266                                                 'name)
5267                          (mail-content-type-get (mm-handle-disposition handle)
5268                                                 'filename))
5269                      nil t)
5270                     (buffer-string))))
5271         (cond
5272          ((not arg)
5273           (unless (setq charset (mail-content-type-get
5274                                  (mm-handle-type handle) 'charset))
5275             (unless (setq coding-system
5276                           (mm-with-unibyte-buffer
5277                             (insert contents)
5278                             (mm-find-buffer-file-coding-system)))
5279               (setq charset gnus-newsgroup-charset))))
5280          ((numberp arg)
5281           (if (mm-handle-undisplayer handle)
5282               (mm-remove-part handle))
5283           (setq charset
5284                 (or (cdr (assq arg
5285                                gnus-summary-show-article-charset-alist))
5286                     (mm-read-coding-system "Charset: "))))
5287          (t
5288           (if (mm-handle-undisplayer handle)
5289               (mm-remove-part handle))))
5290         (forward-line 2)
5291         (mm-display-inline handle)
5292         (goto-char b)))))
5293
5294 (defun gnus-mime-set-charset-parameters (handle charset)
5295   "Set CHARSET to parameters in HANDLE.
5296 CHARSET may either be a string or a symbol."
5297   (unless (stringp charset)
5298     (setq charset (symbol-name charset)))
5299   (if (stringp (car handle))
5300       (dolist (h (cdr handle))
5301         (gnus-mime-set-charset-parameters h charset))
5302     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5303                                             "message/external-body")
5304                                      (progn
5305                                        (unless (mm-handle-cache handle)
5306                                          (mm-extern-cache-contents handle))
5307                                        (mm-handle-cache handle))
5308                                    handle)))
5309            (param (assq 'charset (cdr type))))
5310       (if param
5311           (setcdr param charset)
5312         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5313
5314 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5315   "Insert the MIME part under point into the current buffer using the
5316 specified charset."
5317   (interactive (list nil current-prefix-arg))
5318   (gnus-article-check-buffer)
5319   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5320         (fun (get-text-property (point) 'gnus-callback))
5321         (gnus-newsgroup-ignored-charsets 'gnus-all)
5322         charset form preferred parts)
5323     (when handle
5324       (when (prog1
5325                 (and fun
5326                      (setq charset
5327                            (or (cdr (assq
5328                                      arg
5329                                      gnus-summary-show-article-charset-alist))
5330                                (mm-read-coding-system "Charset: "))))
5331               (if (mm-handle-undisplayer handle)
5332                   (mm-remove-part handle)))
5333         (gnus-mime-set-charset-parameters handle charset)
5334         (when (and (consp (setq form (cdr-safe fun)))
5335                    (setq form (ignore-errors
5336                                 (assq 'gnus-mime-display-alternative form)))
5337                    (setq preferred (caddr form))
5338                    (progn
5339                      (when (eq (car preferred) 'quote)
5340                        (setq preferred (cadr preferred)))
5341                      (not (equal preferred
5342                                  (get-text-property (point) 'gnus-data))))
5343                    (setq parts (get-text-property (point) 'gnus-part))
5344                    (setq parts (cdr (assq parts
5345                                           gnus-article-mime-handle-alist)))
5346                    (equal (mm-handle-media-type parts) "multipart/alternative")
5347                    (setq parts (reverse (cdr parts))))
5348           (setcar (cddr form)
5349                   (list 'quote (or (cadr (member preferred parts))
5350                                    (car parts)))))
5351         (funcall fun handle)))))
5352
5353 (defun gnus-mime-view-part-externally (&optional handle)
5354   "View the MIME part under point with an external viewer."
5355   (interactive)
5356   (gnus-article-check-buffer)
5357   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5358          (mm-user-display-methods nil)
5359          (mm-inlined-types nil)
5360          (mail-parse-charset gnus-newsgroup-charset)
5361          (mail-parse-ignored-charsets
5362           (with-current-buffer gnus-summary-buffer
5363             gnus-newsgroup-ignored-charsets))
5364          (type (mm-handle-media-type handle))
5365          (method (mailcap-mime-info type))
5366          (mm-enable-external t))
5367     (if (not (stringp method))
5368         (gnus-mime-view-part-as-type
5369          nil (lambda (type) (stringp (mailcap-mime-info type))))
5370       (when handle
5371         (mm-display-part handle nil t)))))
5372
5373 (defun gnus-mime-view-part-internally (&optional handle)
5374   "View the MIME part under point with an internal viewer.
5375 If no internal viewer is available, use an external viewer."
5376   (interactive)
5377   (gnus-article-check-buffer)
5378   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5379          (mm-inlined-types '(".*"))
5380          (mm-inline-large-images t)
5381          (mail-parse-charset gnus-newsgroup-charset)
5382          (mail-parse-ignored-charsets
5383           (with-current-buffer gnus-summary-buffer
5384             gnus-newsgroup-ignored-charsets))
5385          (inhibit-read-only t))
5386     (if (not (mm-inlinable-p handle))
5387         (gnus-mime-view-part-as-type
5388          nil (lambda (type) (mm-inlinable-p handle type)))
5389       (when handle
5390         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5391
5392 (defun gnus-mime-action-on-part (&optional action)
5393   "Do something with the MIME attachment at \(point\)."
5394   (interactive
5395    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5396   (gnus-article-check-buffer)
5397   (let ((action-pair (assoc action gnus-mime-action-alist)))
5398     (if action-pair
5399         (funcall (cdr action-pair)))))
5400
5401 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5402   "Call FUNCTION on MIME part N.
5403 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5404 If INTERACTIVE, call FUNCTION interactivly."
5405   (let (window frame)
5406     ;; Check whether the article is displayed.
5407     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5408                  (setq window (get-buffer-window gnus-article-buffer t))
5409                  (frame-visible-p (setq frame (window-frame window))))
5410       (error "No article is displayed"))
5411     (with-current-buffer gnus-article-buffer
5412       ;; Check whether the article displays the right contents.
5413       (unless (with-current-buffer gnus-summary-buffer
5414                 (eq gnus-current-article (gnus-summary-article-number)))
5415         (error "You should select the right article first"))
5416       (if n
5417           (setq n (prefix-numeric-value n))
5418         (let ((pt (point)))
5419           (setq n (or (get-text-property pt 'gnus-part)
5420                       (and (not (bobp))
5421                            (get-text-property (1- pt) 'gnus-part))
5422                       (get-text-property (prog2
5423                                              (forward-line 1)
5424                                              (point)
5425                                            (goto-char pt))
5426                                          'gnus-part)
5427                       (get-text-property
5428                        (or (and (setq pt (previous-single-property-change
5429                                           pt 'gnus-part))
5430                                 (1- pt))
5431                            (next-single-property-change (point) 'gnus-part)
5432                            (point))
5433                        'gnus-part)
5434                       1))))
5435       ;; Check whether the specified part exists.
5436       (when (> n (length gnus-article-mime-handle-alist))
5437         (error "No such part")))
5438     (unless
5439         (progn
5440           ;; To select the window is needed so that the cursor
5441           ;; might be visible on the MIME button.
5442           (select-window (prog1
5443                              window
5444                            (setq window (selected-window))
5445                            ;; Article may be displayed in the other frame.
5446                            (gnus-select-frame-set-input-focus
5447                             (prog1
5448                                 frame
5449                               (setq frame (selected-frame))))))
5450           (when (gnus-article-goto-part n)
5451             ;; We point the cursor and the arrow at the MIME button
5452             ;; when the `function' prompt the user for something.
5453             (unless (and (pos-visible-in-window-p)
5454                          (> (count-lines (point) (window-end))
5455                             (/ (1- (window-height)) 3)))
5456               (recenter (/ (1- (window-height)) 3)))
5457             (let ((cursor-in-non-selected-windows t)
5458                   (overlay-arrow-string "=>")
5459                   (overlay-arrow-position (point-marker)))
5460               (unwind-protect
5461                   (cond
5462                    ((and no-handle interactive)
5463                     (call-interactively function))
5464                    (no-handle
5465                     (funcall function))
5466                    (interactive
5467                     (call-interactively
5468                      function (get-text-property (point) 'gnus-data)))
5469                    (t
5470                     (funcall function
5471                              (get-text-property (point) 'gnus-data))))
5472                 (set-marker overlay-arrow-position nil)
5473                 (unless gnus-auto-select-part
5474                   (gnus-select-frame-set-input-focus frame)
5475                   (select-window window))))
5476             t))
5477       (if gnus-inhibit-mime-unbuttonizing
5478           ;; This is the default though the program shouldn't reach here.
5479           (error "No such part")
5480         ;; The part which doesn't have the MIME button is selected.
5481         ;; So, we display all the buttons and redo it.
5482         (let ((gnus-inhibit-mime-unbuttonizing t))
5483           (gnus-summary-show-article)
5484           (gnus-article-part-wrapper n function no-handle))))))
5485
5486 (defun gnus-article-pipe-part (n)
5487   "Pipe MIME part N, which is the numerical prefix."
5488   (interactive "P")
5489   (gnus-article-part-wrapper n 'mm-pipe-part))
5490
5491 (defun gnus-article-save-part (n)
5492   "Save MIME part N, which is the numerical prefix."
5493   (interactive "P")
5494   (gnus-article-part-wrapper n 'mm-save-part))
5495
5496 (defun gnus-article-interactively-view-part (n)
5497   "View MIME part N interactively, which is the numerical prefix."
5498   (interactive "P")
5499   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5500
5501 (defun gnus-article-copy-part (n)
5502   "Copy MIME part N, which is the numerical prefix."
5503   (interactive "P")
5504   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5505
5506 (defun gnus-article-view-part-as-charset (n)
5507   "View MIME part N using a specified charset.
5508 N is the numerical prefix."
5509   (interactive "P")
5510   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5511
5512 (defun gnus-article-view-part-externally (n)
5513   "View MIME part N externally, which is the numerical prefix."
5514   (interactive "P")
5515   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5516
5517 (defun gnus-article-inline-part (n)
5518   "Inline MIME part N, which is the numerical prefix."
5519   (interactive "P")
5520   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5521
5522 (defun gnus-article-save-part-and-strip (n)
5523   "Save MIME part N and replace it with an external body.
5524 N is the numerical prefix."
5525   (interactive "P")
5526   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5527
5528 (defun gnus-article-replace-part (n)
5529   "Replace MIME part N with an external body.
5530 N is the numerical prefix."
5531   (interactive "P")
5532   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5533
5534 (defun gnus-article-delete-part (n)
5535   "Delete MIME part N and add some information about the removed part.
5536 N is the numerical prefix."
5537   (interactive "P")
5538   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5539
5540 (defun gnus-article-view-part-as-type (n)
5541   "Choose a MIME media type, and view part N as such.
5542 N is the numerical prefix."
5543   (interactive "P")
5544   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5545
5546 (defun gnus-article-mime-match-handle-first (condition)
5547   (if condition
5548       (let (n)
5549         (dolist (ihandle gnus-article-mime-handle-alist)
5550           (if (and (cond
5551                     ((functionp condition)
5552                      (funcall condition (cdr ihandle)))
5553                     ((eq condition 'undisplayed)
5554                      (not (or (mm-handle-undisplayer (cdr ihandle))
5555                               (equal (mm-handle-media-type (cdr ihandle))
5556                                      "multipart/alternative"))))
5557                     ((eq condition 'undisplayed-alternative)
5558                      (not (mm-handle-undisplayer (cdr ihandle))))
5559                     (t t))
5560                    (gnus-article-goto-part (car ihandle))
5561                    (or (not n) (< (car ihandle) n)))
5562               (setq n (car ihandle))))
5563         (or n 1))
5564     1))
5565
5566 (defun gnus-article-view-part (&optional n)
5567   "View MIME part N, which is the numerical prefix.
5568 If the part is already shown, hide the part.  If N is nil, view
5569 all parts."
5570   (interactive "P")
5571   (with-current-buffer gnus-article-buffer
5572     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5573                              gnus-article-mime-match-handle-function)))
5574     (when (> n (length gnus-article-mime-handle-alist))
5575       (error "No such part"))
5576     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5577       (when (gnus-article-goto-part n)
5578         (if (equal (car handle) "multipart/alternative")
5579             (gnus-article-press-button)
5580           (when (eq (gnus-mm-display-part handle) 'internal)
5581             (gnus-set-window-start)))))))
5582
5583 (defsubst gnus-article-mime-total-parts ()
5584   (if (bufferp (car gnus-article-mime-handles))
5585       1 ;; single part
5586     (1- (length gnus-article-mime-handles))))
5587
5588 (defun gnus-mm-display-part (handle)
5589   "Display HANDLE and fix MIME button."
5590   (let ((id (get-text-property (point) 'gnus-part))
5591         (point (point))
5592         (inhibit-read-only t))
5593     (forward-line 1)
5594     (prog1
5595         (let ((window (selected-window))
5596               (mail-parse-charset gnus-newsgroup-charset)
5597               (mail-parse-ignored-charsets
5598                (if (gnus-buffer-live-p gnus-summary-buffer)
5599                    (with-current-buffer gnus-summary-buffer
5600                      gnus-newsgroup-ignored-charsets)
5601                  nil)))
5602           (save-excursion
5603             (unwind-protect
5604                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5605                       (beg (point)))
5606                   (when win
5607                     (select-window win))
5608                   (goto-char point)
5609                   (forward-line)
5610                   (if (mm-handle-displayed-p handle)
5611                       ;; This will remove the part.
5612                       (mm-display-part handle)
5613                     (save-restriction
5614                       (narrow-to-region (point)
5615                                         (if (eobp) (point) (1+ (point))))
5616                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5617                       ;; We narrow to the part itself and
5618                       ;; then call the treatment functions.
5619                       (goto-char (point-min))
5620                       (forward-line 1)
5621                       (narrow-to-region (point) (point-max))
5622                       (gnus-treat-article
5623                        nil id
5624                        (gnus-article-mime-total-parts)
5625                        (mm-handle-media-type handle)))))
5626               (if (window-live-p window)
5627                   (select-window window)))))
5628       (goto-char point)
5629       (gnus-delete-line)
5630       (gnus-insert-mime-button
5631        handle id (list (mm-handle-displayed-p handle)))
5632       (goto-char point))))
5633
5634 (defun gnus-article-goto-part (n)
5635   "Go to MIME part N."
5636   (when gnus-break-pages
5637     (widen))
5638   (prog1
5639       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5640             part handle end next handles)
5641         (when start
5642           (goto-char start)
5643           (if (setq handle (get-text-property start 'gnus-data))
5644               start
5645             ;; Go to the displayed subpart, assuming this is
5646             ;; multipart/alternative.
5647             (setq part start
5648                   end (point-at-eol))
5649             (while (and (not handle)
5650                         part
5651                         (< part end)
5652                         (setq next (text-property-not-all part end
5653                                                           'gnus-data nil)))
5654               (setq part next
5655                     handle (get-text-property part 'gnus-data))
5656               (push (cons handle part) handles)
5657               (unless (mm-handle-displayed-p handle)
5658                 (setq handle nil
5659                       part (text-property-any part end 'gnus-data nil))))
5660             (unless handle
5661               ;; No subpart is displayed, so we find preferred one.
5662               (setq part
5663                     (cdr (assq (mm-preferred-alternative
5664                                 (nreverse (mapcar 'car handles)))
5665                                handles))))
5666             (if part
5667                 (goto-char (1+ part))
5668               start))))
5669     (when gnus-break-pages
5670       (gnus-narrow-to-page))))
5671
5672 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5673   (let ((gnus-tmp-name
5674          (or (mail-content-type-get (mm-handle-type handle) 'name)
5675              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5676              (mail-content-type-get (mm-handle-type handle) 'url)
5677              ""))
5678         (gnus-tmp-type (mm-handle-media-type handle))
5679         (gnus-tmp-description (or (mm-handle-description handle) ""))
5680         (gnus-tmp-dots
5681          (if (if displayed (car displayed)
5682                (mm-handle-displayed-p handle))
5683              "" "..."))
5684         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5685                            (buffer-size)))
5686         gnus-tmp-type-long b e)
5687     (when (string-match ".*/" gnus-tmp-name)
5688       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5689     (setq gnus-tmp-type-long (concat gnus-tmp-type
5690                                      (and (not (equal gnus-tmp-name ""))
5691                                           (concat "; " gnus-tmp-name))))
5692     (unless (equal gnus-tmp-description "")
5693       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5694     (unless (bolp)
5695       (insert "\n"))
5696     (setq b (point))
5697     (gnus-eval-format
5698      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5699      `(keymap ,gnus-mime-button-map
5700               gnus-callback gnus-mm-display-part
5701               gnus-part ,gnus-tmp-id
5702               article-type annotation
5703               gnus-data ,handle))
5704     (setq e (if (bolp)
5705                 ;; Exclude a newline.
5706                 (1- (point))
5707               (point)))
5708     (when gnus-article-button-face
5709       (gnus-overlay-put (gnus-make-overlay b e nil t)
5710                         'face gnus-article-button-face))
5711     (widget-convert-button
5712      'link b e
5713      :mime-handle handle
5714      :action 'gnus-widget-press-button
5715      :button-keymap gnus-mime-button-map
5716      :help-echo
5717      (lambda (widget)
5718        ;; Needed to properly clear the message due to a bug in
5719        ;; wid-edit (XEmacs only).
5720        (if (boundp 'help-echo-owns-message)
5721            (setq help-echo-owns-message t))
5722        (format
5723         "%S: %s the MIME part; %S: more options"
5724         (aref gnus-mouse-2 0)
5725         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5726             "hide" "show")
5727         (aref gnus-down-mouse-3 0))))))
5728
5729 (defun gnus-widget-press-button (elems el)
5730   (goto-char (widget-get elems :from))
5731   (gnus-article-press-button))
5732
5733 (defvar gnus-displaying-mime nil)
5734
5735 (defun gnus-display-mime (&optional ihandles)
5736   "Display the MIME parts."
5737   (save-excursion
5738     (save-selected-window
5739       (let ((window (get-buffer-window gnus-article-buffer))
5740             (point (point)))
5741         (when window
5742           (select-window window)
5743           ;; We have to do this since selecting the window
5744           ;; may change the point.  So we set the window point.
5745           (set-window-point window point)))
5746       (let ((handles ihandles)
5747             (inhibit-read-only t)
5748             handle)
5749         (cond (handles)
5750               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5751                (when gnus-article-emulate-mime
5752                  (mm-uu-dissect-text-parts handles)))
5753               (gnus-article-emulate-mime
5754                (setq handles (mm-uu-dissect))))
5755         (when (and (not ihandles)
5756                    (not gnus-displaying-mime))
5757           ;; Top-level call; we clean up.
5758           (when gnus-article-mime-handles
5759             (mm-destroy-parts gnus-article-mime-handles)
5760             (setq gnus-article-mime-handle-alist nil));; A trick.
5761           (setq gnus-article-mime-handles handles)
5762           ;; We allow users to glean info from the handles.
5763           (when gnus-article-mime-part-function
5764             (gnus-mime-part-function handles)))
5765         (if (and handles
5766                  (or (not (stringp (car handles)))
5767                      (cdr handles)))
5768             (progn
5769               (when (and (not ihandles)
5770                          (not gnus-displaying-mime))
5771                 ;; Clean up for mime parts.
5772                 (article-goto-body)
5773                 (delete-region (point) (point-max)))
5774               (let ((gnus-displaying-mime t))
5775                 (gnus-mime-display-part handles)))
5776           (save-restriction
5777             (article-goto-body)
5778             (narrow-to-region (point) (point-max))
5779             (gnus-treat-article nil 1 1 "text/plain")
5780             (widen)))
5781         (unless ihandles
5782           ;; Highlight the headers.
5783           (save-excursion
5784             (save-restriction
5785               (article-goto-body)
5786               (narrow-to-region (point-min) (point))
5787               (gnus-article-save-original-date
5788                (gnus-treat-article 'head)))))))
5789     ;; Cope with broken MIME messages.
5790     (goto-char (point-max))
5791     (unless (bolp)
5792       (insert "\n"))))
5793
5794 (defcustom gnus-mime-display-multipart-as-mixed nil
5795   "Display \"multipart\" parts as  \"multipart/mixed\".
5796
5797 If t, it overrides nil values of
5798 `gnus-mime-display-multipart-alternative-as-mixed' and
5799 `gnus-mime-display-multipart-related-as-mixed'."
5800   :group 'gnus-article-mime
5801   :type 'boolean)
5802
5803 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5804   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5805   :version "22.1"
5806   :group 'gnus-article-mime
5807   :type 'boolean)
5808
5809 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5810   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5811
5812 If displaying \"text/html\" is discouraged \(see
5813 `mm-discouraged-alternatives'\) images or other material inside a
5814 \"multipart/related\" part might be overlooked when this variable is nil."
5815   :version "22.1"
5816   :group 'gnus-article-mime
5817   :type 'boolean)
5818
5819 (defun gnus-mime-display-part (handle)
5820   (cond
5821    ;; Maybe a broken MIME message.
5822    ((null handle))
5823    ;; Single part.
5824    ((not (stringp (car handle)))
5825     (gnus-mime-display-single handle))
5826    ;; User-defined multipart
5827    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5828     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5829              handle))
5830    ;; multipart/alternative
5831    ((and (equal (car handle) "multipart/alternative")
5832          (not (or gnus-mime-display-multipart-as-mixed
5833                   gnus-mime-display-multipart-alternative-as-mixed)))
5834     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5835       (push (cons id handle) gnus-article-mime-handle-alist)
5836       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5837    ;; multipart/related
5838    ((and (equal (car handle) "multipart/related")
5839          (not (or gnus-mime-display-multipart-as-mixed
5840                   gnus-mime-display-multipart-related-as-mixed)))
5841     ;;;!!!We should find the start part, but we just default
5842     ;;;!!!to the first part.
5843     ;;(gnus-mime-display-part (cadr handle))
5844     ;;;!!! Most multipart/related is an HTML message plus images.
5845     ;;;!!! Unfortunately we are unable to let W3 display those
5846     ;;;!!! included images, so we just display it as a mixed multipart.
5847     ;;(gnus-mime-display-mixed (cdr handle))
5848     ;;;!!! No, w3 can display everything just fine.
5849     (gnus-mime-display-part (cadr handle)))
5850    ((equal (car handle) "multipart/signed")
5851     (gnus-add-wash-type 'signed)
5852     (gnus-mime-display-security handle))
5853    ((equal (car handle) "multipart/encrypted")
5854     (gnus-add-wash-type 'encrypted)
5855     (gnus-mime-display-security handle))
5856    ;; Other multiparts are handled like multipart/mixed.
5857    (t
5858     (gnus-mime-display-mixed (cdr handle)))))
5859
5860 (defun gnus-mime-part-function (handles)
5861   (if (stringp (car handles))
5862       (mapcar 'gnus-mime-part-function (cdr handles))
5863     (funcall gnus-article-mime-part-function handles)))
5864
5865 (defun gnus-mime-display-mixed (handles)
5866   (mapcar 'gnus-mime-display-part handles))
5867
5868 (defun gnus-mime-display-single (handle)
5869   (let ((type (mm-handle-media-type handle))
5870         (ignored gnus-ignored-mime-types)
5871         (not-attachment t)
5872         (move nil)
5873         display text)
5874     (catch 'ignored
5875       (progn
5876         (while ignored
5877           (when (string-match (pop ignored) type)
5878             (throw 'ignored nil)))
5879         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5880                                (with-current-buffer gnus-summary-buffer
5881                                  gnus-inhibit-images)
5882                              gnus-inhibit-images)
5883                            (string-match "\\`image/" type)))
5884                  (setq not-attachment
5885                        (and (not (mm-inline-override-p handle))
5886                             (or (not (mm-handle-disposition handle))
5887                                 (equal (car (mm-handle-disposition handle))
5888                                        "inline")
5889                                 (mm-attachment-override-p handle))))
5890                  (mm-automatic-display-p handle)
5891                  (or (and
5892                       (mm-inlinable-p handle)
5893                       (mm-inlined-p handle))
5894                      (mm-automatic-external-display-p type)))
5895             (setq display t)
5896           (when (equal (mm-handle-media-supertype handle) "text")
5897             (setq text t)))
5898         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5899               beg)
5900           (push (cons id handle) gnus-article-mime-handle-alist)
5901           (when (and display
5902                      (equal (mm-handle-media-supertype handle) "message"))
5903             (insert-char
5904              ?\n
5905              (cond ((not (bolp)) 2)
5906                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5907                    (t 1))))
5908           (when (or (not display)
5909                     (not (gnus-unbuttonized-mime-type-p type))
5910                     (eq id gnus-mime-buttonized-part-id))
5911             (gnus-insert-mime-button
5912              handle id (list (or display (and not-attachment text))))
5913             (gnus-article-insert-newline)
5914             ;; Remember modify the number of forward lines.
5915             (setq move t))
5916           (setq beg (point))
5917           (cond
5918            (display
5919             (when move
5920               (forward-line -1)
5921               (setq beg (point)))
5922             (let ((mail-parse-charset gnus-newsgroup-charset)
5923                   (mail-parse-ignored-charsets
5924                    (save-excursion (condition-case ()
5925                                        (set-buffer gnus-summary-buffer)
5926                                      (error))
5927                                    gnus-newsgroup-ignored-charsets)))
5928               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5929             (goto-char (point-max)))
5930            ((and text not-attachment)
5931             (when move
5932               (forward-line -1)
5933               (setq beg (point)))
5934             (gnus-article-insert-newline)
5935             (mm-display-inline handle)
5936             (goto-char (point-max))))
5937           ;; Do highlighting.
5938           (save-excursion
5939             (save-restriction
5940               (narrow-to-region beg (point))
5941               (if (eq handle gnus-article-mime-handles)
5942                   ;; The format=flowed case.
5943                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5944                 ;; Don't count signature parts that are never displayed.
5945                 ;; The part number should be re-calculated supposing this
5946                 ;; might be a message/rfc822 part.
5947                 (let (handles)
5948                   (dolist (part gnus-article-mime-handles)
5949                     (unless (or (stringp part)
5950                                 (equal (car (mm-handle-type part))
5951                                        "application/pgp-signature"))
5952                       (push part handles)))
5953                   (gnus-treat-article
5954                    nil (length (memq handle handles)) (length handles)
5955                    (mm-handle-media-type handle)))))))))))
5956
5957 (defun gnus-unbuttonized-mime-type-p (type)
5958   "Say whether TYPE is to be unbuttonized."
5959   (unless gnus-inhibit-mime-unbuttonizing
5960     (when (catch 'found
5961             (let ((types gnus-unbuttonized-mime-types))
5962               (while types
5963                 (when (string-match (pop types) type)
5964                   (throw 'found t)))))
5965       (not (catch 'found
5966              (let ((types gnus-buttonized-mime-types))
5967                (while types
5968                  (when (string-match (pop types) type)
5969                    (throw 'found t)))))))))
5970
5971 (defun gnus-article-insert-newline ()
5972   "Insert a newline, but mark it as undeletable."
5973   (gnus-put-text-property
5974    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5975
5976 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5977   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5978          (ihandles handles)
5979          (point (point))
5980          handle (inhibit-read-only t) from props begend not-pref)
5981     (save-window-excursion
5982       (save-restriction
5983         (when ibegend
5984           (narrow-to-region (car ibegend)
5985                             (or (cdr ibegend)
5986                                 (progn
5987                                   (goto-char (car ibegend))
5988                                   (forward-line 2)
5989                                   (point))))
5990           (delete-region (point-min) (point-max))
5991           (mm-remove-parts handles))
5992         (setq begend (list (point-marker)))
5993         ;; Do the toggle.
5994         (unless (setq not-pref (cadr (member preferred ihandles)))
5995           (setq not-pref (car ihandles)))
5996         (when (or ibegend
5997                   (not preferred)
5998                   (not (gnus-unbuttonized-mime-type-p
5999                         "multipart/alternative")))
6000           (gnus-add-text-properties
6001            (setq from (point))
6002            (progn
6003              (insert (format "%d.  " id))
6004              (point))
6005            `(gnus-callback
6006              (lambda (handles)
6007                (unless ,(not ibegend)
6008                  (setq gnus-article-mime-handle-alist
6009                        ',gnus-article-mime-handle-alist))
6010                (gnus-mime-display-alternative
6011                 ',ihandles ',not-pref ',begend ,id))
6012              keymap ,gnus-mime-button-map
6013              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6014              face ,gnus-article-button-face
6015              gnus-part ,id
6016              article-type multipart))
6017           (widget-convert-button 'link from (point)
6018                                  :action 'gnus-widget-press-button
6019                                  :button-keymap gnus-widget-button-keymap)
6020           ;; Do the handles
6021           (while (setq handle (pop handles))
6022             (gnus-add-text-properties
6023              (setq from (point))
6024              (progn
6025                (insert (format "(%c) %-18s"
6026                                (if (equal handle preferred) ?* ? )
6027                                (mm-handle-media-type handle)))
6028                (point))
6029              `(gnus-callback
6030                (lambda (handles)
6031                  (unless ,(not ibegend)
6032                    (setq gnus-article-mime-handle-alist
6033                          ',gnus-article-mime-handle-alist))
6034                  (gnus-mime-display-alternative
6035                   ',ihandles ',handle ',begend ,id))
6036                keymap ,gnus-mime-button-map
6037                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6038                face ,gnus-article-button-face
6039                gnus-part ,id
6040                gnus-data ,handle))
6041             (widget-convert-button 'link from (point)
6042                                    :action 'gnus-widget-press-button
6043                                    :button-keymap gnus-widget-button-keymap)
6044             (insert "  "))
6045           (insert "\n\n"))
6046         (when preferred
6047           (if (stringp (car preferred))
6048               (gnus-display-mime preferred)
6049             (let ((mail-parse-charset gnus-newsgroup-charset)
6050                   (mail-parse-ignored-charsets
6051                    (with-current-buffer gnus-summary-buffer
6052                      gnus-newsgroup-ignored-charsets)))
6053               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6054               ;; Do highlighting.
6055               (save-excursion
6056                 (save-restriction
6057                   (narrow-to-region (car begend) (point-max))
6058                   (gnus-treat-article
6059                    nil (length gnus-article-mime-handle-alist)
6060                    (gnus-article-mime-total-parts)
6061                    (mm-handle-media-type preferred))))))
6062           (goto-char (point-max))
6063           (setcdr begend (point-marker)))))
6064     (when ibegend
6065       (goto-char point))))
6066
6067 (defconst gnus-article-wash-status-strings
6068   (let ((alist '((cite "c" "Possible hidden citation text"
6069                        " " "All citation text visible")
6070                  (headers "h" "Hidden headers"
6071                           " " "All headers visible.")
6072                  (pgp "p" "Encrypted or signed message status hidden"
6073                       " " "No hidden encryption nor digital signature status")
6074                  (signature "s" "Signature has been hidden"
6075                             " " "Signature is visible")
6076                  (overstrike "o" "Overstrike (^H) characters applied"
6077                              " " "No overstrike characters applied")
6078                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6079                            " " "No /*_emphasis_*/ characters applied")))
6080         result)
6081     (dolist (entry alist result)
6082       (let ((key (nth 0 entry))
6083             (on (copy-sequence (nth 1 entry)))
6084             (on-help (nth 2 entry))
6085             (off (copy-sequence (nth 3 entry)))
6086             (off-help (nth 4 entry)))
6087         (put-text-property 0 1 'help-echo on-help on)
6088         (put-text-property 0 1 'help-echo off-help off)
6089         (push (list key on off) result))))
6090   "Alist of strings describing wash status in the mode line.
6091 Each entry has the form (KEY ON OF), where the KEY is a symbol
6092 representing the particular washing function, ON is the string to use
6093 in the article mode line when the washing function is active, and OFF
6094 is the string to use when it is inactive.")
6095
6096 (defun gnus-article-wash-status-entry (key value)
6097   (let ((entry (assoc key gnus-article-wash-status-strings)))
6098     (if value (nth 1 entry) (nth 2 entry))))
6099
6100 (defun gnus-article-wash-status ()
6101   "Return a string which display status of article washing."
6102   (with-current-buffer gnus-article-buffer
6103     (let ((cite (memq 'cite gnus-article-wash-types))
6104           (headers (memq 'headers gnus-article-wash-types))
6105           (boring (memq 'boring-headers gnus-article-wash-types))
6106           (pgp (memq 'pgp gnus-article-wash-types))
6107           (pem (memq 'pem gnus-article-wash-types))
6108           (signed (memq 'signed gnus-article-wash-types))
6109           (encrypted (memq 'encrypted gnus-article-wash-types))
6110           (signature (memq 'signature gnus-article-wash-types))
6111           (overstrike (memq 'overstrike gnus-article-wash-types))
6112           (emphasis (memq 'emphasis gnus-article-wash-types)))
6113       (concat
6114        (gnus-article-wash-status-entry 'cite cite)
6115        (gnus-article-wash-status-entry 'headers (or headers boring))
6116        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6117        (gnus-article-wash-status-entry 'signature signature)
6118        (gnus-article-wash-status-entry 'overstrike overstrike)
6119        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6120
6121 (defun gnus-add-wash-type (type)
6122   "Add a washing of TYPE to the current status."
6123   (add-to-list 'gnus-article-wash-types type))
6124
6125 (defun gnus-delete-wash-type (type)
6126   "Add a washing of TYPE to the current status."
6127   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6128
6129 (defun gnus-add-image (category image)
6130   "Add IMAGE of CATEGORY to the list of displayed images."
6131   (let ((entry (assq category gnus-article-image-alist)))
6132     (unless entry
6133       (setq entry (list category))
6134       (push entry gnus-article-image-alist))
6135     (nconc entry (list image))))
6136
6137 (defun gnus-delete-images (category)
6138   "Delete all images in CATEGORY."
6139   (let ((entry (assq category gnus-article-image-alist)))
6140     (dolist (image (cdr entry))
6141       (gnus-remove-image image category))
6142     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6143     (gnus-delete-wash-type category)))
6144
6145 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6146
6147 (defun gnus-article-maybe-hide-headers ()
6148   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6149 Provided for backwards compatibility."
6150   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6151                  (not (with-current-buffer gnus-summary-buffer
6152                         gnus-have-all-headers)))
6153              (not gnus-inhibit-hiding))
6154     (gnus-article-hide-headers)))
6155
6156 ;;; Article savers.
6157
6158 (defun gnus-output-to-file (file-name)
6159   "Append the current article to a file named FILE-NAME.
6160 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6161 text and used as the value of the coding cookie which is added to the
6162 top of a file.  Otherwise, this function saves a raw article without
6163 the coding cookie."
6164   (let* ((artbuf (current-buffer))
6165          (file-name-coding-system nnmail-pathname-coding-system)
6166          (coding gnus-article-save-coding-system)
6167          (coding-system-for-read (if coding
6168                                      nil ;; Rely on the coding cookie.
6169                                    mm-text-coding-system))
6170          (coding-system-for-write (or coding
6171                                       mm-text-coding-system-for-write
6172                                       mm-text-coding-system))
6173          (exists (file-exists-p file-name)))
6174     (with-temp-buffer
6175       (when exists
6176         (insert-file-contents file-name)
6177         (goto-char (point-min))
6178         ;; Remove the existing coding cookie.
6179         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6180           (delete-region (match-beginning 0) (match-end 0))))
6181       (goto-char (point-max))
6182       (insert-buffer-substring artbuf)
6183       ;; Append newline at end of the buffer as separator, and then
6184       ;; save it to file.
6185       (goto-char (point-max))
6186       (insert "\n")
6187       (when coding
6188         ;; If the coding system is not suitable to encode the text,
6189         ;; ask a user for a proper one.
6190         (when (fboundp 'select-safe-coding-system)
6191           (setq coding (coding-system-base
6192                         (save-window-excursion
6193                           (select-safe-coding-system (point-min) (point-max)
6194                                                      coding))))
6195           (setq coding-system-for-write
6196                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6197                     coding)))
6198         (goto-char (point-min))
6199         ;; Add the coding cookie.
6200         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6201                         coding-system-for-write)))
6202       (if exists
6203           (progn
6204             (write-region (point-min) (point-max) file-name nil 'no-message)
6205             (message "Appended to %s" file-name))
6206         (write-region (point-min) (point-max) file-name))))
6207   t)
6208
6209 (defun gnus-narrow-to-page (&optional arg)
6210   "Narrow the article buffer to a page.
6211 If given a numerical ARG, move forward ARG pages."
6212   (interactive "P")
6213   (setq arg (if arg (prefix-numeric-value arg) 0))
6214   (with-current-buffer gnus-article-buffer
6215     (widen)
6216     ;; Remove any old next/prev buttons.
6217     (when (gnus-visual-p 'page-marker)
6218       (let ((inhibit-read-only t))
6219         (gnus-remove-text-with-property 'gnus-prev)
6220         (gnus-remove-text-with-property 'gnus-next)))
6221     (let (st nd pt)
6222       (when (save-excursion
6223               (cond ((< arg 0)
6224                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6225                          (prog1
6226                              (setq nd (match-beginning 0)
6227                                    pt nd)
6228                            (when (re-search-backward page-delimiter nil t)
6229                              (setq st (match-end 0))))
6230                        (when (re-search-forward page-delimiter nil t)
6231                          (setq nd (match-beginning 0)
6232                                pt (point-min)))))
6233                     ((> arg 0)
6234                      (if (re-search-forward page-delimiter nil 'move arg)
6235                          (prog1
6236                              (setq st (match-end 0)
6237                                    pt st)
6238                            (when (re-search-forward page-delimiter nil t)
6239                              (setq nd (match-beginning 0))))
6240                        (when (re-search-backward page-delimiter nil t)
6241                          (setq st (match-end 0)
6242                                pt (point-max)))))
6243                     (t
6244                      (when (re-search-backward page-delimiter nil t)
6245                        (goto-char (setq st (match-end 0))))
6246                      (when (re-search-forward page-delimiter nil t)
6247                        (setq nd (match-beginning 0)))
6248                      (or st nd))))
6249         (setq gnus-page-broken t)
6250         (when pt (goto-char pt))
6251         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6252         (when (gnus-visual-p 'page-marker)
6253           (save-excursion
6254             (when nd
6255               (goto-char nd)
6256               (gnus-insert-next-page-button))
6257             (when st
6258               (goto-char st)
6259               (gnus-insert-prev-page-button))))))))
6260
6261 ;; Article mode commands
6262
6263 (defun gnus-article-goto-next-page ()
6264   "Show the next page of the article."
6265   (interactive)
6266   (when (gnus-article-next-page)
6267     (goto-char (point-min))
6268     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6269
6270
6271 (defun gnus-article-goto-prev-page ()
6272   "Show the previous page of the article."
6273   (interactive)
6274   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6275       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6276     (gnus-article-prev-page nil)))
6277
6278 ;; This is cleaner but currently breaks `gnus-pick-mode':
6279 ;;
6280 ;; (defun gnus-article-goto-next-page ()
6281 ;;   "Show the next page of the article."
6282 ;;   (interactive)
6283 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6284 ;;     (gnus-summary-next-page)))
6285 ;;
6286 ;; (defun gnus-article-goto-prev-page ()
6287 ;;   "Show the next page of the article."
6288 ;;   (interactive)
6289 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6290 ;;     (gnus-summary-prev-page)))
6291
6292 (defun gnus-article-next-page (&optional lines)
6293   "Show the next page of the current article.
6294 If end of article, return non-nil.  Otherwise return nil.
6295 Argument LINES specifies lines to be scrolled up."
6296   (interactive "p")
6297   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6298   (if (and (not (and gnus-article-over-scroll
6299                      (> (count-lines (window-start) (point-max))
6300                         (if (featurep 'xemacs)
6301                             (or lines (1- (window-height)))
6302                           (+ (or lines (1- (window-height))) scroll-margin)))))
6303            (save-excursion
6304              (end-of-line)
6305              (and (pos-visible-in-window-p)     ;Not continuation line.
6306                   (>= (point) (point-max)))))
6307       ;; Nothing in this page.
6308       (if (or (not gnus-page-broken)
6309               (save-excursion
6310                 (save-restriction
6311                   (widen)
6312                   (forward-line)
6313                   (eobp)))) ;Real end-of-buffer?
6314           (progn
6315             (when gnus-article-over-scroll
6316               (gnus-article-next-page-1 lines))
6317             t)                  ;Nothing more.
6318         (gnus-narrow-to-page 1)         ;Go to next page.
6319         nil)
6320     ;; More in this page.
6321     (gnus-article-next-page-1 lines)
6322     nil))
6323
6324 (defun gnus-article-beginning-of-window ()
6325   "Move point to the beginning of the window.
6326 In Emacs, the point is placed at the line number which `scroll-margin'
6327 specifies."
6328   (if (featurep 'xemacs)
6329       (move-to-window-line 0)
6330     ;; There is an obscure bug in Emacs that makes it impossible to
6331     ;; scroll past big pictures in the article buffer.  Try to fix
6332     ;; this by adding a sanity check by counting the lines visible.
6333     (when (> (count-lines (window-start) (window-end)) 30)
6334       (move-to-window-line
6335        (min (max 0 scroll-margin)
6336             (max 1 (- (window-height)
6337                       (if mode-line-format 1 0)
6338                       (if header-line-format 1 0)
6339                       2)))))))
6340
6341 (defun gnus-article-next-page-1 (lines)
6342   (condition-case ()
6343       (let ((scroll-in-place nil)
6344             (auto-window-vscroll nil))
6345         (scroll-up lines))
6346     (end-of-buffer
6347      ;; Long lines may cause an end-of-buffer error.
6348      (goto-char (point-max))))
6349   (gnus-article-beginning-of-window))
6350
6351 (defun gnus-article-prev-page (&optional lines)
6352   "Show previous page of current article.
6353 Argument LINES specifies lines to be scrolled down."
6354   (interactive "p")
6355   (move-to-window-line 0)
6356   (if (and gnus-page-broken
6357            (bobp)
6358            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6359       (progn
6360         (gnus-narrow-to-page -1)        ;Go to previous page.
6361         (goto-char (point-max))
6362         (recenter (if gnus-article-over-scroll
6363                       (if lines
6364                           (max (if (featurep 'xemacs)
6365                                    lines
6366                                  (+ lines scroll-margin))
6367                                3)
6368                         (- (window-height) 2))
6369                     -1)))
6370     (prog1
6371         (condition-case ()
6372             (let ((scroll-in-place nil))
6373               (scroll-down lines))
6374           (beginning-of-buffer
6375            (goto-char (point-min))))
6376       (gnus-article-beginning-of-window))))
6377
6378 (defun gnus-article-only-boring-p ()
6379   "Decide whether there is only boring text remaining in the article.
6380 Something \"interesting\" is a word of at least two letters that does
6381 not have a face in `gnus-article-boring-faces'."
6382   (when (and gnus-article-skip-boring
6383              (boundp 'gnus-article-boring-faces)
6384              (symbol-value 'gnus-article-boring-faces))
6385     (save-excursion
6386       (let ((inhibit-point-motion-hooks t))
6387         (catch 'only-boring
6388           (while (re-search-forward "\\b\\w\\w" nil t)
6389             (forward-char -1)
6390             (when (not (gnus-intersection
6391                         (gnus-faces-at (point))
6392                         (symbol-value 'gnus-article-boring-faces)))
6393               (throw 'only-boring nil)))
6394           (throw 'only-boring t))))))
6395
6396 (defun gnus-article-refer-article ()
6397   "Read article specified by message-id around point."
6398   (interactive)
6399   (save-excursion
6400     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6401     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6402     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6403         (let ((msg-id (concat "<" (match-string 0) ">")))
6404           (set-buffer gnus-summary-buffer)
6405           (gnus-summary-refer-article msg-id))
6406       (error "No references around point"))))
6407
6408 (defun gnus-article-show-summary ()
6409   "Reconfigure windows to show summary buffer."
6410   (interactive)
6411   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6412       (error "There is no summary buffer for this article buffer")
6413     (gnus-article-set-globals)
6414     (gnus-configure-windows 'article)
6415     (gnus-summary-goto-subject gnus-current-article)
6416     (gnus-summary-position-point)))
6417
6418 (defun gnus-article-describe-briefly ()
6419   "Describe article mode commands briefly."
6420   (interactive)
6421   (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")))
6422
6423 (defun gnus-article-check-buffer ()
6424   "Beep if not in an article buffer."
6425   (unless (equal major-mode 'gnus-article-mode)
6426     (error "Command invoked outside of a Gnus article buffer")))
6427
6428 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6429   "Read a summary buffer key sequence and execute it from the article buffer."
6430   (interactive "P")
6431   (gnus-article-check-buffer)
6432   (let ((nosaves
6433          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6434            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6435            "=" "^" "\M-^" "|"))
6436         (nosave-but-article
6437          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6438            "An" "Ap" [?A (meta return)] [?A delete]))
6439         (nosave-in-article
6440          '("AS" "\C-d"))
6441         (up-to-top
6442          '("n" "Gn" "p" "Gp"))
6443         keys new-sum-point)
6444     (with-current-buffer gnus-article-current-summary
6445       (let (gnus-pick-mode)
6446         (setq unread-command-events (nconc unread-command-events
6447                                            (list (or key last-command-event)))
6448               keys (if (featurep 'xemacs)
6449                        (events-to-keys (read-key-sequence nil t))
6450                      (read-key-sequence nil t)))))
6451
6452     (message "")
6453
6454     (cond
6455      ((eq (aref keys (1- (length keys))) ?\C-h)
6456       (gnus-article-describe-bindings (substring keys 0 -1)))
6457      ((or (member keys nosaves)
6458           (member keys nosave-but-article)
6459           (member keys nosave-in-article))
6460       (let (func)
6461         (save-window-excursion
6462           (pop-to-buffer gnus-article-current-summary)
6463           ;; We disable the pick minor mode commands.
6464           (let (gnus-pick-mode)
6465             (setq func (key-binding keys t))))
6466         (if (or (not func)
6467                 (numberp func))
6468             (ding)
6469           (unless (member keys nosave-in-article)
6470             (set-buffer gnus-article-current-summary))
6471           (when (get func 'disabled)
6472             (error "Function %s disabled" func))
6473           (call-interactively func)
6474           (setq new-sum-point (point)))
6475         (when (member keys nosave-but-article)
6476           (pop-to-buffer gnus-article-buffer))))
6477      (t
6478       ;; These commands should restore window configuration.
6479       (let ((obuf (current-buffer))
6480             (owin (current-window-configuration))
6481             win func in-buffer selected new-sum-start new-sum-hscroll err)
6482         (cond (not-restore-window
6483                (pop-to-buffer gnus-article-current-summary)
6484                (setq win (selected-window)))
6485               ((setq win (get-buffer-window gnus-article-current-summary))
6486                (select-window win))
6487               (t
6488                (let ((summary-buffer gnus-article-current-summary))
6489                  (gnus-configure-windows 'article)
6490                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6491                    (let ((gnus-buffer-configuration
6492                           '((article ((vertical 1.0
6493                                                 (summary 0.25 point)
6494                                                 (article 1.0)))))))
6495                      (gnus-configure-windows 'article))
6496                    (setq win (get-buffer-window summary-buffer 'visible)))
6497                  (gnus-select-frame-set-input-focus (window-frame win))
6498                  (select-window win))))
6499         (setq in-buffer (current-buffer))
6500         ;; We disable the pick minor mode commands.
6501         (setq func (let (gnus-pick-mode)
6502                      (key-binding keys t)))
6503         (when (get func 'disabled)
6504           (error "Function %s disabled" func))
6505         (if (and func
6506                  (functionp func)
6507                  (condition-case code
6508                      (progn
6509                        (call-interactively func)
6510                        t)
6511                    (error
6512                     (setq err code)
6513                     nil)))
6514             (progn
6515               (when (eq win (selected-window))
6516                 (setq new-sum-point (point)
6517                       new-sum-start (window-start win)
6518                       new-sum-hscroll (window-hscroll win)))
6519               (when (or (eq in-buffer (current-buffer))
6520                         (when (eq obuf (current-buffer))
6521                           (set-buffer in-buffer)
6522                           t))
6523                 (setq selected (gnus-summary-select-article))
6524                 (set-buffer obuf)
6525                 (unless not-restore-window
6526                   (set-window-configuration owin))
6527                 (when (and (eq selected 'old)
6528                            new-sum-point)
6529                   (set-window-start (get-buffer-window (current-buffer))
6530                                     1)
6531                   (set-window-point (get-buffer-window (current-buffer))
6532                                     (if (article-goto-body)
6533                                         (1- (point))
6534                                       (point))))
6535                 (when (and (not not-restore-window)
6536                            new-sum-point
6537                            (window-live-p win)
6538                            (with-current-buffer (window-buffer win)
6539                              (eq major-mode 'gnus-summary-mode)))
6540                   (set-window-point win new-sum-point)
6541                   (set-window-start win new-sum-start)
6542                   (set-window-hscroll win new-sum-hscroll))))
6543           (set-window-configuration owin)
6544           (if err
6545               (signal (car err) (cdr err))
6546             (ding))))))))
6547
6548 (defun gnus-article-read-summary-send-keys ()
6549   (interactive)
6550   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6551     (gnus-article-read-summary-keys)))
6552
6553 (defun gnus-article-describe-key (key)
6554   "Display documentation of the function invoked by KEY.
6555 KEY is a string or a vector."
6556   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6557                        (read-key-sequence "Describe key: "))))
6558   (gnus-article-check-buffer)
6559   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6560                                   gnus-article-read-summary-send-keys))
6561       (with-current-buffer gnus-article-current-summary
6562         (setq unread-command-events
6563               (if (featurep 'xemacs)
6564                   (append key nil)
6565                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6566                                         (list 'meta (- x 128))
6567                                       x))
6568                         key)))
6569         (let ((cursor-in-echo-area t)
6570               gnus-pick-mode)
6571           (describe-key (read-key-sequence nil t))))
6572     (describe-key key)))
6573
6574 (defun gnus-article-describe-key-briefly (key &optional insert)
6575   "Display documentation of the function invoked by KEY.
6576 KEY is a string or a vector."
6577   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6578                        (read-key-sequence "Describe key: "))
6579                      current-prefix-arg))
6580   (gnus-article-check-buffer)
6581   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6582                                   gnus-article-read-summary-send-keys))
6583       (with-current-buffer gnus-article-current-summary
6584         (setq unread-command-events
6585               (if (featurep 'xemacs)
6586                   (append key nil)
6587                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6588                                         (list 'meta (- x 128))
6589                                       x))
6590                         key)))
6591         (let ((cursor-in-echo-area t)
6592               gnus-pick-mode)
6593           (describe-key-briefly (read-key-sequence nil t) insert)))
6594     (describe-key-briefly key insert)))
6595
6596 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6597 (defvar gnus-agent-summary-mode)
6598 (defvar gnus-draft-mode)
6599 ;; Calling help-buffer will autoload help-mode.
6600 (defvar help-xref-stack-item)
6601 ;; Emacs 22 doesn't load it in the batch mode.
6602 (eval-when-compile
6603   (autoload 'help-buffer "help-mode"))
6604
6605 (defun gnus-article-describe-bindings (&optional prefix)
6606   "Show a list of all defined keys, and their definitions.
6607 The optional argument PREFIX, if non-nil, should be a key sequence;
6608 then we display only bindings that start with that prefix."
6609   (interactive)
6610   (gnus-article-check-buffer)
6611   (let ((keymap (copy-keymap gnus-article-mode-map))
6612         (map (copy-keymap gnus-article-send-map))
6613         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6614         parent agent draft)
6615     (define-key keymap "S" map)
6616     (define-key map [t] nil)
6617     (with-current-buffer gnus-article-current-summary
6618       (set-keymap-parent
6619        keymap
6620        (if (setq parent (keymap-parent gnus-article-mode-map))
6621            (prog1
6622                (setq parent (copy-keymap parent))
6623              (set-keymap-parent parent (current-local-map)))
6624          (current-local-map)))
6625       (set-keymap-parent map (key-binding "S"))
6626       (let (key def gnus-pick-mode)
6627         (while sumkeys
6628           (setq key (pop sumkeys))
6629           (cond ((and (vectorp key) (= (length key) 1)
6630                       (consp (setq def (aref key 0)))
6631                       (numberp (car def)) (numberp (cdr def)))
6632                  (when (< (max (car def) (cdr def)) 128)
6633                    (setq sumkeys
6634                          (append (mapcar
6635                                   #'vector
6636                                   (nreverse (gnus-uncompress-range def)))
6637                                  sumkeys))))
6638                 ((setq def (key-binding key))
6639                  (unless (eq def 'undefined)
6640                    (define-key keymap key def))))))
6641       (when (boundp 'gnus-agent-summary-mode)
6642         (setq agent gnus-agent-summary-mode))
6643       (when (boundp 'gnus-draft-mode)
6644         (setq draft gnus-draft-mode)))
6645     (with-temp-buffer
6646       (use-local-map keymap)
6647       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6648       (set (make-local-variable 'gnus-draft-mode) draft)
6649       (describe-bindings prefix))
6650     (let ((item `((lambda (prefix)
6651                     (with-current-buffer ,(current-buffer)
6652                       (gnus-article-describe-bindings prefix)))
6653                   ,prefix)))
6654       (with-current-buffer (let (help-xref-following) (help-buffer))
6655         (setq help-xref-stack-item item)))))
6656
6657 (defun gnus-article-reply-with-original (&optional wide)
6658   "Start composing a reply mail to the current message.
6659 The text in the region will be yanked.  If the region isn't active,
6660 the entire article will be yanked."
6661   (interactive)
6662   (let ((article (cdr gnus-article-current))
6663         contents)
6664     (if (not (gnus-region-active-p))
6665         (with-current-buffer gnus-summary-buffer
6666           (gnus-summary-reply (list (list article)) wide))
6667       (setq contents (buffer-substring (point) (mark t)))
6668       ;; Deactivate active regions.
6669       (when (and (boundp 'transient-mark-mode)
6670                  transient-mark-mode)
6671         (setq mark-active nil))
6672       (with-current-buffer gnus-summary-buffer
6673         (gnus-summary-reply
6674          (list (list article contents)) wide)))))
6675
6676 (defun gnus-article-wide-reply-with-original ()
6677   "Start composing a wide reply mail to the current message.
6678 The text in the region will be yanked.  If the region isn't active,
6679 the entire article will be yanked."
6680   (interactive)
6681   (gnus-article-reply-with-original t))
6682
6683 (defun gnus-article-followup-with-original ()
6684   "Compose a followup to the current article.
6685 The text in the region will be yanked.  If the region isn't active,
6686 the entire article will be yanked."
6687   (interactive)
6688   (let ((article (cdr gnus-article-current))
6689         contents)
6690       (if (not (gnus-region-active-p))
6691           (with-current-buffer gnus-summary-buffer
6692             (gnus-summary-followup (list (list article))))
6693         (setq contents (buffer-substring (point) (mark t)))
6694         ;; Deactivate active regions.
6695         (when (and (boundp 'transient-mark-mode)
6696                    transient-mark-mode)
6697           (setq mark-active nil))
6698         (with-current-buffer gnus-summary-buffer
6699           (gnus-summary-followup
6700            (list (list article contents)))))))
6701
6702 (defun gnus-article-hide (&optional arg force)
6703   "Hide all the gruft in the current article.
6704 This means that signatures, cited text and (some) headers will be
6705 hidden.
6706 If given a prefix, show the hidden text instead."
6707   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6708   (gnus-article-hide-headers arg)
6709   (gnus-article-hide-list-identifiers arg)
6710   (gnus-article-hide-citation-maybe arg force)
6711   (gnus-article-hide-signature arg))
6712
6713 (defun gnus-article-maybe-highlight ()
6714   "Do some article highlighting if article highlighting is requested."
6715   (when (gnus-visual-p 'article-highlight 'highlight)
6716     (gnus-article-highlight-some)))
6717
6718 (defun gnus-check-group-server ()
6719   ;; Make sure the connection to the server is alive.
6720   (unless (gnus-server-opened
6721            (gnus-find-method-for-group gnus-newsgroup-name))
6722     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6723     (gnus-request-group gnus-newsgroup-name t)))
6724
6725 (eval-when-compile
6726   (autoload 'nneething-get-file-name "nneething"))
6727
6728 (defun gnus-request-article-this-buffer (article group)
6729   "Get an article and insert it into this buffer."
6730   (let (do-update-line sparse-header)
6731     (prog1
6732         (save-excursion
6733           (erase-buffer)
6734           (gnus-kill-all-overlays)
6735           (setq group (or group gnus-newsgroup-name))
6736
6737           ;; Using `gnus-request-article' directly will insert the article into
6738           ;; `nntp-server-buffer' - so we'll save some time by not having to
6739           ;; copy it from the server buffer into the article buffer.
6740
6741           ;; We only request an article by message-id when we do not have the
6742           ;; headers for it, so we'll have to get those.
6743           (when (stringp article)
6744             (gnus-read-header article))
6745
6746           ;; If the article number is negative, that means that this article
6747           ;; doesn't belong in this newsgroup (possibly), so we find its
6748           ;; message-id and request it by id instead of number.
6749           (when (and (numberp article)
6750                      gnus-summary-buffer
6751                      (get-buffer gnus-summary-buffer)
6752                      (gnus-buffer-exists-p gnus-summary-buffer))
6753             (with-current-buffer gnus-summary-buffer
6754               (let ((header (gnus-summary-article-header article)))
6755                 (when (< article 0)
6756                   (cond
6757                    ((memq article gnus-newsgroup-sparse)
6758                     ;; This is a sparse gap article.
6759                     (setq do-update-line article)
6760                     (setq article (mail-header-id header))
6761                     (setq sparse-header (gnus-read-header article))
6762                     (setq gnus-newsgroup-sparse
6763                           (delq article gnus-newsgroup-sparse)))
6764                    ((vectorp header)
6765                     ;; It's a real article.
6766                     (setq article (mail-header-id header)))
6767                    (t
6768                     ;; It is an extracted pseudo-article.
6769                     (setq article 'pseudo)
6770                     (gnus-request-pseudo-article header))))
6771
6772                 (let ((method (gnus-find-method-for-group
6773                                gnus-newsgroup-name)))
6774                   (when (and (eq (car method) 'nneething)
6775                              (vectorp header))
6776                     (let ((dir (nneething-get-file-name
6777                                 (mail-header-id header))))
6778                       (when (and (stringp dir)
6779                                  (file-directory-p dir))
6780                         (setq article 'nneething)
6781                         (gnus-group-enter-directory dir))))))))
6782
6783           (cond
6784            ;; Refuse to select canceled articles.
6785            ((and (numberp article)
6786                  gnus-summary-buffer
6787                  (get-buffer gnus-summary-buffer)
6788                  (gnus-buffer-exists-p gnus-summary-buffer)
6789                  (eq (cdr (with-current-buffer gnus-summary-buffer
6790                             (assq article gnus-newsgroup-reads)))
6791                      gnus-canceled-mark))
6792             nil)
6793            ;; We first check `gnus-original-article-buffer'.
6794            ((and (get-buffer gnus-original-article-buffer)
6795                  (numberp article)
6796                  (with-current-buffer gnus-original-article-buffer
6797                    (and (equal (car gnus-original-article) group)
6798                         (eq (cdr gnus-original-article) article))))
6799             ;; `insert-buffer-substring' would incorrectly use the
6800             ;; equivalent of string-make-multibyte which amount to decoding
6801             ;; with locale-coding-system, causing failure of
6802             ;; subsequent decoding.
6803             (insert (mm-string-to-multibyte
6804                      (with-current-buffer gnus-original-article-buffer
6805                        (buffer-substring (point-min) (point-max)))))
6806             'article)
6807            ;; Check the backlog.
6808            ((and gnus-keep-backlog
6809                  (gnus-backlog-request-article group article (current-buffer)))
6810             'article)
6811            ;; Check asynchronous pre-fetch.
6812            ((gnus-async-request-fetched-article group article (current-buffer))
6813             (gnus-async-prefetch-next group article gnus-summary-buffer)
6814             (when (and (numberp article) gnus-keep-backlog)
6815               (gnus-backlog-enter-article group article (current-buffer)))
6816             'article)
6817            ;; Check the cache.
6818            ((and gnus-use-cache
6819                  (numberp article)
6820                  (gnus-cache-request-article article group))
6821             'article)
6822            ;; Check the agent cache.
6823            ((gnus-agent-request-article article group)
6824             'article)
6825            ;; Get the article and put into the article buffer.
6826            ((or (stringp article)
6827                 (numberp article))
6828             (let ((gnus-override-method gnus-override-method)
6829                   (methods (and (stringp article)
6830                                 gnus-refer-article-method))
6831                   (backend (car (gnus-find-method-for-group
6832                                  gnus-newsgroup-name)))
6833                   result
6834                   (inhibit-read-only t))
6835               (if (or (not (listp methods))
6836                       (and (symbolp (car methods))
6837                            (assq (car methods) nnoo-definition-alist)))
6838                   (setq methods (list methods)))
6839               (when (and (null gnus-override-method)
6840                          methods)
6841                 (setq gnus-override-method (pop methods)))
6842               (while (not result)
6843                 (when (eq gnus-override-method 'current)
6844                   (setq gnus-override-method
6845                         (with-current-buffer gnus-summary-buffer
6846                           gnus-current-select-method)))
6847                 (erase-buffer)
6848                 (gnus-kill-all-overlays)
6849                 (let ((gnus-newsgroup-name group))
6850                   (gnus-check-group-server))
6851                 (cond
6852                  ((gnus-request-article article group (current-buffer))
6853                   (when (numberp article)
6854                     (gnus-async-prefetch-next group article
6855                                               gnus-summary-buffer)
6856                     (when gnus-keep-backlog
6857                       (gnus-backlog-enter-article
6858                        group article (current-buffer))))
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