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