Use the local gnus-simplify-subject-fuzzy-regex instead of the global one.
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (defvar tool-bar-map)
33 (defvar w3m-minor-mode-map)
34
35 (require 'gnus)
36 (require 'gnus-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'gnus-win)
40 (require 'mm-bodies)
41 (require 'mail-parse)
42 (require 'mm-decode)
43 (require 'mm-view)
44 (require 'wid-edit)
45 (require 'mm-uu)
46 (require 'message)
47 (require 'mouse)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54 (autoload 'mm-url-insert-file-contents-external "mm-url")
55 (autoload 'mm-extern-cache-contents "mm-extern")
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   (mapcar
120    (lambda (header)
121      (concat "^" header ":"))
122    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
123      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
124      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
125      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
126      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
127      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
128      "X-Attribution" "X-Originating-IP" "Delivered-To"
129      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
130      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
131      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
132      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
133      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
134      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
135      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
136      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
137      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
138      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
139      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
140      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
141      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
142      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
143      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
144      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
145      "List-[A-Za-z]+" "X-Listprocessor-Version"
146      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
147      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
148      "X-Received" "Content-length" "X-precedence"
149      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
150      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
151      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
152      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
153      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
154      "X-Content-length" "X-Posting-Agent" "Original-Received"
155      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
156      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
157      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
158      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
159      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
160      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
161      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
162      "Envelope-Sender" "Envelope-Recipients"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice :custom-show nil
167                  regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-hidden-properties '(invisible t intangible t)
261   "Property list to use for hiding text."
262   :type 'sexp
263   :group 'gnus-article-hiding)
264
265 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
266 ;; frames in a session.
267 (defcustom gnus-article-x-face-command
268   (if (featurep 'xemacs)
269       (if (or (gnus-image-type-available-p 'xface)
270               (gnus-image-type-available-p 'pbm))
271           'gnus-display-x-face-in-from
272         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
273     (if (gnus-image-type-available-p 'pbm)
274         'gnus-display-x-face-in-from
275       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
276 display -"))
277   "*String or function to be executed to display an X-Face header.
278 If it is a string, the command will be executed in a sub-shell
279 asynchronously.  The compressed face will be piped to this command."
280   :type `(choice string
281                  (function-item gnus-display-x-face-in-from)
282                  function)
283   :version "21.1"
284   :group 'gnus-picon
285   :group 'gnus-article-washing)
286
287 (defcustom gnus-article-x-face-too-ugly nil
288   "Regexp matching posters whose face shouldn't be shown automatically."
289   :type '(choice regexp (const nil))
290   :group 'gnus-article-washing)
291
292 (defcustom gnus-article-banner-alist nil
293   "Banner alist for stripping.
294 For example,
295      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
296   :version "21.1"
297   :type '(repeat (cons symbol regexp))
298   :group 'gnus-article-washing)
299
300 (gnus-define-group-parameter
301  banner
302  :variable-document
303  "Alist of regexps (to match group names) and banner."
304  :variable-group gnus-article-washing
305  :parameter-type
306  '(choice :tag "Banner"
307           :value nil
308           (const :tag "Remove signature" signature)
309           (symbol :tag "Item in `gnus-article-banner-alist'" none)
310           regexp
311           (const :tag "None" nil))
312  :parameter-document
313  "If non-nil, specify how to remove `banners' from articles.
314
315 Symbol `signature' means to remove signatures delimited by
316 `gnus-signature-separator'.  Any other symbol is used to look up a
317 regular expression to match the banner in `gnus-article-banner-alist'.
318 A string is used as a regular expression to match the banner
319 directly.")
320
321 (defcustom gnus-article-address-banner-alist nil
322   "Alist of mail addresses and banners.
323 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
324 to match a mail address in the From: header, BANNER is one of a symbol
325 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
326 If ADDRESS matches author's mail address, it will remove things like
327 advertisements.  For example:
328
329 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
330 "
331   :type '(repeat
332           (cons
333            (regexp :tag "Address")
334            (choice :tag "Banner" :value nil
335                    (const :tag "Remove signature" signature)
336                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
337                    regexp
338                    (const :tag "None" nil))))
339   :version "22.1"
340   :group 'gnus-article-washing)
341
342 (defmacro gnus-emphasis-custom-with-format (&rest body)
343   `(let ((format "\
344 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
345 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
346      ,@body))
347
348 (defun gnus-emphasis-custom-value-to-external (value)
349   (gnus-emphasis-custom-with-format
350    (if (consp (car value))
351        (list (format format (car (car value)) (cdr (car value)))
352              2
353              (if (nth 1 value) 2 3)
354              (nth 2 value))
355      value)))
356
357 (defun gnus-emphasis-custom-value-to-internal (value)
358   (gnus-emphasis-custom-with-format
359    (let ((regexp (concat "\\`"
360                          (format (regexp-quote format)
361                                  "\\([^()]+\\)" "\\([^()]+\\)")
362                          "\\'"))
363          pattern)
364      (if (string-match regexp (setq pattern (car value)))
365          (list (cons (match-string 1 pattern) (match-string 2 pattern))
366                (= (nth 2 value) 2)
367                (nth 3 value))
368        value))))
369
370 (defcustom gnus-emphasis-alist
371   (let ((types
372          '(("\\*" "\\*" bold nil 2)
373            ("_" "_" underline)
374            ("/" "/" italic)
375            ("_/" "/_" underline-italic)
376            ("_\\*" "\\*_" underline-bold)
377            ("\\*/" "/\\*" bold-italic)
378            ("_\\*/" "/\\*_" underline-bold-italic))))
379     (nconc
380      (gnus-emphasis-custom-with-format
381       (mapcar (lambda (spec)
382                 (list (format format (car spec) (cadr spec))
383                       (or (nth 3 spec) 2)
384                       (or (nth 4 spec) 3)
385                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
386               types))
387      '(;; I've never seen anyone use this strikethru convention whereas I've
388        ;; several times seen it triggered by normal text.  --Stef
389        ;; Miles suggests that this form is sometimes used but for italics,
390        ;; so maybe we should map it to `italic'.
391        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
392        ;; 2 3 gnus-emphasis-strikethru)
393        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
394         2 3 gnus-emphasis-underline))))
395   "*Alist that says how to fontify certain phrases.
396 Each item looks like this:
397
398   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
399
400 The first element is a regular expression to be matched.  The second
401 is a number that says what regular expression grouping used to find
402 the entire emphasized word.  The third is a number that says what
403 regexp grouping should be displayed and highlighted.  The fourth
404 is the face used for highlighting."
405   :type
406   '(repeat
407     (menu-choice
408      :format "%[Customizing Style%]\n%v"
409      :indent 2
410      (group :tag "Default"
411             :value ("" 0 0 default)
412             :value-create
413             (lambda (widget)
414               (let ((value (widget-get
415                             (cadr (widget-get (widget-get widget :parent)
416                                               :args))
417                             :value)))
418                 (if (not (eq (nth 2 value) 'default))
419                     (widget-put
420                      widget
421                      :value
422                      (gnus-emphasis-custom-value-to-external value))))
423               (widget-group-value-create widget))
424             regexp
425             (integer :format "Match group: %v")
426             (integer :format "Emphasize group: %v")
427             face)
428      (group :tag "Simple"
429             :value (("_" . "_") nil default)
430             (cons :format "%v"
431                   (regexp :format "Start regexp: %v")
432                   (regexp :format "End regexp: %v"))
433             (boolean :format "Show start and end patterns: %[%v%]\n"
434                      :on " On " :off " Off ")
435             face)))
436   :get (lambda (symbol)
437          (mapcar 'gnus-emphasis-custom-value-to-internal
438                  (default-value symbol)))
439   :set (lambda (symbol value)
440          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
441                                      value)))
442   :group 'gnus-article-emphasis)
443
444 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
445   "A regexp to describe whitespace which should not be emphasized.
446 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
447 The former avoids underlining of leading and trailing whitespace,
448 and the latter avoids underlining any whitespace at all."
449   :version "21.1"
450   :group 'gnus-article-emphasis
451   :type 'regexp)
452
453 (defface gnus-emphasis-bold '((t (:bold t)))
454   "Face used for displaying strong emphasized text (*word*)."
455   :group 'gnus-article-emphasis)
456
457 (defface gnus-emphasis-italic '((t (:italic t)))
458   "Face used for displaying italic emphasized text (/word/)."
459   :group 'gnus-article-emphasis)
460
461 (defface gnus-emphasis-underline '((t (:underline t)))
462   "Face used for displaying underlined emphasized text (_word_)."
463   :group 'gnus-article-emphasis)
464
465 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
466   "Face used for displaying underlined bold emphasized text (_*word*_)."
467   :group 'gnus-article-emphasis)
468
469 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
470   "Face used for displaying underlined italic emphasized text (_/word/_)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
474   "Face used for displaying bold italic emphasized text (/*word*/)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-underline-bold-italic
478   '((t (:bold t :italic t :underline t)))
479   "Face used for displaying underlined bold italic emphasized text.
480 Example: (_/*word*/_)."
481   :group 'gnus-article-emphasis)
482
483 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
484                                       '((t (:strikethru t)))
485                                     '((t (:strike-through t))))
486   "Face used for displaying strike-through text (-word-)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-highlight-words
490   '((t (:background "black" :foreground "yellow")))
491   "Face used for displaying highlighted words."
492   :group 'gnus-article-emphasis)
493
494 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
495   "Format for display of Date headers in article bodies.
496 See `format-time-string' for the possible values.
497
498 The variable can also be function, which should return a complete Date
499 header.  The function is called with one argument, the time, which can
500 be fed to `format-time-string'."
501   :type '(choice string function)
502   :link '(custom-manual "(gnus)Article Date")
503   :group 'gnus-article-washing)
504
505 (defcustom gnus-save-all-headers t
506   "*If non-nil, don't remove any headers before saving.
507 This will be overridden by the `:headers' property that the symbol of
508 the saver function, which is specified by `gnus-default-article-saver',
509 might have."
510   :group 'gnus-article-saving
511   :type 'boolean)
512
513 (defcustom gnus-prompt-before-saving 'always
514   "*This variable says how much prompting is to be done when saving articles.
515 If it is nil, no prompting will be done, and the articles will be
516 saved to the default files.  If this variable is `always', each and
517 every article that is saved will be preceded by a prompt, even when
518 saving large batches of articles.  If this variable is neither nil not
519 `always', there the user will be prompted once for a file name for
520 each invocation of the saving commands."
521   :group 'gnus-article-saving
522   :type '(choice (item always)
523                  (item :tag "never" nil)
524                  (sexp :tag "once" :format "%t\n" :value t)))
525
526 (defcustom gnus-saved-headers gnus-visible-headers
527   "Headers to keep if `gnus-save-all-headers' is nil.
528 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
529 If that variable is nil, however, all headers that match this regexp
530 will be kept while the rest will be deleted before saving.  This and
531 `gnus-save-all-headers' will be overridden by the `:headers' property
532 that the symbol of the saver function, which is specified by
533 `gnus-default-article-saver', might have."
534   :group 'gnus-article-saving
535   :type 'regexp)
536
537 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
538 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
539   "A function to save articles in your favourite format.
540 The function will be called by way of the `gnus-summary-save-article'
541 command, and friends such as `gnus-summary-save-article-rmail'.
542
543 Gnus provides the following functions:
544
545 * gnus-summary-save-in-rmail (Rmail format)
546 * gnus-summary-save-in-mail (Unix mail format)
547 * gnus-summary-save-in-folder (MH folder)
548 * gnus-summary-save-in-file (article format)
549 * gnus-summary-save-body-in-file (article body)
550 * gnus-summary-save-in-vm (use VM's folder format)
551 * gnus-summary-write-to-file (article format -- overwrite)
552 * gnus-summary-write-body-to-file (article body -- overwrite)
553 * gnus-summary-save-in-pipe (article format)
554
555 The symbol of each function may have the following properties:
556
557 * :decode
558 The value non-nil means save decoded articles.  This is meaningful
559 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
560 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
561 `gnus-summary-save-in-pipe'.
562
563 * :function
564 The value specifies an alternative function which appends, not
565 overwrites, articles to a file.  This implies that when saving many
566 articles at a time, `gnus-prompt-before-saving' is bound to t and all
567 articles are saved in a single file.  This is meaningful only with
568 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
569
570 * :headers
571 The value specifies the symbol of a variable of which the value
572 specifies headers to be saved.  If it is omitted,
573 `gnus-save-all-headers' and `gnus-saved-headers' control what
574 headers should be saved."
575   :group 'gnus-article-saving
576   :type '(radio (function-item gnus-summary-save-in-rmail)
577                 (function-item gnus-summary-save-in-mail)
578                 (function-item gnus-summary-save-in-folder)
579                 (function-item gnus-summary-save-in-file)
580                 (function-item gnus-summary-save-body-in-file)
581                 (function-item gnus-summary-save-in-vm)
582                 (function-item gnus-summary-write-to-file)
583                 (function-item gnus-summary-write-body-to-file)
584                 (function-item gnus-summary-save-in-pipe)
585                 (function)))
586
587 (defcustom gnus-article-save-coding-system
588   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
589       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
590       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
591       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
592   "Coding system used to save decoded articles to a file.
593
594 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
595 which can safely encode any characters in text.  This is used by the
596 commands including:
597
598 * gnus-summary-save-article-file
599 * gnus-summary-save-article-body-file
600 * gnus-summary-write-article-file
601 * gnus-summary-write-article-body-file
602
603 and the functions to which you may set `gnus-default-article-saver':
604
605 * gnus-summary-save-in-file
606 * gnus-summary-save-body-in-file
607 * gnus-summary-write-to-file
608 * gnus-summary-write-body-to-file
609
610 Those commands and functions save just text displayed in the article
611 buffer to a file if the value of this variable is non-nil.  Note that
612 buttonized MIME parts will be lost in a saved file in that case.
613 Otherwise, raw articles will be saved."
614   :group 'gnus-article-saving
615   :type `(choice
616           :format "%{%t%}:\n %[Value Menu%] %v"
617           (const :tag "Save raw articles" nil)
618           ,@(delq nil
619                   (mapcar
620                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
621                    '((const :tag "UTF-8" utf-8)
622                      (const :tag "iso-2022-7bit" iso-2022-7bit)
623                      (const :tag "Emacs internal" emacs-mule)
624                      (const :tag "escape-quoted" escape-quoted))))
625           (symbol :tag "Coding system")))
626
627 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
628   "A function generating a file name to save articles in Rmail format.
629 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
630   :group 'gnus-article-saving
631   :type 'function)
632
633 (defcustom gnus-mail-save-name 'gnus-plain-save-name
634   "A function generating a file name to save articles in Unix mail format.
635 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
636   :group 'gnus-article-saving
637   :type 'function)
638
639 (defcustom gnus-folder-save-name 'gnus-folder-save-name
640   "A function generating a file name to save articles in MH folder.
641 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
642   :group 'gnus-article-saving
643   :type 'function)
644
645 (defcustom gnus-file-save-name 'gnus-numeric-save-name
646   "A function generating a file name to save articles in article format.
647 The function is called with NEWSGROUP, HEADERS, and optional
648 LAST-FILE."
649   :group 'gnus-article-saving
650   :type 'function)
651
652 (defcustom gnus-split-methods
653   '((gnus-article-archive-name)
654     (gnus-article-nndoc-name))
655   "*Variable used to suggest where articles are to be saved.
656 For instance, if you would like to save articles related to Gnus in
657 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
658 you could set this variable to something like:
659
660  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
661    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
662
663 This variable is an alist where the key is the match and the
664 value is a list of possible files to save in if the match is
665 non-nil.
666
667 If the match is a string, it is used as a regexp match on the
668 article.  If the match is a symbol, that symbol will be funcalled
669 from the buffer of the article to be saved with the newsgroup as the
670 parameter.  If it is a list, it will be evaled in the same buffer.
671
672 If this form or function returns a string, this string will be used as a
673 possible file name; and if it returns a non-nil list, that list will be
674 used as possible file names."
675   :group 'gnus-article-saving
676   :type '(repeat (choice (list :value (fun) function)
677                          (cons :value ("" "") regexp (repeat string))
678                          (sexp :value nil))))
679
680 (defcustom gnus-page-delimiter "^\^L"
681   "*Regexp describing what to use as article page delimiters.
682 The default value is \"^\^L\", which is a form linefeed at the
683 beginning of a line."
684   :type 'regexp
685   :group 'gnus-article-various)
686
687 (defcustom gnus-article-mode-line-format "Gnus: %g %S%m"
688   "*The format specification for the article mode line.
689 See `gnus-summary-mode-line-format' for a closer description.
690
691 The following additional specs are available:
692
693 %w  The article washing status.
694 %m  The number of MIME parts in the article."
695   :version "24.1"
696   :type 'string
697   :group 'gnus-article-various)
698
699 (defcustom gnus-article-mode-hook nil
700   "*A hook for Gnus article mode."
701   :type 'hook
702   :group 'gnus-article-various)
703
704 (when (featurep 'xemacs)
705   ;; Extracted from gnus-xmas-define in order to preserve user settings
706   (when (fboundp 'turn-off-scroll-in-place)
707     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
708   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
709   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
710
711 (defcustom gnus-article-menu-hook nil
712   "*Hook run after the creation of the article mode menu."
713   :type 'hook
714   :group 'gnus-article-various)
715
716 (defcustom gnus-article-prepare-hook nil
717   "*A hook called after an article has been prepared in the article buffer."
718   :type 'hook
719   :group 'gnus-article-various)
720
721 (defcustom gnus-copy-article-ignored-headers nil
722   "List of headers to be removed when copying an article.
723 Each element is a regular expression."
724   :version "23.1" ;; No Gnus
725   :type '(repeat regexp)
726   :group 'gnus-article-various)
727
728 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
729                         "Gnus 5.10 (Emacs 22.1)")
730
731 (defface gnus-button
732   '((t (:weight bold)))
733   "Face used for highlighting a button in the article buffer."
734   :group 'gnus-article-buttons)
735
736 (defcustom gnus-article-button-face 'gnus-button
737   "Face used for highlighting buttons in the article buffer.
738
739 An article button is a piece of text that you can activate by pressing
740 `RET' or `mouse-2' above it."
741   :type 'face
742   :group 'gnus-article-buttons)
743
744 (defcustom gnus-article-mouse-face 'highlight
745   "Face used for mouse highlighting in the article buffer.
746
747 Article buttons will be displayed in this face when the cursor is
748 above them."
749   :type 'face
750   :group 'gnus-article-buttons)
751
752 (defcustom gnus-signature-face 'gnus-signature
753   "Face used for highlighting a signature in the article buffer.
754 Obsolete; use the face `gnus-signature' for customizations instead."
755   :type 'face
756   :group 'gnus-article-highlight
757   :group 'gnus-article-signature)
758
759 (defface gnus-signature
760   '((t
761      (:italic t)))
762   "Face used for highlighting a signature in the article buffer."
763   :group 'gnus-article-highlight
764   :group 'gnus-article-signature)
765 ;; backward-compatibility alias
766 (put 'gnus-signature-face 'face-alias 'gnus-signature)
767 (put 'gnus-signature-face 'obsolete-face "22.1")
768
769 (defface gnus-header-from
770   '((((class color)
771       (background dark))
772      (:foreground "PaleGreen1"))
773     (((class color)
774       (background light))
775      (:foreground "red3"))
776     (t
777      (:italic t)))
778   "Face used for displaying from headers."
779   :group 'gnus-article-headers
780   :group 'gnus-article-highlight)
781 ;; backward-compatibility alias
782 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
783 (put 'gnus-header-from-face 'obsolete-face "22.1")
784
785 (defface gnus-header-subject
786   '((((class color)
787       (background dark))
788      (:foreground "SeaGreen1"))
789     (((class color)
790       (background light))
791      (:foreground "red4"))
792     (t
793      (:bold t :italic t)))
794   "Face used for displaying subject headers."
795   :group 'gnus-article-headers
796   :group 'gnus-article-highlight)
797 ;; backward-compatibility alias
798 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
799 (put 'gnus-header-subject-face 'obsolete-face "22.1")
800
801 (defface gnus-header-newsgroups
802   '((((class color)
803       (background dark))
804      (:foreground "yellow" :italic t))
805     (((class color)
806       (background light))
807      (:foreground "MidnightBlue" :italic t))
808     (t
809      (:italic t)))
810   "Face used for displaying newsgroups headers.
811 In the default setup this face is only used for crossposted
812 articles."
813   :group 'gnus-article-headers
814   :group 'gnus-article-highlight)
815 ;; backward-compatibility alias
816 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
817 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
818
819 (defface gnus-header-name
820   '((((class color)
821       (background dark))
822      (:foreground "SpringGreen2"))
823     (((class color)
824       (background light))
825      (:foreground "maroon"))
826     (t
827      (:bold t)))
828   "Face used for displaying header names."
829   :group 'gnus-article-headers
830   :group 'gnus-article-highlight)
831 ;; backward-compatibility alias
832 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
833 (put 'gnus-header-name-face 'obsolete-face "22.1")
834
835 (defface gnus-header-content
836   '((((class color)
837       (background dark))
838      (:foreground "SpringGreen1" :italic t))
839     (((class color)
840       (background light))
841      (:foreground "indianred4" :italic t))
842     (t
843      (:italic t)))  "Face used for displaying header content."
844   :group 'gnus-article-headers
845   :group 'gnus-article-highlight)
846 ;; backward-compatibility alias
847 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
848 (put 'gnus-header-content-face 'obsolete-face "22.1")
849
850 (defcustom gnus-header-face-alist
851   '(("From" nil gnus-header-from)
852     ("Subject" nil gnus-header-subject)
853     ("Newsgroups:.*," nil gnus-header-newsgroups)
854     ("" gnus-header-name gnus-header-content))
855   "*Controls highlighting of article headers.
856
857 An alist of the form (HEADER NAME CONTENT).
858
859 HEADER is a regular expression which should match the name of a
860 header and NAME and CONTENT are either face names or nil.
861
862 The name of each header field will be displayed using the face
863 specified by the first element in the list where HEADER matches
864 the header name and NAME is non-nil.  Similarly, the content will
865 be displayed by the first non-nil matching CONTENT face."
866   :group 'gnus-article-headers
867   :group 'gnus-article-highlight
868   :type '(repeat (list (regexp :tag "Header")
869                        (choice :tag "Name"
870                                (item :tag "skip" nil)
871                                (face :value default))
872                        (choice :tag "Content"
873                                (item :tag "skip" nil)
874                                (face :value default)))))
875
876 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
877                                           '((xface . (:face gnus-x-face)))
878                                         '((pbm . (:face gnus-x-face))
879                                           (png . nil)))
880   "Alist of image types and properties applied to Face and X-Face images.
881 Here are examples:
882
883 ;; Specify the altitude of Face images in the From header.
884 \(setq gnus-face-properties-alist
885       '((pbm . (:face gnus-x-face :ascent 80))
886         (png . (:ascent 80))))
887
888 ;; Show Face images as pressed buttons.
889 \(setq gnus-face-properties-alist
890       '((pbm . (:face gnus-x-face :relief -2))
891         (png . (:relief -2))))
892
893 See the manual for the valid properties for various image types.
894 Currently, `pbm' is used for X-Face images and `png' is used for Face
895 images in Emacs.  Only the `:face' property is effective on the `xface'
896 image type in XEmacs if it is built with the libcompface library."
897   :version "23.1" ;; No Gnus
898   :group 'gnus-article-headers
899   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
900
901 (defcustom gnus-article-decode-hook
902   '(article-decode-charset article-decode-encoded-words
903                            article-decode-group-name article-decode-idna-rhs)
904   "*Hook run to decode charsets in articles."
905   :group 'gnus-article-headers
906   :type 'hook)
907
908 (defcustom gnus-display-mime-function 'gnus-display-mime
909   "Function to display MIME articles."
910   :group 'gnus-article-mime
911   :type 'function)
912
913 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
914   "Function used to decode headers.")
915
916 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
917   "Function used to decode addresses.")
918
919 (defvar gnus-article-dumbquotes-map
920   '((?\200 "EUR")
921     (?\202 ",")
922     (?\203 "f")
923     (?\204 ",,")
924     (?\205 "...")
925     (?\213 "<")
926     (?\214 "OE")
927     (?\221 "`")
928     (?\222 "'")
929     (?\223 "``")
930     (?\224 "\"")
931     (?\225 "*")
932     (?\226 "-")
933     (?\227 "--")
934     (?\230 "~")
935     (?\231 "(TM)")
936     (?\233 ">")
937     (?\234 "oe")
938     (?\264 "'"))
939   "Table for MS-to-Latin1 translation.")
940
941 (defcustom gnus-ignored-mime-types nil
942   "List of MIME types that should be ignored by Gnus."
943   :version "21.1"
944   :group 'gnus-article-mime
945   :type '(repeat regexp))
946
947 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
948   "List of MIME types that should not be given buttons when rendered inline.
949 See also `gnus-buttonized-mime-types' which may override this variable.
950 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
951   :version "21.1"
952   :group 'gnus-article-mime
953   :type '(repeat regexp))
954
955 (defcustom gnus-buttonized-mime-types nil
956   "List of MIME types that should be given buttons when rendered inline.
957 If set, this variable overrides `gnus-unbuttonized-mime-types'.
958 To see e.g. security buttons you could set this to
959 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
960 this list to display radio buttons that allow you to choose one of two
961 media types those mails include.  See also `mm-discouraged-alternatives'.
962 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
963   :version "22.1"
964   :group 'gnus-article-mime
965   :type '(repeat regexp))
966
967 (defcustom gnus-inhibit-mime-unbuttonizing nil
968   "If non-nil, all MIME parts get buttons.
969 When nil (the default value), then some MIME parts do not get buttons,
970 as described by the variables `gnus-buttonized-mime-types' and
971 `gnus-unbuttonized-mime-types'."
972   :version "22.1"
973   :group 'gnus-article-mime
974   :type 'boolean)
975
976 (defcustom gnus-body-boundary-delimiter "_"
977   "String used to delimit header and body.
978 This variable is used by `gnus-article-treat-body-boundary' which can
979 be controlled by `gnus-treat-body-boundary'."
980   :version "22.1"
981   :group 'gnus-article-various
982   :type '(choice (item :tag "None" :value nil)
983                  string))
984
985 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
986                                   "/usr/share/picons")
987   "Defines the location of the faces database.
988 For information on obtaining this database of pretty pictures, please
989 see http://www.cs.indiana.edu/picons/ftp/index.html"
990   :version "22.1"
991   :type '(repeat directory)
992   :link '(url-link :tag "download"
993                    "http://www.cs.indiana.edu/picons/ftp/index.html")
994   :link '(custom-manual "(gnus)Picons")
995   :group 'gnus-picon)
996
997 (defun gnus-picons-installed-p ()
998   "Say whether picons are installed on your machine."
999   (let ((installed nil))
1000     (dolist (database gnus-picon-databases)
1001       (when (file-exists-p database)
1002         (setq installed t)))
1003     installed))
1004
1005 (defcustom gnus-article-mime-part-function nil
1006   "Function called with a MIME handle as the argument.
1007 This is meant for people who want to do something automatic based
1008 on parts -- for instance, adding Vcard info to a database."
1009   :group 'gnus-article-mime
1010   :type '(choice (const nil)
1011                  function))
1012
1013 (defcustom gnus-mime-multipart-functions nil
1014   "An alist of MIME types to functions to display them."
1015   :version "21.1"
1016   :group 'gnus-article-mime
1017   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1018
1019 (defcustom gnus-article-date-headers '(combined-lapsed)
1020   "A list of Date header formats to display.
1021 Valid formats are `ut' (universal time), `local' (local time
1022 zone), `english' (readable English), `lapsed' (elapsed time),
1023 `combined-lapsed' (both the original date and the elapsed time),
1024 `original' (the original date header), `iso8601' (ISO8601
1025 format), and `user-defined' (a user-defined format defined by the
1026 `gnus-article-time-format' variable).
1027
1028 You have as many date headers as you want in the article buffer.
1029 Some of these headers are updated automatically.  See
1030 `gnus-article-update-date-headers' for details."
1031   :version "24.1"
1032   :group 'gnus-article-headers
1033   :type '(repeat
1034           (item :tag "Universal time (UT)" :value 'ut)
1035           (item :tag "Local time zone" :value 'local)
1036           (item :tag "Readable English" :value 'english)
1037           (item :tag "Elapsed time" :value 'lapsed)
1038           (item :tag "Original and elapsed time" :value 'combined-lapsed)
1039           (item :tag "Original date header" :value 'original)
1040           (item :tag "ISO8601 format" :value 'iso8601)
1041           (item :tag "User-defined" :value 'user-defined)))
1042
1043 (defcustom gnus-article-update-date-headers 1
1044   "A number that says how often to update the date header (in seconds).
1045 If nil, don't update it at all."
1046   :version "24.1"
1047   :group 'gnus-article-headers
1048   :type '(choice
1049           (item :tag "Don't update" :value nil)
1050           integer))
1051
1052 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1053   "Function called with a MIME handle as the argument.
1054 This is meant for people who want to view first matched part.
1055 For `undisplayed-alternative' (default), the first undisplayed
1056 part or alternative part is used.  For `undisplayed', the first
1057 undisplayed part is used.  For a function, the first part which
1058 the function return t is used.  For nil, the first part is
1059 used."
1060   :version "21.1"
1061   :group 'gnus-article-mime
1062   :type '(choice
1063           (item :tag "first" :value nil)
1064           (item :tag "undisplayed" :value undisplayed)
1065           (item :tag "undisplayed or alternative"
1066                 :value undisplayed-alternative)
1067           (function)))
1068
1069 (defcustom gnus-mime-action-alist
1070   '(("save to file" . gnus-mime-save-part)
1071     ("save and strip" . gnus-mime-save-part-and-strip)
1072     ("replace with file" . gnus-mime-replace-part)
1073     ("delete part" . gnus-mime-delete-part)
1074     ("display as text" . gnus-mime-inline-part)
1075     ("view the part" . gnus-mime-view-part)
1076     ("pipe to command" . gnus-mime-pipe-part)
1077     ("toggle display" . gnus-article-press-button)
1078     ("toggle display" . gnus-article-view-part-as-charset)
1079     ("view as type" . gnus-mime-view-part-as-type)
1080     ("view internally" . gnus-mime-view-part-internally)
1081     ("view externally" . gnus-mime-view-part-externally))
1082   "An alist of actions that run on the MIME attachment."
1083   :group 'gnus-article-mime
1084   :type '(repeat (cons (string :tag "name")
1085                        (function))))
1086
1087 (defcustom gnus-auto-select-part 1
1088   "Advance to next MIME part when deleting or stripping parts.
1089
1090 When 0, point will be placed on the same part as before.  When
1091 positive (negative), move point forward (backwards) this many
1092 parts.  When nil, redisplay article."
1093   :version "23.1" ;; No Gnus
1094   :group 'gnus-article-mime
1095   :type '(choice (const nil :tag "Redisplay article.")
1096                  (const 1 :tag "Next part.")
1097                  (const 0 :tag "Current part.")
1098                  integer))
1099
1100 ;;;
1101 ;;; The treatment variables
1102 ;;;
1103
1104 (defvar gnus-part-display-hook nil
1105   "Hook called on parts that are to receive treatment.")
1106
1107 (defvar gnus-article-treat-custom
1108   '(choice (const :tag "Off" nil)
1109            (const :tag "On" t)
1110            (const :tag "Header" head)
1111            (const :tag "First" first)
1112            (const :tag "Last" last)
1113            (integer :tag "Less")
1114            (repeat :tag "Groups" regexp)
1115            (sexp :tag "Predicate")))
1116
1117 (defvar gnus-article-treat-head-custom
1118   '(choice (const :tag "Off" nil)
1119            (const :tag "Header" head)))
1120
1121 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1122                                    "text/x-patch")
1123   "Parts to treat.")
1124
1125 (defvar gnus-inhibit-treatment nil
1126   "Whether to inhibit treatment.")
1127
1128 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1129   "Highlight the signature.
1130 Valid values are nil, t, `head', `first', `last', an integer or a
1131 predicate.  See Info node `(gnus)Customizing Articles'."
1132   :group 'gnus-article-treat
1133   :link '(custom-manual "(gnus)Customizing Articles")
1134   :type gnus-article-treat-custom)
1135 (put 'gnus-treat-highlight-signature 'highlight t)
1136
1137 (defcustom gnus-treat-buttonize 100000
1138   "Add buttons.
1139 Valid values are nil, t, `head', `first', `last', an integer or a
1140 predicate.  See Info node `(gnus)Customizing Articles'."
1141   :group 'gnus-article-treat
1142   :link '(custom-manual "(gnus)Customizing Articles")
1143   :type gnus-article-treat-custom)
1144 (put 'gnus-treat-buttonize 'highlight t)
1145
1146 (defcustom gnus-treat-buttonize-head 'head
1147   "Add buttons to the head.
1148 Valid values are nil, t, `head', `first', `last', an integer or a
1149 predicate.  See Info node `(gnus)Customizing Articles'."
1150   :group 'gnus-article-treat
1151   :link '(custom-manual "(gnus)Customizing Articles")
1152   :type gnus-article-treat-head-custom)
1153 (put 'gnus-treat-buttonize-head 'highlight t)
1154
1155 (defcustom gnus-treat-date 'head
1156   "Display dates according to the `gnus-article-date-headers' variable.
1157 Valid values are nil, t, `head', `first', `last', an integer or a
1158 predicate.  See Info node `(gnus)Customizing Articles'."
1159   :version "24.1"
1160   :group 'gnus-article-treat
1161   :link '(custom-manual "(gnus)Customizing Articles")
1162   :type gnus-article-treat-head-custom)
1163
1164 (defcustom gnus-treat-emphasize 50000
1165   "Emphasize text.
1166 Valid values are nil, t, `head', `first', `last', an integer or a
1167 predicate.  See Info node `(gnus)Customizing Articles'."
1168   :group 'gnus-article-treat
1169   :link '(custom-manual "(gnus)Customizing Articles")
1170   :type gnus-article-treat-custom)
1171 (put 'gnus-treat-emphasize 'highlight t)
1172
1173 (defcustom gnus-treat-strip-cr nil
1174   "Remove carriage returns.
1175 Valid values are nil, t, `head', `first', `last', an integer or a
1176 predicate.  See Info node `(gnus)Customizing Articles'."
1177   :version "22.1"
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-custom)
1181
1182 (defcustom gnus-treat-unsplit-urls nil
1183   "Remove newlines from within URLs.
1184 Valid values are nil, t, `head', `first', `last', an integer or a
1185 predicate.  See Info node `(gnus)Customizing Articles'."
1186   :version "22.1"
1187   :group 'gnus-article-treat
1188   :link '(custom-manual "(gnus)Customizing Articles")
1189   :type gnus-article-treat-custom)
1190
1191 (defcustom gnus-treat-leading-whitespace nil
1192   "Remove leading whitespace in headers.
1193 Valid values are nil, t, `head', `first', `last', an integer or a
1194 predicate.  See Info node `(gnus)Customizing Articles'."
1195   :version "22.1"
1196   :group 'gnus-article-treat
1197   :link '(custom-manual "(gnus)Customizing Articles")
1198   :type gnus-article-treat-custom)
1199
1200 (defcustom gnus-treat-hide-headers 'head
1201   "Hide headers.
1202 Valid values are nil, t, `head', `first', `last', an integer or a
1203 predicate.  See Info node `(gnus)Customizing Articles'."
1204   :group 'gnus-article-treat
1205   :link '(custom-manual "(gnus)Customizing Articles")
1206   :type gnus-article-treat-head-custom)
1207
1208 (defcustom gnus-treat-hide-boring-headers nil
1209   "Hide boring headers.
1210 Valid values are nil, t, `head', `first', `last', an integer or a
1211 predicate.  See Info node `(gnus)Customizing Articles'."
1212   :group 'gnus-article-treat
1213   :link '(custom-manual "(gnus)Customizing Articles")
1214   :type gnus-article-treat-head-custom)
1215
1216 (defcustom gnus-treat-hide-signature nil
1217   "Hide the signature.
1218 Valid values are nil, t, `head', `first', `last', an integer or a
1219 predicate.  See Info node `(gnus)Customizing Articles'."
1220   :group 'gnus-article-treat
1221   :link '(custom-manual "(gnus)Customizing Articles")
1222   :type gnus-article-treat-custom)
1223
1224 (defcustom gnus-treat-fill-article nil
1225   "Fill the article.
1226 Valid values are nil, t, `head', `first', `last', an integer or a
1227 predicate.  See Info node `(gnus)Customizing Articles'."
1228   :group 'gnus-article-treat
1229   :link '(custom-manual "(gnus)Customizing Articles")
1230   :type gnus-article-treat-custom)
1231
1232 (defcustom gnus-treat-hide-citation nil
1233   "Hide cited text.
1234 Valid values are nil, t, `head', `first', `last', an integer or a
1235 predicate.  See Info node `(gnus)Customizing Articles'."
1236   :group 'gnus-article-treat
1237   :link '(custom-manual "(gnus)Customizing Articles")
1238   :type gnus-article-treat-custom)
1239
1240 (defcustom gnus-treat-hide-citation-maybe nil
1241   "Hide cited text.
1242 Valid values are nil, t, `head', `first', `last', an integer or a
1243 predicate.  See Info node `(gnus)Customizing Articles'."
1244   :group 'gnus-article-treat
1245   :link '(custom-manual "(gnus)Customizing Articles")
1246   :type gnus-article-treat-custom)
1247
1248 (defcustom gnus-treat-strip-list-identifiers 'head
1249   "Strip list identifiers from `gnus-list-identifiers`.
1250 Valid values are nil, t, `head', `first', `last', an integer or a
1251 predicate.  See Info node `(gnus)Customizing Articles'."
1252   :version "21.1"
1253   :group 'gnus-article-treat
1254   :link '(custom-manual "(gnus)Customizing Articles")
1255   :type gnus-article-treat-custom)
1256
1257 (gnus-define-group-parameter
1258  list-identifier
1259  :variable-document
1260  "Alist of regexps and correspondent identifiers."
1261  :variable-group gnus-article-washing
1262  :parameter-type
1263  '(choice :tag "Identifier"
1264           :value nil
1265           (symbol :tag "Item in `gnus-list-identifiers'" none)
1266           regexp
1267           (const :tag "None" nil))
1268  :parameter-document
1269  "If non-nil, specify how to remove `identifiers' from articles' subject.
1270
1271 Any symbol is used to look up a regular expression to match the
1272 banner in `gnus-list-identifiers'.  A string is used as a regular
1273 expression to match the identifier directly.")
1274
1275 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1276                         "Gnus 5.10 (Emacs 22.1)")
1277
1278 (defcustom gnus-treat-strip-pem nil
1279   "Strip PEM signatures.
1280 Valid values are nil, t, `head', `first', `last', an integer or a
1281 predicate.  See Info node `(gnus)Customizing Articles'."
1282   :group 'gnus-article-treat
1283   :link '(custom-manual "(gnus)Customizing Articles")
1284   :type gnus-article-treat-custom)
1285
1286 (defcustom gnus-treat-strip-banner t
1287   "Strip banners from articles.
1288 The banner to be stripped is specified in the `banner' group parameter.
1289 Valid values are nil, t, `head', `first', `last', an integer or a
1290 predicate.  See Info node `(gnus)Customizing Articles'."
1291   :group 'gnus-article-treat
1292   :link '(custom-manual "(gnus)Customizing Articles")
1293   :type gnus-article-treat-custom)
1294
1295 (defcustom gnus-treat-highlight-headers 'head
1296   "Highlight the headers.
1297 Valid values are nil, t, `head', `first', `last', an integer or a
1298 predicate.  See Info node `(gnus)Customizing Articles'."
1299   :group 'gnus-article-treat
1300   :link '(custom-manual "(gnus)Customizing Articles")
1301   :type gnus-article-treat-head-custom)
1302 (put 'gnus-treat-highlight-headers 'highlight t)
1303
1304 (defcustom gnus-treat-highlight-citation t
1305   "Highlight cited text.
1306 Valid values are nil, t, `head', `first', `last', an integer or a
1307 predicate.  See Info node `(gnus)Customizing Articles'."
1308   :group 'gnus-article-treat
1309   :link '(custom-manual "(gnus)Customizing Articles")
1310   :type gnus-article-treat-custom)
1311 (put 'gnus-treat-highlight-citation 'highlight t)
1312
1313 (defcustom gnus-treat-strip-headers-in-body t
1314   "Strip the X-No-Archive header line from the beginning of the body.
1315 Valid values are nil, t, `head', `first', `last', an integer or a
1316 predicate.  See Info node `(gnus)Customizing Articles'."
1317   :version "21.1"
1318   :group 'gnus-article-treat
1319   :link '(custom-manual "(gnus)Customizing Articles")
1320   :type gnus-article-treat-custom)
1321
1322 (defcustom gnus-treat-strip-trailing-blank-lines nil
1323   "Strip trailing blank lines.
1324 Valid values are nil, t, `head', `first', `last', an integer or a
1325 predicate.  See Info node `(gnus)Customizing Articles'.
1326
1327 When set to t, it also strips trailing blanks in all MIME parts.
1328 Consider to use `last' instead."
1329   :group 'gnus-article-treat
1330   :link '(custom-manual "(gnus)Customizing Articles")
1331   :type gnus-article-treat-custom)
1332
1333 (defcustom gnus-treat-strip-leading-blank-lines nil
1334   "Strip leading blank lines.
1335 Valid values are nil, t, `head', `first', `last', an integer or a
1336 predicate.  See Info node `(gnus)Customizing Articles'.
1337
1338 When set to t, it also strips trailing blanks in all MIME parts."
1339   :group 'gnus-article-treat
1340   :link '(custom-manual "(gnus)Customizing Articles")
1341   :type gnus-article-treat-custom)
1342
1343 (defcustom gnus-treat-strip-multiple-blank-lines nil
1344   "Strip multiple blank lines.
1345 Valid values are nil, t, `head', `first', `last', an integer or a
1346 predicate.  See Info node `(gnus)Customizing Articles'."
1347   :group 'gnus-article-treat
1348   :link '(custom-manual "(gnus)Customizing Articles")
1349   :type gnus-article-treat-custom)
1350
1351 (defcustom gnus-treat-unfold-headers 'head
1352   "Unfold folded header lines.
1353 Valid values are nil, t, `head', `first', `last', an integer or a
1354 predicate.  See Info node `(gnus)Customizing Articles'."
1355   :version "22.1"
1356   :group 'gnus-article-treat
1357   :link '(custom-manual "(gnus)Customizing Articles")
1358   :type gnus-article-treat-custom)
1359
1360 (defcustom gnus-article-unfold-long-headers nil
1361   "If non-nil, allow unfolding headers even if the header is long.
1362 If it is a regexp, only long headers matching this regexp are unfolded.
1363 If it is t, all long headers are unfolded.
1364
1365 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1366   :version "23.1" ;; No Gnus
1367   :group 'gnus-article-treat
1368   :type '(choice (const nil)
1369                  (const :tag "all" t)
1370                  (regexp)))
1371
1372 (defcustom gnus-treat-fold-headers nil
1373   "Fold headers.
1374 Valid values are nil, t, `head', `first', `last', an integer or a
1375 predicate.  See Info node `(gnus)Customizing Articles'."
1376   :version "22.1"
1377   :group 'gnus-article-treat
1378   :link '(custom-manual "(gnus)Customizing Articles")
1379   :type gnus-article-treat-custom)
1380
1381 (defcustom gnus-treat-fold-newsgroups 'head
1382   "Fold the Newsgroups and Followup-To headers.
1383 Valid values are nil, t, `head', `first', `last', an integer or a
1384 predicate.  See Info node `(gnus)Customizing Articles'."
1385   :version "22.1"
1386   :group 'gnus-article-treat
1387   :link '(custom-manual "(gnus)Customizing Articles")
1388   :type gnus-article-treat-custom)
1389
1390 (defcustom gnus-treat-overstrike t
1391   "Treat overstrike highlighting.
1392 Valid values are nil, t, `head', `first', `last', an integer or a
1393 predicate.  See Info node `(gnus)Customizing Articles'."
1394   :group 'gnus-article-treat
1395   :link '(custom-manual "(gnus)Customizing Articles")
1396   :type gnus-article-treat-custom)
1397 (put 'gnus-treat-overstrike 'highlight t)
1398
1399 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1400   "Treat ANSI SGR control sequences.
1401 Valid values are nil, t, `head', `first', `last', an integer or a
1402 predicate.  See Info node `(gnus)Customizing Articles'."
1403   :group 'gnus-article-treat
1404   :link '(custom-manual "(gnus)Customizing Articles")
1405   :type gnus-article-treat-custom)
1406
1407 (make-obsolete-variable 'gnus-treat-display-xface
1408                         'gnus-treat-display-x-face "Emacs 22.1")
1409
1410 (defcustom gnus-treat-display-x-face
1411   (and (not noninteractive)
1412        (gnus-image-type-available-p 'xbm)
1413        (if (featurep 'xemacs)
1414            (featurep 'xface)
1415          (condition-case nil
1416              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1417                   (executable-find "icontopbm"))
1418            ;; shell-command-to-string may signal an error, e.g. if
1419            ;; shell-file-name is not found.
1420            (error nil)))
1421        'head)
1422   "Display X-Face headers.
1423 Valid values are nil and `head'.
1424 See Info node `(gnus)Customizing Articles' and Info node
1425 `(gnus)X-Face' for details."
1426   :group 'gnus-article-treat
1427   :version "21.1"
1428   :link '(custom-manual "(gnus)Customizing Articles")
1429   :link '(custom-manual "(gnus)X-Face")
1430   :type gnus-article-treat-head-custom
1431   :set (lambda (symbol value)
1432          (set-default
1433           symbol
1434           (cond ((or (boundp symbol) (get symbol 'saved-value))
1435                  value)
1436                 ((boundp 'gnus-treat-display-xface)
1437                  (message "\
1438 ** gnus-treat-display-xface is an obsolete variable;\
1439  use gnus-treat-display-x-face instead")
1440                  (default-value 'gnus-treat-display-xface))
1441                 ((get 'gnus-treat-display-xface 'saved-value)
1442                  (message "\
1443 ** gnus-treat-display-xface is an obsolete variable;\
1444  use gnus-treat-display-x-face instead")
1445                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1446                 (t
1447                  value)))))
1448 (put 'gnus-treat-display-x-face 'highlight t)
1449
1450 (defcustom gnus-treat-display-face
1451   (and (not noninteractive)
1452        (gnus-image-type-available-p 'png)
1453        'head)
1454   "Display Face headers.
1455 Valid values are nil, t, `head', `first', `last', an integer or a
1456 predicate.  See Info node `(gnus)Customizing Articles' and Info
1457 node `(gnus)Face' for details."
1458   :group 'gnus-article-treat
1459   :version "22.1"
1460   :link '(custom-manual "(gnus)Customizing Articles")
1461   :link '(custom-manual "(gnus)X-Face")
1462   :type gnus-article-treat-head-custom)
1463 (put 'gnus-treat-display-face 'highlight t)
1464
1465 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1466   "Display smileys.
1467 Valid values are nil, t, `head', `first', `last', an integer or a
1468 predicate.  See Info node `(gnus)Customizing Articles' and Info
1469 node `(gnus)Smileys' for details."
1470   :group 'gnus-article-treat
1471   :version "21.1"
1472   :link '(custom-manual "(gnus)Customizing Articles")
1473   :link '(custom-manual "(gnus)Smileys")
1474   :type gnus-article-treat-custom)
1475 (put 'gnus-treat-display-smileys 'highlight t)
1476
1477 (defcustom gnus-treat-from-picon
1478   (if (and (gnus-image-type-available-p 'xpm)
1479            (gnus-picons-installed-p))
1480       'head nil)
1481   "Display picons in the From header.
1482 Valid values are nil, t, `head', `first', `last', an integer or a
1483 predicate.  See Info node `(gnus)Customizing Articles' and Info
1484 node `(gnus)Picons' for details."
1485   :version "22.1"
1486   :group 'gnus-article-treat
1487   :group 'gnus-picon
1488   :link '(custom-manual "(gnus)Customizing Articles")
1489   :link '(custom-manual "(gnus)Picons")
1490   :type gnus-article-treat-head-custom)
1491 (put 'gnus-treat-from-picon 'highlight t)
1492
1493 (defcustom gnus-treat-mail-picon
1494   (if (and (gnus-image-type-available-p 'xpm)
1495            (gnus-picons-installed-p))
1496       'head nil)
1497   "Display picons in To and Cc headers.
1498 Valid values are nil, t, `head', `first', `last', an integer or a
1499 predicate.  See Info node `(gnus)Customizing Articles' and Info
1500 node `(gnus)Picons' for details."
1501   :version "22.1"
1502   :group 'gnus-article-treat
1503   :group 'gnus-picon
1504   :link '(custom-manual "(gnus)Customizing Articles")
1505   :link '(custom-manual "(gnus)Picons")
1506   :type gnus-article-treat-head-custom)
1507 (put 'gnus-treat-mail-picon 'highlight t)
1508
1509 (defcustom gnus-treat-newsgroups-picon
1510   (if (and (gnus-image-type-available-p 'xpm)
1511            (gnus-picons-installed-p))
1512       'head nil)
1513   "Display picons in the Newsgroups and Followup-To headers.
1514 Valid values are nil, t, `head', `first', `last', an integer or a
1515 predicate.  See Info node `(gnus)Customizing Articles' and Info
1516 node `(gnus)Picons' for details."
1517   :version "22.1"
1518   :group 'gnus-article-treat
1519   :group 'gnus-picon
1520   :link '(custom-manual "(gnus)Customizing Articles")
1521   :link '(custom-manual "(gnus)Picons")
1522   :type gnus-article-treat-head-custom)
1523 (put 'gnus-treat-newsgroups-picon 'highlight t)
1524
1525 (defcustom gnus-treat-from-gravatar nil
1526   "Display gravatars in the From header.
1527 Valid values are nil, t, `head', `first', `last', an integer or a
1528 predicate.  See Info node `(gnus)Customizing Articles' and Info
1529 node `(gnus)Gravatars' for details."
1530   :version "24.1"
1531   :group 'gnus-article-treat
1532   :group 'gnus-gravatar
1533   :link '(custom-manual "(gnus)Customizing Articles")
1534   :link '(custom-manual "(gnus)Gravatars")
1535   :type gnus-article-treat-head-custom)
1536 (put 'gnus-treat-from-gravatar 'highlight t)
1537
1538 (defcustom gnus-treat-mail-gravatar nil
1539   "Display gravatars in To and Cc headers.
1540 Valid values are nil, t, `head', `first', `last', an integer or a
1541 predicate.  See Info node `(gnus)Customizing Articles' and Info
1542 node `(gnus)Gravatars' for details."
1543   :version "24.1"
1544   :group 'gnus-article-treat
1545   :group 'gnus-gravatar
1546   :link '(custom-manual "(gnus)Customizing Articles")
1547   :link '(custom-manual "(gnus)Gravatars")
1548   :type gnus-article-treat-head-custom)
1549 (put 'gnus-treat-mail-gravatar 'highlight t)
1550
1551 (defcustom gnus-treat-body-boundary
1552   (if (or gnus-treat-newsgroups-picon
1553           gnus-treat-mail-picon
1554           gnus-treat-from-picon
1555           gnus-treat-from-gravatar
1556           gnus-treat-mail-gravatar)
1557       ;; If there's much decoration, the user might prefer a boundery.
1558       'head
1559     nil)
1560   "Draw a boundary at the end of the headers.
1561 Valid values are nil and `head'.
1562 See Info node `(gnus)Customizing Articles' for details."
1563   :version "22.1"
1564   :group 'gnus-article-treat
1565   :link '(custom-manual "(gnus)Customizing Articles")
1566   :type gnus-article-treat-head-custom)
1567
1568 (defcustom gnus-treat-capitalize-sentences nil
1569   "Capitalize sentence-starting words.
1570 Valid values are nil, t, `head', `first', `last', an integer or a
1571 predicate.  See Info node `(gnus)Customizing Articles'."
1572   :version "21.1"
1573   :group 'gnus-article-treat
1574   :link '(custom-manual "(gnus)Customizing Articles")
1575   :type gnus-article-treat-custom)
1576
1577 (defcustom gnus-treat-wash-html nil
1578   "Format as HTML.
1579 Valid values are nil, t, `head', `first', `last', an integer or a
1580 predicate.  See Info node `(gnus)Customizing Articles'."
1581   :version "22.1"
1582   :group 'gnus-article-treat
1583   :link '(custom-manual "(gnus)Customizing Articles")
1584   :type gnus-article-treat-custom)
1585
1586 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1587   "Fill long lines.
1588 Valid values are nil, t, `head', `first', `last', an integer or a
1589 predicate.  See Info node `(gnus)Customizing Articles'."
1590   :version "24.1"
1591   :group 'gnus-article-treat
1592   :link '(custom-manual "(gnus)Customizing Articles")
1593   :type gnus-article-treat-custom)
1594
1595 (defcustom gnus-treat-x-pgp-sig nil
1596   "Verify X-PGP-Sig.
1597 To automatically treat X-PGP-Sig, set it to head.
1598 Valid values are nil, t, `head', `first', `last', an integer or a
1599 predicate.  See Info node `(gnus)Customizing Articles'."
1600   :version "22.1"
1601   :group 'gnus-article-treat
1602   :group 'mime-security
1603   :link '(custom-manual "(gnus)Customizing Articles")
1604   :type gnus-article-treat-custom)
1605
1606 (defvar gnus-article-encrypt-protocol-alist
1607   '(("PGP" . mml2015-self-encrypt)))
1608
1609 ;; Set to nil if more than one protocol added to
1610 ;; gnus-article-encrypt-protocol-alist.
1611 (defcustom gnus-article-encrypt-protocol "PGP"
1612   "The protocol used for encrypt articles.
1613 It is a string, such as \"PGP\". If nil, ask user."
1614   :version "22.1"
1615   :type 'string
1616   :group 'mime-security)
1617
1618 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1619                               (mm-coding-system-p 'utf-8)
1620                               (executable-find idna-program))
1621   "Whether IDNA decoding of headers is used when viewing messages.
1622 This requires GNU Libidn, and by default only enabled if it is found."
1623   :version "22.1"
1624   :group 'gnus-article-headers
1625   :type 'boolean)
1626
1627 (defcustom gnus-article-over-scroll nil
1628   "If non-nil, allow scrolling the article buffer even when there no more text."
1629   :version "22.1"
1630   :group 'gnus-article
1631   :type 'boolean)
1632
1633 (defcustom gnus-inhibit-images nil
1634   "Non-nil means inhibit displaying of images inline in the article body."
1635   :version "24.1"
1636   :group 'gnus-article
1637   :type 'boolean)
1638
1639 (defcustom gnus-blocked-images 'gnus-block-private-groups
1640   "Images that have URLs matching this regexp will be blocked.
1641 This can also be a function to be evaluated.  If so, it will be
1642 called with the group name as the parameter, and should return a
1643 regexp."
1644   :version "24.1"
1645   :group 'gnus-art
1646   :type 'regexp)
1647
1648 ;;; Internal variables
1649
1650 (defvar gnus-english-month-names
1651   '("January" "February" "March" "April" "May" "June" "July" "August"
1652     "September" "October" "November" "December"))
1653
1654 (defvar article-goto-body-goes-to-point-min-p nil)
1655 (defvar gnus-article-wash-types nil)
1656 (defvar gnus-article-emphasis-alist nil)
1657 (defvar gnus-article-image-alist nil)
1658
1659 (defvar gnus-article-mime-handle-alist-1 nil)
1660 (defvar gnus-treatment-function-alist
1661   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1662     (gnus-treat-strip-banner gnus-article-strip-banner)
1663     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1664     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1665     (gnus-treat-buttonize gnus-article-add-buttons)
1666     (gnus-treat-fill-article gnus-article-fill-cited-article)
1667     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1668     (gnus-treat-strip-cr gnus-article-remove-cr)
1669     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1670     (gnus-treat-display-x-face gnus-article-display-x-face)
1671     (gnus-treat-display-face gnus-article-display-face)
1672     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1673     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1674     (gnus-treat-hide-signature gnus-article-hide-signature)
1675     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1676     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1677     (gnus-treat-from-picon gnus-treat-from-picon)
1678     (gnus-treat-mail-picon gnus-treat-mail-picon)
1679     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1680     (gnus-treat-strip-pem gnus-article-hide-pem)
1681     (gnus-treat-date gnus-article-treat-date)
1682     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1683     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1684     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1685     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1686     (gnus-treat-strip-trailing-blank-lines
1687      gnus-article-remove-trailing-blank-lines)
1688     (gnus-treat-strip-leading-blank-lines
1689      gnus-article-strip-leading-blank-lines)
1690     (gnus-treat-strip-multiple-blank-lines
1691      gnus-article-strip-multiple-blank-lines)
1692     (gnus-treat-overstrike gnus-article-treat-overstrike)
1693     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1694     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1695     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1696     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1697     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1698     (gnus-treat-display-smileys gnus-treat-smiley)
1699     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1700     (gnus-treat-wash-html gnus-article-wash-html)
1701     (gnus-treat-emphasize gnus-article-emphasize)
1702     (gnus-treat-hide-citation gnus-article-hide-citation)
1703     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1704     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1705     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1706
1707 (defvar gnus-article-mime-handle-alist nil)
1708 (defvar article-lapsed-timer nil)
1709 (defvar gnus-article-current-summary nil)
1710
1711 (defvar gnus-article-mode-syntax-table
1712   (let ((table (copy-syntax-table text-mode-syntax-table)))
1713     ;; This causes the citation match run O(2^n).
1714     ;; (modify-syntax-entry ?- "w" table)
1715     (modify-syntax-entry ?> ")<" table)
1716     (modify-syntax-entry ?< "(>" table)
1717     ;; make M-. in article buffers work for `foo' strings
1718     (modify-syntax-entry ?' " " table)
1719     (modify-syntax-entry ?` " " table)
1720     table)
1721   "Syntax table used in article mode buffers.
1722 Initialized from `text-mode-syntax-table.")
1723
1724 (defvar gnus-save-article-buffer nil)
1725
1726 (defvar gnus-number-of-articles-to-be-saved nil)
1727
1728 (defvar gnus-inhibit-hiding nil)
1729
1730 (defvar gnus-article-edit-mode nil)
1731
1732 ;;; Macros for dealing with the article buffer.
1733
1734 (defmacro gnus-with-article-headers (&rest forms)
1735   `(with-current-buffer gnus-article-buffer
1736      (save-restriction
1737        (let ((inhibit-read-only t)
1738              (inhibit-point-motion-hooks t)
1739              (case-fold-search t))
1740          (article-narrow-to-head)
1741          ,@forms))))
1742
1743 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1744 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1745
1746 (defmacro gnus-with-article-buffer (&rest forms)
1747   `(when (buffer-live-p (get-buffer gnus-article-buffer))
1748      (with-current-buffer gnus-article-buffer
1749        (let ((inhibit-read-only t))
1750          ,@forms))))
1751
1752 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1753 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1754
1755 (defun gnus-article-goto-header (header)
1756   "Go to HEADER, which is a regular expression."
1757   (re-search-forward (concat "^\\(" header "\\):") nil t))
1758
1759 (defsubst gnus-article-hide-text (b e props)
1760   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1761   (gnus-add-text-properties-when 'article-type nil b e props)
1762   (when (memq 'intangible props)
1763     (put-text-property
1764      (max (1- b) (point-min))
1765      b 'intangible (cddr (memq 'intangible props)))))
1766
1767 (defsubst gnus-article-unhide-text (b e)
1768   "Remove hidden text properties from region between B and E."
1769   (remove-text-properties b e gnus-hidden-properties)
1770   (when (memq 'intangible gnus-hidden-properties)
1771     (put-text-property (max (1- b) (point-min))
1772                        b 'intangible nil)))
1773
1774 (defun gnus-article-hide-text-type (b e type)
1775   "Hide text of TYPE between B and E."
1776   (gnus-add-wash-type type)
1777   (gnus-article-hide-text
1778    b e (cons 'article-type (cons type gnus-hidden-properties))))
1779
1780 (defun gnus-article-unhide-text-type (b e type)
1781   "Unhide text of TYPE between B and E."
1782   (gnus-delete-wash-type type)
1783   (remove-text-properties
1784    b e (cons 'article-type (cons type gnus-hidden-properties)))
1785   (when (memq 'intangible gnus-hidden-properties)
1786     (put-text-property (max (1- b) (point-min))
1787                        b 'intangible nil)))
1788
1789 (defun gnus-article-hide-text-of-type (type)
1790   "Hide text of TYPE in the current buffer."
1791   (save-excursion
1792     (let ((b (point-min))
1793           (e (point-max)))
1794       (while (setq b (text-property-any b e 'article-type type))
1795         (add-text-properties b (incf b) gnus-hidden-properties)))))
1796
1797 (defun gnus-article-delete-text-of-type (type)
1798   "Delete text of TYPE in the current buffer."
1799   (save-excursion
1800     (let ((b (point-min)))
1801       (if (eq type 'multipart)
1802           ;; Remove MIME buttons associated with multipart/alternative parts.
1803           (progn
1804             (goto-char b)
1805             (while (if (get-text-property (point) 'gnus-part)
1806                        (setq b (point))
1807                      (when (setq b (next-single-property-change (point)
1808                                                                 'gnus-part))
1809                        (goto-char b)
1810                        t))
1811               (end-of-line)
1812               (skip-chars-forward "\n")
1813               (when (eq (get-text-property b 'article-type) 'multipart)
1814                 (delete-region b (point)))))
1815         (while (setq b (text-property-any b (point-max) 'article-type type))
1816           (delete-region
1817            b (or (text-property-not-all b (point-max) 'article-type type)
1818                  (point-max))))))))
1819
1820 (defun gnus-article-delete-invisible-text ()
1821   "Delete all invisible text in the current buffer."
1822   (save-excursion
1823     (let ((b (point-min)))
1824       (while (setq b (text-property-any b (point-max) 'invisible t))
1825         (delete-region
1826          b (or (text-property-not-all b (point-max) 'invisible t)
1827                (point-max)))))))
1828
1829 (defun gnus-article-text-type-exists-p (type)
1830   "Say whether any text of type TYPE exists in the buffer."
1831   (text-property-any (point-min) (point-max) 'article-type type))
1832
1833 (defsubst gnus-article-header-rank ()
1834   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1835   (let ((list gnus-sorted-header-list)
1836         (i 1))
1837     (while list
1838       (if (looking-at (car list))
1839           (setq list nil)
1840         (setq list (cdr list))
1841         (incf i)))
1842       i))
1843
1844 (defun article-hide-headers (&optional arg delete)
1845   "Hide unwanted headers and possibly sort them as well."
1846   (interactive)
1847   ;; This function might be inhibited.
1848   (unless gnus-inhibit-hiding
1849     (let ((inhibit-read-only t)
1850           (case-fold-search t)
1851           (max (1+ (length gnus-sorted-header-list)))
1852           (inhibit-point-motion-hooks t)
1853           (cur (current-buffer))
1854           ignored visible beg)
1855       (save-excursion
1856         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1857         ;; group parameters, so we should go to the summary buffer.
1858         (when (prog1
1859                   (condition-case nil
1860                       (progn (set-buffer gnus-summary-buffer) t)
1861                     (error nil))
1862                 (setq ignored (when (not gnus-visible-headers)
1863                                 (cond ((stringp gnus-ignored-headers)
1864                                        gnus-ignored-headers)
1865                                       ((listp gnus-ignored-headers)
1866                                        (mapconcat 'identity
1867                                                   gnus-ignored-headers
1868                                                   "\\|"))))
1869                       visible (cond ((stringp gnus-visible-headers)
1870                                      gnus-visible-headers)
1871                                     ((and gnus-visible-headers
1872                                           (listp gnus-visible-headers))
1873                                      (mapconcat 'identity
1874                                                 gnus-visible-headers
1875                                                 "\\|")))))
1876           (set-buffer cur))
1877         (save-restriction
1878           ;; First we narrow to just the headers.
1879           (article-narrow-to-head)
1880           ;; Hide any "From " lines at the beginning of (mail) articles.
1881           (while (looking-at "From ")
1882             (forward-line 1))
1883           (unless (bobp)
1884             (delete-region (point-min) (point)))
1885           ;; Then treat the rest of the header lines.
1886           ;; Then we use the two regular expressions
1887           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1888           ;; select which header lines is to remain visible in the
1889           ;; article buffer.
1890           (while (re-search-forward "^[^ \t:]*:" nil t)
1891             (beginning-of-line)
1892             ;; Mark the rank of the header.
1893             (put-text-property
1894              (point) (1+ (point)) 'message-rank
1895              (if (or (and visible (looking-at visible))
1896                      (and ignored
1897                           (not (looking-at ignored))))
1898                  (gnus-article-header-rank)
1899                (+ 2 max)))
1900             (forward-line 1))
1901           (message-sort-headers-1)
1902           (when (setq beg (text-property-any
1903                            (point-min) (point-max) 'message-rank (+ 2 max)))
1904             ;; We delete the unwanted headers.
1905             (gnus-add-wash-type 'headers)
1906             (add-text-properties (point-min) (+ 5 (point-min))
1907                                  '(article-type headers dummy-invisible t))
1908             (delete-region beg (point-max))))))))
1909
1910 (defun article-hide-boring-headers (&optional arg)
1911   "Toggle hiding of headers that aren't very interesting.
1912 If given a negative prefix, always show; if given a positive prefix,
1913 always hide."
1914   (interactive (gnus-article-hidden-arg))
1915   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1916              (not gnus-show-all-headers))
1917     (save-excursion
1918       (save-restriction
1919         (let ((inhibit-read-only t)
1920               (inhibit-point-motion-hooks t))
1921           (article-narrow-to-head)
1922           (dolist (elem gnus-boring-article-headers)
1923             (goto-char (point-min))
1924             (cond
1925              ;; Hide empty headers.
1926              ((eq elem 'empty)
1927               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1928                 (forward-line -1)
1929                 (gnus-article-hide-text-type
1930                  (point-at-bol)
1931                  (progn
1932                    (end-of-line)
1933                    (if (re-search-forward "^[^ \t]" nil t)
1934                        (match-beginning 0)
1935                      (point-max)))
1936                  'boring-headers)))
1937              ;; Hide boring Newsgroups header.
1938              ((eq elem 'newsgroups)
1939               (when (gnus-string-equal
1940                      (gnus-fetch-field "newsgroups")
1941                      (gnus-group-real-name
1942                       (if (boundp 'gnus-newsgroup-name)
1943                           gnus-newsgroup-name
1944                         "")))
1945                 (gnus-article-hide-header "newsgroups")))
1946              ((eq elem 'to-address)
1947               (let ((to (message-fetch-field "to"))
1948                     (to-address
1949                      (gnus-parameter-to-address
1950                       (if (boundp 'gnus-newsgroup-name)
1951                           gnus-newsgroup-name ""))))
1952                 (when (and to to-address
1953                            (ignore-errors
1954                              (gnus-string-equal
1955                               ;; only one address in To
1956                               (nth 1 (mail-extract-address-components to))
1957                               to-address)))
1958                   (gnus-article-hide-header "to"))))
1959              ((eq elem 'to-list)
1960               (let ((to (message-fetch-field "to"))
1961                     (to-list
1962                      (gnus-parameter-to-list
1963                       (if (boundp 'gnus-newsgroup-name)
1964                           gnus-newsgroup-name ""))))
1965                 (when (and to to-list
1966                            (ignore-errors
1967                              (gnus-string-equal
1968                               ;; only one address in To
1969                               (nth 1 (mail-extract-address-components to))
1970                               to-list)))
1971                   (gnus-article-hide-header "to"))))
1972              ((eq elem 'cc-list)
1973               (let ((cc (message-fetch-field "cc"))
1974                     (to-list
1975                      (gnus-parameter-to-list
1976                       (if (boundp 'gnus-newsgroup-name)
1977                           gnus-newsgroup-name ""))))
1978                 (when (and cc to-list
1979                            (ignore-errors
1980                              (gnus-string-equal
1981                               ;; only one address in CC
1982                               (nth 1 (mail-extract-address-components cc))
1983                               to-list)))
1984                   (gnus-article-hide-header "cc"))))
1985              ((eq elem 'followup-to)
1986               (when (gnus-string-equal
1987                      (message-fetch-field "followup-to")
1988                      (message-fetch-field "newsgroups"))
1989                 (gnus-article-hide-header "followup-to")))
1990              ((eq elem 'reply-to)
1991               (if (gnus-group-find-parameter
1992                    gnus-newsgroup-name 'broken-reply-to)
1993                   (gnus-article-hide-header "reply-to")
1994                 (let ((from (message-fetch-field "from"))
1995                       (reply-to (message-fetch-field "reply-to")))
1996                   (when
1997                       (and
1998                        from reply-to
1999                        (ignore-errors
2000                          (equal
2001                           (sort (mapcar
2002                                  (lambda (x) (downcase (cadr x)))
2003                                  (mail-extract-address-components from t))
2004                                 'string<)
2005                           (sort (mapcar
2006                                  (lambda (x) (downcase (cadr x)))
2007                                  (mail-extract-address-components reply-to t))
2008                                 'string<))))
2009                     (gnus-article-hide-header "reply-to")))))
2010              ((eq elem 'date)
2011               (let ((date (with-current-buffer gnus-original-article-buffer
2012                             ;; If date in `gnus-article-buffer' is localized
2013                             ;; (`gnus-treat-date-user-defined'),
2014                             ;; `days-between' might fail.
2015                             (message-fetch-field "date"))))
2016                 (when (and date
2017                            (< (days-between (current-time-string) date)
2018                               4))
2019                   (gnus-article-hide-header "date"))))
2020              ((eq elem 'long-to)
2021               (let ((to (message-fetch-field "to"))
2022                     (cc (message-fetch-field "cc")))
2023                 (when (> (length to) 1024)
2024                   (gnus-article-hide-header "to"))
2025                 (when (> (length cc) 1024)
2026                   (gnus-article-hide-header "cc"))))
2027              ((eq elem 'many-to)
2028               (let ((to-count 0)
2029                     (cc-count 0))
2030                 (goto-char (point-min))
2031                 (while (re-search-forward "^to:" nil t)
2032                   (setq to-count (1+ to-count)))
2033                 (when (> to-count 1)
2034                   (while (> to-count 0)
2035                     (goto-char (point-min))
2036                     (save-restriction
2037                       (re-search-forward "^to:" nil nil to-count)
2038                       (forward-line -1)
2039                       (narrow-to-region (point) (point-max))
2040                       (gnus-article-hide-header "to"))
2041                     (setq to-count (1- to-count))))
2042                 (goto-char (point-min))
2043                 (while (re-search-forward "^cc:" nil t)
2044                   (setq cc-count (1+ cc-count)))
2045                 (when (> cc-count 1)
2046                   (while (> cc-count 0)
2047                     (goto-char (point-min))
2048                     (save-restriction
2049                       (re-search-forward "^cc:" nil nil cc-count)
2050                       (forward-line -1)
2051                       (narrow-to-region (point) (point-max))
2052                       (gnus-article-hide-header "cc"))
2053                     (setq cc-count (1- cc-count)))))))))))))
2054
2055 (defun gnus-article-hide-header (header)
2056   (save-excursion
2057     (goto-char (point-min))
2058     (when (re-search-forward (concat "^" header ":") nil t)
2059       (gnus-article-hide-text-type
2060        (point-at-bol)
2061        (progn
2062          (end-of-line)
2063          (if (re-search-forward "^[^ \t]" nil t)
2064              (match-beginning 0)
2065            (point-max)))
2066        'boring-headers))))
2067
2068 (defvar gnus-article-normalized-header-length 40
2069   "Length of normalized headers.")
2070
2071 (defun article-normalize-headers ()
2072   "Make all header lines 40 characters long."
2073   (interactive)
2074   (let ((inhibit-read-only t)
2075         column)
2076     (save-excursion
2077       (save-restriction
2078         (article-narrow-to-head)
2079         (while (not (eobp))
2080           (cond
2081            ((< (setq column (- (point-at-eol) (point)))
2082                gnus-article-normalized-header-length)
2083             (end-of-line)
2084             (insert (make-string
2085                      (- gnus-article-normalized-header-length column)
2086                      ? )))
2087            ((> column gnus-article-normalized-header-length)
2088             (gnus-put-text-property
2089              (progn
2090                (forward-char gnus-article-normalized-header-length)
2091                (point))
2092              (point-at-eol)
2093              'invisible t))
2094            (t
2095             ;; Do nothing.
2096             ))
2097           (forward-line 1))))))
2098
2099 (defun article-treat-dumbquotes ()
2100   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2101 Note that this function guesses whether a character is a sm*rtq**t* or
2102 not, so it should only be used interactively.
2103
2104 Sm*rtq**t*s are M****s***'s unilateral extension to the
2105 iso-8859-1 character map in an attempt to provide more quoting
2106 characters.  If you see something like \\222 or \\264 where
2107 you're expecting some kind of apostrophe or quotation mark, then
2108 try this wash."
2109   (interactive)
2110   (article-translate-strings gnus-article-dumbquotes-map))
2111
2112 (defvar org-entities)
2113
2114 (defun article-treat-non-ascii ()
2115   "Translate many Unicode characters into their ASCII equivalents."
2116   (interactive)
2117   (require 'org-entities)
2118   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2119     (dolist (elem org-entities)
2120       (when (and (listp elem)
2121                  (= (length (nth 6 elem)) 1))
2122         (if (featurep 'xemacs)
2123             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2124           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2125     (save-excursion
2126       (when (article-goto-body)
2127         (let ((inhibit-read-only t)
2128               replace props)
2129           (while (not (eobp))
2130             (if (not (setq replace (if (featurep 'xemacs)
2131                                        (get-char-table (following-char) table)
2132                                      (aref table (following-char)))))
2133                 (forward-char 1)
2134               (if (prog1
2135                       (setq props (text-properties-at (point)))
2136                     (delete-char 1))
2137                   (add-text-properties (point) (progn (insert replace) (point))
2138                                        props)
2139                 (insert replace)))))))))
2140
2141 (defun article-translate-characters (from to)
2142   "Translate all characters in the body of the article according to FROM and TO.
2143 FROM is a string of characters to translate from; to is a string of
2144 characters to translate to."
2145   (save-excursion
2146     (when (article-goto-body)
2147       (let ((inhibit-read-only t)
2148             (x (make-string 225 ?x))
2149             (i -1))
2150         (while (< (incf i) (length x))
2151           (aset x i i))
2152         (setq i 0)
2153         (while (< i (length from))
2154           (aset x (aref from i) (aref to i))
2155           (incf i))
2156         (translate-region (point) (point-max) x)))))
2157
2158 (defun article-translate-strings (map)
2159   "Translate all string in the body of the article according to MAP.
2160 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2161   (save-excursion
2162     (when (article-goto-body)
2163       (let ((inhibit-read-only t))
2164         (dolist (elem map)
2165           (let ((from (car elem))
2166                 (to (cadr elem)))
2167             (save-excursion
2168               (if (stringp from)
2169                   (while (search-forward from nil t)
2170                     (replace-match to))
2171                 (while (not (eobp))
2172                   (if (eq (following-char) from)
2173                       (progn
2174                         (delete-char 1)
2175                         (insert to))
2176                     (forward-char 1)))))))))))
2177
2178 (defun article-treat-overstrike ()
2179   "Translate overstrikes into bold text."
2180   (interactive)
2181   (save-excursion
2182     (when (article-goto-body)
2183       (let ((inhibit-read-only t))
2184         (while (search-forward "\b" nil t)
2185           (let ((next (char-after))
2186                 (previous (char-after (- (point) 2))))
2187             ;; We do the boldification/underlining by hiding the
2188             ;; overstrikes and putting the proper text property
2189             ;; on the letters.
2190             (cond
2191              ((eq next previous)
2192               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2193               (put-text-property (point) (1+ (point)) 'face 'bold))
2194              ((eq next ?_)
2195               (gnus-article-hide-text-type
2196                (1- (point)) (1+ (point)) 'overstrike)
2197               (put-text-property
2198                (- (point) 2) (1- (point)) 'face 'underline))
2199              ((eq previous ?_)
2200               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2201               (put-text-property
2202                (point) (1+ (point)) 'face 'underline)))))))))
2203
2204 (defun article-treat-ansi-sequences ()
2205   "Translate ANSI SGR control sequences into overlays or extents."
2206   (interactive)
2207   (save-excursion
2208     (when (article-goto-body)
2209       (let ((inhibit-read-only t))
2210         (ansi-color-apply-on-region (point) (point-max))))))
2211
2212 (defun gnus-article-treat-unfold-headers ()
2213   "Unfold folded message headers.
2214 Only the headers that fit into the current window width will be
2215 unfolded."
2216   (interactive)
2217   (gnus-with-article-headers
2218     (let (length)
2219       (while (not (eobp))
2220         (save-restriction
2221           (mail-header-narrow-to-field)
2222           (let* ((header (buffer-string))
2223                  (unfoldable
2224                   (or (equal gnus-article-unfold-long-headers t)
2225                       (and (stringp gnus-article-unfold-long-headers)
2226                            (string-match gnus-article-unfold-long-headers header)))))
2227             (with-temp-buffer
2228               (insert header)
2229               (goto-char (point-min))
2230               (while (re-search-forward "\n[\t ]" nil t)
2231                 (replace-match " " t t)))
2232             (setq length (- (point-max) (point-min) 1))
2233             (when (or unfoldable
2234                       (< length (window-width)))
2235               (while (re-search-forward "\n[\t ]" nil t)
2236                 (replace-match " " t t))))
2237           (goto-char (point-max)))))))
2238
2239 (defun gnus-article-treat-fold-headers ()
2240   "Fold message headers."
2241   (interactive)
2242   (gnus-with-article-headers
2243     (while (not (eobp))
2244       (save-restriction
2245         (mail-header-narrow-to-field)
2246         (mail-header-fold-field)
2247         (goto-char (point-max))))))
2248
2249 (defun gnus-treat-smiley ()
2250   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2251   (interactive)
2252   (gnus-with-article-buffer
2253     (if (memq 'smiley gnus-article-wash-types)
2254         (gnus-delete-images 'smiley)
2255       (article-goto-body)
2256       (let ((images (smiley-region (point) (point-max))))
2257         (when images
2258           (gnus-add-wash-type 'smiley)
2259           (dolist (image images)
2260             (gnus-add-image 'smiley image)))))))
2261
2262 (defun gnus-article-remove-images ()
2263   "Remove all images from the article buffer."
2264   (interactive)
2265   (gnus-with-article-buffer
2266     (save-restriction
2267       (widen)
2268       (dolist (elem gnus-article-image-alist)
2269         (gnus-delete-images (car elem))))))
2270
2271 (defun gnus-article-show-images ()
2272   "Show any images that are in the HTML-rendered article buffer.
2273 This only works if the article in question is HTML."
2274   (interactive)
2275   (gnus-with-article-buffer
2276     (save-restriction
2277       (widen)
2278       (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2279                                                       'image-displayer))
2280         (destructuring-bind (start end function) region
2281           (funcall function (get-text-property start 'image-url)
2282                    start end))))))
2283
2284 (defun gnus-article-treat-fold-newsgroups ()
2285   "Unfold folded message headers.
2286 Only the headers that fit into the current window width will be
2287 unfolded."
2288   (interactive)
2289   (gnus-with-article-headers
2290     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2291       (save-restriction
2292         (mail-header-narrow-to-field)
2293         (while (re-search-forward ", *" nil t)
2294           (replace-match ", " t t))
2295         (mail-header-fold-field)
2296         (goto-char (point-max))))))
2297
2298 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2299   "Value of `truncate-lines' in Gnus Article buffer.
2300 Valid values are nil, t, `head', `first', `last', an integer or a
2301 predicate.  See Info node `(gnus)Customizing Articles'."
2302   :version "23.1" ;; No Gnus
2303   :group 'gnus-article
2304   ;; :link '(custom-manual "(gnus)Customizing Articles")
2305   :type 'boolean)
2306
2307 (defun gnus-article-toggle-truncate-lines (&optional arg)
2308   "Toggle whether to fold or truncate long lines in article the buffer.
2309 If ARG is non-nil and not a number, toggle
2310 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2311 long lines if and only if arg is positive."
2312   (interactive "P")
2313   (cond
2314    ((and (numberp arg) (> arg 0))
2315     (setq gnus-article-truncate-lines t))
2316    ((numberp arg)
2317     (setq gnus-article-truncate-lines nil))
2318    (arg
2319     (setq gnus-article-truncate-lines
2320           (not gnus-article-truncate-lines))))
2321   (gnus-with-article-buffer
2322     (cond
2323      ((and (numberp arg) (> arg 0))
2324       (setq truncate-lines nil))
2325      ((numberp arg)
2326       (setq truncate-lines t)))
2327     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2328     ;; `toggle-truncate-lines' needs an argument.
2329     (toggle-truncate-lines)))
2330
2331 (defun gnus-article-treat-body-boundary ()
2332   "Place a boundary line at the end of the headers."
2333   (interactive)
2334   (when (and gnus-body-boundary-delimiter
2335              (> (length gnus-body-boundary-delimiter) 0))
2336     (gnus-with-article-headers
2337       (goto-char (point-max))
2338       (let ((start (point)))
2339         (insert "X-Boundary: ")
2340         (gnus-add-text-properties start (point) '(invisible t intangible t))
2341        (insert (let (str (max (window-width)))
2342                  (if (featurep 'xemacs)
2343                      (setq max (1- max)))
2344                  (while (>= max (length str))
2345                     (setq str (concat str gnus-body-boundary-delimiter)))
2346                  (substring str 0 max))
2347                 "\n")
2348         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2349
2350 (defun article-fill-long-lines ()
2351   "Fill lines that are wider than the window width."
2352   (interactive)
2353   (save-excursion
2354     (let ((inhibit-read-only t)
2355           (width (window-width (get-buffer-window (current-buffer)))))
2356       (save-restriction
2357         (article-goto-body)
2358         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2359           (while (not (eobp))
2360             (end-of-line)
2361             (when (>= (current-column) (min fill-column width))
2362               (narrow-to-region (min (1+ (point)) (point-max))
2363                                 (point-at-bol))
2364               (let ((goback (point-marker)))
2365                 (fill-paragraph nil)
2366                 (goto-char (marker-position goback)))
2367               (widen))
2368             (forward-line 1)))))))
2369
2370 (defun article-capitalize-sentences ()
2371   "Capitalize the first word in each sentence."
2372   (interactive)
2373   (save-excursion
2374     (let ((inhibit-read-only t)
2375           (paragraph-start "^[\n\^L]"))
2376       (article-goto-body)
2377       (while (not (eobp))
2378         (capitalize-word 1)
2379         (forward-sentence)))))
2380
2381 (defun article-remove-cr ()
2382   "Remove trailing CRs and then translate remaining CRs into LFs."
2383   (interactive)
2384   (save-excursion
2385     (let ((inhibit-read-only t))
2386       (goto-char (point-min))
2387       (while (re-search-forward "\r+$" nil t)
2388         (replace-match "" t t))
2389       (goto-char (point-min))
2390       (while (search-forward "\r" nil t)
2391         (replace-match "\n" t t)))))
2392
2393 (defun article-remove-trailing-blank-lines ()
2394   "Remove all trailing blank lines from the article."
2395   (interactive)
2396   (save-excursion
2397     (let ((inhibit-read-only t))
2398       (goto-char (point-max))
2399       (delete-region
2400        (point)
2401        (progn
2402          (while (and (not (bobp))
2403                      (looking-at "^[ \t]*$")
2404                      (not (gnus-annotation-in-region-p
2405                            (point) (point-at-eol))))
2406            (forward-line -1))
2407          (forward-line 1)
2408          (point))))))
2409
2410 (defvar gnus-face-properties-alist)
2411
2412 (defun article-display-face (&optional force)
2413   "Display any Face headers in the header."
2414   (interactive (list 'force))
2415   (let ((wash-face-p buffer-read-only))
2416     (gnus-with-article-headers
2417       ;; When displaying parts, this function can be called several times on
2418       ;; the same article, without any intended toggle semantic (as typing `W
2419       ;; D d' would have). So face deletion must occur only when we come from
2420       ;; an interactive command, that is when the *Article* buffer is
2421       ;; read-only.
2422       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2423           (gnus-delete-images 'face)
2424         (let ((from (message-fetch-field "from"))
2425               face faces)
2426           (save-current-buffer
2427             (when (and wash-face-p
2428                        (gnus-buffer-live-p gnus-original-article-buffer)
2429                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2430               (set-buffer gnus-original-article-buffer))
2431             (save-restriction
2432               (mail-narrow-to-head)
2433               (when (or force
2434                         ;; Check whether this face is censored.
2435                         (not (and gnus-article-x-face-too-ugly
2436                                   (or from
2437                                       (setq from (message-fetch-field "from")))
2438                                   (string-match gnus-article-x-face-too-ugly
2439                                                 from))))
2440                 (while (gnus-article-goto-header "Face")
2441                   (push (mail-header-field-value) faces)))))
2442           (when faces
2443             (goto-char (point-min))
2444             (let (png image)
2445               (unless (setq from (gnus-article-goto-header "from"))
2446                 (insert "From:")
2447                 (setq from (point))
2448                 (insert " [no `from' set]\n"))
2449               (while faces
2450                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2451                   (setq image
2452                         (apply 'gnus-create-image png 'png t
2453                                (cdr (assq 'png gnus-face-properties-alist))))
2454                   (goto-char from)
2455                   (gnus-add-wash-type 'face)
2456                   (gnus-add-image 'face image)
2457                   (gnus-put-image image nil 'face))))))))))
2458
2459 (defun article-display-x-face (&optional force)
2460   "Look for an X-Face header and display it if present."
2461   (interactive (list 'force))
2462   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2463     (gnus-with-article-headers
2464       ;; Delete the old process, if any.
2465       (when (process-status "article-x-face")
2466         (delete-process "article-x-face"))
2467       ;; See the comment in `article-display-face'.
2468       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2469           ;; We have already displayed X-Faces, so we remove them
2470           ;; instead.
2471           (gnus-delete-images 'xface)
2472         ;; Display X-Faces.
2473         (let ((from (message-fetch-field "from"))
2474               x-faces face)
2475           (save-current-buffer
2476             (when (and wash-face-p
2477                        (gnus-buffer-live-p gnus-original-article-buffer)
2478                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2479               ;; If type `W f', use gnus-original-article-buffer,
2480               ;; otherwise use the current buffer because displaying
2481               ;; RFC822 parts calls this function too.
2482               (set-buffer gnus-original-article-buffer))
2483             (save-restriction
2484               (mail-narrow-to-head)
2485               (and gnus-article-x-face-command
2486                    (or force
2487                        ;; Check whether this face is censored.
2488                        (not (and gnus-article-x-face-too-ugly
2489                                  (or from
2490                                      (setq from (message-fetch-field "from")))
2491                                  (string-match gnus-article-x-face-too-ugly
2492                                                from))))
2493                    (while (gnus-article-goto-header "X-Face")
2494                      (push (mail-header-field-value) x-faces)))))
2495           (when x-faces
2496             ;; We display the face.
2497             (cond ((functionp gnus-article-x-face-command)
2498                    ;; The command is a lisp function, so we call it.
2499                    (mapc gnus-article-x-face-command x-faces))
2500                   ((stringp gnus-article-x-face-command)
2501                    ;; The command is a string, so we interpret the command
2502                    ;; as a, well, command, and fork it off.
2503                    (let ((process-connection-type nil))
2504                      (gnus-set-process-query-on-exit-flag
2505                       (start-process
2506                        "article-x-face" nil shell-file-name
2507                        shell-command-switch gnus-article-x-face-command)
2508                       nil)
2509                      ;; Sending multiple EOFs to xv doesn't work,
2510                      ;; so we only do a single external face.
2511                      (with-temp-buffer
2512                        (insert (car x-faces))
2513                        (process-send-region "article-x-face"
2514                                             (point-min) (point-max)))
2515                      (process-send-eof "article-x-face")))
2516                   (t
2517                    (error "`%s' set to `%s' is not a function"
2518                           gnus-article-x-face-command
2519                           'gnus-article-x-face-command)))))))))
2520
2521 (defun article-decode-mime-words ()
2522   "Decode all MIME-encoded words in the article."
2523   (interactive)
2524   (gnus-with-article-buffer
2525     (let ((inhibit-point-motion-hooks t)
2526           (mail-parse-charset gnus-newsgroup-charset)
2527           (mail-parse-ignored-charsets
2528            (with-current-buffer gnus-summary-buffer
2529              gnus-newsgroup-ignored-charsets)))
2530       (mail-decode-encoded-word-region (point-min) (point-max)))))
2531
2532 (defun article-decode-charset (&optional prompt)
2533   "Decode charset-encoded text in the article.
2534 If PROMPT (the prefix), prompt for a coding system to use."
2535   (interactive "P")
2536   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2537         (inhibit-read-only t)
2538         (mail-parse-charset gnus-newsgroup-charset)
2539         (mail-parse-ignored-charsets
2540          (save-excursion (condition-case nil
2541                              (set-buffer gnus-summary-buffer)
2542                            (error))
2543                          gnus-newsgroup-ignored-charsets))
2544         ct cte ctl charset format)
2545     (save-excursion
2546       (save-restriction
2547         (article-narrow-to-head)
2548         (setq ct (message-fetch-field "Content-Type" t)
2549               cte (message-fetch-field "Content-Transfer-Encoding" t)
2550               ctl (and ct (mail-header-parse-content-type ct))
2551               charset (cond
2552                        (prompt
2553                         (mm-read-coding-system "Charset to decode: "))
2554                        (ctl
2555                         (mail-content-type-get ctl 'charset)))
2556               format (and ctl (mail-content-type-get ctl 'format)))
2557         (when cte
2558           (setq cte (mail-header-strip cte)))
2559         (if (and ctl (not (string-match "/" (car ctl))))
2560             (setq ctl nil))
2561         (goto-char (point-max)))
2562       (forward-line 1)
2563       (save-restriction
2564         (narrow-to-region (point) (point-max))
2565         (when (and (eq mail-parse-charset 'gnus-decoded)
2566                    (eq (mm-body-7-or-8) '8bit))
2567           ;; The text code could have been decoded.
2568           (setq charset mail-parse-charset))
2569         (when (and (or (not ctl)
2570                        (equal (car ctl) "text/plain"))
2571                    (not format)) ;; article with format will decode later.
2572           (mm-decode-body
2573            charset (and cte (intern (downcase
2574                                      (gnus-strip-whitespace cte))))
2575            (car ctl)))))))
2576
2577 (defun article-decode-encoded-words ()
2578   "Remove encoded-word encoding from headers."
2579   (let ((inhibit-point-motion-hooks t)
2580         (mail-parse-charset gnus-newsgroup-charset)
2581         (mail-parse-ignored-charsets
2582          (save-excursion (condition-case nil
2583                              (set-buffer gnus-summary-buffer)
2584                            (error))
2585                          gnus-newsgroup-ignored-charsets))
2586         (inhibit-read-only t)
2587         end start)
2588     (goto-char (point-min))
2589     (when (search-forward "\n\n" nil 'move)
2590       (forward-line -1))
2591     (setq end (point))
2592     (while (not (bobp))
2593       (while (progn
2594                (forward-line -1)
2595                (and (not (bobp))
2596                     (memq (char-after) '(?\t ? )))))
2597       (setq start (point))
2598       (if (looking-at "\
2599 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2600 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2601           (funcall gnus-decode-address-function start end)
2602         (funcall gnus-decode-header-function start end))
2603       (goto-char (setq end start)))))
2604
2605 (defun article-decode-group-name ()
2606   "Decode group names in Newsgroups, Followup-To and Xref headers."
2607   (let ((inhibit-point-motion-hooks t)
2608         (inhibit-read-only t)
2609         (method (gnus-find-method-for-group gnus-newsgroup-name))
2610         regexp)
2611     (when (and (or gnus-group-name-charset-method-alist
2612                    gnus-group-name-charset-group-alist)
2613                (gnus-buffer-live-p gnus-original-article-buffer))
2614       (save-restriction
2615         (article-narrow-to-head)
2616         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2617           (with-current-buffer gnus-original-article-buffer
2618             (goto-char (point-min)))
2619           (setq regexp (concat "^" header
2620                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2621           (while (re-search-forward regexp nil t)
2622             (replace-match (save-match-data
2623                              (gnus-decode-newsgroups
2624                               ;; XXX how to use data in article buffer?
2625                               (with-current-buffer gnus-original-article-buffer
2626                                 (re-search-forward regexp nil t)
2627                                 (match-string 1))
2628                               gnus-newsgroup-name method))
2629                            t t nil 1))
2630           (goto-char (point-min)))))))
2631
2632 (autoload 'idna-to-unicode "idna")
2633
2634 (defun article-decode-idna-rhs ()
2635   "Decode IDNA strings in RHS in various headers in current buffer.
2636 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2637 Mail-Reply-To: and Mail-Followup-To:."
2638   (when gnus-use-idna
2639     (save-restriction
2640       (let ((inhibit-point-motion-hooks t)
2641             (inhibit-read-only t))
2642         (article-narrow-to-head)
2643         (goto-char (point-min))
2644         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2645           (let (ace unicode)
2646             (when (save-match-data
2647                     (and (setq ace (match-string 1))
2648                          (save-excursion
2649                            (and (re-search-backward "^[^ \t]" nil t)
2650                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2651                          (setq unicode (idna-to-unicode ace))))
2652               (unless (string= ace unicode)
2653                 (replace-match unicode nil nil nil 1)))))))))
2654
2655 (defun article-de-quoted-unreadable (&optional force read-charset)
2656   "Translate a quoted-printable-encoded article.
2657 If FORCE, decode the article whether it is marked as quoted-printable
2658 or not.
2659 If READ-CHARSET, ask for a coding system."
2660   (interactive (list 'force current-prefix-arg))
2661   (save-excursion
2662     (let ((inhibit-read-only t) type charset)
2663       (if (gnus-buffer-live-p gnus-original-article-buffer)
2664           (with-current-buffer gnus-original-article-buffer
2665             (setq type
2666                   (gnus-fetch-field "content-transfer-encoding"))
2667             (let* ((ct (gnus-fetch-field "content-type"))
2668                    (ctl (and ct (mail-header-parse-content-type ct))))
2669               (setq charset (and ctl
2670                                  (mail-content-type-get ctl 'charset)))
2671               (if (stringp charset)
2672                   (setq charset (intern (downcase charset)))))))
2673       (if read-charset
2674           (setq charset (mm-read-coding-system "Charset: " charset)))
2675       (unless charset
2676         (setq charset gnus-newsgroup-charset))
2677       (when (or force
2678                 (and type (let ((case-fold-search t))
2679                             (string-match "quoted-printable" type))))
2680         (article-goto-body)
2681         (quoted-printable-decode-region
2682          (point) (point-max) (mm-charset-to-coding-system charset))))))
2683
2684 (defun article-de-base64-unreadable (&optional force read-charset)
2685   "Translate a base64 article.
2686 If FORCE, decode the article whether it is marked as base64 not.
2687 If READ-CHARSET, ask for a coding system."
2688   (interactive (list 'force current-prefix-arg))
2689   (save-excursion
2690     (let ((inhibit-read-only t) type charset)
2691       (if (gnus-buffer-live-p gnus-original-article-buffer)
2692           (with-current-buffer gnus-original-article-buffer
2693             (setq type
2694                   (gnus-fetch-field "content-transfer-encoding"))
2695             (let* ((ct (gnus-fetch-field "content-type"))
2696                    (ctl (and ct (mail-header-parse-content-type ct))))
2697               (setq charset (and ctl
2698                                  (mail-content-type-get ctl 'charset)))
2699               (if (stringp charset)
2700                   (setq charset (intern (downcase charset)))))))
2701       (if read-charset
2702           (setq charset (mm-read-coding-system "Charset: " charset)))
2703       (unless charset
2704         (setq charset gnus-newsgroup-charset))
2705       (when (or force
2706                 (and type (let ((case-fold-search t))
2707                             (string-match "base64" type))))
2708         (article-goto-body)
2709         (save-restriction
2710           (narrow-to-region (point) (point-max))
2711           (base64-decode-region (point-min) (point-max))
2712           (mm-decode-coding-region
2713            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2714
2715 (eval-when-compile
2716   (require 'rfc1843))
2717
2718 (defun article-decode-HZ ()
2719   "Translate a HZ-encoded article."
2720   (interactive)
2721   (require 'rfc1843)
2722   (save-excursion
2723     (let ((inhibit-read-only t))
2724       (rfc1843-decode-region (point-min) (point-max)))))
2725
2726 (defun article-unsplit-urls ()
2727   "Remove the newlines that some other mailers insert into URLs."
2728   (interactive)
2729   (save-excursion
2730     (let ((inhibit-read-only t))
2731       (goto-char (point-min))
2732       (while (re-search-forward
2733               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2734         (replace-match "\\1\\3" t)))
2735     (when (interactive-p)
2736       (gnus-treat-article nil))))
2737
2738 (defun article-wash-html ()
2739   "Format an HTML article."
2740   (interactive)
2741   (let ((handles nil)
2742         (buffer-read-only nil))
2743     (when (gnus-buffer-live-p gnus-original-article-buffer)
2744       (setq handles (mm-dissect-buffer t t)))
2745     (article-goto-body)
2746     (delete-region (point) (point-max))
2747     (mm-inline-text-html handles)))
2748
2749 (defvar gnus-article-browse-html-temp-list nil
2750   "List of temporary files created by `gnus-article-browse-html-parts'.
2751 Internal variable.")
2752
2753 (defcustom gnus-article-browse-delete-temp 'ask
2754   "What to do with temporary files from `gnus-article-browse-html-parts'.
2755 If nil, don't delete temporary files.  If it is t, delete them on
2756 exit from the summary buffer.  If it is the symbol `file', query
2757 on each file, if it is `ask' ask once when exiting from the
2758 summary buffer."
2759   :group 'gnus-article
2760   :version "23.1" ;; No Gnus
2761   :type '(choice (const :tag "Don't delete" nil)
2762                  (const :tag "Don't ask" t)
2763                  (const :tag "Ask" ask)
2764                  (const :tag "Ask for each file" file)))
2765
2766 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2767
2768 (defun gnus-article-browse-delete-temp-files (&optional how)
2769   "Delete temp-files created by `gnus-article-browse-html-parts'."
2770   (when (and gnus-article-browse-html-temp-list
2771              (progn
2772                (or how (setq how gnus-article-browse-delete-temp))
2773                (if (eq how 'ask)
2774                    (let ((files (length gnus-article-browse-html-temp-list)))
2775                      (gnus-y-or-n-p (format
2776                                      "Delete all %s temporary HTML file%s? "
2777                                      files
2778                                      (if (> files 1) "s" ""))))
2779                  how)))
2780     (dolist (file gnus-article-browse-html-temp-list)
2781       (cond ((file-directory-p file)
2782              (when (or (not (eq how 'file))
2783                        (gnus-y-or-n-p
2784                         (format
2785                          "Delete temporary HTML file(s) in directory `%s'? "
2786                          (file-name-as-directory file))))
2787                (gnus-delete-directory file)))
2788             ((file-exists-p file)
2789              (when (or (not (eq how 'file))
2790                        (gnus-y-or-n-p
2791                         (format "Delete temporary HTML file `%s'? " file)))
2792                (delete-file file)))))
2793     ;; Also remove file from the list when not deleted or if file doesn't
2794     ;; exist anymore.
2795     (setq gnus-article-browse-html-temp-list nil))
2796   gnus-article-browse-html-temp-list)
2797
2798 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2799   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2800 Return file name."
2801   (save-match-data
2802     (let (file type)
2803       (catch 'found
2804         (dolist (handle handles)
2805           (cond
2806            ((not (listp handle)))
2807            ((equal (mm-handle-media-supertype handle) "multipart")
2808             (when (setq file (gnus-article-browse-html-save-cid-content
2809                               cid handle directory))
2810               (throw 'found file)))
2811            ((equal (concat "<" cid ">") (mm-handle-id handle))
2812             (setq file
2813                   (expand-file-name
2814                    (or (mm-handle-filename handle)
2815                        (concat
2816                         (make-temp-name "cid")
2817                         (car (rassoc (car (mm-handle-type handle)) mailcap-mime-extensions))))
2818                    directory))
2819             (mm-save-part-to-file handle file)
2820             (throw 'found file))))))))
2821
2822 (defun gnus-article-browse-html-parts (list &optional header)
2823   "View all \"text/html\" parts from LIST.
2824 Recurse into multiparts.  The optional HEADER that should be a decoded
2825 message header will be added to the bodies of the \"text/html\" parts."
2826   ;; Internal function used by `gnus-article-browse-html-article'.
2827   (let (type file charset content cid-dir tmp-file showed)
2828     ;; Find and show the html-parts.
2829     (dolist (handle list)
2830       ;; If HTML, show it:
2831       (cond ((not (listp handle)))
2832             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2833                  (and (equal (car type) "message/external-body")
2834                       (or header
2835                           (setq file (mm-handle-filename handle)))
2836                       (or (mm-handle-cache handle)
2837                           (condition-case code
2838                               (progn (mm-extern-cache-contents handle) t)
2839                             (error
2840                              (gnus-message 3 "%s" (error-message-string code))
2841                              (when (>= gnus-verbose 3) (sit-for 2))
2842                              nil)))
2843                       (progn
2844                         (setq handle (mm-handle-cache handle)
2845                               type (mm-handle-type handle))
2846                         (equal (car type) "text/html"))))
2847              (setq charset (mail-content-type-get type 'charset)
2848                    content (mm-get-part handle))
2849              (with-temp-buffer
2850                (if (eq charset 'gnus-decoded)
2851                    (mm-enable-multibyte)
2852                  (mm-disable-multibyte))
2853                (insert content)
2854                ;; resolve cid contents
2855                (let ((case-fold-search t)
2856                      cid-file)
2857                  (goto-char (point-min))
2858                  (while (re-search-forward "\
2859 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2860                                            nil t)
2861                    (unless cid-dir
2862                      (setq cid-dir (mm-make-temp-file "cid" t))
2863                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2864                    (setq file nil
2865                          content nil)
2866                    (when (setq cid-file
2867                                (gnus-article-browse-html-save-cid-content
2868                                 (match-string 2)
2869                                 (with-current-buffer gnus-article-buffer
2870                                   gnus-article-mime-handles)
2871                                 cid-dir))
2872                      (replace-match (concat "file://" cid-file)
2873                                     nil nil nil 1))))
2874                (unless content (setq content (buffer-string))))
2875              (when (or charset header (not file))
2876                (setq tmp-file (mm-make-temp-file
2877                                ;; Do we need to care for 8.3 filenames?
2878                                "mm-" nil ".html")))
2879              ;; Add a meta html tag to specify charset and a header.
2880              (cond
2881               (header
2882                (let (title eheader body hcharset coding force-charset)
2883                  (with-temp-buffer
2884                    (mm-enable-multibyte)
2885                    (setq case-fold-search t)
2886                    (insert header "\n")
2887                    (setq title (message-fetch-field "subject"))
2888                    (goto-char (point-min))
2889                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2890                      (replace-match (cond ((match-beginning 1) "&lt;")
2891                                           ((match-beginning 2) "&gt;")
2892                                           (t "&amp;"))))
2893                    (goto-char (point-min))
2894                    (insert "<pre>\n")
2895                    (goto-char (point-max))
2896                    (insert "</pre>\n<hr>\n")
2897                    ;; We have to examine charset one by one since
2898                    ;; charset specified in parts might be different.
2899                    (if (eq charset 'gnus-decoded)
2900                        (setq charset 'utf-8
2901                              eheader (mm-encode-coding-string (buffer-string)
2902                                                               charset)
2903                              title (when title
2904                                      (mm-encode-coding-string title charset))
2905                              body (mm-encode-coding-string content charset)
2906                              force-charset t)
2907                      (setq hcharset (mm-find-mime-charset-region (point-min)
2908                                                                  (point-max)))
2909                      (cond ((= (length hcharset) 1)
2910                             (setq hcharset (car hcharset)
2911                                   coding (mm-charset-to-coding-system
2912                                           hcharset)))
2913                            ((> (length hcharset) 1)
2914                             (setq hcharset 'utf-8
2915                                   coding hcharset)))
2916                      (if coding
2917                          (if charset
2918                              (progn
2919                                (setq body
2920                                      (mm-charset-to-coding-system charset))
2921                                (if (eq coding body)
2922                                    (setq eheader (mm-encode-coding-string
2923                                                   (buffer-string) coding)
2924                                          title (when title
2925                                                  (mm-encode-coding-string
2926                                                   title coding))
2927                                          body content)
2928                                  (setq charset 'utf-8
2929                                        eheader (mm-encode-coding-string
2930                                                 (buffer-string) charset)
2931                                        title (when title
2932                                                (mm-encode-coding-string
2933                                                 title charset))
2934                                        body (mm-encode-coding-string
2935                                              (mm-decode-coding-string
2936                                               content body)
2937                                              charset)
2938                                        force-charset t)))
2939                            (setq charset hcharset
2940                                  eheader (mm-encode-coding-string
2941                                           (buffer-string) coding)
2942                                  title (when title
2943                                          (mm-encode-coding-string
2944                                           title coding))
2945                                  body content))
2946                        (setq eheader (mm-string-as-unibyte (buffer-string))
2947                              body content)))
2948                    (erase-buffer)
2949                    (mm-disable-multibyte)
2950                    (insert body)
2951                    (when charset
2952                      (mm-add-meta-html-tag handle charset force-charset))
2953                    (when title
2954                      (goto-char (point-min))
2955                      (unless (search-forward "<title>" nil t)
2956                        (re-search-forward "<head>\\s-*" nil t)
2957                        (insert "<title>" title "</title>\n")))
2958                    (goto-char (point-min))
2959                    (or (re-search-forward
2960                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2961                        (re-search-forward
2962                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2963                    (insert eheader)
2964                    (mm-write-region (point-min) (point-max)
2965                                     tmp-file nil nil nil 'binary t))))
2966               (charset
2967                (mm-with-unibyte-buffer
2968                  (insert (if (eq charset 'gnus-decoded)
2969                              (mm-encode-coding-string content
2970                                                       (setq charset 'utf-8))
2971                            content))
2972                  (if (or (mm-add-meta-html-tag handle charset)
2973                          (not file))
2974                      (mm-write-region (point-min) (point-max)
2975                                       tmp-file nil nil nil 'binary t)
2976                    (setq tmp-file nil))))
2977               (tmp-file
2978                (mm-save-part-to-file handle tmp-file)))
2979              (when tmp-file
2980                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2981              (add-hook 'gnus-summary-prepare-exit-hook
2982                        'gnus-article-browse-delete-temp-files)
2983              (add-hook 'gnus-exit-gnus-hook
2984                        (lambda  ()
2985                          (gnus-article-browse-delete-temp-files t)))
2986              ;; FIXME: Warn if there's an <img> tag?
2987              (browse-url-of-file (or tmp-file (expand-file-name file)))
2988              (setq showed t))
2989             ;; If multipart, recurse
2990             ((equal (mm-handle-media-supertype handle) "multipart")
2991              (when (gnus-article-browse-html-parts handle header)
2992                (setq showed t)))
2993             ((equal (mm-handle-media-type handle) "message/rfc822")
2994              (mm-with-multibyte-buffer
2995                (mm-insert-part handle)
2996                (setq handle (mm-dissect-buffer t t))
2997                (when (and (bufferp (car handle))
2998                           (stringp (car (mm-handle-type handle))))
2999                  (setq handle (list handle)))
3000                (when header
3001                  (article-decode-encoded-words)
3002                  (let ((gnus-visible-headers
3003                         (or (get 'gnus-visible-headers 'standard-value)
3004                             gnus-visible-headers)))
3005                    (article-hide-headers))
3006                  (goto-char (point-min))
3007                  (search-forward "\n\n" nil 'move)
3008                  (skip-chars-backward "\t\n ")
3009                  (setq header (buffer-substring (point-min) (point)))))
3010              (when (prog1
3011                        (gnus-article-browse-html-parts handle header)
3012                      (mm-destroy-parts handle))
3013                (setq showed t)))))
3014     showed))
3015
3016 (defun gnus-article-browse-html-article (&optional arg)
3017   "View \"text/html\" parts of the current article with a WWW browser.
3018 Inline images embedded in a message using the cid scheme, as they are
3019 generally considered to be safe, will be processed properly.
3020 The message header is added to the beginning of every html part unless
3021 the prefix argument ARG is given.
3022
3023 Warning: Spammers use links to images (using the http scheme) in HTML
3024 articles to verify whether you have read the message.  As
3025 `gnus-article-browse-html-article' passes the HTML content to the
3026 browser without eliminating these \"web bugs\" you should only
3027 use it for mails from trusted senders.
3028
3029 If you always want to display HTML parts in the browser, set
3030 `mm-text-html-renderer' to nil.
3031
3032 This command creates temporary files to pass HTML contents including
3033 images if any to the browser, and deletes them when exiting the group
3034 \(if you want)."
3035   ;; Cf. `mm-w3m-safe-url-regexp'
3036   (interactive "P")
3037   (if arg
3038       (gnus-summary-show-article)
3039     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3040                                     gnus-visible-headers))
3041           ;; As we insert a <hr>, there's no need for the body boundary.
3042           (gnus-treat-body-boundary nil))
3043       (gnus-summary-show-article)))
3044   (with-current-buffer gnus-article-buffer
3045     (let ((header (unless arg
3046                     (save-restriction
3047                       (widen)
3048                       (buffer-substring-no-properties
3049                        (goto-char (point-min))
3050                        (if (search-forward "\n\n" nil t)
3051                            (match-beginning 0)
3052                          (goto-char (point-max))
3053                          (skip-chars-backward "\t\n ")
3054                          (point))))))
3055           parts)
3056       (set-buffer gnus-original-article-buffer)
3057       (setq parts (mm-dissect-buffer t t))
3058       ;; If singlepart, enforce a list.
3059       (when (and (bufferp (car parts))
3060                  (stringp (car (mm-handle-type parts))))
3061         (setq parts (list parts)))
3062       ;; Process the list
3063       (unless (gnus-article-browse-html-parts parts header)
3064         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3065       (mm-destroy-parts parts)
3066       (unless arg
3067         (gnus-summary-show-article)))))
3068
3069 (defun article-hide-list-identifiers ()
3070   "Remove list identifies from the Subject header.
3071 The `gnus-list-identifiers' variable specifies what to do."
3072   (interactive)
3073   (let ((inhibit-point-motion-hooks t)
3074         (regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
3075         (inhibit-read-only t))
3076     (when regexp
3077       (save-excursion
3078         (save-restriction
3079           (article-narrow-to-head)
3080           (goto-char (point-min))
3081           (while (re-search-forward
3082                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3083                   nil t)
3084             (delete-region (match-beginning 2) (match-end 0))
3085             (beginning-of-line))
3086           (when (re-search-forward
3087                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3088             (delete-region (match-beginning 1) (match-end 1))))))))
3089
3090 (defun article-hide-pem (&optional arg)
3091   "Toggle hiding of any PEM headers and signatures in the current article.
3092 If given a negative prefix, always show; if given a positive prefix,
3093 always hide."
3094   (interactive (gnus-article-hidden-arg))
3095   (unless (gnus-article-check-hidden-text 'pem arg)
3096     (save-excursion
3097       (let ((inhibit-read-only t) end)
3098         (goto-char (point-min))
3099         ;; Hide the horrendously ugly "header".
3100         (when (and (search-forward
3101                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3102                     nil t)
3103                    (setq end (1+ (match-beginning 0))))
3104           (gnus-add-wash-type 'pem)
3105           (gnus-article-hide-text-type
3106            end
3107            (if (search-forward "\n\n" nil t)
3108                (match-end 0)
3109              (point-max))
3110            'pem)
3111           ;; Hide the trailer as well
3112           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3113                                 nil t)
3114             (gnus-article-hide-text-type
3115              (match-beginning 0) (match-end 0) 'pem)))))))
3116
3117 (defun article-strip-banner ()
3118   "Strip the banners specified by the `banner' group parameter and by
3119 `gnus-article-address-banner-alist'."
3120   (interactive)
3121   (save-excursion
3122     (save-restriction
3123       (let ((inhibit-point-motion-hooks t))
3124         (when (gnus-parameter-banner gnus-newsgroup-name)
3125           (article-really-strip-banner
3126            (gnus-parameter-banner gnus-newsgroup-name)))
3127         (when gnus-article-address-banner-alist
3128           ;; Note that the From header is decoded here, so it is
3129           ;; required that the *-extract-address-components function
3130           ;; supports non-ASCII text.
3131           (let ((from (save-restriction
3132                         (widen)
3133                         (article-narrow-to-head)
3134                         (mail-fetch-field "from"))))
3135             (when (and from
3136                        (setq from
3137                              (cadr (funcall gnus-extract-address-components
3138                                             from))))
3139               (catch 'found
3140                 (dolist (pair gnus-article-address-banner-alist)
3141                   (when (string-match (car pair) from)
3142                     (throw 'found
3143                            (article-really-strip-banner (cdr pair)))))))))))))
3144
3145 (defun article-really-strip-banner (banner)
3146   "Strip the banner specified by the argument."
3147   (save-excursion
3148     (save-restriction
3149       (let ((inhibit-point-motion-hooks t)
3150             (gnus-signature-limit nil)
3151             (inhibit-read-only t))
3152         (article-goto-body)
3153         (cond
3154          ((eq banner 'signature)
3155           (when (gnus-article-narrow-to-signature)
3156             (widen)
3157             (forward-line -1)
3158             (delete-region (point) (point-max))))
3159          ((symbolp banner)
3160           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3161               (while (re-search-forward banner nil t)
3162                 (delete-region (match-beginning 0) (match-end 0)))))
3163          ((stringp banner)
3164           (while (re-search-forward banner nil t)
3165             (delete-region (match-beginning 0) (match-end 0)))))))))
3166
3167 (defun article-babel ()
3168   "Translate article using an online translation service."
3169   (interactive)
3170   (require 'babel)
3171   (gnus-with-article-buffer
3172     (when (article-goto-body)
3173       (let* ((start (point))
3174              (end (point-max))
3175              (orig (buffer-substring start end))
3176              (trans (babel-as-string orig)))
3177         (save-restriction
3178           (narrow-to-region start end)
3179           (delete-region start end)
3180           (insert trans))))))
3181
3182 (defun article-hide-signature (&optional arg)
3183   "Hide the signature in the current article.
3184 If given a negative prefix, always show; if given a positive prefix,
3185 always hide."
3186   (interactive (gnus-article-hidden-arg))
3187   (unless (gnus-article-check-hidden-text 'signature arg)
3188     (save-excursion
3189       (save-restriction
3190         (let ((inhibit-read-only t))
3191           (when (gnus-article-narrow-to-signature)
3192             (gnus-article-hide-text-type
3193              (point-min) (point-max) 'signature))))))
3194   (gnus-set-mode-line 'article))
3195
3196 (defun article-strip-headers-in-body ()
3197   "Strip offensive headers from bodies."
3198   (interactive)
3199   (save-excursion
3200     (article-goto-body)
3201     (let ((case-fold-search t))
3202       (when (looking-at "x-no-archive:")
3203         (gnus-delete-line)))))
3204
3205 (defun article-strip-leading-blank-lines ()
3206   "Remove all blank lines from the beginning of the article."
3207   (interactive)
3208   (save-excursion
3209     (let ((inhibit-point-motion-hooks t)
3210           (inhibit-read-only t))
3211       (when (article-goto-body)
3212         (while (and (not (eobp))
3213                     (looking-at "[ \t]*$"))
3214           (gnus-delete-line))))))
3215
3216 (defun article-narrow-to-head ()
3217   "Narrow the buffer to the head of the message.
3218 Point is left at the beginning of the narrowed-to region."
3219   (narrow-to-region
3220    (goto-char (point-min))
3221    (if (search-forward "\n\n" nil 1)
3222        (1- (point))
3223      (point-max)))
3224   (goto-char (point-min)))
3225
3226 (defun article-goto-body ()
3227   "Place point at the start of the body."
3228   (goto-char (point-min))
3229   (cond
3230    ;; This variable is only bound when dealing with separate
3231    ;; MIME body parts.
3232    (article-goto-body-goes-to-point-min-p
3233     t)
3234    ((search-forward "\n\n" nil t)
3235     t)
3236    (t
3237     (goto-char (point-max))
3238     nil)))
3239
3240 (defun article-strip-multiple-blank-lines ()
3241   "Replace consecutive blank lines with one empty line."
3242   (interactive)
3243   (save-excursion
3244     (let ((inhibit-point-motion-hooks t)
3245           (inhibit-read-only t))
3246       ;; First make all blank lines empty.
3247       (article-goto-body)
3248       (while (re-search-forward "^[ \t]+$" nil t)
3249         (unless (gnus-annotation-in-region-p
3250                  (match-beginning 0) (match-end 0))
3251           (replace-match "" nil t)))
3252       ;; Then replace multiple empty lines with a single empty line.
3253       (article-goto-body)
3254       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3255         (unless (gnus-annotation-in-region-p
3256                  (match-beginning 0) (match-end 0))
3257           (delete-region (match-beginning 1) (match-end 1)))))))
3258
3259 (defun article-strip-leading-space ()
3260   "Remove all white space from the beginning of the lines in the article."
3261   (interactive)
3262   (save-excursion
3263     (let ((inhibit-point-motion-hooks t)
3264           (inhibit-read-only t))
3265       (article-goto-body)
3266       (while (re-search-forward "^[ \t]+" nil t)
3267         (replace-match "" t t)))))
3268
3269 (defun article-strip-trailing-space ()
3270   "Remove all white space from the end of the lines in the article."
3271   (interactive)
3272   (save-excursion
3273     (let ((inhibit-point-motion-hooks t)
3274           (inhibit-read-only t))
3275       (article-goto-body)
3276       (while (re-search-forward "[ \t]+$" nil t)
3277         (replace-match "" t t)))))
3278
3279 (defun article-strip-blank-lines ()
3280   "Strip leading, trailing and multiple blank lines."
3281   (interactive)
3282   (article-strip-leading-blank-lines)
3283   (article-remove-trailing-blank-lines)
3284   (article-strip-multiple-blank-lines))
3285
3286 (defun article-strip-all-blank-lines ()
3287   "Strip all blank lines."
3288   (interactive)
3289   (save-excursion
3290     (let ((inhibit-point-motion-hooks t)
3291           (inhibit-read-only t))
3292       (article-goto-body)
3293       (while (re-search-forward "^[ \t]*\n" nil t)
3294         (replace-match "" t t)))))
3295
3296 (defun gnus-article-narrow-to-signature ()
3297   "Narrow to the signature; return t if a signature is found, else nil."
3298   (let ((inhibit-point-motion-hooks t))
3299     (when (gnus-article-search-signature)
3300       (forward-line 1)
3301       ;; Check whether we have some limits to what we consider
3302       ;; to be a signature.
3303       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3304                       (list gnus-signature-limit)))
3305             limit limited)
3306         (while (setq limit (pop limits))
3307           (if (or (and (integerp limit)
3308                        (< (- (point-max) (point)) limit))
3309                   (and (floatp limit)
3310                        (< (count-lines (point) (point-max)) limit))
3311                   (and (functionp limit)
3312                        (funcall limit))
3313                   (and (stringp limit)
3314                        (not (re-search-forward limit nil t))))
3315               ()                        ; This limit did not succeed.
3316             (setq limited t
3317                   limits nil)))
3318         (unless limited
3319           (narrow-to-region (point) (point-max))
3320           t)))))
3321
3322 (defun gnus-article-search-signature ()
3323   "Search the current buffer for the signature separator.
3324 Put point at the beginning of the signature separator."
3325   (let ((cur (point)))
3326     (goto-char (point-max))
3327     (if (if (stringp gnus-signature-separator)
3328             (re-search-backward gnus-signature-separator nil t)
3329           (let ((seps gnus-signature-separator))
3330             (while (and seps
3331                         (not (re-search-backward (car seps) nil t)))
3332               (pop seps))
3333             seps))
3334         t
3335       (goto-char cur)
3336       nil)))
3337
3338 (defun gnus-article-hidden-arg ()
3339   "Return the current prefix arg as a number, or 0 if no prefix."
3340   (list (if current-prefix-arg
3341             (prefix-numeric-value current-prefix-arg)
3342           0)))
3343
3344 (defun gnus-article-check-hidden-text (type arg)
3345   "Return nil if hiding is necessary.
3346 Arg can be nil or a number.  nil and positive means hide, negative
3347 means show, 0 means toggle."
3348   (save-excursion
3349     (save-restriction
3350       (let ((hide (gnus-article-hidden-text-p type)))
3351         (cond
3352          ((or (null arg)
3353               (> arg 0))
3354           nil)
3355          ((< arg 0)
3356           (gnus-article-show-hidden-text type)
3357           t)
3358          (t
3359           (if (eq hide 'hidden)
3360               (progn
3361                 (gnus-article-show-hidden-text type)
3362                 t)
3363             nil)))))))
3364
3365 (defun gnus-article-hidden-text-p (type)
3366   "Say whether the current buffer contains hidden text of type TYPE."
3367   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3368     (while (and pos
3369                 (not (get-text-property pos 'invisible))
3370                 (not (get-text-property pos 'dummy-invisible)))
3371       (setq pos
3372             (text-property-any (1+ pos) (point-max) 'article-type type)))
3373     (if pos
3374         'hidden
3375       nil)))
3376
3377 (defun gnus-article-show-hidden-text (type &optional dummy)
3378   "Show all hidden text of type TYPE.
3379 Originally it is hide instead of DUMMY."
3380   (let ((inhibit-read-only t)
3381         (inhibit-point-motion-hooks t))
3382     (gnus-remove-text-properties-when
3383      'article-type type
3384      (point-min) (point-max)
3385      (cons 'article-type (cons type
3386                                gnus-hidden-properties)))
3387     (gnus-delete-wash-type type)))
3388
3389 (defconst article-time-units
3390   `((year . ,(* 365.25 24 60 60))
3391     (week . ,(* 7 24 60 60))
3392     (day . ,(* 24 60 60))
3393     (hour . ,(* 60 60))
3394     (minute . 60)
3395     (second . 1))
3396   "Mapping from time units to seconds.")
3397
3398 (defun gnus-article-forward-header ()
3399   "Move point to the start of the next header.
3400 If the current header is a continuation header, this can be several
3401 lines forward."
3402   (let ((ended nil))
3403     (while (not ended)
3404       (forward-line 1)
3405       (if (looking-at "[ \t]+[^ \t]")
3406           (forward-line 1)
3407         (setq ended t)))))
3408
3409 (defun article-treat-date ()
3410   (article-date-ut gnus-article-date-headers t))
3411
3412 (defun article-date-ut (&optional type highlight date-position)
3413   "Convert DATE date to TYPE in the current article.
3414 The default type is `ut'.  See `gnus-article-date-headers' for
3415 possible values."
3416   (interactive (list 'ut t))
3417   (let* ((case-fold-search t)
3418          (inhibit-read-only t)
3419          (inhibit-point-motion-hooks t)
3420          (first t)
3421          (visible-date (mail-fetch-field "Date"))
3422          pos date bface eface)
3423     (save-excursion
3424       (save-restriction
3425         (goto-char (point-min))
3426         (when (re-search-forward "^Date:" nil t)
3427           (setq bface (get-text-property (point-at-bol) 'face)
3428                 eface (get-text-property (1- (point-at-eol)) 'face)))
3429         (goto-char (point-min))
3430         ;; Delete any old Date headers.
3431         (if date-position
3432             (progn
3433               (goto-char date-position)
3434               (setq date (get-text-property (point) 'original-date))
3435               (delete-region (point)
3436                              (progn
3437                                (gnus-article-forward-header)
3438                                (point)))
3439               (article-transform-date date type bface eface))
3440           (while (re-search-forward "^Date:" nil t)
3441             (setq date (get-text-property (match-beginning 0) 'original-date))
3442             (delete-region (point-at-bol) (progn
3443                                             (gnus-article-forward-header)
3444                                             (point))))
3445           (when (and (not date)
3446                      visible-date)
3447             (setq date visible-date))
3448           (when date
3449             (article-transform-date date type bface eface)))))))
3450
3451 (defun article-transform-date (date type bface eface)
3452   (dolist (this-type (cond
3453                       ((null type)
3454                        (list 'ut))
3455                       ((atom type)
3456                        (list type))
3457                       (t
3458                        type)))
3459     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3460     (forward-line -1)
3461     (beginning-of-line)
3462     (put-text-property (point) (1+ (point))
3463                        'original-date date)
3464     (put-text-property (point) (1+ (point))
3465                        'gnus-date-type this-type)
3466     ;; Do highlighting.
3467     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3468       (put-text-property (match-beginning 1) (1+ (match-end 1))
3469                          'face bface)
3470       (put-text-property (match-beginning 2) (match-end 2)
3471                          'face eface))
3472     (forward-line 1)))
3473
3474 (defun article-make-date-line (date type)
3475   "Return a DATE line of TYPE."
3476   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3477                              combined-lapsed))
3478     (error "Unknown conversion type: %s" type))
3479   (condition-case ()
3480       (let ((time (ignore-errors (date-to-time date))))
3481         (cond
3482          ;; Convert to the local timezone.
3483          ((eq type 'local)
3484           (concat "Date: " (message-make-date time)))
3485          ;; Convert to Universal Time.
3486          ((eq type 'ut)
3487           (concat "Date: "
3488                   (substring
3489                    (message-make-date
3490                     (let* ((e (parse-time-string date))
3491                            (tm (apply 'encode-time e))
3492                            (ms (car tm))
3493                            (ls (- (cadr tm) (car (current-time-zone time)))))
3494                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3495                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3496                             (t (list ms ls)))))
3497                    0 -5)
3498                   "UT"))
3499          ;; Get the original date from the article.
3500          ((eq type 'original)
3501           (concat "Date: " (if (string-match "\n+$" date)
3502                                (substring date 0 (match-beginning 0))
3503                              date)))
3504          ;; Let the user define the format.
3505          ((eq type 'user-defined)
3506           (let ((format (or (condition-case nil
3507                                 (with-current-buffer gnus-summary-buffer
3508                                   gnus-article-time-format)
3509                               (error nil))
3510                             gnus-article-time-format)))
3511             (if (functionp format)
3512                 (funcall format time)
3513               (concat "Date: " (format-time-string format time)))))
3514          ;; ISO 8601.
3515          ((eq type 'iso8601)
3516           (let ((tz (car (current-time-zone time))))
3517             (concat
3518              "Date: "
3519              (format-time-string "%Y%m%dT%H%M%S" time)
3520              (format "%s%02d%02d"
3521                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3522                      (/ (% (abs tz) 3600) 60)))))
3523          ;; Do a lapsed format.
3524          ((eq type 'lapsed)
3525           (concat "Date: " (article-lapsed-string time)))
3526          ;; A combined date/lapsed format.
3527          ((eq type 'combined-lapsed)
3528           (let ((date-string (article-make-date-line date 'original))
3529                 (segments 3)
3530                 lapsed-string)
3531             (while (and
3532                     time
3533                     (setq lapsed-string
3534                           (concat " (" (article-lapsed-string time segments) ")"))
3535                     (> (+ (length date-string)
3536                           (length lapsed-string))
3537                        (+ fill-column 6))
3538                     (> segments 0))
3539               (setq segments (1- segments)))
3540             (if (> segments 0)
3541                 (concat date-string lapsed-string)
3542               date-string)))
3543          ;; Display the date in proper English
3544          ((eq type 'english)
3545           (let ((dtime (decode-time time)))
3546             (concat
3547              "Date: the "
3548              (number-to-string (nth 3 dtime))
3549              (let ((digit (% (nth 3 dtime) 10)))
3550                (cond
3551                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3552                 ((= digit 1) "st")
3553                 ((= digit 2) "nd")
3554                 ((= digit 3) "rd")
3555                 (t "th")))
3556              " of "
3557              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3558              " "
3559              (number-to-string (nth 5 dtime))
3560              " at "
3561              (format "%02d" (nth 2 dtime))
3562              ":"
3563              (format "%02d" (nth 1 dtime)))))))
3564     (foo
3565      (format "Date: %s (from Gnus)" date))))
3566
3567 (defun article-lapsed-string (time &optional max-segments)
3568   ;; If the date is seriously mangled, the timezone functions are
3569   ;; liable to bug out, so we ignore all errors.
3570   (let* ((now (current-time))
3571          (real-time (subtract-time now time))
3572          (real-sec (and real-time
3573                         (+ (* (float (car real-time)) 65536)
3574                            (cadr real-time))))
3575          (sec (and real-time (abs real-sec)))
3576          (segments 0)
3577          num prev)
3578     (unless max-segments
3579       (setq max-segments (length article-time-units)))
3580     (cond
3581      ((null real-time)
3582       "Unknown")
3583      ((zerop sec)
3584       "Now")
3585      (t
3586       (concat
3587        ;; This is a bit convoluted, but basically we go
3588        ;; through the time units for years, weeks, etc,
3589        ;; and divide things to see whether that results
3590        ;; in positive answers.
3591        (mapconcat
3592         (lambda (unit)
3593           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3594                   (>= segments max-segments))
3595               ;; The (remaining) seconds are too few to
3596               ;; be divided into this time unit.
3597               ""
3598             ;; It's big enough, so we output it.
3599             (setq sec (- sec (* num (cdr unit))))
3600             (prog1
3601                 (concat (if prev ", " "") (int-to-string
3602                                            (floor num))
3603                         " " (symbol-name (car unit))
3604                         (if (> num 1) "s" ""))
3605               (setq prev t
3606                     segments (1+ segments)))))
3607         article-time-units "")
3608        ;; If dates are odd, then it might appear like the
3609        ;; article was sent in the future.
3610        (if (> real-sec 0)
3611            " ago"
3612          " in the future"))))))
3613
3614 (defun article-date-local (&optional highlight)
3615   "Convert the current article date to the local timezone."
3616   (interactive (list t))
3617   (article-date-ut 'local highlight))
3618
3619 (defun article-date-english (&optional highlight)
3620   "Convert the current article date to something that is proper English."
3621   (interactive (list t))
3622   (article-date-ut 'english highlight))
3623
3624 (defun article-date-original (&optional highlight)
3625   "Convert the current article date to what it was originally.
3626 This is only useful if you have used some other date conversion
3627 function and want to see what the date was before converting."
3628   (interactive (list t))
3629   (article-date-ut 'original highlight))
3630
3631 (defun article-date-lapsed (&optional highlight)
3632   "Convert the current article date to time lapsed since it was sent."
3633   (interactive (list t))
3634   (article-date-ut 'lapsed highlight))
3635
3636 (defun article-date-combined-lapsed (&optional highlight)
3637   "Convert the current article date to time lapsed since it was sent."
3638   (interactive (list t))
3639   (article-date-ut 'combined-lapsed highlight))
3640
3641 (defun article-update-date-lapsed ()
3642   "Function to be run from a timer to update the lapsed time line."
3643   (save-match-data
3644     (let ((buffer (current-buffer)))
3645       (ignore-errors
3646         (walk-windows
3647          (lambda (w)
3648            (set-buffer (window-buffer w))
3649            (when (eq major-mode 'gnus-article-mode)
3650              (let ((old-line (count-lines (point-min) (point)))
3651                    (old-column (- (point) (line-beginning-position)))
3652                    (window-start
3653                     (window-start (get-buffer-window (current-buffer)))))
3654                (goto-char (point-min))
3655                (while (re-search-forward "^Date:" nil t)
3656                  (let ((type (get-text-property (match-beginning 0)
3657                                                 'gnus-date-type)))
3658                    (when (memq type '(lapsed combined-lapsed user-format))
3659                      (when (and window-start
3660                                 (not (= window-start
3661                                         (save-excursion
3662                                           (forward-line 1)
3663                                           (point)))))
3664                        (setq window-start nil))
3665                      (save-excursion
3666                        (article-date-ut type t (match-beginning 0)))
3667                      (forward-line 1)
3668                      (when window-start
3669                        (set-window-start (get-buffer-window (current-buffer))
3670                                          (point))))))
3671                (goto-char (point-min))
3672                (when (> old-column 0)
3673                  (setq old-line (1- old-line)))
3674                (forward-line old-line)
3675                (end-of-line)
3676                (when (> (current-column) old-column)
3677                  (beginning-of-line)
3678                  (forward-char old-column)))))
3679          nil 'visible))
3680       (set-buffer buffer))))
3681
3682 (defun gnus-start-date-timer (&optional n)
3683   "Start a timer to update the Date headers in the article buffers.
3684 The numerical prefix says how frequently (in seconds) the function
3685 is to run."
3686   (interactive "p")
3687   (unless n
3688     (setq n 1))
3689   (gnus-stop-date-timer)
3690   (setq article-lapsed-timer
3691         (run-at-time 1 n 'article-update-date-lapsed)))
3692
3693 (defun gnus-stop-date-timer ()
3694   "Stop the Date timer."
3695   (interactive)
3696   (when article-lapsed-timer
3697     (nnheader-cancel-timer article-lapsed-timer)
3698     (setq article-lapsed-timer nil)))
3699
3700 (defun article-date-user (&optional highlight)
3701   "Convert the current article date to the user-defined format.
3702 This format is defined by the `gnus-article-time-format' variable."
3703   (interactive (list t))
3704   (article-date-ut 'user highlight))
3705
3706 (defun article-date-iso8601 (&optional highlight)
3707   "Convert the current article date to ISO8601."
3708   (interactive (list t))
3709   (article-date-ut 'iso8601 highlight))
3710
3711 (defmacro gnus-article-save-original-date (&rest forms)
3712   "Save the original date as a text property and evaluate FORMS."
3713   `(let* ((case-fold-search t)
3714           (start (progn
3715                    (goto-char (point-min))
3716                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3717                               (not (bolp)))
3718                      (match-end 0))))
3719           (date (when (and start
3720                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3721                                               nil t))
3722                   (buffer-substring-no-properties start
3723                                                   (match-beginning 0)))))
3724      (goto-char (point-max))
3725      (skip-chars-backward "\n")
3726      (put-text-property (point-min) (point) 'original-date date)
3727      ,@forms
3728      (goto-char (point-max))
3729      (skip-chars-backward "\n")
3730      (put-text-property (point-min) (point) 'original-date date)))
3731
3732 ;; (defun article-show-all ()
3733 ;;   "Show all hidden text in the article buffer."
3734 ;;   (interactive)
3735 ;;   (save-excursion
3736 ;;     (let ((inhibit-read-only t))
3737 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3738
3739 (defun article-remove-leading-whitespace ()
3740   "Remove excessive whitespace from all headers."
3741   (interactive)
3742   (save-excursion
3743     (save-restriction
3744       (let ((inhibit-read-only t))
3745         (article-narrow-to-head)
3746         (goto-char (point-min))
3747         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3748           (delete-region (match-beginning 1) (match-end 1)))))))
3749
3750 (defun article-emphasize (&optional arg)
3751   "Emphasize text according to `gnus-emphasis-alist'."
3752   (interactive (gnus-article-hidden-arg))
3753   (unless (gnus-article-check-hidden-text 'emphasis arg)
3754     (save-excursion
3755       (let ((alist (or
3756                     (condition-case nil
3757                         (with-current-buffer gnus-summary-buffer
3758                           gnus-article-emphasis-alist)
3759                       (error))
3760                     gnus-emphasis-alist))
3761             (inhibit-read-only t)
3762             (props (append '(article-type emphasis)
3763                            gnus-hidden-properties))
3764             regexp elem beg invisible visible face)
3765         (article-goto-body)
3766         (setq beg (point))
3767         (while (setq elem (pop alist))
3768           (goto-char beg)
3769           (setq regexp (car elem)
3770                 invisible (nth 1 elem)
3771                 visible (nth 2 elem)
3772                 face (nth 3 elem))
3773           (while (re-search-forward regexp nil t)
3774             (when (and (match-beginning visible) (match-beginning invisible))
3775               (gnus-article-hide-text
3776                (match-beginning invisible) (match-end invisible) props)
3777               (gnus-article-unhide-text-type
3778                (match-beginning visible) (match-end visible) 'emphasis)
3779               (gnus-put-overlay-excluding-newlines
3780                (match-beginning visible) (match-end visible) 'face face)
3781               (gnus-add-wash-type 'emphasis)
3782               (goto-char (match-end invisible)))))))))
3783
3784 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3785   "Setup newsgroup emphasis alist."
3786   (unless gnus-article-emphasis-alist
3787     (let ((name (and gnus-newsgroup-name
3788                      (gnus-group-real-name gnus-newsgroup-name))))
3789       (make-local-variable 'gnus-article-emphasis-alist)
3790       (setq gnus-article-emphasis-alist
3791             (nconc
3792              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3793                (while (setq elem (pop alist))
3794                  (when (and name (string-match (car elem) name))
3795                    (setq alist nil
3796                          highlight (copy-sequence (cdr elem)))))
3797                highlight)
3798              (copy-sequence highlight-words)
3799              (if gnus-newsgroup-name
3800                  (copy-sequence (gnus-group-find-parameter
3801                                  gnus-newsgroup-name 'highlight-words t)))
3802              gnus-emphasis-alist)))))
3803
3804 (defvar gnus-summary-article-menu)
3805 (defvar gnus-summary-post-menu)
3806
3807 ;;; Saving functions.
3808
3809 (defun gnus-article-save (save-buffer file &optional num)
3810   "Save the currently selected article."
3811   (when (or (get gnus-default-article-saver :headers)
3812             (not gnus-save-all-headers))
3813     ;; Remove headers according to `gnus-saved-headers' or the value
3814     ;; of the `:headers' property that the saver function might have.
3815     (let ((gnus-visible-headers
3816            (or (symbol-value (get gnus-default-article-saver :headers))
3817                gnus-saved-headers gnus-visible-headers))
3818           ;; Ignore group parameter.  See `article-hide-headers'.
3819           (gnus-summary-buffer nil))
3820       (with-current-buffer save-buffer
3821         (article-hide-headers 1 t))))
3822   (save-window-excursion
3823     (if (not gnus-default-article-saver)
3824         (error "No default saver is defined")
3825       ;; !!! Magic!  The saving functions all save
3826       ;; `gnus-save-article-buffer' (or so they think), but we
3827       ;; bind that variable to our save-buffer.
3828       (set-buffer gnus-article-buffer)
3829       (let* ((gnus-save-article-buffer save-buffer)
3830              (filename
3831               (cond
3832                ((not gnus-prompt-before-saving) 'default)
3833                ((eq gnus-prompt-before-saving 'always) nil)
3834                (t file)))
3835              (gnus-number-of-articles-to-be-saved
3836               (when (eq gnus-prompt-before-saving t)
3837                 num)))                  ; Magic
3838         (set-buffer gnus-article-current-summary)
3839         (funcall gnus-default-article-saver filename)))))
3840
3841 (defun gnus-read-save-file-name (prompt &optional filename
3842                                         function group headers variable
3843                                         dir-var)
3844   (let ((default-name
3845           (funcall function group headers (symbol-value variable)))
3846         result)
3847     (setq result
3848           (expand-file-name
3849            (cond
3850             ((eq filename 'default)
3851              default-name)
3852             ((eq filename t)
3853              default-name)
3854             (filename filename)
3855             (t
3856              (when (symbol-value dir-var)
3857                (setq default-name (expand-file-name
3858                                    (file-name-nondirectory default-name)
3859                                    (symbol-value dir-var))))
3860              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3861                     (prompt
3862                      (format prompt
3863                              (if (and gnus-number-of-articles-to-be-saved
3864                                       (> gnus-number-of-articles-to-be-saved 1))
3865                                  (format "these %d articles"
3866                                          gnus-number-of-articles-to-be-saved)
3867                                "this article")))
3868                     (file
3869                      ;; Let the split methods have their say.
3870                      (cond
3871                       ;; No split name was found.
3872                       ((null split-name)
3873                        (read-file-name
3874                         (concat prompt " (default "
3875                                 (file-name-nondirectory default-name) "): ")
3876                         (file-name-directory default-name)
3877                         default-name))
3878                       ;; A single group name is returned.
3879                       ((stringp split-name)
3880                        (setq default-name
3881                              (funcall function split-name headers
3882                                       (symbol-value variable)))
3883                        (read-file-name
3884                         (concat prompt " (default "
3885                                 (file-name-nondirectory default-name) "): ")
3886                         (file-name-directory default-name)
3887                         default-name))
3888                       ;; A single split name was found
3889                       ((= 1 (length split-name))
3890                        (let* ((name (expand-file-name
3891                                      (car split-name)
3892                                      gnus-article-save-directory))
3893                               (dir (cond ((file-directory-p name)
3894                                           (file-name-as-directory name))
3895                                          ((file-exists-p name) name)
3896                                          (t gnus-article-save-directory))))
3897                          (read-file-name
3898                           (concat prompt " (default " name "): ")
3899                           dir name)))
3900                       ;; A list of splits was found.
3901                       (t
3902                        (setq split-name (nreverse split-name))
3903                        (let (result)
3904                          (let ((file-name-history
3905                                 (nconc split-name file-name-history)))
3906                            (setq result
3907                                  (expand-file-name
3908                                   (read-file-name
3909                                    (concat prompt " (`M-p' for defaults): ")
3910                                    gnus-article-save-directory
3911                                    (car split-name))
3912                                   gnus-article-save-directory)))
3913                          (car (push result file-name-history)))))))
3914                ;; Create the directory.
3915                (gnus-make-directory (file-name-directory file))
3916                ;; If we have read a directory, we append the default file name.
3917                (when (file-directory-p file)
3918                  (setq file (expand-file-name (file-name-nondirectory
3919                                                default-name)
3920                                               (file-name-as-directory file))))
3921                ;; Possibly translate some characters.
3922                (nnheader-translate-file-chars file))))))
3923     (gnus-make-directory (file-name-directory result))
3924     (when variable
3925       (set variable result))
3926     (when dir-var
3927       (set dir-var (file-name-directory result)))
3928     result))
3929
3930 (defun gnus-article-archive-name (group)
3931   "Return the first instance of an \"Archive-name\" in the current buffer."
3932   (let ((case-fold-search t))
3933     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3934       (nnheader-concat gnus-article-save-directory
3935                        (match-string 1)))))
3936
3937 (defun gnus-article-nndoc-name (group)
3938   "If GROUP is an nndoc group, return the name of the parent group."
3939   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3940     (gnus-group-get-parameter group 'save-article-group)))
3941
3942 (defun gnus-summary-save-in-rmail (&optional filename)
3943   "Append this article to Rmail file.
3944 Optional argument FILENAME specifies file name.
3945 Directory to save to is default to `gnus-article-save-directory'."
3946   (setq filename (gnus-read-save-file-name
3947                   "Save %s in rmail file" filename
3948                   gnus-rmail-save-name gnus-newsgroup-name
3949                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3950   (with-current-buffer gnus-save-article-buffer
3951     (save-excursion
3952       (save-restriction
3953         (widen)
3954         ;; Note that unlike gnus-summary-save-in-mail, there is no
3955         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3956         ;; not use Babyl.
3957         (gnus-output-to-rmail filename))))
3958   filename)
3959
3960 (defun gnus-summary-save-in-mail (&optional filename)
3961   "Append this article to Unix mail file.
3962 Optional argument FILENAME specifies file name.
3963 Directory to save to is default to `gnus-article-save-directory'."
3964   (setq filename (gnus-read-save-file-name
3965                   "Save %s in Unix mail file" filename
3966                   gnus-mail-save-name gnus-newsgroup-name
3967                   gnus-current-headers 'gnus-newsgroup-last-mail))
3968   (with-current-buffer gnus-save-article-buffer
3969     (save-excursion
3970       (save-restriction
3971         (widen)
3972         (if (and (file-readable-p filename)
3973                  (file-regular-p filename)
3974                  (mail-file-babyl-p filename))
3975             (gnus-output-to-rmail filename)
3976           (gnus-output-to-mail filename)))))
3977   filename)
3978
3979 (put 'gnus-summary-save-in-file :decode t)
3980 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3981 (defun gnus-summary-save-in-file (&optional filename overwrite)
3982   "Append this article to file.
3983 Optional argument FILENAME specifies file name.
3984 Directory to save to is default to `gnus-article-save-directory'."
3985   (setq filename (gnus-read-save-file-name
3986                   "Save %s in file" filename
3987                   gnus-file-save-name gnus-newsgroup-name
3988                   gnus-current-headers 'gnus-newsgroup-last-file))
3989   (with-current-buffer gnus-save-article-buffer
3990     (save-excursion
3991       (save-restriction
3992         (widen)
3993         (when (and overwrite
3994                    (file-exists-p filename))
3995           (delete-file filename))
3996         (gnus-output-to-file filename))))
3997   filename)
3998
3999 (put 'gnus-summary-write-to-file :decode t)
4000 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4001 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4002 (defun gnus-summary-write-to-file (&optional filename)
4003   "Write this article to a file, overwriting it if the file exists.
4004 Optional argument FILENAME specifies file name.
4005 The directory to save in defaults to `gnus-article-save-directory'."
4006   (setq filename (gnus-read-save-file-name
4007                   "Save %s in file" filename
4008                   gnus-file-save-name gnus-newsgroup-name
4009                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4010   (gnus-summary-save-in-file filename t))
4011
4012 (put 'gnus-summary-save-body-in-file :decode t)
4013 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4014   "Append this article body to a file.
4015 Optional argument FILENAME specifies file name.
4016 The directory to save in defaults to `gnus-article-save-directory'."
4017   (setq filename (gnus-read-save-file-name
4018                   "Save %s body in file" filename
4019                   gnus-file-save-name gnus-newsgroup-name
4020                   gnus-current-headers 'gnus-newsgroup-last-file))
4021   (with-current-buffer gnus-save-article-buffer
4022     (save-excursion
4023       (save-restriction
4024         (widen)
4025         (when (article-goto-body)
4026           (narrow-to-region (point) (point-max)))
4027         (when (and overwrite
4028                    (file-exists-p filename))
4029           (delete-file filename))
4030         (gnus-output-to-file filename))))
4031   filename)
4032
4033 (put 'gnus-summary-write-body-to-file :decode t)
4034 (put 'gnus-summary-write-body-to-file
4035      :function 'gnus-summary-save-body-in-file)
4036 (defun gnus-summary-write-body-to-file (&optional filename)
4037   "Write this article body to a file, overwriting it if the file exists.
4038 Optional argument FILENAME specifies file name.
4039 The directory to save in defaults to `gnus-article-save-directory'."
4040   (setq filename (gnus-read-save-file-name
4041                   "Save %s body in file" filename
4042                   gnus-file-save-name gnus-newsgroup-name
4043                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4044   (gnus-summary-save-body-in-file filename t))
4045
4046 (put 'gnus-summary-save-in-pipe :decode t)
4047 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4048 (defun gnus-summary-save-in-pipe (&optional command raw)
4049   "Pipe this article to subprocess COMMAND.
4050 Valid values for COMMAND include:
4051   a string
4052     The executable command name and possibly arguments.
4053   nil
4054     You will be prompted for the command in the minibuffer.
4055   the symbol `default'
4056     It will be replaced with the command which the variable
4057     `gnus-summary-pipe-output-default-command' holds or the command
4058     last used for saving.
4059 Non-nil value for RAW overrides `:decode' and `:headers' properties
4060 and the raw article including all headers will be piped."
4061   (let ((article (gnus-summary-article-number))
4062         (decode (unless raw
4063                   (get 'gnus-summary-save-in-pipe :decode)))
4064         save-buffer default)
4065     (if article
4066         (if (vectorp (gnus-summary-article-header article))
4067             (save-current-buffer
4068               (gnus-summary-select-article decode decode nil article)
4069               (insert-buffer-substring
4070                (prog1
4071                    (if decode
4072                        gnus-article-buffer
4073                      gnus-original-article-buffer)
4074                  (setq save-buffer
4075                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4076               ;; Remove unwanted headers.
4077               (when (and (not raw)
4078                          (or (get 'gnus-summary-save-in-pipe :headers)
4079                              (not gnus-save-all-headers)))
4080                 (let ((gnus-visible-headers
4081                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4082                                               :headers))
4083                            gnus-saved-headers gnus-visible-headers))
4084                       (gnus-summary-buffer nil))
4085                   (article-hide-headers 1 t))))
4086           (error "%d is not a real article" article))
4087       (error "No article to pipe"))
4088     (setq default (or gnus-summary-pipe-output-default-command
4089                       gnus-last-shell-command))
4090     (unless (stringp command)
4091       (setq command
4092             (if (and (eq command 'default) default)
4093                 default
4094               (gnus-read-shell-command "Shell command on this article: "
4095                                        default))))
4096     (when (string-equal command "")
4097       (if default
4098           (setq command default)
4099         (error "A command is required")))
4100     (with-current-buffer save-buffer
4101       (save-restriction
4102         (widen)
4103         (shell-command-on-region (point-min) (point-max) command nil)))
4104     (gnus-kill-buffer save-buffer))
4105   (setq gnus-summary-pipe-output-default-command command))
4106
4107 (defun gnus-summary-pipe-to-muttprint (&optional command)
4108   "Pipe this article to muttprint."
4109   (unless (stringp command)
4110     (setq command (read-string
4111                    "Print using command: " gnus-summary-muttprint-program
4112                    nil gnus-summary-muttprint-program)))
4113   (let ((gnus-summary-pipe-output-default-command
4114          gnus-summary-pipe-output-default-command))
4115     (gnus-summary-save-in-pipe command))
4116   (setq gnus-summary-muttprint-program command))
4117
4118 ;;; Article file names when saving.
4119
4120 (defun gnus-capitalize-newsgroup (newsgroup)
4121   "Capitalize NEWSGROUP name."
4122   (when (not (zerop (length newsgroup)))
4123     (concat (char-to-string (upcase (aref newsgroup 0)))
4124             (substring newsgroup 1))))
4125
4126 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4127   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4128 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4129 Otherwise, it is like ~/News/news/group/num."
4130   (let ((default
4131           (expand-file-name
4132            (concat (if (gnus-use-long-file-name 'not-save)
4133                        (gnus-capitalize-newsgroup newsgroup)
4134                      (gnus-newsgroup-directory-form newsgroup))
4135                    "/" (int-to-string (mail-header-number headers)))
4136            gnus-article-save-directory)))
4137     (if (and last-file
4138              (string-equal (file-name-directory default)
4139                            (file-name-directory last-file))
4140              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4141         default
4142       (or last-file default))))
4143
4144 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4145   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4146 If variable `gnus-use-long-file-name' is non-nil, it is
4147 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4148   (let ((default
4149           (expand-file-name
4150            (concat (if (gnus-use-long-file-name 'not-save)
4151                        newsgroup
4152                      (gnus-newsgroup-directory-form newsgroup))
4153                    "/" (int-to-string (mail-header-number headers)))
4154            gnus-article-save-directory)))
4155     (if (and last-file
4156              (string-equal (file-name-directory default)
4157                            (file-name-directory last-file))
4158              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4159         default
4160       (or last-file default))))
4161
4162 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4163   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4164 If variable `gnus-use-long-file-name' is non-nil, it is
4165 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4166   (or last-file
4167       (expand-file-name
4168        (if (gnus-use-long-file-name 'not-save)
4169            newsgroup
4170          (file-relative-name
4171           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4172           default-directory))
4173        gnus-article-save-directory)))
4174
4175 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4176   "Generate file name from sender."
4177   (let ((from (mail-header-from headers)))
4178     (expand-file-name
4179      (if (and from (string-match "\\([^ <]+\\)@" from))
4180          (match-string 1 from)
4181        "nobody")
4182      gnus-article-save-directory)))
4183
4184 (defun article-verify-x-pgp-sig ()
4185   "Verify X-PGP-Sig."
4186   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4187   (interactive)
4188   (if (gnus-buffer-live-p gnus-original-article-buffer)
4189       (let ((sig (with-current-buffer gnus-original-article-buffer
4190                    (gnus-fetch-field "X-PGP-Sig")))
4191             items info headers)
4192         (when (and sig
4193                    mml2015-use
4194                    (mml2015-clear-verify-function))
4195           (with-temp-buffer
4196             (insert-buffer-substring gnus-original-article-buffer)
4197             (setq items (split-string sig))
4198             (message-narrow-to-head)
4199             (let ((inhibit-point-motion-hooks t)
4200                   (case-fold-search t))
4201               ;; Don't verify multiple headers.
4202               (setq headers (mapconcat (lambda (header)
4203                                          (concat header ": "
4204                                                  (mail-fetch-field header)
4205                                                  "\n"))
4206                                        (split-string (nth 1 items) ",") "")))
4207             (delete-region (point-min) (point-max))
4208             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4209             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4210             (insert headers)
4211             (widen)
4212             (forward-line)
4213             (while (not (eobp))
4214               (if (looking-at "^-")
4215                   (insert "- "))
4216               (forward-line))
4217             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4218             (insert "Version: " (car items) "\n\n")
4219             (insert (mapconcat 'identity (cddr items) "\n"))
4220             (insert "\n-----END PGP SIGNATURE-----\n")
4221             (let ((mm-security-handle (list (format "multipart/signed"))))
4222               (mml2015-clean-buffer)
4223               (let ((coding-system-for-write (or gnus-newsgroup-charset
4224                                                  'iso-8859-1)))
4225                 (funcall (mml2015-clear-verify-function)))
4226               (setq info
4227                     (or (mm-handle-multipart-ctl-parameter
4228                          mm-security-handle 'gnus-details)
4229                         (mm-handle-multipart-ctl-parameter
4230                          mm-security-handle 'gnus-info)))))
4231           (when info
4232             (let ((inhibit-read-only t) bface eface)
4233               (save-restriction
4234                 (message-narrow-to-head)
4235                 (goto-char (point-max))
4236                 (forward-line -1)
4237                 (setq bface (get-text-property (point-at-bol) 'face)
4238                       eface (get-text-property (1- (point-at-eol)) 'face))
4239                 (message-remove-header "X-Gnus-PGP-Verify")
4240                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4241                     (forward-line)
4242                   (goto-char (point-max)))
4243                 (narrow-to-region (point) (point))
4244                 (insert "X-Gnus-PGP-Verify: " info "\n")
4245                 (goto-char (point-min))
4246                 (forward-line)
4247                 (while (not (eobp))
4248                   (if (not (looking-at "^[ \t]"))
4249                       (insert " "))
4250                   (forward-line))
4251                 ;; Do highlighting.
4252                 (goto-char (point-min))
4253                 (when (looking-at "\\([^:]+\\): *")
4254                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4255                                      'face bface)
4256                   (put-text-property (match-end 0) (point-max)
4257                                      'face eface)))))))))
4258
4259 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4260
4261 (defun article-verify-cancel-lock ()
4262   "Verify Cancel-Lock header."
4263   (interactive)
4264   (if (gnus-buffer-live-p gnus-original-article-buffer)
4265       (canlock-verify gnus-original-article-buffer)))
4266
4267 (eval-and-compile
4268   (mapc
4269    (lambda (func)
4270      (let (afunc gfunc)
4271        (if (consp func)
4272            (setq afunc (car func)
4273                  gfunc (cdr func))
4274          (setq afunc func
4275                gfunc (intern (format "gnus-%s" func))))
4276        (defalias gfunc
4277          (when (fboundp afunc)
4278            `(lambda (&optional interactive &rest args)
4279               ,(documentation afunc t)
4280               (interactive (list t))
4281               (with-current-buffer gnus-article-buffer
4282                 (if interactive
4283                     (call-interactively ',afunc)
4284                   (apply ',afunc args))))))))
4285    '(article-hide-headers
4286      article-verify-x-pgp-sig
4287      article-verify-cancel-lock
4288      article-hide-boring-headers
4289      article-treat-overstrike
4290      article-treat-ansi-sequences
4291      article-fill-long-lines
4292      article-capitalize-sentences
4293      article-remove-cr
4294      article-remove-leading-whitespace
4295      article-display-x-face
4296      article-display-face
4297      article-de-quoted-unreadable
4298      article-de-base64-unreadable
4299      article-decode-HZ
4300      article-wash-html
4301      article-unsplit-urls
4302      article-hide-list-identifiers
4303      article-strip-banner
4304      article-babel
4305      article-hide-pem
4306      article-hide-signature
4307      article-strip-headers-in-body
4308      article-remove-trailing-blank-lines
4309      article-strip-leading-blank-lines
4310      article-strip-multiple-blank-lines
4311      article-strip-leading-space
4312      article-strip-trailing-space
4313      article-strip-blank-lines
4314      article-strip-all-blank-lines
4315      article-date-local
4316      article-date-english
4317      article-date-iso8601
4318      article-date-original
4319      article-treat-date
4320      article-date-ut
4321      article-decode-mime-words
4322      article-decode-charset
4323      article-decode-encoded-words
4324      article-date-user
4325      article-date-lapsed
4326      article-date-combined-lapsed
4327      article-emphasize
4328      article-treat-dumbquotes
4329      article-treat-non-ascii
4330      article-normalize-headers
4331      ;;(article-show-all . gnus-article-show-all-headers)
4332      )))
4333 \f
4334 ;;;
4335 ;;; Gnus article mode
4336 ;;;
4337
4338 (put 'gnus-article-mode 'mode-class 'special)
4339
4340 (set-keymap-parent gnus-article-mode-map widget-keymap)
4341
4342 (gnus-define-keys gnus-article-mode-map
4343   " " gnus-article-goto-next-page
4344   "\177" gnus-article-goto-prev-page
4345   [delete] gnus-article-goto-prev-page
4346   [backspace] gnus-article-goto-prev-page
4347   "\C-c^" gnus-article-refer-article
4348   "h" gnus-article-show-summary
4349   "s" gnus-article-show-summary
4350   "\C-c\C-m" gnus-article-mail
4351   "?" gnus-article-describe-briefly
4352   "<" beginning-of-buffer
4353   ">" end-of-buffer
4354   "\C-c\C-i" gnus-info-find-node
4355   "\C-c\C-b" gnus-bug
4356   "R" gnus-article-reply-with-original
4357   "F" gnus-article-followup-with-original
4358   "\C-hk" gnus-article-describe-key
4359   "\C-hc" gnus-article-describe-key-briefly
4360   "\C-hb" gnus-article-describe-bindings
4361
4362   "e" gnus-article-read-summary-keys
4363   "\C-d" gnus-article-read-summary-keys
4364   "\M-*" gnus-article-read-summary-keys
4365   "\M-#" gnus-article-read-summary-keys
4366   "\M-^" gnus-article-read-summary-keys
4367   "\M-g" gnus-article-read-summary-keys)
4368
4369 (substitute-key-definition
4370  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4371
4372 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4373   "W" gnus-article-wide-reply-with-original)
4374 (if (featurep 'xemacs)
4375     (set-keymap-default-binding gnus-article-send-map
4376                                 'gnus-article-read-summary-send-keys)
4377   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4378
4379 (defun gnus-article-make-menu-bar ()
4380   (unless (boundp 'gnus-article-commands-menu)
4381     (gnus-summary-make-menu-bar))
4382   (unless (boundp 'gnus-article-article-menu)
4383     (easy-menu-define
4384      gnus-article-article-menu gnus-article-mode-map ""
4385      '("Article"
4386        ["Scroll forwards" gnus-article-goto-next-page t]
4387        ["Scroll backwards" gnus-article-goto-prev-page t]
4388        ["Show summary" gnus-article-show-summary t]
4389        ["Fetch Message-ID at point" gnus-article-refer-article t]
4390        ["Mail to address at point" gnus-article-mail t]
4391        ["Send a bug report" gnus-bug t]))
4392
4393     (easy-menu-define
4394      gnus-article-treatment-menu gnus-article-mode-map ""
4395      ;; Fixme: this should use :active (and maybe :visible).
4396      '("Treatment"
4397        ["Hide headers" gnus-article-hide-headers t]
4398        ["Hide signature" gnus-article-hide-signature t]
4399        ["Hide citation" gnus-article-hide-citation t]
4400        ["Treat overstrike" gnus-article-treat-overstrike t]
4401        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4402        ["Remove carriage return" gnus-article-remove-cr t]
4403        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4404        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4405        ["Remove base64" gnus-article-de-base64-unreadable t]
4406        ["Treat html" gnus-article-wash-html t]
4407        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4408        ["Decode HZ" gnus-article-decode-HZ t]))
4409
4410     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4411
4412     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4413
4414     (gnus-run-hooks 'gnus-article-menu-hook)))
4415
4416 (defvar bookmark-make-record-function)
4417
4418 (defun gnus-article-mode ()
4419   "Major mode for displaying an article.
4420
4421 All normal editing commands are switched off.
4422
4423 The following commands are available in addition to all summary mode
4424 commands:
4425 \\<gnus-article-mode-map>
4426 \\[gnus-article-next-page]\t Scroll the article one page forwards
4427 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4428 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4429 \\[gnus-article-show-summary]\t Display the summary buffer
4430 \\[gnus-article-mail]\t Send a reply to the address near point
4431 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4432 \\[gnus-info-find-node]\t Go to the Gnus info node"
4433   (interactive)
4434   (kill-all-local-variables)
4435   (gnus-simplify-mode-line)
4436   (setq mode-name "Article")
4437   (setq major-mode 'gnus-article-mode)
4438   (make-local-variable 'minor-mode-alist)
4439   (use-local-map gnus-article-mode-map)
4440   (when (gnus-visual-p 'article-menu 'menu)
4441     (gnus-article-make-menu-bar)
4442     (when gnus-summary-tool-bar-map
4443       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4444   (gnus-update-format-specifications nil 'article-mode)
4445   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4446   (set (make-local-variable 'gnus-page-broken) nil)
4447   (make-local-variable 'gnus-article-current-summary)
4448   (make-local-variable 'gnus-article-mime-handles)
4449   (make-local-variable 'gnus-article-decoded-p)
4450   (make-local-variable 'gnus-article-mime-handle-alist)
4451   (make-local-variable 'gnus-article-wash-types)
4452   (make-local-variable 'gnus-article-image-alist)
4453   (make-local-variable 'gnus-article-charset)
4454   (make-local-variable 'gnus-article-ignored-charsets)
4455   (set (make-local-variable 'bookmark-make-record-function)
4456        'gnus-summary-bookmark-make-record)
4457   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4458   ;; face.
4459   (set (make-local-variable 'nobreak-char-display) nil)
4460   (setq cursor-in-non-selected-windows nil)
4461   (gnus-set-default-directory)
4462   (buffer-disable-undo)
4463   (setq buffer-read-only t
4464         show-trailing-whitespace nil)
4465   (set-syntax-table gnus-article-mode-syntax-table)
4466   (mm-enable-multibyte)
4467   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4468
4469 (defun gnus-article-setup-buffer ()
4470   "Initialize the article buffer."
4471   (let* ((name (if gnus-single-article-buffer "*Article*"
4472                  (concat "*Article " gnus-newsgroup-name "*")))
4473          (original
4474           (progn (string-match "\\*Article" name)
4475                  (concat " *Original Article"
4476                          (substring name (match-end 0))))))
4477     (setq gnus-article-buffer name)
4478     (setq gnus-original-article-buffer original)
4479     (setq gnus-article-mime-handle-alist nil)
4480     (with-current-buffer gnus-summary-buffer
4481       ;; This might be a variable local to the summary buffer.
4482       (unless gnus-single-article-buffer
4483         (setq gnus-article-buffer name)
4484         (setq gnus-original-article-buffer original)
4485         (gnus-set-global-variables)))
4486     (gnus-article-setup-highlight-words)
4487     ;; Init original article buffer.
4488     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4489       (mm-enable-multibyte)
4490       (setq major-mode 'gnus-original-article-mode)
4491       (make-local-variable 'gnus-original-article))
4492     (if (and (get-buffer name)
4493              (with-current-buffer name
4494                (if gnus-article-edit-mode
4495                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4496                        (progn
4497                          (set-buffer-modified-p nil)
4498                          (gnus-kill-buffer name)
4499                          (message "")
4500                          nil)
4501                      (error "Action aborted"))
4502                  t)))
4503         (with-current-buffer name
4504           (set (make-local-variable 'gnus-article-edit-mode) nil)
4505           (when gnus-article-mime-handles
4506             (mm-destroy-parts gnus-article-mime-handles)
4507             (setq gnus-article-mime-handles nil))
4508           ;; Set it to nil in article-buffer!
4509           (setq gnus-article-mime-handle-alist nil)
4510           (buffer-disable-undo)
4511           (setq buffer-read-only t)
4512           (unless (eq major-mode 'gnus-article-mode)
4513             (gnus-article-mode))
4514           (setq truncate-lines gnus-article-truncate-lines)
4515           (current-buffer))
4516       (with-current-buffer (gnus-get-buffer-create name)
4517         (gnus-article-mode)
4518         (setq truncate-lines gnus-article-truncate-lines)
4519         (make-local-variable 'gnus-summary-buffer)
4520         (setq gnus-summary-buffer
4521               (gnus-summary-buffer-name gnus-newsgroup-name))
4522         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4523         (when article-lapsed-timer
4524           (gnus-stop-date-timer))
4525         (when gnus-article-update-date-headers
4526           (gnus-start-date-timer gnus-article-update-date-headers))
4527         (current-buffer)))))
4528
4529 ;; Set article window start at LINE, where LINE is the number of lines
4530 ;; from the head of the article.
4531 (defun gnus-article-set-window-start (&optional line)
4532   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4533     (when article-window
4534       (set-window-start
4535        article-window
4536        (with-current-buffer gnus-article-buffer
4537          (goto-char (point-min))
4538          (if (not line)
4539              (point-min)
4540            (gnus-message 6 "Moved to bookmark")
4541            (search-forward "\n\n" nil t)
4542            (forward-line line)
4543            (point)))))))
4544
4545 (defun gnus-article-prepare (article &optional all-headers header)
4546   "Prepare ARTICLE in article mode buffer.
4547 ARTICLE should either be an article number or a Message-ID.
4548 If ARTICLE is an id, HEADER should be the article headers.
4549 If ALL-HEADERS is non-nil, no headers are hidden."
4550   (save-excursion
4551     ;; Make sure we start in a summary buffer.
4552     (unless (eq major-mode 'gnus-summary-mode)
4553       (set-buffer gnus-summary-buffer))
4554     (setq gnus-summary-buffer (current-buffer))
4555     (let* ((gnus-article (if header (mail-header-number header) article))
4556            (summary-buffer (current-buffer))
4557            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4558            (group gnus-newsgroup-name)
4559            result)
4560       (save-excursion
4561         (gnus-article-setup-buffer)
4562         (set-buffer gnus-article-buffer)
4563         ;; Deactivate active regions.
4564         (when (and (boundp 'transient-mark-mode)
4565                    transient-mark-mode)
4566           (setq mark-active nil))
4567         (if (not (setq result (let ((inhibit-read-only t))
4568                                 (gnus-request-article-this-buffer
4569                                  article group))))
4570             ;; There is no such article.
4571             (save-excursion
4572               (when (and (numberp article)
4573                          (not (memq article gnus-newsgroup-sparse)))
4574                 (setq gnus-article-current
4575                       (cons gnus-newsgroup-name article))
4576                 (set-buffer gnus-summary-buffer)
4577                 (setq gnus-current-article article)
4578                 (if (and (memq article gnus-newsgroup-undownloaded)
4579                          (not (gnus-online (gnus-find-method-for-group
4580                                             gnus-newsgroup-name))))
4581                     (progn
4582                       (gnus-summary-set-agent-mark article)
4583                       (message "Message marked for downloading"))
4584                   (gnus-summary-mark-article article gnus-canceled-mark)
4585                   (unless (memq article gnus-newsgroup-sparse)
4586                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4587           (if (or (eq result 'pseudo)
4588                   (eq result 'nneething))
4589               (progn
4590                 (with-current-buffer summary-buffer
4591                   (push article gnus-newsgroup-history)
4592                   (setq gnus-last-article gnus-current-article
4593                         gnus-current-article 0
4594                         gnus-current-headers nil
4595                         gnus-article-current nil)
4596                   (if (eq result 'nneething)
4597                       (gnus-configure-windows 'summary)
4598                     (gnus-configure-windows 'article))
4599                   (gnus-set-global-variables))
4600                 (let ((gnus-article-mime-handle-alist-1
4601                        gnus-article-mime-handle-alist))
4602                   (gnus-set-mode-line 'article)))
4603             ;; The result from the `request' was an actual article -
4604             ;; or at least some text that is now displayed in the
4605             ;; article buffer.
4606             (when (and (numberp article)
4607                        (not (eq article gnus-current-article)))
4608               ;; Seems like a new article has been selected.
4609               ;; `gnus-current-article' must be an article number.
4610               (with-current-buffer summary-buffer
4611                 (push article gnus-newsgroup-history)
4612                 (setq gnus-last-article gnus-current-article
4613                       gnus-current-article article
4614                       gnus-current-headers
4615                       (gnus-summary-article-header gnus-current-article)
4616                       gnus-article-current
4617                       (cons gnus-newsgroup-name gnus-current-article))
4618                 (unless (vectorp gnus-current-headers)
4619                   (setq gnus-current-headers nil))
4620                 (gnus-summary-goto-subject gnus-current-article)
4621                 (when (gnus-summary-show-thread)
4622                   ;; If the summary buffer really was folded, the
4623                   ;; previous goto may not actually have gone to
4624                   ;; the right article, but the thread root instead.
4625                   ;; So we go again.
4626                   (gnus-summary-goto-subject gnus-current-article))
4627                 (gnus-run-hooks 'gnus-mark-article-hook)
4628                 (gnus-set-mode-line 'summary)
4629                 (when (gnus-visual-p 'article-highlight 'highlight)
4630                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4631                 ;; Set the global newsgroup variables here.
4632                 (gnus-set-global-variables)
4633                 (setq gnus-have-all-headers
4634                       (or all-headers gnus-show-all-headers))))
4635             (save-excursion
4636               (gnus-configure-windows 'article))
4637             (when (or (numberp article)
4638                       (stringp article))
4639               (gnus-article-prepare-display)
4640               ;; Do page break.
4641               (goto-char (point-min))
4642               (when gnus-break-pages
4643                 (gnus-narrow-to-page)))
4644             (let ((gnus-article-mime-handle-alist-1
4645                    gnus-article-mime-handle-alist))
4646               (gnus-set-mode-line 'article))
4647             (article-goto-body)
4648             (unless (bobp)
4649               (forward-line -1))
4650             (set-window-point (get-buffer-window (current-buffer)) (point))
4651             (gnus-configure-windows 'article)
4652             (gnus-run-hooks 'gnus-article-prepare-hook)
4653             t))))))
4654
4655 ;;;###autoload
4656 (defun gnus-article-prepare-display ()
4657   "Make the current buffer look like a nice article."
4658   ;; Hooks for getting information from the article.
4659   ;; This hook must be called before being narrowed.
4660   (let ((gnus-article-buffer (current-buffer))
4661         buffer-read-only
4662         (inhibit-read-only t))
4663     (unless (eq major-mode 'gnus-article-mode)
4664       (gnus-article-mode))
4665     (setq buffer-read-only nil
4666           gnus-article-wash-types nil
4667           gnus-article-image-alist nil)
4668     (gnus-run-hooks 'gnus-tmp-internal-hook)
4669     (when gnus-display-mime-function
4670       (funcall gnus-display-mime-function))))
4671
4672 ;;;
4673 ;;; Gnus Sticky Article Mode
4674 ;;;
4675
4676 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4677   "Mode for sticky articles."
4678   ;; Release bindings that won't work.
4679   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4680                              gnus-sticky-article-mode-map)
4681   (substitute-key-definition 'gnus-article-refer-article 'undefined
4682                              gnus-sticky-article-mode-map)
4683   (dolist (k '("e" "h" "s" "F" "R"))
4684     (define-key gnus-sticky-article-mode-map k nil))
4685   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4686   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4687   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4688   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4689
4690 (defun gnus-sticky-article (arg)
4691   "Make the current article sticky.
4692 If a prefix ARG is given, ask for a name for this sticky article buffer."
4693   (interactive "P")
4694   (gnus-summary-show-thread)
4695   (gnus-summary-select-article nil nil 'pseudo)
4696   (let (new-art-buf-name)
4697     (gnus-eval-in-buffer-window gnus-article-buffer
4698       (setq new-art-buf-name
4699             (concat
4700              "*Sticky Article: "
4701              (if arg
4702                  (read-from-minibuffer "Sticky article buffer name: ")
4703                (gnus-with-article-headers
4704                  (gnus-article-goto-header "subject")
4705                  (setq new-art-buf-name
4706                        (buffer-substring-no-properties
4707                         (line-beginning-position) (line-end-position)))
4708                  (goto-char (point-min))
4709                  (gnus-article-goto-header "from")
4710                  (setq new-art-buf-name
4711                        (concat
4712                         new-art-buf-name ", "
4713                         (buffer-substring-no-properties
4714                          (line-beginning-position) (line-end-position))))
4715                  (goto-char (point-min))
4716                  (gnus-article-goto-header "date")
4717                  (setq new-art-buf-name
4718                        (concat
4719                         new-art-buf-name ", "
4720                         (buffer-substring-no-properties
4721                          (line-beginning-position) (line-end-position))))))
4722              "*"))
4723       (if (and (gnus-buffer-live-p new-art-buf-name)
4724                (with-current-buffer new-art-buf-name
4725                  (eq major-mode 'gnus-sticky-article-mode)))
4726           (switch-to-buffer new-art-buf-name)
4727         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4728       (gnus-sticky-article-mode))
4729     (setq gnus-article-buffer new-art-buf-name))
4730   (gnus-summary-recenter)
4731   (gnus-summary-position-point))
4732
4733 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4734   "Kill the given sticky article BUFFER.
4735 If none is given, assume the current buffer and kill it if it has
4736 `gnus-sticky-article-mode'."
4737   (interactive)
4738   (unless buffer
4739     (setq buffer (current-buffer)))
4740   (with-current-buffer buffer
4741     (when (eq major-mode 'gnus-sticky-article-mode)
4742       (gnus-kill-buffer buffer))))
4743
4744 (defun gnus-kill-sticky-article-buffers (arg)
4745   "Kill all sticky article buffers.
4746 If a prefix ARG is given, ask for confirmation."
4747   (interactive "P")
4748   (dolist (buf (gnus-buffers))
4749     (with-current-buffer buf
4750       (when (eq major-mode 'gnus-sticky-article-mode)
4751         (if (not arg)
4752             (gnus-kill-buffer buf)
4753           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4754             (gnus-kill-buffer buf)))))))
4755
4756 ;;;
4757 ;;; Gnus MIME viewing functions
4758 ;;;
4759
4760 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4761   "Format of the MIME buttons.
4762
4763 Valid specifiers include:
4764 %t  The MIME type
4765 %T  MIME type, along with additional info
4766 %n  The `name' parameter
4767 %d  The description, if any
4768 %l  The length of the encoded part
4769 %p  The part identifier number
4770 %e  Dots if the part isn't displayed
4771
4772 General format specifiers can also be used.  See Info node
4773 `(gnus)Formatting Variables'.")
4774
4775 (defvar gnus-mime-button-line-format-alist
4776   '((?t gnus-tmp-type ?s)
4777     (?T gnus-tmp-type-long ?s)
4778     (?n gnus-tmp-name ?s)
4779     (?d gnus-tmp-description ?s)
4780     (?p gnus-tmp-id ?s)
4781     (?l gnus-tmp-length ?d)
4782     (?e gnus-tmp-dots ?s)))
4783
4784 (defvar gnus-mime-button-commands
4785   '((gnus-article-press-button "\r" "Toggle Display")
4786     (gnus-mime-view-part "v" "View Interactively...")
4787     (gnus-mime-view-part-as-type "t" "View As Type...")
4788     (gnus-mime-view-part-as-charset "C" "View As charset...")
4789     (gnus-mime-save-part "o" "Save...")
4790     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4791     (gnus-mime-replace-part "r" "Replace part")
4792     (gnus-mime-delete-part "d" "Delete part")
4793     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4794     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4795     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4796     (gnus-mime-view-part-externally "e" "View Externally")
4797     (gnus-mime-print-part "p" "Print")
4798     (gnus-mime-pipe-part "|" "Pipe To Command...")
4799     (gnus-mime-action-on-part "." "Take action on the part...")))
4800
4801 (defun gnus-article-mime-part-status ()
4802   (if gnus-article-mime-handle-alist-1
4803       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4804           " (1 part)"
4805         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4806     ""))
4807
4808 (defvar gnus-mime-button-map
4809   (let ((map (make-sparse-keymap)))
4810     (define-key map gnus-mouse-2 'gnus-article-push-button)
4811     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4812     (dolist (c gnus-mime-button-commands)
4813       (define-key map (cadr c) (car c)))
4814     map))
4815
4816 (easy-menu-define
4817   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4818   `("MIME Part"
4819     ,@(mapcar (lambda (c)
4820                 (vector (caddr c) (car c) :active t))
4821               gnus-mime-button-commands)))
4822
4823 (defvar gnus-url-button-commands
4824   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4825
4826 (defvar gnus-url-button-map
4827   (let ((map (make-sparse-keymap)))
4828     (dolist (c gnus-url-button-commands)
4829       (define-key map (cadr c) (car c)))
4830     map))
4831
4832 (easy-menu-define
4833   gnus-url-button-menu gnus-url-button-map "URL button menu."
4834   `("Url Button"
4835     ,@(mapcar (lambda (c)
4836                 (vector (caddr c) (car c) :active t))
4837               gnus-url-button-commands)))
4838
4839 (defmacro gnus-bind-safe-url-regexp (&rest body)
4840   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4841   `(let ((mm-w3m-safe-url-regexp
4842           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4843                                 (gnus-buffer-live-p
4844                                  gnus-article-current-summary))
4845                            (with-current-buffer gnus-article-current-summary
4846                              gnus-newsgroup-name)
4847                          gnus-newsgroup-name)))
4848             (if (cond ((not group)
4849                        ;; Maybe we're in a mml-preview buffer
4850                        ;; and no group is selected.
4851                        t)
4852                       ((stringp gnus-safe-html-newsgroups)
4853                        (string-match gnus-safe-html-newsgroups group))
4854                       ((consp gnus-safe-html-newsgroups)
4855                        (member group gnus-safe-html-newsgroups)))
4856                 nil
4857               mm-w3m-safe-url-regexp))))
4858      ,@body))
4859
4860 (defun gnus-mime-button-menu (event prefix)
4861  "Construct a context-sensitive menu of MIME commands."
4862  (interactive "e\nP")
4863  (save-window-excursion
4864    (let ((pos (event-start event)))
4865      (select-window (posn-window pos))
4866      (goto-char (posn-point pos))
4867      (gnus-article-check-buffer)
4868      (popup-menu gnus-mime-button-menu nil prefix))))
4869
4870 (defun gnus-mime-view-all-parts (&optional handles)
4871   "View all the MIME parts."
4872   (interactive)
4873   (with-current-buffer gnus-article-buffer
4874     (let ((handles (or handles gnus-article-mime-handles))
4875           (mail-parse-charset gnus-newsgroup-charset)
4876           (mail-parse-ignored-charsets
4877            (with-current-buffer gnus-summary-buffer
4878              gnus-newsgroup-ignored-charsets)))
4879       (when handles
4880         (mm-remove-parts handles)
4881         (goto-char (point-min))
4882         (or (search-forward "\n\n") (goto-char (point-max)))
4883         (let ((inhibit-read-only t))
4884           (delete-region (point) (point-max))
4885           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4886
4887 (defun gnus-article-jump-to-part (n)
4888   "Jump to MIME part N."
4889   (interactive "P")
4890   (let ((parts (with-current-buffer gnus-article-buffer
4891                  (length gnus-article-mime-handle-alist))))
4892     (when (zerop parts)
4893       (error "No such part"))
4894     (pop-to-buffer gnus-article-buffer)
4895     (or n
4896         (setq n (if (= parts 1)
4897                     1
4898                   (read-number (format "Jump to part (1..%s): " parts)))))
4899     (unless (and (integerp n) (<= n parts) (>= n 1))
4900       (setq n
4901             (progn
4902               (gnus-message 7 "Invalid part `%s', using %s instead."
4903                             n parts)
4904               parts)))
4905     (gnus-message 9 "Jumping to part %s." n)
4906     (cond ((>= gnus-auto-select-part 1)
4907            (while (and (<= n parts)
4908                        (not (gnus-article-goto-part n)))
4909              (setq n (1+ n))))
4910           ((< gnus-auto-select-part 0)
4911            (while (and (>= n 1)
4912                        (not (gnus-article-goto-part n)))
4913              (setq n (1- n))))
4914           (t
4915            (gnus-article-goto-part n)))))
4916
4917 (defvar gnus-mime-buttonized-part-id nil
4918   "ID of a mime part that should be buttonized.
4919 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4920
4921 (eval-when-compile
4922   (defsubst gnus-article-edit-part (handles &optional current-id)
4923     "Edit an article in order to delete a mime part.
4924 This function is exclusively used by `gnus-mime-save-part-and-strip'
4925 and `gnus-mime-delete-part', and not provided at run-time normally."
4926     (gnus-article-edit-article
4927      `(lambda ()
4928         (buffer-disable-undo)
4929         (erase-buffer)
4930         (let ((mail-parse-charset (or gnus-article-charset
4931                                       ',gnus-newsgroup-charset))
4932               (mail-parse-ignored-charsets
4933                (or gnus-article-ignored-charsets
4934                    ',gnus-newsgroup-ignored-charsets))
4935               (mbl mml-buffer-list))
4936           (setq mml-buffer-list nil)
4937           (insert-buffer-substring gnus-original-article-buffer)
4938           (mime-to-mml ',handles)
4939           (setq gnus-article-mime-handles nil)
4940           (let ((mbl1 mml-buffer-list))
4941             (setq mml-buffer-list mbl)
4942             (set (make-local-variable 'mml-buffer-list) mbl1))
4943           (gnus-make-local-hook 'kill-buffer-hook)
4944           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4945      `(lambda (no-highlight)
4946         (let ((mail-parse-charset (or gnus-article-charset
4947                                       ',gnus-newsgroup-charset))
4948               (message-options message-options)
4949               (message-options-set-recipient)
4950               (mail-parse-ignored-charsets
4951                (or gnus-article-ignored-charsets
4952                    ',gnus-newsgroup-ignored-charsets)))
4953           (mml-to-mime)
4954           (mml-destroy-buffers)
4955           (remove-hook 'kill-buffer-hook
4956                        'mml-destroy-buffers t)
4957           (kill-local-variable 'mml-buffer-list))
4958         (gnus-summary-edit-article-done
4959          ,(or (mail-header-references gnus-current-headers) "")
4960          ,(gnus-group-read-only-p)
4961          ,gnus-summary-buffer no-highlight))
4962      t)
4963     ;; Force buttonizing this part.
4964     (let ((gnus-mime-buttonized-part-id current-id))
4965       (gnus-article-edit-done))
4966     (gnus-configure-windows 'article)
4967     (when (and current-id (integerp gnus-auto-select-part))
4968       (gnus-article-jump-to-part
4969        (min (max (+ current-id gnus-auto-select-part) 1)
4970             (with-current-buffer gnus-article-buffer
4971               (length gnus-article-mime-handle-alist)))))))
4972
4973 (defun gnus-mime-replace-part (file)
4974   "Replace MIME part under point with an external body."
4975   ;; Useful if file has already been saved to disk
4976   (interactive
4977    (list
4978     (read-file-name "Replace MIME part with file: "
4979                     (or mm-default-directory default-directory)
4980                     nil nil)))
4981   (gnus-mime-save-part-and-strip file))
4982
4983 (defun gnus-mime-save-part-and-strip (&optional file)
4984   "Save the MIME part under point then replace it with an external body.
4985 If FILE is given, use it for the external part."
4986   (interactive)
4987   (gnus-article-check-buffer)
4988   (when (gnus-group-read-only-p)
4989     (error "The current group does not support deleting of parts"))
4990   (when (mm-complicated-handles gnus-article-mime-handles)
4991     (error "\
4992 The current article has a complicated MIME structure, giving up..."))
4993   (let* ((data (get-text-property (point) 'gnus-data))
4994          (id (get-text-property (point) 'gnus-part))
4995          param
4996          (handles gnus-article-mime-handles))
4997     (unless file
4998       (setq file
4999             (and data (mm-save-part data "Delete MIME part and save to: "))))
5000     (when file
5001       (with-current-buffer (mm-handle-buffer data)
5002         (erase-buffer)
5003         (insert "Content-Type: " (mm-handle-media-type data))
5004         (mml-insert-parameter-string (cdr (mm-handle-type data))
5005                                      '(charset))
5006         ;; Add a filename for the sake of saving the part again.
5007         (mml-insert-parameter
5008          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5009         (insert "\n")
5010         (insert "Content-ID: " (message-make-message-id) "\n")
5011         (insert "Content-Transfer-Encoding: binary\n")
5012         (insert "\n"))
5013       (setcdr data
5014               (cdr (mm-make-handle nil
5015                                    `("message/external-body"
5016                                      (access-type . "LOCAL-FILE")
5017                                      (name . ,file)))))
5018       ;; (set-buffer gnus-summary-buffer)
5019       (gnus-article-edit-part handles id))))
5020
5021 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5022 ;; parts...>') but with stripping would be nice.
5023
5024 (defun gnus-mime-delete-part ()
5025   "Delete the MIME part under point.
5026 Replace it with some information about the removed part."
5027   (interactive)
5028   (gnus-article-check-buffer)
5029   (when (gnus-group-read-only-p)
5030     (error "The current group does not support deleting of parts"))
5031   (when (mm-complicated-handles gnus-article-mime-handles)
5032     (error "\
5033 The current article has a complicated MIME structure, giving up..."))
5034   (when (or gnus-expert-user
5035             (gnus-yes-or-no-p "\
5036 Deleting parts may malfunction or destroy the article; continue? "))
5037     (let* ((data (get-text-property (point) 'gnus-data))
5038            (id (get-text-property (point) 'gnus-part))
5039            (handles gnus-article-mime-handles)
5040            (description
5041             (let ((desc (mm-handle-description data)))
5042               (when desc
5043                 (mail-decode-encoded-word-string desc))))
5044            (filename (or (mm-handle-filename (mm-handle-disposition data)) "(none)"))
5045            (type (mm-handle-media-type data)))
5046       (unless data
5047         (error "No MIME part under point"))
5048       (with-current-buffer (mm-handle-buffer data)
5049         (let ((bsize (buffer-size)))
5050           (erase-buffer)
5051           (insert
5052            (concat
5053             ",----\n"
5054             "| The following attachment has been deleted:\n"
5055             "|\n"
5056             "| Type:           " type "\n"
5057             "| Filename:       " filename "\n"
5058             "| Size (encoded): " (format "%s byte%s\n"
5059                                          bsize (if (= bsize 1)
5060                                                    ""
5061                                                  "s"))
5062             (when description
5063               (concat    "| Description:    " description "\n"))
5064             "`----\n"))
5065           (setcdr data
5066                   (cdr (mm-make-handle
5067                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5068                         (list "attachment")
5069                         (format "Deleted attachment (%s bytes)" bsize))))))
5070       ;; (set-buffer gnus-summary-buffer)
5071       (gnus-article-edit-part handles id))))
5072
5073 (defun gnus-mime-save-part ()
5074   "Save the MIME part under point."
5075   (interactive)
5076   (gnus-article-check-buffer)
5077   (let ((data (get-text-property (point) 'gnus-data)))
5078     (when data
5079       (mm-save-part data))))
5080
5081 (defun gnus-mime-pipe-part (&optional cmd)
5082   "Pipe the MIME part under point to a process.
5083 Use CMD as the process."
5084   (interactive)
5085   (gnus-article-check-buffer)
5086   (let ((data (get-text-property (point) 'gnus-data)))
5087     (when data
5088       (mm-pipe-part data cmd))))
5089
5090 (defun gnus-mime-view-part ()
5091   "Interactively choose a viewing method for the MIME part under point."
5092   (interactive)
5093   (gnus-article-check-buffer)
5094   (let ((data (get-text-property (point) 'gnus-data)))
5095     (when data
5096       (setq gnus-article-mime-handles
5097             (mm-merge-handles
5098              gnus-article-mime-handles (setq data (copy-sequence data))))
5099       (mm-interactively-view-part data))))
5100
5101 (defun gnus-mime-view-part-as-type-internal ()
5102   (gnus-article-check-buffer)
5103   (let* ((handle (get-text-property (point) 'gnus-data))
5104          (name (or
5105                 ;; Content-Type: foo/bar; name=...
5106                 (mail-content-type-get (mm-handle-type handle) 'name)
5107                 ;; Content-Disposition: attachment; filename=...
5108                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5109          (def-type (and name (mm-default-file-encoding name))))
5110     (or (and def-type (cons def-type 0))
5111         (and handle
5112              (equal (mm-handle-media-supertype handle) "text")
5113              '("text/plain" . 0))
5114         '("application/octet-stream" . 0))))
5115
5116 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5117   "Choose a MIME media type, and view the part as such.
5118 If non-nil, PRED is a predicate to use during completion to limit the
5119 available media-types."
5120   (interactive)
5121   (unless mime-type
5122     (setq mime-type
5123           (let ((default (gnus-mime-view-part-as-type-internal)))
5124             (gnus-completing-read
5125              "View as MIME type"
5126              (if pred
5127                  (gnus-remove-if-not pred (mailcap-mime-types))
5128                (mailcap-mime-types))
5129              nil nil nil
5130              (car default)))))
5131   (gnus-article-check-buffer)
5132   (let ((handle (get-text-property (point) 'gnus-data)))
5133     (when handle
5134       (when (equal (mm-handle-media-type handle) "message/external-body")
5135         (unless (mm-handle-cache handle)
5136           (mm-extern-cache-contents handle))
5137         (setq handle (mm-handle-cache handle)))
5138       (setq handle
5139             (mm-make-handle (mm-handle-buffer handle)
5140                             (cons mime-type (cdr (mm-handle-type handle)))
5141                             (mm-handle-encoding handle)
5142                             (mm-handle-undisplayer handle)
5143                             (mm-handle-disposition handle)
5144                             (mm-handle-description handle)
5145                             nil
5146                             (mm-handle-id handle)))
5147       (setq gnus-article-mime-handles
5148             (mm-merge-handles gnus-article-mime-handles handle))
5149       (when (mm-handle-displayed-p handle)
5150         (mm-remove-part handle))
5151       (gnus-mm-display-part handle))))
5152
5153 (defun gnus-mime-copy-part (&optional handle arg)
5154   "Put the MIME part under point into a new buffer.
5155 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5156 are decompressed."
5157   (interactive (list nil current-prefix-arg))
5158   (gnus-article-check-buffer)
5159   (unless handle
5160     (setq handle (get-text-property (point) 'gnus-data)))
5161   (when handle
5162     (let ((filename (mm-handle-filename handle))
5163           contents dont-decode charset coding-system)
5164       (mm-with-unibyte-buffer
5165         (mm-insert-part handle)
5166         (setq contents (or (condition-case nil
5167                                (mm-decompress-buffer filename nil 'sig)
5168                              (error
5169                               (setq dont-decode t)
5170                               nil))
5171                            (buffer-string))))
5172       (setq filename (cond (filename (file-name-nondirectory filename))
5173                            (dont-decode "*raw data*")
5174                            (t "*decoded*")))
5175       (cond
5176        (dont-decode)
5177        ((not arg)
5178         (unless (setq charset (mail-content-type-get
5179                                (mm-handle-type handle) 'charset))
5180           (unless (setq coding-system (mm-with-unibyte-buffer
5181                                         (insert contents)
5182                                         (mm-find-buffer-file-coding-system)))
5183             (setq charset gnus-newsgroup-charset))))
5184        ((numberp arg)
5185         (setq charset (or (cdr (assq arg
5186                                      gnus-summary-show-article-charset-alist))
5187                           (mm-read-coding-system "Charset: ")))))
5188       (switch-to-buffer (generate-new-buffer filename))
5189       (if (or coding-system
5190               (and charset
5191                    (setq coding-system (mm-charset-to-coding-system charset))
5192                    (not (eq coding-system 'ascii))))
5193           (progn
5194             (mm-enable-multibyte)
5195             (insert (mm-decode-coding-string contents coding-system))
5196             (setq buffer-file-coding-system
5197                   (if (boundp 'last-coding-system-used)
5198                       (symbol-value 'last-coding-system-used)
5199                     coding-system)))
5200         (mm-disable-multibyte)
5201         (insert contents)
5202         (setq buffer-file-coding-system mm-binary-coding-system))
5203       ;; We do it this way to make `normal-mode' set the appropriate mode.
5204       (unwind-protect
5205           (progn
5206             (setq buffer-file-name (expand-file-name filename))
5207             (normal-mode))
5208         (setq buffer-file-name nil))
5209       (goto-char (point-min)))))
5210
5211 (defun gnus-mime-print-part (&optional handle filename)
5212   "Print the MIME part under point."
5213   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5214   (gnus-article-check-buffer)
5215   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5216          (contents (and handle (mm-get-part handle)))
5217          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5218          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5219     (when contents
5220         (if printer
5221             (unwind-protect
5222                 (progn
5223                   (mm-save-part-to-file handle file)
5224                   (call-process shell-file-name nil
5225                                 (generate-new-buffer " *mm*")
5226                                 nil
5227                                 shell-command-switch
5228                                 (mm-mailcap-command
5229                                  printer file (mm-handle-type handle))))
5230               (delete-file file))
5231           (with-temp-buffer
5232             (insert contents)
5233             (gnus-print-buffer))
5234           (ps-despool filename)))))
5235
5236 (defun gnus-mime-inline-part (&optional handle arg)
5237   "Insert the MIME part under point into the current buffer.
5238 Compressed files like .gz and .bz2 are decompressed."
5239   (interactive (list nil current-prefix-arg))
5240   (gnus-article-check-buffer)
5241   (unless handle
5242     (setq handle (get-text-property (point) 'gnus-data)))
5243   (when handle
5244     (let ((b (point))
5245           (inhibit-read-only t)
5246           contents charset coding-system)
5247       (if (and (not arg) (mm-handle-undisplayer handle))
5248           (mm-remove-part handle)
5249         (mm-with-unibyte-buffer
5250           (mm-insert-part handle)
5251           (setq contents
5252                 (or (mm-decompress-buffer (mm-handle-filename handle) nil t)
5253                     (buffer-string))))
5254         (cond
5255          ((not arg)
5256           (unless (setq charset (mail-content-type-get
5257                                  (mm-handle-type handle) 'charset))
5258             (unless (setq coding-system
5259                           (mm-with-unibyte-buffer
5260                             (insert contents)
5261                             (mm-find-buffer-file-coding-system)))
5262               (setq charset gnus-newsgroup-charset))))
5263          ((numberp arg)
5264           (if (mm-handle-undisplayer handle)
5265               (mm-remove-part handle))
5266           (setq charset
5267                 (or (cdr (assq arg
5268                                gnus-summary-show-article-charset-alist))
5269                     (mm-read-coding-system "Charset: "))))
5270          (t
5271           (if (mm-handle-undisplayer handle)
5272               (mm-remove-part handle))))
5273         (forward-line 2)
5274         (mm-display-inline handle)
5275         (goto-char b)))))
5276
5277 (defun gnus-mime-set-charset-parameters (handle charset)
5278   "Set CHARSET to parameters in HANDLE.
5279 CHARSET may either be a string or a symbol."
5280   (unless (stringp charset)
5281     (setq charset (symbol-name charset)))
5282   (if (stringp (car handle))
5283       (dolist (h (cdr handle))
5284         (gnus-mime-set-charset-parameters h charset))
5285     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5286                                             "message/external-body")
5287                                      (progn
5288                                        (unless (mm-handle-cache handle)
5289                                          (mm-extern-cache-contents handle))
5290                                        (mm-handle-cache handle))
5291                                    handle)))
5292            (param (assq 'charset (cdr type))))
5293       (if param
5294           (setcdr param charset)
5295         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5296
5297 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5298   "Insert the MIME part under point into the current buffer using the
5299 specified charset."
5300   (interactive (list nil current-prefix-arg))
5301   (gnus-article-check-buffer)
5302   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5303         (fun (get-text-property (point) 'gnus-callback))
5304         (gnus-newsgroup-ignored-charsets 'gnus-all)
5305         charset form preferred parts)
5306     (when handle
5307       (when (prog1
5308                 (and fun
5309                      (setq charset
5310                            (or (cdr (assq
5311                                      arg
5312                                      gnus-summary-show-article-charset-alist))
5313                                (mm-read-coding-system "Charset: "))))
5314               (if (mm-handle-undisplayer handle)
5315                   (mm-remove-part handle)))
5316         (gnus-mime-set-charset-parameters handle charset)
5317         (when (and (consp (setq form (cdr-safe fun)))
5318                    (setq form (ignore-errors
5319                                 (assq 'gnus-mime-display-alternative form)))
5320                    (setq preferred (caddr form))
5321                    (progn
5322                      (when (eq (car preferred) 'quote)
5323                        (setq preferred (cadr preferred)))
5324                      (not (equal preferred
5325                                  (get-text-property (point) 'gnus-data))))
5326                    (setq parts (get-text-property (point) 'gnus-part))
5327                    (setq parts (cdr (assq parts
5328                                           gnus-article-mime-handle-alist)))
5329                    (equal (mm-handle-media-type parts) "multipart/alternative")
5330                    (setq parts (reverse (cdr parts))))
5331           (setcar (cddr form)
5332                   (list 'quote (or (cadr (member preferred parts))
5333                                    (car parts)))))
5334         (funcall fun handle)))))
5335
5336 (defun gnus-mime-view-part-externally (&optional handle)
5337   "View the MIME part under point with an external viewer."
5338   (interactive)
5339   (gnus-article-check-buffer)
5340   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5341          (mm-user-display-methods nil)
5342          (mm-inlined-types nil)
5343          (mail-parse-charset gnus-newsgroup-charset)
5344          (mail-parse-ignored-charsets
5345           (with-current-buffer gnus-summary-buffer
5346             gnus-newsgroup-ignored-charsets))
5347          (type (mm-handle-media-type handle))
5348          (method (mailcap-mime-info type))
5349          (mm-enable-external t))
5350     (if (not (stringp method))
5351         (gnus-mime-view-part-as-type
5352          nil (lambda (type) (stringp (mailcap-mime-info type))))
5353       (when handle
5354         (mm-display-part handle nil t)))))
5355
5356 (defun gnus-mime-view-part-internally (&optional handle)
5357   "View the MIME part under point with an internal viewer.
5358 If no internal viewer is available, use an external viewer."
5359   (interactive)
5360   (gnus-article-check-buffer)
5361   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5362          (mm-inlined-types '(".*"))
5363          (mm-inline-large-images t)
5364          (mail-parse-charset gnus-newsgroup-charset)
5365          (mail-parse-ignored-charsets
5366           (with-current-buffer gnus-summary-buffer
5367             gnus-newsgroup-ignored-charsets))
5368          (inhibit-read-only t))
5369     (if (not (mm-inlinable-p handle))
5370         (gnus-mime-view-part-as-type
5371          nil (lambda (type) (mm-inlinable-p handle type)))
5372       (when handle
5373         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5374
5375 (defun gnus-mime-action-on-part (&optional action)
5376   "Do something with the MIME attachment at \(point\)."
5377   (interactive
5378    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5379   (gnus-article-check-buffer)
5380   (let ((action-pair (assoc action gnus-mime-action-alist)))
5381     (if action-pair
5382         (funcall (cdr action-pair)))))
5383
5384 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5385   "Call FUNCTION on MIME part N.
5386 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5387 If INTERACTIVE, call FUNCTION interactivly."
5388   (let (window frame)
5389     ;; Check whether the article is displayed.
5390     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5391                  (setq window (get-buffer-window gnus-article-buffer t))
5392                  (frame-visible-p (setq frame (window-frame window))))
5393       (error "No article is displayed"))
5394     (with-current-buffer gnus-article-buffer
5395       ;; Check whether the article displays the right contents.
5396       (unless (with-current-buffer gnus-summary-buffer
5397                 (eq gnus-current-article (gnus-summary-article-number)))
5398         (error "You should select the right article first"))
5399       (if n
5400           (setq n (prefix-numeric-value n))
5401         (let ((pt (point)))
5402           (setq n (or (get-text-property pt 'gnus-part)
5403                       (and (not (bobp))
5404                            (get-text-property (1- pt) 'gnus-part))
5405                       (get-text-property (prog2
5406                                              (forward-line 1)
5407                                              (point)
5408                                            (goto-char pt))
5409                                          'gnus-part)
5410                       (get-text-property
5411                        (or (and (setq pt (previous-single-property-change
5412                                           pt 'gnus-part))
5413                                 (1- pt))
5414                            (next-single-property-change (point) 'gnus-part)
5415                            (point))
5416                        'gnus-part)
5417                       1))))
5418       ;; Check whether the specified part exists.
5419       (when (> n (length gnus-article-mime-handle-alist))
5420         (error "No such part")))
5421     (unless
5422         (progn
5423           ;; To select the window is needed so that the cursor
5424           ;; might be visible on the MIME button.
5425           (select-window (prog1
5426                              window
5427                            (setq window (selected-window))
5428                            ;; Article may be displayed in the other frame.
5429                            (gnus-select-frame-set-input-focus
5430                             (prog1
5431                                 frame
5432                               (setq frame (selected-frame))))))
5433           (when (gnus-article-goto-part n)
5434             ;; We point the cursor and the arrow at the MIME button
5435             ;; when the `function' prompt the user for something.
5436             (unless (and (pos-visible-in-window-p)
5437                          (> (count-lines (point) (window-end))
5438                             (/ (1- (window-height)) 3)))
5439               (recenter (/ (1- (window-height)) 3)))
5440             (let ((cursor-in-non-selected-windows t)
5441                   (overlay-arrow-string "=>")
5442                   (overlay-arrow-position (point-marker)))
5443               (unwind-protect
5444                   (cond
5445                    ((and no-handle interactive)
5446                     (call-interactively function))
5447                    (no-handle
5448                     (funcall function))
5449                    (interactive
5450                     (call-interactively
5451                      function (get-text-property (point) 'gnus-data)))
5452                    (t
5453                     (funcall function
5454                              (get-text-property (point) 'gnus-data))))
5455                 (set-marker overlay-arrow-position nil)
5456                 (unless gnus-auto-select-part
5457                   (gnus-select-frame-set-input-focus frame)
5458                   (select-window window))))
5459             t))
5460       (if gnus-inhibit-mime-unbuttonizing
5461           ;; This is the default though the program shouldn't reach here.
5462           (error "No such part")
5463         ;; The part which doesn't have the MIME button is selected.
5464         ;; So, we display all the buttons and redo it.
5465         (let ((gnus-inhibit-mime-unbuttonizing t))
5466           (gnus-summary-show-article)
5467           (gnus-article-part-wrapper n function no-handle))))))
5468
5469 (defun gnus-article-pipe-part (n)
5470   "Pipe MIME part N, which is the numerical prefix."
5471   (interactive "P")
5472   (gnus-article-part-wrapper n 'mm-pipe-part))
5473
5474 (defun gnus-article-save-part (n)
5475   "Save MIME part N, which is the numerical prefix."
5476   (interactive "P")
5477   (gnus-article-part-wrapper n 'mm-save-part))
5478
5479 (defun gnus-article-interactively-view-part (n)
5480   "View MIME part N interactively, which is the numerical prefix."
5481   (interactive "P")
5482   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5483
5484 (defun gnus-article-copy-part (n)
5485   "Copy MIME part N, which is the numerical prefix."
5486   (interactive "P")
5487   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5488
5489 (defun gnus-article-view-part-as-charset (n)
5490   "View MIME part N using a specified charset.
5491 N is the numerical prefix."
5492   (interactive "P")
5493   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5494
5495 (defun gnus-article-view-part-externally (n)
5496   "View MIME part N externally, which is the numerical prefix."
5497   (interactive "P")
5498   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5499
5500 (defun gnus-article-inline-part (n)
5501   "Inline MIME part N, which is the numerical prefix."
5502   (interactive "P")
5503   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5504
5505 (defun gnus-article-save-part-and-strip (n)
5506   "Save MIME part N and replace it with an external body.
5507 N is the numerical prefix."
5508   (interactive "P")
5509   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5510
5511 (defun gnus-article-replace-part (n)
5512   "Replace MIME part N with an external body.
5513 N is the numerical prefix."
5514   (interactive "P")
5515   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5516
5517 (defun gnus-article-delete-part (n)
5518   "Delete MIME part N and add some information about the removed part.
5519 N is the numerical prefix."
5520   (interactive "P")
5521   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5522
5523 (defun gnus-article-view-part-as-type (n)
5524   "Choose a MIME media type, and view part N as such.
5525 N is the numerical prefix."
5526   (interactive "P")
5527   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5528
5529 (defun gnus-article-mime-match-handle-first (condition)
5530   (if condition
5531       (let (n)
5532         (dolist (ihandle gnus-article-mime-handle-alist)
5533           (if (and (cond
5534                     ((functionp condition)
5535                      (funcall condition (cdr ihandle)))
5536                     ((eq condition 'undisplayed)
5537                      (not (or (mm-handle-undisplayer (cdr ihandle))
5538                               (equal (mm-handle-media-type (cdr ihandle))
5539                                      "multipart/alternative"))))
5540                     ((eq condition 'undisplayed-alternative)
5541                      (not (mm-handle-undisplayer (cdr ihandle))))
5542                     (t t))
5543                    (gnus-article-goto-part (car ihandle))
5544                    (or (not n) (< (car ihandle) n)))
5545               (setq n (car ihandle))))
5546         (or n 1))
5547     1))
5548
5549 (defun gnus-article-view-part (&optional n)
5550   "View MIME part N, which is the numerical prefix.
5551 If the part is already shown, hide the part.  If N is nil, view
5552 all parts."
5553   (interactive "P")
5554   (with-current-buffer gnus-article-buffer
5555     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5556                              gnus-article-mime-match-handle-function)))
5557     (when (> n (length gnus-article-mime-handle-alist))
5558       (error "No such part"))
5559     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5560       (when (gnus-article-goto-part n)
5561         (if (equal (car handle) "multipart/alternative")
5562             (gnus-article-press-button)
5563           (when (eq (gnus-mm-display-part handle) 'internal)
5564             (gnus-set-window-start)))))))
5565
5566 (defsubst gnus-article-mime-total-parts ()
5567   (if (bufferp (car gnus-article-mime-handles))
5568       1 ;; single part
5569     (1- (length gnus-article-mime-handles))))
5570
5571 (defun gnus-mm-display-part (handle)
5572   "Display HANDLE and fix MIME button."
5573   (let ((id (get-text-property (point) 'gnus-part))
5574         (point (point))
5575         (inhibit-read-only t))
5576     (forward-line 1)
5577     (prog1
5578         (let ((window (selected-window))
5579               (mail-parse-charset gnus-newsgroup-charset)
5580               (mail-parse-ignored-charsets
5581                (if (gnus-buffer-live-p gnus-summary-buffer)
5582                    (with-current-buffer gnus-summary-buffer
5583                      gnus-newsgroup-ignored-charsets)
5584                  nil)))
5585           (save-excursion
5586             (unwind-protect
5587                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5588                       (beg (point)))
5589                   (when win
5590                     (select-window win))
5591                   (goto-char point)
5592                   (forward-line)
5593                   (if (mm-handle-displayed-p handle)
5594                       ;; This will remove the part.
5595                       (mm-display-part handle)
5596                     (save-restriction
5597                       (narrow-to-region (point)
5598                                         (if (eobp) (point) (1+ (point))))
5599                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5600                       ;; We narrow to the part itself and
5601                       ;; then call the treatment functions.
5602                       (goto-char (point-min))
5603                       (forward-line 1)
5604                       (narrow-to-region (point) (point-max))
5605                       (gnus-treat-article
5606                        nil id
5607                        (gnus-article-mime-total-parts)
5608                        (mm-handle-media-type handle)))))
5609               (if (window-live-p window)
5610                   (select-window window)))))
5611       (goto-char point)
5612       (gnus-delete-line)
5613       (gnus-insert-mime-button
5614        handle id (list (mm-handle-displayed-p handle)))
5615       (goto-char point))))
5616
5617 (defun gnus-article-goto-part (n)
5618   "Go to MIME part N."
5619   (when gnus-break-pages
5620     (widen))
5621   (prog1
5622       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5623             part handle end next handles)
5624         (when start
5625           (goto-char start)
5626           (if (setq handle (get-text-property start 'gnus-data))
5627               start
5628             ;; Go to the displayed subpart, assuming this is
5629             ;; multipart/alternative.
5630             (setq part start
5631                   end (point-at-eol))
5632             (while (and (not handle)
5633                         part
5634                         (< part end)
5635                         (setq next (text-property-not-all part end
5636                                                           'gnus-data nil)))
5637               (setq part next
5638                     handle (get-text-property part 'gnus-data))
5639               (push (cons handle part) handles)
5640               (unless (mm-handle-displayed-p handle)
5641                 (setq handle nil
5642                       part (text-property-any part end 'gnus-data nil))))
5643             (unless handle
5644               ;; No subpart is displayed, so we find preferred one.
5645               (setq part
5646                     (cdr (assq (mm-preferred-alternative
5647                                 (nreverse (mapcar 'car handles)))
5648                                handles))))
5649             (if part
5650                 (goto-char (1+ part))
5651               start))))
5652     (when gnus-break-pages
5653       (gnus-narrow-to-page))))
5654
5655 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5656   (let ((gnus-tmp-name
5657          (or (mm-handle-filename handle)
5658              (mail-content-type-get (mm-handle-type handle) 'url)
5659              ""))
5660         (gnus-tmp-type (mm-handle-media-type handle))
5661         (gnus-tmp-description (or (mm-handle-description handle) ""))
5662         (gnus-tmp-dots
5663          (if (if displayed (car displayed)
5664                (mm-handle-displayed-p handle))
5665              "" "..."))
5666         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5667                            (buffer-size)))
5668         gnus-tmp-type-long b e)
5669     (when (string-match ".*/" gnus-tmp-name)
5670       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5671     (setq gnus-tmp-type-long (concat gnus-tmp-type
5672                                      (and (not (equal gnus-tmp-name ""))
5673                                           (concat "; " gnus-tmp-name))))
5674     (unless (equal gnus-tmp-description "")
5675       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5676     (unless (bolp)
5677       (insert "\n"))
5678     (setq b (point))
5679     (gnus-eval-format
5680      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5681      `(keymap ,gnus-mime-button-map
5682               gnus-callback gnus-mm-display-part
5683               gnus-part ,gnus-tmp-id
5684               article-type annotation
5685               gnus-data ,handle))
5686     (setq e (if (bolp)
5687                 ;; Exclude a newline.
5688                 (1- (point))
5689               (point)))
5690     (when gnus-article-button-face
5691       (gnus-overlay-put (gnus-make-overlay b e nil t)
5692                         'face gnus-article-button-face))
5693     (widget-convert-button
5694      'link b e
5695      :mime-handle handle
5696      :action 'gnus-widget-press-button
5697      :button-keymap gnus-mime-button-map
5698      :help-echo
5699      (lambda (widget)
5700        ;; Needed to properly clear the message due to a bug in
5701        ;; wid-edit (XEmacs only).
5702        (if (boundp 'help-echo-owns-message)
5703            (setq help-echo-owns-message t))
5704        (format
5705         "%S: %s the MIME part; %S: more options"
5706         (aref gnus-mouse-2 0)
5707         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5708             "hide" "show")
5709         (aref gnus-down-mouse-3 0))))))
5710
5711 (defun gnus-widget-press-button (elems el)
5712   (goto-char (widget-get elems :from))
5713   (gnus-article-press-button))
5714
5715 (defvar gnus-displaying-mime nil)
5716
5717 (defun gnus-display-mime (&optional ihandles)
5718   "Display the MIME parts."
5719   (save-excursion
5720     (save-selected-window
5721       (let ((window (get-buffer-window gnus-article-buffer))
5722             (point (point)))
5723         (when window
5724           (select-window window)
5725           ;; We have to do this since selecting the window
5726           ;; may change the point.  So we set the window point.
5727           (set-window-point window point)))
5728       (let ((handles ihandles)
5729             (inhibit-read-only t)
5730             handle)
5731         (cond (handles)
5732               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5733                (when gnus-article-emulate-mime
5734                  (mm-uu-dissect-text-parts handles)))
5735               (gnus-article-emulate-mime
5736                (setq handles (mm-uu-dissect))))
5737         (when (and (not ihandles)
5738                    (not gnus-displaying-mime))
5739           ;; Top-level call; we clean up.
5740           (when gnus-article-mime-handles
5741             (mm-destroy-parts gnus-article-mime-handles)
5742             (setq gnus-article-mime-handle-alist nil));; A trick.
5743           (setq gnus-article-mime-handles handles)
5744           ;; We allow users to glean info from the handles.
5745           (when gnus-article-mime-part-function
5746             (gnus-mime-part-function handles)))
5747         (if (and handles
5748                  (or (not (stringp (car handles)))
5749                      (cdr handles)))
5750             (progn
5751               (when (and (not ihandles)
5752                          (not gnus-displaying-mime))
5753                 ;; Clean up for mime parts.
5754                 (article-goto-body)
5755                 (delete-region (point) (point-max)))
5756               (let ((gnus-displaying-mime t))
5757                 (gnus-mime-display-part handles)))
5758           (save-restriction
5759             (article-goto-body)
5760             (narrow-to-region (point) (point-max))
5761             (gnus-treat-article nil 1 1 "text/plain")
5762             (widen)))
5763         (unless ihandles
5764           ;; Highlight the headers.
5765           (save-excursion
5766             (save-restriction
5767               (article-goto-body)
5768               (narrow-to-region (point-min) (point))
5769               (gnus-article-save-original-date
5770                (gnus-treat-article 'head)))))))
5771     ;; Cope with broken MIME messages.
5772     (goto-char (point-max))
5773     (unless (bolp)
5774       (insert "\n"))))
5775
5776 (defcustom gnus-mime-display-multipart-as-mixed nil
5777   "Display \"multipart\" parts as  \"multipart/mixed\".
5778
5779 If t, it overrides nil values of
5780 `gnus-mime-display-multipart-alternative-as-mixed' and
5781 `gnus-mime-display-multipart-related-as-mixed'."
5782   :group 'gnus-article-mime
5783   :type 'boolean)
5784
5785 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5786   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5787   :version "22.1"
5788   :group 'gnus-article-mime
5789   :type 'boolean)
5790
5791 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5792   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5793
5794 If displaying \"text/html\" is discouraged \(see
5795 `mm-discouraged-alternatives'\) images or other material inside a
5796 \"multipart/related\" part might be overlooked when this variable is nil."
5797   :version "22.1"
5798   :group 'gnus-article-mime
5799   :type 'boolean)
5800
5801 (defun gnus-mime-display-part (handle)
5802   (cond
5803    ;; Maybe a broken MIME message.
5804    ((null handle))
5805    ;; Single part.
5806    ((not (stringp (car handle)))
5807     (gnus-mime-display-single handle))
5808    ;; User-defined multipart
5809    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5810     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5811              handle))
5812    ;; multipart/alternative
5813    ((and (equal (car handle) "multipart/alternative")
5814          (not (or gnus-mime-display-multipart-as-mixed
5815                   gnus-mime-display-multipart-alternative-as-mixed)))
5816     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5817       (push (cons id handle) gnus-article-mime-handle-alist)
5818       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5819    ;; multipart/related
5820    ((and (equal (car handle) "multipart/related")
5821          (not (or gnus-mime-display-multipart-as-mixed
5822                   gnus-mime-display-multipart-related-as-mixed)))
5823     ;;;!!!We should find the start part, but we just default
5824     ;;;!!!to the first part.
5825     ;;(gnus-mime-display-part (cadr handle))
5826     ;;;!!! Most multipart/related is an HTML message plus images.
5827     ;;;!!! Unfortunately we are unable to let W3 display those
5828     ;;;!!! included images, so we just display it as a mixed multipart.
5829     ;;(gnus-mime-display-mixed (cdr handle))
5830     ;;;!!! No, w3 can display everything just fine.
5831     (gnus-mime-display-part (cadr handle)))
5832    ((equal (car handle) "multipart/signed")
5833     (gnus-add-wash-type 'signed)
5834     (gnus-mime-display-security handle))
5835    ((equal (car handle) "multipart/encrypted")
5836     (gnus-add-wash-type 'encrypted)
5837     (gnus-mime-display-security handle))
5838    ;; Other multiparts are handled like multipart/mixed.
5839    (t
5840     (gnus-mime-display-mixed (cdr handle)))))
5841
5842 (defun gnus-mime-part-function (handles)
5843   (if (stringp (car handles))
5844       (mapcar 'gnus-mime-part-function (cdr handles))
5845     (funcall gnus-article-mime-part-function handles)))
5846
5847 (defun gnus-mime-display-mixed (handles)
5848   (mapcar 'gnus-mime-display-part handles))
5849
5850 (defun gnus-mime-display-single (handle)
5851   (let ((type (mm-handle-media-type handle))
5852         (ignored gnus-ignored-mime-types)
5853         (not-attachment t)
5854         (move nil)
5855         display text)
5856     (catch 'ignored
5857       (progn
5858         (while ignored
5859           (when (string-match (pop ignored) type)
5860             (throw 'ignored nil)))
5861         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5862                                (with-current-buffer gnus-summary-buffer
5863                                  gnus-inhibit-images)
5864                              gnus-inhibit-images)
5865                            (string-match "\\`image/" type)))
5866                  (setq not-attachment
5867                        (and (not (mm-inline-override-p handle))
5868                             (or (not (mm-handle-disposition handle))
5869                                 (equal (car (mm-handle-disposition handle))
5870                                        "inline")
5871                                 (mm-attachment-override-p handle))))
5872                  (mm-automatic-display-p handle)
5873                  (or (and
5874                       (mm-inlinable-p handle)
5875                       (mm-inlined-p handle))
5876                      (mm-automatic-external-display-p type)))
5877             (setq display t)
5878           (when (equal (mm-handle-media-supertype handle) "text")
5879             (setq text t)))
5880         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5881               beg)
5882           (push (cons id handle) gnus-article-mime-handle-alist)
5883           (when (and display
5884                      (equal (mm-handle-media-supertype handle) "message"))
5885             (insert-char
5886              ?\n
5887              (cond ((not (bolp)) 2)
5888                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5889                    (t 1))))
5890           (when (or (not display)
5891                     (not (gnus-unbuttonized-mime-type-p type))
5892                     (eq id gnus-mime-buttonized-part-id))
5893             (gnus-insert-mime-button
5894              handle id (list (or display (and not-attachment text))))
5895             (gnus-article-insert-newline)
5896             ;; Remember modify the number of forward lines.
5897             (setq move t))
5898           (setq beg (point))
5899           (cond
5900            (display
5901             (when move
5902               (forward-line -1)
5903               (setq beg (point)))
5904             (let ((mail-parse-charset gnus-newsgroup-charset)
5905                   (mail-parse-ignored-charsets
5906                    (save-excursion (condition-case ()
5907                                        (set-buffer gnus-summary-buffer)
5908                                      (error))
5909                                    gnus-newsgroup-ignored-charsets)))
5910               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5911             (goto-char (point-max)))
5912            ((and text not-attachment)
5913             (when move
5914               (forward-line -1)
5915               (setq beg (point)))
5916             (gnus-article-insert-newline)
5917             (mm-display-inline handle)
5918             (goto-char (point-max))))
5919           ;; Do highlighting.
5920           (save-excursion
5921             (save-restriction
5922               (narrow-to-region beg (point))
5923               (if (eq handle gnus-article-mime-handles)
5924                   ;; The format=flowed case.
5925                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5926                 ;; Don't count signature parts that are never displayed.
5927                 ;; The part number should be re-calculated supposing this
5928                 ;; might be a message/rfc822 part.
5929                 (let (handles)
5930                   (dolist (part gnus-article-mime-handles)
5931                     (unless (or (stringp part)
5932                                 (equal (car (mm-handle-type part))
5933                                        "application/pgp-signature"))
5934                       (push part handles)))
5935                   (gnus-treat-article
5936                    nil (length (memq handle handles)) (length handles)
5937                    (mm-handle-media-type handle)))))))))))
5938
5939 (defun gnus-unbuttonized-mime-type-p (type)
5940   "Say whether TYPE is to be unbuttonized."
5941   (unless gnus-inhibit-mime-unbuttonizing
5942     (when (catch 'found
5943             (let ((types gnus-unbuttonized-mime-types))
5944               (while types
5945                 (when (string-match (pop types) type)
5946                   (throw 'found t)))))
5947       (not (catch 'found
5948              (let ((types gnus-buttonized-mime-types))
5949                (while types
5950                  (when (string-match (pop types) type)
5951                    (throw 'found t)))))))))
5952
5953 (defun gnus-article-insert-newline ()
5954   "Insert a newline, but mark it as undeletable."
5955   (gnus-put-text-property
5956    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5957
5958 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5959   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5960          (ihandles handles)
5961          (point (point))
5962          handle (inhibit-read-only t) from props begend not-pref)
5963     (save-window-excursion
5964       (save-restriction
5965         (when ibegend
5966           (narrow-to-region (car ibegend)
5967                             (or (cdr ibegend)
5968                                 (progn
5969                                   (goto-char (car ibegend))
5970                                   (forward-line 2)
5971                                   (point))))
5972           (delete-region (point-min) (point-max))
5973           (mm-remove-parts handles))
5974         (setq begend (list (point-marker)))
5975         ;; Do the toggle.
5976         (unless (setq not-pref (cadr (member preferred ihandles)))
5977           (setq not-pref (car ihandles)))
5978         (when (or ibegend
5979                   (not preferred)
5980                   (not (gnus-unbuttonized-mime-type-p
5981                         "multipart/alternative")))
5982           (gnus-add-text-properties
5983            (setq from (point))
5984            (progn
5985              (insert (format "%d.  " id))
5986              (point))
5987            `(gnus-callback
5988              (lambda (handles)
5989                (unless ,(not ibegend)
5990                  (setq gnus-article-mime-handle-alist
5991                        ',gnus-article-mime-handle-alist))
5992                (gnus-mime-display-alternative
5993                 ',ihandles ',not-pref ',begend ,id))
5994              keymap ,gnus-mime-button-map
5995              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5996              face ,gnus-article-button-face
5997              gnus-part ,id
5998              article-type multipart))
5999           (widget-convert-button 'link from (point)
6000                                  :action 'gnus-widget-press-button
6001                                  :button-keymap gnus-widget-button-keymap)
6002           ;; Do the handles
6003           (while (setq handle (pop handles))
6004             (gnus-add-text-properties
6005              (setq from (point))
6006              (progn
6007                (insert (format "(%c) %-18s"
6008                                (if (equal handle preferred) ?* ? )
6009                                (mm-handle-media-type handle)))
6010                (point))
6011              `(gnus-callback
6012                (lambda (handles)
6013                  (unless ,(not ibegend)
6014                    (setq gnus-article-mime-handle-alist
6015                          ',gnus-article-mime-handle-alist))
6016                  (gnus-mime-display-alternative
6017                   ',ihandles ',handle ',begend ,id))
6018                keymap ,gnus-mime-button-map
6019                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6020                face ,gnus-article-button-face
6021                gnus-part ,id
6022                gnus-data ,handle))
6023             (widget-convert-button 'link from (point)
6024                                    :action 'gnus-widget-press-button
6025                                    :button-keymap gnus-widget-button-keymap)
6026             (insert "  "))
6027           (insert "\n\n"))
6028         (when preferred
6029           (if (stringp (car preferred))
6030               (gnus-display-mime preferred)
6031             (let ((mail-parse-charset gnus-newsgroup-charset)
6032                   (mail-parse-ignored-charsets
6033                    (with-current-buffer gnus-summary-buffer
6034                      gnus-newsgroup-ignored-charsets)))
6035               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6036               ;; Do highlighting.
6037               (save-excursion
6038                 (save-restriction
6039                   (narrow-to-region (car begend) (point-max))
6040                   (gnus-treat-article
6041                    nil (length gnus-article-mime-handle-alist)
6042                    (gnus-article-mime-total-parts)
6043                    (mm-handle-media-type preferred))))))
6044           (goto-char (point-max))
6045           (setcdr begend (point-marker)))))
6046     (when ibegend
6047       (goto-char point))))
6048
6049 (defconst gnus-article-wash-status-strings
6050   (let ((alist '((cite "c" "Possible hidden citation text"
6051                        " " "All citation text visible")
6052                  (headers "h" "Hidden headers"
6053                           " " "All headers visible.")
6054                  (pgp "p" "Encrypted or signed message status hidden"
6055                       " " "No hidden encryption nor digital signature status")
6056                  (signature "s" "Signature has been hidden"
6057                             " " "Signature is visible")
6058                  (overstrike "o" "Overstrike (^H) characters applied"
6059                              " " "No overstrike characters applied")
6060                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6061                            " " "No /*_emphasis_*/ characters applied")))
6062         result)
6063     (dolist (entry alist result)
6064       (let ((key (nth 0 entry))
6065             (on (copy-sequence (nth 1 entry)))
6066             (on-help (nth 2 entry))
6067             (off (copy-sequence (nth 3 entry)))
6068             (off-help (nth 4 entry)))
6069         (put-text-property 0 1 'help-echo on-help on)
6070         (put-text-property 0 1 'help-echo off-help off)
6071         (push (list key on off) result))))
6072   "Alist of strings describing wash status in the mode line.
6073 Each entry has the form (KEY ON OF), where the KEY is a symbol
6074 representing the particular washing function, ON is the string to use
6075 in the article mode line when the washing function is active, and OFF
6076 is the string to use when it is inactive.")
6077
6078 (defun gnus-article-wash-status-entry (key value)
6079   (let ((entry (assoc key gnus-article-wash-status-strings)))
6080     (if value (nth 1 entry) (nth 2 entry))))
6081
6082 (defun gnus-article-wash-status ()
6083   "Return a string which display status of article washing."
6084   (with-current-buffer gnus-article-buffer
6085     (let ((cite (memq 'cite gnus-article-wash-types))
6086           (headers (memq 'headers gnus-article-wash-types))
6087           (boring (memq 'boring-headers gnus-article-wash-types))
6088           (pgp (memq 'pgp gnus-article-wash-types))
6089           (pem (memq 'pem gnus-article-wash-types))
6090           (signed (memq 'signed gnus-article-wash-types))
6091           (encrypted (memq 'encrypted gnus-article-wash-types))
6092           (signature (memq 'signature gnus-article-wash-types))
6093           (overstrike (memq 'overstrike gnus-article-wash-types))
6094           (emphasis (memq 'emphasis gnus-article-wash-types)))
6095       (concat
6096        (gnus-article-wash-status-entry 'cite cite)
6097        (gnus-article-wash-status-entry 'headers (or headers boring))
6098        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6099        (gnus-article-wash-status-entry 'signature signature)
6100        (gnus-article-wash-status-entry 'overstrike overstrike)
6101        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6102
6103 (defun gnus-add-wash-type (type)
6104   "Add a washing of TYPE to the current status."
6105   (add-to-list 'gnus-article-wash-types type))
6106
6107 (defun gnus-delete-wash-type (type)
6108   "Add a washing of TYPE to the current status."
6109   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6110
6111 (defun gnus-add-image (category image)
6112   "Add IMAGE of CATEGORY to the list of displayed images."
6113   (let ((entry (assq category gnus-article-image-alist)))
6114     (unless entry
6115       (setq entry (list category))
6116       (push entry gnus-article-image-alist))
6117     (nconc entry (list image))))
6118
6119 (defun gnus-delete-images (category)
6120   "Delete all images in CATEGORY."
6121   (let ((entry (assq category gnus-article-image-alist)))
6122     (dolist (image (cdr entry))
6123       (gnus-remove-image image category))
6124     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6125     (gnus-delete-wash-type category)))
6126
6127 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6128
6129 (defun gnus-article-maybe-hide-headers ()
6130   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6131 Provided for backwards compatibility."
6132   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6133                  (not (with-current-buffer gnus-summary-buffer
6134                         gnus-have-all-headers)))
6135              (not gnus-inhibit-hiding))
6136     (gnus-article-hide-headers)))
6137
6138 ;;; Article savers.
6139
6140 (defun gnus-output-to-file (file-name)
6141   "Append the current article to a file named FILE-NAME.
6142 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6143 text and used as the value of the coding cookie which is added to the
6144 top of a file.  Otherwise, this function saves a raw article without
6145 the coding cookie."
6146   (let* ((artbuf (current-buffer))
6147          (file-name-coding-system nnmail-pathname-coding-system)
6148          (coding gnus-article-save-coding-system)
6149          (coding-system-for-read (if coding
6150                                      nil ;; Rely on the coding cookie.
6151                                    mm-text-coding-system))
6152          (coding-system-for-write (or coding
6153                                       mm-text-coding-system-for-write
6154                                       mm-text-coding-system))
6155          (exists (file-exists-p file-name)))
6156     (with-temp-buffer
6157       (when exists
6158         (insert-file-contents file-name)
6159         (goto-char (point-min))
6160         ;; Remove the existing coding cookie.
6161         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6162           (delete-region (match-beginning 0) (match-end 0))))
6163       (goto-char (point-max))
6164       (insert-buffer-substring artbuf)
6165       ;; Append newline at end of the buffer as separator, and then
6166       ;; save it to file.
6167       (goto-char (point-max))
6168       (insert "\n")
6169       (when coding
6170         ;; If the coding system is not suitable to encode the text,
6171         ;; ask a user for a proper one.
6172         (when (fboundp 'select-safe-coding-system)
6173           (setq coding (coding-system-base
6174                         (save-window-excursion
6175                           (select-safe-coding-system (point-min) (point-max)
6176                                                      coding))))
6177           (setq coding-system-for-write
6178                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6179                     coding)))
6180         (goto-char (point-min))
6181         ;; Add the coding cookie.
6182         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6183                         coding-system-for-write)))
6184       (if exists
6185           (progn
6186             (write-region (point-min) (point-max) file-name nil 'no-message)
6187             (message "Appended to %s" file-name))
6188         (write-region (point-min) (point-max) file-name))))
6189   t)
6190
6191 (defun gnus-narrow-to-page (&optional arg)
6192   "Narrow the article buffer to a page.
6193 If given a numerical ARG, move forward ARG pages."
6194   (interactive "P")
6195   (setq arg (if arg (prefix-numeric-value arg) 0))
6196   (with-current-buffer gnus-article-buffer
6197     (widen)
6198     ;; Remove any old next/prev buttons.
6199     (when (gnus-visual-p 'page-marker)
6200       (let ((inhibit-read-only t))
6201         (gnus-remove-text-with-property 'gnus-prev)
6202         (gnus-remove-text-with-property 'gnus-next)))
6203     (let (st nd pt)
6204       (when (save-excursion
6205               (cond ((< arg 0)
6206                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6207                          (prog1
6208                              (setq nd (match-beginning 0)
6209                                    pt nd)
6210                            (when (re-search-backward page-delimiter nil t)
6211                              (setq st (match-end 0))))
6212                        (when (re-search-forward page-delimiter nil t)
6213                          (setq nd (match-beginning 0)
6214                                pt (point-min)))))
6215                     ((> arg 0)
6216                      (if (re-search-forward page-delimiter nil 'move arg)
6217                          (prog1
6218                              (setq st (match-end 0)
6219                                    pt st)
6220                            (when (re-search-forward page-delimiter nil t)
6221                              (setq nd (match-beginning 0))))
6222                        (when (re-search-backward page-delimiter nil t)
6223                          (setq st (match-end 0)
6224                                pt (point-max)))))
6225                     (t
6226                      (when (re-search-backward page-delimiter nil t)
6227                        (goto-char (setq st (match-end 0))))
6228                      (when (re-search-forward page-delimiter nil t)
6229                        (setq nd (match-beginning 0)))
6230                      (or st nd))))
6231         (setq gnus-page-broken t)
6232         (when pt (goto-char pt))
6233         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6234         (when (gnus-visual-p 'page-marker)
6235           (save-excursion
6236             (when nd
6237               (goto-char nd)
6238               (gnus-insert-next-page-button))
6239             (when st
6240               (goto-char st)
6241               (gnus-insert-prev-page-button))))))))
6242
6243 ;; Article mode commands
6244
6245 (defun gnus-article-goto-next-page ()
6246   "Show the next page of the article."
6247   (interactive)
6248   (when (gnus-article-next-page)
6249     (goto-char (point-min))
6250     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6251
6252
6253 (defun gnus-article-goto-prev-page ()
6254   "Show the previous page of the article."
6255   (interactive)
6256   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6257       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6258     (gnus-article-prev-page nil)))
6259
6260 ;; This is cleaner but currently breaks `gnus-pick-mode':
6261 ;;
6262 ;; (defun gnus-article-goto-next-page ()
6263 ;;   "Show the next page of the article."
6264 ;;   (interactive)
6265 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6266 ;;     (gnus-summary-next-page)))
6267 ;;
6268 ;; (defun gnus-article-goto-prev-page ()
6269 ;;   "Show the next page of the article."
6270 ;;   (interactive)
6271 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6272 ;;     (gnus-summary-prev-page)))
6273
6274 (defun gnus-article-next-page (&optional lines)
6275   "Show the next page of the current article.
6276 If end of article, return non-nil.  Otherwise return nil.
6277 Argument LINES specifies lines to be scrolled up."
6278   (interactive "p")
6279   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6280   (if (and (not (and gnus-article-over-scroll
6281                      (> (count-lines (window-start) (point-max))
6282                         (if (featurep 'xemacs)
6283                             (or lines (1- (window-height)))
6284                           (+ (or lines (1- (window-height))) scroll-margin)))))
6285            (save-excursion
6286              (end-of-line)
6287              (and (pos-visible-in-window-p)     ;Not continuation line.
6288                   (>= (point) (point-max)))))
6289       ;; Nothing in this page.
6290       (if (or (not gnus-page-broken)
6291               (save-excursion
6292                 (save-restriction
6293                   (widen)
6294                   (forward-line)
6295                   (eobp)))) ;Real end-of-buffer?
6296           (progn
6297             (when gnus-article-over-scroll
6298               (gnus-article-next-page-1 lines))
6299             t)                  ;Nothing more.
6300         (gnus-narrow-to-page 1)         ;Go to next page.
6301         nil)
6302     ;; More in this page.
6303     (gnus-article-next-page-1 lines)
6304     nil))
6305
6306 (defun gnus-article-beginning-of-window ()
6307   "Move point to the beginning of the window.
6308 In Emacs, the point is placed at the line number which `scroll-margin'
6309 specifies."
6310   (if (featurep 'xemacs)
6311       (move-to-window-line 0)
6312     ;; There is an obscure bug in Emacs that makes it impossible to
6313     ;; scroll past big pictures in the article buffer.  Try to fix
6314     ;; this by adding a sanity check by counting the lines visible.
6315     (when (> (count-lines (window-start) (window-end)) 30)
6316       (move-to-window-line
6317        (min (max 0 scroll-margin)
6318             (max 1 (- (window-height)
6319                       (if mode-line-format 1 0)
6320                       (if header-line-format 1 0)
6321                       2)))))))
6322
6323 (defun gnus-article-next-page-1 (lines)
6324   (condition-case ()
6325       (let ((scroll-in-place nil)
6326             (auto-window-vscroll nil))
6327         (scroll-up lines))
6328     (end-of-buffer
6329      ;; Long lines may cause an end-of-buffer error.
6330      (goto-char (point-max))))
6331   (gnus-article-beginning-of-window))
6332
6333 (defun gnus-article-prev-page (&optional lines)
6334   "Show previous page of current article.
6335 Argument LINES specifies lines to be scrolled down."
6336   (interactive "p")
6337   (move-to-window-line 0)
6338   (if (and gnus-page-broken
6339            (bobp)
6340            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6341       (progn
6342         (gnus-narrow-to-page -1)        ;Go to previous page.
6343         (goto-char (point-max))
6344         (recenter (if gnus-article-over-scroll
6345                       (if lines
6346                           (max (if (featurep 'xemacs)
6347                                    lines
6348                                  (+ lines scroll-margin))
6349                                3)
6350                         (- (window-height) 2))
6351                     -1)))
6352     (prog1
6353         (condition-case ()
6354             (let ((scroll-in-place nil))
6355               (scroll-down lines))
6356           (beginning-of-buffer
6357            (goto-char (point-min))))
6358       (gnus-article-beginning-of-window))))
6359
6360 (defun gnus-article-only-boring-p ()
6361   "Decide whether there is only boring text remaining in the article.
6362 Something \"interesting\" is a word of at least two letters that does
6363 not have a face in `gnus-article-boring-faces'."
6364   (when (and gnus-article-skip-boring
6365              (boundp 'gnus-article-boring-faces)
6366              (symbol-value 'gnus-article-boring-faces))
6367     (save-excursion
6368       (let ((inhibit-point-motion-hooks t))
6369         (catch 'only-boring
6370           (while (re-search-forward "\\b\\w\\w" nil t)
6371             (forward-char -1)
6372             (when (not (gnus-intersection
6373                         (gnus-faces-at (point))
6374                         (symbol-value 'gnus-article-boring-faces)))
6375               (throw 'only-boring nil)))
6376           (throw 'only-boring t))))))
6377
6378 (defun gnus-article-refer-article ()
6379   "Read article specified by message-id around point."
6380   (interactive)
6381   (save-excursion
6382     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6383     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6384     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6385         (let ((msg-id (concat "<" (match-string 0) ">")))
6386           (set-buffer gnus-summary-buffer)
6387           (gnus-summary-refer-article msg-id))
6388       (error "No references around point"))))
6389
6390 (defun gnus-article-show-summary ()
6391   "Reconfigure windows to show summary buffer."
6392   (interactive)
6393   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6394       (error "There is no summary buffer for this article buffer")
6395     (gnus-article-set-globals)
6396     (gnus-configure-windows 'article)
6397     (gnus-summary-goto-subject gnus-current-article)
6398     (gnus-summary-position-point)))
6399
6400 (defun gnus-article-describe-briefly ()
6401   "Describe article mode commands briefly."
6402   (interactive)
6403   (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")))
6404
6405 (defun gnus-article-check-buffer ()
6406   "Beep if not in an article buffer."
6407   (unless (equal major-mode 'gnus-article-mode)
6408     (error "Command invoked outside of a Gnus article buffer")))
6409
6410 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6411   "Read a summary buffer key sequence and execute it from the article buffer."
6412   (interactive "P")
6413   (gnus-article-check-buffer)
6414   (let ((nosaves
6415          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6416            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6417            "=" "^" "\M-^" "|"))
6418         (nosave-but-article
6419          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6420            "An" "Ap" [?A (meta return)] [?A delete]))
6421         (nosave-in-article
6422          '("AS" "\C-d"))
6423         (up-to-top
6424          '("n" "Gn" "p" "Gp"))
6425         keys new-sum-point)
6426     (with-current-buffer gnus-article-current-summary
6427       (let (gnus-pick-mode)
6428         (setq unread-command-events (nconc unread-command-events
6429                                            (list (or key last-command-event)))
6430               keys (if (featurep 'xemacs)
6431                        (events-to-keys (read-key-sequence nil t))
6432                      (read-key-sequence nil t)))))
6433
6434     (message "")
6435
6436     (cond
6437      ((eq (aref keys (1- (length keys))) ?\C-h)
6438       (gnus-article-describe-bindings (substring keys 0 -1)))
6439      ((or (member keys nosaves)
6440           (member keys nosave-but-article)
6441           (member keys nosave-in-article))
6442       (let (func)
6443         (save-window-excursion
6444           (pop-to-buffer gnus-article-current-summary)
6445           ;; We disable the pick minor mode commands.
6446           (let (gnus-pick-mode)
6447             (setq func (key-binding keys t))))
6448         (if (or (not func)
6449                 (numberp func))
6450             (ding)
6451           (unless (member keys nosave-in-article)
6452             (set-buffer gnus-article-current-summary))
6453           (when (get func 'disabled)
6454             (error "Function %s disabled" func))
6455           (call-interactively func)
6456           (setq new-sum-point (point)))
6457         (when (member keys nosave-but-article)
6458           (pop-to-buffer gnus-article-buffer))))
6459      (t
6460       ;; These commands should restore window configuration.
6461       (let ((obuf (current-buffer))
6462             (owin (current-window-configuration))
6463             win func in-buffer selected new-sum-start new-sum-hscroll err)
6464         (cond (not-restore-window
6465                (pop-to-buffer gnus-article-current-summary)
6466                (setq win (selected-window)))
6467               ((setq win (get-buffer-window gnus-article-current-summary))
6468                (select-window win))
6469               (t
6470                (let ((summary-buffer gnus-article-current-summary))
6471                  (gnus-configure-windows 'article)
6472                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6473                    (let ((gnus-buffer-configuration
6474                           '((article ((vertical 1.0
6475                                                 (summary 0.25 point)
6476                                                 (article 1.0)))))))
6477                      (gnus-configure-windows 'article))
6478                    (setq win (get-buffer-window summary-buffer 'visible)))
6479                  (gnus-select-frame-set-input-focus (window-frame win))
6480                  (select-window win))))
6481         (setq in-buffer (current-buffer))
6482         ;; We disable the pick minor mode commands.
6483         (setq func (let (gnus-pick-mode)
6484                      (key-binding keys t)))
6485         (when (get func 'disabled)
6486           (error "Function %s disabled" func))
6487         (if (and func
6488                  (functionp func)
6489                  (condition-case code
6490                      (progn
6491                        (call-interactively func)
6492                        t)
6493                    (error
6494                     (setq err code)
6495                     nil)))
6496             (progn
6497               (when (eq win (selected-window))
6498                 (setq new-sum-point (point)
6499                       new-sum-start (window-start win)
6500                       new-sum-hscroll (window-hscroll win)))
6501               (when (or (eq in-buffer (current-buffer))
6502                         (when (eq obuf (current-buffer))
6503                           (set-buffer in-buffer)
6504                           t))
6505                 (setq selected (gnus-summary-select-article))
6506                 (set-buffer obuf)
6507                 (unless not-restore-window
6508                   (set-window-configuration owin))
6509                 (when (and (eq selected 'old)
6510                            new-sum-point)
6511                   (set-window-start (get-buffer-window (current-buffer))
6512                                     1)
6513                   (set-window-point (get-buffer-window (current-buffer))
6514                                     (if (article-goto-body)
6515                                         (1- (point))
6516                                       (point))))
6517                 (when (and (not not-restore-window)
6518                            new-sum-point
6519                            (window-live-p win)
6520                            (with-current-buffer (window-buffer win)
6521                              (eq major-mode 'gnus-summary-mode)))
6522                   (set-window-point win new-sum-point)
6523                   (set-window-start win new-sum-start)
6524                   (set-window-hscroll win new-sum-hscroll))))
6525           (set-window-configuration owin)
6526           (if err
6527               (signal (car err) (cdr err))
6528             (ding))))))))
6529
6530 (defun gnus-article-read-summary-send-keys ()
6531   (interactive)
6532   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6533     (gnus-article-read-summary-keys)))
6534
6535 (defun gnus-article-describe-key (key)
6536   "Display documentation of the function invoked by KEY.
6537 KEY is a string or a vector."
6538   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6539                        (read-key-sequence "Describe key: "))))
6540   (gnus-article-check-buffer)
6541   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6542                                   gnus-article-read-summary-send-keys))
6543       (with-current-buffer gnus-article-current-summary
6544         (setq unread-command-events
6545               (if (featurep 'xemacs)
6546                   (append key nil)
6547                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6548                                         (list 'meta (- x 128))
6549                                       x))
6550                         key)))
6551         (let ((cursor-in-echo-area t)
6552               gnus-pick-mode)
6553           (describe-key (read-key-sequence nil t))))
6554     (describe-key key)))
6555
6556 (defun gnus-article-describe-key-briefly (key &optional insert)
6557   "Display documentation of the function invoked by KEY.
6558 KEY is a string or a vector."
6559   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6560                        (read-key-sequence "Describe key: "))
6561                      current-prefix-arg))
6562   (gnus-article-check-buffer)
6563   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6564                                   gnus-article-read-summary-send-keys))
6565       (with-current-buffer gnus-article-current-summary
6566         (setq unread-command-events
6567               (if (featurep 'xemacs)
6568                   (append key nil)
6569                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6570                                         (list 'meta (- x 128))
6571                                       x))
6572                         key)))
6573         (let ((cursor-in-echo-area t)
6574               gnus-pick-mode)
6575           (describe-key-briefly (read-key-sequence nil t) insert)))
6576     (describe-key-briefly key insert)))
6577
6578 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6579 (defvar gnus-agent-summary-mode)
6580 (defvar gnus-draft-mode)
6581 ;; Calling help-buffer will autoload help-mode.
6582 (defvar help-xref-stack-item)
6583 ;; Emacs 22 doesn't load it in the batch mode.
6584 (eval-when-compile
6585   (autoload 'help-buffer "help-mode"))
6586
6587 (defun gnus-article-describe-bindings (&optional prefix)
6588   "Show a list of all defined keys, and their definitions.
6589 The optional argument PREFIX, if non-nil, should be a key sequence;
6590 then we display only bindings that start with that prefix."
6591   (interactive)
6592   (gnus-article-check-buffer)
6593   (let ((keymap (copy-keymap gnus-article-mode-map))
6594         (map (copy-keymap gnus-article-send-map))
6595         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6596         parent agent draft)
6597     (define-key keymap "S" map)
6598     (define-key map [t] nil)
6599     (with-current-buffer gnus-article-current-summary
6600       (set-keymap-parent
6601        keymap
6602        (if (setq parent (keymap-parent gnus-article-mode-map))
6603            (prog1
6604                (setq parent (copy-keymap parent))
6605              (set-keymap-parent parent (current-local-map)))
6606          (current-local-map)))
6607       (set-keymap-parent map (key-binding "S"))
6608       (let (key def gnus-pick-mode)
6609         (while sumkeys
6610           (setq key (pop sumkeys))
6611           (cond ((and (vectorp key) (= (length key) 1)
6612                       (consp (setq def (aref key 0)))
6613                       (numberp (car def)) (numberp (cdr def)))
6614                  (when (< (max (car def) (cdr def)) 128)
6615                    (setq sumkeys
6616                          (append (mapcar
6617                                   #'vector
6618                                   (nreverse (gnus-uncompress-range def)))
6619                                  sumkeys))))
6620                 ((setq def (key-binding key))
6621                  (unless (eq def 'undefined)
6622                    (define-key keymap key def))))))
6623       (when (boundp 'gnus-agent-summary-mode)
6624         (setq agent gnus-agent-summary-mode))
6625       (when (boundp 'gnus-draft-mode)
6626         (setq draft gnus-draft-mode)))
6627     (with-temp-buffer
6628       (use-local-map keymap)
6629       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6630       (set (make-local-variable 'gnus-draft-mode) draft)
6631       (describe-bindings prefix))
6632     (let ((item `((lambda (prefix)
6633                     (with-current-buffer ,(current-buffer)
6634                       (gnus-article-describe-bindings prefix)))
6635                   ,prefix)))
6636       (with-current-buffer (let (help-xref-following) (help-buffer))
6637         (setq help-xref-stack-item item)))))
6638
6639 (defun gnus-article-reply-with-original (&optional wide)
6640   "Start composing a reply mail to the current message.
6641 The text in the region will be yanked.  If the region isn't active,
6642 the entire article will be yanked."
6643   (interactive)
6644   (let ((article (cdr gnus-article-current))
6645         contents)
6646     (if (not (gnus-region-active-p))
6647         (with-current-buffer gnus-summary-buffer
6648           (gnus-summary-reply (list (list article)) wide))
6649       (setq contents (buffer-substring (point) (mark t)))
6650       ;; Deactivate active regions.
6651       (when (and (boundp 'transient-mark-mode)
6652                  transient-mark-mode)
6653         (setq mark-active nil))
6654       (with-current-buffer gnus-summary-buffer
6655         (gnus-summary-reply
6656          (list (list article contents)) wide)))))
6657
6658 (defun gnus-article-wide-reply-with-original ()
6659   "Start composing a wide reply mail to the current message.
6660 The text in the region will be yanked.  If the region isn't active,
6661 the entire article will be yanked."
6662   (interactive)
6663   (gnus-article-reply-with-original t))
6664
6665 (defun gnus-article-followup-with-original ()
6666   "Compose a followup to the current article.
6667 The text in the region will be yanked.  If the region isn't active,
6668 the entire article will be yanked."
6669   (interactive)
6670   (let ((article (cdr gnus-article-current))
6671         contents)
6672       (if (not (gnus-region-active-p))
6673           (with-current-buffer gnus-summary-buffer
6674             (gnus-summary-followup (list (list article))))
6675         (setq contents (buffer-substring (point) (mark t)))
6676         ;; Deactivate active regions.
6677         (when (and (boundp 'transient-mark-mode)
6678                    transient-mark-mode)
6679           (setq mark-active nil))
6680         (with-current-buffer gnus-summary-buffer
6681           (gnus-summary-followup
6682            (list (list article contents)))))))
6683
6684 (defun gnus-article-hide (&optional arg force)
6685   "Hide all the gruft in the current article.
6686 This means that signatures, cited text and (some) headers will be
6687 hidden.
6688 If given a prefix, show the hidden text instead."
6689   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6690   (gnus-article-hide-headers arg)
6691   (gnus-article-hide-list-identifiers arg)
6692   (gnus-article-hide-citation-maybe arg force)
6693   (gnus-article-hide-signature arg))
6694
6695 (defun gnus-article-maybe-highlight ()
6696   "Do some article highlighting if article highlighting is requested."
6697   (when (gnus-visual-p 'article-highlight 'highlight)
6698     (gnus-article-highlight-some)))
6699
6700 (defun gnus-check-group-server ()
6701   ;; Make sure the connection to the server is alive.
6702   (unless (gnus-server-opened
6703            (gnus-find-method-for-group gnus-newsgroup-name))
6704     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6705     (gnus-request-group gnus-newsgroup-name t)))
6706
6707 (eval-when-compile
6708   (autoload 'nneething-get-file-name "nneething"))
6709
6710 (defun gnus-request-article-this-buffer (article group)
6711   "Get an article and insert it into this buffer."
6712   (let (do-update-line sparse-header)
6713     (prog1
6714         (save-excursion
6715           (erase-buffer)
6716           (gnus-kill-all-overlays)
6717           (setq group (or group gnus-newsgroup-name))
6718
6719           ;; Using `gnus-request-article' directly will insert the article into
6720           ;; `nntp-server-buffer' - so we'll save some time by not having to
6721           ;; copy it from the server buffer into the article buffer.
6722
6723           ;; We only request an article by message-id when we do not have the
6724           ;; headers for it, so we'll have to get those.
6725           (when (stringp article)
6726             (gnus-read-header article))
6727
6728           ;; If the article number is negative, that means that this article
6729           ;; doesn't belong in this newsgroup (possibly), so we find its
6730           ;; message-id and request it by id instead of number.
6731           (when (and (numberp article)
6732                      gnus-summary-buffer
6733                      (get-buffer gnus-summary-buffer)
6734                      (gnus-buffer-exists-p gnus-summary-buffer))
6735             (with-current-buffer gnus-summary-buffer
6736               (let ((header (gnus-summary-article-header article)))
6737                 (when (< article 0)
6738                   (cond
6739                    ((memq article gnus-newsgroup-sparse)
6740                     ;; This is a sparse gap article.
6741                     (setq do-update-line article)
6742                     (setq article (mail-header-id header))
6743                     (setq sparse-header (gnus-read-header article))
6744                     (setq gnus-newsgroup-sparse
6745                           (delq article gnus-newsgroup-sparse)))
6746                    ((vectorp header)
6747                     ;; It's a real article.
6748                     (setq article (mail-header-id header)))
6749                    (t
6750                     ;; It is an extracted pseudo-article.
6751                     (setq article 'pseudo)
6752                     (gnus-request-pseudo-article header))))
6753
6754                 (let ((method (gnus-find-method-for-group
6755                                gnus-newsgroup-name)))
6756                   (when (and (eq (car method) 'nneething)
6757                              (vectorp header))
6758                     (let ((dir (nneething-get-file-name
6759                                 (mail-header-id header))))
6760                       (when (and (stringp dir)
6761                                  (file-directory-p dir))
6762                         (setq article 'nneething)
6763                         (gnus-group-enter-directory dir))))))))
6764
6765           (cond
6766            ;; Refuse to select canceled articles.
6767            ((and (numberp article)
6768                  gnus-summary-buffer
6769                  (get-buffer gnus-summary-buffer)
6770                  (gnus-buffer-exists-p gnus-summary-buffer)
6771                  (eq (cdr (with-current-buffer gnus-summary-buffer
6772                             (assq article gnus-newsgroup-reads)))
6773                      gnus-canceled-mark))
6774             nil)
6775            ;; We first check `gnus-original-article-buffer'.
6776            ((and (get-buffer gnus-original-article-buffer)
6777                  (numberp article)
6778                  (with-current-buffer gnus-original-article-buffer
6779                    (and (equal (car gnus-original-article) group)
6780                         (eq (cdr gnus-original-article) article))))
6781             ;; `insert-buffer-substring' would incorrectly use the
6782             ;; equivalent of string-make-multibyte which amount to decoding
6783             ;; with locale-coding-system, causing failure of
6784             ;; subsequent decoding.
6785             (insert (mm-string-to-multibyte
6786                      (with-current-buffer gnus-original-article-buffer
6787                        (buffer-substring (point-min) (point-max)))))
6788             'article)
6789            ;; Check the backlog.
6790            ((and gnus-keep-backlog
6791                  (gnus-backlog-request-article group article (current-buffer)))
6792             'article)
6793            ;; Check asynchronous pre-fetch.
6794            ((gnus-async-request-fetched-article group article (current-buffer))
6795             (gnus-async-prefetch-next group article gnus-summary-buffer)
6796             (when (and (numberp article) gnus-keep-backlog)
6797               (gnus-backlog-enter-article group article (current-buffer)))
6798             'article)
6799            ;; Check the cache.
6800            ((and gnus-use-cache
6801                  (numberp article)
6802                  (gnus-cache-request-article article group))
6803             'article)
6804            ;; Check the agent cache.
6805            ((gnus-agent-request-article article group)
6806             'article)
6807            ;; Get the article and put into the article buffer.
6808            ((or (stringp article)
6809                 (numberp article))
6810             (let ((gnus-override-method gnus-override-method)
6811                   (methods (and (stringp article)
6812                                 gnus-refer-article-method))
6813                   (backend (car (gnus-find-method-for-group
6814                                  gnus-newsgroup-name)))
6815                   result
6816                   (inhibit-read-only t))
6817               (if (or (not (listp methods))
6818                       (and (symbolp (car methods))
6819                            (assq (car methods) nnoo-definition-alist)))
6820                   (setq methods (list methods)))
6821               (when (and (null gnus-override-method)
6822                          methods)
6823                 (setq gnus-override-method (pop methods)))
6824               (while (not result)
6825                 (when (eq gnus-override-method 'current)
6826                   (setq gnus-override-method
6827                         (with-current-buffer gnus-summary-buffer
6828                           gnus-current-select-method)))
6829                 (erase-buffer)
6830                 (gnus-kill-all-overlays)
6831                 (let ((gnus-newsgroup-name group))
6832                   (gnus-check-group-server))
6833                 (cond
6834                  ((gnus-request-article article group (current-buffer))
6835                   (when (numberp article)
6836                     (gnus-async-prefetch-next group article
6837                                               gnus-summary-buffer)
6838                     (when gnus-keep-backlog
6839                       (gnus-backlog-enter-article
6840                        group article (current-buffer))))
6841                   (setq result 'article))
6842                  (methods
6843                   (setq gnus-override-method (pop methods)))
6844                  ((not (string-match "^400 "
6845                                      (nnheader-get-report backend)))
6846                   ;; If we get 400 server disconnect, reconnect and
6847                   ;; retry; otherwise, assume the article has expired.
6848                   (setq result 'done))))
6849               (and (eq result 'article) 'article)))
6850            ;; It was a pseudo.
6851            (t article)))
6852
6853       ;; Associate this article with the current summary buffer.
6854       (setq gnus-article-current-summary gnus-summary-buffer)
6855
6856       ;; Take the article from the original article buffer
6857       ;; and place it in the buffer it's supposed to be in.
6858       (when (and (get-buffer gnus-article-buffer)
6859                  (equal (buffer-name (current-buffer))
6860                         (buffer-name (get-buffer gnus-article-buffer))))
6861         (save-excursion
6862           (if (get-buffer gnus-original-article-buffer)
6863               (set-buffer gnus-original-article-buffer)
6864             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6865             (buffer-disable-undo)
6866             (setq major-mode 'gnus-original-article-mode)
6867             (setq buffer-read-only t))
6868           (let ((inhibit-read-only t))
6869             (erase-buffer)
6870             (insert-buffer-substring gnus-article-buffer))
6871           (setq gnus-original-article (cons group article)))
6872
6873         ;; Decode charsets.
6874         (run-hooks 'gnus-article-decode-hook)
6875         ;; Mark article as decoded or not.
6876         (setq gnus-article-decoded-p gnus-article-decode-hook))
6877
6878       ;; Update sparse articles.
6879       (when (and do-update-line
6880                  (or (numberp article)
6881                      (stringp article)))
6882         (let ((buf (current-buffer)))
6883           (set-buffer gnus-summary-buffer)
6884           (gnus-summary-update-article do-update-line sparse-header)
6885           (gnus-summary-goto-subject do-update-line nil t)
6886           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6887                             (point))
6888           (set-buffer buf))))))
6889
6890 (defun gnus-block-private-groups (group)
6891   (if (gnus-news-group-p group)
6892       ;; Block nothing in news groups.
6893       nil
6894     ;; Block everything anywhere else.
6895     "."))
6896
6897 (defun gnus-blocked-images ()
6898   (if (functionp gnus-blocked-images)
6899       (funcall gnus-blocked-images gnus-newsgroup-name)
6900     gnus-blocked-images))
6901
6902 ;;;
6903 ;;; Article editing
6904 ;;;
6905
6906 (defcustom gnus-article-edit-mode-hook nil
6907   "Hook run in article edit mode buffers."
6908   :group 'gnus-article-various
6909   :type 'hook)
6910
6911 (defvar gnus-article-edit-done-function nil)
6912
6913 (defvar gnus-article-edit-mode-map nil)
6914
6915 ;; Should we be using derived.el for this?
6916 (unless gnus-article-edit-mode-map
6917   (setq gnus-article-edit-mode-map (make-keymap))
6918   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6919
6920   (gnus-define-keys gnus-article-edit-mode-map
6921     "\C-c?"    describe-mode
6922     "\C-c\C-c" gnus-article-edit-done
6923     "\C-c\C-k" gnus-article-edit-exit
6924     "\C-c\C-f\C-t" message-goto-to
6925     "\C-c\C-f\C-o" message-goto-from
6926     "\C-c\C-f\C-b" message-goto-bcc
6927     ;;"\C-c\C-f\C-w" message-goto-fcc
6928     "\C-c\C-f\C-c" message-goto-cc
6929     "\C-c\C-f\C-s" message-goto-subject
6930     "\C-c\C-f\C-r" message-goto-reply-to
6931     "\C-c\C-f\C-n" message-goto-newsgroups
6932     "\C-c\C-f\C-d" message-goto-distribution
6933     "\C-c\C-f\C-f" message-goto-followup-to
6934     "\C-c\C-f\C-m" message-goto-mail-followup-to
6935     "\C-c\C-f\C-k" message-goto-keywords
6936     "\C-c\C-f\C-u" message-goto-summary
6937     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6938     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6939     "\C-c\C-b" message-goto-body
6940     "\C-c\C-i" message-goto-signature
6941
6942     "\C-c\C-t" message-insert-to
6943     "\C-c\C-n" message-insert-newsgroups
6944     "\C-c\C-o" message-sort-headers
6945     "\C-c\C-e" message-elide-region
6946     "\C-c\C-v" message-delete-not-region
6947     "\C-c\C-z" message-kill-to-signature
6948     "\M-\r" message-newline-and-reformat
6949     "\C-c\C-a" mml-attach-file
6950     "\C-a" message-beginning-of-line
6951     "\t" message-tab
6952     "\M-;" comment-region)
6953
6954   (gnus-define-keys (gnus-article-edit-wash-map
6955                      "\C-c\C-w" gnus-article-edit-mode-map)
6956     "f" gnus-article-edit-full-stops))
6957
6958 (easy-menu-define
6959   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6960   '("Field"
6961     ["Fetch To" message-insert-to t]
6962     ["Fetch Newsgroups" message-insert-newsgroups t]
6963     "----"
6964     ["To" message-goto-to t]
6965     ["From" message-goto-from t]
6966     ["Subject" message-goto-subject t]
6967     ["Cc" message-goto-cc t]
6968     ["Reply-To" message-goto-reply-to t]
6969     ["Summary" message-goto-summary t]
6970     ["Keywords" message-goto-keywords t]
6971     ["Newsgroups" message-goto-newsgroups t]
6972     ["Followup-To" message-goto-followup-to t]
6973     ["Mail-Followup-To" message-goto-mail-followup-to t]
6974     ["Distribution" message-goto-distribution t]
6975     ["Body" message-goto-body t]
6976     ["Signature" message-goto-signature t]))
6977
6978 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6979   "Major mode for editing articles.
6980 This is an extended text-mode.
6981
6982 \\{gnus-article-edit-mode-map}"
6983   (make-local-variable 'gnus-article-edit-done-function)
6984   (make-local-variable 'gnus-prev-winconf)
6985   (set (make-local-variable 'font-lock-defaults)
6986        '(message-font-lock-keywords t))
6987   (set (make-local-variable 'mail-header-separator) "")
6988   (set (make-local-variable 'gnus-article-edit-mode) t)
6989   (easy-menu-add message-mode-field-menu message-mode-map)
6990   (mml-mode)
6991   (setq buffer-read-only nil)
6992   (buffer-enable-undo)
6993   (widen))
6994
6995 (defun gnus-article-edit (&optional force)
6996   "Edit the current article.
6997 This will have permanent effect only in mail groups.
6998 If FORCE is non-nil, allow editing of articles even in read-only
6999 groups."
7000   (interactive "P")
7001   (when (and (not force)
7002              (gnus-group-read-only-p))
7003     (error "The current newsgroup does not support article editing"))
7004   (gnus-article-date-original)
7005   (gnus-article-edit-article
7006    'ignore
7007    `(lambda (no-highlight)
7008       'ignore
7009       (gnus-summary-edit-article-done
7010        ,(or (mail-header-references gnus-current-headers) "")
7011        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7012
7013 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7014   "Start editing the contents of the current article buffer."
7015   (let ((winconf (current-window-configuration)))
7016     (set-buffer gnus-article-buffer)
7017     (let ((message-auto-save-directory
7018            ;; Don't associate the article buffer with a draft file.
7019            nil))
7020       (gnus-article-edit-mode))
7021     (funcall start-func)
7022     (set-buffer-modified-p nil)
7023     (gnus-configure-windows 'edit-article)
7024     (setq gnus-article-edit-done-function exit-func)
7025     (setq gnus-prev-winconf winconf)
7026     (unless quiet
7027       (gnus-message 6 "C-c C-c to end edits"))))
7028
7029 (defun gnus-article-edit-done (&optional arg)
7030   "Update the article edits and exit."
7031   (interactive "P")
7032   (let ((func gnus-article-edit-done-function)
7033         (buf (current-buffer))
7034         (start (window-start))
7035         (p (point))
7036         (winconf gnus-prev-winconf))
7037     (widen) ;; Widen it in case that users narrowed the buffer.
7038     (funcall func arg)
7039     (set-buffer buf)
7040     ;; The cache and backlog have to be flushed somewhat.
7041     (when gnus-keep-backlog
7042       (gnus-backlog-remove-article
7043        (car gnus-article-current) (cdr gnus-article-current)))
7044     ;; Flush original article as well.
7045     (gnus-flush-original-article-buffer)
7046     (when gnus-use-cache
7047       (gnus-cache-update-article
7048        (car gnus-article-current) (cdr gnus-article-current)))
7049     ;; We remove all text props from the article buffer.
7050     (kill-all-local-variables)
7051     (set-text-properties (point-min) (point-max) nil)
7052     (gnus-article-mode)
7053     (set-window-configuration winconf)
7054     (set-buffer buf)
7055     (set-window-start (get-buffer-window buf) start)
7056     (set-window-point (get-buffer-window buf) (point)))
7057   (gnus-summary-show-article))
7058
7059 (defun gnus-flush-original-article-buffer ()
7060   (when (get-buffer gnus-original-article-buffer)
7061     (with-current-buffer gnus-original-article-buffer
7062       (setq gnus-original-article nil))))
7063
7064 (defun gnus-article-edit-exit ()
7065   "Exit the article editing without updating."
7066   (interactive)
7067   (when (or (not (buffer-modified-p))
7068             (yes-or-no-p "Article modified; kill anyway? "))
7069     (let ((curbuf (current-buffer))
7070           (p (point))
7071           (window-start (window-start)))
7072       (erase-buffer)
7073       (if (gnus-buffer-live-p gnus-original-article-buffer)
7074           (insert-buffer-substring gnus-original-article-buffer))
7075       (let ((winconf gnus-prev-winconf))
7076         (kill-all-local-variables)
7077         (gnus-article-mode)
7078         (set-window-configuration winconf)
7079         ;; Tippy-toe some to make sure that point remains where it was.
7080         (save-current-buffer
7081           (set-buffer curbuf)
7082           (set-window-start (get-buffer-window (current-buffer)) window-start)
7083           (goto-char p))))
7084     (gnus-summary-show-article)))
7085
7086 (defun gnus-article-edit-full-stops ()
7087   "Interactively repair spacing at end of sentences."
7088   (interactive)
7089   (save-excursion
7090     (goto-char (point-min))
7091     (search-forward-regexp "^$" nil t)
7092     (let ((case-fold-search nil))
7093       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7094
7095 ;;;
7096 ;;; Article highlights
7097 ;;;
7098
7099 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7100
7101 ;;; Internal Variables:
7102
7103 (defcustom gnus-button-url-regexp
7104   (concat
7105    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7106    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7107    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7108    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7109        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7110              (punct "!?:;.,"))
7111          (concat
7112           "\\(?:"
7113           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7114           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7115           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7116           "\\|"
7117           "[" chars punct     "]+" "[" chars "]"
7118           "\\)"))
7119      (concat ;; XEmacs 21.4 doesn't support POSIX.
7120       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7121       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7122    "\\)")
7123   "Regular expression that matches URLs."
7124   :group 'gnus-article-buttons
7125   :type 'regexp)
7126
7127 (defcustom gnus-button-valid-fqdn-regexp
7128   message-valid-fqdn-regexp
7129   "Regular expression that matches a valid FQDN."
7130   :version "22.1"
7131   :group 'gnus-article-buttons
7132   :type 'regexp)
7133
7134 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7135 (defcustom gnus-button-valid-localpart-regexp
7136   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7137   "Regular expression that matches a localpart of mail addresses or MIDs."
7138   :version "22.1"
7139   :group 'gnus-article-buttons
7140   :type 'regexp)
7141
7142 (defcustom gnus-button-man-handler 'manual-entry
7143   "Function to use for displaying man pages.
7144 The function must take at least one argument with a string naming the
7145 man page."
7146   :version "22.1"
7147   :type '(choice (function-item :tag "Man" manual-entry)
7148                  (function-item :tag "Woman" woman)
7149                  (function :tag "Other"))
7150   :group 'gnus-article-buttons)
7151
7152 (defcustom gnus-button-mid-or-mail-regexp
7153   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7154           gnus-button-valid-fqdn-regexp
7155           ">?\\)\\b")
7156   "Regular expression that matches a message ID or a mail address."
7157   :version "22.1"
7158   :group 'gnus-article-buttons
7159   :type 'regexp)
7160
7161 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7162   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7163 Strings like this can be either a message ID or a mail address.  If it is one
7164 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7165 message ID or a mail address, respectively.  If this variable is set to the
7166 symbol `ask', always query the user what do do.  If it is a function, this
7167 function will be called with the string as its only argument.  The function
7168 must return `mid', `mail', `invalid' or `ask'."
7169   :version "22.1"
7170   :group 'gnus-article-buttons
7171   :type '(choice (function-item :tag "Heuristic function"
7172                                 gnus-button-mid-or-mail-heuristic)
7173                  (const ask)
7174                  (const mid)
7175                  (const mail)))
7176
7177 (defcustom gnus-button-mid-or-mail-heuristic-alist
7178   '((-10.0 . ".+\\$.+@")
7179     (-10.0 . "#")
7180     (-10.0 . "\\*")
7181     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7182     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7183     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7184     (-1.0  . "^[^a-z]+@")
7185     ;;
7186     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7187     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7188     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7189     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7190     ;;
7191     (-2.0  . "^[0-9]")
7192     (-1.0  . "^[0-9][0-9]")
7193     ;;
7194     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7195     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7196     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7197     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7198     ;;
7199     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7200     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7201     ;;       "[0-9]{8,}.*\@"
7202     (-3.0
7203      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7204     ;; "[0-9]{12,}.*\@"
7205     ;; compensation for TDMA dated mail addresses:
7206     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7207     ;;
7208     (-20.0 . "\\.fsf@") ;; Gnus
7209     (-20.0 . "^slrn")
7210     (-20.0 . "^Pine")
7211     (-20.0 . "^alpine\\.")
7212     (-20.0 . "_-_") ;; Subject change in thread
7213     ;;
7214     (-20.0 . "\\.ln@") ;; leafnode
7215     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7216     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7217     ;;
7218     ;; (5.0 . "") ;; $local_part_len <= 7
7219     (10.0  . "^[^0-9]+@")
7220     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7221     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7222     (3.0   . "\@stud")
7223     ;;
7224     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7225     ;;
7226     (0.5   . "^[A-Z][a-z]")
7227     (0.5   . "^[A-Z][a-z][a-z]")
7228     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7229     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7230   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7231
7232 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7233 address.  The REGEXP is processed with `case-fold-search' set to nil."
7234   :version "22.1"
7235   :group 'gnus-article-buttons
7236   :type '(repeat (cons (number :tag "Rate")
7237                        (regexp :tag "Regexp"))))
7238
7239 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7240   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7241 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7242 address, `ask' if unsure and `invalid' if the string is invalid."
7243   (let ((case-fold-search nil)
7244         (list gnus-button-mid-or-mail-heuristic-alist)
7245         (result 0) rate regexp lpartlen elem)
7246     (setq lpartlen
7247           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7248     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7249     ;; Certain special cases...
7250     (when (string-match
7251            (concat
7252             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7253             "^[0-9]+\\.[0-9]+@compuserve\\|"
7254             "@public\\.gmane\\.org")
7255            mid-or-mail)
7256       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7257       (setq result 'mail))
7258     (when (string-match "@.*@\\| " mid-or-mail)
7259       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7260       (setq result 'invalid))
7261     ;; Nothing more to do, if result is not a number here...
7262     (when (numberp result)
7263       (while list
7264         (setq elem (car list)
7265               rate (car elem)
7266               regexp (cdr elem)
7267               list (cdr list))
7268         (when (string-match regexp mid-or-mail)
7269           (setq result (+ result rate))
7270           (gnus-message
7271            9 "`%s' matched `%s', rate `%s', result `%s'."
7272            mid-or-mail regexp rate result)))
7273       (when (<= lpartlen 7)
7274         (setq result (+ result 5.0))
7275         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7276                       mid-or-mail result))
7277       (when (>= lpartlen 12)
7278         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7279         (cond
7280          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7281           ;; Long local part should contain realname if e-mail address,
7282           ;; too many digits: message-id.
7283           ;; $score -= 5.0 + 0.1 * $local_part_len;
7284           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7285           (setq result (+ result rate))
7286           (gnus-message
7287            9 "Many digits in `%s', rate `%s', result `%s'."
7288            mid-or-mail rate result))
7289          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7290                         mid-or-mail)
7291           ;; Too few vowels [^aeiouy]{4,}.*\@
7292           (setq result (+ result -5.0))
7293           (gnus-message
7294            9 "Few vowels in `%s', rate `%s', result `%s'."
7295            mid-or-mail -5.0 result))
7296          (t
7297           (setq result (+ result 5.0))
7298           (gnus-message
7299            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7300     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7301     ;; Maybe we should make this a customizable alist: (condition . 'result)
7302     (cond
7303      ((symbolp result) result)
7304      ;; Now convert number into proper results:
7305      ((< result -10.0) 'mid)
7306      ((> result  10.0) 'mail)
7307      (t 'ask))))
7308
7309 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7310   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7311          (url-mid (concat "news" ":" mid-or-mail))
7312          (url-mailto (concat "mailto" ":" mid-or-mail)))
7313     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7314     (when (fboundp pref)
7315       (setq guessed
7316             ;; get rid of surrounding angles...
7317             (funcall pref
7318                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7319       (if (or (eq 'mid guessed) (eq 'mail guessed))
7320           (setq pref guessed)
7321         (setq pref 'ask)))
7322     (if (eq pref 'ask)
7323         (save-window-excursion
7324           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7325               (setq pref 'mail)
7326             (setq pref 'mid))))
7327     (cond ((eq pref 'mid)
7328            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7329            (gnus-button-handle-news url-mid))
7330           ((eq pref 'mail)
7331            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7332            (gnus-url-mailto url-mailto))
7333           (t (gnus-message 3 "Invalid string.")))))
7334
7335 (defun gnus-button-handle-custom (fun arg)
7336   "Call function FUN on argument ARG.
7337 Both FUN and ARG are supposed to be strings.  ARG will be passed
7338 as a symbol to FUN."
7339   (funcall (intern fun)
7340            (if (string-match "^customize-apropos" fun)
7341                arg
7342              (intern arg))))
7343
7344 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7345
7346 (defun gnus-button-handle-describe-function (url)
7347   "Call `describe-function' when pushing the corresponding URL button."
7348   (describe-function
7349    (intern
7350     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7351
7352 (defun gnus-button-handle-describe-variable (url)
7353   "Call `describe-variable' when pushing the corresponding URL button."
7354   (describe-variable
7355    (intern
7356     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7357
7358 (defun gnus-button-handle-symbol (url)
7359 "Display help on variable or function.
7360 Calls `describe-variable' or `describe-function'."
7361   (let ((sym (intern url)))
7362     (cond
7363      ((fboundp sym) (describe-function sym))
7364      ((boundp sym) (describe-variable sym))
7365      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7366
7367 (defun gnus-button-handle-describe-key (url)
7368   "Call `describe-key' when pushing the corresponding URL button."
7369   (let* ((key-string
7370           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7371          (keys (ignore-errors (eval `(kbd ,key-string)))))
7372     (if keys
7373         (describe-key keys)
7374       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7375
7376 (defun gnus-button-handle-apropos (url)
7377   "Call `apropos' when pushing the corresponding URL button."
7378   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7379
7380 (defun gnus-button-handle-apropos-command (url)
7381   "Call `apropos' when pushing the corresponding URL button."
7382   (apropos-command
7383    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7384
7385 (defun gnus-button-handle-apropos-variable (url)
7386   "Call `apropos' when pushing the corresponding URL button."
7387   (funcall
7388    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7389    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7390
7391 (defun gnus-button-handle-apropos-documentation (url)
7392   "Call `apropos' when pushing the corresponding URL button."
7393   (funcall
7394    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7395    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7396
7397 (defun gnus-button-handle-library (url)
7398   "Call `locate-library' when pushing the corresponding URL button."
7399   (gnus-message 9 "url=`%s'" url)
7400   (let* ((lib (locate-library url))
7401          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7402     (if (not lib)
7403         (gnus-message 1 "Cannot locale library `%s'." url)
7404       (find-file-read-only file))))
7405
7406 (defcustom gnus-button-man-level 5
7407   "*Integer that says how many man-related buttons Gnus will show.
7408 The higher the number, the more buttons will appear and the more false
7409 positives are possible.  Note that you can set this variable local to
7410 specific groups.  Setting it higher in Unix groups is probably a good idea.
7411 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7412 how to set variables in specific groups."
7413   :version "22.1"
7414   :group 'gnus-article-buttons
7415   :link '(custom-manual "(gnus)Group Parameters")
7416   :type 'integer)
7417
7418 (defcustom gnus-button-emacs-level 5
7419   "*Integer that says how many emacs-related buttons Gnus will show.
7420 The higher the number, the more buttons will appear and the more false
7421 positives are possible.  Note that you can set this variable local to
7422 specific groups.  Setting it higher in Emacs or Gnus related groups is
7423 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7424 `gnus-parameters' on how to set variables in specific groups."
7425   :version "22.1"
7426   :group 'gnus-article-buttons
7427   :link '(custom-manual "(gnus)Group Parameters")
7428   :type 'integer)
7429
7430 (defcustom gnus-button-message-level 5
7431   "*Integer that says how many buttons for news or mail messages will appear.
7432 The higher the number, the more buttons will appear and the more false
7433 positives are possible."
7434   ;; mail addresses, MIDs, URLs for news, ...
7435   :version "22.1"
7436   :group 'gnus-article-buttons
7437   :type 'integer)
7438
7439 (defcustom gnus-button-browse-level 5
7440   "*Integer that says how many buttons for browsing will appear.
7441 The higher the number, the more buttons will appear and the more false
7442 positives are possible."
7443   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7444   :version "22.1"
7445   :group 'gnus-article-buttons
7446   :type 'integer)
7447
7448 (defcustom gnus-button-alist
7449   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7450      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7451     ((concat "\\b\\(nntp\\|news\\):\\("
7452              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7453      0 t gnus-button-handle-news 2)
7454     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7455      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7456     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7457      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7458     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7459     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7460      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7461     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7462      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7463     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7464      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7465     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7466      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7467     ;; RFC 2368 (The mailto URL scheme)
7468     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7469      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7470     ("\\bmailto:\\([^ \n\t]+\\)"
7471      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7472     ;; Info Konqueror style <info:/foo/bar baz>.
7473     ;; Must come before " Gnus home-grown style".
7474     ("\\binfo://?\\([^'\">\n\t]+\\)"
7475      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7476    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7477     ("\\binfo://\\([^'\">\n\t ]+\\)"
7478      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7479     ;; Info GNOME style <info:foo#bar_baz>
7480     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7481      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7482     ;; Info KDE style <info:(foo)bar baz>
7483     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7484      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7485     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7486      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7487     ("\\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,]*\\)\\)?"
7488      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7489      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7490     ;; This is custom
7491     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7492      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7493     ;; Emacs help commands
7494     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7495      ;; regexp doesn't match arguments containing ` '.
7496      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7497     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7498      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7499     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7500      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7501     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7502      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7503     ;; The following entries may lead to many false positives so don't enable
7504     ;; them by default (use a high button level).
7505     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7506      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7507      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7508     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7509      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7510     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7511      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7512     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7513      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7514     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7515      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7516     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7517      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7518     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7519      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7520     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7521      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7522     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7523      ;; Unlike the other regexps we really have to require quoting
7524      ;; here to determine where it ends.
7525      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7526     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7527     ("<URL: *\\([^\n<>]*\\)>"
7528      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7529     ;; RFC 2396 (2.4.3., delims) ...
7530     ("\"URL: *\\([^\n\"]*\\)\""
7531      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7532     ;; Raw URLs.
7533     (gnus-button-url-regexp
7534      0 (>= gnus-button-browse-level 0) browse-url 0)
7535     ;; man pages
7536     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7537      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7538      gnus-button-handle-man 1)
7539     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7540     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7541      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7542      gnus-button-handle-man 1)
7543     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7544     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7545     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7546      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7547     ;; Recognizing patches to .el files.  This is somewhat obscure,
7548     ;; but considering the percentage of Gnus users who hack Emacs
7549     ;; Lisp files...
7550     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7551      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7552     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7553      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7554     ;; MID or mail: To avoid too many false positives we don't try to catch
7555     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7556     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7557     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7558     ;; so that non-ambiguous entries (see above) match first.
7559     (gnus-button-mid-or-mail-regexp
7560      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7561   "*Alist of regexps matching buttons in article bodies.
7562
7563 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7564 REGEXP: is the string (case insensitive) matching text around the button (can
7565 also be Lisp expression evaluating to a string),
7566 BUTTON: is the number of the regexp grouping actually matching the button,
7567 FORM: is a Lisp expression which must eval to true for the button to
7568 be added,
7569 CALLBACK: is the function to call when the user push this button, and each
7570 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7571
7572 CALLBACK can also be a variable, in that case the value of that
7573 variable it the real callback function."
7574   :group 'gnus-article-buttons
7575   :type '(repeat (list (choice regexp variable sexp)
7576                        (integer :tag "Button")
7577                        (sexp :tag "Form")
7578                        (function :tag "Callback")
7579                        (repeat :tag "Par"
7580                                :inline t
7581                                (integer :tag "Regexp group")))))
7582 (put 'gnus-button-alist 'risky-local-variable t)
7583
7584 (defcustom gnus-header-button-alist
7585   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7586      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7587     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7588      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7589     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7590      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7591     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7592      0 (>= gnus-button-browse-level 0) browse-url 0)
7593     ("^Subject:" gnus-button-url-regexp
7594      0 (>= gnus-button-browse-level 0) browse-url 0)
7595     ("^[^:]+:" gnus-button-url-regexp
7596      0 (>= gnus-button-browse-level 0) browse-url 0)
7597     ("^OpenPGP:.*url=" gnus-button-url-regexp
7598      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7599     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7600      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7601     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7602      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7603   "*Alist of headers and regexps to match buttons in article heads.
7604
7605 This alist is very similar to `gnus-button-alist', except that each
7606 alist has an additional HEADER element first in each entry:
7607
7608 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7609
7610 HEADER is a regexp to match a header.  For a fuller explanation, see
7611 `gnus-button-alist'."
7612   :group 'gnus-article-buttons
7613   :group 'gnus-article-headers
7614   :type '(repeat (list (regexp :tag "Header")
7615                        (choice regexp variable)
7616                        (integer :tag "Button")
7617                        (sexp :tag "Form")
7618                        (function :tag "Callback")
7619                        (repeat :tag "Par"
7620                                :inline t
7621                                (integer :tag "Regexp group")))))
7622 (put 'gnus-header-button-alist 'risky-local-variable t)
7623
7624 ;;; Commands:
7625
7626 (defun gnus-article-push-button (event)
7627   "Check text under the mouse pointer for a callback function.
7628 If the text under the mouse pointer has a `gnus-callback' property,
7629 call it with the value of the `gnus-data' text property."
7630   (interactive "e")
7631   (set-buffer (window-buffer (posn-window (event-start event))))
7632   (let* ((pos (posn-point (event-start event)))
7633          (data (get-text-property pos 'gnus-data))
7634          (fun (get-text-property pos 'gnus-callback)))
7635     (goto-char pos)
7636     (when fun
7637       (funcall fun data))))
7638
7639 (defun gnus-article-press-button ()
7640   "Check text at point for a callback function.
7641 If the text at point has a `gnus-callback' property,
7642 call it with the value of the `gnus-data' text property."
7643   (interactive)
7644   (let ((data (get-text-property (point) 'gnus-data))
7645         (fun (get-text-property (point) 'gnus-callback)))
7646     (when fun
7647       (funcall fun data))))
7648
7649 (defun gnus-article-highlight (&optional force)
7650   "Highlight current article.
7651 This function calls `gnus-article-highlight-headers',
7652 `gnus-article-highlight-citation',
7653 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7654 do the highlighting.  See the documentation for those functions."
7655   (interactive (list 'force))
7656   (gnus-article-highlight-headers)
7657   (gnus-article-highlight-citation force)
7658   (gnus-article-highlight-signature)
7659   (gnus-article-add-buttons)
7660   (gnus-article-add-buttons-to-head))
7661
7662 (defun gnus-article-highlight-some (&optional force)
7663   "Highlight current article.
7664 This function calls `gnus-article-highlight-headers',
7665 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7666 do the highlighting.  See the documentation for those functions."
7667   (interactive (list 'force))
7668   (gnus-article-highlight-headers)
7669   (gnus-article-highlight-signature)
7670   (gnus-article-add-buttons))
7671
7672 (defun gnus-article-highlight-headers ()
7673   "Highlight article headers as specified by `gnus-header-face-alist'."
7674   (interactive)
7675   (gnus-with-article-headers
7676     (let (regexp header-face field-face from hpoints fpoints)
7677       (dolist (entry gnus-header-face-alist)
7678         (goto-char (point-min))
7679         (setq regexp (concat "^\\("
7680                              (if (string-equal "" (nth 0 entry))
7681                                  "[^\t ]"
7682                                (nth 0 entry))
7683                              "\\)")
7684               header-face (nth 1 entry)
7685               field-face (nth 2 entry))
7686         (while (and (re-search-forward regexp nil t)
7687                     (not (eobp)))
7688           (beginning-of-line)
7689           (setq from (point))
7690           (unless (search-forward ":" nil t)
7691             (forward-char 1))
7692           (when (and header-face
7693                      (not (memq (point) hpoints)))
7694             (push (point) hpoints)
7695             (gnus-put-text-property from (point) 'face header-face))
7696           (when (and field-face
7697                      (not (memq (setq from (point)) fpoints)))
7698             (push from fpoints)
7699             (if (re-search-forward "^[^ \t]" nil t)
7700                 (forward-char -2)
7701               (goto-char (point-max)))
7702             (gnus-put-text-property from (point) 'face field-face)))))))
7703
7704 (defun gnus-article-highlight-signature ()
7705   "Highlight the signature in an article.
7706 It does this by highlighting everything after
7707 `gnus-signature-separator' using the face `gnus-signature'."
7708   (interactive)
7709   (gnus-with-article-buffer
7710     (let ((inhibit-point-motion-hooks t))
7711       (save-restriction
7712         (when (and gnus-signature-face
7713                    (gnus-article-narrow-to-signature))
7714           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7715                             'face gnus-signature-face)
7716           (widen)
7717           (gnus-article-search-signature)
7718           (let ((start (match-beginning 0))
7719                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7720             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7721                                      end)))))))
7722
7723 (defun gnus-button-in-region-p (b e prop)
7724   "Say whether PROP exists in the region."
7725   (text-property-not-all b e prop nil))
7726
7727 (defun gnus-article-add-buttons ()
7728   "Find external references in the article and make buttons of them.
7729 \"External references\" are things like Message-IDs and URLs, as
7730 specified by `gnus-button-alist'."
7731   (interactive)
7732   (gnus-with-article-buffer
7733     (let ((inhibit-point-motion-hooks t)
7734           (case-fold-search t)
7735           (alist gnus-button-alist)
7736           beg entry regexp)
7737       ;; We skip the headers.
7738       (article-goto-body)
7739       (setq beg (point))
7740       (while (setq entry (pop alist))
7741         (setq regexp (eval (car entry)))
7742         (goto-char beg)
7743         (while (re-search-forward regexp nil t)
7744           (let ((start (match-beginning (nth 1 entry)))
7745                 (end (match-end (nth 1 entry)))
7746                 (from (match-beginning 0)))
7747             (when (and (eval (nth 2 entry))
7748                        (not (gnus-button-in-region-p
7749                              start end 'gnus-callback)))
7750               ;; That optional form returned non-nil, so we add the
7751               ;; button.
7752               (setq from (set-marker (make-marker) from))
7753               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7754                            (gnus-article-extend-url-button from start end))
7755                 (gnus-article-add-button start end
7756                                          'gnus-button-push (list from entry))
7757                 (gnus-put-text-property
7758                  start end
7759                  'gnus-string (buffer-substring-no-properties
7760                                start end))))))))))
7761
7762 (defun gnus-article-extend-url-button (beg start end)
7763   "Extend url button if url is folded into two or more lines.
7764 Return non-nil if button is extended.  BEG is a marker that points to
7765 the beginning position of a text containing url.  START and END are
7766 the endpoints of a url button before it is extended.  The concatenated
7767 url is put as the `gnus-button-url' overlay property on the button."
7768   (let ((opoint (point))
7769         (points (list start end))
7770         url delim regexp)
7771     (prog1
7772         (when (and (progn
7773                      (goto-char end)
7774                      (not (looking-at "[\t ]*[\">]")))
7775                    (progn
7776                      (goto-char start)
7777                      (string-match
7778                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7779                       (buffer-substring (point-at-bol) start)))
7780                    (progn
7781                      (setq url (list (buffer-substring start end))
7782                            delim (if (match-beginning 1) ">" "\""))
7783                      (beginning-of-line)
7784                      (setq regexp (concat
7785                                    (when (and (looking-at
7786                                                message-cite-prefix-regexp)
7787                                               (< (match-end 0) start))
7788                                      (regexp-quote (match-string 0)))
7789                                    "\
7790 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7791                                    delim "\\)"))
7792                      (while (progn
7793                               (forward-line 1)
7794                               (and (looking-at regexp)
7795                                    (prog1
7796                                        (match-beginning 1)
7797                                      (push (or (match-string 2)
7798                                                (match-string 1))
7799                                            url)
7800                                      (push (setq end (or (match-end 2)
7801                                                          (match-end 1)))
7802                                            points)
7803                                      (push (or (match-beginning 2)
7804                                                (match-beginning 1))
7805                                            points)))))
7806                      (match-beginning 2)))
7807           (let (gnus-article-mouse-face widget-mouse-face)
7808             (while points
7809               (gnus-article-add-button (pop points) (pop points)
7810                                        'gnus-button-push beg)))
7811           (let ((overlay (gnus-make-overlay start end)))
7812             (gnus-overlay-put overlay 'evaporate t)
7813             (gnus-overlay-put overlay 'gnus-button-url
7814                               (list (mapconcat 'identity (nreverse url) "")))
7815             (when gnus-article-mouse-face
7816               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7817           t)
7818       (goto-char opoint))))
7819
7820 ;; Add buttons to the head of an article.
7821 (defun gnus-article-add-buttons-to-head ()
7822   "Add buttons to the head of the article."
7823   (interactive)
7824   (gnus-with-article-headers
7825     (let (beg end)
7826       (dolist (entry gnus-header-button-alist)
7827         ;; Each alist entry.
7828         (goto-char (point-min))
7829         (while (re-search-forward (car entry) nil t)
7830           ;; Each header matching the entry.
7831           (setq beg (match-beginning 0))
7832           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7833                              (match-beginning 0))
7834                         (point-max)))
7835           (goto-char beg)
7836           (while (re-search-forward (eval (nth 1 entry)) end t)
7837             ;; Each match within a header.
7838             (let* ((entry (cdr entry))
7839                    (start (match-beginning (nth 1 entry)))
7840                    (end (match-end (nth 1 entry)))
7841                    (form (nth 2 entry)))
7842               (goto-char (match-end 0))
7843               (when (eval form)
7844                 (gnus-article-add-button
7845                  start end (nth 3 entry)
7846                  (buffer-substring (match-beginning (nth 4 entry))
7847                                    (match-end (nth 4 entry)))))))
7848           (goto-char end))))))
7849
7850 ;;; External functions:
7851
7852 (defun gnus-article-add-button (from to fun &optional data text)
7853   "Create a button between FROM and TO with callback FUN and data DATA."
7854   (when gnus-article-button-face
7855     (gnus-overlay-put (gnus-make-overlay from to nil t)
7856                       'face gnus-article-button-face))
7857   (gnus-add-text-properties
7858    from to
7859    (nconc (and gnus-article-mouse-face
7860                (list gnus-mouse-face-prop gnus-article-mouse-face))
7861           (list 'gnus-callback fun)
7862           (and data (list 'gnus-data data))))
7863   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7864                          :help-echo (or text "Follow the link")
7865                          :keymap gnus-url-button-map
7866                          :button-keymap gnus-widget-button-keymap))
7867
7868 (defun gnus-article-copy-string ()
7869   "Copy the string in the button to the kill ring."
7870   (interactive)
7871   (gnus-article-check-buffer)
7872   (let ((data (get-text-property (point) 'gnus-string)))
7873     (when data
7874       (with-temp-buffer
7875         (insert data)
7876         (copy-region-as-kill (point-min) (point-max))
7877         (message "Copied %s" data)))))
7878
7879 ;;; Internal functions:
7880
7881 (defun gnus-article-set-globals ()
7882   (with-current-buffer gnus-summary-buffer
7883     (gnus-set-global-variables)))
7884
7885 (defun gnus-signature-toggle (end)
7886   (gnus-with-article-buffer
7887     (let ((inhibit-point-motion-hooks t))
7888       (if (text-property-any end (point-max) 'article-type 'signature)
7889           (progn
7890             (gnus-delete-wash-type 'signature)
7891             (gnus-remove-text-properties-when
7892              'article-type 'signature end (point-max)
7893              (cons 'article-type (cons 'signature
7894                                        gnus-hidden-properties))))
7895         (gnus-add-wash-type 'signature)
7896         (gnus-add-text-properties-when
7897          'article-type nil end (point-max)
7898          (cons 'article-type (cons 'signature
7899                                    gnus-hidden-properties)))))
7900     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7901       (gnus-set-mode-line 'article))))
7902
7903 (defun gnus-button-push (marker-and-entry)
7904   ;; Push button starting at MARKER.
7905   (save-excursion
7906     (let* ((marker (car marker-and-entry))
7907            (entry (cadr marker-and-entry))
7908            (regexp (car entry))
7909            (inhibit-point-motion-hooks t))
7910       (goto-char marker)
7911       ;; This is obviously true, or something bad is happening :)
7912       ;; But we need it to have the match-data
7913       (when (looking-at (or (if (symbolp regexp)
7914                                 (symbol-value regexp)
7915                               regexp)))
7916         (let ((fun (nth 3 entry))
7917               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7918                              (get-char-property marker 'gnus-button-url))
7919                         (mapcar (lambda (group)
7920                                   (let ((string (match-string group)))
7921                                     (set-text-properties
7922                                      0 (length string) nil string)
7923                                     string))
7924                                 (nthcdr 4 entry)))))
7925
7926           (cond
7927            ((fboundp fun)
7928             (apply fun args))
7929            ((and (boundp fun)
7930                  (fboundp (symbol-value fun)))
7931             (apply (symbol-value fun) args))
7932            (t
7933             (gnus-message 1 "You must define `%S' to use this button"
7934                           (cons fun args)))))))))
7935
7936 (defun gnus-parse-news-url (url)
7937   (let (scheme server port group message-id articles)
7938     (with-temp-buffer
7939       (insert url)
7940       (goto-char (point-min))
7941       (when (looking-at "\\([A-Za-z]+\\):")
7942         (setq scheme (match-string 1))
7943         (goto-char (match-end 0)))
7944       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7945         (setq server (match-string 1))
7946         (setq port (if (stringp (match-string 3))
7947                        (string-to-number (match-string 3))
7948                      (match-string 3)))
7949         (goto-char (match-end 0)))
7950
7951       (cond
7952        ((looking-at "\\(.*@.*\\)")
7953         (setq message-id (match-string 1)))
7954        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7955         (setq group (match-string 1)
7956               articles (split-string (match-string 2) "-")))
7957        ((looking-at "\\([^/]+\\)/?")
7958         (setq group (match-string 1)))
7959        (t
7960         (error "Unknown news URL syntax"))))
7961     (list scheme server port group message-id articles)))
7962
7963 (defun gnus-button-handle-news (url)
7964   "Fetch a news URL."
7965   (destructuring-bind (scheme server port group message-id articles)
7966       (gnus-parse-news-url url)
7967     (cond
7968      (message-id
7969       (with-current-buffer gnus-summary-buffer
7970         (if server
7971             (let ((gnus-refer-article-method
7972                    (nconc (list (list 'nntp server))
7973                           gnus-refer-article-method))
7974                   (nntp-port-number (or port "nntp")))
7975               (gnus-message 7 "Fetching %s with %s"
7976                             message-id gnus-refer-article-method)
7977               (gnus-summary-refer-article message-id))
7978           (gnus-summary-refer-article message-id))))
7979      (group
7980       (gnus-button-fetch-group url)))))
7981
7982 (defun gnus-button-patch (library line)
7983   "Visit an Emacs Lisp library LIBRARY on line LINE."
7984   (interactive)
7985   (let ((file (locate-library (file-name-nondirectory library))))
7986     (unless file
7987       (error "Couldn't find library %s" library))
7988     (find-file file)
7989     (goto-char (point-min))
7990     (forward-line (1- (string-to-number line)))))
7991
7992 (defun gnus-button-handle-man (url)
7993   "Fetch a man page."
7994   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7995   (when (eq gnus-button-man-handler 'woman)
7996     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7997   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7998   (funcall gnus-button-man-handler url))
7999
8000 (defun gnus-button-handle-info-url (url)
8001   "Fetch an info URL."
8002   (setq url (mm-subst-char-in-string ?+ ?\  url))
8003   (cond
8004    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8005     (gnus-info-find-node
8006      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8007                      "Gnus")
8008              ")" (gnus-url-unhex-string (match-string 2 url)))))
8009    ((string-match "([^)\"]+)[^\"]+" url)
8010     (setq url
8011           (gnus-replace-in-string
8012            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8013     (gnus-info-find-node url))
8014    (t (error "Can't parse %s" url))))
8015
8016 (defun gnus-button-handle-info-url-gnome (url)
8017   "Fetch GNOME style info URL."
8018   (setq url (mm-subst-char-in-string ?_ ?\  url))
8019   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8020       (gnus-info-find-node
8021        (concat "("
8022                (gnus-url-unhex-string
8023                  (match-string 1 url))
8024                ")"
8025                (or (gnus-url-unhex-string
8026                     (match-string 2 url))
8027                    "Top")))
8028     (error "Can't parse %s" url)))
8029
8030 (defun gnus-button-handle-info-url-kde (url)
8031   "Fetch KDE style info URL."
8032   (gnus-info-find-node (gnus-url-unhex-string url)))
8033
8034 ;; (info) will autoload info.el
8035 (declare-function Info-menu "info" (menu-item &optional fork))
8036 (declare-function Info-index-next "info" (num))
8037
8038 (defun gnus-button-handle-info-keystrokes (url)
8039   "Call `info' when pushing the corresponding URL button."
8040   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8041   (let (node indx comma)
8042     (if (string-match
8043          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8044                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8045                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8046                  "\\(?:[ \t\n,]*\\)\\)?")
8047          url)
8048         (setq node (match-string 2 url)
8049               indx (match-string 3 url))
8050       (error "Can't parse %s" url))
8051     (info)
8052     (Info-directory)
8053     (Info-menu node)
8054     (when (> (length indx) 0)
8055       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8056                             "\\([ \t\n,]*\\)")
8057                     indx)
8058       (setq comma (match-string 2 indx))
8059       (setq indx  (match-string 1 indx))
8060       (Info-index indx)
8061       (when comma
8062         (dotimes (i (with-temp-buffer
8063                       (insert comma)
8064                       ;; Note: the XEmacs version of `how-many' takes
8065                       ;; no optional argument.
8066                       (goto-char (point-min))
8067                       (how-many ",")))
8068           (Info-index-next 1)))
8069       nil)))
8070
8071 (autoload 'pgg-snarf-keys-region "pgg")
8072 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8073 (declare-function pgg-display-output-buffer "pgg" (start end status))
8074
8075 (defun gnus-button-openpgp (url)
8076   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8077   (with-temp-buffer
8078     (mm-url-insert-file-contents-external url)
8079     (pgg-snarf-keys-region (point-min) (point-max))
8080     (pgg-display-output-buffer nil nil nil)))
8081
8082 (defun gnus-button-message-id (message-id)
8083   "Fetch MESSAGE-ID."
8084   (with-current-buffer gnus-summary-buffer
8085     (gnus-summary-refer-article message-id)))
8086
8087 (defun gnus-button-fetch-group (address &rest ignore)
8088   "Fetch GROUP specified by ADDRESS."
8089   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8090                       address)
8091     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8092     ;; for nntp:// and news://
8093     (setq address (match-string 3 address)))
8094   (if (not (string-match "[:/]" address))
8095       ;; This is just a simple group url.
8096       (gnus-group-read-ephemeral-group address gnus-select-method)
8097     (if (not
8098          (string-match
8099           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8100           address))
8101         (error "Can't parse %s" address)
8102       (gnus-group-read-ephemeral-group
8103        (match-string 4 address)
8104        `(nntp ,(match-string 1 address)
8105               (nntp-address ,(match-string 1 address))
8106               (nntp-port-number ,(if (match-end 3)
8107                                      (match-string 3 address)
8108                                    "nntp")))
8109        nil nil nil
8110        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8111
8112 (defun gnus-url-parse-query-string (query &optional downcase)
8113   (let (retval pairs cur key val)
8114     (setq pairs (split-string query "&"))
8115     (while pairs
8116       (setq cur (car pairs)
8117             pairs (cdr pairs))
8118       (if (not (string-match "=" cur))
8119           nil                           ; Grace
8120         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8121               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8122         (if downcase
8123             (setq key (downcase key)))
8124         (setq cur (assoc key retval))
8125         (if cur
8126             (setcdr cur (cons val (cdr cur)))
8127           (setq retval (cons (list key val) retval)))))
8128     retval))
8129
8130 (defun gnus-url-mailto (url)
8131   ;; Send mail to someone
8132   (setq url (replace-regexp-in-string "\n" " " url))
8133   (when (string-match "mailto:/*\\(.*\\)" url)
8134     (setq url (substring url (match-beginning 1) nil)))
8135   (let (to args subject func)
8136     (setq args (gnus-url-parse-query-string
8137                 (if (string-match "^\\?" url)
8138                     (substring url 1)
8139                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8140                       (concat "to=" (match-string 1 url) "&"
8141                               (match-string 2 url))
8142                     (concat "to=" url))))
8143           subject (cdr-safe (assoc "subject" args)))
8144     (gnus-msg-mail)
8145     (while args
8146       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8147       (if (fboundp func)
8148           (funcall func)
8149         (message-position-on-field (caar args)))
8150       (insert (gnus-replace-in-string
8151                (mapconcat 'identity (reverse (cdar args)) ", ")
8152                "\r\n" "\n" t))
8153       (setq args (cdr args)))
8154     (if subject
8155         (message-goto-body)
8156       (message-goto-subject))))
8157
8158 (defun gnus-button-embedded-url (address)
8159   "Activate ADDRESS with `browse-url'."
8160   (browse-url (gnus-strip-whitespace address)))
8161
8162 ;;; Next/prev buttons in the article buffer.
8163
8164 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8165 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8166
8167 (defvar gnus-prev-page-map
8168   (let ((map (make-sparse-keymap)))
8169     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8170     (define-key map "\r" 'gnus-button-prev-page)
8171     map))
8172
8173 (defvar gnus-next-page-map
8174   (let ((map (make-sparse-keymap)))
8175     (define-key map gnus-mouse-2 'gnus-button-next-page)
8176     (define-key map "\r" 'gnus-button-next-page)
8177     map))
8178
8179 (defun gnus-insert-prev-page-button ()
8180   (let ((b (point)) e
8181         (inhibit-read-only t))
8182     (gnus-eval-format
8183      gnus-prev-page-line-format nil
8184      `(keymap ,gnus-prev-page-map
8185               gnus-prev t
8186               gnus-callback gnus-article-button-prev-page
8187               article-type annotation))
8188     (setq e (if (bolp)
8189                 ;; Exclude a newline.
8190                 (1- (point))
8191               (point)))
8192     (when gnus-article-button-face
8193       (gnus-overlay-put (gnus-make-overlay b e nil t)
8194                         'face gnus-article-button-face))
8195     (widget-convert-button
8196      'link b e
8197      :action 'gnus-button-prev-page
8198      :button-keymap gnus-prev-page-map)))
8199
8200 (defun gnus-button-next-page (&optional args more-args)
8201   "Go to the next page."
8202   (interactive)
8203   (let ((win (selected-window)))
8204     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8205     (gnus-article-next-page)
8206     (select-window win)))
8207
8208 (defun gnus-button-prev-page (&optional args more-args)
8209   "Go to the prev page."
8210   (interactive)
8211   (let ((win (selected-window)))
8212     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8213     (gnus-article-prev-page)
8214     (select-window win)))
8215
8216 (defun gnus-insert-next-page-button ()
8217   (let ((b (point)) e
8218         (inhibit-read-only t))
8219     (gnus-eval-format gnus-next-page-line-format nil
8220                       `(keymap ,gnus-next-page-map
8221                                gnus-next t
8222                                gnus-callback gnus-article-button-next-page
8223                                article-type annotation))
8224     (setq e (if (bolp)
8225                 ;; Exclude a newline.
8226                 (1- (point))
8227               (point)))
8228     (when gnus-article-button-face
8229       (gnus-overlay-put (gnus-make-overlay b e nil t)
8230                         'face gnus-article-button-face))
8231     (widget-convert-button
8232      'link b e
8233      :action 'gnus-button-next-page
8234      :button-keymap gnus-next-page-map)))
8235
8236 (defun gnus-article-button-next-page (arg)
8237   "Go to the next page."
8238   (interactive "P")
8239   (let ((win (selected-window)))
8240     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8241     (gnus-article-next-page)
8242     (select-window win)))
8243
8244 (defun gnus-article-button-prev-page (arg)
8245   "Go to the prev page."
8246   (interactive "P")
8247   (let ((win (selected-window)))
8248     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8249     (gnus-article-prev-page)
8250     (select-window win)))
8251
8252 (defvar gnus-decode-header-methods
8253   '(mail-decode-encoded-word-region)
8254   "List of methods used to decode headers.
8255
8256 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8257 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8258 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8259 whose names match REGEXP.
8260
8261 For example:
8262 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8263  mail-decode-encoded-word-region
8264  (\"chinese\" . rfc1843-decode-region))
8265 ")
8266
8267 (defvar gnus-decode-header-methods-cache nil)
8268
8269 (defun gnus-multi-decode-header (start end)
8270   "Apply the functions from `gnus-encoded-word-methods' that match."
8271   (unless (and gnus-decode-header-methods-cache
8272                (eq gnus-newsgroup-name
8273                    (car gnus-decode-header-methods-cache)))
8274     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8275     (dolist (x gnus-decode-header-methods)
8276       (if (symbolp x)
8277           (nconc gnus-decode-header-methods-cache (list x))
8278         (if (and gnus-newsgroup-name
8279                  (string-match (car x) gnus-newsgroup-name))
8280             (nconc gnus-decode-header-methods-cache
8281                    (list (cdr x)))))))
8282   (let ((xlist gnus-decode-header-methods-cache))
8283     (pop xlist)
8284     (save-restriction
8285       (narrow-to-region start end)
8286       (while xlist
8287         (funcall (pop xlist) (point-min) (point-max))))))
8288
8289 ;;;
8290 ;;; Treatment top-level handling.
8291 ;;;
8292
8293 (defvar gnus-inhibit-article-treatments nil)
8294
8295 (defun gnus-treat-article (gnus-treat-condition
8296                            &optional part-number total-parts gnus-treat-type)
8297   (let ((gnus-treat-length (- (point-max) (point-min)))
8298         (alist gnus-treatment-function-alist)
8299         (article-goto-body-goes-to-point-min-p t)
8300         (treated-type
8301          (or (not gnus-treat-type)
8302              (catch 'found
8303                (let ((list gnus-article-treat-types))
8304                  (while list
8305                    (when (string-match (pop list) gnus-treat-type)
8306                      (throw 'found t)))))))
8307         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8308         val elem)
8309     (gnus-run-hooks 'gnus-part-display-hook)
8310     (dolist (elem alist)
8311       (setq val
8312             (save-excursion
8313               (when (gnus-buffer-live-p gnus-summary-buffer)
8314                 (set-buffer gnus-summary-buffer))
8315               (symbol-value (car elem))))
8316       (when (and (or (consp val)
8317                      treated-type)
8318                  (or (not gnus-inhibit-article-treatments)
8319                      (eq gnus-treat-condition 'head))
8320                  (gnus-treat-predicate val)
8321                  (or (not (get (car elem) 'highlight))
8322                      highlightp))
8323         (save-restriction
8324           (funcall (cadr elem)))))))
8325
8326 ;; Dynamic variables.
8327 (defvar part-number)
8328 (defvar total-parts)
8329 (defvar gnus-treat-type)
8330 (defvar gnus-treat-condition)
8331 (defvar gnus-treat-length)
8332
8333 (defun gnus-treat-predicate (val)
8334   (cond
8335    ((null val)
8336     nil)
8337    (gnus-treat-condition
8338     (eq gnus-treat-condition val))
8339    ((and (listp val)
8340          (stringp (car val)))
8341     (apply 'gnus-or (mapcar `(lambda (s)
8342                                (string-match s ,(or gnus-newsgroup-name "")))
8343                             val)))
8344    ((listp val)
8345     (let ((pred (pop val)))
8346       (cond
8347        ((eq pred 'or)
8348         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8349        ((eq pred 'and)
8350         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8351        ((eq pred 'not)
8352         (not (gnus-treat-predicate (car val))))
8353        ((eq pred 'typep)
8354         (equal (car val) gnus-treat-type))
8355        (t
8356         (error "%S is not a valid predicate" pred)))))
8357    ((eq val t)
8358     t)
8359    ((eq val 'head)
8360     nil)
8361    ((eq val 'first)
8362     (eq part-number 1))
8363    ((eq val 'last)
8364     (eq part-number total-parts))
8365    ((numberp val)
8366     (< gnus-treat-length val))
8367    (t
8368     (error "%S is not a valid value" val))))
8369
8370 (defun gnus-article-encrypt-body (protocol &optional n)
8371   "Encrypt the article body."
8372   (interactive
8373    (list
8374     (or gnus-article-encrypt-protocol
8375         (gnus-completing-read "Encrypt protocol"
8376                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8377                               t))
8378     current-prefix-arg))
8379   ;; User might hit `K E' instead of `K e', so prompt once.
8380   (when (and gnus-article-encrypt-protocol
8381              gnus-novice-user)
8382     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8383       (error "Encrypt aborted")))
8384   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8385     (unless func
8386       (error "Can't find the encrypt protocol %s" protocol))
8387     (if (member gnus-newsgroup-name '("nndraft:delayed"
8388                                       "nndraft:drafts"
8389                                       "nndraft:queue"))
8390         (error "Can't encrypt the article in group %s"
8391                gnus-newsgroup-name))
8392     (gnus-summary-iterate n
8393       (with-current-buffer gnus-summary-buffer
8394         (let ((mail-parse-charset gnus-newsgroup-charset)
8395               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8396               (summary-buffer gnus-summary-buffer)
8397               references point)
8398           (gnus-set-global-variables)
8399           (when (gnus-group-read-only-p)
8400             (error "The current newsgroup does not support article encrypt"))
8401           (gnus-summary-show-article t)
8402           (setq references
8403               (or (mail-header-references gnus-current-headers) ""))
8404           (set-buffer gnus-article-buffer)
8405           (let* ((inhibit-read-only t)
8406                  (headers
8407                   (mapcar (lambda (field)
8408                             (and (save-restriction
8409                                    (message-narrow-to-head)
8410                                    (goto-char (point-min))
8411                                    (search-forward field nil t))
8412                                  (prog2
8413                                      (message-narrow-to-field)
8414                                      (buffer-string)
8415                                    (delete-region (point-min) (point-max))
8416                                    (widen))))
8417                           '("Content-Type:" "Content-Transfer-Encoding:"
8418                             "Content-Disposition:"))))
8419             (message-narrow-to-head)
8420             (message-remove-header "MIME-Version")
8421             (goto-char (point-max))
8422             (setq point (point))
8423             (insert (apply 'concat headers))
8424             (widen)
8425             (narrow-to-region point (point-max))
8426             (let ((message-options message-options))
8427               (message-options-set 'message-sender user-mail-address)
8428               (message-options-set 'message-recipients user-mail-address)
8429               (message-options-set 'message-sign-encrypt 'not)
8430               (funcall func))
8431             (goto-char (point-min))
8432             (insert "MIME-Version: 1.0\n")
8433             (widen)
8434             (gnus-summary-edit-article-done
8435              references nil summary-buffer t))
8436           (when gnus-keep-backlog
8437             (gnus-backlog-remove-article
8438              (car gnus-article-current) (cdr gnus-article-current)))
8439           (gnus-flush-original-article-buffer)
8440           (when gnus-use-cache
8441             (gnus-cache-update-article
8442              (car gnus-article-current) (cdr gnus-article-current))))))))
8443
8444 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8445   "The following specs can be used:
8446 %t  The security MIME type
8447 %i  Additional info
8448 %d  Details
8449 %D  Details if button is pressed")
8450
8451 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8452   "The following specs can be used:
8453 %t  The security MIME type
8454 %i  Additional info
8455 %d  Details
8456 %D  Details if button is pressed")
8457
8458 (defvar gnus-mime-security-button-line-format-alist
8459   '((?t gnus-tmp-type ?s)
8460     (?i gnus-tmp-info ?s)
8461     (?d gnus-tmp-details ?s)
8462     (?D gnus-tmp-pressed-details ?s)))
8463
8464 (defvar gnus-mime-security-button-commands
8465   '((gnus-article-press-button "\r" "Show Detail")
8466     (undefined "v")
8467     (undefined "t")
8468     (undefined "C")
8469     (gnus-mime-security-save-part "o" "Save...")
8470     (undefined "\C-o")
8471     (undefined "r")
8472     (undefined "d")
8473     (undefined "c")
8474     (undefined "i")
8475     (undefined "E")
8476     (undefined "e")
8477     (undefined "p")
8478     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8479     (undefined ".")))
8480
8481 (defvar gnus-mime-security-button-map
8482   (let ((map (make-sparse-keymap)))
8483     (define-key map gnus-mouse-2 'gnus-article-push-button)
8484     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8485     (dolist (c gnus-mime-security-button-commands)
8486       (define-key map (cadr c) (car c)))
8487     map))
8488
8489 (easy-menu-define
8490   gnus-mime-security-button-menu gnus-mime-security-button-map
8491   "Security button menu."
8492   `("Security Part"
8493     ,@(delq nil
8494             (mapcar (lambda (c)
8495                       (unless (eq (car c) 'undefined)
8496                         (vector (caddr c) (car c) :active t)))
8497                     gnus-mime-security-button-commands))))
8498
8499 (defun gnus-mime-security-button-menu (event prefix)
8500   "Construct a context-sensitive menu of security commands."
8501   (interactive "e\nP")
8502   (save-window-excursion
8503     (let ((pos (event-start event)))
8504       (select-window (posn-window pos))
8505       (goto-char (posn-point pos))
8506       (gnus-article-check-buffer)
8507       (popup-menu gnus-mime-security-button-menu nil prefix))))
8508
8509 (defvar gnus-mime-security-details-buffer nil)
8510
8511 (defvar gnus-mime-security-button-pressed nil)
8512
8513 (defvar gnus-mime-security-show-details-inline t
8514   "If non-nil, show details in the article buffer.")
8515
8516 (defun gnus-mime-security-verify-or-decrypt (handle)
8517   (mm-remove-parts (cdr handle))
8518   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8519         point (inhibit-read-only t))
8520     (if region
8521         (goto-char (car region)))
8522     (setq point (point))
8523     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8524       (let* ((mm-verify-option 'known)
8525              (mm-decrypt-option 'known)
8526              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8527         (unless (eq nparts (cdr handle))
8528           (mm-destroy-parts (cdr handle))
8529           (setcdr handle nparts))))
8530     (gnus-mime-display-security handle)
8531     (when region
8532       (delete-region (point) (cdr region))
8533       (set-marker (car region) nil)
8534       (set-marker (cdr region) nil))
8535     (goto-char point)))
8536
8537 (defun gnus-mime-security-show-details (handle)
8538   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8539     (if (not details)
8540         (gnus-message 5 "No details.")
8541       (if gnus-mime-security-show-details-inline
8542           (let ((gnus-mime-security-button-pressed
8543                  (not (get-text-property (point) 'gnus-mime-details)))
8544                 (gnus-mime-security-button-line-format
8545                  (get-text-property (point) 'gnus-line-format))
8546                 (inhibit-read-only t))
8547             (forward-char -1)
8548             (while (eq (get-text-property (point) 'gnus-line-format)
8549                        gnus-mime-security-button-line-format)
8550               (forward-char -1))
8551             (forward-char)
8552             (save-restriction
8553               (narrow-to-region (point) (point))
8554               (gnus-insert-mime-security-button handle))
8555             (delete-region (point)
8556                            (or (text-property-not-all
8557                                 (point) (point-max)
8558                                 'gnus-line-format
8559                                 gnus-mime-security-button-line-format)
8560                                (point-max))))
8561         ;; Not inlined.
8562         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8563             (with-current-buffer gnus-mime-security-details-buffer
8564               (erase-buffer)
8565               t)
8566           (setq gnus-mime-security-details-buffer
8567                 (gnus-get-buffer-create "*MIME Security Details*")))
8568         (with-current-buffer gnus-mime-security-details-buffer
8569           (insert details)
8570           (goto-char (point-min)))
8571         (pop-to-buffer gnus-mime-security-details-buffer)))))
8572
8573 (defun gnus-mime-security-press-button (handle)
8574   (save-excursion
8575     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8576         (gnus-mime-security-show-details handle)
8577       (gnus-mime-security-verify-or-decrypt handle))))
8578
8579 (defun gnus-insert-mime-security-button (handle &optional displayed)
8580   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8581          (gnus-tmp-type
8582           (concat
8583            (or (nth 2 (assoc protocol mm-verify-function-alist))
8584                (nth 2 (assoc protocol mm-decrypt-function-alist))
8585                "Unknown")
8586            (if (equal (car handle) "multipart/signed")
8587                " Signed" " Encrypted")
8588            " Part"))
8589          (gnus-tmp-info
8590           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8591               "Undecided"))
8592          (gnus-tmp-details
8593           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8594          gnus-tmp-pressed-details
8595          b e)
8596     (setq gnus-tmp-details
8597           (if gnus-tmp-details
8598               (concat "\n" gnus-tmp-details)
8599             ""))
8600     (setq gnus-tmp-pressed-details
8601           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8602     (unless (bolp)
8603       (insert "\n"))
8604     (setq b (point))
8605     (gnus-eval-format
8606      gnus-mime-security-button-line-format
8607      gnus-mime-security-button-line-format-alist
8608      `(keymap ,gnus-mime-security-button-map
8609          gnus-callback gnus-mime-security-press-button
8610          gnus-line-format ,gnus-mime-security-button-line-format
8611          gnus-mime-details ,gnus-mime-security-button-pressed
8612          article-type annotation
8613          gnus-data ,handle))
8614     (setq e (if (bolp)
8615                 ;; Exclude a newline.
8616                 (1- (point))
8617               (point)))
8618     (when gnus-article-button-face
8619       (gnus-overlay-put (gnus-make-overlay b e nil t)
8620                         'face gnus-article-button-face))
8621     (widget-convert-button
8622      'link b e
8623      :mime-handle handle
8624      :action 'gnus-widget-press-button
8625      :button-keymap gnus-mime-security-button-map
8626      :help-echo
8627      (lambda (widget)
8628        ;; Needed to properly clear the message due to a bug in
8629        ;; wid-edit (XEmacs only).
8630        (when (boundp 'help-echo-owns-message)
8631          (setq help-echo-owns-message t))
8632        (format
8633         "%S: show detail; %S: more options"
8634         (aref gnus-mouse-2 0)
8635         (aref gnus-down-mouse-3 0))))))
8636
8637 (defun gnus-mime-display-security (handle)
8638   (save-restriction
8639     (narrow-to-region (point) (point))
8640     (unless (gnus-unbuttonized-mime-type-p (car handle))
8641       (gnus-insert-mime-security-button handle))
8642     (gnus-mime-display-part (cadr handle))
8643     (unless (bolp)
8644       (insert "\n"))
8645     (unless (gnus-unbuttonized-mime-type-p (car handle))
8646       (let ((gnus-mime-security-button-line-format
8647              gnus-mime-security-button-end-line-format))
8648         (gnus-insert-mime-security-button handle)))
8649     (mm-set-handle-multipart-parameter
8650      handle 'gnus-region
8651      (cons (set-marker (make-marker) (point-min))
8652            (set-marker (make-marker) (point-max))))
8653     (goto-char (point-max))))
8654
8655 (defun gnus-mime-security-run-function (function)
8656   "Run FUNCTION with the security part under point."
8657   (gnus-article-check-buffer)
8658   (let ((data (get-text-property (point) 'gnus-data))
8659         buffer handle)
8660     (when (and (stringp (car-safe data))
8661                (setq buffer (mm-handle-multipart-original-buffer data))
8662                (setq handle (cadr data)))
8663       (if (bufferp (mm-handle-buffer handle))
8664           (progn
8665             (setq handle (cons buffer (copy-sequence (cdr handle))))
8666             (mm-handle-set-undisplayer handle nil))
8667         (setq handle (mm-make-handle
8668                       buffer
8669                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8670                       nil nil nil nil nil nil)))
8671       (funcall function handle))))
8672
8673 (defun gnus-mime-security-save-part ()
8674   "Save the security part under point."
8675   (interactive)
8676   (gnus-mime-security-run-function 'mm-save-part))
8677
8678 (defun gnus-mime-security-pipe-part ()
8679   "Pipe the security part under point to a process."
8680   (interactive)
8681   (gnus-mime-security-run-function 'mm-pipe-part))
8682
8683 (gnus-ems-redefine)
8684
8685 (provide 'gnus-art)
8686
8687 (run-hooks 'gnus-art-load-hook)
8688
8689 ;;; gnus-art.el ends here