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