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