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