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