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