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