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