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