Ensure that point stays at the "same place" even if point is on the line being replaced.
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (defvar tool-bar-map)
33 (defvar w3m-minor-mode-map)
34
35 (require 'gnus)
36 (require 'gnus-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'gnus-win)
40 (require 'mm-bodies)
41 (require 'mail-parse)
42 (require 'mm-decode)
43 (require 'mm-view)
44 (require 'wid-edit)
45 (require 'mm-uu)
46 (require 'message)
47
48 (autoload 'gnus-msg-mail "gnus-msg" nil t)
49 (autoload 'gnus-button-mailto "gnus-msg")
50 (autoload 'gnus-button-reply "gnus-msg" nil t)
51 (autoload 'parse-time-string "parse-time" nil nil)
52 (autoload 'ansi-color-apply-on-region "ansi-color")
53 (autoload 'mm-url-insert-file-contents-external "mm-url")
54 (autoload 'mm-extern-cache-contents "mm-extern")
55
56 (defgroup gnus-article nil
57   "Article display."
58   :link '(custom-manual "(gnus)Article Buffer")
59   :group 'gnus)
60
61 (defgroup gnus-article-treat nil
62   "Treating article parts."
63   :link '(custom-manual "(gnus)Article Hiding")
64   :group 'gnus-article)
65
66 (defgroup gnus-article-hiding nil
67   "Hiding article parts."
68   :link '(custom-manual "(gnus)Article Hiding")
69   :group 'gnus-article)
70
71 (defgroup gnus-article-highlight nil
72   "Article highlighting."
73   :link '(custom-manual "(gnus)Article Highlighting")
74   :group 'gnus-article
75   :group 'gnus-visual)
76
77 (defgroup gnus-article-signature nil
78   "Article signatures."
79   :link '(custom-manual "(gnus)Article Signature")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-headers nil
83   "Article headers."
84   :link '(custom-manual "(gnus)Hiding Headers")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-washing nil
88   "Special commands on articles."
89   :link '(custom-manual "(gnus)Article Washing")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-emphasis nil
93   "Fontisizing articles."
94   :link '(custom-manual "(gnus)Article Fontisizing")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-saving nil
98   "Saving articles."
99   :link '(custom-manual "(gnus)Saving Articles")
100   :group 'gnus-article)
101
102 (defgroup gnus-article-mime nil
103   "Worshiping the MIME wonder."
104   :link '(custom-manual "(gnus)Using MIME")
105   :group 'gnus-article)
106
107 (defgroup gnus-article-buttons nil
108   "Pushable buttons in the article buffer."
109   :link '(custom-manual "(gnus)Article Buttons")
110   :group 'gnus-article)
111
112 (defgroup gnus-article-various nil
113   "Other article options."
114   :link '(custom-manual "(gnus)Misc Article")
115   :group 'gnus-article)
116
117 (defcustom gnus-ignored-headers
118   (mapcar
119    (lambda (header)
120      (concat "^" header ":"))
121    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
122      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
123      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
124      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
125      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
126      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
127      "X-Attribution" "X-Originating-IP" "Delivered-To"
128      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
129      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
130      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
131      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
132      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
133      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
134      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
135      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
136      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
137      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
138      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
139      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
140      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
141      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
142      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
143      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
144      "List-[A-Za-z]+" "X-Listprocessor-Version"
145      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
146      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
147      "X-Received" "Content-length" "X-precedence"
148      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
149      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
150      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
151      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
152      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
153      "X-Content-length" "X-Posting-Agent" "Original-Received"
154      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
155      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
156      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
157      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
158      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
159      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
160      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
161      "Envelope-Sender" "Envelope-Recipients"))
162   "*All headers that start with this regexp will be hidden.
163 This variable can also be a list of regexps of headers to be ignored.
164 If `gnus-visible-headers' is non-nil, this variable will be ignored."
165   :type '(choice :custom-show nil
166                  regexp
167                  (repeat regexp))
168   :group 'gnus-article-hiding)
169
170 (defcustom gnus-visible-headers
171   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
172   "*All headers that do not match this regexp will be hidden.
173 This variable can also be a list of regexp of headers to remain visible.
174 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
175   :type '(choice
176           (repeat :value-to-internal (lambda (widget value)
177                                        (custom-split-regexp-maybe value))
178                   :match (lambda (widget value)
179                            (or (stringp value)
180                                (widget-editable-list-match widget value)))
181                   regexp)
182           (const :tag "Use gnus-ignored-headers" nil)
183           regexp)
184   :group 'gnus-article-hiding)
185
186 (defcustom gnus-sorted-header-list
187   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
188     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
189   "*This variable is a list of regular expressions.
190 If it is non-nil, headers that match the regular expressions will
191 be placed first in the article buffer in the sequence specified by
192 this list."
193   :type '(repeat regexp)
194   :group 'gnus-article-hiding)
195
196 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
197   "Headers that are only to be displayed if they have interesting data.
198 Possible values in this list are:
199
200   'empty       Headers with no content.
201   'newsgroups  Newsgroup identical to Gnus group.
202   'to-address  To identical to To-address.
203   'to-list     To identical to To-list.
204   'cc-list     CC identical to To-list.
205   'followup-to Followup-to identical to Newsgroups.
206   'reply-to    Reply-to identical to From.
207   'date        Date less than four days old.
208   'long-to     To and/or Cc longer than 1024 characters.
209   'many-to     Multiple To and/or Cc."
210   :type '(set (const :tag "Headers with no content." empty)
211               (const :tag "Newsgroups identical to Gnus group." newsgroups)
212               (const :tag "To identical to To-address." to-address)
213               (const :tag "To identical to To-list." to-list)
214               (const :tag "CC identical to To-list." cc-list)
215               (const :tag "Followup-to identical to Newsgroups." followup-to)
216               (const :tag "Reply-to identical to From." reply-to)
217               (const :tag "Date less than four days old." date)
218               (const :tag "To and/or Cc longer than 1024 characters." long-to)
219               (const :tag "Multiple To and/or Cc headers." many-to))
220   :group 'gnus-article-hiding)
221
222 (defcustom gnus-article-skip-boring nil
223   "Skip over text that is not worth reading.
224 By default, if you set this t, then Gnus will display citations and
225 signatures, but will never scroll down to show you a page consisting
226 only of boring text.  Boring text is controlled by
227 `gnus-article-boring-faces'."
228   :version "22.1"
229   :type 'boolean
230   :group 'gnus-article-hiding)
231
232 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
233   "Regexp matching signature separator.
234 This can also be a list of regexps.  In that case, it will be checked
235 from head to tail looking for a separator.  Searches will be done from
236 the end of the buffer."
237   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
238                  (regexp)
239                  (repeat :tag "List of regexp" regexp))
240   :group 'gnus-article-signature)
241
242 (defcustom gnus-signature-limit nil
243   "Provide a limit to what is considered a signature.
244 If it is a number, no signature may not be longer (in characters) than
245 that number.  If it is a floating point number, no signature may be
246 longer (in lines) than that number.  If it is a function, the function
247 will be called without any parameters, and if it returns nil, there is
248 no signature in the buffer.  If it is a string, it will be used as a
249 regexp.  If it matches, the text in question is not a signature.
250
251 This can also be a list of the above values."
252   :type '(choice (const nil)
253                  (integer :value 200)
254                  (number :value 4.0)
255                  function
256                  (regexp :value ".*"))
257   :group 'gnus-article-signature)
258
259 (defcustom gnus-hidden-properties '(invisible t intangible t)
260   "Property list to use for hiding text."
261   :type 'sexp
262   :group 'gnus-article-hiding)
263
264 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
265 ;; frames in a session.
266 (defcustom gnus-article-x-face-command
267   (if (featurep 'xemacs)
268       (if (or (gnus-image-type-available-p 'xface)
269               (gnus-image-type-available-p 'pbm))
270           'gnus-display-x-face-in-from
271         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
272     (if (gnus-image-type-available-p 'pbm)
273         'gnus-display-x-face-in-from
274       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
275 display -"))
276   "*String or function to be executed to display an X-Face header.
277 If it is a string, the command will be executed in a sub-shell
278 asynchronously.  The compressed face will be piped to this command."
279   :type `(choice string
280                  (function-item gnus-display-x-face-in-from)
281                  function)
282   :version "21.1"
283   :group 'gnus-picon
284   :group 'gnus-article-washing)
285
286 (defcustom gnus-article-x-face-too-ugly nil
287   "Regexp matching posters whose face shouldn't be shown automatically."
288   :type '(choice regexp (const nil))
289   :group 'gnus-article-washing)
290
291 (defcustom gnus-article-banner-alist nil
292   "Banner alist for stripping.
293 For example,
294      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
295   :version "21.1"
296   :type '(repeat (cons symbol regexp))
297   :group 'gnus-article-washing)
298
299 (gnus-define-group-parameter
300  banner
301  :variable-document
302  "Alist of regexps (to match group names) and banner."
303  :variable-group gnus-article-washing
304  :parameter-type
305  '(choice :tag "Banner"
306           :value nil
307           (const :tag "Remove signature" signature)
308           (symbol :tag "Item in `gnus-article-banner-alist'" none)
309           regexp
310           (const :tag "None" nil))
311  :parameter-document
312  "If non-nil, specify how to remove `banners' from articles.
313
314 Symbol `signature' means to remove signatures delimited by
315 `gnus-signature-separator'.  Any other symbol is used to look up a
316 regular expression to match the banner in `gnus-article-banner-alist'.
317 A string is used as a regular expression to match the banner
318 directly.")
319
320 (defcustom gnus-article-address-banner-alist nil
321   "Alist of mail addresses and banners.
322 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
323 to match a mail address in the From: header, BANNER is one of a symbol
324 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
325 If ADDRESS matches author's mail address, it will remove things like
326 advertisements.  For example:
327
328 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
329 "
330   :type '(repeat
331           (cons
332            (regexp :tag "Address")
333            (choice :tag "Banner" :value nil
334                    (const :tag "Remove signature" signature)
335                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
336                    regexp
337                    (const :tag "None" nil))))
338   :version "22.1"
339   :group 'gnus-article-washing)
340
341 (defmacro gnus-emphasis-custom-with-format (&rest body)
342   `(let ((format "\
343 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
344 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
345      ,@body))
346
347 (defun gnus-emphasis-custom-value-to-external (value)
348   (gnus-emphasis-custom-with-format
349    (if (consp (car value))
350        (list (format format (car (car value)) (cdr (car value)))
351              2
352              (if (nth 1 value) 2 3)
353              (nth 2 value))
354      value)))
355
356 (defun gnus-emphasis-custom-value-to-internal (value)
357   (gnus-emphasis-custom-with-format
358    (let ((regexp (concat "\\`"
359                          (format (regexp-quote format)
360                                  "\\([^()]+\\)" "\\([^()]+\\)")
361                          "\\'"))
362          pattern)
363      (if (string-match regexp (setq pattern (car value)))
364          (list (cons (match-string 1 pattern) (match-string 2 pattern))
365                (= (nth 2 value) 2)
366                (nth 3 value))
367        value))))
368
369 (defcustom gnus-emphasis-alist
370   (let ((types
371          '(("\\*" "\\*" bold nil 2)
372            ("_" "_" underline)
373            ("/" "/" italic)
374            ("_/" "/_" underline-italic)
375            ("_\\*" "\\*_" underline-bold)
376            ("\\*/" "/\\*" bold-italic)
377            ("_\\*/" "/\\*_" underline-bold-italic))))
378     (nconc
379      (gnus-emphasis-custom-with-format
380       (mapcar (lambda (spec)
381                 (list (format format (car spec) (cadr spec))
382                       (or (nth 3 spec) 2)
383                       (or (nth 4 spec) 3)
384                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
385               types))
386      '(;; I've never seen anyone use this strikethru convention whereas I've
387        ;; several times seen it triggered by normal text.  --Stef
388        ;; Miles suggests that this form is sometimes used but for italics,
389        ;; so maybe we should map it to `italic'.
390        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
391        ;; 2 3 gnus-emphasis-strikethru)
392        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
393         2 3 gnus-emphasis-underline))))
394   "*Alist that says how to fontify certain phrases.
395 Each item looks like this:
396
397   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
398
399 The first element is a regular expression to be matched.  The second
400 is a number that says what regular expression grouping used to find
401 the entire emphasized word.  The third is a number that says what
402 regexp grouping should be displayed and highlighted.  The fourth
403 is the face used for highlighting."
404   :type
405   '(repeat
406     (menu-choice
407      :format "%[Customizing Style%]\n%v"
408      :indent 2
409      (group :tag "Default"
410             :value ("" 0 0 default)
411             :value-create
412             (lambda (widget)
413               (let ((value (widget-get
414                             (cadr (widget-get (widget-get widget :parent)
415                                               :args))
416                             :value)))
417                 (if (not (eq (nth 2 value) 'default))
418                     (widget-put
419                      widget
420                      :value
421                      (gnus-emphasis-custom-value-to-external value))))
422               (widget-group-value-create widget))
423             regexp
424             (integer :format "Match group: %v")
425             (integer :format "Emphasize group: %v")
426             face)
427      (group :tag "Simple"
428             :value (("_" . "_") nil default)
429             (cons :format "%v"
430                   (regexp :format "Start regexp: %v")
431                   (regexp :format "End regexp: %v"))
432             (boolean :format "Show start and end patterns: %[%v%]\n"
433                      :on " On " :off " Off ")
434             face)))
435   :get (lambda (symbol)
436          (mapcar 'gnus-emphasis-custom-value-to-internal
437                  (default-value symbol)))
438   :set (lambda (symbol value)
439          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
440                                      value)))
441   :group 'gnus-article-emphasis)
442
443 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
444   "A regexp to describe whitespace which should not be emphasized.
445 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
446 The former avoids underlining of leading and trailing whitespace,
447 and the latter avoids underlining any whitespace at all."
448   :version "21.1"
449   :group 'gnus-article-emphasis
450   :type 'regexp)
451
452 (defface gnus-emphasis-bold '((t (:bold t)))
453   "Face used for displaying strong emphasized text (*word*)."
454   :group 'gnus-article-emphasis)
455
456 (defface gnus-emphasis-italic '((t (:italic t)))
457   "Face used for displaying italic emphasized text (/word/)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-underline '((t (:underline t)))
461   "Face used for displaying underlined emphasized text (_word_)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
465   "Face used for displaying underlined bold emphasized text (_*word*_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
469   "Face used for displaying underlined italic emphasized text (_/word/_)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
473   "Face used for displaying bold italic emphasized text (/*word*/)."
474   :group 'gnus-article-emphasis)
475
476 (defface gnus-emphasis-underline-bold-italic
477   '((t (:bold t :italic t :underline t)))
478   "Face used for displaying underlined bold italic emphasized text.
479 Example: (_/*word*/_)."
480   :group 'gnus-article-emphasis)
481
482 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
483                                       '((t (:strikethru t)))
484                                     '((t (:strike-through t))))
485   "Face used for displaying strike-through text (-word-)."
486   :group 'gnus-article-emphasis)
487
488 (defface gnus-emphasis-highlight-words
489   '((t (:background "black" :foreground "yellow")))
490   "Face used for displaying highlighted words."
491   :group 'gnus-article-emphasis)
492
493 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
494   "Format for display of Date headers in article bodies.
495 See `format-time-string' for the possible values.
496
497 The variable can also be function, which should return a complete Date
498 header.  The function is called with one argument, the time, which can
499 be fed to `format-time-string'."
500   :type '(choice string function)
501   :link '(custom-manual "(gnus)Article Date")
502   :group 'gnus-article-washing)
503
504 (defcustom gnus-save-all-headers t
505   "*If non-nil, don't remove any headers before saving.
506 This will be overridden by the `:headers' property that the symbol of
507 the saver function, which is specified by `gnus-default-article-saver',
508 might have."
509   :group 'gnus-article-saving
510   :type 'boolean)
511
512 (defcustom gnus-prompt-before-saving 'always
513   "*This variable says how much prompting is to be done when saving articles.
514 If it is nil, no prompting will be done, and the articles will be
515 saved to the default files.  If this variable is `always', each and
516 every article that is saved will be preceded by a prompt, even when
517 saving large batches of articles.  If this variable is neither nil not
518 `always', there the user will be prompted once for a file name for
519 each invocation of the saving commands."
520   :group 'gnus-article-saving
521   :type '(choice (item always)
522                  (item :tag "never" nil)
523                  (sexp :tag "once" :format "%t\n" :value t)))
524
525 (defcustom gnus-saved-headers gnus-visible-headers
526   "Headers to keep if `gnus-save-all-headers' is nil.
527 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
528 If that variable is nil, however, all headers that match this regexp
529 will be kept while the rest will be deleted before saving.  This and
530 `gnus-save-all-headers' will be overridden by the `:headers' property
531 that the symbol of the saver function, which is specified by
532 `gnus-default-article-saver', might have."
533   :group 'gnus-article-saving
534   :type 'regexp)
535
536 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
537 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
538   "A function to save articles in your favourite format.
539 The function will be called by way of the `gnus-summary-save-article'
540 command, and friends such as `gnus-summary-save-article-rmail'.
541
542 Gnus provides the following functions:
543
544 * gnus-summary-save-in-rmail (Rmail format)
545 * gnus-summary-save-in-mail (Unix mail format)
546 * gnus-summary-save-in-folder (MH folder)
547 * gnus-summary-save-in-file (article format)
548 * gnus-summary-save-body-in-file (article body)
549 * gnus-summary-save-in-vm (use VM's folder format)
550 * gnus-summary-write-to-file (article format -- overwrite)
551 * gnus-summary-write-body-to-file (article body -- overwrite)
552 * gnus-summary-save-in-pipe (article format)
553
554 The symbol of each function may have the following properties:
555
556 * :decode
557 The value non-nil means save decoded articles.  This is meaningful
558 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
559 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
560 `gnus-summary-save-in-pipe'.
561
562 * :function
563 The value specifies an alternative function which appends, not
564 overwrites, articles to a file.  This implies that when saving many
565 articles at a time, `gnus-prompt-before-saving' is bound to t and all
566 articles are saved in a single file.  This is meaningful only with
567 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
568
569 * :headers
570 The value specifies the symbol of a variable of which the value
571 specifies headers to be saved.  If it is omitted,
572 `gnus-save-all-headers' and `gnus-saved-headers' control what
573 headers should be saved."
574   :group 'gnus-article-saving
575   :type '(radio (function-item gnus-summary-save-in-rmail)
576                 (function-item gnus-summary-save-in-mail)
577                 (function-item gnus-summary-save-in-folder)
578                 (function-item gnus-summary-save-in-file)
579                 (function-item gnus-summary-save-body-in-file)
580                 (function-item gnus-summary-save-in-vm)
581                 (function-item gnus-summary-write-to-file)
582                 (function-item gnus-summary-write-body-to-file)
583                 (function-item gnus-summary-save-in-pipe)
584                 (function)))
585
586 (defcustom gnus-article-save-coding-system
587   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
588       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
589       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
590       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
591   "Coding system used to save decoded articles to a file.
592
593 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
594 which can safely encode any characters in text.  This is used by the
595 commands including:
596
597 * gnus-summary-save-article-file
598 * gnus-summary-save-article-body-file
599 * gnus-summary-write-article-file
600 * gnus-summary-write-article-body-file
601
602 and the functions to which you may set `gnus-default-article-saver':
603
604 * gnus-summary-save-in-file
605 * gnus-summary-save-body-in-file
606 * gnus-summary-write-to-file
607 * gnus-summary-write-body-to-file
608
609 Those commands and functions save just text displayed in the article
610 buffer to a file if the value of this variable is non-nil.  Note that
611 buttonized MIME parts will be lost in a saved file in that case.
612 Otherwise, raw articles will be saved."
613   :group 'gnus-article-saving
614   :type `(choice
615           :format "%{%t%}:\n %[Value Menu%] %v"
616           (const :tag "Save raw articles" nil)
617           ,@(delq nil
618                   (mapcar
619                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
620                    '((const :tag "UTF-8" utf-8)
621                      (const :tag "iso-2022-7bit" iso-2022-7bit)
622                      (const :tag "Emacs internal" emacs-mule)
623                      (const :tag "escape-quoted" escape-quoted))))
624           (symbol :tag "Coding system")))
625
626 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
627   "A function generating a file name to save articles in Rmail format.
628 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
629   :group 'gnus-article-saving
630   :type 'function)
631
632 (defcustom gnus-mail-save-name 'gnus-plain-save-name
633   "A function generating a file name to save articles in Unix mail format.
634 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
635   :group 'gnus-article-saving
636   :type 'function)
637
638 (defcustom gnus-folder-save-name 'gnus-folder-save-name
639   "A function generating a file name to save articles in MH folder.
640 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
641   :group 'gnus-article-saving
642   :type 'function)
643
644 (defcustom gnus-file-save-name 'gnus-numeric-save-name
645   "A function generating a file name to save articles in article format.
646 The function is called with NEWSGROUP, HEADERS, and optional
647 LAST-FILE."
648   :group 'gnus-article-saving
649   :type 'function)
650
651 (defcustom gnus-split-methods
652   '((gnus-article-archive-name)
653     (gnus-article-nndoc-name))
654   "*Variable used to suggest where articles are to be saved.
655 For instance, if you would like to save articles related to Gnus in
656 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
657 you could set this variable to something like:
658
659  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
660    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
661
662 This variable is an alist where the key is the match and the
663 value is a list of possible files to save in if the match is
664 non-nil.
665
666 If the match is a string, it is used as a regexp match on the
667 article.  If the match is a symbol, that symbol will be funcalled
668 from the buffer of the article to be saved with the newsgroup as the
669 parameter.  If it is a list, it will be evaled in the same buffer.
670
671 If this form or function returns a string, this string will be used as a
672 possible file name; and if it returns a non-nil list, that list will be
673 used as possible file names."
674   :group 'gnus-article-saving
675   :type '(repeat (choice (list :value (fun) function)
676                          (cons :value ("" "") regexp (repeat string))
677                          (sexp :value nil))))
678
679 (defcustom gnus-page-delimiter "^\^L"
680   "*Regexp describing what to use as article page delimiters.
681 The default value is \"^\^L\", which is a form linefeed at the
682 beginning of a line."
683   :type 'regexp
684   :group 'gnus-article-various)
685
686 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
687   "*The format specification for the article mode line.
688 See `gnus-summary-mode-line-format' for a closer description.
689
690 The following additional specs are available:
691
692 %w  The article washing status.
693 %m  The number of MIME parts in the article."
694   :type 'string
695   :group 'gnus-article-various)
696
697 (defcustom gnus-article-mode-hook nil
698   "*A hook for Gnus article mode."
699   :type 'hook
700   :group 'gnus-article-various)
701
702 (when (featurep 'xemacs)
703   ;; Extracted from gnus-xmas-define in order to preserve user settings
704   (when (fboundp 'turn-off-scroll-in-place)
705     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
706   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
707   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
708
709 (defcustom gnus-article-menu-hook nil
710   "*Hook run after the creation of the article mode menu."
711   :type 'hook
712   :group 'gnus-article-various)
713
714 (defcustom gnus-article-prepare-hook nil
715   "*A hook called after an article has been prepared in the article buffer."
716   :type 'hook
717   :group 'gnus-article-various)
718
719 (defcustom gnus-copy-article-ignored-headers nil
720   "List of headers to be removed when copying an article.
721 Each element is a regular expression."
722   :version "23.1" ;; No Gnus
723   :type '(repeat regexp)
724   :group 'gnus-article-various)
725
726 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
727                         "Gnus 5.10 (Emacs 22.1)")
728
729 (defface gnus-button
730   '((t (:weight bold)))
731   "Face used for highlighting a button in the article buffer."
732   :group 'gnus-article-buttons)
733
734 (defcustom gnus-article-button-face 'gnus-button
735   "Face used for highlighting buttons in the article buffer.
736
737 An article button is a piece of text that you can activate by pressing
738 `RET' or `mouse-2' above it."
739   :type 'face
740   :group 'gnus-article-buttons)
741
742 (defcustom gnus-article-mouse-face 'highlight
743   "Face used for mouse highlighting in the article buffer.
744
745 Article buttons will be displayed in this face when the cursor is
746 above them."
747   :type 'face
748   :group 'gnus-article-buttons)
749
750 (defcustom gnus-signature-face 'gnus-signature
751   "Face used for highlighting a signature in the article buffer.
752 Obsolete; use the face `gnus-signature' for customizations instead."
753   :type 'face
754   :group 'gnus-article-highlight
755   :group 'gnus-article-signature)
756
757 (defface gnus-signature
758   '((t
759      (:italic t)))
760   "Face used for highlighting a signature in the article buffer."
761   :group 'gnus-article-highlight
762   :group 'gnus-article-signature)
763 ;; backward-compatibility alias
764 (put 'gnus-signature-face 'face-alias 'gnus-signature)
765 (put 'gnus-signature-face 'obsolete-face "22.1")
766
767 (defface gnus-header-from
768   '((((class color)
769       (background dark))
770      (:foreground "PaleGreen1"))
771     (((class color)
772       (background light))
773      (:foreground "red3"))
774     (t
775      (:italic t)))
776   "Face used for displaying from headers."
777   :group 'gnus-article-headers
778   :group 'gnus-article-highlight)
779 ;; backward-compatibility alias
780 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
781 (put 'gnus-header-from-face 'obsolete-face "22.1")
782
783 (defface gnus-header-subject
784   '((((class color)
785       (background dark))
786      (:foreground "SeaGreen1"))
787     (((class color)
788       (background light))
789      (:foreground "red4"))
790     (t
791      (:bold t :italic t)))
792   "Face used for displaying subject headers."
793   :group 'gnus-article-headers
794   :group 'gnus-article-highlight)
795 ;; backward-compatibility alias
796 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
797 (put 'gnus-header-subject-face 'obsolete-face "22.1")
798
799 (defface gnus-header-newsgroups
800   '((((class color)
801       (background dark))
802      (:foreground "yellow" :italic t))
803     (((class color)
804       (background light))
805      (:foreground "MidnightBlue" :italic t))
806     (t
807      (:italic t)))
808   "Face used for displaying newsgroups headers.
809 In the default setup this face is only used for crossposted
810 articles."
811   :group 'gnus-article-headers
812   :group 'gnus-article-highlight)
813 ;; backward-compatibility alias
814 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
815 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
816
817 (defface gnus-header-name
818   '((((class color)
819       (background dark))
820      (:foreground "SpringGreen2"))
821     (((class color)
822       (background light))
823      (:foreground "maroon"))
824     (t
825      (:bold t)))
826   "Face used for displaying header names."
827   :group 'gnus-article-headers
828   :group 'gnus-article-highlight)
829 ;; backward-compatibility alias
830 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
831 (put 'gnus-header-name-face 'obsolete-face "22.1")
832
833 (defface gnus-header-content
834   '((((class color)
835       (background dark))
836      (:foreground "SpringGreen1" :italic t))
837     (((class color)
838       (background light))
839      (:foreground "indianred4" :italic t))
840     (t
841      (:italic t)))  "Face used for displaying header content."
842   :group 'gnus-article-headers
843   :group 'gnus-article-highlight)
844 ;; backward-compatibility alias
845 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
846 (put 'gnus-header-content-face 'obsolete-face "22.1")
847
848 (defcustom gnus-header-face-alist
849   '(("From" nil gnus-header-from)
850     ("Subject" nil gnus-header-subject)
851     ("Newsgroups:.*," nil gnus-header-newsgroups)
852     ("" gnus-header-name gnus-header-content))
853   "*Controls highlighting of article headers.
854
855 An alist of the form (HEADER NAME CONTENT).
856
857 HEADER is a regular expression which should match the name of a
858 header and NAME and CONTENT are either face names or nil.
859
860 The name of each header field will be displayed using the face
861 specified by the first element in the list where HEADER matches
862 the header name and NAME is non-nil.  Similarly, the content will
863 be displayed by the first non-nil matching CONTENT face."
864   :group 'gnus-article-headers
865   :group 'gnus-article-highlight
866   :type '(repeat (list (regexp :tag "Header")
867                        (choice :tag "Name"
868                                (item :tag "skip" nil)
869                                (face :value default))
870                        (choice :tag "Content"
871                                (item :tag "skip" nil)
872                                (face :value default)))))
873
874 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
875                                           '((xface . (:face gnus-x-face)))
876                                         '((pbm . (:face gnus-x-face))
877                                           (png . nil)))
878   "Alist of image types and properties applied to Face and X-Face images.
879 Here are examples:
880
881 ;; Specify the altitude of Face images in the From header.
882 \(setq gnus-face-properties-alist
883       '((pbm . (:face gnus-x-face :ascent 80))
884         (png . (:ascent 80))))
885
886 ;; Show Face images as pressed buttons.
887 \(setq gnus-face-properties-alist
888       '((pbm . (:face gnus-x-face :relief -2))
889         (png . (:relief -2))))
890
891 See the manual for the valid properties for various image types.
892 Currently, `pbm' is used for X-Face images and `png' is used for Face
893 images in Emacs.  Only the `:face' property is effective on the `xface'
894 image type in XEmacs if it is built with the libcompface library."
895   :version "23.1" ;; No Gnus
896   :group 'gnus-article-headers
897   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
898
899 (defcustom gnus-article-decode-hook
900   '(article-decode-charset article-decode-encoded-words
901                            article-decode-group-name article-decode-idna-rhs)
902   "*Hook run to decode charsets in articles."
903   :group 'gnus-article-headers
904   :type 'hook)
905
906 (defcustom gnus-display-mime-function 'gnus-display-mime
907   "Function to display MIME articles."
908   :group 'gnus-article-mime
909   :type 'function)
910
911 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
912   "Function used to decode headers.")
913
914 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
915   "Function used to decode addresses.")
916
917 (defvar gnus-article-dumbquotes-map
918   '((?\200 "EUR")
919     (?\202 ",")
920     (?\203 "f")
921     (?\204 ",,")
922     (?\205 "...")
923     (?\213 "<")
924     (?\214 "OE")
925     (?\221 "`")
926     (?\222 "'")
927     (?\223 "``")
928     (?\224 "\"")
929     (?\225 "*")
930     (?\226 "-")
931     (?\227 "--")
932     (?\230 "~")
933     (?\231 "(TM)")
934     (?\233 ">")
935     (?\234 "oe")
936     (?\264 "'"))
937   "Table for MS-to-Latin1 translation.")
938
939 (defcustom gnus-ignored-mime-types nil
940   "List of MIME types that should be ignored by Gnus."
941   :version "21.1"
942   :group 'gnus-article-mime
943   :type '(repeat regexp))
944
945 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
946   "List of MIME types that should not be given buttons when rendered inline.
947 See also `gnus-buttonized-mime-types' which may override this variable.
948 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
949   :version "21.1"
950   :group 'gnus-article-mime
951   :type '(repeat regexp))
952
953 (defcustom gnus-buttonized-mime-types nil
954   "List of MIME types that should be given buttons when rendered inline.
955 If set, this variable overrides `gnus-unbuttonized-mime-types'.
956 To see e.g. security buttons you could set this to
957 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
958 this list to display radio buttons that allow you to choose one of two
959 media types those mails include.  See also `mm-discouraged-alternatives'.
960 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
961   :version "22.1"
962   :group 'gnus-article-mime
963   :type '(repeat regexp))
964
965 (defcustom gnus-inhibit-mime-unbuttonizing nil
966   "If non-nil, all MIME parts get buttons.
967 When nil (the default value), then some MIME parts do not get buttons,
968 as described by the variables `gnus-buttonized-mime-types' and
969 `gnus-unbuttonized-mime-types'."
970   :version "22.1"
971   :group 'gnus-article-mime
972   :type 'boolean)
973
974 (defcustom gnus-body-boundary-delimiter "_"
975   "String used to delimit header and body.
976 This variable is used by `gnus-article-treat-body-boundary' which can
977 be controlled by `gnus-treat-body-boundary'."
978   :version "22.1"
979   :group 'gnus-article-various
980   :type '(choice (item :tag "None" :value nil)
981                  string))
982
983 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
984                                   "/usr/share/picons")
985   "Defines the location of the faces database.
986 For information on obtaining this database of pretty pictures, please
987 see http://www.cs.indiana.edu/picons/ftp/index.html"
988   :version "22.1"
989   :type '(repeat directory)
990   :link '(url-link :tag "download"
991                    "http://www.cs.indiana.edu/picons/ftp/index.html")
992   :link '(custom-manual "(gnus)Picons")
993   :group 'gnus-picon)
994
995 (defun gnus-picons-installed-p ()
996   "Say whether picons are installed on your machine."
997   (let ((installed nil))
998     (dolist (database gnus-picon-databases)
999       (when (file-exists-p database)
1000         (setq installed t)))
1001     installed))
1002
1003 (defcustom gnus-article-mime-part-function nil
1004   "Function called with a MIME handle as the argument.
1005 This is meant for people who want to do something automatic based
1006 on parts -- for instance, adding Vcard info to a database."
1007   :group 'gnus-article-mime
1008   :type '(choice (const nil)
1009                  function))
1010
1011 (defcustom gnus-mime-multipart-functions nil
1012   "An alist of MIME types to functions to display them."
1013   :version "21.1"
1014   :group 'gnus-article-mime
1015   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1016
1017 (defcustom gnus-article-date-lapsed-new-header nil
1018   "Whether the X-Sent and Date headers can coexist.
1019 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1020 either replace the old \"Date:\" header (if this variable is nil), or
1021 be added below it (otherwise)."
1022   :version "21.1"
1023   :group 'gnus-article-headers
1024   :type 'boolean)
1025
1026 (defcustom gnus-article-update-lapsed-header 1
1027   "How often to update the lapsed date header.
1028 If nil, don't update it at all."
1029   :version "24.1"
1030   :group 'gnus-article-headers
1031   :type '(choice
1032           (item :tag "Don't update" :value nil)
1033           integer))
1034
1035 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1036   "Function called with a MIME handle as the argument.
1037 This is meant for people who want to view first matched part.
1038 For `undisplayed-alternative' (default), the first undisplayed
1039 part or alternative part is used.  For `undisplayed', the first
1040 undisplayed part is used.  For a function, the first part which
1041 the function return t is used.  For nil, the first part is
1042 used."
1043   :version "21.1"
1044   :group 'gnus-article-mime
1045   :type '(choice
1046           (item :tag "first" :value nil)
1047           (item :tag "undisplayed" :value undisplayed)
1048           (item :tag "undisplayed or alternative"
1049                 :value undisplayed-alternative)
1050           (function)))
1051
1052 (defcustom gnus-mime-action-alist
1053   '(("save to file" . gnus-mime-save-part)
1054     ("save and strip" . gnus-mime-save-part-and-strip)
1055     ("replace with file" . gnus-mime-replace-part)
1056     ("delete part" . gnus-mime-delete-part)
1057     ("display as text" . gnus-mime-inline-part)
1058     ("view the part" . gnus-mime-view-part)
1059     ("pipe to command" . gnus-mime-pipe-part)
1060     ("toggle display" . gnus-article-press-button)
1061     ("toggle display" . gnus-article-view-part-as-charset)
1062     ("view as type" . gnus-mime-view-part-as-type)
1063     ("view internally" . gnus-mime-view-part-internally)
1064     ("view externally" . gnus-mime-view-part-externally))
1065   "An alist of actions that run on the MIME attachment."
1066   :group 'gnus-article-mime
1067   :type '(repeat (cons (string :tag "name")
1068                        (function))))
1069
1070 (defcustom gnus-auto-select-part 1
1071   "Advance to next MIME part when deleting or stripping parts.
1072
1073 When 0, point will be placed on the same part as before.  When
1074 positive (negative), move point forward (backwards) this many
1075 parts.  When nil, redisplay article."
1076   :version "23.1" ;; No Gnus
1077   :group 'gnus-article-mime
1078   :type '(choice (const nil :tag "Redisplay article.")
1079                  (const 1 :tag "Next part.")
1080                  (const 0 :tag "Current part.")
1081                  integer))
1082
1083 ;;;
1084 ;;; The treatment variables
1085 ;;;
1086
1087 (defvar gnus-part-display-hook nil
1088   "Hook called on parts that are to receive treatment.")
1089
1090 (defvar gnus-article-treat-custom
1091   '(choice (const :tag "Off" nil)
1092            (const :tag "On" t)
1093            (const :tag "Header" head)
1094            (const :tag "First" first)
1095            (const :tag "Last" last)
1096            (integer :tag "Less")
1097            (repeat :tag "Groups" regexp)
1098            (sexp :tag "Predicate")))
1099
1100 (defvar gnus-article-treat-head-custom
1101   '(choice (const :tag "Off" nil)
1102            (const :tag "Header" head)))
1103
1104 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1105                                    "text/x-patch")
1106   "Parts to treat.")
1107
1108 (defvar gnus-inhibit-treatment nil
1109   "Whether to inhibit treatment.")
1110
1111 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1112   "Highlight the signature.
1113 Valid values are nil, t, `head', `first', `last', an integer or a
1114 predicate.  See Info node `(gnus)Customizing Articles'."
1115   :group 'gnus-article-treat
1116   :link '(custom-manual "(gnus)Customizing Articles")
1117   :type gnus-article-treat-custom)
1118 (put 'gnus-treat-highlight-signature 'highlight t)
1119
1120 (defcustom gnus-treat-buttonize 100000
1121   "Add buttons.
1122 Valid values are nil, t, `head', `first', `last', an integer or a
1123 predicate.  See Info node `(gnus)Customizing Articles'."
1124   :group 'gnus-article-treat
1125   :link '(custom-manual "(gnus)Customizing Articles")
1126   :type gnus-article-treat-custom)
1127 (put 'gnus-treat-buttonize 'highlight t)
1128
1129 (defcustom gnus-treat-buttonize-head 'head
1130   "Add buttons to the head.
1131 Valid values are nil, t, `head', `first', `last', an integer or a
1132 predicate.  See Info node `(gnus)Customizing Articles'."
1133   :group 'gnus-article-treat
1134   :link '(custom-manual "(gnus)Customizing Articles")
1135   :type gnus-article-treat-head-custom)
1136 (put 'gnus-treat-buttonize-head 'highlight t)
1137
1138 (defcustom gnus-treat-emphasize 50000
1139   "Emphasize text.
1140 Valid values are nil, t, `head', `first', `last', an integer or a
1141 predicate.  See Info node `(gnus)Customizing Articles'."
1142   :group 'gnus-article-treat
1143   :link '(custom-manual "(gnus)Customizing Articles")
1144   :type gnus-article-treat-custom)
1145 (put 'gnus-treat-emphasize 'highlight t)
1146
1147 (defcustom gnus-treat-strip-cr nil
1148   "Remove carriage returns.
1149 Valid values are nil, t, `head', `first', `last', an integer or a
1150 predicate.  See Info node `(gnus)Customizing Articles'."
1151   :version "22.1"
1152   :group 'gnus-article-treat
1153   :link '(custom-manual "(gnus)Customizing Articles")
1154   :type gnus-article-treat-custom)
1155
1156 (defcustom gnus-treat-unsplit-urls nil
1157   "Remove newlines from within URLs.
1158 Valid values are nil, t, `head', `first', `last', an integer or a
1159 predicate.  See Info node `(gnus)Customizing Articles'."
1160   :version "22.1"
1161   :group 'gnus-article-treat
1162   :link '(custom-manual "(gnus)Customizing Articles")
1163   :type gnus-article-treat-custom)
1164
1165 (defcustom gnus-treat-leading-whitespace nil
1166   "Remove leading whitespace in headers.
1167 Valid values are nil, t, `head', `first', `last', an integer or a
1168 predicate.  See Info node `(gnus)Customizing Articles'."
1169   :version "22.1"
1170   :group 'gnus-article-treat
1171   :link '(custom-manual "(gnus)Customizing Articles")
1172   :type gnus-article-treat-custom)
1173
1174 (defcustom gnus-treat-hide-headers 'head
1175   "Hide headers.
1176 Valid values are nil, t, `head', `first', `last', an integer or a
1177 predicate.  See Info node `(gnus)Customizing Articles'."
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-head-custom)
1181
1182 (defcustom gnus-treat-hide-boring-headers nil
1183   "Hide boring headers.
1184 Valid values are nil, t, `head', `first', `last', an integer or a
1185 predicate.  See Info node `(gnus)Customizing Articles'."
1186   :group 'gnus-article-treat
1187   :link '(custom-manual "(gnus)Customizing Articles")
1188   :type gnus-article-treat-head-custom)
1189
1190 (defcustom gnus-treat-hide-signature nil
1191   "Hide the signature.
1192 Valid values are nil, t, `head', `first', `last', an integer or a
1193 predicate.  See Info node `(gnus)Customizing Articles'."
1194   :group 'gnus-article-treat
1195   :link '(custom-manual "(gnus)Customizing Articles")
1196   :type gnus-article-treat-custom)
1197
1198 (defcustom gnus-treat-fill-article nil
1199   "Fill the article.
1200 Valid values are nil, t, `head', `first', `last', an integer or a
1201 predicate.  See Info node `(gnus)Customizing Articles'."
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-custom)
1205
1206 (defcustom gnus-treat-hide-citation nil
1207   "Hide cited text.
1208 Valid values are nil, t, `head', `first', `last', an integer or a
1209 predicate.  See Info node `(gnus)Customizing Articles'."
1210   :group 'gnus-article-treat
1211   :link '(custom-manual "(gnus)Customizing Articles")
1212   :type gnus-article-treat-custom)
1213
1214 (defcustom gnus-treat-hide-citation-maybe nil
1215   "Hide cited text.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :group 'gnus-article-treat
1219   :link '(custom-manual "(gnus)Customizing Articles")
1220   :type gnus-article-treat-custom)
1221
1222 (defcustom gnus-treat-strip-list-identifiers 'head
1223   "Strip list identifiers from `gnus-list-identifiers`.
1224 Valid values are nil, t, `head', `first', `last', an integer or a
1225 predicate.  See Info node `(gnus)Customizing Articles'."
1226   :version "21.1"
1227   :group 'gnus-article-treat
1228   :link '(custom-manual "(gnus)Customizing Articles")
1229   :type gnus-article-treat-custom)
1230
1231 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1232                         "Gnus 5.10 (Emacs 22.1)")
1233
1234 (defcustom gnus-treat-strip-pem nil
1235   "Strip PEM signatures.
1236 Valid values are nil, t, `head', `first', `last', an integer or a
1237 predicate.  See Info node `(gnus)Customizing Articles'."
1238   :group 'gnus-article-treat
1239   :link '(custom-manual "(gnus)Customizing Articles")
1240   :type gnus-article-treat-custom)
1241
1242 (defcustom gnus-treat-strip-banner t
1243   "Strip banners from articles.
1244 The banner to be stripped is specified in the `banner' group parameter.
1245 Valid values are nil, t, `head', `first', `last', an integer or a
1246 predicate.  See Info node `(gnus)Customizing Articles'."
1247   :group 'gnus-article-treat
1248   :link '(custom-manual "(gnus)Customizing Articles")
1249   :type gnus-article-treat-custom)
1250
1251 (defcustom gnus-treat-highlight-headers 'head
1252   "Highlight the headers.
1253 Valid values are nil, t, `head', `first', `last', an integer or a
1254 predicate.  See Info node `(gnus)Customizing Articles'."
1255   :group 'gnus-article-treat
1256   :link '(custom-manual "(gnus)Customizing Articles")
1257   :type gnus-article-treat-head-custom)
1258 (put 'gnus-treat-highlight-headers 'highlight t)
1259
1260 (defcustom gnus-treat-highlight-citation t
1261   "Highlight cited text.
1262 Valid values are nil, t, `head', `first', `last', an integer or a
1263 predicate.  See Info node `(gnus)Customizing Articles'."
1264   :group 'gnus-article-treat
1265   :link '(custom-manual "(gnus)Customizing Articles")
1266   :type gnus-article-treat-custom)
1267 (put 'gnus-treat-highlight-citation 'highlight t)
1268
1269 (defcustom gnus-treat-date-ut nil
1270   "Display the Date in UT (GMT).
1271 Valid values are nil, t, `head', `first', `last', an integer or a
1272 predicate.  See Info node `(gnus)Customizing Articles'."
1273   :group 'gnus-article-treat
1274   :link '(custom-manual "(gnus)Customizing Articles")
1275   :type gnus-article-treat-head-custom)
1276
1277 (defcustom gnus-treat-date-local nil
1278   "Display the Date in the local timezone.
1279 Valid values are nil, t, `head', `first', `last', an integer or a
1280 predicate.  See Info node `(gnus)Customizing Articles'."
1281   :group 'gnus-article-treat
1282   :link '(custom-manual "(gnus)Customizing Articles")
1283   :type gnus-article-treat-head-custom)
1284
1285 (defcustom gnus-treat-date-english nil
1286   "Display the Date in a format that can be read aloud in English.
1287 Valid values are nil, t, `head', `first', `last', an integer or a
1288 predicate.  See Info node `(gnus)Customizing Articles'."
1289   :version "22.1"
1290   :group 'gnus-article-treat
1291   :link '(custom-manual "(gnus)Customizing Articles")
1292   :type gnus-article-treat-head-custom)
1293
1294 (defcustom gnus-treat-date-lapsed nil
1295   "Display the Date header in a way that says how much time has elapsed.
1296 Valid values are nil, t, `head', `first', `last', an integer or a
1297 predicate.  See Info node `(gnus)Customizing Articles'."
1298   :group 'gnus-article-treat
1299   :link '(custom-manual "(gnus)Customizing Articles")
1300   :type gnus-article-treat-head-custom)
1301
1302 (defcustom gnus-treat-date-combined-lapsed 'head
1303   "Display the Date header in a way that says how much time has elapsed.
1304 Valid values are nil, t, `head', `first', `last', an integer or a
1305 predicate.  See Info node `(gnus)Customizing Articles'."
1306   :group 'gnus-article-treat
1307   :link '(custom-manual "(gnus)Customizing Articles")
1308   :type gnus-article-treat-head-custom)
1309
1310 (defcustom gnus-treat-date-original nil
1311   "Display the date in the original timezone.
1312 Valid values are nil, t, `head', `first', `last', an integer or a
1313 predicate.  See Info node `(gnus)Customizing Articles'."
1314   :group 'gnus-article-treat
1315   :link '(custom-manual "(gnus)Customizing Articles")
1316   :type gnus-article-treat-head-custom)
1317
1318 (defcustom gnus-treat-date-iso8601 nil
1319   "Display the date in the ISO8601 format.
1320 Valid values are nil, t, `head', `first', `last', an integer or a
1321 predicate.  See Info node `(gnus)Customizing Articles'."
1322   :version "21.1"
1323   :group 'gnus-article-treat
1324   :link '(custom-manual "(gnus)Customizing Articles")
1325   :type gnus-article-treat-head-custom)
1326
1327 (defcustom gnus-treat-date-user-defined nil
1328   "Display the date in a user-defined format.
1329 The format is defined by the `gnus-article-time-format' variable.
1330 Valid values are nil, t, `head', `first', `last', an integer or a
1331 predicate.  See Info node `(gnus)Customizing Articles'."
1332   :group 'gnus-article-treat
1333   :link '(custom-manual "(gnus)Customizing Articles")
1334   :type gnus-article-treat-head-custom)
1335
1336 (defcustom gnus-treat-strip-headers-in-body t
1337   "Strip the X-No-Archive header line from the beginning of the body.
1338 Valid values are nil, t, `head', `first', `last', an integer or a
1339 predicate.  See Info node `(gnus)Customizing Articles'."
1340   :version "21.1"
1341   :group 'gnus-article-treat
1342   :link '(custom-manual "(gnus)Customizing Articles")
1343   :type gnus-article-treat-custom)
1344
1345 (defcustom gnus-treat-strip-trailing-blank-lines nil
1346   "Strip trailing blank lines.
1347 Valid values are nil, t, `head', `first', `last', an integer or a
1348 predicate.  See Info node `(gnus)Customizing Articles'.
1349
1350 When set to t, it also strips trailing blanks in all MIME parts.
1351 Consider to use `last' instead."
1352   :group 'gnus-article-treat
1353   :link '(custom-manual "(gnus)Customizing Articles")
1354   :type gnus-article-treat-custom)
1355
1356 (defcustom gnus-treat-strip-leading-blank-lines nil
1357   "Strip leading blank lines.
1358 Valid values are nil, t, `head', `first', `last', an integer or a
1359 predicate.  See Info node `(gnus)Customizing Articles'.
1360
1361 When set to t, it also strips trailing blanks in all MIME parts."
1362   :group 'gnus-article-treat
1363   :link '(custom-manual "(gnus)Customizing Articles")
1364   :type gnus-article-treat-custom)
1365
1366 (defcustom gnus-treat-strip-multiple-blank-lines nil
1367   "Strip multiple blank lines.
1368 Valid values are nil, t, `head', `first', `last', an integer or a
1369 predicate.  See Info node `(gnus)Customizing Articles'."
1370   :group 'gnus-article-treat
1371   :link '(custom-manual "(gnus)Customizing Articles")
1372   :type gnus-article-treat-custom)
1373
1374 (defcustom gnus-treat-unfold-headers 'head
1375   "Unfold folded header lines.
1376 Valid values are nil, t, `head', `first', `last', an integer or a
1377 predicate.  See Info node `(gnus)Customizing Articles'."
1378   :version "22.1"
1379   :group 'gnus-article-treat
1380   :link '(custom-manual "(gnus)Customizing Articles")
1381   :type gnus-article-treat-custom)
1382
1383 (defcustom gnus-article-unfold-long-headers nil
1384   "If non-nil, allow unfolding headers even if the header is long.
1385 If it is a regexp, only long headers matching this regexp are unfolded.
1386 If it is t, all long headers are unfolded.
1387
1388 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1389   :version "23.1" ;; No Gnus
1390   :group 'gnus-article-treat
1391   :type '(choice (const nil)
1392                  (const :tag "all" t)
1393                  (regexp)))
1394
1395 (defcustom gnus-treat-fold-headers nil
1396   "Fold headers.
1397 Valid values are nil, t, `head', `first', `last', an integer or a
1398 predicate.  See Info node `(gnus)Customizing Articles'."
1399   :version "22.1"
1400   :group 'gnus-article-treat
1401   :link '(custom-manual "(gnus)Customizing Articles")
1402   :type gnus-article-treat-custom)
1403
1404 (defcustom gnus-treat-fold-newsgroups 'head
1405   "Fold the Newsgroups and Followup-To headers.
1406 Valid values are nil, t, `head', `first', `last', an integer or a
1407 predicate.  See Info node `(gnus)Customizing Articles'."
1408   :version "22.1"
1409   :group 'gnus-article-treat
1410   :link '(custom-manual "(gnus)Customizing Articles")
1411   :type gnus-article-treat-custom)
1412
1413 (defcustom gnus-treat-overstrike t
1414   "Treat overstrike highlighting.
1415 Valid values are nil, t, `head', `first', `last', an integer or a
1416 predicate.  See Info node `(gnus)Customizing Articles'."
1417   :group 'gnus-article-treat
1418   :link '(custom-manual "(gnus)Customizing Articles")
1419   :type gnus-article-treat-custom)
1420 (put 'gnus-treat-overstrike 'highlight t)
1421
1422 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1423   "Treat ANSI SGR control sequences.
1424 Valid values are nil, t, `head', `first', `last', an integer or a
1425 predicate.  See Info node `(gnus)Customizing Articles'."
1426   :group 'gnus-article-treat
1427   :link '(custom-manual "(gnus)Customizing Articles")
1428   :type gnus-article-treat-custom)
1429
1430 (make-obsolete-variable 'gnus-treat-display-xface
1431                         'gnus-treat-display-x-face "Emacs 22.1")
1432
1433 (defcustom gnus-treat-display-x-face
1434   (and (not noninteractive)
1435        (gnus-image-type-available-p 'xbm)
1436        (if (featurep 'xemacs)
1437            (featurep 'xface)
1438          (condition-case nil
1439              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1440                   (executable-find "icontopbm"))
1441            ;; shell-command-to-string may signal an error, e.g. if
1442            ;; shell-file-name is not found.
1443            (error nil)))
1444        'head)
1445   "Display X-Face headers.
1446 Valid values are nil and `head'.
1447 See Info node `(gnus)Customizing Articles' and Info node
1448 `(gnus)X-Face' for details."
1449   :group 'gnus-article-treat
1450   :version "21.1"
1451   :link '(custom-manual "(gnus)Customizing Articles")
1452   :link '(custom-manual "(gnus)X-Face")
1453   :type gnus-article-treat-head-custom
1454   :set (lambda (symbol value)
1455          (set-default
1456           symbol
1457           (cond ((or (boundp symbol) (get symbol 'saved-value))
1458                  value)
1459                 ((boundp 'gnus-treat-display-xface)
1460                  (message "\
1461 ** gnus-treat-display-xface is an obsolete variable;\
1462  use gnus-treat-display-x-face instead")
1463                  (default-value 'gnus-treat-display-xface))
1464                 ((get 'gnus-treat-display-xface 'saved-value)
1465                  (message "\
1466 ** gnus-treat-display-xface is an obsolete variable;\
1467  use gnus-treat-display-x-face instead")
1468                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1469                 (t
1470                  value)))))
1471 (put 'gnus-treat-display-x-face 'highlight t)
1472
1473 (defcustom gnus-treat-display-face
1474   (and (not noninteractive)
1475        (gnus-image-type-available-p 'png)
1476        'head)
1477   "Display Face headers.
1478 Valid values are nil, t, `head', `first', `last', an integer or a
1479 predicate.  See Info node `(gnus)Customizing Articles' and Info
1480 node `(gnus)Face' for details."
1481   :group 'gnus-article-treat
1482   :version "22.1"
1483   :link '(custom-manual "(gnus)Customizing Articles")
1484   :link '(custom-manual "(gnus)X-Face")
1485   :type gnus-article-treat-head-custom)
1486 (put 'gnus-treat-display-face 'highlight t)
1487
1488 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1489   "Display smileys.
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)Smileys' for details."
1493   :group 'gnus-article-treat
1494   :version "21.1"
1495   :link '(custom-manual "(gnus)Customizing Articles")
1496   :link '(custom-manual "(gnus)Smileys")
1497   :type gnus-article-treat-custom)
1498 (put 'gnus-treat-display-smileys 'highlight t)
1499
1500 (defcustom gnus-treat-from-picon
1501   (if (and (gnus-image-type-available-p 'xpm)
1502            (gnus-picons-installed-p))
1503       'head nil)
1504   "Display picons in the From header.
1505 Valid values are nil, t, `head', `first', `last', an integer or a
1506 predicate.  See Info node `(gnus)Customizing Articles' and Info
1507 node `(gnus)Picons' for details."
1508   :version "22.1"
1509   :group 'gnus-article-treat
1510   :group 'gnus-picon
1511   :link '(custom-manual "(gnus)Customizing Articles")
1512   :link '(custom-manual "(gnus)Picons")
1513   :type gnus-article-treat-head-custom)
1514 (put 'gnus-treat-from-picon 'highlight t)
1515
1516 (defcustom gnus-treat-mail-picon
1517   (if (and (gnus-image-type-available-p 'xpm)
1518            (gnus-picons-installed-p))
1519       'head nil)
1520   "Display picons in To and Cc headers.
1521 Valid values are nil, t, `head', `first', `last', an integer or a
1522 predicate.  See Info node `(gnus)Customizing Articles' and Info
1523 node `(gnus)Picons' for details."
1524   :version "22.1"
1525   :group 'gnus-article-treat
1526   :group 'gnus-picon
1527   :link '(custom-manual "(gnus)Customizing Articles")
1528   :link '(custom-manual "(gnus)Picons")
1529   :type gnus-article-treat-head-custom)
1530 (put 'gnus-treat-mail-picon 'highlight t)
1531
1532 (defcustom gnus-treat-newsgroups-picon
1533   (if (and (gnus-image-type-available-p 'xpm)
1534            (gnus-picons-installed-p))
1535       'head nil)
1536   "Display picons in the Newsgroups and Followup-To headers.
1537 Valid values are nil, t, `head', `first', `last', an integer or a
1538 predicate.  See Info node `(gnus)Customizing Articles' and Info
1539 node `(gnus)Picons' for details."
1540   :version "22.1"
1541   :group 'gnus-article-treat
1542   :group 'gnus-picon
1543   :link '(custom-manual "(gnus)Customizing Articles")
1544   :link '(custom-manual "(gnus)Picons")
1545   :type gnus-article-treat-head-custom)
1546 (put 'gnus-treat-newsgroups-picon 'highlight t)
1547
1548 (defcustom gnus-treat-from-gravatar nil
1549   "Display gravatars in the From header.
1550 Valid values are nil, t, `head', `first', `last', an integer or a
1551 predicate.  See Info node `(gnus)Customizing Articles' and Info
1552 node `(gnus)Gravatars' for details."
1553   :version "24.1"
1554   :group 'gnus-article-treat
1555   :group 'gnus-gravatar
1556   :link '(custom-manual "(gnus)Customizing Articles")
1557   :link '(custom-manual "(gnus)Gravatars")
1558   :type gnus-article-treat-head-custom)
1559 (put 'gnus-treat-from-gravatar 'highlight t)
1560
1561 (defcustom gnus-treat-mail-gravatar nil
1562   "Display gravatars in To and Cc headers.
1563 Valid values are nil, t, `head', `first', `last', an integer or a
1564 predicate.  See Info node `(gnus)Customizing Articles' and Info
1565 node `(gnus)Gravatars' for details."
1566   :version "24.1"
1567   :group 'gnus-article-treat
1568   :group 'gnus-gravatar
1569   :link '(custom-manual "(gnus)Customizing Articles")
1570   :link '(custom-manual "(gnus)Gravatars")
1571   :type gnus-article-treat-head-custom)
1572 (put 'gnus-treat-mail-gravatar 'highlight t)
1573
1574 (defcustom gnus-treat-body-boundary
1575   (if (or gnus-treat-newsgroups-picon
1576           gnus-treat-mail-picon
1577           gnus-treat-from-picon
1578           gnus-treat-from-gravatar
1579           gnus-treat-mail-gravatar)
1580       ;; If there's much decoration, the user might prefer a boundery.
1581       'head
1582     nil)
1583   "Draw a boundary at the end of the headers.
1584 Valid values are nil and `head'.
1585 See Info node `(gnus)Customizing Articles' for details."
1586   :version "22.1"
1587   :group 'gnus-article-treat
1588   :link '(custom-manual "(gnus)Customizing Articles")
1589   :type gnus-article-treat-head-custom)
1590
1591 (defcustom gnus-treat-capitalize-sentences nil
1592   "Capitalize sentence-starting words.
1593 Valid values are nil, t, `head', `first', `last', an integer or a
1594 predicate.  See Info node `(gnus)Customizing Articles'."
1595   :version "21.1"
1596   :group 'gnus-article-treat
1597   :link '(custom-manual "(gnus)Customizing Articles")
1598   :type gnus-article-treat-custom)
1599
1600 (defcustom gnus-treat-wash-html nil
1601   "Format as HTML.
1602 Valid values are nil, t, `head', `first', `last', an integer or a
1603 predicate.  See Info node `(gnus)Customizing Articles'."
1604   :version "22.1"
1605   :group 'gnus-article-treat
1606   :link '(custom-manual "(gnus)Customizing Articles")
1607   :type gnus-article-treat-custom)
1608
1609 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1610   "Fill long lines.
1611 Valid values are nil, t, `head', `first', `last', an integer or a
1612 predicate.  See Info node `(gnus)Customizing Articles'."
1613   :version "24.1"
1614   :group 'gnus-article-treat
1615   :link '(custom-manual "(gnus)Customizing Articles")
1616   :type gnus-article-treat-custom)
1617
1618 (defcustom gnus-treat-x-pgp-sig nil
1619   "Verify X-PGP-Sig.
1620 To automatically treat X-PGP-Sig, set it to head.
1621 Valid values are nil, t, `head', `first', `last', an integer or a
1622 predicate.  See Info node `(gnus)Customizing Articles'."
1623   :version "22.1"
1624   :group 'gnus-article-treat
1625   :group 'mime-security
1626   :link '(custom-manual "(gnus)Customizing Articles")
1627   :type gnus-article-treat-custom)
1628
1629 (defvar gnus-article-encrypt-protocol-alist
1630   '(("PGP" . mml2015-self-encrypt)))
1631
1632 ;; Set to nil if more than one protocol added to
1633 ;; gnus-article-encrypt-protocol-alist.
1634 (defcustom gnus-article-encrypt-protocol "PGP"
1635   "The protocol used for encrypt articles.
1636 It is a string, such as \"PGP\". If nil, ask user."
1637   :version "22.1"
1638   :type 'string
1639   :group 'mime-security)
1640
1641 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1642                               (mm-coding-system-p 'utf-8)
1643                               (executable-find idna-program))
1644   "Whether IDNA decoding of headers is used when viewing messages.
1645 This requires GNU Libidn, and by default only enabled if it is found."
1646   :version "22.1"
1647   :group 'gnus-article-headers
1648   :type 'boolean)
1649
1650 (defcustom gnus-article-over-scroll nil
1651   "If non-nil, allow scrolling the article buffer even when there no more text."
1652   :version "22.1"
1653   :group 'gnus-article
1654   :type 'boolean)
1655
1656 (defcustom gnus-inhibit-images nil
1657   "Non-nil means inhibit displaying of images inline in the article body."
1658   :version "24.1"
1659   :group 'gnus-article
1660   :type 'boolean)
1661
1662 (defcustom gnus-blocked-images 'gnus-block-private-groups
1663   "Images that have URLs matching this regexp will be blocked.
1664 This can also be a function to be evaluated.  If so, it will be
1665 called with the group name as the parameter, and should return a
1666 regexp."
1667   :version "24.1"
1668   :group 'gnus-art
1669   :type 'regexp)
1670
1671 ;;; Internal variables
1672
1673 (defvar gnus-english-month-names
1674   '("January" "February" "March" "April" "May" "June" "July" "August"
1675     "September" "October" "November" "December"))
1676
1677 (defvar article-goto-body-goes-to-point-min-p nil)
1678 (defvar gnus-article-wash-types nil)
1679 (defvar gnus-article-emphasis-alist nil)
1680 (defvar gnus-article-image-alist nil)
1681
1682 (defvar gnus-article-mime-handle-alist-1 nil)
1683 (defvar gnus-treatment-function-alist
1684   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1685     (gnus-treat-strip-banner gnus-article-strip-banner)
1686     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1687     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1688     (gnus-treat-buttonize gnus-article-add-buttons)
1689     (gnus-treat-fill-article gnus-article-fill-cited-article)
1690     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1691     (gnus-treat-strip-cr gnus-article-remove-cr)
1692     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1693     (gnus-treat-date-ut gnus-article-date-ut)
1694     (gnus-treat-date-local gnus-article-date-local)
1695     (gnus-treat-date-english gnus-article-date-english)
1696     (gnus-treat-date-original gnus-article-date-original)
1697     (gnus-treat-date-user-defined gnus-article-date-user)
1698     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1699     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1700     (gnus-treat-date-combined-lapsed gnus-article-date-combined-lapsed)
1701     (gnus-treat-display-x-face gnus-article-display-x-face)
1702     (gnus-treat-display-face gnus-article-display-face)
1703     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1704     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1705     (gnus-treat-hide-signature gnus-article-hide-signature)
1706     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1707     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1708     (gnus-treat-from-picon gnus-treat-from-picon)
1709     (gnus-treat-mail-picon gnus-treat-mail-picon)
1710     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1711     (gnus-treat-strip-pem gnus-article-hide-pem)
1712     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1713     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1714     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1715     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1716     (gnus-treat-strip-trailing-blank-lines
1717      gnus-article-remove-trailing-blank-lines)
1718     (gnus-treat-strip-leading-blank-lines
1719      gnus-article-strip-leading-blank-lines)
1720     (gnus-treat-strip-multiple-blank-lines
1721      gnus-article-strip-multiple-blank-lines)
1722     (gnus-treat-overstrike gnus-article-treat-overstrike)
1723     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1724     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1725     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1726     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1727     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1728     (gnus-treat-display-smileys gnus-treat-smiley)
1729     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1730     (gnus-treat-wash-html gnus-article-wash-html)
1731     (gnus-treat-emphasize gnus-article-emphasize)
1732     (gnus-treat-hide-citation gnus-article-hide-citation)
1733     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1734     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1735     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1736
1737 (defvar gnus-article-mime-handle-alist nil)
1738 (defvar article-lapsed-timer nil)
1739 (defvar gnus-article-current-summary nil)
1740
1741 (defvar gnus-article-mode-syntax-table
1742   (let ((table (copy-syntax-table text-mode-syntax-table)))
1743     ;; This causes the citation match run O(2^n).
1744     ;; (modify-syntax-entry ?- "w" table)
1745     (modify-syntax-entry ?> ")<" table)
1746     (modify-syntax-entry ?< "(>" table)
1747     ;; make M-. in article buffers work for `foo' strings
1748     (modify-syntax-entry ?' " " table)
1749     (modify-syntax-entry ?` " " table)
1750     table)
1751   "Syntax table used in article mode buffers.
1752 Initialized from `text-mode-syntax-table.")
1753
1754 (defvar gnus-save-article-buffer nil)
1755
1756 (defvar gnus-number-of-articles-to-be-saved nil)
1757
1758 (defvar gnus-inhibit-hiding nil)
1759
1760 (defvar gnus-article-edit-mode nil)
1761
1762 ;;; Macros for dealing with the article buffer.
1763
1764 (defmacro gnus-with-article-headers (&rest forms)
1765   `(with-current-buffer gnus-article-buffer
1766      (save-restriction
1767        (let ((inhibit-read-only t)
1768              (inhibit-point-motion-hooks t)
1769              (case-fold-search t))
1770          (article-narrow-to-head)
1771          ,@forms))))
1772
1773 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1774 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1775
1776 (defmacro gnus-with-article-buffer (&rest forms)
1777   `(with-current-buffer gnus-article-buffer
1778      (let ((inhibit-read-only t))
1779        ,@forms)))
1780
1781 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1782 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1783
1784 (defun gnus-article-goto-header (header)
1785   "Go to HEADER, which is a regular expression."
1786   (re-search-forward (concat "^\\(" header "\\):") nil t))
1787
1788 (defsubst gnus-article-hide-text (b e props)
1789   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1790   (gnus-add-text-properties-when 'article-type nil b e props)
1791   (when (memq 'intangible props)
1792     (put-text-property
1793      (max (1- b) (point-min))
1794      b 'intangible (cddr (memq 'intangible props)))))
1795
1796 (defsubst gnus-article-unhide-text (b e)
1797   "Remove hidden text properties from region between B and E."
1798   (remove-text-properties b e gnus-hidden-properties)
1799   (when (memq 'intangible gnus-hidden-properties)
1800     (put-text-property (max (1- b) (point-min))
1801                        b 'intangible nil)))
1802
1803 (defun gnus-article-hide-text-type (b e type)
1804   "Hide text of TYPE between B and E."
1805   (gnus-add-wash-type type)
1806   (gnus-article-hide-text
1807    b e (cons 'article-type (cons type gnus-hidden-properties))))
1808
1809 (defun gnus-article-unhide-text-type (b e type)
1810   "Unhide text of TYPE between B and E."
1811   (gnus-delete-wash-type type)
1812   (remove-text-properties
1813    b e (cons 'article-type (cons type gnus-hidden-properties)))
1814   (when (memq 'intangible gnus-hidden-properties)
1815     (put-text-property (max (1- b) (point-min))
1816                        b 'intangible nil)))
1817
1818 (defun gnus-article-hide-text-of-type (type)
1819   "Hide text of TYPE in the current buffer."
1820   (save-excursion
1821     (let ((b (point-min))
1822           (e (point-max)))
1823       (while (setq b (text-property-any b e 'article-type type))
1824         (add-text-properties b (incf b) gnus-hidden-properties)))))
1825
1826 (defun gnus-article-delete-text-of-type (type)
1827   "Delete text of TYPE in the current buffer."
1828   (save-excursion
1829     (let ((b (point-min)))
1830       (if (eq type 'multipart)
1831           ;; Remove MIME buttons associated with multipart/alternative parts.
1832           (progn
1833             (goto-char b)
1834             (while (if (get-text-property (point) 'gnus-part)
1835                        (setq b (point))
1836                      (when (setq b (next-single-property-change (point)
1837                                                                 'gnus-part))
1838                        (goto-char b)
1839                        t))
1840               (end-of-line)
1841               (skip-chars-forward "\n")
1842               (when (eq (get-text-property b 'article-type) 'multipart)
1843                 (delete-region b (point)))))
1844         (while (setq b (text-property-any b (point-max) 'article-type type))
1845           (delete-region
1846            b (or (text-property-not-all b (point-max) 'article-type type)
1847                  (point-max))))))))
1848
1849 (defun gnus-article-delete-invisible-text ()
1850   "Delete all invisible text in the current buffer."
1851   (save-excursion
1852     (let ((b (point-min)))
1853       (while (setq b (text-property-any b (point-max) 'invisible t))
1854         (delete-region
1855          b (or (text-property-not-all b (point-max) 'invisible t)
1856                (point-max)))))))
1857
1858 (defun gnus-article-text-type-exists-p (type)
1859   "Say whether any text of type TYPE exists in the buffer."
1860   (text-property-any (point-min) (point-max) 'article-type type))
1861
1862 (defsubst gnus-article-header-rank ()
1863   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1864   (let ((list gnus-sorted-header-list)
1865         (i 1))
1866     (while list
1867       (if (looking-at (car list))
1868           (setq list nil)
1869         (setq list (cdr list))
1870         (incf i)))
1871       i))
1872
1873 (defun article-hide-headers (&optional arg delete)
1874   "Hide unwanted headers and possibly sort them as well."
1875   (interactive)
1876   ;; This function might be inhibited.
1877   (unless gnus-inhibit-hiding
1878     (let ((inhibit-read-only t)
1879           (case-fold-search t)
1880           (max (1+ (length gnus-sorted-header-list)))
1881           (inhibit-point-motion-hooks t)
1882           (cur (current-buffer))
1883           ignored visible beg)
1884       (save-excursion
1885         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1886         ;; group parameters, so we should go to the summary buffer.
1887         (when (prog1
1888                   (condition-case nil
1889                       (progn (set-buffer gnus-summary-buffer) t)
1890                     (error nil))
1891                 (setq ignored (when (not gnus-visible-headers)
1892                                 (cond ((stringp gnus-ignored-headers)
1893                                        gnus-ignored-headers)
1894                                       ((listp gnus-ignored-headers)
1895                                        (mapconcat 'identity
1896                                                   gnus-ignored-headers
1897                                                   "\\|"))))
1898                       visible (cond ((stringp gnus-visible-headers)
1899                                      gnus-visible-headers)
1900                                     ((and gnus-visible-headers
1901                                           (listp gnus-visible-headers))
1902                                      (mapconcat 'identity
1903                                                 gnus-visible-headers
1904                                                 "\\|")))))
1905           (set-buffer cur))
1906         (save-restriction
1907           ;; First we narrow to just the headers.
1908           (article-narrow-to-head)
1909           ;; Hide any "From " lines at the beginning of (mail) articles.
1910           (while (looking-at "From ")
1911             (forward-line 1))
1912           (unless (bobp)
1913             (delete-region (point-min) (point)))
1914           ;; Then treat the rest of the header lines.
1915           ;; Then we use the two regular expressions
1916           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1917           ;; select which header lines is to remain visible in the
1918           ;; article buffer.
1919           (while (re-search-forward "^[^ \t:]*:" nil t)
1920             (beginning-of-line)
1921             ;; Mark the rank of the header.
1922             (put-text-property
1923              (point) (1+ (point)) 'message-rank
1924              (if (or (and visible (looking-at visible))
1925                      (and ignored
1926                           (not (looking-at ignored))))
1927                  (gnus-article-header-rank)
1928                (+ 2 max)))
1929             (forward-line 1))
1930           (message-sort-headers-1)
1931           (when (setq beg (text-property-any
1932                            (point-min) (point-max) 'message-rank (+ 2 max)))
1933             ;; We delete the unwanted headers.
1934             (gnus-add-wash-type 'headers)
1935             (add-text-properties (point-min) (+ 5 (point-min))
1936                                  '(article-type headers dummy-invisible t))
1937             (delete-region beg (point-max))))))))
1938
1939 (defun article-hide-boring-headers (&optional arg)
1940   "Toggle hiding of headers that aren't very interesting.
1941 If given a negative prefix, always show; if given a positive prefix,
1942 always hide."
1943   (interactive (gnus-article-hidden-arg))
1944   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1945              (not gnus-show-all-headers))
1946     (save-excursion
1947       (save-restriction
1948         (let ((inhibit-read-only t)
1949               (inhibit-point-motion-hooks t))
1950           (article-narrow-to-head)
1951           (dolist (elem gnus-boring-article-headers)
1952             (goto-char (point-min))
1953             (cond
1954              ;; Hide empty headers.
1955              ((eq elem 'empty)
1956               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1957                 (forward-line -1)
1958                 (gnus-article-hide-text-type
1959                  (point-at-bol)
1960                  (progn
1961                    (end-of-line)
1962                    (if (re-search-forward "^[^ \t]" nil t)
1963                        (match-beginning 0)
1964                      (point-max)))
1965                  'boring-headers)))
1966              ;; Hide boring Newsgroups header.
1967              ((eq elem 'newsgroups)
1968               (when (gnus-string-equal
1969                      (gnus-fetch-field "newsgroups")
1970                      (gnus-group-real-name
1971                       (if (boundp 'gnus-newsgroup-name)
1972                           gnus-newsgroup-name
1973                         "")))
1974                 (gnus-article-hide-header "newsgroups")))
1975              ((eq elem 'to-address)
1976               (let ((to (message-fetch-field "to"))
1977                     (to-address
1978                      (gnus-parameter-to-address
1979                       (if (boundp 'gnus-newsgroup-name)
1980                           gnus-newsgroup-name ""))))
1981                 (when (and to to-address
1982                            (ignore-errors
1983                              (gnus-string-equal
1984                               ;; only one address in To
1985                               (nth 1 (mail-extract-address-components to))
1986                               to-address)))
1987                   (gnus-article-hide-header "to"))))
1988              ((eq elem 'to-list)
1989               (let ((to (message-fetch-field "to"))
1990                     (to-list
1991                      (gnus-parameter-to-list
1992                       (if (boundp 'gnus-newsgroup-name)
1993                           gnus-newsgroup-name ""))))
1994                 (when (and to to-list
1995                            (ignore-errors
1996                              (gnus-string-equal
1997                               ;; only one address in To
1998                               (nth 1 (mail-extract-address-components to))
1999                               to-list)))
2000                   (gnus-article-hide-header "to"))))
2001              ((eq elem 'cc-list)
2002               (let ((cc (message-fetch-field "cc"))
2003                     (to-list
2004                      (gnus-parameter-to-list
2005                       (if (boundp 'gnus-newsgroup-name)
2006                           gnus-newsgroup-name ""))))
2007                 (when (and cc to-list
2008                            (ignore-errors
2009                              (gnus-string-equal
2010                               ;; only one address in CC
2011                               (nth 1 (mail-extract-address-components cc))
2012                               to-list)))
2013                   (gnus-article-hide-header "cc"))))
2014              ((eq elem 'followup-to)
2015               (when (gnus-string-equal
2016                      (message-fetch-field "followup-to")
2017                      (message-fetch-field "newsgroups"))
2018                 (gnus-article-hide-header "followup-to")))
2019              ((eq elem 'reply-to)
2020               (if (gnus-group-find-parameter
2021                    gnus-newsgroup-name 'broken-reply-to)
2022                   (gnus-article-hide-header "reply-to")
2023                 (let ((from (message-fetch-field "from"))
2024                       (reply-to (message-fetch-field "reply-to")))
2025                   (when
2026                       (and
2027                        from reply-to
2028                        (ignore-errors
2029                          (equal
2030                           (sort (mapcar
2031                                  (lambda (x) (downcase (cadr x)))
2032                                  (mail-extract-address-components from t))
2033                                 'string<)
2034                           (sort (mapcar
2035                                  (lambda (x) (downcase (cadr x)))
2036                                  (mail-extract-address-components reply-to t))
2037                                 'string<))))
2038                     (gnus-article-hide-header "reply-to")))))
2039              ((eq elem 'date)
2040               (let ((date (with-current-buffer gnus-original-article-buffer
2041                             ;; If date in `gnus-article-buffer' is localized
2042                             ;; (`gnus-treat-date-user-defined'),
2043                             ;; `days-between' might fail.
2044                             (message-fetch-field "date"))))
2045                 (when (and date
2046                            (< (days-between (current-time-string) date)
2047                               4))
2048                   (gnus-article-hide-header "date"))))
2049              ((eq elem 'long-to)
2050               (let ((to (message-fetch-field "to"))
2051                     (cc (message-fetch-field "cc")))
2052                 (when (> (length to) 1024)
2053                   (gnus-article-hide-header "to"))
2054                 (when (> (length cc) 1024)
2055                   (gnus-article-hide-header "cc"))))
2056              ((eq elem 'many-to)
2057               (let ((to-count 0)
2058                     (cc-count 0))
2059                 (goto-char (point-min))
2060                 (while (re-search-forward "^to:" nil t)
2061                   (setq to-count (1+ to-count)))
2062                 (when (> to-count 1)
2063                   (while (> to-count 0)
2064                     (goto-char (point-min))
2065                     (save-restriction
2066                       (re-search-forward "^to:" nil nil to-count)
2067                       (forward-line -1)
2068                       (narrow-to-region (point) (point-max))
2069                       (gnus-article-hide-header "to"))
2070                     (setq to-count (1- to-count))))
2071                 (goto-char (point-min))
2072                 (while (re-search-forward "^cc:" nil t)
2073                   (setq cc-count (1+ cc-count)))
2074                 (when (> cc-count 1)
2075                   (while (> cc-count 0)
2076                     (goto-char (point-min))
2077                     (save-restriction
2078                       (re-search-forward "^cc:" nil nil cc-count)
2079                       (forward-line -1)
2080                       (narrow-to-region (point) (point-max))
2081                       (gnus-article-hide-header "cc"))
2082                     (setq cc-count (1- cc-count)))))))))))))
2083
2084 (defun gnus-article-hide-header (header)
2085   (save-excursion
2086     (goto-char (point-min))
2087     (when (re-search-forward (concat "^" header ":") nil t)
2088       (gnus-article-hide-text-type
2089        (point-at-bol)
2090        (progn
2091          (end-of-line)
2092          (if (re-search-forward "^[^ \t]" nil t)
2093              (match-beginning 0)
2094            (point-max)))
2095        'boring-headers))))
2096
2097 (defvar gnus-article-normalized-header-length 40
2098   "Length of normalized headers.")
2099
2100 (defun article-normalize-headers ()
2101   "Make all header lines 40 characters long."
2102   (interactive)
2103   (let ((inhibit-read-only t)
2104         column)
2105     (save-excursion
2106       (save-restriction
2107         (article-narrow-to-head)
2108         (while (not (eobp))
2109           (cond
2110            ((< (setq column (- (point-at-eol) (point)))
2111                gnus-article-normalized-header-length)
2112             (end-of-line)
2113             (insert (make-string
2114                      (- gnus-article-normalized-header-length column)
2115                      ? )))
2116            ((> column gnus-article-normalized-header-length)
2117             (gnus-put-text-property
2118              (progn
2119                (forward-char gnus-article-normalized-header-length)
2120                (point))
2121              (point-at-eol)
2122              'invisible t))
2123            (t
2124             ;; Do nothing.
2125             ))
2126           (forward-line 1))))))
2127
2128 (defun article-treat-dumbquotes ()
2129   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2130 Note that this function guesses whether a character is a sm*rtq**t* or
2131 not, so it should only be used interactively.
2132
2133 Sm*rtq**t*s are M****s***'s unilateral extension to the
2134 iso-8859-1 character map in an attempt to provide more quoting
2135 characters.  If you see something like \\222 or \\264 where
2136 you're expecting some kind of apostrophe or quotation mark, then
2137 try this wash."
2138   (interactive)
2139   (article-translate-strings gnus-article-dumbquotes-map))
2140
2141 (defvar org-entities)
2142
2143 (defun article-treat-non-ascii ()
2144   "Translate many Unicode characters into their ASCII equivalents."
2145   (interactive)
2146   (require 'org-entities)
2147   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2148     (dolist (elem org-entities)
2149       (when (and (listp elem)
2150                  (= (length (nth 6 elem)) 1))
2151         (if (featurep 'xemacs)
2152             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2153           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2154     (save-excursion
2155       (when (article-goto-body)
2156         (let ((inhibit-read-only t)
2157               replace props)
2158           (while (not (eobp))
2159             (if (not (setq replace (if (featurep 'xemacs)
2160                                        (get-char-table (following-char) table)
2161                                      (aref table (following-char)))))
2162                 (forward-char 1)
2163               (if (prog1
2164                       (setq props (text-properties-at (point)))
2165                     (delete-char 1))
2166                   (add-text-properties (point) (progn (insert replace) (point))
2167                                        props)
2168                 (insert replace)))))))))
2169
2170 (defun article-translate-characters (from to)
2171   "Translate all characters in the body of the article according to FROM and TO.
2172 FROM is a string of characters to translate from; to is a string of
2173 characters to translate to."
2174   (save-excursion
2175     (when (article-goto-body)
2176       (let ((inhibit-read-only t)
2177             (x (make-string 225 ?x))
2178             (i -1))
2179         (while (< (incf i) (length x))
2180           (aset x i i))
2181         (setq i 0)
2182         (while (< i (length from))
2183           (aset x (aref from i) (aref to i))
2184           (incf i))
2185         (translate-region (point) (point-max) x)))))
2186
2187 (defun article-translate-strings (map)
2188   "Translate all string in the body of the article according to MAP.
2189 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2190   (save-excursion
2191     (when (article-goto-body)
2192       (let ((inhibit-read-only t))
2193         (dolist (elem map)
2194           (let ((from (car elem))
2195                 (to (cadr elem)))
2196             (save-excursion
2197               (if (stringp from)
2198                   (while (search-forward from nil t)
2199                     (replace-match to))
2200                 (while (not (eobp))
2201                   (if (eq (following-char) from)
2202                       (progn
2203                         (delete-char 1)
2204                         (insert to))
2205                     (forward-char 1)))))))))))
2206
2207 (defun article-treat-overstrike ()
2208   "Translate overstrikes into bold text."
2209   (interactive)
2210   (save-excursion
2211     (when (article-goto-body)
2212       (let ((inhibit-read-only t))
2213         (while (search-forward "\b" nil t)
2214           (let ((next (char-after))
2215                 (previous (char-after (- (point) 2))))
2216             ;; We do the boldification/underlining by hiding the
2217             ;; overstrikes and putting the proper text property
2218             ;; on the letters.
2219             (cond
2220              ((eq next previous)
2221               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2222               (put-text-property (point) (1+ (point)) 'face 'bold))
2223              ((eq next ?_)
2224               (gnus-article-hide-text-type
2225                (1- (point)) (1+ (point)) 'overstrike)
2226               (put-text-property
2227                (- (point) 2) (1- (point)) 'face 'underline))
2228              ((eq previous ?_)
2229               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2230               (put-text-property
2231                (point) (1+ (point)) 'face 'underline)))))))))
2232
2233 (defun article-treat-ansi-sequences ()
2234   "Translate ANSI SGR control sequences into overlays or extents."
2235   (interactive)
2236   (save-excursion
2237     (when (article-goto-body)
2238       (let ((inhibit-read-only t))
2239         (ansi-color-apply-on-region (point) (point-max))))))
2240
2241 (defun gnus-article-treat-unfold-headers ()
2242   "Unfold folded message headers.
2243 Only the headers that fit into the current window width will be
2244 unfolded."
2245   (interactive)
2246   (gnus-with-article-headers
2247     (let (length)
2248       (while (not (eobp))
2249         (save-restriction
2250           (mail-header-narrow-to-field)
2251           (let* ((header (buffer-string))
2252                  (unfoldable
2253                   (or (equal gnus-article-unfold-long-headers t)
2254                       (and (stringp gnus-article-unfold-long-headers)
2255                            (string-match gnus-article-unfold-long-headers header)))))
2256             (with-temp-buffer
2257               (insert header)
2258               (goto-char (point-min))
2259               (while (re-search-forward "\n[\t ]" nil t)
2260                 (replace-match " " t t)))
2261             (setq length (- (point-max) (point-min) 1))
2262             (when (or unfoldable
2263                       (< length (window-width)))
2264               (while (re-search-forward "\n[\t ]" nil t)
2265                 (replace-match " " t t))))
2266           (goto-char (point-max)))))))
2267
2268 (defun gnus-article-treat-fold-headers ()
2269   "Fold message headers."
2270   (interactive)
2271   (gnus-with-article-headers
2272     (while (not (eobp))
2273       (save-restriction
2274         (mail-header-narrow-to-field)
2275         (mail-header-fold-field)
2276         (goto-char (point-max))))))
2277
2278 (defun gnus-treat-smiley ()
2279   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2280   (interactive)
2281   (gnus-with-article-buffer
2282     (if (memq 'smiley gnus-article-wash-types)
2283         (gnus-delete-images 'smiley)
2284       (article-goto-body)
2285       (let ((images (smiley-region (point) (point-max))))
2286         (when images
2287           (gnus-add-wash-type 'smiley)
2288           (dolist (image images)
2289             (gnus-add-image 'smiley image)))))))
2290
2291 (defun gnus-article-remove-images ()
2292   "Remove all images from the article buffer."
2293   (interactive)
2294   (gnus-with-article-buffer
2295     (save-restriction
2296       (widen)
2297       (dolist (elem gnus-article-image-alist)
2298         (gnus-delete-images (car elem))))))
2299
2300 (defun gnus-article-show-images ()
2301   "Show any images that are in the HTML-rendered article buffer.
2302 This only works if the article in question is HTML."
2303   (interactive)
2304   (gnus-with-article-buffer
2305     (save-restriction
2306       (widen)
2307       (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2308                                                       'image-displayer))
2309         (destructuring-bind (start end function) region
2310           (funcall function (get-text-property start 'image-url)
2311                    start end))))))
2312
2313 (defun gnus-article-treat-fold-newsgroups ()
2314   "Unfold folded message headers.
2315 Only the headers that fit into the current window width will be
2316 unfolded."
2317   (interactive)
2318   (gnus-with-article-headers
2319     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2320       (save-restriction
2321         (mail-header-narrow-to-field)
2322         (while (re-search-forward ", *" nil t)
2323           (replace-match ", " t t))
2324         (mail-header-fold-field)
2325         (goto-char (point-max))))))
2326
2327 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2328   "Value of `truncate-lines' in Gnus Article buffer.
2329 Valid values are nil, t, `head', `first', `last', an integer or a
2330 predicate.  See Info node `(gnus)Customizing Articles'."
2331   :version "23.1" ;; No Gnus
2332   :group 'gnus-article
2333   ;; :link '(custom-manual "(gnus)Customizing Articles")
2334   :type 'boolean)
2335
2336 (defun gnus-article-toggle-truncate-lines (&optional arg)
2337   "Toggle whether to fold or truncate long lines in article the buffer.
2338 If ARG is non-nil and not a number, toggle
2339 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2340 long lines if and only if arg is positive."
2341   (interactive "P")
2342   (cond
2343    ((and (numberp arg) (> arg 0))
2344     (setq gnus-article-truncate-lines t))
2345    ((numberp arg)
2346     (setq gnus-article-truncate-lines nil))
2347    (arg
2348     (setq gnus-article-truncate-lines
2349           (not gnus-article-truncate-lines))))
2350   (gnus-with-article-buffer
2351     (cond
2352      ((and (numberp arg) (> arg 0))
2353       (setq truncate-lines nil))
2354      ((numberp arg)
2355       (setq truncate-lines t)))
2356     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2357     ;; `toggle-truncate-lines' needs an argument.
2358     (toggle-truncate-lines)))
2359
2360 (defun gnus-article-treat-body-boundary ()
2361   "Place a boundary line at the end of the headers."
2362   (interactive)
2363   (when (and gnus-body-boundary-delimiter
2364              (> (length gnus-body-boundary-delimiter) 0))
2365     (gnus-with-article-headers
2366       (goto-char (point-max))
2367       (let ((start (point)))
2368         (insert "X-Boundary: ")
2369         (gnus-add-text-properties start (point) '(invisible t intangible t))
2370         (insert (let (str)
2371                   (while (>= (window-width) (length str))
2372                     (setq str (concat str gnus-body-boundary-delimiter)))
2373                   (substring str 0 (window-width)))
2374                 "\n")
2375         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2376
2377 (defun article-fill-long-lines ()
2378   "Fill lines that are wider than the window width."
2379   (interactive)
2380   (save-excursion
2381     (let ((inhibit-read-only t)
2382           (width (window-width (get-buffer-window (current-buffer)))))
2383       (save-restriction
2384         (article-goto-body)
2385         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2386           (while (not (eobp))
2387             (end-of-line)
2388             (when (>= (current-column) (min fill-column width))
2389               (narrow-to-region (min (1+ (point)) (point-max))
2390                                 (point-at-bol))
2391               (let ((goback (point-marker)))
2392                 (fill-paragraph nil)
2393                 (goto-char (marker-position goback)))
2394               (widen))
2395             (forward-line 1)))))))
2396
2397 (defun article-capitalize-sentences ()
2398   "Capitalize the first word in each sentence."
2399   (interactive)
2400   (save-excursion
2401     (let ((inhibit-read-only t)
2402           (paragraph-start "^[\n\^L]"))
2403       (article-goto-body)
2404       (while (not (eobp))
2405         (capitalize-word 1)
2406         (forward-sentence)))))
2407
2408 (defun article-remove-cr ()
2409   "Remove trailing CRs and then translate remaining CRs into LFs."
2410   (interactive)
2411   (save-excursion
2412     (let ((inhibit-read-only t))
2413       (goto-char (point-min))
2414       (while (re-search-forward "\r+$" nil t)
2415         (replace-match "" t t))
2416       (goto-char (point-min))
2417       (while (search-forward "\r" nil t)
2418         (replace-match "\n" t t)))))
2419
2420 (defun article-remove-trailing-blank-lines ()
2421   "Remove all trailing blank lines from the article."
2422   (interactive)
2423   (save-excursion
2424     (let ((inhibit-read-only t))
2425       (goto-char (point-max))
2426       (delete-region
2427        (point)
2428        (progn
2429          (while (and (not (bobp))
2430                      (looking-at "^[ \t]*$")
2431                      (not (gnus-annotation-in-region-p
2432                            (point) (point-at-eol))))
2433            (forward-line -1))
2434          (forward-line 1)
2435          (point))))))
2436
2437 (defvar gnus-face-properties-alist)
2438
2439 (defun article-display-face (&optional force)
2440   "Display any Face headers in the header."
2441   (interactive (list 'force))
2442   (let ((wash-face-p buffer-read-only))
2443     (gnus-with-article-headers
2444       ;; When displaying parts, this function can be called several times on
2445       ;; the same article, without any intended toggle semantic (as typing `W
2446       ;; D d' would have). So face deletion must occur only when we come from
2447       ;; an interactive command, that is when the *Article* buffer is
2448       ;; read-only.
2449       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2450           (gnus-delete-images 'face)
2451         (let ((from (message-fetch-field "from"))
2452               face faces)
2453           (save-current-buffer
2454             (when (and wash-face-p
2455                        (gnus-buffer-live-p gnus-original-article-buffer)
2456                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2457               (set-buffer gnus-original-article-buffer))
2458             (save-restriction
2459               (mail-narrow-to-head)
2460               (when (or force
2461                         ;; Check whether this face is censored.
2462                         (not (and gnus-article-x-face-too-ugly
2463                                   (or from
2464                                       (setq from (message-fetch-field "from")))
2465                                   (string-match gnus-article-x-face-too-ugly
2466                                                 from))))
2467                 (while (gnus-article-goto-header "Face")
2468                   (push (mail-header-field-value) faces)))))
2469           (when faces
2470             (goto-char (point-min))
2471             (let (png image)
2472               (unless (setq from (gnus-article-goto-header "from"))
2473                 (insert "From:")
2474                 (setq from (point))
2475                 (insert " [no `from' set]\n"))
2476               (while faces
2477                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2478                   (setq image
2479                         (apply 'gnus-create-image png 'png t
2480                                (cdr (assq 'png gnus-face-properties-alist))))
2481                   (goto-char from)
2482                   (gnus-add-wash-type 'face)
2483                   (gnus-add-image 'face image)
2484                   (gnus-put-image image nil 'face))))))))))
2485
2486 (defun article-display-x-face (&optional force)
2487   "Look for an X-Face header and display it if present."
2488   (interactive (list 'force))
2489   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2490     (gnus-with-article-headers
2491       ;; Delete the old process, if any.
2492       (when (process-status "article-x-face")
2493         (delete-process "article-x-face"))
2494       ;; See the comment in `article-display-face'.
2495       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2496           ;; We have already displayed X-Faces, so we remove them
2497           ;; instead.
2498           (gnus-delete-images 'xface)
2499         ;; Display X-Faces.
2500         (let ((from (message-fetch-field "from"))
2501               x-faces face)
2502           (save-current-buffer
2503             (when (and wash-face-p
2504                        (gnus-buffer-live-p gnus-original-article-buffer)
2505                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2506               ;; If type `W f', use gnus-original-article-buffer,
2507               ;; otherwise use the current buffer because displaying
2508               ;; RFC822 parts calls this function too.
2509               (set-buffer gnus-original-article-buffer))
2510             (save-restriction
2511               (mail-narrow-to-head)
2512               (and gnus-article-x-face-command
2513                    (or force
2514                        ;; Check whether this face is censored.
2515                        (not (and gnus-article-x-face-too-ugly
2516                                  (or from
2517                                      (setq from (message-fetch-field "from")))
2518                                  (string-match gnus-article-x-face-too-ugly
2519                                                from))))
2520                    (while (gnus-article-goto-header "X-Face")
2521                      (push (mail-header-field-value) x-faces)))))
2522           (when x-faces
2523             ;; We display the face.
2524             (cond ((functionp gnus-article-x-face-command)
2525                    ;; The command is a lisp function, so we call it.
2526                    (mapc gnus-article-x-face-command x-faces))
2527                   ((stringp gnus-article-x-face-command)
2528                    ;; The command is a string, so we interpret the command
2529                    ;; as a, well, command, and fork it off.
2530                    (let ((process-connection-type nil))
2531                      (gnus-set-process-query-on-exit-flag
2532                       (start-process
2533                        "article-x-face" nil shell-file-name
2534                        shell-command-switch gnus-article-x-face-command)
2535                       nil)
2536                      ;; Sending multiple EOFs to xv doesn't work,
2537                      ;; so we only do a single external face.
2538                      (with-temp-buffer
2539                        (insert (car x-faces))
2540                        (process-send-region "article-x-face"
2541                                             (point-min) (point-max)))
2542                      (process-send-eof "article-x-face")))
2543                   (t
2544                    (error "`%s' set to `%s' is not a function"
2545                           gnus-article-x-face-command
2546                           'gnus-article-x-face-command)))))))))
2547
2548 (defun article-decode-mime-words ()
2549   "Decode all MIME-encoded words in the article."
2550   (interactive)
2551   (gnus-with-article-buffer
2552     (let ((inhibit-point-motion-hooks t)
2553           (mail-parse-charset gnus-newsgroup-charset)
2554           (mail-parse-ignored-charsets
2555            (with-current-buffer gnus-summary-buffer
2556              gnus-newsgroup-ignored-charsets)))
2557       (mail-decode-encoded-word-region (point-min) (point-max)))))
2558
2559 (defun article-decode-charset (&optional prompt)
2560   "Decode charset-encoded text in the article.
2561 If PROMPT (the prefix), prompt for a coding system to use."
2562   (interactive "P")
2563   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2564         (inhibit-read-only t)
2565         (mail-parse-charset gnus-newsgroup-charset)
2566         (mail-parse-ignored-charsets
2567          (save-excursion (condition-case nil
2568                              (set-buffer gnus-summary-buffer)
2569                            (error))
2570                          gnus-newsgroup-ignored-charsets))
2571         ct cte ctl charset format)
2572     (save-excursion
2573       (save-restriction
2574         (article-narrow-to-head)
2575         (setq ct (message-fetch-field "Content-Type" t)
2576               cte (message-fetch-field "Content-Transfer-Encoding" t)
2577               ctl (and ct (mail-header-parse-content-type ct))
2578               charset (cond
2579                        (prompt
2580                         (mm-read-coding-system "Charset to decode: "))
2581                        (ctl
2582                         (mail-content-type-get ctl 'charset)))
2583               format (and ctl (mail-content-type-get ctl 'format)))
2584         (when cte
2585           (setq cte (mail-header-strip cte)))
2586         (if (and ctl (not (string-match "/" (car ctl))))
2587             (setq ctl nil))
2588         (goto-char (point-max)))
2589       (forward-line 1)
2590       (save-restriction
2591         (narrow-to-region (point) (point-max))
2592         (when (and (eq mail-parse-charset 'gnus-decoded)
2593                    (eq (mm-body-7-or-8) '8bit))
2594           ;; The text code could have been decoded.
2595           (setq charset mail-parse-charset))
2596         (when (and (or (not ctl)
2597                        (equal (car ctl) "text/plain"))
2598                    (not format)) ;; article with format will decode later.
2599           (mm-decode-body
2600            charset (and cte (intern (downcase
2601                                      (gnus-strip-whitespace cte))))
2602            (car ctl)))))))
2603
2604 (defun article-decode-encoded-words ()
2605   "Remove encoded-word encoding from headers."
2606   (let ((inhibit-point-motion-hooks t)
2607         (mail-parse-charset gnus-newsgroup-charset)
2608         (mail-parse-ignored-charsets
2609          (save-excursion (condition-case nil
2610                              (set-buffer gnus-summary-buffer)
2611                            (error))
2612                          gnus-newsgroup-ignored-charsets))
2613         (inhibit-read-only t)
2614         end start)
2615     (goto-char (point-min))
2616     (when (search-forward "\n\n" nil 'move)
2617       (forward-line -1))
2618     (setq end (point))
2619     (while (not (bobp))
2620       (while (progn
2621                (forward-line -1)
2622                (and (not (bobp))
2623                     (memq (char-after) '(?\t ? )))))
2624       (setq start (point))
2625       (if (looking-at "\
2626 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2627 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2628           (funcall gnus-decode-address-function start end)
2629         (funcall gnus-decode-header-function start end))
2630       (goto-char (setq end start)))))
2631
2632 (defun article-decode-group-name ()
2633   "Decode group names in Newsgroups, Followup-To and Xref headers."
2634   (let ((inhibit-point-motion-hooks t)
2635         (inhibit-read-only t)
2636         (method (gnus-find-method-for-group gnus-newsgroup-name))
2637         regexp)
2638     (when (and (or gnus-group-name-charset-method-alist
2639                    gnus-group-name-charset-group-alist)
2640                (gnus-buffer-live-p gnus-original-article-buffer))
2641       (save-restriction
2642         (article-narrow-to-head)
2643         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2644           (with-current-buffer gnus-original-article-buffer
2645             (goto-char (point-min)))
2646           (setq regexp (concat "^" header
2647                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2648           (while (re-search-forward regexp nil t)
2649             (replace-match (save-match-data
2650                              (gnus-decode-newsgroups
2651                               ;; XXX how to use data in article buffer?
2652                               (with-current-buffer gnus-original-article-buffer
2653                                 (re-search-forward regexp nil t)
2654                                 (match-string 1))
2655                               gnus-newsgroup-name method))
2656                            t t nil 1))
2657           (goto-char (point-min)))))))
2658
2659 (autoload 'idna-to-unicode "idna")
2660
2661 (defun article-decode-idna-rhs ()
2662   "Decode IDNA strings in RHS in various headers in current buffer.
2663 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2664 Mail-Reply-To: and Mail-Followup-To:."
2665   (when gnus-use-idna
2666     (save-restriction
2667       (let ((inhibit-point-motion-hooks t)
2668             (inhibit-read-only t))
2669         (article-narrow-to-head)
2670         (goto-char (point-min))
2671         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2672           (let (ace unicode)
2673             (when (save-match-data
2674                     (and (setq ace (match-string 1))
2675                          (save-excursion
2676                            (and (re-search-backward "^[^ \t]" nil t)
2677                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2678                          (setq unicode (idna-to-unicode ace))))
2679               (unless (string= ace unicode)
2680                 (replace-match unicode nil nil nil 1)))))))))
2681
2682 (defun article-de-quoted-unreadable (&optional force read-charset)
2683   "Translate a quoted-printable-encoded article.
2684 If FORCE, decode the article whether it is marked as quoted-printable
2685 or not.
2686 If READ-CHARSET, ask for a coding system."
2687   (interactive (list 'force current-prefix-arg))
2688   (save-excursion
2689     (let ((inhibit-read-only t) type charset)
2690       (if (gnus-buffer-live-p gnus-original-article-buffer)
2691           (with-current-buffer gnus-original-article-buffer
2692             (setq type
2693                   (gnus-fetch-field "content-transfer-encoding"))
2694             (let* ((ct (gnus-fetch-field "content-type"))
2695                    (ctl (and ct (mail-header-parse-content-type ct))))
2696               (setq charset (and ctl
2697                                  (mail-content-type-get ctl 'charset)))
2698               (if (stringp charset)
2699                   (setq charset (intern (downcase charset)))))))
2700       (if read-charset
2701           (setq charset (mm-read-coding-system "Charset: " charset)))
2702       (unless charset
2703         (setq charset gnus-newsgroup-charset))
2704       (when (or force
2705                 (and type (let ((case-fold-search t))
2706                             (string-match "quoted-printable" type))))
2707         (article-goto-body)
2708         (quoted-printable-decode-region
2709          (point) (point-max) (mm-charset-to-coding-system charset))))))
2710
2711 (defun article-de-base64-unreadable (&optional force read-charset)
2712   "Translate a base64 article.
2713 If FORCE, decode the article whether it is marked as base64 not.
2714 If READ-CHARSET, ask for a coding system."
2715   (interactive (list 'force current-prefix-arg))
2716   (save-excursion
2717     (let ((inhibit-read-only t) type charset)
2718       (if (gnus-buffer-live-p gnus-original-article-buffer)
2719           (with-current-buffer gnus-original-article-buffer
2720             (setq type
2721                   (gnus-fetch-field "content-transfer-encoding"))
2722             (let* ((ct (gnus-fetch-field "content-type"))
2723                    (ctl (and ct (mail-header-parse-content-type ct))))
2724               (setq charset (and ctl
2725                                  (mail-content-type-get ctl 'charset)))
2726               (if (stringp charset)
2727                   (setq charset (intern (downcase charset)))))))
2728       (if read-charset
2729           (setq charset (mm-read-coding-system "Charset: " charset)))
2730       (unless charset
2731         (setq charset gnus-newsgroup-charset))
2732       (when (or force
2733                 (and type (let ((case-fold-search t))
2734                             (string-match "base64" type))))
2735         (article-goto-body)
2736         (save-restriction
2737           (narrow-to-region (point) (point-max))
2738           (base64-decode-region (point-min) (point-max))
2739           (mm-decode-coding-region
2740            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2741
2742 (eval-when-compile
2743   (require 'rfc1843))
2744
2745 (defun article-decode-HZ ()
2746   "Translate a HZ-encoded article."
2747   (interactive)
2748   (require 'rfc1843)
2749   (save-excursion
2750     (let ((inhibit-read-only t))
2751       (rfc1843-decode-region (point-min) (point-max)))))
2752
2753 (defun article-unsplit-urls ()
2754   "Remove the newlines that some other mailers insert into URLs."
2755   (interactive)
2756   (save-excursion
2757     (let ((inhibit-read-only t))
2758       (goto-char (point-min))
2759       (while (re-search-forward
2760               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2761         (replace-match "\\1\\3" t)))
2762     (when (interactive-p)
2763       (gnus-treat-article nil))))
2764
2765 (defun article-wash-html ()
2766   "Format an HTML article."
2767   (interactive)
2768   (let ((handles nil)
2769         (buffer-read-only nil))
2770     (when (gnus-buffer-live-p gnus-original-article-buffer)
2771       (setq handles (mm-dissect-buffer t t)))
2772     (article-goto-body)
2773     (delete-region (point) (point-max))
2774     (mm-inline-text-html handles)))
2775
2776 (defvar gnus-article-browse-html-temp-list nil
2777   "List of temporary files created by `gnus-article-browse-html-parts'.
2778 Internal variable.")
2779
2780 (defcustom gnus-article-browse-delete-temp 'ask
2781   "What to do with temporary files from `gnus-article-browse-html-parts'.
2782 If nil, don't delete temporary files.  If it is t, delete them on
2783 exit from the summary buffer.  If it is the symbol `file', query
2784 on each file, if it is `ask' ask once when exiting from the
2785 summary buffer."
2786   :group 'gnus-article
2787   :version "23.1" ;; No Gnus
2788   :type '(choice (const :tag "Don't delete" nil)
2789                  (const :tag "Don't ask" t)
2790                  (const :tag "Ask" ask)
2791                  (const :tag "Ask for each file" file)))
2792
2793 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2794
2795 (defun gnus-article-browse-delete-temp-files (&optional how)
2796   "Delete temp-files created by `gnus-article-browse-html-parts'."
2797   (when (and gnus-article-browse-html-temp-list
2798              (progn
2799                (or how (setq how gnus-article-browse-delete-temp))
2800                (if (eq how 'ask)
2801                    (let ((files (length gnus-article-browse-html-temp-list)))
2802                      (gnus-y-or-n-p (format
2803                                      "Delete all %s temporary HTML file%s? "
2804                                      files
2805                                      (if (> files 1) "s" ""))))
2806                  how)))
2807     (dolist (file gnus-article-browse-html-temp-list)
2808       (cond ((file-directory-p file)
2809              (when (or (not (eq how 'file))
2810                        (gnus-y-or-n-p
2811                         (format
2812                          "Delete temporary HTML file(s) in directory `%s'? "
2813                          (file-name-as-directory file))))
2814                (gnus-delete-directory file)))
2815             ((file-exists-p file)
2816              (when (or (not (eq how 'file))
2817                        (gnus-y-or-n-p
2818                         (format "Delete temporary HTML file `%s'? " file)))
2819                (delete-file file)))))
2820     ;; Also remove file from the list when not deleted or if file doesn't
2821     ;; exist anymore.
2822     (setq gnus-article-browse-html-temp-list nil))
2823   gnus-article-browse-html-temp-list)
2824
2825 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2826   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2827 Return file name."
2828   (save-match-data
2829     (let (file type)
2830       (catch 'found
2831         (dolist (handle handles)
2832           (cond
2833            ((not (listp handle)))
2834            ((equal (mm-handle-media-supertype handle) "multipart")
2835             (when (setq file (gnus-article-browse-html-save-cid-content
2836                               cid handle directory))
2837               (throw 'found file)))
2838            ((equal (concat "<" cid ">") (mm-handle-id handle))
2839             (setq file
2840                   (expand-file-name
2841                    (or (mail-content-type-get
2842                         (mm-handle-disposition handle) 'filename)
2843                        (mail-content-type-get
2844                         (setq type (mm-handle-type handle)) 'name)
2845                        (concat
2846                         (make-temp-name "cid")
2847                         (car (rassoc (car type) mailcap-mime-extensions))))
2848                    directory))
2849             (mm-save-part-to-file handle file)
2850             (throw 'found file))))))))
2851
2852 (defun gnus-article-browse-html-parts (list &optional header)
2853   "View all \"text/html\" parts from LIST.
2854 Recurse into multiparts.  The optional HEADER that should be a decoded
2855 message header will be added to the bodies of the \"text/html\" parts."
2856   ;; Internal function used by `gnus-article-browse-html-article'.
2857   (let (type file charset content cid-dir tmp-file showed)
2858     ;; Find and show the html-parts.
2859     (dolist (handle list)
2860       ;; If HTML, show it:
2861       (cond ((not (listp handle)))
2862             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2863                  (and (equal (car type) "message/external-body")
2864                       (or header
2865                           (setq file (or (mail-content-type-get type 'name)
2866                                          (mail-content-type-get
2867                                           (mm-handle-disposition handle)
2868                                           'filename))))
2869                       (or (mm-handle-cache handle)
2870                           (condition-case code
2871                               (progn (mm-extern-cache-contents handle) t)
2872                             (error
2873                              (gnus-message 3 "%s" (error-message-string code))
2874                              (when (>= gnus-verbose 3) (sit-for 2))
2875                              nil)))
2876                       (progn
2877                         (setq handle (mm-handle-cache handle)
2878                               type (mm-handle-type handle))
2879                         (equal (car type) "text/html"))))
2880              (setq charset (mail-content-type-get type 'charset)
2881                    content (mm-get-part handle))
2882              (with-temp-buffer
2883                (if (eq charset 'gnus-decoded)
2884                    (mm-enable-multibyte)
2885                  (mm-disable-multibyte))
2886                (insert content)
2887                ;; resolve cid contents
2888                (let ((case-fold-search t)
2889                      cid-file)
2890                  (goto-char (point-min))
2891                  (while (re-search-forward "\
2892 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2893                                            nil t)
2894                    (unless cid-dir
2895                      (setq cid-dir (mm-make-temp-file "cid" t))
2896                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2897                    (setq file nil
2898                          content nil)
2899                    (when (setq cid-file
2900                                (gnus-article-browse-html-save-cid-content
2901                                 (match-string 2)
2902                                 (with-current-buffer gnus-article-buffer
2903                                   gnus-article-mime-handles)
2904                                 cid-dir))
2905                      (replace-match (concat "file://" cid-file)
2906                                     nil nil nil 1))))
2907                (unless content (setq content (buffer-string))))
2908              (when (or charset header (not file))
2909                (setq tmp-file (mm-make-temp-file
2910                                ;; Do we need to care for 8.3 filenames?
2911                                "mm-" nil ".html")))
2912              ;; Add a meta html tag to specify charset and a header.
2913              (cond
2914               (header
2915                (let (title eheader body hcharset coding force-charset)
2916                  (with-temp-buffer
2917                    (mm-enable-multibyte)
2918                    (setq case-fold-search t)
2919                    (insert header "\n")
2920                    (setq title (message-fetch-field "subject"))
2921                    (goto-char (point-min))
2922                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2923                      (replace-match (cond ((match-beginning 1) "&lt;")
2924                                           ((match-beginning 2) "&gt;")
2925                                           (t "&amp;"))))
2926                    (goto-char (point-min))
2927                    (insert "<pre>\n")
2928                    (goto-char (point-max))
2929                    (insert "</pre>\n<hr>\n")
2930                    ;; We have to examine charset one by one since
2931                    ;; charset specified in parts might be different.
2932                    (if (eq charset 'gnus-decoded)
2933                        (setq charset 'utf-8
2934                              eheader (mm-encode-coding-string (buffer-string)
2935                                                               charset)
2936                              title (when title
2937                                      (mm-encode-coding-string title charset))
2938                              body (mm-encode-coding-string content charset)
2939                              force-charset t)
2940                      (setq hcharset (mm-find-mime-charset-region (point-min)
2941                                                                  (point-max)))
2942                      (cond ((= (length hcharset) 1)
2943                             (setq hcharset (car hcharset)
2944                                   coding (mm-charset-to-coding-system
2945                                           hcharset)))
2946                            ((> (length hcharset) 1)
2947                             (setq hcharset 'utf-8
2948                                   coding hcharset)))
2949                      (if coding
2950                          (if charset
2951                              (progn
2952                                (setq body
2953                                      (mm-charset-to-coding-system charset))
2954                                (if (eq coding body)
2955                                    (setq eheader (mm-encode-coding-string
2956                                                   (buffer-string) coding)
2957                                          title (when title
2958                                                  (mm-encode-coding-string
2959                                                   title coding))
2960                                          body content)
2961                                  (setq charset 'utf-8
2962                                        eheader (mm-encode-coding-string
2963                                                 (buffer-string) charset)
2964                                        title (when title
2965                                                (mm-encode-coding-string
2966                                                 title charset))
2967                                        body (mm-encode-coding-string
2968                                              (mm-decode-coding-string
2969                                               content body)
2970                                              charset)
2971                                        force-charset t)))
2972                            (setq charset hcharset
2973                                  eheader (mm-encode-coding-string
2974                                           (buffer-string) coding)
2975                                  title (when title
2976                                          (mm-encode-coding-string
2977                                           title coding))
2978                                  body content))
2979                        (setq eheader (mm-string-as-unibyte (buffer-string))
2980                              body content)))
2981                    (erase-buffer)
2982                    (mm-disable-multibyte)
2983                    (insert body)
2984                    (when charset
2985                      (mm-add-meta-html-tag handle charset force-charset))
2986                    (when title
2987                      (goto-char (point-min))
2988                      (unless (search-forward "<title>" nil t)
2989                        (re-search-forward "<head>\\s-*" nil t)
2990                        (insert "<title>" title "</title>\n")))
2991                    (goto-char (point-min))
2992                    (or (re-search-forward
2993                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2994                        (re-search-forward
2995                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2996                    (insert eheader)
2997                    (mm-write-region (point-min) (point-max)
2998                                     tmp-file nil nil nil 'binary t))))
2999               (charset
3000                (mm-with-unibyte-buffer
3001                  (insert (if (eq charset 'gnus-decoded)
3002                              (mm-encode-coding-string content
3003                                                       (setq charset 'utf-8))
3004                            content))
3005                  (if (or (mm-add-meta-html-tag handle charset)
3006                          (not file))
3007                      (mm-write-region (point-min) (point-max)
3008                                       tmp-file nil nil nil 'binary t)
3009                    (setq tmp-file nil))))
3010               (tmp-file
3011                (mm-save-part-to-file handle tmp-file)))
3012              (when tmp-file
3013                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3014              (add-hook 'gnus-summary-prepare-exit-hook
3015                        'gnus-article-browse-delete-temp-files)
3016              (add-hook 'gnus-exit-gnus-hook
3017                        (lambda  ()
3018                          (gnus-article-browse-delete-temp-files t)))
3019              ;; FIXME: Warn if there's an <img> tag?
3020              (browse-url-of-file (or tmp-file (expand-file-name file)))
3021              (setq showed t))
3022             ;; If multipart, recurse
3023             ((equal (mm-handle-media-supertype handle) "multipart")
3024              (when (gnus-article-browse-html-parts handle header)
3025                (setq showed t)))
3026             ((equal (mm-handle-media-type handle) "message/rfc822")
3027              (mm-with-multibyte-buffer
3028                (mm-insert-part handle)
3029                (setq handle (mm-dissect-buffer t t))
3030                (when (and (bufferp (car handle))
3031                           (stringp (car (mm-handle-type handle))))
3032                  (setq handle (list handle)))
3033                (when header
3034                  (article-decode-encoded-words)
3035                  (let ((gnus-visible-headers
3036                         (or (get 'gnus-visible-headers 'standard-value)
3037                             gnus-visible-headers)))
3038                    (article-hide-headers))
3039                  (goto-char (point-min))
3040                  (search-forward "\n\n" nil 'move)
3041                  (skip-chars-backward "\t\n ")
3042                  (setq header (buffer-substring (point-min) (point)))))
3043              (when (prog1
3044                        (gnus-article-browse-html-parts handle header)
3045                      (mm-destroy-parts handle))
3046                (setq showed t)))))
3047     showed))
3048
3049 (defun gnus-article-browse-html-article (&optional arg)
3050   "View \"text/html\" parts of the current article with a WWW browser.
3051 Inline images embedded in a message using the cid scheme, as they are
3052 generally considered to be safe, will be processed properly.
3053 The message header is added to the beginning of every html part unless
3054 the prefix argument ARG is given.
3055
3056 Warning: Spammers use links to images (using the http scheme) in HTML
3057 articles to verify whether you have read the message.  As
3058 `gnus-article-browse-html-article' passes the HTML content to the
3059 browser without eliminating these \"web bugs\" you should only
3060 use it for mails from trusted senders.
3061
3062 If you always want to display HTML parts in the browser, set
3063 `mm-text-html-renderer' to nil.
3064
3065 This command creates temporary files to pass HTML contents including
3066 images if any to the browser, and deletes them when exiting the group
3067 \(if you want)."
3068   ;; Cf. `mm-w3m-safe-url-regexp'
3069   (interactive "P")
3070   (if arg
3071       (gnus-summary-show-article)
3072     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3073                                     gnus-visible-headers))
3074           ;; As we insert a <hr>, there's no need for the body boundary.
3075           (gnus-treat-body-boundary nil))
3076       (gnus-summary-show-article)))
3077   (with-current-buffer gnus-article-buffer
3078     (let ((header (unless arg
3079                     (save-restriction
3080                       (widen)
3081                       (buffer-substring-no-properties
3082                        (goto-char (point-min))
3083                        (if (search-forward "\n\n" nil t)
3084                            (match-beginning 0)
3085                          (goto-char (point-max))
3086                          (skip-chars-backward "\t\n ")
3087                          (point))))))
3088           parts)
3089       (set-buffer gnus-original-article-buffer)
3090       (setq parts (mm-dissect-buffer t t))
3091       ;; If singlepart, enforce a list.
3092       (when (and (bufferp (car parts))
3093                  (stringp (car (mm-handle-type parts))))
3094         (setq parts (list parts)))
3095       ;; Process the list
3096       (unless (gnus-article-browse-html-parts parts header)
3097         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3098       (mm-destroy-parts parts)
3099       (unless arg
3100         (gnus-summary-show-article)))))
3101
3102 (defun article-hide-list-identifiers ()
3103   "Remove list identifies from the Subject header.
3104 The `gnus-list-identifiers' variable specifies what to do."
3105   (interactive)
3106   (let ((inhibit-point-motion-hooks t)
3107         (regexp (if (consp gnus-list-identifiers)
3108                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3109                   gnus-list-identifiers))
3110         (inhibit-read-only t))
3111     (when regexp
3112       (save-excursion
3113         (save-restriction
3114           (article-narrow-to-head)
3115           (goto-char (point-min))
3116           (while (re-search-forward
3117                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3118                   nil t)
3119             (delete-region (match-beginning 2) (match-end 0))
3120             (beginning-of-line))
3121           (when (re-search-forward
3122                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3123             (delete-region (match-beginning 1) (match-end 1))))))))
3124
3125 (defun article-hide-pem (&optional arg)
3126   "Toggle hiding of any PEM headers and signatures in the current article.
3127 If given a negative prefix, always show; if given a positive prefix,
3128 always hide."
3129   (interactive (gnus-article-hidden-arg))
3130   (unless (gnus-article-check-hidden-text 'pem arg)
3131     (save-excursion
3132       (let ((inhibit-read-only t) end)
3133         (goto-char (point-min))
3134         ;; Hide the horrendously ugly "header".
3135         (when (and (search-forward
3136                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3137                     nil t)
3138                    (setq end (1+ (match-beginning 0))))
3139           (gnus-add-wash-type 'pem)
3140           (gnus-article-hide-text-type
3141            end
3142            (if (search-forward "\n\n" nil t)
3143                (match-end 0)
3144              (point-max))
3145            'pem)
3146           ;; Hide the trailer as well
3147           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3148                                 nil t)
3149             (gnus-article-hide-text-type
3150              (match-beginning 0) (match-end 0) 'pem)))))))
3151
3152 (defun article-strip-banner ()
3153   "Strip the banners specified by the `banner' group parameter and by
3154 `gnus-article-address-banner-alist'."
3155   (interactive)
3156   (save-excursion
3157     (save-restriction
3158       (let ((inhibit-point-motion-hooks t))
3159         (when (gnus-parameter-banner gnus-newsgroup-name)
3160           (article-really-strip-banner
3161            (gnus-parameter-banner gnus-newsgroup-name)))
3162         (when gnus-article-address-banner-alist
3163           ;; Note that the From header is decoded here, so it is
3164           ;; required that the *-extract-address-components function
3165           ;; supports non-ASCII text.
3166           (let ((from (save-restriction
3167                         (widen)
3168                         (article-narrow-to-head)
3169                         (mail-fetch-field "from"))))
3170             (when (and from
3171                        (setq from
3172                              (cadr (funcall gnus-extract-address-components
3173                                             from))))
3174               (catch 'found
3175                 (dolist (pair gnus-article-address-banner-alist)
3176                   (when (string-match (car pair) from)
3177                     (throw 'found
3178                            (article-really-strip-banner (cdr pair)))))))))))))
3179
3180 (defun article-really-strip-banner (banner)
3181   "Strip the banner specified by the argument."
3182   (save-excursion
3183     (save-restriction
3184       (let ((inhibit-point-motion-hooks t)
3185             (gnus-signature-limit nil)
3186             (inhibit-read-only t))
3187         (article-goto-body)
3188         (cond
3189          ((eq banner 'signature)
3190           (when (gnus-article-narrow-to-signature)
3191             (widen)
3192             (forward-line -1)
3193             (delete-region (point) (point-max))))
3194          ((symbolp banner)
3195           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3196               (while (re-search-forward banner nil t)
3197                 (delete-region (match-beginning 0) (match-end 0)))))
3198          ((stringp banner)
3199           (while (re-search-forward banner nil t)
3200             (delete-region (match-beginning 0) (match-end 0)))))))))
3201
3202 (defun article-babel ()
3203   "Translate article using an online translation service."
3204   (interactive)
3205   (require 'babel)
3206   (gnus-with-article-buffer
3207     (when (article-goto-body)
3208       (let* ((start (point))
3209              (end (point-max))
3210              (orig (buffer-substring start end))
3211              (trans (babel-as-string orig)))
3212         (save-restriction
3213           (narrow-to-region start end)
3214           (delete-region start end)
3215           (insert trans))))))
3216
3217 (defun article-hide-signature (&optional arg)
3218   "Hide the signature in the current article.
3219 If given a negative prefix, always show; if given a positive prefix,
3220 always hide."
3221   (interactive (gnus-article-hidden-arg))
3222   (unless (gnus-article-check-hidden-text 'signature arg)
3223     (save-excursion
3224       (save-restriction
3225         (let ((inhibit-read-only t))
3226           (when (gnus-article-narrow-to-signature)
3227             (gnus-article-hide-text-type
3228              (point-min) (point-max) 'signature))))))
3229   (gnus-set-mode-line 'article))
3230
3231 (defun article-strip-headers-in-body ()
3232   "Strip offensive headers from bodies."
3233   (interactive)
3234   (save-excursion
3235     (article-goto-body)
3236     (let ((case-fold-search t))
3237       (when (looking-at "x-no-archive:")
3238         (gnus-delete-line)))))
3239
3240 (defun article-strip-leading-blank-lines ()
3241   "Remove all blank lines from the beginning of the article."
3242   (interactive)
3243   (save-excursion
3244     (let ((inhibit-point-motion-hooks t)
3245           (inhibit-read-only t))
3246       (when (article-goto-body)
3247         (while (and (not (eobp))
3248                     (looking-at "[ \t]*$"))
3249           (gnus-delete-line))))))
3250
3251 (defun article-narrow-to-head ()
3252   "Narrow the buffer to the head of the message.
3253 Point is left at the beginning of the narrowed-to region."
3254   (narrow-to-region
3255    (goto-char (point-min))
3256    (if (search-forward "\n\n" nil 1)
3257        (1- (point))
3258      (point-max)))
3259   (goto-char (point-min)))
3260
3261 (defun article-goto-body ()
3262   "Place point at the start of the body."
3263   (goto-char (point-min))
3264   (cond
3265    ;; This variable is only bound when dealing with separate
3266    ;; MIME body parts.
3267    (article-goto-body-goes-to-point-min-p
3268     t)
3269    ((search-forward "\n\n" nil t)
3270     t)
3271    (t
3272     (goto-char (point-max))
3273     nil)))
3274
3275 (defun article-strip-multiple-blank-lines ()
3276   "Replace consecutive blank lines with one empty line."
3277   (interactive)
3278   (save-excursion
3279     (let ((inhibit-point-motion-hooks t)
3280           (inhibit-read-only t))
3281       ;; First make all blank lines empty.
3282       (article-goto-body)
3283       (while (re-search-forward "^[ \t]+$" nil t)
3284         (unless (gnus-annotation-in-region-p
3285                  (match-beginning 0) (match-end 0))
3286           (replace-match "" nil t)))
3287       ;; Then replace multiple empty lines with a single empty line.
3288       (article-goto-body)
3289       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3290         (unless (gnus-annotation-in-region-p
3291                  (match-beginning 0) (match-end 0))
3292           (delete-region (match-beginning 1) (match-end 1)))))))
3293
3294 (defun article-strip-leading-space ()
3295   "Remove all white space from the beginning of the lines in the article."
3296   (interactive)
3297   (save-excursion
3298     (let ((inhibit-point-motion-hooks t)
3299           (inhibit-read-only t))
3300       (article-goto-body)
3301       (while (re-search-forward "^[ \t]+" nil t)
3302         (replace-match "" t t)))))
3303
3304 (defun article-strip-trailing-space ()
3305   "Remove all white space from the end of the lines in the article."
3306   (interactive)
3307   (save-excursion
3308     (let ((inhibit-point-motion-hooks t)
3309           (inhibit-read-only t))
3310       (article-goto-body)
3311       (while (re-search-forward "[ \t]+$" nil t)
3312         (replace-match "" t t)))))
3313
3314 (defun article-strip-blank-lines ()
3315   "Strip leading, trailing and multiple blank lines."
3316   (interactive)
3317   (article-strip-leading-blank-lines)
3318   (article-remove-trailing-blank-lines)
3319   (article-strip-multiple-blank-lines))
3320
3321 (defun article-strip-all-blank-lines ()
3322   "Strip all blank lines."
3323   (interactive)
3324   (save-excursion
3325     (let ((inhibit-point-motion-hooks t)
3326           (inhibit-read-only t))
3327       (article-goto-body)
3328       (while (re-search-forward "^[ \t]*\n" nil t)
3329         (replace-match "" t t)))))
3330
3331 (defun gnus-article-narrow-to-signature ()
3332   "Narrow to the signature; return t if a signature is found, else nil."
3333   (let ((inhibit-point-motion-hooks t))
3334     (when (gnus-article-search-signature)
3335       (forward-line 1)
3336       ;; Check whether we have some limits to what we consider
3337       ;; to be a signature.
3338       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3339                       (list gnus-signature-limit)))
3340             limit limited)
3341         (while (setq limit (pop limits))
3342           (if (or (and (integerp limit)
3343                        (< (- (point-max) (point)) limit))
3344                   (and (floatp limit)
3345                        (< (count-lines (point) (point-max)) limit))
3346                   (and (functionp limit)
3347                        (funcall limit))
3348                   (and (stringp limit)
3349                        (not (re-search-forward limit nil t))))
3350               ()                        ; This limit did not succeed.
3351             (setq limited t
3352                   limits nil)))
3353         (unless limited
3354           (narrow-to-region (point) (point-max))
3355           t)))))
3356
3357 (defun gnus-article-search-signature ()
3358   "Search the current buffer for the signature separator.
3359 Put point at the beginning of the signature separator."
3360   (let ((cur (point)))
3361     (goto-char (point-max))
3362     (if (if (stringp gnus-signature-separator)
3363             (re-search-backward gnus-signature-separator nil t)
3364           (let ((seps gnus-signature-separator))
3365             (while (and seps
3366                         (not (re-search-backward (car seps) nil t)))
3367               (pop seps))
3368             seps))
3369         t
3370       (goto-char cur)
3371       nil)))
3372
3373 (defun gnus-article-hidden-arg ()
3374   "Return the current prefix arg as a number, or 0 if no prefix."
3375   (list (if current-prefix-arg
3376             (prefix-numeric-value current-prefix-arg)
3377           0)))
3378
3379 (defun gnus-article-check-hidden-text (type arg)
3380   "Return nil if hiding is necessary.
3381 Arg can be nil or a number.  nil and positive means hide, negative
3382 means show, 0 means toggle."
3383   (save-excursion
3384     (save-restriction
3385       (let ((hide (gnus-article-hidden-text-p type)))
3386         (cond
3387          ((or (null arg)
3388               (> arg 0))
3389           nil)
3390          ((< arg 0)
3391           (gnus-article-show-hidden-text type)
3392           t)
3393          (t
3394           (if (eq hide 'hidden)
3395               (progn
3396                 (gnus-article-show-hidden-text type)
3397                 t)
3398             nil)))))))
3399
3400 (defun gnus-article-hidden-text-p (type)
3401   "Say whether the current buffer contains hidden text of type TYPE."
3402   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3403     (while (and pos
3404                 (not (get-text-property pos 'invisible))
3405                 (not (get-text-property pos 'dummy-invisible)))
3406       (setq pos
3407             (text-property-any (1+ pos) (point-max) 'article-type type)))
3408     (if pos
3409         'hidden
3410       nil)))
3411
3412 (defun gnus-article-show-hidden-text (type &optional dummy)
3413   "Show all hidden text of type TYPE.
3414 Originally it is hide instead of DUMMY."
3415   (let ((inhibit-read-only t)
3416         (inhibit-point-motion-hooks t))
3417     (gnus-remove-text-properties-when
3418      'article-type type
3419      (point-min) (point-max)
3420      (cons 'article-type (cons type
3421                                gnus-hidden-properties)))
3422     (gnus-delete-wash-type type)))
3423
3424 (defconst article-time-units
3425   `((year . ,(* 365.25 24 60 60))
3426     (week . ,(* 7 24 60 60))
3427     (day . ,(* 24 60 60))
3428     (hour . ,(* 60 60))
3429     (minute . 60)
3430     (second . 1))
3431   "Mapping from time units to seconds.")
3432
3433 (defun gnus-article-forward-header ()
3434   "Move point to the start of the next header.
3435 If the current header is a continuation header, this can be several
3436 lines forward."
3437   (let ((ended nil))
3438     (while (not ended)
3439       (forward-line 1)
3440       (if (looking-at "[ \t]+[^ \t]")
3441           (forward-line 1)
3442         (setq ended t)))))
3443
3444 (defun article-date-ut (&optional type highlight)
3445   "Convert DATE date to universal time in the current article.
3446 If TYPE is `local', convert to local time; if it is `lapsed', output
3447 how much time has lapsed since DATE.  For `lapsed', the value of
3448 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3449 should replace the \"Date:\" one, or should be added below it."
3450   (interactive (list 'ut t))
3451   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3452          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3453                              tdate-regexp)
3454                             ((eq type 'lapsed)
3455                              "^X-Sent:[ \t]")
3456                             (article-lapsed-timer
3457                              "^Date:[ \t]")
3458                             (t
3459                              tdate-regexp)))
3460          (case-fold-search t)
3461          (inhibit-read-only t)
3462          (inhibit-point-motion-hooks t)
3463          pos date bface eface)
3464     (save-excursion
3465       (save-restriction
3466         (widen)
3467         (goto-char (point-min))
3468         (while (or (setq date (get-text-property (setq pos (point))
3469                                                  'original-date))
3470                    (when (setq pos (next-single-property-change
3471                                     (point) 'original-date))
3472                      (setq date (get-text-property pos 'original-date))
3473                      t))
3474           (narrow-to-region
3475            pos (if (setq pos (text-property-any pos (point-max)
3476                                                 'original-date nil))
3477                    (progn
3478                      (goto-char pos)
3479                      (if (or (bolp) (eobp))
3480                          (point)
3481                        (1+ (point))))
3482                  (point-max)))
3483           (goto-char (point-min))
3484           (when (re-search-forward tdate-regexp nil t)
3485             (setq bface (get-text-property (point-at-bol) 'face)
3486                   eface (get-text-property (1- (point-at-eol)) 'face)))
3487           (goto-char (point-min))
3488           (setq pos nil)
3489           ;; Delete any old Date headers.
3490           (while (re-search-forward date-regexp nil t)
3491             (if pos
3492                 (delete-region (point-at-bol) (progn
3493                                                 (gnus-article-forward-header)
3494                                                 (point)))
3495               (delete-region (point-at-bol) (progn
3496                                               (gnus-article-forward-header)
3497                                               (forward-char -1)
3498                                               (point)))
3499               (setq pos (point))))
3500           (when (and (not pos)
3501                      (re-search-forward tdate-regexp nil t))
3502             (forward-line 1))
3503           (gnus-goto-char pos)
3504           (insert (article-make-date-line date (or type 'ut)))
3505           (unless pos
3506             (insert "\n")
3507             (forward-line -1))
3508           ;; Do highlighting.
3509           (beginning-of-line)
3510           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3511             (put-text-property (match-beginning 1) (1+ (match-end 1))
3512                                'face bface)
3513             (put-text-property (match-beginning 2) (match-end 2)
3514                                'face eface))
3515           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3516           (goto-char (point-max))
3517           (widen))))))
3518
3519 (defun article-make-date-line (date type)
3520   "Return a DATE line of TYPE."
3521   (unless (memq type '(local ut original user iso8601 lapsed english
3522                              combined-lapsed))
3523     (error "Unknown conversion type: %s" type))
3524   (condition-case ()
3525       (let ((time (date-to-time date)))
3526         (cond
3527          ;; Convert to the local timezone.
3528          ((eq type 'local)
3529           (concat "Date: " (message-make-date time)))
3530          ;; Convert to Universal Time.
3531          ((eq type 'ut)
3532           (concat "Date: "
3533                   (substring
3534                    (message-make-date
3535                     (let* ((e (parse-time-string date))
3536                            (tm (apply 'encode-time e))
3537                            (ms (car tm))
3538                            (ls (- (cadr tm) (car (current-time-zone time)))))
3539                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3540                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3541                             (t (list ms ls)))))
3542                    0 -5)
3543                   "UT"))
3544          ;; Get the original date from the article.
3545          ((eq type 'original)
3546           (concat "Date: " (if (string-match "\n+$" date)
3547                                (substring date 0 (match-beginning 0))
3548                              date)))
3549          ;; Let the user define the format.
3550          ((eq type 'user)
3551           (let ((format (or (condition-case nil
3552                                 (with-current-buffer gnus-summary-buffer
3553                                   gnus-article-time-format)
3554                               (error nil))
3555                             gnus-article-time-format)))
3556             (if (functionp format)
3557                 (funcall format time)
3558               (concat "Date: " (format-time-string format time)))))
3559          ;; ISO 8601.
3560          ((eq type 'iso8601)
3561           (let ((tz (car (current-time-zone time))))
3562             (concat
3563              "Date: "
3564              (format-time-string "%Y%m%dT%H%M%S" time)
3565              (format "%s%02d%02d"
3566                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3567                      (/ (% (abs tz) 3600) 60)))))
3568          ;; Do an X-Sent lapsed format.
3569          ((eq type 'lapsed)
3570           (concat "X-Sent: " (article-lapsed-string time)))
3571          ;; A combined date/lapsed format.
3572          ((eq type 'combined-lapsed)
3573           (concat (article-make-date-line date 'original)
3574                   " (" (article-lapsed-string time 3) ")"))
3575          ;; Display the date in proper English
3576          ((eq type 'english)
3577           (let ((dtime (decode-time time)))
3578             (concat
3579              "Date: the "
3580              (number-to-string (nth 3 dtime))
3581              (let ((digit (% (nth 3 dtime) 10)))
3582                (cond
3583                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3584                 ((= digit 1) "st")
3585                 ((= digit 2) "nd")
3586                 ((= digit 3) "rd")
3587                 (t "th")))
3588              " of "
3589              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3590              " "
3591              (number-to-string (nth 5 dtime))
3592              " at "
3593              (format "%02d" (nth 2 dtime))
3594              ":"
3595              (format "%02d" (nth 1 dtime)))))))
3596     (foo
3597      (format "Date: %s (from Gnus)" date))))
3598
3599 (defun article-lapsed-string (time &optional max-segments)
3600   ;; If the date is seriously mangled, the timezone functions are
3601   ;; liable to bug out, so we ignore all errors.
3602   (let* ((now (current-time))
3603          (real-time (subtract-time now time))
3604          (real-sec (and real-time
3605                         (+ (* (float (car real-time)) 65536)
3606                            (cadr real-time))))
3607          (sec (and real-time (abs real-sec)))
3608          (segments 0)
3609          num prev)
3610     (unless max-segments
3611       (setq max-segments (length article-time-units)))
3612     (cond
3613      ((null real-time)
3614       "Unknown")
3615      ((zerop sec)
3616       "Now")
3617      (t
3618       (concat
3619        ;; This is a bit convoluted, but basically we go
3620        ;; through the time units for years, weeks, etc,
3621        ;; and divide things to see whether that results
3622        ;; in positive answers.
3623        (mapconcat
3624         (lambda (unit)
3625           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3626                   (>= segments max-segments))
3627               ;; The (remaining) seconds are too few to
3628               ;; be divided into this time unit.
3629               ""
3630             ;; It's big enough, so we output it.
3631             (setq sec (- sec (* num (cdr unit))))
3632             (prog1
3633                 (concat (if prev ", " "") (int-to-string
3634                                            (floor num))
3635                         " " (symbol-name (car unit))
3636                         (if (> num 1) "s" ""))
3637               (setq prev t
3638                     segments (1+ segments)))))
3639         article-time-units "")
3640        ;; If dates are odd, then it might appear like the
3641        ;; article was sent in the future.
3642        (if (> real-sec 0)
3643            " ago"
3644          " in the future"))))))
3645
3646 (defun article-date-local (&optional highlight)
3647   "Convert the current article date to the local timezone."
3648   (interactive (list t))
3649   (article-date-ut 'local highlight))
3650
3651 (defun article-date-english (&optional highlight)
3652   "Convert the current article date to something that is proper English."
3653   (interactive (list t))
3654   (article-date-ut 'english highlight))
3655
3656 (defun article-date-original (&optional highlight)
3657   "Convert the current article date to what it was originally.
3658 This is only useful if you have used some other date conversion
3659 function and want to see what the date was before converting."
3660   (interactive (list t))
3661   (article-date-ut 'original highlight))
3662
3663 (defun article-date-lapsed (&optional highlight)
3664   "Convert the current article date to time lapsed since it was sent."
3665   (interactive (list t))
3666   (article-date-ut 'lapsed highlight))
3667
3668 (defun article-date-combined-lapsed (&optional highlight)
3669   "Convert the current article date to time lapsed since it was sent."
3670   (interactive (list t))
3671   (article-date-ut 'combined-lapsed highlight))
3672
3673 (defun article-update-date-lapsed ()
3674   "Function to be run from a timer to update the lapsed time line."
3675   (save-match-data
3676     (let (deactivate-mark)
3677       (save-window-excursion
3678         (ignore-errors
3679          (walk-windows
3680           (lambda (w)
3681             (set-buffer (window-buffer w))
3682             (when (eq major-mode 'gnus-article-mode)
3683               (let ((mark (point-marker))
3684                     (old-point (point)))
3685                 (goto-char (point-min))
3686                 (when (re-search-forward "^X-Sent:\\|^Date:" nil t)
3687                   ;; If the point is on the Date line, then use that
3688                   ;; absolute position.  Otherwise, use the mark.
3689                   ;; This will ensure that point stays at the "same
3690                   ;; place".
3691                   (when (or (< old-point (match-beginning 0))
3692                             (> old-point (line-end-position)))
3693                     (setq old-point nil))
3694                   (if gnus-treat-date-combined-lapsed
3695                       (article-date-combined-lapsed t)
3696                     (article-date-lapsed t)))
3697                 (goto-char (or old-point (marker-position mark)))
3698                 (move-marker mark nil))))
3699           nil 'visible))))))
3700
3701 (defun gnus-start-date-timer (&optional n)
3702   "Start a timer to update the X-Sent header in the article buffers.
3703 The numerical prefix says how frequently (in seconds) the function
3704 is to run."
3705   (interactive "p")
3706   (unless n
3707     (setq n 1))
3708   (gnus-stop-date-timer)
3709   (setq article-lapsed-timer
3710         (run-at-time 1 n 'article-update-date-lapsed)))
3711
3712 (defun gnus-stop-date-timer ()
3713   "Stop the X-Sent timer."
3714   (interactive)
3715   (when article-lapsed-timer
3716     (nnheader-cancel-timer article-lapsed-timer)
3717     (setq article-lapsed-timer nil)))
3718
3719 (defun article-date-user (&optional highlight)
3720   "Convert the current article date to the user-defined format.
3721 This format is defined by the `gnus-article-time-format' variable."
3722   (interactive (list t))
3723   (article-date-ut 'user highlight))
3724
3725 (defun article-date-iso8601 (&optional highlight)
3726   "Convert the current article date to ISO8601."
3727   (interactive (list t))
3728   (article-date-ut 'iso8601 highlight))
3729
3730 (defmacro gnus-article-save-original-date (&rest forms)
3731   "Save the original date as a text property and evaluate FORMS."
3732   `(let* ((case-fold-search t)
3733           (start (progn
3734                    (goto-char (point-min))
3735                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3736                               (not (bolp)))
3737                      (match-end 0))))
3738           (date (when (and start
3739                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3740                                               nil t))
3741                   (buffer-substring-no-properties start
3742                                                   (match-beginning 0)))))
3743      (goto-char (point-max))
3744      (skip-chars-backward "\n")
3745      (put-text-property (point-min) (point) 'original-date date)
3746      ,@forms
3747      (goto-char (point-max))
3748      (skip-chars-backward "\n")
3749      (put-text-property (point-min) (point) 'original-date date)))
3750
3751 ;; (defun article-show-all ()
3752 ;;   "Show all hidden text in the article buffer."
3753 ;;   (interactive)
3754 ;;   (save-excursion
3755 ;;     (let ((inhibit-read-only t))
3756 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3757
3758 (defun article-remove-leading-whitespace ()
3759   "Remove excessive whitespace from all headers."
3760   (interactive)
3761   (save-excursion
3762     (save-restriction
3763       (let ((inhibit-read-only t))
3764         (article-narrow-to-head)
3765         (goto-char (point-min))
3766         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3767           (delete-region (match-beginning 1) (match-end 1)))))))
3768
3769 (defun article-emphasize (&optional arg)
3770   "Emphasize text according to `gnus-emphasis-alist'."
3771   (interactive (gnus-article-hidden-arg))
3772   (unless (gnus-article-check-hidden-text 'emphasis arg)
3773     (save-excursion
3774       (let ((alist (or
3775                     (condition-case nil
3776                         (with-current-buffer gnus-summary-buffer
3777                           gnus-article-emphasis-alist)
3778                       (error))
3779                     gnus-emphasis-alist))
3780             (inhibit-read-only t)
3781             (props (append '(article-type emphasis)
3782                            gnus-hidden-properties))
3783             regexp elem beg invisible visible face)
3784         (article-goto-body)
3785         (setq beg (point))
3786         (while (setq elem (pop alist))
3787           (goto-char beg)
3788           (setq regexp (car elem)
3789                 invisible (nth 1 elem)
3790                 visible (nth 2 elem)
3791                 face (nth 3 elem))
3792           (while (re-search-forward regexp nil t)
3793             (when (and (match-beginning visible) (match-beginning invisible))
3794               (gnus-article-hide-text
3795                (match-beginning invisible) (match-end invisible) props)
3796               (gnus-article-unhide-text-type
3797                (match-beginning visible) (match-end visible) 'emphasis)
3798               (gnus-put-overlay-excluding-newlines
3799                (match-beginning visible) (match-end visible) 'face face)
3800               (gnus-add-wash-type 'emphasis)
3801               (goto-char (match-end invisible)))))))))
3802
3803 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3804   "Setup newsgroup emphasis alist."
3805   (unless gnus-article-emphasis-alist
3806     (let ((name (and gnus-newsgroup-name
3807                      (gnus-group-real-name gnus-newsgroup-name))))
3808       (make-local-variable 'gnus-article-emphasis-alist)
3809       (setq gnus-article-emphasis-alist
3810             (nconc
3811              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3812                (while (setq elem (pop alist))
3813                  (when (and name (string-match (car elem) name))
3814                    (setq alist nil
3815                          highlight (copy-sequence (cdr elem)))))
3816                highlight)
3817              (copy-sequence highlight-words)
3818              (if gnus-newsgroup-name
3819                  (copy-sequence (gnus-group-find-parameter
3820                                  gnus-newsgroup-name 'highlight-words t)))
3821              gnus-emphasis-alist)))))
3822
3823 (defvar gnus-summary-article-menu)
3824 (defvar gnus-summary-post-menu)
3825
3826 ;;; Saving functions.
3827
3828 (defun gnus-article-save (save-buffer file &optional num)
3829   "Save the currently selected article."
3830   (when (or (get gnus-default-article-saver :headers)
3831             (not gnus-save-all-headers))
3832     ;; Remove headers according to `gnus-saved-headers' or the value
3833     ;; of the `:headers' property that the saver function might have.
3834     (let ((gnus-visible-headers
3835            (or (symbol-value (get gnus-default-article-saver :headers))
3836                gnus-saved-headers gnus-visible-headers))
3837           ;; Ignore group parameter.  See `article-hide-headers'.
3838           (gnus-summary-buffer nil))
3839       (with-current-buffer save-buffer
3840         (article-hide-headers 1 t))))
3841   (save-window-excursion
3842     (if (not gnus-default-article-saver)
3843         (error "No default saver is defined")
3844       ;; !!! Magic!  The saving functions all save
3845       ;; `gnus-save-article-buffer' (or so they think), but we
3846       ;; bind that variable to our save-buffer.
3847       (set-buffer gnus-article-buffer)
3848       (let* ((gnus-save-article-buffer save-buffer)
3849              (filename
3850               (cond
3851                ((not gnus-prompt-before-saving) 'default)
3852                ((eq gnus-prompt-before-saving 'always) nil)
3853                (t file)))
3854              (gnus-number-of-articles-to-be-saved
3855               (when (eq gnus-prompt-before-saving t)
3856                 num)))                  ; Magic
3857         (set-buffer gnus-article-current-summary)
3858         (funcall gnus-default-article-saver filename)))))
3859
3860 (defun gnus-read-save-file-name (prompt &optional filename
3861                                         function group headers variable
3862                                         dir-var)
3863   (let ((default-name
3864           (funcall function group headers (symbol-value variable)))
3865         result)
3866     (setq result
3867           (expand-file-name
3868            (cond
3869             ((eq filename 'default)
3870              default-name)
3871             ((eq filename t)
3872              default-name)
3873             (filename filename)
3874             (t
3875              (when (symbol-value dir-var)
3876                (setq default-name (expand-file-name
3877                                    (file-name-nondirectory default-name)
3878                                    (symbol-value dir-var))))
3879              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3880                     (prompt
3881                      (format prompt
3882                              (if (and gnus-number-of-articles-to-be-saved
3883                                       (> gnus-number-of-articles-to-be-saved 1))
3884                                  (format "these %d articles"
3885                                          gnus-number-of-articles-to-be-saved)
3886                                "this article")))
3887                     (file
3888                      ;; Let the split methods have their say.
3889                      (cond
3890                       ;; No split name was found.
3891                       ((null split-name)
3892                        (read-file-name
3893                         (concat prompt " (default "
3894                                 (file-name-nondirectory default-name) "): ")
3895                         (file-name-directory default-name)
3896                         default-name))
3897                       ;; A single group name is returned.
3898                       ((stringp split-name)
3899                        (setq default-name
3900                              (funcall function split-name headers
3901                                       (symbol-value variable)))
3902                        (read-file-name
3903                         (concat prompt " (default "
3904                                 (file-name-nondirectory default-name) "): ")
3905                         (file-name-directory default-name)
3906                         default-name))
3907                       ;; A single split name was found
3908                       ((= 1 (length split-name))
3909                        (let* ((name (expand-file-name
3910                                      (car split-name)
3911                                      gnus-article-save-directory))
3912                               (dir (cond ((file-directory-p name)
3913                                           (file-name-as-directory name))
3914                                          ((file-exists-p name) name)
3915                                          (t gnus-article-save-directory))))
3916                          (read-file-name
3917                           (concat prompt " (default " name "): ")
3918                           dir name)))
3919                       ;; A list of splits was found.
3920                       (t
3921                        (setq split-name (nreverse split-name))
3922                        (let (result)
3923                          (let ((file-name-history
3924                                 (nconc split-name file-name-history)))
3925                            (setq result
3926                                  (expand-file-name
3927                                   (read-file-name
3928                                    (concat prompt " (`M-p' for defaults): ")
3929                                    gnus-article-save-directory
3930                                    (car split-name))
3931                                   gnus-article-save-directory)))
3932                          (car (push result file-name-history)))))))
3933                ;; Create the directory.
3934                (gnus-make-directory (file-name-directory file))
3935                ;; If we have read a directory, we append the default file name.
3936                (when (file-directory-p file)
3937                  (setq file (expand-file-name (file-name-nondirectory
3938                                                default-name)
3939                                               (file-name-as-directory file))))
3940                ;; Possibly translate some characters.
3941                (nnheader-translate-file-chars file))))))
3942     (gnus-make-directory (file-name-directory result))
3943     (when variable
3944       (set variable result))
3945     (when dir-var
3946       (set dir-var (file-name-directory result)))
3947     result))
3948
3949 (defun gnus-article-archive-name (group)
3950   "Return the first instance of an \"Archive-name\" in the current buffer."
3951   (let ((case-fold-search t))
3952     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3953       (nnheader-concat gnus-article-save-directory
3954                        (match-string 1)))))
3955
3956 (defun gnus-article-nndoc-name (group)
3957   "If GROUP is an nndoc group, return the name of the parent group."
3958   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3959     (gnus-group-get-parameter group 'save-article-group)))
3960
3961 (defun gnus-summary-save-in-rmail (&optional filename)
3962   "Append this article to Rmail file.
3963 Optional argument FILENAME specifies file name.
3964 Directory to save to is default to `gnus-article-save-directory'."
3965   (setq filename (gnus-read-save-file-name
3966                   "Save %s in rmail file" filename
3967                   gnus-rmail-save-name gnus-newsgroup-name
3968                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3969   (with-current-buffer gnus-save-article-buffer
3970     (save-excursion
3971       (save-restriction
3972         (widen)
3973         ;; Note that unlike gnus-summary-save-in-mail, there is no
3974         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3975         ;; not use Babyl.
3976         (gnus-output-to-rmail filename))))
3977   filename)
3978
3979 (defun gnus-summary-save-in-mail (&optional filename)
3980   "Append this article to Unix mail file.
3981 Optional argument FILENAME specifies file name.
3982 Directory to save to is default to `gnus-article-save-directory'."
3983   (setq filename (gnus-read-save-file-name
3984                   "Save %s in Unix mail file" filename
3985                   gnus-mail-save-name gnus-newsgroup-name
3986                   gnus-current-headers 'gnus-newsgroup-last-mail))
3987   (with-current-buffer gnus-save-article-buffer
3988     (save-excursion
3989       (save-restriction
3990         (widen)
3991         (if (and (file-readable-p filename)
3992                  (file-regular-p filename)
3993                  (mail-file-babyl-p filename))
3994             (gnus-output-to-rmail filename)
3995           (gnus-output-to-mail filename)))))
3996   filename)
3997
3998 (put 'gnus-summary-save-in-file :decode t)
3999 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4000 (defun gnus-summary-save-in-file (&optional filename overwrite)
4001   "Append this article to file.
4002 Optional argument FILENAME specifies file name.
4003 Directory to save to is default to `gnus-article-save-directory'."
4004   (setq filename (gnus-read-save-file-name
4005                   "Save %s in file" filename
4006                   gnus-file-save-name gnus-newsgroup-name
4007                   gnus-current-headers 'gnus-newsgroup-last-file))
4008   (with-current-buffer gnus-save-article-buffer
4009     (save-excursion
4010       (save-restriction
4011         (widen)
4012         (when (and overwrite
4013                    (file-exists-p filename))
4014           (delete-file filename))
4015         (gnus-output-to-file filename))))
4016   filename)
4017
4018 (put 'gnus-summary-write-to-file :decode t)
4019 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4020 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4021 (defun gnus-summary-write-to-file (&optional filename)
4022   "Write this article to a file, overwriting it if the file exists.
4023 Optional argument FILENAME specifies file name.
4024 The directory to save in defaults to `gnus-article-save-directory'."
4025   (setq filename (gnus-read-save-file-name
4026                   "Save %s in file" filename
4027                   gnus-file-save-name gnus-newsgroup-name
4028                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4029   (gnus-summary-save-in-file filename t))
4030
4031 (put 'gnus-summary-save-body-in-file :decode t)
4032 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4033   "Append this article body to a file.
4034 Optional argument FILENAME specifies file name.
4035 The directory to save in defaults to `gnus-article-save-directory'."
4036   (setq filename (gnus-read-save-file-name
4037                   "Save %s body in file" filename
4038                   gnus-file-save-name gnus-newsgroup-name
4039                   gnus-current-headers 'gnus-newsgroup-last-file))
4040   (with-current-buffer gnus-save-article-buffer
4041     (save-excursion
4042       (save-restriction
4043         (widen)
4044         (when (article-goto-body)
4045           (narrow-to-region (point) (point-max)))
4046         (when (and overwrite
4047                    (file-exists-p filename))
4048           (delete-file filename))
4049         (gnus-output-to-file filename))))
4050   filename)
4051
4052 (put 'gnus-summary-write-body-to-file :decode t)
4053 (put 'gnus-summary-write-body-to-file
4054      :function 'gnus-summary-save-body-in-file)
4055 (defun gnus-summary-write-body-to-file (&optional filename)
4056   "Write this article body to a file, overwriting it if the file exists.
4057 Optional argument FILENAME specifies file name.
4058 The directory to save in defaults to `gnus-article-save-directory'."
4059   (setq filename (gnus-read-save-file-name
4060                   "Save %s body in file" filename
4061                   gnus-file-save-name gnus-newsgroup-name
4062                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4063   (gnus-summary-save-body-in-file filename t))
4064
4065 (put 'gnus-summary-save-in-pipe :decode t)
4066 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4067 (defun gnus-summary-save-in-pipe (&optional command raw)
4068   "Pipe this article to subprocess COMMAND.
4069 Valid values for COMMAND include:
4070   a string
4071     The executable command name and possibly arguments.
4072   nil
4073     You will be prompted for the command in the minibuffer.
4074   the symbol `default'
4075     It will be replaced with the command which the variable
4076     `gnus-summary-pipe-output-default-command' holds or the command
4077     last used for saving.
4078 Non-nil value for RAW overrides `:decode' and `:headers' properties
4079 and the raw article including all headers will be piped."
4080   (let ((article (gnus-summary-article-number))
4081         (decode (unless raw
4082                   (get 'gnus-summary-save-in-pipe :decode)))
4083         save-buffer default)
4084     (if article
4085         (if (vectorp (gnus-summary-article-header article))
4086             (save-current-buffer
4087               (gnus-summary-select-article decode decode nil article)
4088               (insert-buffer-substring
4089                (prog1
4090                    (if decode
4091                        gnus-article-buffer
4092                      gnus-original-article-buffer)
4093                  (setq save-buffer
4094                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4095               ;; Remove unwanted headers.
4096               (when (and (not raw)
4097                          (or (get 'gnus-summary-save-in-pipe :headers)
4098                              (not gnus-save-all-headers)))
4099                 (let ((gnus-visible-headers
4100                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4101                                               :headers))
4102                            gnus-saved-headers gnus-visible-headers))
4103                       (gnus-summary-buffer nil))
4104                   (article-hide-headers 1 t))))
4105           (error "%d is not a real article" article))
4106       (error "No article to pipe"))
4107     (setq default (or gnus-summary-pipe-output-default-command
4108                       gnus-last-shell-command))
4109     (unless (stringp command)
4110       (setq command
4111             (if (and (eq command 'default) default)
4112                 default
4113               (gnus-read-shell-command "Shell command on this article: "
4114                                        default))))
4115     (when (string-equal command "")
4116       (if default
4117           (setq command default)
4118         (error "A command is required")))
4119     (with-current-buffer save-buffer
4120       (save-restriction
4121         (widen)
4122         (shell-command-on-region (point-min) (point-max) command nil)))
4123     (gnus-kill-buffer save-buffer))
4124   (setq gnus-summary-pipe-output-default-command command))
4125
4126 (defun gnus-summary-pipe-to-muttprint (&optional command)
4127   "Pipe this article to muttprint."
4128   (unless (stringp command)
4129     (setq command (read-string
4130                    "Print using command: " gnus-summary-muttprint-program
4131                    nil gnus-summary-muttprint-program)))
4132   (let ((gnus-summary-pipe-output-default-command
4133          gnus-summary-pipe-output-default-command))
4134     (gnus-summary-save-in-pipe command))
4135   (setq gnus-summary-muttprint-program command))
4136
4137 ;;; Article file names when saving.
4138
4139 (defun gnus-capitalize-newsgroup (newsgroup)
4140   "Capitalize NEWSGROUP name."
4141   (when (not (zerop (length newsgroup)))
4142     (concat (char-to-string (upcase (aref newsgroup 0)))
4143             (substring newsgroup 1))))
4144
4145 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4146   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4147 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4148 Otherwise, it is like ~/News/news/group/num."
4149   (let ((default
4150           (expand-file-name
4151            (concat (if (gnus-use-long-file-name 'not-save)
4152                        (gnus-capitalize-newsgroup newsgroup)
4153                      (gnus-newsgroup-directory-form newsgroup))
4154                    "/" (int-to-string (mail-header-number headers)))
4155            gnus-article-save-directory)))
4156     (if (and last-file
4157              (string-equal (file-name-directory default)
4158                            (file-name-directory last-file))
4159              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4160         default
4161       (or last-file default))))
4162
4163 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4164   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4165 If variable `gnus-use-long-file-name' is non-nil, it is
4166 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4167   (let ((default
4168           (expand-file-name
4169            (concat (if (gnus-use-long-file-name 'not-save)
4170                        newsgroup
4171                      (gnus-newsgroup-directory-form newsgroup))
4172                    "/" (int-to-string (mail-header-number headers)))
4173            gnus-article-save-directory)))
4174     (if (and last-file
4175              (string-equal (file-name-directory default)
4176                            (file-name-directory last-file))
4177              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4178         default
4179       (or last-file default))))
4180
4181 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4182   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4183 If variable `gnus-use-long-file-name' is non-nil, it is
4184 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4185   (or last-file
4186       (expand-file-name
4187        (if (gnus-use-long-file-name 'not-save)
4188            newsgroup
4189          (file-relative-name
4190           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4191           default-directory))
4192        gnus-article-save-directory)))
4193
4194 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4195   "Generate file name from sender."
4196   (let ((from (mail-header-from headers)))
4197     (expand-file-name
4198      (if (and from (string-match "\\([^ <]+\\)@" from))
4199          (match-string 1 from)
4200        "nobody")
4201      gnus-article-save-directory)))
4202
4203 (defun article-verify-x-pgp-sig ()
4204   "Verify X-PGP-Sig."
4205   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4206   (interactive)
4207   (if (gnus-buffer-live-p gnus-original-article-buffer)
4208       (let ((sig (with-current-buffer gnus-original-article-buffer
4209                    (gnus-fetch-field "X-PGP-Sig")))
4210             items info headers)
4211         (when (and sig
4212                    mml2015-use
4213                    (mml2015-clear-verify-function))
4214           (with-temp-buffer
4215             (insert-buffer-substring gnus-original-article-buffer)
4216             (setq items (split-string sig))
4217             (message-narrow-to-head)
4218             (let ((inhibit-point-motion-hooks t)
4219                   (case-fold-search t))
4220               ;; Don't verify multiple headers.
4221               (setq headers (mapconcat (lambda (header)
4222                                          (concat header ": "
4223                                                  (mail-fetch-field header)
4224                                                  "\n"))
4225                                        (split-string (nth 1 items) ",") "")))
4226             (delete-region (point-min) (point-max))
4227             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4228             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4229             (insert headers)
4230             (widen)
4231             (forward-line)
4232             (while (not (eobp))
4233               (if (looking-at "^-")
4234                   (insert "- "))
4235               (forward-line))
4236             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4237             (insert "Version: " (car items) "\n\n")
4238             (insert (mapconcat 'identity (cddr items) "\n"))
4239             (insert "\n-----END PGP SIGNATURE-----\n")
4240             (let ((mm-security-handle (list (format "multipart/signed"))))
4241               (mml2015-clean-buffer)
4242               (let ((coding-system-for-write (or gnus-newsgroup-charset
4243                                                  'iso-8859-1)))
4244                 (funcall (mml2015-clear-verify-function)))
4245               (setq info
4246                     (or (mm-handle-multipart-ctl-parameter
4247                          mm-security-handle 'gnus-details)
4248                         (mm-handle-multipart-ctl-parameter
4249                          mm-security-handle 'gnus-info)))))
4250           (when info
4251             (let ((inhibit-read-only t) bface eface)
4252               (save-restriction
4253                 (message-narrow-to-head)
4254                 (goto-char (point-max))
4255                 (forward-line -1)
4256                 (setq bface (get-text-property (point-at-bol) 'face)
4257                       eface (get-text-property (1- (point-at-eol)) 'face))
4258                 (message-remove-header "X-Gnus-PGP-Verify")
4259                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4260                     (forward-line)
4261                   (goto-char (point-max)))
4262                 (narrow-to-region (point) (point))
4263                 (insert "X-Gnus-PGP-Verify: " info "\n")
4264                 (goto-char (point-min))
4265                 (forward-line)
4266                 (while (not (eobp))
4267                   (if (not (looking-at "^[ \t]"))
4268                       (insert " "))
4269                   (forward-line))
4270                 ;; Do highlighting.
4271                 (goto-char (point-min))
4272                 (when (looking-at "\\([^:]+\\): *")
4273                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4274                                      'face bface)
4275                   (put-text-property (match-end 0) (point-max)
4276                                      'face eface)))))))))
4277
4278 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4279
4280 (defun article-verify-cancel-lock ()
4281   "Verify Cancel-Lock header."
4282   (interactive)
4283   (if (gnus-buffer-live-p gnus-original-article-buffer)
4284       (canlock-verify gnus-original-article-buffer)))
4285
4286 (eval-and-compile
4287   (mapc
4288    (lambda (func)
4289      (let (afunc gfunc)
4290        (if (consp func)
4291            (setq afunc (car func)
4292                  gfunc (cdr func))
4293          (setq afunc func
4294                gfunc (intern (format "gnus-%s" func))))
4295        (defalias gfunc
4296          (when (fboundp afunc)
4297            `(lambda (&optional interactive &rest args)
4298               ,(documentation afunc t)
4299               (interactive (list t))
4300               (with-current-buffer gnus-article-buffer
4301                 (if interactive
4302                     (call-interactively ',afunc)
4303                   (apply ',afunc args))))))))
4304    '(article-hide-headers
4305      article-verify-x-pgp-sig
4306      article-verify-cancel-lock
4307      article-hide-boring-headers
4308      article-treat-overstrike
4309      article-treat-ansi-sequences
4310      article-fill-long-lines
4311      article-capitalize-sentences
4312      article-remove-cr
4313      article-remove-leading-whitespace
4314      article-display-x-face
4315      article-display-face
4316      article-de-quoted-unreadable
4317      article-de-base64-unreadable
4318      article-decode-HZ
4319      article-wash-html
4320      article-unsplit-urls
4321      article-hide-list-identifiers
4322      article-strip-banner
4323      article-babel
4324      article-hide-pem
4325      article-hide-signature
4326      article-strip-headers-in-body
4327      article-remove-trailing-blank-lines
4328      article-strip-leading-blank-lines
4329      article-strip-multiple-blank-lines
4330      article-strip-leading-space
4331      article-strip-trailing-space
4332      article-strip-blank-lines
4333      article-strip-all-blank-lines
4334      article-date-local
4335      article-date-english
4336      article-date-iso8601
4337      article-date-original
4338      article-date-ut
4339      article-decode-mime-words
4340      article-decode-charset
4341      article-decode-encoded-words
4342      article-date-user
4343      article-date-lapsed
4344      article-date-combined-lapsed
4345      article-emphasize
4346      article-treat-dumbquotes
4347      article-treat-non-ascii
4348      article-normalize-headers
4349      ;;(article-show-all . gnus-article-show-all-headers)
4350      )))
4351 \f
4352 ;;;
4353 ;;; Gnus article mode
4354 ;;;
4355
4356 (put 'gnus-article-mode 'mode-class 'special)
4357
4358 (set-keymap-parent gnus-article-mode-map widget-keymap)
4359
4360 (gnus-define-keys gnus-article-mode-map
4361   " " gnus-article-goto-next-page
4362   "\177" gnus-article-goto-prev-page
4363   [delete] gnus-article-goto-prev-page
4364   [backspace] gnus-article-goto-prev-page
4365   "\C-c^" gnus-article-refer-article
4366   "h" gnus-article-show-summary
4367   "s" gnus-article-show-summary
4368   "\C-c\C-m" gnus-article-mail
4369   "?" gnus-article-describe-briefly
4370   "<" beginning-of-buffer
4371   ">" end-of-buffer
4372   "\C-c\C-i" gnus-info-find-node
4373   "\C-c\C-b" gnus-bug
4374   "R" gnus-article-reply-with-original
4375   "F" gnus-article-followup-with-original
4376   "\C-hk" gnus-article-describe-key
4377   "\C-hc" gnus-article-describe-key-briefly
4378   "\C-hb" gnus-article-describe-bindings
4379
4380   "e" gnus-article-read-summary-keys
4381   "\C-d" gnus-article-read-summary-keys
4382   "\M-*" gnus-article-read-summary-keys
4383   "\M-#" gnus-article-read-summary-keys
4384   "\M-^" gnus-article-read-summary-keys
4385   "\M-g" gnus-article-read-summary-keys)
4386
4387 (substitute-key-definition
4388  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4389
4390 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4391   "W" gnus-article-wide-reply-with-original)
4392 (if (featurep 'xemacs)
4393     (set-keymap-default-binding gnus-article-send-map
4394                                 'gnus-article-read-summary-send-keys)
4395   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4396
4397 (defun gnus-article-make-menu-bar ()
4398   (unless (boundp 'gnus-article-commands-menu)
4399     (gnus-summary-make-menu-bar))
4400   (unless (boundp 'gnus-article-article-menu)
4401     (easy-menu-define
4402      gnus-article-article-menu gnus-article-mode-map ""
4403      '("Article"
4404        ["Scroll forwards" gnus-article-goto-next-page t]
4405        ["Scroll backwards" gnus-article-goto-prev-page t]
4406        ["Show summary" gnus-article-show-summary t]
4407        ["Fetch Message-ID at point" gnus-article-refer-article t]
4408        ["Mail to address at point" gnus-article-mail t]
4409        ["Send a bug report" gnus-bug t]))
4410
4411     (easy-menu-define
4412      gnus-article-treatment-menu gnus-article-mode-map ""
4413      ;; Fixme: this should use :active (and maybe :visible).
4414      '("Treatment"
4415        ["Hide headers" gnus-article-hide-headers t]
4416        ["Hide signature" gnus-article-hide-signature t]
4417        ["Hide citation" gnus-article-hide-citation t]
4418        ["Treat overstrike" gnus-article-treat-overstrike t]
4419        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4420        ["Remove carriage return" gnus-article-remove-cr t]
4421        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4422        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4423        ["Remove base64" gnus-article-de-base64-unreadable t]
4424        ["Treat html" gnus-article-wash-html t]
4425        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4426        ["Decode HZ" gnus-article-decode-HZ t]))
4427
4428     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4429
4430     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4431
4432     (gnus-run-hooks 'gnus-article-menu-hook)))
4433
4434 (defvar bookmark-make-record-function)
4435
4436 (defun gnus-article-mode ()
4437   "Major mode for displaying an article.
4438
4439 All normal editing commands are switched off.
4440
4441 The following commands are available in addition to all summary mode
4442 commands:
4443 \\<gnus-article-mode-map>
4444 \\[gnus-article-next-page]\t Scroll the article one page forwards
4445 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4446 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4447 \\[gnus-article-show-summary]\t Display the summary buffer
4448 \\[gnus-article-mail]\t Send a reply to the address near point
4449 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4450 \\[gnus-info-find-node]\t Go to the Gnus info node"
4451   (interactive)
4452   (kill-all-local-variables)
4453   (gnus-simplify-mode-line)
4454   (setq mode-name "Article")
4455   (setq major-mode 'gnus-article-mode)
4456   (make-local-variable 'minor-mode-alist)
4457   (use-local-map gnus-article-mode-map)
4458   (when (gnus-visual-p 'article-menu 'menu)
4459     (gnus-article-make-menu-bar)
4460     (when gnus-summary-tool-bar-map
4461       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4462   (gnus-update-format-specifications nil 'article-mode)
4463   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4464   (set (make-local-variable 'gnus-page-broken) nil)
4465   (make-local-variable 'gnus-article-current-summary)
4466   (make-local-variable 'gnus-article-mime-handles)
4467   (make-local-variable 'gnus-article-decoded-p)
4468   (make-local-variable 'gnus-article-mime-handle-alist)
4469   (make-local-variable 'gnus-article-wash-types)
4470   (make-local-variable 'gnus-article-image-alist)
4471   (make-local-variable 'gnus-article-charset)
4472   (make-local-variable 'gnus-article-ignored-charsets)
4473   (set (make-local-variable 'bookmark-make-record-function)
4474        'gnus-summary-bookmark-make-record)
4475   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4476   ;; face.
4477   (set (make-local-variable 'nobreak-char-display) nil)
4478   (setq cursor-in-non-selected-windows nil)
4479   (gnus-set-default-directory)
4480   (buffer-disable-undo)
4481   (setq buffer-read-only t
4482         show-trailing-whitespace nil)
4483   (set-syntax-table gnus-article-mode-syntax-table)
4484   (mm-enable-multibyte)
4485   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4486
4487 (defun gnus-article-setup-buffer ()
4488   "Initialize the article buffer."
4489   (let* ((name (if gnus-single-article-buffer "*Article*"
4490                  (concat "*Article " gnus-newsgroup-name "*")))
4491          (original
4492           (progn (string-match "\\*Article" name)
4493                  (concat " *Original Article"
4494                          (substring name (match-end 0))))))
4495     (setq gnus-article-buffer name)
4496     (setq gnus-original-article-buffer original)
4497     (setq gnus-article-mime-handle-alist nil)
4498     (with-current-buffer gnus-summary-buffer
4499       ;; This might be a variable local to the summary buffer.
4500       (unless gnus-single-article-buffer
4501         (setq gnus-article-buffer name)
4502         (setq gnus-original-article-buffer original)
4503         (gnus-set-global-variables)))
4504     (gnus-article-setup-highlight-words)
4505     ;; Init original article buffer.
4506     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4507       (mm-enable-multibyte)
4508       (setq major-mode 'gnus-original-article-mode)
4509       (make-local-variable 'gnus-original-article))
4510     (if (and (get-buffer name)
4511              (with-current-buffer name
4512                (if gnus-article-edit-mode
4513                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4514                        (progn
4515                          (set-buffer-modified-p nil)
4516                          (gnus-kill-buffer name)
4517                          (message "")
4518                          nil)
4519                      (error "Action aborted"))
4520                  t)))
4521         (with-current-buffer name
4522           (set (make-local-variable 'gnus-article-edit-mode) nil)
4523           (when gnus-article-mime-handles
4524             (mm-destroy-parts gnus-article-mime-handles)
4525             (setq gnus-article-mime-handles nil))
4526           ;; Set it to nil in article-buffer!
4527           (setq gnus-article-mime-handle-alist nil)
4528           (buffer-disable-undo)
4529           (setq buffer-read-only t)
4530           (unless (eq major-mode 'gnus-article-mode)
4531             (gnus-article-mode))
4532           (setq truncate-lines gnus-article-truncate-lines)
4533           (current-buffer))
4534       (with-current-buffer (gnus-get-buffer-create name)
4535         (gnus-article-mode)
4536         (setq truncate-lines gnus-article-truncate-lines)
4537         (make-local-variable 'gnus-summary-buffer)
4538         (setq gnus-summary-buffer
4539               (gnus-summary-buffer-name gnus-newsgroup-name))
4540         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4541         (when (and gnus-article-update-lapsed-header
4542                    (not article-lapsed-timer))
4543           (gnus-start-date-timer gnus-article-update-lapsed-header))
4544         (current-buffer)))))
4545
4546 ;; Set article window start at LINE, where LINE is the number of lines
4547 ;; from the head of the article.
4548 (defun gnus-article-set-window-start (&optional line)
4549   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4550     (when article-window
4551       (set-window-start
4552        article-window
4553        (with-current-buffer gnus-article-buffer
4554          (goto-char (point-min))
4555          (if (not line)
4556              (point-min)
4557            (gnus-message 6 "Moved to bookmark")
4558            (search-forward "\n\n" nil t)
4559            (forward-line line)
4560            (point)))))))
4561
4562 (defun gnus-article-prepare (article &optional all-headers header)
4563   "Prepare ARTICLE in article mode buffer.
4564 ARTICLE should either be an article number or a Message-ID.
4565 If ARTICLE is an id, HEADER should be the article headers.
4566 If ALL-HEADERS is non-nil, no headers are hidden."
4567   (save-excursion
4568     ;; Make sure we start in a summary buffer.
4569     (unless (eq major-mode 'gnus-summary-mode)
4570       (set-buffer gnus-summary-buffer))
4571     (setq gnus-summary-buffer (current-buffer))
4572     (let* ((gnus-article (if header (mail-header-number header) article))
4573            (summary-buffer (current-buffer))
4574            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4575            (group gnus-newsgroup-name)
4576            result)
4577       (save-excursion
4578         (gnus-article-setup-buffer)
4579         (set-buffer gnus-article-buffer)
4580         ;; Deactivate active regions.
4581         (when (and (boundp 'transient-mark-mode)
4582                    transient-mark-mode)
4583           (setq mark-active nil))
4584         (if (not (setq result (let ((inhibit-read-only t))
4585                                 (gnus-request-article-this-buffer
4586                                  article group))))
4587             ;; There is no such article.
4588             (save-excursion
4589               (when (and (numberp article)
4590                          (not (memq article gnus-newsgroup-sparse)))
4591                 (setq gnus-article-current
4592                       (cons gnus-newsgroup-name article))
4593                 (set-buffer gnus-summary-buffer)
4594                 (setq gnus-current-article article)
4595                 (if (and (memq article gnus-newsgroup-undownloaded)
4596                          (not (gnus-online (gnus-find-method-for-group
4597                                             gnus-newsgroup-name))))
4598                     (progn
4599                       (gnus-summary-set-agent-mark article)
4600                       (message "Message marked for downloading"))
4601                   (gnus-summary-mark-article article gnus-canceled-mark)
4602                   (unless (memq article gnus-newsgroup-sparse)
4603                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4604           (if (or (eq result 'pseudo)
4605                   (eq result 'nneething))
4606               (progn
4607                 (with-current-buffer summary-buffer
4608                   (push article gnus-newsgroup-history)
4609                   (setq gnus-last-article gnus-current-article
4610                         gnus-current-article 0
4611                         gnus-current-headers nil
4612                         gnus-article-current nil)
4613                   (if (eq result 'nneething)
4614                       (gnus-configure-windows 'summary)
4615                     (gnus-configure-windows 'article))
4616                   (gnus-set-global-variables))
4617                 (let ((gnus-article-mime-handle-alist-1
4618                        gnus-article-mime-handle-alist))
4619                   (gnus-set-mode-line 'article)))
4620             ;; The result from the `request' was an actual article -
4621             ;; or at least some text that is now displayed in the
4622             ;; article buffer.
4623             (when (and (numberp article)
4624                        (not (eq article gnus-current-article)))
4625               ;; Seems like a new article has been selected.
4626               ;; `gnus-current-article' must be an article number.
4627               (with-current-buffer summary-buffer
4628                 (push article gnus-newsgroup-history)
4629                 (setq gnus-last-article gnus-current-article
4630                       gnus-current-article article
4631                       gnus-current-headers
4632                       (gnus-summary-article-header gnus-current-article)
4633                       gnus-article-current
4634                       (cons gnus-newsgroup-name gnus-current-article))
4635                 (unless (vectorp gnus-current-headers)
4636                   (setq gnus-current-headers nil))
4637                 (gnus-summary-goto-subject gnus-current-article)
4638                 (when (gnus-summary-show-thread)
4639                   ;; If the summary buffer really was folded, the
4640                   ;; previous goto may not actually have gone to
4641                   ;; the right article, but the thread root instead.
4642                   ;; So we go again.
4643                   (gnus-summary-goto-subject gnus-current-article))
4644                 (gnus-run-hooks 'gnus-mark-article-hook)
4645                 (gnus-set-mode-line 'summary)
4646                 (when (gnus-visual-p 'article-highlight 'highlight)
4647                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4648                 ;; Set the global newsgroup variables here.
4649                 (gnus-set-global-variables)
4650                 (setq gnus-have-all-headers
4651                       (or all-headers gnus-show-all-headers))))
4652             (save-excursion
4653               (gnus-configure-windows 'article))
4654             (when (or (numberp article)
4655                       (stringp article))
4656               (gnus-article-prepare-display)
4657               ;; Do page break.
4658               (goto-char (point-min))
4659               (when gnus-break-pages
4660                 (gnus-narrow-to-page)))
4661             (let ((gnus-article-mime-handle-alist-1
4662                    gnus-article-mime-handle-alist))
4663               (gnus-set-mode-line 'article))
4664             (article-goto-body)
4665             (unless (bobp)
4666               (forward-line -1))
4667             (set-window-point (get-buffer-window (current-buffer)) (point))
4668             (gnus-configure-windows 'article)
4669             t))))))
4670
4671 ;;;###autoload
4672 (defun gnus-article-prepare-display ()
4673   "Make the current buffer look like a nice article."
4674   ;; Hooks for getting information from the article.
4675   ;; This hook must be called before being narrowed.
4676   (let ((gnus-article-buffer (current-buffer))
4677         buffer-read-only
4678         (inhibit-read-only t))
4679     (unless (eq major-mode 'gnus-article-mode)
4680       (gnus-article-mode))
4681     (setq buffer-read-only nil
4682           gnus-article-wash-types nil
4683           gnus-article-image-alist nil)
4684     (gnus-run-hooks 'gnus-tmp-internal-hook)
4685     (when gnus-display-mime-function
4686       (funcall gnus-display-mime-function))
4687     (gnus-run-hooks 'gnus-article-prepare-hook)))
4688
4689 ;;;
4690 ;;; Gnus Sticky Article Mode
4691 ;;;
4692
4693 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4694   "Mode for sticky articles."
4695   ;; Release bindings that won't work.
4696   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4697                              gnus-sticky-article-mode-map)
4698   (substitute-key-definition 'gnus-article-refer-article 'undefined
4699                              gnus-sticky-article-mode-map)
4700   (dolist (k '("e" "h" "s" "F" "R"))
4701     (define-key gnus-sticky-article-mode-map k nil))
4702   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4703   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4704   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4705   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4706
4707 (defun gnus-sticky-article (arg)
4708   "Make the current article sticky.
4709 If a prefix ARG is given, ask for a name for this sticky article buffer."
4710   (interactive "P")
4711   (gnus-summary-show-thread)
4712   (gnus-summary-select-article nil nil 'pseudo)
4713   (let (new-art-buf-name)
4714     (gnus-eval-in-buffer-window gnus-article-buffer
4715       (setq new-art-buf-name
4716             (concat
4717              "*Sticky Article: "
4718              (if arg
4719                  (read-from-minibuffer "Sticky article buffer name: ")
4720                (gnus-with-article-headers
4721                  (gnus-article-goto-header "subject")
4722                  (setq new-art-buf-name
4723                        (buffer-substring-no-properties
4724                         (line-beginning-position) (line-end-position)))
4725                  (goto-char (point-min))
4726                  (gnus-article-goto-header "from")
4727                  (setq new-art-buf-name
4728                        (concat
4729                         new-art-buf-name ", "
4730                         (buffer-substring-no-properties
4731                          (line-beginning-position) (line-end-position))))
4732                  (goto-char (point-min))
4733                  (gnus-article-goto-header "date")
4734                  (setq new-art-buf-name
4735                        (concat
4736                         new-art-buf-name ", "
4737                         (buffer-substring-no-properties
4738                          (line-beginning-position) (line-end-position))))))
4739              "*"))
4740       (if (and (gnus-buffer-live-p new-art-buf-name)
4741                (with-current-buffer new-art-buf-name
4742                  (eq major-mode 'gnus-sticky-article-mode)))
4743           (switch-to-buffer new-art-buf-name)
4744         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4745       (gnus-sticky-article-mode))
4746     (setq gnus-article-buffer new-art-buf-name))
4747   (gnus-summary-recenter)
4748   (gnus-summary-position-point))
4749
4750 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4751   "Kill the given sticky article BUFFER.
4752 If none is given, assume the current buffer and kill it if it has
4753 `gnus-sticky-article-mode'."
4754   (interactive)
4755   (unless buffer
4756     (setq buffer (current-buffer)))
4757   (with-current-buffer buffer
4758     (when (eq major-mode 'gnus-sticky-article-mode)
4759       (gnus-kill-buffer buffer))))
4760
4761 (defun gnus-kill-sticky-article-buffers (arg)
4762   "Kill all sticky article buffers.
4763 If a prefix ARG is given, ask for confirmation."
4764   (interactive "P")
4765   (dolist (buf (gnus-buffers))
4766     (with-current-buffer buf
4767       (when (eq major-mode 'gnus-sticky-article-mode)
4768         (if (not arg)
4769             (gnus-kill-buffer buf)
4770           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4771             (gnus-kill-buffer buf)))))))
4772
4773 ;;;
4774 ;;; Gnus MIME viewing functions
4775 ;;;
4776
4777 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4778   "Format of the MIME buttons.
4779
4780 Valid specifiers include:
4781 %t  The MIME type
4782 %T  MIME type, along with additional info
4783 %n  The `name' parameter
4784 %d  The description, if any
4785 %l  The length of the encoded part
4786 %p  The part identifier number
4787 %e  Dots if the part isn't displayed
4788
4789 General format specifiers can also be used.  See Info node
4790 `(gnus)Formatting Variables'.")
4791
4792 (defvar gnus-mime-button-line-format-alist
4793   '((?t gnus-tmp-type ?s)
4794     (?T gnus-tmp-type-long ?s)
4795     (?n gnus-tmp-name ?s)
4796     (?d gnus-tmp-description ?s)
4797     (?p gnus-tmp-id ?s)
4798     (?l gnus-tmp-length ?d)
4799     (?e gnus-tmp-dots ?s)))
4800
4801 (defvar gnus-mime-button-commands
4802   '((gnus-article-press-button "\r" "Toggle Display")
4803     (gnus-mime-view-part "v" "View Interactively...")
4804     (gnus-mime-view-part-as-type "t" "View As Type...")
4805     (gnus-mime-view-part-as-charset "C" "View As charset...")
4806     (gnus-mime-save-part "o" "Save...")
4807     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4808     (gnus-mime-replace-part "r" "Replace part")
4809     (gnus-mime-delete-part "d" "Delete part")
4810     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4811     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4812     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4813     (gnus-mime-view-part-externally "e" "View Externally")
4814     (gnus-mime-print-part "p" "Print")
4815     (gnus-mime-pipe-part "|" "Pipe To Command...")
4816     (gnus-mime-action-on-part "." "Take action on the part...")))
4817
4818 (defun gnus-article-mime-part-status ()
4819   (if gnus-article-mime-handle-alist-1
4820       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4821           " (1 part)"
4822         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4823     ""))
4824
4825 (defvar gnus-mime-button-map
4826   (let ((map (make-sparse-keymap)))
4827     (define-key map gnus-mouse-2 'gnus-article-push-button)
4828     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4829     (dolist (c gnus-mime-button-commands)
4830       (define-key map (cadr c) (car c)))
4831     map))
4832
4833 (easy-menu-define
4834   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4835   `("MIME Part"
4836     ,@(mapcar (lambda (c)
4837                 (vector (caddr c) (car c) :active t))
4838               gnus-mime-button-commands)))
4839
4840 (defvar gnus-url-button-commands
4841   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4842
4843 (defvar gnus-url-button-map
4844   (let ((map (make-sparse-keymap)))
4845     (dolist (c gnus-url-button-commands)
4846       (define-key map (cadr c) (car c)))
4847     map))
4848
4849 (easy-menu-define
4850   gnus-url-button-menu gnus-url-button-map "URL button menu."
4851   `("Url Button"
4852     ,@(mapcar (lambda (c)
4853                 (vector (caddr c) (car c) :active t))
4854               gnus-url-button-commands)))
4855
4856 (defmacro gnus-bind-safe-url-regexp (&rest body)
4857   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4858   `(let ((mm-w3m-safe-url-regexp
4859           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4860                                 (gnus-buffer-live-p
4861                                  gnus-article-current-summary))
4862                            (with-current-buffer gnus-article-current-summary
4863                              gnus-newsgroup-name)
4864                          gnus-newsgroup-name)))
4865             (if (cond ((not group)
4866                        ;; Maybe we're in a mml-preview buffer
4867                        ;; and no group is selected.
4868                        t)
4869                       ((stringp gnus-safe-html-newsgroups)
4870                        (string-match gnus-safe-html-newsgroups group))
4871                       ((consp gnus-safe-html-newsgroups)
4872                        (member group gnus-safe-html-newsgroups)))
4873                 nil
4874               mm-w3m-safe-url-regexp))))
4875      ,@body))
4876
4877 (defun gnus-mime-button-menu (event prefix)
4878  "Construct a context-sensitive menu of MIME commands."
4879  (interactive "e\nP")
4880  (save-window-excursion
4881    (let ((pos (event-start event)))
4882      (select-window (posn-window pos))
4883      (goto-char (posn-point pos))
4884      (gnus-article-check-buffer)
4885      (popup-menu gnus-mime-button-menu nil prefix))))
4886
4887 (defun gnus-mime-view-all-parts (&optional handles)
4888   "View all the MIME parts."
4889   (interactive)
4890   (with-current-buffer gnus-article-buffer
4891     (let ((handles (or handles gnus-article-mime-handles))
4892           (mail-parse-charset gnus-newsgroup-charset)
4893           (mail-parse-ignored-charsets
4894            (with-current-buffer gnus-summary-buffer
4895              gnus-newsgroup-ignored-charsets)))
4896       (when handles
4897         (mm-remove-parts handles)
4898         (goto-char (point-min))
4899         (or (search-forward "\n\n") (goto-char (point-max)))
4900         (let ((inhibit-read-only t))
4901           (delete-region (point) (point-max))
4902           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4903
4904 (defun gnus-article-jump-to-part (n)
4905   "Jump to MIME part N."
4906   (interactive "P")
4907   (let ((parts (with-current-buffer gnus-article-buffer
4908                  (length gnus-article-mime-handle-alist))))
4909     (when (zerop parts)
4910       (error "No such part"))
4911     (pop-to-buffer gnus-article-buffer)
4912     ;; FIXME: why is it necessary?
4913     (sit-for 0)
4914     (or n
4915         (setq n (if (= parts 1)
4916                     1
4917                   (read-number (format "Jump to part (1..%s): " parts)))))
4918     (unless (and (integerp n) (<= n parts) (>= n 1))
4919       (setq n
4920             (progn
4921               (gnus-message 7 "Invalid part `%s', using %s instead."
4922                             n parts)
4923               parts)))
4924     (gnus-message 9 "Jumping to part %s." n)
4925     (cond ((>= gnus-auto-select-part 1)
4926            (while (and (<= n parts)
4927                        (not (gnus-article-goto-part n)))
4928              (setq n (1+ n))))
4929           ((< gnus-auto-select-part 0)
4930            (while (and (>= n 1)
4931                        (not (gnus-article-goto-part n)))
4932              (setq n (1- n))))
4933           (t
4934            (gnus-article-goto-part n)))))
4935
4936 (defvar gnus-mime-buttonized-part-id nil
4937   "ID of a mime part that should be buttonized.
4938 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4939
4940 (eval-when-compile
4941   (defsubst gnus-article-edit-part (handles &optional current-id)
4942     "Edit an article in order to delete a mime part.
4943 This function is exclusively used by `gnus-mime-save-part-and-strip'
4944 and `gnus-mime-delete-part', and not provided at run-time normally."
4945     (gnus-article-edit-article
4946      `(lambda ()
4947         (buffer-disable-undo)
4948         (erase-buffer)
4949         (let ((mail-parse-charset (or gnus-article-charset
4950                                       ',gnus-newsgroup-charset))
4951               (mail-parse-ignored-charsets
4952                (or gnus-article-ignored-charsets
4953                    ',gnus-newsgroup-ignored-charsets))
4954               (mbl mml-buffer-list))
4955           (setq mml-buffer-list nil)
4956           (insert-buffer-substring gnus-original-article-buffer)
4957           (mime-to-mml ',handles)
4958           (setq gnus-article-mime-handles nil)
4959           (let ((mbl1 mml-buffer-list))
4960             (setq mml-buffer-list mbl)
4961             (set (make-local-variable 'mml-buffer-list) mbl1))
4962           (gnus-make-local-hook 'kill-buffer-hook)
4963           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4964      `(lambda (no-highlight)
4965         (let ((mail-parse-charset (or gnus-article-charset
4966                                       ',gnus-newsgroup-charset))
4967               (message-options message-options)
4968               (message-options-set-recipient)
4969               (mail-parse-ignored-charsets
4970                (or gnus-article-ignored-charsets
4971                    ',gnus-newsgroup-ignored-charsets)))
4972           (mml-to-mime)
4973           (mml-destroy-buffers)
4974           (remove-hook 'kill-buffer-hook
4975                        'mml-destroy-buffers t)
4976           (kill-local-variable 'mml-buffer-list))
4977         (gnus-summary-edit-article-done
4978          ,(or (mail-header-references gnus-current-headers) "")
4979          ,(gnus-group-read-only-p)
4980          ,gnus-summary-buffer no-highlight))
4981      t)
4982     ;; Force buttonizing this part.
4983     (let ((gnus-mime-buttonized-part-id current-id))
4984       (gnus-article-edit-done))
4985     (gnus-configure-windows 'article)
4986     (when (and current-id (integerp gnus-auto-select-part))
4987       (gnus-article-jump-to-part
4988        (min (max (+ current-id gnus-auto-select-part) 1)
4989             (with-current-buffer gnus-article-buffer
4990               (length gnus-article-mime-handle-alist)))))))
4991
4992 (defun gnus-mime-replace-part (file)
4993   "Replace MIME part under point with an external body."
4994   ;; Useful if file has already been saved to disk
4995   (interactive
4996    (list
4997     (read-file-name "Replace MIME part with file: "
4998                     (or mm-default-directory default-directory)
4999                     nil nil)))
5000   (gnus-mime-save-part-and-strip file))
5001
5002 (defun gnus-mime-save-part-and-strip (&optional file)
5003   "Save the MIME part under point then replace it with an external body.
5004 If FILE is given, use it for the external part."
5005   (interactive)
5006   (gnus-article-check-buffer)
5007   (when (gnus-group-read-only-p)
5008     (error "The current group does not support deleting of parts"))
5009   (when (mm-complicated-handles gnus-article-mime-handles)
5010     (error "\
5011 The current article has a complicated MIME structure, giving up..."))
5012   (let* ((data (get-text-property (point) 'gnus-data))
5013          (id (get-text-property (point) 'gnus-part))
5014          param
5015          (handles gnus-article-mime-handles))
5016     (unless file
5017       (setq file
5018             (and data (mm-save-part data "Delete MIME part and save to: "))))
5019     (when file
5020       (with-current-buffer (mm-handle-buffer data)
5021         (erase-buffer)
5022         (insert "Content-Type: " (mm-handle-media-type data))
5023         (mml-insert-parameter-string (cdr (mm-handle-type data))
5024                                      '(charset))
5025         ;; Add a filename for the sake of saving the part again.
5026         (mml-insert-parameter
5027          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5028         (insert "\n")
5029         (insert "Content-ID: " (message-make-message-id) "\n")
5030         (insert "Content-Transfer-Encoding: binary\n")
5031         (insert "\n"))
5032       (setcdr data
5033               (cdr (mm-make-handle nil
5034                                    `("message/external-body"
5035                                      (access-type . "LOCAL-FILE")
5036                                      (name . ,file)))))
5037       ;; (set-buffer gnus-summary-buffer)
5038       (gnus-article-edit-part handles id))))
5039
5040 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5041 ;; parts...>') but with stripping would be nice.
5042
5043 (defun gnus-mime-delete-part ()
5044   "Delete the MIME part under point.
5045 Replace it with some information about the removed part."
5046   (interactive)
5047   (gnus-article-check-buffer)
5048   (when (gnus-group-read-only-p)
5049     (error "The current group does not support deleting of parts"))
5050   (when (mm-complicated-handles gnus-article-mime-handles)
5051     (error "\
5052 The current article has a complicated MIME structure, giving up..."))
5053   (when (or gnus-expert-user
5054             (gnus-yes-or-no-p "\
5055 Deleting parts may malfunction or destroy the article; continue? "))
5056     (let* ((data (get-text-property (point) 'gnus-data))
5057            (id (get-text-property (point) 'gnus-part))
5058            (handles gnus-article-mime-handles)
5059            (none "(none)")
5060            (description
5061             (let ((desc (mm-handle-description data)))
5062               (when desc
5063                 (mail-decode-encoded-word-string desc))))
5064            (filename
5065             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5066                 none))
5067            (type (mm-handle-media-type data)))
5068       (unless data
5069         (error "No MIME part under point"))
5070       (with-current-buffer (mm-handle-buffer data)
5071         (let ((bsize (buffer-size)))
5072           (erase-buffer)
5073           (insert
5074            (concat
5075             ",----\n"
5076             "| The following attachment has been deleted:\n"
5077             "|\n"
5078             "| Type:           " type "\n"
5079             "| Filename:       " filename "\n"
5080             "| Size (encoded): " (format "%s byte%s\n"
5081                                          bsize (if (= bsize 1)
5082                                                    ""
5083                                                  "s"))
5084             (when description
5085               (concat    "| Description:    " description "\n"))
5086             "`----\n"))
5087           (setcdr data
5088                   (cdr (mm-make-handle
5089                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5090                         (list "attachment")
5091                         (format "Deleted attachment (%s bytes)" bsize))))))
5092       ;; (set-buffer gnus-summary-buffer)
5093       (gnus-article-edit-part handles id))))
5094
5095 (defun gnus-mime-save-part ()
5096   "Save the MIME part under point."
5097   (interactive)
5098   (gnus-article-check-buffer)
5099   (let ((data (get-text-property (point) 'gnus-data)))
5100     (when data
5101       (mm-save-part data))))
5102
5103 (defun gnus-mime-pipe-part (&optional cmd)
5104   "Pipe the MIME part under point to a process.
5105 Use CMD as the process."
5106   (interactive)
5107   (gnus-article-check-buffer)
5108   (let ((data (get-text-property (point) 'gnus-data)))
5109     (when data
5110       (mm-pipe-part data cmd))))
5111
5112 (defun gnus-mime-view-part ()
5113   "Interactively choose a viewing method for the MIME part under point."
5114   (interactive)
5115   (gnus-article-check-buffer)
5116   (let ((data (get-text-property (point) 'gnus-data)))
5117     (when data
5118       (setq gnus-article-mime-handles
5119             (mm-merge-handles
5120              gnus-article-mime-handles (setq data (copy-sequence data))))
5121       (mm-interactively-view-part data))))
5122
5123 (defun gnus-mime-view-part-as-type-internal ()
5124   (gnus-article-check-buffer)
5125   (let* ((handle (get-text-property (point) 'gnus-data))
5126          (name (or
5127                 ;; Content-Type: foo/bar; name=...
5128                 (mail-content-type-get (mm-handle-type handle) 'name)
5129                 ;; Content-Disposition: attachment; filename=...
5130                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5131          (def-type (and name (mm-default-file-encoding name))))
5132     (or (and def-type (cons def-type 0))
5133         (and handle
5134              (equal (mm-handle-media-supertype handle) "text")
5135              '("text/plain" . 0))
5136         '("application/octet-stream" . 0))))
5137
5138 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5139   "Choose a MIME media type, and view the part as such.
5140 If non-nil, PRED is a predicate to use during completion to limit the
5141 available media-types."
5142   (interactive)
5143   (unless mime-type
5144     (setq mime-type
5145           (let ((default (gnus-mime-view-part-as-type-internal)))
5146             (gnus-completing-read
5147              "View as MIME type"
5148              (if pred
5149                  (gnus-remove-if-not pred (mailcap-mime-types))
5150                (mailcap-mime-types))
5151              nil nil nil
5152              (car default)))))
5153   (gnus-article-check-buffer)
5154   (let ((handle (get-text-property (point) 'gnus-data)))
5155     (when handle
5156       (when (equal (mm-handle-media-type handle) "message/external-body")
5157         (unless (mm-handle-cache handle)
5158           (mm-extern-cache-contents handle))
5159         (setq handle (mm-handle-cache handle)))
5160       (setq handle
5161             (mm-make-handle (mm-handle-buffer handle)
5162                             (cons mime-type (cdr (mm-handle-type handle)))
5163                             (mm-handle-encoding handle)
5164                             (mm-handle-undisplayer handle)
5165                             (mm-handle-disposition handle)
5166                             (mm-handle-description handle)
5167                             nil
5168                             (mm-handle-id handle)))
5169       (setq gnus-article-mime-handles
5170             (mm-merge-handles gnus-article-mime-handles handle))
5171       (when (mm-handle-displayed-p handle)
5172         (mm-remove-part handle))
5173       (gnus-mm-display-part handle))))
5174
5175 (defun gnus-mime-copy-part (&optional handle arg)
5176   "Put the MIME part under point into a new buffer.
5177 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5178 are decompressed."
5179   (interactive (list nil current-prefix-arg))
5180   (gnus-article-check-buffer)
5181   (unless handle
5182     (setq handle (get-text-property (point) 'gnus-data)))
5183   (when handle
5184     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5185                                                'name)
5186                         (mail-content-type-get (mm-handle-disposition handle)
5187                                                'filename)))
5188           contents dont-decode charset coding-system)
5189       (mm-with-unibyte-buffer
5190         (mm-insert-part handle)
5191         (setq contents (or (condition-case nil
5192                                (mm-decompress-buffer filename nil 'sig)
5193                              (error
5194                               (setq dont-decode t)
5195                               nil))
5196                            (buffer-string))))
5197       (setq filename (cond (filename (file-name-nondirectory filename))
5198                            (dont-decode "*raw data*")
5199                            (t "*decoded*")))
5200       (cond
5201        (dont-decode)
5202        ((not arg)
5203         (unless (setq charset (mail-content-type-get
5204                                (mm-handle-type handle) 'charset))
5205           (unless (setq coding-system (mm-with-unibyte-buffer
5206                                         (insert contents)
5207                                         (mm-find-buffer-file-coding-system)))
5208             (setq charset gnus-newsgroup-charset))))
5209        ((numberp arg)
5210         (setq charset (or (cdr (assq arg
5211                                      gnus-summary-show-article-charset-alist))
5212                           (mm-read-coding-system "Charset: ")))))
5213       (switch-to-buffer (generate-new-buffer filename))
5214       (if (or coding-system
5215               (and charset
5216                    (setq coding-system (mm-charset-to-coding-system charset))
5217                    (not (eq coding-system 'ascii))))
5218           (progn
5219             (mm-enable-multibyte)
5220             (insert (mm-decode-coding-string contents coding-system))
5221             (setq buffer-file-coding-system
5222                   (if (boundp 'last-coding-system-used)
5223                       (symbol-value 'last-coding-system-used)
5224                     coding-system)))
5225         (mm-disable-multibyte)
5226         (insert contents)
5227         (setq buffer-file-coding-system mm-binary-coding-system))
5228       ;; We do it this way to make `normal-mode' set the appropriate mode.
5229       (unwind-protect
5230           (progn
5231             (setq buffer-file-name (expand-file-name filename))
5232             (normal-mode))
5233         (setq buffer-file-name nil))
5234       (goto-char (point-min)))))
5235
5236 (defun gnus-mime-print-part (&optional handle filename)
5237   "Print the MIME part under point."
5238   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5239   (gnus-article-check-buffer)
5240   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5241          (contents (and handle (mm-get-part handle)))
5242          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5243          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5244     (when contents
5245         (if printer
5246             (unwind-protect
5247                 (progn
5248                   (mm-save-part-to-file handle file)
5249                   (call-process shell-file-name nil
5250                                 (generate-new-buffer " *mm*")
5251                                 nil
5252                                 shell-command-switch
5253                                 (mm-mailcap-command
5254                                  printer file (mm-handle-type handle))))
5255               (delete-file file))
5256           (with-temp-buffer
5257             (insert contents)
5258             (gnus-print-buffer))
5259           (ps-despool filename)))))
5260
5261 (defun gnus-mime-inline-part (&optional handle arg)
5262   "Insert the MIME part under point into the current buffer.
5263 Compressed files like .gz and .bz2 are decompressed."
5264   (interactive (list nil current-prefix-arg))
5265   (gnus-article-check-buffer)
5266   (unless handle
5267     (setq handle (get-text-property (point) 'gnus-data)))
5268   (when handle
5269     (let ((b (point))
5270           (inhibit-read-only t)
5271           contents charset coding-system)
5272       (if (and (not arg) (mm-handle-undisplayer handle))
5273           (mm-remove-part handle)
5274         (mm-with-unibyte-buffer
5275           (mm-insert-part handle)
5276           (setq contents
5277                 (or (mm-decompress-buffer
5278                      (or (mail-content-type-get (mm-handle-type handle)
5279                                                 'name)
5280                          (mail-content-type-get (mm-handle-disposition handle)
5281                                                 'filename))
5282                      nil t)
5283                     (buffer-string))))
5284         (cond
5285          ((not arg)
5286           (unless (setq charset (mail-content-type-get
5287                                  (mm-handle-type handle) 'charset))
5288             (unless (setq coding-system
5289                           (mm-with-unibyte-buffer
5290                             (insert contents)
5291                             (mm-find-buffer-file-coding-system)))
5292               (setq charset gnus-newsgroup-charset))))
5293          ((numberp arg)
5294           (if (mm-handle-undisplayer handle)
5295               (mm-remove-part handle))
5296           (setq charset
5297                 (or (cdr (assq arg
5298                                gnus-summary-show-article-charset-alist))
5299                     (mm-read-coding-system "Charset: "))))
5300          (t
5301           (if (mm-handle-undisplayer handle)
5302               (mm-remove-part handle))))
5303         (forward-line 2)
5304         (mm-display-inline handle)
5305         (goto-char b)))))
5306
5307 (defun gnus-mime-set-charset-parameters (handle charset)
5308   "Set CHARSET to parameters in HANDLE.
5309 CHARSET may either be a string or a symbol."
5310   (unless (stringp charset)
5311     (setq charset (symbol-name charset)))
5312   (if (stringp (car handle))
5313       (dolist (h (cdr handle))
5314         (gnus-mime-set-charset-parameters h charset))
5315     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5316                                             "message/external-body")
5317                                      (progn
5318                                        (unless (mm-handle-cache handle)
5319                                          (mm-extern-cache-contents handle))
5320                                        (mm-handle-cache handle))
5321                                    handle)))
5322            (param (assq 'charset (cdr type))))
5323       (if param
5324           (setcdr param charset)
5325         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5326
5327 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5328   "Insert the MIME part under point into the current buffer using the
5329 specified charset."
5330   (interactive (list nil current-prefix-arg))
5331   (gnus-article-check-buffer)
5332   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5333         (fun (get-text-property (point) 'gnus-callback))
5334         (gnus-newsgroup-ignored-charsets 'gnus-all)
5335         charset form preferred parts)
5336     (when handle
5337       (when (prog1
5338                 (and fun
5339                      (setq charset
5340                            (or (cdr (assq
5341                                      arg
5342                                      gnus-summary-show-article-charset-alist))
5343                                (mm-read-coding-system "Charset: "))))
5344               (if (mm-handle-undisplayer handle)
5345                   (mm-remove-part handle)))
5346         (gnus-mime-set-charset-parameters handle charset)
5347         (when (and (consp (setq form (cdr-safe fun)))
5348                    (setq form (ignore-errors
5349                                 (assq 'gnus-mime-display-alternative form)))
5350                    (setq preferred (caddr form))
5351                    (progn
5352                      (when (eq (car preferred) 'quote)
5353                        (setq preferred (cadr preferred)))
5354                      (not (equal preferred
5355                                  (get-text-property (point) 'gnus-data))))
5356                    (setq parts (get-text-property (point) 'gnus-part))
5357                    (setq parts (cdr (assq parts
5358                                           gnus-article-mime-handle-alist)))
5359                    (equal (mm-handle-media-type parts) "multipart/alternative")
5360                    (setq parts (reverse (cdr parts))))
5361           (setcar (cddr form)
5362                   (list 'quote (or (cadr (member preferred parts))
5363                                    (car parts)))))
5364         (funcall fun handle)))))
5365
5366 (defun gnus-mime-view-part-externally (&optional handle)
5367   "View the MIME part under point with an external viewer."
5368   (interactive)
5369   (gnus-article-check-buffer)
5370   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5371          (mm-user-display-methods nil)
5372          (mm-inlined-types nil)
5373          (mail-parse-charset gnus-newsgroup-charset)
5374          (mail-parse-ignored-charsets
5375           (with-current-buffer gnus-summary-buffer
5376             gnus-newsgroup-ignored-charsets))
5377          (type (mm-handle-media-type handle))
5378          (method (mailcap-mime-info type))
5379          (mm-enable-external t))
5380     (if (not (stringp method))
5381         (gnus-mime-view-part-as-type
5382          nil (lambda (type) (stringp (mailcap-mime-info type))))
5383       (when handle
5384         (mm-display-part handle nil t)))))
5385
5386 (defun gnus-mime-view-part-internally (&optional handle)
5387   "View the MIME part under point with an internal viewer.
5388 If no internal viewer is available, use an external viewer."
5389   (interactive)
5390   (gnus-article-check-buffer)
5391   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5392          (mm-inlined-types '(".*"))
5393          (mm-inline-large-images t)
5394          (mail-parse-charset gnus-newsgroup-charset)
5395          (mail-parse-ignored-charsets
5396           (with-current-buffer gnus-summary-buffer
5397             gnus-newsgroup-ignored-charsets))
5398          (inhibit-read-only t))
5399     (if (not (mm-inlinable-p handle))
5400         (gnus-mime-view-part-as-type
5401          nil (lambda (type) (mm-inlinable-p handle type)))
5402       (when handle
5403         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5404
5405 (defun gnus-mime-action-on-part (&optional action)
5406   "Do something with the MIME attachment at \(point\)."
5407   (interactive
5408    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5409   (gnus-article-check-buffer)
5410   (let ((action-pair (assoc action gnus-mime-action-alist)))
5411     (if action-pair
5412         (funcall (cdr action-pair)))))
5413
5414 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5415   "Call FUNCTION on MIME part N.
5416 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5417 If INTERACTIVE, call FUNCTION interactivly."
5418   (let (window frame)
5419     ;; Check whether the article is displayed.
5420     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5421                  (setq window (get-buffer-window gnus-article-buffer t))
5422                  (frame-visible-p (setq frame (window-frame window))))
5423       (error "No article is displayed"))
5424     (with-current-buffer gnus-article-buffer
5425       ;; Check whether the article displays the right contents.
5426       (unless (with-current-buffer gnus-summary-buffer
5427                 (eq gnus-current-article (gnus-summary-article-number)))
5428         (error "You should select the right article first"))
5429       (if n
5430           (setq n (prefix-numeric-value n))
5431         (let ((pt (point)))
5432           (setq n (or (get-text-property pt 'gnus-part)
5433                       (and (not (bobp))
5434                            (get-text-property (1- pt) 'gnus-part))
5435                       (get-text-property (prog2
5436                                              (forward-line 1)
5437                                              (point)
5438                                            (goto-char pt))
5439                                          'gnus-part)
5440                       (get-text-property
5441                        (or (and (setq pt (previous-single-property-change
5442                                           pt 'gnus-part))
5443                                 (1- pt))
5444                            (next-single-property-change (point) 'gnus-part)
5445                            (point))
5446                        'gnus-part)
5447                       1))))
5448       ;; Check whether the specified part exists.
5449       (when (> n (length gnus-article-mime-handle-alist))
5450         (error "No such part")))
5451     (unless
5452         (progn
5453           ;; To select the window is needed so that the cursor
5454           ;; might be visible on the MIME button.
5455           (select-window (prog1
5456                              window
5457                            (setq window (selected-window))
5458                            ;; Article may be displayed in the other frame.
5459                            (gnus-select-frame-set-input-focus
5460                             (prog1
5461                                 frame
5462                               (setq frame (selected-frame))))))
5463           (when (gnus-article-goto-part n)
5464             ;; We point the cursor and the arrow at the MIME button
5465             ;; when the `function' prompt the user for something.
5466             (unless (and (pos-visible-in-window-p)
5467                          (> (count-lines (point) (window-end))
5468                             (/ (1- (window-height)) 3)))
5469               (recenter (/ (1- (window-height)) 3)))
5470             (let ((cursor-in-non-selected-windows t)
5471                   (overlay-arrow-string "=>")
5472                   (overlay-arrow-position (point-marker)))
5473               (unwind-protect
5474                   (cond
5475                    ((and no-handle interactive)
5476                     (call-interactively function))
5477                    (no-handle
5478                     (funcall function))
5479                    (interactive
5480                     (call-interactively
5481                      function (get-text-property (point) 'gnus-data)))
5482                    (t
5483                     (funcall function
5484                              (get-text-property (point) 'gnus-data))))
5485                 (set-marker overlay-arrow-position nil)
5486                 (unless gnus-auto-select-part
5487                   (gnus-select-frame-set-input-focus frame)
5488                   (select-window window))))
5489             t))
5490       (if gnus-inhibit-mime-unbuttonizing
5491           ;; This is the default though the program shouldn't reach here.
5492           (error "No such part")
5493         ;; The part which doesn't have the MIME button is selected.
5494         ;; So, we display all the buttons and redo it.
5495         (let ((gnus-inhibit-mime-unbuttonizing t))
5496           (gnus-summary-show-article)
5497           (gnus-article-part-wrapper n function no-handle))))))
5498
5499 (defun gnus-article-pipe-part (n)
5500   "Pipe MIME part N, which is the numerical prefix."
5501   (interactive "P")
5502   (gnus-article-part-wrapper n 'mm-pipe-part))
5503
5504 (defun gnus-article-save-part (n)
5505   "Save MIME part N, which is the numerical prefix."
5506   (interactive "P")
5507   (gnus-article-part-wrapper n 'mm-save-part))
5508
5509 (defun gnus-article-interactively-view-part (n)
5510   "View MIME part N interactively, which is the numerical prefix."
5511   (interactive "P")
5512   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5513
5514 (defun gnus-article-copy-part (n)
5515   "Copy MIME part N, which is the numerical prefix."
5516   (interactive "P")
5517   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5518
5519 (defun gnus-article-view-part-as-charset (n)
5520   "View MIME part N using a specified charset.
5521 N is the numerical prefix."
5522   (interactive "P")
5523   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5524
5525 (defun gnus-article-view-part-externally (n)
5526   "View MIME part N externally, which is the numerical prefix."
5527   (interactive "P")
5528   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5529
5530 (defun gnus-article-inline-part (n)
5531   "Inline MIME part N, which is the numerical prefix."
5532   (interactive "P")
5533   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5534
5535 (defun gnus-article-save-part-and-strip (n)
5536   "Save MIME part N and replace it with an external body.
5537 N is the numerical prefix."
5538   (interactive "P")
5539   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5540
5541 (defun gnus-article-replace-part (n)
5542   "Replace MIME part N with an external body.
5543 N is the numerical prefix."
5544   (interactive "P")
5545   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5546
5547 (defun gnus-article-delete-part (n)
5548   "Delete MIME part N and add some information about the removed part.
5549 N is the numerical prefix."
5550   (interactive "P")
5551   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5552
5553 (defun gnus-article-view-part-as-type (n)
5554   "Choose a MIME media type, and view part N as such.
5555 N is the numerical prefix."
5556   (interactive "P")
5557   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5558
5559 (defun gnus-article-mime-match-handle-first (condition)
5560   (if condition
5561       (let (n)
5562         (dolist (ihandle gnus-article-mime-handle-alist)
5563           (if (and (cond
5564                     ((functionp condition)
5565                      (funcall condition (cdr ihandle)))
5566                     ((eq condition 'undisplayed)
5567                      (not (or (mm-handle-undisplayer (cdr ihandle))
5568                               (equal (mm-handle-media-type (cdr ihandle))
5569                                      "multipart/alternative"))))
5570                     ((eq condition 'undisplayed-alternative)
5571                      (not (mm-handle-undisplayer (cdr ihandle))))
5572                     (t t))
5573                    (gnus-article-goto-part (car ihandle))
5574                    (or (not n) (< (car ihandle) n)))
5575               (setq n (car ihandle))))
5576         (or n 1))
5577     1))
5578
5579 (defun gnus-article-view-part (&optional n)
5580   "View MIME part N, which is the numerical prefix.
5581 If the part is already shown, hide the part.  If N is nil, view
5582 all parts."
5583   (interactive "P")
5584   (with-current-buffer gnus-article-buffer
5585     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5586                              gnus-article-mime-match-handle-function)))
5587     (when (> n (length gnus-article-mime-handle-alist))
5588       (error "No such part"))
5589     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5590       (when (gnus-article-goto-part n)
5591         (if (equal (car handle) "multipart/alternative")
5592             (gnus-article-press-button)
5593           (when (eq (gnus-mm-display-part handle) 'internal)
5594             (gnus-set-window-start)))))))
5595
5596 (defsubst gnus-article-mime-total-parts ()
5597   (if (bufferp (car gnus-article-mime-handles))
5598       1 ;; single part
5599     (1- (length gnus-article-mime-handles))))
5600
5601 (defun gnus-mm-display-part (handle)
5602   "Display HANDLE and fix MIME button."
5603   (let ((id (get-text-property (point) 'gnus-part))
5604         (point (point))
5605         (inhibit-read-only t))
5606     (forward-line 1)
5607     (prog1
5608         (let ((window (selected-window))
5609               (mail-parse-charset gnus-newsgroup-charset)
5610               (mail-parse-ignored-charsets
5611                (if (gnus-buffer-live-p gnus-summary-buffer)
5612                    (with-current-buffer gnus-summary-buffer
5613                      gnus-newsgroup-ignored-charsets)
5614                  nil)))
5615           (save-excursion
5616             (unwind-protect
5617                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5618                       (beg (point)))
5619                   (when win
5620                     (select-window win))
5621                   (goto-char point)
5622                   (forward-line)
5623                   (if (mm-handle-displayed-p handle)
5624                       ;; This will remove the part.
5625                       (mm-display-part handle)
5626                     (save-restriction
5627                       (narrow-to-region (point)
5628                                         (if (eobp) (point) (1+ (point))))
5629                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5630                       ;; We narrow to the part itself and
5631                       ;; then call the treatment functions.
5632                       (goto-char (point-min))
5633                       (forward-line 1)
5634                       (narrow-to-region (point) (point-max))
5635                       (gnus-treat-article
5636                        nil id
5637                        (gnus-article-mime-total-parts)
5638                        (mm-handle-media-type handle)))))
5639               (if (window-live-p window)
5640                   (select-window window)))))
5641       (goto-char point)
5642       (gnus-delete-line)
5643       (gnus-insert-mime-button
5644        handle id (list (mm-handle-displayed-p handle)))
5645       (goto-char point))))
5646
5647 (defun gnus-article-goto-part (n)
5648   "Go to MIME part N."
5649   (when gnus-break-pages
5650     (widen))
5651   (prog1
5652       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5653             part handle end next handles)
5654         (when start
5655           (goto-char start)
5656           (if (setq handle (get-text-property start 'gnus-data))
5657               start
5658             ;; Go to the displayed subpart, assuming this is
5659             ;; multipart/alternative.
5660             (setq part start
5661                   end (point-at-eol))
5662             (while (and (not handle)
5663                         part
5664                         (< part end)
5665                         (setq next (text-property-not-all part end
5666                                                           'gnus-data nil)))
5667               (setq part next
5668                     handle (get-text-property part 'gnus-data))
5669               (push (cons handle part) handles)
5670               (unless (mm-handle-displayed-p handle)
5671                 (setq handle nil
5672                       part (text-property-any part end 'gnus-data nil))))
5673             (unless handle
5674               ;; No subpart is displayed, so we find preferred one.
5675               (setq part
5676                     (cdr (assq (mm-preferred-alternative
5677                                 (nreverse (mapcar 'car handles)))
5678                                handles))))
5679             (if part
5680                 (goto-char (1+ part))
5681               start))))
5682     (when gnus-break-pages
5683       (gnus-narrow-to-page))))
5684
5685 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5686   (let ((gnus-tmp-name
5687          (or (mail-content-type-get (mm-handle-type handle) 'name)
5688              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5689              (mail-content-type-get (mm-handle-type handle) 'url)
5690              ""))
5691         (gnus-tmp-type (mm-handle-media-type handle))
5692         (gnus-tmp-description (or (mm-handle-description handle) ""))
5693         (gnus-tmp-dots
5694          (if (if displayed (car displayed)
5695                (mm-handle-displayed-p handle))
5696              "" "..."))
5697         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5698                            (buffer-size)))
5699         gnus-tmp-type-long b e)
5700     (when (string-match ".*/" gnus-tmp-name)
5701       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5702     (setq gnus-tmp-type-long (concat gnus-tmp-type
5703                                      (and (not (equal gnus-tmp-name ""))
5704                                           (concat "; " gnus-tmp-name))))
5705     (unless (equal gnus-tmp-description "")
5706       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5707     (unless (bolp)
5708       (insert "\n"))
5709     (setq b (point))
5710     (gnus-eval-format
5711      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5712      `(keymap ,gnus-mime-button-map
5713               gnus-callback gnus-mm-display-part
5714               gnus-part ,gnus-tmp-id
5715               article-type annotation
5716               gnus-data ,handle))
5717     (setq e (if (bolp)
5718                 ;; Exclude a newline.
5719                 (1- (point))
5720               (point)))
5721     (when gnus-article-button-face
5722       (gnus-overlay-put (gnus-make-overlay b e nil t)
5723                         'face gnus-article-button-face))
5724     (widget-convert-button
5725      'link b e
5726      :mime-handle handle
5727      :action 'gnus-widget-press-button
5728      :button-keymap gnus-mime-button-map
5729      :help-echo
5730      (lambda (widget)
5731        ;; Needed to properly clear the message due to a bug in
5732        ;; wid-edit (XEmacs only).
5733        (if (boundp 'help-echo-owns-message)
5734            (setq help-echo-owns-message t))
5735        (format
5736         "%S: %s the MIME part; %S: more options"
5737         (aref gnus-mouse-2 0)
5738         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5739             "hide" "show")
5740         (aref gnus-down-mouse-3 0))))))
5741
5742 (defun gnus-widget-press-button (elems el)
5743   (goto-char (widget-get elems :from))
5744   (gnus-article-press-button))
5745
5746 (defvar gnus-displaying-mime nil)
5747
5748 (defun gnus-display-mime (&optional ihandles)
5749   "Display the MIME parts."
5750   (save-excursion
5751     (save-selected-window
5752       (let ((window (get-buffer-window gnus-article-buffer))
5753             (point (point)))
5754         (when window
5755           (select-window window)
5756           ;; We have to do this since selecting the window
5757           ;; may change the point.  So we set the window point.
5758           (set-window-point window point)))
5759       (let ((handles ihandles)
5760             (inhibit-read-only t)
5761             handle)
5762         (cond (handles)
5763               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5764                (when gnus-article-emulate-mime
5765                  (mm-uu-dissect-text-parts handles)))
5766               (gnus-article-emulate-mime
5767                (setq handles (mm-uu-dissect))))
5768         (when (and (not ihandles)
5769                    (not gnus-displaying-mime))
5770           ;; Top-level call; we clean up.
5771           (when gnus-article-mime-handles
5772             (mm-destroy-parts gnus-article-mime-handles)
5773             (setq gnus-article-mime-handle-alist nil));; A trick.
5774           (setq gnus-article-mime-handles handles)
5775           ;; We allow users to glean info from the handles.
5776           (when gnus-article-mime-part-function
5777             (gnus-mime-part-function handles)))
5778         (if (and handles
5779                  (or (not (stringp (car handles)))
5780                      (cdr handles)))
5781             (progn
5782               (when (and (not ihandles)
5783                          (not gnus-displaying-mime))
5784                 ;; Clean up for mime parts.
5785                 (article-goto-body)
5786                 (delete-region (point) (point-max)))
5787               (let ((gnus-displaying-mime t))
5788                 (gnus-mime-display-part handles)))
5789           (save-restriction
5790             (article-goto-body)
5791             (narrow-to-region (point) (point-max))
5792             (gnus-treat-article nil 1 1 "text/plain")
5793             (widen)))
5794         (unless ihandles
5795           ;; Highlight the headers.
5796           (save-excursion
5797             (save-restriction
5798               (article-goto-body)
5799               (narrow-to-region (point-min) (point))
5800               (gnus-article-save-original-date
5801                (gnus-treat-article 'head)))))))
5802     ;; Cope with broken MIME messages.
5803     (goto-char (point-max))
5804     (unless (bolp)
5805       (insert "\n"))))
5806
5807 (defcustom gnus-mime-display-multipart-as-mixed nil
5808   "Display \"multipart\" parts as  \"multipart/mixed\".
5809
5810 If t, it overrides nil values of
5811 `gnus-mime-display-multipart-alternative-as-mixed' and
5812 `gnus-mime-display-multipart-related-as-mixed'."
5813   :group 'gnus-article-mime
5814   :type 'boolean)
5815
5816 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5817   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5818   :version "22.1"
5819   :group 'gnus-article-mime
5820   :type 'boolean)
5821
5822 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5823   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5824
5825 If displaying \"text/html\" is discouraged \(see
5826 `mm-discouraged-alternatives'\) images or other material inside a
5827 \"multipart/related\" part might be overlooked when this variable is nil."
5828   :version "22.1"
5829   :group 'gnus-article-mime
5830   :type 'boolean)
5831
5832 (defun gnus-mime-display-part (handle)
5833   (cond
5834    ;; Maybe a broken MIME message.
5835    ((null handle))
5836    ;; Single part.
5837    ((not (stringp (car handle)))
5838     (gnus-mime-display-single handle))
5839    ;; User-defined multipart
5840    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5841     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5842              handle))
5843    ;; multipart/alternative
5844    ((and (equal (car handle) "multipart/alternative")
5845          (not (or gnus-mime-display-multipart-as-mixed
5846                   gnus-mime-display-multipart-alternative-as-mixed)))
5847     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5848       (push (cons id handle) gnus-article-mime-handle-alist)
5849       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5850    ;; multipart/related
5851    ((and (equal (car handle) "multipart/related")
5852          (not (or gnus-mime-display-multipart-as-mixed
5853                   gnus-mime-display-multipart-related-as-mixed)))
5854     ;;;!!!We should find the start part, but we just default
5855     ;;;!!!to the first part.
5856     ;;(gnus-mime-display-part (cadr handle))
5857     ;;;!!! Most multipart/related is an HTML message plus images.
5858     ;;;!!! Unfortunately we are unable to let W3 display those
5859     ;;;!!! included images, so we just display it as a mixed multipart.
5860     ;;(gnus-mime-display-mixed (cdr handle))
5861     ;;;!!! No, w3 can display everything just fine.
5862     (gnus-mime-display-part (cadr handle)))
5863    ((equal (car handle) "multipart/signed")
5864     (gnus-add-wash-type 'signed)
5865     (gnus-mime-display-security handle))
5866    ((equal (car handle) "multipart/encrypted")
5867     (gnus-add-wash-type 'encrypted)
5868     (gnus-mime-display-security handle))
5869    ;; Other multiparts are handled like multipart/mixed.
5870    (t
5871     (gnus-mime-display-mixed (cdr handle)))))
5872
5873 (defun gnus-mime-part-function (handles)
5874   (if (stringp (car handles))
5875       (mapcar 'gnus-mime-part-function (cdr handles))
5876     (funcall gnus-article-mime-part-function handles)))
5877
5878 (defun gnus-mime-display-mixed (handles)
5879   (mapcar 'gnus-mime-display-part handles))
5880
5881 (defun gnus-mime-display-single (handle)
5882   (let ((type (mm-handle-media-type handle))
5883         (ignored gnus-ignored-mime-types)
5884         (not-attachment t)
5885         (move nil)
5886         display text)
5887     (catch 'ignored
5888       (progn
5889         (while ignored
5890           (when (string-match (pop ignored) type)
5891             (throw 'ignored nil)))
5892         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5893                                (with-current-buffer gnus-summary-buffer
5894                                  gnus-inhibit-images)
5895                              gnus-inhibit-images)
5896                            (string-match "\\`image/" type)))
5897                  (setq not-attachment
5898                        (and (not (mm-inline-override-p handle))
5899                             (or (not (mm-handle-disposition handle))
5900                                 (equal (car (mm-handle-disposition handle))
5901                                        "inline")
5902                                 (mm-attachment-override-p handle))))
5903                  (mm-automatic-display-p handle)
5904                  (or (and
5905                       (mm-inlinable-p handle)
5906                       (mm-inlined-p handle))
5907                      (mm-automatic-external-display-p type)))
5908             (setq display t)
5909           (when (equal (mm-handle-media-supertype handle) "text")
5910             (setq text t)))
5911         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5912               beg)
5913           (push (cons id handle) gnus-article-mime-handle-alist)
5914           (when (and display
5915                      (equal (mm-handle-media-supertype handle) "message"))
5916             (insert-char
5917              ?\n
5918              (cond ((not (bolp)) 2)
5919                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5920                    (t 1))))
5921           (when (or (not display)
5922                     (not (gnus-unbuttonized-mime-type-p type))
5923                     (eq id gnus-mime-buttonized-part-id))
5924             (gnus-insert-mime-button
5925              handle id (list (or display (and not-attachment text))))
5926             (gnus-article-insert-newline)
5927             ;; Remember modify the number of forward lines.
5928             (setq move t))
5929           (setq beg (point))
5930           (cond
5931            (display
5932             (when move
5933               (forward-line -1)
5934               (setq beg (point)))
5935             (let ((mail-parse-charset gnus-newsgroup-charset)
5936                   (mail-parse-ignored-charsets
5937                    (save-excursion (condition-case ()
5938                                        (set-buffer gnus-summary-buffer)
5939                                      (error))
5940                                    gnus-newsgroup-ignored-charsets)))
5941               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5942             (goto-char (point-max)))
5943            ((and text not-attachment)
5944             (when move
5945               (forward-line -1)
5946               (setq beg (point)))
5947             (gnus-article-insert-newline)
5948             (mm-display-inline handle)
5949             (goto-char (point-max))))
5950           ;; Do highlighting.
5951           (save-excursion
5952             (save-restriction
5953               (narrow-to-region beg (point))
5954               (if (eq handle gnus-article-mime-handles)
5955                   ;; The format=flowed case.
5956                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5957                 ;; Don't count signature parts that are never displayed.
5958                 ;; The part number should be re-calculated supposing this
5959                 ;; might be a message/rfc822 part.
5960                 (let (handles)
5961                   (dolist (part gnus-article-mime-handles)
5962                     (unless (or (stringp part)
5963                                 (equal (car (mm-handle-type part))
5964                                        "application/pgp-signature"))
5965                       (push part handles)))
5966                   (gnus-treat-article
5967                    nil (length (memq handle handles)) (length handles)
5968                    (mm-handle-media-type handle)))))))))))
5969
5970 (defun gnus-unbuttonized-mime-type-p (type)
5971   "Say whether TYPE is to be unbuttonized."
5972   (unless gnus-inhibit-mime-unbuttonizing
5973     (when (catch 'found
5974             (let ((types gnus-unbuttonized-mime-types))
5975               (while types
5976                 (when (string-match (pop types) type)
5977                   (throw 'found t)))))
5978       (not (catch 'found
5979              (let ((types gnus-buttonized-mime-types))
5980                (while types
5981                  (when (string-match (pop types) type)
5982                    (throw 'found t)))))))))
5983
5984 (defun gnus-article-insert-newline ()
5985   "Insert a newline, but mark it as undeletable."
5986   (gnus-put-text-property
5987    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5988
5989 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5990   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5991          (ihandles handles)
5992          (point (point))
5993          handle (inhibit-read-only t) from props begend not-pref)
5994     (save-window-excursion
5995       (save-restriction
5996         (when ibegend
5997           (narrow-to-region (car ibegend)
5998                             (or (cdr ibegend)
5999                                 (progn
6000                                   (goto-char (car ibegend))
6001                                   (forward-line 2)
6002                                   (point))))
6003           (delete-region (point-min) (point-max))
6004           (mm-remove-parts handles))
6005         (setq begend (list (point-marker)))
6006         ;; Do the toggle.
6007         (unless (setq not-pref (cadr (member preferred ihandles)))
6008           (setq not-pref (car ihandles)))
6009         (when (or ibegend
6010                   (not preferred)
6011                   (not (gnus-unbuttonized-mime-type-p
6012                         "multipart/alternative")))
6013           (gnus-add-text-properties
6014            (setq from (point))
6015            (progn
6016              (insert (format "%d.  " id))
6017              (point))
6018            `(gnus-callback
6019              (lambda (handles)
6020                (unless ,(not ibegend)
6021                  (setq gnus-article-mime-handle-alist
6022                        ',gnus-article-mime-handle-alist))
6023                (gnus-mime-display-alternative
6024                 ',ihandles ',not-pref ',begend ,id))
6025              keymap ,gnus-mime-button-map
6026              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6027              face ,gnus-article-button-face
6028              gnus-part ,id
6029              article-type multipart))
6030           (widget-convert-button 'link from (point)
6031                                  :action 'gnus-widget-press-button
6032                                  :button-keymap gnus-widget-button-keymap)
6033           ;; Do the handles
6034           (while (setq handle (pop handles))
6035             (gnus-add-text-properties
6036              (setq from (point))
6037              (progn
6038                (insert (format "(%c) %-18s"
6039                                (if (equal handle preferred) ?* ? )
6040                                (mm-handle-media-type handle)))
6041                (point))
6042              `(gnus-callback
6043                (lambda (handles)
6044                  (unless ,(not ibegend)
6045                    (setq gnus-article-mime-handle-alist
6046                          ',gnus-article-mime-handle-alist))
6047                  (gnus-mime-display-alternative
6048                   ',ihandles ',handle ',begend ,id))
6049                keymap ,gnus-mime-button-map
6050                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6051                face ,gnus-article-button-face
6052                gnus-part ,id
6053                gnus-data ,handle))
6054             (widget-convert-button 'link from (point)
6055                                    :action 'gnus-widget-press-button
6056                                    :button-keymap gnus-widget-button-keymap)
6057             (insert "  "))
6058           (insert "\n\n"))
6059         (when preferred
6060           (if (stringp (car preferred))
6061               (gnus-display-mime preferred)
6062             (let ((mail-parse-charset gnus-newsgroup-charset)
6063                   (mail-parse-ignored-charsets
6064                    (with-current-buffer gnus-summary-buffer
6065                      gnus-newsgroup-ignored-charsets)))
6066               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6067               ;; Do highlighting.
6068               (save-excursion
6069                 (save-restriction
6070                   (narrow-to-region (car begend) (point-max))
6071                   (gnus-treat-article
6072                    nil (length gnus-article-mime-handle-alist)
6073                    (gnus-article-mime-total-parts)
6074                    (mm-handle-media-type preferred))))))
6075           (goto-char (point-max))
6076           (setcdr begend (point-marker)))))
6077     (when ibegend
6078       (goto-char point))))
6079
6080 (defconst gnus-article-wash-status-strings
6081   (let ((alist '((cite "c" "Possible hidden citation text"
6082                        " " "All citation text visible")
6083                  (headers "h" "Hidden headers"
6084                           " " "All headers visible.")
6085                  (pgp "p" "Encrypted or signed message status hidden"
6086                       " " "No hidden encryption nor digital signature status")
6087                  (signature "s" "Signature has been hidden"
6088                             " " "Signature is visible")
6089                  (overstrike "o" "Overstrike (^H) characters applied"
6090                              " " "No overstrike characters applied")
6091                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6092                            " " "No /*_emphasis_*/ characters applied")))
6093         result)
6094     (dolist (entry alist result)
6095       (let ((key (nth 0 entry))
6096             (on (copy-sequence (nth 1 entry)))
6097             (on-help (nth 2 entry))
6098             (off (copy-sequence (nth 3 entry)))
6099             (off-help (nth 4 entry)))
6100         (put-text-property 0 1 'help-echo on-help on)
6101         (put-text-property 0 1 'help-echo off-help off)
6102         (push (list key on off) result))))
6103   "Alist of strings describing wash status in the mode line.
6104 Each entry has the form (KEY ON OF), where the KEY is a symbol
6105 representing the particular washing function, ON is the string to use
6106 in the article mode line when the washing function is active, and OFF
6107 is the string to use when it is inactive.")
6108
6109 (defun gnus-article-wash-status-entry (key value)
6110   (let ((entry (assoc key gnus-article-wash-status-strings)))
6111     (if value (nth 1 entry) (nth 2 entry))))
6112
6113 (defun gnus-article-wash-status ()
6114   "Return a string which display status of article washing."
6115   (with-current-buffer gnus-article-buffer
6116     (let ((cite (memq 'cite gnus-article-wash-types))
6117           (headers (memq 'headers gnus-article-wash-types))
6118           (boring (memq 'boring-headers gnus-article-wash-types))
6119           (pgp (memq 'pgp gnus-article-wash-types))
6120           (pem (memq 'pem gnus-article-wash-types))
6121           (signed (memq 'signed gnus-article-wash-types))
6122           (encrypted (memq 'encrypted gnus-article-wash-types))
6123           (signature (memq 'signature gnus-article-wash-types))
6124           (overstrike (memq 'overstrike gnus-article-wash-types))
6125           (emphasis (memq 'emphasis gnus-article-wash-types)))
6126       (concat
6127        (gnus-article-wash-status-entry 'cite cite)
6128        (gnus-article-wash-status-entry 'headers (or headers boring))
6129        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6130        (gnus-article-wash-status-entry 'signature signature)
6131        (gnus-article-wash-status-entry 'overstrike overstrike)
6132        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6133
6134 (defun gnus-add-wash-type (type)
6135   "Add a washing of TYPE to the current status."
6136   (add-to-list 'gnus-article-wash-types type))
6137
6138 (defun gnus-delete-wash-type (type)
6139   "Add a washing of TYPE to the current status."
6140   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6141
6142 (defun gnus-add-image (category image)
6143   "Add IMAGE of CATEGORY to the list of displayed images."
6144   (let ((entry (assq category gnus-article-image-alist)))
6145     (unless entry
6146       (setq entry (list category))
6147       (push entry gnus-article-image-alist))
6148     (nconc entry (list image))))
6149
6150 (defun gnus-delete-images (category)
6151   "Delete all images in CATEGORY."
6152   (let ((entry (assq category gnus-article-image-alist)))
6153     (dolist (image (cdr entry))
6154       (gnus-remove-image image category))
6155     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6156     (gnus-delete-wash-type category)))
6157
6158 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6159
6160 (defun gnus-article-maybe-hide-headers ()
6161   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6162 Provided for backwards compatibility."
6163   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6164                  (not (with-current-buffer gnus-summary-buffer
6165                         gnus-have-all-headers)))
6166              (not gnus-inhibit-hiding))
6167     (gnus-article-hide-headers)))
6168
6169 ;;; Article savers.
6170
6171 (defun gnus-output-to-file (file-name)
6172   "Append the current article to a file named FILE-NAME.
6173 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6174 text and used as the value of the coding cookie which is added to the
6175 top of a file.  Otherwise, this function saves a raw article without
6176 the coding cookie."
6177   (let* ((artbuf (current-buffer))
6178          (file-name-coding-system nnmail-pathname-coding-system)
6179          (coding gnus-article-save-coding-system)
6180          (coding-system-for-read (if coding
6181                                      nil ;; Rely on the coding cookie.
6182                                    mm-text-coding-system))
6183          (coding-system-for-write (or coding
6184                                       mm-text-coding-system-for-write
6185                                       mm-text-coding-system))
6186          (exists (file-exists-p file-name)))
6187     (with-temp-buffer
6188       (when exists
6189         (insert-file-contents file-name)
6190         (goto-char (point-min))
6191         ;; Remove the existing coding cookie.
6192         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6193           (delete-region (match-beginning 0) (match-end 0))))
6194       (goto-char (point-max))
6195       (insert-buffer-substring artbuf)
6196       ;; Append newline at end of the buffer as separator, and then
6197       ;; save it to file.
6198       (goto-char (point-max))
6199       (insert "\n")
6200       (when coding
6201         ;; If the coding system is not suitable to encode the text,
6202         ;; ask a user for a proper one.
6203         (when (fboundp 'select-safe-coding-system)
6204           (setq coding (coding-system-base
6205                         (save-window-excursion
6206                           (select-safe-coding-system (point-min) (point-max)
6207                                                      coding))))
6208           (setq coding-system-for-write
6209                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6210                     coding)))
6211         (goto-char (point-min))
6212         ;; Add the coding cookie.
6213         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6214                         coding-system-for-write)))
6215       (if exists
6216           (progn
6217             (write-region (point-min) (point-max) file-name nil 'no-message)
6218             (message "Appended to %s" file-name))
6219         (write-region (point-min) (point-max) file-name))))
6220   t)
6221
6222 (defun gnus-narrow-to-page (&optional arg)
6223   "Narrow the article buffer to a page.
6224 If given a numerical ARG, move forward ARG pages."
6225   (interactive "P")
6226   (setq arg (if arg (prefix-numeric-value arg) 0))
6227   (with-current-buffer gnus-article-buffer
6228     (widen)
6229     ;; Remove any old next/prev buttons.
6230     (when (gnus-visual-p 'page-marker)
6231       (let ((inhibit-read-only t))
6232         (gnus-remove-text-with-property 'gnus-prev)
6233         (gnus-remove-text-with-property 'gnus-next)))
6234     (let (st nd pt)
6235       (when (save-excursion
6236               (cond ((< arg 0)
6237                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6238                          (prog1
6239                              (setq nd (match-beginning 0)
6240                                    pt nd)
6241                            (when (re-search-backward page-delimiter nil t)
6242                              (setq st (match-end 0))))
6243                        (when (re-search-forward page-delimiter nil t)
6244                          (setq nd (match-beginning 0)
6245                                pt (point-min)))))
6246                     ((> arg 0)
6247                      (if (re-search-forward page-delimiter nil 'move arg)
6248                          (prog1
6249                              (setq st (match-end 0)
6250                                    pt st)
6251                            (when (re-search-forward page-delimiter nil t)
6252                              (setq nd (match-beginning 0))))
6253                        (when (re-search-backward page-delimiter nil t)
6254                          (setq st (match-end 0)
6255                                pt (point-max)))))
6256                     (t
6257                      (when (re-search-backward page-delimiter nil t)
6258                        (goto-char (setq st (match-end 0))))
6259                      (when (re-search-forward page-delimiter nil t)
6260                        (setq nd (match-beginning 0)))
6261                      (or st nd))))
6262         (setq gnus-page-broken t)
6263         (when pt (goto-char pt))
6264         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6265         (when (gnus-visual-p 'page-marker)
6266           (save-excursion
6267             (when nd
6268               (goto-char nd)
6269               (gnus-insert-next-page-button))
6270             (when st
6271               (goto-char st)
6272               (gnus-insert-prev-page-button))))))))
6273
6274 ;; Article mode commands
6275
6276 (defun gnus-article-goto-next-page ()
6277   "Show the next page of the article."
6278   (interactive)
6279   (when (gnus-article-next-page)
6280     (goto-char (point-min))
6281     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6282
6283
6284 (defun gnus-article-goto-prev-page ()
6285   "Show the previous page of the article."
6286   (interactive)
6287   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6288       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6289     (gnus-article-prev-page nil)))
6290
6291 ;; This is cleaner but currently breaks `gnus-pick-mode':
6292 ;;
6293 ;; (defun gnus-article-goto-next-page ()
6294 ;;   "Show the next page of the article."
6295 ;;   (interactive)
6296 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6297 ;;     (gnus-summary-next-page)))
6298 ;;
6299 ;; (defun gnus-article-goto-prev-page ()
6300 ;;   "Show the next page of the article."
6301 ;;   (interactive)
6302 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6303 ;;     (gnus-summary-prev-page)))
6304
6305 (defun gnus-article-next-page (&optional lines)
6306   "Show the next page of the current article.
6307 If end of article, return non-nil.  Otherwise return nil.
6308 Argument LINES specifies lines to be scrolled up."
6309   (interactive "p")
6310   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6311   (if (and (not (and gnus-article-over-scroll
6312                      (> (count-lines (window-start) (point-max))
6313                         (if (featurep 'xemacs)
6314                             (or lines (1- (window-height)))
6315                           (+ (or lines (1- (window-height))) scroll-margin)))))
6316            (save-excursion
6317              (end-of-line)
6318              (and (pos-visible-in-window-p)     ;Not continuation line.
6319                   (>= (point) (point-max)))))
6320       ;; Nothing in this page.
6321       (if (or (not gnus-page-broken)
6322               (save-excursion
6323                 (save-restriction
6324                   (widen)
6325                   (forward-line)
6326                   (eobp)))) ;Real end-of-buffer?
6327           (progn
6328             (when gnus-article-over-scroll
6329               (gnus-article-next-page-1 lines))
6330             t)                  ;Nothing more.
6331         (gnus-narrow-to-page 1)         ;Go to next page.
6332         nil)
6333     ;; More in this page.
6334     (gnus-article-next-page-1 lines)
6335     nil))
6336
6337 (defun gnus-article-beginning-of-window ()
6338   "Move point to the beginning of the window.
6339 In Emacs, the point is placed at the line number which `scroll-margin'
6340 specifies."
6341   (if (featurep 'xemacs)
6342       (move-to-window-line 0)
6343     ;; There is an obscure bug in Emacs that makes it impossible to
6344     ;; scroll past big pictures in the article buffer.  Try to fix
6345     ;; this by adding a sanity check by counting the lines visible.
6346     (when (> (count-lines (window-start) (window-end)) 30)
6347       (move-to-window-line
6348        (min (max 0 scroll-margin)
6349             (max 1 (- (window-height)
6350                       (if mode-line-format 1 0)
6351                       (if header-line-format 1 0)
6352                       2)))))))
6353
6354 (defun gnus-article-next-page-1 (lines)
6355   (condition-case ()
6356       (let ((scroll-in-place nil))
6357         (scroll-up lines))
6358     (end-of-buffer
6359      ;; Long lines may cause an end-of-buffer error.
6360      (goto-char (point-max))))
6361   (gnus-article-beginning-of-window))
6362
6363 (defun gnus-article-prev-page (&optional lines)
6364   "Show previous page of current article.
6365 Argument LINES specifies lines to be scrolled down."
6366   (interactive "p")
6367   (move-to-window-line 0)
6368   (if (and gnus-page-broken
6369            (bobp)
6370            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6371       (progn
6372         (gnus-narrow-to-page -1)        ;Go to previous page.
6373         (goto-char (point-max))
6374         (recenter (if gnus-article-over-scroll
6375                       (if lines
6376                           (max (if (featurep 'xemacs)
6377                                    lines
6378                                  (+ lines scroll-margin))
6379                                3)
6380                         (- (window-height) 2))
6381                     -1)))
6382     (prog1
6383         (condition-case ()
6384             (let ((scroll-in-place nil))
6385               (scroll-down lines))
6386           (beginning-of-buffer
6387            (goto-char (point-min))))
6388       (gnus-article-beginning-of-window))))
6389
6390 (defun gnus-article-only-boring-p ()
6391   "Decide whether there is only boring text remaining in the article.
6392 Something \"interesting\" is a word of at least two letters that does
6393 not have a face in `gnus-article-boring-faces'."
6394   (when (and gnus-article-skip-boring
6395              (boundp 'gnus-article-boring-faces)
6396              (symbol-value 'gnus-article-boring-faces))
6397     (save-excursion
6398       (let ((inhibit-point-motion-hooks t))
6399         (catch 'only-boring
6400           (while (re-search-forward "\\b\\w\\w" nil t)
6401             (forward-char -1)
6402             (when (not (gnus-intersection
6403                         (gnus-faces-at (point))
6404                         (symbol-value 'gnus-article-boring-faces)))
6405               (throw 'only-boring nil)))
6406           (throw 'only-boring t))))))
6407
6408 (defun gnus-article-refer-article ()
6409   "Read article specified by message-id around point."
6410   (interactive)
6411   (save-excursion
6412     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6413     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6414     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6415         (let ((msg-id (concat "<" (match-string 0) ">")))
6416           (set-buffer gnus-summary-buffer)
6417           (gnus-summary-refer-article msg-id))
6418       (error "No references around point"))))
6419
6420 (defun gnus-article-show-summary ()
6421   "Reconfigure windows to show summary buffer."
6422   (interactive)
6423   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6424       (error "There is no summary buffer for this article buffer")
6425     (gnus-article-set-globals)
6426     (gnus-configure-windows 'article)
6427     (gnus-summary-goto-subject gnus-current-article)
6428     (gnus-summary-position-point)))
6429
6430 (defun gnus-article-describe-briefly ()
6431   "Describe article mode commands briefly."
6432   (interactive)
6433   (gnus-message 6 "%s" (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-goto-next-page]:Next page      \\[gnus-article-goto-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
6434
6435 (defun gnus-article-check-buffer ()
6436   "Beep if not in an article buffer."
6437   (unless (equal major-mode 'gnus-article-mode)
6438     (error "Command invoked outside of a Gnus article buffer")))
6439
6440 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6441   "Read a summary buffer key sequence and execute it from the article buffer."
6442   (interactive "P")
6443   (gnus-article-check-buffer)
6444   (let ((nosaves
6445          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6446            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6447            "=" "^" "\M-^" "|"))
6448         (nosave-but-article
6449          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6450            "An" "Ap" [?A (meta return)] [?A delete]))
6451         (nosave-in-article
6452          '("AS" "\C-d"))
6453         (up-to-top
6454          '("n" "Gn" "p" "Gp"))
6455         keys new-sum-point)
6456     (with-current-buffer gnus-article-current-summary
6457       (let (gnus-pick-mode)
6458         (setq unread-command-events (nconc unread-command-events
6459                                            (list (or key last-command-event)))
6460               keys (if (featurep 'xemacs)
6461                        (events-to-keys (read-key-sequence nil t))
6462                      (read-key-sequence nil t)))))
6463
6464     (message "")
6465
6466     (cond
6467      ((eq (aref keys (1- (length keys))) ?\C-h)
6468       (gnus-article-describe-bindings (substring keys 0 -1)))
6469      ((or (member keys nosaves)
6470           (member keys nosave-but-article)
6471           (member keys nosave-in-article))
6472       (let (func)
6473         (save-window-excursion
6474           (pop-to-buffer gnus-article-current-summary)
6475           ;; We disable the pick minor mode commands.
6476           (let (gnus-pick-mode)
6477             (setq func (key-binding keys t))))
6478         (if (or (not func)
6479                 (numberp func))
6480             (ding)
6481           (unless (member keys nosave-in-article)
6482             (set-buffer gnus-article-current-summary))
6483           (when (get func 'disabled)
6484             (error "Function %s disabled" func))
6485           (call-interactively func)
6486           (setq new-sum-point (point)))
6487         (when (member keys nosave-but-article)
6488           (pop-to-buffer gnus-article-buffer))))
6489      (t
6490       ;; These commands should restore window configuration.
6491       (let ((obuf (current-buffer))
6492             (owin (current-window-configuration))
6493             win func in-buffer selected new-sum-start new-sum-hscroll err)
6494         (cond (not-restore-window
6495                (pop-to-buffer gnus-article-current-summary)
6496                (setq win (selected-window)))
6497               ((setq win (get-buffer-window gnus-article-current-summary))
6498                (select-window win))
6499               (t
6500                (let ((summary-buffer gnus-article-current-summary))
6501                  (gnus-configure-windows 'article)
6502                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6503                    (let ((gnus-buffer-configuration
6504                           '((article ((vertical 1.0
6505                                                 (summary 0.25 point)
6506                                                 (article 1.0)))))))
6507                      (gnus-configure-windows 'article))
6508                    (setq win (get-buffer-window summary-buffer 'visible)))
6509                  (gnus-select-frame-set-input-focus (window-frame win))
6510                  (select-window win))))
6511         (setq in-buffer (current-buffer))
6512         ;; We disable the pick minor mode commands.
6513         (setq func (let (gnus-pick-mode)
6514                      (key-binding keys t)))
6515         (when (get func 'disabled)
6516           (error "Function %s disabled" func))
6517         (if (and func
6518                  (functionp func)
6519                  (condition-case code
6520                      (progn
6521                        (call-interactively func)
6522                        t)
6523                    (error
6524                     (setq err code)
6525                     nil)))
6526             (progn
6527               (when (eq win (selected-window))
6528                 (setq new-sum-point (point)
6529                       new-sum-start (window-start win)
6530                       new-sum-hscroll (window-hscroll win)))
6531               (when (or (eq in-buffer (current-buffer))
6532                         (when (eq obuf (current-buffer))
6533                           (set-buffer in-buffer)
6534                           t))
6535                 (setq selected (gnus-summary-select-article))
6536                 (set-buffer obuf)
6537                 (unless not-restore-window
6538                   (set-window-configuration owin))
6539                 (when (and (eq selected 'old)
6540                            new-sum-point)
6541                   (set-window-start (get-buffer-window (current-buffer))
6542                                     1)
6543                   (set-window-point (get-buffer-window (current-buffer))
6544                                     (if (article-goto-body)
6545                                         (1- (point))
6546                                       (point))))
6547                 (when (and (not not-restore-window)
6548                            new-sum-point
6549                            (window-live-p win)
6550                            (with-current-buffer (window-buffer win)
6551                              (eq major-mode 'gnus-summary-mode)))
6552                   (set-window-point win new-sum-point)
6553                   (set-window-start win new-sum-start)
6554                   (set-window-hscroll win new-sum-hscroll))))
6555           (set-window-configuration owin)
6556           (if err
6557               (signal (car err) (cdr err))
6558             (ding))))))))
6559
6560 (defun gnus-article-read-summary-send-keys ()
6561   (interactive)
6562   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6563     (gnus-article-read-summary-keys)))
6564
6565 (defun gnus-article-describe-key (key)
6566   "Display documentation of the function invoked by KEY.
6567 KEY is a string or a vector."
6568   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6569                        (read-key-sequence "Describe key: "))))
6570   (gnus-article-check-buffer)
6571   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6572                                   gnus-article-read-summary-send-keys))
6573       (with-current-buffer gnus-article-current-summary
6574         (setq unread-command-events
6575               (if (featurep 'xemacs)
6576                   (append key nil)
6577                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6578                                         (list 'meta (- x 128))
6579                                       x))
6580                         key)))
6581         (let ((cursor-in-echo-area t)
6582               gnus-pick-mode)
6583           (describe-key (read-key-sequence nil t))))
6584     (describe-key key)))
6585
6586 (defun gnus-article-describe-key-briefly (key &optional insert)
6587   "Display documentation of the function invoked by KEY.
6588 KEY is a string or a vector."
6589   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6590                        (read-key-sequence "Describe key: "))
6591                      current-prefix-arg))
6592   (gnus-article-check-buffer)
6593   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6594                                   gnus-article-read-summary-send-keys))
6595       (with-current-buffer gnus-article-current-summary
6596         (setq unread-command-events
6597               (if (featurep 'xemacs)
6598                   (append key nil)
6599                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6600                                         (list 'meta (- x 128))
6601                                       x))
6602                         key)))
6603         (let ((cursor-in-echo-area t)
6604               gnus-pick-mode)
6605           (describe-key-briefly (read-key-sequence nil t) insert)))
6606     (describe-key-briefly key insert)))
6607
6608 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6609 (defvar gnus-agent-summary-mode)
6610 (defvar gnus-draft-mode)
6611 ;; Calling help-buffer will autoload help-mode.
6612 (defvar help-xref-stack-item)
6613 ;; Emacs 22 doesn't load it in the batch mode.
6614 (eval-when-compile
6615   (autoload 'help-buffer "help-mode"))
6616
6617 (defun gnus-article-describe-bindings (&optional prefix)
6618   "Show a list of all defined keys, and their definitions.
6619 The optional argument PREFIX, if non-nil, should be a key sequence;
6620 then we display only bindings that start with that prefix."
6621   (interactive)
6622   (gnus-article-check-buffer)
6623   (let ((keymap (copy-keymap gnus-article-mode-map))
6624         (map (copy-keymap gnus-article-send-map))
6625         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6626         parent agent draft)
6627     (define-key keymap "S" map)
6628     (define-key map [t] nil)
6629     (with-current-buffer gnus-article-current-summary
6630       (set-keymap-parent
6631        keymap
6632        (if (setq parent (keymap-parent gnus-article-mode-map))
6633            (prog1
6634                (setq parent (copy-keymap parent))
6635              (set-keymap-parent parent (current-local-map)))
6636          (current-local-map)))
6637       (set-keymap-parent map (key-binding "S"))
6638       (let (key def gnus-pick-mode)
6639         (while sumkeys
6640           (setq key (pop sumkeys))
6641           (cond ((and (vectorp key) (= (length key) 1)
6642                       (consp (setq def (aref key 0)))
6643                       (numberp (car def)) (numberp (cdr def)))
6644                  (when (< (max (car def) (cdr def)) 128)
6645                    (setq sumkeys
6646                          (append (mapcar
6647                                   #'vector
6648                                   (nreverse (gnus-uncompress-range def)))
6649                                  sumkeys))))
6650                 ((setq def (key-binding key))
6651                  (unless (eq def 'undefined)
6652                    (define-key keymap key def))))))
6653       (when (boundp 'gnus-agent-summary-mode)
6654         (setq agent gnus-agent-summary-mode))
6655       (when (boundp 'gnus-draft-mode)
6656         (setq draft gnus-draft-mode)))
6657     (with-temp-buffer
6658       (use-local-map keymap)
6659       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6660       (set (make-local-variable 'gnus-draft-mode) draft)
6661       (describe-bindings prefix))
6662     (let ((item `((lambda (prefix)
6663                     (with-current-buffer ,(current-buffer)
6664                       (gnus-article-describe-bindings prefix)))
6665                   ,prefix)))
6666       (with-current-buffer (let (help-xref-following) (help-buffer))
6667         (setq help-xref-stack-item item)))))
6668
6669 (defun gnus-article-reply-with-original (&optional wide)
6670   "Start composing a reply mail to the current message.
6671 The text in the region will be yanked.  If the region isn't active,
6672 the entire article will be yanked."
6673   (interactive)
6674   (let ((article (cdr gnus-article-current))
6675         contents)
6676     (if (not (gnus-region-active-p))
6677         (with-current-buffer gnus-summary-buffer
6678           (gnus-summary-reply (list (list article)) wide))
6679       (setq contents (buffer-substring (point) (mark t)))
6680       ;; Deactivate active regions.
6681       (when (and (boundp 'transient-mark-mode)
6682                  transient-mark-mode)
6683         (setq mark-active nil))
6684       (with-current-buffer gnus-summary-buffer
6685         (gnus-summary-reply
6686          (list (list article contents)) wide)))))
6687
6688 (defun gnus-article-wide-reply-with-original ()
6689   "Start composing a wide reply mail to the current message.
6690 The text in the region will be yanked.  If the region isn't active,
6691 the entire article will be yanked."
6692   (interactive)
6693   (gnus-article-reply-with-original t))
6694
6695 (defun gnus-article-followup-with-original ()
6696   "Compose a followup to the current article.
6697 The text in the region will be yanked.  If the region isn't active,
6698 the entire article will be yanked."
6699   (interactive)
6700   (let ((article (cdr gnus-article-current))
6701         contents)
6702       (if (not (gnus-region-active-p))
6703           (with-current-buffer gnus-summary-buffer
6704             (gnus-summary-followup (list (list article))))
6705         (setq contents (buffer-substring (point) (mark t)))
6706         ;; Deactivate active regions.
6707         (when (and (boundp 'transient-mark-mode)
6708                    transient-mark-mode)
6709           (setq mark-active nil))
6710         (with-current-buffer gnus-summary-buffer
6711           (gnus-summary-followup
6712            (list (list article contents)))))))
6713
6714 (defun gnus-article-hide (&optional arg force)
6715   "Hide all the gruft in the current article.
6716 This means that signatures, cited text and (some) headers will be
6717 hidden.
6718 If given a prefix, show the hidden text instead."
6719   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6720   (gnus-article-hide-headers arg)
6721   (gnus-article-hide-list-identifiers arg)
6722   (gnus-article-hide-citation-maybe arg force)
6723   (gnus-article-hide-signature arg))
6724
6725 (defun gnus-article-maybe-highlight ()
6726   "Do some article highlighting if article highlighting is requested."
6727   (when (gnus-visual-p 'article-highlight 'highlight)
6728     (gnus-article-highlight-some)))
6729
6730 (defun gnus-check-group-server ()
6731   ;; Make sure the connection to the server is alive.
6732   (unless (gnus-server-opened
6733            (gnus-find-method-for-group gnus-newsgroup-name))
6734     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6735     (gnus-request-group gnus-newsgroup-name t)))
6736
6737 (eval-when-compile
6738   (autoload 'nneething-get-file-name "nneething"))
6739
6740 (defun gnus-request-article-this-buffer (article group)
6741   "Get an article and insert it into this buffer."
6742   (let (do-update-line sparse-header)
6743     (prog1
6744         (save-excursion
6745           (erase-buffer)
6746           (gnus-kill-all-overlays)
6747           (setq group (or group gnus-newsgroup-name))
6748
6749           ;; Using `gnus-request-article' directly will insert the article into
6750           ;; `nntp-server-buffer' - so we'll save some time by not having to
6751           ;; copy it from the server buffer into the article buffer.
6752
6753           ;; We only request an article by message-id when we do not have the
6754           ;; headers for it, so we'll have to get those.
6755           (when (stringp article)
6756             (gnus-read-header article))
6757
6758           ;; If the article number is negative, that means that this article
6759           ;; doesn't belong in this newsgroup (possibly), so we find its
6760           ;; message-id and request it by id instead of number.
6761           (when (and (numberp article)
6762                      gnus-summary-buffer
6763                      (get-buffer gnus-summary-buffer)
6764                      (gnus-buffer-exists-p gnus-summary-buffer))
6765             (with-current-buffer gnus-summary-buffer
6766               (let ((header (gnus-summary-article-header article)))
6767                 (when (< article 0)
6768                   (cond
6769                    ((memq article gnus-newsgroup-sparse)
6770                     ;; This is a sparse gap article.
6771                     (setq do-update-line article)
6772                     (setq article (mail-header-id header))
6773                     (setq sparse-header (gnus-read-header article))
6774                     (setq gnus-newsgroup-sparse
6775                           (delq article gnus-newsgroup-sparse)))
6776                    ((vectorp header)
6777                     ;; It's a real article.
6778                     (setq article (mail-header-id header)))
6779                    (t
6780                     ;; It is an extracted pseudo-article.
6781                     (setq article 'pseudo)
6782                     (gnus-request-pseudo-article header))))
6783
6784                 (let ((method (gnus-find-method-for-group
6785                                gnus-newsgroup-name)))
6786                   (when (and (eq (car method) 'nneething)
6787                              (vectorp header))
6788                     (let ((dir (nneething-get-file-name
6789                                 (mail-header-id header))))
6790                       (when (and (stringp dir)
6791                                  (file-directory-p dir))
6792                         (setq article 'nneething)
6793                         (gnus-group-enter-directory dir))))))))
6794
6795           (cond
6796            ;; Refuse to select canceled articles.
6797            ((and (numberp article)
6798                  gnus-summary-buffer
6799                  (get-buffer gnus-summary-buffer)
6800                  (gnus-buffer-exists-p gnus-summary-buffer)
6801                  (eq (cdr (with-current-buffer gnus-summary-buffer
6802                             (assq article gnus-newsgroup-reads)))
6803                      gnus-canceled-mark))
6804             nil)
6805            ;; We first check `gnus-original-article-buffer'.
6806            ((and (get-buffer gnus-original-article-buffer)
6807                  (numberp article)
6808                  (with-current-buffer gnus-original-article-buffer
6809                    (and (equal (car gnus-original-article) group)
6810                         (eq (cdr gnus-original-article) article))))
6811             ;; `insert-buffer-substring' would incorrectly use the
6812             ;; equivalent of string-make-multibyte which amount to decoding
6813             ;; with locale-coding-system, causing failure of
6814             ;; subsequent decoding.
6815             (insert (mm-string-to-multibyte
6816                      (with-current-buffer gnus-original-article-buffer
6817                        (buffer-substring (point-min) (point-max)))))
6818             'article)
6819            ;; Check the backlog.
6820            ((and gnus-keep-backlog
6821                  (gnus-backlog-request-article group article (current-buffer)))
6822             'article)
6823            ;; Check asynchronous pre-fetch.
6824            ((gnus-async-request-fetched-article group article (current-buffer))
6825             (gnus-async-prefetch-next group article gnus-summary-buffer)
6826             (when (and (numberp article) gnus-keep-backlog)
6827               (gnus-backlog-enter-article group article (current-buffer)))
6828             'article)
6829            ;; Check the cache.
6830            ((and gnus-use-cache
6831                  (numberp article)
6832                  (gnus-cache-request-article article group))
6833             'article)
6834            ;; Check the agent cache.
6835            ((gnus-agent-request-article article group)
6836             'article)
6837            ;; Get the article and put into the article buffer.
6838            ((or (stringp article)
6839                 (numberp article))
6840             (let ((gnus-override-method gnus-override-method)
6841                   (methods (and (stringp article)
6842                                 gnus-refer-article-method))
6843                   (backend (car (gnus-find-method-for-group
6844                                  gnus-newsgroup-name)))
6845                   result
6846                   (inhibit-read-only t))
6847               (if (or (not (listp methods))
6848                       (and (symbolp (car methods))
6849                            (assq (car methods) nnoo-definition-alist)))
6850                   (setq methods (list methods)))
6851               (when (and (null gnus-override-method)
6852                          methods)
6853                 (setq gnus-override-method (pop methods)))
6854               (while (not result)
6855                 (when (eq gnus-override-method 'current)
6856                   (setq gnus-override-method
6857                         (with-current-buffer gnus-summary-buffer
6858                           gnus-current-select-method)))
6859                 (erase-buffer)
6860                 (gnus-kill-all-overlays)
6861                 (let ((gnus-newsgroup-name group))
6862                   (gnus-check-group-server))
6863                 (cond
6864                  ((gnus-request-article article group (current-buffer))
6865                   (when (numberp article)
6866                     (gnus-async-prefetch-next group article
6867                                               gnus-summary-buffer)
6868                     (when gnus-keep-backlog
6869                       (gnus-backlog-enter-article
6870                        group article (current-buffer))))
6871                   (setq result 'article))
6872                  (methods
6873                   (setq gnus-override-method (pop methods)))
6874                  ((not (string-match "^400 "
6875                                      (nnheader-get-report backend)))
6876                   ;; If we get 400 server disconnect, reconnect and
6877                   ;; retry; otherwise, assume the article has expired.
6878                   (setq result 'done))))
6879               (and (eq result 'article) 'article)))
6880            ;; It was a pseudo.
6881            (t article)))
6882
6883       ;; Associate this article with the current summary buffer.
6884       (setq gnus-article-current-summary gnus-summary-buffer)
6885
6886       ;; Take the article from the original article buffer
6887       ;; and place it in the buffer it's supposed to be in.
6888       (when (and (get-buffer gnus-article-buffer)
6889                  (equal (buffer-name (current-buffer))
6890                         (buffer-name (get-buffer gnus-article-buffer))))
6891         (save-excursion
6892           (if (get-buffer gnus-original-article-buffer)
6893               (set-buffer gnus-original-article-buffer)
6894             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6895             (buffer-disable-undo)
6896             (setq major-mode 'gnus-original-article-mode)
6897             (setq buffer-read-only t))
6898           (let ((inhibit-read-only t))
6899             (erase-buffer)
6900             (insert-buffer-substring gnus-article-buffer))
6901           (setq gnus-original-article (cons group article)))
6902
6903         ;; Decode charsets.
6904         (run-hooks 'gnus-article-decode-hook)
6905         ;; Mark article as decoded or not.
6906         (setq gnus-article-decoded-p gnus-article-decode-hook))
6907
6908       ;; Update sparse articles.
6909       (when (and do-update-line
6910                  (or (numberp article)
6911                      (stringp article)))
6912         (let ((buf (current-buffer)))
6913           (set-buffer gnus-summary-buffer)
6914           (gnus-summary-update-article do-update-line sparse-header)
6915           (gnus-summary-goto-subject do-update-line nil t)
6916           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6917                             (point))
6918           (set-buffer buf))))))
6919
6920 (defun gnus-block-private-groups (group)
6921   (if (gnus-news-group-p group)
6922       ;; Block nothing in news groups.
6923       nil
6924     ;; Block everything anywhere else.
6925     "."))
6926
6927 (defun gnus-blocked-images ()
6928   (if (functionp gnus-blocked-images)
6929       (funcall gnus-blocked-images gnus-newsgroup-name)
6930     gnus-blocked-images))
6931
6932 ;;;
6933 ;;; Article editing
6934 ;;;
6935
6936 (defcustom gnus-article-edit-mode-hook nil
6937   "Hook run in article edit mode buffers."
6938   :group 'gnus-article-various
6939   :type 'hook)
6940
6941 (defvar gnus-article-edit-done-function nil)
6942
6943 (defvar gnus-article-edit-mode-map nil)
6944
6945 ;; Should we be using derived.el for this?
6946 (unless gnus-article-edit-mode-map
6947   (setq gnus-article-edit-mode-map (make-keymap))
6948   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6949
6950   (gnus-define-keys gnus-article-edit-mode-map
6951     "\C-c?"    describe-mode
6952     "\C-c\C-c" gnus-article-edit-done
6953     "\C-c\C-k" gnus-article-edit-exit
6954     "\C-c\C-f\C-t" message-goto-to
6955     "\C-c\C-f\C-o" message-goto-from
6956     "\C-c\C-f\C-b" message-goto-bcc
6957     ;;"\C-c\C-f\C-w" message-goto-fcc
6958     "\C-c\C-f\C-c" message-goto-cc
6959     "\C-c\C-f\C-s" message-goto-subject
6960     "\C-c\C-f\C-r" message-goto-reply-to
6961     "\C-c\C-f\C-n" message-goto-newsgroups
6962     "\C-c\C-f\C-d" message-goto-distribution
6963     "\C-c\C-f\C-f" message-goto-followup-to
6964     "\C-c\C-f\C-m" message-goto-mail-followup-to
6965     "\C-c\C-f\C-k" message-goto-keywords
6966     "\C-c\C-f\C-u" message-goto-summary
6967     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6968     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6969     "\C-c\C-b" message-goto-body
6970     "\C-c\C-i" message-goto-signature
6971
6972     "\C-c\C-t" message-insert-to
6973     "\C-c\C-n" message-insert-newsgroups
6974     "\C-c\C-o" message-sort-headers
6975     "\C-c\C-e" message-elide-region
6976     "\C-c\C-v" message-delete-not-region
6977     "\C-c\C-z" message-kill-to-signature
6978     "\M-\r" message-newline-and-reformat
6979     "\C-c\C-a" mml-attach-file
6980     "\C-a" message-beginning-of-line
6981     "\t" message-tab
6982     "\M-;" comment-region)
6983
6984   (gnus-define-keys (gnus-article-edit-wash-map
6985                      "\C-c\C-w" gnus-article-edit-mode-map)
6986     "f" gnus-article-edit-full-stops))
6987
6988 (easy-menu-define
6989   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6990   '("Field"
6991     ["Fetch To" message-insert-to t]
6992     ["Fetch Newsgroups" message-insert-newsgroups t]
6993     "----"
6994     ["To" message-goto-to t]
6995     ["From" message-goto-from t]
6996     ["Subject" message-goto-subject t]
6997     ["Cc" message-goto-cc t]
6998     ["Reply-To" message-goto-reply-to t]
6999     ["Summary" message-goto-summary t]
7000     ["Keywords" message-goto-keywords t]
7001     ["Newsgroups" message-goto-newsgroups t]
7002     ["Followup-To" message-goto-followup-to t]
7003     ["Mail-Followup-To" message-goto-mail-followup-to t]
7004     ["Distribution" message-goto-distribution t]
7005     ["Body" message-goto-body t]
7006     ["Signature" message-goto-signature t]))
7007
7008 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
7009   "Major mode for editing articles.
7010 This is an extended text-mode.
7011
7012 \\{gnus-article-edit-mode-map}"
7013   (make-local-variable 'gnus-article-edit-done-function)
7014   (make-local-variable 'gnus-prev-winconf)
7015   (set (make-local-variable 'font-lock-defaults)
7016        '(message-font-lock-keywords t))
7017   (set (make-local-variable 'mail-header-separator) "")
7018   (set (make-local-variable 'gnus-article-edit-mode) t)
7019   (easy-menu-add message-mode-field-menu message-mode-map)
7020   (mml-mode)
7021   (setq buffer-read-only nil)
7022   (buffer-enable-undo)
7023   (widen))
7024
7025 (defun gnus-article-edit (&optional force)
7026   "Edit the current article.
7027 This will have permanent effect only in mail groups.
7028 If FORCE is non-nil, allow editing of articles even in read-only
7029 groups."
7030   (interactive "P")
7031   (when (and (not force)
7032              (gnus-group-read-only-p))
7033     (error "The current newsgroup does not support article editing"))
7034   (gnus-article-date-original)
7035   (gnus-article-edit-article
7036    'ignore
7037    `(lambda (no-highlight)
7038       'ignore
7039       (gnus-summary-edit-article-done
7040        ,(or (mail-header-references gnus-current-headers) "")
7041        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7042
7043 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7044   "Start editing the contents of the current article buffer."
7045   (let ((winconf (current-window-configuration)))
7046     (set-buffer gnus-article-buffer)
7047     (let ((message-auto-save-directory
7048            ;; Don't associate the article buffer with a draft file.
7049            nil))
7050       (gnus-article-edit-mode))
7051     (funcall start-func)
7052     (set-buffer-modified-p nil)
7053     (gnus-configure-windows 'edit-article)
7054     (setq gnus-article-edit-done-function exit-func)
7055     (setq gnus-prev-winconf winconf)
7056     (unless quiet
7057       (gnus-message 6 "C-c C-c to end edits"))))
7058
7059 (defun gnus-article-edit-done (&optional arg)
7060   "Update the article edits and exit."
7061   (interactive "P")
7062   (let ((func gnus-article-edit-done-function)
7063         (buf (current-buffer))
7064         (start (window-start))
7065         (p (point))
7066         (winconf gnus-prev-winconf))
7067     (widen) ;; Widen it in case that users narrowed the buffer.
7068     (funcall func arg)
7069     (set-buffer buf)
7070     ;; The cache and backlog have to be flushed somewhat.
7071     (when gnus-keep-backlog
7072       (gnus-backlog-remove-article
7073        (car gnus-article-current) (cdr gnus-article-current)))
7074     ;; Flush original article as well.
7075     (gnus-flush-original-article-buffer)
7076     (when gnus-use-cache
7077       (gnus-cache-update-article
7078        (car gnus-article-current) (cdr gnus-article-current)))
7079     ;; We remove all text props from the article buffer.
7080     (kill-all-local-variables)
7081     (set-text-properties (point-min) (point-max) nil)
7082     (gnus-article-mode)
7083     (set-window-configuration winconf)
7084     (set-buffer buf)
7085     (set-window-start (get-buffer-window buf) start)
7086     (set-window-point (get-buffer-window buf) (point)))
7087   (gnus-summary-show-article))
7088
7089 (defun gnus-flush-original-article-buffer ()
7090   (when (get-buffer gnus-original-article-buffer)
7091     (with-current-buffer gnus-original-article-buffer
7092       (setq gnus-original-article nil))))
7093
7094 (defun gnus-article-edit-exit ()
7095   "Exit the article editing without updating."
7096   (interactive)
7097   (when (or (not (buffer-modified-p))
7098             (yes-or-no-p "Article modified; kill anyway? "))
7099     (let ((curbuf (current-buffer))
7100           (p (point))
7101           (window-start (window-start)))
7102       (erase-buffer)
7103       (if (gnus-buffer-live-p gnus-original-article-buffer)
7104           (insert-buffer-substring gnus-original-article-buffer))
7105       (let ((winconf gnus-prev-winconf))
7106         (kill-all-local-variables)
7107         (gnus-article-mode)
7108         (set-window-configuration winconf)
7109         ;; Tippy-toe some to make sure that point remains where it was.
7110         (save-current-buffer
7111           (set-buffer curbuf)
7112           (set-window-start (get-buffer-window (current-buffer)) window-start)
7113           (goto-char p))))
7114     (gnus-summary-show-article)))
7115
7116 (defun gnus-article-edit-full-stops ()
7117   "Interactively repair spacing at end of sentences."
7118   (interactive)
7119   (save-excursion
7120     (goto-char (point-min))
7121     (search-forward-regexp "^$" nil t)
7122     (let ((case-fold-search nil))
7123       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7124
7125 ;;;
7126 ;;; Article highlights
7127 ;;;
7128
7129 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7130
7131 ;;; Internal Variables:
7132
7133 (defcustom gnus-button-url-regexp
7134   (concat
7135    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7136    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7137    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7138    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7139        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7140              (punct "!?:;.,"))
7141          (concat
7142           "\\(?:"
7143           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7144           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7145           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7146           "\\|"
7147           "[" chars punct     "]+" "[" chars "]"
7148           "\\)"))
7149      (concat ;; XEmacs 21.4 doesn't support POSIX.
7150       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7151       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7152    "\\)")
7153   "Regular expression that matches URLs."
7154   :group 'gnus-article-buttons
7155   :type 'regexp)
7156
7157 (defcustom gnus-button-valid-fqdn-regexp
7158   message-valid-fqdn-regexp
7159   "Regular expression that matches a valid FQDN."
7160   :version "22.1"
7161   :group 'gnus-article-buttons
7162   :type 'regexp)
7163
7164 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7165 (defcustom gnus-button-valid-localpart-regexp
7166   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7167   "Regular expression that matches a localpart of mail addresses or MIDs."
7168   :version "22.1"
7169   :group 'gnus-article-buttons
7170   :type 'regexp)
7171
7172 (defcustom gnus-button-man-handler 'manual-entry
7173   "Function to use for displaying man pages.
7174 The function must take at least one argument with a string naming the
7175 man page."
7176   :version "22.1"
7177   :type '(choice (function-item :tag "Man" manual-entry)
7178                  (function-item :tag "Woman" woman)
7179                  (function :tag "Other"))
7180   :group 'gnus-article-buttons)
7181
7182 (defcustom gnus-button-mid-or-mail-regexp
7183   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7184           gnus-button-valid-fqdn-regexp
7185           ">?\\)\\b")
7186   "Regular expression that matches a message ID or a mail address."
7187   :version "22.1"
7188   :group 'gnus-article-buttons
7189   :type 'regexp)
7190
7191 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7192   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7193 Strings like this can be either a message ID or a mail address.  If it is one
7194 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7195 message ID or a mail address, respectively.  If this variable is set to the
7196 symbol `ask', always query the user what do do.  If it is a function, this
7197 function will be called with the string as its only argument.  The function
7198 must return `mid', `mail', `invalid' or `ask'."
7199   :version "22.1"
7200   :group 'gnus-article-buttons
7201   :type '(choice (function-item :tag "Heuristic function"
7202                                 gnus-button-mid-or-mail-heuristic)
7203                  (const ask)
7204                  (const mid)
7205                  (const mail)))
7206
7207 (defcustom gnus-button-mid-or-mail-heuristic-alist
7208   '((-10.0 . ".+\\$.+@")
7209     (-10.0 . "#")
7210     (-10.0 . "\\*")
7211     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7212     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7213     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7214     (-1.0  . "^[^a-z]+@")
7215     ;;
7216     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7217     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7218     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7219     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7220     ;;
7221     (-2.0  . "^[0-9]")
7222     (-1.0  . "^[0-9][0-9]")
7223     ;;
7224     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7225     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7226     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7227     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7228     ;;
7229     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7230     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7231     ;;       "[0-9]{8,}.*\@"
7232     (-3.0
7233      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7234     ;; "[0-9]{12,}.*\@"
7235     ;; compensation for TDMA dated mail addresses:
7236     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7237     ;;
7238     (-20.0 . "\\.fsf@") ;; Gnus
7239     (-20.0 . "^slrn")
7240     (-20.0 . "^Pine")
7241     (-20.0 . "^alpine\\.")
7242     (-20.0 . "_-_") ;; Subject change in thread
7243     ;;
7244     (-20.0 . "\\.ln@") ;; leafnode
7245     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7246     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7247     ;;
7248     ;; (5.0 . "") ;; $local_part_len <= 7
7249     (10.0  . "^[^0-9]+@")
7250     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7251     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7252     (3.0   . "\@stud")
7253     ;;
7254     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7255     ;;
7256     (0.5   . "^[A-Z][a-z]")
7257     (0.5   . "^[A-Z][a-z][a-z]")
7258     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7259     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7260   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7261
7262 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7263 address.  The REGEXP is processed with `case-fold-search' set to nil."
7264   :version "22.1"
7265   :group 'gnus-article-buttons
7266   :type '(repeat (cons (number :tag "Rate")
7267                        (regexp :tag "Regexp"))))
7268
7269 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7270   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7271 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7272 address, `ask' if unsure and `invalid' if the string is invalid."
7273   (let ((case-fold-search nil)
7274         (list gnus-button-mid-or-mail-heuristic-alist)
7275         (result 0) rate regexp lpartlen elem)
7276     (setq lpartlen
7277           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7278     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7279     ;; Certain special cases...
7280     (when (string-match
7281            (concat
7282             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7283             "^[0-9]+\\.[0-9]+@compuserve\\|"
7284             "@public\\.gmane\\.org")
7285            mid-or-mail)
7286       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7287       (setq result 'mail))
7288     (when (string-match "@.*@\\| " mid-or-mail)
7289       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7290       (setq result 'invalid))
7291     ;; Nothing more to do, if result is not a number here...
7292     (when (numberp result)
7293       (while list
7294         (setq elem (car list)
7295               rate (car elem)
7296               regexp (cdr elem)
7297               list (cdr list))
7298         (when (string-match regexp mid-or-mail)
7299           (setq result (+ result rate))
7300           (gnus-message
7301            9 "`%s' matched `%s', rate `%s', result `%s'."
7302            mid-or-mail regexp rate result)))
7303       (when (<= lpartlen 7)
7304         (setq result (+ result 5.0))
7305         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7306                       mid-or-mail result))
7307       (when (>= lpartlen 12)
7308         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7309         (cond
7310          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7311           ;; Long local part should contain realname if e-mail address,
7312           ;; too many digits: message-id.
7313           ;; $score -= 5.0 + 0.1 * $local_part_len;
7314           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7315           (setq result (+ result rate))
7316           (gnus-message
7317            9 "Many digits in `%s', rate `%s', result `%s'."
7318            mid-or-mail rate result))
7319          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7320                         mid-or-mail)
7321           ;; Too few vowels [^aeiouy]{4,}.*\@
7322           (setq result (+ result -5.0))
7323           (gnus-message
7324            9 "Few vowels in `%s', rate `%s', result `%s'."
7325            mid-or-mail -5.0 result))
7326          (t
7327           (setq result (+ result 5.0))
7328           (gnus-message
7329            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7330     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7331     ;; Maybe we should make this a customizable alist: (condition . 'result)
7332     (cond
7333      ((symbolp result) result)
7334      ;; Now convert number into proper results:
7335      ((< result -10.0) 'mid)
7336      ((> result  10.0) 'mail)
7337      (t 'ask))))
7338
7339 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7340   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7341          (url-mid (concat "news" ":" mid-or-mail))
7342          (url-mailto (concat "mailto" ":" mid-or-mail)))
7343     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7344     (when (fboundp pref)
7345       (setq guessed
7346             ;; get rid of surrounding angles...
7347             (funcall pref
7348                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7349       (if (or (eq 'mid guessed) (eq 'mail guessed))
7350           (setq pref guessed)
7351         (setq pref 'ask)))
7352     (if (eq pref 'ask)
7353         (save-window-excursion
7354           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7355               (setq pref 'mail)
7356             (setq pref 'mid))))
7357     (cond ((eq pref 'mid)
7358            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7359            (gnus-button-handle-news url-mid))
7360           ((eq pref 'mail)
7361            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7362            (gnus-url-mailto url-mailto))
7363           (t (gnus-message 3 "Invalid string.")))))
7364
7365 (defun gnus-button-handle-custom (fun arg)
7366   "Call function FUN on argument ARG.
7367 Both FUN and ARG are supposed to be strings.  ARG will be passed
7368 as a symbol to FUN."
7369   (funcall (intern fun)
7370            (if (string-match "^customize-apropos" fun)
7371                arg
7372              (intern arg))))
7373
7374 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7375
7376 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7377 ;; stuff?
7378
7379 (defun gnus-button-handle-describe-function (url)
7380   "Call `describe-function' when pushing the corresponding URL button."
7381   (describe-function
7382    (intern
7383     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7384
7385 (defun gnus-button-handle-describe-variable (url)
7386   "Call `describe-variable' when pushing the corresponding URL button."
7387   (describe-variable
7388    (intern
7389     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7390
7391 (defun gnus-button-handle-symbol (url)
7392 "Display help on variable or function.
7393 Calls `describe-variable' or `describe-function'."
7394   (let ((sym (intern url)))
7395     (cond
7396      ((fboundp sym) (describe-function sym))
7397      ((boundp sym) (describe-variable sym))
7398      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7399
7400 (defun gnus-button-handle-describe-key (url)
7401   "Call `describe-key' when pushing the corresponding URL button."
7402   (let* ((key-string
7403           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7404          (keys (ignore-errors (eval `(kbd ,key-string)))))
7405     (if keys
7406         (describe-key keys)
7407       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7408
7409 (defun gnus-button-handle-apropos (url)
7410   "Call `apropos' when pushing the corresponding URL button."
7411   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7412
7413 (defun gnus-button-handle-apropos-command (url)
7414   "Call `apropos' when pushing the corresponding URL button."
7415   (apropos-command
7416    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7417
7418 (defun gnus-button-handle-apropos-variable (url)
7419   "Call `apropos' when pushing the corresponding URL button."
7420   (funcall
7421    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7422    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7423
7424 (defun gnus-button-handle-apropos-documentation (url)
7425   "Call `apropos' when pushing the corresponding URL button."
7426   (funcall
7427    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7428    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7429
7430 (defun gnus-button-handle-library (url)
7431   "Call `locate-library' when pushing the corresponding URL button."
7432   (gnus-message 9 "url=`%s'" url)
7433   (let* ((lib (locate-library url))
7434          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7435     (if (not lib)
7436         (gnus-message 1 "Cannot locale library `%s'." url)
7437       (find-file-read-only file))))
7438
7439 (defcustom gnus-button-man-level 5
7440   "*Integer that says how many man-related buttons Gnus will show.
7441 The higher the number, the more buttons will appear and the more false
7442 positives are possible.  Note that you can set this variable local to
7443 specific groups.  Setting it higher in Unix groups is probably a good idea.
7444 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7445 how to set variables in specific groups."
7446   :version "22.1"
7447   :group 'gnus-article-buttons
7448   :link '(custom-manual "(gnus)Group Parameters")
7449   :type 'integer)
7450
7451 (defcustom gnus-button-emacs-level 5
7452   "*Integer that says how many emacs-related buttons Gnus will show.
7453 The higher the number, the more buttons will appear and the more false
7454 positives are possible.  Note that you can set this variable local to
7455 specific groups.  Setting it higher in Emacs or Gnus related groups is
7456 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7457 `gnus-parameters' on how to set variables in specific groups."
7458   :version "22.1"
7459   :group 'gnus-article-buttons
7460   :link '(custom-manual "(gnus)Group Parameters")
7461   :type 'integer)
7462
7463 (defcustom gnus-button-message-level 5
7464   "*Integer that says how many buttons for news or mail messages will appear.
7465 The higher the number, the more buttons will appear and the more false
7466 positives are possible."
7467   ;; mail addresses, MIDs, URLs for news, ...
7468   :version "22.1"
7469   :group 'gnus-article-buttons
7470   :type 'integer)
7471
7472 (defcustom gnus-button-browse-level 5
7473   "*Integer that says how many buttons for browsing will appear.
7474 The higher the number, the more buttons will appear and the more false
7475 positives are possible."
7476   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7477   :version "22.1"
7478   :group 'gnus-article-buttons
7479   :type 'integer)
7480
7481 (defcustom gnus-button-alist
7482   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7483      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7484     ((concat "\\b\\(nntp\\|news\\):\\("
7485              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7486      0 t gnus-button-handle-news 2)
7487     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7488      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7489     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7490      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7491     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7492     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7493      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7494     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7495      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7496     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7497      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7498     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7499      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7500     ;; RFC 2368 (The mailto URL scheme)
7501     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7502      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7503     ("\\bmailto:\\([^ \n\t]+\\)"
7504      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7505     ;; Info Konqueror style <info:/foo/bar baz>.
7506     ;; Must come before " Gnus home-grown style".
7507     ("\\binfo://?\\([^'\">\n\t]+\\)"
7508      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7509    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7510     ("\\binfo://\\([^'\">\n\t ]+\\)"
7511      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7512     ;; Info GNOME style <info:foo#bar_baz>
7513     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7514      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7515     ;; Info KDE style <info:(foo)bar baz>
7516     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7517      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7518     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7519      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7520     ("\\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,]*\\)\\)?"
7521      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7522      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7523     ;; This is custom
7524     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7525      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7526     ;; Emacs help commands
7527     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7528      ;; regexp doesn't match arguments containing ` '.
7529      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7530     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7531      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7532     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7533      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7534     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7535      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7536     ;; The following entries may lead to many false positives so don't enable
7537     ;; them by default (use a high button level).
7538     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7539      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7540      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7541     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7542      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7543     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7544      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7545     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7546      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7547     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7548      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7549     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7550      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7551     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7552      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7553     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7554      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7555     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7556      ;; Unlike the other regexps we really have to require quoting
7557      ;; here to determine where it ends.
7558      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7559     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7560     ("<URL: *\\([^\n<>]*\\)>"
7561      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7562     ;; RFC 2396 (2.4.3., delims) ...
7563     ("\"URL: *\\([^\n\"]*\\)\""
7564      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7565     ;; Raw URLs.
7566     (gnus-button-url-regexp
7567      0 (>= gnus-button-browse-level 0) browse-url 0)
7568     ;; man pages
7569     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7570      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7571      gnus-button-handle-man 1)
7572     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7573     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7574      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7575      gnus-button-handle-man 1)
7576     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7577     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7578     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7579      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7580     ;; Recognizing patches to .el files.  This is somewhat obscure,
7581     ;; but considering the percentage of Gnus users who hack Emacs
7582     ;; Lisp files...
7583     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7584      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7585     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7586      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7587     ;; MID or mail: To avoid too many false positives we don't try to catch
7588     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7589     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7590     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7591     ;; so that non-ambiguous entries (see above) match first.
7592     (gnus-button-mid-or-mail-regexp
7593      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7594   "*Alist of regexps matching buttons in article bodies.
7595
7596 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7597 REGEXP: is the string (case insensitive) matching text around the button (can
7598 also be Lisp expression evaluating to a string),
7599 BUTTON: is the number of the regexp grouping actually matching the button,
7600 FORM: is a Lisp expression which must eval to true for the button to
7601 be added,
7602 CALLBACK: is the function to call when the user push this button, and each
7603 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7604
7605 CALLBACK can also be a variable, in that case the value of that
7606 variable it the real callback function."
7607   :group 'gnus-article-buttons
7608   :type '(repeat (list (choice regexp variable sexp)
7609                        (integer :tag "Button")
7610                        (sexp :tag "Form")
7611                        (function :tag "Callback")
7612                        (repeat :tag "Par"
7613                                :inline t
7614                                (integer :tag "Regexp group")))))
7615 (put 'gnus-button-alist 'risky-local-variable t)
7616
7617 (defcustom gnus-header-button-alist
7618   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7619      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7620     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7621      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7622     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7623      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7624     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7625      0 (>= gnus-button-browse-level 0) browse-url 0)
7626     ("^Subject:" gnus-button-url-regexp
7627      0 (>= gnus-button-browse-level 0) browse-url 0)
7628     ("^[^:]+:" gnus-button-url-regexp
7629      0 (>= gnus-button-browse-level 0) browse-url 0)
7630     ("^OpenPGP:.*url=" gnus-button-url-regexp
7631      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7632     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7633      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7634     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7635      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7636   "*Alist of headers and regexps to match buttons in article heads.
7637
7638 This alist is very similar to `gnus-button-alist', except that each
7639 alist has an additional HEADER element first in each entry:
7640
7641 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7642
7643 HEADER is a regexp to match a header.  For a fuller explanation, see
7644 `gnus-button-alist'."
7645   :group 'gnus-article-buttons
7646   :group 'gnus-article-headers
7647   :type '(repeat (list (regexp :tag "Header")
7648                        (choice regexp variable)
7649                        (integer :tag "Button")
7650                        (sexp :tag "Form")
7651                        (function :tag "Callback")
7652                        (repeat :tag "Par"
7653                                :inline t
7654                                (integer :tag "Regexp group")))))
7655 (put 'gnus-header-button-alist 'risky-local-variable t)
7656
7657 ;;; Commands:
7658
7659 (defun gnus-article-push-button (event)
7660   "Check text under the mouse pointer for a callback function.
7661 If the text under the mouse pointer has a `gnus-callback' property,
7662 call it with the value of the `gnus-data' text property."
7663   (interactive "e")
7664   (set-buffer (window-buffer (posn-window (event-start event))))
7665   (let* ((pos (posn-point (event-start event)))
7666          (data (get-text-property pos 'gnus-data))
7667          (fun (get-text-property pos 'gnus-callback)))
7668     (goto-char pos)
7669     (when fun
7670       (funcall fun data))))
7671
7672 (defun gnus-article-press-button ()
7673   "Check text at point for a callback function.
7674 If the text at point has a `gnus-callback' property,
7675 call it with the value of the `gnus-data' text property."
7676   (interactive)
7677   (let ((data (get-text-property (point) 'gnus-data))
7678         (fun (get-text-property (point) 'gnus-callback)))
7679     (when fun
7680       (funcall fun data))))
7681
7682 (defun gnus-article-highlight (&optional force)
7683   "Highlight current article.
7684 This function calls `gnus-article-highlight-headers',
7685 `gnus-article-highlight-citation',
7686 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7687 do the highlighting.  See the documentation for those functions."
7688   (interactive (list 'force))
7689   (gnus-article-highlight-headers)
7690   (gnus-article-highlight-citation force)
7691   (gnus-article-highlight-signature)
7692   (gnus-article-add-buttons)
7693   (gnus-article-add-buttons-to-head))
7694
7695 (defun gnus-article-highlight-some (&optional force)
7696   "Highlight current article.
7697 This function calls `gnus-article-highlight-headers',
7698 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7699 do the highlighting.  See the documentation for those functions."
7700   (interactive (list 'force))
7701   (gnus-article-highlight-headers)
7702   (gnus-article-highlight-signature)
7703   (gnus-article-add-buttons))
7704
7705 (defun gnus-article-highlight-headers ()
7706   "Highlight article headers as specified by `gnus-header-face-alist'."
7707   (interactive)
7708   (gnus-with-article-headers
7709     (let (regexp header-face field-face from hpoints fpoints)
7710       (dolist (entry gnus-header-face-alist)
7711         (goto-char (point-min))
7712         (setq regexp (concat "^\\("
7713                              (if (string-equal "" (nth 0 entry))
7714                                  "[^\t ]"
7715                                (nth 0 entry))
7716                              "\\)")
7717               header-face (nth 1 entry)
7718               field-face (nth 2 entry))
7719         (while (and (re-search-forward regexp nil t)
7720                     (not (eobp)))
7721           (beginning-of-line)
7722           (setq from (point))
7723           (unless (search-forward ":" nil t)
7724             (forward-char 1))
7725           (when (and header-face
7726                      (not (memq (point) hpoints)))
7727             (push (point) hpoints)
7728             (gnus-put-text-property from (point) 'face header-face))
7729           (when (and field-face
7730                      (not (memq (setq from (point)) fpoints)))
7731             (push from fpoints)
7732             (if (re-search-forward "^[^ \t]" nil t)
7733                 (forward-char -2)
7734               (goto-char (point-max)))
7735             (gnus-put-text-property from (point) 'face field-face)))))))
7736
7737 (defun gnus-article-highlight-signature ()
7738   "Highlight the signature in an article.
7739 It does this by highlighting everything after
7740 `gnus-signature-separator' using the face `gnus-signature'."
7741   (interactive)
7742   (gnus-with-article-buffer
7743     (let ((inhibit-point-motion-hooks t))
7744       (save-restriction
7745         (when (and gnus-signature-face
7746                    (gnus-article-narrow-to-signature))
7747           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7748                             'face gnus-signature-face)
7749           (widen)
7750           (gnus-article-search-signature)
7751           (let ((start (match-beginning 0))
7752                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7753             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7754                                      end)))))))
7755
7756 (defun gnus-button-in-region-p (b e prop)
7757   "Say whether PROP exists in the region."
7758   (text-property-not-all b e prop nil))
7759
7760 (defun gnus-article-add-buttons ()
7761   "Find external references in the article and make buttons of them.
7762 \"External references\" are things like Message-IDs and URLs, as
7763 specified by `gnus-button-alist'."
7764   (interactive)
7765   (gnus-with-article-buffer
7766     (let ((inhibit-point-motion-hooks t)
7767           (case-fold-search t)
7768           (alist gnus-button-alist)
7769           beg entry regexp)
7770       ;; We skip the headers.
7771       (article-goto-body)
7772       (setq beg (point))
7773       (while (setq entry (pop alist))
7774         (setq regexp (eval (car entry)))
7775         (goto-char beg)
7776         (while (re-search-forward regexp nil t)
7777           (let ((start (match-beginning (nth 1 entry)))
7778                 (end (match-end (nth 1 entry)))
7779                 (from (match-beginning 0)))
7780             (when (and (eval (nth 2 entry))
7781                        (not (gnus-button-in-region-p
7782                              start end 'gnus-callback)))
7783               ;; That optional form returned non-nil, so we add the
7784               ;; button.
7785               (setq from (set-marker (make-marker) from))
7786               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7787                            (gnus-article-extend-url-button from start end))
7788                 (gnus-article-add-button start end
7789                                          'gnus-button-push (list from entry))
7790                 (gnus-put-text-property
7791                  start end
7792                  'gnus-string (buffer-substring-no-properties
7793                                start end))))))))))
7794
7795 (defun gnus-article-extend-url-button (beg start end)
7796   "Extend url button if url is folded into two or more lines.
7797 Return non-nil if button is extended.  BEG is a marker that points to
7798 the beginning position of a text containing url.  START and END are
7799 the endpoints of a url button before it is extended.  The concatenated
7800 url is put as the `gnus-button-url' overlay property on the button."
7801   (let ((opoint (point))
7802         (points (list start end))
7803         url delim regexp)
7804     (prog1
7805         (when (and (progn
7806                      (goto-char end)
7807                      (not (looking-at "[\t ]*[\">]")))
7808                    (progn
7809                      (goto-char start)
7810                      (string-match
7811                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7812                       (buffer-substring (point-at-bol) start)))
7813                    (progn
7814                      (setq url (list (buffer-substring start end))
7815                            delim (if (match-beginning 1) ">" "\""))
7816                      (beginning-of-line)
7817                      (setq regexp (concat
7818                                    (when (and (looking-at
7819                                                message-cite-prefix-regexp)
7820                                               (< (match-end 0) start))
7821                                      (regexp-quote (match-string 0)))
7822                                    "\
7823 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7824                                    delim "\\)"))
7825                      (while (progn
7826                               (forward-line 1)
7827                               (and (looking-at regexp)
7828                                    (prog1
7829                                        (match-beginning 1)
7830                                      (push (or (match-string 2)
7831                                                (match-string 1))
7832                                            url)
7833                                      (push (setq end (or (match-end 2)
7834                                                          (match-end 1)))
7835                                            points)
7836                                      (push (or (match-beginning 2)
7837                                                (match-beginning 1))
7838                                            points)))))
7839                      (match-beginning 2)))
7840           (let (gnus-article-mouse-face widget-mouse-face)
7841             (while points
7842               (gnus-article-add-button (pop points) (pop points)
7843                                        'gnus-button-push beg)))
7844           (let ((overlay (gnus-make-overlay start end)))
7845             (gnus-overlay-put overlay 'evaporate t)
7846             (gnus-overlay-put overlay 'gnus-button-url
7847                               (list (mapconcat 'identity (nreverse url) "")))
7848             (when gnus-article-mouse-face
7849               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7850           t)
7851       (goto-char opoint))))
7852
7853 ;; Add buttons to the head of an article.
7854 (defun gnus-article-add-buttons-to-head ()
7855   "Add buttons to the head of the article."
7856   (interactive)
7857   (gnus-with-article-headers
7858     (let (beg end)
7859       (dolist (entry gnus-header-button-alist)
7860         ;; Each alist entry.
7861         (goto-char (point-min))
7862         (while (re-search-forward (car entry) nil t)
7863           ;; Each header matching the entry.
7864           (setq beg (match-beginning 0))
7865           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7866                              (match-beginning 0))
7867                         (point-max)))
7868           (goto-char beg)
7869           (while (re-search-forward (eval (nth 1 entry)) end t)
7870             ;; Each match within a header.
7871             (let* ((entry (cdr entry))
7872                    (start (match-beginning (nth 1 entry)))
7873                    (end (match-end (nth 1 entry)))
7874                    (form (nth 2 entry)))
7875               (goto-char (match-end 0))
7876               (when (eval form)
7877                 (gnus-article-add-button
7878                  start end (nth 3 entry)
7879                  (buffer-substring (match-beginning (nth 4 entry))
7880                                    (match-end (nth 4 entry)))))))
7881           (goto-char end))))))
7882
7883 ;;; External functions:
7884
7885 (defun gnus-article-add-button (from to fun &optional data text)
7886   "Create a button between FROM and TO with callback FUN and data DATA."
7887   (when gnus-article-button-face
7888     (gnus-overlay-put (gnus-make-overlay from to nil t)
7889                       'face gnus-article-button-face))
7890   (gnus-add-text-properties
7891    from to
7892    (nconc (and gnus-article-mouse-face
7893                (list gnus-mouse-face-prop gnus-article-mouse-face))
7894           (list 'gnus-callback fun)
7895           (and data (list 'gnus-data data))))
7896   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7897                          :help-echo (or text "Follow the link")
7898                          :keymap gnus-url-button-map
7899                          :button-keymap gnus-widget-button-keymap))
7900
7901 (defun gnus-article-copy-string ()
7902   "Copy the string in the button to the kill ring."
7903   (interactive)
7904   (gnus-article-check-buffer)
7905   (let ((data (get-text-property (point) 'gnus-string)))
7906     (when data
7907       (with-temp-buffer
7908         (insert data)
7909         (copy-region-as-kill (point-min) (point-max))
7910         (message "Copied %s" data)))))
7911
7912 ;;; Internal functions:
7913
7914 (defun gnus-article-set-globals ()
7915   (with-current-buffer gnus-summary-buffer
7916     (gnus-set-global-variables)))
7917
7918 (defun gnus-signature-toggle (end)
7919   (gnus-with-article-buffer
7920     (let ((inhibit-point-motion-hooks t))
7921       (if (text-property-any end (point-max) 'article-type 'signature)
7922           (progn
7923             (gnus-delete-wash-type 'signature)
7924             (gnus-remove-text-properties-when
7925              'article-type 'signature end (point-max)
7926              (cons 'article-type (cons 'signature
7927                                        gnus-hidden-properties))))
7928         (gnus-add-wash-type 'signature)
7929         (gnus-add-text-properties-when
7930          'article-type nil end (point-max)
7931          (cons 'article-type (cons 'signature
7932                                    gnus-hidden-properties)))))
7933     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7934       (gnus-set-mode-line 'article))))
7935
7936 (defun gnus-button-push (marker-and-entry)
7937   ;; Push button starting at MARKER.
7938   (save-excursion
7939     (let* ((marker (car marker-and-entry))
7940            (entry (cadr marker-and-entry))
7941            (regexp (car entry))
7942            (inhibit-point-motion-hooks t))
7943       (goto-char marker)
7944       ;; This is obviously true, or something bad is happening :)
7945       ;; But we need it to have the match-data
7946       (when (looking-at (or (if (symbolp regexp)
7947                                 (symbol-value regexp)
7948                               regexp)))
7949         (let ((fun (nth 3 entry))
7950               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7951                              (get-char-property marker 'gnus-button-url))
7952                         (mapcar (lambda (group)
7953                                   (let ((string (match-string group)))
7954                                     (set-text-properties
7955                                      0 (length string) nil string)
7956                                     string))
7957                                 (nthcdr 4 entry)))))
7958
7959           (cond
7960            ((fboundp fun)
7961             (apply fun args))
7962            ((and (boundp fun)
7963                  (fboundp (symbol-value fun)))
7964             (apply (symbol-value fun) args))
7965            (t
7966             (gnus-message 1 "You must define `%S' to use this button"
7967                           (cons fun args)))))))))
7968
7969 (defun gnus-parse-news-url (url)
7970   (let (scheme server port group message-id articles)
7971     (with-temp-buffer
7972       (insert url)
7973       (goto-char (point-min))
7974       (when (looking-at "\\([A-Za-z]+\\):")
7975         (setq scheme (match-string 1))
7976         (goto-char (match-end 0)))
7977       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7978         (setq server (match-string 1))
7979         (setq port (if (stringp (match-string 3))
7980                        (string-to-number (match-string 3))
7981                      (match-string 3)))
7982         (goto-char (match-end 0)))
7983
7984       (cond
7985        ((looking-at "\\(.*@.*\\)")
7986         (setq message-id (match-string 1)))
7987        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7988         (setq group (match-string 1)
7989               articles (split-string (match-string 2) "-")))
7990        ((looking-at "\\([^/]+\\)/?")
7991         (setq group (match-string 1)))
7992        (t
7993         (error "Unknown news URL syntax"))))
7994     (list scheme server port group message-id articles)))
7995
7996 (defun gnus-button-handle-news (url)
7997   "Fetch a news URL."
7998   (destructuring-bind (scheme server port group message-id articles)
7999       (gnus-parse-news-url url)
8000     (cond
8001      (message-id
8002       (with-current-buffer gnus-summary-buffer
8003         (if server
8004             (let ((gnus-refer-article-method
8005                    (nconc (list (list 'nntp server))
8006                           gnus-refer-article-method))
8007                   (nntp-port-number (or port "nntp")))
8008               (gnus-message 7 "Fetching %s with %s"
8009                             message-id gnus-refer-article-method)
8010               (gnus-summary-refer-article message-id))
8011           (gnus-summary-refer-article message-id))))
8012      (group
8013       (gnus-button-fetch-group url)))))
8014
8015 (defun gnus-button-patch (library line)
8016   "Visit an Emacs Lisp library LIBRARY on line LINE."
8017   (interactive)
8018   (let ((file (locate-library (file-name-nondirectory library))))
8019     (unless file
8020       (error "Couldn't find library %s" library))
8021     (find-file file)
8022     (goto-char (point-min))
8023     (forward-line (1- (string-to-number line)))))
8024
8025 (defun gnus-button-handle-man (url)
8026   "Fetch a man page."
8027   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8028   (when (eq gnus-button-man-handler 'woman)
8029     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8030   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8031   (funcall gnus-button-man-handler url))
8032
8033 (defun gnus-button-handle-info-url (url)
8034   "Fetch an info URL."
8035   (setq url (mm-subst-char-in-string ?+ ?\  url))
8036   (cond
8037    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8038     (gnus-info-find-node
8039      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8040                      "Gnus")
8041              ")" (gnus-url-unhex-string (match-string 2 url)))))
8042    ((string-match "([^)\"]+)[^\"]+" url)
8043     (setq url
8044           (gnus-replace-in-string
8045            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8046     (gnus-info-find-node url))
8047    (t (error "Can't parse %s" url))))
8048
8049 (defun gnus-button-handle-info-url-gnome (url)
8050   "Fetch GNOME style info URL."
8051   (setq url (mm-subst-char-in-string ?_ ?\  url))
8052   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8053       (gnus-info-find-node
8054        (concat "("
8055                (gnus-url-unhex-string
8056                  (match-string 1 url))
8057                ")"
8058                (or (gnus-url-unhex-string
8059                     (match-string 2 url))
8060                    "Top")))
8061     (error "Can't parse %s" url)))
8062
8063 (defun gnus-button-handle-info-url-kde (url)
8064   "Fetch KDE style info URL."
8065   (gnus-info-find-node (gnus-url-unhex-string url)))
8066
8067 ;; (info) will autoload info.el
8068 (declare-function Info-menu "info" (menu-item &optional fork))
8069 (declare-function Info-index-next "info" (num))
8070
8071 (defun gnus-button-handle-info-keystrokes (url)
8072   "Call `info' when pushing the corresponding URL button."
8073   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8074   (let (node indx comma)
8075     (if (string-match
8076          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8077                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8078                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8079                  "\\(?:[ \t\n,]*\\)\\)?")
8080          url)
8081         (setq node (match-string 2 url)
8082               indx (match-string 3 url))
8083       (error "Can't parse %s" url))
8084     (info)
8085     (Info-directory)
8086     (Info-menu node)
8087     (when (> (length indx) 0)
8088       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8089                             "\\([ \t\n,]*\\)")
8090                     indx)
8091       (setq comma (match-string 2 indx))
8092       (setq indx  (match-string 1 indx))
8093       (Info-index indx)
8094       (when comma
8095         (dotimes (i (with-temp-buffer
8096                       (insert comma)
8097                       ;; Note: the XEmacs version of `how-many' takes
8098                       ;; no optional argument.
8099                       (goto-char (point-min))
8100                       (how-many ",")))
8101           (Info-index-next 1)))
8102       nil)))
8103
8104 (autoload 'pgg-snarf-keys-region "pgg")
8105 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8106 (declare-function pgg-display-output-buffer "pgg" (start end status))
8107
8108 (defun gnus-button-openpgp (url)
8109   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8110   (with-temp-buffer
8111     (mm-url-insert-file-contents-external url)
8112     (pgg-snarf-keys-region (point-min) (point-max))
8113     (pgg-display-output-buffer nil nil nil)))
8114
8115 (defun gnus-button-message-id (message-id)
8116   "Fetch MESSAGE-ID."
8117   (with-current-buffer gnus-summary-buffer
8118     (gnus-summary-refer-article message-id)))
8119
8120 (defun gnus-button-fetch-group (address &rest ignore)
8121   "Fetch GROUP specified by ADDRESS."
8122   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8123                       address)
8124     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8125     ;; for nntp:// and news://
8126     (setq address (match-string 3 address)))
8127   (if (not (string-match "[:/]" address))
8128       ;; This is just a simple group url.
8129       (gnus-group-read-ephemeral-group address gnus-select-method)
8130     (if (not
8131          (string-match
8132           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8133           address))
8134         (error "Can't parse %s" address)
8135       (gnus-group-read-ephemeral-group
8136        (match-string 4 address)
8137        `(nntp ,(match-string 1 address)
8138               (nntp-address ,(match-string 1 address))
8139               (nntp-port-number ,(if (match-end 3)
8140                                      (match-string 3 address)
8141                                    "nntp")))
8142        nil nil nil
8143        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8144
8145 (defun gnus-url-parse-query-string (query &optional downcase)
8146   (let (retval pairs cur key val)
8147     (setq pairs (split-string query "&"))
8148     (while pairs
8149       (setq cur (car pairs)
8150             pairs (cdr pairs))
8151       (if (not (string-match "=" cur))
8152           nil                           ; Grace
8153         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8154               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8155         (if downcase
8156             (setq key (downcase key)))
8157         (setq cur (assoc key retval))
8158         (if cur
8159             (setcdr cur (cons val (cdr cur)))
8160           (setq retval (cons (list key val) retval)))))
8161     retval))
8162
8163 (defun gnus-url-mailto (url)
8164   ;; Send mail to someone
8165   (setq url (replace-regexp-in-string "\n" " " url))
8166   (when (string-match "mailto:/*\\(.*\\)" url)
8167     (setq url (substring url (match-beginning 1) nil)))
8168   (let (to args subject func)
8169     (setq args (gnus-url-parse-query-string
8170                 (if (string-match "^\\?" url)
8171                     (substring url 1)
8172                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8173                       (concat "to=" (match-string 1 url) "&"
8174                               (match-string 2 url))
8175                     (concat "to=" url))))
8176           subject (cdr-safe (assoc "subject" args)))
8177     (gnus-msg-mail)
8178     (while args
8179       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8180       (if (fboundp func)
8181           (funcall func)
8182         (message-position-on-field (caar args)))
8183       (insert (gnus-replace-in-string
8184                (mapconcat 'identity (reverse (cdar args)) ", ")
8185                "\r\n" "\n" t))
8186       (setq args (cdr args)))
8187     (if subject
8188         (message-goto-body)
8189       (message-goto-subject))))
8190
8191 (defun gnus-button-embedded-url (address)
8192   "Activate ADDRESS with `browse-url'."
8193   (browse-url (gnus-strip-whitespace address)))
8194
8195 ;;; Next/prev buttons in the article buffer.
8196
8197 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8198 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8199
8200 (defvar gnus-prev-page-map
8201   (let ((map (make-sparse-keymap)))
8202     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8203     (define-key map "\r" 'gnus-button-prev-page)
8204     map))
8205
8206 (defvar gnus-next-page-map
8207   (let ((map (make-sparse-keymap)))
8208     (define-key map gnus-mouse-2 'gnus-button-next-page)
8209     (define-key map "\r" 'gnus-button-next-page)
8210     map))
8211
8212 (defun gnus-insert-prev-page-button ()
8213   (let ((b (point)) e
8214         (inhibit-read-only t))
8215     (gnus-eval-format
8216      gnus-prev-page-line-format nil
8217      `(keymap ,gnus-prev-page-map
8218               gnus-prev t
8219               gnus-callback gnus-article-button-prev-page
8220               article-type annotation))
8221     (setq e (if (bolp)
8222                 ;; Exclude a newline.
8223                 (1- (point))
8224               (point)))
8225     (when gnus-article-button-face
8226       (gnus-overlay-put (gnus-make-overlay b e nil t)
8227                         'face gnus-article-button-face))
8228     (widget-convert-button
8229      'link b e
8230      :action 'gnus-button-prev-page
8231      :button-keymap gnus-prev-page-map)))
8232
8233 (defun gnus-button-next-page (&optional args more-args)
8234   "Go to the next page."
8235   (interactive)
8236   (let ((win (selected-window)))
8237     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8238     (gnus-article-next-page)
8239     (select-window win)))
8240
8241 (defun gnus-button-prev-page (&optional args more-args)
8242   "Go to the prev page."
8243   (interactive)
8244   (let ((win (selected-window)))
8245     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8246     (gnus-article-prev-page)
8247     (select-window win)))
8248
8249 (defun gnus-insert-next-page-button ()
8250   (let ((b (point)) e
8251         (inhibit-read-only t))
8252     (gnus-eval-format gnus-next-page-line-format nil
8253                       `(keymap ,gnus-next-page-map
8254                                gnus-next t
8255                                gnus-callback gnus-article-button-next-page
8256                                article-type annotation))
8257     (setq e (if (bolp)
8258                 ;; Exclude a newline.
8259                 (1- (point))
8260               (point)))
8261     (when gnus-article-button-face
8262       (gnus-overlay-put (gnus-make-overlay b e nil t)
8263                         'face gnus-article-button-face))
8264     (widget-convert-button
8265      'link b e
8266      :action 'gnus-button-next-page
8267      :button-keymap gnus-next-page-map)))
8268
8269 (defun gnus-article-button-next-page (arg)
8270   "Go to the next page."
8271   (interactive "P")
8272   (let ((win (selected-window)))
8273     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8274     (gnus-article-next-page)
8275     (select-window win)))
8276
8277 (defun gnus-article-button-prev-page (arg)
8278   "Go to the prev page."
8279   (interactive "P")
8280   (let ((win (selected-window)))
8281     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8282     (gnus-article-prev-page)
8283     (select-window win)))
8284
8285 (defvar gnus-decode-header-methods
8286   '(mail-decode-encoded-word-region)
8287   "List of methods used to decode headers.
8288
8289 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8290 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8291 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8292 whose names match REGEXP.
8293
8294 For example:
8295 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8296  mail-decode-encoded-word-region
8297  (\"chinese\" . rfc1843-decode-region))
8298 ")
8299
8300 (defvar gnus-decode-header-methods-cache nil)
8301
8302 (defun gnus-multi-decode-header (start end)
8303   "Apply the functions from `gnus-encoded-word-methods' that match."
8304   (unless (and gnus-decode-header-methods-cache
8305                (eq gnus-newsgroup-name
8306                    (car gnus-decode-header-methods-cache)))
8307     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8308     (dolist (x gnus-decode-header-methods)
8309       (if (symbolp x)
8310           (nconc gnus-decode-header-methods-cache (list x))
8311         (if (and gnus-newsgroup-name
8312                  (string-match (car x) gnus-newsgroup-name))
8313             (nconc gnus-decode-header-methods-cache
8314                    (list (cdr x)))))))
8315   (let ((xlist gnus-decode-header-methods-cache))
8316     (pop xlist)
8317     (save-restriction
8318       (narrow-to-region start end)
8319       (while xlist
8320         (funcall (pop xlist) (point-min) (point-max))))))
8321
8322 ;;;
8323 ;;; Treatment top-level handling.
8324 ;;;
8325
8326 (defvar gnus-inhibit-article-treatments nil)
8327
8328 (defun gnus-treat-article (gnus-treat-condition
8329                            &optional part-number total-parts gnus-treat-type)
8330   (let ((gnus-treat-length (- (point-max) (point-min)))
8331         (alist gnus-treatment-function-alist)
8332         (article-goto-body-goes-to-point-min-p t)
8333         (treated-type
8334          (or (not gnus-treat-type)
8335              (catch 'found
8336                (let ((list gnus-article-treat-types))
8337                  (while list
8338                    (when (string-match (pop list) gnus-treat-type)
8339                      (throw 'found t)))))))
8340         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8341         val elem)
8342     (gnus-run-hooks 'gnus-part-display-hook)
8343     (dolist (elem alist)
8344       (setq val
8345             (save-excursion
8346               (when (gnus-buffer-live-p gnus-summary-buffer)
8347                 (set-buffer gnus-summary-buffer))
8348               (symbol-value (car elem))))
8349       (when (and (or (consp val)
8350                      treated-type)
8351                  (or (not gnus-inhibit-article-treatments)
8352                      (eq gnus-treat-condition 'head))
8353                  (gnus-treat-predicate val)
8354                  (or (not (get (car elem) 'highlight))
8355                      highlightp))
8356         (save-restriction
8357           (funcall (cadr elem)))))))
8358
8359 ;; Dynamic variables.
8360 (defvar part-number)
8361 (defvar total-parts)
8362 (defvar gnus-treat-type)
8363 (defvar gnus-treat-condition)
8364 (defvar gnus-treat-length)
8365
8366 (defun gnus-treat-predicate (val)
8367   (cond
8368    ((null val)
8369     nil)
8370    (gnus-treat-condition
8371     (eq gnus-treat-condition val))
8372    ((and (listp val)
8373          (stringp (car val)))
8374     (apply 'gnus-or (mapcar `(lambda (s)
8375                                (string-match s ,(or gnus-newsgroup-name "")))
8376                             val)))
8377    ((listp val)
8378     (let ((pred (pop val)))
8379       (cond
8380        ((eq pred 'or)
8381         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8382        ((eq pred 'and)
8383         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8384        ((eq pred 'not)
8385         (not (gnus-treat-predicate (car val))))
8386        ((eq pred 'typep)
8387         (equal (car val) gnus-treat-type))
8388        (t
8389         (error "%S is not a valid predicate" pred)))))
8390    ((eq val t)
8391     t)
8392    ((eq val 'head)
8393     nil)
8394    ((eq val 'first)
8395     (eq part-number 1))
8396    ((eq val 'last)
8397     (eq part-number total-parts))
8398    ((numberp val)
8399     (< gnus-treat-length val))
8400    (t
8401     (error "%S is not a valid value" val))))
8402
8403 (defun gnus-article-encrypt-body (protocol &optional n)
8404   "Encrypt the article body."
8405   (interactive
8406    (list
8407     (or gnus-article-encrypt-protocol
8408         (gnus-completing-read "Encrypt protocol"
8409                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8410                               t))
8411     current-prefix-arg))
8412   ;; User might hit `K E' instead of `K e', so prompt once.
8413   (when (and gnus-article-encrypt-protocol
8414              gnus-novice-user)
8415     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8416       (error "Encrypt aborted")))
8417   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8418     (unless func
8419       (error "Can't find the encrypt protocol %s" protocol))
8420     (if (member gnus-newsgroup-name '("nndraft:delayed"
8421                                       "nndraft:drafts"
8422                                       "nndraft:queue"))
8423         (error "Can't encrypt the article in group %s"
8424                gnus-newsgroup-name))
8425     (gnus-summary-iterate n
8426       (with-current-buffer gnus-summary-buffer
8427         (let ((mail-parse-charset gnus-newsgroup-charset)
8428               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8429               (summary-buffer gnus-summary-buffer)
8430               references point)
8431           (gnus-set-global-variables)
8432           (when (gnus-group-read-only-p)
8433             (error "The current newsgroup does not support article encrypt"))
8434           (gnus-summary-show-article t)
8435           (setq references
8436               (or (mail-header-references gnus-current-headers) ""))
8437           (set-buffer gnus-article-buffer)
8438           (let* ((inhibit-read-only t)
8439                  (headers
8440                   (mapcar (lambda (field)
8441                             (and (save-restriction
8442                                    (message-narrow-to-head)
8443                                    (goto-char (point-min))
8444                                    (search-forward field nil t))
8445                                  (prog2
8446                                      (message-narrow-to-field)
8447                                      (buffer-string)
8448                                    (delete-region (point-min) (point-max))
8449                                    (widen))))
8450                           '("Content-Type:" "Content-Transfer-Encoding:"
8451                             "Content-Disposition:"))))
8452             (message-narrow-to-head)
8453             (message-remove-header "MIME-Version")
8454             (goto-char (point-max))
8455             (setq point (point))
8456             (insert (apply 'concat headers))
8457             (widen)
8458             (narrow-to-region point (point-max))
8459             (let ((message-options message-options))
8460               (message-options-set 'message-sender user-mail-address)
8461               (message-options-set 'message-recipients user-mail-address)
8462               (message-options-set 'message-sign-encrypt 'not)
8463               (funcall func))
8464             (goto-char (point-min))
8465             (insert "MIME-Version: 1.0\n")
8466             (widen)
8467             (gnus-summary-edit-article-done
8468              references nil summary-buffer t))
8469           (when gnus-keep-backlog
8470             (gnus-backlog-remove-article
8471              (car gnus-article-current) (cdr gnus-article-current)))
8472           (gnus-flush-original-article-buffer)
8473           (when gnus-use-cache
8474             (gnus-cache-update-article
8475              (car gnus-article-current) (cdr gnus-article-current))))))))
8476
8477 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8478   "The following specs can be used:
8479 %t  The security MIME type
8480 %i  Additional info
8481 %d  Details
8482 %D  Details if button is pressed")
8483
8484 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8485   "The following specs can be used:
8486 %t  The security MIME type
8487 %i  Additional info
8488 %d  Details
8489 %D  Details if button is pressed")
8490
8491 (defvar gnus-mime-security-button-line-format-alist
8492   '((?t gnus-tmp-type ?s)
8493     (?i gnus-tmp-info ?s)
8494     (?d gnus-tmp-details ?s)
8495     (?D gnus-tmp-pressed-details ?s)))
8496
8497 (defvar gnus-mime-security-button-commands
8498   '((gnus-article-press-button "\r" "Show Detail")
8499     (undefined "v")
8500     (undefined "t")
8501     (undefined "C")
8502     (gnus-mime-security-save-part "o" "Save...")
8503     (undefined "\C-o")
8504     (undefined "r")
8505     (undefined "d")
8506     (undefined "c")
8507     (undefined "i")
8508     (undefined "E")
8509     (undefined "e")
8510     (undefined "p")
8511     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8512     (undefined ".")))
8513
8514 (defvar gnus-mime-security-button-map
8515   (let ((map (make-sparse-keymap)))
8516     (define-key map gnus-mouse-2 'gnus-article-push-button)
8517     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8518     (dolist (c gnus-mime-security-button-commands)
8519       (define-key map (cadr c) (car c)))
8520     map))
8521
8522 (easy-menu-define
8523   gnus-mime-security-button-menu gnus-mime-security-button-map
8524   "Security button menu."
8525   `("Security Part"
8526     ,@(delq nil
8527             (mapcar (lambda (c)
8528                       (unless (eq (car c) 'undefined)
8529                         (vector (caddr c) (car c) :active t)))
8530                     gnus-mime-security-button-commands))))
8531
8532 (defun gnus-mime-security-button-menu (event prefix)
8533   "Construct a context-sensitive menu of security commands."
8534   (interactive "e\nP")
8535   (save-window-excursion
8536     (let ((pos (event-start event)))
8537       (select-window (posn-window pos))
8538       (goto-char (posn-point pos))
8539       (gnus-article-check-buffer)
8540       (popup-menu gnus-mime-security-button-menu nil prefix))))
8541
8542 (defvar gnus-mime-security-details-buffer nil)
8543
8544 (defvar gnus-mime-security-button-pressed nil)
8545
8546 (defvar gnus-mime-security-show-details-inline t
8547   "If non-nil, show details in the article buffer.")
8548
8549 (defun gnus-mime-security-verify-or-decrypt (handle)
8550   (mm-remove-parts (cdr handle))
8551   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8552         point (inhibit-read-only t))
8553     (if region
8554         (goto-char (car region)))
8555     (setq point (point))
8556     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8557       (let* ((mm-verify-option 'known)
8558              (mm-decrypt-option 'known)
8559              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8560         (unless (eq nparts (cdr handle))
8561           (mm-destroy-parts (cdr handle))
8562           (setcdr handle nparts))))
8563     (gnus-mime-display-security handle)
8564     (when region
8565       (delete-region (point) (cdr region))
8566       (set-marker (car region) nil)
8567       (set-marker (cdr region) nil))
8568     (goto-char point)))
8569
8570 (defun gnus-mime-security-show-details (handle)
8571   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8572     (if (not details)
8573         (gnus-message 5 "No details.")
8574       (if gnus-mime-security-show-details-inline
8575           (let ((gnus-mime-security-button-pressed
8576                  (not (get-text-property (point) 'gnus-mime-details)))
8577                 (gnus-mime-security-button-line-format
8578                  (get-text-property (point) 'gnus-line-format))
8579                 (inhibit-read-only t))
8580             (forward-char -1)
8581             (while (eq (get-text-property (point) 'gnus-line-format)
8582                        gnus-mime-security-button-line-format)
8583               (forward-char -1))
8584             (forward-char)
8585             (save-restriction
8586               (narrow-to-region (point) (point))
8587               (gnus-insert-mime-security-button handle))
8588             (delete-region (point)
8589                            (or (text-property-not-all
8590                                 (point) (point-max)
8591                                 'gnus-line-format
8592                                 gnus-mime-security-button-line-format)
8593                                (point-max))))
8594         ;; Not inlined.
8595         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8596             (with-current-buffer gnus-mime-security-details-buffer
8597               (erase-buffer)
8598               t)
8599           (setq gnus-mime-security-details-buffer
8600                 (gnus-get-buffer-create "*MIME Security Details*")))
8601         (with-current-buffer gnus-mime-security-details-buffer
8602           (insert details)
8603           (goto-char (point-min)))
8604         (pop-to-buffer gnus-mime-security-details-buffer)))))
8605
8606 (defun gnus-mime-security-press-button (handle)
8607   (save-excursion
8608     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8609         (gnus-mime-security-show-details handle)
8610       (gnus-mime-security-verify-or-decrypt handle))))
8611
8612 (defun gnus-insert-mime-security-button (handle &optional displayed)
8613   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8614          (gnus-tmp-type
8615           (concat
8616            (or (nth 2 (assoc protocol mm-verify-function-alist))
8617                (nth 2 (assoc protocol mm-decrypt-function-alist))
8618                "Unknown")
8619            (if (equal (car handle) "multipart/signed")
8620                " Signed" " Encrypted")
8621            " Part"))
8622          (gnus-tmp-info
8623           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8624               "Undecided"))
8625          (gnus-tmp-details
8626           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8627          gnus-tmp-pressed-details
8628          b e)
8629     (setq gnus-tmp-details
8630           (if gnus-tmp-details
8631               (concat "\n" gnus-tmp-details)
8632             ""))
8633     (setq gnus-tmp-pressed-details
8634           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8635     (unless (bolp)
8636       (insert "\n"))
8637     (setq b (point))
8638     (gnus-eval-format
8639      gnus-mime-security-button-line-format
8640      gnus-mime-security-button-line-format-alist
8641      `(keymap ,gnus-mime-security-button-map
8642          gnus-callback gnus-mime-security-press-button
8643          gnus-line-format ,gnus-mime-security-button-line-format
8644          gnus-mime-details ,gnus-mime-security-button-pressed
8645          article-type annotation
8646          gnus-data ,handle))
8647     (setq e (if (bolp)
8648                 ;; Exclude a newline.
8649                 (1- (point))
8650               (point)))
8651     (when gnus-article-button-face
8652       (gnus-overlay-put (gnus-make-overlay b e nil t)
8653                         'face gnus-article-button-face))
8654     (widget-convert-button
8655      'link b e
8656      :mime-handle handle
8657      :action 'gnus-widget-press-button
8658      :button-keymap gnus-mime-security-button-map
8659      :help-echo
8660      (lambda (widget)
8661        ;; Needed to properly clear the message due to a bug in
8662        ;; wid-edit (XEmacs only).
8663        (when (boundp 'help-echo-owns-message)
8664          (setq help-echo-owns-message t))
8665        (format
8666         "%S: show detail; %S: more options"
8667         (aref gnus-mouse-2 0)
8668         (aref gnus-down-mouse-3 0))))))
8669
8670 (defun gnus-mime-display-security (handle)
8671   (save-restriction
8672     (narrow-to-region (point) (point))
8673     (unless (gnus-unbuttonized-mime-type-p (car handle))
8674       (gnus-insert-mime-security-button handle))
8675     (gnus-mime-display-part (cadr handle))
8676     (unless (bolp)
8677       (insert "\n"))
8678     (unless (gnus-unbuttonized-mime-type-p (car handle))
8679       (let ((gnus-mime-security-button-line-format
8680              gnus-mime-security-button-end-line-format))
8681         (gnus-insert-mime-security-button handle)))
8682     (mm-set-handle-multipart-parameter
8683      handle 'gnus-region
8684      (cons (set-marker (make-marker) (point-min))
8685            (set-marker (make-marker) (point-max))))
8686     (goto-char (point-max))))
8687
8688 (defun gnus-mime-security-run-function (function)
8689   "Run FUNCTION with the security part under point."
8690   (gnus-article-check-buffer)
8691   (let ((data (get-text-property (point) 'gnus-data))
8692         buffer handle)
8693     (when (and (stringp (car-safe data))
8694                (setq buffer (mm-handle-multipart-original-buffer data))
8695                (setq handle (cadr data)))
8696       (if (bufferp (mm-handle-buffer handle))
8697           (progn
8698             (setq handle (cons buffer (copy-sequence (cdr handle))))
8699             (mm-handle-set-undisplayer handle nil))
8700         (setq handle (mm-make-handle
8701                       buffer
8702                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8703                       nil nil nil nil nil nil)))
8704       (funcall function handle))))
8705
8706 (defun gnus-mime-security-save-part ()
8707   "Save the security part under point."
8708   (interactive)
8709   (gnus-mime-security-run-function 'mm-save-part))
8710
8711 (defun gnus-mime-security-pipe-part ()
8712   "Pipe the security part under point to a process."
8713   (interactive)
8714   (gnus-mime-security-run-function 'mm-pipe-part))
8715
8716 (gnus-ems-redefine)
8717
8718 (provide 'gnus-art)
8719
8720 (run-hooks 'gnus-art-load-hook)
8721
8722 ;;; gnus-art.el ends here