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