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