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