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