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