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