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