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