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