gnus-art: remove old FIXME
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (defvar tool-bar-map)
33 (defvar w3m-minor-mode-map)
34
35 (require 'gnus)
36 (require 'gnus-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'gnus-win)
40 (require 'mm-bodies)
41 (require 'mail-parse)
42 (require 'mm-decode)
43 (require 'mm-view)
44 (require 'wid-edit)
45 (require 'mm-uu)
46 (require 'message)
47
48 (autoload 'gnus-msg-mail "gnus-msg" nil t)
49 (autoload 'gnus-button-mailto "gnus-msg")
50 (autoload 'gnus-button-reply "gnus-msg" nil t)
51 (autoload 'parse-time-string "parse-time" nil nil)
52 (autoload 'ansi-color-apply-on-region "ansi-color")
53 (autoload 'mm-url-insert-file-contents-external "mm-url")
54 (autoload 'mm-extern-cache-contents "mm-extern")
55
56 (defgroup gnus-article nil
57   "Article display."
58   :link '(custom-manual "(gnus)Article Buffer")
59   :group 'gnus)
60
61 (defgroup gnus-article-treat nil
62   "Treating article parts."
63   :link '(custom-manual "(gnus)Article Hiding")
64   :group 'gnus-article)
65
66 (defgroup gnus-article-hiding nil
67   "Hiding article parts."
68   :link '(custom-manual "(gnus)Article Hiding")
69   :group 'gnus-article)
70
71 (defgroup gnus-article-highlight nil
72   "Article highlighting."
73   :link '(custom-manual "(gnus)Article Highlighting")
74   :group 'gnus-article
75   :group 'gnus-visual)
76
77 (defgroup gnus-article-signature nil
78   "Article signatures."
79   :link '(custom-manual "(gnus)Article Signature")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-headers nil
83   "Article headers."
84   :link '(custom-manual "(gnus)Hiding Headers")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-washing nil
88   "Special commands on articles."
89   :link '(custom-manual "(gnus)Article Washing")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-emphasis nil
93   "Fontisizing articles."
94   :link '(custom-manual "(gnus)Article Fontisizing")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-saving nil
98   "Saving articles."
99   :link '(custom-manual "(gnus)Saving Articles")
100   :group 'gnus-article)
101
102 (defgroup gnus-article-mime nil
103   "Worshiping the MIME wonder."
104   :link '(custom-manual "(gnus)Using MIME")
105   :group 'gnus-article)
106
107 (defgroup gnus-article-buttons nil
108   "Pushable buttons in the article buffer."
109   :link '(custom-manual "(gnus)Article Buttons")
110   :group 'gnus-article)
111
112 (defgroup gnus-article-various nil
113   "Other article options."
114   :link '(custom-manual "(gnus)Misc Article")
115   :group 'gnus-article)
116
117 (defcustom gnus-ignored-headers
118   (mapcar
119    (lambda (header)
120      (concat "^" header ":"))
121    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
122      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
123      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
124      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
125      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
126      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
127      "X-Attribution" "X-Originating-IP" "Delivered-To"
128      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
129      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
130      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
131      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
132      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
133      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
134      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
135      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
136      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
137      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
138      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
139      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
140      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
141      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
142      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
143      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
144      "List-[A-Za-z]+" "X-Listprocessor-Version"
145      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
146      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
147      "X-Received" "Content-length" "X-precedence"
148      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
149      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
150      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
151      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
152      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
153      "X-Content-length" "X-Posting-Agent" "Original-Received"
154      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
155      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
156      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
157      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
158      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
159      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
160      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
161      "Envelope-Sender" "Envelope-Recipients"))
162   "*All headers that start with this regexp will be hidden.
163 This variable can also be a list of regexps of headers to be ignored.
164 If `gnus-visible-headers' is non-nil, this variable will be ignored."
165   :type '(choice :custom-show nil
166                  regexp
167                  (repeat regexp))
168   :group 'gnus-article-hiding)
169
170 (defcustom gnus-visible-headers
171   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:"
172   "*All headers that do not match this regexp will be hidden.
173 This variable can also be a list of regexp of headers to remain visible.
174 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
175   :type '(choice
176           (repeat :value-to-internal (lambda (widget value)
177                                        (custom-split-regexp-maybe value))
178                   :match (lambda (widget value)
179                            (or (stringp value)
180                                (widget-editable-list-match widget value)))
181                   regexp)
182           (const :tag "Use gnus-ignored-headers" nil)
183           regexp)
184   :group 'gnus-article-hiding)
185
186 (defcustom gnus-sorted-header-list
187   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
188     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
189   "*This variable is a list of regular expressions.
190 If it is non-nil, headers that match the regular expressions will
191 be placed first in the article buffer in the sequence specified by
192 this list."
193   :type '(repeat regexp)
194   :group 'gnus-article-hiding)
195
196 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
197   "Headers that are only to be displayed if they have interesting data.
198 Possible values in this list are:
199
200   'empty       Headers with no content.
201   'newsgroups  Newsgroup identical to Gnus group.
202   'to-address  To identical to To-address.
203   'to-list     To identical to To-list.
204   'cc-list     CC identical to To-list.
205   'followup-to Followup-to identical to Newsgroups.
206   'reply-to    Reply-to identical to From.
207   'date        Date less than four days old.
208   'long-to     To and/or Cc longer than 1024 characters.
209   'many-to     Multiple To and/or Cc."
210   :type '(set (const :tag "Headers with no content." empty)
211               (const :tag "Newsgroups identical to Gnus group." newsgroups)
212               (const :tag "To identical to To-address." to-address)
213               (const :tag "To identical to To-list." to-list)
214               (const :tag "CC identical to To-list." cc-list)
215               (const :tag "Followup-to identical to Newsgroups." followup-to)
216               (const :tag "Reply-to identical to From." reply-to)
217               (const :tag "Date less than four days old." date)
218               (const :tag "To and/or Cc longer than 1024 characters." long-to)
219               (const :tag "Multiple To and/or Cc headers." many-to))
220   :group 'gnus-article-hiding)
221
222 (defcustom gnus-article-skip-boring nil
223   "Skip over text that is not worth reading.
224 By default, if you set this t, then Gnus will display citations and
225 signatures, but will never scroll down to show you a page consisting
226 only of boring text.  Boring text is controlled by
227 `gnus-article-boring-faces'."
228   :version "22.1"
229   :type 'boolean
230   :group 'gnus-article-hiding)
231
232 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
233   "Regexp matching signature separator.
234 This can also be a list of regexps.  In that case, it will be checked
235 from head to tail looking for a separator.  Searches will be done from
236 the end of the buffer."
237   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
238                  (regexp)
239                  (repeat :tag "List of regexp" regexp))
240   :group 'gnus-article-signature)
241
242 (defcustom gnus-signature-limit nil
243   "Provide a limit to what is considered a signature.
244 If it is a number, no signature may not be longer (in characters) than
245 that number.  If it is a floating point number, no signature may be
246 longer (in lines) than that number.  If it is a function, the function
247 will be called without any parameters, and if it returns nil, there is
248 no signature in the buffer.  If it is a string, it will be used as a
249 regexp.  If it matches, the text in question is not a signature.
250
251 This can also be a list of the above values."
252   :type '(choice (const nil)
253                  (integer :value 200)
254                  (number :value 4.0)
255                  function
256                  (regexp :value ".*"))
257   :group 'gnus-article-signature)
258
259 (defcustom gnus-hidden-properties '(invisible t intangible t)
260   "Property list to use for hiding text."
261   :type 'sexp
262   :group 'gnus-article-hiding)
263
264 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
265 ;; frames in a session.
266 (defcustom gnus-article-x-face-command
267   (if (featurep 'xemacs)
268       (if (or (gnus-image-type-available-p 'xface)
269               (gnus-image-type-available-p 'pbm))
270           'gnus-display-x-face-in-from
271         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
272     (if (gnus-image-type-available-p 'pbm)
273         'gnus-display-x-face-in-from
274       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
275 display -"))
276   "*String or function to be executed to display an X-Face header.
277 If it is a string, the command will be executed in a sub-shell
278 asynchronously.  The compressed face will be piped to this command."
279   :type `(choice string
280                  (function-item gnus-display-x-face-in-from)
281                  function)
282   :version "21.1"
283   :group 'gnus-picon
284   :group 'gnus-article-washing)
285
286 (defcustom gnus-article-x-face-too-ugly nil
287   "Regexp matching posters whose face shouldn't be shown automatically."
288   :type '(choice regexp (const nil))
289   :group 'gnus-article-washing)
290
291 (defcustom gnus-article-banner-alist nil
292   "Banner alist for stripping.
293 For example,
294      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
295   :version "21.1"
296   :type '(repeat (cons symbol regexp))
297   :group 'gnus-article-washing)
298
299 (gnus-define-group-parameter
300  banner
301  :variable-document
302  "Alist of regexps (to match group names) and banner."
303  :variable-group gnus-article-washing
304  :parameter-type
305  '(choice :tag "Banner"
306           :value nil
307           (const :tag "Remove signature" signature)
308           (symbol :tag "Item in `gnus-article-banner-alist'" none)
309           regexp
310           (const :tag "None" nil))
311  :parameter-document
312  "If non-nil, specify how to remove `banners' from articles.
313
314 Symbol `signature' means to remove signatures delimited by
315 `gnus-signature-separator'.  Any other symbol is used to look up a
316 regular expression to match the banner in `gnus-article-banner-alist'.
317 A string is used as a regular expression to match the banner
318 directly.")
319
320 (defcustom gnus-article-address-banner-alist nil
321   "Alist of mail addresses and banners.
322 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
323 to match a mail address in the From: header, BANNER is one of a symbol
324 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
325 If ADDRESS matches author's mail address, it will remove things like
326 advertisements.  For example:
327
328 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
329 "
330   :type '(repeat
331           (cons
332            (regexp :tag "Address")
333            (choice :tag "Banner" :value nil
334                    (const :tag "Remove signature" signature)
335                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
336                    regexp
337                    (const :tag "None" nil))))
338   :version "22.1"
339   :group 'gnus-article-washing)
340
341 (defmacro gnus-emphasis-custom-with-format (&rest body)
342   `(let ((format "\
343 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
344 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
345      ,@body))
346
347 (defun gnus-emphasis-custom-value-to-external (value)
348   (gnus-emphasis-custom-with-format
349    (if (consp (car value))
350        (list (format format (car (car value)) (cdr (car value)))
351              2
352              (if (nth 1 value) 2 3)
353              (nth 2 value))
354      value)))
355
356 (defun gnus-emphasis-custom-value-to-internal (value)
357   (gnus-emphasis-custom-with-format
358    (let ((regexp (concat "\\`"
359                          (format (regexp-quote format)
360                                  "\\([^()]+\\)" "\\([^()]+\\)")
361                          "\\'"))
362          pattern)
363      (if (string-match regexp (setq pattern (car value)))
364          (list (cons (match-string 1 pattern) (match-string 2 pattern))
365                (= (nth 2 value) 2)
366                (nth 3 value))
367        value))))
368
369 (defcustom gnus-emphasis-alist
370   (let ((types
371          '(("\\*" "\\*" bold nil 2)
372            ("_" "_" underline)
373            ("/" "/" italic)
374            ("_/" "/_" underline-italic)
375            ("_\\*" "\\*_" underline-bold)
376            ("\\*/" "/\\*" bold-italic)
377            ("_\\*/" "/\\*_" underline-bold-italic))))
378     (nconc
379      (gnus-emphasis-custom-with-format
380       (mapcar (lambda (spec)
381                 (list (format format (car spec) (cadr spec))
382                       (or (nth 3 spec) 2)
383                       (or (nth 4 spec) 3)
384                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
385               types))
386      '(;; I've never seen anyone use this strikethru convention whereas I've
387        ;; several times seen it triggered by normal text.  --Stef
388        ;; Miles suggests that this form is sometimes used but for italics,
389        ;; so maybe we should map it to `italic'.
390        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
391        ;; 2 3 gnus-emphasis-strikethru)
392        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
393         2 3 gnus-emphasis-underline))))
394   "*Alist that says how to fontify certain phrases.
395 Each item looks like this:
396
397   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
398
399 The first element is a regular expression to be matched.  The second
400 is a number that says what regular expression grouping used to find
401 the entire emphasized word.  The third is a number that says what
402 regexp grouping should be displayed and highlighted.  The fourth
403 is the face used for highlighting."
404   :type
405   '(repeat
406     (menu-choice
407      :format "%[Customizing Style%]\n%v"
408      :indent 2
409      (group :tag "Default"
410             :value ("" 0 0 default)
411             :value-create
412             (lambda (widget)
413               (let ((value (widget-get
414                             (cadr (widget-get (widget-get widget :parent)
415                                               :args))
416                             :value)))
417                 (if (not (eq (nth 2 value) 'default))
418                     (widget-put
419                      widget
420                      :value
421                      (gnus-emphasis-custom-value-to-external value))))
422               (widget-group-value-create widget))
423             regexp
424             (integer :format "Match group: %v")
425             (integer :format "Emphasize group: %v")
426             face)
427      (group :tag "Simple"
428             :value (("_" . "_") nil default)
429             (cons :format "%v"
430                   (regexp :format "Start regexp: %v")
431                   (regexp :format "End regexp: %v"))
432             (boolean :format "Show start and end patterns: %[%v%]\n"
433                      :on " On " :off " Off ")
434             face)))
435   :get (lambda (symbol)
436          (mapcar 'gnus-emphasis-custom-value-to-internal
437                  (default-value symbol)))
438   :set (lambda (symbol value)
439          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
440                                      value)))
441   :group 'gnus-article-emphasis)
442
443 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
444   "A regexp to describe whitespace which should not be emphasized.
445 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
446 The former avoids underlining of leading and trailing whitespace,
447 and the latter avoids underlining any whitespace at all."
448   :version "21.1"
449   :group 'gnus-article-emphasis
450   :type 'regexp)
451
452 (defface gnus-emphasis-bold '((t (:bold t)))
453   "Face used for displaying strong emphasized text (*word*)."
454   :group 'gnus-article-emphasis)
455
456 (defface gnus-emphasis-italic '((t (:italic t)))
457   "Face used for displaying italic emphasized text (/word/)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-underline '((t (:underline t)))
461   "Face used for displaying underlined emphasized text (_word_)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
465   "Face used for displaying underlined bold emphasized text (_*word*_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
469   "Face used for displaying underlined italic emphasized text (_/word/_)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
473   "Face used for displaying bold italic emphasized text (/*word*/)."
474   :group 'gnus-article-emphasis)
475
476 (defface gnus-emphasis-underline-bold-italic
477   '((t (:bold t :italic t :underline t)))
478   "Face used for displaying underlined bold italic emphasized text.
479 Example: (_/*word*/_)."
480   :group 'gnus-article-emphasis)
481
482 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
483                                       '((t (:strikethru t)))
484                                     '((t (:strike-through t))))
485   "Face used for displaying strike-through text (-word-)."
486   :group 'gnus-article-emphasis)
487
488 (defface gnus-emphasis-highlight-words
489   '((t (:background "black" :foreground "yellow")))
490   "Face used for displaying highlighted words."
491   :group 'gnus-article-emphasis)
492
493 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
494   "Format for display of Date headers in article bodies.
495 See `format-time-string' for the possible values.
496
497 The variable can also be function, which should return a complete Date
498 header.  The function is called with one argument, the time, which can
499 be fed to `format-time-string'."
500   :type '(choice string function)
501   :link '(custom-manual "(gnus)Article Date")
502   :group 'gnus-article-washing)
503
504 (defcustom gnus-save-all-headers t
505   "*If non-nil, don't remove any headers before saving.
506 This will be overridden by the `:headers' property that the symbol of
507 the saver function, which is specified by `gnus-default-article-saver',
508 might have."
509   :group 'gnus-article-saving
510   :type 'boolean)
511
512 (defcustom gnus-prompt-before-saving 'always
513   "*This variable says how much prompting is to be done when saving articles.
514 If it is nil, no prompting will be done, and the articles will be
515 saved to the default files.  If this variable is `always', each and
516 every article that is saved will be preceded by a prompt, even when
517 saving large batches of articles.  If this variable is neither nil not
518 `always', there the user will be prompted once for a file name for
519 each invocation of the saving commands."
520   :group 'gnus-article-saving
521   :type '(choice (item always)
522                  (item :tag "never" nil)
523                  (sexp :tag "once" :format "%t\n" :value t)))
524
525 (defcustom gnus-saved-headers gnus-visible-headers
526   "Headers to keep if `gnus-save-all-headers' is nil.
527 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
528 If that variable is nil, however, all headers that match this regexp
529 will be kept while the rest will be deleted before saving.  This and
530 `gnus-save-all-headers' will be overridden by the `:headers' property
531 that the symbol of the saver function, which is specified by
532 `gnus-default-article-saver', might have."
533   :group 'gnus-article-saving
534   :type 'regexp)
535
536 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
537 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
538   "A function to save articles in your favourite format.
539 The function will be called by way of the `gnus-summary-save-article'
540 command, and friends such as `gnus-summary-save-article-rmail'.
541
542 Gnus provides the following functions:
543
544 * gnus-summary-save-in-rmail (Rmail format)
545 * gnus-summary-save-in-mail (Unix mail format)
546 * gnus-summary-save-in-folder (MH folder)
547 * gnus-summary-save-in-file (article format)
548 * gnus-summary-save-body-in-file (article body)
549 * gnus-summary-save-in-vm (use VM's folder format)
550 * gnus-summary-write-to-file (article format -- overwrite)
551 * gnus-summary-write-body-to-file (article body -- overwrite)
552 * gnus-summary-save-in-pipe (article format)
553
554 The symbol of each function may have the following properties:
555
556 * :decode
557 The value non-nil means save decoded articles.  This is meaningful
558 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
559 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
560 `gnus-summary-save-in-pipe'.
561
562 * :function
563 The value specifies an alternative function which appends, not
564 overwrites, articles to a file.  This implies that when saving many
565 articles at a time, `gnus-prompt-before-saving' is bound to t and all
566 articles are saved in a single file.  This is meaningful only with
567 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
568
569 * :headers
570 The value specifies the symbol of a variable of which the value
571 specifies headers to be saved.  If it is omitted,
572 `gnus-save-all-headers' and `gnus-saved-headers' control what
573 headers should be saved."
574   :group 'gnus-article-saving
575   :type '(radio (function-item gnus-summary-save-in-rmail)
576                 (function-item gnus-summary-save-in-mail)
577                 (function-item gnus-summary-save-in-folder)
578                 (function-item gnus-summary-save-in-file)
579                 (function-item gnus-summary-save-body-in-file)
580                 (function-item gnus-summary-save-in-vm)
581                 (function-item gnus-summary-write-to-file)
582                 (function-item gnus-summary-write-body-to-file)
583                 (function-item gnus-summary-save-in-pipe)
584                 (function)))
585
586 (defcustom gnus-article-save-coding-system
587   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
588       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
589       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
590       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
591   "Coding system used to save decoded articles to a file.
592
593 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
594 which can safely encode any characters in text.  This is used by the
595 commands including:
596
597 * gnus-summary-save-article-file
598 * gnus-summary-save-article-body-file
599 * gnus-summary-write-article-file
600 * gnus-summary-write-article-body-file
601
602 and the functions to which you may set `gnus-default-article-saver':
603
604 * gnus-summary-save-in-file
605 * gnus-summary-save-body-in-file
606 * gnus-summary-write-to-file
607 * gnus-summary-write-body-to-file
608
609 Those commands and functions save just text displayed in the article
610 buffer to a file if the value of this variable is non-nil.  Note that
611 buttonized MIME parts will be lost in a saved file in that case.
612 Otherwise, raw articles will be saved."
613   :group 'gnus-article-saving
614   :type `(choice
615           :format "%{%t%}:\n %[Value Menu%] %v"
616           (const :tag "Save raw articles" nil)
617           ,@(delq nil
618                   (mapcar
619                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
620                    '((const :tag "UTF-8" utf-8)
621                      (const :tag "iso-2022-7bit" iso-2022-7bit)
622                      (const :tag "Emacs internal" emacs-mule)
623                      (const :tag "escape-quoted" escape-quoted))))
624           (symbol :tag "Coding system")))
625
626 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
627   "A function generating a file name to save articles in Rmail format.
628 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
629   :group 'gnus-article-saving
630   :type 'function)
631
632 (defcustom gnus-mail-save-name 'gnus-plain-save-name
633   "A function generating a file name to save articles in Unix mail format.
634 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
635   :group 'gnus-article-saving
636   :type 'function)
637
638 (defcustom gnus-folder-save-name 'gnus-folder-save-name
639   "A function generating a file name to save articles in MH folder.
640 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
641   :group 'gnus-article-saving
642   :type 'function)
643
644 (defcustom gnus-file-save-name 'gnus-numeric-save-name
645   "A function generating a file name to save articles in article format.
646 The function is called with NEWSGROUP, HEADERS, and optional
647 LAST-FILE."
648   :group 'gnus-article-saving
649   :type 'function)
650
651 (defcustom gnus-split-methods
652   '((gnus-article-archive-name)
653     (gnus-article-nndoc-name))
654   "*Variable used to suggest where articles are to be saved.
655 For instance, if you would like to save articles related to Gnus in
656 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
657 you could set this variable to something like:
658
659  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
660    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
661
662 This variable is an alist where the key is the match and the
663 value is a list of possible files to save in if the match is
664 non-nil.
665
666 If the match is a string, it is used as a regexp match on the
667 article.  If the match is a symbol, that symbol will be funcalled
668 from the buffer of the article to be saved with the newsgroup as the
669 parameter.  If it is a list, it will be evaled in the same buffer.
670
671 If this form or function returns a string, this string will be used as a
672 possible file name; and if it returns a non-nil list, that list will be
673 used as possible file names."
674   :group 'gnus-article-saving
675   :type '(repeat (choice (list :value (fun) function)
676                          (cons :value ("" "") regexp (repeat string))
677                          (sexp :value nil))))
678
679 (defcustom gnus-page-delimiter "^\^L"
680   "*Regexp describing what to use as article page delimiters.
681 The default value is \"^\^L\", which is a form linefeed at the
682 beginning of a line."
683   :type 'regexp
684   :group 'gnus-article-various)
685
686 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
687   "*The format specification for the article mode line.
688 See `gnus-summary-mode-line-format' for a closer description.
689
690 The following additional specs are available:
691
692 %w  The article washing status.
693 %m  The number of MIME parts in the article."
694   :type 'string
695   :group 'gnus-article-various)
696
697 (defcustom gnus-article-mode-hook nil
698   "*A hook for Gnus article mode."
699   :type 'hook
700   :group 'gnus-article-various)
701
702 (when (featurep 'xemacs)
703   ;; Extracted from gnus-xmas-define in order to preserve user settings
704   (when (fboundp 'turn-off-scroll-in-place)
705     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
706   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
707   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
708
709 (defcustom gnus-article-menu-hook nil
710   "*Hook run after the creation of the article mode menu."
711   :type 'hook
712   :group 'gnus-article-various)
713
714 (defcustom gnus-article-prepare-hook nil
715   "*A hook called after an article has been prepared in the article buffer."
716   :type 'hook
717   :group 'gnus-article-various)
718
719 (defcustom gnus-copy-article-ignored-headers nil
720   "List of headers to be removed when copying an article.
721 Each element is a regular expression."
722   :version "23.1" ;; No Gnus
723   :type '(repeat regexp)
724   :group 'gnus-article-various)
725
726 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
727                         "Gnus 5.10 (Emacs 22.1)")
728
729 (defface gnus-button
730   '((t (:weight bold)))
731   "Face used for highlighting a button in the article buffer."
732   :group 'gnus-article-buttons)
733
734 (defcustom gnus-article-button-face 'gnus-button
735   "Face used for highlighting buttons in the article buffer.
736
737 An article button is a piece of text that you can activate by pressing
738 `RET' or `mouse-2' above it."
739   :type 'face
740   :group 'gnus-article-buttons)
741
742 (defcustom gnus-article-mouse-face 'highlight
743   "Face used for mouse highlighting in the article buffer.
744
745 Article buttons will be displayed in this face when the cursor is
746 above them."
747   :type 'face
748   :group 'gnus-article-buttons)
749
750 (defcustom gnus-signature-face 'gnus-signature
751   "Face used for highlighting a signature in the article buffer.
752 Obsolete; use the face `gnus-signature' for customizations instead."
753   :type 'face
754   :group 'gnus-article-highlight
755   :group 'gnus-article-signature)
756
757 (defface gnus-signature
758   '((t
759      (:italic t)))
760   "Face used for highlighting a signature in the article buffer."
761   :group 'gnus-article-highlight
762   :group 'gnus-article-signature)
763 ;; backward-compatibility alias
764 (put 'gnus-signature-face 'face-alias 'gnus-signature)
765 (put 'gnus-signature-face 'obsolete-face "22.1")
766
767 (defface gnus-header-from
768   '((((class color)
769       (background dark))
770      (:foreground "PaleGreen1"))
771     (((class color)
772       (background light))
773      (:foreground "red3"))
774     (t
775      (:italic t)))
776   "Face used for displaying from headers."
777   :group 'gnus-article-headers
778   :group 'gnus-article-highlight)
779 ;; backward-compatibility alias
780 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
781 (put 'gnus-header-from-face 'obsolete-face "22.1")
782
783 (defface gnus-header-subject
784   '((((class color)
785       (background dark))
786      (:foreground "SeaGreen1"))
787     (((class color)
788       (background light))
789      (:foreground "red4"))
790     (t
791      (:bold t :italic t)))
792   "Face used for displaying subject headers."
793   :group 'gnus-article-headers
794   :group 'gnus-article-highlight)
795 ;; backward-compatibility alias
796 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
797 (put 'gnus-header-subject-face 'obsolete-face "22.1")
798
799 (defface gnus-header-newsgroups
800   '((((class color)
801       (background dark))
802      (:foreground "yellow" :italic t))
803     (((class color)
804       (background light))
805      (:foreground "MidnightBlue" :italic t))
806     (t
807      (:italic t)))
808   "Face used for displaying newsgroups headers.
809 In the default setup this face is only used for crossposted
810 articles."
811   :group 'gnus-article-headers
812   :group 'gnus-article-highlight)
813 ;; backward-compatibility alias
814 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
815 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
816
817 (defface gnus-header-name
818   '((((class color)
819       (background dark))
820      (:foreground "SpringGreen2"))
821     (((class color)
822       (background light))
823      (:foreground "maroon"))
824     (t
825      (:bold t)))
826   "Face used for displaying header names."
827   :group 'gnus-article-headers
828   :group 'gnus-article-highlight)
829 ;; backward-compatibility alias
830 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
831 (put 'gnus-header-name-face 'obsolete-face "22.1")
832
833 (defface gnus-header-content
834   '((((class color)
835       (background dark))
836      (:foreground "SpringGreen1" :italic t))
837     (((class color)
838       (background light))
839      (:foreground "indianred4" :italic t))
840     (t
841      (:italic t)))  "Face used for displaying header content."
842   :group 'gnus-article-headers
843   :group 'gnus-article-highlight)
844 ;; backward-compatibility alias
845 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
846 (put 'gnus-header-content-face 'obsolete-face "22.1")
847
848 (defcustom gnus-header-face-alist
849   '(("From" nil gnus-header-from)
850     ("Subject" nil gnus-header-subject)
851     ("Newsgroups:.*," nil gnus-header-newsgroups)
852     ("" gnus-header-name gnus-header-content))
853   "*Controls highlighting of article headers.
854
855 An alist of the form (HEADER NAME CONTENT).
856
857 HEADER is a regular expression which should match the name of a
858 header and NAME and CONTENT are either face names or nil.
859
860 The name of each header field will be displayed using the face
861 specified by the first element in the list where HEADER matches
862 the header name and NAME is non-nil.  Similarly, the content will
863 be displayed by the first non-nil matching CONTENT face."
864   :group 'gnus-article-headers
865   :group 'gnus-article-highlight
866   :type '(repeat (list (regexp :tag "Header")
867                        (choice :tag "Name"
868                                (item :tag "skip" nil)
869                                (face :value default))
870                        (choice :tag "Content"
871                                (item :tag "skip" nil)
872                                (face :value default)))))
873
874 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
875                                           '((xface . (:face gnus-x-face)))
876                                         '((pbm . (:face gnus-x-face))
877                                           (png . nil)))
878   "Alist of image types and properties applied to Face and X-Face images.
879 Here are examples:
880
881 ;; Specify the altitude of Face images in the From header.
882 \(setq gnus-face-properties-alist
883       '((pbm . (:face gnus-x-face :ascent 80))
884         (png . (:ascent 80))))
885
886 ;; Show Face images as pressed buttons.
887 \(setq gnus-face-properties-alist
888       '((pbm . (:face gnus-x-face :relief -2))
889         (png . (:relief -2))))
890
891 See the manual for the valid properties for various image types.
892 Currently, `pbm' is used for X-Face images and `png' is used for Face
893 images in Emacs.  Only the `:face' property is effective on the `xface'
894 image type in XEmacs if it is built with the libcompface library."
895   :version "23.1" ;; No Gnus
896   :group 'gnus-article-headers
897   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
898
899 (defcustom gnus-article-decode-hook
900   '(article-decode-charset article-decode-encoded-words
901                            article-decode-group-name article-decode-idna-rhs)
902   "*Hook run to decode charsets in articles."
903   :group 'gnus-article-headers
904   :type 'hook)
905
906 (defcustom gnus-display-mime-function 'gnus-display-mime
907   "Function to display MIME articles."
908   :group 'gnus-article-mime
909   :type 'function)
910
911 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
912   "Function used to decode headers.")
913
914 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
915   "Function used to decode addresses.")
916
917 (defvar gnus-article-dumbquotes-map
918   '((?\200 "EUR")
919     (?\202 ",")
920     (?\203 "f")
921     (?\204 ",,")
922     (?\205 "...")
923     (?\213 "<")
924     (?\214 "OE")
925     (?\221 "`")
926     (?\222 "'")
927     (?\223 "``")
928     (?\224 "\"")
929     (?\225 "*")
930     (?\226 "-")
931     (?\227 "--")
932     (?\230 "~")
933     (?\231 "(TM)")
934     (?\233 ">")
935     (?\234 "oe")
936     (?\264 "'"))
937   "Table for MS-to-Latin1 translation.")
938
939 (defcustom gnus-ignored-mime-types nil
940   "List of MIME types that should be ignored by Gnus."
941   :version "21.1"
942   :group 'gnus-article-mime
943   :type '(repeat regexp))
944
945 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
946   "List of MIME types that should not be given buttons when rendered inline.
947 See also `gnus-buttonized-mime-types' which may override this variable.
948 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
949   :version "21.1"
950   :group 'gnus-article-mime
951   :type '(repeat regexp))
952
953 (defcustom gnus-buttonized-mime-types nil
954   "List of MIME types that should be given buttons when rendered inline.
955 If set, this variable overrides `gnus-unbuttonized-mime-types'.
956 To see e.g. security buttons you could set this to
957 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
958 this list to display radio buttons that allow you to choose one of two
959 media types those mails include.  See also `mm-discouraged-alternatives'.
960 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
961   :version "22.1"
962   :group 'gnus-article-mime
963   :type '(repeat regexp))
964
965 (defcustom gnus-inhibit-mime-unbuttonizing nil
966   "If non-nil, all MIME parts get buttons.
967 When nil (the default value), then some MIME parts do not get buttons,
968 as described by the variables `gnus-buttonized-mime-types' and
969 `gnus-unbuttonized-mime-types'."
970   :version "22.1"
971   :group 'gnus-article-mime
972   :type 'boolean)
973
974 (defcustom gnus-body-boundary-delimiter "_"
975   "String used to delimit header and body.
976 This variable is used by `gnus-article-treat-body-boundary' which can
977 be controlled by `gnus-treat-body-boundary'."
978   :version "22.1"
979   :group 'gnus-article-various
980   :type '(choice (item :tag "None" :value nil)
981                  string))
982
983 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
984                                   "/usr/share/picons")
985   "Defines the location of the faces database.
986 For information on obtaining this database of pretty pictures, please
987 see http://www.cs.indiana.edu/picons/ftp/index.html"
988   :version "22.1"
989   :type '(repeat directory)
990   :link '(url-link :tag "download"
991                    "http://www.cs.indiana.edu/picons/ftp/index.html")
992   :link '(custom-manual "(gnus)Picons")
993   :group 'gnus-picon)
994
995 (defun gnus-picons-installed-p ()
996   "Say whether picons are installed on your machine."
997   (let ((installed nil))
998     (dolist (database gnus-picon-databases)
999       (when (file-exists-p database)
1000         (setq installed t)))
1001     installed))
1002
1003 (defcustom gnus-article-mime-part-function nil
1004   "Function called with a MIME handle as the argument.
1005 This is meant for people who want to do something automatic based
1006 on parts -- for instance, adding Vcard info to a database."
1007   :group 'gnus-article-mime
1008   :type '(choice (const nil)
1009                  function))
1010
1011 (defcustom gnus-mime-multipart-functions nil
1012   "An alist of MIME types to functions to display them."
1013   :version "21.1"
1014   :group 'gnus-article-mime
1015   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1016
1017 (defcustom gnus-article-date-headers '(combined-lapsed)
1018   "A list of Date header formats to display.
1019 Valid formats are `ut' (universal time), `local' (local time
1020 zone), `english' (readable English), `lapsed' (elapsed time),
1021 `combined-lapsed' (both the original date and the elapsed time),
1022 `original' (the original date header), `iso8601' (ISO8601
1023 format), and `user-defined' (a user-defined format defined by the
1024 `gnus-article-time-format' variable).
1025
1026 You have as many date headers as you want in the article buffer.
1027 Some of these headers are updated automatically.  See
1028 `gnus-article-update-date-headers' for details."
1029   :version "24.1"
1030   :group 'gnus-article-headers
1031   :type '(repeat
1032           (item :tag "Universal time (UT)" :value 'ut)
1033           (item :tag "Local time zone" :value 'local)
1034           (item :tag "Readable English" :value 'english)
1035           (item :tag "Elapsed time" :value 'lapsed)
1036           (item :tag "Original and elapsed time" :value 'combined-lapsed)
1037           (item :tag "Original date header" :value 'original)
1038           (item :tag "ISO8601 format" :value 'iso8601)
1039           (item :tag "User-defined" :value 'user-defined)))
1040
1041 (defcustom gnus-article-update-date-headers 1
1042   "How often to update the date header.
1043 If nil, don't update it at all."
1044   :version "24.1"
1045   :group 'gnus-article-headers
1046   :type '(choice
1047           (item :tag "Don't update" :value nil)
1048           integer))
1049
1050 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1051   "Function called with a MIME handle as the argument.
1052 This is meant for people who want to view first matched part.
1053 For `undisplayed-alternative' (default), the first undisplayed
1054 part or alternative part is used.  For `undisplayed', the first
1055 undisplayed part is used.  For a function, the first part which
1056 the function return t is used.  For nil, the first part is
1057 used."
1058   :version "21.1"
1059   :group 'gnus-article-mime
1060   :type '(choice
1061           (item :tag "first" :value nil)
1062           (item :tag "undisplayed" :value undisplayed)
1063           (item :tag "undisplayed or alternative"
1064                 :value undisplayed-alternative)
1065           (function)))
1066
1067 (defcustom gnus-mime-action-alist
1068   '(("save to file" . gnus-mime-save-part)
1069     ("save and strip" . gnus-mime-save-part-and-strip)
1070     ("replace with file" . gnus-mime-replace-part)
1071     ("delete part" . gnus-mime-delete-part)
1072     ("display as text" . gnus-mime-inline-part)
1073     ("view the part" . gnus-mime-view-part)
1074     ("pipe to command" . gnus-mime-pipe-part)
1075     ("toggle display" . gnus-article-press-button)
1076     ("toggle display" . gnus-article-view-part-as-charset)
1077     ("view as type" . gnus-mime-view-part-as-type)
1078     ("view internally" . gnus-mime-view-part-internally)
1079     ("view externally" . gnus-mime-view-part-externally))
1080   "An alist of actions that run on the MIME attachment."
1081   :group 'gnus-article-mime
1082   :type '(repeat (cons (string :tag "name")
1083                        (function))))
1084
1085 (defcustom gnus-auto-select-part 1
1086   "Advance to next MIME part when deleting or stripping parts.
1087
1088 When 0, point will be placed on the same part as before.  When
1089 positive (negative), move point forward (backwards) this many
1090 parts.  When nil, redisplay article."
1091   :version "23.1" ;; No Gnus
1092   :group 'gnus-article-mime
1093   :type '(choice (const nil :tag "Redisplay article.")
1094                  (const 1 :tag "Next part.")
1095                  (const 0 :tag "Current part.")
1096                  integer))
1097
1098 ;;;
1099 ;;; The treatment variables
1100 ;;;
1101
1102 (defvar gnus-part-display-hook nil
1103   "Hook called on parts that are to receive treatment.")
1104
1105 (defvar gnus-article-treat-custom
1106   '(choice (const :tag "Off" nil)
1107            (const :tag "On" t)
1108            (const :tag "Header" head)
1109            (const :tag "First" first)
1110            (const :tag "Last" last)
1111            (integer :tag "Less")
1112            (repeat :tag "Groups" regexp)
1113            (sexp :tag "Predicate")))
1114
1115 (defvar gnus-article-treat-head-custom
1116   '(choice (const :tag "Off" nil)
1117            (const :tag "Header" head)))
1118
1119 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1120                                    "text/x-patch")
1121   "Parts to treat.")
1122
1123 (defvar gnus-inhibit-treatment nil
1124   "Whether to inhibit treatment.")
1125
1126 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1127   "Highlight the signature.
1128 Valid values are nil, t, `head', `first', `last', an integer or a
1129 predicate.  See Info node `(gnus)Customizing Articles'."
1130   :group 'gnus-article-treat
1131   :link '(custom-manual "(gnus)Customizing Articles")
1132   :type gnus-article-treat-custom)
1133 (put 'gnus-treat-highlight-signature 'highlight t)
1134
1135 (defcustom gnus-treat-buttonize 100000
1136   "Add buttons.
1137 Valid values are nil, t, `head', `first', `last', an integer or a
1138 predicate.  See Info node `(gnus)Customizing Articles'."
1139   :group 'gnus-article-treat
1140   :link '(custom-manual "(gnus)Customizing Articles")
1141   :type gnus-article-treat-custom)
1142 (put 'gnus-treat-buttonize 'highlight t)
1143
1144 (defcustom gnus-treat-buttonize-head 'head
1145   "Add buttons to the head.
1146 Valid values are nil, t, `head', `first', `last', an integer or a
1147 predicate.  See Info node `(gnus)Customizing Articles'."
1148   :group 'gnus-article-treat
1149   :link '(custom-manual "(gnus)Customizing Articles")
1150   :type gnus-article-treat-head-custom)
1151 (put 'gnus-treat-buttonize-head 'highlight t)
1152
1153 (defcustom gnus-treat-date 'head
1154   "Display dates according to the `gnus-article-date-headers' variable.
1155 Valid values are nil, t, `head', `first', `last', an integer or a
1156 predicate.  See Info node `(gnus)Customizing Articles'."
1157   :version "24.1"
1158   :group 'gnus-article-treat
1159   :link '(custom-manual "(gnus)Customizing Articles")
1160   :type gnus-article-treat-head-custom)
1161
1162 (defcustom gnus-treat-emphasize 50000
1163   "Emphasize text.
1164 Valid values are nil, t, `head', `first', `last', an integer or a
1165 predicate.  See Info node `(gnus)Customizing Articles'."
1166   :group 'gnus-article-treat
1167   :link '(custom-manual "(gnus)Customizing Articles")
1168   :type gnus-article-treat-custom)
1169 (put 'gnus-treat-emphasize 'highlight t)
1170
1171 (defcustom gnus-treat-strip-cr nil
1172   "Remove carriage returns.
1173 Valid values are nil, t, `head', `first', `last', an integer or a
1174 predicate.  See Info node `(gnus)Customizing Articles'."
1175   :version "22.1"
1176   :group 'gnus-article-treat
1177   :link '(custom-manual "(gnus)Customizing Articles")
1178   :type gnus-article-treat-custom)
1179
1180 (defcustom gnus-treat-unsplit-urls nil
1181   "Remove newlines from within URLs.
1182 Valid values are nil, t, `head', `first', `last', an integer or a
1183 predicate.  See Info node `(gnus)Customizing Articles'."
1184   :version "22.1"
1185   :group 'gnus-article-treat
1186   :link '(custom-manual "(gnus)Customizing Articles")
1187   :type gnus-article-treat-custom)
1188
1189 (defcustom gnus-treat-leading-whitespace nil
1190   "Remove leading whitespace in headers.
1191 Valid values are nil, t, `head', `first', `last', an integer or a
1192 predicate.  See Info node `(gnus)Customizing Articles'."
1193   :version "22.1"
1194   :group 'gnus-article-treat
1195   :link '(custom-manual "(gnus)Customizing Articles")
1196   :type gnus-article-treat-custom)
1197
1198 (defcustom gnus-treat-hide-headers 'head
1199   "Hide headers.
1200 Valid values are nil, t, `head', `first', `last', an integer or a
1201 predicate.  See Info node `(gnus)Customizing Articles'."
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-head-custom)
1205
1206 (defcustom gnus-treat-hide-boring-headers nil
1207   "Hide boring headers.
1208 Valid values are nil, t, `head', `first', `last', an integer or a
1209 predicate.  See Info node `(gnus)Customizing Articles'."
1210   :group 'gnus-article-treat
1211   :link '(custom-manual "(gnus)Customizing Articles")
1212   :type gnus-article-treat-head-custom)
1213
1214 (defcustom gnus-treat-hide-signature nil
1215   "Hide the signature.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :group 'gnus-article-treat
1219   :link '(custom-manual "(gnus)Customizing Articles")
1220   :type gnus-article-treat-custom)
1221
1222 (defcustom gnus-treat-fill-article nil
1223   "Fill the article.
1224 Valid values are nil, t, `head', `first', `last', an integer or a
1225 predicate.  See Info node `(gnus)Customizing Articles'."
1226   :group 'gnus-article-treat
1227   :link '(custom-manual "(gnus)Customizing Articles")
1228   :type gnus-article-treat-custom)
1229
1230 (defcustom gnus-treat-hide-citation nil
1231   "Hide cited text.
1232 Valid values are nil, t, `head', `first', `last', an integer or a
1233 predicate.  See Info node `(gnus)Customizing Articles'."
1234   :group 'gnus-article-treat
1235   :link '(custom-manual "(gnus)Customizing Articles")
1236   :type gnus-article-treat-custom)
1237
1238 (defcustom gnus-treat-hide-citation-maybe nil
1239   "Hide cited text.
1240 Valid values are nil, t, `head', `first', `last', an integer or a
1241 predicate.  See Info node `(gnus)Customizing Articles'."
1242   :group 'gnus-article-treat
1243   :link '(custom-manual "(gnus)Customizing Articles")
1244   :type gnus-article-treat-custom)
1245
1246 (defcustom gnus-treat-strip-list-identifiers 'head
1247   "Strip list identifiers from `gnus-list-identifiers`.
1248 Valid values are nil, t, `head', `first', `last', an integer or a
1249 predicate.  See Info node `(gnus)Customizing Articles'."
1250   :version "21.1"
1251   :group 'gnus-article-treat
1252   :link '(custom-manual "(gnus)Customizing Articles")
1253   :type gnus-article-treat-custom)
1254
1255 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1256                         "Gnus 5.10 (Emacs 22.1)")
1257
1258 (defcustom gnus-treat-strip-pem nil
1259   "Strip PEM signatures.
1260 Valid values are nil, t, `head', `first', `last', an integer or a
1261 predicate.  See Info node `(gnus)Customizing Articles'."
1262   :group 'gnus-article-treat
1263   :link '(custom-manual "(gnus)Customizing Articles")
1264   :type gnus-article-treat-custom)
1265
1266 (defcustom gnus-treat-strip-banner t
1267   "Strip banners from articles.
1268 The banner to be stripped is specified in the `banner' group parameter.
1269 Valid values are nil, t, `head', `first', `last', an integer or a
1270 predicate.  See Info node `(gnus)Customizing Articles'."
1271   :group 'gnus-article-treat
1272   :link '(custom-manual "(gnus)Customizing Articles")
1273   :type gnus-article-treat-custom)
1274
1275 (defcustom gnus-treat-highlight-headers 'head
1276   "Highlight the headers.
1277 Valid values are nil, t, `head', `first', `last', an integer or a
1278 predicate.  See Info node `(gnus)Customizing Articles'."
1279   :group 'gnus-article-treat
1280   :link '(custom-manual "(gnus)Customizing Articles")
1281   :type gnus-article-treat-head-custom)
1282 (put 'gnus-treat-highlight-headers 'highlight t)
1283
1284 (defcustom gnus-treat-highlight-citation t
1285   "Highlight cited text.
1286 Valid values are nil, t, `head', `first', `last', an integer or a
1287 predicate.  See Info node `(gnus)Customizing Articles'."
1288   :group 'gnus-article-treat
1289   :link '(custom-manual "(gnus)Customizing Articles")
1290   :type gnus-article-treat-custom)
1291 (put 'gnus-treat-highlight-citation 'highlight t)
1292
1293 (defcustom gnus-treat-strip-headers-in-body t
1294   "Strip the X-No-Archive header line from the beginning of the body.
1295 Valid values are nil, t, `head', `first', `last', an integer or a
1296 predicate.  See Info node `(gnus)Customizing Articles'."
1297   :version "21.1"
1298   :group 'gnus-article-treat
1299   :link '(custom-manual "(gnus)Customizing Articles")
1300   :type gnus-article-treat-custom)
1301
1302 (defcustom gnus-treat-strip-trailing-blank-lines nil
1303   "Strip trailing blank lines.
1304 Valid values are nil, t, `head', `first', `last', an integer or a
1305 predicate.  See Info node `(gnus)Customizing Articles'.
1306
1307 When set to t, it also strips trailing blanks in all MIME parts.
1308 Consider to use `last' instead."
1309   :group 'gnus-article-treat
1310   :link '(custom-manual "(gnus)Customizing Articles")
1311   :type gnus-article-treat-custom)
1312
1313 (defcustom gnus-treat-strip-leading-blank-lines nil
1314   "Strip leading blank lines.
1315 Valid values are nil, t, `head', `first', `last', an integer or a
1316 predicate.  See Info node `(gnus)Customizing Articles'.
1317
1318 When set to t, it also strips trailing blanks in all MIME parts."
1319   :group 'gnus-article-treat
1320   :link '(custom-manual "(gnus)Customizing Articles")
1321   :type gnus-article-treat-custom)
1322
1323 (defcustom gnus-treat-strip-multiple-blank-lines nil
1324   "Strip multiple blank lines.
1325 Valid values are nil, t, `head', `first', `last', an integer or a
1326 predicate.  See Info node `(gnus)Customizing Articles'."
1327   :group 'gnus-article-treat
1328   :link '(custom-manual "(gnus)Customizing Articles")
1329   :type gnus-article-treat-custom)
1330
1331 (defcustom gnus-treat-unfold-headers 'head
1332   "Unfold folded header lines.
1333 Valid values are nil, t, `head', `first', `last', an integer or a
1334 predicate.  See Info node `(gnus)Customizing Articles'."
1335   :version "22.1"
1336   :group 'gnus-article-treat
1337   :link '(custom-manual "(gnus)Customizing Articles")
1338   :type gnus-article-treat-custom)
1339
1340 (defcustom gnus-article-unfold-long-headers nil
1341   "If non-nil, allow unfolding headers even if the header is long.
1342 If it is a regexp, only long headers matching this regexp are unfolded.
1343 If it is t, all long headers are unfolded.
1344
1345 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1346   :version "23.1" ;; No Gnus
1347   :group 'gnus-article-treat
1348   :type '(choice (const nil)
1349                  (const :tag "all" t)
1350                  (regexp)))
1351
1352 (defcustom gnus-treat-fold-headers nil
1353   "Fold headers.
1354 Valid values are nil, t, `head', `first', `last', an integer or a
1355 predicate.  See Info node `(gnus)Customizing Articles'."
1356   :version "22.1"
1357   :group 'gnus-article-treat
1358   :link '(custom-manual "(gnus)Customizing Articles")
1359   :type gnus-article-treat-custom)
1360
1361 (defcustom gnus-treat-fold-newsgroups 'head
1362   "Fold the Newsgroups and Followup-To headers.
1363 Valid values are nil, t, `head', `first', `last', an integer or a
1364 predicate.  See Info node `(gnus)Customizing Articles'."
1365   :version "22.1"
1366   :group 'gnus-article-treat
1367   :link '(custom-manual "(gnus)Customizing Articles")
1368   :type gnus-article-treat-custom)
1369
1370 (defcustom gnus-treat-overstrike t
1371   "Treat overstrike highlighting.
1372 Valid values are nil, t, `head', `first', `last', an integer or a
1373 predicate.  See Info node `(gnus)Customizing Articles'."
1374   :group 'gnus-article-treat
1375   :link '(custom-manual "(gnus)Customizing Articles")
1376   :type gnus-article-treat-custom)
1377 (put 'gnus-treat-overstrike 'highlight t)
1378
1379 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1380   "Treat ANSI SGR control sequences.
1381 Valid values are nil, t, `head', `first', `last', an integer or a
1382 predicate.  See Info node `(gnus)Customizing Articles'."
1383   :group 'gnus-article-treat
1384   :link '(custom-manual "(gnus)Customizing Articles")
1385   :type gnus-article-treat-custom)
1386
1387 (make-obsolete-variable 'gnus-treat-display-xface
1388                         'gnus-treat-display-x-face "Emacs 22.1")
1389
1390 (defcustom gnus-treat-display-x-face
1391   (and (not noninteractive)
1392        (gnus-image-type-available-p 'xbm)
1393        (if (featurep 'xemacs)
1394            (featurep 'xface)
1395          (condition-case nil
1396              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1397                   (executable-find "icontopbm"))
1398            ;; shell-command-to-string may signal an error, e.g. if
1399            ;; shell-file-name is not found.
1400            (error nil)))
1401        'head)
1402   "Display X-Face headers.
1403 Valid values are nil and `head'.
1404 See Info node `(gnus)Customizing Articles' and Info node
1405 `(gnus)X-Face' for details."
1406   :group 'gnus-article-treat
1407   :version "21.1"
1408   :link '(custom-manual "(gnus)Customizing Articles")
1409   :link '(custom-manual "(gnus)X-Face")
1410   :type gnus-article-treat-head-custom
1411   :set (lambda (symbol value)
1412          (set-default
1413           symbol
1414           (cond ((or (boundp symbol) (get symbol 'saved-value))
1415                  value)
1416                 ((boundp 'gnus-treat-display-xface)
1417                  (message "\
1418 ** gnus-treat-display-xface is an obsolete variable;\
1419  use gnus-treat-display-x-face instead")
1420                  (default-value 'gnus-treat-display-xface))
1421                 ((get 'gnus-treat-display-xface 'saved-value)
1422                  (message "\
1423 ** gnus-treat-display-xface is an obsolete variable;\
1424  use gnus-treat-display-x-face instead")
1425                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1426                 (t
1427                  value)))))
1428 (put 'gnus-treat-display-x-face 'highlight t)
1429
1430 (defcustom gnus-treat-display-face
1431   (and (not noninteractive)
1432        (gnus-image-type-available-p 'png)
1433        'head)
1434   "Display Face headers.
1435 Valid values are nil, t, `head', `first', `last', an integer or a
1436 predicate.  See Info node `(gnus)Customizing Articles' and Info
1437 node `(gnus)Face' for details."
1438   :group 'gnus-article-treat
1439   :version "22.1"
1440   :link '(custom-manual "(gnus)Customizing Articles")
1441   :link '(custom-manual "(gnus)X-Face")
1442   :type gnus-article-treat-head-custom)
1443 (put 'gnus-treat-display-face 'highlight t)
1444
1445 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1446   "Display smileys.
1447 Valid values are nil, t, `head', `first', `last', an integer or a
1448 predicate.  See Info node `(gnus)Customizing Articles' and Info
1449 node `(gnus)Smileys' for details."
1450   :group 'gnus-article-treat
1451   :version "21.1"
1452   :link '(custom-manual "(gnus)Customizing Articles")
1453   :link '(custom-manual "(gnus)Smileys")
1454   :type gnus-article-treat-custom)
1455 (put 'gnus-treat-display-smileys 'highlight t)
1456
1457 (defcustom gnus-treat-from-picon
1458   (if (and (gnus-image-type-available-p 'xpm)
1459            (gnus-picons-installed-p))
1460       'head nil)
1461   "Display picons in the From header.
1462 Valid values are nil, t, `head', `first', `last', an integer or a
1463 predicate.  See Info node `(gnus)Customizing Articles' and Info
1464 node `(gnus)Picons' for details."
1465   :version "22.1"
1466   :group 'gnus-article-treat
1467   :group 'gnus-picon
1468   :link '(custom-manual "(gnus)Customizing Articles")
1469   :link '(custom-manual "(gnus)Picons")
1470   :type gnus-article-treat-head-custom)
1471 (put 'gnus-treat-from-picon 'highlight t)
1472
1473 (defcustom gnus-treat-mail-picon
1474   (if (and (gnus-image-type-available-p 'xpm)
1475            (gnus-picons-installed-p))
1476       'head nil)
1477   "Display picons in To and Cc headers.
1478 Valid values are nil, t, `head', `first', `last', an integer or a
1479 predicate.  See Info node `(gnus)Customizing Articles' and Info
1480 node `(gnus)Picons' for details."
1481   :version "22.1"
1482   :group 'gnus-article-treat
1483   :group 'gnus-picon
1484   :link '(custom-manual "(gnus)Customizing Articles")
1485   :link '(custom-manual "(gnus)Picons")
1486   :type gnus-article-treat-head-custom)
1487 (put 'gnus-treat-mail-picon 'highlight t)
1488
1489 (defcustom gnus-treat-newsgroups-picon
1490   (if (and (gnus-image-type-available-p 'xpm)
1491            (gnus-picons-installed-p))
1492       'head nil)
1493   "Display picons in the Newsgroups and Followup-To headers.
1494 Valid values are nil, t, `head', `first', `last', an integer or a
1495 predicate.  See Info node `(gnus)Customizing Articles' and Info
1496 node `(gnus)Picons' for details."
1497   :version "22.1"
1498   :group 'gnus-article-treat
1499   :group 'gnus-picon
1500   :link '(custom-manual "(gnus)Customizing Articles")
1501   :link '(custom-manual "(gnus)Picons")
1502   :type gnus-article-treat-head-custom)
1503 (put 'gnus-treat-newsgroups-picon 'highlight t)
1504
1505 (defcustom gnus-treat-from-gravatar nil
1506   "Display gravatars in the From header.
1507 Valid values are nil, t, `head', `first', `last', an integer or a
1508 predicate.  See Info node `(gnus)Customizing Articles' and Info
1509 node `(gnus)Gravatars' for details."
1510   :version "24.1"
1511   :group 'gnus-article-treat
1512   :group 'gnus-gravatar
1513   :link '(custom-manual "(gnus)Customizing Articles")
1514   :link '(custom-manual "(gnus)Gravatars")
1515   :type gnus-article-treat-head-custom)
1516 (put 'gnus-treat-from-gravatar 'highlight t)
1517
1518 (defcustom gnus-treat-mail-gravatar nil
1519   "Display gravatars in To and Cc headers.
1520 Valid values are nil, t, `head', `first', `last', an integer or a
1521 predicate.  See Info node `(gnus)Customizing Articles' and Info
1522 node `(gnus)Gravatars' for details."
1523   :version "24.1"
1524   :group 'gnus-article-treat
1525   :group 'gnus-gravatar
1526   :link '(custom-manual "(gnus)Customizing Articles")
1527   :link '(custom-manual "(gnus)Gravatars")
1528   :type gnus-article-treat-head-custom)
1529 (put 'gnus-treat-mail-gravatar 'highlight t)
1530
1531 (defcustom gnus-treat-body-boundary
1532   (if (or gnus-treat-newsgroups-picon
1533           gnus-treat-mail-picon
1534           gnus-treat-from-picon
1535           gnus-treat-from-gravatar
1536           gnus-treat-mail-gravatar)
1537       ;; If there's much decoration, the user might prefer a boundery.
1538       'head
1539     nil)
1540   "Draw a boundary at the end of the headers.
1541 Valid values are nil and `head'.
1542 See Info node `(gnus)Customizing Articles' for details."
1543   :version "22.1"
1544   :group 'gnus-article-treat
1545   :link '(custom-manual "(gnus)Customizing Articles")
1546   :type gnus-article-treat-head-custom)
1547
1548 (defcustom gnus-treat-capitalize-sentences nil
1549   "Capitalize sentence-starting words.
1550 Valid values are nil, t, `head', `first', `last', an integer or a
1551 predicate.  See Info node `(gnus)Customizing Articles'."
1552   :version "21.1"
1553   :group 'gnus-article-treat
1554   :link '(custom-manual "(gnus)Customizing Articles")
1555   :type gnus-article-treat-custom)
1556
1557 (defcustom gnus-treat-wash-html nil
1558   "Format as HTML.
1559 Valid values are nil, t, `head', `first', `last', an integer or a
1560 predicate.  See Info node `(gnus)Customizing Articles'."
1561   :version "22.1"
1562   :group 'gnus-article-treat
1563   :link '(custom-manual "(gnus)Customizing Articles")
1564   :type gnus-article-treat-custom)
1565
1566 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1567   "Fill long lines.
1568 Valid values are nil, t, `head', `first', `last', an integer or a
1569 predicate.  See Info node `(gnus)Customizing Articles'."
1570   :version "24.1"
1571   :group 'gnus-article-treat
1572   :link '(custom-manual "(gnus)Customizing Articles")
1573   :type gnus-article-treat-custom)
1574
1575 (defcustom gnus-treat-x-pgp-sig nil
1576   "Verify X-PGP-Sig.
1577 To automatically treat X-PGP-Sig, set it to head.
1578 Valid values are nil, t, `head', `first', `last', an integer or a
1579 predicate.  See Info node `(gnus)Customizing Articles'."
1580   :version "22.1"
1581   :group 'gnus-article-treat
1582   :group 'mime-security
1583   :link '(custom-manual "(gnus)Customizing Articles")
1584   :type gnus-article-treat-custom)
1585
1586 (defvar gnus-article-encrypt-protocol-alist
1587   '(("PGP" . mml2015-self-encrypt)))
1588
1589 ;; Set to nil if more than one protocol added to
1590 ;; gnus-article-encrypt-protocol-alist.
1591 (defcustom gnus-article-encrypt-protocol "PGP"
1592   "The protocol used for encrypt articles.
1593 It is a string, such as \"PGP\". If nil, ask user."
1594   :version "22.1"
1595   :type 'string
1596   :group 'mime-security)
1597
1598 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1599                               (mm-coding-system-p 'utf-8)
1600                               (executable-find idna-program))
1601   "Whether IDNA decoding of headers is used when viewing messages.
1602 This requires GNU Libidn, and by default only enabled if it is found."
1603   :version "22.1"
1604   :group 'gnus-article-headers
1605   :type 'boolean)
1606
1607 (defcustom gnus-article-over-scroll nil
1608   "If non-nil, allow scrolling the article buffer even when there no more text."
1609   :version "22.1"
1610   :group 'gnus-article
1611   :type 'boolean)
1612
1613 (defcustom gnus-inhibit-images nil
1614   "Non-nil means inhibit displaying of images inline in the article body."
1615   :version "24.1"
1616   :group 'gnus-article
1617   :type 'boolean)
1618
1619 (defcustom gnus-blocked-images 'gnus-block-private-groups
1620   "Images that have URLs matching this regexp will be blocked.
1621 This can also be a function to be evaluated.  If so, it will be
1622 called with the group name as the parameter, and should return a
1623 regexp."
1624   :version "24.1"
1625   :group 'gnus-art
1626   :type 'regexp)
1627
1628 ;;; Internal variables
1629
1630 (defvar gnus-english-month-names
1631   '("January" "February" "March" "April" "May" "June" "July" "August"
1632     "September" "October" "November" "December"))
1633
1634 (defvar article-goto-body-goes-to-point-min-p nil)
1635 (defvar gnus-article-wash-types nil)
1636 (defvar gnus-article-emphasis-alist nil)
1637 (defvar gnus-article-image-alist nil)
1638
1639 (defvar gnus-article-mime-handle-alist-1 nil)
1640 (defvar gnus-treatment-function-alist
1641   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1642     (gnus-treat-strip-banner gnus-article-strip-banner)
1643     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1644     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1645     (gnus-treat-buttonize gnus-article-add-buttons)
1646     (gnus-treat-fill-article gnus-article-fill-cited-article)
1647     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1648     (gnus-treat-strip-cr gnus-article-remove-cr)
1649     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1650     (gnus-treat-display-x-face gnus-article-display-x-face)
1651     (gnus-treat-display-face gnus-article-display-face)
1652     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1653     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1654     (gnus-treat-hide-signature gnus-article-hide-signature)
1655     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1656     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1657     (gnus-treat-from-picon gnus-treat-from-picon)
1658     (gnus-treat-mail-picon gnus-treat-mail-picon)
1659     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1660     (gnus-treat-strip-pem gnus-article-hide-pem)
1661     (gnus-treat-date gnus-article-treat-date)
1662     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1663     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1664     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1665     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1666     (gnus-treat-strip-trailing-blank-lines
1667      gnus-article-remove-trailing-blank-lines)
1668     (gnus-treat-strip-leading-blank-lines
1669      gnus-article-strip-leading-blank-lines)
1670     (gnus-treat-strip-multiple-blank-lines
1671      gnus-article-strip-multiple-blank-lines)
1672     (gnus-treat-overstrike gnus-article-treat-overstrike)
1673     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1674     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1675     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1676     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1677     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1678     (gnus-treat-display-smileys gnus-treat-smiley)
1679     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1680     (gnus-treat-wash-html gnus-article-wash-html)
1681     (gnus-treat-emphasize gnus-article-emphasize)
1682     (gnus-treat-hide-citation gnus-article-hide-citation)
1683     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1684     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1685     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1686
1687 (defvar gnus-article-mime-handle-alist nil)
1688 (defvar article-lapsed-timer nil)
1689 (defvar gnus-article-current-summary nil)
1690
1691 (defvar gnus-article-mode-syntax-table
1692   (let ((table (copy-syntax-table text-mode-syntax-table)))
1693     ;; This causes the citation match run O(2^n).
1694     ;; (modify-syntax-entry ?- "w" table)
1695     (modify-syntax-entry ?> ")<" table)
1696     (modify-syntax-entry ?< "(>" table)
1697     ;; make M-. in article buffers work for `foo' strings
1698     (modify-syntax-entry ?' " " table)
1699     (modify-syntax-entry ?` " " table)
1700     table)
1701   "Syntax table used in article mode buffers.
1702 Initialized from `text-mode-syntax-table.")
1703
1704 (defvar gnus-save-article-buffer nil)
1705
1706 (defvar gnus-number-of-articles-to-be-saved nil)
1707
1708 (defvar gnus-inhibit-hiding nil)
1709
1710 (defvar gnus-article-edit-mode nil)
1711
1712 ;;; Macros for dealing with the article buffer.
1713
1714 (defmacro gnus-with-article-headers (&rest forms)
1715   `(with-current-buffer gnus-article-buffer
1716      (save-restriction
1717        (let ((inhibit-read-only t)
1718              (inhibit-point-motion-hooks t)
1719              (case-fold-search t))
1720          (article-narrow-to-head)
1721          ,@forms))))
1722
1723 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1724 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1725
1726 (defmacro gnus-with-article-buffer (&rest forms)
1727   `(with-current-buffer gnus-article-buffer
1728      (let ((inhibit-read-only t))
1729        ,@forms)))
1730
1731 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1732 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1733
1734 (defun gnus-article-goto-header (header)
1735   "Go to HEADER, which is a regular expression."
1736   (re-search-forward (concat "^\\(" header "\\):") nil t))
1737
1738 (defsubst gnus-article-hide-text (b e props)
1739   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1740   (gnus-add-text-properties-when 'article-type nil b e props)
1741   (when (memq 'intangible props)
1742     (put-text-property
1743      (max (1- b) (point-min))
1744      b 'intangible (cddr (memq 'intangible props)))))
1745
1746 (defsubst gnus-article-unhide-text (b e)
1747   "Remove hidden text properties from region between B and E."
1748   (remove-text-properties b e gnus-hidden-properties)
1749   (when (memq 'intangible gnus-hidden-properties)
1750     (put-text-property (max (1- b) (point-min))
1751                        b 'intangible nil)))
1752
1753 (defun gnus-article-hide-text-type (b e type)
1754   "Hide text of TYPE between B and E."
1755   (gnus-add-wash-type type)
1756   (gnus-article-hide-text
1757    b e (cons 'article-type (cons type gnus-hidden-properties))))
1758
1759 (defun gnus-article-unhide-text-type (b e type)
1760   "Unhide text of TYPE between B and E."
1761   (gnus-delete-wash-type type)
1762   (remove-text-properties
1763    b e (cons 'article-type (cons type gnus-hidden-properties)))
1764   (when (memq 'intangible gnus-hidden-properties)
1765     (put-text-property (max (1- b) (point-min))
1766                        b 'intangible nil)))
1767
1768 (defun gnus-article-hide-text-of-type (type)
1769   "Hide text of TYPE in the current buffer."
1770   (save-excursion
1771     (let ((b (point-min))
1772           (e (point-max)))
1773       (while (setq b (text-property-any b e 'article-type type))
1774         (add-text-properties b (incf b) gnus-hidden-properties)))))
1775
1776 (defun gnus-article-delete-text-of-type (type)
1777   "Delete text of TYPE in the current buffer."
1778   (save-excursion
1779     (let ((b (point-min)))
1780       (if (eq type 'multipart)
1781           ;; Remove MIME buttons associated with multipart/alternative parts.
1782           (progn
1783             (goto-char b)
1784             (while (if (get-text-property (point) 'gnus-part)
1785                        (setq b (point))
1786                      (when (setq b (next-single-property-change (point)
1787                                                                 'gnus-part))
1788                        (goto-char b)
1789                        t))
1790               (end-of-line)
1791               (skip-chars-forward "\n")
1792               (when (eq (get-text-property b 'article-type) 'multipart)
1793                 (delete-region b (point)))))
1794         (while (setq b (text-property-any b (point-max) 'article-type type))
1795           (delete-region
1796            b (or (text-property-not-all b (point-max) 'article-type type)
1797                  (point-max))))))))
1798
1799 (defun gnus-article-delete-invisible-text ()
1800   "Delete all invisible text in the current buffer."
1801   (save-excursion
1802     (let ((b (point-min)))
1803       (while (setq b (text-property-any b (point-max) 'invisible t))
1804         (delete-region
1805          b (or (text-property-not-all b (point-max) 'invisible t)
1806                (point-max)))))))
1807
1808 (defun gnus-article-text-type-exists-p (type)
1809   "Say whether any text of type TYPE exists in the buffer."
1810   (text-property-any (point-min) (point-max) 'article-type type))
1811
1812 (defsubst gnus-article-header-rank ()
1813   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1814   (let ((list gnus-sorted-header-list)
1815         (i 1))
1816     (while list
1817       (if (looking-at (car list))
1818           (setq list nil)
1819         (setq list (cdr list))
1820         (incf i)))
1821       i))
1822
1823 (defun article-hide-headers (&optional arg delete)
1824   "Hide unwanted headers and possibly sort them as well."
1825   (interactive)
1826   ;; This function might be inhibited.
1827   (unless gnus-inhibit-hiding
1828     (let ((inhibit-read-only t)
1829           (case-fold-search t)
1830           (max (1+ (length gnus-sorted-header-list)))
1831           (inhibit-point-motion-hooks t)
1832           (cur (current-buffer))
1833           ignored visible beg)
1834       (save-excursion
1835         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1836         ;; group parameters, so we should go to the summary buffer.
1837         (when (prog1
1838                   (condition-case nil
1839                       (progn (set-buffer gnus-summary-buffer) t)
1840                     (error nil))
1841                 (setq ignored (when (not gnus-visible-headers)
1842                                 (cond ((stringp gnus-ignored-headers)
1843                                        gnus-ignored-headers)
1844                                       ((listp gnus-ignored-headers)
1845                                        (mapconcat 'identity
1846                                                   gnus-ignored-headers
1847                                                   "\\|"))))
1848                       visible (cond ((stringp gnus-visible-headers)
1849                                      gnus-visible-headers)
1850                                     ((and gnus-visible-headers
1851                                           (listp gnus-visible-headers))
1852                                      (mapconcat 'identity
1853                                                 gnus-visible-headers
1854                                                 "\\|")))))
1855           (set-buffer cur))
1856         (save-restriction
1857           ;; First we narrow to just the headers.
1858           (article-narrow-to-head)
1859           ;; Hide any "From " lines at the beginning of (mail) articles.
1860           (while (looking-at "From ")
1861             (forward-line 1))
1862           (unless (bobp)
1863             (delete-region (point-min) (point)))
1864           ;; Then treat the rest of the header lines.
1865           ;; Then we use the two regular expressions
1866           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1867           ;; select which header lines is to remain visible in the
1868           ;; article buffer.
1869           (while (re-search-forward "^[^ \t:]*:" nil t)
1870             (beginning-of-line)
1871             ;; Mark the rank of the header.
1872             (put-text-property
1873              (point) (1+ (point)) 'message-rank
1874              (if (or (and visible (looking-at visible))
1875                      (and ignored
1876                           (not (looking-at ignored))))
1877                  (gnus-article-header-rank)
1878                (+ 2 max)))
1879             (forward-line 1))
1880           (message-sort-headers-1)
1881           (when (setq beg (text-property-any
1882                            (point-min) (point-max) 'message-rank (+ 2 max)))
1883             ;; We delete the unwanted headers.
1884             (gnus-add-wash-type 'headers)
1885             (add-text-properties (point-min) (+ 5 (point-min))
1886                                  '(article-type headers dummy-invisible t))
1887             (delete-region beg (point-max))))))))
1888
1889 (defun article-hide-boring-headers (&optional arg)
1890   "Toggle hiding of headers that aren't very interesting.
1891 If given a negative prefix, always show; if given a positive prefix,
1892 always hide."
1893   (interactive (gnus-article-hidden-arg))
1894   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1895              (not gnus-show-all-headers))
1896     (save-excursion
1897       (save-restriction
1898         (let ((inhibit-read-only t)
1899               (inhibit-point-motion-hooks t))
1900           (article-narrow-to-head)
1901           (dolist (elem gnus-boring-article-headers)
1902             (goto-char (point-min))
1903             (cond
1904              ;; Hide empty headers.
1905              ((eq elem 'empty)
1906               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1907                 (forward-line -1)
1908                 (gnus-article-hide-text-type
1909                  (point-at-bol)
1910                  (progn
1911                    (end-of-line)
1912                    (if (re-search-forward "^[^ \t]" nil t)
1913                        (match-beginning 0)
1914                      (point-max)))
1915                  'boring-headers)))
1916              ;; Hide boring Newsgroups header.
1917              ((eq elem 'newsgroups)
1918               (when (gnus-string-equal
1919                      (gnus-fetch-field "newsgroups")
1920                      (gnus-group-real-name
1921                       (if (boundp 'gnus-newsgroup-name)
1922                           gnus-newsgroup-name
1923                         "")))
1924                 (gnus-article-hide-header "newsgroups")))
1925              ((eq elem 'to-address)
1926               (let ((to (message-fetch-field "to"))
1927                     (to-address
1928                      (gnus-parameter-to-address
1929                       (if (boundp 'gnus-newsgroup-name)
1930                           gnus-newsgroup-name ""))))
1931                 (when (and to to-address
1932                            (ignore-errors
1933                              (gnus-string-equal
1934                               ;; only one address in To
1935                               (nth 1 (mail-extract-address-components to))
1936                               to-address)))
1937                   (gnus-article-hide-header "to"))))
1938              ((eq elem 'to-list)
1939               (let ((to (message-fetch-field "to"))
1940                     (to-list
1941                      (gnus-parameter-to-list
1942                       (if (boundp 'gnus-newsgroup-name)
1943                           gnus-newsgroup-name ""))))
1944                 (when (and to to-list
1945                            (ignore-errors
1946                              (gnus-string-equal
1947                               ;; only one address in To
1948                               (nth 1 (mail-extract-address-components to))
1949                               to-list)))
1950                   (gnus-article-hide-header "to"))))
1951              ((eq elem 'cc-list)
1952               (let ((cc (message-fetch-field "cc"))
1953                     (to-list
1954                      (gnus-parameter-to-list
1955                       (if (boundp 'gnus-newsgroup-name)
1956                           gnus-newsgroup-name ""))))
1957                 (when (and cc to-list
1958                            (ignore-errors
1959                              (gnus-string-equal
1960                               ;; only one address in CC
1961                               (nth 1 (mail-extract-address-components cc))
1962                               to-list)))
1963                   (gnus-article-hide-header "cc"))))
1964              ((eq elem 'followup-to)
1965               (when (gnus-string-equal
1966                      (message-fetch-field "followup-to")
1967                      (message-fetch-field "newsgroups"))
1968                 (gnus-article-hide-header "followup-to")))
1969              ((eq elem 'reply-to)
1970               (if (gnus-group-find-parameter
1971                    gnus-newsgroup-name 'broken-reply-to)
1972                   (gnus-article-hide-header "reply-to")
1973                 (let ((from (message-fetch-field "from"))
1974                       (reply-to (message-fetch-field "reply-to")))
1975                   (when
1976                       (and
1977                        from reply-to
1978                        (ignore-errors
1979                          (equal
1980                           (sort (mapcar
1981                                  (lambda (x) (downcase (cadr x)))
1982                                  (mail-extract-address-components from t))
1983                                 'string<)
1984                           (sort (mapcar
1985                                  (lambda (x) (downcase (cadr x)))
1986                                  (mail-extract-address-components reply-to t))
1987                                 'string<))))
1988                     (gnus-article-hide-header "reply-to")))))
1989              ((eq elem 'date)
1990               (let ((date (with-current-buffer gnus-original-article-buffer
1991                             ;; If date in `gnus-article-buffer' is localized
1992                             ;; (`gnus-treat-date-user-defined'),
1993                             ;; `days-between' might fail.
1994                             (message-fetch-field "date"))))
1995                 (when (and date
1996                            (< (days-between (current-time-string) date)
1997                               4))
1998                   (gnus-article-hide-header "date"))))
1999              ((eq elem 'long-to)
2000               (let ((to (message-fetch-field "to"))
2001                     (cc (message-fetch-field "cc")))
2002                 (when (> (length to) 1024)
2003                   (gnus-article-hide-header "to"))
2004                 (when (> (length cc) 1024)
2005                   (gnus-article-hide-header "cc"))))
2006              ((eq elem 'many-to)
2007               (let ((to-count 0)
2008                     (cc-count 0))
2009                 (goto-char (point-min))
2010                 (while (re-search-forward "^to:" nil t)
2011                   (setq to-count (1+ to-count)))
2012                 (when (> to-count 1)
2013                   (while (> to-count 0)
2014                     (goto-char (point-min))
2015                     (save-restriction
2016                       (re-search-forward "^to:" nil nil to-count)
2017                       (forward-line -1)
2018                       (narrow-to-region (point) (point-max))
2019                       (gnus-article-hide-header "to"))
2020                     (setq to-count (1- to-count))))
2021                 (goto-char (point-min))
2022                 (while (re-search-forward "^cc:" nil t)
2023                   (setq cc-count (1+ cc-count)))
2024                 (when (> cc-count 1)
2025                   (while (> cc-count 0)
2026                     (goto-char (point-min))
2027                     (save-restriction
2028                       (re-search-forward "^cc:" nil nil cc-count)
2029                       (forward-line -1)
2030                       (narrow-to-region (point) (point-max))
2031                       (gnus-article-hide-header "cc"))
2032                     (setq cc-count (1- cc-count)))))))))))))
2033
2034 (defun gnus-article-hide-header (header)
2035   (save-excursion
2036     (goto-char (point-min))
2037     (when (re-search-forward (concat "^" header ":") nil t)
2038       (gnus-article-hide-text-type
2039        (point-at-bol)
2040        (progn
2041          (end-of-line)
2042          (if (re-search-forward "^[^ \t]" nil t)
2043              (match-beginning 0)
2044            (point-max)))
2045        'boring-headers))))
2046
2047 (defvar gnus-article-normalized-header-length 40
2048   "Length of normalized headers.")
2049
2050 (defun article-normalize-headers ()
2051   "Make all header lines 40 characters long."
2052   (interactive)
2053   (let ((inhibit-read-only t)
2054         column)
2055     (save-excursion
2056       (save-restriction
2057         (article-narrow-to-head)
2058         (while (not (eobp))
2059           (cond
2060            ((< (setq column (- (point-at-eol) (point)))
2061                gnus-article-normalized-header-length)
2062             (end-of-line)
2063             (insert (make-string
2064                      (- gnus-article-normalized-header-length column)
2065                      ? )))
2066            ((> column gnus-article-normalized-header-length)
2067             (gnus-put-text-property
2068              (progn
2069                (forward-char gnus-article-normalized-header-length)
2070                (point))
2071              (point-at-eol)
2072              'invisible t))
2073            (t
2074             ;; Do nothing.
2075             ))
2076           (forward-line 1))))))
2077
2078 (defun article-treat-dumbquotes ()
2079   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2080 Note that this function guesses whether a character is a sm*rtq**t* or
2081 not, so it should only be used interactively.
2082
2083 Sm*rtq**t*s are M****s***'s unilateral extension to the
2084 iso-8859-1 character map in an attempt to provide more quoting
2085 characters.  If you see something like \\222 or \\264 where
2086 you're expecting some kind of apostrophe or quotation mark, then
2087 try this wash."
2088   (interactive)
2089   (article-translate-strings gnus-article-dumbquotes-map))
2090
2091 (defvar org-entities)
2092
2093 (defun article-treat-non-ascii ()
2094   "Translate many Unicode characters into their ASCII equivalents."
2095   (interactive)
2096   (require 'org-entities)
2097   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2098     (dolist (elem org-entities)
2099       (when (and (listp elem)
2100                  (= (length (nth 6 elem)) 1))
2101         (if (featurep 'xemacs)
2102             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2103           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2104     (save-excursion
2105       (when (article-goto-body)
2106         (let ((inhibit-read-only t)
2107               replace props)
2108           (while (not (eobp))
2109             (if (not (setq replace (if (featurep 'xemacs)
2110                                        (get-char-table (following-char) table)
2111                                      (aref table (following-char)))))
2112                 (forward-char 1)
2113               (if (prog1
2114                       (setq props (text-properties-at (point)))
2115                     (delete-char 1))
2116                   (add-text-properties (point) (progn (insert replace) (point))
2117                                        props)
2118                 (insert replace)))))))))
2119
2120 (defun article-translate-characters (from to)
2121   "Translate all characters in the body of the article according to FROM and TO.
2122 FROM is a string of characters to translate from; to is a string of
2123 characters to translate to."
2124   (save-excursion
2125     (when (article-goto-body)
2126       (let ((inhibit-read-only t)
2127             (x (make-string 225 ?x))
2128             (i -1))
2129         (while (< (incf i) (length x))
2130           (aset x i i))
2131         (setq i 0)
2132         (while (< i (length from))
2133           (aset x (aref from i) (aref to i))
2134           (incf i))
2135         (translate-region (point) (point-max) x)))))
2136
2137 (defun article-translate-strings (map)
2138   "Translate all string in the body of the article according to MAP.
2139 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2140   (save-excursion
2141     (when (article-goto-body)
2142       (let ((inhibit-read-only t))
2143         (dolist (elem map)
2144           (let ((from (car elem))
2145                 (to (cadr elem)))
2146             (save-excursion
2147               (if (stringp from)
2148                   (while (search-forward from nil t)
2149                     (replace-match to))
2150                 (while (not (eobp))
2151                   (if (eq (following-char) from)
2152                       (progn
2153                         (delete-char 1)
2154                         (insert to))
2155                     (forward-char 1)))))))))))
2156
2157 (defun article-treat-overstrike ()
2158   "Translate overstrikes into bold text."
2159   (interactive)
2160   (save-excursion
2161     (when (article-goto-body)
2162       (let ((inhibit-read-only t))
2163         (while (search-forward "\b" nil t)
2164           (let ((next (char-after))
2165                 (previous (char-after (- (point) 2))))
2166             ;; We do the boldification/underlining by hiding the
2167             ;; overstrikes and putting the proper text property
2168             ;; on the letters.
2169             (cond
2170              ((eq next previous)
2171               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2172               (put-text-property (point) (1+ (point)) 'face 'bold))
2173              ((eq next ?_)
2174               (gnus-article-hide-text-type
2175                (1- (point)) (1+ (point)) 'overstrike)
2176               (put-text-property
2177                (- (point) 2) (1- (point)) 'face 'underline))
2178              ((eq previous ?_)
2179               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2180               (put-text-property
2181                (point) (1+ (point)) 'face 'underline)))))))))
2182
2183 (defun article-treat-ansi-sequences ()
2184   "Translate ANSI SGR control sequences into overlays or extents."
2185   (interactive)
2186   (save-excursion
2187     (when (article-goto-body)
2188       (let ((inhibit-read-only t))
2189         (ansi-color-apply-on-region (point) (point-max))))))
2190
2191 (defun gnus-article-treat-unfold-headers ()
2192   "Unfold folded message headers.
2193 Only the headers that fit into the current window width will be
2194 unfolded."
2195   (interactive)
2196   (gnus-with-article-headers
2197     (let (length)
2198       (while (not (eobp))
2199         (save-restriction
2200           (mail-header-narrow-to-field)
2201           (let* ((header (buffer-string))
2202                  (unfoldable
2203                   (or (equal gnus-article-unfold-long-headers t)
2204                       (and (stringp gnus-article-unfold-long-headers)
2205                            (string-match gnus-article-unfold-long-headers header)))))
2206             (with-temp-buffer
2207               (insert header)
2208               (goto-char (point-min))
2209               (while (re-search-forward "\n[\t ]" nil t)
2210                 (replace-match " " t t)))
2211             (setq length (- (point-max) (point-min) 1))
2212             (when (or unfoldable
2213                       (< length (window-width)))
2214               (while (re-search-forward "\n[\t ]" nil t)
2215                 (replace-match " " t t))))
2216           (goto-char (point-max)))))))
2217
2218 (defun gnus-article-treat-fold-headers ()
2219   "Fold message headers."
2220   (interactive)
2221   (gnus-with-article-headers
2222     (while (not (eobp))
2223       (save-restriction
2224         (mail-header-narrow-to-field)
2225         (mail-header-fold-field)
2226         (goto-char (point-max))))))
2227
2228 (defun gnus-treat-smiley ()
2229   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2230   (interactive)
2231   (gnus-with-article-buffer
2232     (if (memq 'smiley gnus-article-wash-types)
2233         (gnus-delete-images 'smiley)
2234       (article-goto-body)
2235       (let ((images (smiley-region (point) (point-max))))
2236         (when images
2237           (gnus-add-wash-type 'smiley)
2238           (dolist (image images)
2239             (gnus-add-image 'smiley image)))))))
2240
2241 (defun gnus-article-remove-images ()
2242   "Remove all images from the article buffer."
2243   (interactive)
2244   (gnus-with-article-buffer
2245     (save-restriction
2246       (widen)
2247       (dolist (elem gnus-article-image-alist)
2248         (gnus-delete-images (car elem))))))
2249
2250 (defun gnus-article-show-images ()
2251   "Show any images that are in the HTML-rendered article buffer.
2252 This only works if the article in question is HTML."
2253   (interactive)
2254   (gnus-with-article-buffer
2255     (save-restriction
2256       (widen)
2257       (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2258                                                       'image-displayer))
2259         (destructuring-bind (start end function) region
2260           (funcall function (get-text-property start 'image-url)
2261                    start end))))))
2262
2263 (defun gnus-article-treat-fold-newsgroups ()
2264   "Unfold folded message headers.
2265 Only the headers that fit into the current window width will be
2266 unfolded."
2267   (interactive)
2268   (gnus-with-article-headers
2269     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2270       (save-restriction
2271         (mail-header-narrow-to-field)
2272         (while (re-search-forward ", *" nil t)
2273           (replace-match ", " t t))
2274         (mail-header-fold-field)
2275         (goto-char (point-max))))))
2276
2277 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2278   "Value of `truncate-lines' in Gnus Article buffer.
2279 Valid values are nil, t, `head', `first', `last', an integer or a
2280 predicate.  See Info node `(gnus)Customizing Articles'."
2281   :version "23.1" ;; No Gnus
2282   :group 'gnus-article
2283   ;; :link '(custom-manual "(gnus)Customizing Articles")
2284   :type 'boolean)
2285
2286 (defun gnus-article-toggle-truncate-lines (&optional arg)
2287   "Toggle whether to fold or truncate long lines in article the buffer.
2288 If ARG is non-nil and not a number, toggle
2289 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2290 long lines if and only if arg is positive."
2291   (interactive "P")
2292   (cond
2293    ((and (numberp arg) (> arg 0))
2294     (setq gnus-article-truncate-lines t))
2295    ((numberp arg)
2296     (setq gnus-article-truncate-lines nil))
2297    (arg
2298     (setq gnus-article-truncate-lines
2299           (not gnus-article-truncate-lines))))
2300   (gnus-with-article-buffer
2301     (cond
2302      ((and (numberp arg) (> arg 0))
2303       (setq truncate-lines nil))
2304      ((numberp arg)
2305       (setq truncate-lines t)))
2306     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2307     ;; `toggle-truncate-lines' needs an argument.
2308     (toggle-truncate-lines)))
2309
2310 (defun gnus-article-treat-body-boundary ()
2311   "Place a boundary line at the end of the headers."
2312   (interactive)
2313   (when (and gnus-body-boundary-delimiter
2314              (> (length gnus-body-boundary-delimiter) 0))
2315     (gnus-with-article-headers
2316       (goto-char (point-max))
2317       (let ((start (point)))
2318         (insert "X-Boundary: ")
2319         (gnus-add-text-properties start (point) '(invisible t intangible t))
2320         (insert (let (str)
2321                   (while (>= (window-width) (length str))
2322                     (setq str (concat str gnus-body-boundary-delimiter)))
2323                   (substring str 0 (window-width)))
2324                 "\n")
2325         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2326
2327 (defun article-fill-long-lines ()
2328   "Fill lines that are wider than the window width."
2329   (interactive)
2330   (save-excursion
2331     (let ((inhibit-read-only t)
2332           (width (window-width (get-buffer-window (current-buffer)))))
2333       (save-restriction
2334         (article-goto-body)
2335         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2336           (while (not (eobp))
2337             (end-of-line)
2338             (when (>= (current-column) (min fill-column width))
2339               (narrow-to-region (min (1+ (point)) (point-max))
2340                                 (point-at-bol))
2341               (let ((goback (point-marker)))
2342                 (fill-paragraph nil)
2343                 (goto-char (marker-position goback)))
2344               (widen))
2345             (forward-line 1)))))))
2346
2347 (defun article-capitalize-sentences ()
2348   "Capitalize the first word in each sentence."
2349   (interactive)
2350   (save-excursion
2351     (let ((inhibit-read-only t)
2352           (paragraph-start "^[\n\^L]"))
2353       (article-goto-body)
2354       (while (not (eobp))
2355         (capitalize-word 1)
2356         (forward-sentence)))))
2357
2358 (defun article-remove-cr ()
2359   "Remove trailing CRs and then translate remaining CRs into LFs."
2360   (interactive)
2361   (save-excursion
2362     (let ((inhibit-read-only t))
2363       (goto-char (point-min))
2364       (while (re-search-forward "\r+$" nil t)
2365         (replace-match "" t t))
2366       (goto-char (point-min))
2367       (while (search-forward "\r" nil t)
2368         (replace-match "\n" t t)))))
2369
2370 (defun article-remove-trailing-blank-lines ()
2371   "Remove all trailing blank lines from the article."
2372   (interactive)
2373   (save-excursion
2374     (let ((inhibit-read-only t))
2375       (goto-char (point-max))
2376       (delete-region
2377        (point)
2378        (progn
2379          (while (and (not (bobp))
2380                      (looking-at "^[ \t]*$")
2381                      (not (gnus-annotation-in-region-p
2382                            (point) (point-at-eol))))
2383            (forward-line -1))
2384          (forward-line 1)
2385          (point))))))
2386
2387 (defvar gnus-face-properties-alist)
2388
2389 (defun article-display-face (&optional force)
2390   "Display any Face headers in the header."
2391   (interactive (list 'force))
2392   (let ((wash-face-p buffer-read-only))
2393     (gnus-with-article-headers
2394       ;; When displaying parts, this function can be called several times on
2395       ;; the same article, without any intended toggle semantic (as typing `W
2396       ;; D d' would have). So face deletion must occur only when we come from
2397       ;; an interactive command, that is when the *Article* buffer is
2398       ;; read-only.
2399       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2400           (gnus-delete-images 'face)
2401         (let ((from (message-fetch-field "from"))
2402               face faces)
2403           (save-current-buffer
2404             (when (and wash-face-p
2405                        (gnus-buffer-live-p gnus-original-article-buffer)
2406                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2407               (set-buffer gnus-original-article-buffer))
2408             (save-restriction
2409               (mail-narrow-to-head)
2410               (when (or force
2411                         ;; Check whether this face is censored.
2412                         (not (and gnus-article-x-face-too-ugly
2413                                   (or from
2414                                       (setq from (message-fetch-field "from")))
2415                                   (string-match gnus-article-x-face-too-ugly
2416                                                 from))))
2417                 (while (gnus-article-goto-header "Face")
2418                   (push (mail-header-field-value) faces)))))
2419           (when faces
2420             (goto-char (point-min))
2421             (let (png image)
2422               (unless (setq from (gnus-article-goto-header "from"))
2423                 (insert "From:")
2424                 (setq from (point))
2425                 (insert " [no `from' set]\n"))
2426               (while faces
2427                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2428                   (setq image
2429                         (apply 'gnus-create-image png 'png t
2430                                (cdr (assq 'png gnus-face-properties-alist))))
2431                   (goto-char from)
2432                   (gnus-add-wash-type 'face)
2433                   (gnus-add-image 'face image)
2434                   (gnus-put-image image nil 'face))))))))))
2435
2436 (defun article-display-x-face (&optional force)
2437   "Look for an X-Face header and display it if present."
2438   (interactive (list 'force))
2439   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2440     (gnus-with-article-headers
2441       ;; Delete the old process, if any.
2442       (when (process-status "article-x-face")
2443         (delete-process "article-x-face"))
2444       ;; See the comment in `article-display-face'.
2445       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2446           ;; We have already displayed X-Faces, so we remove them
2447           ;; instead.
2448           (gnus-delete-images 'xface)
2449         ;; Display X-Faces.
2450         (let ((from (message-fetch-field "from"))
2451               x-faces face)
2452           (save-current-buffer
2453             (when (and wash-face-p
2454                        (gnus-buffer-live-p gnus-original-article-buffer)
2455                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2456               ;; If type `W f', use gnus-original-article-buffer,
2457               ;; otherwise use the current buffer because displaying
2458               ;; RFC822 parts calls this function too.
2459               (set-buffer gnus-original-article-buffer))
2460             (save-restriction
2461               (mail-narrow-to-head)
2462               (and gnus-article-x-face-command
2463                    (or force
2464                        ;; Check whether this face is censored.
2465                        (not (and gnus-article-x-face-too-ugly
2466                                  (or from
2467                                      (setq from (message-fetch-field "from")))
2468                                  (string-match gnus-article-x-face-too-ugly
2469                                                from))))
2470                    (while (gnus-article-goto-header "X-Face")
2471                      (push (mail-header-field-value) x-faces)))))
2472           (when x-faces
2473             ;; We display the face.
2474             (cond ((functionp gnus-article-x-face-command)
2475                    ;; The command is a lisp function, so we call it.
2476                    (mapc gnus-article-x-face-command x-faces))
2477                   ((stringp gnus-article-x-face-command)
2478                    ;; The command is a string, so we interpret the command
2479                    ;; as a, well, command, and fork it off.
2480                    (let ((process-connection-type nil))
2481                      (gnus-set-process-query-on-exit-flag
2482                       (start-process
2483                        "article-x-face" nil shell-file-name
2484                        shell-command-switch gnus-article-x-face-command)
2485                       nil)
2486                      ;; Sending multiple EOFs to xv doesn't work,
2487                      ;; so we only do a single external face.
2488                      (with-temp-buffer
2489                        (insert (car x-faces))
2490                        (process-send-region "article-x-face"
2491                                             (point-min) (point-max)))
2492                      (process-send-eof "article-x-face")))
2493                   (t
2494                    (error "`%s' set to `%s' is not a function"
2495                           gnus-article-x-face-command
2496                           'gnus-article-x-face-command)))))))))
2497
2498 (defun article-decode-mime-words ()
2499   "Decode all MIME-encoded words in the article."
2500   (interactive)
2501   (gnus-with-article-buffer
2502     (let ((inhibit-point-motion-hooks t)
2503           (mail-parse-charset gnus-newsgroup-charset)
2504           (mail-parse-ignored-charsets
2505            (with-current-buffer gnus-summary-buffer
2506              gnus-newsgroup-ignored-charsets)))
2507       (mail-decode-encoded-word-region (point-min) (point-max)))))
2508
2509 (defun article-decode-charset (&optional prompt)
2510   "Decode charset-encoded text in the article.
2511 If PROMPT (the prefix), prompt for a coding system to use."
2512   (interactive "P")
2513   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2514         (inhibit-read-only t)
2515         (mail-parse-charset gnus-newsgroup-charset)
2516         (mail-parse-ignored-charsets
2517          (save-excursion (condition-case nil
2518                              (set-buffer gnus-summary-buffer)
2519                            (error))
2520                          gnus-newsgroup-ignored-charsets))
2521         ct cte ctl charset format)
2522     (save-excursion
2523       (save-restriction
2524         (article-narrow-to-head)
2525         (setq ct (message-fetch-field "Content-Type" t)
2526               cte (message-fetch-field "Content-Transfer-Encoding" t)
2527               ctl (and ct (mail-header-parse-content-type ct))
2528               charset (cond
2529                        (prompt
2530                         (mm-read-coding-system "Charset to decode: "))
2531                        (ctl
2532                         (mail-content-type-get ctl 'charset)))
2533               format (and ctl (mail-content-type-get ctl 'format)))
2534         (when cte
2535           (setq cte (mail-header-strip cte)))
2536         (if (and ctl (not (string-match "/" (car ctl))))
2537             (setq ctl nil))
2538         (goto-char (point-max)))
2539       (forward-line 1)
2540       (save-restriction
2541         (narrow-to-region (point) (point-max))
2542         (when (and (eq mail-parse-charset 'gnus-decoded)
2543                    (eq (mm-body-7-or-8) '8bit))
2544           ;; The text code could have been decoded.
2545           (setq charset mail-parse-charset))
2546         (when (and (or (not ctl)
2547                        (equal (car ctl) "text/plain"))
2548                    (not format)) ;; article with format will decode later.
2549           (mm-decode-body
2550            charset (and cte (intern (downcase
2551                                      (gnus-strip-whitespace cte))))
2552            (car ctl)))))))
2553
2554 (defun article-decode-encoded-words ()
2555   "Remove encoded-word encoding from headers."
2556   (let ((inhibit-point-motion-hooks t)
2557         (mail-parse-charset gnus-newsgroup-charset)
2558         (mail-parse-ignored-charsets
2559          (save-excursion (condition-case nil
2560                              (set-buffer gnus-summary-buffer)
2561                            (error))
2562                          gnus-newsgroup-ignored-charsets))
2563         (inhibit-read-only t)
2564         end start)
2565     (goto-char (point-min))
2566     (when (search-forward "\n\n" nil 'move)
2567       (forward-line -1))
2568     (setq end (point))
2569     (while (not (bobp))
2570       (while (progn
2571                (forward-line -1)
2572                (and (not (bobp))
2573                     (memq (char-after) '(?\t ? )))))
2574       (setq start (point))
2575       (if (looking-at "\
2576 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2577 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2578           (funcall gnus-decode-address-function start end)
2579         (funcall gnus-decode-header-function start end))
2580       (goto-char (setq end start)))))
2581
2582 (defun article-decode-group-name ()
2583   "Decode group names in Newsgroups, Followup-To and Xref headers."
2584   (let ((inhibit-point-motion-hooks t)
2585         (inhibit-read-only t)
2586         (method (gnus-find-method-for-group gnus-newsgroup-name))
2587         regexp)
2588     (when (and (or gnus-group-name-charset-method-alist
2589                    gnus-group-name-charset-group-alist)
2590                (gnus-buffer-live-p gnus-original-article-buffer))
2591       (save-restriction
2592         (article-narrow-to-head)
2593         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2594           (with-current-buffer gnus-original-article-buffer
2595             (goto-char (point-min)))
2596           (setq regexp (concat "^" header
2597                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2598           (while (re-search-forward regexp nil t)
2599             (replace-match (save-match-data
2600                              (gnus-decode-newsgroups
2601                               ;; XXX how to use data in article buffer?
2602                               (with-current-buffer gnus-original-article-buffer
2603                                 (re-search-forward regexp nil t)
2604                                 (match-string 1))
2605                               gnus-newsgroup-name method))
2606                            t t nil 1))
2607           (goto-char (point-min)))))))
2608
2609 (autoload 'idna-to-unicode "idna")
2610
2611 (defun article-decode-idna-rhs ()
2612   "Decode IDNA strings in RHS in various headers in current buffer.
2613 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2614 Mail-Reply-To: and Mail-Followup-To:."
2615   (when gnus-use-idna
2616     (save-restriction
2617       (let ((inhibit-point-motion-hooks t)
2618             (inhibit-read-only t))
2619         (article-narrow-to-head)
2620         (goto-char (point-min))
2621         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2622           (let (ace unicode)
2623             (when (save-match-data
2624                     (and (setq ace (match-string 1))
2625                          (save-excursion
2626                            (and (re-search-backward "^[^ \t]" nil t)
2627                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2628                          (setq unicode (idna-to-unicode ace))))
2629               (unless (string= ace unicode)
2630                 (replace-match unicode nil nil nil 1)))))))))
2631
2632 (defun article-de-quoted-unreadable (&optional force read-charset)
2633   "Translate a quoted-printable-encoded article.
2634 If FORCE, decode the article whether it is marked as quoted-printable
2635 or not.
2636 If READ-CHARSET, ask for a coding system."
2637   (interactive (list 'force current-prefix-arg))
2638   (save-excursion
2639     (let ((inhibit-read-only t) type charset)
2640       (if (gnus-buffer-live-p gnus-original-article-buffer)
2641           (with-current-buffer gnus-original-article-buffer
2642             (setq type
2643                   (gnus-fetch-field "content-transfer-encoding"))
2644             (let* ((ct (gnus-fetch-field "content-type"))
2645                    (ctl (and ct (mail-header-parse-content-type ct))))
2646               (setq charset (and ctl
2647                                  (mail-content-type-get ctl 'charset)))
2648               (if (stringp charset)
2649                   (setq charset (intern (downcase charset)))))))
2650       (if read-charset
2651           (setq charset (mm-read-coding-system "Charset: " charset)))
2652       (unless charset
2653         (setq charset gnus-newsgroup-charset))
2654       (when (or force
2655                 (and type (let ((case-fold-search t))
2656                             (string-match "quoted-printable" type))))
2657         (article-goto-body)
2658         (quoted-printable-decode-region
2659          (point) (point-max) (mm-charset-to-coding-system charset))))))
2660
2661 (defun article-de-base64-unreadable (&optional force read-charset)
2662   "Translate a base64 article.
2663 If FORCE, decode the article whether it is marked as base64 not.
2664 If READ-CHARSET, ask for a coding system."
2665   (interactive (list 'force current-prefix-arg))
2666   (save-excursion
2667     (let ((inhibit-read-only t) type charset)
2668       (if (gnus-buffer-live-p gnus-original-article-buffer)
2669           (with-current-buffer gnus-original-article-buffer
2670             (setq type
2671                   (gnus-fetch-field "content-transfer-encoding"))
2672             (let* ((ct (gnus-fetch-field "content-type"))
2673                    (ctl (and ct (mail-header-parse-content-type ct))))
2674               (setq charset (and ctl
2675                                  (mail-content-type-get ctl 'charset)))
2676               (if (stringp charset)
2677                   (setq charset (intern (downcase charset)))))))
2678       (if read-charset
2679           (setq charset (mm-read-coding-system "Charset: " charset)))
2680       (unless charset
2681         (setq charset gnus-newsgroup-charset))
2682       (when (or force
2683                 (and type (let ((case-fold-search t))
2684                             (string-match "base64" type))))
2685         (article-goto-body)
2686         (save-restriction
2687           (narrow-to-region (point) (point-max))
2688           (base64-decode-region (point-min) (point-max))
2689           (mm-decode-coding-region
2690            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2691
2692 (eval-when-compile
2693   (require 'rfc1843))
2694
2695 (defun article-decode-HZ ()
2696   "Translate a HZ-encoded article."
2697   (interactive)
2698   (require 'rfc1843)
2699   (save-excursion
2700     (let ((inhibit-read-only t))
2701       (rfc1843-decode-region (point-min) (point-max)))))
2702
2703 (defun article-unsplit-urls ()
2704   "Remove the newlines that some other mailers insert into URLs."
2705   (interactive)
2706   (save-excursion
2707     (let ((inhibit-read-only t))
2708       (goto-char (point-min))
2709       (while (re-search-forward
2710               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2711         (replace-match "\\1\\3" t)))
2712     (when (interactive-p)
2713       (gnus-treat-article nil))))
2714
2715 (defun article-wash-html ()
2716   "Format an HTML article."
2717   (interactive)
2718   (let ((handles nil)
2719         (buffer-read-only nil))
2720     (when (gnus-buffer-live-p gnus-original-article-buffer)
2721       (setq handles (mm-dissect-buffer t t)))
2722     (article-goto-body)
2723     (delete-region (point) (point-max))
2724     (mm-inline-text-html handles)))
2725
2726 (defvar gnus-article-browse-html-temp-list nil
2727   "List of temporary files created by `gnus-article-browse-html-parts'.
2728 Internal variable.")
2729
2730 (defcustom gnus-article-browse-delete-temp 'ask
2731   "What to do with temporary files from `gnus-article-browse-html-parts'.
2732 If nil, don't delete temporary files.  If it is t, delete them on
2733 exit from the summary buffer.  If it is the symbol `file', query
2734 on each file, if it is `ask' ask once when exiting from the
2735 summary buffer."
2736   :group 'gnus-article
2737   :version "23.1" ;; No Gnus
2738   :type '(choice (const :tag "Don't delete" nil)
2739                  (const :tag "Don't ask" t)
2740                  (const :tag "Ask" ask)
2741                  (const :tag "Ask for each file" file)))
2742
2743 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2744
2745 (defun gnus-article-browse-delete-temp-files (&optional how)
2746   "Delete temp-files created by `gnus-article-browse-html-parts'."
2747   (when (and gnus-article-browse-html-temp-list
2748              (progn
2749                (or how (setq how gnus-article-browse-delete-temp))
2750                (if (eq how 'ask)
2751                    (let ((files (length gnus-article-browse-html-temp-list)))
2752                      (gnus-y-or-n-p (format
2753                                      "Delete all %s temporary HTML file%s? "
2754                                      files
2755                                      (if (> files 1) "s" ""))))
2756                  how)))
2757     (dolist (file gnus-article-browse-html-temp-list)
2758       (cond ((file-directory-p file)
2759              (when (or (not (eq how 'file))
2760                        (gnus-y-or-n-p
2761                         (format
2762                          "Delete temporary HTML file(s) in directory `%s'? "
2763                          (file-name-as-directory file))))
2764                (gnus-delete-directory file)))
2765             ((file-exists-p file)
2766              (when (or (not (eq how 'file))
2767                        (gnus-y-or-n-p
2768                         (format "Delete temporary HTML file `%s'? " file)))
2769                (delete-file file)))))
2770     ;; Also remove file from the list when not deleted or if file doesn't
2771     ;; exist anymore.
2772     (setq gnus-article-browse-html-temp-list nil))
2773   gnus-article-browse-html-temp-list)
2774
2775 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2776   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2777 Return file name."
2778   (save-match-data
2779     (let (file type)
2780       (catch 'found
2781         (dolist (handle handles)
2782           (cond
2783            ((not (listp handle)))
2784            ((equal (mm-handle-media-supertype handle) "multipart")
2785             (when (setq file (gnus-article-browse-html-save-cid-content
2786                               cid handle directory))
2787               (throw 'found file)))
2788            ((equal (concat "<" cid ">") (mm-handle-id handle))
2789             (setq file
2790                   (expand-file-name
2791                    (or (mail-content-type-get
2792                         (mm-handle-disposition handle) 'filename)
2793                        (mail-content-type-get
2794                         (setq type (mm-handle-type handle)) 'name)
2795                        (concat
2796                         (make-temp-name "cid")
2797                         (car (rassoc (car type) mailcap-mime-extensions))))
2798                    directory))
2799             (mm-save-part-to-file handle file)
2800             (throw 'found file))))))))
2801
2802 (defun gnus-article-browse-html-parts (list &optional header)
2803   "View all \"text/html\" parts from LIST.
2804 Recurse into multiparts.  The optional HEADER that should be a decoded
2805 message header will be added to the bodies of the \"text/html\" parts."
2806   ;; Internal function used by `gnus-article-browse-html-article'.
2807   (let (type file charset content cid-dir tmp-file showed)
2808     ;; Find and show the html-parts.
2809     (dolist (handle list)
2810       ;; If HTML, show it:
2811       (cond ((not (listp handle)))
2812             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2813                  (and (equal (car type) "message/external-body")
2814                       (or header
2815                           (setq file (or (mail-content-type-get type 'name)
2816                                          (mail-content-type-get
2817                                           (mm-handle-disposition handle)
2818                                           'filename))))
2819                       (or (mm-handle-cache handle)
2820                           (condition-case code
2821                               (progn (mm-extern-cache-contents handle) t)
2822                             (error
2823                              (gnus-message 3 "%s" (error-message-string code))
2824                              (when (>= gnus-verbose 3) (sit-for 2))
2825                              nil)))
2826                       (progn
2827                         (setq handle (mm-handle-cache handle)
2828                               type (mm-handle-type handle))
2829                         (equal (car type) "text/html"))))
2830              (setq charset (mail-content-type-get type 'charset)
2831                    content (mm-get-part handle))
2832              (with-temp-buffer
2833                (if (eq charset 'gnus-decoded)
2834                    (mm-enable-multibyte)
2835                  (mm-disable-multibyte))
2836                (insert content)
2837                ;; resolve cid contents
2838                (let ((case-fold-search t)
2839                      cid-file)
2840                  (goto-char (point-min))
2841                  (while (re-search-forward "\
2842 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2843                                            nil t)
2844                    (unless cid-dir
2845                      (setq cid-dir (mm-make-temp-file "cid" t))
2846                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2847                    (setq file nil
2848                          content nil)
2849                    (when (setq cid-file
2850                                (gnus-article-browse-html-save-cid-content
2851                                 (match-string 2)
2852                                 (with-current-buffer gnus-article-buffer
2853                                   gnus-article-mime-handles)
2854                                 cid-dir))
2855                      (replace-match (concat "file://" cid-file)
2856                                     nil nil nil 1))))
2857                (unless content (setq content (buffer-string))))
2858              (when (or charset header (not file))
2859                (setq tmp-file (mm-make-temp-file
2860                                ;; Do we need to care for 8.3 filenames?
2861                                "mm-" nil ".html")))
2862              ;; Add a meta html tag to specify charset and a header.
2863              (cond
2864               (header
2865                (let (title eheader body hcharset coding force-charset)
2866                  (with-temp-buffer
2867                    (mm-enable-multibyte)
2868                    (setq case-fold-search t)
2869                    (insert header "\n")
2870                    (setq title (message-fetch-field "subject"))
2871                    (goto-char (point-min))
2872                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2873                      (replace-match (cond ((match-beginning 1) "&lt;")
2874                                           ((match-beginning 2) "&gt;")
2875                                           (t "&amp;"))))
2876                    (goto-char (point-min))
2877                    (insert "<pre>\n")
2878                    (goto-char (point-max))
2879                    (insert "</pre>\n<hr>\n")
2880                    ;; We have to examine charset one by one since
2881                    ;; charset specified in parts might be different.
2882                    (if (eq charset 'gnus-decoded)
2883                        (setq charset 'utf-8
2884                              eheader (mm-encode-coding-string (buffer-string)
2885                                                               charset)
2886                              title (when title
2887                                      (mm-encode-coding-string title charset))
2888                              body (mm-encode-coding-string content charset)
2889                              force-charset t)
2890                      (setq hcharset (mm-find-mime-charset-region (point-min)
2891                                                                  (point-max)))
2892                      (cond ((= (length hcharset) 1)
2893                             (setq hcharset (car hcharset)
2894                                   coding (mm-charset-to-coding-system
2895                                           hcharset)))
2896                            ((> (length hcharset) 1)
2897                             (setq hcharset 'utf-8
2898                                   coding hcharset)))
2899                      (if coding
2900                          (if charset
2901                              (progn
2902                                (setq body
2903                                      (mm-charset-to-coding-system charset))
2904                                (if (eq coding body)
2905                                    (setq eheader (mm-encode-coding-string
2906                                                   (buffer-string) coding)
2907                                          title (when title
2908                                                  (mm-encode-coding-string
2909                                                   title coding))
2910                                          body content)
2911                                  (setq charset 'utf-8
2912                                        eheader (mm-encode-coding-string
2913                                                 (buffer-string) charset)
2914                                        title (when title
2915                                                (mm-encode-coding-string
2916                                                 title charset))
2917                                        body (mm-encode-coding-string
2918                                              (mm-decode-coding-string
2919                                               content body)
2920                                              charset)
2921                                        force-charset t)))
2922                            (setq charset hcharset
2923                                  eheader (mm-encode-coding-string
2924                                           (buffer-string) coding)
2925                                  title (when title
2926                                          (mm-encode-coding-string
2927                                           title coding))
2928                                  body content))
2929                        (setq eheader (mm-string-as-unibyte (buffer-string))
2930                              body content)))
2931                    (erase-buffer)
2932                    (mm-disable-multibyte)
2933                    (insert body)
2934                    (when charset
2935                      (mm-add-meta-html-tag handle charset force-charset))
2936                    (when title
2937                      (goto-char (point-min))
2938                      (unless (search-forward "<title>" nil t)
2939                        (re-search-forward "<head>\\s-*" nil t)
2940                        (insert "<title>" title "</title>\n")))
2941                    (goto-char (point-min))
2942                    (or (re-search-forward
2943                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2944                        (re-search-forward
2945                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2946                    (insert eheader)
2947                    (mm-write-region (point-min) (point-max)
2948                                     tmp-file nil nil nil 'binary t))))
2949               (charset
2950                (mm-with-unibyte-buffer
2951                  (insert (if (eq charset 'gnus-decoded)
2952                              (mm-encode-coding-string content
2953                                                       (setq charset 'utf-8))
2954                            content))
2955                  (if (or (mm-add-meta-html-tag handle charset)
2956                          (not file))
2957                      (mm-write-region (point-min) (point-max)
2958                                       tmp-file nil nil nil 'binary t)
2959                    (setq tmp-file nil))))
2960               (tmp-file
2961                (mm-save-part-to-file handle tmp-file)))
2962              (when tmp-file
2963                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2964              (add-hook 'gnus-summary-prepare-exit-hook
2965                        'gnus-article-browse-delete-temp-files)
2966              (add-hook 'gnus-exit-gnus-hook
2967                        (lambda  ()
2968                          (gnus-article-browse-delete-temp-files t)))
2969              ;; FIXME: Warn if there's an <img> tag?
2970              (browse-url-of-file (or tmp-file (expand-file-name file)))
2971              (setq showed t))
2972             ;; If multipart, recurse
2973             ((equal (mm-handle-media-supertype handle) "multipart")
2974              (when (gnus-article-browse-html-parts handle header)
2975                (setq showed t)))
2976             ((equal (mm-handle-media-type handle) "message/rfc822")
2977              (mm-with-multibyte-buffer
2978                (mm-insert-part handle)
2979                (setq handle (mm-dissect-buffer t t))
2980                (when (and (bufferp (car handle))
2981                           (stringp (car (mm-handle-type handle))))
2982                  (setq handle (list handle)))
2983                (when header
2984                  (article-decode-encoded-words)
2985                  (let ((gnus-visible-headers
2986                         (or (get 'gnus-visible-headers 'standard-value)
2987                             gnus-visible-headers)))
2988                    (article-hide-headers))
2989                  (goto-char (point-min))
2990                  (search-forward "\n\n" nil 'move)
2991                  (skip-chars-backward "\t\n ")
2992                  (setq header (buffer-substring (point-min) (point)))))
2993              (when (prog1
2994                        (gnus-article-browse-html-parts handle header)
2995                      (mm-destroy-parts handle))
2996                (setq showed t)))))
2997     showed))
2998
2999 (defun gnus-article-browse-html-article (&optional arg)
3000   "View \"text/html\" parts of the current article with a WWW browser.
3001 Inline images embedded in a message using the cid scheme, as they are
3002 generally considered to be safe, will be processed properly.
3003 The message header is added to the beginning of every html part unless
3004 the prefix argument ARG is given.
3005
3006 Warning: Spammers use links to images (using the http scheme) in HTML
3007 articles to verify whether you have read the message.  As
3008 `gnus-article-browse-html-article' passes the HTML content to the
3009 browser without eliminating these \"web bugs\" you should only
3010 use it for mails from trusted senders.
3011
3012 If you always want to display HTML parts in the browser, set
3013 `mm-text-html-renderer' to nil.
3014
3015 This command creates temporary files to pass HTML contents including
3016 images if any to the browser, and deletes them when exiting the group
3017 \(if you want)."
3018   ;; Cf. `mm-w3m-safe-url-regexp'
3019   (interactive "P")
3020   (if arg
3021       (gnus-summary-show-article)
3022     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3023                                     gnus-visible-headers))
3024           ;; As we insert a <hr>, there's no need for the body boundary.
3025           (gnus-treat-body-boundary nil))
3026       (gnus-summary-show-article)))
3027   (with-current-buffer gnus-article-buffer
3028     (let ((header (unless arg
3029                     (save-restriction
3030                       (widen)
3031                       (buffer-substring-no-properties
3032                        (goto-char (point-min))
3033                        (if (search-forward "\n\n" nil t)
3034                            (match-beginning 0)
3035                          (goto-char (point-max))
3036                          (skip-chars-backward "\t\n ")
3037                          (point))))))
3038           parts)
3039       (set-buffer gnus-original-article-buffer)
3040       (setq parts (mm-dissect-buffer t t))
3041       ;; If singlepart, enforce a list.
3042       (when (and (bufferp (car parts))
3043                  (stringp (car (mm-handle-type parts))))
3044         (setq parts (list parts)))
3045       ;; Process the list
3046       (unless (gnus-article-browse-html-parts parts header)
3047         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3048       (mm-destroy-parts parts)
3049       (unless arg
3050         (gnus-summary-show-article)))))
3051
3052 (defun article-hide-list-identifiers ()
3053   "Remove list identifies from the Subject header.
3054 The `gnus-list-identifiers' variable specifies what to do."
3055   (interactive)
3056   (let ((inhibit-point-motion-hooks t)
3057         (regexp (if (consp gnus-list-identifiers)
3058                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3059                   gnus-list-identifiers))
3060         (inhibit-read-only t))
3061     (when regexp
3062       (save-excursion
3063         (save-restriction
3064           (article-narrow-to-head)
3065           (goto-char (point-min))
3066           (while (re-search-forward
3067                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3068                   nil t)
3069             (delete-region (match-beginning 2) (match-end 0))
3070             (beginning-of-line))
3071           (when (re-search-forward
3072                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3073             (delete-region (match-beginning 1) (match-end 1))))))))
3074
3075 (defun article-hide-pem (&optional arg)
3076   "Toggle hiding of any PEM headers and signatures in the current article.
3077 If given a negative prefix, always show; if given a positive prefix,
3078 always hide."
3079   (interactive (gnus-article-hidden-arg))
3080   (unless (gnus-article-check-hidden-text 'pem arg)
3081     (save-excursion
3082       (let ((inhibit-read-only t) end)
3083         (goto-char (point-min))
3084         ;; Hide the horrendously ugly "header".
3085         (when (and (search-forward
3086                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3087                     nil t)
3088                    (setq end (1+ (match-beginning 0))))
3089           (gnus-add-wash-type 'pem)
3090           (gnus-article-hide-text-type
3091            end
3092            (if (search-forward "\n\n" nil t)
3093                (match-end 0)
3094              (point-max))
3095            'pem)
3096           ;; Hide the trailer as well
3097           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3098                                 nil t)
3099             (gnus-article-hide-text-type
3100              (match-beginning 0) (match-end 0) 'pem)))))))
3101
3102 (defun article-strip-banner ()
3103   "Strip the banners specified by the `banner' group parameter and by
3104 `gnus-article-address-banner-alist'."
3105   (interactive)
3106   (save-excursion
3107     (save-restriction
3108       (let ((inhibit-point-motion-hooks t))
3109         (when (gnus-parameter-banner gnus-newsgroup-name)
3110           (article-really-strip-banner
3111            (gnus-parameter-banner gnus-newsgroup-name)))
3112         (when gnus-article-address-banner-alist
3113           ;; Note that the From header is decoded here, so it is
3114           ;; required that the *-extract-address-components function
3115           ;; supports non-ASCII text.
3116           (let ((from (save-restriction
3117                         (widen)
3118                         (article-narrow-to-head)
3119                         (mail-fetch-field "from"))))
3120             (when (and from
3121                        (setq from
3122                              (cadr (funcall gnus-extract-address-components
3123                                             from))))
3124               (catch 'found
3125                 (dolist (pair gnus-article-address-banner-alist)
3126                   (when (string-match (car pair) from)
3127                     (throw 'found
3128                            (article-really-strip-banner (cdr pair)))))))))))))
3129
3130 (defun article-really-strip-banner (banner)
3131   "Strip the banner specified by the argument."
3132   (save-excursion
3133     (save-restriction
3134       (let ((inhibit-point-motion-hooks t)
3135             (gnus-signature-limit nil)
3136             (inhibit-read-only t))
3137         (article-goto-body)
3138         (cond
3139          ((eq banner 'signature)
3140           (when (gnus-article-narrow-to-signature)
3141             (widen)
3142             (forward-line -1)
3143             (delete-region (point) (point-max))))
3144          ((symbolp banner)
3145           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3146               (while (re-search-forward banner nil t)
3147                 (delete-region (match-beginning 0) (match-end 0)))))
3148          ((stringp banner)
3149           (while (re-search-forward banner nil t)
3150             (delete-region (match-beginning 0) (match-end 0)))))))))
3151
3152 (defun article-babel ()
3153   "Translate article using an online translation service."
3154   (interactive)
3155   (require 'babel)
3156   (gnus-with-article-buffer
3157     (when (article-goto-body)
3158       (let* ((start (point))
3159              (end (point-max))
3160              (orig (buffer-substring start end))
3161              (trans (babel-as-string orig)))
3162         (save-restriction
3163           (narrow-to-region start end)
3164           (delete-region start end)
3165           (insert trans))))))
3166
3167 (defun article-hide-signature (&optional arg)
3168   "Hide the signature in the current article.
3169 If given a negative prefix, always show; if given a positive prefix,
3170 always hide."
3171   (interactive (gnus-article-hidden-arg))
3172   (unless (gnus-article-check-hidden-text 'signature arg)
3173     (save-excursion
3174       (save-restriction
3175         (let ((inhibit-read-only t))
3176           (when (gnus-article-narrow-to-signature)
3177             (gnus-article-hide-text-type
3178              (point-min) (point-max) 'signature))))))
3179   (gnus-set-mode-line 'article))
3180
3181 (defun article-strip-headers-in-body ()
3182   "Strip offensive headers from bodies."
3183   (interactive)
3184   (save-excursion
3185     (article-goto-body)
3186     (let ((case-fold-search t))
3187       (when (looking-at "x-no-archive:")
3188         (gnus-delete-line)))))
3189
3190 (defun article-strip-leading-blank-lines ()
3191   "Remove all blank lines from the beginning of the article."
3192   (interactive)
3193   (save-excursion
3194     (let ((inhibit-point-motion-hooks t)
3195           (inhibit-read-only t))
3196       (when (article-goto-body)
3197         (while (and (not (eobp))
3198                     (looking-at "[ \t]*$"))
3199           (gnus-delete-line))))))
3200
3201 (defun article-narrow-to-head ()
3202   "Narrow the buffer to the head of the message.
3203 Point is left at the beginning of the narrowed-to region."
3204   (narrow-to-region
3205    (goto-char (point-min))
3206    (if (search-forward "\n\n" nil 1)
3207        (1- (point))
3208      (point-max)))
3209   (goto-char (point-min)))
3210
3211 (defun article-goto-body ()
3212   "Place point at the start of the body."
3213   (goto-char (point-min))
3214   (cond
3215    ;; This variable is only bound when dealing with separate
3216    ;; MIME body parts.
3217    (article-goto-body-goes-to-point-min-p
3218     t)
3219    ((search-forward "\n\n" nil t)
3220     t)
3221    (t
3222     (goto-char (point-max))
3223     nil)))
3224
3225 (defun article-strip-multiple-blank-lines ()
3226   "Replace consecutive blank lines with one empty line."
3227   (interactive)
3228   (save-excursion
3229     (let ((inhibit-point-motion-hooks t)
3230           (inhibit-read-only t))
3231       ;; First make all blank lines empty.
3232       (article-goto-body)
3233       (while (re-search-forward "^[ \t]+$" nil t)
3234         (unless (gnus-annotation-in-region-p
3235                  (match-beginning 0) (match-end 0))
3236           (replace-match "" nil t)))
3237       ;; Then replace multiple empty lines with a single empty line.
3238       (article-goto-body)
3239       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3240         (unless (gnus-annotation-in-region-p
3241                  (match-beginning 0) (match-end 0))
3242           (delete-region (match-beginning 1) (match-end 1)))))))
3243
3244 (defun article-strip-leading-space ()
3245   "Remove all white space from the beginning of the lines in the article."
3246   (interactive)
3247   (save-excursion
3248     (let ((inhibit-point-motion-hooks t)
3249           (inhibit-read-only t))
3250       (article-goto-body)
3251       (while (re-search-forward "^[ \t]+" nil t)
3252         (replace-match "" t t)))))
3253
3254 (defun article-strip-trailing-space ()
3255   "Remove all white space from the end of the lines in the article."
3256   (interactive)
3257   (save-excursion
3258     (let ((inhibit-point-motion-hooks t)
3259           (inhibit-read-only t))
3260       (article-goto-body)
3261       (while (re-search-forward "[ \t]+$" nil t)
3262         (replace-match "" t t)))))
3263
3264 (defun article-strip-blank-lines ()
3265   "Strip leading, trailing and multiple blank lines."
3266   (interactive)
3267   (article-strip-leading-blank-lines)
3268   (article-remove-trailing-blank-lines)
3269   (article-strip-multiple-blank-lines))
3270
3271 (defun article-strip-all-blank-lines ()
3272   "Strip all blank lines."
3273   (interactive)
3274   (save-excursion
3275     (let ((inhibit-point-motion-hooks t)
3276           (inhibit-read-only t))
3277       (article-goto-body)
3278       (while (re-search-forward "^[ \t]*\n" nil t)
3279         (replace-match "" t t)))))
3280
3281 (defun gnus-article-narrow-to-signature ()
3282   "Narrow to the signature; return t if a signature is found, else nil."
3283   (let ((inhibit-point-motion-hooks t))
3284     (when (gnus-article-search-signature)
3285       (forward-line 1)
3286       ;; Check whether we have some limits to what we consider
3287       ;; to be a signature.
3288       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3289                       (list gnus-signature-limit)))
3290             limit limited)
3291         (while (setq limit (pop limits))
3292           (if (or (and (integerp limit)
3293                        (< (- (point-max) (point)) limit))
3294                   (and (floatp limit)
3295                        (< (count-lines (point) (point-max)) limit))
3296                   (and (functionp limit)
3297                        (funcall limit))
3298                   (and (stringp limit)
3299                        (not (re-search-forward limit nil t))))
3300               ()                        ; This limit did not succeed.
3301             (setq limited t
3302                   limits nil)))
3303         (unless limited
3304           (narrow-to-region (point) (point-max))
3305           t)))))
3306
3307 (defun gnus-article-search-signature ()
3308   "Search the current buffer for the signature separator.
3309 Put point at the beginning of the signature separator."
3310   (let ((cur (point)))
3311     (goto-char (point-max))
3312     (if (if (stringp gnus-signature-separator)
3313             (re-search-backward gnus-signature-separator nil t)
3314           (let ((seps gnus-signature-separator))
3315             (while (and seps
3316                         (not (re-search-backward (car seps) nil t)))
3317               (pop seps))
3318             seps))
3319         t
3320       (goto-char cur)
3321       nil)))
3322
3323 (defun gnus-article-hidden-arg ()
3324   "Return the current prefix arg as a number, or 0 if no prefix."
3325   (list (if current-prefix-arg
3326             (prefix-numeric-value current-prefix-arg)
3327           0)))
3328
3329 (defun gnus-article-check-hidden-text (type arg)
3330   "Return nil if hiding is necessary.
3331 Arg can be nil or a number.  nil and positive means hide, negative
3332 means show, 0 means toggle."
3333   (save-excursion
3334     (save-restriction
3335       (let ((hide (gnus-article-hidden-text-p type)))
3336         (cond
3337          ((or (null arg)
3338               (> arg 0))
3339           nil)
3340          ((< arg 0)
3341           (gnus-article-show-hidden-text type)
3342           t)
3343          (t
3344           (if (eq hide 'hidden)
3345               (progn
3346                 (gnus-article-show-hidden-text type)
3347                 t)
3348             nil)))))))
3349
3350 (defun gnus-article-hidden-text-p (type)
3351   "Say whether the current buffer contains hidden text of type TYPE."
3352   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3353     (while (and pos
3354                 (not (get-text-property pos 'invisible))
3355                 (not (get-text-property pos 'dummy-invisible)))
3356       (setq pos
3357             (text-property-any (1+ pos) (point-max) 'article-type type)))
3358     (if pos
3359         'hidden
3360       nil)))
3361
3362 (defun gnus-article-show-hidden-text (type &optional dummy)
3363   "Show all hidden text of type TYPE.
3364 Originally it is hide instead of DUMMY."
3365   (let ((inhibit-read-only t)
3366         (inhibit-point-motion-hooks t))
3367     (gnus-remove-text-properties-when
3368      'article-type type
3369      (point-min) (point-max)
3370      (cons 'article-type (cons type
3371                                gnus-hidden-properties)))
3372     (gnus-delete-wash-type type)))
3373
3374 (defconst article-time-units
3375   `((year . ,(* 365.25 24 60 60))
3376     (week . ,(* 7 24 60 60))
3377     (day . ,(* 24 60 60))
3378     (hour . ,(* 60 60))
3379     (minute . 60)
3380     (second . 1))
3381   "Mapping from time units to seconds.")
3382
3383 (defun gnus-article-forward-header ()
3384   "Move point to the start of the next header.
3385 If the current header is a continuation header, this can be several
3386 lines forward."
3387   (let ((ended nil))
3388     (while (not ended)
3389       (forward-line 1)
3390       (if (looking-at "[ \t]+[^ \t]")
3391           (forward-line 1)
3392         (setq ended t)))))
3393
3394 (defun article-treat-date ()
3395   (article-date-ut gnus-article-date-headers t))
3396
3397 (defun article-date-ut (&optional type highlight date-position)
3398   "Convert DATE date to TYPE in the current article.
3399 The default type is `ut'.  See `gnus-article-date-headers' for
3400 possible values."
3401   (interactive (list 'ut t))
3402   (let* ((case-fold-search t)
3403          (inhibit-read-only t)
3404          (inhibit-point-motion-hooks t)
3405          (first t)
3406          pos date bface eface)
3407     (save-excursion
3408       (save-restriction
3409         (goto-char (point-min))
3410         (when (re-search-forward "^Date:" nil t)
3411           (setq bface (get-text-property (point-at-bol) 'face)
3412                 eface (get-text-property (1- (point-at-eol)) 'face)))
3413         (goto-char (point-min))
3414         ;; Delete any old Date headers.
3415         (if date-position
3416             (progn
3417               (goto-char date-position)
3418               (setq date (get-text-property (point) 'original-date))
3419               (delete-region (point)
3420                              (progn
3421                                (gnus-article-forward-header)
3422                                (point)))
3423               (article-transform-date date type bface eface))
3424           (while (re-search-forward "^Date:" nil t)
3425             (setq date (get-text-property (match-beginning 0) 'original-date))
3426             (delete-region (point-at-bol) (progn
3427                                             (gnus-article-forward-header)
3428                                             (point))))
3429           (when date
3430             (article-transform-date date type bface eface)))))))
3431
3432 (defun article-transform-date (date type bface eface)
3433   (dolist (this-type (cond
3434                       ((null type)
3435                        (list 'ut))
3436                       ((atom type)
3437                        (list type))
3438                       (t
3439                        type)))
3440     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3441     (forward-line -1)
3442     (beginning-of-line)
3443     (put-text-property (point) (1+ (point))
3444                        'original-date date)
3445     (put-text-property (point) (1+ (point))
3446                        'gnus-date-type this-type)
3447     ;; Do highlighting.
3448     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3449       (put-text-property (match-beginning 1) (1+ (match-end 1))
3450                          'face bface)
3451       (put-text-property (match-beginning 2) (match-end 2)
3452                          'face eface))
3453     (forward-line 1)))
3454
3455 (defun article-make-date-line (date type)
3456   "Return a DATE line of TYPE."
3457   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3458                              combined-lapsed))
3459     (error "Unknown conversion type: %s" type))
3460   (condition-case ()
3461       (let ((time (date-to-time date)))
3462         (cond
3463          ;; Convert to the local timezone.
3464          ((eq type 'local)
3465           (concat "Date: " (message-make-date time)))
3466          ;; Convert to Universal Time.
3467          ((eq type 'ut)
3468           (concat "Date: "
3469                   (substring
3470                    (message-make-date
3471                     (let* ((e (parse-time-string date))
3472                            (tm (apply 'encode-time e))
3473                            (ms (car tm))
3474                            (ls (- (cadr tm) (car (current-time-zone time)))))
3475                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3476                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3477                             (t (list ms ls)))))
3478                    0 -5)
3479                   "UT"))
3480          ;; Get the original date from the article.
3481          ((eq type 'original)
3482           (concat "Date: " (if (string-match "\n+$" date)
3483                                (substring date 0 (match-beginning 0))
3484                              date)))
3485          ;; Let the user define the format.
3486          ((eq type 'user-defined)
3487           (let ((format (or (condition-case nil
3488                                 (with-current-buffer gnus-summary-buffer
3489                                   gnus-article-time-format)
3490                               (error nil))
3491                             gnus-article-time-format)))
3492             (if (functionp format)
3493                 (funcall format time)
3494               (concat "Date: " (format-time-string format time)))))
3495          ;; ISO 8601.
3496          ((eq type 'iso8601)
3497           (let ((tz (car (current-time-zone time))))
3498             (concat
3499              "Date: "
3500              (format-time-string "%Y%m%dT%H%M%S" time)
3501              (format "%s%02d%02d"
3502                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3503                      (/ (% (abs tz) 3600) 60)))))
3504          ;; Do a lapsed format.
3505          ((eq type 'lapsed)
3506           (concat "Date: " (article-lapsed-string time)))
3507          ;; A combined date/lapsed format.
3508          ((eq type 'combined-lapsed)
3509           (let ((date-string (article-make-date-line date 'original))
3510                 (segments 3)
3511                 lapsed-string)
3512             (while (and
3513                     (setq lapsed-string
3514                           (concat " (" (article-lapsed-string time segments) ")"))
3515                     (> (+ (length date-string)
3516                           (length lapsed-string))
3517                        (+ fill-column 6))
3518                     (> segments 0))
3519               (setq segments (1- segments)))
3520             (if (> segments 0)
3521                 (concat date-string lapsed-string)
3522               date-string)))
3523          ;; Display the date in proper English
3524          ((eq type 'english)
3525           (let ((dtime (decode-time time)))
3526             (concat
3527              "Date: the "
3528              (number-to-string (nth 3 dtime))
3529              (let ((digit (% (nth 3 dtime) 10)))
3530                (cond
3531                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3532                 ((= digit 1) "st")
3533                 ((= digit 2) "nd")
3534                 ((= digit 3) "rd")
3535                 (t "th")))
3536              " of "
3537              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3538              " "
3539              (number-to-string (nth 5 dtime))
3540              " at "
3541              (format "%02d" (nth 2 dtime))
3542              ":"
3543              (format "%02d" (nth 1 dtime)))))))
3544     (foo
3545      (format "Date: %s (from Gnus)" date))))
3546
3547 (defun article-lapsed-string (time &optional max-segments)
3548   ;; If the date is seriously mangled, the timezone functions are
3549   ;; liable to bug out, so we ignore all errors.
3550   (let* ((now (current-time))
3551          (real-time (subtract-time now time))
3552          (real-sec (and real-time
3553                         (+ (* (float (car real-time)) 65536)
3554                            (cadr real-time))))
3555          (sec (and real-time (abs real-sec)))
3556          (segments 0)
3557          num prev)
3558     (unless max-segments
3559       (setq max-segments (length article-time-units)))
3560     (cond
3561      ((null real-time)
3562       "Unknown")
3563      ((zerop sec)
3564       "Now")
3565      (t
3566       (concat
3567        ;; This is a bit convoluted, but basically we go
3568        ;; through the time units for years, weeks, etc,
3569        ;; and divide things to see whether that results
3570        ;; in positive answers.
3571        (mapconcat
3572         (lambda (unit)
3573           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3574                   (>= segments max-segments))
3575               ;; The (remaining) seconds are too few to
3576               ;; be divided into this time unit.
3577               ""
3578             ;; It's big enough, so we output it.
3579             (setq sec (- sec (* num (cdr unit))))
3580             (prog1
3581                 (concat (if prev ", " "") (int-to-string
3582                                            (floor num))
3583                         " " (symbol-name (car unit))
3584                         (if (> num 1) "s" ""))
3585               (setq prev t
3586                     segments (1+ segments)))))
3587         article-time-units "")
3588        ;; If dates are odd, then it might appear like the
3589        ;; article was sent in the future.
3590        (if (> real-sec 0)
3591            " ago"
3592          " in the future"))))))
3593
3594 (defun article-date-local (&optional highlight)
3595   "Convert the current article date to the local timezone."
3596   (interactive (list t))
3597   (article-date-ut 'local highlight))
3598
3599 (defun article-date-english (&optional highlight)
3600   "Convert the current article date to something that is proper English."
3601   (interactive (list t))
3602   (article-date-ut 'english highlight))
3603
3604 (defun article-date-original (&optional highlight)
3605   "Convert the current article date to what it was originally.
3606 This is only useful if you have used some other date conversion
3607 function and want to see what the date was before converting."
3608   (interactive (list t))
3609   (article-date-ut 'original highlight))
3610
3611 (defun article-date-lapsed (&optional highlight)
3612   "Convert the current article date to time lapsed since it was sent."
3613   (interactive (list t))
3614   (article-date-ut 'lapsed highlight))
3615
3616 (defun article-date-combined-lapsed (&optional highlight)
3617   "Convert the current article date to time lapsed since it was sent."
3618   (interactive (list t))
3619   (article-date-ut 'combined-lapsed highlight))
3620
3621 (defun article-update-date-lapsed ()
3622   "Function to be run from a timer to update the lapsed time line."
3623   (save-match-data
3624     (let ((buffer (current-buffer)))
3625       (ignore-errors
3626         (walk-windows
3627          (lambda (w)
3628            (set-buffer (window-buffer w))
3629            (when (eq major-mode 'gnus-article-mode)
3630              (let ((old-line (count-lines (point-min) (point)))
3631                    (old-column (- (point) (line-beginning-position)))
3632                    (window-start
3633                     (window-start (get-buffer-window (current-buffer)))))
3634                (goto-char (point-min))
3635                (while (re-search-forward "^Date:" nil t)
3636                  (let ((type (get-text-property (match-beginning 0)
3637                                                 'gnus-date-type)))
3638                    (when (memq type '(lapsed combined-lapsed user-format))
3639                      (unless (= window-start
3640                                 (save-excursion
3641                                   (forward-line 1)
3642                                   (point)))
3643                        (setq window-start nil))
3644                      (save-excursion
3645                        (article-date-ut type t (match-beginning 0)))
3646                      (forward-line 1)
3647                      (when window-start
3648                        (set-window-start (get-buffer-window (current-buffer))
3649                                          (point))))))
3650                (goto-char (point-min))
3651                (when (> old-column 0)
3652                  (setq old-line (1- old-line)))
3653                (forward-line old-line)
3654                (end-of-line)
3655                (when (> (current-column) old-column)
3656                  (beginning-of-line)
3657                  (forward-char old-column)))))
3658          nil 'visible))
3659       (set-buffer buffer))))
3660
3661 (defun gnus-start-date-timer (&optional n)
3662   "Start a timer to update the Date headers in the article buffers.
3663 The numerical prefix says how frequently (in seconds) the function
3664 is to run."
3665   (interactive "p")
3666   (unless n
3667     (setq n 1))
3668   (gnus-stop-date-timer)
3669   (setq article-lapsed-timer
3670         (run-at-time 1 n 'article-update-date-lapsed)))
3671
3672 (defun gnus-stop-date-timer ()
3673   "Stop the Date timer."
3674   (interactive)
3675   (when article-lapsed-timer
3676     (nnheader-cancel-timer article-lapsed-timer)
3677     (setq article-lapsed-timer nil)))
3678
3679 (defun article-date-user (&optional highlight)
3680   "Convert the current article date to the user-defined format.
3681 This format is defined by the `gnus-article-time-format' variable."
3682   (interactive (list t))
3683   (article-date-ut 'user highlight))
3684
3685 (defun article-date-iso8601 (&optional highlight)
3686   "Convert the current article date to ISO8601."
3687   (interactive (list t))
3688   (article-date-ut 'iso8601 highlight))
3689
3690 (defmacro gnus-article-save-original-date (&rest forms)
3691   "Save the original date as a text property and evaluate FORMS."
3692   `(let* ((case-fold-search t)
3693           (start (progn
3694                    (goto-char (point-min))
3695                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3696                               (not (bolp)))
3697                      (match-end 0))))
3698           (date (when (and start
3699                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3700                                               nil t))
3701                   (buffer-substring-no-properties start
3702                                                   (match-beginning 0)))))
3703      (goto-char (point-max))
3704      (skip-chars-backward "\n")
3705      (put-text-property (point-min) (point) 'original-date date)
3706      ,@forms
3707      (goto-char (point-max))
3708      (skip-chars-backward "\n")
3709      (put-text-property (point-min) (point) 'original-date date)))
3710
3711 ;; (defun article-show-all ()
3712 ;;   "Show all hidden text in the article buffer."
3713 ;;   (interactive)
3714 ;;   (save-excursion
3715 ;;     (let ((inhibit-read-only t))
3716 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3717
3718 (defun article-remove-leading-whitespace ()
3719   "Remove excessive whitespace from all headers."
3720   (interactive)
3721   (save-excursion
3722     (save-restriction
3723       (let ((inhibit-read-only t))
3724         (article-narrow-to-head)
3725         (goto-char (point-min))
3726         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3727           (delete-region (match-beginning 1) (match-end 1)))))))
3728
3729 (defun article-emphasize (&optional arg)
3730   "Emphasize text according to `gnus-emphasis-alist'."
3731   (interactive (gnus-article-hidden-arg))
3732   (unless (gnus-article-check-hidden-text 'emphasis arg)
3733     (save-excursion
3734       (let ((alist (or
3735                     (condition-case nil
3736                         (with-current-buffer gnus-summary-buffer
3737                           gnus-article-emphasis-alist)
3738                       (error))
3739                     gnus-emphasis-alist))
3740             (inhibit-read-only t)
3741             (props (append '(article-type emphasis)
3742                            gnus-hidden-properties))
3743             regexp elem beg invisible visible face)
3744         (article-goto-body)
3745         (setq beg (point))
3746         (while (setq elem (pop alist))
3747           (goto-char beg)
3748           (setq regexp (car elem)
3749                 invisible (nth 1 elem)
3750                 visible (nth 2 elem)
3751                 face (nth 3 elem))
3752           (while (re-search-forward regexp nil t)
3753             (when (and (match-beginning visible) (match-beginning invisible))
3754               (gnus-article-hide-text
3755                (match-beginning invisible) (match-end invisible) props)
3756               (gnus-article-unhide-text-type
3757                (match-beginning visible) (match-end visible) 'emphasis)
3758               (gnus-put-overlay-excluding-newlines
3759                (match-beginning visible) (match-end visible) 'face face)
3760               (gnus-add-wash-type 'emphasis)
3761               (goto-char (match-end invisible)))))))))
3762
3763 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3764   "Setup newsgroup emphasis alist."
3765   (unless gnus-article-emphasis-alist
3766     (let ((name (and gnus-newsgroup-name
3767                      (gnus-group-real-name gnus-newsgroup-name))))
3768       (make-local-variable 'gnus-article-emphasis-alist)
3769       (setq gnus-article-emphasis-alist
3770             (nconc
3771              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3772                (while (setq elem (pop alist))
3773                  (when (and name (string-match (car elem) name))
3774                    (setq alist nil
3775                          highlight (copy-sequence (cdr elem)))))
3776                highlight)
3777              (copy-sequence highlight-words)
3778              (if gnus-newsgroup-name
3779                  (copy-sequence (gnus-group-find-parameter
3780                                  gnus-newsgroup-name 'highlight-words t)))
3781              gnus-emphasis-alist)))))
3782
3783 (defvar gnus-summary-article-menu)
3784 (defvar gnus-summary-post-menu)
3785
3786 ;;; Saving functions.
3787
3788 (defun gnus-article-save (save-buffer file &optional num)
3789   "Save the currently selected article."
3790   (when (or (get gnus-default-article-saver :headers)
3791             (not gnus-save-all-headers))
3792     ;; Remove headers according to `gnus-saved-headers' or the value
3793     ;; of the `:headers' property that the saver function might have.
3794     (let ((gnus-visible-headers
3795            (or (symbol-value (get gnus-default-article-saver :headers))
3796                gnus-saved-headers gnus-visible-headers))
3797           ;; Ignore group parameter.  See `article-hide-headers'.
3798           (gnus-summary-buffer nil))
3799       (with-current-buffer save-buffer
3800         (article-hide-headers 1 t))))
3801   (save-window-excursion
3802     (if (not gnus-default-article-saver)
3803         (error "No default saver is defined")
3804       ;; !!! Magic!  The saving functions all save
3805       ;; `gnus-save-article-buffer' (or so they think), but we
3806       ;; bind that variable to our save-buffer.
3807       (set-buffer gnus-article-buffer)
3808       (let* ((gnus-save-article-buffer save-buffer)
3809              (filename
3810               (cond
3811                ((not gnus-prompt-before-saving) 'default)
3812                ((eq gnus-prompt-before-saving 'always) nil)
3813                (t file)))
3814              (gnus-number-of-articles-to-be-saved
3815               (when (eq gnus-prompt-before-saving t)
3816                 num)))                  ; Magic
3817         (set-buffer gnus-article-current-summary)
3818         (funcall gnus-default-article-saver filename)))))
3819
3820 (defun gnus-read-save-file-name (prompt &optional filename
3821                                         function group headers variable
3822                                         dir-var)
3823   (let ((default-name
3824           (funcall function group headers (symbol-value variable)))
3825         result)
3826     (setq result
3827           (expand-file-name
3828            (cond
3829             ((eq filename 'default)
3830              default-name)
3831             ((eq filename t)
3832              default-name)
3833             (filename filename)
3834             (t
3835              (when (symbol-value dir-var)
3836                (setq default-name (expand-file-name
3837                                    (file-name-nondirectory default-name)
3838                                    (symbol-value dir-var))))
3839              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3840                     (prompt
3841                      (format prompt
3842                              (if (and gnus-number-of-articles-to-be-saved
3843                                       (> gnus-number-of-articles-to-be-saved 1))
3844                                  (format "these %d articles"
3845                                          gnus-number-of-articles-to-be-saved)
3846                                "this article")))
3847                     (file
3848                      ;; Let the split methods have their say.
3849                      (cond
3850                       ;; No split name was found.
3851                       ((null split-name)
3852                        (read-file-name
3853                         (concat prompt " (default "
3854                                 (file-name-nondirectory default-name) "): ")
3855                         (file-name-directory default-name)
3856                         default-name))
3857                       ;; A single group name is returned.
3858                       ((stringp split-name)
3859                        (setq default-name
3860                              (funcall function split-name headers
3861                                       (symbol-value variable)))
3862                        (read-file-name
3863                         (concat prompt " (default "
3864                                 (file-name-nondirectory default-name) "): ")
3865                         (file-name-directory default-name)
3866                         default-name))
3867                       ;; A single split name was found
3868                       ((= 1 (length split-name))
3869                        (let* ((name (expand-file-name
3870                                      (car split-name)
3871                                      gnus-article-save-directory))
3872                               (dir (cond ((file-directory-p name)
3873                                           (file-name-as-directory name))
3874                                          ((file-exists-p name) name)
3875                                          (t gnus-article-save-directory))))
3876                          (read-file-name
3877                           (concat prompt " (default " name "): ")
3878                           dir name)))
3879                       ;; A list of splits was found.
3880                       (t
3881                        (setq split-name (nreverse split-name))
3882                        (let (result)
3883                          (let ((file-name-history
3884                                 (nconc split-name file-name-history)))
3885                            (setq result
3886                                  (expand-file-name
3887                                   (read-file-name
3888                                    (concat prompt " (`M-p' for defaults): ")
3889                                    gnus-article-save-directory
3890                                    (car split-name))
3891                                   gnus-article-save-directory)))
3892                          (car (push result file-name-history)))))))
3893                ;; Create the directory.
3894                (gnus-make-directory (file-name-directory file))
3895                ;; If we have read a directory, we append the default file name.
3896                (when (file-directory-p file)
3897                  (setq file (expand-file-name (file-name-nondirectory
3898                                                default-name)
3899                                               (file-name-as-directory file))))
3900                ;; Possibly translate some characters.
3901                (nnheader-translate-file-chars file))))))
3902     (gnus-make-directory (file-name-directory result))
3903     (when variable
3904       (set variable result))
3905     (when dir-var
3906       (set dir-var (file-name-directory result)))
3907     result))
3908
3909 (defun gnus-article-archive-name (group)
3910   "Return the first instance of an \"Archive-name\" in the current buffer."
3911   (let ((case-fold-search t))
3912     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3913       (nnheader-concat gnus-article-save-directory
3914                        (match-string 1)))))
3915
3916 (defun gnus-article-nndoc-name (group)
3917   "If GROUP is an nndoc group, return the name of the parent group."
3918   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3919     (gnus-group-get-parameter group 'save-article-group)))
3920
3921 (defun gnus-summary-save-in-rmail (&optional filename)
3922   "Append this article to Rmail file.
3923 Optional argument FILENAME specifies file name.
3924 Directory to save to is default to `gnus-article-save-directory'."
3925   (setq filename (gnus-read-save-file-name
3926                   "Save %s in rmail file" filename
3927                   gnus-rmail-save-name gnus-newsgroup-name
3928                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3929   (with-current-buffer gnus-save-article-buffer
3930     (save-excursion
3931       (save-restriction
3932         (widen)
3933         ;; Note that unlike gnus-summary-save-in-mail, there is no
3934         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3935         ;; not use Babyl.
3936         (gnus-output-to-rmail filename))))
3937   filename)
3938
3939 (defun gnus-summary-save-in-mail (&optional filename)
3940   "Append this article to Unix mail file.
3941 Optional argument FILENAME specifies file name.
3942 Directory to save to is default to `gnus-article-save-directory'."
3943   (setq filename (gnus-read-save-file-name
3944                   "Save %s in Unix mail file" filename
3945                   gnus-mail-save-name gnus-newsgroup-name
3946                   gnus-current-headers 'gnus-newsgroup-last-mail))
3947   (with-current-buffer gnus-save-article-buffer
3948     (save-excursion
3949       (save-restriction
3950         (widen)
3951         (if (and (file-readable-p filename)
3952                  (file-regular-p filename)
3953                  (mail-file-babyl-p filename))
3954             (gnus-output-to-rmail filename)
3955           (gnus-output-to-mail filename)))))
3956   filename)
3957
3958 (put 'gnus-summary-save-in-file :decode t)
3959 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3960 (defun gnus-summary-save-in-file (&optional filename overwrite)
3961   "Append this article to file.
3962 Optional argument FILENAME specifies file name.
3963 Directory to save to is default to `gnus-article-save-directory'."
3964   (setq filename (gnus-read-save-file-name
3965                   "Save %s in file" filename
3966                   gnus-file-save-name gnus-newsgroup-name
3967                   gnus-current-headers 'gnus-newsgroup-last-file))
3968   (with-current-buffer gnus-save-article-buffer
3969     (save-excursion
3970       (save-restriction
3971         (widen)
3972         (when (and overwrite
3973                    (file-exists-p filename))
3974           (delete-file filename))
3975         (gnus-output-to-file filename))))
3976   filename)
3977
3978 (put 'gnus-summary-write-to-file :decode t)
3979 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
3980 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
3981 (defun gnus-summary-write-to-file (&optional filename)
3982   "Write this article to a file, overwriting it if the file exists.
3983 Optional argument FILENAME specifies file name.
3984 The directory to save in defaults to `gnus-article-save-directory'."
3985   (setq filename (gnus-read-save-file-name
3986                   "Save %s in file" filename
3987                   gnus-file-save-name gnus-newsgroup-name
3988                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3989   (gnus-summary-save-in-file filename t))
3990
3991 (put 'gnus-summary-save-body-in-file :decode t)
3992 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
3993   "Append this article body to a file.
3994 Optional argument FILENAME specifies file name.
3995 The directory to save in defaults to `gnus-article-save-directory'."
3996   (setq filename (gnus-read-save-file-name
3997                   "Save %s body in file" filename
3998                   gnus-file-save-name gnus-newsgroup-name
3999                   gnus-current-headers 'gnus-newsgroup-last-file))
4000   (with-current-buffer gnus-save-article-buffer
4001     (save-excursion
4002       (save-restriction
4003         (widen)
4004         (when (article-goto-body)
4005           (narrow-to-region (point) (point-max)))
4006         (when (and overwrite
4007                    (file-exists-p filename))
4008           (delete-file filename))
4009         (gnus-output-to-file filename))))
4010   filename)
4011
4012 (put 'gnus-summary-write-body-to-file :decode t)
4013 (put 'gnus-summary-write-body-to-file
4014      :function 'gnus-summary-save-body-in-file)
4015 (defun gnus-summary-write-body-to-file (&optional filename)
4016   "Write this article body to a file, overwriting it if the file exists.
4017 Optional argument FILENAME specifies file name.
4018 The directory to save in defaults to `gnus-article-save-directory'."
4019   (setq filename (gnus-read-save-file-name
4020                   "Save %s body in file" filename
4021                   gnus-file-save-name gnus-newsgroup-name
4022                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4023   (gnus-summary-save-body-in-file filename t))
4024
4025 (put 'gnus-summary-save-in-pipe :decode t)
4026 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4027 (defun gnus-summary-save-in-pipe (&optional command raw)
4028   "Pipe this article to subprocess COMMAND.
4029 Valid values for COMMAND include:
4030   a string
4031     The executable command name and possibly arguments.
4032   nil
4033     You will be prompted for the command in the minibuffer.
4034   the symbol `default'
4035     It will be replaced with the command which the variable
4036     `gnus-summary-pipe-output-default-command' holds or the command
4037     last used for saving.
4038 Non-nil value for RAW overrides `:decode' and `:headers' properties
4039 and the raw article including all headers will be piped."
4040   (let ((article (gnus-summary-article-number))
4041         (decode (unless raw
4042                   (get 'gnus-summary-save-in-pipe :decode)))
4043         save-buffer default)
4044     (if article
4045         (if (vectorp (gnus-summary-article-header article))
4046             (save-current-buffer
4047               (gnus-summary-select-article decode decode nil article)
4048               (insert-buffer-substring
4049                (prog1
4050                    (if decode
4051                        gnus-article-buffer
4052                      gnus-original-article-buffer)
4053                  (setq save-buffer
4054                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4055               ;; Remove unwanted headers.
4056               (when (and (not raw)
4057                          (or (get 'gnus-summary-save-in-pipe :headers)
4058                              (not gnus-save-all-headers)))
4059                 (let ((gnus-visible-headers
4060                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4061                                               :headers))
4062                            gnus-saved-headers gnus-visible-headers))
4063                       (gnus-summary-buffer nil))
4064                   (article-hide-headers 1 t))))
4065           (error "%d is not a real article" article))
4066       (error "No article to pipe"))
4067     (setq default (or gnus-summary-pipe-output-default-command
4068                       gnus-last-shell-command))
4069     (unless (stringp command)
4070       (setq command
4071             (if (and (eq command 'default) default)
4072                 default
4073               (gnus-read-shell-command "Shell command on this article: "
4074                                        default))))
4075     (when (string-equal command "")
4076       (if default
4077           (setq command default)
4078         (error "A command is required")))
4079     (with-current-buffer save-buffer
4080       (save-restriction
4081         (widen)
4082         (shell-command-on-region (point-min) (point-max) command nil)))
4083     (gnus-kill-buffer save-buffer))
4084   (setq gnus-summary-pipe-output-default-command command))
4085
4086 (defun gnus-summary-pipe-to-muttprint (&optional command)
4087   "Pipe this article to muttprint."
4088   (unless (stringp command)
4089     (setq command (read-string
4090                    "Print using command: " gnus-summary-muttprint-program
4091                    nil gnus-summary-muttprint-program)))
4092   (let ((gnus-summary-pipe-output-default-command
4093          gnus-summary-pipe-output-default-command))
4094     (gnus-summary-save-in-pipe command))
4095   (setq gnus-summary-muttprint-program command))
4096
4097 ;;; Article file names when saving.
4098
4099 (defun gnus-capitalize-newsgroup (newsgroup)
4100   "Capitalize NEWSGROUP name."
4101   (when (not (zerop (length newsgroup)))
4102     (concat (char-to-string (upcase (aref newsgroup 0)))
4103             (substring newsgroup 1))))
4104
4105 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4106   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4107 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4108 Otherwise, it is like ~/News/news/group/num."
4109   (let ((default
4110           (expand-file-name
4111            (concat (if (gnus-use-long-file-name 'not-save)
4112                        (gnus-capitalize-newsgroup newsgroup)
4113                      (gnus-newsgroup-directory-form newsgroup))
4114                    "/" (int-to-string (mail-header-number headers)))
4115            gnus-article-save-directory)))
4116     (if (and last-file
4117              (string-equal (file-name-directory default)
4118                            (file-name-directory last-file))
4119              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4120         default
4121       (or last-file default))))
4122
4123 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4124   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4125 If variable `gnus-use-long-file-name' is non-nil, it is
4126 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4127   (let ((default
4128           (expand-file-name
4129            (concat (if (gnus-use-long-file-name 'not-save)
4130                        newsgroup
4131                      (gnus-newsgroup-directory-form newsgroup))
4132                    "/" (int-to-string (mail-header-number headers)))
4133            gnus-article-save-directory)))
4134     (if (and last-file
4135              (string-equal (file-name-directory default)
4136                            (file-name-directory last-file))
4137              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4138         default
4139       (or last-file default))))
4140
4141 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4142   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4143 If variable `gnus-use-long-file-name' is non-nil, it is
4144 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4145   (or last-file
4146       (expand-file-name
4147        (if (gnus-use-long-file-name 'not-save)
4148            newsgroup
4149          (file-relative-name
4150           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4151           default-directory))
4152        gnus-article-save-directory)))
4153
4154 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4155   "Generate file name from sender."
4156   (let ((from (mail-header-from headers)))
4157     (expand-file-name
4158      (if (and from (string-match "\\([^ <]+\\)@" from))
4159          (match-string 1 from)
4160        "nobody")
4161      gnus-article-save-directory)))
4162
4163 (defun article-verify-x-pgp-sig ()
4164   "Verify X-PGP-Sig."
4165   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4166   (interactive)
4167   (if (gnus-buffer-live-p gnus-original-article-buffer)
4168       (let ((sig (with-current-buffer gnus-original-article-buffer
4169                    (gnus-fetch-field "X-PGP-Sig")))
4170             items info headers)
4171         (when (and sig
4172                    mml2015-use
4173                    (mml2015-clear-verify-function))
4174           (with-temp-buffer
4175             (insert-buffer-substring gnus-original-article-buffer)
4176             (setq items (split-string sig))
4177             (message-narrow-to-head)
4178             (let ((inhibit-point-motion-hooks t)
4179                   (case-fold-search t))
4180               ;; Don't verify multiple headers.
4181               (setq headers (mapconcat (lambda (header)
4182                                          (concat header ": "
4183                                                  (mail-fetch-field header)
4184                                                  "\n"))
4185                                        (split-string (nth 1 items) ",") "")))
4186             (delete-region (point-min) (point-max))
4187             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4188             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4189             (insert headers)
4190             (widen)
4191             (forward-line)
4192             (while (not (eobp))
4193               (if (looking-at "^-")
4194                   (insert "- "))
4195               (forward-line))
4196             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4197             (insert "Version: " (car items) "\n\n")
4198             (insert (mapconcat 'identity (cddr items) "\n"))
4199             (insert "\n-----END PGP SIGNATURE-----\n")
4200             (let ((mm-security-handle (list (format "multipart/signed"))))
4201               (mml2015-clean-buffer)
4202               (let ((coding-system-for-write (or gnus-newsgroup-charset
4203                                                  'iso-8859-1)))
4204                 (funcall (mml2015-clear-verify-function)))
4205               (setq info
4206                     (or (mm-handle-multipart-ctl-parameter
4207                          mm-security-handle 'gnus-details)
4208                         (mm-handle-multipart-ctl-parameter
4209                          mm-security-handle 'gnus-info)))))
4210           (when info
4211             (let ((inhibit-read-only t) bface eface)
4212               (save-restriction
4213                 (message-narrow-to-head)
4214                 (goto-char (point-max))
4215                 (forward-line -1)
4216                 (setq bface (get-text-property (point-at-bol) 'face)
4217                       eface (get-text-property (1- (point-at-eol)) 'face))
4218                 (message-remove-header "X-Gnus-PGP-Verify")
4219                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4220                     (forward-line)
4221                   (goto-char (point-max)))
4222                 (narrow-to-region (point) (point))
4223                 (insert "X-Gnus-PGP-Verify: " info "\n")
4224                 (goto-char (point-min))
4225                 (forward-line)
4226                 (while (not (eobp))
4227                   (if (not (looking-at "^[ \t]"))
4228                       (insert " "))
4229                   (forward-line))
4230                 ;; Do highlighting.
4231                 (goto-char (point-min))
4232                 (when (looking-at "\\([^:]+\\): *")
4233                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4234                                      'face bface)
4235                   (put-text-property (match-end 0) (point-max)
4236                                      'face eface)))))))))
4237
4238 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4239
4240 (defun article-verify-cancel-lock ()
4241   "Verify Cancel-Lock header."
4242   (interactive)
4243   (if (gnus-buffer-live-p gnus-original-article-buffer)
4244       (canlock-verify gnus-original-article-buffer)))
4245
4246 (eval-and-compile
4247   (mapc
4248    (lambda (func)
4249      (let (afunc gfunc)
4250        (if (consp func)
4251            (setq afunc (car func)
4252                  gfunc (cdr func))
4253          (setq afunc func
4254                gfunc (intern (format "gnus-%s" func))))
4255        (defalias gfunc
4256          (when (fboundp afunc)
4257            `(lambda (&optional interactive &rest args)
4258               ,(documentation afunc t)
4259               (interactive (list t))
4260               (with-current-buffer gnus-article-buffer
4261                 (if interactive
4262                     (call-interactively ',afunc)
4263                   (apply ',afunc args))))))))
4264    '(article-hide-headers
4265      article-verify-x-pgp-sig
4266      article-verify-cancel-lock
4267      article-hide-boring-headers
4268      article-treat-overstrike
4269      article-treat-ansi-sequences
4270      article-fill-long-lines
4271      article-capitalize-sentences
4272      article-remove-cr
4273      article-remove-leading-whitespace
4274      article-display-x-face
4275      article-display-face
4276      article-de-quoted-unreadable
4277      article-de-base64-unreadable
4278      article-decode-HZ
4279      article-wash-html
4280      article-unsplit-urls
4281      article-hide-list-identifiers
4282      article-strip-banner
4283      article-babel
4284      article-hide-pem
4285      article-hide-signature
4286      article-strip-headers-in-body
4287      article-remove-trailing-blank-lines
4288      article-strip-leading-blank-lines
4289      article-strip-multiple-blank-lines
4290      article-strip-leading-space
4291      article-strip-trailing-space
4292      article-strip-blank-lines
4293      article-strip-all-blank-lines
4294      article-date-local
4295      article-date-english
4296      article-date-iso8601
4297      article-date-original
4298      article-treat-date
4299      article-date-ut
4300      article-decode-mime-words
4301      article-decode-charset
4302      article-decode-encoded-words
4303      article-date-user
4304      article-date-lapsed
4305      article-date-combined-lapsed
4306      article-emphasize
4307      article-treat-dumbquotes
4308      article-treat-non-ascii
4309      article-normalize-headers
4310      ;;(article-show-all . gnus-article-show-all-headers)
4311      )))
4312 \f
4313 ;;;
4314 ;;; Gnus article mode
4315 ;;;
4316
4317 (put 'gnus-article-mode 'mode-class 'special)
4318
4319 (set-keymap-parent gnus-article-mode-map widget-keymap)
4320
4321 (gnus-define-keys gnus-article-mode-map
4322   " " gnus-article-goto-next-page
4323   "\177" gnus-article-goto-prev-page
4324   [delete] gnus-article-goto-prev-page
4325   [backspace] gnus-article-goto-prev-page
4326   "\C-c^" gnus-article-refer-article
4327   "h" gnus-article-show-summary
4328   "s" gnus-article-show-summary
4329   "\C-c\C-m" gnus-article-mail
4330   "?" gnus-article-describe-briefly
4331   "<" beginning-of-buffer
4332   ">" end-of-buffer
4333   "\C-c\C-i" gnus-info-find-node
4334   "\C-c\C-b" gnus-bug
4335   "R" gnus-article-reply-with-original
4336   "F" gnus-article-followup-with-original
4337   "\C-hk" gnus-article-describe-key
4338   "\C-hc" gnus-article-describe-key-briefly
4339   "\C-hb" gnus-article-describe-bindings
4340
4341   "e" gnus-article-read-summary-keys
4342   "\C-d" gnus-article-read-summary-keys
4343   "\M-*" gnus-article-read-summary-keys
4344   "\M-#" gnus-article-read-summary-keys
4345   "\M-^" gnus-article-read-summary-keys
4346   "\M-g" gnus-article-read-summary-keys)
4347
4348 (substitute-key-definition
4349  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4350
4351 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4352   "W" gnus-article-wide-reply-with-original)
4353 (if (featurep 'xemacs)
4354     (set-keymap-default-binding gnus-article-send-map
4355                                 'gnus-article-read-summary-send-keys)
4356   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4357
4358 (defun gnus-article-make-menu-bar ()
4359   (unless (boundp 'gnus-article-commands-menu)
4360     (gnus-summary-make-menu-bar))
4361   (unless (boundp 'gnus-article-article-menu)
4362     (easy-menu-define
4363      gnus-article-article-menu gnus-article-mode-map ""
4364      '("Article"
4365        ["Scroll forwards" gnus-article-goto-next-page t]
4366        ["Scroll backwards" gnus-article-goto-prev-page t]
4367        ["Show summary" gnus-article-show-summary t]
4368        ["Fetch Message-ID at point" gnus-article-refer-article t]
4369        ["Mail to address at point" gnus-article-mail t]
4370        ["Send a bug report" gnus-bug t]))
4371
4372     (easy-menu-define
4373      gnus-article-treatment-menu gnus-article-mode-map ""
4374      ;; Fixme: this should use :active (and maybe :visible).
4375      '("Treatment"
4376        ["Hide headers" gnus-article-hide-headers t]
4377        ["Hide signature" gnus-article-hide-signature t]
4378        ["Hide citation" gnus-article-hide-citation t]
4379        ["Treat overstrike" gnus-article-treat-overstrike t]
4380        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4381        ["Remove carriage return" gnus-article-remove-cr t]
4382        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4383        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4384        ["Remove base64" gnus-article-de-base64-unreadable t]
4385        ["Treat html" gnus-article-wash-html t]
4386        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4387        ["Decode HZ" gnus-article-decode-HZ t]))
4388
4389     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4390
4391     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4392
4393     (gnus-run-hooks 'gnus-article-menu-hook)))
4394
4395 (defvar bookmark-make-record-function)
4396
4397 (defun gnus-article-mode ()
4398   "Major mode for displaying an article.
4399
4400 All normal editing commands are switched off.
4401
4402 The following commands are available in addition to all summary mode
4403 commands:
4404 \\<gnus-article-mode-map>
4405 \\[gnus-article-next-page]\t Scroll the article one page forwards
4406 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4407 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4408 \\[gnus-article-show-summary]\t Display the summary buffer
4409 \\[gnus-article-mail]\t Send a reply to the address near point
4410 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4411 \\[gnus-info-find-node]\t Go to the Gnus info node"
4412   (interactive)
4413   (kill-all-local-variables)
4414   (gnus-simplify-mode-line)
4415   (setq mode-name "Article")
4416   (setq major-mode 'gnus-article-mode)
4417   (make-local-variable 'minor-mode-alist)
4418   (use-local-map gnus-article-mode-map)
4419   (when (gnus-visual-p 'article-menu 'menu)
4420     (gnus-article-make-menu-bar)
4421     (when gnus-summary-tool-bar-map
4422       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4423   (gnus-update-format-specifications nil 'article-mode)
4424   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4425   (set (make-local-variable 'gnus-page-broken) nil)
4426   (make-local-variable 'gnus-article-current-summary)
4427   (make-local-variable 'gnus-article-mime-handles)
4428   (make-local-variable 'gnus-article-decoded-p)
4429   (make-local-variable 'gnus-article-mime-handle-alist)
4430   (make-local-variable 'gnus-article-wash-types)
4431   (make-local-variable 'gnus-article-image-alist)
4432   (make-local-variable 'gnus-article-charset)
4433   (make-local-variable 'gnus-article-ignored-charsets)
4434   (set (make-local-variable 'bookmark-make-record-function)
4435        'gnus-summary-bookmark-make-record)
4436   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4437   ;; face.
4438   (set (make-local-variable 'nobreak-char-display) nil)
4439   (setq cursor-in-non-selected-windows nil)
4440   (gnus-set-default-directory)
4441   (buffer-disable-undo)
4442   (setq buffer-read-only t
4443         show-trailing-whitespace nil)
4444   (set-syntax-table gnus-article-mode-syntax-table)
4445   (mm-enable-multibyte)
4446   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4447
4448 (defun gnus-article-setup-buffer ()
4449   "Initialize the article buffer."
4450   (let* ((name (if gnus-single-article-buffer "*Article*"
4451                  (concat "*Article " gnus-newsgroup-name "*")))
4452          (original
4453           (progn (string-match "\\*Article" name)
4454                  (concat " *Original Article"
4455                          (substring name (match-end 0))))))
4456     (setq gnus-article-buffer name)
4457     (setq gnus-original-article-buffer original)
4458     (setq gnus-article-mime-handle-alist nil)
4459     (with-current-buffer gnus-summary-buffer
4460       ;; This might be a variable local to the summary buffer.
4461       (unless gnus-single-article-buffer
4462         (setq gnus-article-buffer name)
4463         (setq gnus-original-article-buffer original)
4464         (gnus-set-global-variables)))
4465     (gnus-article-setup-highlight-words)
4466     ;; Init original article buffer.
4467     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4468       (mm-enable-multibyte)
4469       (setq major-mode 'gnus-original-article-mode)
4470       (make-local-variable 'gnus-original-article))
4471     (if (and (get-buffer name)
4472              (with-current-buffer name
4473                (if gnus-article-edit-mode
4474                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4475                        (progn
4476                          (set-buffer-modified-p nil)
4477                          (gnus-kill-buffer name)
4478                          (message "")
4479                          nil)
4480                      (error "Action aborted"))
4481                  t)))
4482         (with-current-buffer name
4483           (set (make-local-variable 'gnus-article-edit-mode) nil)
4484           (when gnus-article-mime-handles
4485             (mm-destroy-parts gnus-article-mime-handles)
4486             (setq gnus-article-mime-handles nil))
4487           ;; Set it to nil in article-buffer!
4488           (setq gnus-article-mime-handle-alist nil)
4489           (buffer-disable-undo)
4490           (setq buffer-read-only t)
4491           (unless (eq major-mode 'gnus-article-mode)
4492             (gnus-article-mode))
4493           (setq truncate-lines gnus-article-truncate-lines)
4494           (current-buffer))
4495       (with-current-buffer (gnus-get-buffer-create name)
4496         (gnus-article-mode)
4497         (setq truncate-lines gnus-article-truncate-lines)
4498         (make-local-variable 'gnus-summary-buffer)
4499         (setq gnus-summary-buffer
4500               (gnus-summary-buffer-name gnus-newsgroup-name))
4501         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4502         (cond
4503          ((and gnus-article-update-date-headers
4504                (not article-lapsed-timer))
4505           (gnus-start-date-timer gnus-article-update-date-headers))
4506          ((and (not gnus-article-update-date-headers)
4507                article-lapsed-timer)
4508           (gnus-stop-date-timer)))
4509         (current-buffer)))))
4510
4511 ;; Set article window start at LINE, where LINE is the number of lines
4512 ;; from the head of the article.
4513 (defun gnus-article-set-window-start (&optional line)
4514   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4515     (when article-window
4516       (set-window-start
4517        article-window
4518        (with-current-buffer gnus-article-buffer
4519          (goto-char (point-min))
4520          (if (not line)
4521              (point-min)
4522            (gnus-message 6 "Moved to bookmark")
4523            (search-forward "\n\n" nil t)
4524            (forward-line line)
4525            (point)))))))
4526
4527 (defun gnus-article-prepare (article &optional all-headers header)
4528   "Prepare ARTICLE in article mode buffer.
4529 ARTICLE should either be an article number or a Message-ID.
4530 If ARTICLE is an id, HEADER should be the article headers.
4531 If ALL-HEADERS is non-nil, no headers are hidden."
4532   (save-excursion
4533     ;; Make sure we start in a summary buffer.
4534     (unless (eq major-mode 'gnus-summary-mode)
4535       (set-buffer gnus-summary-buffer))
4536     (setq gnus-summary-buffer (current-buffer))
4537     (let* ((gnus-article (if header (mail-header-number header) article))
4538            (summary-buffer (current-buffer))
4539            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4540            (group gnus-newsgroup-name)
4541            result)
4542       (save-excursion
4543         (gnus-article-setup-buffer)
4544         (set-buffer gnus-article-buffer)
4545         ;; Deactivate active regions.
4546         (when (and (boundp 'transient-mark-mode)
4547                    transient-mark-mode)
4548           (setq mark-active nil))
4549         (if (not (setq result (let ((inhibit-read-only t))
4550                                 (gnus-request-article-this-buffer
4551                                  article group))))
4552             ;; There is no such article.
4553             (save-excursion
4554               (when (and (numberp article)
4555                          (not (memq article gnus-newsgroup-sparse)))
4556                 (setq gnus-article-current
4557                       (cons gnus-newsgroup-name article))
4558                 (set-buffer gnus-summary-buffer)
4559                 (setq gnus-current-article article)
4560                 (if (and (memq article gnus-newsgroup-undownloaded)
4561                          (not (gnus-online (gnus-find-method-for-group
4562                                             gnus-newsgroup-name))))
4563                     (progn
4564                       (gnus-summary-set-agent-mark article)
4565                       (message "Message marked for downloading"))
4566                   (gnus-summary-mark-article article gnus-canceled-mark)
4567                   (unless (memq article gnus-newsgroup-sparse)
4568                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4569           (if (or (eq result 'pseudo)
4570                   (eq result 'nneething))
4571               (progn
4572                 (with-current-buffer summary-buffer
4573                   (push article gnus-newsgroup-history)
4574                   (setq gnus-last-article gnus-current-article
4575                         gnus-current-article 0
4576                         gnus-current-headers nil
4577                         gnus-article-current nil)
4578                   (if (eq result 'nneething)
4579                       (gnus-configure-windows 'summary)
4580                     (gnus-configure-windows 'article))
4581                   (gnus-set-global-variables))
4582                 (let ((gnus-article-mime-handle-alist-1
4583                        gnus-article-mime-handle-alist))
4584                   (gnus-set-mode-line 'article)))
4585             ;; The result from the `request' was an actual article -
4586             ;; or at least some text that is now displayed in the
4587             ;; article buffer.
4588             (when (and (numberp article)
4589                        (not (eq article gnus-current-article)))
4590               ;; Seems like a new article has been selected.
4591               ;; `gnus-current-article' must be an article number.
4592               (with-current-buffer summary-buffer
4593                 (push article gnus-newsgroup-history)
4594                 (setq gnus-last-article gnus-current-article
4595                       gnus-current-article article
4596                       gnus-current-headers
4597                       (gnus-summary-article-header gnus-current-article)
4598                       gnus-article-current
4599                       (cons gnus-newsgroup-name gnus-current-article))
4600                 (unless (vectorp gnus-current-headers)
4601                   (setq gnus-current-headers nil))
4602                 (gnus-summary-goto-subject gnus-current-article)
4603                 (when (gnus-summary-show-thread)
4604                   ;; If the summary buffer really was folded, the
4605                   ;; previous goto may not actually have gone to
4606                   ;; the right article, but the thread root instead.
4607                   ;; So we go again.
4608                   (gnus-summary-goto-subject gnus-current-article))
4609                 (gnus-run-hooks 'gnus-mark-article-hook)
4610                 (gnus-set-mode-line 'summary)
4611                 (when (gnus-visual-p 'article-highlight 'highlight)
4612                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4613                 ;; Set the global newsgroup variables here.
4614                 (gnus-set-global-variables)
4615                 (setq gnus-have-all-headers
4616                       (or all-headers gnus-show-all-headers))))
4617             (save-excursion
4618               (gnus-configure-windows 'article))
4619             (when (or (numberp article)
4620                       (stringp article))
4621               (gnus-article-prepare-display)
4622               ;; Do page break.
4623               (goto-char (point-min))
4624               (when gnus-break-pages
4625                 (gnus-narrow-to-page)))
4626             (let ((gnus-article-mime-handle-alist-1
4627                    gnus-article-mime-handle-alist))
4628               (gnus-set-mode-line 'article))
4629             (article-goto-body)
4630             (unless (bobp)
4631               (forward-line -1))
4632             (set-window-point (get-buffer-window (current-buffer)) (point))
4633             (gnus-configure-windows 'article)
4634             t))))))
4635
4636 ;;;###autoload
4637 (defun gnus-article-prepare-display ()
4638   "Make the current buffer look like a nice article."
4639   ;; Hooks for getting information from the article.
4640   ;; This hook must be called before being narrowed.
4641   (let ((gnus-article-buffer (current-buffer))
4642         buffer-read-only
4643         (inhibit-read-only t))
4644     (unless (eq major-mode 'gnus-article-mode)
4645       (gnus-article-mode))
4646     (setq buffer-read-only nil
4647           gnus-article-wash-types nil
4648           gnus-article-image-alist nil)
4649     (gnus-run-hooks 'gnus-tmp-internal-hook)
4650     (when gnus-display-mime-function
4651       (funcall gnus-display-mime-function))
4652     (gnus-run-hooks 'gnus-article-prepare-hook)))
4653
4654 ;;;
4655 ;;; Gnus Sticky Article Mode
4656 ;;;
4657
4658 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4659   "Mode for sticky articles."
4660   ;; Release bindings that won't work.
4661   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4662                              gnus-sticky-article-mode-map)
4663   (substitute-key-definition 'gnus-article-refer-article 'undefined
4664                              gnus-sticky-article-mode-map)
4665   (dolist (k '("e" "h" "s" "F" "R"))
4666     (define-key gnus-sticky-article-mode-map k nil))
4667   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4668   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4669   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4670   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4671
4672 (defun gnus-sticky-article (arg)
4673   "Make the current article sticky.
4674 If a prefix ARG is given, ask for a name for this sticky article buffer."
4675   (interactive "P")
4676   (gnus-summary-show-thread)
4677   (gnus-summary-select-article nil nil 'pseudo)
4678   (let (new-art-buf-name)
4679     (gnus-eval-in-buffer-window gnus-article-buffer
4680       (setq new-art-buf-name
4681             (concat
4682              "*Sticky Article: "
4683              (if arg
4684                  (read-from-minibuffer "Sticky article buffer name: ")
4685                (gnus-with-article-headers
4686                  (gnus-article-goto-header "subject")
4687                  (setq new-art-buf-name
4688                        (buffer-substring-no-properties
4689                         (line-beginning-position) (line-end-position)))
4690                  (goto-char (point-min))
4691                  (gnus-article-goto-header "from")
4692                  (setq new-art-buf-name
4693                        (concat
4694                         new-art-buf-name ", "
4695                         (buffer-substring-no-properties
4696                          (line-beginning-position) (line-end-position))))
4697                  (goto-char (point-min))
4698                  (gnus-article-goto-header "date")
4699                  (setq new-art-buf-name
4700                        (concat
4701                         new-art-buf-name ", "
4702                         (buffer-substring-no-properties
4703                          (line-beginning-position) (line-end-position))))))
4704              "*"))
4705       (if (and (gnus-buffer-live-p new-art-buf-name)
4706                (with-current-buffer new-art-buf-name
4707                  (eq major-mode 'gnus-sticky-article-mode)))
4708           (switch-to-buffer new-art-buf-name)
4709         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4710       (gnus-sticky-article-mode))
4711     (setq gnus-article-buffer new-art-buf-name))
4712   (gnus-summary-recenter)
4713   (gnus-summary-position-point))
4714
4715 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4716   "Kill the given sticky article BUFFER.
4717 If none is given, assume the current buffer and kill it if it has
4718 `gnus-sticky-article-mode'."
4719   (interactive)
4720   (unless buffer
4721     (setq buffer (current-buffer)))
4722   (with-current-buffer buffer
4723     (when (eq major-mode 'gnus-sticky-article-mode)
4724       (gnus-kill-buffer buffer))))
4725
4726 (defun gnus-kill-sticky-article-buffers (arg)
4727   "Kill all sticky article buffers.
4728 If a prefix ARG is given, ask for confirmation."
4729   (interactive "P")
4730   (dolist (buf (gnus-buffers))
4731     (with-current-buffer buf
4732       (when (eq major-mode 'gnus-sticky-article-mode)
4733         (if (not arg)
4734             (gnus-kill-buffer buf)
4735           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4736             (gnus-kill-buffer buf)))))))
4737
4738 ;;;
4739 ;;; Gnus MIME viewing functions
4740 ;;;
4741
4742 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4743   "Format of the MIME buttons.
4744
4745 Valid specifiers include:
4746 %t  The MIME type
4747 %T  MIME type, along with additional info
4748 %n  The `name' parameter
4749 %d  The description, if any
4750 %l  The length of the encoded part
4751 %p  The part identifier number
4752 %e  Dots if the part isn't displayed
4753
4754 General format specifiers can also be used.  See Info node
4755 `(gnus)Formatting Variables'.")
4756
4757 (defvar gnus-mime-button-line-format-alist
4758   '((?t gnus-tmp-type ?s)
4759     (?T gnus-tmp-type-long ?s)
4760     (?n gnus-tmp-name ?s)
4761     (?d gnus-tmp-description ?s)
4762     (?p gnus-tmp-id ?s)
4763     (?l gnus-tmp-length ?d)
4764     (?e gnus-tmp-dots ?s)))
4765
4766 (defvar gnus-mime-button-commands
4767   '((gnus-article-press-button "\r" "Toggle Display")
4768     (gnus-mime-view-part "v" "View Interactively...")
4769     (gnus-mime-view-part-as-type "t" "View As Type...")
4770     (gnus-mime-view-part-as-charset "C" "View As charset...")
4771     (gnus-mime-save-part "o" "Save...")
4772     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4773     (gnus-mime-replace-part "r" "Replace part")
4774     (gnus-mime-delete-part "d" "Delete part")
4775     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4776     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4777     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4778     (gnus-mime-view-part-externally "e" "View Externally")
4779     (gnus-mime-print-part "p" "Print")
4780     (gnus-mime-pipe-part "|" "Pipe To Command...")
4781     (gnus-mime-action-on-part "." "Take action on the part...")))
4782
4783 (defun gnus-article-mime-part-status ()
4784   (if gnus-article-mime-handle-alist-1
4785       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4786           " (1 part)"
4787         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4788     ""))
4789
4790 (defvar gnus-mime-button-map
4791   (let ((map (make-sparse-keymap)))
4792     (define-key map gnus-mouse-2 'gnus-article-push-button)
4793     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4794     (dolist (c gnus-mime-button-commands)
4795       (define-key map (cadr c) (car c)))
4796     map))
4797
4798 (easy-menu-define
4799   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4800   `("MIME Part"
4801     ,@(mapcar (lambda (c)
4802                 (vector (caddr c) (car c) :active t))
4803               gnus-mime-button-commands)))
4804
4805 (defvar gnus-url-button-commands
4806   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4807
4808 (defvar gnus-url-button-map
4809   (let ((map (make-sparse-keymap)))
4810     (dolist (c gnus-url-button-commands)
4811       (define-key map (cadr c) (car c)))
4812     map))
4813
4814 (easy-menu-define
4815   gnus-url-button-menu gnus-url-button-map "URL button menu."
4816   `("Url Button"
4817     ,@(mapcar (lambda (c)
4818                 (vector (caddr c) (car c) :active t))
4819               gnus-url-button-commands)))
4820
4821 (defmacro gnus-bind-safe-url-regexp (&rest body)
4822   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4823   `(let ((mm-w3m-safe-url-regexp
4824           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4825                                 (gnus-buffer-live-p
4826                                  gnus-article-current-summary))
4827                            (with-current-buffer gnus-article-current-summary
4828                              gnus-newsgroup-name)
4829                          gnus-newsgroup-name)))
4830             (if (cond ((not group)
4831                        ;; Maybe we're in a mml-preview buffer
4832                        ;; and no group is selected.
4833                        t)
4834                       ((stringp gnus-safe-html-newsgroups)
4835                        (string-match gnus-safe-html-newsgroups group))
4836                       ((consp gnus-safe-html-newsgroups)
4837                        (member group gnus-safe-html-newsgroups)))
4838                 nil
4839               mm-w3m-safe-url-regexp))))
4840      ,@body))
4841
4842 (defun gnus-mime-button-menu (event prefix)
4843  "Construct a context-sensitive menu of MIME commands."
4844  (interactive "e\nP")
4845  (save-window-excursion
4846    (let ((pos (event-start event)))
4847      (select-window (posn-window pos))
4848      (goto-char (posn-point pos))
4849      (gnus-article-check-buffer)
4850      (popup-menu gnus-mime-button-menu nil prefix))))
4851
4852 (defun gnus-mime-view-all-parts (&optional handles)
4853   "View all the MIME parts."
4854   (interactive)
4855   (with-current-buffer gnus-article-buffer
4856     (let ((handles (or handles gnus-article-mime-handles))
4857           (mail-parse-charset gnus-newsgroup-charset)
4858           (mail-parse-ignored-charsets
4859            (with-current-buffer gnus-summary-buffer
4860              gnus-newsgroup-ignored-charsets)))
4861       (when handles
4862         (mm-remove-parts handles)
4863         (goto-char (point-min))
4864         (or (search-forward "\n\n") (goto-char (point-max)))
4865         (let ((inhibit-read-only t))
4866           (delete-region (point) (point-max))
4867           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4868
4869 (defun gnus-article-jump-to-part (n)
4870   "Jump to MIME part N."
4871   (interactive "P")
4872   (let ((parts (with-current-buffer gnus-article-buffer
4873                  (length gnus-article-mime-handle-alist))))
4874     (when (zerop parts)
4875       (error "No such part"))
4876     (pop-to-buffer gnus-article-buffer)
4877     (or n
4878         (setq n (if (= parts 1)
4879                     1
4880                   (read-number (format "Jump to part (1..%s): " parts)))))
4881     (unless (and (integerp n) (<= n parts) (>= n 1))
4882       (setq n
4883             (progn
4884               (gnus-message 7 "Invalid part `%s', using %s instead."
4885                             n parts)
4886               parts)))
4887     (gnus-message 9 "Jumping to part %s." n)
4888     (cond ((>= gnus-auto-select-part 1)
4889            (while (and (<= n parts)
4890                        (not (gnus-article-goto-part n)))
4891              (setq n (1+ n))))
4892           ((< gnus-auto-select-part 0)
4893            (while (and (>= n 1)
4894                        (not (gnus-article-goto-part n)))
4895              (setq n (1- n))))
4896           (t
4897            (gnus-article-goto-part n)))))
4898
4899 (defvar gnus-mime-buttonized-part-id nil
4900   "ID of a mime part that should be buttonized.
4901 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4902
4903 (eval-when-compile
4904   (defsubst gnus-article-edit-part (handles &optional current-id)
4905     "Edit an article in order to delete a mime part.
4906 This function is exclusively used by `gnus-mime-save-part-and-strip'
4907 and `gnus-mime-delete-part', and not provided at run-time normally."
4908     (gnus-article-edit-article
4909      `(lambda ()
4910         (buffer-disable-undo)
4911         (erase-buffer)
4912         (let ((mail-parse-charset (or gnus-article-charset
4913                                       ',gnus-newsgroup-charset))
4914               (mail-parse-ignored-charsets
4915                (or gnus-article-ignored-charsets
4916                    ',gnus-newsgroup-ignored-charsets))
4917               (mbl mml-buffer-list))
4918           (setq mml-buffer-list nil)
4919           (insert-buffer-substring gnus-original-article-buffer)
4920           (mime-to-mml ',handles)
4921           (setq gnus-article-mime-handles nil)
4922           (let ((mbl1 mml-buffer-list))
4923             (setq mml-buffer-list mbl)
4924             (set (make-local-variable 'mml-buffer-list) mbl1))
4925           (gnus-make-local-hook 'kill-buffer-hook)
4926           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4927      `(lambda (no-highlight)
4928         (let ((mail-parse-charset (or gnus-article-charset
4929                                       ',gnus-newsgroup-charset))
4930               (message-options message-options)
4931               (message-options-set-recipient)
4932               (mail-parse-ignored-charsets
4933                (or gnus-article-ignored-charsets
4934                    ',gnus-newsgroup-ignored-charsets)))
4935           (mml-to-mime)
4936           (mml-destroy-buffers)
4937           (remove-hook 'kill-buffer-hook
4938                        'mml-destroy-buffers t)
4939           (kill-local-variable 'mml-buffer-list))
4940         (gnus-summary-edit-article-done
4941          ,(or (mail-header-references gnus-current-headers) "")
4942          ,(gnus-group-read-only-p)
4943          ,gnus-summary-buffer no-highlight))
4944      t)
4945     ;; Force buttonizing this part.
4946     (let ((gnus-mime-buttonized-part-id current-id))
4947       (gnus-article-edit-done))
4948     (gnus-configure-windows 'article)
4949     (when (and current-id (integerp gnus-auto-select-part))
4950       (gnus-article-jump-to-part
4951        (min (max (+ current-id gnus-auto-select-part) 1)
4952             (with-current-buffer gnus-article-buffer
4953               (length gnus-article-mime-handle-alist)))))))
4954
4955 (defun gnus-mime-replace-part (file)
4956   "Replace MIME part under point with an external body."
4957   ;; Useful if file has already been saved to disk
4958   (interactive
4959    (list
4960     (read-file-name "Replace MIME part with file: "
4961                     (or mm-default-directory default-directory)
4962                     nil nil)))
4963   (gnus-mime-save-part-and-strip file))
4964
4965 (defun gnus-mime-save-part-and-strip (&optional file)
4966   "Save the MIME part under point then replace it with an external body.
4967 If FILE is given, use it for the external part."
4968   (interactive)
4969   (gnus-article-check-buffer)
4970   (when (gnus-group-read-only-p)
4971     (error "The current group does not support deleting of parts"))
4972   (when (mm-complicated-handles gnus-article-mime-handles)
4973     (error "\
4974 The current article has a complicated MIME structure, giving up..."))
4975   (let* ((data (get-text-property (point) 'gnus-data))
4976          (id (get-text-property (point) 'gnus-part))
4977          param
4978          (handles gnus-article-mime-handles))
4979     (unless file
4980       (setq file
4981             (and data (mm-save-part data "Delete MIME part and save to: "))))
4982     (when file
4983       (with-current-buffer (mm-handle-buffer data)
4984         (erase-buffer)
4985         (insert "Content-Type: " (mm-handle-media-type data))
4986         (mml-insert-parameter-string (cdr (mm-handle-type data))
4987                                      '(charset))
4988         ;; Add a filename for the sake of saving the part again.
4989         (mml-insert-parameter
4990          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
4991         (insert "\n")
4992         (insert "Content-ID: " (message-make-message-id) "\n")
4993         (insert "Content-Transfer-Encoding: binary\n")
4994         (insert "\n"))
4995       (setcdr data
4996               (cdr (mm-make-handle nil
4997                                    `("message/external-body"
4998                                      (access-type . "LOCAL-FILE")
4999                                      (name . ,file)))))
5000       ;; (set-buffer gnus-summary-buffer)
5001       (gnus-article-edit-part handles id))))
5002
5003 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5004 ;; parts...>') but with stripping would be nice.
5005
5006 (defun gnus-mime-delete-part ()
5007   "Delete the MIME part under point.
5008 Replace it with some information about the removed part."
5009   (interactive)
5010   (gnus-article-check-buffer)
5011   (when (gnus-group-read-only-p)
5012     (error "The current group does not support deleting of parts"))
5013   (when (mm-complicated-handles gnus-article-mime-handles)
5014     (error "\
5015 The current article has a complicated MIME structure, giving up..."))
5016   (when (or gnus-expert-user
5017             (gnus-yes-or-no-p "\
5018 Deleting parts may malfunction or destroy the article; continue? "))
5019     (let* ((data (get-text-property (point) 'gnus-data))
5020            (id (get-text-property (point) 'gnus-part))
5021            (handles gnus-article-mime-handles)
5022            (none "(none)")
5023            (description
5024             (let ((desc (mm-handle-description data)))
5025               (when desc
5026                 (mail-decode-encoded-word-string desc))))
5027            (filename
5028             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5029                 none))
5030            (type (mm-handle-media-type data)))
5031       (unless data
5032         (error "No MIME part under point"))
5033       (with-current-buffer (mm-handle-buffer data)
5034         (let ((bsize (buffer-size)))
5035           (erase-buffer)
5036           (insert
5037            (concat
5038             ",----\n"
5039             "| The following attachment has been deleted:\n"
5040             "|\n"
5041             "| Type:           " type "\n"
5042             "| Filename:       " filename "\n"
5043             "| Size (encoded): " (format "%s byte%s\n"
5044                                          bsize (if (= bsize 1)
5045                                                    ""
5046                                                  "s"))
5047             (when description
5048               (concat    "| Description:    " description "\n"))
5049             "`----\n"))
5050           (setcdr data
5051                   (cdr (mm-make-handle
5052                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5053                         (list "attachment")
5054                         (format "Deleted attachment (%s bytes)" bsize))))))
5055       ;; (set-buffer gnus-summary-buffer)
5056       (gnus-article-edit-part handles id))))
5057
5058 (defun gnus-mime-save-part ()
5059   "Save the MIME part under point."
5060   (interactive)
5061   (gnus-article-check-buffer)
5062   (let ((data (get-text-property (point) 'gnus-data)))
5063     (when data
5064       (mm-save-part data))))
5065
5066 (defun gnus-mime-pipe-part (&optional cmd)
5067   "Pipe the MIME part under point to a process.
5068 Use CMD as the process."
5069   (interactive)
5070   (gnus-article-check-buffer)
5071   (let ((data (get-text-property (point) 'gnus-data)))
5072     (when data
5073       (mm-pipe-part data cmd))))
5074
5075 (defun gnus-mime-view-part ()
5076   "Interactively choose a viewing method for the MIME part under point."
5077   (interactive)
5078   (gnus-article-check-buffer)
5079   (let ((data (get-text-property (point) 'gnus-data)))
5080     (when data
5081       (setq gnus-article-mime-handles
5082             (mm-merge-handles
5083              gnus-article-mime-handles (setq data (copy-sequence data))))
5084       (mm-interactively-view-part data))))
5085
5086 (defun gnus-mime-view-part-as-type-internal ()
5087   (gnus-article-check-buffer)
5088   (let* ((handle (get-text-property (point) 'gnus-data))
5089          (name (or
5090                 ;; Content-Type: foo/bar; name=...
5091                 (mail-content-type-get (mm-handle-type handle) 'name)
5092                 ;; Content-Disposition: attachment; filename=...
5093                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5094          (def-type (and name (mm-default-file-encoding name))))
5095     (or (and def-type (cons def-type 0))
5096         (and handle
5097              (equal (mm-handle-media-supertype handle) "text")
5098              '("text/plain" . 0))
5099         '("application/octet-stream" . 0))))
5100
5101 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5102   "Choose a MIME media type, and view the part as such.
5103 If non-nil, PRED is a predicate to use during completion to limit the
5104 available media-types."
5105   (interactive)
5106   (unless mime-type
5107     (setq mime-type
5108           (let ((default (gnus-mime-view-part-as-type-internal)))
5109             (gnus-completing-read
5110              "View as MIME type"
5111              (if pred
5112                  (gnus-remove-if-not pred (mailcap-mime-types))
5113                (mailcap-mime-types))
5114              nil nil nil
5115              (car default)))))
5116   (gnus-article-check-buffer)
5117   (let ((handle (get-text-property (point) 'gnus-data)))
5118     (when handle
5119       (when (equal (mm-handle-media-type handle) "message/external-body")
5120         (unless (mm-handle-cache handle)
5121           (mm-extern-cache-contents handle))
5122         (setq handle (mm-handle-cache handle)))
5123       (setq handle
5124             (mm-make-handle (mm-handle-buffer handle)
5125                             (cons mime-type (cdr (mm-handle-type handle)))
5126                             (mm-handle-encoding handle)
5127                             (mm-handle-undisplayer handle)
5128                             (mm-handle-disposition handle)
5129                             (mm-handle-description handle)
5130                             nil
5131                             (mm-handle-id handle)))
5132       (setq gnus-article-mime-handles
5133             (mm-merge-handles gnus-article-mime-handles handle))
5134       (when (mm-handle-displayed-p handle)
5135         (mm-remove-part handle))
5136       (gnus-mm-display-part handle))))
5137
5138 (defun gnus-mime-copy-part (&optional handle arg)
5139   "Put the MIME part under point into a new buffer.
5140 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5141 are decompressed."
5142   (interactive (list nil current-prefix-arg))
5143   (gnus-article-check-buffer)
5144   (unless handle
5145     (setq handle (get-text-property (point) 'gnus-data)))
5146   (when handle
5147     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5148                                                'name)
5149                         (mail-content-type-get (mm-handle-disposition handle)
5150                                                'filename)))
5151           contents dont-decode charset coding-system)
5152       (mm-with-unibyte-buffer
5153         (mm-insert-part handle)
5154         (setq contents (or (condition-case nil
5155                                (mm-decompress-buffer filename nil 'sig)
5156                              (error
5157                               (setq dont-decode t)
5158                               nil))
5159                            (buffer-string))))
5160       (setq filename (cond (filename (file-name-nondirectory filename))
5161                            (dont-decode "*raw data*")
5162                            (t "*decoded*")))
5163       (cond
5164        (dont-decode)
5165        ((not arg)
5166         (unless (setq charset (mail-content-type-get
5167                                (mm-handle-type handle) 'charset))
5168           (unless (setq coding-system (mm-with-unibyte-buffer
5169                                         (insert contents)
5170                                         (mm-find-buffer-file-coding-system)))
5171             (setq charset gnus-newsgroup-charset))))
5172        ((numberp arg)
5173         (setq charset (or (cdr (assq arg
5174                                      gnus-summary-show-article-charset-alist))
5175                           (mm-read-coding-system "Charset: ")))))
5176       (switch-to-buffer (generate-new-buffer filename))
5177       (if (or coding-system
5178               (and charset
5179                    (setq coding-system (mm-charset-to-coding-system charset))
5180                    (not (eq coding-system 'ascii))))
5181           (progn
5182             (mm-enable-multibyte)
5183             (insert (mm-decode-coding-string contents coding-system))
5184             (setq buffer-file-coding-system
5185                   (if (boundp 'last-coding-system-used)
5186                       (symbol-value 'last-coding-system-used)
5187                     coding-system)))
5188         (mm-disable-multibyte)
5189         (insert contents)
5190         (setq buffer-file-coding-system mm-binary-coding-system))
5191       ;; We do it this way to make `normal-mode' set the appropriate mode.
5192       (unwind-protect
5193           (progn
5194             (setq buffer-file-name (expand-file-name filename))
5195             (normal-mode))
5196         (setq buffer-file-name nil))
5197       (goto-char (point-min)))))
5198
5199 (defun gnus-mime-print-part (&optional handle filename)
5200   "Print the MIME part under point."
5201   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5202   (gnus-article-check-buffer)
5203   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5204          (contents (and handle (mm-get-part handle)))
5205          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5206          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5207     (when contents
5208         (if printer
5209             (unwind-protect
5210                 (progn
5211                   (mm-save-part-to-file handle file)
5212                   (call-process shell-file-name nil
5213                                 (generate-new-buffer " *mm*")
5214                                 nil
5215                                 shell-command-switch
5216                                 (mm-mailcap-command
5217                                  printer file (mm-handle-type handle))))
5218               (delete-file file))
5219           (with-temp-buffer
5220             (insert contents)
5221             (gnus-print-buffer))
5222           (ps-despool filename)))))
5223
5224 (defun gnus-mime-inline-part (&optional handle arg)
5225   "Insert the MIME part under point into the current buffer.
5226 Compressed files like .gz and .bz2 are decompressed."
5227   (interactive (list nil current-prefix-arg))
5228   (gnus-article-check-buffer)
5229   (unless handle
5230     (setq handle (get-text-property (point) 'gnus-data)))
5231   (when handle
5232     (let ((b (point))
5233           (inhibit-read-only t)
5234           contents charset coding-system)
5235       (if (and (not arg) (mm-handle-undisplayer handle))
5236           (mm-remove-part handle)
5237         (mm-with-unibyte-buffer
5238           (mm-insert-part handle)
5239           (setq contents
5240                 (or (mm-decompress-buffer
5241                      (or (mail-content-type-get (mm-handle-type handle)
5242                                                 'name)
5243                          (mail-content-type-get (mm-handle-disposition handle)
5244                                                 'filename))
5245                      nil t)
5246                     (buffer-string))))
5247         (cond
5248          ((not arg)
5249           (unless (setq charset (mail-content-type-get
5250                                  (mm-handle-type handle) 'charset))
5251             (unless (setq coding-system
5252                           (mm-with-unibyte-buffer
5253                             (insert contents)
5254                             (mm-find-buffer-file-coding-system)))
5255               (setq charset gnus-newsgroup-charset))))
5256          ((numberp arg)
5257           (if (mm-handle-undisplayer handle)
5258               (mm-remove-part handle))
5259           (setq charset
5260                 (or (cdr (assq arg
5261                                gnus-summary-show-article-charset-alist))
5262                     (mm-read-coding-system "Charset: "))))
5263          (t
5264           (if (mm-handle-undisplayer handle)
5265               (mm-remove-part handle))))
5266         (forward-line 2)
5267         (mm-display-inline handle)
5268         (goto-char b)))))
5269
5270 (defun gnus-mime-set-charset-parameters (handle charset)
5271   "Set CHARSET to parameters in HANDLE.
5272 CHARSET may either be a string or a symbol."
5273   (unless (stringp charset)
5274     (setq charset (symbol-name charset)))
5275   (if (stringp (car handle))
5276       (dolist (h (cdr handle))
5277         (gnus-mime-set-charset-parameters h charset))
5278     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5279                                             "message/external-body")
5280                                      (progn
5281                                        (unless (mm-handle-cache handle)
5282                                          (mm-extern-cache-contents handle))
5283                                        (mm-handle-cache handle))
5284                                    handle)))
5285            (param (assq 'charset (cdr type))))
5286       (if param
5287           (setcdr param charset)
5288         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5289
5290 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5291   "Insert the MIME part under point into the current buffer using the
5292 specified charset."
5293   (interactive (list nil current-prefix-arg))
5294   (gnus-article-check-buffer)
5295   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5296         (fun (get-text-property (point) 'gnus-callback))
5297         (gnus-newsgroup-ignored-charsets 'gnus-all)
5298         charset form preferred parts)
5299     (when handle
5300       (when (prog1
5301                 (and fun
5302                      (setq charset
5303                            (or (cdr (assq
5304                                      arg
5305                                      gnus-summary-show-article-charset-alist))
5306                                (mm-read-coding-system "Charset: "))))
5307               (if (mm-handle-undisplayer handle)
5308                   (mm-remove-part handle)))
5309         (gnus-mime-set-charset-parameters handle charset)
5310         (when (and (consp (setq form (cdr-safe fun)))
5311                    (setq form (ignore-errors
5312                                 (assq 'gnus-mime-display-alternative form)))
5313                    (setq preferred (caddr form))
5314                    (progn
5315                      (when (eq (car preferred) 'quote)
5316                        (setq preferred (cadr preferred)))
5317                      (not (equal preferred
5318                                  (get-text-property (point) 'gnus-data))))
5319                    (setq parts (get-text-property (point) 'gnus-part))
5320                    (setq parts (cdr (assq parts
5321                                           gnus-article-mime-handle-alist)))
5322                    (equal (mm-handle-media-type parts) "multipart/alternative")
5323                    (setq parts (reverse (cdr parts))))
5324           (setcar (cddr form)
5325                   (list 'quote (or (cadr (member preferred parts))
5326                                    (car parts)))))
5327         (funcall fun handle)))))
5328
5329 (defun gnus-mime-view-part-externally (&optional handle)
5330   "View the MIME part under point with an external viewer."
5331   (interactive)
5332   (gnus-article-check-buffer)
5333   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5334          (mm-user-display-methods nil)
5335          (mm-inlined-types nil)
5336          (mail-parse-charset gnus-newsgroup-charset)
5337          (mail-parse-ignored-charsets
5338           (with-current-buffer gnus-summary-buffer
5339             gnus-newsgroup-ignored-charsets))
5340          (type (mm-handle-media-type handle))
5341          (method (mailcap-mime-info type))
5342          (mm-enable-external t))
5343     (if (not (stringp method))
5344         (gnus-mime-view-part-as-type
5345          nil (lambda (type) (stringp (mailcap-mime-info type))))
5346       (when handle
5347         (mm-display-part handle nil t)))))
5348
5349 (defun gnus-mime-view-part-internally (&optional handle)
5350   "View the MIME part under point with an internal viewer.
5351 If no internal viewer is available, use an external viewer."
5352   (interactive)
5353   (gnus-article-check-buffer)
5354   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5355          (mm-inlined-types '(".*"))
5356          (mm-inline-large-images t)
5357          (mail-parse-charset gnus-newsgroup-charset)
5358          (mail-parse-ignored-charsets
5359           (with-current-buffer gnus-summary-buffer
5360             gnus-newsgroup-ignored-charsets))
5361          (inhibit-read-only t))
5362     (if (not (mm-inlinable-p handle))
5363         (gnus-mime-view-part-as-type
5364          nil (lambda (type) (mm-inlinable-p handle type)))
5365       (when handle
5366         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5367
5368 (defun gnus-mime-action-on-part (&optional action)
5369   "Do something with the MIME attachment at \(point\)."
5370   (interactive
5371    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5372   (gnus-article-check-buffer)
5373   (let ((action-pair (assoc action gnus-mime-action-alist)))
5374     (if action-pair
5375         (funcall (cdr action-pair)))))
5376
5377 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5378   "Call FUNCTION on MIME part N.
5379 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5380 If INTERACTIVE, call FUNCTION interactivly."
5381   (let (window frame)
5382     ;; Check whether the article is displayed.
5383     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5384                  (setq window (get-buffer-window gnus-article-buffer t))
5385                  (frame-visible-p (setq frame (window-frame window))))
5386       (error "No article is displayed"))
5387     (with-current-buffer gnus-article-buffer
5388       ;; Check whether the article displays the right contents.
5389       (unless (with-current-buffer gnus-summary-buffer
5390                 (eq gnus-current-article (gnus-summary-article-number)))
5391         (error "You should select the right article first"))
5392       (if n
5393           (setq n (prefix-numeric-value n))
5394         (let ((pt (point)))
5395           (setq n (or (get-text-property pt 'gnus-part)
5396                       (and (not (bobp))
5397                            (get-text-property (1- pt) 'gnus-part))
5398                       (get-text-property (prog2
5399                                              (forward-line 1)
5400                                              (point)
5401                                            (goto-char pt))
5402                                          'gnus-part)
5403                       (get-text-property
5404                        (or (and (setq pt (previous-single-property-change
5405                                           pt 'gnus-part))
5406                                 (1- pt))
5407                            (next-single-property-change (point) 'gnus-part)
5408                            (point))
5409                        'gnus-part)
5410                       1))))
5411       ;; Check whether the specified part exists.
5412       (when (> n (length gnus-article-mime-handle-alist))
5413         (error "No such part")))
5414     (unless
5415         (progn
5416           ;; To select the window is needed so that the cursor
5417           ;; might be visible on the MIME button.
5418           (select-window (prog1
5419                              window
5420                            (setq window (selected-window))
5421                            ;; Article may be displayed in the other frame.
5422                            (gnus-select-frame-set-input-focus
5423                             (prog1
5424                                 frame
5425                               (setq frame (selected-frame))))))
5426           (when (gnus-article-goto-part n)
5427             ;; We point the cursor and the arrow at the MIME button
5428             ;; when the `function' prompt the user for something.
5429             (unless (and (pos-visible-in-window-p)
5430                          (> (count-lines (point) (window-end))
5431                             (/ (1- (window-height)) 3)))
5432               (recenter (/ (1- (window-height)) 3)))
5433             (let ((cursor-in-non-selected-windows t)
5434                   (overlay-arrow-string "=>")
5435                   (overlay-arrow-position (point-marker)))
5436               (unwind-protect
5437                   (cond
5438                    ((and no-handle interactive)
5439                     (call-interactively function))
5440                    (no-handle
5441                     (funcall function))
5442                    (interactive
5443                     (call-interactively
5444                      function (get-text-property (point) 'gnus-data)))
5445                    (t
5446                     (funcall function
5447                              (get-text-property (point) 'gnus-data))))
5448                 (set-marker overlay-arrow-position nil)
5449                 (unless gnus-auto-select-part
5450                   (gnus-select-frame-set-input-focus frame)
5451                   (select-window window))))
5452             t))
5453       (if gnus-inhibit-mime-unbuttonizing
5454           ;; This is the default though the program shouldn't reach here.
5455           (error "No such part")
5456         ;; The part which doesn't have the MIME button is selected.
5457         ;; So, we display all the buttons and redo it.
5458         (let ((gnus-inhibit-mime-unbuttonizing t))
5459           (gnus-summary-show-article)
5460           (gnus-article-part-wrapper n function no-handle))))))
5461
5462 (defun gnus-article-pipe-part (n)
5463   "Pipe MIME part N, which is the numerical prefix."
5464   (interactive "P")
5465   (gnus-article-part-wrapper n 'mm-pipe-part))
5466
5467 (defun gnus-article-save-part (n)
5468   "Save MIME part N, which is the numerical prefix."
5469   (interactive "P")
5470   (gnus-article-part-wrapper n 'mm-save-part))
5471
5472 (defun gnus-article-interactively-view-part (n)
5473   "View MIME part N interactively, which is the numerical prefix."
5474   (interactive "P")
5475   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5476
5477 (defun gnus-article-copy-part (n)
5478   "Copy MIME part N, which is the numerical prefix."
5479   (interactive "P")
5480   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5481
5482 (defun gnus-article-view-part-as-charset (n)
5483   "View MIME part N using a specified charset.
5484 N is the numerical prefix."
5485   (interactive "P")
5486   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5487
5488 (defun gnus-article-view-part-externally (n)
5489   "View MIME part N externally, which is the numerical prefix."
5490   (interactive "P")
5491   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5492
5493 (defun gnus-article-inline-part (n)
5494   "Inline MIME part N, which is the numerical prefix."
5495   (interactive "P")
5496   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5497
5498 (defun gnus-article-save-part-and-strip (n)
5499   "Save MIME part N and replace it with an external body.
5500 N is the numerical prefix."
5501   (interactive "P")
5502   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5503
5504 (defun gnus-article-replace-part (n)
5505   "Replace MIME part N with an external body.
5506 N is the numerical prefix."
5507   (interactive "P")
5508   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5509
5510 (defun gnus-article-delete-part (n)
5511   "Delete MIME part N and add some information about the removed part.
5512 N is the numerical prefix."
5513   (interactive "P")
5514   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5515
5516 (defun gnus-article-view-part-as-type (n)
5517   "Choose a MIME media type, and view part N as such.
5518 N is the numerical prefix."
5519   (interactive "P")
5520   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5521
5522 (defun gnus-article-mime-match-handle-first (condition)
5523   (if condition
5524       (let (n)
5525         (dolist (ihandle gnus-article-mime-handle-alist)
5526           (if (and (cond
5527                     ((functionp condition)
5528                      (funcall condition (cdr ihandle)))
5529                     ((eq condition 'undisplayed)
5530                      (not (or (mm-handle-undisplayer (cdr ihandle))
5531                               (equal (mm-handle-media-type (cdr ihandle))
5532                                      "multipart/alternative"))))
5533                     ((eq condition 'undisplayed-alternative)
5534                      (not (mm-handle-undisplayer (cdr ihandle))))
5535                     (t t))
5536                    (gnus-article-goto-part (car ihandle))
5537                    (or (not n) (< (car ihandle) n)))
5538               (setq n (car ihandle))))
5539         (or n 1))
5540     1))
5541
5542 (defun gnus-article-view-part (&optional n)
5543   "View MIME part N, which is the numerical prefix.
5544 If the part is already shown, hide the part.  If N is nil, view
5545 all parts."
5546   (interactive "P")
5547   (with-current-buffer gnus-article-buffer
5548     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5549                              gnus-article-mime-match-handle-function)))
5550     (when (> n (length gnus-article-mime-handle-alist))
5551       (error "No such part"))
5552     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5553       (when (gnus-article-goto-part n)
5554         (if (equal (car handle) "multipart/alternative")
5555             (gnus-article-press-button)
5556           (when (eq (gnus-mm-display-part handle) 'internal)
5557             (gnus-set-window-start)))))))
5558
5559 (defsubst gnus-article-mime-total-parts ()
5560   (if (bufferp (car gnus-article-mime-handles))
5561       1 ;; single part
5562     (1- (length gnus-article-mime-handles))))
5563
5564 (defun gnus-mm-display-part (handle)
5565   "Display HANDLE and fix MIME button."
5566   (let ((id (get-text-property (point) 'gnus-part))
5567         (point (point))
5568         (inhibit-read-only t))
5569     (forward-line 1)
5570     (prog1
5571         (let ((window (selected-window))
5572               (mail-parse-charset gnus-newsgroup-charset)
5573               (mail-parse-ignored-charsets
5574                (if (gnus-buffer-live-p gnus-summary-buffer)
5575                    (with-current-buffer gnus-summary-buffer
5576                      gnus-newsgroup-ignored-charsets)
5577                  nil)))
5578           (save-excursion
5579             (unwind-protect
5580                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5581                       (beg (point)))
5582                   (when win
5583                     (select-window win))
5584                   (goto-char point)
5585                   (forward-line)
5586                   (if (mm-handle-displayed-p handle)
5587                       ;; This will remove the part.
5588                       (mm-display-part handle)
5589                     (save-restriction
5590                       (narrow-to-region (point)
5591                                         (if (eobp) (point) (1+ (point))))
5592                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5593                       ;; We narrow to the part itself and
5594                       ;; then call the treatment functions.
5595                       (goto-char (point-min))
5596                       (forward-line 1)
5597                       (narrow-to-region (point) (point-max))
5598                       (gnus-treat-article
5599                        nil id
5600                        (gnus-article-mime-total-parts)
5601                        (mm-handle-media-type handle)))))
5602               (if (window-live-p window)
5603                   (select-window window)))))
5604       (goto-char point)
5605       (gnus-delete-line)
5606       (gnus-insert-mime-button
5607        handle id (list (mm-handle-displayed-p handle)))
5608       (goto-char point))))
5609
5610 (defun gnus-article-goto-part (n)
5611   "Go to MIME part N."
5612   (when gnus-break-pages
5613     (widen))
5614   (prog1
5615       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5616             part handle end next handles)
5617         (when start
5618           (goto-char start)
5619           (if (setq handle (get-text-property start 'gnus-data))
5620               start
5621             ;; Go to the displayed subpart, assuming this is
5622             ;; multipart/alternative.
5623             (setq part start
5624                   end (point-at-eol))
5625             (while (and (not handle)
5626                         part
5627                         (< part end)
5628                         (setq next (text-property-not-all part end
5629                                                           'gnus-data nil)))
5630               (setq part next
5631                     handle (get-text-property part 'gnus-data))
5632               (push (cons handle part) handles)
5633               (unless (mm-handle-displayed-p handle)
5634                 (setq handle nil
5635                       part (text-property-any part end 'gnus-data nil))))
5636             (unless handle
5637               ;; No subpart is displayed, so we find preferred one.
5638               (setq part
5639                     (cdr (assq (mm-preferred-alternative
5640                                 (nreverse (mapcar 'car handles)))
5641                                handles))))
5642             (if part
5643                 (goto-char (1+ part))
5644               start))))
5645     (when gnus-break-pages
5646       (gnus-narrow-to-page))))
5647
5648 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5649   (let ((gnus-tmp-name
5650          (or (mail-content-type-get (mm-handle-type handle) 'name)
5651              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5652              (mail-content-type-get (mm-handle-type handle) 'url)
5653              ""))
5654         (gnus-tmp-type (mm-handle-media-type handle))
5655         (gnus-tmp-description (or (mm-handle-description handle) ""))
5656         (gnus-tmp-dots
5657          (if (if displayed (car displayed)
5658                (mm-handle-displayed-p handle))
5659              "" "..."))
5660         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5661                            (buffer-size)))
5662         gnus-tmp-type-long b e)
5663     (when (string-match ".*/" gnus-tmp-name)
5664       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5665     (setq gnus-tmp-type-long (concat gnus-tmp-type
5666                                      (and (not (equal gnus-tmp-name ""))
5667                                           (concat "; " gnus-tmp-name))))
5668     (unless (equal gnus-tmp-description "")
5669       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5670     (unless (bolp)
5671       (insert "\n"))
5672     (setq b (point))
5673     (gnus-eval-format
5674      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5675      `(keymap ,gnus-mime-button-map
5676               gnus-callback gnus-mm-display-part
5677               gnus-part ,gnus-tmp-id
5678               article-type annotation
5679               gnus-data ,handle))
5680     (setq e (if (bolp)
5681                 ;; Exclude a newline.
5682                 (1- (point))
5683               (point)))
5684     (when gnus-article-button-face
5685       (gnus-overlay-put (gnus-make-overlay b e nil t)
5686                         'face gnus-article-button-face))
5687     (widget-convert-button
5688      'link b e
5689      :mime-handle handle
5690      :action 'gnus-widget-press-button
5691      :button-keymap gnus-mime-button-map
5692      :help-echo
5693      (lambda (widget)
5694        ;; Needed to properly clear the message due to a bug in
5695        ;; wid-edit (XEmacs only).
5696        (if (boundp 'help-echo-owns-message)
5697            (setq help-echo-owns-message t))
5698        (format
5699         "%S: %s the MIME part; %S: more options"
5700         (aref gnus-mouse-2 0)
5701         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5702             "hide" "show")
5703         (aref gnus-down-mouse-3 0))))))
5704
5705 (defun gnus-widget-press-button (elems el)
5706   (goto-char (widget-get elems :from))
5707   (gnus-article-press-button))
5708
5709 (defvar gnus-displaying-mime nil)
5710
5711 (defun gnus-display-mime (&optional ihandles)
5712   "Display the MIME parts."
5713   (save-excursion
5714     (save-selected-window
5715       (let ((window (get-buffer-window gnus-article-buffer))
5716             (point (point)))
5717         (when window
5718           (select-window window)
5719           ;; We have to do this since selecting the window
5720           ;; may change the point.  So we set the window point.
5721           (set-window-point window point)))
5722       (let ((handles ihandles)
5723             (inhibit-read-only t)
5724             handle)
5725         (cond (handles)
5726               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5727                (when gnus-article-emulate-mime
5728                  (mm-uu-dissect-text-parts handles)))
5729               (gnus-article-emulate-mime
5730                (setq handles (mm-uu-dissect))))
5731         (when (and (not ihandles)
5732                    (not gnus-displaying-mime))
5733           ;; Top-level call; we clean up.
5734           (when gnus-article-mime-handles
5735             (mm-destroy-parts gnus-article-mime-handles)
5736             (setq gnus-article-mime-handle-alist nil));; A trick.
5737           (setq gnus-article-mime-handles handles)
5738           ;; We allow users to glean info from the handles.
5739           (when gnus-article-mime-part-function
5740             (gnus-mime-part-function handles)))
5741         (if (and handles
5742                  (or (not (stringp (car handles)))
5743                      (cdr handles)))
5744             (progn
5745               (when (and (not ihandles)
5746                          (not gnus-displaying-mime))
5747                 ;; Clean up for mime parts.
5748                 (article-goto-body)
5749                 (delete-region (point) (point-max)))
5750               (let ((gnus-displaying-mime t))
5751                 (gnus-mime-display-part handles)))
5752           (save-restriction
5753             (article-goto-body)
5754             (narrow-to-region (point) (point-max))
5755             (gnus-treat-article nil 1 1 "text/plain")
5756             (widen)))
5757         (unless ihandles
5758           ;; Highlight the headers.
5759           (save-excursion
5760             (save-restriction
5761               (article-goto-body)
5762               (narrow-to-region (point-min) (point))
5763               (gnus-article-save-original-date
5764                (gnus-treat-article 'head)))))))
5765     ;; Cope with broken MIME messages.
5766     (goto-char (point-max))
5767     (unless (bolp)
5768       (insert "\n"))))
5769
5770 (defcustom gnus-mime-display-multipart-as-mixed nil
5771   "Display \"multipart\" parts as  \"multipart/mixed\".
5772
5773 If t, it overrides nil values of
5774 `gnus-mime-display-multipart-alternative-as-mixed' and
5775 `gnus-mime-display-multipart-related-as-mixed'."
5776   :group 'gnus-article-mime
5777   :type 'boolean)
5778
5779 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5780   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5781   :version "22.1"
5782   :group 'gnus-article-mime
5783   :type 'boolean)
5784
5785 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5786   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5787
5788 If displaying \"text/html\" is discouraged \(see
5789 `mm-discouraged-alternatives'\) images or other material inside a
5790 \"multipart/related\" part might be overlooked when this variable is nil."
5791   :version "22.1"
5792   :group 'gnus-article-mime
5793   :type 'boolean)
5794
5795 (defun gnus-mime-display-part (handle)
5796   (cond
5797    ;; Maybe a broken MIME message.
5798    ((null handle))
5799    ;; Single part.
5800    ((not (stringp (car handle)))
5801     (gnus-mime-display-single handle))
5802    ;; User-defined multipart
5803    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5804     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5805              handle))
5806    ;; multipart/alternative
5807    ((and (equal (car handle) "multipart/alternative")
5808          (not (or gnus-mime-display-multipart-as-mixed
5809                   gnus-mime-display-multipart-alternative-as-mixed)))
5810     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5811       (push (cons id handle) gnus-article-mime-handle-alist)
5812       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5813    ;; multipart/related
5814    ((and (equal (car handle) "multipart/related")
5815          (not (or gnus-mime-display-multipart-as-mixed
5816                   gnus-mime-display-multipart-related-as-mixed)))
5817     ;;;!!!We should find the start part, but we just default
5818     ;;;!!!to the first part.
5819     ;;(gnus-mime-display-part (cadr handle))
5820     ;;;!!! Most multipart/related is an HTML message plus images.
5821     ;;;!!! Unfortunately we are unable to let W3 display those
5822     ;;;!!! included images, so we just display it as a mixed multipart.
5823     ;;(gnus-mime-display-mixed (cdr handle))
5824     ;;;!!! No, w3 can display everything just fine.
5825     (gnus-mime-display-part (cadr handle)))
5826    ((equal (car handle) "multipart/signed")
5827     (gnus-add-wash-type 'signed)
5828     (gnus-mime-display-security handle))
5829    ((equal (car handle) "multipart/encrypted")
5830     (gnus-add-wash-type 'encrypted)
5831     (gnus-mime-display-security handle))
5832    ;; Other multiparts are handled like multipart/mixed.
5833    (t
5834     (gnus-mime-display-mixed (cdr handle)))))
5835
5836 (defun gnus-mime-part-function (handles)
5837   (if (stringp (car handles))
5838       (mapcar 'gnus-mime-part-function (cdr handles))
5839     (funcall gnus-article-mime-part-function handles)))
5840
5841 (defun gnus-mime-display-mixed (handles)
5842   (mapcar 'gnus-mime-display-part handles))
5843
5844 (defun gnus-mime-display-single (handle)
5845   (let ((type (mm-handle-media-type handle))
5846         (ignored gnus-ignored-mime-types)
5847         (not-attachment t)
5848         (move nil)
5849         display text)
5850     (catch 'ignored
5851       (progn
5852         (while ignored
5853           (when (string-match (pop ignored) type)
5854             (throw 'ignored nil)))
5855         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5856                                (with-current-buffer gnus-summary-buffer
5857                                  gnus-inhibit-images)
5858                              gnus-inhibit-images)
5859                            (string-match "\\`image/" type)))
5860                  (setq not-attachment
5861                        (and (not (mm-inline-override-p handle))
5862                             (or (not (mm-handle-disposition handle))
5863                                 (equal (car (mm-handle-disposition handle))
5864                                        "inline")
5865                                 (mm-attachment-override-p handle))))
5866                  (mm-automatic-display-p handle)
5867                  (or (and
5868                       (mm-inlinable-p handle)
5869                       (mm-inlined-p handle))
5870                      (mm-automatic-external-display-p type)))
5871             (setq display t)
5872           (when (equal (mm-handle-media-supertype handle) "text")
5873             (setq text t)))
5874         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5875               beg)
5876           (push (cons id handle) gnus-article-mime-handle-alist)
5877           (when (and display
5878                      (equal (mm-handle-media-supertype handle) "message"))
5879             (insert-char
5880              ?\n
5881              (cond ((not (bolp)) 2)
5882                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5883                    (t 1))))
5884           (when (or (not display)
5885                     (not (gnus-unbuttonized-mime-type-p type))
5886                     (eq id gnus-mime-buttonized-part-id))
5887             (gnus-insert-mime-button
5888              handle id (list (or display (and not-attachment text))))
5889             (gnus-article-insert-newline)
5890             ;; Remember modify the number of forward lines.
5891             (setq move t))
5892           (setq beg (point))
5893           (cond
5894            (display
5895             (when move
5896               (forward-line -1)
5897               (setq beg (point)))
5898             (let ((mail-parse-charset gnus-newsgroup-charset)
5899                   (mail-parse-ignored-charsets
5900                    (save-excursion (condition-case ()
5901                                        (set-buffer gnus-summary-buffer)
5902                                      (error))
5903                                    gnus-newsgroup-ignored-charsets)))
5904               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5905             (goto-char (point-max)))
5906            ((and text not-attachment)
5907             (when move
5908               (forward-line -1)
5909               (setq beg (point)))
5910             (gnus-article-insert-newline)
5911             (mm-display-inline handle)
5912             (goto-char (point-max))))
5913           ;; Do highlighting.
5914           (save-excursion
5915             (save-restriction
5916               (narrow-to-region beg (point))
5917               (if (eq handle gnus-article-mime-handles)
5918                   ;; The format=flowed case.
5919                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5920                 ;; Don't count signature parts that are never displayed.
5921                 ;; The part number should be re-calculated supposing this
5922                 ;; might be a message/rfc822 part.
5923                 (let (handles)
5924                   (dolist (part gnus-article-mime-handles)
5925                     (unless (or (stringp part)
5926                                 (equal (car (mm-handle-type part))
5927                                        "application/pgp-signature"))
5928                       (push part handles)))
5929                   (gnus-treat-article
5930                    nil (length (memq handle handles)) (length handles)
5931                    (mm-handle-media-type handle)))))))))))
5932
5933 (defun gnus-unbuttonized-mime-type-p (type)
5934   "Say whether TYPE is to be unbuttonized."
5935   (unless gnus-inhibit-mime-unbuttonizing
5936     (when (catch 'found
5937             (let ((types gnus-unbuttonized-mime-types))
5938               (while types
5939                 (when (string-match (pop types) type)
5940                   (throw 'found t)))))
5941       (not (catch 'found
5942              (let ((types gnus-buttonized-mime-types))
5943                (while types
5944                  (when (string-match (pop types) type)
5945                    (throw 'found t)))))))))
5946
5947 (defun gnus-article-insert-newline ()
5948   "Insert a newline, but mark it as undeletable."
5949   (gnus-put-text-property
5950    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5951
5952 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5953   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5954          (ihandles handles)
5955          (point (point))
5956          handle (inhibit-read-only t) from props begend not-pref)
5957     (save-window-excursion
5958       (save-restriction
5959         (when ibegend
5960           (narrow-to-region (car ibegend)
5961                             (or (cdr ibegend)
5962                                 (progn
5963                                   (goto-char (car ibegend))
5964                                   (forward-line 2)
5965                                   (point))))
5966           (delete-region (point-min) (point-max))
5967           (mm-remove-parts handles))
5968         (setq begend (list (point-marker)))
5969         ;; Do the toggle.
5970         (unless (setq not-pref (cadr (member preferred ihandles)))
5971           (setq not-pref (car ihandles)))
5972         (when (or ibegend
5973                   (not preferred)
5974                   (not (gnus-unbuttonized-mime-type-p
5975                         "multipart/alternative")))
5976           (gnus-add-text-properties
5977            (setq from (point))
5978            (progn
5979              (insert (format "%d.  " id))
5980              (point))
5981            `(gnus-callback
5982              (lambda (handles)
5983                (unless ,(not ibegend)
5984                  (setq gnus-article-mime-handle-alist
5985                        ',gnus-article-mime-handle-alist))
5986                (gnus-mime-display-alternative
5987                 ',ihandles ',not-pref ',begend ,id))
5988              keymap ,gnus-mime-button-map
5989              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5990              face ,gnus-article-button-face
5991              gnus-part ,id
5992              article-type multipart))
5993           (widget-convert-button 'link from (point)
5994                                  :action 'gnus-widget-press-button
5995                                  :button-keymap gnus-widget-button-keymap)
5996           ;; Do the handles
5997           (while (setq handle (pop handles))
5998             (gnus-add-text-properties
5999              (setq from (point))
6000              (progn
6001                (insert (format "(%c) %-18s"
6002                                (if (equal handle preferred) ?* ? )
6003                                (mm-handle-media-type handle)))
6004                (point))
6005              `(gnus-callback
6006                (lambda (handles)
6007                  (unless ,(not ibegend)
6008                    (setq gnus-article-mime-handle-alist
6009                          ',gnus-article-mime-handle-alist))
6010                  (gnus-mime-display-alternative
6011                   ',ihandles ',handle ',begend ,id))
6012                keymap ,gnus-mime-button-map
6013                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6014                face ,gnus-article-button-face
6015                gnus-part ,id
6016                gnus-data ,handle))
6017             (widget-convert-button 'link from (point)
6018                                    :action 'gnus-widget-press-button
6019                                    :button-keymap gnus-widget-button-keymap)
6020             (insert "  "))
6021           (insert "\n\n"))
6022         (when preferred
6023           (if (stringp (car preferred))
6024               (gnus-display-mime preferred)
6025             (let ((mail-parse-charset gnus-newsgroup-charset)
6026                   (mail-parse-ignored-charsets
6027                    (with-current-buffer gnus-summary-buffer
6028                      gnus-newsgroup-ignored-charsets)))
6029               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6030               ;; Do highlighting.
6031               (save-excursion
6032                 (save-restriction
6033                   (narrow-to-region (car begend) (point-max))
6034                   (gnus-treat-article
6035                    nil (length gnus-article-mime-handle-alist)
6036                    (gnus-article-mime-total-parts)
6037                    (mm-handle-media-type preferred))))))
6038           (goto-char (point-max))
6039           (setcdr begend (point-marker)))))
6040     (when ibegend
6041       (goto-char point))))
6042
6043 (defconst gnus-article-wash-status-strings
6044   (let ((alist '((cite "c" "Possible hidden citation text"
6045                        " " "All citation text visible")
6046                  (headers "h" "Hidden headers"
6047                           " " "All headers visible.")
6048                  (pgp "p" "Encrypted or signed message status hidden"
6049                       " " "No hidden encryption nor digital signature status")
6050                  (signature "s" "Signature has been hidden"
6051                             " " "Signature is visible")
6052                  (overstrike "o" "Overstrike (^H) characters applied"
6053                              " " "No overstrike characters applied")
6054                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6055                            " " "No /*_emphasis_*/ characters applied")))
6056         result)
6057     (dolist (entry alist result)
6058       (let ((key (nth 0 entry))
6059             (on (copy-sequence (nth 1 entry)))
6060             (on-help (nth 2 entry))
6061             (off (copy-sequence (nth 3 entry)))
6062             (off-help (nth 4 entry)))
6063         (put-text-property 0 1 'help-echo on-help on)
6064         (put-text-property 0 1 'help-echo off-help off)
6065         (push (list key on off) result))))
6066   "Alist of strings describing wash status in the mode line.
6067 Each entry has the form (KEY ON OF), where the KEY is a symbol
6068 representing the particular washing function, ON is the string to use
6069 in the article mode line when the washing function is active, and OFF
6070 is the string to use when it is inactive.")
6071
6072 (defun gnus-article-wash-status-entry (key value)
6073   (let ((entry (assoc key gnus-article-wash-status-strings)))
6074     (if value (nth 1 entry) (nth 2 entry))))
6075
6076 (defun gnus-article-wash-status ()
6077   "Return a string which display status of article washing."
6078   (with-current-buffer gnus-article-buffer
6079     (let ((cite (memq 'cite gnus-article-wash-types))
6080           (headers (memq 'headers gnus-article-wash-types))
6081           (boring (memq 'boring-headers gnus-article-wash-types))
6082           (pgp (memq 'pgp gnus-article-wash-types))
6083           (pem (memq 'pem gnus-article-wash-types))
6084           (signed (memq 'signed gnus-article-wash-types))
6085           (encrypted (memq 'encrypted gnus-article-wash-types))
6086           (signature (memq 'signature gnus-article-wash-types))
6087           (overstrike (memq 'overstrike gnus-article-wash-types))
6088           (emphasis (memq 'emphasis gnus-article-wash-types)))
6089       (concat
6090        (gnus-article-wash-status-entry 'cite cite)
6091        (gnus-article-wash-status-entry 'headers (or headers boring))
6092        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6093        (gnus-article-wash-status-entry 'signature signature)
6094        (gnus-article-wash-status-entry 'overstrike overstrike)
6095        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6096
6097 (defun gnus-add-wash-type (type)
6098   "Add a washing of TYPE to the current status."
6099   (add-to-list 'gnus-article-wash-types type))
6100
6101 (defun gnus-delete-wash-type (type)
6102   "Add a washing of TYPE to the current status."
6103   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6104
6105 (defun gnus-add-image (category image)
6106   "Add IMAGE of CATEGORY to the list of displayed images."
6107   (let ((entry (assq category gnus-article-image-alist)))
6108     (unless entry
6109       (setq entry (list category))
6110       (push entry gnus-article-image-alist))
6111     (nconc entry (list image))))
6112
6113 (defun gnus-delete-images (category)
6114   "Delete all images in CATEGORY."
6115   (let ((entry (assq category gnus-article-image-alist)))
6116     (dolist (image (cdr entry))
6117       (gnus-remove-image image category))
6118     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6119     (gnus-delete-wash-type category)))
6120
6121 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6122
6123 (defun gnus-article-maybe-hide-headers ()
6124   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6125 Provided for backwards compatibility."
6126   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6127                  (not (with-current-buffer gnus-summary-buffer
6128                         gnus-have-all-headers)))
6129              (not gnus-inhibit-hiding))
6130     (gnus-article-hide-headers)))
6131
6132 ;;; Article savers.
6133
6134 (defun gnus-output-to-file (file-name)
6135   "Append the current article to a file named FILE-NAME.
6136 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6137 text and used as the value of the coding cookie which is added to the
6138 top of a file.  Otherwise, this function saves a raw article without
6139 the coding cookie."
6140   (let* ((artbuf (current-buffer))
6141          (file-name-coding-system nnmail-pathname-coding-system)
6142          (coding gnus-article-save-coding-system)
6143          (coding-system-for-read (if coding
6144                                      nil ;; Rely on the coding cookie.
6145                                    mm-text-coding-system))
6146          (coding-system-for-write (or coding
6147                                       mm-text-coding-system-for-write
6148                                       mm-text-coding-system))
6149          (exists (file-exists-p file-name)))
6150     (with-temp-buffer
6151       (when exists
6152         (insert-file-contents file-name)
6153         (goto-char (point-min))
6154         ;; Remove the existing coding cookie.
6155         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6156           (delete-region (match-beginning 0) (match-end 0))))
6157       (goto-char (point-max))
6158       (insert-buffer-substring artbuf)
6159       ;; Append newline at end of the buffer as separator, and then
6160       ;; save it to file.
6161       (goto-char (point-max))
6162       (insert "\n")
6163       (when coding
6164         ;; If the coding system is not suitable to encode the text,
6165         ;; ask a user for a proper one.
6166         (when (fboundp 'select-safe-coding-system)
6167           (setq coding (coding-system-base
6168                         (save-window-excursion
6169                           (select-safe-coding-system (point-min) (point-max)
6170                                                      coding))))
6171           (setq coding-system-for-write
6172                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6173                     coding)))
6174         (goto-char (point-min))
6175         ;; Add the coding cookie.
6176         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6177                         coding-system-for-write)))
6178       (if exists
6179           (progn
6180             (write-region (point-min) (point-max) file-name nil 'no-message)
6181             (message "Appended to %s" file-name))
6182         (write-region (point-min) (point-max) file-name))))
6183   t)
6184
6185 (defun gnus-narrow-to-page (&optional arg)
6186   "Narrow the article buffer to a page.
6187 If given a numerical ARG, move forward ARG pages."
6188   (interactive "P")
6189   (setq arg (if arg (prefix-numeric-value arg) 0))
6190   (with-current-buffer gnus-article-buffer
6191     (widen)
6192     ;; Remove any old next/prev buttons.
6193     (when (gnus-visual-p 'page-marker)
6194       (let ((inhibit-read-only t))
6195         (gnus-remove-text-with-property 'gnus-prev)
6196         (gnus-remove-text-with-property 'gnus-next)))
6197     (let (st nd pt)
6198       (when (save-excursion
6199               (cond ((< arg 0)
6200                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6201                          (prog1
6202                              (setq nd (match-beginning 0)
6203                                    pt nd)
6204                            (when (re-search-backward page-delimiter nil t)
6205                              (setq st (match-end 0))))
6206                        (when (re-search-forward page-delimiter nil t)
6207                          (setq nd (match-beginning 0)
6208                                pt (point-min)))))
6209                     ((> arg 0)
6210                      (if (re-search-forward page-delimiter nil 'move arg)
6211                          (prog1
6212                              (setq st (match-end 0)
6213                                    pt st)
6214                            (when (re-search-forward page-delimiter nil t)
6215                              (setq nd (match-beginning 0))))
6216                        (when (re-search-backward page-delimiter nil t)
6217                          (setq st (match-end 0)
6218                                pt (point-max)))))
6219                     (t
6220                      (when (re-search-backward page-delimiter nil t)
6221                        (goto-char (setq st (match-end 0))))
6222                      (when (re-search-forward page-delimiter nil t)
6223                        (setq nd (match-beginning 0)))
6224                      (or st nd))))
6225         (setq gnus-page-broken t)
6226         (when pt (goto-char pt))
6227         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6228         (when (gnus-visual-p 'page-marker)
6229           (save-excursion
6230             (when nd
6231               (goto-char nd)
6232               (gnus-insert-next-page-button))
6233             (when st
6234               (goto-char st)
6235               (gnus-insert-prev-page-button))))))))
6236
6237 ;; Article mode commands
6238
6239 (defun gnus-article-goto-next-page ()
6240   "Show the next page of the article."
6241   (interactive)
6242   (when (gnus-article-next-page)
6243     (goto-char (point-min))
6244     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6245
6246
6247 (defun gnus-article-goto-prev-page ()
6248   "Show the previous page of the article."
6249   (interactive)
6250   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6251       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6252     (gnus-article-prev-page nil)))
6253
6254 ;; This is cleaner but currently breaks `gnus-pick-mode':
6255 ;;
6256 ;; (defun gnus-article-goto-next-page ()
6257 ;;   "Show the next page of the article."
6258 ;;   (interactive)
6259 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6260 ;;     (gnus-summary-next-page)))
6261 ;;
6262 ;; (defun gnus-article-goto-prev-page ()
6263 ;;   "Show the next page of the article."
6264 ;;   (interactive)
6265 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6266 ;;     (gnus-summary-prev-page)))
6267
6268 (defun gnus-article-next-page (&optional lines)
6269   "Show the next page of the current article.
6270 If end of article, return non-nil.  Otherwise return nil.
6271 Argument LINES specifies lines to be scrolled up."
6272   (interactive "p")
6273   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6274   (if (and (not (and gnus-article-over-scroll
6275                      (> (count-lines (window-start) (point-max))
6276                         (if (featurep 'xemacs)
6277                             (or lines (1- (window-height)))
6278                           (+ (or lines (1- (window-height))) scroll-margin)))))
6279            (save-excursion
6280              (end-of-line)
6281              (and (pos-visible-in-window-p)     ;Not continuation line.
6282                   (>= (point) (point-max)))))
6283       ;; Nothing in this page.
6284       (if (or (not gnus-page-broken)
6285               (save-excursion
6286                 (save-restriction
6287                   (widen)
6288                   (forward-line)
6289                   (eobp)))) ;Real end-of-buffer?
6290           (progn
6291             (when gnus-article-over-scroll
6292               (gnus-article-next-page-1 lines))
6293             t)                  ;Nothing more.
6294         (gnus-narrow-to-page 1)         ;Go to next page.
6295         nil)
6296     ;; More in this page.
6297     (gnus-article-next-page-1 lines)
6298     nil))
6299
6300 (defun gnus-article-beginning-of-window ()
6301   "Move point to the beginning of the window.
6302 In Emacs, the point is placed at the line number which `scroll-margin'
6303 specifies."
6304   (if (featurep 'xemacs)
6305       (move-to-window-line 0)
6306     ;; There is an obscure bug in Emacs that makes it impossible to
6307     ;; scroll past big pictures in the article buffer.  Try to fix
6308     ;; this by adding a sanity check by counting the lines visible.
6309     (when (> (count-lines (window-start) (window-end)) 30)
6310       (move-to-window-line
6311        (min (max 0 scroll-margin)
6312             (max 1 (- (window-height)
6313                       (if mode-line-format 1 0)
6314                       (if header-line-format 1 0)
6315                       2)))))))
6316
6317 (defun gnus-article-next-page-1 (lines)
6318   (condition-case ()
6319       (let ((scroll-in-place nil))
6320         (scroll-up lines))
6321     (end-of-buffer
6322      ;; Long lines may cause an end-of-buffer error.
6323      (goto-char (point-max))))
6324   (gnus-article-beginning-of-window))
6325
6326 (defun gnus-article-prev-page (&optional lines)
6327   "Show previous page of current article.
6328 Argument LINES specifies lines to be scrolled down."
6329   (interactive "p")
6330   (move-to-window-line 0)
6331   (if (and gnus-page-broken
6332            (bobp)
6333            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6334       (progn
6335         (gnus-narrow-to-page -1)        ;Go to previous page.
6336         (goto-char (point-max))
6337         (recenter (if gnus-article-over-scroll
6338                       (if lines
6339                           (max (if (featurep 'xemacs)
6340                                    lines
6341                                  (+ lines scroll-margin))
6342                                3)
6343                         (- (window-height) 2))
6344                     -1)))
6345     (prog1
6346         (condition-case ()
6347             (let ((scroll-in-place nil))
6348               (scroll-down lines))
6349           (beginning-of-buffer
6350            (goto-char (point-min))))
6351       (gnus-article-beginning-of-window))))
6352
6353 (defun gnus-article-only-boring-p ()
6354   "Decide whether there is only boring text remaining in the article.
6355 Something \"interesting\" is a word of at least two letters that does
6356 not have a face in `gnus-article-boring-faces'."
6357   (when (and gnus-article-skip-boring
6358              (boundp 'gnus-article-boring-faces)
6359              (symbol-value 'gnus-article-boring-faces))
6360     (save-excursion
6361       (let ((inhibit-point-motion-hooks t))
6362         (catch 'only-boring
6363           (while (re-search-forward "\\b\\w\\w" nil t)
6364             (forward-char -1)
6365             (when (not (gnus-intersection
6366                         (gnus-faces-at (point))
6367                         (symbol-value 'gnus-article-boring-faces)))
6368               (throw 'only-boring nil)))
6369           (throw 'only-boring t))))))
6370
6371 (defun gnus-article-refer-article ()
6372   "Read article specified by message-id around point."
6373   (interactive)
6374   (save-excursion
6375     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6376     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6377     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6378         (let ((msg-id (concat "<" (match-string 0) ">")))
6379           (set-buffer gnus-summary-buffer)
6380           (gnus-summary-refer-article msg-id))
6381       (error "No references around point"))))
6382
6383 (defun gnus-article-show-summary ()
6384   "Reconfigure windows to show summary buffer."
6385   (interactive)
6386   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6387       (error "There is no summary buffer for this article buffer")
6388     (gnus-article-set-globals)
6389     (gnus-configure-windows 'article)
6390     (gnus-summary-goto-subject gnus-current-article)
6391     (gnus-summary-position-point)))
6392
6393 (defun gnus-article-describe-briefly ()
6394   "Describe article mode commands briefly."
6395   (interactive)
6396   (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")))
6397
6398 (defun gnus-article-check-buffer ()
6399   "Beep if not in an article buffer."
6400   (unless (equal major-mode 'gnus-article-mode)
6401     (error "Command invoked outside of a Gnus article buffer")))
6402
6403 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6404   "Read a summary buffer key sequence and execute it from the article buffer."
6405   (interactive "P")
6406   (gnus-article-check-buffer)
6407   (let ((nosaves
6408          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6409            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6410            "=" "^" "\M-^" "|"))
6411         (nosave-but-article
6412          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6413            "An" "Ap" [?A (meta return)] [?A delete]))
6414         (nosave-in-article
6415          '("AS" "\C-d"))
6416         (up-to-top
6417          '("n" "Gn" "p" "Gp"))
6418         keys new-sum-point)
6419     (with-current-buffer gnus-article-current-summary
6420       (let (gnus-pick-mode)
6421         (setq unread-command-events (nconc unread-command-events
6422                                            (list (or key last-command-event)))
6423               keys (if (featurep 'xemacs)
6424                        (events-to-keys (read-key-sequence nil t))
6425                      (read-key-sequence nil t)))))
6426
6427     (message "")
6428
6429     (cond
6430      ((eq (aref keys (1- (length keys))) ?\C-h)
6431       (gnus-article-describe-bindings (substring keys 0 -1)))
6432      ((or (member keys nosaves)
6433           (member keys nosave-but-article)
6434           (member keys nosave-in-article))
6435       (let (func)
6436         (save-window-excursion
6437           (pop-to-buffer gnus-article-current-summary)
6438           ;; We disable the pick minor mode commands.
6439           (let (gnus-pick-mode)
6440             (setq func (key-binding keys t))))
6441         (if (or (not func)
6442                 (numberp func))
6443             (ding)
6444           (unless (member keys nosave-in-article)
6445             (set-buffer gnus-article-current-summary))
6446           (when (get func 'disabled)
6447             (error "Function %s disabled" func))
6448           (call-interactively func)
6449           (setq new-sum-point (point)))
6450         (when (member keys nosave-but-article)
6451           (pop-to-buffer gnus-article-buffer))))
6452      (t
6453       ;; These commands should restore window configuration.
6454       (let ((obuf (current-buffer))
6455             (owin (current-window-configuration))
6456             win func in-buffer selected new-sum-start new-sum-hscroll err)
6457         (cond (not-restore-window
6458                (pop-to-buffer gnus-article-current-summary)
6459                (setq win (selected-window)))
6460               ((setq win (get-buffer-window gnus-article-current-summary))
6461                (select-window win))
6462               (t
6463                (let ((summary-buffer gnus-article-current-summary))
6464                  (gnus-configure-windows 'article)
6465                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6466                    (let ((gnus-buffer-configuration
6467                           '((article ((vertical 1.0
6468                                                 (summary 0.25 point)
6469                                                 (article 1.0)))))))
6470                      (gnus-configure-windows 'article))
6471                    (setq win (get-buffer-window summary-buffer 'visible)))
6472                  (gnus-select-frame-set-input-focus (window-frame win))
6473                  (select-window win))))
6474         (setq in-buffer (current-buffer))
6475         ;; We disable the pick minor mode commands.
6476         (setq func (let (gnus-pick-mode)
6477                      (key-binding keys t)))
6478         (when (get func 'disabled)
6479           (error "Function %s disabled" func))
6480         (if (and func
6481                  (functionp func)
6482                  (condition-case code
6483                      (progn
6484                        (call-interactively func)
6485                        t)
6486                    (error
6487                     (setq err code)
6488                     nil)))
6489             (progn
6490               (when (eq win (selected-window))
6491                 (setq new-sum-point (point)
6492                       new-sum-start (window-start win)
6493                       new-sum-hscroll (window-hscroll win)))
6494               (when (or (eq in-buffer (current-buffer))
6495                         (when (eq obuf (current-buffer))
6496                           (set-buffer in-buffer)
6497                           t))
6498                 (setq selected (gnus-summary-select-article))
6499                 (set-buffer obuf)
6500                 (unless not-restore-window
6501                   (set-window-configuration owin))
6502                 (when (and (eq selected 'old)
6503                            new-sum-point)
6504                   (set-window-start (get-buffer-window (current-buffer))
6505                                     1)
6506                   (set-window-point (get-buffer-window (current-buffer))
6507                                     (if (article-goto-body)
6508                                         (1- (point))
6509                                       (point))))
6510                 (when (and (not not-restore-window)
6511                            new-sum-point
6512                            (window-live-p win)
6513                            (with-current-buffer (window-buffer win)
6514                              (eq major-mode 'gnus-summary-mode)))
6515                   (set-window-point win new-sum-point)
6516                   (set-window-start win new-sum-start)
6517                   (set-window-hscroll win new-sum-hscroll))))
6518           (set-window-configuration owin)
6519           (if err
6520               (signal (car err) (cdr err))
6521             (ding))))))))
6522
6523 (defun gnus-article-read-summary-send-keys ()
6524   (interactive)
6525   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6526     (gnus-article-read-summary-keys)))
6527
6528 (defun gnus-article-describe-key (key)
6529   "Display documentation of the function invoked by KEY.
6530 KEY is a string or a vector."
6531   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6532                        (read-key-sequence "Describe key: "))))
6533   (gnus-article-check-buffer)
6534   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6535                                   gnus-article-read-summary-send-keys))
6536       (with-current-buffer gnus-article-current-summary
6537         (setq unread-command-events
6538               (if (featurep 'xemacs)
6539                   (append key nil)
6540                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6541                                         (list 'meta (- x 128))
6542                                       x))
6543                         key)))
6544         (let ((cursor-in-echo-area t)
6545               gnus-pick-mode)
6546           (describe-key (read-key-sequence nil t))))
6547     (describe-key key)))
6548
6549 (defun gnus-article-describe-key-briefly (key &optional insert)
6550   "Display documentation of the function invoked by KEY.
6551 KEY is a string or a vector."
6552   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6553                        (read-key-sequence "Describe key: "))
6554                      current-prefix-arg))
6555   (gnus-article-check-buffer)
6556   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6557                                   gnus-article-read-summary-send-keys))
6558       (with-current-buffer gnus-article-current-summary
6559         (setq unread-command-events
6560               (if (featurep 'xemacs)
6561                   (append key nil)
6562                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6563                                         (list 'meta (- x 128))
6564                                       x))
6565                         key)))
6566         (let ((cursor-in-echo-area t)
6567               gnus-pick-mode)
6568           (describe-key-briefly (read-key-sequence nil t) insert)))
6569     (describe-key-briefly key insert)))
6570
6571 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6572 (defvar gnus-agent-summary-mode)
6573 (defvar gnus-draft-mode)
6574 ;; Calling help-buffer will autoload help-mode.
6575 (defvar help-xref-stack-item)
6576 ;; Emacs 22 doesn't load it in the batch mode.
6577 (eval-when-compile
6578   (autoload 'help-buffer "help-mode"))
6579
6580 (defun gnus-article-describe-bindings (&optional prefix)
6581   "Show a list of all defined keys, and their definitions.
6582 The optional argument PREFIX, if non-nil, should be a key sequence;
6583 then we display only bindings that start with that prefix."
6584   (interactive)
6585   (gnus-article-check-buffer)
6586   (let ((keymap (copy-keymap gnus-article-mode-map))
6587         (map (copy-keymap gnus-article-send-map))
6588         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6589         parent agent draft)
6590     (define-key keymap "S" map)
6591     (define-key map [t] nil)
6592     (with-current-buffer gnus-article-current-summary
6593       (set-keymap-parent
6594        keymap
6595        (if (setq parent (keymap-parent gnus-article-mode-map))
6596            (prog1
6597                (setq parent (copy-keymap parent))
6598              (set-keymap-parent parent (current-local-map)))
6599          (current-local-map)))
6600       (set-keymap-parent map (key-binding "S"))
6601       (let (key def gnus-pick-mode)
6602         (while sumkeys
6603           (setq key (pop sumkeys))
6604           (cond ((and (vectorp key) (= (length key) 1)
6605                       (consp (setq def (aref key 0)))
6606                       (numberp (car def)) (numberp (cdr def)))
6607                  (when (< (max (car def) (cdr def)) 128)
6608                    (setq sumkeys
6609                          (append (mapcar
6610                                   #'vector
6611                                   (nreverse (gnus-uncompress-range def)))
6612                                  sumkeys))))
6613                 ((setq def (key-binding key))
6614                  (unless (eq def 'undefined)
6615                    (define-key keymap key def))))))
6616       (when (boundp 'gnus-agent-summary-mode)
6617         (setq agent gnus-agent-summary-mode))
6618       (when (boundp 'gnus-draft-mode)
6619         (setq draft gnus-draft-mode)))
6620     (with-temp-buffer
6621       (use-local-map keymap)
6622       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6623       (set (make-local-variable 'gnus-draft-mode) draft)
6624       (describe-bindings prefix))
6625     (let ((item `((lambda (prefix)
6626                     (with-current-buffer ,(current-buffer)
6627                       (gnus-article-describe-bindings prefix)))
6628                   ,prefix)))
6629       (with-current-buffer (let (help-xref-following) (help-buffer))
6630         (setq help-xref-stack-item item)))))
6631
6632 (defun gnus-article-reply-with-original (&optional wide)
6633   "Start composing a reply mail to the current message.
6634 The text in the region will be yanked.  If the region isn't active,
6635 the entire article will be yanked."
6636   (interactive)
6637   (let ((article (cdr gnus-article-current))
6638         contents)
6639     (if (not (gnus-region-active-p))
6640         (with-current-buffer gnus-summary-buffer
6641           (gnus-summary-reply (list (list article)) wide))
6642       (setq contents (buffer-substring (point) (mark t)))
6643       ;; Deactivate active regions.
6644       (when (and (boundp 'transient-mark-mode)
6645                  transient-mark-mode)
6646         (setq mark-active nil))
6647       (with-current-buffer gnus-summary-buffer
6648         (gnus-summary-reply
6649          (list (list article contents)) wide)))))
6650
6651 (defun gnus-article-wide-reply-with-original ()
6652   "Start composing a wide reply mail to the current message.
6653 The text in the region will be yanked.  If the region isn't active,
6654 the entire article will be yanked."
6655   (interactive)
6656   (gnus-article-reply-with-original t))
6657
6658 (defun gnus-article-followup-with-original ()
6659   "Compose a followup to the current article.
6660 The text in the region will be yanked.  If the region isn't active,
6661 the entire article will be yanked."
6662   (interactive)
6663   (let ((article (cdr gnus-article-current))
6664         contents)
6665       (if (not (gnus-region-active-p))
6666           (with-current-buffer gnus-summary-buffer
6667             (gnus-summary-followup (list (list article))))
6668         (setq contents (buffer-substring (point) (mark t)))
6669         ;; Deactivate active regions.
6670         (when (and (boundp 'transient-mark-mode)
6671                    transient-mark-mode)
6672           (setq mark-active nil))
6673         (with-current-buffer gnus-summary-buffer
6674           (gnus-summary-followup
6675            (list (list article contents)))))))
6676
6677 (defun gnus-article-hide (&optional arg force)
6678   "Hide all the gruft in the current article.
6679 This means that signatures, cited text and (some) headers will be
6680 hidden.
6681 If given a prefix, show the hidden text instead."
6682   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6683   (gnus-article-hide-headers arg)
6684   (gnus-article-hide-list-identifiers arg)
6685   (gnus-article-hide-citation-maybe arg force)
6686   (gnus-article-hide-signature arg))
6687
6688 (defun gnus-article-maybe-highlight ()
6689   "Do some article highlighting if article highlighting is requested."
6690   (when (gnus-visual-p 'article-highlight 'highlight)
6691     (gnus-article-highlight-some)))
6692
6693 (defun gnus-check-group-server ()
6694   ;; Make sure the connection to the server is alive.
6695   (unless (gnus-server-opened
6696            (gnus-find-method-for-group gnus-newsgroup-name))
6697     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6698     (gnus-request-group gnus-newsgroup-name t)))
6699
6700 (eval-when-compile
6701   (autoload 'nneething-get-file-name "nneething"))
6702
6703 (defun gnus-request-article-this-buffer (article group)
6704   "Get an article and insert it into this buffer."
6705   (let (do-update-line sparse-header)
6706     (prog1
6707         (save-excursion
6708           (erase-buffer)
6709           (gnus-kill-all-overlays)
6710           (setq group (or group gnus-newsgroup-name))
6711
6712           ;; Using `gnus-request-article' directly will insert the article into
6713           ;; `nntp-server-buffer' - so we'll save some time by not having to
6714           ;; copy it from the server buffer into the article buffer.
6715
6716           ;; We only request an article by message-id when we do not have the
6717           ;; headers for it, so we'll have to get those.
6718           (when (stringp article)
6719             (gnus-read-header article))
6720
6721           ;; If the article number is negative, that means that this article
6722           ;; doesn't belong in this newsgroup (possibly), so we find its
6723           ;; message-id and request it by id instead of number.
6724           (when (and (numberp article)
6725                      gnus-summary-buffer
6726                      (get-buffer gnus-summary-buffer)
6727                      (gnus-buffer-exists-p gnus-summary-buffer))
6728             (with-current-buffer gnus-summary-buffer
6729               (let ((header (gnus-summary-article-header article)))
6730                 (when (< article 0)
6731                   (cond
6732                    ((memq article gnus-newsgroup-sparse)
6733                     ;; This is a sparse gap article.
6734                     (setq do-update-line article)
6735                     (setq article (mail-header-id header))
6736                     (setq sparse-header (gnus-read-header article))
6737                     (setq gnus-newsgroup-sparse
6738                           (delq article gnus-newsgroup-sparse)))
6739                    ((vectorp header)
6740                     ;; It's a real article.
6741                     (setq article (mail-header-id header)))
6742                    (t
6743                     ;; It is an extracted pseudo-article.
6744                     (setq article 'pseudo)
6745                     (gnus-request-pseudo-article header))))
6746
6747                 (let ((method (gnus-find-method-for-group
6748                                gnus-newsgroup-name)))
6749                   (when (and (eq (car method) 'nneething)
6750                              (vectorp header))
6751                     (let ((dir (nneething-get-file-name
6752                                 (mail-header-id header))))
6753                       (when (and (stringp dir)
6754                                  (file-directory-p dir))
6755                         (setq article 'nneething)
6756                         (gnus-group-enter-directory dir))))))))
6757
6758           (cond
6759            ;; Refuse to select canceled articles.
6760            ((and (numberp article)
6761                  gnus-summary-buffer
6762                  (get-buffer gnus-summary-buffer)
6763                  (gnus-buffer-exists-p gnus-summary-buffer)
6764                  (eq (cdr (with-current-buffer gnus-summary-buffer
6765                             (assq article gnus-newsgroup-reads)))
6766                      gnus-canceled-mark))
6767             nil)
6768            ;; We first check `gnus-original-article-buffer'.
6769            ((and (get-buffer gnus-original-article-buffer)
6770                  (numberp article)
6771                  (with-current-buffer gnus-original-article-buffer
6772                    (and (equal (car gnus-original-article) group)
6773                         (eq (cdr gnus-original-article) article))))
6774             ;; `insert-buffer-substring' would incorrectly use the
6775             ;; equivalent of string-make-multibyte which amount to decoding
6776             ;; with locale-coding-system, causing failure of
6777             ;; subsequent decoding.
6778             (insert (mm-string-to-multibyte
6779                      (with-current-buffer gnus-original-article-buffer
6780                        (buffer-substring (point-min) (point-max)))))
6781             'article)
6782            ;; Check the backlog.
6783            ((and gnus-keep-backlog
6784                  (gnus-backlog-request-article group article (current-buffer)))
6785             'article)
6786            ;; Check asynchronous pre-fetch.
6787            ((gnus-async-request-fetched-article group article (current-buffer))
6788             (gnus-async-prefetch-next group article gnus-summary-buffer)
6789             (when (and (numberp article) gnus-keep-backlog)
6790               (gnus-backlog-enter-article group article (current-buffer)))
6791             'article)
6792            ;; Check the cache.
6793            ((and gnus-use-cache
6794                  (numberp article)
6795                  (gnus-cache-request-article article group))
6796             'article)
6797            ;; Check the agent cache.
6798            ((gnus-agent-request-article article group)
6799             'article)
6800            ;; Get the article and put into the article buffer.
6801            ((or (stringp article)
6802                 (numberp article))
6803             (let ((gnus-override-method gnus-override-method)
6804                   (methods (and (stringp article)
6805                                 gnus-refer-article-method))
6806                   (backend (car (gnus-find-method-for-group
6807                                  gnus-newsgroup-name)))
6808                   result
6809                   (inhibit-read-only t))
6810               (if (or (not (listp methods))
6811                       (and (symbolp (car methods))
6812                            (assq (car methods) nnoo-definition-alist)))
6813                   (setq methods (list methods)))
6814               (when (and (null gnus-override-method)
6815                          methods)
6816                 (setq gnus-override-method (pop methods)))
6817               (while (not result)
6818                 (when (eq gnus-override-method 'current)
6819                   (setq gnus-override-method
6820                         (with-current-buffer gnus-summary-buffer
6821                           gnus-current-select-method)))
6822                 (erase-buffer)
6823                 (gnus-kill-all-overlays)
6824                 (let ((gnus-newsgroup-name group))
6825                   (gnus-check-group-server))
6826                 (cond
6827                  ((gnus-request-article article group (current-buffer))
6828                   (when (numberp article)
6829                     (gnus-async-prefetch-next group article
6830                                               gnus-summary-buffer)
6831                     (when gnus-keep-backlog
6832                       (gnus-backlog-enter-article
6833                        group article (current-buffer))))
6834                   (setq result 'article))
6835                  (methods
6836                   (setq gnus-override-method (pop methods)))
6837                  ((not (string-match "^400 "
6838                                      (nnheader-get-report backend)))
6839                   ;; If we get 400 server disconnect, reconnect and
6840                   ;; retry; otherwise, assume the article has expired.
6841                   (setq result 'done))))
6842               (and (eq result 'article) 'article)))
6843            ;; It was a pseudo.
6844            (t article)))
6845
6846       ;; Associate this article with the current summary buffer.
6847       (setq gnus-article-current-summary gnus-summary-buffer)
6848
6849       ;; Take the article from the original article buffer
6850       ;; and place it in the buffer it's supposed to be in.
6851       (when (and (get-buffer gnus-article-buffer)
6852                  (equal (buffer-name (current-buffer))
6853                         (buffer-name (get-buffer gnus-article-buffer))))
6854         (save-excursion
6855           (if (get-buffer gnus-original-article-buffer)
6856               (set-buffer gnus-original-article-buffer)
6857             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6858             (buffer-disable-undo)
6859             (setq major-mode 'gnus-original-article-mode)
6860             (setq buffer-read-only t))
6861           (let ((inhibit-read-only t))
6862             (erase-buffer)
6863             (insert-buffer-substring gnus-article-buffer))
6864           (setq gnus-original-article (cons group article)))
6865
6866         ;; Decode charsets.
6867         (run-hooks 'gnus-article-decode-hook)
6868         ;; Mark article as decoded or not.
6869         (setq gnus-article-decoded-p gnus-article-decode-hook))
6870
6871       ;; Update sparse articles.
6872       (when (and do-update-line
6873                  (or (numberp article)
6874                      (stringp article)))
6875         (let ((buf (current-buffer)))
6876           (set-buffer gnus-summary-buffer)
6877           (gnus-summary-update-article do-update-line sparse-header)
6878           (gnus-summary-goto-subject do-update-line nil t)
6879           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6880                             (point))
6881           (set-buffer buf))))))
6882
6883 (defun gnus-block-private-groups (group)
6884   (if (gnus-news-group-p group)
6885       ;; Block nothing in news groups.
6886       nil
6887     ;; Block everything anywhere else.
6888     "."))
6889
6890 (defun gnus-blocked-images ()
6891   (if (functionp gnus-blocked-images)
6892       (funcall gnus-blocked-images gnus-newsgroup-name)
6893     gnus-blocked-images))
6894
6895 ;;;
6896 ;;; Article editing
6897 ;;;
6898
6899 (defcustom gnus-article-edit-mode-hook nil
6900   "Hook run in article edit mode buffers."
6901   :group 'gnus-article-various
6902   :type 'hook)
6903
6904 (defvar gnus-article-edit-done-function nil)
6905
6906 (defvar gnus-article-edit-mode-map nil)
6907
6908 ;; Should we be using derived.el for this?
6909 (unless gnus-article-edit-mode-map
6910   (setq gnus-article-edit-mode-map (make-keymap))
6911   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6912
6913   (gnus-define-keys gnus-article-edit-mode-map
6914     "\C-c?"    describe-mode
6915     "\C-c\C-c" gnus-article-edit-done
6916     "\C-c\C-k" gnus-article-edit-exit
6917     "\C-c\C-f\C-t" message-goto-to
6918     "\C-c\C-f\C-o" message-goto-from
6919     "\C-c\C-f\C-b" message-goto-bcc
6920     ;;"\C-c\C-f\C-w" message-goto-fcc
6921     "\C-c\C-f\C-c" message-goto-cc
6922     "\C-c\C-f\C-s" message-goto-subject
6923     "\C-c\C-f\C-r" message-goto-reply-to
6924     "\C-c\C-f\C-n" message-goto-newsgroups
6925     "\C-c\C-f\C-d" message-goto-distribution
6926     "\C-c\C-f\C-f" message-goto-followup-to
6927     "\C-c\C-f\C-m" message-goto-mail-followup-to
6928     "\C-c\C-f\C-k" message-goto-keywords
6929     "\C-c\C-f\C-u" message-goto-summary
6930     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6931     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6932     "\C-c\C-b" message-goto-body
6933     "\C-c\C-i" message-goto-signature
6934
6935     "\C-c\C-t" message-insert-to
6936     "\C-c\C-n" message-insert-newsgroups
6937     "\C-c\C-o" message-sort-headers
6938     "\C-c\C-e" message-elide-region
6939     "\C-c\C-v" message-delete-not-region
6940     "\C-c\C-z" message-kill-to-signature
6941     "\M-\r" message-newline-and-reformat
6942     "\C-c\C-a" mml-attach-file
6943     "\C-a" message-beginning-of-line
6944     "\t" message-tab
6945     "\M-;" comment-region)
6946
6947   (gnus-define-keys (gnus-article-edit-wash-map
6948                      "\C-c\C-w" gnus-article-edit-mode-map)
6949     "f" gnus-article-edit-full-stops))
6950
6951 (easy-menu-define
6952   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6953   '("Field"
6954     ["Fetch To" message-insert-to t]
6955     ["Fetch Newsgroups" message-insert-newsgroups t]
6956     "----"
6957     ["To" message-goto-to t]
6958     ["From" message-goto-from t]
6959     ["Subject" message-goto-subject t]
6960     ["Cc" message-goto-cc t]
6961     ["Reply-To" message-goto-reply-to t]
6962     ["Summary" message-goto-summary t]
6963     ["Keywords" message-goto-keywords t]
6964     ["Newsgroups" message-goto-newsgroups t]
6965     ["Followup-To" message-goto-followup-to t]
6966     ["Mail-Followup-To" message-goto-mail-followup-to t]
6967     ["Distribution" message-goto-distribution t]
6968     ["Body" message-goto-body t]
6969     ["Signature" message-goto-signature t]))
6970
6971 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6972   "Major mode for editing articles.
6973 This is an extended text-mode.
6974
6975 \\{gnus-article-edit-mode-map}"
6976   (make-local-variable 'gnus-article-edit-done-function)
6977   (make-local-variable 'gnus-prev-winconf)
6978   (set (make-local-variable 'font-lock-defaults)
6979        '(message-font-lock-keywords t))
6980   (set (make-local-variable 'mail-header-separator) "")
6981   (set (make-local-variable 'gnus-article-edit-mode) t)
6982   (easy-menu-add message-mode-field-menu message-mode-map)
6983   (mml-mode)
6984   (setq buffer-read-only nil)
6985   (buffer-enable-undo)
6986   (widen))
6987
6988 (defun gnus-article-edit (&optional force)
6989   "Edit the current article.
6990 This will have permanent effect only in mail groups.
6991 If FORCE is non-nil, allow editing of articles even in read-only
6992 groups."
6993   (interactive "P")
6994   (when (and (not force)
6995              (gnus-group-read-only-p))
6996     (error "The current newsgroup does not support article editing"))
6997   (gnus-article-date-original)
6998   (gnus-article-edit-article
6999    'ignore
7000    `(lambda (no-highlight)
7001       'ignore
7002       (gnus-summary-edit-article-done
7003        ,(or (mail-header-references gnus-current-headers) "")
7004        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7005
7006 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7007   "Start editing the contents of the current article buffer."
7008   (let ((winconf (current-window-configuration)))
7009     (set-buffer gnus-article-buffer)
7010     (let ((message-auto-save-directory
7011            ;; Don't associate the article buffer with a draft file.
7012            nil))
7013       (gnus-article-edit-mode))
7014     (funcall start-func)
7015     (set-buffer-modified-p nil)
7016     (gnus-configure-windows 'edit-article)
7017     (setq gnus-article-edit-done-function exit-func)
7018     (setq gnus-prev-winconf winconf)
7019     (unless quiet
7020       (gnus-message 6 "C-c C-c to end edits"))))
7021
7022 (defun gnus-article-edit-done (&optional arg)
7023   "Update the article edits and exit."
7024   (interactive "P")
7025   (let ((func gnus-article-edit-done-function)
7026         (buf (current-buffer))
7027         (start (window-start))
7028         (p (point))
7029         (winconf gnus-prev-winconf))
7030     (widen) ;; Widen it in case that users narrowed the buffer.
7031     (funcall func arg)
7032     (set-buffer buf)
7033     ;; The cache and backlog have to be flushed somewhat.
7034     (when gnus-keep-backlog
7035       (gnus-backlog-remove-article
7036        (car gnus-article-current) (cdr gnus-article-current)))
7037     ;; Flush original article as well.
7038     (gnus-flush-original-article-buffer)
7039     (when gnus-use-cache
7040       (gnus-cache-update-article
7041        (car gnus-article-current) (cdr gnus-article-current)))
7042     ;; We remove all text props from the article buffer.
7043     (kill-all-local-variables)
7044     (set-text-properties (point-min) (point-max) nil)
7045     (gnus-article-mode)
7046     (set-window-configuration winconf)
7047     (set-buffer buf)
7048     (set-window-start (get-buffer-window buf) start)
7049     (set-window-point (get-buffer-window buf) (point)))
7050   (gnus-summary-show-article))
7051
7052 (defun gnus-flush-original-article-buffer ()
7053   (when (get-buffer gnus-original-article-buffer)
7054     (with-current-buffer gnus-original-article-buffer
7055       (setq gnus-original-article nil))))
7056
7057 (defun gnus-article-edit-exit ()
7058   "Exit the article editing without updating."
7059   (interactive)
7060   (when (or (not (buffer-modified-p))
7061             (yes-or-no-p "Article modified; kill anyway? "))
7062     (let ((curbuf (current-buffer))
7063           (p (point))
7064           (window-start (window-start)))
7065       (erase-buffer)
7066       (if (gnus-buffer-live-p gnus-original-article-buffer)
7067           (insert-buffer-substring gnus-original-article-buffer))
7068       (let ((winconf gnus-prev-winconf))
7069         (kill-all-local-variables)
7070         (gnus-article-mode)
7071         (set-window-configuration winconf)
7072         ;; Tippy-toe some to make sure that point remains where it was.
7073         (save-current-buffer
7074           (set-buffer curbuf)
7075           (set-window-start (get-buffer-window (current-buffer)) window-start)
7076           (goto-char p))))
7077     (gnus-summary-show-article)))
7078
7079 (defun gnus-article-edit-full-stops ()
7080   "Interactively repair spacing at end of sentences."
7081   (interactive)
7082   (save-excursion
7083     (goto-char (point-min))
7084     (search-forward-regexp "^$" nil t)
7085     (let ((case-fold-search nil))
7086       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7087
7088 ;;;
7089 ;;; Article highlights
7090 ;;;
7091
7092 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7093
7094 ;;; Internal Variables:
7095
7096 (defcustom gnus-button-url-regexp
7097   (concat
7098    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7099    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7100    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7101    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7102        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7103              (punct "!?:;.,"))
7104          (concat
7105           "\\(?:"
7106           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7107           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7108           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7109           "\\|"
7110           "[" chars punct     "]+" "[" chars "]"
7111           "\\)"))
7112      (concat ;; XEmacs 21.4 doesn't support POSIX.
7113       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7114       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7115    "\\)")
7116   "Regular expression that matches URLs."
7117   :group 'gnus-article-buttons
7118   :type 'regexp)
7119
7120 (defcustom gnus-button-valid-fqdn-regexp
7121   message-valid-fqdn-regexp
7122   "Regular expression that matches a valid FQDN."
7123   :version "22.1"
7124   :group 'gnus-article-buttons
7125   :type 'regexp)
7126
7127 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7128 (defcustom gnus-button-valid-localpart-regexp
7129   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7130   "Regular expression that matches a localpart of mail addresses or MIDs."
7131   :version "22.1"
7132   :group 'gnus-article-buttons
7133   :type 'regexp)
7134
7135 (defcustom gnus-button-man-handler 'manual-entry
7136   "Function to use for displaying man pages.
7137 The function must take at least one argument with a string naming the
7138 man page."
7139   :version "22.1"
7140   :type '(choice (function-item :tag "Man" manual-entry)
7141                  (function-item :tag "Woman" woman)
7142                  (function :tag "Other"))
7143   :group 'gnus-article-buttons)
7144
7145 (defcustom gnus-button-mid-or-mail-regexp
7146   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7147           gnus-button-valid-fqdn-regexp
7148           ">?\\)\\b")
7149   "Regular expression that matches a message ID or a mail address."
7150   :version "22.1"
7151   :group 'gnus-article-buttons
7152   :type 'regexp)
7153
7154 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7155   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7156 Strings like this can be either a message ID or a mail address.  If it is one
7157 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7158 message ID or a mail address, respectively.  If this variable is set to the
7159 symbol `ask', always query the user what do do.  If it is a function, this
7160 function will be called with the string as its only argument.  The function
7161 must return `mid', `mail', `invalid' or `ask'."
7162   :version "22.1"
7163   :group 'gnus-article-buttons
7164   :type '(choice (function-item :tag "Heuristic function"
7165                                 gnus-button-mid-or-mail-heuristic)
7166                  (const ask)
7167                  (const mid)
7168                  (const mail)))
7169
7170 (defcustom gnus-button-mid-or-mail-heuristic-alist
7171   '((-10.0 . ".+\\$.+@")
7172     (-10.0 . "#")
7173     (-10.0 . "\\*")
7174     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7175     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7176     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7177     (-1.0  . "^[^a-z]+@")
7178     ;;
7179     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7180     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7181     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7182     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7183     ;;
7184     (-2.0  . "^[0-9]")
7185     (-1.0  . "^[0-9][0-9]")
7186     ;;
7187     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7188     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7189     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7190     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7191     ;;
7192     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7193     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7194     ;;       "[0-9]{8,}.*\@"
7195     (-3.0
7196      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7197     ;; "[0-9]{12,}.*\@"
7198     ;; compensation for TDMA dated mail addresses:
7199     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7200     ;;
7201     (-20.0 . "\\.fsf@") ;; Gnus
7202     (-20.0 . "^slrn")
7203     (-20.0 . "^Pine")
7204     (-20.0 . "^alpine\\.")
7205     (-20.0 . "_-_") ;; Subject change in thread
7206     ;;
7207     (-20.0 . "\\.ln@") ;; leafnode
7208     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7209     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7210     ;;
7211     ;; (5.0 . "") ;; $local_part_len <= 7
7212     (10.0  . "^[^0-9]+@")
7213     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7214     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7215     (3.0   . "\@stud")
7216     ;;
7217     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7218     ;;
7219     (0.5   . "^[A-Z][a-z]")
7220     (0.5   . "^[A-Z][a-z][a-z]")
7221     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7222     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7223   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7224
7225 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7226 address.  The REGEXP is processed with `case-fold-search' set to nil."
7227   :version "22.1"
7228   :group 'gnus-article-buttons
7229   :type '(repeat (cons (number :tag "Rate")
7230                        (regexp :tag "Regexp"))))
7231
7232 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7233   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7234 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7235 address, `ask' if unsure and `invalid' if the string is invalid."
7236   (let ((case-fold-search nil)
7237         (list gnus-button-mid-or-mail-heuristic-alist)
7238         (result 0) rate regexp lpartlen elem)
7239     (setq lpartlen
7240           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7241     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7242     ;; Certain special cases...
7243     (when (string-match
7244            (concat
7245             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7246             "^[0-9]+\\.[0-9]+@compuserve\\|"
7247             "@public\\.gmane\\.org")
7248            mid-or-mail)
7249       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7250       (setq result 'mail))
7251     (when (string-match "@.*@\\| " mid-or-mail)
7252       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7253       (setq result 'invalid))
7254     ;; Nothing more to do, if result is not a number here...
7255     (when (numberp result)
7256       (while list
7257         (setq elem (car list)
7258               rate (car elem)
7259               regexp (cdr elem)
7260               list (cdr list))
7261         (when (string-match regexp mid-or-mail)
7262           (setq result (+ result rate))
7263           (gnus-message
7264            9 "`%s' matched `%s', rate `%s', result `%s'."
7265            mid-or-mail regexp rate result)))
7266       (when (<= lpartlen 7)
7267         (setq result (+ result 5.0))
7268         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7269                       mid-or-mail result))
7270       (when (>= lpartlen 12)
7271         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7272         (cond
7273          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7274           ;; Long local part should contain realname if e-mail address,
7275           ;; too many digits: message-id.
7276           ;; $score -= 5.0 + 0.1 * $local_part_len;
7277           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7278           (setq result (+ result rate))
7279           (gnus-message
7280            9 "Many digits in `%s', rate `%s', result `%s'."
7281            mid-or-mail rate result))
7282          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7283                         mid-or-mail)
7284           ;; Too few vowels [^aeiouy]{4,}.*\@
7285           (setq result (+ result -5.0))
7286           (gnus-message
7287            9 "Few vowels in `%s', rate `%s', result `%s'."
7288            mid-or-mail -5.0 result))
7289          (t
7290           (setq result (+ result 5.0))
7291           (gnus-message
7292            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7293     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7294     ;; Maybe we should make this a customizable alist: (condition . 'result)
7295     (cond
7296      ((symbolp result) result)
7297      ;; Now convert number into proper results:
7298      ((< result -10.0) 'mid)
7299      ((> result  10.0) 'mail)
7300      (t 'ask))))
7301
7302 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7303   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7304          (url-mid (concat "news" ":" mid-or-mail))
7305          (url-mailto (concat "mailto" ":" mid-or-mail)))
7306     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7307     (when (fboundp pref)
7308       (setq guessed
7309             ;; get rid of surrounding angles...
7310             (funcall pref
7311                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7312       (if (or (eq 'mid guessed) (eq 'mail guessed))
7313           (setq pref guessed)
7314         (setq pref 'ask)))
7315     (if (eq pref 'ask)
7316         (save-window-excursion
7317           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7318               (setq pref 'mail)
7319             (setq pref 'mid))))
7320     (cond ((eq pref 'mid)
7321            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7322            (gnus-button-handle-news url-mid))
7323           ((eq pref 'mail)
7324            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7325            (gnus-url-mailto url-mailto))
7326           (t (gnus-message 3 "Invalid string.")))))
7327
7328 (defun gnus-button-handle-custom (fun arg)
7329   "Call function FUN on argument ARG.
7330 Both FUN and ARG are supposed to be strings.  ARG will be passed
7331 as a symbol to FUN."
7332   (funcall (intern fun)
7333            (if (string-match "^customize-apropos" fun)
7334                arg
7335              (intern arg))))
7336
7337 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7338
7339 (defun gnus-button-handle-describe-function (url)
7340   "Call `describe-function' when pushing the corresponding URL button."
7341   (describe-function
7342    (intern
7343     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7344
7345 (defun gnus-button-handle-describe-variable (url)
7346   "Call `describe-variable' when pushing the corresponding URL button."
7347   (describe-variable
7348    (intern
7349     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7350
7351 (defun gnus-button-handle-symbol (url)
7352 "Display help on variable or function.
7353 Calls `describe-variable' or `describe-function'."
7354   (let ((sym (intern url)))
7355     (cond
7356      ((fboundp sym) (describe-function sym))
7357      ((boundp sym) (describe-variable sym))
7358      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7359
7360 (defun gnus-button-handle-describe-key (url)
7361   "Call `describe-key' when pushing the corresponding URL button."
7362   (let* ((key-string
7363           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7364          (keys (ignore-errors (eval `(kbd ,key-string)))))
7365     (if keys
7366         (describe-key keys)
7367       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7368
7369 (defun gnus-button-handle-apropos (url)
7370   "Call `apropos' when pushing the corresponding URL button."
7371   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7372
7373 (defun gnus-button-handle-apropos-command (url)
7374   "Call `apropos' when pushing the corresponding URL button."
7375   (apropos-command
7376    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7377
7378 (defun gnus-button-handle-apropos-variable (url)
7379   "Call `apropos' when pushing the corresponding URL button."
7380   (funcall
7381    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7382    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7383
7384 (defun gnus-button-handle-apropos-documentation (url)
7385   "Call `apropos' when pushing the corresponding URL button."
7386   (funcall
7387    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7388    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7389
7390 (defun gnus-button-handle-library (url)
7391   "Call `locate-library' when pushing the corresponding URL button."
7392   (gnus-message 9 "url=`%s'" url)
7393   (let* ((lib (locate-library url))
7394          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7395     (if (not lib)
7396         (gnus-message 1 "Cannot locale library `%s'." url)
7397       (find-file-read-only file))))
7398
7399 (defcustom gnus-button-man-level 5
7400   "*Integer that says how many man-related buttons Gnus will show.
7401 The higher the number, the more buttons will appear and the more false
7402 positives are possible.  Note that you can set this variable local to
7403 specific groups.  Setting it higher in Unix groups is probably a good idea.
7404 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7405 how to set variables in specific groups."
7406   :version "22.1"
7407   :group 'gnus-article-buttons
7408   :link '(custom-manual "(gnus)Group Parameters")
7409   :type 'integer)
7410
7411 (defcustom gnus-button-emacs-level 5
7412   "*Integer that says how many emacs-related buttons Gnus will show.
7413 The higher the number, the more buttons will appear and the more false
7414 positives are possible.  Note that you can set this variable local to
7415 specific groups.  Setting it higher in Emacs or Gnus related groups is
7416 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7417 `gnus-parameters' on how to set variables in specific groups."
7418   :version "22.1"
7419   :group 'gnus-article-buttons
7420   :link '(custom-manual "(gnus)Group Parameters")
7421   :type 'integer)
7422
7423 (defcustom gnus-button-message-level 5
7424   "*Integer that says how many buttons for news or mail messages will appear.
7425 The higher the number, the more buttons will appear and the more false
7426 positives are possible."
7427   ;; mail addresses, MIDs, URLs for news, ...
7428   :version "22.1"
7429   :group 'gnus-article-buttons
7430   :type 'integer)
7431
7432 (defcustom gnus-button-browse-level 5
7433   "*Integer that says how many buttons for browsing will appear.
7434 The higher the number, the more buttons will appear and the more false
7435 positives are possible."
7436   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7437   :version "22.1"
7438   :group 'gnus-article-buttons
7439   :type 'integer)
7440
7441 (defcustom gnus-button-alist
7442   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7443      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7444     ((concat "\\b\\(nntp\\|news\\):\\("
7445              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7446      0 t gnus-button-handle-news 2)
7447     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7448      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7449     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7450      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7451     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7452     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7453      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7454     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7455      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7456     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7457      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7458     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7459      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7460     ;; RFC 2368 (The mailto URL scheme)
7461     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7462      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7463     ("\\bmailto:\\([^ \n\t]+\\)"
7464      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7465     ;; Info Konqueror style <info:/foo/bar baz>.
7466     ;; Must come before " Gnus home-grown style".
7467     ("\\binfo://?\\([^'\">\n\t]+\\)"
7468      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7469    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7470     ("\\binfo://\\([^'\">\n\t ]+\\)"
7471      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7472     ;; Info GNOME style <info:foo#bar_baz>
7473     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7474      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7475     ;; Info KDE style <info:(foo)bar baz>
7476     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7477      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7478     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7479      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7480     ("\\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,]*\\)\\)?"
7481      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7482      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7483     ;; This is custom
7484     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7485      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7486     ;; Emacs help commands
7487     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7488      ;; regexp doesn't match arguments containing ` '.
7489      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7490     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7491      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7492     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7493      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7494     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7495      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7496     ;; The following entries may lead to many false positives so don't enable
7497     ;; them by default (use a high button level).
7498     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7499      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7500      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7501     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7502      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7503     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7504      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7505     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7506      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7507     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7508      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7509     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7510      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7511     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7512      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7513     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7514      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7515     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7516      ;; Unlike the other regexps we really have to require quoting
7517      ;; here to determine where it ends.
7518      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7519     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7520     ("<URL: *\\([^\n<>]*\\)>"
7521      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7522     ;; RFC 2396 (2.4.3., delims) ...
7523     ("\"URL: *\\([^\n\"]*\\)\""
7524      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7525     ;; Raw URLs.
7526     (gnus-button-url-regexp
7527      0 (>= gnus-button-browse-level 0) browse-url 0)
7528     ;; man pages
7529     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7530      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7531      gnus-button-handle-man 1)
7532     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7533     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7534      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7535      gnus-button-handle-man 1)
7536     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7537     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7538     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7539      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7540     ;; Recognizing patches to .el files.  This is somewhat obscure,
7541     ;; but considering the percentage of Gnus users who hack Emacs
7542     ;; Lisp files...
7543     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7544      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7545     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7546      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7547     ;; MID or mail: To avoid too many false positives we don't try to catch
7548     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7549     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7550     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7551     ;; so that non-ambiguous entries (see above) match first.
7552     (gnus-button-mid-or-mail-regexp
7553      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7554   "*Alist of regexps matching buttons in article bodies.
7555
7556 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7557 REGEXP: is the string (case insensitive) matching text around the button (can
7558 also be Lisp expression evaluating to a string),
7559 BUTTON: is the number of the regexp grouping actually matching the button,
7560 FORM: is a Lisp expression which must eval to true for the button to
7561 be added,
7562 CALLBACK: is the function to call when the user push this button, and each
7563 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7564
7565 CALLBACK can also be a variable, in that case the value of that
7566 variable it the real callback function."
7567   :group 'gnus-article-buttons
7568   :type '(repeat (list (choice regexp variable sexp)
7569                        (integer :tag "Button")
7570                        (sexp :tag "Form")
7571                        (function :tag "Callback")
7572                        (repeat :tag "Par"
7573                                :inline t
7574                                (integer :tag "Regexp group")))))
7575 (put 'gnus-button-alist 'risky-local-variable t)
7576
7577 (defcustom gnus-header-button-alist
7578   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7579      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7580     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7581      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7582     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7583      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7584     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7585      0 (>= gnus-button-browse-level 0) browse-url 0)
7586     ("^Subject:" gnus-button-url-regexp
7587      0 (>= gnus-button-browse-level 0) browse-url 0)
7588     ("^[^:]+:" gnus-button-url-regexp
7589      0 (>= gnus-button-browse-level 0) browse-url 0)
7590     ("^OpenPGP:.*url=" gnus-button-url-regexp
7591      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7592     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7593      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7594     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7595      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7596   "*Alist of headers and regexps to match buttons in article heads.
7597
7598 This alist is very similar to `gnus-button-alist', except that each
7599 alist has an additional HEADER element first in each entry:
7600
7601 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7602
7603 HEADER is a regexp to match a header.  For a fuller explanation, see
7604 `gnus-button-alist'."
7605   :group 'gnus-article-buttons
7606   :group 'gnus-article-headers
7607   :type '(repeat (list (regexp :tag "Header")
7608                        (choice regexp variable)
7609                        (integer :tag "Button")
7610                        (sexp :tag "Form")
7611                        (function :tag "Callback")
7612                        (repeat :tag "Par"
7613                                :inline t
7614                                (integer :tag "Regexp group")))))
7615 (put 'gnus-header-button-alist 'risky-local-variable t)
7616
7617 ;;; Commands:
7618
7619 (defun gnus-article-push-button (event)
7620   "Check text under the mouse pointer for a callback function.
7621 If the text under the mouse pointer has a `gnus-callback' property,
7622 call it with the value of the `gnus-data' text property."
7623   (interactive "e")
7624   (set-buffer (window-buffer (posn-window (event-start event))))
7625   (let* ((pos (posn-point (event-start event)))
7626          (data (get-text-property pos 'gnus-data))
7627          (fun (get-text-property pos 'gnus-callback)))
7628     (goto-char pos)
7629     (when fun
7630       (funcall fun data))))
7631
7632 (defun gnus-article-press-button ()
7633   "Check text at point for a callback function.
7634 If the text at point has a `gnus-callback' property,
7635 call it with the value of the `gnus-data' text property."
7636   (interactive)
7637   (let ((data (get-text-property (point) 'gnus-data))
7638         (fun (get-text-property (point) 'gnus-callback)))
7639     (when fun
7640       (funcall fun data))))
7641
7642 (defun gnus-article-highlight (&optional force)
7643   "Highlight current article.
7644 This function calls `gnus-article-highlight-headers',
7645 `gnus-article-highlight-citation',
7646 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7647 do the highlighting.  See the documentation for those functions."
7648   (interactive (list 'force))
7649   (gnus-article-highlight-headers)
7650   (gnus-article-highlight-citation force)
7651   (gnus-article-highlight-signature)
7652   (gnus-article-add-buttons)
7653   (gnus-article-add-buttons-to-head))
7654
7655 (defun gnus-article-highlight-some (&optional force)
7656   "Highlight current article.
7657 This function calls `gnus-article-highlight-headers',
7658 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7659 do the highlighting.  See the documentation for those functions."
7660   (interactive (list 'force))
7661   (gnus-article-highlight-headers)
7662   (gnus-article-highlight-signature)
7663   (gnus-article-add-buttons))
7664
7665 (defun gnus-article-highlight-headers ()
7666   "Highlight article headers as specified by `gnus-header-face-alist'."
7667   (interactive)
7668   (gnus-with-article-headers
7669     (let (regexp header-face field-face from hpoints fpoints)
7670       (dolist (entry gnus-header-face-alist)
7671         (goto-char (point-min))
7672         (setq regexp (concat "^\\("
7673                              (if (string-equal "" (nth 0 entry))
7674                                  "[^\t ]"
7675                                (nth 0 entry))
7676                              "\\)")
7677               header-face (nth 1 entry)
7678               field-face (nth 2 entry))
7679         (while (and (re-search-forward regexp nil t)
7680                     (not (eobp)))
7681           (beginning-of-line)
7682           (setq from (point))
7683           (unless (search-forward ":" nil t)
7684             (forward-char 1))
7685           (when (and header-face
7686                      (not (memq (point) hpoints)))
7687             (push (point) hpoints)
7688             (gnus-put-text-property from (point) 'face header-face))
7689           (when (and field-face
7690                      (not (memq (setq from (point)) fpoints)))
7691             (push from fpoints)
7692             (if (re-search-forward "^[^ \t]" nil t)
7693                 (forward-char -2)
7694               (goto-char (point-max)))
7695             (gnus-put-text-property from (point) 'face field-face)))))))
7696
7697 (defun gnus-article-highlight-signature ()
7698   "Highlight the signature in an article.
7699 It does this by highlighting everything after
7700 `gnus-signature-separator' using the face `gnus-signature'."
7701   (interactive)
7702   (gnus-with-article-buffer
7703     (let ((inhibit-point-motion-hooks t))
7704       (save-restriction
7705         (when (and gnus-signature-face
7706                    (gnus-article-narrow-to-signature))
7707           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7708                             'face gnus-signature-face)
7709           (widen)
7710           (gnus-article-search-signature)
7711           (let ((start (match-beginning 0))
7712                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7713             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7714                                      end)))))))
7715
7716 (defun gnus-button-in-region-p (b e prop)
7717   "Say whether PROP exists in the region."
7718   (text-property-not-all b e prop nil))
7719
7720 (defun gnus-article-add-buttons ()
7721   "Find external references in the article and make buttons of them.
7722 \"External references\" are things like Message-IDs and URLs, as
7723 specified by `gnus-button-alist'."
7724   (interactive)
7725   (gnus-with-article-buffer
7726     (let ((inhibit-point-motion-hooks t)
7727           (case-fold-search t)
7728           (alist gnus-button-alist)
7729           beg entry regexp)
7730       ;; We skip the headers.
7731       (article-goto-body)
7732       (setq beg (point))
7733       (while (setq entry (pop alist))
7734         (setq regexp (eval (car entry)))
7735         (goto-char beg)
7736         (while (re-search-forward regexp nil t)
7737           (let ((start (match-beginning (nth 1 entry)))
7738                 (end (match-end (nth 1 entry)))
7739                 (from (match-beginning 0)))
7740             (when (and (eval (nth 2 entry))
7741                        (not (gnus-button-in-region-p
7742                              start end 'gnus-callback)))
7743               ;; That optional form returned non-nil, so we add the
7744               ;; button.
7745               (setq from (set-marker (make-marker) from))
7746               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7747                            (gnus-article-extend-url-button from start end))
7748                 (gnus-article-add-button start end
7749                                          'gnus-button-push (list from entry))
7750                 (gnus-put-text-property
7751                  start end
7752                  'gnus-string (buffer-substring-no-properties
7753                                start end))))))))))
7754
7755 (defun gnus-article-extend-url-button (beg start end)
7756   "Extend url button if url is folded into two or more lines.
7757 Return non-nil if button is extended.  BEG is a marker that points to
7758 the beginning position of a text containing url.  START and END are
7759 the endpoints of a url button before it is extended.  The concatenated
7760 url is put as the `gnus-button-url' overlay property on the button."
7761   (let ((opoint (point))
7762         (points (list start end))
7763         url delim regexp)
7764     (prog1
7765         (when (and (progn
7766                      (goto-char end)
7767                      (not (looking-at "[\t ]*[\">]")))
7768                    (progn
7769                      (goto-char start)
7770                      (string-match
7771                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7772                       (buffer-substring (point-at-bol) start)))
7773                    (progn
7774                      (setq url (list (buffer-substring start end))
7775                            delim (if (match-beginning 1) ">" "\""))
7776                      (beginning-of-line)
7777                      (setq regexp (concat
7778                                    (when (and (looking-at
7779                                                message-cite-prefix-regexp)
7780                                               (< (match-end 0) start))
7781                                      (regexp-quote (match-string 0)))
7782                                    "\
7783 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7784                                    delim "\\)"))
7785                      (while (progn
7786                               (forward-line 1)
7787                               (and (looking-at regexp)
7788                                    (prog1
7789                                        (match-beginning 1)
7790                                      (push (or (match-string 2)
7791                                                (match-string 1))
7792                                            url)
7793                                      (push (setq end (or (match-end 2)
7794                                                          (match-end 1)))
7795                                            points)
7796                                      (push (or (match-beginning 2)
7797                                                (match-beginning 1))
7798                                            points)))))
7799                      (match-beginning 2)))
7800           (let (gnus-article-mouse-face widget-mouse-face)
7801             (while points
7802               (gnus-article-add-button (pop points) (pop points)
7803                                        'gnus-button-push beg)))
7804           (let ((overlay (gnus-make-overlay start end)))
7805             (gnus-overlay-put overlay 'evaporate t)
7806             (gnus-overlay-put overlay 'gnus-button-url
7807                               (list (mapconcat 'identity (nreverse url) "")))
7808             (when gnus-article-mouse-face
7809               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7810           t)
7811       (goto-char opoint))))
7812
7813 ;; Add buttons to the head of an article.
7814 (defun gnus-article-add-buttons-to-head ()
7815   "Add buttons to the head of the article."
7816   (interactive)
7817   (gnus-with-article-headers
7818     (let (beg end)
7819       (dolist (entry gnus-header-button-alist)
7820         ;; Each alist entry.
7821         (goto-char (point-min))
7822         (while (re-search-forward (car entry) nil t)
7823           ;; Each header matching the entry.
7824           (setq beg (match-beginning 0))
7825           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7826                              (match-beginning 0))
7827                         (point-max)))
7828           (goto-char beg)
7829           (while (re-search-forward (eval (nth 1 entry)) end t)
7830             ;; Each match within a header.
7831             (let* ((entry (cdr entry))
7832                    (start (match-beginning (nth 1 entry)))
7833                    (end (match-end (nth 1 entry)))
7834                    (form (nth 2 entry)))
7835               (goto-char (match-end 0))
7836               (when (eval form)
7837                 (gnus-article-add-button
7838                  start end (nth 3 entry)
7839                  (buffer-substring (match-beginning (nth 4 entry))
7840                                    (match-end (nth 4 entry)))))))
7841           (goto-char end))))))
7842
7843 ;;; External functions:
7844
7845 (defun gnus-article-add-button (from to fun &optional data text)
7846   "Create a button between FROM and TO with callback FUN and data DATA."
7847   (when gnus-article-button-face
7848     (gnus-overlay-put (gnus-make-overlay from to nil t)
7849                       'face gnus-article-button-face))
7850   (gnus-add-text-properties
7851    from to
7852    (nconc (and gnus-article-mouse-face
7853                (list gnus-mouse-face-prop gnus-article-mouse-face))
7854           (list 'gnus-callback fun)
7855           (and data (list 'gnus-data data))))
7856   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7857                          :help-echo (or text "Follow the link")
7858                          :keymap gnus-url-button-map
7859                          :button-keymap gnus-widget-button-keymap))
7860
7861 (defun gnus-article-copy-string ()
7862   "Copy the string in the button to the kill ring."
7863   (interactive)
7864   (gnus-article-check-buffer)
7865   (let ((data (get-text-property (point) 'gnus-string)))
7866     (when data
7867       (with-temp-buffer
7868         (insert data)
7869         (copy-region-as-kill (point-min) (point-max))
7870         (message "Copied %s" data)))))
7871
7872 ;;; Internal functions:
7873
7874 (defun gnus-article-set-globals ()
7875   (with-current-buffer gnus-summary-buffer
7876     (gnus-set-global-variables)))
7877
7878 (defun gnus-signature-toggle (end)
7879   (gnus-with-article-buffer
7880     (let ((inhibit-point-motion-hooks t))
7881       (if (text-property-any end (point-max) 'article-type 'signature)
7882           (progn
7883             (gnus-delete-wash-type 'signature)
7884             (gnus-remove-text-properties-when
7885              'article-type 'signature end (point-max)
7886              (cons 'article-type (cons 'signature
7887                                        gnus-hidden-properties))))
7888         (gnus-add-wash-type 'signature)
7889         (gnus-add-text-properties-when
7890          'article-type nil end (point-max)
7891          (cons 'article-type (cons 'signature
7892                                    gnus-hidden-properties)))))
7893     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7894       (gnus-set-mode-line 'article))))
7895
7896 (defun gnus-button-push (marker-and-entry)
7897   ;; Push button starting at MARKER.
7898   (save-excursion
7899     (let* ((marker (car marker-and-entry))
7900            (entry (cadr marker-and-entry))
7901            (regexp (car entry))
7902            (inhibit-point-motion-hooks t))
7903       (goto-char marker)
7904       ;; This is obviously true, or something bad is happening :)
7905       ;; But we need it to have the match-data
7906       (when (looking-at (or (if (symbolp regexp)
7907                                 (symbol-value regexp)
7908                               regexp)))
7909         (let ((fun (nth 3 entry))
7910               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7911                              (get-char-property marker 'gnus-button-url))
7912                         (mapcar (lambda (group)
7913                                   (let ((string (match-string group)))
7914                                     (set-text-properties
7915                                      0 (length string) nil string)
7916                                     string))
7917                                 (nthcdr 4 entry)))))
7918
7919           (cond
7920            ((fboundp fun)
7921             (apply fun args))
7922            ((and (boundp fun)
7923                  (fboundp (symbol-value fun)))
7924             (apply (symbol-value fun) args))
7925            (t
7926             (gnus-message 1 "You must define `%S' to use this button"
7927                           (cons fun args)))))))))
7928
7929 (defun gnus-parse-news-url (url)
7930   (let (scheme server port group message-id articles)
7931     (with-temp-buffer
7932       (insert url)
7933       (goto-char (point-min))
7934       (when (looking-at "\\([A-Za-z]+\\):")
7935         (setq scheme (match-string 1))
7936         (goto-char (match-end 0)))
7937       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7938         (setq server (match-string 1))
7939         (setq port (if (stringp (match-string 3))
7940                        (string-to-number (match-string 3))
7941                      (match-string 3)))
7942         (goto-char (match-end 0)))
7943
7944       (cond
7945        ((looking-at "\\(.*@.*\\)")
7946         (setq message-id (match-string 1)))
7947        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7948         (setq group (match-string 1)
7949               articles (split-string (match-string 2) "-")))
7950        ((looking-at "\\([^/]+\\)/?")
7951         (setq group (match-string 1)))
7952        (t
7953         (error "Unknown news URL syntax"))))
7954     (list scheme server port group message-id articles)))
7955
7956 (defun gnus-button-handle-news (url)
7957   "Fetch a news URL."
7958   (destructuring-bind (scheme server port group message-id articles)
7959       (gnus-parse-news-url url)
7960     (cond
7961      (message-id
7962       (with-current-buffer gnus-summary-buffer
7963         (if server
7964             (let ((gnus-refer-article-method
7965                    (nconc (list (list 'nntp server))
7966                           gnus-refer-article-method))
7967                   (nntp-port-number (or port "nntp")))
7968               (gnus-message 7 "Fetching %s with %s"
7969                             message-id gnus-refer-article-method)
7970               (gnus-summary-refer-article message-id))
7971           (gnus-summary-refer-article message-id))))
7972      (group
7973       (gnus-button-fetch-group url)))))
7974
7975 (defun gnus-button-patch (library line)
7976   "Visit an Emacs Lisp library LIBRARY on line LINE."
7977   (interactive)
7978   (let ((file (locate-library (file-name-nondirectory library))))
7979     (unless file
7980       (error "Couldn't find library %s" library))
7981     (find-file file)
7982     (goto-char (point-min))
7983     (forward-line (1- (string-to-number line)))))
7984
7985 (defun gnus-button-handle-man (url)
7986   "Fetch a man page."
7987   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7988   (when (eq gnus-button-man-handler 'woman)
7989     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7990   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7991   (funcall gnus-button-man-handler url))
7992
7993 (defun gnus-button-handle-info-url (url)
7994   "Fetch an info URL."
7995   (setq url (mm-subst-char-in-string ?+ ?\  url))
7996   (cond
7997    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7998     (gnus-info-find-node
7999      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8000                      "Gnus")
8001              ")" (gnus-url-unhex-string (match-string 2 url)))))
8002    ((string-match "([^)\"]+)[^\"]+" url)
8003     (setq url
8004           (gnus-replace-in-string
8005            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8006     (gnus-info-find-node url))
8007    (t (error "Can't parse %s" url))))
8008
8009 (defun gnus-button-handle-info-url-gnome (url)
8010   "Fetch GNOME style info URL."
8011   (setq url (mm-subst-char-in-string ?_ ?\  url))
8012   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8013       (gnus-info-find-node
8014        (concat "("
8015                (gnus-url-unhex-string
8016                  (match-string 1 url))
8017                ")"
8018                (or (gnus-url-unhex-string
8019                     (match-string 2 url))
8020                    "Top")))
8021     (error "Can't parse %s" url)))
8022
8023 (defun gnus-button-handle-info-url-kde (url)
8024   "Fetch KDE style info URL."
8025   (gnus-info-find-node (gnus-url-unhex-string url)))
8026
8027 ;; (info) will autoload info.el
8028 (declare-function Info-menu "info" (menu-item &optional fork))
8029 (declare-function Info-index-next "info" (num))
8030
8031 (defun gnus-button-handle-info-keystrokes (url)
8032   "Call `info' when pushing the corresponding URL button."
8033   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8034   (let (node indx comma)
8035     (if (string-match
8036          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8037                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8038                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8039                  "\\(?:[ \t\n,]*\\)\\)?")
8040          url)
8041         (setq node (match-string 2 url)
8042               indx (match-string 3 url))
8043       (error "Can't parse %s" url))
8044     (info)
8045     (Info-directory)
8046     (Info-menu node)
8047     (when (> (length indx) 0)
8048       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8049                             "\\([ \t\n,]*\\)")
8050                     indx)
8051       (setq comma (match-string 2 indx))
8052       (setq indx  (match-string 1 indx))
8053       (Info-index indx)
8054       (when comma
8055         (dotimes (i (with-temp-buffer
8056                       (insert comma)
8057                       ;; Note: the XEmacs version of `how-many' takes
8058                       ;; no optional argument.
8059                       (goto-char (point-min))
8060                       (how-many ",")))
8061           (Info-index-next 1)))
8062       nil)))
8063
8064 (autoload 'pgg-snarf-keys-region "pgg")
8065 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8066 (declare-function pgg-display-output-buffer "pgg" (start end status))
8067
8068 (defun gnus-button-openpgp (url)
8069   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8070   (with-temp-buffer
8071     (mm-url-insert-file-contents-external url)
8072     (pgg-snarf-keys-region (point-min) (point-max))
8073     (pgg-display-output-buffer nil nil nil)))
8074
8075 (defun gnus-button-message-id (message-id)
8076   "Fetch MESSAGE-ID."
8077   (with-current-buffer gnus-summary-buffer
8078     (gnus-summary-refer-article message-id)))
8079
8080 (defun gnus-button-fetch-group (address &rest ignore)
8081   "Fetch GROUP specified by ADDRESS."
8082   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8083                       address)
8084     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8085     ;; for nntp:// and news://
8086     (setq address (match-string 3 address)))
8087   (if (not (string-match "[:/]" address))
8088       ;; This is just a simple group url.
8089       (gnus-group-read-ephemeral-group address gnus-select-method)
8090     (if (not
8091          (string-match
8092           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8093           address))
8094         (error "Can't parse %s" address)
8095       (gnus-group-read-ephemeral-group
8096        (match-string 4 address)
8097        `(nntp ,(match-string 1 address)
8098               (nntp-address ,(match-string 1 address))
8099               (nntp-port-number ,(if (match-end 3)
8100                                      (match-string 3 address)
8101                                    "nntp")))
8102        nil nil nil
8103        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8104
8105 (defun gnus-url-parse-query-string (query &optional downcase)
8106   (let (retval pairs cur key val)
8107     (setq pairs (split-string query "&"))
8108     (while pairs
8109       (setq cur (car pairs)
8110             pairs (cdr pairs))
8111       (if (not (string-match "=" cur))
8112           nil                           ; Grace
8113         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8114               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8115         (if downcase
8116             (setq key (downcase key)))
8117         (setq cur (assoc key retval))
8118         (if cur
8119             (setcdr cur (cons val (cdr cur)))
8120           (setq retval (cons (list key val) retval)))))
8121     retval))
8122
8123 (defun gnus-url-mailto (url)
8124   ;; Send mail to someone
8125   (setq url (replace-regexp-in-string "\n" " " url))
8126   (when (string-match "mailto:/*\\(.*\\)" url)
8127     (setq url (substring url (match-beginning 1) nil)))
8128   (let (to args subject func)
8129     (setq args (gnus-url-parse-query-string
8130                 (if (string-match "^\\?" url)
8131                     (substring url 1)
8132                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8133                       (concat "to=" (match-string 1 url) "&"
8134                               (match-string 2 url))
8135                     (concat "to=" url))))
8136           subject (cdr-safe (assoc "subject" args)))
8137     (gnus-msg-mail)
8138     (while args
8139       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8140       (if (fboundp func)
8141           (funcall func)
8142         (message-position-on-field (caar args)))
8143       (insert (gnus-replace-in-string
8144                (mapconcat 'identity (reverse (cdar args)) ", ")
8145                "\r\n" "\n" t))
8146       (setq args (cdr args)))
8147     (if subject
8148         (message-goto-body)
8149       (message-goto-subject))))
8150
8151 (defun gnus-button-embedded-url (address)
8152   "Activate ADDRESS with `browse-url'."
8153   (browse-url (gnus-strip-whitespace address)))
8154
8155 ;;; Next/prev buttons in the article buffer.
8156
8157 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8158 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8159
8160 (defvar gnus-prev-page-map
8161   (let ((map (make-sparse-keymap)))
8162     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8163     (define-key map "\r" 'gnus-button-prev-page)
8164     map))
8165
8166 (defvar gnus-next-page-map
8167   (let ((map (make-sparse-keymap)))
8168     (define-key map gnus-mouse-2 'gnus-button-next-page)
8169     (define-key map "\r" 'gnus-button-next-page)
8170     map))
8171
8172 (defun gnus-insert-prev-page-button ()
8173   (let ((b (point)) e
8174         (inhibit-read-only t))
8175     (gnus-eval-format
8176      gnus-prev-page-line-format nil
8177      `(keymap ,gnus-prev-page-map
8178               gnus-prev t
8179               gnus-callback gnus-article-button-prev-page
8180               article-type annotation))
8181     (setq e (if (bolp)
8182                 ;; Exclude a newline.
8183                 (1- (point))
8184               (point)))
8185     (when gnus-article-button-face
8186       (gnus-overlay-put (gnus-make-overlay b e nil t)
8187                         'face gnus-article-button-face))
8188     (widget-convert-button
8189      'link b e
8190      :action 'gnus-button-prev-page
8191      :button-keymap gnus-prev-page-map)))
8192
8193 (defun gnus-button-next-page (&optional args more-args)
8194   "Go to the next page."
8195   (interactive)
8196   (let ((win (selected-window)))
8197     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8198     (gnus-article-next-page)
8199     (select-window win)))
8200
8201 (defun gnus-button-prev-page (&optional args more-args)
8202   "Go to the prev page."
8203   (interactive)
8204   (let ((win (selected-window)))
8205     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8206     (gnus-article-prev-page)
8207     (select-window win)))
8208
8209 (defun gnus-insert-next-page-button ()
8210   (let ((b (point)) e
8211         (inhibit-read-only t))
8212     (gnus-eval-format gnus-next-page-line-format nil
8213                       `(keymap ,gnus-next-page-map
8214                                gnus-next t
8215                                gnus-callback gnus-article-button-next-page
8216                                article-type annotation))
8217     (setq e (if (bolp)
8218                 ;; Exclude a newline.
8219                 (1- (point))
8220               (point)))
8221     (when gnus-article-button-face
8222       (gnus-overlay-put (gnus-make-overlay b e nil t)
8223                         'face gnus-article-button-face))
8224     (widget-convert-button
8225      'link b e
8226      :action 'gnus-button-next-page
8227      :button-keymap gnus-next-page-map)))
8228
8229 (defun gnus-article-button-next-page (arg)
8230   "Go to the next page."
8231   (interactive "P")
8232   (let ((win (selected-window)))
8233     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8234     (gnus-article-next-page)
8235     (select-window win)))
8236
8237 (defun gnus-article-button-prev-page (arg)
8238   "Go to the prev page."
8239   (interactive "P")
8240   (let ((win (selected-window)))
8241     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8242     (gnus-article-prev-page)
8243     (select-window win)))
8244
8245 (defvar gnus-decode-header-methods
8246   '(mail-decode-encoded-word-region)
8247   "List of methods used to decode headers.
8248
8249 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8250 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8251 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8252 whose names match REGEXP.
8253
8254 For example:
8255 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8256  mail-decode-encoded-word-region
8257  (\"chinese\" . rfc1843-decode-region))
8258 ")
8259
8260 (defvar gnus-decode-header-methods-cache nil)
8261
8262 (defun gnus-multi-decode-header (start end)
8263   "Apply the functions from `gnus-encoded-word-methods' that match."
8264   (unless (and gnus-decode-header-methods-cache
8265                (eq gnus-newsgroup-name
8266                    (car gnus-decode-header-methods-cache)))
8267     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8268     (dolist (x gnus-decode-header-methods)
8269       (if (symbolp x)
8270           (nconc gnus-decode-header-methods-cache (list x))
8271         (if (and gnus-newsgroup-name
8272                  (string-match (car x) gnus-newsgroup-name))
8273             (nconc gnus-decode-header-methods-cache
8274                    (list (cdr x)))))))
8275   (let ((xlist gnus-decode-header-methods-cache))
8276     (pop xlist)
8277     (save-restriction
8278       (narrow-to-region start end)
8279       (while xlist
8280         (funcall (pop xlist) (point-min) (point-max))))))
8281
8282 ;;;
8283 ;;; Treatment top-level handling.
8284 ;;;
8285
8286 (defvar gnus-inhibit-article-treatments nil)
8287
8288 (defun gnus-treat-article (gnus-treat-condition
8289                            &optional part-number total-parts gnus-treat-type)
8290   (let ((gnus-treat-length (- (point-max) (point-min)))
8291         (alist gnus-treatment-function-alist)
8292         (article-goto-body-goes-to-point-min-p t)
8293         (treated-type
8294          (or (not gnus-treat-type)
8295              (catch 'found
8296                (let ((list gnus-article-treat-types))
8297                  (while list
8298                    (when (string-match (pop list) gnus-treat-type)
8299                      (throw 'found t)))))))
8300         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8301         val elem)
8302     (gnus-run-hooks 'gnus-part-display-hook)
8303     (dolist (elem alist)
8304       (setq val
8305             (save-excursion
8306               (when (gnus-buffer-live-p gnus-summary-buffer)
8307                 (set-buffer gnus-summary-buffer))
8308               (symbol-value (car elem))))
8309       (when (and (or (consp val)
8310                      treated-type)
8311                  (or (not gnus-inhibit-article-treatments)
8312                      (eq gnus-treat-condition 'head))
8313                  (gnus-treat-predicate val)
8314                  (or (not (get (car elem) 'highlight))
8315                      highlightp))
8316         (save-restriction
8317           (funcall (cadr elem)))))))
8318
8319 ;; Dynamic variables.
8320 (defvar part-number)
8321 (defvar total-parts)
8322 (defvar gnus-treat-type)
8323 (defvar gnus-treat-condition)
8324 (defvar gnus-treat-length)
8325
8326 (defun gnus-treat-predicate (val)
8327   (cond
8328    ((null val)
8329     nil)
8330    (gnus-treat-condition
8331     (eq gnus-treat-condition val))
8332    ((and (listp val)
8333          (stringp (car val)))
8334     (apply 'gnus-or (mapcar `(lambda (s)
8335                                (string-match s ,(or gnus-newsgroup-name "")))
8336                             val)))
8337    ((listp val)
8338     (let ((pred (pop val)))
8339       (cond
8340        ((eq pred 'or)
8341         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8342        ((eq pred 'and)
8343         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8344        ((eq pred 'not)
8345         (not (gnus-treat-predicate (car val))))
8346        ((eq pred 'typep)
8347         (equal (car val) gnus-treat-type))
8348        (t
8349         (error "%S is not a valid predicate" pred)))))
8350    ((eq val t)
8351     t)
8352    ((eq val 'head)
8353     nil)
8354    ((eq val 'first)
8355     (eq part-number 1))
8356    ((eq val 'last)
8357     (eq part-number total-parts))
8358    ((numberp val)
8359     (< gnus-treat-length val))
8360    (t
8361     (error "%S is not a valid value" val))))
8362
8363 (defun gnus-article-encrypt-body (protocol &optional n)
8364   "Encrypt the article body."
8365   (interactive
8366    (list
8367     (or gnus-article-encrypt-protocol
8368         (gnus-completing-read "Encrypt protocol"
8369                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8370                               t))
8371     current-prefix-arg))
8372   ;; User might hit `K E' instead of `K e', so prompt once.
8373   (when (and gnus-article-encrypt-protocol
8374              gnus-novice-user)
8375     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8376       (error "Encrypt aborted")))
8377   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8378     (unless func
8379       (error "Can't find the encrypt protocol %s" protocol))
8380     (if (member gnus-newsgroup-name '("nndraft:delayed"
8381                                       "nndraft:drafts"
8382                                       "nndraft:queue"))
8383         (error "Can't encrypt the article in group %s"
8384                gnus-newsgroup-name))
8385     (gnus-summary-iterate n
8386       (with-current-buffer gnus-summary-buffer
8387         (let ((mail-parse-charset gnus-newsgroup-charset)
8388               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8389               (summary-buffer gnus-summary-buffer)
8390               references point)
8391           (gnus-set-global-variables)
8392           (when (gnus-group-read-only-p)
8393             (error "The current newsgroup does not support article encrypt"))
8394           (gnus-summary-show-article t)
8395           (setq references
8396               (or (mail-header-references gnus-current-headers) ""))
8397           (set-buffer gnus-article-buffer)
8398           (let* ((inhibit-read-only t)
8399                  (headers
8400                   (mapcar (lambda (field)
8401                             (and (save-restriction
8402                                    (message-narrow-to-head)
8403                                    (goto-char (point-min))
8404                                    (search-forward field nil t))
8405                                  (prog2
8406                                      (message-narrow-to-field)
8407                                      (buffer-string)
8408                                    (delete-region (point-min) (point-max))
8409                                    (widen))))
8410                           '("Content-Type:" "Content-Transfer-Encoding:"
8411                             "Content-Disposition:"))))
8412             (message-narrow-to-head)
8413             (message-remove-header "MIME-Version")
8414             (goto-char (point-max))
8415             (setq point (point))
8416             (insert (apply 'concat headers))
8417             (widen)
8418             (narrow-to-region point (point-max))
8419             (let ((message-options message-options))
8420               (message-options-set 'message-sender user-mail-address)
8421               (message-options-set 'message-recipients user-mail-address)
8422               (message-options-set 'message-sign-encrypt 'not)
8423               (funcall func))
8424             (goto-char (point-min))
8425             (insert "MIME-Version: 1.0\n")
8426             (widen)
8427             (gnus-summary-edit-article-done
8428              references nil summary-buffer t))
8429           (when gnus-keep-backlog
8430             (gnus-backlog-remove-article
8431              (car gnus-article-current) (cdr gnus-article-current)))
8432           (gnus-flush-original-article-buffer)
8433           (when gnus-use-cache
8434             (gnus-cache-update-article
8435              (car gnus-article-current) (cdr gnus-article-current))))))))
8436
8437 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8438   "The following specs can be used:
8439 %t  The security MIME type
8440 %i  Additional info
8441 %d  Details
8442 %D  Details if button is pressed")
8443
8444 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8445   "The following specs can be used:
8446 %t  The security MIME type
8447 %i  Additional info
8448 %d  Details
8449 %D  Details if button is pressed")
8450
8451 (defvar gnus-mime-security-button-line-format-alist
8452   '((?t gnus-tmp-type ?s)
8453     (?i gnus-tmp-info ?s)
8454     (?d gnus-tmp-details ?s)
8455     (?D gnus-tmp-pressed-details ?s)))
8456
8457 (defvar gnus-mime-security-button-commands
8458   '((gnus-article-press-button "\r" "Show Detail")
8459     (undefined "v")
8460     (undefined "t")
8461     (undefined "C")
8462     (gnus-mime-security-save-part "o" "Save...")
8463     (undefined "\C-o")
8464     (undefined "r")
8465     (undefined "d")
8466     (undefined "c")
8467     (undefined "i")
8468     (undefined "E")
8469     (undefined "e")
8470     (undefined "p")
8471     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8472     (undefined ".")))
8473
8474 (defvar gnus-mime-security-button-map
8475   (let ((map (make-sparse-keymap)))
8476     (define-key map gnus-mouse-2 'gnus-article-push-button)
8477     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8478     (dolist (c gnus-mime-security-button-commands)
8479       (define-key map (cadr c) (car c)))
8480     map))
8481
8482 (easy-menu-define
8483   gnus-mime-security-button-menu gnus-mime-security-button-map
8484   "Security button menu."
8485   `("Security Part"
8486     ,@(delq nil
8487             (mapcar (lambda (c)
8488                       (unless (eq (car c) 'undefined)
8489                         (vector (caddr c) (car c) :active t)))
8490                     gnus-mime-security-button-commands))))
8491
8492 (defun gnus-mime-security-button-menu (event prefix)
8493   "Construct a context-sensitive menu of security commands."
8494   (interactive "e\nP")
8495   (save-window-excursion
8496     (let ((pos (event-start event)))
8497       (select-window (posn-window pos))
8498       (goto-char (posn-point pos))
8499       (gnus-article-check-buffer)
8500       (popup-menu gnus-mime-security-button-menu nil prefix))))
8501
8502 (defvar gnus-mime-security-details-buffer nil)
8503
8504 (defvar gnus-mime-security-button-pressed nil)
8505
8506 (defvar gnus-mime-security-show-details-inline t
8507   "If non-nil, show details in the article buffer.")
8508
8509 (defun gnus-mime-security-verify-or-decrypt (handle)
8510   (mm-remove-parts (cdr handle))
8511   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8512         point (inhibit-read-only t))
8513     (if region
8514         (goto-char (car region)))
8515     (setq point (point))
8516     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8517       (let* ((mm-verify-option 'known)
8518              (mm-decrypt-option 'known)
8519              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8520         (unless (eq nparts (cdr handle))
8521           (mm-destroy-parts (cdr handle))
8522           (setcdr handle nparts))))
8523     (gnus-mime-display-security handle)
8524     (when region
8525       (delete-region (point) (cdr region))
8526       (set-marker (car region) nil)
8527       (set-marker (cdr region) nil))
8528     (goto-char point)))
8529
8530 (defun gnus-mime-security-show-details (handle)
8531   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8532     (if (not details)
8533         (gnus-message 5 "No details.")
8534       (if gnus-mime-security-show-details-inline
8535           (let ((gnus-mime-security-button-pressed
8536                  (not (get-text-property (point) 'gnus-mime-details)))
8537                 (gnus-mime-security-button-line-format
8538                  (get-text-property (point) 'gnus-line-format))
8539                 (inhibit-read-only t))
8540             (forward-char -1)
8541             (while (eq (get-text-property (point) 'gnus-line-format)
8542                        gnus-mime-security-button-line-format)
8543               (forward-char -1))
8544             (forward-char)
8545             (save-restriction
8546               (narrow-to-region (point) (point))
8547               (gnus-insert-mime-security-button handle))
8548             (delete-region (point)
8549                            (or (text-property-not-all
8550                                 (point) (point-max)
8551                                 'gnus-line-format
8552                                 gnus-mime-security-button-line-format)
8553                                (point-max))))
8554         ;; Not inlined.
8555         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8556             (with-current-buffer gnus-mime-security-details-buffer
8557               (erase-buffer)
8558               t)
8559           (setq gnus-mime-security-details-buffer
8560                 (gnus-get-buffer-create "*MIME Security Details*")))
8561         (with-current-buffer gnus-mime-security-details-buffer
8562           (insert details)
8563           (goto-char (point-min)))
8564         (pop-to-buffer gnus-mime-security-details-buffer)))))
8565
8566 (defun gnus-mime-security-press-button (handle)
8567   (save-excursion
8568     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8569         (gnus-mime-security-show-details handle)
8570       (gnus-mime-security-verify-or-decrypt handle))))
8571
8572 (defun gnus-insert-mime-security-button (handle &optional displayed)
8573   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8574          (gnus-tmp-type
8575           (concat
8576            (or (nth 2 (assoc protocol mm-verify-function-alist))
8577                (nth 2 (assoc protocol mm-decrypt-function-alist))
8578                "Unknown")
8579            (if (equal (car handle) "multipart/signed")
8580                " Signed" " Encrypted")
8581            " Part"))
8582          (gnus-tmp-info
8583           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8584               "Undecided"))
8585          (gnus-tmp-details
8586           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8587          gnus-tmp-pressed-details
8588          b e)
8589     (setq gnus-tmp-details
8590           (if gnus-tmp-details
8591               (concat "\n" gnus-tmp-details)
8592             ""))
8593     (setq gnus-tmp-pressed-details
8594           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8595     (unless (bolp)
8596       (insert "\n"))
8597     (setq b (point))
8598     (gnus-eval-format
8599      gnus-mime-security-button-line-format
8600      gnus-mime-security-button-line-format-alist
8601      `(keymap ,gnus-mime-security-button-map
8602          gnus-callback gnus-mime-security-press-button
8603          gnus-line-format ,gnus-mime-security-button-line-format
8604          gnus-mime-details ,gnus-mime-security-button-pressed
8605          article-type annotation
8606          gnus-data ,handle))
8607     (setq e (if (bolp)
8608                 ;; Exclude a newline.
8609                 (1- (point))
8610               (point)))
8611     (when gnus-article-button-face
8612       (gnus-overlay-put (gnus-make-overlay b e nil t)
8613                         'face gnus-article-button-face))
8614     (widget-convert-button
8615      'link b e
8616      :mime-handle handle
8617      :action 'gnus-widget-press-button
8618      :button-keymap gnus-mime-security-button-map
8619      :help-echo
8620      (lambda (widget)
8621        ;; Needed to properly clear the message due to a bug in
8622        ;; wid-edit (XEmacs only).
8623        (when (boundp 'help-echo-owns-message)
8624          (setq help-echo-owns-message t))
8625        (format
8626         "%S: show detail; %S: more options"
8627         (aref gnus-mouse-2 0)
8628         (aref gnus-down-mouse-3 0))))))
8629
8630 (defun gnus-mime-display-security (handle)
8631   (save-restriction
8632     (narrow-to-region (point) (point))
8633     (unless (gnus-unbuttonized-mime-type-p (car handle))
8634       (gnus-insert-mime-security-button handle))
8635     (gnus-mime-display-part (cadr handle))
8636     (unless (bolp)
8637       (insert "\n"))
8638     (unless (gnus-unbuttonized-mime-type-p (car handle))
8639       (let ((gnus-mime-security-button-line-format
8640              gnus-mime-security-button-end-line-format))
8641         (gnus-insert-mime-security-button handle)))
8642     (mm-set-handle-multipart-parameter
8643      handle 'gnus-region
8644      (cons (set-marker (make-marker) (point-min))
8645            (set-marker (make-marker) (point-max))))
8646     (goto-char (point-max))))
8647
8648 (defun gnus-mime-security-run-function (function)
8649   "Run FUNCTION with the security part under point."
8650   (gnus-article-check-buffer)
8651   (let ((data (get-text-property (point) 'gnus-data))
8652         buffer handle)
8653     (when (and (stringp (car-safe data))
8654                (setq buffer (mm-handle-multipart-original-buffer data))
8655                (setq handle (cadr data)))
8656       (if (bufferp (mm-handle-buffer handle))
8657           (progn
8658             (setq handle (cons buffer (copy-sequence (cdr handle))))
8659             (mm-handle-set-undisplayer handle nil))
8660         (setq handle (mm-make-handle
8661                       buffer
8662                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8663                       nil nil nil nil nil nil)))
8664       (funcall function handle))))
8665
8666 (defun gnus-mime-security-save-part ()
8667   "Save the security part under point."
8668   (interactive)
8669   (gnus-mime-security-run-function 'mm-save-part))
8670
8671 (defun gnus-mime-security-pipe-part ()
8672   "Pipe the security part under point to a process."
8673   (interactive)
8674   (gnus-mime-security-run-function 'mm-pipe-part))
8675
8676 (gnus-ems-redefine)
8677
8678 (provide 'gnus-art)
8679
8680 (run-hooks 'gnus-art-load-hook)
8681
8682 ;;; gnus-art.el ends here