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