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