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