d92d29d621c33c0ed8cef387d4afa2f80e08f0ed
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (defvar tool-bar-map)
34 (defvar w3m-minor-mode-map)
35
36 (require 'gnus)
37 (require 'gnus-sum)
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54 (autoload 'mm-url-insert-file-contents-external "mm-url")
55 (autoload 'mm-extern-cache-contents "mm-extern")
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   (mapcar
120    (lambda (header)
121      (concat "^" header ":"))
122    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
123      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
124      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
125      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
126      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
127      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
128      "X-Attribution" "X-Originating-IP" "Delivered-To"
129      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
130      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
131      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
132      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
133      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
134      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
135      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
136      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
137      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
138      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
139      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
140      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
141      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
142      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
143      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
144      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
145      "List-[A-Za-z]+" "X-Listprocessor-Version"
146      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
147      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
148      "X-Received" "Content-length" "X-precedence"
149      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
150      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
151      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
152      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
153      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
154      "X-Content-length" "X-Posting-Agent" "Original-Received"
155      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
156      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
157      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
158      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
159      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
160      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
161      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
162      "Envelope-Sender" "Envelope-Recipients"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice :custom-show nil
167                  regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^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:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-hidden-properties '(invisible t intangible t)
261   "Property list to use for hiding text."
262   :type 'sexp
263   :group 'gnus-article-hiding)
264
265 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
266 ;; frames in a session.
267 (defcustom gnus-article-x-face-command
268   (if (featurep 'xemacs)
269       (if (or (gnus-image-type-available-p 'xface)
270               (gnus-image-type-available-p 'pbm))
271           'gnus-display-x-face-in-from
272         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
273     (if (gnus-image-type-available-p 'pbm)
274         'gnus-display-x-face-in-from
275       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
276 display -"))
277   "*String or function to be executed to display an X-Face header.
278 If it is a string, the command will be executed in a sub-shell
279 asynchronously.  The compressed face will be piped to this command."
280   :type `(choice string
281                  (function-item gnus-display-x-face-in-from)
282                  function)
283   :version "21.1"
284   :group 'gnus-picon
285   :group 'gnus-article-washing)
286
287 (defcustom gnus-article-x-face-too-ugly nil
288   "Regexp matching posters whose face shouldn't be shown automatically."
289   :type '(choice regexp (const nil))
290   :group 'gnus-article-washing)
291
292 (defcustom gnus-article-banner-alist nil
293   "Banner alist for stripping.
294 For example,
295      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
296   :version "21.1"
297   :type '(repeat (cons symbol regexp))
298   :group 'gnus-article-washing)
299
300 (gnus-define-group-parameter
301  banner
302  :variable-document
303  "Alist of regexps (to match group names) and banner."
304  :variable-group gnus-article-washing
305  :parameter-type
306  '(choice :tag "Banner"
307           :value nil
308           (const :tag "Remove signature" signature)
309           (symbol :tag "Item in `gnus-article-banner-alist'" none)
310           regexp
311           (const :tag "None" nil))
312  :parameter-document
313  "If non-nil, specify how to remove `banners' from articles.
314
315 Symbol `signature' means to remove signatures delimited by
316 `gnus-signature-separator'.  Any other symbol is used to look up a
317 regular expression to match the banner in `gnus-article-banner-alist'.
318 A string is used as a regular expression to match the banner
319 directly.")
320
321 (defcustom gnus-article-address-banner-alist nil
322   "Alist of mail addresses and banners.
323 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
324 to match a mail address in the From: header, BANNER is one of a symbol
325 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
326 If ADDRESS matches author's mail address, it will remove things like
327 advertisements.  For example:
328
329 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
330 "
331   :type '(repeat
332           (cons
333            (regexp :tag "Address")
334            (choice :tag "Banner" :value nil
335                    (const :tag "Remove signature" signature)
336                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
337                    regexp
338                    (const :tag "None" nil))))
339   :version "22.1"
340   :group 'gnus-article-washing)
341
342 (defmacro gnus-emphasis-custom-with-format (&rest body)
343   `(let ((format "\
344 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
345 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
346      ,@body))
347
348 (defun gnus-emphasis-custom-value-to-external (value)
349   (gnus-emphasis-custom-with-format
350    (if (consp (car value))
351        (list (format format (car (car value)) (cdr (car value)))
352              2
353              (if (nth 1 value) 2 3)
354              (nth 2 value))
355      value)))
356
357 (defun gnus-emphasis-custom-value-to-internal (value)
358   (gnus-emphasis-custom-with-format
359    (let ((regexp (concat "\\`"
360                          (format (regexp-quote format)
361                                  "\\([^()]+\\)" "\\([^()]+\\)")
362                          "\\'"))
363          pattern)
364      (if (string-match regexp (setq pattern (car value)))
365          (list (cons (match-string 1 pattern) (match-string 2 pattern))
366                (= (nth 2 value) 2)
367                (nth 3 value))
368        value))))
369
370 (defcustom gnus-emphasis-alist
371   (let ((types
372          '(("\\*" "\\*" bold nil 2)
373            ("_" "_" underline)
374            ("/" "/" italic)
375            ("_/" "/_" underline-italic)
376            ("_\\*" "\\*_" underline-bold)
377            ("\\*/" "/\\*" bold-italic)
378            ("_\\*/" "/\\*_" underline-bold-italic))))
379     (nconc
380      (gnus-emphasis-custom-with-format
381       (mapcar (lambda (spec)
382                 (list (format format (car spec) (cadr spec))
383                       (or (nth 3 spec) 2)
384                       (or (nth 4 spec) 3)
385                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
386               types))
387      '(;; I've never seen anyone use this strikethru convention whereas I've
388        ;; several times seen it triggered by normal text.  --Stef
389        ;; Miles suggests that this form is sometimes used but for italics,
390        ;; so maybe we should map it to `italic'.
391        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
392        ;; 2 3 gnus-emphasis-strikethru)
393        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
394         2 3 gnus-emphasis-underline))))
395   "*Alist that says how to fontify certain phrases.
396 Each item looks like this:
397
398   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
399
400 The first element is a regular expression to be matched.  The second
401 is a number that says what regular expression grouping used to find
402 the entire emphasized word.  The third is a number that says what
403 regexp grouping should be displayed and highlighted.  The fourth
404 is the face used for highlighting."
405   :type
406   '(repeat
407     (menu-choice
408      :format "%[Customizing Style%]\n%v"
409      :indent 2
410      (group :tag "Default"
411             :value ("" 0 0 default)
412             :value-create
413             (lambda (widget)
414               (let ((value (widget-get
415                             (cadr (widget-get (widget-get widget :parent)
416                                               :args))
417                             :value)))
418                 (if (not (eq (nth 2 value) 'default))
419                     (widget-put
420                      widget
421                      :value
422                      (gnus-emphasis-custom-value-to-external value))))
423               (widget-group-value-create widget))
424             regexp
425             (integer :format "Match group: %v")
426             (integer :format "Emphasize group: %v")
427             face)
428      (group :tag "Simple"
429             :value (("_" . "_") nil default)
430             (cons :format "%v"
431                   (regexp :format "Start regexp: %v")
432                   (regexp :format "End regexp: %v"))
433             (boolean :format "Show start and end patterns: %[%v%]\n"
434                      :on " On " :off " Off ")
435             face)))
436   :get (lambda (symbol)
437          (mapcar 'gnus-emphasis-custom-value-to-internal
438                  (default-value symbol)))
439   :set (lambda (symbol value)
440          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
441                                      value)))
442   :group 'gnus-article-emphasis)
443
444 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
445   "A regexp to describe whitespace which should not be emphasized.
446 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
447 The former avoids underlining of leading and trailing whitespace,
448 and the latter avoids underlining any whitespace at all."
449   :version "21.1"
450   :group 'gnus-article-emphasis
451   :type 'regexp)
452
453 (defface gnus-emphasis-bold '((t (:bold t)))
454   "Face used for displaying strong emphasized text (*word*)."
455   :group 'gnus-article-emphasis)
456
457 (defface gnus-emphasis-italic '((t (:italic t)))
458   "Face used for displaying italic emphasized text (/word/)."
459   :group 'gnus-article-emphasis)
460
461 (defface gnus-emphasis-underline '((t (:underline t)))
462   "Face used for displaying underlined emphasized text (_word_)."
463   :group 'gnus-article-emphasis)
464
465 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
466   "Face used for displaying underlined bold emphasized text (_*word*_)."
467   :group 'gnus-article-emphasis)
468
469 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
470   "Face used for displaying underlined italic emphasized text (_/word/_)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
474   "Face used for displaying bold italic emphasized text (/*word*/)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-underline-bold-italic
478   '((t (:bold t :italic t :underline t)))
479   "Face used for displaying underlined bold italic emphasized text.
480 Example: (_/*word*/_)."
481   :group 'gnus-article-emphasis)
482
483 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
484                                       '((t (:strikethru t)))
485                                     '((t (:strike-through t))))
486   "Face used for displaying strike-through text (-word-)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-highlight-words
490   '((t (:background "black" :foreground "yellow")))
491   "Face used for displaying highlighted words."
492   :group 'gnus-article-emphasis)
493
494 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
495   "Format for display of Date headers in article bodies.
496 See `format-time-string' for the possible values.
497
498 The variable can also be function, which should return a complete Date
499 header.  The function is called with one argument, the time, which can
500 be fed to `format-time-string'."
501   :type '(choice string function)
502   :link '(custom-manual "(gnus)Article Date")
503   :group 'gnus-article-washing)
504
505 (defcustom gnus-save-all-headers t
506   "*If non-nil, don't remove any headers before saving.
507 This will be overridden by the `:headers' property that the symbol of
508 the saver function, which is specified by `gnus-default-article-saver',
509 might have."
510   :group 'gnus-article-saving
511   :type 'boolean)
512
513 (defcustom gnus-prompt-before-saving 'always
514   "*This variable says how much prompting is to be done when saving articles.
515 If it is nil, no prompting will be done, and the articles will be
516 saved to the default files.  If this variable is `always', each and
517 every article that is saved will be preceded by a prompt, even when
518 saving large batches of articles.  If this variable is neither nil not
519 `always', there the user will be prompted once for a file name for
520 each invocation of the saving commands."
521   :group 'gnus-article-saving
522   :type '(choice (item always)
523                  (item :tag "never" nil)
524                  (sexp :tag "once" :format "%t\n" :value t)))
525
526 (defcustom gnus-saved-headers gnus-visible-headers
527   "Headers to keep if `gnus-save-all-headers' is nil.
528 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
529 If that variable is nil, however, all headers that match this regexp
530 will be kept while the rest will be deleted before saving.  This and
531 `gnus-save-all-headers' will be overridden by the `:headers' property
532 that the symbol of the saver function, which is specified by
533 `gnus-default-article-saver', might have."
534   :group 'gnus-article-saving
535   :type 'regexp)
536
537 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
538 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
539   "A function to save articles in your favourite format.
540 The function will be called by way of the `gnus-summary-save-article'
541 command, and friends such as `gnus-summary-save-article-rmail'.
542
543 Gnus provides the following functions:
544
545 * gnus-summary-save-in-rmail (Rmail format)
546 * gnus-summary-save-in-mail (Unix mail format)
547 * gnus-summary-save-in-folder (MH folder)
548 * gnus-summary-save-in-file (article format)
549 * gnus-summary-save-body-in-file (article body)
550 * gnus-summary-save-in-vm (use VM's folder format)
551 * gnus-summary-write-to-file (article format -- overwrite)
552 * gnus-summary-write-body-to-file (article body -- overwrite)
553 * gnus-summary-save-in-pipe (article format)
554
555 The symbol of each function may have the following properties:
556
557 * :decode
558 The value non-nil means save decoded articles.  This is meaningful
559 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
560 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
561 `gnus-summary-save-in-pipe'.
562
563 * :function
564 The value specifies an alternative function which appends, not
565 overwrites, articles to a file.  This implies that when saving many
566 articles at a time, `gnus-prompt-before-saving' is bound to t and all
567 articles are saved in a single file.  This is meaningful only with
568 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
569
570 * :headers
571 The value specifies the symbol of a variable of which the value
572 specifies headers to be saved.  If it is omitted,
573 `gnus-save-all-headers' and `gnus-saved-headers' control what
574 headers should be saved."
575   :group 'gnus-article-saving
576   :type '(radio (function-item gnus-summary-save-in-rmail)
577                 (function-item gnus-summary-save-in-mail)
578                 (function-item gnus-summary-save-in-folder)
579                 (function-item gnus-summary-save-in-file)
580                 (function-item gnus-summary-save-body-in-file)
581                 (function-item gnus-summary-save-in-vm)
582                 (function-item gnus-summary-write-to-file)
583                 (function-item gnus-summary-write-body-to-file)
584                 (function-item gnus-summary-save-in-pipe)
585                 (function)))
586
587 (defcustom gnus-article-save-coding-system
588   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
589       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
590       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
591       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
592   "Coding system used to save decoded articles to a file.
593
594 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
595 which can safely encode any characters in text.  This is used by the
596 commands including:
597
598 * gnus-summary-save-article-file
599 * gnus-summary-save-article-body-file
600 * gnus-summary-write-article-file
601 * gnus-summary-write-article-body-file
602
603 and the functions to which you may set `gnus-default-article-saver':
604
605 * gnus-summary-save-in-file
606 * gnus-summary-save-body-in-file
607 * gnus-summary-write-to-file
608 * gnus-summary-write-body-to-file
609
610 Those commands and functions save just text displayed in the article
611 buffer to a file if the value of this variable is non-nil.  Note that
612 buttonized MIME parts will be lost in a saved file in that case.
613 Otherwise, raw articles will be saved."
614   :group 'gnus-article-saving
615   :type `(choice
616           :format "%{%t%}:\n %[Value Menu%] %v"
617           (const :tag "Save raw articles" nil)
618           ,@(delq nil
619                   (mapcar
620                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
621                    '((const :tag "UTF-8" utf-8)
622                      (const :tag "iso-2022-7bit" iso-2022-7bit)
623                      (const :tag "Emacs internal" emacs-mule)
624                      (const :tag "escape-quoted" escape-quoted))))
625           (symbol :tag "Coding system")))
626
627 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
628   "A function generating a file name to save articles in Rmail format.
629 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
630   :group 'gnus-article-saving
631   :type 'function)
632
633 (defcustom gnus-mail-save-name 'gnus-plain-save-name
634   "A function generating a file name to save articles in Unix mail format.
635 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
636   :group 'gnus-article-saving
637   :type 'function)
638
639 (defcustom gnus-folder-save-name 'gnus-folder-save-name
640   "A function generating a file name to save articles in MH folder.
641 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
642   :group 'gnus-article-saving
643   :type 'function)
644
645 (defcustom gnus-file-save-name 'gnus-numeric-save-name
646   "A function generating a file name to save articles in article format.
647 The function is called with NEWSGROUP, HEADERS, and optional
648 LAST-FILE."
649   :group 'gnus-article-saving
650   :type 'function)
651
652 (defcustom gnus-split-methods
653   '((gnus-article-archive-name)
654     (gnus-article-nndoc-name))
655   "*Variable used to suggest where articles are to be saved.
656 For instance, if you would like to save articles related to Gnus in
657 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
658 you could set this variable to something like:
659
660  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
661    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
662
663 This variable is an alist where the key is the match and the
664 value is a list of possible files to save in if the match is
665 non-nil.
666
667 If the match is a string, it is used as a regexp match on the
668 article.  If the match is a symbol, that symbol will be funcalled
669 from the buffer of the article to be saved with the newsgroup as the
670 parameter.  If it is a list, it will be evaled in the same buffer.
671
672 If this form or function returns a string, this string will be used as a
673 possible file name; and if it returns a non-nil list, that list will be
674 used as possible file names."
675   :group 'gnus-article-saving
676   :type '(repeat (choice (list :value (fun) function)
677                          (cons :value ("" "") regexp (repeat string))
678                          (sexp :value nil))))
679
680 (defcustom gnus-page-delimiter "^\^L"
681   "*Regexp describing what to use as article page delimiters.
682 The default value is \"^\^L\", which is a form linefeed at the
683 beginning of a line."
684   :type 'regexp
685   :group 'gnus-article-various)
686
687 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
688   "*The format specification for the article mode line.
689 See `gnus-summary-mode-line-format' for a closer description.
690
691 The following additional specs are available:
692
693 %w  The article washing status.
694 %m  The number of MIME parts in the article."
695   :type 'string
696   :group 'gnus-article-various)
697
698 (defcustom gnus-article-mode-hook nil
699   "*A hook for Gnus article mode."
700   :type 'hook
701   :group 'gnus-article-various)
702
703 (when (featurep 'xemacs)
704   ;; Extracted from gnus-xmas-define in order to preserve user settings
705   (when (fboundp 'turn-off-scroll-in-place)
706     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
707   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
708   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
709
710 (defcustom gnus-article-menu-hook nil
711   "*Hook run after the creation of the article mode menu."
712   :type 'hook
713   :group 'gnus-article-various)
714
715 (defcustom gnus-article-prepare-hook nil
716   "*A hook called after an article has been prepared in the article buffer."
717   :type 'hook
718   :group 'gnus-article-various)
719
720 (defcustom gnus-copy-article-ignored-headers nil
721   "List of headers to be removed when copying an article.
722 Each element is a regular expression."
723   :version "23.1" ;; No Gnus
724   :type '(repeat regexp)
725   :group 'gnus-article-various)
726
727 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
728                         "Gnus 5.10 (Emacs-22.1)")
729
730 (defface gnus-button
731   '((t (:weight bold)))
732   "Face used for highlighting a button in the article buffer."
733   :group 'gnus-article-buttons)
734
735 (defcustom gnus-article-button-face 'gnus-button
736   "Face used for highlighting buttons in the article buffer.
737
738 An article button is a piece of text that you can activate by pressing
739 `RET' or `mouse-2' above it."
740   :type 'face
741   :group 'gnus-article-buttons)
742
743 (defcustom gnus-article-mouse-face 'highlight
744   "Face used for mouse highlighting in the article buffer.
745
746 Article buttons will be displayed in this face when the cursor is
747 above them."
748   :type 'face
749   :group 'gnus-article-buttons)
750
751 (defcustom gnus-signature-face 'gnus-signature
752   "Face used for highlighting a signature in the article buffer.
753 Obsolete; use the face `gnus-signature' for customizations instead."
754   :type 'face
755   :group 'gnus-article-highlight
756   :group 'gnus-article-signature)
757
758 (defface gnus-signature
759   '((t
760      (:italic t)))
761   "Face used for highlighting a signature in the article buffer."
762   :group 'gnus-article-highlight
763   :group 'gnus-article-signature)
764 ;; backward-compatibility alias
765 (put 'gnus-signature-face 'face-alias 'gnus-signature)
766 (put 'gnus-signature-face 'obsolete-face "22.1")
767
768 (defface gnus-header-from
769   '((((class color)
770       (background dark))
771      (:foreground "PaleGreen1"))
772     (((class color)
773       (background light))
774      (:foreground "red3"))
775     (t
776      (:italic t)))
777   "Face used for displaying from headers."
778   :group 'gnus-article-headers
779   :group 'gnus-article-highlight)
780 ;; backward-compatibility alias
781 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
782 (put 'gnus-header-from-face 'obsolete-face "22.1")
783
784 (defface gnus-header-subject
785   '((((class color)
786       (background dark))
787      (:foreground "SeaGreen1"))
788     (((class color)
789       (background light))
790      (:foreground "red4"))
791     (t
792      (:bold t :italic t)))
793   "Face used for displaying subject headers."
794   :group 'gnus-article-headers
795   :group 'gnus-article-highlight)
796 ;; backward-compatibility alias
797 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
798 (put 'gnus-header-subject-face 'obsolete-face "22.1")
799
800 (defface gnus-header-newsgroups
801   '((((class color)
802       (background dark))
803      (:foreground "yellow" :italic t))
804     (((class color)
805       (background light))
806      (:foreground "MidnightBlue" :italic t))
807     (t
808      (:italic t)))
809   "Face used for displaying newsgroups headers.
810 In the default setup this face is only used for crossposted
811 articles."
812   :group 'gnus-article-headers
813   :group 'gnus-article-highlight)
814 ;; backward-compatibility alias
815 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
816 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
817
818 (defface gnus-header-name
819   '((((class color)
820       (background dark))
821      (:foreground "SpringGreen2"))
822     (((class color)
823       (background light))
824      (:foreground "maroon"))
825     (t
826      (:bold t)))
827   "Face used for displaying header names."
828   :group 'gnus-article-headers
829   :group 'gnus-article-highlight)
830 ;; backward-compatibility alias
831 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
832 (put 'gnus-header-name-face 'obsolete-face "22.1")
833
834 (defface gnus-header-content
835   '((((class color)
836       (background dark))
837      (:foreground "SpringGreen1" :italic t))
838     (((class color)
839       (background light))
840      (:foreground "indianred4" :italic t))
841     (t
842      (:italic t)))  "Face used for displaying header content."
843   :group 'gnus-article-headers
844   :group 'gnus-article-highlight)
845 ;; backward-compatibility alias
846 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
847 (put 'gnus-header-content-face 'obsolete-face "22.1")
848
849 (defcustom gnus-header-face-alist
850   '(("From" nil gnus-header-from)
851     ("Subject" nil gnus-header-subject)
852     ("Newsgroups:.*," nil gnus-header-newsgroups)
853     ("" gnus-header-name gnus-header-content))
854   "*Controls highlighting of article headers.
855
856 An alist of the form (HEADER NAME CONTENT).
857
858 HEADER is a regular expression which should match the name of a
859 header and NAME and CONTENT are either face names or nil.
860
861 The name of each header field will be displayed using the face
862 specified by the first element in the list where HEADER matches
863 the header name and NAME is non-nil.  Similarly, the content will
864 be displayed by the first non-nil matching CONTENT face."
865   :group 'gnus-article-headers
866   :group 'gnus-article-highlight
867   :type '(repeat (list (regexp :tag "Header")
868                        (choice :tag "Name"
869                                (item :tag "skip" nil)
870                                (face :value default))
871                        (choice :tag "Content"
872                                (item :tag "skip" nil)
873                                (face :value default)))))
874
875 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
876                                           '((xface . (:face gnus-x-face)))
877                                         '((pbm . (:face gnus-x-face))
878                                           (png . nil)))
879   "Alist of image types and properties applied to Face and X-Face images.
880 Here are examples:
881
882 ;; Specify the altitude of Face images in the From header.
883 \(setq gnus-face-properties-alist
884       '((pbm . (:face gnus-x-face :ascent 80))
885         (png . (:ascent 80))))
886
887 ;; Show Face images as pressed buttons.
888 \(setq gnus-face-properties-alist
889       '((pbm . (:face gnus-x-face :relief -2))
890         (png . (:relief -2))))
891
892 See the manual for the valid properties for various image types.
893 Currently, `pbm' is used for X-Face images and `png' is used for Face
894 images in Emacs.  Only the `:face' property is effective on the `xface'
895 image type in XEmacs if it is built with the libcompface library."
896   :version "23.1" ;; No Gnus
897   :group 'gnus-article-headers
898   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
899
900 (defcustom gnus-article-decode-hook
901   '(article-decode-charset article-decode-encoded-words
902                            article-decode-group-name article-decode-idna-rhs)
903   "*Hook run to decode charsets in articles."
904   :group 'gnus-article-headers
905   :type 'hook)
906
907 (defcustom gnus-display-mime-function 'gnus-display-mime
908   "Function to display MIME articles."
909   :group 'gnus-article-mime
910   :type 'function)
911
912 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
913   "Function used to decode headers.")
914
915 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
916   "Function used to decode addresses.")
917
918 (defvar gnus-article-dumbquotes-map
919   '(("\200" "EUR")
920     ("\202" ",")
921     ("\203" "f")
922     ("\204" ",,")
923     ("\205" "...")
924     ("\213" "<")
925     ("\214" "OE")
926     ("\221" "`")
927     ("\222" "'")
928     ("\223" "``")
929     ("\224" "\"")
930     ("\225" "*")
931     ("\226" "-")
932     ("\227" "--")
933     ("\230" "~")
934     ("\231" "(TM)")
935     ("\233" ">")
936     ("\234" "oe")
937     ("\264" "'"))
938   "Table for MS-to-Latin1 translation.")
939
940 (defcustom gnus-ignored-mime-types nil
941   "List of MIME types that should be ignored by Gnus."
942   :version "21.1"
943   :group 'gnus-article-mime
944   :type '(repeat regexp))
945
946 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
947   "List of MIME types that should not be given buttons when rendered inline.
948 See also `gnus-buttonized-mime-types' which may override this variable.
949 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
950   :version "21.1"
951   :group 'gnus-article-mime
952   :type '(repeat regexp))
953
954 (defcustom gnus-buttonized-mime-types nil
955   "List of MIME types that should be given buttons when rendered inline.
956 If set, this variable overrides `gnus-unbuttonized-mime-types'.
957 To see e.g. security buttons you could set this to
958 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
959 this list to display radio buttons that allow you to choose one of two
960 media types those mails include.  See also `mm-discouraged-alternatives'.
961 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
962   :version "22.1"
963   :group 'gnus-article-mime
964   :type '(repeat regexp))
965
966 (defcustom gnus-inhibit-mime-unbuttonizing nil
967   "If non-nil, all MIME parts get buttons.
968 When nil (the default value), then some MIME parts do not get buttons,
969 as described by the variables `gnus-buttonized-mime-types' and
970 `gnus-unbuttonized-mime-types'."
971   :version "22.1"
972   :group 'gnus-article-mime
973   :type 'boolean)
974
975 (defcustom gnus-body-boundary-delimiter "_"
976   "String used to delimit header and body.
977 This variable is used by `gnus-article-treat-body-boundary' which can
978 be controlled by `gnus-treat-body-boundary'."
979   :version "22.1"
980   :group 'gnus-article-various
981   :type '(choice (item :tag "None" :value nil)
982                  string))
983
984 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
985                                   "/usr/share/picons")
986   "Defines the location of the faces database.
987 For information on obtaining this database of pretty pictures, please
988 see http://www.cs.indiana.edu/picons/ftp/index.html"
989   :version "22.1"
990   :type '(repeat directory)
991   :link '(url-link :tag "download"
992                    "http://www.cs.indiana.edu/picons/ftp/index.html")
993   :link '(custom-manual "(gnus)Picons")
994   :group 'gnus-picon)
995
996 (defun gnus-picons-installed-p ()
997   "Say whether picons are installed on your machine."
998   (let ((installed nil))
999     (dolist (database gnus-picon-databases)
1000       (when (file-exists-p database)
1001         (setq installed t)))
1002     installed))
1003
1004 (defcustom gnus-article-mime-part-function nil
1005   "Function called with a MIME handle as the argument.
1006 This is meant for people who want to do something automatic based
1007 on parts -- for instance, adding Vcard info to a database."
1008   :group 'gnus-article-mime
1009   :type '(choice (const nil)
1010                  function))
1011
1012 (defcustom gnus-mime-multipart-functions nil
1013   "An alist of MIME types to functions to display them."
1014   :version "21.1"
1015   :group 'gnus-article-mime
1016   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1017
1018 (defcustom gnus-article-date-lapsed-new-header nil
1019   "Whether the X-Sent and Date headers can coexist.
1020 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1021 either replace the old \"Date:\" header (if this variable is nil), or
1022 be added below it (otherwise)."
1023   :version "21.1"
1024   :group 'gnus-article-headers
1025   :type 'boolean)
1026
1027 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1028   "Function called with a MIME handle as the argument.
1029 This is meant for people who want to view first matched part.
1030 For `undisplayed-alternative' (default), the first undisplayed
1031 part or alternative part is used.  For `undisplayed', the first
1032 undisplayed part is used.  For a function, the first part which
1033 the function return t is used.  For nil, the first part is
1034 used."
1035   :version "21.1"
1036   :group 'gnus-article-mime
1037   :type '(choice
1038           (item :tag "first" :value nil)
1039           (item :tag "undisplayed" :value undisplayed)
1040           (item :tag "undisplayed or alternative"
1041                 :value undisplayed-alternative)
1042           (function)))
1043
1044 (defcustom gnus-mime-action-alist
1045   '(("save to file" . gnus-mime-save-part)
1046     ("save and strip" . gnus-mime-save-part-and-strip)
1047     ("replace with file" . gnus-mime-replace-part)
1048     ("delete part" . gnus-mime-delete-part)
1049     ("display as text" . gnus-mime-inline-part)
1050     ("view the part" . gnus-mime-view-part)
1051     ("pipe to command" . gnus-mime-pipe-part)
1052     ("toggle display" . gnus-article-press-button)
1053     ("toggle display" . gnus-article-view-part-as-charset)
1054     ("view as type" . gnus-mime-view-part-as-type)
1055     ("view internally" . gnus-mime-view-part-internally)
1056     ("view externally" . gnus-mime-view-part-externally))
1057   "An alist of actions that run on the MIME attachment."
1058   :group 'gnus-article-mime
1059   :type '(repeat (cons (string :tag "name")
1060                        (function))))
1061
1062 (defcustom gnus-auto-select-part 1
1063   "Advance to next MIME part when deleting or stripping parts.
1064
1065 When 0, point will be placed on the same part as before.  When
1066 positive (negative), move point forward (backwards) this many
1067 parts.  When nil, redisplay article."
1068   :version "23.1" ;; No Gnus
1069   :group 'gnus-article-mime
1070   :type '(choice (const nil :tag "Redisplay article.")
1071                  (const 1 :tag "Next part.")
1072                  (const 0 :tag "Current part.")
1073                  integer))
1074
1075 ;;;
1076 ;;; The treatment variables
1077 ;;;
1078
1079 (defvar gnus-part-display-hook nil
1080   "Hook called on parts that are to receive treatment.")
1081
1082 (defvar gnus-article-treat-custom
1083   '(choice (const :tag "Off" nil)
1084            (const :tag "On" t)
1085            (const :tag "Header" head)
1086            (const :tag "First" first)
1087            (const :tag "Last" last)
1088            (integer :tag "Less")
1089            (repeat :tag "Groups" regexp)
1090            (sexp :tag "Predicate")))
1091
1092 (defvar gnus-article-treat-head-custom
1093   '(choice (const :tag "Off" nil)
1094            (const :tag "Header" head)))
1095
1096 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1097                                    "text/x-patch")
1098   "Parts to treat.")
1099
1100 (defvar gnus-inhibit-treatment nil
1101   "Whether to inhibit treatment.")
1102
1103 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1104   "Highlight the signature.
1105 Valid values are nil, t, `head', `first', `last', an integer or a
1106 predicate.  See Info node `(gnus)Customizing Articles'."
1107   :group 'gnus-article-treat
1108   :link '(custom-manual "(gnus)Customizing Articles")
1109   :type gnus-article-treat-custom)
1110 (put 'gnus-treat-highlight-signature 'highlight t)
1111
1112 (defcustom gnus-treat-buttonize 100000
1113   "Add buttons.
1114 Valid values are nil, t, `head', `first', `last', an integer or a
1115 predicate.  See Info node `(gnus)Customizing Articles'."
1116   :group 'gnus-article-treat
1117   :link '(custom-manual "(gnus)Customizing Articles")
1118   :type gnus-article-treat-custom)
1119 (put 'gnus-treat-buttonize 'highlight t)
1120
1121 (defcustom gnus-treat-buttonize-head 'head
1122   "Add buttons to the head.
1123 Valid values are nil, t, `head', `first', `last', an integer or a
1124 predicate.  See Info node `(gnus)Customizing Articles'."
1125   :group 'gnus-article-treat
1126   :link '(custom-manual "(gnus)Customizing Articles")
1127   :type gnus-article-treat-head-custom)
1128 (put 'gnus-treat-buttonize-head 'highlight t)
1129
1130 (defcustom gnus-treat-emphasize 50000
1131   "Emphasize text.
1132 Valid values are nil, t, `head', `first', `last', an integer or a
1133 predicate.  See Info node `(gnus)Customizing Articles'."
1134   :group 'gnus-article-treat
1135   :link '(custom-manual "(gnus)Customizing Articles")
1136   :type gnus-article-treat-custom)
1137 (put 'gnus-treat-emphasize 'highlight t)
1138
1139 (defcustom gnus-treat-strip-cr nil
1140   "Remove carriage returns.
1141 Valid values are nil, t, `head', `first', `last', an integer or a
1142 predicate.  See Info node `(gnus)Customizing Articles'."
1143   :version "22.1"
1144   :group 'gnus-article-treat
1145   :link '(custom-manual "(gnus)Customizing Articles")
1146   :type gnus-article-treat-custom)
1147
1148 (defcustom gnus-treat-unsplit-urls nil
1149   "Remove newlines from within URLs.
1150 Valid values are nil, t, `head', `first', `last', an integer or a
1151 predicate.  See Info node `(gnus)Customizing Articles'."
1152   :version "22.1"
1153   :group 'gnus-article-treat
1154   :link '(custom-manual "(gnus)Customizing Articles")
1155   :type gnus-article-treat-custom)
1156
1157 (defcustom gnus-treat-leading-whitespace nil
1158   "Remove leading whitespace in headers.
1159 Valid values are nil, t, `head', `first', `last', an integer or a
1160 predicate.  See Info node `(gnus)Customizing Articles'."
1161   :version "22.1"
1162   :group 'gnus-article-treat
1163   :link '(custom-manual "(gnus)Customizing Articles")
1164   :type gnus-article-treat-custom)
1165
1166 (defcustom gnus-treat-hide-headers 'head
1167   "Hide headers.
1168 Valid values are nil, t, `head', `first', `last', an integer or a
1169 predicate.  See Info node `(gnus)Customizing Articles'."
1170   :group 'gnus-article-treat
1171   :link '(custom-manual "(gnus)Customizing Articles")
1172   :type gnus-article-treat-head-custom)
1173
1174 (defcustom gnus-treat-hide-boring-headers nil
1175   "Hide boring 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-signature nil
1183   "Hide the signature.
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-custom)
1189
1190 (defcustom gnus-treat-fill-article nil
1191   "Fill the article.
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-hide-citation nil
1199   "Hide cited text.
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-maybe 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-strip-list-identifiers 'head
1215   "Strip list identifiers from `gnus-list-identifiers`.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :version "21.1"
1219   :group 'gnus-article-treat
1220   :link '(custom-manual "(gnus)Customizing Articles")
1221   :type gnus-article-treat-custom)
1222
1223 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1224                         "Gnus 5.10 (Emacs 22.1)")
1225
1226 (defcustom gnus-treat-strip-pem nil
1227   "Strip PEM signatures.
1228 Valid values are nil, t, `head', `first', `last', an integer or a
1229 predicate.  See Info node `(gnus)Customizing Articles'."
1230   :group 'gnus-article-treat
1231   :link '(custom-manual "(gnus)Customizing Articles")
1232   :type gnus-article-treat-custom)
1233
1234 (defcustom gnus-treat-strip-banner t
1235   "Strip banners from articles.
1236 The banner to be stripped is specified in the `banner' group parameter.
1237 Valid values are nil, t, `head', `first', `last', an integer or a
1238 predicate.  See Info node `(gnus)Customizing Articles'."
1239   :group 'gnus-article-treat
1240   :link '(custom-manual "(gnus)Customizing Articles")
1241   :type gnus-article-treat-custom)
1242
1243 (defcustom gnus-treat-highlight-headers 'head
1244   "Highlight the headers.
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-head-custom)
1250 (put 'gnus-treat-highlight-headers 'highlight t)
1251
1252 (defcustom gnus-treat-highlight-citation t
1253   "Highlight cited text.
1254 Valid values are nil, t, `head', `first', `last', an integer or a
1255 predicate.  See Info node `(gnus)Customizing Articles'."
1256   :group 'gnus-article-treat
1257   :link '(custom-manual "(gnus)Customizing Articles")
1258   :type gnus-article-treat-custom)
1259 (put 'gnus-treat-highlight-citation 'highlight t)
1260
1261 (defcustom gnus-treat-date-ut nil
1262   "Display the Date in UT (GMT).
1263 Valid values are nil, t, `head', `first', `last', an integer or a
1264 predicate.  See Info node `(gnus)Customizing Articles'."
1265   :group 'gnus-article-treat
1266   :link '(custom-manual "(gnus)Customizing Articles")
1267   :type gnus-article-treat-head-custom)
1268
1269 (defcustom gnus-treat-date-local nil
1270   "Display the Date in the local timezone.
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-english nil
1278   "Display the Date in a format that can be read aloud in English.
1279 Valid values are nil, t, `head', `first', `last', an integer or a
1280 predicate.  See Info node `(gnus)Customizing Articles'."
1281   :version "22.1"
1282   :group 'gnus-article-treat
1283   :link '(custom-manual "(gnus)Customizing Articles")
1284   :type gnus-article-treat-head-custom)
1285
1286 (defcustom gnus-treat-date-lapsed nil
1287   "Display the Date header in a way that says how much time has elapsed.
1288 Valid values are nil, t, `head', `first', `last', an integer or a
1289 predicate.  See Info node `(gnus)Customizing Articles'."
1290   :group 'gnus-article-treat
1291   :link '(custom-manual "(gnus)Customizing Articles")
1292   :type gnus-article-treat-head-custom)
1293
1294 (defcustom gnus-treat-date-original nil
1295   "Display the date in the original timezone.
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-iso8601 nil
1303   "Display the date in the ISO8601 format.
1304 Valid values are nil, t, `head', `first', `last', an integer or a
1305 predicate.  See Info node `(gnus)Customizing Articles'."
1306   :version "21.1"
1307   :group 'gnus-article-treat
1308   :link '(custom-manual "(gnus)Customizing Articles")
1309   :type gnus-article-treat-head-custom)
1310
1311 (defcustom gnus-treat-date-user-defined nil
1312   "Display the date in a user-defined format.
1313 The format is defined by the `gnus-article-time-format' variable.
1314 Valid values are nil, t, `head', `first', `last', an integer or a
1315 predicate.  See Info node `(gnus)Customizing Articles'."
1316   :group 'gnus-article-treat
1317   :link '(custom-manual "(gnus)Customizing Articles")
1318   :type gnus-article-treat-head-custom)
1319
1320 (defcustom gnus-treat-strip-headers-in-body t
1321   "Strip the X-No-Archive header line from the beginning of the body.
1322 Valid values are nil, t, `head', `first', `last', an integer or a
1323 predicate.  See Info node `(gnus)Customizing Articles'."
1324   :version "21.1"
1325   :group 'gnus-article-treat
1326   :link '(custom-manual "(gnus)Customizing Articles")
1327   :type gnus-article-treat-custom)
1328
1329 (defcustom gnus-treat-strip-trailing-blank-lines nil
1330   "Strip trailing blank lines.
1331 Valid values are nil, t, `head', `first', `last', an integer or a
1332 predicate.  See Info node `(gnus)Customizing Articles'.
1333
1334 When set to t, it also strips trailing blanks in all MIME parts.
1335 Consider to use `last' instead."
1336   :group 'gnus-article-treat
1337   :link '(custom-manual "(gnus)Customizing Articles")
1338   :type gnus-article-treat-custom)
1339
1340 (defcustom gnus-treat-strip-leading-blank-lines nil
1341   "Strip leading blank lines.
1342 Valid values are nil, t, `head', `first', `last', an integer or a
1343 predicate.  See Info node `(gnus)Customizing Articles'.
1344
1345 When set to t, it also strips trailing blanks in all MIME parts."
1346   :group 'gnus-article-treat
1347   :link '(custom-manual "(gnus)Customizing Articles")
1348   :type gnus-article-treat-custom)
1349
1350 (defcustom gnus-treat-strip-multiple-blank-lines nil
1351   "Strip multiple blank lines.
1352 Valid values are nil, t, `head', `first', `last', an integer or a
1353 predicate.  See Info node `(gnus)Customizing Articles'."
1354   :group 'gnus-article-treat
1355   :link '(custom-manual "(gnus)Customizing Articles")
1356   :type gnus-article-treat-custom)
1357
1358 (defcustom gnus-treat-unfold-headers 'head
1359   "Unfold folded header lines.
1360 Valid values are nil, t, `head', `first', `last', an integer or a
1361 predicate.  See Info node `(gnus)Customizing Articles'."
1362   :version "22.1"
1363   :group 'gnus-article-treat
1364   :link '(custom-manual "(gnus)Customizing Articles")
1365   :type gnus-article-treat-custom)
1366
1367 (defcustom gnus-article-unfold-long-headers nil
1368   "If non-nil, allow unfolding headers even if the header is long.
1369 If it is a regexp, only long headers matching this regexp are unfolded.
1370 If it is t, all long headers are unfolded.
1371
1372 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1373   :version "23.1" ;; No Gnus
1374   :group 'gnus-article-treat
1375   :type '(choice (const nil)
1376                  (const :tag "all" t)
1377                  (regexp)))
1378
1379 (defcustom gnus-treat-fold-headers nil
1380   "Fold headers.
1381 Valid values are nil, t, `head', `first', `last', an integer or a
1382 predicate.  See Info node `(gnus)Customizing Articles'."
1383   :version "22.1"
1384   :group 'gnus-article-treat
1385   :link '(custom-manual "(gnus)Customizing Articles")
1386   :type gnus-article-treat-custom)
1387
1388 (defcustom gnus-treat-fold-newsgroups 'head
1389   "Fold the Newsgroups and Followup-To headers.
1390 Valid values are nil, t, `head', `first', `last', an integer or a
1391 predicate.  See Info node `(gnus)Customizing Articles'."
1392   :version "22.1"
1393   :group 'gnus-article-treat
1394   :link '(custom-manual "(gnus)Customizing Articles")
1395   :type gnus-article-treat-custom)
1396
1397 (defcustom gnus-treat-overstrike t
1398   "Treat overstrike highlighting.
1399 Valid values are nil, t, `head', `first', `last', an integer or a
1400 predicate.  See Info node `(gnus)Customizing Articles'."
1401   :group 'gnus-article-treat
1402   :link '(custom-manual "(gnus)Customizing Articles")
1403   :type gnus-article-treat-custom)
1404 (put 'gnus-treat-overstrike 'highlight t)
1405
1406 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1407   "Treat ANSI SGR control sequences.
1408 Valid values are nil, t, `head', `first', `last', an integer or a
1409 predicate.  See Info node `(gnus)Customizing Articles'."
1410   :group 'gnus-article-treat
1411   :link '(custom-manual "(gnus)Customizing Articles")
1412   :type gnus-article-treat-custom)
1413
1414 (make-obsolete-variable 'gnus-treat-display-xface
1415                         'gnus-treat-display-x-face "22.1")
1416
1417 (defcustom gnus-treat-display-x-face
1418   (and (not noninteractive)
1419        (gnus-image-type-available-p 'xbm)
1420        (if (featurep 'xemacs)
1421            (featurep 'xface)
1422          (condition-case nil
1423              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1424                   (executable-find "icontopbm"))
1425            ;; shell-command-to-string may signal an error, e.g. if
1426            ;; shell-file-name is not found.
1427            (error nil)))
1428        'head)
1429   "Display X-Face headers.
1430 Valid values are nil and `head'.
1431 See Info node `(gnus)Customizing Articles' and Info node
1432 `(gnus)X-Face' for details."
1433   :group 'gnus-article-treat
1434   :version "21.1"
1435   :link '(custom-manual "(gnus)Customizing Articles")
1436   :link '(custom-manual "(gnus)X-Face")
1437   :type gnus-article-treat-head-custom
1438   :set (lambda (symbol value)
1439          (set-default
1440           symbol
1441           (cond ((or (boundp symbol) (get symbol 'saved-value))
1442                  value)
1443                 ((boundp 'gnus-treat-display-xface)
1444                  (message "\
1445 ** gnus-treat-display-xface is an obsolete variable;\
1446  use gnus-treat-display-x-face instead")
1447                  (default-value 'gnus-treat-display-xface))
1448                 ((get 'gnus-treat-display-xface 'saved-value)
1449                  (message "\
1450 ** gnus-treat-display-xface is an obsolete variable;\
1451  use gnus-treat-display-x-face instead")
1452                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1453                 (t
1454                  value)))))
1455 (put 'gnus-treat-display-x-face 'highlight t)
1456
1457 (defcustom gnus-treat-display-face
1458   (and (not noninteractive)
1459        (gnus-image-type-available-p 'png)
1460        'head)
1461   "Display Face headers.
1462 Valid values are nil, t, `head', `first', `last', an integer or a
1463 predicate.  See Info node `(gnus)Customizing Articles' and Info
1464 node `(gnus)Face' for details."
1465   :group 'gnus-article-treat
1466   :version "22.1"
1467   :link '(custom-manual "(gnus)Customizing Articles")
1468   :link '(custom-manual "(gnus)X-Face")
1469   :type gnus-article-treat-head-custom)
1470 (put 'gnus-treat-display-face 'highlight t)
1471
1472 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1473   "Display smileys.
1474 Valid values are nil, t, `head', `first', `last', an integer or a
1475 predicate.  See Info node `(gnus)Customizing Articles' and Info
1476 node `(gnus)Smileys' for details."
1477   :group 'gnus-article-treat
1478   :version "21.1"
1479   :link '(custom-manual "(gnus)Customizing Articles")
1480   :link '(custom-manual "(gnus)Smileys")
1481   :type gnus-article-treat-custom)
1482 (put 'gnus-treat-display-smileys 'highlight t)
1483
1484 (defcustom gnus-treat-from-picon
1485   (if (and (gnus-image-type-available-p 'xpm)
1486            (gnus-picons-installed-p))
1487       'head nil)
1488   "Display picons in the From header.
1489 Valid values are nil, t, `head', `first', `last', an integer or a
1490 predicate.  See Info node `(gnus)Customizing Articles' and Info
1491 node `(gnus)Picons' for details."
1492   :version "22.1"
1493   :group 'gnus-article-treat
1494   :group 'gnus-picon
1495   :link '(custom-manual "(gnus)Customizing Articles")
1496   :link '(custom-manual "(gnus)Picons")
1497   :type gnus-article-treat-head-custom)
1498 (put 'gnus-treat-from-picon 'highlight t)
1499
1500 (defcustom gnus-treat-mail-picon
1501   (if (and (gnus-image-type-available-p 'xpm)
1502            (gnus-picons-installed-p))
1503       'head nil)
1504   "Display picons in To and Cc headers.
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-mail-picon 'highlight t)
1515
1516 (defcustom gnus-treat-newsgroups-picon
1517   (if (and (gnus-image-type-available-p 'xpm)
1518            (gnus-picons-installed-p))
1519       'head nil)
1520   "Display picons in the Newsgroups and Followup-To 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-newsgroups-picon 'highlight t)
1531
1532 (defcustom gnus-treat-body-boundary
1533   (if (or gnus-treat-newsgroups-picon
1534           gnus-treat-mail-picon
1535           gnus-treat-from-picon)
1536       ;; If there's much decoration, the user might prefer a boundery.
1537       'head
1538     nil)
1539   "Draw a boundary at the end of the headers.
1540 Valid values are nil and `head'.
1541 See Info node `(gnus)Customizing Articles' for details."
1542   :version "22.1"
1543   :group 'gnus-article-treat
1544   :link '(custom-manual "(gnus)Customizing Articles")
1545   :type gnus-article-treat-head-custom)
1546
1547 (defcustom gnus-treat-capitalize-sentences nil
1548   "Capitalize sentence-starting words.
1549 Valid values are nil, t, `head', `first', `last', an integer or a
1550 predicate.  See Info node `(gnus)Customizing Articles'."
1551   :version "21.1"
1552   :group 'gnus-article-treat
1553   :link '(custom-manual "(gnus)Customizing Articles")
1554   :type gnus-article-treat-custom)
1555
1556 (defcustom gnus-treat-wash-html nil
1557   "Format as HTML.
1558 Valid values are nil, t, `head', `first', `last', an integer or a
1559 predicate.  See Info node `(gnus)Customizing Articles'."
1560   :version "22.1"
1561   :group 'gnus-article-treat
1562   :link '(custom-manual "(gnus)Customizing Articles")
1563   :type gnus-article-treat-custom)
1564
1565 (defcustom gnus-treat-fill-long-lines nil
1566   "Fill long lines.
1567 Valid values are nil, t, `head', `first', `last', an integer or a
1568 predicate.  See Info node `(gnus)Customizing Articles'."
1569   :group 'gnus-article-treat
1570   :link '(custom-manual "(gnus)Customizing Articles")
1571   :type gnus-article-treat-custom)
1572
1573 (defcustom gnus-treat-play-sounds nil
1574   "Play sounds.
1575 Valid values are nil, t, `head', `first', `last', an integer or a
1576 predicate.  See Info node `(gnus)Customizing Articles'."
1577   :version "21.1"
1578   :group 'gnus-article-treat
1579   :link '(custom-manual "(gnus)Customizing Articles")
1580   :type gnus-article-treat-custom)
1581
1582 (defcustom gnus-treat-translate nil
1583   "Translate articles from one language to another.
1584 Valid values are nil, t, `head', `first', `last', an integer or a
1585 predicate.  See Info node `(gnus)Customizing Articles'."
1586   :version "21.1"
1587   :group 'gnus-article-treat
1588   :link '(custom-manual "(gnus)Customizing Articles")
1589   :type gnus-article-treat-custom)
1590
1591 (defcustom gnus-treat-x-pgp-sig nil
1592   "Verify X-PGP-Sig.
1593 To automatically treat X-PGP-Sig, set it to head.
1594 Valid values are nil, t, `head', `first', `last', an integer or a
1595 predicate.  See Info node `(gnus)Customizing Articles'."
1596   :version "22.1"
1597   :group 'gnus-article-treat
1598   :group 'mime-security
1599   :link '(custom-manual "(gnus)Customizing Articles")
1600   :type gnus-article-treat-custom)
1601
1602 (defvar gnus-article-encrypt-protocol-alist
1603   '(("PGP" . mml2015-self-encrypt)))
1604
1605 ;; Set to nil if more than one protocol added to
1606 ;; gnus-article-encrypt-protocol-alist.
1607 (defcustom gnus-article-encrypt-protocol "PGP"
1608   "The protocol used for encrypt articles.
1609 It is a string, such as \"PGP\". If nil, ask user."
1610   :version "22.1"
1611   :type 'string
1612   :group 'mime-security)
1613
1614 (defvar gnus-article-wash-function nil
1615   "Function used for converting HTML into text.")
1616
1617 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1618                               (mm-coding-system-p 'utf-8)
1619                               (executable-find idna-program))
1620   "Whether IDNA decoding of headers is used when viewing messages.
1621 This requires GNU Libidn, and by default only enabled if it is found."
1622   :version "22.1"
1623   :group 'gnus-article-headers
1624   :type 'boolean)
1625
1626 (defcustom gnus-article-over-scroll nil
1627   "If non-nil, allow scrolling the article buffer even when there no more text."
1628   :version "22.1"
1629   :group 'gnus-article
1630   :type 'boolean)
1631
1632 ;;; Internal variables
1633
1634 (defvar gnus-english-month-names
1635   '("January" "February" "March" "April" "May" "June" "July" "August"
1636     "September" "October" "November" "December"))
1637
1638 (defvar article-goto-body-goes-to-point-min-p nil)
1639 (defvar gnus-article-wash-types nil)
1640 (defvar gnus-article-emphasis-alist nil)
1641 (defvar gnus-article-image-alist nil)
1642
1643 (defvar gnus-article-mime-handle-alist-1 nil)
1644 (defvar gnus-treatment-function-alist
1645   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1646     (gnus-treat-strip-banner gnus-article-strip-banner)
1647     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1648     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1649     (gnus-treat-buttonize gnus-article-add-buttons)
1650     (gnus-treat-fill-article gnus-article-fill-cited-article)
1651     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1652     (gnus-treat-strip-cr gnus-article-remove-cr)
1653     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1654     (gnus-treat-date-ut gnus-article-date-ut)
1655     (gnus-treat-date-local gnus-article-date-local)
1656     (gnus-treat-date-english gnus-article-date-english)
1657     (gnus-treat-date-original gnus-article-date-original)
1658     (gnus-treat-date-user-defined gnus-article-date-user)
1659     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1660     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1661     (gnus-treat-display-x-face gnus-article-display-x-face)
1662     (gnus-treat-display-face gnus-article-display-face)
1663     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1664     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1665     (gnus-treat-hide-signature gnus-article-hide-signature)
1666     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1667     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1668     (gnus-treat-strip-pem gnus-article-hide-pem)
1669     (gnus-treat-from-picon gnus-treat-from-picon)
1670     (gnus-treat-mail-picon gnus-treat-mail-picon)
1671     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1672     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1673     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1674     (gnus-treat-strip-trailing-blank-lines
1675      gnus-article-remove-trailing-blank-lines)
1676     (gnus-treat-strip-leading-blank-lines
1677      gnus-article-strip-leading-blank-lines)
1678     (gnus-treat-strip-multiple-blank-lines
1679      gnus-article-strip-multiple-blank-lines)
1680     (gnus-treat-overstrike gnus-article-treat-overstrike)
1681     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1682     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1683     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1684     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1685     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1686     (gnus-treat-display-smileys gnus-treat-smiley)
1687     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1688     (gnus-treat-wash-html gnus-article-wash-html)
1689     (gnus-treat-emphasize gnus-article-emphasize)
1690     (gnus-treat-hide-citation gnus-article-hide-citation)
1691     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1692     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1693     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1694     (gnus-treat-play-sounds gnus-earcon-display)))
1695
1696 (defvar gnus-article-mime-handle-alist nil)
1697 (defvar article-lapsed-timer nil)
1698 (defvar gnus-article-current-summary nil)
1699
1700 (defvar gnus-article-mode-syntax-table
1701   (let ((table (copy-syntax-table text-mode-syntax-table)))
1702     ;; This causes the citation match run O(2^n).
1703     ;; (modify-syntax-entry ?- "w" table)
1704     (modify-syntax-entry ?> ")<" table)
1705     (modify-syntax-entry ?< "(>" table)
1706     ;; make M-. in article buffers work for `foo' strings
1707     (modify-syntax-entry ?' " " table)
1708     (modify-syntax-entry ?` " " table)
1709     table)
1710   "Syntax table used in article mode buffers.
1711 Initialized from `text-mode-syntax-table.")
1712
1713 (defvar gnus-save-article-buffer nil)
1714
1715 (defvar gnus-number-of-articles-to-be-saved nil)
1716
1717 (defvar gnus-inhibit-hiding nil)
1718
1719 (defvar gnus-article-edit-mode nil)
1720
1721 ;;; Macros for dealing with the article buffer.
1722
1723 (defmacro gnus-with-article-headers (&rest forms)
1724   `(with-current-buffer gnus-article-buffer
1725      (save-restriction
1726        (let ((inhibit-read-only t)
1727              (inhibit-point-motion-hooks t)
1728              (case-fold-search t))
1729          (article-narrow-to-head)
1730          ,@forms))))
1731
1732 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1733 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1734
1735 (defmacro gnus-with-article-buffer (&rest forms)
1736   `(with-current-buffer gnus-article-buffer
1737      (let ((inhibit-read-only t))
1738        ,@forms)))
1739
1740 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1741 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1742
1743 (defun gnus-article-goto-header (header)
1744   "Go to HEADER, which is a regular expression."
1745   (re-search-forward (concat "^\\(" header "\\):") nil t))
1746
1747 (defsubst gnus-article-hide-text (b e props)
1748   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1749   (gnus-add-text-properties-when 'article-type nil b e props)
1750   (when (memq 'intangible props)
1751     (put-text-property
1752      (max (1- b) (point-min))
1753      b 'intangible (cddr (memq 'intangible props)))))
1754
1755 (defsubst gnus-article-unhide-text (b e)
1756   "Remove hidden text properties from region between B and E."
1757   (remove-text-properties b e gnus-hidden-properties)
1758   (when (memq 'intangible gnus-hidden-properties)
1759     (put-text-property (max (1- b) (point-min))
1760                        b 'intangible nil)))
1761
1762 (defun gnus-article-hide-text-type (b e type)
1763   "Hide text of TYPE between B and E."
1764   (gnus-add-wash-type type)
1765   (gnus-article-hide-text
1766    b e (cons 'article-type (cons type gnus-hidden-properties))))
1767
1768 (defun gnus-article-unhide-text-type (b e type)
1769   "Unhide text of TYPE between B and E."
1770   (gnus-delete-wash-type type)
1771   (remove-text-properties
1772    b e (cons 'article-type (cons type gnus-hidden-properties)))
1773   (when (memq 'intangible gnus-hidden-properties)
1774     (put-text-property (max (1- b) (point-min))
1775                        b 'intangible nil)))
1776
1777 (defun gnus-article-hide-text-of-type (type)
1778   "Hide text of TYPE in the current buffer."
1779   (save-excursion
1780     (let ((b (point-min))
1781           (e (point-max)))
1782       (while (setq b (text-property-any b e 'article-type type))
1783         (add-text-properties b (incf b) gnus-hidden-properties)))))
1784
1785 (defun gnus-article-delete-text-of-type (type)
1786   "Delete text of TYPE in the current buffer."
1787   (save-excursion
1788     (let ((b (point-min)))
1789       (if (eq type 'multipart)
1790           ;; Remove MIME buttons associated with multipart/alternative parts.
1791           (progn
1792             (goto-char b)
1793             (while (if (get-text-property (point) 'gnus-part)
1794                        (setq b (point))
1795                      (when (setq b (next-single-property-change (point)
1796                                                                 'gnus-part))
1797                        (goto-char b)
1798                        t))
1799               (end-of-line)
1800               (skip-chars-forward "\n")
1801               (when (eq (get-text-property b 'article-type) 'multipart)
1802                 (delete-region b (point)))))
1803         (while (setq b (text-property-any b (point-max) 'article-type type))
1804           (delete-region
1805            b (or (text-property-not-all b (point-max) 'article-type type)
1806                  (point-max))))))))
1807
1808 (defun gnus-article-delete-invisible-text ()
1809   "Delete all invisible text in the current buffer."
1810   (save-excursion
1811     (let ((b (point-min)))
1812       (while (setq b (text-property-any b (point-max) 'invisible t))
1813         (delete-region
1814          b (or (text-property-not-all b (point-max) 'invisible t)
1815                (point-max)))))))
1816
1817 (defun gnus-article-text-type-exists-p (type)
1818   "Say whether any text of type TYPE exists in the buffer."
1819   (text-property-any (point-min) (point-max) 'article-type type))
1820
1821 (defsubst gnus-article-header-rank ()
1822   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1823   (let ((list gnus-sorted-header-list)
1824         (i 1))
1825     (while list
1826       (if (looking-at (car list))
1827           (setq list nil)
1828         (setq list (cdr list))
1829         (incf i)))
1830       i))
1831
1832 (defun article-hide-headers (&optional arg delete)
1833   "Hide unwanted headers and possibly sort them as well."
1834   (interactive)
1835   ;; This function might be inhibited.
1836   (unless gnus-inhibit-hiding
1837     (let ((inhibit-read-only t)
1838           (case-fold-search t)
1839           (max (1+ (length gnus-sorted-header-list)))
1840           (inhibit-point-motion-hooks t)
1841           (cur (current-buffer))
1842           ignored visible beg)
1843       (save-excursion
1844         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1845         ;; group parameters, so we should go to the summary buffer.
1846         (when (prog1
1847                   (condition-case nil
1848                       (progn (set-buffer gnus-summary-buffer) t)
1849                     (error nil))
1850                 (setq ignored (when (not gnus-visible-headers)
1851                                 (cond ((stringp gnus-ignored-headers)
1852                                        gnus-ignored-headers)
1853                                       ((listp gnus-ignored-headers)
1854                                        (mapconcat 'identity
1855                                                   gnus-ignored-headers
1856                                                   "\\|"))))
1857                       visible (cond ((stringp gnus-visible-headers)
1858                                      gnus-visible-headers)
1859                                     ((and gnus-visible-headers
1860                                           (listp gnus-visible-headers))
1861                                      (mapconcat 'identity
1862                                                 gnus-visible-headers
1863                                                 "\\|")))))
1864           (set-buffer cur))
1865         (save-restriction
1866           ;; First we narrow to just the headers.
1867           (article-narrow-to-head)
1868           ;; Hide any "From " lines at the beginning of (mail) articles.
1869           (while (looking-at "From ")
1870             (forward-line 1))
1871           (unless (bobp)
1872             (delete-region (point-min) (point)))
1873           ;; Then treat the rest of the header lines.
1874           ;; Then we use the two regular expressions
1875           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1876           ;; select which header lines is to remain visible in the
1877           ;; article buffer.
1878           (while (re-search-forward "^[^ \t:]*:" nil t)
1879             (beginning-of-line)
1880             ;; Mark the rank of the header.
1881             (put-text-property
1882              (point) (1+ (point)) 'message-rank
1883              (if (or (and visible (looking-at visible))
1884                      (and ignored
1885                           (not (looking-at ignored))))
1886                  (gnus-article-header-rank)
1887                (+ 2 max)))
1888             (forward-line 1))
1889           (message-sort-headers-1)
1890           (when (setq beg (text-property-any
1891                            (point-min) (point-max) 'message-rank (+ 2 max)))
1892             ;; We delete the unwanted headers.
1893             (gnus-add-wash-type 'headers)
1894             (add-text-properties (point-min) (+ 5 (point-min))
1895                                  '(article-type headers dummy-invisible t))
1896             (delete-region beg (point-max))))))))
1897
1898 (defun article-hide-boring-headers (&optional arg)
1899   "Toggle hiding of headers that aren't very interesting.
1900 If given a negative prefix, always show; if given a positive prefix,
1901 always hide."
1902   (interactive (gnus-article-hidden-arg))
1903   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1904              (not gnus-show-all-headers))
1905     (save-excursion
1906       (save-restriction
1907         (let ((inhibit-read-only t)
1908               (inhibit-point-motion-hooks t))
1909           (article-narrow-to-head)
1910           (dolist (elem gnus-boring-article-headers)
1911             (goto-char (point-min))
1912             (cond
1913              ;; Hide empty headers.
1914              ((eq elem 'empty)
1915               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1916                 (forward-line -1)
1917                 (gnus-article-hide-text-type
1918                  (point-at-bol)
1919                  (progn
1920                    (end-of-line)
1921                    (if (re-search-forward "^[^ \t]" nil t)
1922                        (match-beginning 0)
1923                      (point-max)))
1924                  'boring-headers)))
1925              ;; Hide boring Newsgroups header.
1926              ((eq elem 'newsgroups)
1927               (when (gnus-string-equal
1928                      (gnus-fetch-field "newsgroups")
1929                      (gnus-group-real-name
1930                       (if (boundp 'gnus-newsgroup-name)
1931                           gnus-newsgroup-name
1932                         "")))
1933                 (gnus-article-hide-header "newsgroups")))
1934              ((eq elem 'to-address)
1935               (let ((to (message-fetch-field "to"))
1936                     (to-address
1937                      (gnus-parameter-to-address
1938                       (if (boundp 'gnus-newsgroup-name)
1939                           gnus-newsgroup-name ""))))
1940                 (when (and to to-address
1941                            (ignore-errors
1942                              (gnus-string-equal
1943                               ;; only one address in To
1944                               (nth 1 (mail-extract-address-components to))
1945                               to-address)))
1946                   (gnus-article-hide-header "to"))))
1947              ((eq elem 'to-list)
1948               (let ((to (message-fetch-field "to"))
1949                     (to-list
1950                      (gnus-parameter-to-list
1951                       (if (boundp 'gnus-newsgroup-name)
1952                           gnus-newsgroup-name ""))))
1953                 (when (and to to-list
1954                            (ignore-errors
1955                              (gnus-string-equal
1956                               ;; only one address in To
1957                               (nth 1 (mail-extract-address-components to))
1958                               to-list)))
1959                   (gnus-article-hide-header "to"))))
1960              ((eq elem 'cc-list)
1961               (let ((cc (message-fetch-field "cc"))
1962                     (to-list
1963                      (gnus-parameter-to-list
1964                       (if (boundp 'gnus-newsgroup-name)
1965                           gnus-newsgroup-name ""))))
1966                 (when (and cc to-list
1967                            (ignore-errors
1968                              (gnus-string-equal
1969                               ;; only one address in CC
1970                               (nth 1 (mail-extract-address-components cc))
1971                               to-list)))
1972                   (gnus-article-hide-header "cc"))))
1973              ((eq elem 'followup-to)
1974               (when (gnus-string-equal
1975                      (message-fetch-field "followup-to")
1976                      (message-fetch-field "newsgroups"))
1977                 (gnus-article-hide-header "followup-to")))
1978              ((eq elem 'reply-to)
1979               (if (gnus-group-find-parameter
1980                    gnus-newsgroup-name 'broken-reply-to)
1981                   (gnus-article-hide-header "reply-to")
1982                 (let ((from (message-fetch-field "from"))
1983                       (reply-to (message-fetch-field "reply-to")))
1984                   (when
1985                       (and
1986                        from reply-to
1987                        (ignore-errors
1988                          (equal
1989                           (sort (mapcar
1990                                  (lambda (x) (downcase (cadr x)))
1991                                  (mail-extract-address-components from t))
1992                                 'string<)
1993                           (sort (mapcar
1994                                  (lambda (x) (downcase (cadr x)))
1995                                  (mail-extract-address-components reply-to t))
1996                                 'string<))))
1997                     (gnus-article-hide-header "reply-to")))))
1998              ((eq elem 'date)
1999               (let ((date (with-current-buffer gnus-original-article-buffer
2000                             ;; If date in `gnus-article-buffer' is localized
2001                             ;; (`gnus-treat-date-user-defined'),
2002                             ;; `days-between' might fail.
2003                             (message-fetch-field "date"))))
2004                 (when (and date
2005                            (< (days-between (current-time-string) date)
2006                               4))
2007                   (gnus-article-hide-header "date"))))
2008              ((eq elem 'long-to)
2009               (let ((to (message-fetch-field "to"))
2010                     (cc (message-fetch-field "cc")))
2011                 (when (> (length to) 1024)
2012                   (gnus-article-hide-header "to"))
2013                 (when (> (length cc) 1024)
2014                   (gnus-article-hide-header "cc"))))
2015              ((eq elem 'many-to)
2016               (let ((to-count 0)
2017                     (cc-count 0))
2018                 (goto-char (point-min))
2019                 (while (re-search-forward "^to:" nil t)
2020                   (setq to-count (1+ to-count)))
2021                 (when (> to-count 1)
2022                   (while (> to-count 0)
2023                     (goto-char (point-min))
2024                     (save-restriction
2025                       (re-search-forward "^to:" nil nil to-count)
2026                       (forward-line -1)
2027                       (narrow-to-region (point) (point-max))
2028                       (gnus-article-hide-header "to"))
2029                     (setq to-count (1- to-count))))
2030                 (goto-char (point-min))
2031                 (while (re-search-forward "^cc:" nil t)
2032                   (setq cc-count (1+ cc-count)))
2033                 (when (> cc-count 1)
2034                   (while (> cc-count 0)
2035                     (goto-char (point-min))
2036                     (save-restriction
2037                       (re-search-forward "^cc:" nil nil cc-count)
2038                       (forward-line -1)
2039                       (narrow-to-region (point) (point-max))
2040                       (gnus-article-hide-header "cc"))
2041                     (setq cc-count (1- cc-count)))))))))))))
2042
2043 (defun gnus-article-hide-header (header)
2044   (save-excursion
2045     (goto-char (point-min))
2046     (when (re-search-forward (concat "^" header ":") nil t)
2047       (gnus-article-hide-text-type
2048        (point-at-bol)
2049        (progn
2050          (end-of-line)
2051          (if (re-search-forward "^[^ \t]" nil t)
2052              (match-beginning 0)
2053            (point-max)))
2054        'boring-headers))))
2055
2056 (defvar gnus-article-normalized-header-length 40
2057   "Length of normalized headers.")
2058
2059 (defun article-normalize-headers ()
2060   "Make all header lines 40 characters long."
2061   (interactive)
2062   (let ((inhibit-read-only t)
2063         column)
2064     (save-excursion
2065       (save-restriction
2066         (article-narrow-to-head)
2067         (while (not (eobp))
2068           (cond
2069            ((< (setq column (- (point-at-eol) (point)))
2070                gnus-article-normalized-header-length)
2071             (end-of-line)
2072             (insert (make-string
2073                      (- gnus-article-normalized-header-length column)
2074                      ? )))
2075            ((> column gnus-article-normalized-header-length)
2076             (gnus-put-text-property
2077              (progn
2078                (forward-char gnus-article-normalized-header-length)
2079                (point))
2080              (point-at-eol)
2081              'invisible t))
2082            (t
2083             ;; Do nothing.
2084             ))
2085           (forward-line 1))))))
2086
2087 (defun article-treat-dumbquotes ()
2088   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2089 Note that this function guesses whether a character is a sm*rtq**t* or
2090 not, so it should only be used interactively.
2091
2092 Sm*rtq**t*s are M****s***'s unilateral extension to the
2093 iso-8859-1 character map in an attempt to provide more quoting
2094 characters.  If you see something like \\222 or \\264 where
2095 you're expecting some kind of apostrophe or quotation mark, then
2096 try this wash."
2097   (interactive)
2098   (article-translate-strings gnus-article-dumbquotes-map))
2099
2100 (defun article-translate-characters (from to)
2101   "Translate all characters in the body of the article according to FROM and TO.
2102 FROM is a string of characters to translate from; to is a string of
2103 characters to translate to."
2104   (save-excursion
2105     (when (article-goto-body)
2106       (let ((inhibit-read-only t)
2107             (x (make-string 225 ?x))
2108             (i -1))
2109         (while (< (incf i) (length x))
2110           (aset x i i))
2111         (setq i 0)
2112         (while (< i (length from))
2113           (aset x (aref from i) (aref to i))
2114           (incf i))
2115         (translate-region (point) (point-max) x)))))
2116
2117 (defun article-translate-strings (map)
2118   "Translate all string in the body of the article according to MAP.
2119 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2120   (save-excursion
2121     (when (article-goto-body)
2122       (let ((inhibit-read-only t))
2123         (dolist (elem map)
2124           (save-excursion
2125             (while (search-forward (car elem) nil t)
2126               (replace-match (cadr elem)))))))))
2127
2128 (defun article-treat-overstrike ()
2129   "Translate overstrikes into bold text."
2130   (interactive)
2131   (save-excursion
2132     (when (article-goto-body)
2133       (let ((inhibit-read-only t))
2134         (while (search-forward "\b" nil t)
2135           (let ((next (char-after))
2136                 (previous (char-after (- (point) 2))))
2137             ;; We do the boldification/underlining by hiding the
2138             ;; overstrikes and putting the proper text property
2139             ;; on the letters.
2140             (cond
2141              ((eq next previous)
2142               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2143               (put-text-property (point) (1+ (point)) 'face 'bold))
2144              ((eq next ?_)
2145               (gnus-article-hide-text-type
2146                (1- (point)) (1+ (point)) 'overstrike)
2147               (put-text-property
2148                (- (point) 2) (1- (point)) 'face 'underline))
2149              ((eq previous ?_)
2150               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2151               (put-text-property
2152                (point) (1+ (point)) 'face 'underline)))))))))
2153
2154 (defun article-treat-ansi-sequences ()
2155   "Translate ANSI SGR control sequences into overlays or extents."
2156   (interactive)
2157   (save-excursion
2158     (when (article-goto-body)
2159       (let ((inhibit-read-only t))
2160         (ansi-color-apply-on-region (point) (point-max))))))
2161
2162 (defun gnus-article-treat-unfold-headers ()
2163   "Unfold folded message headers.
2164 Only the headers that fit into the current window width will be
2165 unfolded."
2166   (interactive)
2167   (gnus-with-article-headers
2168     (let (length)
2169       (while (not (eobp))
2170         (save-restriction
2171           (mail-header-narrow-to-field)
2172           (let* ((header (buffer-string))
2173                  (unfoldable
2174                   (or (equal gnus-article-unfold-long-headers t)
2175                       (and (stringp gnus-article-unfold-long-headers)
2176                            (string-match gnus-article-unfold-long-headers header)))))
2177             (with-temp-buffer
2178               (insert header)
2179               (goto-char (point-min))
2180               (while (re-search-forward "\n[\t ]" nil t)
2181                 (replace-match " " t t)))
2182             (setq length (- (point-max) (point-min) 1))
2183             (when (or unfoldable
2184                       (< length (window-width)))
2185               (while (re-search-forward "\n[\t ]" nil t)
2186                 (replace-match " " t t))))
2187           (goto-char (point-max)))))))
2188
2189 (defun gnus-article-treat-fold-headers ()
2190   "Fold message headers."
2191   (interactive)
2192   (gnus-with-article-headers
2193     (while (not (eobp))
2194       (save-restriction
2195         (mail-header-narrow-to-field)
2196         (mail-header-fold-field)
2197         (goto-char (point-max))))))
2198
2199 (defun gnus-treat-smiley ()
2200   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2201   (interactive)
2202   (gnus-with-article-buffer
2203     (if (memq 'smiley gnus-article-wash-types)
2204         (gnus-delete-images 'smiley)
2205       (article-goto-body)
2206       (let ((images (smiley-region (point) (point-max))))
2207         (when images
2208           (gnus-add-wash-type 'smiley)
2209           (dolist (image images)
2210             (gnus-add-image 'smiley image)))))))
2211
2212 (defun gnus-article-remove-images ()
2213   "Remove all images from the article buffer."
2214   (interactive)
2215   (gnus-with-article-buffer
2216     (dolist (elem gnus-article-image-alist)
2217       (gnus-delete-images (car elem)))))
2218
2219 (defun gnus-article-treat-fold-newsgroups ()
2220   "Unfold folded message headers.
2221 Only the headers that fit into the current window width will be
2222 unfolded."
2223   (interactive)
2224   (gnus-with-article-headers
2225     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2226       (save-restriction
2227         (mail-header-narrow-to-field)
2228         (while (re-search-forward ", *" nil t)
2229           (replace-match ", " t t))
2230         (mail-header-fold-field)
2231         (goto-char (point-max))))))
2232
2233 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2234   "Value of `truncate-lines' in Gnus Article buffer.
2235 Valid values are nil, t, `head', `first', `last', an integer or a
2236 predicate.  See Info node `(gnus)Customizing Articles'."
2237   :version "23.1" ;; No Gnus
2238   :group 'gnus-article
2239   ;; :link '(custom-manual "(gnus)Customizing Articles")
2240   :type 'boolean)
2241
2242 (defun gnus-article-toggle-truncate-lines (&optional arg)
2243   "Toggle whether to fold or truncate long lines in article the buffer.
2244 If ARG is non-nil and not a number, toggle
2245 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2246 long lines if and only if arg is positive."
2247   (interactive "P")
2248   (cond
2249    ((and (numberp arg) (> arg 0))
2250     (setq gnus-article-truncate-lines t))
2251    ((numberp arg)
2252     (setq gnus-article-truncate-lines nil))
2253    (arg
2254     (setq gnus-article-truncate-lines
2255           (not gnus-article-truncate-lines))))
2256   (gnus-with-article-buffer
2257     (cond
2258      ((and (numberp arg) (> arg 0))
2259       (setq truncate-lines nil))
2260      ((numberp arg)
2261       (setq truncate-lines t)))
2262     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2263     ;; `toggle-truncate-lines' needs an argument.
2264     (toggle-truncate-lines)))
2265
2266 (defun gnus-article-treat-body-boundary ()
2267   "Place a boundary line at the end of the headers."
2268   (interactive)
2269   (when (and gnus-body-boundary-delimiter
2270              (> (length gnus-body-boundary-delimiter) 0))
2271     (gnus-with-article-headers
2272       (goto-char (point-max))
2273       (let ((start (point)))
2274         (insert "X-Boundary: ")
2275         (gnus-add-text-properties start (point) '(invisible t intangible t))
2276         (insert (let (str)
2277                   (while (>= (1- (window-width)) (length str))
2278                     (setq str (concat str gnus-body-boundary-delimiter)))
2279                   (substring str 0 (1- (window-width))))
2280                 "\n")
2281         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2282
2283 (defun article-fill-long-lines ()
2284   "Fill lines that are wider than the window width."
2285   (interactive)
2286   (save-excursion
2287     (let ((inhibit-read-only t)
2288           (width (window-width (get-buffer-window (current-buffer)))))
2289       (save-restriction
2290         (article-goto-body)
2291         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2292           (while (not (eobp))
2293             (end-of-line)
2294             (when (>= (current-column) (min fill-column width))
2295               (narrow-to-region (min (1+ (point)) (point-max))
2296                                 (point-at-bol))
2297               (let ((goback (point-marker)))
2298                 (fill-paragraph nil)
2299                 (goto-char (marker-position goback)))
2300               (widen))
2301             (forward-line 1)))))))
2302
2303 (defun article-capitalize-sentences ()
2304   "Capitalize the first word in each sentence."
2305   (interactive)
2306   (save-excursion
2307     (let ((inhibit-read-only t)
2308           (paragraph-start "^[\n\^L]"))
2309       (article-goto-body)
2310       (while (not (eobp))
2311         (capitalize-word 1)
2312         (forward-sentence)))))
2313
2314 (defun article-remove-cr ()
2315   "Remove trailing CRs and then translate remaining CRs into LFs."
2316   (interactive)
2317   (save-excursion
2318     (let ((inhibit-read-only t))
2319       (goto-char (point-min))
2320       (while (re-search-forward "\r+$" nil t)
2321         (replace-match "" t t))
2322       (goto-char (point-min))
2323       (while (search-forward "\r" nil t)
2324         (replace-match "\n" t t)))))
2325
2326 (defun article-remove-trailing-blank-lines ()
2327   "Remove all trailing blank lines from the article."
2328   (interactive)
2329   (save-excursion
2330     (let ((inhibit-read-only t))
2331       (goto-char (point-max))
2332       (delete-region
2333        (point)
2334        (progn
2335          (while (and (not (bobp))
2336                      (looking-at "^[ \t]*$")
2337                      (not (gnus-annotation-in-region-p
2338                            (point) (point-at-eol))))
2339            (forward-line -1))
2340          (forward-line 1)
2341          (point))))))
2342
2343 (defvar gnus-face-properties-alist)
2344
2345 (defun article-display-face (&optional force)
2346   "Display any Face headers in the header."
2347   (interactive (list 'force))
2348   (let ((wash-face-p buffer-read-only))
2349     (gnus-with-article-headers
2350       ;; When displaying parts, this function can be called several times on
2351       ;; the same article, without any intended toggle semantic (as typing `W
2352       ;; D d' would have). So face deletion must occur only when we come from
2353       ;; an interactive command, that is when the *Article* buffer is
2354       ;; read-only.
2355       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2356           (gnus-delete-images 'face)
2357         (let ((from (message-fetch-field "from"))
2358               face faces)
2359           (save-current-buffer
2360             (when (and wash-face-p
2361                        (gnus-buffer-live-p gnus-original-article-buffer)
2362                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2363               (set-buffer gnus-original-article-buffer))
2364             (save-restriction
2365               (mail-narrow-to-head)
2366               (when (or force
2367                         ;; Check whether this face is censored.
2368                         (not (and gnus-article-x-face-too-ugly
2369                                   (or from
2370                                       (setq from (message-fetch-field "from")))
2371                                   (string-match gnus-article-x-face-too-ugly
2372                                                 from))))
2373                 (while (gnus-article-goto-header "Face")
2374                   (push (mail-header-field-value) faces)))))
2375           (when faces
2376             (goto-char (point-min))
2377             (let (png image)
2378               (unless (setq from (gnus-article-goto-header "from"))
2379                 (insert "From:")
2380                 (setq from (point))
2381                 (insert " [no `from' set]\n"))
2382               (while faces
2383                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2384                   (setq image
2385                         (apply 'gnus-create-image png 'png t
2386                                (cdr (assq 'png gnus-face-properties-alist))))
2387                   (goto-char from)
2388                   (gnus-add-wash-type 'face)
2389                   (gnus-add-image 'face image)
2390                   (gnus-put-image image nil 'face))))))))))
2391
2392 (defun article-display-x-face (&optional force)
2393   "Look for an X-Face header and display it if present."
2394   (interactive (list 'force))
2395   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2396     (gnus-with-article-headers
2397       ;; Delete the old process, if any.
2398       (when (process-status "article-x-face")
2399         (delete-process "article-x-face"))
2400       ;; See the comment in `article-display-face'.
2401       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2402           ;; We have already displayed X-Faces, so we remove them
2403           ;; instead.
2404           (gnus-delete-images 'xface)
2405         ;; Display X-Faces.
2406         (let ((from (message-fetch-field "from"))
2407               x-faces face)
2408           (save-current-buffer
2409             (when (and wash-face-p
2410                        (gnus-buffer-live-p gnus-original-article-buffer)
2411                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2412               ;; If type `W f', use gnus-original-article-buffer,
2413               ;; otherwise use the current buffer because displaying
2414               ;; RFC822 parts calls this function too.
2415               (set-buffer gnus-original-article-buffer))
2416             (save-restriction
2417               (mail-narrow-to-head)
2418               (and gnus-article-x-face-command
2419                    (or force
2420                        ;; Check whether this face is censored.
2421                        (not (and gnus-article-x-face-too-ugly
2422                                  (or from
2423                                      (setq from (message-fetch-field "from")))
2424                                  (string-match gnus-article-x-face-too-ugly
2425                                                from))))
2426                    (while (gnus-article-goto-header "X-Face")
2427                      (push (mail-header-field-value) x-faces)))))
2428           (when x-faces
2429             ;; We display the face.
2430             (cond ((functionp gnus-article-x-face-command)
2431                    ;; The command is a lisp function, so we call it.
2432                    (mapc gnus-article-x-face-command x-faces))
2433                   ((stringp gnus-article-x-face-command)
2434                    ;; The command is a string, so we interpret the command
2435                    ;; as a, well, command, and fork it off.
2436                    (let ((process-connection-type nil))
2437                      (gnus-set-process-query-on-exit-flag
2438                       (start-process
2439                        "article-x-face" nil shell-file-name
2440                        shell-command-switch gnus-article-x-face-command)
2441                       nil)
2442                      ;; Sending multiple EOFs to xv doesn't work,
2443                      ;; so we only do a single external face.
2444                      (with-temp-buffer
2445                        (insert (car x-faces))
2446                        (process-send-region "article-x-face"
2447                                             (point-min) (point-max)))
2448                      (process-send-eof "article-x-face")))
2449                   (t
2450                    (error "`%s' set to `%s' is not a function"
2451                           gnus-article-x-face-command
2452                           'gnus-article-x-face-command)))))))))
2453
2454 (defun article-decode-mime-words ()
2455   "Decode all MIME-encoded words in the article."
2456   (interactive)
2457   (gnus-with-article-buffer
2458     (let ((inhibit-point-motion-hooks t)
2459           (mail-parse-charset gnus-newsgroup-charset)
2460           (mail-parse-ignored-charsets
2461            (with-current-buffer gnus-summary-buffer
2462              gnus-newsgroup-ignored-charsets)))
2463       (mail-decode-encoded-word-region (point-min) (point-max)))))
2464
2465 (defun article-decode-charset (&optional prompt)
2466   "Decode charset-encoded text in the article.
2467 If PROMPT (the prefix), prompt for a coding system to use."
2468   (interactive "P")
2469   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2470         (inhibit-read-only t)
2471         (mail-parse-charset gnus-newsgroup-charset)
2472         (mail-parse-ignored-charsets
2473          (save-excursion (condition-case nil
2474                              (set-buffer gnus-summary-buffer)
2475                            (error))
2476                          gnus-newsgroup-ignored-charsets))
2477         ct cte ctl charset format)
2478     (save-excursion
2479       (save-restriction
2480         (article-narrow-to-head)
2481         (setq ct (message-fetch-field "Content-Type" t)
2482               cte (message-fetch-field "Content-Transfer-Encoding" t)
2483               ctl (and ct (mail-header-parse-content-type ct))
2484               charset (cond
2485                        (prompt
2486                         (mm-read-coding-system "Charset to decode: "))
2487                        (ctl
2488                         (mail-content-type-get ctl 'charset)))
2489               format (and ctl (mail-content-type-get ctl 'format)))
2490         (when cte
2491           (setq cte (mail-header-strip cte)))
2492         (if (and ctl (not (string-match "/" (car ctl))))
2493             (setq ctl nil))
2494         (goto-char (point-max)))
2495       (forward-line 1)
2496       (save-restriction
2497         (narrow-to-region (point) (point-max))
2498         (when (and (eq mail-parse-charset 'gnus-decoded)
2499                    (eq (mm-body-7-or-8) '8bit))
2500           ;; The text code could have been decoded.
2501           (setq charset mail-parse-charset))
2502         (when (and (or (not ctl)
2503                        (equal (car ctl) "text/plain"))
2504                    (not format)) ;; article with format will decode later.
2505           (mm-decode-body
2506            charset (and cte (intern (downcase
2507                                      (gnus-strip-whitespace cte))))
2508            (car ctl)))))))
2509
2510 (defun article-decode-encoded-words ()
2511   "Remove encoded-word encoding from headers."
2512   (let ((inhibit-point-motion-hooks t)
2513         (mail-parse-charset gnus-newsgroup-charset)
2514         (mail-parse-ignored-charsets
2515          (save-excursion (condition-case nil
2516                              (set-buffer gnus-summary-buffer)
2517                            (error))
2518                          gnus-newsgroup-ignored-charsets))
2519         (inhibit-read-only t)
2520         end start)
2521     (goto-char (point-min))
2522     (when (search-forward "\n\n" nil 'move)
2523       (forward-line -1))
2524     (setq end (point))
2525     (while (not (bobp))
2526       (while (progn
2527                (forward-line -1)
2528                (and (not (bobp))
2529                     (memq (char-after) '(?\t ? )))))
2530       (setq start (point))
2531       (if (looking-at "\
2532 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2533 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2534           (funcall gnus-decode-address-function start end)
2535         (funcall gnus-decode-header-function start end))
2536       (goto-char (setq end start)))))
2537
2538 (defun article-decode-group-name ()
2539   "Decode group names in Newsgroups, Followup-To and Xref headers."
2540   (let ((inhibit-point-motion-hooks t)
2541         (inhibit-read-only t)
2542         (method (gnus-find-method-for-group gnus-newsgroup-name))
2543         regexp)
2544     (when (and (or gnus-group-name-charset-method-alist
2545                    gnus-group-name-charset-group-alist)
2546                (gnus-buffer-live-p gnus-original-article-buffer))
2547       (save-restriction
2548         (article-narrow-to-head)
2549         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2550           (with-current-buffer gnus-original-article-buffer
2551             (goto-char (point-min)))
2552           (setq regexp (concat "^" header
2553                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2554           (while (re-search-forward regexp nil t)
2555             (replace-match (save-match-data
2556                              (gnus-decode-newsgroups
2557                               ;; XXX how to use data in article buffer?
2558                               (with-current-buffer gnus-original-article-buffer
2559                                 (re-search-forward regexp nil t)
2560                                 (match-string 1))
2561                               gnus-newsgroup-name method))
2562                            t t nil 1))
2563           (goto-char (point-min)))))))
2564
2565 (autoload 'idna-to-unicode "idna")
2566
2567 (defun article-decode-idna-rhs ()
2568   "Decode IDNA strings in RHS in various headers in current buffer.
2569 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2570 Mail-Reply-To: and Mail-Followup-To:."
2571   (when gnus-use-idna
2572     (save-restriction
2573       (let ((inhibit-point-motion-hooks t)
2574             (inhibit-read-only t))
2575         (article-narrow-to-head)
2576         (goto-char (point-min))
2577         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2578           (let (ace unicode)
2579             (when (save-match-data
2580                     (and (setq ace (match-string 1))
2581                          (save-excursion
2582                            (and (re-search-backward "^[^ \t]" nil t)
2583                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2584                          (setq unicode (idna-to-unicode ace))))
2585               (unless (string= ace unicode)
2586                 (replace-match unicode nil nil nil 1)))))))))
2587
2588 (defun article-de-quoted-unreadable (&optional force read-charset)
2589   "Translate a quoted-printable-encoded article.
2590 If FORCE, decode the article whether it is marked as quoted-printable
2591 or not.
2592 If READ-CHARSET, ask for a coding system."
2593   (interactive (list 'force current-prefix-arg))
2594   (save-excursion
2595     (let ((inhibit-read-only t) type charset)
2596       (if (gnus-buffer-live-p gnus-original-article-buffer)
2597           (with-current-buffer gnus-original-article-buffer
2598             (setq type
2599                   (gnus-fetch-field "content-transfer-encoding"))
2600             (let* ((ct (gnus-fetch-field "content-type"))
2601                    (ctl (and ct (mail-header-parse-content-type ct))))
2602               (setq charset (and ctl
2603                                  (mail-content-type-get ctl 'charset)))
2604               (if (stringp charset)
2605                   (setq charset (intern (downcase charset)))))))
2606       (if read-charset
2607           (setq charset (mm-read-coding-system "Charset: " charset)))
2608       (unless charset
2609         (setq charset gnus-newsgroup-charset))
2610       (when (or force
2611                 (and type (let ((case-fold-search t))
2612                             (string-match "quoted-printable" type))))
2613         (article-goto-body)
2614         (quoted-printable-decode-region
2615          (point) (point-max) (mm-charset-to-coding-system charset))))))
2616
2617 (defun article-de-base64-unreadable (&optional force read-charset)
2618   "Translate a base64 article.
2619 If FORCE, decode the article whether it is marked as base64 not.
2620 If READ-CHARSET, ask for a coding system."
2621   (interactive (list 'force current-prefix-arg))
2622   (save-excursion
2623     (let ((inhibit-read-only t) type charset)
2624       (if (gnus-buffer-live-p gnus-original-article-buffer)
2625           (with-current-buffer gnus-original-article-buffer
2626             (setq type
2627                   (gnus-fetch-field "content-transfer-encoding"))
2628             (let* ((ct (gnus-fetch-field "content-type"))
2629                    (ctl (and ct (mail-header-parse-content-type ct))))
2630               (setq charset (and ctl
2631                                  (mail-content-type-get ctl 'charset)))
2632               (if (stringp charset)
2633                   (setq charset (intern (downcase charset)))))))
2634       (if read-charset
2635           (setq charset (mm-read-coding-system "Charset: " charset)))
2636       (unless charset
2637         (setq charset gnus-newsgroup-charset))
2638       (when (or force
2639                 (and type (let ((case-fold-search t))
2640                             (string-match "base64" type))))
2641         (article-goto-body)
2642         (save-restriction
2643           (narrow-to-region (point) (point-max))
2644           (base64-decode-region (point-min) (point-max))
2645           (mm-decode-coding-region
2646            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2647
2648 (eval-when-compile
2649   (require 'rfc1843))
2650
2651 (defun article-decode-HZ ()
2652   "Translate a HZ-encoded article."
2653   (interactive)
2654   (require 'rfc1843)
2655   (save-excursion
2656     (let ((inhibit-read-only t))
2657       (rfc1843-decode-region (point-min) (point-max)))))
2658
2659 (defun article-unsplit-urls ()
2660   "Remove the newlines that some other mailers insert into URLs."
2661   (interactive)
2662   (save-excursion
2663     (let ((inhibit-read-only t))
2664       (goto-char (point-min))
2665       (while (re-search-forward
2666               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2667         (replace-match "\\1\\3" t)))
2668     (when (interactive-p)
2669       (gnus-treat-article nil))))
2670
2671
2672 (defun article-wash-html (&optional read-charset)
2673   "Format an HTML article.
2674 If READ-CHARSET, ask for a coding system.  If it is a number, the
2675 charset defined in `gnus-summary-show-article-charset-alist' is used."
2676   (interactive "P")
2677   (save-excursion
2678     (let ((inhibit-read-only t)
2679           charset)
2680       (if read-charset
2681           (if (or (and (numberp read-charset)
2682                        (setq charset
2683                              (cdr
2684                               (assq read-charset
2685                                     gnus-summary-show-article-charset-alist))))
2686                   (setq charset (mm-read-coding-system "Charset: ")))
2687               (let ((gnus-summary-show-article-charset-alist
2688                      (list (cons 1 charset))))
2689                 (with-current-buffer gnus-summary-buffer
2690                   (gnus-summary-show-article 1)))
2691             (error "No charset is given"))
2692         (when (gnus-buffer-live-p gnus-original-article-buffer)
2693           (with-current-buffer gnus-original-article-buffer
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               (when (stringp charset)
2699                 (setq charset (intern (downcase charset)))))))
2700         (unless charset
2701           (setq charset gnus-newsgroup-charset)))
2702       (article-goto-body)
2703       (save-window-excursion
2704         (save-restriction
2705           (narrow-to-region (point) (point-max))
2706           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2707                  (entry (assq func mm-text-html-washer-alist)))
2708             (when entry
2709               (setq func (cdr entry)))
2710             (cond
2711              ((functionp func)
2712               (funcall func))
2713              (t
2714               (apply (car func) (cdr func))))))))))
2715
2716 ;; External.
2717 (declare-function w3-region "ext:w3-display" (st nd))
2718
2719 (defun gnus-article-wash-html-with-w3 ()
2720   "Wash the current buffer with w3."
2721   (mm-setup-w3)
2722   (let ((w3-strict-width (window-width))
2723         (url-standalone-mode t)
2724         (url-gateway-unplugged t)
2725         (w3-honor-stylesheets nil))
2726     (condition-case ()
2727         (w3-region (point-min) (point-max))
2728       (error))))
2729
2730 ;; External.
2731 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
2732
2733 (defun gnus-article-wash-html-with-w3m ()
2734   "Wash the current buffer with emacs-w3m."
2735   (mm-setup-w3m)
2736   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2737         w3m-force-redisplay)
2738     (w3m-region (point-min) (point-max)))
2739   ;; Put the mark meaning this part was rendered by emacs-w3m.
2740   (put-text-property (point-min) (point-max) 'mm-inline-text-html-with-w3m t)
2741   (when (and mm-inline-text-html-with-w3m-keymap
2742              (boundp 'w3m-minor-mode-map)
2743              w3m-minor-mode-map)
2744     (if (and (boundp 'w3m-link-map)
2745              w3m-link-map)
2746         (let* ((start (point-min))
2747                (end (point-max))
2748                (on (get-text-property start 'w3m-href-anchor))
2749                (map (copy-keymap w3m-link-map))
2750                next)
2751           (set-keymap-parent map w3m-minor-mode-map)
2752           (while (< start end)
2753             (if on
2754                 (progn
2755                   (setq next (or (text-property-any start end
2756                                                     'w3m-href-anchor nil)
2757                                  end))
2758                   (put-text-property start next 'keymap map))
2759               (setq next (or (text-property-not-all start end
2760                                                     'w3m-href-anchor nil)
2761                              end))
2762               (put-text-property start next 'keymap w3m-minor-mode-map))
2763             (setq start next
2764                   on (not on))))
2765       (put-text-property (point-min) (point-max) 'keymap w3m-minor-mode-map))))
2766
2767 (defvar charset) ;; Bound by `article-wash-html'.
2768
2769 (defun gnus-article-wash-html-with-w3m-standalone ()
2770   "Wash the current buffer with w3m."
2771   (if (mm-w3m-standalone-supports-m17n-p)
2772       (progn
2773         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2774           ;; The default.
2775           (setq charset 'iso-8859-1))
2776         (let ((coding-system-for-write charset)
2777               (coding-system-for-read charset))
2778           (call-process-region
2779            (point-min) (point-max)
2780            "w3m" t t nil "-dump" "-T" "text/html"
2781            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2782     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2783
2784 (defvar gnus-article-browse-html-temp-list nil
2785   "List of temporary files created by `gnus-article-browse-html-parts'.
2786 Internal variable.")
2787
2788 (defcustom gnus-article-browse-delete-temp 'ask
2789   "What to do with temporary files from `gnus-article-browse-html-parts'.
2790 If nil, don't delete temporary files.  If it is t, delete them on
2791 exit from the summary buffer.  If it is the symbol `file', query
2792 on each file, if it is `ask' ask once when exiting from the
2793 summary buffer."
2794   :group 'gnus-article
2795   :version "23.1" ;; No Gnus
2796   :type '(choice (const :tag "Don't delete" nil)
2797                  (const :tag "Don't ask" t)
2798                  (const :tag "Ask" ask)
2799                  (const :tag "Ask for each file" file)))
2800
2801 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2802
2803 (defun gnus-article-browse-delete-temp-files (&optional how)
2804   "Delete temp-files created by `gnus-article-browse-html-parts'."
2805   (when (and gnus-article-browse-html-temp-list
2806              (progn
2807                (or how (setq how gnus-article-browse-delete-temp))
2808                (if (eq how 'ask)
2809                    (let ((files (length gnus-article-browse-html-temp-list)))
2810                      (gnus-y-or-n-p (format
2811                                      "Delete all %s temporary HTML file%s? "
2812                                      files
2813                                      (if (> files 1) "s" ""))))
2814                  how)))
2815     (dolist (file gnus-article-browse-html-temp-list)
2816       (cond ((file-directory-p file)
2817              (when (or (not (eq how 'file))
2818                        (gnus-y-or-n-p
2819                         (format
2820                          "Delete temporary HTML file(s) in directory `%s'? "
2821                          (file-name-as-directory file))))
2822                (gnus-delete-directory file)))
2823             ((file-exists-p file)
2824              (when (or (not (eq how 'file))
2825                        (gnus-y-or-n-p
2826                         (format "Delete temporary HTML file `%s'? " file)))
2827                (delete-file file)))))
2828     ;; Also remove file from the list when not deleted or if file doesn't
2829     ;; exist anymore.
2830     (setq gnus-article-browse-html-temp-list nil))
2831   gnus-article-browse-html-temp-list)
2832
2833 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2834   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2835 Return file name."
2836   (save-match-data
2837     (let (file type)
2838       (catch 'found
2839         (dolist (handle handles)
2840           (cond
2841            ((not (listp handle)))
2842            ((equal (mm-handle-media-supertype handle) "multipart")
2843             (when (setq file (gnus-article-browse-html-save-cid-content
2844                               cid handle directory))
2845               (throw 'found file)))
2846            ((equal (concat "<" cid ">") (mm-handle-id handle))
2847             (setq file
2848                   (expand-file-name
2849                    (or (mail-content-type-get
2850                         (mm-handle-disposition handle) 'filename)
2851                        (mail-content-type-get
2852                         (setq type (mm-handle-type handle)) 'name)
2853                        (concat
2854                         (make-temp-name "cid")
2855                         (car (rassoc (car type) mailcap-mime-extensions))))
2856                    directory))
2857             (mm-save-part-to-file handle file)
2858             (throw 'found file))))))))
2859
2860 (defun gnus-article-browse-html-parts (list &optional header)
2861   "View all \"text/html\" parts from LIST.
2862 Recurse into multiparts.  The optional HEADER that should be a decoded
2863 message header will be added to the bodies of the \"text/html\" parts."
2864   ;; Internal function used by `gnus-article-browse-html-article'.
2865   (let (type file charset content cid-dir tmp-file showed)
2866     ;; Find and show the html-parts.
2867     (dolist (handle list)
2868       ;; If HTML, show it:
2869       (cond ((not (listp handle)))
2870             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2871                  (and (equal (car type) "message/external-body")
2872                       (or header
2873                           (setq file (or (mail-content-type-get type 'name)
2874                                          (mail-content-type-get
2875                                           (mm-handle-disposition handle)
2876                                           'filename))))
2877                       (or (mm-handle-cache handle)
2878                           (condition-case code
2879                               (progn (mm-extern-cache-contents handle) t)
2880                             (error
2881                              (gnus-message 3 "%s" (error-message-string code))
2882                              (when (>= gnus-verbose 3) (sit-for 2))
2883                              nil)))
2884                       (progn
2885                         (setq handle (mm-handle-cache handle)
2886                               type (mm-handle-type handle))
2887                         (equal (car type) "text/html"))))
2888              (setq charset (mail-content-type-get type 'charset)
2889                    content (mm-get-part handle))
2890              (with-temp-buffer
2891                (if (eq charset 'gnus-decoded)
2892                    (mm-enable-multibyte)
2893                  (mm-disable-multibyte))
2894                (insert content)
2895                ;; resolve cid contents
2896                (let ((case-fold-search t)
2897                      cid-file)
2898                  (goto-char (point-min))
2899                  (while (re-search-forward "\
2900 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2901                                            nil t)
2902                    (unless cid-dir
2903                      (setq cid-dir (mm-make-temp-file "cid" t))
2904                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2905                    (setq file nil
2906                          content nil)
2907                    (when (setq cid-file
2908                                (gnus-article-browse-html-save-cid-content
2909                                 (match-string 2)
2910                                 (with-current-buffer gnus-article-buffer
2911                                   gnus-article-mime-handles)
2912                                 cid-dir))
2913                      (replace-match (concat "file://" cid-file)
2914                                     nil nil nil 1))))
2915                (unless content (setq content (buffer-string))))
2916              (when (or charset header (not file))
2917                (setq tmp-file (mm-make-temp-file
2918                                ;; Do we need to care for 8.3 filenames?
2919                                "mm-" nil ".html")))
2920              ;; Add a meta html tag to specify charset and a header.
2921              (cond
2922               (header
2923                (let (title eheader body hcharset coding force-charset)
2924                  (with-temp-buffer
2925                    (mm-enable-multibyte)
2926                    (setq case-fold-search t)
2927                    (insert header "\n")
2928                    (setq title (message-fetch-field "subject"))
2929                    (goto-char (point-min))
2930                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2931                      (replace-match (cond ((match-beginning 1) "&lt;")
2932                                           ((match-beginning 2) "&gt;")
2933                                           (t "&amp;"))))
2934                    (goto-char (point-min))
2935                    (insert "<pre>\n")
2936                    (goto-char (point-max))
2937                    (insert "</pre>\n<hr>\n")
2938                    ;; We have to examine charset one by one since
2939                    ;; charset specified in parts might be different.
2940                    (if (eq charset 'gnus-decoded)
2941                        (setq charset 'utf-8
2942                              eheader (mm-encode-coding-string (buffer-string)
2943                                                               charset)
2944                              title (when title
2945                                      (mm-encode-coding-string title charset))
2946                              body (mm-encode-coding-string content charset)
2947                              force-charset t)
2948                      (setq hcharset (mm-find-mime-charset-region (point-min)
2949                                                                  (point-max)))
2950                      (cond ((= (length hcharset) 1)
2951                             (setq hcharset (car hcharset)
2952                                   coding (mm-charset-to-coding-system
2953                                           hcharset)))
2954                            ((> (length hcharset) 1)
2955                             (setq hcharset 'utf-8
2956                                   coding hcharset)))
2957                      (if coding
2958                          (if charset
2959                              (progn
2960                                (setq body
2961                                      (mm-charset-to-coding-system charset))
2962                                (if (eq coding body)
2963                                    (setq eheader (mm-encode-coding-string
2964                                                   (buffer-string) coding)
2965                                          title (when title
2966                                                  (mm-encode-coding-string
2967                                                   title coding))
2968                                          body content)
2969                                  (setq charset 'utf-8
2970                                        eheader (mm-encode-coding-string
2971                                                 (buffer-string) charset)
2972                                        title (when title
2973                                                (mm-encode-coding-string
2974                                                 title charset))
2975                                        body (mm-encode-coding-string
2976                                              (mm-decode-coding-string
2977                                               content body)
2978                                              charset)
2979                                        force-charset t)))
2980                            (setq charset hcharset
2981                                  eheader (mm-encode-coding-string
2982                                           (buffer-string) coding)
2983                                  title (when title
2984                                          (mm-encode-coding-string
2985                                           title coding))
2986                                  body content))
2987                        (setq eheader (mm-string-as-unibyte (buffer-string))
2988                              body content)))
2989                    (erase-buffer)
2990                    (mm-disable-multibyte)
2991                    (insert body)
2992                    (when charset
2993                      (mm-add-meta-html-tag handle charset force-charset))
2994                    (when title
2995                      (goto-char (point-min))
2996                      (unless (search-forward "<title>" nil t)
2997                        (re-search-forward "<head>\\s-*" nil t)
2998                        (insert "<title>" title "</title>\n")))
2999                    (goto-char (point-min))
3000                    (or (re-search-forward
3001                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
3002                        (re-search-forward
3003                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
3004                    (insert eheader)
3005                    (mm-write-region (point-min) (point-max)
3006                                     tmp-file nil nil nil 'binary t))))
3007               (charset
3008                (mm-with-unibyte-buffer
3009                  (insert (if (eq charset 'gnus-decoded)
3010                              (mm-encode-coding-string content
3011                                                       (setq charset 'utf-8))
3012                            content))
3013                  (if (or (mm-add-meta-html-tag handle charset)
3014                          (not file))
3015                      (mm-write-region (point-min) (point-max)
3016                                       tmp-file nil nil nil 'binary t)
3017                    (setq tmp-file nil))))
3018               (tmp-file
3019                (mm-save-part-to-file handle tmp-file)))
3020              (when tmp-file
3021                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3022              (add-hook 'gnus-summary-prepare-exit-hook
3023                        'gnus-article-browse-delete-temp-files)
3024              (add-hook 'gnus-exit-gnus-hook
3025                        (lambda  ()
3026                          (gnus-article-browse-delete-temp-files t)))
3027              ;; FIXME: Warn if there's an <img> tag?
3028              (browse-url-of-file (or tmp-file (expand-file-name file)))
3029              (setq showed t))
3030             ;; If multipart, recurse
3031             ((equal (mm-handle-media-supertype handle) "multipart")
3032              (when (gnus-article-browse-html-parts handle header)
3033                (setq showed t)))
3034             ((equal (mm-handle-media-type handle) "message/rfc822")
3035              (mm-with-multibyte-buffer
3036                (mm-insert-part handle)
3037                (setq handle (mm-dissect-buffer t t))
3038                (when (and (bufferp (car handle))
3039                           (stringp (car (mm-handle-type handle))))
3040                  (setq handle (list handle)))
3041                (when header
3042                  (article-decode-encoded-words)
3043                  (let ((gnus-visible-headers
3044                         (or (get 'gnus-visible-headers 'standard-value)
3045                             gnus-visible-headers)))
3046                    (article-hide-headers))
3047                  (goto-char (point-min))
3048                  (search-forward "\n\n" nil 'move)
3049                  (skip-chars-backward "\t\n ")
3050                  (setq header (buffer-substring (point-min) (point)))))
3051              (when (prog1
3052                        (gnus-article-browse-html-parts handle header)
3053                      (mm-destroy-parts handle))
3054                (setq showed t)))))
3055     showed))
3056
3057 (defun gnus-article-browse-html-article (&optional arg)
3058   "View \"text/html\" parts of the current article with a WWW browser.
3059 Inline images embedded in a message using the cid scheme, as they are
3060 generally considered to be safe, will be processed properly.
3061 The message header is added to the beginning of every html part unless
3062 the prefix argument ARG is given.
3063
3064 Warning: Spammers use links to images (using the http scheme) in HTML
3065 articles to verify whether you have read the message.  As
3066 `gnus-article-browse-html-article' passes the HTML content to the
3067 browser without eliminating these \"web bugs\" you should only
3068 use it for mails from trusted senders.
3069
3070 If you always want to display HTML parts in the browser, set
3071 `mm-text-html-renderer' to nil.
3072
3073 This command creates temporary files to pass HTML contents including
3074 images if any to the browser, and deletes them when exiting the group
3075 \(if you want)."
3076   ;; Cf. `mm-w3m-safe-url-regexp'
3077   (interactive "P")
3078   (if arg
3079       (gnus-summary-show-article)
3080     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3081                                     gnus-visible-headers))
3082           ;; As we insert a <hr>, there's no need for the body boundary.
3083           (gnus-treat-body-boundary nil))
3084       (gnus-summary-show-article)))
3085   (with-current-buffer gnus-article-buffer
3086     (let ((header (unless arg
3087                     (save-restriction
3088                       (widen)
3089                       (buffer-substring-no-properties
3090                        (goto-char (point-min))
3091                        (if (search-forward "\n\n" nil t)
3092                            (match-beginning 0)
3093                          (goto-char (point-max))
3094                          (skip-chars-backward "\t\n ")
3095                          (point))))))
3096           parts)
3097       (set-buffer gnus-original-article-buffer)
3098       (setq parts (mm-dissect-buffer t t))
3099       ;; If singlepart, enforce a list.
3100       (when (and (bufferp (car parts))
3101                  (stringp (car (mm-handle-type parts))))
3102         (setq parts (list parts)))
3103       ;; Process the list
3104       (unless (gnus-article-browse-html-parts parts header)
3105         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3106       (mm-destroy-parts parts)
3107       (unless arg
3108         (gnus-summary-show-article)))))
3109
3110 (defun article-hide-list-identifiers ()
3111   "Remove list identifies from the Subject header.
3112 The `gnus-list-identifiers' variable specifies what to do."
3113   (interactive)
3114   (let ((inhibit-point-motion-hooks t)
3115         (regexp (if (consp gnus-list-identifiers)
3116                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3117                   gnus-list-identifiers))
3118         (inhibit-read-only t))
3119     (when regexp
3120       (save-excursion
3121         (save-restriction
3122           (article-narrow-to-head)
3123           (goto-char (point-min))
3124           (while (re-search-forward
3125                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3126                   nil t)
3127             (delete-region (match-beginning 2) (match-end 0))
3128             (beginning-of-line))
3129           (when (re-search-forward
3130                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3131             (delete-region (match-beginning 1) (match-end 1))))))))
3132
3133 (defun article-hide-pem (&optional arg)
3134   "Toggle hiding of any PEM headers and signatures in the current article.
3135 If given a negative prefix, always show; if given a positive prefix,
3136 always hide."
3137   (interactive (gnus-article-hidden-arg))
3138   (unless (gnus-article-check-hidden-text 'pem arg)
3139     (save-excursion
3140       (let ((inhibit-read-only t) end)
3141         (goto-char (point-min))
3142         ;; Hide the horrendously ugly "header".
3143         (when (and (search-forward
3144                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3145                     nil t)
3146                    (setq end (1+ (match-beginning 0))))
3147           (gnus-add-wash-type 'pem)
3148           (gnus-article-hide-text-type
3149            end
3150            (if (search-forward "\n\n" nil t)
3151                (match-end 0)
3152              (point-max))
3153            'pem)
3154           ;; Hide the trailer as well
3155           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3156                                 nil t)
3157             (gnus-article-hide-text-type
3158              (match-beginning 0) (match-end 0) 'pem)))))))
3159
3160 (defun article-strip-banner ()
3161   "Strip the banners specified by the `banner' group parameter and by
3162 `gnus-article-address-banner-alist'."
3163   (interactive)
3164   (save-excursion
3165     (save-restriction
3166       (let ((inhibit-point-motion-hooks t))
3167         (when (gnus-parameter-banner gnus-newsgroup-name)
3168           (article-really-strip-banner
3169            (gnus-parameter-banner gnus-newsgroup-name)))
3170         (when gnus-article-address-banner-alist
3171           ;; Note that the From header is decoded here, so it is
3172           ;; required that the *-extract-address-components function
3173           ;; supports non-ASCII text.
3174           (let ((from (save-restriction
3175                         (widen)
3176                         (article-narrow-to-head)
3177                         (mail-fetch-field "from"))))
3178             (when (and from
3179                        (setq from
3180                              (cadr (funcall gnus-extract-address-components
3181                                             from))))
3182               (catch 'found
3183                 (dolist (pair gnus-article-address-banner-alist)
3184                   (when (string-match (car pair) from)
3185                     (throw 'found
3186                            (article-really-strip-banner (cdr pair)))))))))))))
3187
3188 (defun article-really-strip-banner (banner)
3189   "Strip the banner specified by the argument."
3190   (save-excursion
3191     (save-restriction
3192       (let ((inhibit-point-motion-hooks t)
3193             (gnus-signature-limit nil)
3194             (inhibit-read-only t))
3195         (article-goto-body)
3196         (cond
3197          ((eq banner 'signature)
3198           (when (gnus-article-narrow-to-signature)
3199             (widen)
3200             (forward-line -1)
3201             (delete-region (point) (point-max))))
3202          ((symbolp banner)
3203           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3204               (while (re-search-forward banner nil t)
3205                 (delete-region (match-beginning 0) (match-end 0)))))
3206          ((stringp banner)
3207           (while (re-search-forward banner nil t)
3208             (delete-region (match-beginning 0) (match-end 0)))))))))
3209
3210 (defun article-babel ()
3211   "Translate article using an online translation service."
3212   (interactive)
3213   (require 'babel)
3214   (gnus-with-article-buffer
3215     (when (article-goto-body)
3216       (let* ((start (point))
3217              (end (point-max))
3218              (orig (buffer-substring start end))
3219              (trans (babel-as-string orig)))
3220         (save-restriction
3221           (narrow-to-region start end)
3222           (delete-region start end)
3223           (insert trans))))))
3224
3225 (defun article-hide-signature (&optional arg)
3226   "Hide the signature in the current article.
3227 If given a negative prefix, always show; if given a positive prefix,
3228 always hide."
3229   (interactive (gnus-article-hidden-arg))
3230   (unless (gnus-article-check-hidden-text 'signature arg)
3231     (save-excursion
3232       (save-restriction
3233         (let ((inhibit-read-only t))
3234           (when (gnus-article-narrow-to-signature)
3235             (gnus-article-hide-text-type
3236              (point-min) (point-max) 'signature))))))
3237   (gnus-set-mode-line 'article))
3238
3239 (defun article-strip-headers-in-body ()
3240   "Strip offensive headers from bodies."
3241   (interactive)
3242   (save-excursion
3243     (article-goto-body)
3244     (let ((case-fold-search t))
3245       (when (looking-at "x-no-archive:")
3246         (gnus-delete-line)))))
3247
3248 (defun article-strip-leading-blank-lines ()
3249   "Remove all blank lines from the beginning of the article."
3250   (interactive)
3251   (save-excursion
3252     (let ((inhibit-point-motion-hooks t)
3253           (inhibit-read-only t))
3254       (when (article-goto-body)
3255         (while (and (not (eobp))
3256                     (looking-at "[ \t]*$"))
3257           (gnus-delete-line))))))
3258
3259 (defun article-narrow-to-head ()
3260   "Narrow the buffer to the head of the message.
3261 Point is left at the beginning of the narrowed-to region."
3262   (narrow-to-region
3263    (goto-char (point-min))
3264    (if (search-forward "\n\n" nil 1)
3265        (1- (point))
3266      (point-max)))
3267   (goto-char (point-min)))
3268
3269 (defun article-goto-body ()
3270   "Place point at the start of the body."
3271   (goto-char (point-min))
3272   (cond
3273    ;; This variable is only bound when dealing with separate
3274    ;; MIME body parts.
3275    (article-goto-body-goes-to-point-min-p
3276     t)
3277    ((search-forward "\n\n" nil t)
3278     t)
3279    (t
3280     (goto-char (point-max))
3281     nil)))
3282
3283 (defun article-strip-multiple-blank-lines ()
3284   "Replace consecutive blank lines with one empty line."
3285   (interactive)
3286   (save-excursion
3287     (let ((inhibit-point-motion-hooks t)
3288           (inhibit-read-only t))
3289       ;; First make all blank lines empty.
3290       (article-goto-body)
3291       (while (re-search-forward "^[ \t]+$" nil t)
3292         (unless (gnus-annotation-in-region-p
3293                  (match-beginning 0) (match-end 0))
3294           (replace-match "" nil t)))
3295       ;; Then replace multiple empty lines with a single empty line.
3296       (article-goto-body)
3297       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3298         (unless (gnus-annotation-in-region-p
3299                  (match-beginning 0) (match-end 0))
3300           (delete-region (match-beginning 1) (match-end 1)))))))
3301
3302 (defun article-strip-leading-space ()
3303   "Remove all white space from the beginning of the lines in the article."
3304   (interactive)
3305   (save-excursion
3306     (let ((inhibit-point-motion-hooks t)
3307           (inhibit-read-only t))
3308       (article-goto-body)
3309       (while (re-search-forward "^[ \t]+" nil t)
3310         (replace-match "" t t)))))
3311
3312 (defun article-strip-trailing-space ()
3313   "Remove all white space from the end of the lines in the article."
3314   (interactive)
3315   (save-excursion
3316     (let ((inhibit-point-motion-hooks t)
3317           (inhibit-read-only t))
3318       (article-goto-body)
3319       (while (re-search-forward "[ \t]+$" nil t)
3320         (replace-match "" t t)))))
3321
3322 (defun article-strip-blank-lines ()
3323   "Strip leading, trailing and multiple blank lines."
3324   (interactive)
3325   (article-strip-leading-blank-lines)
3326   (article-remove-trailing-blank-lines)
3327   (article-strip-multiple-blank-lines))
3328
3329 (defun article-strip-all-blank-lines ()
3330   "Strip all blank lines."
3331   (interactive)
3332   (save-excursion
3333     (let ((inhibit-point-motion-hooks t)
3334           (inhibit-read-only t))
3335       (article-goto-body)
3336       (while (re-search-forward "^[ \t]*\n" nil t)
3337         (replace-match "" t t)))))
3338
3339 (defun gnus-article-narrow-to-signature ()
3340   "Narrow to the signature; return t if a signature is found, else nil."
3341   (let ((inhibit-point-motion-hooks t))
3342     (when (gnus-article-search-signature)
3343       (forward-line 1)
3344       ;; Check whether we have some limits to what we consider
3345       ;; to be a signature.
3346       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3347                       (list gnus-signature-limit)))
3348             limit limited)
3349         (while (setq limit (pop limits))
3350           (if (or (and (integerp limit)
3351                        (< (- (point-max) (point)) limit))
3352                   (and (floatp limit)
3353                        (< (count-lines (point) (point-max)) limit))
3354                   (and (functionp limit)
3355                        (funcall limit))
3356                   (and (stringp limit)
3357                        (not (re-search-forward limit nil t))))
3358               ()                        ; This limit did not succeed.
3359             (setq limited t
3360                   limits nil)))
3361         (unless limited
3362           (narrow-to-region (point) (point-max))
3363           t)))))
3364
3365 (defun gnus-article-search-signature ()
3366   "Search the current buffer for the signature separator.
3367 Put point at the beginning of the signature separator."
3368   (let ((cur (point)))
3369     (goto-char (point-max))
3370     (if (if (stringp gnus-signature-separator)
3371             (re-search-backward gnus-signature-separator nil t)
3372           (let ((seps gnus-signature-separator))
3373             (while (and seps
3374                         (not (re-search-backward (car seps) nil t)))
3375               (pop seps))
3376             seps))
3377         t
3378       (goto-char cur)
3379       nil)))
3380
3381 (defun gnus-article-hidden-arg ()
3382   "Return the current prefix arg as a number, or 0 if no prefix."
3383   (list (if current-prefix-arg
3384             (prefix-numeric-value current-prefix-arg)
3385           0)))
3386
3387 (defun gnus-article-check-hidden-text (type arg)
3388   "Return nil if hiding is necessary.
3389 Arg can be nil or a number.  nil and positive means hide, negative
3390 means show, 0 means toggle."
3391   (save-excursion
3392     (save-restriction
3393       (let ((hide (gnus-article-hidden-text-p type)))
3394         (cond
3395          ((or (null arg)
3396               (> arg 0))
3397           nil)
3398          ((< arg 0)
3399           (gnus-article-show-hidden-text type)
3400           t)
3401          (t
3402           (if (eq hide 'hidden)
3403               (progn
3404                 (gnus-article-show-hidden-text type)
3405                 t)
3406             nil)))))))
3407
3408 (defun gnus-article-hidden-text-p (type)
3409   "Say whether the current buffer contains hidden text of type TYPE."
3410   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3411     (while (and pos
3412                 (not (get-text-property pos 'invisible))
3413                 (not (get-text-property pos 'dummy-invisible)))
3414       (setq pos
3415             (text-property-any (1+ pos) (point-max) 'article-type type)))
3416     (if pos
3417         'hidden
3418       nil)))
3419
3420 (defun gnus-article-show-hidden-text (type &optional dummy)
3421   "Show all hidden text of type TYPE.
3422 Originally it is hide instead of DUMMY."
3423   (let ((inhibit-read-only t)
3424         (inhibit-point-motion-hooks t))
3425     (gnus-remove-text-properties-when
3426      'article-type type
3427      (point-min) (point-max)
3428      (cons 'article-type (cons type
3429                                gnus-hidden-properties)))
3430     (gnus-delete-wash-type type)))
3431
3432 (defconst article-time-units
3433   `((year . ,(* 365.25 24 60 60))
3434     (week . ,(* 7 24 60 60))
3435     (day . ,(* 24 60 60))
3436     (hour . ,(* 60 60))
3437     (minute . 60)
3438     (second . 1))
3439   "Mapping from time units to seconds.")
3440
3441 (defun gnus-article-forward-header ()
3442   "Move point to the start of the next header.
3443 If the current header is a continuation header, this can be several
3444 lines forward."
3445   (let ((ended nil))
3446     (while (not ended)
3447       (forward-line 1)
3448       (if (looking-at "[ \t]+[^ \t]")
3449           (forward-line 1)
3450         (setq ended t)))))
3451
3452 (defun article-date-ut (&optional type highlight)
3453   "Convert DATE date to universal time in the current article.
3454 If TYPE is `local', convert to local time; if it is `lapsed', output
3455 how much time has lapsed since DATE.  For `lapsed', the value of
3456 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3457 should replace the \"Date:\" one, or should be added below it."
3458   (interactive (list 'ut t))
3459   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3460          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3461                              tdate-regexp)
3462                             ((eq type 'lapsed)
3463                              "^X-Sent:[ \t]")
3464                             (article-lapsed-timer
3465                              "^Date:[ \t]")
3466                             (t
3467                              tdate-regexp)))
3468          (case-fold-search t)
3469          (inhibit-read-only t)
3470          (inhibit-point-motion-hooks t)
3471          pos date bface eface)
3472     (save-excursion
3473       (save-restriction
3474         (widen)
3475         (goto-char (point-min))
3476         (while (or (setq date (get-text-property (setq pos (point))
3477                                                  'original-date))
3478                    (when (setq pos (next-single-property-change
3479                                     (point) 'original-date))
3480                      (setq date (get-text-property pos 'original-date))
3481                      t))
3482           (narrow-to-region
3483            pos (if (setq pos (text-property-any pos (point-max)
3484                                                 'original-date nil))
3485                    (progn
3486                      (goto-char pos)
3487                      (if (or (bolp) (eobp))
3488                          (point)
3489                        (1+ (point))))
3490                  (point-max)))
3491           (goto-char (point-min))
3492           (when (re-search-forward tdate-regexp nil t)
3493             (setq bface (get-text-property (point-at-bol) 'face)
3494                   eface (get-text-property (1- (point-at-eol)) 'face)))
3495           (goto-char (point-min))
3496           (setq pos nil)
3497           ;; Delete any old Date headers.
3498           (while (re-search-forward date-regexp nil t)
3499             (if pos
3500                 (delete-region (point-at-bol) (progn
3501                                                 (gnus-article-forward-header)
3502                                                 (point)))
3503               (delete-region (point-at-bol) (progn
3504                                               (gnus-article-forward-header)
3505                                               (forward-char -1)
3506                                               (point)))
3507               (setq pos (point))))
3508           (when (and (not pos)
3509                      (re-search-forward tdate-regexp nil t))
3510             (forward-line 1))
3511           (gnus-goto-char pos)
3512           (insert (article-make-date-line date (or type 'ut)))
3513           (unless pos
3514             (insert "\n")
3515             (forward-line -1))
3516           ;; Do highlighting.
3517           (beginning-of-line)
3518           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3519             (put-text-property (match-beginning 1) (1+ (match-end 1))
3520                                'face bface)
3521             (put-text-property (match-beginning 2) (match-end 2)
3522                                'face eface))
3523           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3524           (goto-char (point-max))
3525           (widen))))))
3526
3527 (defun article-make-date-line (date type)
3528   "Return a DATE line of TYPE."
3529   (unless (memq type '(local ut original user iso8601 lapsed english))
3530     (error "Unknown conversion type: %s" type))
3531   (condition-case ()
3532       (let ((time (date-to-time date)))
3533         (cond
3534          ;; Convert to the local timezone.
3535          ((eq type 'local)
3536           (concat "Date: " (message-make-date time)))
3537          ;; Convert to Universal Time.
3538          ((eq type 'ut)
3539           (concat "Date: "
3540                   (substring
3541                    (message-make-date
3542                     (let* ((e (parse-time-string date))
3543                            (tm (apply 'encode-time e))
3544                            (ms (car tm))
3545                            (ls (- (cadr tm) (car (current-time-zone time)))))
3546                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3547                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3548                             (t (list ms ls)))))
3549                    0 -5)
3550                   "UT"))
3551          ;; Get the original date from the article.
3552          ((eq type 'original)
3553           (concat "Date: " (if (string-match "\n+$" date)
3554                                (substring date 0 (match-beginning 0))
3555                              date)))
3556          ;; Let the user define the format.
3557          ((eq type 'user)
3558           (let ((format (or (condition-case nil
3559                                 (with-current-buffer gnus-summary-buffer
3560                                   gnus-article-time-format)
3561                               (error nil))
3562                             gnus-article-time-format)))
3563             (if (functionp format)
3564                 (funcall format time)
3565               (concat "Date: " (format-time-string format time)))))
3566          ;; ISO 8601.
3567          ((eq type 'iso8601)
3568           (let ((tz (car (current-time-zone time))))
3569             (concat
3570              "Date: "
3571              (format-time-string "%Y%m%dT%H%M%S" time)
3572              (format "%s%02d%02d"
3573                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3574                      (/ (% (abs tz) 3600) 60)))))
3575          ;; Do an X-Sent lapsed format.
3576          ((eq type 'lapsed)
3577           ;; If the date is seriously mangled, the timezone functions are
3578           ;; liable to bug out, so we ignore all errors.
3579           (let* ((now (current-time))
3580                  (real-time (subtract-time now time))
3581                  (real-sec (and real-time
3582                                 (+ (* (float (car real-time)) 65536)
3583                                    (cadr real-time))))
3584                  (sec (and real-time (abs real-sec)))
3585                  num prev)
3586             (cond
3587              ((null real-time)
3588               "X-Sent: Unknown")
3589              ((zerop sec)
3590               "X-Sent: Now")
3591              (t
3592               (concat
3593                "X-Sent: "
3594                ;; This is a bit convoluted, but basically we go
3595                ;; through the time units for years, weeks, etc,
3596                ;; and divide things to see whether that results
3597                ;; in positive answers.
3598                (mapconcat
3599                 (lambda (unit)
3600                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3601                       ;; The (remaining) seconds are too few to
3602                       ;; be divided into this time unit.
3603                       ""
3604                     ;; It's big enough, so we output it.
3605                     (setq sec (- sec (* num (cdr unit))))
3606                     (prog1
3607                         (concat (if prev ", " "") (int-to-string
3608                                                    (floor num))
3609                                 " " (symbol-name (car unit))
3610                                 (if (> num 1) "s" ""))
3611                       (setq prev t))))
3612                 article-time-units "")
3613                ;; If dates are odd, then it might appear like the
3614                ;; article was sent in the future.
3615                (if (> real-sec 0)
3616                    " ago"
3617                  " in the future"))))))
3618          ;; Display the date in proper English
3619          ((eq type 'english)
3620           (let ((dtime (decode-time time)))
3621             (concat
3622              "Date: the "
3623              (number-to-string (nth 3 dtime))
3624              (let ((digit (% (nth 3 dtime) 10)))
3625                (cond
3626                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3627                 ((= digit 1) "st")
3628                 ((= digit 2) "nd")
3629                 ((= digit 3) "rd")
3630                 (t "th")))
3631              " of "
3632              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3633              " "
3634              (number-to-string (nth 5 dtime))
3635              " at "
3636              (format "%02d" (nth 2 dtime))
3637              ":"
3638              (format "%02d" (nth 1 dtime)))))))
3639     (error
3640      (format "Date: %s (from Gnus)" date))))
3641
3642 (defun article-date-local (&optional highlight)
3643   "Convert the current article date to the local timezone."
3644   (interactive (list t))
3645   (article-date-ut 'local highlight))
3646
3647 (defun article-date-english (&optional highlight)
3648   "Convert the current article date to something that is proper English."
3649   (interactive (list t))
3650   (article-date-ut 'english highlight))
3651
3652 (defun article-date-original (&optional highlight)
3653   "Convert the current article date to what it was originally.
3654 This is only useful if you have used some other date conversion
3655 function and want to see what the date was before converting."
3656   (interactive (list t))
3657   (article-date-ut 'original highlight))
3658
3659 (defun article-date-lapsed (&optional highlight)
3660   "Convert the current article date to time lapsed since it was sent."
3661   (interactive (list t))
3662   (article-date-ut 'lapsed highlight))
3663
3664 (defun article-update-date-lapsed ()
3665   "Function to be run from a timer to update the lapsed time line."
3666   (save-match-data
3667     (let (deactivate-mark)
3668       (save-excursion
3669         (ignore-errors
3670          (walk-windows
3671           (lambda (w)
3672             (set-buffer (window-buffer w))
3673             (when (eq major-mode 'gnus-article-mode)
3674               (let ((mark (point-marker)))
3675                 (goto-char (point-min))
3676                 (when (re-search-forward "^X-Sent:" nil t)
3677                   (article-date-lapsed t))
3678                 (goto-char (marker-position mark))
3679                 (move-marker mark nil))))
3680           nil 'visible))))))
3681
3682 (defun gnus-start-date-timer (&optional n)
3683   "Start a timer to update the X-Sent header in the article buffers.
3684 The numerical prefix says how frequently (in seconds) the function
3685 is to run."
3686   (interactive "p")
3687   (unless n
3688     (setq n 1))
3689   (gnus-stop-date-timer)
3690   (setq article-lapsed-timer
3691         (run-at-time 1 n 'article-update-date-lapsed)))
3692
3693 (defun gnus-stop-date-timer ()
3694   "Stop the X-Sent timer."
3695   (interactive)
3696   (when article-lapsed-timer
3697     (nnheader-cancel-timer article-lapsed-timer)
3698     (setq article-lapsed-timer nil)))
3699
3700 (defun article-date-user (&optional highlight)
3701   "Convert the current article date to the user-defined format.
3702 This format is defined by the `gnus-article-time-format' variable."
3703   (interactive (list t))
3704   (article-date-ut 'user highlight))
3705
3706 (defun article-date-iso8601 (&optional highlight)
3707   "Convert the current article date to ISO8601."
3708   (interactive (list t))
3709   (article-date-ut 'iso8601 highlight))
3710
3711 (defmacro gnus-article-save-original-date (&rest forms)
3712   "Save the original date as a text property and evaluate FORMS."
3713   `(let* ((case-fold-search t)
3714           (start (progn
3715                    (goto-char (point-min))
3716                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3717                               (not (bolp)))
3718                      (match-end 0))))
3719           (date (when (and start
3720                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3721                                               nil t))
3722                   (buffer-substring-no-properties start
3723                                                   (match-beginning 0)))))
3724      (goto-char (point-max))
3725      (skip-chars-backward "\n")
3726      (put-text-property (point-min) (point) 'original-date date)
3727      ,@forms
3728      (goto-char (point-max))
3729      (skip-chars-backward "\n")
3730      (put-text-property (point-min) (point) 'original-date date)))
3731
3732 ;; (defun article-show-all ()
3733 ;;   "Show all hidden text in the article buffer."
3734 ;;   (interactive)
3735 ;;   (save-excursion
3736 ;;     (let ((inhibit-read-only t))
3737 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3738
3739 (defun article-remove-leading-whitespace ()
3740   "Remove excessive whitespace from all headers."
3741   (interactive)
3742   (save-excursion
3743     (save-restriction
3744       (let ((inhibit-read-only t))
3745         (article-narrow-to-head)
3746         (goto-char (point-min))
3747         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3748           (delete-region (match-beginning 1) (match-end 1)))))))
3749
3750 (defun article-emphasize (&optional arg)
3751   "Emphasize text according to `gnus-emphasis-alist'."
3752   (interactive (gnus-article-hidden-arg))
3753   (unless (gnus-article-check-hidden-text 'emphasis arg)
3754     (save-excursion
3755       (let ((alist (or
3756                     (condition-case nil
3757                         (with-current-buffer gnus-summary-buffer
3758                           gnus-article-emphasis-alist)
3759                       (error))
3760                     gnus-emphasis-alist))
3761             (inhibit-read-only t)
3762             (props (append '(article-type emphasis)
3763                            gnus-hidden-properties))
3764             regexp elem beg invisible visible face)
3765         (article-goto-body)
3766         (setq beg (point))
3767         (while (setq elem (pop alist))
3768           (goto-char beg)
3769           (setq regexp (car elem)
3770                 invisible (nth 1 elem)
3771                 visible (nth 2 elem)
3772                 face (nth 3 elem))
3773           (while (re-search-forward regexp nil t)
3774             (when (and (match-beginning visible) (match-beginning invisible))
3775               (gnus-article-hide-text
3776                (match-beginning invisible) (match-end invisible) props)
3777               (gnus-article-unhide-text-type
3778                (match-beginning visible) (match-end visible) 'emphasis)
3779               (gnus-put-overlay-excluding-newlines
3780                (match-beginning visible) (match-end visible) 'face face)
3781               (gnus-add-wash-type 'emphasis)
3782               (goto-char (match-end invisible)))))))))
3783
3784 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3785   "Setup newsgroup emphasis alist."
3786   (unless gnus-article-emphasis-alist
3787     (let ((name (and gnus-newsgroup-name
3788                      (gnus-group-real-name gnus-newsgroup-name))))
3789       (make-local-variable 'gnus-article-emphasis-alist)
3790       (setq gnus-article-emphasis-alist
3791             (nconc
3792              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3793                (while (setq elem (pop alist))
3794                  (when (and name (string-match (car elem) name))
3795                    (setq alist nil
3796                          highlight (copy-sequence (cdr elem)))))
3797                highlight)
3798              (copy-sequence highlight-words)
3799              (if gnus-newsgroup-name
3800                  (copy-sequence (gnus-group-find-parameter
3801                                  gnus-newsgroup-name 'highlight-words t)))
3802              gnus-emphasis-alist)))))
3803
3804 (defvar gnus-summary-article-menu)
3805 (defvar gnus-summary-post-menu)
3806
3807 ;;; Saving functions.
3808
3809 (defun gnus-article-save (save-buffer file &optional num)
3810   "Save the currently selected article."
3811   (when (or (get gnus-default-article-saver :headers)
3812             (not gnus-save-all-headers))
3813     ;; Remove headers according to `gnus-saved-headers' or the value
3814     ;; of the `:headers' property that the saver function might have.
3815     (let ((gnus-visible-headers
3816            (or (symbol-value (get gnus-default-article-saver :headers))
3817                gnus-saved-headers gnus-visible-headers))
3818           ;; Ignore group parameter.  See `article-hide-headers'.
3819           (gnus-summary-buffer nil))
3820       (with-current-buffer save-buffer
3821         (article-hide-headers 1 t))))
3822   (save-window-excursion
3823     (if (not gnus-default-article-saver)
3824         (error "No default saver is defined")
3825       ;; !!! Magic!  The saving functions all save
3826       ;; `gnus-save-article-buffer' (or so they think), but we
3827       ;; bind that variable to our save-buffer.
3828       (set-buffer gnus-article-buffer)
3829       (let* ((gnus-save-article-buffer save-buffer)
3830              (filename
3831               (cond
3832                ((not gnus-prompt-before-saving) 'default)
3833                ((eq gnus-prompt-before-saving 'always) nil)
3834                (t file)))
3835              (gnus-number-of-articles-to-be-saved
3836               (when (eq gnus-prompt-before-saving t)
3837                 num)))                  ; Magic
3838         (set-buffer gnus-article-current-summary)
3839         (funcall gnus-default-article-saver filename)))))
3840
3841 (defun gnus-read-save-file-name (prompt &optional filename
3842                                         function group headers variable
3843                                         dir-var)
3844   (let ((default-name
3845           (funcall function group headers (symbol-value variable)))
3846         result)
3847     (setq result
3848           (expand-file-name
3849            (cond
3850             ((eq filename 'default)
3851              default-name)
3852             ((eq filename t)
3853              default-name)
3854             (filename filename)
3855             (t
3856              (when (symbol-value dir-var)
3857                (setq default-name (expand-file-name
3858                                    (file-name-nondirectory default-name)
3859                                    (symbol-value dir-var))))
3860              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3861                     (prompt
3862                      (format prompt
3863                              (if (and gnus-number-of-articles-to-be-saved
3864                                       (> gnus-number-of-articles-to-be-saved 1))
3865                                  (format "these %d articles"
3866                                          gnus-number-of-articles-to-be-saved)
3867                                "this article")))
3868                     (file
3869                      ;; Let the split methods have their say.
3870                      (cond
3871                       ;; No split name was found.
3872                       ((null split-name)
3873                        (read-file-name
3874                         (concat prompt " (default "
3875                                 (file-name-nondirectory default-name) "): ")
3876                         (file-name-directory default-name)
3877                         default-name))
3878                       ;; A single group name is returned.
3879                       ((stringp split-name)
3880                        (setq default-name
3881                              (funcall function split-name headers
3882                                       (symbol-value variable)))
3883                        (read-file-name
3884                         (concat prompt " (default "
3885                                 (file-name-nondirectory default-name) "): ")
3886                         (file-name-directory default-name)
3887                         default-name))
3888                       ;; A single split name was found
3889                       ((= 1 (length split-name))
3890                        (let* ((name (expand-file-name
3891                                      (car split-name)
3892                                      gnus-article-save-directory))
3893                               (dir (cond ((file-directory-p name)
3894                                           (file-name-as-directory name))
3895                                          ((file-exists-p name) name)
3896                                          (t gnus-article-save-directory))))
3897                          (read-file-name
3898                           (concat prompt " (default " name "): ")
3899                           dir name)))
3900                       ;; A list of splits was found.
3901                       (t
3902                        (setq split-name (nreverse split-name))
3903                        (let (result)
3904                          (let ((file-name-history
3905                                 (nconc split-name file-name-history)))
3906                            (setq result
3907                                  (expand-file-name
3908                                   (read-file-name
3909                                    (concat prompt " (`M-p' for defaults): ")
3910                                    gnus-article-save-directory
3911                                    (car split-name))
3912                                   gnus-article-save-directory)))
3913                          (car (push result file-name-history)))))))
3914                ;; Create the directory.
3915                (gnus-make-directory (file-name-directory file))
3916                ;; If we have read a directory, we append the default file name.
3917                (when (file-directory-p file)
3918                  (setq file (expand-file-name (file-name-nondirectory
3919                                                default-name)
3920                                               (file-name-as-directory file))))
3921                ;; Possibly translate some characters.
3922                (nnheader-translate-file-chars file))))))
3923     (gnus-make-directory (file-name-directory result))
3924     (when variable
3925       (set variable result))
3926     (when dir-var
3927       (set dir-var (file-name-directory result)))
3928     result))
3929
3930 (defun gnus-article-archive-name (group)
3931   "Return the first instance of an \"Archive-name\" in the current buffer."
3932   (let ((case-fold-search t))
3933     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3934       (nnheader-concat gnus-article-save-directory
3935                        (match-string 1)))))
3936
3937 (defun gnus-article-nndoc-name (group)
3938   "If GROUP is an nndoc group, return the name of the parent group."
3939   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3940     (gnus-group-get-parameter group 'save-article-group)))
3941
3942 (defun gnus-summary-save-in-rmail (&optional filename)
3943   "Append this article to Rmail file.
3944 Optional argument FILENAME specifies file name.
3945 Directory to save to is default to `gnus-article-save-directory'."
3946   (setq filename (gnus-read-save-file-name
3947                   "Save %s in rmail file" filename
3948                   gnus-rmail-save-name gnus-newsgroup-name
3949                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3950   (gnus-eval-in-buffer-window gnus-save-article-buffer
3951     (save-excursion
3952       (save-restriction
3953         (widen)
3954         ;; Note that unlike gnus-summary-save-in-mail, there is no
3955         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3956         ;; not use Babyl.
3957         (gnus-output-to-rmail filename))))
3958   filename)
3959
3960 (defun gnus-summary-save-in-mail (&optional filename)
3961   "Append this article to Unix mail file.
3962 Optional argument FILENAME specifies file name.
3963 Directory to save to is default to `gnus-article-save-directory'."
3964   (setq filename (gnus-read-save-file-name
3965                   "Save %s in Unix mail file" filename
3966                   gnus-mail-save-name gnus-newsgroup-name
3967                   gnus-current-headers 'gnus-newsgroup-last-mail))
3968   (gnus-eval-in-buffer-window gnus-save-article-buffer
3969     (save-excursion
3970       (save-restriction
3971         (widen)
3972         (if (and (file-readable-p filename)
3973                  (file-regular-p filename)
3974                  (mail-file-babyl-p filename))
3975             (gnus-output-to-rmail filename)
3976           (gnus-output-to-mail filename)))))
3977   filename)
3978
3979 (put 'gnus-summary-save-in-file :decode t)
3980 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3981 (defun gnus-summary-save-in-file (&optional filename overwrite)
3982   "Append this article to file.
3983 Optional argument FILENAME specifies file name.
3984 Directory to save to is default to `gnus-article-save-directory'."
3985   (setq filename (gnus-read-save-file-name
3986                   "Save %s in file" filename
3987                   gnus-file-save-name gnus-newsgroup-name
3988                   gnus-current-headers 'gnus-newsgroup-last-file))
3989   (gnus-eval-in-buffer-window gnus-save-article-buffer
3990     (save-excursion
3991       (save-restriction
3992         (widen)
3993         (when (and overwrite
3994                    (file-exists-p filename))
3995           (delete-file filename))
3996         (gnus-output-to-file filename))))
3997   filename)
3998
3999 (put 'gnus-summary-write-to-file :decode t)
4000 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4001 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4002 (defun gnus-summary-write-to-file (&optional filename)
4003   "Write this article to a file, overwriting it if the file exists.
4004 Optional argument FILENAME specifies file name.
4005 The directory to save in defaults to `gnus-article-save-directory'."
4006   (setq filename (gnus-read-save-file-name
4007                   "Save %s in file" filename
4008                   gnus-file-save-name gnus-newsgroup-name
4009                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4010   (gnus-summary-save-in-file filename t))
4011
4012 (put 'gnus-summary-save-body-in-file :decode t)
4013 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4014   "Append this article body to a file.
4015 Optional argument FILENAME specifies file name.
4016 The directory to save in defaults to `gnus-article-save-directory'."
4017   (setq filename (gnus-read-save-file-name
4018                   "Save %s body in file" filename
4019                   gnus-file-save-name gnus-newsgroup-name
4020                   gnus-current-headers 'gnus-newsgroup-last-file))
4021   (gnus-eval-in-buffer-window gnus-save-article-buffer
4022     (save-excursion
4023       (save-restriction
4024         (widen)
4025         (when (article-goto-body)
4026           (narrow-to-region (point) (point-max)))
4027         (when (and overwrite
4028                    (file-exists-p filename))
4029           (delete-file filename))
4030         (gnus-output-to-file filename))))
4031   filename)
4032
4033 (put 'gnus-summary-write-body-to-file :decode t)
4034 (put 'gnus-summary-write-body-to-file
4035      :function 'gnus-summary-save-body-in-file)
4036 (defun gnus-summary-write-body-to-file (&optional filename)
4037   "Write this article body to a file, overwriting it if the file exists.
4038 Optional argument FILENAME specifies file name.
4039 The directory to save in defaults to `gnus-article-save-directory'."
4040   (setq filename (gnus-read-save-file-name
4041                   "Save %s body in file" filename
4042                   gnus-file-save-name gnus-newsgroup-name
4043                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4044   (gnus-summary-save-body-in-file filename t))
4045
4046 (put 'gnus-summary-save-in-pipe :decode t)
4047 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4048 (defun gnus-summary-save-in-pipe (&optional command raw)
4049   "Pipe this article to subprocess COMMAND.
4050 Valid values for COMMAND include:
4051   a string
4052     The executable command name and possibly arguments.
4053   nil
4054     You will be prompted for the command in the minibuffer.
4055   the symbol `default'
4056     It will be replaced with the command which the variable
4057     `gnus-summary-pipe-output-default-command' holds or the command
4058     last used for saving.
4059 Non-nil value for RAW overrides `:decode' and `:headers' properties
4060 and the raw article including all headers will be piped."
4061   (let ((article (gnus-summary-article-number))
4062         (decode (unless raw
4063                   (get 'gnus-summary-save-in-pipe :decode)))
4064         save-buffer default)
4065     (if article
4066         (if (vectorp (gnus-summary-article-header article))
4067             (save-current-buffer
4068               (gnus-summary-select-article decode decode nil article)
4069               (insert-buffer-substring
4070                (prog1
4071                    (if decode
4072                        gnus-article-buffer
4073                      gnus-original-article-buffer)
4074                  (setq save-buffer
4075                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4076               ;; Remove unwanted headers.
4077               (when (and (not raw)
4078                          (or (get 'gnus-summary-save-in-pipe :headers)
4079                              (not gnus-save-all-headers)))
4080                 (let ((gnus-visible-headers
4081                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4082                                               :headers))
4083                            gnus-saved-headers gnus-visible-headers))
4084                       (gnus-summary-buffer nil))
4085                   (article-hide-headers 1 t))))
4086           (error "%d is not a real article" article))
4087       (error "No article to pipe"))
4088     (setq default (or gnus-summary-pipe-output-default-command
4089                       gnus-last-shell-command))
4090     (unless (stringp command)
4091       (setq command
4092             (if (and (eq command 'default) default)
4093                 default
4094               (gnus-read-shell-command "Shell command on this article: "
4095                                        default))))
4096     (when (string-equal command "")
4097       (if default
4098           (setq command default)
4099         (error "A command is required")))
4100     (gnus-eval-in-buffer-window save-buffer
4101       (save-restriction
4102         (widen)
4103         (shell-command-on-region (point-min) (point-max) command nil)))
4104     (gnus-kill-buffer save-buffer))
4105   (setq gnus-summary-pipe-output-default-command command))
4106
4107 (defun gnus-summary-pipe-to-muttprint (&optional command)
4108   "Pipe this article to muttprint."
4109   (unless (stringp command)
4110     (setq command (read-string
4111                    "Print using command: " gnus-summary-muttprint-program
4112                    nil gnus-summary-muttprint-program)))
4113   (let ((gnus-summary-pipe-output-default-command
4114          gnus-summary-pipe-output-default-command))
4115     (gnus-summary-save-in-pipe command))
4116   (setq gnus-summary-muttprint-program command))
4117
4118 ;;; Article file names when saving.
4119
4120 (defun gnus-capitalize-newsgroup (newsgroup)
4121   "Capitalize NEWSGROUP name."
4122   (when (not (zerop (length newsgroup)))
4123     (concat (char-to-string (upcase (aref newsgroup 0)))
4124             (substring newsgroup 1))))
4125
4126 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4127   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4128 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4129 Otherwise, it is like ~/News/news/group/num."
4130   (let ((default
4131           (expand-file-name
4132            (concat (if (gnus-use-long-file-name 'not-save)
4133                        (gnus-capitalize-newsgroup newsgroup)
4134                      (gnus-newsgroup-directory-form newsgroup))
4135                    "/" (int-to-string (mail-header-number headers)))
4136            gnus-article-save-directory)))
4137     (if (and last-file
4138              (string-equal (file-name-directory default)
4139                            (file-name-directory last-file))
4140              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4141         default
4142       (or last-file default))))
4143
4144 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4145   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4146 If variable `gnus-use-long-file-name' is non-nil, it is
4147 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4148   (let ((default
4149           (expand-file-name
4150            (concat (if (gnus-use-long-file-name 'not-save)
4151                        newsgroup
4152                      (gnus-newsgroup-directory-form newsgroup))
4153                    "/" (int-to-string (mail-header-number headers)))
4154            gnus-article-save-directory)))
4155     (if (and last-file
4156              (string-equal (file-name-directory default)
4157                            (file-name-directory last-file))
4158              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4159         default
4160       (or last-file default))))
4161
4162 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4163   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4164 If variable `gnus-use-long-file-name' is non-nil, it is
4165 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4166   (or last-file
4167       (expand-file-name
4168        (if (gnus-use-long-file-name 'not-save)
4169            newsgroup
4170          (file-relative-name
4171           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4172           default-directory))
4173        gnus-article-save-directory)))
4174
4175 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4176   "Generate file name from sender."
4177   (let ((from (mail-header-from headers)))
4178     (expand-file-name
4179      (if (and from (string-match "\\([^ <]+\\)@" from))
4180          (match-string 1 from)
4181        "nobody")
4182      gnus-article-save-directory)))
4183
4184 (defun article-verify-x-pgp-sig ()
4185   "Verify X-PGP-Sig."
4186   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4187   (interactive)
4188   (if (gnus-buffer-live-p gnus-original-article-buffer)
4189       (let ((sig (with-current-buffer gnus-original-article-buffer
4190                    (gnus-fetch-field "X-PGP-Sig")))
4191             items info headers)
4192         (when (and sig
4193                    mml2015-use
4194                    (mml2015-clear-verify-function))
4195           (with-temp-buffer
4196             (insert-buffer-substring gnus-original-article-buffer)
4197             (setq items (split-string sig))
4198             (message-narrow-to-head)
4199             (let ((inhibit-point-motion-hooks t)
4200                   (case-fold-search t))
4201               ;; Don't verify multiple headers.
4202               (setq headers (mapconcat (lambda (header)
4203                                          (concat header ": "
4204                                                  (mail-fetch-field header)
4205                                                  "\n"))
4206                                        (split-string (nth 1 items) ",") "")))
4207             (delete-region (point-min) (point-max))
4208             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4209             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4210             (insert headers)
4211             (widen)
4212             (forward-line)
4213             (while (not (eobp))
4214               (if (looking-at "^-")
4215                   (insert "- "))
4216               (forward-line))
4217             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4218             (insert "Version: " (car items) "\n\n")
4219             (insert (mapconcat 'identity (cddr items) "\n"))
4220             (insert "\n-----END PGP SIGNATURE-----\n")
4221             (let ((mm-security-handle (list (format "multipart/signed"))))
4222               (mml2015-clean-buffer)
4223               (let ((coding-system-for-write (or gnus-newsgroup-charset
4224                                                  'iso-8859-1)))
4225                 (funcall (mml2015-clear-verify-function)))
4226               (setq info
4227                     (or (mm-handle-multipart-ctl-parameter
4228                          mm-security-handle 'gnus-details)
4229                         (mm-handle-multipart-ctl-parameter
4230                          mm-security-handle 'gnus-info)))))
4231           (when info
4232             (let ((inhibit-read-only t) bface eface)
4233               (save-restriction
4234                 (message-narrow-to-head)
4235                 (goto-char (point-max))
4236                 (forward-line -1)
4237                 (setq bface (get-text-property (point-at-bol) 'face)
4238                       eface (get-text-property (1- (point-at-eol)) 'face))
4239                 (message-remove-header "X-Gnus-PGP-Verify")
4240                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4241                     (forward-line)
4242                   (goto-char (point-max)))
4243                 (narrow-to-region (point) (point))
4244                 (insert "X-Gnus-PGP-Verify: " info "\n")
4245                 (goto-char (point-min))
4246                 (forward-line)
4247                 (while (not (eobp))
4248                   (if (not (looking-at "^[ \t]"))
4249                       (insert " "))
4250                   (forward-line))
4251                 ;; Do highlighting.
4252                 (goto-char (point-min))
4253                 (when (looking-at "\\([^:]+\\): *")
4254                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4255                                      'face bface)
4256                   (put-text-property (match-end 0) (point-max)
4257                                      'face eface)))))))))
4258
4259 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4260
4261 (defun article-verify-cancel-lock ()
4262   "Verify Cancel-Lock header."
4263   (interactive)
4264   (if (gnus-buffer-live-p gnus-original-article-buffer)
4265       (canlock-verify gnus-original-article-buffer)))
4266
4267 (eval-and-compile
4268   (mapc
4269    (lambda (func)
4270      (let (afunc gfunc)
4271        (if (consp func)
4272            (setq afunc (car func)
4273                  gfunc (cdr func))
4274          (setq afunc func
4275                gfunc (intern (format "gnus-%s" func))))
4276        (defalias gfunc
4277          (when (fboundp afunc)
4278            `(lambda (&optional interactive &rest args)
4279               ,(documentation afunc t)
4280               (interactive (list t))
4281               (with-current-buffer gnus-article-buffer
4282                 (if interactive
4283                     (call-interactively ',afunc)
4284                   (apply ',afunc args))))))))
4285    '(article-hide-headers
4286      article-verify-x-pgp-sig
4287      article-verify-cancel-lock
4288      article-hide-boring-headers
4289      article-treat-overstrike
4290      article-treat-ansi-sequences
4291      article-fill-long-lines
4292      article-capitalize-sentences
4293      article-remove-cr
4294      article-remove-leading-whitespace
4295      article-display-x-face
4296      article-display-face
4297      article-de-quoted-unreadable
4298      article-de-base64-unreadable
4299      article-decode-HZ
4300      article-wash-html
4301      article-unsplit-urls
4302      article-hide-list-identifiers
4303      article-strip-banner
4304      article-babel
4305      article-hide-pem
4306      article-hide-signature
4307      article-strip-headers-in-body
4308      article-remove-trailing-blank-lines
4309      article-strip-leading-blank-lines
4310      article-strip-multiple-blank-lines
4311      article-strip-leading-space
4312      article-strip-trailing-space
4313      article-strip-blank-lines
4314      article-strip-all-blank-lines
4315      article-date-local
4316      article-date-english
4317      article-date-iso8601
4318      article-date-original
4319      article-date-ut
4320      article-decode-mime-words
4321      article-decode-charset
4322      article-decode-encoded-words
4323      article-date-user
4324      article-date-lapsed
4325      article-emphasize
4326      article-treat-dumbquotes
4327      article-normalize-headers
4328      ;;(article-show-all . gnus-article-show-all-headers)
4329      )))
4330 \f
4331 ;;;
4332 ;;; Gnus article mode
4333 ;;;
4334
4335 (put 'gnus-article-mode 'mode-class 'special)
4336
4337 (set-keymap-parent gnus-article-mode-map widget-keymap)
4338
4339 (gnus-define-keys gnus-article-mode-map
4340   " " gnus-article-goto-next-page
4341   "\177" gnus-article-goto-prev-page
4342   [delete] gnus-article-goto-prev-page
4343   [backspace] gnus-article-goto-prev-page
4344   "\C-c^" gnus-article-refer-article
4345   "h" gnus-article-show-summary
4346   "s" gnus-article-show-summary
4347   "\C-c\C-m" gnus-article-mail
4348   "?" gnus-article-describe-briefly
4349   "<" beginning-of-buffer
4350   ">" end-of-buffer
4351   "\C-c\C-i" gnus-info-find-node
4352   "\C-c\C-b" gnus-bug
4353   "R" gnus-article-reply-with-original
4354   "F" gnus-article-followup-with-original
4355   "\C-hk" gnus-article-describe-key
4356   "\C-hc" gnus-article-describe-key-briefly
4357   "\C-hb" gnus-article-describe-bindings
4358
4359   "e" gnus-article-read-summary-keys
4360   "\C-d" gnus-article-read-summary-keys
4361   "\M-*" gnus-article-read-summary-keys
4362   "\M-#" gnus-article-read-summary-keys
4363   "\M-^" gnus-article-read-summary-keys
4364   "\M-g" gnus-article-read-summary-keys)
4365
4366 (substitute-key-definition
4367  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4368
4369 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4370   "W" gnus-article-wide-reply-with-original)
4371 (if (featurep 'xemacs)
4372     (set-keymap-default-binding gnus-article-send-map
4373                                 'gnus-article-read-summary-send-keys)
4374   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4375
4376 (defun gnus-article-make-menu-bar ()
4377   (unless (boundp 'gnus-article-commands-menu)
4378     (gnus-summary-make-menu-bar))
4379   (gnus-turn-off-edit-menu 'article)
4380   (unless (boundp 'gnus-article-article-menu)
4381     (easy-menu-define
4382      gnus-article-article-menu gnus-article-mode-map ""
4383      '("Article"
4384        ["Scroll forwards" gnus-article-goto-next-page t]
4385        ["Scroll backwards" gnus-article-goto-prev-page t]
4386        ["Show summary" gnus-article-show-summary t]
4387        ["Fetch Message-ID at point" gnus-article-refer-article t]
4388        ["Mail to address at point" gnus-article-mail t]
4389        ["Send a bug report" gnus-bug t]))
4390
4391     (easy-menu-define
4392      gnus-article-treatment-menu gnus-article-mode-map ""
4393      ;; Fixme: this should use :active (and maybe :visible).
4394      '("Treatment"
4395        ["Hide headers" gnus-article-hide-headers t]
4396        ["Hide signature" gnus-article-hide-signature t]
4397        ["Hide citation" gnus-article-hide-citation t]
4398        ["Treat overstrike" gnus-article-treat-overstrike t]
4399        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4400        ["Remove carriage return" gnus-article-remove-cr t]
4401        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4402        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4403        ["Remove base64" gnus-article-de-base64-unreadable t]
4404        ["Treat html" gnus-article-wash-html t]
4405        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4406        ["Decode HZ" gnus-article-decode-HZ t]))
4407
4408     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4409
4410     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4411
4412     (gnus-run-hooks 'gnus-article-menu-hook)))
4413
4414 (defvar bookmark-make-record-function)
4415
4416 (defun gnus-article-mode ()
4417   "Major mode for displaying an article.
4418
4419 All normal editing commands are switched off.
4420
4421 The following commands are available in addition to all summary mode
4422 commands:
4423 \\<gnus-article-mode-map>
4424 \\[gnus-article-next-page]\t Scroll the article one page forwards
4425 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4426 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4427 \\[gnus-article-show-summary]\t Display the summary buffer
4428 \\[gnus-article-mail]\t Send a reply to the address near point
4429 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4430 \\[gnus-info-find-node]\t Go to the Gnus info node"
4431   (interactive)
4432   (kill-all-local-variables)
4433   (gnus-simplify-mode-line)
4434   (setq mode-name "Article")
4435   (setq major-mode 'gnus-article-mode)
4436   (make-local-variable 'minor-mode-alist)
4437   (use-local-map gnus-article-mode-map)
4438   (when (gnus-visual-p 'article-menu 'menu)
4439     (gnus-article-make-menu-bar)
4440     (when gnus-summary-tool-bar-map
4441       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4442   (gnus-update-format-specifications nil 'article-mode)
4443   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4444   (set (make-local-variable 'gnus-page-broken) nil)
4445   (make-local-variable 'gnus-button-marker-list)
4446   (make-local-variable 'gnus-article-current-summary)
4447   (make-local-variable 'gnus-article-mime-handles)
4448   (make-local-variable 'gnus-article-decoded-p)
4449   (make-local-variable 'gnus-article-mime-handle-alist)
4450   (make-local-variable 'gnus-article-wash-types)
4451   (make-local-variable 'gnus-article-image-alist)
4452   (make-local-variable 'gnus-article-charset)
4453   (make-local-variable 'gnus-article-ignored-charsets)
4454   (set (make-local-variable 'bookmark-make-record-function)
4455        'gnus-summary-bookmark-make-record)
4456   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4457   ;; face.
4458   (set (make-local-variable 'nobreak-char-display) nil)
4459   (setq cursor-in-non-selected-windows nil)
4460   (setq truncate-lines gnus-article-truncate-lines)
4461   (gnus-set-default-directory)
4462   (buffer-disable-undo)
4463   (setq buffer-read-only t
4464         show-trailing-whitespace nil)
4465   (set-syntax-table gnus-article-mode-syntax-table)
4466   (mm-enable-multibyte)
4467   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4468
4469 (defvar gnus-button-marker-list nil
4470   "Regexp matching any of the regexps from `gnus-button-alist'.
4471 Internal variable.")
4472
4473 (defun gnus-article-setup-buffer ()
4474   "Initialize the article buffer."
4475   (let* ((name (if gnus-single-article-buffer "*Article*"
4476                  (concat "*Article " gnus-newsgroup-name "*")))
4477          (original
4478           (progn (string-match "\\*Article" name)
4479                  (concat " *Original Article"
4480                          (substring name (match-end 0))))))
4481     (setq gnus-article-buffer name)
4482     (setq gnus-original-article-buffer original)
4483     (setq gnus-article-mime-handle-alist nil)
4484     (with-current-buffer gnus-summary-buffer
4485       ;; This might be a variable local to the summary buffer.
4486       (unless gnus-single-article-buffer
4487         (setq gnus-article-buffer name)
4488         (setq gnus-original-article-buffer original)
4489         (gnus-set-global-variables)))
4490     (gnus-article-setup-highlight-words)
4491     ;; Init original article buffer.
4492     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4493       (mm-enable-multibyte)
4494       (setq major-mode 'gnus-original-article-mode)
4495       (make-local-variable 'gnus-original-article))
4496     (if (and (get-buffer name)
4497              (with-current-buffer name
4498                (if gnus-article-edit-mode
4499                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4500                        (progn
4501                          (set-buffer-modified-p nil)
4502                          (gnus-kill-buffer name)
4503                          (message "")
4504                          nil)
4505                      (error "Action aborted"))
4506                  t)))
4507         (with-current-buffer name
4508           (set (make-local-variable 'gnus-article-edit-mode) nil)
4509           (when gnus-article-mime-handles
4510             (mm-destroy-parts gnus-article-mime-handles)
4511             (setq gnus-article-mime-handles nil))
4512           ;; Set it to nil in article-buffer!
4513           (setq gnus-article-mime-handle-alist nil)
4514           (buffer-disable-undo)
4515           (setq buffer-read-only t)
4516           ;; This list just keeps growing if we don't reset it.
4517           (setq gnus-button-marker-list nil)
4518           (unless (eq major-mode 'gnus-article-mode)
4519             (gnus-article-mode))
4520           (current-buffer))
4521       (with-current-buffer (gnus-get-buffer-create name)
4522         (gnus-article-mode)
4523         (make-local-variable 'gnus-summary-buffer)
4524         (setq gnus-summary-buffer
4525               (gnus-summary-buffer-name gnus-newsgroup-name))
4526         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4527         (current-buffer)))))
4528
4529 ;; Set article window start at LINE, where LINE is the number of lines
4530 ;; from the head of the article.
4531 (defun gnus-article-set-window-start (&optional line)
4532   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4533     (when article-window
4534       (set-window-start
4535        article-window
4536        (with-current-buffer gnus-article-buffer
4537          (goto-char (point-min))
4538          (if (not line)
4539              (point-min)
4540            (gnus-message 6 "Moved to bookmark")
4541            (search-forward "\n\n" nil t)
4542            (forward-line line)
4543            (point)))))))
4544
4545 (defun gnus-article-prepare (article &optional all-headers header)
4546   "Prepare ARTICLE in article mode buffer.
4547 ARTICLE should either be an article number or a Message-ID.
4548 If ARTICLE is an id, HEADER should be the article headers.
4549 If ALL-HEADERS is non-nil, no headers are hidden."
4550   (save-excursion
4551     ;; Make sure we start in a summary buffer.
4552     (unless (eq major-mode 'gnus-summary-mode)
4553       (set-buffer gnus-summary-buffer))
4554     (setq gnus-summary-buffer (current-buffer))
4555     (let* ((gnus-article (if header (mail-header-number header) article))
4556            (summary-buffer (current-buffer))
4557            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4558            (group gnus-newsgroup-name)
4559            result)
4560       (save-excursion
4561         (gnus-article-setup-buffer)
4562         (set-buffer gnus-article-buffer)
4563         ;; Deactivate active regions.
4564         (when (and (boundp 'transient-mark-mode)
4565                    transient-mark-mode)
4566           (setq mark-active nil))
4567         (if (not (setq result (let ((inhibit-read-only t))
4568                                 (gnus-request-article-this-buffer
4569                                  article group))))
4570             ;; There is no such article.
4571             (save-excursion
4572               (when (and (numberp article)
4573                          (not (memq article gnus-newsgroup-sparse)))
4574                 (setq gnus-article-current
4575                       (cons gnus-newsgroup-name article))
4576                 (set-buffer gnus-summary-buffer)
4577                 (setq gnus-current-article article)
4578                 (if (and (memq article gnus-newsgroup-undownloaded)
4579                          (not (gnus-online (gnus-find-method-for-group
4580                                             gnus-newsgroup-name))))
4581                     (progn
4582                       (gnus-summary-set-agent-mark article)
4583                       (message "Message marked for downloading"))
4584                   (gnus-summary-mark-article article gnus-canceled-mark)
4585                   (unless (memq article gnus-newsgroup-sparse)
4586                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4587           (if (or (eq result 'pseudo)
4588                   (eq result 'nneething))
4589               (progn
4590                 (with-current-buffer summary-buffer
4591                   (push article gnus-newsgroup-history)
4592                   (setq gnus-last-article gnus-current-article
4593                         gnus-current-article 0
4594                         gnus-current-headers nil
4595                         gnus-article-current nil)
4596                   (if (eq result 'nneething)
4597                       (gnus-configure-windows 'summary)
4598                     (gnus-configure-windows 'article))
4599                   (gnus-set-global-variables))
4600                 (let ((gnus-article-mime-handle-alist-1
4601                        gnus-article-mime-handle-alist))
4602                   (gnus-set-mode-line 'article)))
4603             ;; The result from the `request' was an actual article -
4604             ;; or at least some text that is now displayed in the
4605             ;; article buffer.
4606             (when (and (numberp article)
4607                        (not (eq article gnus-current-article)))
4608               ;; Seems like a new article has been selected.
4609               ;; `gnus-current-article' must be an article number.
4610               (with-current-buffer summary-buffer
4611                 (push article gnus-newsgroup-history)
4612                 (setq gnus-last-article gnus-current-article
4613                       gnus-current-article article
4614                       gnus-current-headers
4615                       (gnus-summary-article-header gnus-current-article)
4616                       gnus-article-current
4617                       (cons gnus-newsgroup-name gnus-current-article))
4618                 (unless (vectorp gnus-current-headers)
4619                   (setq gnus-current-headers nil))
4620                 (gnus-summary-goto-subject gnus-current-article)
4621                 (when (gnus-summary-show-thread)
4622                   ;; If the summary buffer really was folded, the
4623                   ;; previous goto may not actually have gone to
4624                   ;; the right article, but the thread root instead.
4625                   ;; So we go again.
4626                   (gnus-summary-goto-subject gnus-current-article))
4627                 (gnus-run-hooks 'gnus-mark-article-hook)
4628                 (gnus-set-mode-line 'summary)
4629                 (when (gnus-visual-p 'article-highlight 'highlight)
4630                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4631                 ;; Set the global newsgroup variables here.
4632                 (gnus-set-global-variables)
4633                 (setq gnus-have-all-headers
4634                       (or all-headers gnus-show-all-headers))))
4635             (save-excursion
4636               (gnus-configure-windows 'article))
4637             (when (or (numberp article)
4638                       (stringp article))
4639               (gnus-article-prepare-display)
4640               ;; Do page break.
4641               (goto-char (point-min))
4642               (when gnus-break-pages
4643                 (gnus-narrow-to-page)))
4644             (let ((gnus-article-mime-handle-alist-1
4645                    gnus-article-mime-handle-alist))
4646               (gnus-set-mode-line 'article))
4647             (article-goto-body)
4648             (unless (bobp)
4649               (forward-line -1))
4650             (set-window-point (get-buffer-window (current-buffer)) (point))
4651             (gnus-configure-windows 'article)
4652             t))))))
4653
4654 ;;;###autoload
4655 (defun gnus-article-prepare-display ()
4656   "Make the current buffer look like a nice article."
4657   ;; Hooks for getting information from the article.
4658   ;; This hook must be called before being narrowed.
4659   (let ((gnus-article-buffer (current-buffer))
4660         buffer-read-only
4661         (inhibit-read-only t))
4662     (unless (eq major-mode 'gnus-article-mode)
4663       (gnus-article-mode))
4664     (setq buffer-read-only nil
4665           gnus-article-wash-types nil
4666           gnus-article-image-alist nil)
4667     (gnus-run-hooks 'gnus-tmp-internal-hook)
4668     (when gnus-display-mime-function
4669       (funcall gnus-display-mime-function))
4670     (gnus-run-hooks 'gnus-article-prepare-hook)))
4671
4672 ;;;
4673 ;;; Gnus Sticky Article Mode
4674 ;;;
4675
4676 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4677   "Mode for sticky articles."
4678   ;; Release bindings that won't work.
4679   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4680                              gnus-sticky-article-mode-map)
4681   (substitute-key-definition 'gnus-article-refer-article 'undefined
4682                              gnus-sticky-article-mode-map)
4683   (dolist (k '("e" "h" "s" "F" "R"))
4684     (define-key gnus-sticky-article-mode-map k nil))
4685   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4686   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4687   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4688   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4689
4690 (defun gnus-sticky-article (arg)
4691   "Make the current article sticky.
4692 If a prefix ARG is given, ask for a name for this sticky article buffer."
4693   (interactive "P")
4694   (gnus-summary-show-thread)
4695   (gnus-summary-select-article nil nil 'pseudo)
4696   (let (new-art-buf-name)
4697     (gnus-eval-in-buffer-window gnus-article-buffer
4698       (setq new-art-buf-name
4699             (concat
4700              "*Sticky Article: "
4701              (if arg
4702                  (read-from-minibuffer "Sticky article buffer name: ")
4703                (gnus-with-article-headers
4704                  (gnus-article-goto-header "subject")
4705                  (setq new-art-buf-name
4706                        (buffer-substring-no-properties
4707                         (line-beginning-position) (line-end-position)))
4708                  (goto-char (point-min))
4709                  (gnus-article-goto-header "from")
4710                  (setq new-art-buf-name
4711                        (concat
4712                         new-art-buf-name ", "
4713                         (buffer-substring-no-properties
4714                          (line-beginning-position) (line-end-position))))
4715                  (goto-char (point-min))
4716                  (gnus-article-goto-header "date")
4717                  (setq new-art-buf-name
4718                        (concat
4719                         new-art-buf-name ", "
4720                         (buffer-substring-no-properties
4721                          (line-beginning-position) (line-end-position))))))
4722              "*"))
4723       (if (and (gnus-buffer-live-p new-art-buf-name)
4724                (with-current-buffer new-art-buf-name
4725                  (eq major-mode 'gnus-sticky-article-mode)))
4726           (switch-to-buffer new-art-buf-name)
4727         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4728       (gnus-sticky-article-mode))
4729     (setq gnus-article-buffer new-art-buf-name))
4730   (gnus-summary-recenter)
4731   (gnus-summary-position-point))
4732
4733 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4734   "Kill the given sticky article BUFFER.
4735 If none is given, assume the current buffer and kill it if it has
4736 `gnus-sticky-article-mode'."
4737   (interactive)
4738   (unless buffer
4739     (setq buffer (current-buffer)))
4740   (with-current-buffer buffer
4741     (when (eq major-mode 'gnus-sticky-article-mode)
4742       (gnus-kill-buffer buffer))))
4743
4744 (defun gnus-kill-sticky-article-buffers (arg)
4745   "Kill all sticky article buffers.
4746 If a prefix ARG is given, ask for confirmation."
4747   (interactive "P")
4748   (dolist (buf (gnus-buffers))
4749     (with-current-buffer buf
4750       (when (eq major-mode 'gnus-sticky-article-mode)
4751         (if (not arg)
4752             (gnus-kill-buffer buf)
4753           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4754             (gnus-kill-buffer buf)))))))
4755
4756 ;;;
4757 ;;; Gnus MIME viewing functions
4758 ;;;
4759
4760 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4761   "Format of the MIME buttons.
4762
4763 Valid specifiers include:
4764 %t  The MIME type
4765 %T  MIME type, along with additional info
4766 %n  The `name' parameter
4767 %d  The description, if any
4768 %l  The length of the encoded part
4769 %p  The part identifier number
4770 %e  Dots if the part isn't displayed
4771
4772 General format specifiers can also be used.  See Info node
4773 `(gnus)Formatting Variables'.")
4774
4775 (defvar gnus-mime-button-line-format-alist
4776   '((?t gnus-tmp-type ?s)
4777     (?T gnus-tmp-type-long ?s)
4778     (?n gnus-tmp-name ?s)
4779     (?d gnus-tmp-description ?s)
4780     (?p gnus-tmp-id ?s)
4781     (?l gnus-tmp-length ?d)
4782     (?e gnus-tmp-dots ?s)))
4783
4784 (defvar gnus-mime-button-commands
4785   '((gnus-article-press-button "\r" "Toggle Display")
4786     (gnus-mime-view-part "v" "View Interactively...")
4787     (gnus-mime-view-part-as-type "t" "View As Type...")
4788     (gnus-mime-view-part-as-charset "C" "View As charset...")
4789     (gnus-mime-save-part "o" "Save...")
4790     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4791     (gnus-mime-replace-part "r" "Replace part")
4792     (gnus-mime-delete-part "d" "Delete part")
4793     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4794     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4795     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4796     (gnus-mime-view-part-externally "e" "View Externally")
4797     (gnus-mime-print-part "p" "Print")
4798     (gnus-mime-pipe-part "|" "Pipe To Command...")
4799     (gnus-mime-action-on-part "." "Take action on the part...")))
4800
4801 (defun gnus-article-mime-part-status ()
4802   (if gnus-article-mime-handle-alist-1
4803       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4804           " (1 part)"
4805         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4806     ""))
4807
4808 (defvar gnus-mime-button-map
4809   (let ((map (make-sparse-keymap)))
4810     (define-key map gnus-mouse-2 'gnus-article-push-button)
4811     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4812     (dolist (c gnus-mime-button-commands)
4813       (define-key map (cadr c) (car c)))
4814     map))
4815
4816 (easy-menu-define
4817   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4818   `("MIME Part"
4819     ,@(mapcar (lambda (c)
4820                 (vector (caddr c) (car c) :active t))
4821               gnus-mime-button-commands)))
4822
4823 (defvar gnus-url-button-commands
4824   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4825
4826 (defvar gnus-url-button-map
4827   (let ((map (make-sparse-keymap)))
4828     (dolist (c gnus-url-button-commands)
4829       (define-key map (cadr c) (car c)))
4830     map))
4831
4832 (easy-menu-define
4833   gnus-url-button-menu gnus-url-button-map "URL button menu."
4834   `("Url Button"
4835     ,@(mapcar (lambda (c)
4836                 (vector (caddr c) (car c) :active t))
4837               gnus-url-button-commands)))
4838
4839 (defmacro gnus-bind-safe-url-regexp (&rest body)
4840   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4841   `(let ((mm-w3m-safe-url-regexp
4842           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4843                                 (gnus-buffer-live-p
4844                                  gnus-article-current-summary))
4845                            (with-current-buffer gnus-article-current-summary
4846                              gnus-newsgroup-name)
4847                          gnus-newsgroup-name)))
4848             (if (cond ((not group)
4849                        ;; Maybe we're in a mml-preview buffer
4850                        ;; and no group is selected.
4851                        t)
4852                       ((stringp gnus-safe-html-newsgroups)
4853                        (string-match gnus-safe-html-newsgroups group))
4854                       ((consp gnus-safe-html-newsgroups)
4855                        (member group gnus-safe-html-newsgroups)))
4856                 nil
4857               mm-w3m-safe-url-regexp))))
4858      ,@body))
4859
4860 (defun gnus-mime-button-menu (event prefix)
4861  "Construct a context-sensitive menu of MIME commands."
4862  (interactive "e\nP")
4863  (save-window-excursion
4864    (let ((pos (event-start event)))
4865      (select-window (posn-window pos))
4866      (goto-char (posn-point pos))
4867      (gnus-article-check-buffer)
4868      (popup-menu gnus-mime-button-menu nil prefix))))
4869
4870 (defun gnus-mime-view-all-parts (&optional handles)
4871   "View all the MIME parts."
4872   (interactive)
4873   (with-current-buffer gnus-article-buffer
4874     (let ((handles (or handles gnus-article-mime-handles))
4875           (mail-parse-charset gnus-newsgroup-charset)
4876           (mail-parse-ignored-charsets
4877            (with-current-buffer gnus-summary-buffer
4878              gnus-newsgroup-ignored-charsets)))
4879       (when handles
4880         (mm-remove-parts handles)
4881         (goto-char (point-min))
4882         (or (search-forward "\n\n") (goto-char (point-max)))
4883         (let ((inhibit-read-only t))
4884           (delete-region (point) (point-max))
4885           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4886
4887 (defun gnus-article-jump-to-part (n)
4888   "Jump to MIME part N."
4889   (interactive "P")
4890   (pop-to-buffer gnus-article-buffer)
4891   ;; FIXME: why is it necessary?
4892   (sit-for 0)
4893   (let ((parts (length gnus-article-mime-handle-alist)))
4894     (or n (setq n (read-number (format "Jump to part (2..%s): " parts))))
4895     (unless (and (integerp n) (<= n parts) (>= n 1))
4896       (setq n
4897             (progn
4898               (gnus-message 7 "Invalid part `%s', using %s instead."
4899                             n parts)
4900               parts)))
4901     (gnus-message 9 "Jumping to part %s." n)
4902     (cond ((>= gnus-auto-select-part 1)
4903            (while (and (<= n parts)
4904                        (not (gnus-article-goto-part n)))
4905              (setq n (1+ n))))
4906           ((< gnus-auto-select-part 0)
4907            (while (and (>= n 1)
4908                        (not (gnus-article-goto-part n)))
4909              (setq n (1- n))))
4910           (t
4911            (gnus-article-goto-part n)))))
4912
4913 (defvar gnus-mime-buttonized-part-id nil
4914   "ID of a mime part that should be buttonized.
4915 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4916
4917 (eval-when-compile
4918   (defsubst gnus-article-edit-part (handles &optional current-id)
4919     "Edit an article in order to delete a mime part.
4920 This function is exclusively used by `gnus-mime-save-part-and-strip'
4921 and `gnus-mime-delete-part', and not provided at run-time normally."
4922     (gnus-article-edit-article
4923      `(lambda ()
4924         (buffer-disable-undo)
4925         (erase-buffer)
4926         (let ((mail-parse-charset (or gnus-article-charset
4927                                       ',gnus-newsgroup-charset))
4928               (mail-parse-ignored-charsets
4929                (or gnus-article-ignored-charsets
4930                    ',gnus-newsgroup-ignored-charsets))
4931               (mbl mml-buffer-list))
4932           (setq mml-buffer-list nil)
4933           (insert-buffer-substring gnus-original-article-buffer)
4934           (mime-to-mml ',handles)
4935           (setq gnus-article-mime-handles nil)
4936           (let ((mbl1 mml-buffer-list))
4937             (setq mml-buffer-list mbl)
4938             (set (make-local-variable 'mml-buffer-list) mbl1))
4939           (gnus-make-local-hook 'kill-buffer-hook)
4940           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4941      `(lambda (no-highlight)
4942         (let ((mail-parse-charset (or gnus-article-charset
4943                                       ',gnus-newsgroup-charset))
4944               (message-options message-options)
4945               (message-options-set-recipient)
4946               (mail-parse-ignored-charsets
4947                (or gnus-article-ignored-charsets
4948                    ',gnus-newsgroup-ignored-charsets)))
4949           (mml-to-mime)
4950           (mml-destroy-buffers)
4951           (remove-hook 'kill-buffer-hook
4952                        'mml-destroy-buffers t)
4953           (kill-local-variable 'mml-buffer-list))
4954         (gnus-summary-edit-article-done
4955          ,(or (mail-header-references gnus-current-headers) "")
4956          ,(gnus-group-read-only-p)
4957          ,gnus-summary-buffer no-highlight))
4958      t)
4959     ;; Force buttonizing this part.
4960     (let ((gnus-mime-buttonized-part-id current-id))
4961       (gnus-article-edit-done))
4962     (gnus-configure-windows 'article)
4963     (when (and current-id (integerp gnus-auto-select-part))
4964       (gnus-article-jump-to-part
4965        (min (max (+ current-id gnus-auto-select-part) 1)
4966             (with-current-buffer gnus-article-buffer
4967               (length gnus-article-mime-handle-alist)))))))
4968
4969 (defun gnus-mime-replace-part (file)
4970   "Replace MIME part under point with an external body."
4971   ;; Useful if file has already been saved to disk
4972   (interactive
4973    (list
4974     (read-file-name "Replace MIME part with file: "
4975                     (or mm-default-directory default-directory)
4976                     nil nil)))
4977   (gnus-mime-save-part-and-strip file))
4978
4979 (defun gnus-mime-save-part-and-strip (&optional file)
4980   "Save the MIME part under point then replace it with an external body.
4981 If FILE is given, use it for the external part."
4982   (interactive)
4983   (gnus-article-check-buffer)
4984   (when (gnus-group-read-only-p)
4985     (error "The current group does not support deleting of parts"))
4986   (when (mm-complicated-handles gnus-article-mime-handles)
4987     (error "\
4988 The current article has a complicated MIME structure, giving up..."))
4989   (let* ((data (get-text-property (point) 'gnus-data))
4990          (id (get-text-property (point) 'gnus-part))
4991          param
4992          (handles gnus-article-mime-handles))
4993     (unless file
4994       (setq file
4995             (and data (mm-save-part data "Delete MIME part and save to: "))))
4996     (when file
4997       (with-current-buffer (mm-handle-buffer data)
4998         (erase-buffer)
4999         (insert "Content-Type: " (mm-handle-media-type data))
5000         (mml-insert-parameter-string (cdr (mm-handle-type data))
5001                                      '(charset))
5002         ;; Add a filename for the sake of saving the part again.
5003         (mml-insert-parameter
5004          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5005         (insert "\n")
5006         (insert "Content-ID: " (message-make-message-id) "\n")
5007         (insert "Content-Transfer-Encoding: binary\n")
5008         (insert "\n"))
5009       (setcdr data
5010               (cdr (mm-make-handle nil
5011                                    `("message/external-body"
5012                                      (access-type . "LOCAL-FILE")
5013                                      (name . ,file)))))
5014       ;; (set-buffer gnus-summary-buffer)
5015       (gnus-article-edit-part handles id))))
5016
5017 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5018 ;; parts...>') but with stripping would be nice.
5019
5020 (defun gnus-mime-delete-part ()
5021   "Delete the MIME part under point.
5022 Replace it with some information about the removed part."
5023   (interactive)
5024   (gnus-article-check-buffer)
5025   (when (gnus-group-read-only-p)
5026     (error "The current group does not support deleting of parts"))
5027   (when (mm-complicated-handles gnus-article-mime-handles)
5028     (error "\
5029 The current article has a complicated MIME structure, giving up..."))
5030   (when (or gnus-expert-user
5031             (gnus-yes-or-no-p "\
5032 Deleting parts may malfunction or destroy the article; continue? "))
5033     (let* ((data (get-text-property (point) 'gnus-data))
5034            (id (get-text-property (point) 'gnus-part))
5035            (handles gnus-article-mime-handles)
5036            (none "(none)")
5037            (description
5038             (let ((desc (mm-handle-description data)))
5039               (when desc
5040                 (mail-decode-encoded-word-string desc))))
5041            (filename
5042             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5043                 none))
5044            (type (mm-handle-media-type data)))
5045       (unless data
5046         (error "No MIME part under point"))
5047       (with-current-buffer (mm-handle-buffer data)
5048         (let ((bsize (format "%s" (buffer-size))))
5049           (erase-buffer)
5050           (insert
5051            (concat
5052             ",----\n"
5053             "| The following attachment has been deleted:\n"
5054             "|\n"
5055             "| Type:           " type "\n"
5056             "| Filename:       " filename "\n"
5057             "| Size (encoded): " bsize " Byte\n"
5058             (when description
5059               (concat    "| Description:    " description "\n"))
5060             "`----\n"))
5061           (setcdr data
5062                   (cdr (mm-make-handle
5063                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5064                         (list "attachment")
5065                         (format "Deleted attachment (%s bytes)" bsize))))))
5066       ;; (set-buffer gnus-summary-buffer)
5067       (gnus-article-edit-part handles id))))
5068
5069 (defun gnus-mime-save-part ()
5070   "Save the MIME part under point."
5071   (interactive)
5072   (gnus-article-check-buffer)
5073   (let ((data (get-text-property (point) 'gnus-data)))
5074     (when data
5075       (mm-save-part data))))
5076
5077 (defun gnus-mime-pipe-part (&optional cmd)
5078   "Pipe the MIME part under point to a process.
5079 Use CMD as the process."
5080   (interactive)
5081   (gnus-article-check-buffer)
5082   (let ((data (get-text-property (point) 'gnus-data)))
5083     (when data
5084       (mm-pipe-part data cmd))))
5085
5086 (defun gnus-mime-view-part ()
5087   "Interactively choose a viewing method for the MIME part under point."
5088   (interactive)
5089   (gnus-article-check-buffer)
5090   (let ((data (get-text-property (point) 'gnus-data)))
5091     (when data
5092       (setq gnus-article-mime-handles
5093             (mm-merge-handles
5094              gnus-article-mime-handles (setq data (copy-sequence data))))
5095       (mm-interactively-view-part data))))
5096
5097 (defun gnus-mime-view-part-as-type-internal ()
5098   (gnus-article-check-buffer)
5099   (let* ((handle (get-text-property (point) 'gnus-data))
5100          (name (or
5101                 ;; Content-Type: foo/bar; name=...
5102                 (mail-content-type-get (mm-handle-type handle) 'name)
5103                 ;; Content-Disposition: attachment; filename=...
5104                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5105          (def-type (and name (mm-default-file-encoding name))))
5106     (or (and def-type (cons def-type 0))
5107         (and handle
5108              (equal (mm-handle-media-supertype handle) "text")
5109              '("text/plain" . 0))
5110         '("application/octet-stream" . 0))))
5111
5112 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5113   "Choose a MIME media type, and view the part as such.
5114 If non-nil, PRED is a predicate to use during completion to limit the
5115 available media-types."
5116   (interactive)
5117   (unless mime-type
5118     (setq mime-type
5119           (let ((default (gnus-mime-view-part-as-type-internal)))
5120             (completing-read
5121              (format "View as MIME type (default %s): "
5122                      (car default))
5123              (mapcar #'list (mailcap-mime-types))
5124              pred nil nil nil
5125              (car default)))))
5126   (gnus-article-check-buffer)
5127   (let ((handle (get-text-property (point) 'gnus-data)))
5128     (when handle
5129       (when (equal (mm-handle-media-type handle) "message/external-body")
5130         (unless (mm-handle-cache handle)
5131           (mm-extern-cache-contents handle))
5132         (setq handle (mm-handle-cache handle)))
5133       (setq handle
5134             (mm-make-handle (mm-handle-buffer handle)
5135                             (cons mime-type (cdr (mm-handle-type handle)))
5136                             (mm-handle-encoding handle)
5137                             (mm-handle-undisplayer handle)
5138                             (mm-handle-disposition handle)
5139                             (mm-handle-description handle)
5140                             nil
5141                             (mm-handle-id handle)))
5142       (setq gnus-article-mime-handles
5143             (mm-merge-handles gnus-article-mime-handles handle))
5144       (when (mm-handle-displayed-p handle)
5145         (mm-remove-part handle))
5146       (gnus-mm-display-part handle))))
5147
5148 (defun gnus-mime-copy-part (&optional handle arg)
5149   "Put the MIME part under point into a new buffer.
5150 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5151 are decompressed."
5152   (interactive (list nil current-prefix-arg))
5153   (gnus-article-check-buffer)
5154   (unless handle
5155     (setq handle (get-text-property (point) 'gnus-data)))
5156   (when handle
5157     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5158                                                'name)
5159                         (mail-content-type-get (mm-handle-disposition handle)
5160                                                'filename)))
5161           contents dont-decode charset coding-system)
5162       (mm-with-unibyte-buffer
5163         (mm-insert-part handle)
5164         (setq contents (or (condition-case nil
5165                                (mm-decompress-buffer filename nil 'sig)
5166                              (error
5167                               (setq dont-decode t)
5168                               nil))
5169                            (buffer-string))))
5170       (setq filename (cond (filename (file-name-nondirectory filename))
5171                            (dont-decode "*raw data*")
5172                            (t "*decoded*")))
5173       (cond
5174        (dont-decode)
5175        ((not arg)
5176         (unless (setq charset (mail-content-type-get
5177                                (mm-handle-type handle) 'charset))
5178           (unless (setq coding-system (mm-with-unibyte-buffer
5179                                         (insert contents)
5180                                         (mm-find-buffer-file-coding-system)))
5181             (setq charset gnus-newsgroup-charset))))
5182        ((numberp arg)
5183         (setq charset (or (cdr (assq arg
5184                                      gnus-summary-show-article-charset-alist))
5185                           (mm-read-coding-system "Charset: ")))))
5186       (switch-to-buffer (generate-new-buffer filename))
5187       (if (or coding-system
5188               (and charset
5189                    (setq coding-system (mm-charset-to-coding-system charset))
5190                    (not (eq charset 'ascii))))
5191           (progn
5192             (mm-enable-multibyte)
5193             (insert (mm-decode-coding-string contents coding-system))
5194             (setq buffer-file-coding-system
5195                   (if (boundp 'last-coding-system-used)
5196                       (symbol-value 'last-coding-system-used)
5197                     coding-system)))
5198         (mm-disable-multibyte)
5199         (insert contents)
5200         (setq buffer-file-coding-system mm-binary-coding-system))
5201       ;; We do it this way to make `normal-mode' set the appropriate mode.
5202       (unwind-protect
5203           (progn
5204             (setq buffer-file-name (expand-file-name filename))
5205             (normal-mode))
5206         (setq buffer-file-name nil))
5207       (goto-char (point-min)))))
5208
5209 (defun gnus-mime-print-part (&optional handle filename)
5210   "Print the MIME part under point."
5211   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5212   (gnus-article-check-buffer)
5213   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5214          (contents (and handle (mm-get-part handle)))
5215          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5216          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5217     (when contents
5218         (if printer
5219             (unwind-protect
5220                 (progn
5221                   (mm-save-part-to-file handle file)
5222                   (call-process shell-file-name nil
5223                                 (generate-new-buffer " *mm*")
5224                                 nil
5225                                 shell-command-switch
5226                                 (mm-mailcap-command
5227                                  printer file (mm-handle-type handle))))
5228               (delete-file file))
5229           (with-temp-buffer
5230             (insert contents)
5231             (gnus-print-buffer))
5232           (ps-despool filename)))))
5233
5234 (defun gnus-mime-inline-part (&optional handle arg)
5235   "Insert the MIME part under point into the current buffer.
5236 Compressed files like .gz and .bz2 are decompressed."
5237   (interactive (list nil current-prefix-arg))
5238   (gnus-article-check-buffer)
5239   (unless handle
5240     (setq handle (get-text-property (point) 'gnus-data)))
5241   (when handle
5242     (let ((b (point))
5243           (inhibit-read-only t)
5244           contents charset coding-system)
5245       (if (and (not arg) (mm-handle-undisplayer handle))
5246           (mm-remove-part handle)
5247         (mm-with-unibyte-buffer
5248           (mm-insert-part handle)
5249           (setq contents
5250                 (or (mm-decompress-buffer
5251                      (or (mail-content-type-get (mm-handle-type handle)
5252                                                 'name)
5253                          (mail-content-type-get (mm-handle-disposition handle)
5254                                                 'filename))
5255                      nil t)
5256                     (buffer-string))))
5257         (cond
5258          ((not arg)
5259           (unless (setq charset (mail-content-type-get
5260                                  (mm-handle-type handle) 'charset))
5261             (unless (setq coding-system
5262                           (mm-with-unibyte-buffer
5263                             (insert contents)
5264                             (mm-find-buffer-file-coding-system)))
5265               (setq charset gnus-newsgroup-charset))))
5266          ((numberp arg)
5267           (if (mm-handle-undisplayer handle)
5268               (mm-remove-part handle))
5269           (setq charset
5270                 (or (cdr (assq arg
5271                                gnus-summary-show-article-charset-alist))
5272                     (mm-read-coding-system "Charset: "))))
5273          (t
5274           (if (mm-handle-undisplayer handle)
5275               (mm-remove-part handle))))
5276         (forward-line 2)
5277         (mm-insert-inline
5278          handle
5279          (if (or coding-system
5280                  (and charset
5281                       (setq coding-system
5282                             (mm-charset-to-coding-system charset))
5283                       (not (eq coding-system 'ascii))))
5284              (mm-decode-coding-string contents coding-system)
5285            (mm-string-to-multibyte contents)))
5286         (goto-char b)))))
5287
5288 (defun gnus-mime-set-charset-parameters (handle charset)
5289   "Set CHARSET to parameters in HANDLE.
5290 CHARSET may either be a string or a symbol."
5291   (unless (stringp charset)
5292     (setq charset (symbol-name charset)))
5293   (if (stringp (car handle))
5294       (dolist (h (cdr handle))
5295         (gnus-mime-set-charset-parameters h charset))
5296     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5297                                             "message/external-body")
5298                                      (progn
5299                                        (unless (mm-handle-cache handle)
5300                                          (mm-extern-cache-contents handle))
5301                                        (mm-handle-cache handle))
5302                                    handle)))
5303            (param (assq 'charset (cdr type))))
5304       (if param
5305           (setcdr param charset)
5306         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5307
5308 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5309   "Insert the MIME part under point into the current buffer using the
5310 specified charset."
5311   (interactive (list nil current-prefix-arg))
5312   (gnus-article-check-buffer)
5313   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5314         (fun (get-text-property (point) 'gnus-callback))
5315         (gnus-newsgroup-ignored-charsets 'gnus-all)
5316         charset form preferred parts)
5317     (when handle
5318       (when (prog1
5319                 (and fun
5320                      (setq charset
5321                            (or (cdr (assq
5322                                      arg
5323                                      gnus-summary-show-article-charset-alist))
5324                                (mm-read-coding-system "Charset: "))))
5325               (if (mm-handle-undisplayer handle)
5326                   (mm-remove-part handle)))
5327         (gnus-mime-set-charset-parameters handle charset)
5328         (when (and (consp (setq form (cdr-safe fun)))
5329                    (setq form (ignore-errors
5330                                 (assq 'gnus-mime-display-alternative form)))
5331                    (setq preferred (caddr form))
5332                    (progn
5333                      (when (eq (car preferred) 'quote)
5334                        (setq preferred (cadr preferred)))
5335                      (not (equal preferred
5336                                  (get-text-property (point) 'gnus-data))))
5337                    (setq parts (get-text-property (point) 'gnus-part))
5338                    (setq parts (cdr (assq parts
5339                                           gnus-article-mime-handle-alist)))
5340                    (equal (mm-handle-media-type parts) "multipart/alternative")
5341                    (setq parts (reverse (cdr parts))))
5342           (setcar (cddr form)
5343                   (list 'quote (or (cadr (member preferred parts))
5344                                    (car parts)))))
5345         (funcall fun handle)))))
5346
5347 (defun gnus-mime-view-part-externally (&optional handle)
5348   "View the MIME part under point with an external viewer."
5349   (interactive)
5350   (gnus-article-check-buffer)
5351   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5352          (mm-user-display-methods nil)
5353          (mm-inlined-types nil)
5354          (mail-parse-charset gnus-newsgroup-charset)
5355          (mail-parse-ignored-charsets
5356           (with-current-buffer gnus-summary-buffer
5357             gnus-newsgroup-ignored-charsets))
5358          (type (mm-handle-media-type handle))
5359          (method (mailcap-mime-info type))
5360          (mm-enable-external t))
5361     (if (not (stringp method))
5362         (gnus-mime-view-part-as-type
5363          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5364       (when handle
5365         (if (mm-handle-undisplayer handle)
5366             (mm-remove-part handle)
5367           (mm-display-part handle))))))
5368
5369 (defun gnus-mime-view-part-internally (&optional handle)
5370   "View the MIME part under point with an internal viewer.
5371 If no internal viewer is available, use an external viewer."
5372   (interactive)
5373   (gnus-article-check-buffer)
5374   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5375          (mm-inlined-types '(".*"))
5376          (mm-inline-large-images t)
5377          (mail-parse-charset gnus-newsgroup-charset)
5378          (mail-parse-ignored-charsets
5379           (with-current-buffer gnus-summary-buffer
5380             gnus-newsgroup-ignored-charsets))
5381          (inhibit-read-only t))
5382     (if (not (mm-inlinable-p handle))
5383         (gnus-mime-view-part-as-type
5384          nil (lambda (types) (mm-inlinable-p handle (car types))))
5385       (when handle
5386         (if (mm-handle-undisplayer handle)
5387             (mm-remove-part handle)
5388           (gnus-bind-safe-url-regexp (mm-display-part handle)))))))
5389
5390 (defun gnus-mime-action-on-part (&optional action)
5391   "Do something with the MIME attachment at \(point\)."
5392   (interactive
5393    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5394   (gnus-article-check-buffer)
5395   (let ((action-pair (assoc action gnus-mime-action-alist)))
5396     (if action-pair
5397         (funcall (cdr action-pair)))))
5398
5399 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5400   "Call FUNCTION on MIME part N.
5401 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5402 If INTERACTIVE, call FUNCTION interactivly."
5403   (let (window frame)
5404     ;; Check whether the article is displayed.
5405     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5406                  (setq window (get-buffer-window gnus-article-buffer t))
5407                  (frame-visible-p (setq frame (window-frame window))))
5408       (error "No article is displayed"))
5409     (with-current-buffer gnus-article-buffer
5410       ;; Check whether the article displays the right contents.
5411       (unless (with-current-buffer gnus-summary-buffer
5412                 (eq gnus-current-article (gnus-summary-article-number)))
5413         (error "You should select the right article first"))
5414       (if n
5415           (setq n (prefix-numeric-value n))
5416         (let ((pt (point)))
5417           (setq n (or (get-text-property pt 'gnus-part)
5418                       (and (not (bobp))
5419                            (get-text-property (1- pt) 'gnus-part))
5420                       (get-text-property (prog2
5421                                              (forward-line 1)
5422                                              (point)
5423                                            (goto-char pt))
5424                                          'gnus-part)
5425                       (get-text-property
5426                        (or (and (setq pt (previous-single-property-change
5427                                           pt 'gnus-part))
5428                                 (1- pt))
5429                            (next-single-property-change (point) 'gnus-part)
5430                            (point))
5431                        'gnus-part)
5432                       1))))
5433       ;; Check whether the specified part exists.
5434       (when (> n (length gnus-article-mime-handle-alist))
5435         (error "No such part")))
5436     (unless
5437         (progn
5438           ;; To select the window is needed so that the cursor
5439           ;; might be visible on the MIME button.
5440           (select-window (prog1
5441                              window
5442                            (setq window (selected-window))
5443                            ;; Article may be displayed in the other frame.
5444                            (gnus-select-frame-set-input-focus
5445                             (prog1
5446                                 frame
5447                               (setq frame (selected-frame))))))
5448           (when (gnus-article-goto-part n)
5449             ;; We point the cursor and the arrow at the MIME button
5450             ;; when the `function' prompt the user for something.
5451             (let ((cursor-in-non-selected-windows t)
5452                   (overlay-arrow-string "=>")
5453                   (overlay-arrow-position (point-marker)))
5454               (unwind-protect
5455                   (cond
5456                    ((and no-handle interactive)
5457                     (call-interactively function))
5458                    (no-handle
5459                     (funcall function))
5460                    (interactive
5461                     (call-interactively
5462                      function
5463                      (cdr (assq n gnus-article-mime-handle-alist))))
5464                    (t
5465                     (funcall function
5466                              (cdr (assq n gnus-article-mime-handle-alist)))))
5467                 (set-marker overlay-arrow-position nil)
5468                 (unless gnus-auto-select-part
5469                   (gnus-select-frame-set-input-focus frame)
5470                   (select-window window))))
5471             t))
5472       (if gnus-inhibit-mime-unbuttonizing
5473           ;; This is the default though the program shouldn't reach here.
5474           (error "No such part")
5475         ;; The part which doesn't have the MIME button is selected.
5476         ;; So, we display all the buttons and redo it.
5477         (let ((gnus-inhibit-mime-unbuttonizing t))
5478           (gnus-summary-show-article)
5479           (gnus-article-part-wrapper n function no-handle))))))
5480
5481 (defun gnus-article-pipe-part (n)
5482   "Pipe MIME part N, which is the numerical prefix."
5483   (interactive "P")
5484   (gnus-article-part-wrapper n 'mm-pipe-part))
5485
5486 (defun gnus-article-save-part (n)
5487   "Save MIME part N, which is the numerical prefix."
5488   (interactive "P")
5489   (gnus-article-part-wrapper n 'mm-save-part))
5490
5491 (defun gnus-article-interactively-view-part (n)
5492   "View MIME part N interactively, which is the numerical prefix."
5493   (interactive "P")
5494   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5495
5496 (defun gnus-article-copy-part (n)
5497   "Copy MIME part N, which is the numerical prefix."
5498   (interactive "P")
5499   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5500
5501 (defun gnus-article-view-part-as-charset (n)
5502   "View MIME part N using a specified charset.
5503 N is the numerical prefix."
5504   (interactive "P")
5505   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5506
5507 (defun gnus-article-view-part-externally (n)
5508   "View MIME part N externally, which is the numerical prefix."
5509   (interactive "P")
5510   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5511
5512 (defun gnus-article-inline-part (n)
5513   "Inline MIME part N, which is the numerical prefix."
5514   (interactive "P")
5515   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5516
5517 (defun gnus-article-save-part-and-strip (n)
5518   "Save MIME part N and replace it with an external body.
5519 N is the numerical prefix."
5520   (interactive "P")
5521   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5522
5523 (defun gnus-article-replace-part (n)
5524   "Replace MIME part N with an external body.
5525 N is the numerical prefix."
5526   (interactive "P")
5527   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5528
5529 (defun gnus-article-delete-part (n)
5530   "Delete MIME part N and add some information about the removed part.
5531 N is the numerical prefix."
5532   (interactive "P")
5533   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5534
5535 (defun gnus-article-view-part-as-type (n)
5536   "Choose a MIME media type, and view part N as such.
5537 N is the numerical prefix."
5538   (interactive "P")
5539   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5540
5541 (defun gnus-article-mime-match-handle-first (condition)
5542   (if condition
5543       (let (n)
5544         (dolist (ihandle gnus-article-mime-handle-alist)
5545           (if (and (cond
5546                     ((functionp condition)
5547                      (funcall condition (cdr ihandle)))
5548                     ((eq condition 'undisplayed)
5549                      (not (or (mm-handle-undisplayer (cdr ihandle))
5550                               (equal (mm-handle-media-type (cdr ihandle))
5551                                      "multipart/alternative"))))
5552                     ((eq condition 'undisplayed-alternative)
5553                      (not (mm-handle-undisplayer (cdr ihandle))))
5554                     (t t))
5555                    (gnus-article-goto-part (car ihandle))
5556                    (or (not n) (< (car ihandle) n)))
5557               (setq n (car ihandle))))
5558         (or n 1))
5559     1))
5560
5561 (defun gnus-article-view-part (&optional n)
5562   "View MIME part N, which is the numerical prefix.
5563 If the part is already shown, hide the part.  If N is nil, view
5564 all parts."
5565   (interactive "P")
5566   (with-current-buffer gnus-article-buffer
5567     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5568                              gnus-article-mime-match-handle-function)))
5569     (when (> n (length gnus-article-mime-handle-alist))
5570       (error "No such part"))
5571     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5572       (when (gnus-article-goto-part n)
5573         (if (equal (car handle) "multipart/alternative")
5574             (gnus-article-press-button)
5575           (when (eq (gnus-mm-display-part handle) 'internal)
5576             (gnus-set-window-start)))))))
5577
5578 (defsubst gnus-article-mime-total-parts ()
5579   (if (bufferp (car gnus-article-mime-handles))
5580       1 ;; single part
5581     (1- (length gnus-article-mime-handles))))
5582
5583 (defun gnus-mm-display-part (handle)
5584   "Display HANDLE and fix MIME button."
5585   (let ((id (get-text-property (point) 'gnus-part))
5586         (point (point))
5587         (inhibit-read-only t))
5588     (forward-line 1)
5589     (prog1
5590         (let ((window (selected-window))
5591               (mail-parse-charset gnus-newsgroup-charset)
5592               (mail-parse-ignored-charsets
5593                (if (gnus-buffer-live-p gnus-summary-buffer)
5594                    (with-current-buffer gnus-summary-buffer
5595                      gnus-newsgroup-ignored-charsets)
5596                  nil)))
5597           (save-excursion
5598             (unwind-protect
5599                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5600                       (beg (point)))
5601                   (when win
5602                     (select-window win))
5603                   (goto-char point)
5604                   (forward-line)
5605                   (if (mm-handle-displayed-p handle)
5606                       ;; This will remove the part.
5607                       (mm-display-part handle)
5608                     (save-restriction
5609                       (narrow-to-region (point)
5610                                         (if (eobp) (point) (1+ (point))))
5611                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5612                       ;; We narrow to the part itself and
5613                       ;; then call the treatment functions.
5614                       (goto-char (point-min))
5615                       (forward-line 1)
5616                       (narrow-to-region (point) (point-max))
5617                       (gnus-treat-article
5618                        nil id
5619                        (gnus-article-mime-total-parts)
5620                        (mm-handle-media-type handle)))))
5621               (if (window-live-p window)
5622                   (select-window window)))))
5623       (goto-char point)
5624       (gnus-delete-line)
5625       (gnus-insert-mime-button
5626        handle id (list (mm-handle-displayed-p handle)))
5627       (goto-char point))))
5628
5629 (defun gnus-article-goto-part (n)
5630   "Go to MIME part N."
5631   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5632
5633 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5634   (let ((gnus-tmp-name
5635          (or (mail-content-type-get (mm-handle-type handle) 'name)
5636              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5637              (mail-content-type-get (mm-handle-type handle) 'url)
5638              ""))
5639         (gnus-tmp-type (mm-handle-media-type handle))
5640         (gnus-tmp-description (or (mm-handle-description handle) ""))
5641         (gnus-tmp-dots
5642          (if (if displayed (car displayed)
5643                (mm-handle-displayed-p handle))
5644              "" "..."))
5645         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5646                            (buffer-size)))
5647         gnus-tmp-type-long b e)
5648     (when (string-match ".*/" gnus-tmp-name)
5649       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5650     (setq gnus-tmp-type-long (concat gnus-tmp-type
5651                                      (and (not (equal gnus-tmp-name ""))
5652                                           (concat "; " gnus-tmp-name))))
5653     (unless (equal gnus-tmp-description "")
5654       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5655     (unless (bolp)
5656       (insert "\n"))
5657     (setq b (point))
5658     (gnus-eval-format
5659      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5660      `(keymap ,gnus-mime-button-map
5661               gnus-callback gnus-mm-display-part
5662               gnus-part ,gnus-tmp-id
5663               article-type annotation
5664               gnus-data ,handle))
5665     (setq e (if (bolp)
5666                 ;; Exclude a newline.
5667                 (1- (point))
5668               (point)))
5669     (when gnus-article-button-face
5670       (gnus-overlay-put (gnus-make-overlay b e nil t)
5671                         'face gnus-article-button-face))
5672     (widget-convert-button
5673      'link b e
5674      :mime-handle handle
5675      :action 'gnus-widget-press-button
5676      :button-keymap gnus-mime-button-map
5677      :help-echo
5678      (lambda (widget)
5679        ;; Needed to properly clear the message due to a bug in
5680        ;; wid-edit (XEmacs only).
5681        (if (boundp 'help-echo-owns-message)
5682            (setq help-echo-owns-message t))
5683        (format
5684         "%S: %s the MIME part; %S: more options"
5685         (aref gnus-mouse-2 0)
5686         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5687             "hide" "show")
5688         (aref gnus-down-mouse-3 0))))))
5689
5690 (defun gnus-widget-press-button (elems el)
5691   (goto-char (widget-get elems :from))
5692   (gnus-article-press-button))
5693
5694 (defvar gnus-displaying-mime nil)
5695
5696 (defun gnus-display-mime (&optional ihandles)
5697   "Display the MIME parts."
5698   (save-excursion
5699     (save-selected-window
5700       (let ((window (get-buffer-window gnus-article-buffer))
5701             (point (point)))
5702         (when window
5703           (select-window window)
5704           ;; We have to do this since selecting the window
5705           ;; may change the point.  So we set the window point.
5706           (set-window-point window point)))
5707       (let ((handles ihandles)
5708             (inhibit-read-only t)
5709             handle)
5710         (cond (handles)
5711               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5712                (when gnus-article-emulate-mime
5713                  (mm-uu-dissect-text-parts handles)))
5714               (gnus-article-emulate-mime
5715                (setq handles (mm-uu-dissect))))
5716         (when (and (not ihandles)
5717                    (not gnus-displaying-mime))
5718           ;; Top-level call; we clean up.
5719           (when gnus-article-mime-handles
5720             (mm-destroy-parts gnus-article-mime-handles)
5721             (setq gnus-article-mime-handle-alist nil));; A trick.
5722           (setq gnus-article-mime-handles handles)
5723           ;; We allow users to glean info from the handles.
5724           (when gnus-article-mime-part-function
5725             (gnus-mime-part-function handles)))
5726         (if (and handles
5727                  (or (not (stringp (car handles)))
5728                      (cdr handles)))
5729             (progn
5730               (when (and (not ihandles)
5731                          (not gnus-displaying-mime))
5732                 ;; Clean up for mime parts.
5733                 (article-goto-body)
5734                 (delete-region (point) (point-max)))
5735               (let ((gnus-displaying-mime t))
5736                 (gnus-mime-display-part handles)))
5737           (save-restriction
5738             (article-goto-body)
5739             (narrow-to-region (point) (point-max))
5740             (gnus-treat-article nil 1 1)
5741             (widen)))
5742         (unless ihandles
5743           ;; Highlight the headers.
5744           (save-excursion
5745             (save-restriction
5746               (article-goto-body)
5747               (narrow-to-region (point-min) (point))
5748               (gnus-article-save-original-date
5749                (gnus-treat-article 'head)))))))
5750     ;; Cope with broken MIME messages.
5751     (goto-char (point-max))
5752     (unless (bolp)
5753       (insert "\n"))))
5754
5755 (defcustom gnus-mime-display-multipart-as-mixed nil
5756   "Display \"multipart\" parts as  \"multipart/mixed\".
5757
5758 If t, it overrides nil values of
5759 `gnus-mime-display-multipart-alternative-as-mixed' and
5760 `gnus-mime-display-multipart-related-as-mixed'."
5761   :group 'gnus-article-mime
5762   :type 'boolean)
5763
5764 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5765   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5766   :version "22.1"
5767   :group 'gnus-article-mime
5768   :type 'boolean)
5769
5770 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5771   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5772
5773 If displaying \"text/html\" is discouraged \(see
5774 `mm-discouraged-alternatives'\) images or other material inside a
5775 \"multipart/related\" part might be overlooked when this variable is nil."
5776   :version "22.1"
5777   :group 'gnus-article-mime
5778   :type 'boolean)
5779
5780 (defun gnus-mime-display-part (handle)
5781   (cond
5782    ;; Maybe a broken MIME message.
5783    ((null handle))
5784    ;; Single part.
5785    ((not (stringp (car handle)))
5786     (gnus-mime-display-single handle))
5787    ;; User-defined multipart
5788    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5789     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5790              handle))
5791    ;; multipart/alternative
5792    ((and (equal (car handle) "multipart/alternative")
5793          (not (or gnus-mime-display-multipart-as-mixed
5794                   gnus-mime-display-multipart-alternative-as-mixed)))
5795     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5796       (push (cons id handle) gnus-article-mime-handle-alist)
5797       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5798    ;; multipart/related
5799    ((and (equal (car handle) "multipart/related")
5800          (not (or gnus-mime-display-multipart-as-mixed
5801                   gnus-mime-display-multipart-related-as-mixed)))
5802     ;;;!!!We should find the start part, but we just default
5803     ;;;!!!to the first part.
5804     ;;(gnus-mime-display-part (cadr handle))
5805     ;;;!!! Most multipart/related is an HTML message plus images.
5806     ;;;!!! Unfortunately we are unable to let W3 display those
5807     ;;;!!! included images, so we just display it as a mixed multipart.
5808     ;;(gnus-mime-display-mixed (cdr handle))
5809     ;;;!!! No, w3 can display everything just fine.
5810     (gnus-mime-display-part (cadr handle)))
5811    ((equal (car handle) "multipart/signed")
5812     (gnus-add-wash-type 'signed)
5813     (gnus-mime-display-security handle))
5814    ((equal (car handle) "multipart/encrypted")
5815     (gnus-add-wash-type 'encrypted)
5816     (gnus-mime-display-security handle))
5817    ;; Other multiparts are handled like multipart/mixed.
5818    (t
5819     (gnus-mime-display-mixed (cdr handle)))))
5820
5821 (defun gnus-mime-part-function (handles)
5822   (if (stringp (car handles))
5823       (mapcar 'gnus-mime-part-function (cdr handles))
5824     (funcall gnus-article-mime-part-function handles)))
5825
5826 (defun gnus-mime-display-mixed (handles)
5827   (mapcar 'gnus-mime-display-part handles))
5828
5829 (defun gnus-mime-display-single (handle)
5830   (let ((type (mm-handle-media-type handle))
5831         (ignored gnus-ignored-mime-types)
5832         (not-attachment t)
5833         (move nil)
5834         display text)
5835     (catch 'ignored
5836       (progn
5837         (while ignored
5838           (when (string-match (pop ignored) type)
5839             (throw 'ignored nil)))
5840         (if (and (setq not-attachment
5841                        (and (not (mm-inline-override-p handle))
5842                             (or (not (mm-handle-disposition handle))
5843                                 (equal (car (mm-handle-disposition handle))
5844                                        "inline")
5845                                 (mm-attachment-override-p handle))))
5846                  (mm-automatic-display-p handle)
5847                  (or (and
5848                       (mm-inlinable-p handle)
5849                       (mm-inlined-p handle))
5850                      (mm-automatic-external-display-p type)))
5851             (setq display t)
5852           (when (equal (mm-handle-media-supertype handle) "text")
5853             (setq text t)))
5854         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5855               beg)
5856           (push (cons id handle) gnus-article-mime-handle-alist)
5857           (when (and display
5858                      (equal (mm-handle-media-supertype handle) "message"))
5859             (insert-char
5860              ?\n
5861              (cond ((not (bolp)) 2)
5862                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5863                    (t 1))))
5864           (when (or (not display)
5865                     (not (gnus-unbuttonized-mime-type-p type))
5866                     (eq id gnus-mime-buttonized-part-id))
5867             (gnus-insert-mime-button
5868              handle id (list (or display (and not-attachment text))))
5869             (gnus-article-insert-newline)
5870             ;; Remember modify the number of forward lines.
5871             (setq move t))
5872           (setq beg (point))
5873           (cond
5874            (display
5875             (when move
5876               (forward-line -1)
5877               (setq beg (point)))
5878             (let ((mail-parse-charset gnus-newsgroup-charset)
5879                   (mail-parse-ignored-charsets
5880                    (save-excursion (condition-case ()
5881                                        (set-buffer gnus-summary-buffer)
5882                                      (error))
5883                                    gnus-newsgroup-ignored-charsets)))
5884               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5885             (goto-char (point-max)))
5886            ((and text not-attachment)
5887             (when move
5888               (forward-line -1)
5889               (setq beg (point)))
5890             (gnus-article-insert-newline)
5891             (mm-insert-inline
5892              handle
5893              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5894                                                        'charset)
5895                                 (and (equal type "text/calendar") 'utf-8))))
5896                (cond ((not charset)
5897                       (mm-string-as-multibyte (mm-get-part handle)))
5898                      ((eq charset 'gnus-decoded)
5899                       (with-current-buffer (mm-handle-buffer handle)
5900                         (buffer-string)))
5901                      (t
5902                       (mm-decode-string (mm-get-part handle) charset)))))
5903             (goto-char (point-max))))
5904           ;; Do highlighting.
5905           (save-excursion
5906             (save-restriction
5907               (narrow-to-region beg (point))
5908               (if (eq handle gnus-article-mime-handles)
5909                   ;; The format=flowed case.
5910                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5911                 ;; Don't count signature parts that are never displayed.
5912                 ;; The part number should be re-calculated supposing this
5913                 ;; might be a message/rfc822 part.
5914                 (let (handles)
5915                   (dolist (part gnus-article-mime-handles)
5916                     (unless (or (stringp part)
5917                                 (equal (car (mm-handle-type part))
5918                                        "application/pgp-signature"))
5919                       (push part handles)))
5920                   (gnus-treat-article
5921                    nil (length (memq handle handles)) (length handles)
5922                    (mm-handle-media-type handle)))))))))))
5923
5924 (defun gnus-unbuttonized-mime-type-p (type)
5925   "Say whether TYPE is to be unbuttonized."
5926   (unless gnus-inhibit-mime-unbuttonizing
5927     (when (catch 'found
5928             (let ((types gnus-unbuttonized-mime-types))
5929               (while types
5930                 (when (string-match (pop types) type)
5931                   (throw 'found t)))))
5932       (not (catch 'found
5933              (let ((types gnus-buttonized-mime-types))
5934                (while types
5935                  (when (string-match (pop types) type)
5936                    (throw 'found t)))))))))
5937
5938 (defun gnus-article-insert-newline ()
5939   "Insert a newline, but mark it as undeletable."
5940   (gnus-put-text-property
5941    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5942
5943 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5944   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5945          (ihandles handles)
5946          (point (point))
5947          handle (inhibit-read-only t) from props begend not-pref)
5948     (save-window-excursion
5949       (save-restriction
5950         (when ibegend
5951           (narrow-to-region (car ibegend)
5952                             (or (cdr ibegend)
5953                                 (progn
5954                                   (goto-char (car ibegend))
5955                                   (forward-line 2)
5956                                   (point))))
5957           (delete-region (point-min) (point-max))
5958           (mm-remove-parts handles))
5959         (setq begend (list (point-marker)))
5960         ;; Do the toggle.
5961         (unless (setq not-pref (cadr (member preferred ihandles)))
5962           (setq not-pref (car ihandles)))
5963         (when (or ibegend
5964                   (not preferred)
5965                   (not (gnus-unbuttonized-mime-type-p
5966                         "multipart/alternative")))
5967           (gnus-add-text-properties
5968            (setq from (point))
5969            (progn
5970              (insert (format "%d.  " id))
5971              (point))
5972            `(gnus-callback
5973              (lambda (handles)
5974                (unless ,(not ibegend)
5975                  (setq gnus-article-mime-handle-alist
5976                        ',gnus-article-mime-handle-alist))
5977                (gnus-mime-display-alternative
5978                 ',ihandles ',not-pref ',begend ,id))
5979              keymap ,gnus-mime-button-map
5980              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5981              face ,gnus-article-button-face
5982              gnus-part ,id
5983              article-type multipart))
5984           (widget-convert-button 'link from (point)
5985                                  :action 'gnus-widget-press-button
5986                                  :button-keymap gnus-widget-button-keymap)
5987           ;; Do the handles
5988           (while (setq handle (pop handles))
5989             (gnus-add-text-properties
5990              (setq from (point))
5991              (progn
5992                (insert (format "(%c) %-18s"
5993                                (if (equal handle preferred) ?* ? )
5994                                (mm-handle-media-type handle)))
5995                (point))
5996              `(gnus-callback
5997                (lambda (handles)
5998                  (unless ,(not ibegend)
5999                    (setq gnus-article-mime-handle-alist
6000                          ',gnus-article-mime-handle-alist))
6001                  (gnus-mime-display-alternative
6002                   ',ihandles ',handle ',begend ,id))
6003                keymap ,gnus-mime-button-map
6004                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6005                face ,gnus-article-button-face
6006                gnus-part ,id
6007                gnus-data ,handle))
6008             (widget-convert-button 'link from (point)
6009                                    :action 'gnus-widget-press-button
6010                                    :button-keymap gnus-widget-button-keymap)
6011             (insert "  "))
6012           (insert "\n\n"))
6013         (when preferred
6014           (if (stringp (car preferred))
6015               (gnus-display-mime preferred)
6016             (let ((mail-parse-charset gnus-newsgroup-charset)
6017                   (mail-parse-ignored-charsets
6018                    (with-current-buffer gnus-summary-buffer
6019                      gnus-newsgroup-ignored-charsets)))
6020               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6021               ;; Do highlighting.
6022               (save-excursion
6023                 (save-restriction
6024                   (narrow-to-region (car begend) (point-max))
6025                   (gnus-treat-article
6026                    nil (length gnus-article-mime-handle-alist)
6027                    (gnus-article-mime-total-parts)
6028                    (mm-handle-media-type handle))))))
6029           (goto-char (point-max))
6030           (setcdr begend (point-marker)))))
6031     (when ibegend
6032       (goto-char point))))
6033
6034 (defconst gnus-article-wash-status-strings
6035   (let ((alist '((cite "c" "Possible hidden citation text"
6036                        " " "All citation text visible")
6037                  (headers "h" "Hidden headers"
6038                           " " "All headers visible.")
6039                  (pgp "p" "Encrypted or signed message status hidden"
6040                       " " "No hidden encryption nor digital signature status")
6041                  (signature "s" "Signature has been hidden"
6042                             " " "Signature is visible")
6043                  (overstrike "o" "Overstrike (^H) characters applied"
6044                              " " "No overstrike characters applied")
6045                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6046                            " " "No /*_emphasis_*/ characters applied")))
6047         result)
6048     (dolist (entry alist result)
6049       (let ((key (nth 0 entry))
6050             (on (copy-sequence (nth 1 entry)))
6051             (on-help (nth 2 entry))
6052             (off (copy-sequence (nth 3 entry)))
6053             (off-help (nth 4 entry)))
6054         (put-text-property 0 1 'help-echo on-help on)
6055         (put-text-property 0 1 'help-echo off-help off)
6056         (push (list key on off) result))))
6057   "Alist of strings describing wash status in the mode line.
6058 Each entry has the form (KEY ON OF), where the KEY is a symbol
6059 representing the particular washing function, ON is the string to use
6060 in the article mode line when the washing function is active, and OFF
6061 is the string to use when it is inactive.")
6062
6063 (defun gnus-article-wash-status-entry (key value)
6064   (let ((entry (assoc key gnus-article-wash-status-strings)))
6065     (if value (nth 1 entry) (nth 2 entry))))
6066
6067 (defun gnus-article-wash-status ()
6068   "Return a string which display status of article washing."
6069   (with-current-buffer gnus-article-buffer
6070     (let ((cite (memq 'cite gnus-article-wash-types))
6071           (headers (memq 'headers gnus-article-wash-types))
6072           (boring (memq 'boring-headers gnus-article-wash-types))
6073           (pgp (memq 'pgp gnus-article-wash-types))
6074           (pem (memq 'pem gnus-article-wash-types))
6075           (signed (memq 'signed gnus-article-wash-types))
6076           (encrypted (memq 'encrypted gnus-article-wash-types))
6077           (signature (memq 'signature gnus-article-wash-types))
6078           (overstrike (memq 'overstrike gnus-article-wash-types))
6079           (emphasis (memq 'emphasis gnus-article-wash-types)))
6080       (concat
6081        (gnus-article-wash-status-entry 'cite cite)
6082        (gnus-article-wash-status-entry 'headers (or headers boring))
6083        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6084        (gnus-article-wash-status-entry 'signature signature)
6085        (gnus-article-wash-status-entry 'overstrike overstrike)
6086        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6087
6088 (defun gnus-add-wash-type (type)
6089   "Add a washing of TYPE to the current status."
6090   (add-to-list 'gnus-article-wash-types type))
6091
6092 (defun gnus-delete-wash-type (type)
6093   "Add a washing of TYPE to the current status."
6094   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6095
6096 (defun gnus-add-image (category image)
6097   "Add IMAGE of CATEGORY to the list of displayed images."
6098   (let ((entry (assq category gnus-article-image-alist)))
6099     (unless entry
6100       (setq entry (list category))
6101       (push entry gnus-article-image-alist))
6102     (nconc entry (list image))))
6103
6104 (defun gnus-delete-images (category)
6105   "Delete all images in CATEGORY."
6106   (let ((entry (assq category gnus-article-image-alist)))
6107     (dolist (image (cdr entry))
6108       (gnus-remove-image image category))
6109     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6110     (gnus-delete-wash-type category)))
6111
6112 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6113
6114 (defun gnus-article-maybe-hide-headers ()
6115   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6116 Provided for backwards compatibility."
6117   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6118                  (not (with-current-buffer gnus-summary-buffer
6119                         gnus-have-all-headers)))
6120              (not gnus-inhibit-hiding))
6121     (gnus-article-hide-headers)))
6122
6123 ;;; Article savers.
6124
6125 (defun gnus-output-to-file (file-name)
6126   "Append the current article to a file named FILE-NAME.
6127 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6128 text and used as the value of the coding cookie which is added to the
6129 top of a file.  Otherwise, this function saves a raw article without
6130 the coding cookie."
6131   (let* ((artbuf (current-buffer))
6132          (file-name-coding-system nnmail-pathname-coding-system)
6133          (coding gnus-article-save-coding-system)
6134          (coding-system-for-read (if coding
6135                                      nil ;; Rely on the coding cookie.
6136                                    mm-text-coding-system))
6137          (coding-system-for-write (or coding
6138                                       mm-text-coding-system-for-write
6139                                       mm-text-coding-system))
6140          (exists (file-exists-p file-name)))
6141     (with-temp-buffer
6142       (when exists
6143         (insert-file-contents file-name)
6144         (goto-char (point-min))
6145         ;; Remove the existing coding cookie.
6146         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6147           (delete-region (match-beginning 0) (match-end 0))))
6148       (goto-char (point-max))
6149       (insert-buffer-substring artbuf)
6150       ;; Append newline at end of the buffer as separator, and then
6151       ;; save it to file.
6152       (goto-char (point-max))
6153       (insert "\n")
6154       (when coding
6155         ;; If the coding system is not suitable to encode the text,
6156         ;; ask a user for a proper one.
6157         (when (fboundp 'select-safe-coding-system)
6158           (setq coding (coding-system-base
6159                         (save-window-excursion
6160                           (select-safe-coding-system (point-min) (point-max)
6161                                                      coding))))
6162           (setq coding-system-for-write
6163                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6164                     coding)))
6165         (goto-char (point-min))
6166         ;; Add the coding cookie.
6167         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6168                         coding-system-for-write)))
6169       (if exists
6170           (progn
6171             (write-region (point-min) (point-max) file-name nil 'no-message)
6172             (message "Appended to %s" file-name))
6173         (write-region (point-min) (point-max) file-name))))
6174   t)
6175
6176 (defun gnus-narrow-to-page (&optional arg)
6177   "Narrow the article buffer to a page.
6178 If given a numerical ARG, move forward ARG pages."
6179   (interactive "P")
6180   (setq arg (if arg (prefix-numeric-value arg) 0))
6181   (with-current-buffer gnus-article-buffer
6182     (widen)
6183     ;; Remove any old next/prev buttons.
6184     (when (gnus-visual-p 'page-marker)
6185       (let ((inhibit-read-only t))
6186         (gnus-remove-text-with-property 'gnus-prev)
6187         (gnus-remove-text-with-property 'gnus-next)))
6188     (let (st nd pt)
6189       (when (save-excursion
6190               (cond ((< arg 0)
6191                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6192                          (prog1
6193                              (setq nd (match-beginning 0)
6194                                    pt nd)
6195                            (when (re-search-backward page-delimiter nil t)
6196                              (setq st (match-end 0))))
6197                        (when (re-search-forward page-delimiter nil t)
6198                          (setq nd (match-beginning 0)
6199                                pt (point-min)))))
6200                     ((> arg 0)
6201                      (if (re-search-forward page-delimiter nil 'move arg)
6202                          (prog1
6203                              (setq st (match-end 0)
6204                                    pt st)
6205                            (when (re-search-forward page-delimiter nil t)
6206                              (setq nd (match-beginning 0))))
6207                        (when (re-search-backward page-delimiter nil t)
6208                          (setq st (match-end 0)
6209                                pt (point-max)))))
6210                     (t
6211                      (when (re-search-backward page-delimiter nil t)
6212                        (goto-char (setq st (match-end 0))))
6213                      (when (re-search-forward page-delimiter nil t)
6214                        (setq nd (match-beginning 0)))
6215                      (or st nd))))
6216         (setq gnus-page-broken t)
6217         (when pt (goto-char pt))
6218         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6219         (when (gnus-visual-p 'page-marker)
6220           (save-excursion
6221             (when nd
6222               (goto-char nd)
6223               (gnus-insert-next-page-button))
6224             (when st
6225               (goto-char st)
6226               (gnus-insert-prev-page-button))))))))
6227
6228 ;; Article mode commands
6229
6230 (defun gnus-article-goto-next-page ()
6231   "Show the next page of the article."
6232   (interactive)
6233   (when (gnus-article-next-page)
6234     (goto-char (point-min))
6235     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6236
6237
6238 (defun gnus-article-goto-prev-page ()
6239   "Show the previous page of the article."
6240   (interactive)
6241   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6242       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6243     (gnus-article-prev-page nil)))
6244
6245 ;; This is cleaner but currently breaks `gnus-pick-mode':
6246 ;;
6247 ;; (defun gnus-article-goto-next-page ()
6248 ;;   "Show the next page of the article."
6249 ;;   (interactive)
6250 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6251 ;;     (gnus-summary-next-page)))
6252 ;;
6253 ;; (defun gnus-article-goto-prev-page ()
6254 ;;   "Show the next page of the article."
6255 ;;   (interactive)
6256 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6257 ;;     (gnus-summary-prev-page)))
6258
6259 (defun gnus-article-next-page (&optional lines)
6260   "Show the next page of the current article.
6261 If end of article, return non-nil.  Otherwise return nil.
6262 Argument LINES specifies lines to be scrolled up."
6263   (interactive "p")
6264   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6265   (if (and (not (and gnus-article-over-scroll
6266                      (> (count-lines (window-start) (point-max))
6267                         (if (featurep 'xemacs)
6268                             (or lines (1- (window-height)))
6269                           (+ (or lines (1- (window-height))) scroll-margin)))))
6270            (save-excursion
6271              (end-of-line)
6272              (and (pos-visible-in-window-p)     ;Not continuation line.
6273                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6274       ;; Nothing in this page.
6275       (if (or (not gnus-page-broken)
6276               (save-excursion
6277                 (save-restriction
6278                   (widen)
6279                   (forward-line)
6280                   (eobp)))) ;Real end-of-buffer?
6281           (progn
6282             (when gnus-article-over-scroll
6283               (gnus-article-next-page-1 lines))
6284             t)                  ;Nothing more.
6285         (gnus-narrow-to-page 1)         ;Go to next page.
6286         nil)
6287     ;; More in this page.
6288     (gnus-article-next-page-1 lines)
6289     nil))
6290
6291 (defun gnus-article-beginning-of-window ()
6292   "Move point to the beginning of the window.
6293 In Emacs, the point is placed at the line number which `scroll-margin'
6294 specifies."
6295   (if (featurep 'xemacs)
6296       (move-to-window-line 0)
6297     ;; There is an obscure bug in Emacs that makes it impossible to
6298     ;; scroll past big pictures in the article buffer.  Try to fix
6299     ;; this by adding a sanity check by counting the lines visible.
6300     (when (> (count-lines (window-start) (window-end)) 30)
6301       (move-to-window-line
6302        (min (max 0 scroll-margin)
6303             (max 1 (- (window-height)
6304                       (if mode-line-format 1 0)
6305                       (if header-line-format 1 0)
6306                       2)))))))
6307
6308 (defun gnus-article-next-page-1 (lines)
6309   (condition-case ()
6310       (let ((scroll-in-place nil))
6311         (scroll-up lines))
6312     (end-of-buffer
6313      ;; Long lines may cause an end-of-buffer error.
6314      (goto-char (point-max))))
6315   (gnus-article-beginning-of-window))
6316
6317 (defun gnus-article-prev-page (&optional lines)
6318   "Show previous page of current article.
6319 Argument LINES specifies lines to be scrolled down."
6320   (interactive "p")
6321   (move-to-window-line 0)
6322   (if (and gnus-page-broken
6323            (bobp)
6324            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6325       (progn
6326         (gnus-narrow-to-page -1)        ;Go to previous page.
6327         (goto-char (point-max))
6328         (recenter (if gnus-article-over-scroll
6329                       (if lines
6330                           (max (if (featurep 'xemacs)
6331                                    lines
6332                                  (+ lines scroll-margin))
6333                                3)
6334                         (- (window-height) 2))
6335                     -1)))
6336     (prog1
6337         (condition-case ()
6338             (let ((scroll-in-place nil))
6339               (scroll-down lines))
6340           (beginning-of-buffer
6341            (goto-char (point-min))))
6342       (gnus-article-beginning-of-window))))
6343
6344 (defun gnus-article-only-boring-p ()
6345   "Decide whether there is only boring text remaining in the article.
6346 Something \"interesting\" is a word of at least two letters that does
6347 not have a face in `gnus-article-boring-faces'."
6348   (when (and gnus-article-skip-boring
6349              (boundp 'gnus-article-boring-faces)
6350              (symbol-value 'gnus-article-boring-faces))
6351     (save-excursion
6352       (let ((inhibit-point-motion-hooks t))
6353         (catch 'only-boring
6354           (while (re-search-forward "\\b\\w\\w" nil t)
6355             (forward-char -1)
6356             (when (not (gnus-intersection
6357                         (gnus-faces-at (point))
6358                         (symbol-value 'gnus-article-boring-faces)))
6359               (throw 'only-boring nil)))
6360           (throw 'only-boring t))))))
6361
6362 (defun gnus-article-refer-article ()
6363   "Read article specified by message-id around point."
6364   (interactive)
6365   (save-excursion
6366     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6367     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6368     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6369         (let ((msg-id (concat "<" (match-string 0) ">")))
6370           (set-buffer gnus-summary-buffer)
6371           (gnus-summary-refer-article msg-id))
6372       (error "No references around point"))))
6373
6374 (defun gnus-article-show-summary ()
6375   "Reconfigure windows to show summary buffer."
6376   (interactive)
6377   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6378       (error "There is no summary buffer for this article buffer")
6379     (gnus-article-set-globals)
6380     (gnus-configure-windows 'article)
6381     (gnus-summary-goto-subject gnus-current-article)
6382     (gnus-summary-position-point)))
6383
6384 (defun gnus-article-describe-briefly ()
6385   "Describe article mode commands briefly."
6386   (interactive)
6387   (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")))
6388
6389 (defun gnus-article-check-buffer ()
6390   "Beep if not in an article buffer."
6391   (unless (equal major-mode 'gnus-article-mode)
6392     (error "Command invoked outside of a Gnus article buffer")))
6393
6394 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6395   "Read a summary buffer key sequence and execute it from the article buffer."
6396   (interactive "P")
6397   (gnus-article-check-buffer)
6398   (let ((nosaves
6399          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6400            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6401            "=" "^" "\M-^" "|"))
6402         (nosave-but-article
6403          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6404            "An" "Ap" [?A (meta return)] [?A delete]))
6405         (nosave-in-article
6406          '("AS" "\C-d"))
6407         (up-to-top
6408          '("n" "Gn" "p" "Gp"))
6409         keys new-sum-point)
6410     (with-current-buffer gnus-article-current-summary
6411       (let (gnus-pick-mode)
6412         (setq unread-command-events (nconc unread-command-events
6413                                            (list (or key last-command-event)))
6414               keys (if (featurep 'xemacs)
6415                        (events-to-keys (read-key-sequence nil t))
6416                      (read-key-sequence nil t)))))
6417
6418     (message "")
6419
6420     (cond
6421      ((eq (aref keys (1- (length keys))) ?\C-h)
6422       (gnus-article-describe-bindings (substring keys 0 -1)))
6423      ((or (member keys nosaves)
6424           (member keys nosave-but-article)
6425           (member keys nosave-in-article))
6426       (let (func)
6427         (save-window-excursion
6428           (pop-to-buffer gnus-article-current-summary)
6429           ;; We disable the pick minor mode commands.
6430           (let (gnus-pick-mode)
6431             (setq func (key-binding keys t))))
6432         (if (or (not func)
6433                 (numberp func))
6434             (ding)
6435           (unless (member keys nosave-in-article)
6436             (set-buffer gnus-article-current-summary))
6437           (call-interactively func)
6438           (setq new-sum-point (point)))
6439         (when (member keys nosave-but-article)
6440           (pop-to-buffer gnus-article-buffer))))
6441      (t
6442       ;; These commands should restore window configuration.
6443       (let ((obuf (current-buffer))
6444             (owin (current-window-configuration))
6445             win func in-buffer selected new-sum-start new-sum-hscroll err)
6446         (cond (not-restore-window
6447                (pop-to-buffer gnus-article-current-summary)
6448                (setq win (selected-window)))
6449               ((setq win (get-buffer-window gnus-article-current-summary))
6450                (select-window win))
6451               (t
6452                (let ((summary-buffer gnus-article-current-summary))
6453                  (gnus-configure-windows 'article)
6454                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6455                    (let ((gnus-buffer-configuration
6456                           '((article ((vertical 1.0
6457                                                 (summary 0.25 point)
6458                                                 (article 1.0)))))))
6459                      (gnus-configure-windows 'article))
6460                    (setq win (get-buffer-window summary-buffer 'visible)))
6461                  (gnus-select-frame-set-input-focus (window-frame win))
6462                  (select-window win))))
6463         (setq in-buffer (current-buffer))
6464         ;; We disable the pick minor mode commands.
6465         (if (and (setq func (let (gnus-pick-mode)
6466                               (key-binding keys t)))
6467                  (functionp func)
6468                  (condition-case code
6469                      (progn
6470                        (call-interactively func)
6471                        t)
6472                    (error
6473                     (setq err code)
6474                     nil)))
6475             (progn
6476               (when (eq win (selected-window))
6477                 (setq new-sum-point (point)
6478                       new-sum-start (window-start win)
6479                       new-sum-hscroll (window-hscroll win)))
6480               (when (or (eq in-buffer (current-buffer))
6481                         (when (eq obuf (current-buffer))
6482                           (set-buffer in-buffer)
6483                           t))
6484                 (setq selected (gnus-summary-select-article))
6485                 (set-buffer obuf)
6486                 (unless not-restore-window
6487                   (set-window-configuration owin))
6488                 (when (and (eq selected 'old)
6489                            new-sum-point)
6490                   (set-window-start (get-buffer-window (current-buffer))
6491                                     1)
6492                   (set-window-point (get-buffer-window (current-buffer))
6493                                     (if (article-goto-body)
6494                                         (1- (point))
6495                                       (point))))
6496                 (when (and (not not-restore-window)
6497                            new-sum-point
6498                            (window-live-p win)
6499                            (with-current-buffer (window-buffer win)
6500                              (eq major-mode 'gnus-summary-mode)))
6501                   (set-window-point win new-sum-point)
6502                   (set-window-start win new-sum-start)
6503                   (set-window-hscroll win new-sum-hscroll))))
6504           (set-window-configuration owin)
6505           (if err
6506               (signal (car err) (cdr err))
6507             (ding))))))))
6508
6509 (defun gnus-article-read-summary-send-keys ()
6510   (interactive)
6511   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6512     (gnus-article-read-summary-keys)))
6513
6514 (defun gnus-article-describe-key (key)
6515   "Display documentation of the function invoked by KEY.
6516 KEY is a string or a vector."
6517   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6518                        (read-key-sequence "Describe key: "))))
6519   (gnus-article-check-buffer)
6520   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6521                                   gnus-article-read-summary-send-keys))
6522       (with-current-buffer gnus-article-current-summary
6523         (setq unread-command-events
6524               (if (featurep 'xemacs)
6525                   (append key nil)
6526                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6527                                         (list 'meta (- x 128))
6528                                       x))
6529                         key)))
6530         (let ((cursor-in-echo-area t)
6531               gnus-pick-mode)
6532           (describe-key (read-key-sequence nil t))))
6533     (describe-key key)))
6534
6535 (defun gnus-article-describe-key-briefly (key &optional insert)
6536   "Display documentation of the function invoked by KEY.
6537 KEY is a string or a vector."
6538   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6539                        (read-key-sequence "Describe key: "))
6540                      current-prefix-arg))
6541   (gnus-article-check-buffer)
6542   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6543                                   gnus-article-read-summary-send-keys))
6544       (with-current-buffer gnus-article-current-summary
6545         (setq unread-command-events
6546               (if (featurep 'xemacs)
6547                   (append key nil)
6548                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6549                                         (list 'meta (- x 128))
6550                                       x))
6551                         key)))
6552         (let ((cursor-in-echo-area t)
6553               gnus-pick-mode)
6554           (describe-key-briefly (read-key-sequence nil t) insert)))
6555     (describe-key-briefly key insert)))
6556
6557 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6558 (defvar gnus-agent-summary-mode)
6559 (defvar gnus-draft-mode)
6560 ;; Calling help-buffer will autoload help-mode.
6561 (defvar help-xref-stack-item)
6562 ;; Emacs 22 doesn't load it in the batch mode.
6563 (eval-when-compile
6564   (autoload 'help-buffer "help-mode"))
6565
6566 (defun gnus-article-describe-bindings (&optional prefix)
6567   "Show a list of all defined keys, and their definitions.
6568 The optional argument PREFIX, if non-nil, should be a key sequence;
6569 then we display only bindings that start with that prefix."
6570   (interactive)
6571   (gnus-article-check-buffer)
6572   (let ((keymap (copy-keymap gnus-article-mode-map))
6573         (map (copy-keymap gnus-article-send-map))
6574         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6575         parent agent draft)
6576     (define-key keymap "S" map)
6577     (define-key map [t] nil)
6578     (with-current-buffer gnus-article-current-summary
6579       (set-keymap-parent
6580        keymap
6581        (if (setq parent (keymap-parent gnus-article-mode-map))
6582            (prog1
6583                (setq parent (copy-keymap parent))
6584              (set-keymap-parent parent (current-local-map)))
6585          (current-local-map)))
6586       (set-keymap-parent map (key-binding "S"))
6587       (let (key def gnus-pick-mode)
6588         (while sumkeys
6589           (setq key (pop sumkeys))
6590           (cond ((and (vectorp key) (= (length key) 1)
6591                       (consp (setq def (aref key 0)))
6592                       (numberp (car def)) (numberp (cdr def)))
6593                  (when (< (max (car def) (cdr def)) 128)
6594                    (setq sumkeys
6595                          (append (mapcar
6596                                   #'vector
6597                                   (nreverse (gnus-uncompress-range def)))
6598                                  sumkeys))))
6599                 ((setq def (key-binding key))
6600                  (unless (eq def 'undefined)
6601                    (define-key keymap key def))))))
6602       (when (boundp 'gnus-agent-summary-mode)
6603         (setq agent gnus-agent-summary-mode))
6604       (when (boundp 'gnus-draft-mode)
6605         (setq draft gnus-draft-mode)))
6606     (with-temp-buffer
6607       (use-local-map keymap)
6608       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6609       (set (make-local-variable 'gnus-draft-mode) draft)
6610       (describe-bindings prefix))
6611     (let ((item `((lambda (prefix)
6612                     (with-current-buffer ,(current-buffer)
6613                       (gnus-article-describe-bindings prefix)))
6614                   ,prefix)))
6615       (with-current-buffer (let (help-xref-following) (help-buffer))
6616         (setq help-xref-stack-item item)))))
6617
6618 (defun gnus-article-reply-with-original (&optional wide)
6619   "Start composing a reply mail to the current message.
6620 The text in the region will be yanked.  If the region isn't active,
6621 the entire article will be yanked."
6622   (interactive)
6623   (let ((article (cdr gnus-article-current))
6624         contents)
6625     (if (not (gnus-region-active-p))
6626         (with-current-buffer gnus-summary-buffer
6627           (gnus-summary-reply (list (list article)) wide))
6628       (setq contents (buffer-substring (point) (mark t)))
6629       ;; Deactivate active regions.
6630       (when (and (boundp 'transient-mark-mode)
6631                  transient-mark-mode)
6632         (setq mark-active nil))
6633       (with-current-buffer gnus-summary-buffer
6634         (gnus-summary-reply
6635          (list (list article contents)) wide)))))
6636
6637 (defun gnus-article-wide-reply-with-original ()
6638   "Start composing a wide reply mail to the current message.
6639 The text in the region will be yanked.  If the region isn't active,
6640 the entire article will be yanked."
6641   (interactive)
6642   (gnus-article-reply-with-original t))
6643
6644 (defun gnus-article-followup-with-original ()
6645   "Compose a followup to the current article.
6646 The text in the region will be yanked.  If the region isn't active,
6647 the entire article will be yanked."
6648   (interactive)
6649   (let ((article (cdr gnus-article-current))
6650         contents)
6651       (if (not (gnus-region-active-p))
6652           (with-current-buffer gnus-summary-buffer
6653             (gnus-summary-followup (list (list article))))
6654         (setq contents (buffer-substring (point) (mark t)))
6655         ;; Deactivate active regions.
6656         (when (and (boundp 'transient-mark-mode)
6657                    transient-mark-mode)
6658           (setq mark-active nil))
6659         (with-current-buffer gnus-summary-buffer
6660           (gnus-summary-followup
6661            (list (list article contents)))))))
6662
6663 (defun gnus-article-hide (&optional arg force)
6664   "Hide all the gruft in the current article.
6665 This means that signatures, cited text and (some) headers will be
6666 hidden.
6667 If given a prefix, show the hidden text instead."
6668   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6669   (gnus-article-hide-headers arg)
6670   (gnus-article-hide-list-identifiers arg)
6671   (gnus-article-hide-citation-maybe arg force)
6672   (gnus-article-hide-signature arg))
6673
6674 (defun gnus-article-maybe-highlight ()
6675   "Do some article highlighting if article highlighting is requested."
6676   (when (gnus-visual-p 'article-highlight 'highlight)
6677     (gnus-article-highlight-some)))
6678
6679 (defun gnus-check-group-server ()
6680   ;; Make sure the connection to the server is alive.
6681   (unless (gnus-server-opened
6682            (gnus-find-method-for-group gnus-newsgroup-name))
6683     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6684     (gnus-request-group gnus-newsgroup-name t)))
6685
6686 (eval-when-compile
6687   (autoload 'nneething-get-file-name "nneething"))
6688
6689 (defun gnus-request-article-this-buffer (article group)
6690   "Get an article and insert it into this buffer."
6691   (let (do-update-line sparse-header)
6692     (prog1
6693         (save-excursion
6694           (erase-buffer)
6695           (gnus-kill-all-overlays)
6696           (setq group (or group gnus-newsgroup-name))
6697
6698           ;; Using `gnus-request-article' directly will insert the article into
6699           ;; `nntp-server-buffer' - so we'll save some time by not having to
6700           ;; copy it from the server buffer into the article buffer.
6701
6702           ;; We only request an article by message-id when we do not have the
6703           ;; headers for it, so we'll have to get those.
6704           (when (stringp article)
6705             (gnus-read-header article))
6706
6707           ;; If the article number is negative, that means that this article
6708           ;; doesn't belong in this newsgroup (possibly), so we find its
6709           ;; message-id and request it by id instead of number.
6710           (when (and (numberp article)
6711                      gnus-summary-buffer
6712                      (get-buffer gnus-summary-buffer)
6713                      (gnus-buffer-exists-p gnus-summary-buffer))
6714             (with-current-buffer gnus-summary-buffer
6715               (let ((header (gnus-summary-article-header article)))
6716                 (when (< article 0)
6717                   (cond
6718                    ((memq article gnus-newsgroup-sparse)
6719                     ;; This is a sparse gap article.
6720                     (setq do-update-line article)
6721                     (setq article (mail-header-id header))
6722                     (setq sparse-header (gnus-read-header article))
6723                     (setq gnus-newsgroup-sparse
6724                           (delq article gnus-newsgroup-sparse)))
6725                    ((vectorp header)
6726                     ;; It's a real article.
6727                     (setq article (mail-header-id header)))
6728                    (t
6729                     ;; It is an extracted pseudo-article.
6730                     (setq article 'pseudo)
6731                     (gnus-request-pseudo-article header))))
6732
6733                 (let ((method (gnus-find-method-for-group
6734                                gnus-newsgroup-name)))
6735                   (when (and (eq (car method) 'nneething)
6736                              (vectorp header))
6737                     (let ((dir (nneething-get-file-name
6738                                 (mail-header-id header))))
6739                       (when (and (stringp dir)
6740                                  (file-directory-p dir))
6741                         (setq article 'nneething)
6742                         (gnus-group-enter-directory dir))))))))
6743
6744           (cond
6745            ;; Refuse to select canceled articles.
6746            ((and (numberp article)
6747                  gnus-summary-buffer
6748                  (get-buffer gnus-summary-buffer)
6749                  (gnus-buffer-exists-p gnus-summary-buffer)
6750                  (eq (cdr (with-current-buffer gnus-summary-buffer
6751                             (assq article gnus-newsgroup-reads)))
6752                      gnus-canceled-mark))
6753             nil)
6754            ;; We first check `gnus-original-article-buffer'.
6755            ((and (get-buffer gnus-original-article-buffer)
6756                  (numberp article)
6757                  (with-current-buffer gnus-original-article-buffer
6758                    (and (equal (car gnus-original-article) group)
6759                         (eq (cdr gnus-original-article) article))))
6760             ;; `insert-buffer-substring' would incorrectly use the
6761             ;; equivalent of string-make-multibyte which amount to decoding
6762             ;; with locale-coding-system, causing failure of
6763             ;; subsequent decoding.
6764             (insert (mm-string-to-multibyte
6765                      (with-current-buffer gnus-original-article-buffer
6766                        (buffer-substring (point-min) (point-max)))))
6767             'article)
6768            ;; Check the backlog.
6769            ((and gnus-keep-backlog
6770                  (gnus-backlog-request-article group article (current-buffer)))
6771             'article)
6772            ;; Check asynchronous pre-fetch.
6773            ((gnus-async-request-fetched-article group article (current-buffer))
6774             (gnus-async-prefetch-next group article gnus-summary-buffer)
6775             (when (and (numberp article) gnus-keep-backlog)
6776               (gnus-backlog-enter-article group article (current-buffer)))
6777             'article)
6778            ;; Check the cache.
6779            ((and gnus-use-cache
6780                  (numberp article)
6781                  (gnus-cache-request-article article group))
6782             'article)
6783            ;; Check the agent cache.
6784            ((gnus-agent-request-article article group)
6785             'article)
6786            ;; Get the article and put into the article buffer.
6787            ((or (stringp article)
6788                 (numberp article))
6789             (let ((gnus-override-method gnus-override-method)
6790                   (methods (and (stringp article)
6791                                 gnus-refer-article-method))
6792                   (backend (car (gnus-find-method-for-group
6793                                  gnus-newsgroup-name)))
6794                   result
6795                   (inhibit-read-only t))
6796               (if (or (not (listp methods))
6797                       (and (symbolp (car methods))
6798                            (assq (car methods) nnoo-definition-alist)))
6799                   (setq methods (list methods)))
6800               (when (and (null gnus-override-method)
6801                          methods)
6802                 (setq gnus-override-method (pop methods)))
6803               (while (not result)
6804                 (when (eq gnus-override-method 'current)
6805                   (setq gnus-override-method
6806                         (with-current-buffer gnus-summary-buffer
6807                           gnus-current-select-method)))
6808                 (erase-buffer)
6809                 (gnus-kill-all-overlays)
6810                 (let ((gnus-newsgroup-name group))
6811                   (gnus-check-group-server))
6812                 (cond
6813                  ((gnus-request-article article group (current-buffer))
6814                   (when (numberp article)
6815                     (gnus-async-prefetch-next group article
6816                                               gnus-summary-buffer)
6817                     (when gnus-keep-backlog
6818                       (gnus-backlog-enter-article
6819                        group article (current-buffer))))
6820                   (setq result 'article))
6821                  (methods
6822                   (setq gnus-override-method (pop methods)))
6823                  ((not (string-match "^400 "
6824                                      (nnheader-get-report backend)))
6825                   ;; If we get 400 server disconnect, reconnect and
6826                   ;; retry; otherwise, assume the article has expired.
6827                   (setq result 'done))))
6828               (and (eq result 'article) 'article)))
6829            ;; It was a pseudo.
6830            (t article)))
6831
6832       ;; Associate this article with the current summary buffer.
6833       (setq gnus-article-current-summary gnus-summary-buffer)
6834
6835       ;; Take the article from the original article buffer
6836       ;; and place it in the buffer it's supposed to be in.
6837       (when (and (get-buffer gnus-article-buffer)
6838                  (equal (buffer-name (current-buffer))
6839                         (buffer-name (get-buffer gnus-article-buffer))))
6840         (save-excursion
6841           (if (get-buffer gnus-original-article-buffer)
6842               (set-buffer gnus-original-article-buffer)
6843             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6844             (buffer-disable-undo)
6845             (setq major-mode 'gnus-original-article-mode)
6846             (setq buffer-read-only t))
6847           (let ((inhibit-read-only t))
6848             (erase-buffer)
6849             (insert-buffer-substring gnus-article-buffer))
6850           (setq gnus-original-article (cons group article)))
6851
6852         ;; Decode charsets.
6853         (run-hooks 'gnus-article-decode-hook)
6854         ;; Mark article as decoded or not.
6855         (setq gnus-article-decoded-p gnus-article-decode-hook))
6856
6857       ;; Update sparse articles.
6858       (when (and do-update-line
6859                  (or (numberp article)
6860                      (stringp article)))
6861         (let ((buf (current-buffer)))
6862           (set-buffer gnus-summary-buffer)
6863           (gnus-summary-update-article do-update-line sparse-header)
6864           (gnus-summary-goto-subject do-update-line nil t)
6865           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6866                             (point))
6867           (set-buffer buf))))))
6868
6869 ;;;
6870 ;;; Article editing
6871 ;;;
6872
6873 (defcustom gnus-article-edit-mode-hook nil
6874   "Hook run in article edit mode buffers."
6875   :group 'gnus-article-various
6876   :type 'hook)
6877
6878 (defvar gnus-article-edit-done-function nil)
6879
6880 (defvar gnus-article-edit-mode-map nil)
6881
6882 ;; Should we be using derived.el for this?
6883 (unless gnus-article-edit-mode-map
6884   (setq gnus-article-edit-mode-map (make-keymap))
6885   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6886
6887   (gnus-define-keys gnus-article-edit-mode-map
6888     "\C-c?"    describe-mode
6889     "\C-c\C-c" gnus-article-edit-done
6890     "\C-c\C-k" gnus-article-edit-exit
6891     "\C-c\C-f\C-t" message-goto-to
6892     "\C-c\C-f\C-o" message-goto-from
6893     "\C-c\C-f\C-b" message-goto-bcc
6894     ;;"\C-c\C-f\C-w" message-goto-fcc
6895     "\C-c\C-f\C-c" message-goto-cc
6896     "\C-c\C-f\C-s" message-goto-subject
6897     "\C-c\C-f\C-r" message-goto-reply-to
6898     "\C-c\C-f\C-n" message-goto-newsgroups
6899     "\C-c\C-f\C-d" message-goto-distribution
6900     "\C-c\C-f\C-f" message-goto-followup-to
6901     "\C-c\C-f\C-m" message-goto-mail-followup-to
6902     "\C-c\C-f\C-k" message-goto-keywords
6903     "\C-c\C-f\C-u" message-goto-summary
6904     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6905     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6906     "\C-c\C-b" message-goto-body
6907     "\C-c\C-i" message-goto-signature
6908
6909     "\C-c\C-t" message-insert-to
6910     "\C-c\C-n" message-insert-newsgroups
6911     "\C-c\C-o" message-sort-headers
6912     "\C-c\C-e" message-elide-region
6913     "\C-c\C-v" message-delete-not-region
6914     "\C-c\C-z" message-kill-to-signature
6915     "\M-\r" message-newline-and-reformat
6916     "\C-c\C-a" mml-attach-file
6917     "\C-a" message-beginning-of-line
6918     "\t" message-tab
6919     "\M-;" comment-region)
6920
6921   (gnus-define-keys (gnus-article-edit-wash-map
6922                      "\C-c\C-w" gnus-article-edit-mode-map)
6923     "f" gnus-article-edit-full-stops))
6924
6925 (easy-menu-define
6926   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6927   '("Field"
6928     ["Fetch To" message-insert-to t]
6929     ["Fetch Newsgroups" message-insert-newsgroups t]
6930     "----"
6931     ["To" message-goto-to t]
6932     ["From" message-goto-from t]
6933     ["Subject" message-goto-subject t]
6934     ["Cc" message-goto-cc t]
6935     ["Reply-To" message-goto-reply-to t]
6936     ["Summary" message-goto-summary t]
6937     ["Keywords" message-goto-keywords t]
6938     ["Newsgroups" message-goto-newsgroups t]
6939     ["Followup-To" message-goto-followup-to t]
6940     ["Mail-Followup-To" message-goto-mail-followup-to t]
6941     ["Distribution" message-goto-distribution t]
6942     ["Body" message-goto-body t]
6943     ["Signature" message-goto-signature t]))
6944
6945 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6946   "Major mode for editing articles.
6947 This is an extended text-mode.
6948
6949 \\{gnus-article-edit-mode-map}"
6950   (make-local-variable 'gnus-article-edit-done-function)
6951   (make-local-variable 'gnus-prev-winconf)
6952   (set (make-local-variable 'font-lock-defaults)
6953        '(message-font-lock-keywords t))
6954   (set (make-local-variable 'mail-header-separator) "")
6955   (set (make-local-variable 'gnus-article-edit-mode) t)
6956   (easy-menu-add message-mode-field-menu message-mode-map)
6957   (mml-mode)
6958   (setq buffer-read-only nil)
6959   (buffer-enable-undo)
6960   (widen))
6961
6962 (defun gnus-article-edit (&optional force)
6963   "Edit the current article.
6964 This will have permanent effect only in mail groups.
6965 If FORCE is non-nil, allow editing of articles even in read-only
6966 groups."
6967   (interactive "P")
6968   (when (and (not force)
6969              (gnus-group-read-only-p))
6970     (error "The current newsgroup does not support article editing"))
6971   (gnus-article-date-original)
6972   (gnus-article-edit-article
6973    'ignore
6974    `(lambda (no-highlight)
6975       'ignore
6976       (gnus-summary-edit-article-done
6977        ,(or (mail-header-references gnus-current-headers) "")
6978        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6979
6980 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6981   "Start editing the contents of the current article buffer."
6982   (let ((winconf (current-window-configuration)))
6983     (set-buffer gnus-article-buffer)
6984     (let ((message-auto-save-directory
6985            ;; Don't associate the article buffer with a draft file.
6986            nil))
6987       (gnus-article-edit-mode))
6988     (funcall start-func)
6989     (set-buffer-modified-p nil)
6990     (gnus-configure-windows 'edit-article)
6991     (setq gnus-article-edit-done-function exit-func)
6992     (setq gnus-prev-winconf winconf)
6993     (unless quiet
6994       (gnus-message 6 "C-c C-c to end edits"))))
6995
6996 (defun gnus-article-edit-done (&optional arg)
6997   "Update the article edits and exit."
6998   (interactive "P")
6999   (let ((func gnus-article-edit-done-function)
7000         (buf (current-buffer))
7001         (start (window-start))
7002         (p (point))
7003         (winconf gnus-prev-winconf))
7004     (widen) ;; Widen it in case that users narrowed the buffer.
7005     (funcall func arg)
7006     (set-buffer buf)
7007     ;; The cache and backlog have to be flushed somewhat.
7008     (when gnus-keep-backlog
7009       (gnus-backlog-remove-article
7010        (car gnus-article-current) (cdr gnus-article-current)))
7011     ;; Flush original article as well.
7012     (when (get-buffer gnus-original-article-buffer)
7013       (with-current-buffer gnus-original-article-buffer
7014         (setq gnus-original-article nil)))
7015     (when gnus-use-cache
7016       (gnus-cache-update-article
7017        (car gnus-article-current) (cdr gnus-article-current)))
7018     ;; We remove all text props from the article buffer.
7019     (kill-all-local-variables)
7020     (set-text-properties (point-min) (point-max) nil)
7021     (gnus-article-mode)
7022     (set-window-configuration winconf)
7023     (set-buffer buf)
7024     (set-window-start (get-buffer-window buf) start)
7025     (set-window-point (get-buffer-window buf) (point)))
7026   (gnus-summary-show-article))
7027
7028 (defun gnus-article-edit-exit ()
7029   "Exit the article editing without updating."
7030   (interactive)
7031   (when (or (not (buffer-modified-p))
7032             (yes-or-no-p "Article modified; kill anyway? "))
7033     (let ((curbuf (current-buffer))
7034           (p (point))
7035           (window-start (window-start)))
7036       (erase-buffer)
7037       (if (gnus-buffer-live-p gnus-original-article-buffer)
7038           (insert-buffer-substring gnus-original-article-buffer))
7039       (let ((winconf gnus-prev-winconf))
7040         (kill-all-local-variables)
7041         (gnus-article-mode)
7042         (set-window-configuration winconf)
7043         ;; Tippy-toe some to make sure that point remains where it was.
7044         (save-current-buffer
7045           (set-buffer curbuf)
7046           (set-window-start (get-buffer-window (current-buffer)) window-start)
7047           (goto-char p))))
7048     (gnus-summary-show-article)))
7049
7050 (defun gnus-article-edit-full-stops ()
7051   "Interactively repair spacing at end of sentences."
7052   (interactive)
7053   (save-excursion
7054     (goto-char (point-min))
7055     (search-forward-regexp "^$" nil t)
7056     (let ((case-fold-search nil))
7057       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7058
7059 ;;;
7060 ;;; Article highlights
7061 ;;;
7062
7063 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7064
7065 ;;; Internal Variables:
7066
7067 (defcustom gnus-button-url-regexp
7068   (concat
7069    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7070    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7071    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7072    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7073        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7074              (punct "!?:;.,"))
7075          (concat
7076           "\\(?:"
7077           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7078           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7079           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7080           "\\|"
7081           "[" chars punct     "]+" "[" chars "]"
7082           "\\)"))
7083      (concat ;; XEmacs 21.4 doesn't support POSIX.
7084       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7085       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7086    "\\)")
7087   "Regular expression that matches URLs."
7088   :group 'gnus-article-buttons
7089   :type 'regexp)
7090
7091 (defcustom gnus-button-valid-fqdn-regexp
7092   message-valid-fqdn-regexp
7093   "Regular expression that matches a valid FQDN."
7094   :version "22.1"
7095   :group 'gnus-article-buttons
7096   :type 'regexp)
7097
7098 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7099 (defcustom gnus-button-valid-localpart-regexp
7100   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7101   "Regular expression that matches a localpart of mail addresses or MIDs."
7102   :version "22.1"
7103   :group 'gnus-article-buttons
7104   :type 'regexp)
7105
7106 (defcustom gnus-button-man-handler 'manual-entry
7107   "Function to use for displaying man pages.
7108 The function must take at least one argument with a string naming the
7109 man page."
7110   :version "22.1"
7111   :type '(choice (function-item :tag "Man" manual-entry)
7112                  (function-item :tag "Woman" woman)
7113                  (function :tag "Other"))
7114   :group 'gnus-article-buttons)
7115
7116 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
7117   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
7118 If the default site is too slow, try to find a CTAN mirror, see
7119 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
7120 the variable `gnus-button-handle-ctan'."
7121   :version "22.1"
7122   :group 'gnus-article-buttons
7123   :link '(custom-manual "(gnus)Group Parameters")
7124   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
7125                  (const "http://tug.ctan.org/tex-archive/")
7126                  (const "http://www.dante.de/CTAN/")
7127                  (string :tag "Other")))
7128
7129 (defcustom gnus-button-ctan-handler 'browse-url
7130   "Function to use for displaying CTAN links.
7131 The function must take one argument, the string naming the URL."
7132   :version "22.1"
7133   :type '(choice (function-item :tag "Browse Url" browse-url)
7134                  (function :tag "Other"))
7135   :group 'gnus-article-buttons)
7136
7137 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
7138   "Bogus strings removed from CTAN URLs."
7139   :version "22.1"
7140   :group 'gnus-article-buttons
7141   :type '(choice (const "^/?tex-archive/\\|/")
7142                  (regexp :tag "Other")))
7143
7144 (defcustom gnus-button-ctan-directory-regexp
7145   (regexp-opt
7146    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
7147          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
7148          "languages" "macros" "nonfree" "obsolete" "support" "systems"
7149          "tds" "tools" "usergrps" "web") t)
7150   "Regular expression for ctan directories.
7151 It should match all directories in the top level of `gnus-ctan-url'."
7152   :version "22.1"
7153   :group 'gnus-article-buttons
7154   :type 'regexp)
7155
7156 (defcustom gnus-button-mid-or-mail-regexp
7157   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7158           gnus-button-valid-fqdn-regexp
7159           ">?\\)\\b")
7160   "Regular expression that matches a message ID or a mail address."
7161   :version "22.1"
7162   :group 'gnus-article-buttons
7163   :type 'regexp)
7164
7165 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7166   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7167 Strings like this can be either a message ID or a mail address.  If it is one
7168 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7169 message ID or a mail address, respectively.  If this variable is set to the
7170 symbol `ask', always query the user what do do.  If it is a function, this
7171 function will be called with the string as its only argument.  The function
7172 must return `mid', `mail', `invalid' or `ask'."
7173   :version "22.1"
7174   :group 'gnus-article-buttons
7175   :type '(choice (function-item :tag "Heuristic function"
7176                                 gnus-button-mid-or-mail-heuristic)
7177                  (const ask)
7178                  (const mid)
7179                  (const mail)))
7180
7181 (defcustom gnus-button-mid-or-mail-heuristic-alist
7182   '((-10.0 . ".+\\$.+@")
7183     (-10.0 . "#")
7184     (-10.0 . "\\*")
7185     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7186     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7187     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7188     (-1.0  . "^[^a-z]+@")
7189     ;;
7190     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7191     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7192     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7193     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7194     ;;
7195     (-2.0  . "^[0-9]")
7196     (-1.0  . "^[0-9][0-9]")
7197     ;;
7198     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7199     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7200     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7201     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7202     ;;
7203     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7204     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7205     ;;       "[0-9]{8,}.*\@"
7206     (-3.0
7207      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7208     ;; "[0-9]{12,}.*\@"
7209     ;; compensation for TDMA dated mail addresses:
7210     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7211     ;;
7212     (-20.0 . "\\.fsf@") ;; Gnus
7213     (-20.0 . "^slrn")
7214     (-20.0 . "^Pine")
7215     (-20.0 . "^alpine\\.")
7216     (-20.0 . "_-_") ;; Subject change in thread
7217     ;;
7218     (-20.0 . "\\.ln@") ;; leafnode
7219     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7220     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7221     ;;
7222     ;; (5.0 . "") ;; $local_part_len <= 7
7223     (10.0  . "^[^0-9]+@")
7224     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7225     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7226     (3.0   . "\@stud")
7227     ;;
7228     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7229     ;;
7230     (0.5   . "^[A-Z][a-z]")
7231     (0.5   . "^[A-Z][a-z][a-z]")
7232     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7233     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7234   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7235
7236 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7237 address.  The REGEXP is processed with `case-fold-search' set to nil."
7238   :version "22.1"
7239   :group 'gnus-article-buttons
7240   :type '(repeat (cons (number :tag "Rate")
7241                        (regexp :tag "Regexp"))))
7242
7243 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7244   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7245 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7246 address, `ask' if unsure and `invalid' if the string is invalid."
7247   (let ((case-fold-search nil)
7248         (list gnus-button-mid-or-mail-heuristic-alist)
7249         (result 0) rate regexp lpartlen elem)
7250     (setq lpartlen
7251           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7252     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7253     ;; Certain special cases...
7254     (when (string-match
7255            (concat
7256             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7257             "^[0-9]+\\.[0-9]+@compuserve\\|"
7258             "@public\\.gmane\\.org")
7259            mid-or-mail)
7260       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7261       (setq result 'mail))
7262     (when (string-match "@.*@\\| " mid-or-mail)
7263       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7264       (setq result 'invalid))
7265     ;; Nothing more to do, if result is not a number here...
7266     (when (numberp result)
7267       (while list
7268         (setq elem (car list)
7269               rate (car elem)
7270               regexp (cdr elem)
7271               list (cdr list))
7272         (when (string-match regexp mid-or-mail)
7273           (setq result (+ result rate))
7274           (gnus-message
7275            9 "`%s' matched `%s', rate `%s', result `%s'."
7276            mid-or-mail regexp rate result)))
7277       (when (<= lpartlen 7)
7278         (setq result (+ result 5.0))
7279         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7280                       mid-or-mail result))
7281       (when (>= lpartlen 12)
7282         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7283         (cond
7284          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7285           ;; Long local part should contain realname if e-mail address,
7286           ;; too many digits: message-id.
7287           ;; $score -= 5.0 + 0.1 * $local_part_len;
7288           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7289           (setq result (+ result rate))
7290           (gnus-message
7291            9 "Many digits in `%s', rate `%s', result `%s'."
7292            mid-or-mail rate result))
7293          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7294                         mid-or-mail)
7295           ;; Too few vowels [^aeiouy]{4,}.*\@
7296           (setq result (+ result -5.0))
7297           (gnus-message
7298            9 "Few vowels in `%s', rate `%s', result `%s'."
7299            mid-or-mail -5.0 result))
7300          (t
7301           (setq result (+ result 5.0))
7302           (gnus-message
7303            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7304     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7305     ;; Maybe we should make this a customizable alist: (condition . 'result)
7306     (cond
7307      ((symbolp result) result)
7308      ;; Now convert number into proper results:
7309      ((< result -10.0) 'mid)
7310      ((> result  10.0) 'mail)
7311      (t 'ask))))
7312
7313 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7314   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7315          (url-mid (concat "news" ":" mid-or-mail))
7316          (url-mailto (concat "mailto" ":" mid-or-mail)))
7317     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7318     (when (fboundp pref)
7319       (setq guessed
7320             ;; get rid of surrounding angles...
7321             (funcall pref
7322                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7323       (if (or (eq 'mid guessed) (eq 'mail guessed))
7324           (setq pref guessed)
7325         (setq pref 'ask)))
7326     (if (eq pref 'ask)
7327         (save-window-excursion
7328           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7329               (setq pref 'mail)
7330             (setq pref 'mid))))
7331     (cond ((eq pref 'mid)
7332            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7333            (gnus-button-handle-news url-mid))
7334           ((eq pref 'mail)
7335            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7336            (gnus-url-mailto url-mailto))
7337           (t (gnus-message 3 "Invalid string.")))))
7338
7339 (defun gnus-button-handle-custom (fun arg)
7340   "Call function FUN on argument ARG.
7341 Both FUN and ARG are supposed to be strings.  ARG will be passed
7342 as a symbol to FUN."
7343   (funcall (intern fun)
7344            (if (string-match "^customize-apropos" fun)
7345                arg
7346              (intern arg))))
7347
7348 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7349
7350 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7351 ;; stuff?
7352
7353 (defun gnus-button-handle-describe-function (url)
7354   "Call `describe-function' when pushing the corresponding URL button."
7355   (describe-function
7356    (intern
7357     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7358
7359 (defun gnus-button-handle-describe-variable (url)
7360   "Call `describe-variable' when pushing the corresponding URL button."
7361   (describe-variable
7362    (intern
7363     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7364
7365 (defun gnus-button-handle-symbol (url)
7366 "Display help on variable or function.
7367 Calls `describe-variable' or `describe-function'."
7368   (let ((sym (intern url)))
7369     (cond
7370      ((fboundp sym) (describe-function sym))
7371      ((boundp sym) (describe-variable sym))
7372      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7373
7374 (defun gnus-button-handle-describe-key (url)
7375   "Call `describe-key' when pushing the corresponding URL button."
7376   (let* ((key-string
7377           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7378          (keys (ignore-errors (eval `(kbd ,key-string)))))
7379     (if keys
7380         (describe-key keys)
7381       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7382
7383 (defun gnus-button-handle-apropos (url)
7384   "Call `apropos' when pushing the corresponding URL button."
7385   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7386
7387 (defun gnus-button-handle-apropos-command (url)
7388   "Call `apropos' when pushing the corresponding URL button."
7389   (apropos-command
7390    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7391
7392 (defun gnus-button-handle-apropos-variable (url)
7393   "Call `apropos' when pushing the corresponding URL button."
7394   (funcall
7395    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7396    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7397
7398 (defun gnus-button-handle-apropos-documentation (url)
7399   "Call `apropos' when pushing the corresponding URL button."
7400   (funcall
7401    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7402    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7403
7404 (defun gnus-button-handle-library (url)
7405   "Call `locate-library' when pushing the corresponding URL button."
7406   (gnus-message 9 "url=`%s'" url)
7407   (let* ((lib (locate-library url))
7408          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7409     (if (not lib)
7410         (gnus-message 1 "Cannot locale library `%s'." url)
7411       (find-file-read-only file))))
7412
7413 (defun gnus-button-handle-ctan (url)
7414   "Call `browse-url' when pushing a CTAN URL button."
7415   (funcall
7416    gnus-button-ctan-handler
7417    (concat
7418     gnus-ctan-url
7419     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
7420
7421 (defcustom gnus-button-tex-level 5
7422   "*Integer that says how many TeX-related buttons Gnus will show.
7423 The higher the number, the more buttons will appear and the more false
7424 positives are possible.  Note that you can set this variable local to
7425 specific groups.  Setting it higher in TeX groups is probably a good idea.
7426 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7427 how to set variables in specific groups."
7428   :version "22.1"
7429   :group 'gnus-article-buttons
7430   :link '(custom-manual "(gnus)Group Parameters")
7431   :type 'integer)
7432
7433 (defcustom gnus-button-man-level 5
7434   "*Integer that says how many man-related buttons Gnus will show.
7435 The higher the number, the more buttons will appear and the more false
7436 positives are possible.  Note that you can set this variable local to
7437 specific groups.  Setting it higher in Unix groups is probably a good idea.
7438 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7439 how to set variables in specific groups."
7440   :version "22.1"
7441   :group 'gnus-article-buttons
7442   :link '(custom-manual "(gnus)Group Parameters")
7443   :type 'integer)
7444
7445 (defcustom gnus-button-emacs-level 5
7446   "*Integer that says how many emacs-related buttons Gnus will show.
7447 The higher the number, the more buttons will appear and the more false
7448 positives are possible.  Note that you can set this variable local to
7449 specific groups.  Setting it higher in Emacs or Gnus related groups is
7450 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7451 `gnus-parameters' on how to set variables in specific groups."
7452   :version "22.1"
7453   :group 'gnus-article-buttons
7454   :link '(custom-manual "(gnus)Group Parameters")
7455   :type 'integer)
7456
7457 (defcustom gnus-button-message-level 5
7458   "*Integer that says how many buttons for news or mail messages will appear.
7459 The higher the number, the more buttons will appear and the more false
7460 positives are possible."
7461   ;; mail addresses, MIDs, URLs for news, ...
7462   :version "22.1"
7463   :group 'gnus-article-buttons
7464   :type 'integer)
7465
7466 (defcustom gnus-button-browse-level 5
7467   "*Integer that says how many buttons for browsing will appear.
7468 The higher the number, the more buttons will appear and the more false
7469 positives are possible."
7470   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7471   :version "22.1"
7472   :group 'gnus-article-buttons
7473   :type 'integer)
7474
7475 (defcustom gnus-button-alist
7476   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7477      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7478     ((concat "\\b\\(nntp\\|news\\):\\("
7479              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7480      0 t gnus-button-handle-news 2)
7481     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7482      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7483     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7484      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7485     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7486     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7487      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7488     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7489      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7490     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7491      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7492     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7493      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7494     ;; RFC 2368 (The mailto URL scheme)
7495     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7496      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7497     ("\\bmailto:\\([^ \n\t]+\\)"
7498      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7499     ;; CTAN
7500     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
7501              gnus-button-ctan-directory-regexp
7502              "[^][>)!;:,'\n\t ]+\\)")
7503      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
7504     ((concat "\\btex-archive/\\("
7505              gnus-button-ctan-directory-regexp
7506              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
7507      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
7508     ((concat
7509       "\\b\\("
7510       gnus-button-ctan-directory-regexp
7511       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
7512      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
7513     ;; Info Konqueror style <info:/foo/bar baz>.
7514     ;; Must come before " Gnus home-grown style".
7515     ("\\binfo://?\\([^'\">\n\t]+\\)"
7516      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7517    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7518     ("\\binfo://\\([^'\">\n\t ]+\\)"
7519      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7520     ;; Info GNOME style <info:foo#bar_baz>
7521     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7522      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7523     ;; Info KDE style <info:(foo)bar baz>
7524     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7525      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7526     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7527      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7528     ("\\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,]*\\)\\)?"
7529      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7530      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7531     ;; This is custom
7532     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7533      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7534     ;; Emacs help commands
7535     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7536      ;; regexp doesn't match arguments containing ` '.
7537      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7538     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7539      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7540     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7541      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7542     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7543      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7544     ;; The following entries may lead to many false positives so don't enable
7545     ;; them by default (use a high button level).
7546     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7547      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7548      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7549     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7550      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7551     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7552      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7553     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7554      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7555     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7556      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7557     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7558      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7559     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7560      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7561     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7562      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7563     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7564      ;; Unlike the other regexps we really have to require quoting
7565      ;; here to determine where it ends.
7566      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7567     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7568     ("<URL: *\\([^\n<>]*\\)>"
7569      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7570     ;; RFC 2396 (2.4.3., delims) ...
7571     ("\"URL: *\\([^\n\"]*\\)\""
7572      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7573     ;; Raw URLs.
7574     (gnus-button-url-regexp
7575      0 (>= gnus-button-browse-level 0) browse-url 0)
7576     ;; man pages
7577     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7578      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7579      gnus-button-handle-man 1)
7580     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7581     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7582      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7583      gnus-button-handle-man 1)
7584     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7585     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7586     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7587      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7588     ;; Recognizing patches to .el files.  This is somewhat obscure,
7589     ;; but considering the percentage of Gnus users who hack Emacs
7590     ;; Lisp files...
7591     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7592      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7593     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7594      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7595     ;; MID or mail: To avoid too many false positives we don't try to catch
7596     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7597     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7598     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7599     ;; so that non-ambiguous entries (see above) match first.
7600     (gnus-button-mid-or-mail-regexp
7601      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7602   "*Alist of regexps matching buttons in article bodies.
7603
7604 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7605 REGEXP: is the string (case insensitive) matching text around the button (can
7606 also be Lisp expression evaluating to a string),
7607 BUTTON: is the number of the regexp grouping actually matching the button,
7608 FORM: is a Lisp expression which must eval to true for the button to
7609 be added,
7610 CALLBACK: is the function to call when the user push this button, and each
7611 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7612
7613 CALLBACK can also be a variable, in that case the value of that
7614 variable it the real callback function."
7615   :group 'gnus-article-buttons
7616   :type '(repeat (list (choice regexp variable sexp)
7617                        (integer :tag "Button")
7618                        (sexp :tag "Form")
7619                        (function :tag "Callback")
7620                        (repeat :tag "Par"
7621                                :inline t
7622                                (integer :tag "Regexp group")))))
7623 (put 'gnus-button-alist 'risky-local-variable t)
7624
7625 (defcustom gnus-header-button-alist
7626   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7627      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7628     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7629      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7630     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7631      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7632     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7633      0 (>= gnus-button-browse-level 0) browse-url 0)
7634     ("^Subject:" gnus-button-url-regexp
7635      0 (>= gnus-button-browse-level 0) browse-url 0)
7636     ("^[^:]+:" gnus-button-url-regexp
7637      0 (>= gnus-button-browse-level 0) browse-url 0)
7638     ("^OpenPGP:.*url=" gnus-button-url-regexp
7639      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7640     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7641      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7642     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7643      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7644   "*Alist of headers and regexps to match buttons in article heads.
7645
7646 This alist is very similar to `gnus-button-alist', except that each
7647 alist has an additional HEADER element first in each entry:
7648
7649 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7650
7651 HEADER is a regexp to match a header.  For a fuller explanation, see
7652 `gnus-button-alist'."
7653   :group 'gnus-article-buttons
7654   :group 'gnus-article-headers
7655   :type '(repeat (list (regexp :tag "Header")
7656                        (choice regexp variable)
7657                        (integer :tag "Button")
7658                        (sexp :tag "Form")
7659                        (function :tag "Callback")
7660                        (repeat :tag "Par"
7661                                :inline t
7662                                (integer :tag "Regexp group")))))
7663 (put 'gnus-header-button-alist 'risky-local-variable t)
7664
7665 ;;; Commands:
7666
7667 (defun gnus-article-push-button (event)
7668   "Check text under the mouse pointer for a callback function.
7669 If the text under the mouse pointer has a `gnus-callback' property,
7670 call it with the value of the `gnus-data' text property."
7671   (interactive "e")
7672   (set-buffer (window-buffer (posn-window (event-start event))))
7673   (let* ((pos (posn-point (event-start event)))
7674          (data (get-text-property pos 'gnus-data))
7675          (fun (get-text-property pos 'gnus-callback)))
7676     (goto-char pos)
7677     (when fun
7678       (funcall fun data))))
7679
7680 (defun gnus-article-press-button ()
7681   "Check text at point for a callback function.
7682 If the text at point has a `gnus-callback' property,
7683 call it with the value of the `gnus-data' text property."
7684   (interactive)
7685   (let ((data (get-text-property (point) 'gnus-data))
7686         (fun (get-text-property (point) 'gnus-callback)))
7687     (when fun
7688       (funcall fun data))))
7689
7690 (defun gnus-article-highlight (&optional force)
7691   "Highlight current article.
7692 This function calls `gnus-article-highlight-headers',
7693 `gnus-article-highlight-citation',
7694 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7695 do the highlighting.  See the documentation for those functions."
7696   (interactive (list 'force))
7697   (gnus-article-highlight-headers)
7698   (gnus-article-highlight-citation force)
7699   (gnus-article-highlight-signature)
7700   (gnus-article-add-buttons force)
7701   (gnus-article-add-buttons-to-head))
7702
7703 (defun gnus-article-highlight-some (&optional force)
7704   "Highlight current article.
7705 This function calls `gnus-article-highlight-headers',
7706 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7707 do the highlighting.  See the documentation for those functions."
7708   (interactive (list 'force))
7709   (gnus-article-highlight-headers)
7710   (gnus-article-highlight-signature)
7711   (gnus-article-add-buttons))
7712
7713 (defun gnus-article-highlight-headers ()
7714   "Highlight article headers as specified by `gnus-header-face-alist'."
7715   (interactive)
7716   (gnus-with-article-headers
7717     (let (regexp header-face field-face from hpoints fpoints)
7718       (dolist (entry gnus-header-face-alist)
7719         (goto-char (point-min))
7720         (setq regexp (concat "^\\("
7721                              (if (string-equal "" (nth 0 entry))
7722                                  "[^\t ]"
7723                                (nth 0 entry))
7724                              "\\)")
7725               header-face (nth 1 entry)
7726               field-face (nth 2 entry))
7727         (while (and (re-search-forward regexp nil t)
7728                     (not (eobp)))
7729           (beginning-of-line)
7730           (setq from (point))
7731           (unless (search-forward ":" nil t)
7732             (forward-char 1))
7733           (when (and header-face
7734                      (not (memq (point) hpoints)))
7735             (push (point) hpoints)
7736             (gnus-put-text-property from (point) 'face header-face))
7737           (when (and field-face
7738                      (not (memq (setq from (point)) fpoints)))
7739             (push from fpoints)
7740             (if (re-search-forward "^[^ \t]" nil t)
7741                 (forward-char -2)
7742               (goto-char (point-max)))
7743             (gnus-put-text-property from (point) 'face field-face)))))))
7744
7745 (defun gnus-article-highlight-signature ()
7746   "Highlight the signature in an article.
7747 It does this by highlighting everything after
7748 `gnus-signature-separator' using the face `gnus-signature'."
7749   (interactive)
7750   (gnus-with-article-buffer
7751     (let ((inhibit-point-motion-hooks t))
7752       (save-restriction
7753         (when (and gnus-signature-face
7754                    (gnus-article-narrow-to-signature))
7755           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7756                             'face gnus-signature-face)
7757           (widen)
7758           (gnus-article-search-signature)
7759           (let ((start (match-beginning 0))
7760                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7761             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7762                                      end)))))))
7763
7764 (defun gnus-button-in-region-p (b e prop)
7765   "Say whether PROP exists in the region."
7766   (text-property-not-all b e prop nil))
7767
7768 (defun gnus-article-add-buttons (&optional force)
7769   "Find external references in the article and make buttons of them.
7770 \"External references\" are things like Message-IDs and URLs, as
7771 specified by `gnus-button-alist'."
7772   (interactive (list 'force))
7773   (gnus-with-article-buffer
7774     (let ((inhibit-point-motion-hooks t)
7775           (case-fold-search t)
7776           (alist gnus-button-alist)
7777           beg entry regexp)
7778       ;; Remove all old markers.
7779       (let (marker entry new-list)
7780         (while (setq marker (pop gnus-button-marker-list))
7781           (if (or (< marker (point-min)) (>= marker (point-max)))
7782               (push marker new-list)
7783             (goto-char marker)
7784             (when (setq entry (gnus-button-entry))
7785               (put-text-property (match-beginning (nth 1 entry))
7786                                  (match-end (nth 1 entry))
7787                                  'gnus-callback nil))
7788             (set-marker marker nil)))
7789         (setq gnus-button-marker-list new-list))
7790       ;; We skip the headers.
7791       (article-goto-body)
7792       (setq beg (point))
7793       (while (setq entry (pop alist))
7794         (setq regexp (eval (car entry)))
7795         (goto-char beg)
7796         (while (re-search-forward regexp nil t)
7797           (let ((start (match-beginning (nth 1 entry)))
7798                 (end (match-end (nth 1 entry)))
7799                 (from (match-beginning 0)))
7800             (when (and (or (eq t (nth 2 entry))
7801                            (eval (nth 2 entry)))
7802                        (not (gnus-button-in-region-p
7803                              start end 'gnus-callback)))
7804               ;; That optional form returned non-nil, so we add the
7805               ;; button.
7806               (setq from (set-marker (make-marker) from))
7807               (push from gnus-button-marker-list)
7808               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7809                            (gnus-article-extend-url-button from start end))
7810                 (gnus-article-add-button start end
7811                                          'gnus-button-push from)
7812                 (gnus-put-text-property
7813                  start end
7814                  'gnus-string (buffer-substring-no-properties
7815                                start end))))))))))
7816
7817 (defun gnus-article-extend-url-button (beg start end)
7818   "Extend url button if url is folded into two or more lines.
7819 Return non-nil if button is extended.  BEG is a marker that points to
7820 the beginning position of a text containing url.  START and END are
7821 the endpoints of a url button before it is extended.  The concatenated
7822 url is put as the `gnus-button-url' overlay property on the button."
7823   (let ((opoint (point))
7824         (points (list start end))
7825         url delim regexp)
7826     (prog1
7827         (when (and (progn
7828                      (goto-char end)
7829                      (not (looking-at "[\t ]*[\">]")))
7830                    (progn
7831                      (goto-char start)
7832                      (string-match
7833                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7834                       (buffer-substring (point-at-bol) start)))
7835                    (progn
7836                      (setq url (list (buffer-substring start end))
7837                            delim (if (match-beginning 1) ">" "\""))
7838                      (beginning-of-line)
7839                      (setq regexp (concat
7840                                    (when (and (looking-at
7841                                                message-cite-prefix-regexp)
7842                                               (< (match-end 0) start))
7843                                      (regexp-quote (match-string 0)))
7844                                    "\
7845 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7846                                    delim "\\)"))
7847                      (while (progn
7848                               (forward-line 1)
7849                               (and (looking-at regexp)
7850                                    (prog1
7851                                        (match-beginning 1)
7852                                      (push (or (match-string 2)
7853                                                (match-string 1))
7854                                            url)
7855                                      (push (setq end (or (match-end 2)
7856                                                          (match-end 1)))
7857                                            points)
7858                                      (push (or (match-beginning 2)
7859                                                (match-beginning 1))
7860                                            points)))))
7861                      (match-beginning 2)))
7862           (let (gnus-article-mouse-face widget-mouse-face)
7863             (while points
7864               (gnus-article-add-button (pop points) (pop points)
7865                                        'gnus-button-push beg)))
7866           (let ((overlay (gnus-make-overlay start end)))
7867             (gnus-overlay-put overlay 'evaporate t)
7868             (gnus-overlay-put overlay 'gnus-button-url
7869                               (list (mapconcat 'identity (nreverse url) "")))
7870             (when gnus-article-mouse-face
7871               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7872           t)
7873       (goto-char opoint))))
7874
7875 ;; Add buttons to the head of an article.
7876 (defun gnus-article-add-buttons-to-head ()
7877   "Add buttons to the head of the article."
7878   (interactive)
7879   (gnus-with-article-headers
7880     (let (beg end)
7881       (dolist (entry gnus-header-button-alist)
7882         ;; Each alist entry.
7883         (goto-char (point-min))
7884         (while (re-search-forward (car entry) nil t)
7885           ;; Each header matching the entry.
7886           (setq beg (match-beginning 0))
7887           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7888                              (match-beginning 0))
7889                         (point-max)))
7890           (goto-char beg)
7891           (while (re-search-forward (eval (nth 1 entry)) end t)
7892             ;; Each match within a header.
7893             (let* ((entry (cdr entry))
7894                    (start (match-beginning (nth 1 entry)))
7895                    (end (match-end (nth 1 entry)))
7896                    (form (nth 2 entry)))
7897               (goto-char (match-end 0))
7898               (when (eval form)
7899                 (gnus-article-add-button
7900                  start end (nth 3 entry)
7901                  (buffer-substring (match-beginning (nth 4 entry))
7902                                    (match-end (nth 4 entry)))))))
7903           (goto-char end))))))
7904
7905 ;;; External functions:
7906
7907 (defun gnus-article-add-button (from to fun &optional data text)
7908   "Create a button between FROM and TO with callback FUN and data DATA."
7909   (when gnus-article-button-face
7910     (gnus-overlay-put (gnus-make-overlay from to nil t)
7911                       'face gnus-article-button-face))
7912   (gnus-add-text-properties
7913    from to
7914    (nconc (and gnus-article-mouse-face
7915                (list gnus-mouse-face-prop gnus-article-mouse-face))
7916           (list 'gnus-callback fun)
7917           (and data (list 'gnus-data data))))
7918   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7919                          :help-echo (or text "Follow the link")
7920                          :keymap gnus-url-button-map
7921                          :button-keymap gnus-widget-button-keymap))
7922
7923 (defun gnus-article-copy-string ()
7924   "Copy the string in the button to the kill ring."
7925   (interactive)
7926   (gnus-article-check-buffer)
7927   (let ((data (get-text-property (point) 'gnus-string)))
7928     (when data
7929       (with-temp-buffer
7930         (insert data)
7931         (copy-region-as-kill (point-min) (point-max))
7932         (message "Copied %s" data)))))
7933
7934 ;;; Internal functions:
7935
7936 (defun gnus-article-set-globals ()
7937   (with-current-buffer gnus-summary-buffer
7938     (gnus-set-global-variables)))
7939
7940 (defun gnus-signature-toggle (end)
7941   (gnus-with-article-buffer
7942     (let ((inhibit-point-motion-hooks t))
7943       (if (text-property-any end (point-max) 'article-type 'signature)
7944           (progn
7945             (gnus-delete-wash-type 'signature)
7946             (gnus-remove-text-properties-when
7947              'article-type 'signature end (point-max)
7948              (cons 'article-type (cons 'signature
7949                                        gnus-hidden-properties))))
7950         (gnus-add-wash-type 'signature)
7951         (gnus-add-text-properties-when
7952          'article-type nil end (point-max)
7953          (cons 'article-type (cons 'signature
7954                                    gnus-hidden-properties)))))
7955     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7956       (gnus-set-mode-line 'article))))
7957
7958 (defun gnus-button-entry ()
7959   ;; Return the first entry in `gnus-button-alist' matching this place.
7960   (let ((alist gnus-button-alist)
7961         (entry nil))
7962     (while alist
7963       (setq entry (pop alist))
7964       (if (looking-at (eval (car entry)))
7965           (setq alist nil)
7966         (setq entry nil)))
7967     entry))
7968
7969 (defun gnus-button-push (marker)
7970   ;; Push button starting at MARKER.
7971   (save-excursion
7972     (goto-char marker)
7973     (let* ((entry (gnus-button-entry))
7974            (inhibit-point-motion-hooks t)
7975            (fun (nth 3 entry))
7976            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7977                           (get-char-property marker 'gnus-button-url))
7978                      (mapcar (lambda (group)
7979                                (let ((string (match-string group)))
7980                                  (set-text-properties
7981                                   0 (length string) nil string)
7982                                  string))
7983                              (nthcdr 4 entry)))))
7984       (cond
7985        ((fboundp fun)
7986         (apply fun args))
7987        ((and (boundp fun)
7988              (fboundp (symbol-value fun)))
7989         (apply (symbol-value fun) args))
7990        (t
7991         (gnus-message 1 "You must define `%S' to use this button"
7992                       (cons fun args)))))))
7993
7994 (defun gnus-parse-news-url (url)
7995   (let (scheme server port group message-id articles)
7996     (with-temp-buffer
7997       (insert url)
7998       (goto-char (point-min))
7999       (when (looking-at "\\([A-Za-z]+\\):")
8000         (setq scheme (match-string 1))
8001         (goto-char (match-end 0)))
8002       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8003         (setq server (match-string 1))
8004         (setq port (if (stringp (match-string 3))
8005                        (string-to-number (match-string 3))
8006                      (match-string 3)))
8007         (goto-char (match-end 0)))
8008
8009       (cond
8010        ((looking-at "\\(.*@.*\\)")
8011         (setq message-id (match-string 1)))
8012        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8013         (setq group (match-string 1)
8014               articles (split-string (match-string 2) "-")))
8015        ((looking-at "\\([^/]+\\)/?")
8016         (setq group (match-string 1)))
8017        (t
8018         (error "Unknown news URL syntax"))))
8019     (list scheme server port group message-id articles)))
8020
8021 (defun gnus-button-handle-news (url)
8022   "Fetch a news URL."
8023   (destructuring-bind (scheme server port group message-id articles)
8024       (gnus-parse-news-url url)
8025     (cond
8026      (message-id
8027       (with-current-buffer gnus-summary-buffer
8028         (if server
8029             (let ((gnus-refer-article-method
8030                    (nconc (list (list 'nntp server))
8031                           gnus-refer-article-method))
8032                   (nntp-port-number (or port "nntp")))
8033               (gnus-message 7 "Fetching %s with %s"
8034                             message-id gnus-refer-article-method)
8035               (gnus-summary-refer-article message-id))
8036           (gnus-summary-refer-article message-id))))
8037      (group
8038       (gnus-button-fetch-group url)))))
8039
8040 (defun gnus-button-patch (library line)
8041   "Visit an Emacs Lisp library LIBRARY on line LINE."
8042   (interactive)
8043   (let ((file (locate-library (file-name-nondirectory library))))
8044     (unless file
8045       (error "Couldn't find library %s" library))
8046     (find-file file)
8047     (goto-char (point-min))
8048     (forward-line (1- (string-to-number line)))))
8049
8050 (defun gnus-button-handle-man (url)
8051   "Fetch a man page."
8052   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8053   (when (eq gnus-button-man-handler 'woman)
8054     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8055   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8056   (funcall gnus-button-man-handler url))
8057
8058 (defun gnus-button-handle-info-url (url)
8059   "Fetch an info URL."
8060   (setq url (mm-subst-char-in-string ?+ ?\  url))
8061   (cond
8062    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8063     (gnus-info-find-node
8064      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8065                      "Gnus")
8066              ")" (gnus-url-unhex-string (match-string 2 url)))))
8067    ((string-match "([^)\"]+)[^\"]+" url)
8068     (setq url
8069           (gnus-replace-in-string
8070            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8071     (gnus-info-find-node url))
8072    (t (error "Can't parse %s" url))))
8073
8074 (defun gnus-button-handle-info-url-gnome (url)
8075   "Fetch GNOME style info URL."
8076   (setq url (mm-subst-char-in-string ?_ ?\  url))
8077   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8078       (gnus-info-find-node
8079        (concat "("
8080                (gnus-url-unhex-string
8081                  (match-string 1 url))
8082                ")"
8083                (or (gnus-url-unhex-string
8084                     (match-string 2 url))
8085                    "Top")))
8086     (error "Can't parse %s" url)))
8087
8088 (defun gnus-button-handle-info-url-kde (url)
8089   "Fetch KDE style info URL."
8090   (gnus-info-find-node (gnus-url-unhex-string url)))
8091
8092 ;; (info) will autoload info.el
8093 (declare-function Info-menu "info" (menu-item &optional fork))
8094 (declare-function Info-index-next "info" (num))
8095
8096 (defun gnus-button-handle-info-keystrokes (url)
8097   "Call `info' when pushing the corresponding URL button."
8098   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8099   (let (node indx comma)
8100     (if (string-match
8101          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8102                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8103                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
8104                  "\\(?:[ \t\n,]*\\)\\)?")
8105          url)
8106         (setq node (match-string 2 url)
8107               indx (match-string 3 url))
8108       (error "Can't parse %s" url))
8109     (info)
8110     (Info-directory)
8111     (Info-menu node)
8112     (when (> (length indx) 0)
8113       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8114                             "\\([ \t\n,]*\\)")
8115                     indx)
8116       (setq comma (match-string 2 indx))
8117       (setq indx  (match-string 1 indx))
8118       (Info-index indx)
8119       (when comma
8120         (dotimes (i (with-temp-buffer
8121                       (insert comma)
8122                       ;; Note: the XEmacs version of `how-many' takes
8123                       ;; no optional argument.
8124                       (goto-char (point-min))
8125                       (how-many ",")))
8126           (Info-index-next 1)))
8127       nil)))
8128
8129 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8130 (declare-function pgg-display-output-buffer "pgg" (start end status))
8131
8132 (defun gnus-button-openpgp (url)
8133   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8134   (with-temp-buffer
8135     (mm-url-insert-file-contents-external url)
8136     (pgg-snarf-keys-region (point-min) (point-max))
8137     (pgg-display-output-buffer nil nil nil)))
8138
8139 (defun gnus-button-message-id (message-id)
8140   "Fetch MESSAGE-ID."
8141   (with-current-buffer gnus-summary-buffer
8142     (gnus-summary-refer-article message-id)))
8143
8144 (defun gnus-button-fetch-group (address &rest ignore)
8145   "Fetch GROUP specified by ADDRESS."
8146   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8147                       address)
8148     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8149     ;; for nntp:// and news://
8150     (setq address (match-string 3 address)))
8151   (if (not (string-match "[:/]" address))
8152       ;; This is just a simple group url.
8153       (gnus-group-read-ephemeral-group address gnus-select-method)
8154     (if (not
8155          (string-match
8156           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8157           address))
8158         (error "Can't parse %s" address)
8159       (gnus-group-read-ephemeral-group
8160        (match-string 4 address)
8161        `(nntp ,(match-string 1 address)
8162               (nntp-address ,(match-string 1 address))
8163               (nntp-port-number ,(if (match-end 3)
8164                                      (match-string 3 address)
8165                                    "nntp")))
8166        nil nil nil
8167        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8168
8169 (defun gnus-url-parse-query-string (query &optional downcase)
8170   (let (retval pairs cur key val)
8171     (setq pairs (split-string query "&"))
8172     (while pairs
8173       (setq cur (car pairs)
8174             pairs (cdr pairs))
8175       (if (not (string-match "=" cur))
8176           nil                           ; Grace
8177         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8178               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8179         (if downcase
8180             (setq key (downcase key)))
8181         (setq cur (assoc key retval))
8182         (if cur
8183             (setcdr cur (cons val (cdr cur)))
8184           (setq retval (cons (list key val) retval)))))
8185     retval))
8186
8187 (defun gnus-url-mailto (url)
8188   ;; Send mail to someone
8189   (when (string-match "mailto:/*\\(.*\\)" url)
8190     (setq url (substring url (match-beginning 1) nil)))
8191   (let (to args subject func)
8192     (setq args (gnus-url-parse-query-string
8193                 (if (string-match "^\\?" url)
8194                     (substring url 1)
8195                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8196                       (concat "to=" (match-string 1 url) "&"
8197                               (match-string 2 url))
8198                     (concat "to=" url)))
8199                 t)
8200           subject (cdr-safe (assoc "subject" args)))
8201     (gnus-msg-mail)
8202     (while args
8203       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8204       (if (fboundp func)
8205           (funcall func)
8206         (message-position-on-field (caar args)))
8207       (insert (gnus-replace-in-string
8208                (mapconcat 'identity (reverse (cdar args)) ", ")
8209                "\r\n" "\n" t))
8210       (setq args (cdr args)))
8211     (if subject
8212         (message-goto-body)
8213       (message-goto-subject))))
8214
8215 (defun gnus-button-embedded-url (address)
8216   "Activate ADDRESS with `browse-url'."
8217   (browse-url (gnus-strip-whitespace address)))
8218
8219 ;;; Next/prev buttons in the article buffer.
8220
8221 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8222 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8223
8224 (defvar gnus-prev-page-map
8225   (let ((map (make-sparse-keymap)))
8226     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8227     (define-key map "\r" 'gnus-button-prev-page)
8228     map))
8229
8230 (defvar gnus-next-page-map
8231   (let ((map (make-sparse-keymap)))
8232     (unless (>= emacs-major-version 21)
8233       ;; XEmacs doesn't care.
8234       (set-keymap-parent map gnus-article-mode-map))
8235     (define-key map gnus-mouse-2 'gnus-button-next-page)
8236     (define-key map "\r" 'gnus-button-next-page)
8237     map))
8238
8239 (defun gnus-insert-prev-page-button ()
8240   (let ((b (point)) e
8241         (inhibit-read-only t))
8242     (gnus-eval-format
8243      gnus-prev-page-line-format nil
8244      `(keymap ,gnus-prev-page-map
8245               gnus-prev t
8246               gnus-callback gnus-article-button-prev-page
8247               article-type annotation))
8248     (setq e (if (bolp)
8249                 ;; Exclude a newline.
8250                 (1- (point))
8251               (point)))
8252     (when gnus-article-button-face
8253       (gnus-overlay-put (gnus-make-overlay b e nil t)
8254                         'face gnus-article-button-face))
8255     (widget-convert-button
8256      'link b e
8257      :action 'gnus-button-prev-page
8258      :button-keymap gnus-prev-page-map)))
8259
8260 (defun gnus-button-next-page (&optional args more-args)
8261   "Go to the next page."
8262   (interactive)
8263   (let ((win (selected-window)))
8264     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8265     (gnus-article-next-page)
8266     (select-window win)))
8267
8268 (defun gnus-button-prev-page (&optional args more-args)
8269   "Go to the prev page."
8270   (interactive)
8271   (let ((win (selected-window)))
8272     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8273     (gnus-article-prev-page)
8274     (select-window win)))
8275
8276 (defun gnus-insert-next-page-button ()
8277   (let ((b (point)) e
8278         (inhibit-read-only t))
8279     (gnus-eval-format gnus-next-page-line-format nil
8280                       `(keymap ,gnus-next-page-map
8281                                gnus-next t
8282                                gnus-callback gnus-article-button-next-page
8283                                article-type annotation))
8284     (setq e (if (bolp)
8285                 ;; Exclude a newline.
8286                 (1- (point))
8287               (point)))
8288     (when gnus-article-button-face
8289       (gnus-overlay-put (gnus-make-overlay b e nil t)
8290                         'face gnus-article-button-face))
8291     (widget-convert-button
8292      'link b e
8293      :action 'gnus-button-next-page
8294      :button-keymap gnus-next-page-map)))
8295
8296 (defun gnus-article-button-next-page (arg)
8297   "Go to the next page."
8298   (interactive "P")
8299   (let ((win (selected-window)))
8300     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8301     (gnus-article-next-page)
8302     (select-window win)))
8303
8304 (defun gnus-article-button-prev-page (arg)
8305   "Go to the prev page."
8306   (interactive "P")
8307   (let ((win (selected-window)))
8308     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8309     (gnus-article-prev-page)
8310     (select-window win)))
8311
8312 (defvar gnus-decode-header-methods
8313   '(mail-decode-encoded-word-region)
8314   "List of methods used to decode headers.
8315
8316 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8317 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8318 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8319 whose names match REGEXP.
8320
8321 For example:
8322 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8323  mail-decode-encoded-word-region
8324  (\"chinese\" . rfc1843-decode-region))
8325 ")
8326
8327 (defvar gnus-decode-header-methods-cache nil)
8328
8329 (defun gnus-multi-decode-header (start end)
8330   "Apply the functions from `gnus-encoded-word-methods' that match."
8331   (unless (and gnus-decode-header-methods-cache
8332                (eq gnus-newsgroup-name
8333                    (car gnus-decode-header-methods-cache)))
8334     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8335     (dolist (x gnus-decode-header-methods)
8336       (if (symbolp x)
8337           (nconc gnus-decode-header-methods-cache (list x))
8338         (if (and gnus-newsgroup-name
8339                  (string-match (car x) gnus-newsgroup-name))
8340             (nconc gnus-decode-header-methods-cache
8341                    (list (cdr x)))))))
8342   (let ((xlist gnus-decode-header-methods-cache))
8343     (pop xlist)
8344     (save-restriction
8345       (narrow-to-region start end)
8346       (while xlist
8347         (funcall (pop xlist) (point-min) (point-max))))))
8348
8349 ;;;
8350 ;;; Treatment top-level handling.
8351 ;;;
8352
8353 (defun gnus-treat-article (condition &optional part-number total-parts type)
8354   (let ((length (- (point-max) (point-min)))
8355         (alist gnus-treatment-function-alist)
8356         (article-goto-body-goes-to-point-min-p t)
8357         (treated-type
8358          (or (not type)
8359              (catch 'found
8360                (let ((list gnus-article-treat-types))
8361                  (while list
8362                    (when (string-match (pop list) type)
8363                      (throw 'found t)))))))
8364         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8365         val elem)
8366     (gnus-run-hooks 'gnus-part-display-hook)
8367     (dolist (elem alist)
8368       (setq val
8369             (save-excursion
8370               (when (gnus-buffer-live-p gnus-summary-buffer)
8371                 (set-buffer gnus-summary-buffer))
8372               (symbol-value (car elem))))
8373       (when (and (or (consp val)
8374                      treated-type)
8375                  (gnus-treat-predicate val)
8376                  (or (not (get (car elem) 'highlight))
8377                      highlightp))
8378         (save-restriction
8379           (funcall (cadr elem)))))))
8380
8381 ;; Dynamic variables.
8382 (defvar part-number)
8383 (defvar total-parts)
8384 (defvar type)
8385 (defvar condition)
8386 (defvar length)
8387
8388 (defun gnus-treat-predicate (val)
8389   (cond
8390    ((null val)
8391     nil)
8392    (condition
8393     (eq condition val))
8394    ((and (listp val)
8395          (stringp (car val)))
8396     (apply 'gnus-or (mapcar `(lambda (s)
8397                                (string-match s ,(or gnus-newsgroup-name "")))
8398                             val)))
8399    ((listp val)
8400     (let ((pred (pop val)))
8401       (cond
8402        ((eq pred 'or)
8403         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8404        ((eq pred 'and)
8405         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8406        ((eq pred 'not)
8407         (not (gnus-treat-predicate (car val))))
8408        ((eq pred 'typep)
8409         (equal (car val) type))
8410        (t
8411         (error "%S is not a valid predicate" pred)))))
8412    ((eq val t)
8413     t)
8414    ((eq val 'head)
8415     nil)
8416    ((eq val 'first)
8417     (eq part-number 1))
8418    ((eq val 'last)
8419     (eq part-number total-parts))
8420    ((numberp val)
8421     (< length val))
8422    (t
8423     (error "%S is not a valid value" val))))
8424
8425 (defun gnus-article-encrypt-body (protocol &optional n)
8426   "Encrypt the article body."
8427   (interactive
8428    (list
8429     (or gnus-article-encrypt-protocol
8430         (completing-read "Encrypt protocol: "
8431                          gnus-article-encrypt-protocol-alist
8432                          nil t))
8433     current-prefix-arg))
8434   ;; User might hit `K E' instead of `K e', so prompt once.
8435   (when (and gnus-article-encrypt-protocol
8436              gnus-novice-user)
8437     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8438       (error "Encrypt aborted")))
8439   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8440     (unless func
8441       (error "Can't find the encrypt protocol %s" protocol))
8442     (if (member gnus-newsgroup-name '("nndraft:delayed"
8443                                       "nndraft:drafts"
8444                                       "nndraft:queue"))
8445         (error "Can't encrypt the article in group %s"
8446                gnus-newsgroup-name))
8447     (gnus-summary-iterate n
8448       (with-current-buffer gnus-summary-buffer
8449         (let ((mail-parse-charset gnus-newsgroup-charset)
8450               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8451               (summary-buffer gnus-summary-buffer)
8452               references point)
8453           (gnus-set-global-variables)
8454           (when (gnus-group-read-only-p)
8455             (error "The current newsgroup does not support article encrypt"))
8456           (gnus-summary-show-article t)
8457           (setq references
8458               (or (mail-header-references gnus-current-headers) ""))
8459           (set-buffer gnus-article-buffer)
8460           (let* ((inhibit-read-only t)
8461                  (headers
8462                   (mapcar (lambda (field)
8463                             (and (save-restriction
8464                                    (message-narrow-to-head)
8465                                    (goto-char (point-min))
8466                                    (search-forward field nil t))
8467                                  (prog2
8468                                      (message-narrow-to-field)
8469                                      (buffer-string)
8470                                    (delete-region (point-min) (point-max))
8471                                    (widen))))
8472                           '("Content-Type:" "Content-Transfer-Encoding:"
8473                             "Content-Disposition:"))))
8474             (message-narrow-to-head)
8475             (message-remove-header "MIME-Version")
8476             (goto-char (point-max))
8477             (setq point (point))
8478             (insert (apply 'concat headers))
8479             (widen)
8480             (narrow-to-region point (point-max))
8481             (let ((message-options message-options))
8482               (message-options-set 'message-sender user-mail-address)
8483               (message-options-set 'message-recipients user-mail-address)
8484               (message-options-set 'message-sign-encrypt 'not)
8485               (funcall func))
8486             (goto-char (point-min))
8487             (insert "MIME-Version: 1.0\n")
8488             (widen)
8489             (gnus-summary-edit-article-done
8490              references nil summary-buffer t))
8491           (when gnus-keep-backlog
8492             (gnus-backlog-remove-article
8493              (car gnus-article-current) (cdr gnus-article-current)))
8494           (when (get-buffer gnus-original-article-buffer)
8495             (with-current-buffer gnus-original-article-buffer
8496               (setq gnus-original-article nil)))
8497           (when gnus-use-cache
8498             (gnus-cache-update-article
8499              (car gnus-article-current) (cdr gnus-article-current))))))))
8500
8501 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8502   "The following specs can be used:
8503 %t  The security MIME type
8504 %i  Additional info
8505 %d  Details
8506 %D  Details if button is pressed")
8507
8508 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8509   "The following specs can be used:
8510 %t  The security MIME type
8511 %i  Additional info
8512 %d  Details
8513 %D  Details if button is pressed")
8514
8515 (defvar gnus-mime-security-button-line-format-alist
8516   '((?t gnus-tmp-type ?s)
8517     (?i gnus-tmp-info ?s)
8518     (?d gnus-tmp-details ?s)
8519     (?D gnus-tmp-pressed-details ?s)))
8520
8521 (defvar gnus-mime-security-button-commands
8522   '((gnus-article-press-button "\r" "Show Detail")
8523     (undefined "v")
8524     (undefined "t")
8525     (undefined "C")
8526     (gnus-mime-security-save-part "o" "Save...")
8527     (undefined "\C-o")
8528     (undefined "r")
8529     (undefined "d")
8530     (undefined "c")
8531     (undefined "i")
8532     (undefined "E")
8533     (undefined "e")
8534     (undefined "p")
8535     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8536     (undefined ".")))
8537
8538 (defvar gnus-mime-security-button-map
8539   (let ((map (make-sparse-keymap)))
8540     (define-key map gnus-mouse-2 'gnus-article-push-button)
8541     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8542     (dolist (c gnus-mime-security-button-commands)
8543       (define-key map (cadr c) (car c)))
8544     map))
8545
8546 (easy-menu-define
8547   gnus-mime-security-button-menu gnus-mime-security-button-map
8548   "Security button menu."
8549   `("Security Part"
8550     ,@(delq nil
8551             (mapcar (lambda (c)
8552                       (unless (eq (car c) 'undefined)
8553                         (vector (caddr c) (car c) :active t)))
8554                     gnus-mime-security-button-commands))))
8555
8556 (defun gnus-mime-security-button-menu (event prefix)
8557   "Construct a context-sensitive menu of security commands."
8558   (interactive "e\nP")
8559   (save-window-excursion
8560     (let ((pos (event-start event)))
8561       (select-window (posn-window pos))
8562       (goto-char (posn-point pos))
8563       (gnus-article-check-buffer)
8564       (popup-menu gnus-mime-security-button-menu nil prefix))))
8565
8566 (defvar gnus-mime-security-details-buffer nil)
8567
8568 (defvar gnus-mime-security-button-pressed nil)
8569
8570 (defvar gnus-mime-security-show-details-inline t
8571   "If non-nil, show details in the article buffer.")
8572
8573 (defun gnus-mime-security-verify-or-decrypt (handle)
8574   (mm-remove-parts (cdr handle))
8575   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8576         point (inhibit-read-only t))
8577     (if region
8578         (goto-char (car region)))
8579     (setq point (point))
8580     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8581       (let* ((mm-verify-option 'known)
8582              (mm-decrypt-option 'known)
8583              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8584         (unless (eq nparts (cdr handle))
8585           (mm-destroy-parts (cdr handle))
8586           (setcdr handle nparts))))
8587     (gnus-mime-display-security handle)
8588     (when region
8589       (delete-region (point) (cdr region))
8590       (set-marker (car region) nil)
8591       (set-marker (cdr region) nil))
8592     (goto-char point)))
8593
8594 (defun gnus-mime-security-show-details (handle)
8595   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8596     (if (not details)
8597         (gnus-message 5 "No details.")
8598       (if gnus-mime-security-show-details-inline
8599           (let ((gnus-mime-security-button-pressed
8600                  (not (get-text-property (point) 'gnus-mime-details)))
8601                 (gnus-mime-security-button-line-format
8602                  (get-text-property (point) 'gnus-line-format))
8603                 (inhibit-read-only t))
8604             (forward-char -1)
8605             (while (eq (get-text-property (point) 'gnus-line-format)
8606                        gnus-mime-security-button-line-format)
8607               (forward-char -1))
8608             (forward-char)
8609             (save-restriction
8610               (narrow-to-region (point) (point))
8611               (gnus-insert-mime-security-button handle))
8612             (delete-region (point)
8613                            (or (text-property-not-all
8614                                 (point) (point-max)
8615                                 'gnus-line-format
8616                                 gnus-mime-security-button-line-format)
8617                                (point-max))))
8618         ;; Not inlined.
8619         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8620             (with-current-buffer gnus-mime-security-details-buffer
8621               (erase-buffer)
8622               t)
8623           (setq gnus-mime-security-details-buffer
8624                 (gnus-get-buffer-create "*MIME Security Details*")))
8625         (with-current-buffer gnus-mime-security-details-buffer
8626           (insert details)
8627           (goto-char (point-min)))
8628         (pop-to-buffer gnus-mime-security-details-buffer)))))
8629
8630 (defun gnus-mime-security-press-button (handle)
8631   (save-excursion
8632     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8633         (gnus-mime-security-show-details handle)
8634       (gnus-mime-security-verify-or-decrypt handle))))
8635
8636 (defun gnus-insert-mime-security-button (handle &optional displayed)
8637   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8638          (gnus-tmp-type
8639           (concat
8640            (or (nth 2 (assoc protocol mm-verify-function-alist))
8641                (nth 2 (assoc protocol mm-decrypt-function-alist))
8642                "Unknown")
8643            (if (equal (car handle) "multipart/signed")
8644                " Signed" " Encrypted")
8645            " Part"))
8646          (gnus-tmp-info
8647           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8648               "Undecided"))
8649          (gnus-tmp-details
8650           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8651          gnus-tmp-pressed-details
8652          b e)
8653     (setq gnus-tmp-details
8654           (if gnus-tmp-details
8655               (concat "\n" gnus-tmp-details)
8656             ""))
8657     (setq gnus-tmp-pressed-details
8658           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8659     (unless (bolp)
8660       (insert "\n"))
8661     (setq b (point))
8662     (gnus-eval-format
8663      gnus-mime-security-button-line-format
8664      gnus-mime-security-button-line-format-alist
8665      `(keymap ,gnus-mime-security-button-map
8666          gnus-callback gnus-mime-security-press-button
8667          gnus-line-format ,gnus-mime-security-button-line-format
8668          gnus-mime-details ,gnus-mime-security-button-pressed
8669          article-type annotation
8670          gnus-data ,handle))
8671     (setq e (if (bolp)
8672                 ;; Exclude a newline.
8673                 (1- (point))
8674               (point)))
8675     (when gnus-article-button-face
8676       (gnus-overlay-put (gnus-make-overlay b e nil t)
8677                         'face gnus-article-button-face))
8678     (widget-convert-button
8679      'link b e
8680      :mime-handle handle
8681      :action 'gnus-widget-press-button
8682      :button-keymap gnus-mime-security-button-map
8683      :help-echo
8684      (lambda (widget)
8685        ;; Needed to properly clear the message due to a bug in
8686        ;; wid-edit (XEmacs only).
8687        (when (boundp 'help-echo-owns-message)
8688          (setq help-echo-owns-message t))
8689        (format
8690         "%S: show detail; %S: more options"
8691         (aref gnus-mouse-2 0)
8692         (aref gnus-down-mouse-3 0))))))
8693
8694 (defun gnus-mime-display-security (handle)
8695   (save-restriction
8696     (narrow-to-region (point) (point))
8697     (unless (gnus-unbuttonized-mime-type-p (car handle))
8698       (gnus-insert-mime-security-button handle))
8699     (gnus-mime-display-part (cadr handle))
8700     (unless (bolp)
8701       (insert "\n"))
8702     (unless (gnus-unbuttonized-mime-type-p (car handle))
8703       (let ((gnus-mime-security-button-line-format
8704              gnus-mime-security-button-end-line-format))
8705         (gnus-insert-mime-security-button handle)))
8706     (mm-set-handle-multipart-parameter
8707      handle 'gnus-region
8708      (cons (set-marker (make-marker) (point-min))
8709            (set-marker (make-marker) (point-max))))
8710     (goto-char (point-max))))
8711
8712 (defun gnus-mime-security-run-function (function)
8713   "Run FUNCTION with the security part under point."
8714   (gnus-article-check-buffer)
8715   (let ((data (get-text-property (point) 'gnus-data))
8716         buffer handle)
8717     (when (and (stringp (car-safe data))
8718                (setq buffer (mm-handle-multipart-original-buffer data))
8719                (setq handle (cadr data)))
8720       (if (bufferp (mm-handle-buffer handle))
8721           (progn
8722             (setq handle (cons buffer (copy-sequence (cdr handle))))
8723             (mm-handle-set-undisplayer handle nil))
8724         (setq handle (mm-make-handle
8725                       buffer
8726                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8727                       nil nil nil nil nil nil)))
8728       (funcall function handle))))
8729
8730 (defun gnus-mime-security-save-part ()
8731   "Save the security part under point."
8732   (interactive)
8733   (gnus-mime-security-run-function 'mm-save-part))
8734
8735 (defun gnus-mime-security-pipe-part ()
8736   "Pipe the security part under point to a process."
8737   (interactive)
8738   (gnus-mime-security-run-function 'mm-pipe-part))
8739
8740 (gnus-ems-redefine)
8741
8742 (provide 'gnus-art)
8743
8744 (run-hooks 'gnus-art-load-hook)
8745
8746 ;;; gnus-art.el ends here