* gnus-art.el (article-decode-charset): Don't use ignore-errors when calling
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-map)
33   (defvar w3m-minor-mode-map))
34
35 (require 'gnus)
36 ;; Avoid the "Recursive load suspected" error in Emacs 21.1.
37 (eval-and-compile
38   (let ((recursive-load-depth-limit 100))
39     (require 'gnus-sum)))
40 (require 'gnus-spec)
41 (require 'gnus-int)
42 (require 'gnus-win)
43 (require 'mm-bodies)
44 (require 'mail-parse)
45 (require 'mm-decode)
46 (require 'mm-view)
47 (require 'wid-edit)
48 (require 'mm-uu)
49 (require 'message)
50
51 (autoload 'gnus-msg-mail "gnus-msg" nil t)
52 (autoload 'gnus-button-mailto "gnus-msg")
53 (autoload 'gnus-button-reply "gnus-msg" nil t)
54 (autoload 'parse-time-string "parse-time" nil nil)
55 (autoload 'ansi-color-apply-on-region "ansi-color")
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   (mapcar
120    (lambda (header)
121      (concat "^" header ":"))
122    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
123      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
124      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
125      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
126      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
127      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
128      "X-Attribution" "X-Originating-IP" "Delivered-To"
129      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
130      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
131      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
132      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
133      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
134      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
135      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
136      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
137      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
138      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
139      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
140      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
141      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
142      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
143      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
144      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
145      "List-[A-Za-z]+" "X-Listprocessor-Version"
146      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
147      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
148      "X-Received" "Content-length" "X-precedence"
149      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
150      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
151      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
152      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
153      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
154      "X-Content-length" "X-Posting-Agent" "Original-Received"
155      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
156      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
157      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
158      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
159      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"))
160   "*All headers that start with this regexp will be hidden.
161 This variable can also be a list of regexps of headers to be ignored.
162 If `gnus-visible-headers' is non-nil, this variable will be ignored."
163   :type '(choice :custom-show nil
164                  regexp
165                  (repeat regexp))
166   :group 'gnus-article-hiding)
167
168 (defcustom gnus-visible-headers
169   "^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:"
170   "*All headers that do not match this regexp will be hidden.
171 This variable can also be a list of regexp of headers to remain visible.
172 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
173   :type '(repeat :value-to-internal (lambda (widget value)
174                                       (custom-split-regexp-maybe value))
175                  :match (lambda (widget value)
176                           (or (stringp value)
177                               (widget-editable-list-match widget value)))
178                  regexp)
179   :group 'gnus-article-hiding)
180
181 (defcustom gnus-sorted-header-list
182   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
183     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
184   "*This variable is a list of regular expressions.
185 If it is non-nil, headers that match the regular expressions will
186 be placed first in the article buffer in the sequence specified by
187 this list."
188   :type '(repeat regexp)
189   :group 'gnus-article-hiding)
190
191 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
192   "Headers that are only to be displayed if they have interesting data.
193 Possible values in this list are:
194
195   'empty       Headers with no content.
196   'newsgroups  Newsgroup identical to Gnus group.
197   'to-address  To identical to To-address.
198   'to-list     To identical to To-list.
199   'cc-list     CC identical to To-list.
200   'followup-to Followup-to identical to Newsgroups.
201   'reply-to    Reply-to identical to From.
202   'date        Date less than four days old.
203   'long-to     To and/or Cc longer than 1024 characters.
204   'many-to     Multiple To and/or Cc."
205   :type '(set (const :tag "Headers with no content." empty)
206               (const :tag "Newsgroups identical to Gnus group." newsgroups)
207               (const :tag "To identical to To-address." to-address)
208               (const :tag "To identical to To-list." to-list)
209               (const :tag "CC identical to To-list." cc-list)
210               (const :tag "Followup-to identical to Newsgroups." followup-to)
211               (const :tag "Reply-to identical to From." reply-to)
212               (const :tag "Date less than four days old." date)
213               (const :tag "To and/or Cc longer than 1024 characters." long-to)
214               (const :tag "Multiple To and/or Cc headers." many-to))
215   :group 'gnus-article-hiding)
216
217 (defcustom gnus-article-skip-boring nil
218   "Skip over text that is not worth reading.
219 By default, if you set this t, then Gnus will display citations and
220 signatures, but will never scroll down to show you a page consisting
221 only of boring text.  Boring text is controlled by
222 `gnus-article-boring-faces'."
223   :version "22.1"
224   :type 'boolean
225   :group 'gnus-article-hiding)
226
227 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
228   "Regexp matching signature separator.
229 This can also be a list of regexps.  In that case, it will be checked
230 from head to tail looking for a separator.  Searches will be done from
231 the end of the buffer."
232   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
233                  (regexp)
234                  (repeat :tag "List of regexp" regexp))
235   :group 'gnus-article-signature)
236
237 (defcustom gnus-signature-limit nil
238   "Provide a limit to what is considered a signature.
239 If it is a number, no signature may not be longer (in characters) than
240 that number.  If it is a floating point number, no signature may be
241 longer (in lines) than that number.  If it is a function, the function
242 will be called without any parameters, and if it returns nil, there is
243 no signature in the buffer.  If it is a string, it will be used as a
244 regexp.  If it matches, the text in question is not a signature.
245
246 This can also be a list of the above values."
247   :type '(choice (const nil)
248                  (integer :value 200)
249                  (number :value 4.0)
250                  (function :value fun)
251                  (regexp :value ".*"))
252   :group 'gnus-article-signature)
253
254 (defcustom gnus-hidden-properties '(invisible t intangible t)
255   "Property list to use for hiding text."
256   :type 'sexp
257   :group 'gnus-article-hiding)
258
259 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
260 ;; frames in a session.
261 (defcustom gnus-article-x-face-command
262   (if (featurep 'xemacs)
263       (if (or (gnus-image-type-available-p 'xface)
264               (gnus-image-type-available-p 'pbm))
265           'gnus-display-x-face-in-from
266         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
267     (if (gnus-image-type-available-p 'pbm)
268         'gnus-display-x-face-in-from
269       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
270 display -"))
271   "*String or function to be executed to display an X-Face header.
272 If it is a string, the command will be executed in a sub-shell
273 asynchronously.  The compressed face will be piped to this command."
274   :type `(choice string
275                  (function-item gnus-display-x-face-in-from)
276                  function)
277   :version "21.1"
278   :group 'gnus-picon
279   :group 'gnus-article-washing)
280
281 (defcustom gnus-article-x-face-too-ugly nil
282   "Regexp matching posters whose face shouldn't be shown automatically."
283   :type '(choice regexp (const nil))
284   :group 'gnus-article-washing)
285
286 (defcustom gnus-article-banner-alist nil
287   "Banner alist for stripping.
288 For example,
289      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
290   :version "21.1"
291   :type '(repeat (cons symbol regexp))
292   :group 'gnus-article-washing)
293
294 (gnus-define-group-parameter
295  banner
296  :variable-document
297  "Alist of regexps (to match group names) and banner."
298  :variable-group gnus-article-washing
299  :parameter-type
300  '(choice :tag "Banner"
301           :value nil
302           (const :tag "Remove signature" signature)
303           (symbol :tag "Item in `gnus-article-banner-alist'" none)
304           regexp
305           (const :tag "None" nil))
306  :parameter-document
307  "If non-nil, specify how to remove `banners' from articles.
308
309 Symbol `signature' means to remove signatures delimited by
310 `gnus-signature-separator'.  Any other symbol is used to look up a
311 regular expression to match the banner in `gnus-article-banner-alist'.
312 A string is used as a regular expression to match the banner
313 directly.")
314
315 (defcustom gnus-article-address-banner-alist nil
316   "Alist of mail addresses and banners.
317 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
318 to match a mail address in the From: header, BANNER is one of a symbol
319 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
320 If ADDRESS matches author's mail address, it will remove things like
321 advertisements.  For example:
322
323 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
324 "
325   :type '(repeat
326           (cons
327            (regexp :tag "Address")
328            (choice :tag "Banner" :value nil
329                    (const :tag "Remove signature" signature)
330                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
331                    regexp
332                    (const :tag "None" nil))))
333   :version "22.1"
334   :group 'gnus-article-washing)
335
336 (defmacro gnus-emphasis-custom-with-format (&rest body)
337   `(let ((format "\
338 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
339 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
340      ,@body))
341
342 (defun gnus-emphasis-custom-value-to-external (value)
343   (gnus-emphasis-custom-with-format
344    (if (consp (car value))
345        (list (format format (car (car value)) (cdr (car value)))
346              2
347              (if (nth 1 value) 2 3)
348              (nth 2 value))
349      value)))
350
351 (defun gnus-emphasis-custom-value-to-internal (value)
352   (gnus-emphasis-custom-with-format
353    (let ((regexp (concat "\\`"
354                          (format (regexp-quote format)
355                                  "\\([^()]+\\)" "\\([^()]+\\)")
356                          "\\'"))
357          pattern)
358      (if (string-match regexp (setq pattern (car value)))
359          (list (cons (match-string 1 pattern) (match-string 2 pattern))
360                (= (nth 2 value) 2)
361                (nth 3 value))
362        value))))
363
364 (defcustom gnus-emphasis-alist
365   (let ((types
366          '(("\\*" "\\*" bold nil 2)
367            ("_" "_" underline)
368            ("/" "/" italic)
369            ("_/" "/_" underline-italic)
370            ("_\\*" "\\*_" underline-bold)
371            ("\\*/" "/\\*" bold-italic)
372            ("_\\*/" "/\\*_" underline-bold-italic))))
373     (nconc
374      (gnus-emphasis-custom-with-format
375       (mapcar (lambda (spec)
376                 (list (format format (car spec) (cadr spec))
377                       (or (nth 3 spec) 2)
378                       (or (nth 4 spec) 3)
379                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
380               types))
381      '(;; I've never seen anyone use this strikethru convention whereas I've
382        ;; several times seen it triggered by normal text.  --Stef
383        ;; Miles suggests that this form is sometimes used but for italics,
384        ;; so maybe we should map it to `italic'.
385        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
386        ;; 2 3 gnus-emphasis-strikethru)
387        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
388         2 3 gnus-emphasis-underline))))
389   "*Alist that says how to fontify certain phrases.
390 Each item looks like this:
391
392   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
393
394 The first element is a regular expression to be matched.  The second
395 is a number that says what regular expression grouping used to find
396 the entire emphasized word.  The third is a number that says what
397 regexp grouping should be displayed and highlighted.  The fourth
398 is the face used for highlighting."
399   :type
400   '(repeat
401     (menu-choice
402      :format "%[Customizing Style%]\n%v"
403      :indent 2
404      (group :tag "Default"
405             :value ("" 0 0 default)
406             :value-create
407             (lambda (widget)
408               (let ((value (widget-get
409                             (cadr (widget-get (widget-get widget :parent)
410                                               :args))
411                             :value)))
412                 (if (not (eq (nth 2 value) 'default))
413                     (widget-put
414                      widget
415                      :value
416                      (gnus-emphasis-custom-value-to-external value))))
417               (widget-group-value-create widget))
418             regexp
419             (integer :format "Match group: %v")
420             (integer :format "Emphasize group: %v")
421             face)
422      (group :tag "Simple"
423             :value (("_" . "_") nil default)
424             (cons :format "%v"
425                   (regexp :format "Start regexp: %v")
426                   (regexp :format "End regexp: %v"))
427             (boolean :format "Show start and end patterns: %[%v%]\n"
428                      :on " On " :off " Off ")
429             face)))
430   :get (lambda (symbol)
431          (mapcar 'gnus-emphasis-custom-value-to-internal
432                  (default-value symbol)))
433   :set (lambda (symbol value)
434          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
435                                      value)))
436   :group 'gnus-article-emphasis)
437
438 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
439   "A regexp to describe whitespace which should not be emphasized.
440 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
441 The former avoids underlining of leading and trailing whitespace,
442 and the latter avoids underlining any whitespace at all."
443   :version "21.1"
444   :group 'gnus-article-emphasis
445   :type 'regexp)
446
447 (defface gnus-emphasis-bold '((t (:bold t)))
448   "Face used for displaying strong emphasized text (*word*)."
449   :group 'gnus-article-emphasis)
450
451 (defface gnus-emphasis-italic '((t (:italic t)))
452   "Face used for displaying italic emphasized text (/word/)."
453   :group 'gnus-article-emphasis)
454
455 (defface gnus-emphasis-underline '((t (:underline t)))
456   "Face used for displaying underlined emphasized text (_word_)."
457   :group 'gnus-article-emphasis)
458
459 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
460   "Face used for displaying underlined bold emphasized text (_*word*_)."
461   :group 'gnus-article-emphasis)
462
463 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
464   "Face used for displaying underlined italic emphasized text (_/word/_)."
465   :group 'gnus-article-emphasis)
466
467 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
468   "Face used for displaying bold italic emphasized text (/*word*/)."
469   :group 'gnus-article-emphasis)
470
471 (defface gnus-emphasis-underline-bold-italic
472   '((t (:bold t :italic t :underline t)))
473   "Face used for displaying underlined bold italic emphasized text.
474 Example: (_/*word*/_)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
478                                       '((t (:strikethru t)))
479                                     '((t (:strike-through t))))
480   "Face used for displaying strike-through text (-word-)."
481   :group 'gnus-article-emphasis)
482
483 (defface gnus-emphasis-highlight-words
484   '((t (:background "black" :foreground "yellow")))
485   "Face used for displaying highlighted words."
486   :group 'gnus-article-emphasis)
487
488 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
489   "Format for display of Date headers in article bodies.
490 See `format-time-string' for the possible values.
491
492 The variable can also be function, which should return a complete Date
493 header.  The function is called with one argument, the time, which can
494 be fed to `format-time-string'."
495   :type '(choice string function)
496   :link '(custom-manual "(gnus)Article Date")
497   :group 'gnus-article-washing)
498
499 (defcustom gnus-save-all-headers t
500   "*If non-nil, don't remove any headers before saving."
501   :group 'gnus-article-saving
502   :type 'boolean)
503
504 (defcustom gnus-prompt-before-saving 'always
505   "*This variable says how much prompting is to be done when saving articles.
506 If it is nil, no prompting will be done, and the articles will be
507 saved to the default files.  If this variable is `always', each and
508 every article that is saved will be preceded by a prompt, even when
509 saving large batches of articles.  If this variable is neither nil not
510 `always', there the user will be prompted once for a file name for
511 each invocation of the saving commands."
512   :group 'gnus-article-saving
513   :type '(choice (item always)
514                  (item :tag "never" nil)
515                  (sexp :tag "once" :format "%t\n" :value t)))
516
517 (defcustom gnus-saved-headers gnus-visible-headers
518   "Headers to keep if `gnus-save-all-headers' is nil.
519 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
520 If that variable is nil, however, all headers that match this regexp
521 will be kept while the rest will be deleted before saving."
522   :group 'gnus-article-saving
523   :type 'regexp)
524
525 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
526   "A function to save articles in your favourite format.
527 The function must be interactively callable (in other words, it must
528 be an Emacs command).
529
530 Gnus provides the following functions:
531
532 * gnus-summary-save-in-rmail (Rmail format)
533 * gnus-summary-save-in-mail (Unix mail format)
534 * gnus-summary-save-in-folder (MH folder)
535 * gnus-summary-save-in-file (article format)
536 * gnus-summary-save-body-in-file (article body)
537 * gnus-summary-save-in-vm (use VM's folder format)
538 * gnus-summary-write-to-file (article format -- overwrite)."
539   :group 'gnus-article-saving
540   :type '(radio (function-item gnus-summary-save-in-rmail)
541                 (function-item gnus-summary-save-in-mail)
542                 (function-item gnus-summary-save-in-folder)
543                 (function-item gnus-summary-save-in-file)
544                 (function-item gnus-summary-save-body-in-file)
545                 (function-item gnus-summary-save-in-vm)
546                 (function-item gnus-summary-write-to-file)
547                 (function)))
548
549 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
550   "A function generating a file name to save articles in Rmail format.
551 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
552   :group 'gnus-article-saving
553   :type 'function)
554
555 (defcustom gnus-mail-save-name 'gnus-plain-save-name
556   "A function generating a file name to save articles in Unix mail format.
557 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
558   :group 'gnus-article-saving
559   :type 'function)
560
561 (defcustom gnus-folder-save-name 'gnus-folder-save-name
562   "A function generating a file name to save articles in MH folder.
563 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
564   :group 'gnus-article-saving
565   :type 'function)
566
567 (defcustom gnus-file-save-name 'gnus-numeric-save-name
568   "A function generating a file name to save articles in article format.
569 The function is called with NEWSGROUP, HEADERS, and optional
570 LAST-FILE."
571   :group 'gnus-article-saving
572   :type 'function)
573
574 (defcustom gnus-split-methods
575   '((gnus-article-archive-name)
576     (gnus-article-nndoc-name))
577   "*Variable used to suggest where articles are to be saved.
578 For instance, if you would like to save articles related to Gnus in
579 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
580 you could set this variable to something like:
581
582  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
583    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
584
585 This variable is an alist where the key is the match and the
586 value is a list of possible files to save in if the match is
587 non-nil.
588
589 If the match is a string, it is used as a regexp match on the
590 article.  If the match is a symbol, that symbol will be funcalled
591 from the buffer of the article to be saved with the newsgroup as
592 the parameter.  If it is a list, it will be evaled in the same
593 buffer.
594
595 If this form or function returns a string, this string will be
596 used as a possible file name; and if it returns a non-nil list,
597 that list will be used as possible file names."
598   :group 'gnus-article-saving
599   :type '(repeat (choice (list :value (fun) function)
600                          (cons :value ("" "") regexp (repeat string))
601                          (sexp :value nil))))
602
603 (defcustom gnus-page-delimiter "^\^L"
604   "*Regexp describing what to use as article page delimiters.
605 The default value is \"^\^L\", which is a form linefeed at the
606 beginning of a line."
607   :type 'regexp
608   :group 'gnus-article-various)
609
610 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
611   "*The format specification for the article mode line.
612 See `gnus-summary-mode-line-format' for a closer description.
613
614 The following additional specs are available:
615
616 %w  The article washing status.
617 %m  The number of MIME parts in the article."
618   :type 'string
619   :group 'gnus-article-various)
620
621 (defcustom gnus-article-mode-hook nil
622   "*A hook for Gnus article mode."
623   :type 'hook
624   :group 'gnus-article-various)
625
626 (when (featurep 'xemacs)
627   ;; Extracted from gnus-xmas-define in order to preserve user settings
628   (when (fboundp 'turn-off-scroll-in-place)
629     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
630   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
631   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
632
633 (defcustom gnus-article-menu-hook nil
634   "*Hook run after the creation of the article mode menu."
635   :type 'hook
636   :group 'gnus-article-various)
637
638 (defcustom gnus-article-prepare-hook nil
639   "*A hook called after an article has been prepared in the article buffer."
640   :type 'hook
641   :group 'gnus-article-various)
642
643 (defcustom gnus-copy-article-ignored-headers nil
644   "List of headers to be removed when copying an article.
645 Each element is a regular expression."
646   :version "23.0" ;; No Gnus
647   :type '(repeat regexp)
648   :group 'gnus-article-various)
649
650 (make-obsolete-variable 'gnus-article-hide-pgp-hook
651                         "This variable is obsolete in Gnus 5.10.")
652
653 (defcustom gnus-article-button-face 'bold
654   "Face used for highlighting buttons in the article buffer.
655
656 An article button is a piece of text that you can activate by pressing
657 `RET' or `mouse-2' above it."
658   :type 'face
659   :group 'gnus-article-buttons)
660
661 (defcustom gnus-article-mouse-face 'highlight
662   "Face used for mouse highlighting in the article buffer.
663
664 Article buttons will be displayed in this face when the cursor is
665 above them."
666   :type 'face
667   :group 'gnus-article-buttons)
668
669 (defcustom gnus-signature-face 'gnus-signature
670   "Face used for highlighting a signature in the article buffer.
671 Obsolete; use the face `gnus-signature' for customizations instead."
672   :type 'face
673   :group 'gnus-article-highlight
674   :group 'gnus-article-signature)
675
676 (defface gnus-signature
677   '((t
678      (:italic t)))
679   "Face used for highlighting a signature in the article buffer."
680   :group 'gnus-article-highlight
681   :group 'gnus-article-signature)
682 ;; backward-compatibility alias
683 (put 'gnus-signature-face 'face-alias 'gnus-signature)
684
685 (defface gnus-header-from
686   '((((class color)
687       (background dark))
688      (:foreground "spring green"))
689     (((class color)
690       (background light))
691      (:foreground "red3"))
692     (t
693      (:italic t)))
694   "Face used for displaying from headers."
695   :group 'gnus-article-headers
696   :group 'gnus-article-highlight)
697 ;; backward-compatibility alias
698 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
699
700 (defface gnus-header-subject
701   '((((class color)
702       (background dark))
703      (:foreground "SeaGreen3"))
704     (((class color)
705       (background light))
706      (:foreground "red4"))
707     (t
708      (:bold t :italic t)))
709   "Face used for displaying subject headers."
710   :group 'gnus-article-headers
711   :group 'gnus-article-highlight)
712 ;; backward-compatibility alias
713 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
714
715 (defface gnus-header-newsgroups
716   '((((class color)
717       (background dark))
718      (:foreground "yellow" :italic t))
719     (((class color)
720       (background light))
721      (:foreground "MidnightBlue" :italic t))
722     (t
723      (:italic t)))
724   "Face used for displaying newsgroups headers.
725 In the default setup this face is only used for crossposted
726 articles."
727   :group 'gnus-article-headers
728   :group 'gnus-article-highlight)
729 ;; backward-compatibility alias
730 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
731
732 (defface gnus-header-name
733   '((((class color)
734       (background dark))
735      (:foreground "SeaGreen"))
736     (((class color)
737       (background light))
738      (:foreground "maroon"))
739     (t
740      (:bold t)))
741   "Face used for displaying header names."
742   :group 'gnus-article-headers
743   :group 'gnus-article-highlight)
744 ;; backward-compatibility alias
745 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
746
747 (defface gnus-header-content
748   '((((class color)
749       (background dark))
750      (:foreground "forest green" :italic t))
751     (((class color)
752       (background light))
753      (:foreground "indianred4" :italic t))
754     (t
755      (:italic t)))  "Face used for displaying header content."
756   :group 'gnus-article-headers
757   :group 'gnus-article-highlight)
758 ;; backward-compatibility alias
759 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
760
761 (defcustom gnus-header-face-alist
762   '(("From" nil gnus-header-from)
763     ("Subject" nil gnus-header-subject)
764     ("Newsgroups:.*," nil gnus-header-newsgroups)
765     ("" gnus-header-name gnus-header-content))
766   "*Controls highlighting of article headers.
767
768 An alist of the form (HEADER NAME CONTENT).
769
770 HEADER is a regular expression which should match the name of a
771 header and NAME and CONTENT are either face names or nil.
772
773 The name of each header field will be displayed using the face
774 specified by the first element in the list where HEADER matches
775 the header name and NAME is non-nil.  Similarly, the content will
776 be displayed by the first non-nil matching CONTENT face."
777   :group 'gnus-article-headers
778   :group 'gnus-article-highlight
779   :type '(repeat (list (regexp :tag "Header")
780                        (choice :tag "Name"
781                                (item :tag "skip" nil)
782                                (face :value default))
783                        (choice :tag "Content"
784                                (item :tag "skip" nil)
785                                (face :value default)))))
786
787 (defcustom gnus-article-decode-hook
788   '(article-decode-charset article-decode-encoded-words
789                            article-decode-group-name article-decode-idna-rhs)
790   "*Hook run to decode charsets in articles."
791   :group 'gnus-article-headers
792   :type 'hook)
793
794 (defcustom gnus-display-mime-function 'gnus-display-mime
795   "Function to display MIME articles."
796   :group 'gnus-article-mime
797   :type 'function)
798
799 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
800   "Function used to decode headers.")
801
802 (defvar gnus-article-dumbquotes-map
803   '(("\200" "EUR")
804     ("\202" ",")
805     ("\203" "f")
806     ("\204" ",,")
807     ("\205" "...")
808     ("\213" "<")
809     ("\214" "OE")
810     ("\221" "`")
811     ("\222" "'")
812     ("\223" "``")
813     ("\224" "\"")
814     ("\225" "*")
815     ("\226" "-")
816     ("\227" "--")
817     ("\230" "~")
818     ("\231" "(TM)")
819     ("\233" ">")
820     ("\234" "oe")
821     ("\264" "'"))
822   "Table for MS-to-Latin1 translation.")
823
824 (defcustom gnus-ignored-mime-types nil
825   "List of MIME types that should be ignored by Gnus."
826   :version "21.1"
827   :group 'gnus-article-mime
828   :type '(repeat regexp))
829
830 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
831   "List of MIME types that should not be given buttons when rendered inline.
832 See also `gnus-buttonized-mime-types' which may override this variable.
833 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
834   :version "21.1"
835   :group 'gnus-article-mime
836   :type '(repeat regexp))
837
838 (defcustom gnus-buttonized-mime-types nil
839   "List of MIME types that should be given buttons when rendered inline.
840 If set, this variable overrides `gnus-unbuttonized-mime-types'.
841 To see e.g. security buttons you could set this to
842 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
843 this list to display radio buttons that allow you to choose one of two
844 media types those mails include.  See also `mm-discouraged-alternatives'.
845 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
846   :version "22.1"
847   :group 'gnus-article-mime
848   :type '(repeat regexp))
849
850 (defcustom gnus-inhibit-mime-unbuttonizing nil
851   "If non-nil, all MIME parts get buttons.
852 When nil (the default value), then some MIME parts do not get buttons,
853 as described by the variables `gnus-buttonized-mime-types' and
854 `gnus-unbuttonized-mime-types'."
855   :version "22.1"
856   :group 'gnus-article-mime
857   :type 'boolean)
858
859 (defcustom gnus-body-boundary-delimiter "_"
860   "String used to delimit header and body.
861 This variable is used by `gnus-article-treat-body-boundary' which can
862 be controlled by `gnus-treat-body-boundary'."
863   :version "22.1"
864   :group 'gnus-article-various
865   :type '(choice (item :tag "None" :value nil)
866                  string))
867
868 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
869                                   "/usr/share/picons")
870   "Defines the location of the faces database.
871 For information on obtaining this database of pretty pictures, please
872 see http://www.cs.indiana.edu/picons/ftp/index.html"
873   :version "22.1"
874   :type '(repeat directory)
875   :link '(url-link :tag "download"
876                    "http://www.cs.indiana.edu/picons/ftp/index.html")
877   :link '(custom-manual "(gnus)Picons")
878   :group 'gnus-picon)
879
880 (defun gnus-picons-installed-p ()
881   "Say whether picons are installed on your machine."
882   (let ((installed nil))
883     (dolist (database gnus-picon-databases)
884       (when (file-exists-p database)
885         (setq installed t)))
886     installed))
887
888 (defcustom gnus-article-mime-part-function nil
889   "Function called with a MIME handle as the argument.
890 This is meant for people who want to do something automatic based
891 on parts -- for instance, adding Vcard info to a database."
892   :group 'gnus-article-mime
893   :type '(choice (const nil)
894                  function))
895
896 (defcustom gnus-mime-multipart-functions nil
897   "An alist of MIME types to functions to display them."
898   :version "21.1"
899   :group 'gnus-article-mime
900   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
901
902 (defcustom gnus-article-date-lapsed-new-header nil
903   "Whether the X-Sent and Date headers can coexist.
904 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
905 either replace the old \"Date:\" header (if this variable is nil), or
906 be added below it (otherwise)."
907   :version "21.1"
908   :group 'gnus-article-headers
909   :type 'boolean)
910
911 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
912   "Function called with a MIME handle as the argument.
913 This is meant for people who want to view first matched part.
914 For `undisplayed-alternative' (default), the first undisplayed
915 part or alternative part is used.  For `undisplayed', the first
916 undisplayed part is used.  For a function, the first part which
917 the function return t is used.  For nil, the first part is
918 used."
919   :version "21.1"
920   :group 'gnus-article-mime
921   :type '(choice
922           (item :tag "first" :value nil)
923           (item :tag "undisplayed" :value undisplayed)
924           (item :tag "undisplayed or alternative"
925                 :value undisplayed-alternative)
926           (function)))
927
928 (defcustom gnus-mime-action-alist
929   '(("save to file" . gnus-mime-save-part)
930     ("save and strip" . gnus-mime-save-part-and-strip)
931     ("replace with file" . gnus-mime-replace-part)
932     ("delete part" . gnus-mime-delete-part)
933     ("display as text" . gnus-mime-inline-part)
934     ("view the part" . gnus-mime-view-part)
935     ("pipe to command" . gnus-mime-pipe-part)
936     ("toggle display" . gnus-article-press-button)
937     ("toggle display" . gnus-article-view-part-as-charset)
938     ("view as type" . gnus-mime-view-part-as-type)
939     ("view internally" . gnus-mime-view-part-internally)
940     ("view externally" . gnus-mime-view-part-externally))
941   "An alist of actions that run on the MIME attachment."
942   :group 'gnus-article-mime
943   :type '(repeat (cons (string :tag "name")
944                        (function))))
945
946 (defcustom gnus-auto-select-part 1
947   "Advance to next MIME part when deleting or stripping parts.
948
949 When 0, point will be placed on the same part as before.  When
950 positive (negative), move point forward (backwards) this many
951 parts.  When nil, redisplay article."
952   :version "23.0" ;; No Gnus
953   :group 'gnus-article-mime
954   :type '(choice (const nil :tag "Redisplay article.")
955                  (const 1 :tag "Next part.")
956                  (const 0 :tag "Current part.")
957                  integer))
958
959 ;;;
960 ;;; The treatment variables
961 ;;;
962
963 (defvar gnus-part-display-hook nil
964   "Hook called on parts that are to receive treatment.")
965
966 (defvar gnus-article-treat-custom
967   '(choice (const :tag "Off" nil)
968            (const :tag "On" t)
969            (const :tag "Header" head)
970            (const :tag "First" first)
971            (const :tag "Last" last)
972            (integer :tag "Less")
973            (repeat :tag "Groups" regexp)
974            (sexp :tag "Predicate")))
975
976 (defvar gnus-article-treat-head-custom
977   '(choice (const :tag "Off" nil)
978            (const :tag "Header" head)))
979
980 (defvar gnus-article-treat-types '("text/plain")
981   "Parts to treat.")
982
983 (defvar gnus-inhibit-treatment nil
984   "Whether to inhibit treatment.")
985
986 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
987   "Highlight the signature.
988 Valid values are nil, t, `head', `first', `last', an integer or a
989 predicate.  See Info node `(gnus)Customizing Articles'."
990   :group 'gnus-article-treat
991   :link '(custom-manual "(gnus)Customizing Articles")
992   :type gnus-article-treat-custom)
993 (put 'gnus-treat-highlight-signature 'highlight t)
994
995 (defcustom gnus-treat-buttonize 100000
996   "Add buttons.
997 Valid values are nil, t, `head', `first', `last', an integer or a
998 predicate.  See Info node `(gnus)Customizing Articles'."
999   :group 'gnus-article-treat
1000   :link '(custom-manual "(gnus)Customizing Articles")
1001   :type gnus-article-treat-custom)
1002 (put 'gnus-treat-buttonize 'highlight t)
1003
1004 (defcustom gnus-treat-buttonize-head 'head
1005   "Add buttons to the head.
1006 Valid values are nil, t, `head', `first', `last', an integer or a
1007 predicate.  See Info node `(gnus)Customizing Articles'."
1008   :group 'gnus-article-treat
1009   :link '(custom-manual "(gnus)Customizing Articles")
1010   :type gnus-article-treat-head-custom)
1011 (put 'gnus-treat-buttonize-head 'highlight t)
1012
1013 (defcustom gnus-treat-emphasize
1014   (and (or window-system
1015            (featurep 'xemacs))
1016        50000)
1017   "Emphasize text.
1018 Valid values are nil, t, `head', `first', `last', an integer or a
1019 predicate.  See Info node `(gnus)Customizing Articles'."
1020   :group 'gnus-article-treat
1021   :link '(custom-manual "(gnus)Customizing Articles")
1022   :type gnus-article-treat-custom)
1023 (put 'gnus-treat-emphasize 'highlight t)
1024
1025 (defcustom gnus-treat-strip-cr nil
1026   "Remove carriage returns.
1027 Valid values are nil, t, `head', `first', `last', an integer or a
1028 predicate.  See Info node `(gnus)Customizing Articles'."
1029   :version "22.1"
1030   :group 'gnus-article-treat
1031   :link '(custom-manual "(gnus)Customizing Articles")
1032   :type gnus-article-treat-custom)
1033
1034 (defcustom gnus-treat-unsplit-urls nil
1035   "Remove newlines from within URLs.
1036 Valid values are nil, t, `head', `first', `last', an integer or a
1037 predicate.  See Info node `(gnus)Customizing Articles'."
1038   :version "22.1"
1039   :group 'gnus-article-treat
1040   :link '(custom-manual "(gnus)Customizing Articles")
1041   :type gnus-article-treat-custom)
1042
1043 (defcustom gnus-treat-leading-whitespace nil
1044   "Remove leading whitespace in headers.
1045 Valid values are nil, t, `head', `first', `last', an integer or a
1046 predicate.  See Info node `(gnus)Customizing Articles'."
1047   :version "22.1"
1048   :group 'gnus-article-treat
1049   :link '(custom-manual "(gnus)Customizing Articles")
1050   :type gnus-article-treat-custom)
1051
1052 (defcustom gnus-treat-hide-headers 'head
1053   "Hide headers.
1054 Valid values are nil, t, `head', `first', `last', an integer or a
1055 predicate.  See Info node `(gnus)Customizing Articles'."
1056   :group 'gnus-article-treat
1057   :link '(custom-manual "(gnus)Customizing Articles")
1058   :type gnus-article-treat-head-custom)
1059
1060 (defcustom gnus-treat-hide-boring-headers nil
1061   "Hide boring headers.
1062 Valid values are nil, t, `head', `first', `last', an integer or a
1063 predicate.  See Info node `(gnus)Customizing Articles'."
1064   :group 'gnus-article-treat
1065   :link '(custom-manual "(gnus)Customizing Articles")
1066   :type gnus-article-treat-head-custom)
1067
1068 (defcustom gnus-treat-hide-signature nil
1069   "Hide the signature.
1070 Valid values are nil, t, `head', `first', `last', an integer or a
1071 predicate.  See Info node `(gnus)Customizing Articles'."
1072   :group 'gnus-article-treat
1073   :link '(custom-manual "(gnus)Customizing Articles")
1074   :type gnus-article-treat-custom)
1075
1076 (defcustom gnus-treat-fill-article nil
1077   "Fill the article.
1078 Valid values are nil, t, `head', `first', `last', an integer or a
1079 predicate.  See Info node `(gnus)Customizing Articles'."
1080   :group 'gnus-article-treat
1081   :link '(custom-manual "(gnus)Customizing Articles")
1082   :type gnus-article-treat-custom)
1083
1084 (defcustom gnus-treat-hide-citation nil
1085   "Hide cited text.
1086 Valid values are nil, t, `head', `first', `last', an integer or a
1087 predicate.  See Info node `(gnus)Customizing Articles'."
1088   :group 'gnus-article-treat
1089   :link '(custom-manual "(gnus)Customizing Articles")
1090   :type gnus-article-treat-custom)
1091
1092 (defcustom gnus-treat-hide-citation-maybe nil
1093   "Hide cited text.
1094 Valid values are nil, t, `head', `first', `last', an integer or a
1095 predicate.  See Info node `(gnus)Customizing Articles'."
1096   :group 'gnus-article-treat
1097   :link '(custom-manual "(gnus)Customizing Articles")
1098   :type gnus-article-treat-custom)
1099
1100 (defcustom gnus-treat-strip-list-identifiers 'head
1101   "Strip list identifiers from `gnus-list-identifiers`.
1102 Valid values are nil, t, `head', `first', `last', an integer or a
1103 predicate.  See Info node `(gnus)Customizing Articles'."
1104   :version "21.1"
1105   :group 'gnus-article-treat
1106   :link '(custom-manual "(gnus)Customizing Articles")
1107   :type gnus-article-treat-custom)
1108
1109 (make-obsolete-variable 'gnus-treat-strip-pgp
1110                         "This option is obsolete in Gnus 5.10.")
1111
1112 (defcustom gnus-treat-strip-pem nil
1113   "Strip PEM signatures.
1114 Valid values are nil, t, `head', `first', `last', an integer or a
1115 predicate.  See Info node `(gnus)Customizing Articles'."
1116   :group 'gnus-article-treat
1117   :link '(custom-manual "(gnus)Customizing Articles")
1118   :type gnus-article-treat-custom)
1119
1120 (defcustom gnus-treat-strip-banner t
1121   "Strip banners from articles.
1122 The banner to be stripped is specified in the `banner' group parameter.
1123 Valid values are nil, t, `head', `first', `last', an integer or a
1124 predicate.  See Info node `(gnus)Customizing Articles'."
1125   :group 'gnus-article-treat
1126   :link '(custom-manual "(gnus)Customizing Articles")
1127   :type gnus-article-treat-custom)
1128
1129 (defcustom gnus-treat-highlight-headers 'head
1130   "Highlight the headers.
1131 Valid values are nil, t, `head', `first', `last', an integer or a
1132 predicate.  See Info node `(gnus)Customizing Articles'."
1133   :group 'gnus-article-treat
1134   :link '(custom-manual "(gnus)Customizing Articles")
1135   :type gnus-article-treat-head-custom)
1136 (put 'gnus-treat-highlight-headers 'highlight t)
1137
1138 (defcustom gnus-treat-highlight-citation t
1139   "Highlight cited text.
1140 Valid values are nil, t, `head', `first', `last', an integer or a
1141 predicate.  See Info node `(gnus)Customizing Articles'."
1142   :group 'gnus-article-treat
1143   :link '(custom-manual "(gnus)Customizing Articles")
1144   :type gnus-article-treat-custom)
1145 (put 'gnus-treat-highlight-citation 'highlight t)
1146
1147 (defcustom gnus-treat-date-ut nil
1148   "Display the Date in UT (GMT).
1149 Valid values are nil, t, `head', `first', `last', an integer or a
1150 predicate.  See Info node `(gnus)Customizing Articles'."
1151   :group 'gnus-article-treat
1152   :link '(custom-manual "(gnus)Customizing Articles")
1153   :type gnus-article-treat-head-custom)
1154
1155 (defcustom gnus-treat-date-local nil
1156   "Display the Date in the local timezone.
1157 Valid values are nil, t, `head', `first', `last', an integer or a
1158 predicate.  See Info node `(gnus)Customizing Articles'."
1159   :group 'gnus-article-treat
1160   :link '(custom-manual "(gnus)Customizing Articles")
1161   :type gnus-article-treat-head-custom)
1162
1163 (defcustom gnus-treat-date-english nil
1164   "Display the Date in a format that can be read aloud in English.
1165 Valid values are nil, t, `head', `first', `last', an integer or a
1166 predicate.  See Info node `(gnus)Customizing Articles'."
1167   :version "22.1"
1168   :group 'gnus-article-treat
1169   :link '(custom-manual "(gnus)Customizing Articles")
1170   :type gnus-article-treat-head-custom)
1171
1172 (defcustom gnus-treat-date-lapsed nil
1173   "Display the Date header in a way that says how much time has elapsed.
1174 Valid values are nil, t, `head', `first', `last', an integer or a
1175 predicate.  See Info node `(gnus)Customizing Articles'."
1176   :group 'gnus-article-treat
1177   :link '(custom-manual "(gnus)Customizing Articles")
1178   :type gnus-article-treat-head-custom)
1179
1180 (defcustom gnus-treat-date-original nil
1181   "Display the date in the original timezone.
1182 Valid values are nil, t, `head', `first', `last', an integer or a
1183 predicate.  See Info node `(gnus)Customizing Articles'."
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-iso8601 nil
1189   "Display the date in the ISO8601 format.
1190 Valid values are nil, t, `head', `first', `last', an integer or a
1191 predicate.  See Info node `(gnus)Customizing Articles'."
1192   :version "21.1"
1193   :group 'gnus-article-treat
1194   :link '(custom-manual "(gnus)Customizing Articles")
1195   :type gnus-article-treat-head-custom)
1196
1197 (defcustom gnus-treat-date-user-defined nil
1198   "Display the date in a user-defined format.
1199 The format is defined by the `gnus-article-time-format' variable.
1200 Valid values are nil, t, `head', `first', `last', an integer or a
1201 predicate.  See Info node `(gnus)Customizing Articles'."
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-head-custom)
1205
1206 (defcustom gnus-treat-strip-headers-in-body t
1207   "Strip the X-No-Archive header line from the beginning of the body.
1208 Valid values are nil, t, `head', `first', `last', an integer or a
1209 predicate.  See Info node `(gnus)Customizing Articles'."
1210   :version "21.1"
1211   :group 'gnus-article-treat
1212   :link '(custom-manual "(gnus)Customizing Articles")
1213   :type gnus-article-treat-custom)
1214
1215 (defcustom gnus-treat-strip-trailing-blank-lines nil
1216   "Strip trailing blank lines.
1217 Valid values are nil, t, `head', `first', `last', an integer or a
1218 predicate.  See Info node `(gnus)Customizing Articles'.
1219
1220 When set to t, it also strips trailing blanks in all MIME parts.
1221 Consider to use `last' instead."
1222   :group 'gnus-article-treat
1223   :link '(custom-manual "(gnus)Customizing Articles")
1224   :type gnus-article-treat-custom)
1225
1226 (defcustom gnus-treat-strip-leading-blank-lines nil
1227   "Strip leading blank lines.
1228 Valid values are nil, t, `head', `first', `last', an integer or a
1229 predicate.  See Info node `(gnus)Customizing Articles'.
1230
1231 When set to t, it also strips trailing blanks in all MIME parts."
1232   :group 'gnus-article-treat
1233   :link '(custom-manual "(gnus)Customizing Articles")
1234   :type gnus-article-treat-custom)
1235
1236 (defcustom gnus-treat-strip-multiple-blank-lines nil
1237   "Strip multiple blank lines.
1238 Valid values are nil, t, `head', `first', `last', an integer or a
1239 predicate.  See Info node `(gnus)Customizing Articles'."
1240   :group 'gnus-article-treat
1241   :link '(custom-manual "(gnus)Customizing Articles")
1242   :type gnus-article-treat-custom)
1243
1244 (defcustom gnus-treat-unfold-headers 'head
1245   "Unfold folded header lines.
1246 Valid values are nil, t, `head', `first', `last', an integer or a
1247 predicate.  See Info node `(gnus)Customizing Articles'."
1248   :version "22.1"
1249   :group 'gnus-article-treat
1250   :link '(custom-manual "(gnus)Customizing Articles")
1251   :type gnus-article-treat-custom)
1252
1253 (defcustom gnus-treat-fold-headers nil
1254   "Fold headers.
1255 Valid values are nil, t, `head', `first', `last', an integer or a
1256 predicate.  See Info node `(gnus)Customizing Articles'."
1257   :version "22.1"
1258   :group 'gnus-article-treat
1259   :link '(custom-manual "(gnus)Customizing Articles")
1260   :type gnus-article-treat-custom)
1261
1262 (defcustom gnus-treat-fold-newsgroups 'head
1263   "Fold the Newsgroups and Followup-To headers.
1264 Valid values are nil, t, `head', `first', `last', an integer or a
1265 predicate.  See Info node `(gnus)Customizing Articles'."
1266   :version "22.1"
1267   :group 'gnus-article-treat
1268   :link '(custom-manual "(gnus)Customizing Articles")
1269   :type gnus-article-treat-custom)
1270
1271 (defcustom gnus-treat-overstrike t
1272   "Treat overstrike highlighting.
1273 Valid values are nil, t, `head', `first', `last', an integer or a
1274 predicate.  See Info node `(gnus)Customizing Articles'."
1275   :group 'gnus-article-treat
1276   :link '(custom-manual "(gnus)Customizing Articles")
1277   :type gnus-article-treat-custom)
1278 (put 'gnus-treat-overstrike 'highlight t)
1279
1280 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1281   "Treat ANSI SGR control sequences.
1282 Valid values are nil, t, `head', `first', `last', an integer or a
1283 predicate.  See Info node `(gnus)Customizing Articles'."
1284   :group 'gnus-article-treat
1285   :link '(custom-manual "(gnus)Customizing Articles")
1286   :type gnus-article-treat-custom)
1287
1288 (make-obsolete-variable 'gnus-treat-display-xface
1289                         'gnus-treat-display-x-face)
1290
1291 (defcustom gnus-treat-display-x-face
1292   (and (not noninteractive)
1293        (or (and (fboundp 'image-type-available-p)
1294                 (image-type-available-p 'xbm)
1295                 (string-match "^0x" (shell-command-to-string "uncompface"))
1296                 (executable-find "icontopbm"))
1297            (and (featurep 'xemacs)
1298                 (featurep 'xface)))
1299        'head)
1300   "Display X-Face headers.
1301 Valid values are nil, t, `head', `first', `last', an integer or a
1302 predicate.  See Info node `(gnus)Customizing Articles' and Info
1303 node `(gnus)X-Face' for details."
1304   :group 'gnus-article-treat
1305   :version "21.1"
1306   :link '(custom-manual "(gnus)Customizing Articles")
1307   :link '(custom-manual "(gnus)X-Face")
1308   :type gnus-article-treat-head-custom
1309   :set (lambda (symbol value)
1310          (set-default
1311           symbol
1312           (cond ((or (boundp symbol) (get symbol 'saved-value))
1313                  value)
1314                 ((boundp 'gnus-treat-display-xface)
1315                  (message "\
1316 ** gnus-treat-display-xface is an obsolete variable;\
1317  use gnus-treat-display-x-face instead")
1318                  (default-value 'gnus-treat-display-xface))
1319                 ((get 'gnus-treat-display-xface 'saved-value)
1320                  (message "\
1321 ** gnus-treat-display-xface is an obsolete variable;\
1322  use gnus-treat-display-x-face instead")
1323                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1324                 (t
1325                  value)))))
1326 (put 'gnus-treat-display-x-face 'highlight t)
1327
1328 (defcustom gnus-treat-display-face
1329   (and (not noninteractive)
1330        (or (and (fboundp 'image-type-available-p)
1331                 (image-type-available-p 'png))
1332            (and (featurep 'xemacs)
1333                 (featurep 'png)))
1334        'head)
1335   "Display Face headers.
1336 Valid values are nil, t, `head', `first', `last', an integer or a
1337 predicate.  See Info node `(gnus)Customizing Articles' and Info
1338 node `(gnus)X-Face' for details."
1339   :group 'gnus-article-treat
1340   :version "22.1"
1341   :link '(custom-manual "(gnus)Customizing Articles")
1342   :link '(custom-manual "(gnus)X-Face")
1343   :type gnus-article-treat-head-custom)
1344 (put 'gnus-treat-display-face 'highlight t)
1345
1346 (defcustom gnus-treat-display-smileys
1347   (if (or (and (featurep 'xemacs)
1348                (featurep 'xpm))
1349           (and (fboundp 'image-type-available-p)
1350                (image-type-available-p 'pbm)))
1351       t nil)
1352   "Display smileys.
1353 Valid values are nil, t, `head', `first', `last', an integer or a
1354 predicate.  See Info node `(gnus)Customizing Articles' and Info
1355 node `(gnus)Smileys' for details."
1356   :group 'gnus-article-treat
1357   :version "21.1"
1358   :link '(custom-manual "(gnus)Customizing Articles")
1359   :link '(custom-manual "(gnus)Smileys")
1360   :type gnus-article-treat-custom)
1361 (put 'gnus-treat-display-smileys 'highlight t)
1362
1363 (defcustom gnus-treat-from-picon
1364   (if (and (gnus-image-type-available-p 'xpm)
1365            (gnus-picons-installed-p))
1366       'head nil)
1367   "Display picons in the From header.
1368 Valid values are nil, t, `head', `first', `last', an integer or a
1369 predicate.  See Info node `(gnus)Customizing Articles' and Info
1370 node `(gnus)Picons' for details."
1371   :version "22.1"
1372   :group 'gnus-article-treat
1373   :group 'gnus-picon
1374   :link '(custom-manual "(gnus)Customizing Articles")
1375   :link '(custom-manual "(gnus)Picons")
1376   :type gnus-article-treat-head-custom)
1377 (put 'gnus-treat-from-picon 'highlight t)
1378
1379 (defcustom gnus-treat-mail-picon
1380   (if (and (gnus-image-type-available-p 'xpm)
1381            (gnus-picons-installed-p))
1382       'head nil)
1383   "Display picons in To and Cc headers.
1384 Valid values are nil, t, `head', `first', `last', an integer or a
1385 predicate.  See Info node `(gnus)Customizing Articles' and Info
1386 node `(gnus)Picons' for details."
1387   :version "22.1"
1388   :group 'gnus-article-treat
1389   :group 'gnus-picon
1390   :link '(custom-manual "(gnus)Customizing Articles")
1391   :link '(custom-manual "(gnus)Picons")
1392   :type gnus-article-treat-head-custom)
1393 (put 'gnus-treat-mail-picon 'highlight t)
1394
1395 (defcustom gnus-treat-newsgroups-picon
1396   (if (and (gnus-image-type-available-p 'xpm)
1397            (gnus-picons-installed-p))
1398       'head nil)
1399   "Display picons in the Newsgroups and Followup-To headers.
1400 Valid values are nil, t, `head', `first', `last', an integer or a
1401 predicate.  See Info node `(gnus)Customizing Articles' and Info
1402 node `(gnus)Picons' for details."
1403   :version "22.1"
1404   :group 'gnus-article-treat
1405   :group 'gnus-picon
1406   :link '(custom-manual "(gnus)Customizing Articles")
1407   :link '(custom-manual "(gnus)Picons")
1408   :type gnus-article-treat-head-custom)
1409 (put 'gnus-treat-newsgroups-picon 'highlight t)
1410
1411 (defcustom gnus-treat-body-boundary
1412   (if (and (eq window-system 'x)
1413            (or gnus-treat-newsgroups-picon
1414                gnus-treat-mail-picon
1415                gnus-treat-from-picon))
1416       'head nil)
1417   "Draw a boundary at the end of the headers.
1418 Valid values are nil and `head'.
1419 See Info node `(gnus)Customizing Articles' for details."
1420   :version "22.1"
1421   :group 'gnus-article-treat
1422   :link '(custom-manual "(gnus)Customizing Articles")
1423   :type gnus-article-treat-head-custom)
1424
1425 (defcustom gnus-treat-capitalize-sentences nil
1426   "Capitalize sentence-starting words.
1427 Valid values are nil, t, `head', `first', `last', an integer or a
1428 predicate.  See Info node `(gnus)Customizing Articles'."
1429   :version "21.1"
1430   :group 'gnus-article-treat
1431   :link '(custom-manual "(gnus)Customizing Articles")
1432   :type gnus-article-treat-custom)
1433
1434 (defcustom gnus-treat-wash-html nil
1435   "Format as HTML.
1436 Valid values are nil, t, `head', `first', `last', an integer or a
1437 predicate.  See Info node `(gnus)Customizing Articles'."
1438   :version "22.1"
1439   :group 'gnus-article-treat
1440   :link '(custom-manual "(gnus)Customizing Articles")
1441   :type gnus-article-treat-custom)
1442
1443 (defcustom gnus-treat-fill-long-lines nil
1444   "Fill long lines.
1445 Valid values are nil, t, `head', `first', `last', an integer or a
1446 predicate.  See Info node `(gnus)Customizing Articles'."
1447   :group 'gnus-article-treat
1448   :link '(custom-manual "(gnus)Customizing Articles")
1449   :type gnus-article-treat-custom)
1450
1451 (defcustom gnus-treat-play-sounds nil
1452   "Play sounds.
1453 Valid values are nil, t, `head', `first', `last', an integer or a
1454 predicate.  See Info node `(gnus)Customizing Articles'."
1455   :version "21.1"
1456   :group 'gnus-article-treat
1457   :link '(custom-manual "(gnus)Customizing Articles")
1458   :type gnus-article-treat-custom)
1459
1460 (defcustom gnus-treat-translate nil
1461   "Translate articles from one language to another.
1462 Valid values are nil, t, `head', `first', `last', an integer or a
1463 predicate.  See Info node `(gnus)Customizing Articles'."
1464   :version "21.1"
1465   :group 'gnus-article-treat
1466   :link '(custom-manual "(gnus)Customizing Articles")
1467   :type gnus-article-treat-custom)
1468
1469 (defcustom gnus-treat-x-pgp-sig nil
1470   "Verify X-PGP-Sig.
1471 To automatically treat X-PGP-Sig, set it to head.
1472 Valid values are nil, t, `head', `first', `last', an integer or a
1473 predicate.  See Info node `(gnus)Customizing Articles'."
1474   :version "22.1"
1475   :group 'gnus-article-treat
1476   :group 'mime-security
1477   :link '(custom-manual "(gnus)Customizing Articles")
1478   :type gnus-article-treat-custom)
1479
1480 (defvar gnus-article-encrypt-protocol-alist
1481   '(("PGP" . mml2015-self-encrypt)))
1482
1483 ;; Set to nil if more than one protocol added to
1484 ;; gnus-article-encrypt-protocol-alist.
1485 (defcustom gnus-article-encrypt-protocol "PGP"
1486   "The protocol used for encrypt articles.
1487 It is a string, such as \"PGP\". If nil, ask user."
1488   :version "22.1"
1489   :type 'string
1490   :group 'mime-security)
1491
1492 (defvar gnus-article-wash-function nil
1493   "Function used for converting HTML into text.")
1494
1495 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1496                               (mm-coding-system-p 'utf-8)
1497                               (executable-find idna-program))
1498   "Whether IDNA decoding of headers is used when viewing messages.
1499 This requires GNU Libidn, and by default only enabled if it is found."
1500   :version "22.1"
1501   :group 'gnus-article-headers
1502   :type 'boolean)
1503
1504 (defcustom gnus-article-over-scroll nil
1505   "If non-nil, allow scrolling the article buffer even when there no more text."
1506   :version "22.1"
1507   :group 'gnus-article
1508   :type 'boolean)
1509
1510 ;;; Internal variables
1511
1512 (defvar gnus-english-month-names
1513   '("January" "February" "March" "April" "May" "June" "July" "August"
1514     "September" "October" "November" "December"))
1515
1516 (defvar gnus-button-regexp nil)
1517 (defvar gnus-button-marker-list nil)
1518 ;; Regexp matching any of the regexps from `gnus-button-alist'.
1519
1520 (defvar gnus-button-last nil)
1521 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
1522
1523 (defvar article-goto-body-goes-to-point-min-p nil)
1524 (defvar gnus-article-wash-types nil)
1525 (defvar gnus-article-emphasis-alist nil)
1526 (defvar gnus-article-image-alist nil)
1527
1528 (defvar gnus-article-mime-handle-alist-1 nil)
1529 (defvar gnus-treatment-function-alist
1530   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1531     (gnus-treat-strip-banner gnus-article-strip-banner)
1532     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1533     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1534     (gnus-treat-buttonize gnus-article-add-buttons)
1535     (gnus-treat-fill-article gnus-article-fill-cited-article)
1536     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1537     (gnus-treat-strip-cr gnus-article-remove-cr)
1538     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1539     (gnus-treat-date-ut gnus-article-date-ut)
1540     (gnus-treat-date-local gnus-article-date-local)
1541     (gnus-treat-date-english gnus-article-date-english)
1542     (gnus-treat-date-original gnus-article-date-original)
1543     (gnus-treat-date-user-defined gnus-article-date-user)
1544     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1545     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1546     (gnus-treat-display-x-face gnus-article-display-x-face)
1547     (gnus-treat-display-face gnus-article-display-face)
1548     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1549     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1550     (gnus-treat-hide-signature gnus-article-hide-signature)
1551     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1552     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1553     (gnus-treat-strip-pem gnus-article-hide-pem)
1554     (gnus-treat-from-picon gnus-treat-from-picon)
1555     (gnus-treat-mail-picon gnus-treat-mail-picon)
1556     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1557     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1558     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1559     (gnus-treat-strip-trailing-blank-lines
1560      gnus-article-remove-trailing-blank-lines)
1561     (gnus-treat-strip-leading-blank-lines
1562      gnus-article-strip-leading-blank-lines)
1563     (gnus-treat-strip-multiple-blank-lines
1564      gnus-article-strip-multiple-blank-lines)
1565     (gnus-treat-overstrike gnus-article-treat-overstrike)
1566     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1567     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1568     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1569     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1570     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1571     (gnus-treat-display-smileys gnus-treat-smiley)
1572     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1573     (gnus-treat-wash-html gnus-article-wash-html)
1574     (gnus-treat-emphasize gnus-article-emphasize)
1575     (gnus-treat-hide-citation gnus-article-hide-citation)
1576     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1577     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1578     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1579     (gnus-treat-play-sounds gnus-earcon-display)))
1580
1581 (defvar gnus-article-mime-handle-alist nil)
1582 (defvar article-lapsed-timer nil)
1583 (defvar gnus-article-current-summary nil)
1584
1585 (defvar gnus-article-mode-syntax-table
1586   (let ((table (copy-syntax-table text-mode-syntax-table)))
1587     ;; This causes the citation match run O(2^n).
1588     ;; (modify-syntax-entry ?- "w" table)
1589     (modify-syntax-entry ?> ")<" table)
1590     (modify-syntax-entry ?< "(>" table)
1591     ;; make M-. in article buffers work for `foo' strings
1592     (modify-syntax-entry ?' " " table)
1593     (modify-syntax-entry ?` " " table)
1594     table)
1595   "Syntax table used in article mode buffers.
1596 Initialized from `text-mode-syntax-table.")
1597
1598 (defvar gnus-save-article-buffer nil)
1599
1600 (defvar gnus-article-mode-line-format-alist
1601   (nconc '((?w (gnus-article-wash-status) ?s)
1602            (?m (gnus-article-mime-part-status) ?s))
1603          gnus-summary-mode-line-format-alist))
1604
1605 (defvar gnus-number-of-articles-to-be-saved nil)
1606
1607 (defvar gnus-inhibit-hiding nil)
1608
1609 (defvar gnus-article-edit-mode nil)
1610
1611 ;;; Macros for dealing with the article buffer.
1612
1613 (defmacro gnus-with-article-headers (&rest forms)
1614   `(save-excursion
1615      (set-buffer gnus-article-buffer)
1616      (save-restriction
1617        (let ((inhibit-read-only t)
1618              (inhibit-point-motion-hooks t)
1619              (case-fold-search t))
1620          (article-narrow-to-head)
1621          ,@forms))))
1622
1623 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1624 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1625
1626 (defmacro gnus-with-article-buffer (&rest forms)
1627   `(save-excursion
1628      (set-buffer gnus-article-buffer)
1629      (let ((inhibit-read-only t))
1630        ,@forms)))
1631
1632 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1633 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1634
1635 (defun gnus-article-goto-header (header)
1636   "Go to HEADER, which is a regular expression."
1637   (re-search-forward (concat "^\\(" header "\\):") nil t))
1638
1639 (defsubst gnus-article-hide-text (b e props)
1640   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1641   (gnus-add-text-properties-when 'article-type nil b e props)
1642   (when (memq 'intangible props)
1643     (put-text-property
1644      (max (1- b) (point-min))
1645      b 'intangible (cddr (memq 'intangible props)))))
1646
1647 (defsubst gnus-article-unhide-text (b e)
1648   "Remove hidden text properties from region between B and E."
1649   (remove-text-properties b e gnus-hidden-properties)
1650   (when (memq 'intangible gnus-hidden-properties)
1651     (put-text-property (max (1- b) (point-min))
1652                        b 'intangible nil)))
1653
1654 (defun gnus-article-hide-text-type (b e type)
1655   "Hide text of TYPE between B and E."
1656   (gnus-add-wash-type type)
1657   (gnus-article-hide-text
1658    b e (cons 'article-type (cons type gnus-hidden-properties))))
1659
1660 (defun gnus-article-unhide-text-type (b e type)
1661   "Unhide text of TYPE between B and E."
1662   (gnus-delete-wash-type type)
1663   (remove-text-properties
1664    b e (cons 'article-type (cons type gnus-hidden-properties)))
1665   (when (memq 'intangible gnus-hidden-properties)
1666     (put-text-property (max (1- b) (point-min))
1667                        b 'intangible nil)))
1668
1669 (defun gnus-article-hide-text-of-type (type)
1670   "Hide text of TYPE in the current buffer."
1671   (save-excursion
1672     (let ((b (point-min))
1673           (e (point-max)))
1674       (while (setq b (text-property-any b e 'article-type type))
1675         (add-text-properties b (incf b) gnus-hidden-properties)))))
1676
1677 (defun gnus-article-delete-text-of-type (type)
1678   "Delete text of TYPE in the current buffer."
1679   (save-excursion
1680     (let ((b (point-min)))
1681       (if (eq type 'multipart)
1682           ;; Remove MIME buttons associated with multipart/alternative parts.
1683           (progn
1684             (goto-char b)
1685             (while (if (get-text-property (point) 'gnus-part)
1686                        (setq b (point))
1687                      (when (setq b (next-single-property-change (point)
1688                                                                 'gnus-part))
1689                        (goto-char b)
1690                        t))
1691               (end-of-line)
1692               (skip-chars-forward "\n")
1693               (when (eq (get-text-property b 'article-type) 'multipart)
1694                 (delete-region b (point)))))
1695         (while (setq b (text-property-any b (point-max) 'article-type type))
1696           (delete-region
1697            b (or (text-property-not-all b (point-max) 'article-type type)
1698                  (point-max))))))))
1699
1700 (defun gnus-article-delete-invisible-text ()
1701   "Delete all invisible text in the current buffer."
1702   (save-excursion
1703     (let ((b (point-min)))
1704       (while (setq b (text-property-any b (point-max) 'invisible t))
1705         (delete-region
1706          b (or (text-property-not-all b (point-max) 'invisible t)
1707                (point-max)))))))
1708
1709 (defun gnus-article-text-type-exists-p (type)
1710   "Say whether any text of type TYPE exists in the buffer."
1711   (text-property-any (point-min) (point-max) 'article-type type))
1712
1713 (defsubst gnus-article-header-rank ()
1714   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1715   (let ((list gnus-sorted-header-list)
1716         (i 1))
1717     (while list
1718       (if (looking-at (car list))
1719           (setq list nil)
1720         (setq list (cdr list))
1721         (incf i)))
1722       i))
1723
1724 (defun article-hide-headers (&optional arg delete)
1725   "Hide unwanted headers and possibly sort them as well."
1726   (interactive)
1727   ;; This function might be inhibited.
1728   (unless gnus-inhibit-hiding
1729     (let ((inhibit-read-only nil)
1730           (case-fold-search t)
1731           (max (1+ (length gnus-sorted-header-list)))
1732           (inhibit-point-motion-hooks t)
1733           (cur (current-buffer))
1734           ignored visible beg)
1735       (save-excursion
1736         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1737         ;; group parameters, so we should go to the summary buffer.
1738         (when (prog1
1739                   (condition-case nil
1740                       (progn (set-buffer gnus-summary-buffer) t)
1741                     (error nil))
1742                 (setq ignored (when (not gnus-visible-headers)
1743                                 (cond ((stringp gnus-ignored-headers)
1744                                        gnus-ignored-headers)
1745                                       ((listp gnus-ignored-headers)
1746                                        (mapconcat 'identity
1747                                                   gnus-ignored-headers
1748                                                   "\\|"))))
1749                       visible (cond ((stringp gnus-visible-headers)
1750                                      gnus-visible-headers)
1751                                     ((and gnus-visible-headers
1752                                           (listp gnus-visible-headers))
1753                                      (mapconcat 'identity
1754                                                 gnus-visible-headers
1755                                                 "\\|")))))
1756           (set-buffer cur))
1757         (save-restriction
1758           ;; First we narrow to just the headers.
1759           (article-narrow-to-head)
1760           ;; Hide any "From " lines at the beginning of (mail) articles.
1761           (while (looking-at "From ")
1762             (forward-line 1))
1763           (unless (bobp)
1764             (delete-region (point-min) (point)))
1765           ;; Then treat the rest of the header lines.
1766           ;; Then we use the two regular expressions
1767           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1768           ;; select which header lines is to remain visible in the
1769           ;; article buffer.
1770           (while (re-search-forward "^[^ \t:]*:" nil t)
1771             (beginning-of-line)
1772             ;; Mark the rank of the header.
1773             (put-text-property
1774              (point) (1+ (point)) 'message-rank
1775              (if (or (and visible (looking-at visible))
1776                      (and ignored
1777                           (not (looking-at ignored))))
1778                  (gnus-article-header-rank)
1779                (+ 2 max)))
1780             (forward-line 1))
1781           (message-sort-headers-1)
1782           (when (setq beg (text-property-any
1783                            (point-min) (point-max) 'message-rank (+ 2 max)))
1784             ;; We delete the unwanted headers.
1785             (gnus-add-wash-type 'headers)
1786             (add-text-properties (point-min) (+ 5 (point-min))
1787                                  '(article-type headers dummy-invisible t))
1788             (delete-region beg (point-max))))))))
1789
1790 (defun article-hide-boring-headers (&optional arg)
1791   "Toggle hiding of headers that aren't very interesting.
1792 If given a negative prefix, always show; if given a positive prefix,
1793 always hide."
1794   (interactive (gnus-article-hidden-arg))
1795   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1796              (not gnus-show-all-headers))
1797     (save-excursion
1798       (save-restriction
1799         (let ((inhibit-read-only t)
1800               (inhibit-point-motion-hooks t))
1801           (article-narrow-to-head)
1802           (dolist (elem gnus-boring-article-headers)
1803             (goto-char (point-min))
1804             (cond
1805              ;; Hide empty headers.
1806              ((eq elem 'empty)
1807               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1808                 (forward-line -1)
1809                 (gnus-article-hide-text-type
1810                  (point-at-bol)
1811                  (progn
1812                    (end-of-line)
1813                    (if (re-search-forward "^[^ \t]" nil t)
1814                        (match-beginning 0)
1815                      (point-max)))
1816                  'boring-headers)))
1817              ;; Hide boring Newsgroups header.
1818              ((eq elem 'newsgroups)
1819               (when (gnus-string-equal
1820                      (gnus-fetch-field "newsgroups")
1821                      (gnus-group-real-name
1822                       (if (boundp 'gnus-newsgroup-name)
1823                           gnus-newsgroup-name
1824                         "")))
1825                 (gnus-article-hide-header "newsgroups")))
1826              ((eq elem 'to-address)
1827               (let ((to (message-fetch-field "to"))
1828                     (to-address
1829                      (gnus-parameter-to-address
1830                       (if (boundp 'gnus-newsgroup-name)
1831                           gnus-newsgroup-name ""))))
1832                 (when (and to to-address
1833                            (ignore-errors
1834                              (gnus-string-equal
1835                               ;; only one address in To
1836                               (nth 1 (mail-extract-address-components to))
1837                               to-address)))
1838                   (gnus-article-hide-header "to"))))
1839              ((eq elem 'to-list)
1840               (let ((to (message-fetch-field "to"))
1841                     (to-list
1842                      (gnus-parameter-to-list
1843                       (if (boundp 'gnus-newsgroup-name)
1844                           gnus-newsgroup-name ""))))
1845                 (when (and to to-list
1846                            (ignore-errors
1847                              (gnus-string-equal
1848                               ;; only one address in To
1849                               (nth 1 (mail-extract-address-components to))
1850                               to-list)))
1851                   (gnus-article-hide-header "to"))))
1852              ((eq elem 'cc-list)
1853               (let ((cc (message-fetch-field "cc"))
1854                     (to-list
1855                      (gnus-parameter-to-list
1856                       (if (boundp 'gnus-newsgroup-name)
1857                           gnus-newsgroup-name ""))))
1858                 (when (and cc to-list
1859                            (ignore-errors
1860                              (gnus-string-equal
1861                               ;; only one address in CC
1862                               (nth 1 (mail-extract-address-components cc))
1863                               to-list)))
1864                   (gnus-article-hide-header "cc"))))
1865              ((eq elem 'followup-to)
1866               (when (gnus-string-equal
1867                      (message-fetch-field "followup-to")
1868                      (message-fetch-field "newsgroups"))
1869                 (gnus-article-hide-header "followup-to")))
1870              ((eq elem 'reply-to)
1871               (if (gnus-group-find-parameter
1872                    gnus-newsgroup-name 'broken-reply-to)
1873                   (gnus-article-hide-header "reply-to")
1874                 (let ((from (message-fetch-field "from"))
1875                       (reply-to (message-fetch-field "reply-to")))
1876                   (when
1877                       (and
1878                        from reply-to
1879                        (ignore-errors
1880                          (equal
1881                           (sort (mapcar
1882                                  (lambda (x) (downcase (cadr x)))
1883                                  (mail-extract-address-components from t))
1884                                 'string<)
1885                           (sort (mapcar
1886                                  (lambda (x) (downcase (cadr x)))
1887                                  (mail-extract-address-components reply-to t))
1888                                 'string<))))
1889                     (gnus-article-hide-header "reply-to")))))
1890              ((eq elem 'date)
1891               (let ((date (message-fetch-field "date")))
1892                 (when (and date
1893                            (< (days-between (current-time-string) date)
1894                               4))
1895                   (gnus-article-hide-header "date"))))
1896              ((eq elem 'long-to)
1897               (let ((to (message-fetch-field "to"))
1898                     (cc (message-fetch-field "cc")))
1899                 (when (> (length to) 1024)
1900                   (gnus-article-hide-header "to"))
1901                 (when (> (length cc) 1024)
1902                   (gnus-article-hide-header "cc"))))
1903              ((eq elem 'many-to)
1904               (let ((to-count 0)
1905                     (cc-count 0))
1906                 (goto-char (point-min))
1907                 (while (re-search-forward "^to:" nil t)
1908                   (setq to-count (1+ to-count)))
1909                 (when (> to-count 1)
1910                   (while (> to-count 0)
1911                     (goto-char (point-min))
1912                     (save-restriction
1913                       (re-search-forward "^to:" nil nil to-count)
1914                       (forward-line -1)
1915                       (narrow-to-region (point) (point-max))
1916                       (gnus-article-hide-header "to"))
1917                     (setq to-count (1- to-count))))
1918                 (goto-char (point-min))
1919                 (while (re-search-forward "^cc:" nil t)
1920                   (setq cc-count (1+ cc-count)))
1921                 (when (> cc-count 1)
1922                   (while (> cc-count 0)
1923                     (goto-char (point-min))
1924                     (save-restriction
1925                       (re-search-forward "^cc:" nil nil cc-count)
1926                       (forward-line -1)
1927                       (narrow-to-region (point) (point-max))
1928                       (gnus-article-hide-header "cc"))
1929                     (setq cc-count (1- cc-count)))))))))))))
1930
1931 (defun gnus-article-hide-header (header)
1932   (save-excursion
1933     (goto-char (point-min))
1934     (when (re-search-forward (concat "^" header ":") nil t)
1935       (gnus-article-hide-text-type
1936        (point-at-bol)
1937        (progn
1938          (end-of-line)
1939          (if (re-search-forward "^[^ \t]" nil t)
1940              (match-beginning 0)
1941            (point-max)))
1942        'boring-headers))))
1943
1944 (defvar gnus-article-normalized-header-length 40
1945   "Length of normalized headers.")
1946
1947 (defun article-normalize-headers ()
1948   "Make all header lines 40 characters long."
1949   (interactive)
1950   (let ((inhibit-read-only t)
1951         column)
1952     (save-excursion
1953       (save-restriction
1954         (article-narrow-to-head)
1955         (while (not (eobp))
1956           (cond
1957            ((< (setq column (- (point-at-eol) (point)))
1958                gnus-article-normalized-header-length)
1959             (end-of-line)
1960             (insert (make-string
1961                      (- gnus-article-normalized-header-length column)
1962                      ? )))
1963            ((> column gnus-article-normalized-header-length)
1964             (gnus-put-text-property
1965              (progn
1966                (forward-char gnus-article-normalized-header-length)
1967                (point))
1968              (point-at-eol)
1969              'invisible t))
1970            (t
1971             ;; Do nothing.
1972             ))
1973           (forward-line 1))))))
1974
1975 (defun article-treat-dumbquotes ()
1976   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
1977 Note that this function guesses whether a character is a sm*rtq**t* or
1978 not, so it should only be used interactively.
1979
1980 Sm*rtq**t*s are M****s***'s unilateral extension to the
1981 iso-8859-1 character map in an attempt to provide more quoting
1982 characters.  If you see something like \\222 or \\264 where
1983 you're expecting some kind of apostrophe or quotation mark, then
1984 try this wash."
1985   (interactive)
1986   (article-translate-strings gnus-article-dumbquotes-map))
1987
1988 (defun article-translate-characters (from to)
1989   "Translate all characters in the body of the article according to FROM and TO.
1990 FROM is a string of characters to translate from; to is a string of
1991 characters to translate to."
1992   (save-excursion
1993     (when (article-goto-body)
1994       (let ((inhibit-read-only t)
1995             (x (make-string 225 ?x))
1996             (i -1))
1997         (while (< (incf i) (length x))
1998           (aset x i i))
1999         (setq i 0)
2000         (while (< i (length from))
2001           (aset x (aref from i) (aref to i))
2002           (incf i))
2003         (translate-region (point) (point-max) x)))))
2004
2005 (defun article-translate-strings (map)
2006   "Translate all string in the body of the article according to MAP.
2007 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2008   (save-excursion
2009     (when (article-goto-body)
2010       (let ((inhibit-read-only t))
2011         (dolist (elem map)
2012           (save-excursion
2013             (while (search-forward (car elem) nil t)
2014               (replace-match (cadr elem)))))))))
2015
2016 (defun article-treat-overstrike ()
2017   "Translate overstrikes into bold text."
2018   (interactive)
2019   (save-excursion
2020     (when (article-goto-body)
2021       (let ((inhibit-read-only t))
2022         (while (search-forward "\b" nil t)
2023           (let ((next (char-after))
2024                 (previous (char-after (- (point) 2))))
2025             ;; We do the boldification/underlining by hiding the
2026             ;; overstrikes and putting the proper text property
2027             ;; on the letters.
2028             (cond
2029              ((eq next previous)
2030               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2031               (put-text-property (point) (1+ (point)) 'face 'bold))
2032              ((eq next ?_)
2033               (gnus-article-hide-text-type
2034                (1- (point)) (1+ (point)) 'overstrike)
2035               (put-text-property
2036                (- (point) 2) (1- (point)) 'face 'underline))
2037              ((eq previous ?_)
2038               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2039               (put-text-property
2040                (point) (1+ (point)) 'face 'underline)))))))))
2041
2042 (defun article-treat-ansi-sequences ()
2043   "Translate ANSI SGR control sequences into overlays or extents."
2044   (interactive)
2045   (save-excursion
2046     (when (article-goto-body)
2047       (let ((inhibit-read-only t))
2048         (ansi-color-apply-on-region (point) (point-max))))))
2049
2050 (defun gnus-article-treat-unfold-headers ()
2051   "Unfold folded message headers.
2052 Only the headers that fit into the current window width will be
2053 unfolded."
2054   (interactive)
2055   (gnus-with-article-headers
2056     (let (length)
2057       (while (not (eobp))
2058         (save-restriction
2059           (mail-header-narrow-to-field)
2060           (let ((header (buffer-string)))
2061             (with-temp-buffer
2062               (insert header)
2063               (goto-char (point-min))
2064               (while (re-search-forward "\n[\t ]" nil t)
2065                 (replace-match " " t t)))
2066             (setq length (- (point-max) (point-min) 1)))
2067           (when (< length (window-width))
2068             (while (re-search-forward "\n[\t ]" nil t)
2069               (replace-match " " t t)))
2070           (goto-char (point-max)))))))
2071
2072 (defun gnus-article-treat-fold-headers ()
2073   "Fold message headers."
2074   (interactive)
2075   (gnus-with-article-headers
2076     (while (not (eobp))
2077       (save-restriction
2078         (mail-header-narrow-to-field)
2079         (mail-header-fold-field)
2080         (goto-char (point-max))))))
2081
2082 (defun gnus-treat-smiley ()
2083   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2084   (interactive)
2085   (gnus-with-article-buffer
2086     (if (memq 'smiley gnus-article-wash-types)
2087         (gnus-delete-images 'smiley)
2088       (article-goto-body)
2089       (let ((images (smiley-region (point) (point-max))))
2090         (when images
2091           (gnus-add-wash-type 'smiley)
2092           (dolist (image images)
2093             (gnus-add-image 'smiley image)))))))
2094
2095 (defun gnus-article-remove-images ()
2096   "Remove all images from the article buffer."
2097   (interactive)
2098   (gnus-with-article-buffer
2099     (dolist (elem gnus-article-image-alist)
2100       (gnus-delete-images (car elem)))))
2101
2102 (defun gnus-article-treat-fold-newsgroups ()
2103   "Unfold folded message headers.
2104 Only the headers that fit into the current window width will be
2105 unfolded."
2106   (interactive)
2107   (gnus-with-article-headers
2108     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2109       (save-restriction
2110         (mail-header-narrow-to-field)
2111         (while (re-search-forward ", *" nil t)
2112           (replace-match ", " t t))
2113         (mail-header-fold-field)
2114         (goto-char (point-max))))))
2115
2116 (defun gnus-article-treat-body-boundary ()
2117   "Place a boundary line at the end of the headers."
2118   (interactive)
2119   (when (and gnus-body-boundary-delimiter
2120              (> (length gnus-body-boundary-delimiter) 0))
2121     (gnus-with-article-headers
2122       (goto-char (point-max))
2123       (let ((start (point)))
2124         (insert "X-Boundary: ")
2125         (gnus-add-text-properties start (point) '(invisible t intangible t))
2126         (insert (let (str)
2127                   (while (>= (1- (window-width)) (length str))
2128                     (setq str (concat str gnus-body-boundary-delimiter)))
2129                   (substring str 0 (1- (window-width))))
2130                 "\n")
2131         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2132
2133 (defun article-fill-long-lines ()
2134   "Fill lines that are wider than the window width."
2135   (interactive)
2136   (save-excursion
2137     (let ((inhibit-read-only t)
2138           (width (window-width (get-buffer-window (current-buffer)))))
2139       (save-restriction
2140         (article-goto-body)
2141         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2142           (while (not (eobp))
2143             (end-of-line)
2144             (when (>= (current-column) (min fill-column width))
2145               (narrow-to-region (min (1+ (point)) (point-max))
2146                                 (point-at-bol))
2147               (let ((goback (point-marker)))
2148                 (fill-paragraph nil)
2149                 (goto-char (marker-position goback)))
2150               (widen))
2151             (forward-line 1)))))))
2152
2153 (defun article-capitalize-sentences ()
2154   "Capitalize the first word in each sentence."
2155   (interactive)
2156   (save-excursion
2157     (let ((inhibit-read-only t)
2158           (paragraph-start "^[\n\^L]"))
2159       (article-goto-body)
2160       (while (not (eobp))
2161         (capitalize-word 1)
2162         (forward-sentence)))))
2163
2164 (defun article-remove-cr ()
2165   "Remove trailing CRs and then translate remaining CRs into LFs."
2166   (interactive)
2167   (save-excursion
2168     (let ((inhibit-read-only t))
2169       (goto-char (point-min))
2170       (while (re-search-forward "\r+$" nil t)
2171         (replace-match "" t t))
2172       (goto-char (point-min))
2173       (while (search-forward "\r" nil t)
2174         (replace-match "\n" t t)))))
2175
2176 (defun article-remove-trailing-blank-lines ()
2177   "Remove all trailing blank lines from the article."
2178   (interactive)
2179   (save-excursion
2180     (let ((inhibit-read-only t))
2181       (goto-char (point-max))
2182       (delete-region
2183        (point)
2184        (progn
2185          (while (and (not (bobp))
2186                      (looking-at "^[ \t]*$")
2187                      (not (gnus-annotation-in-region-p
2188                            (point) (point-at-eol))))
2189            (forward-line -1))
2190          (forward-line 1)
2191          (point))))))
2192
2193 (eval-when-compile
2194   (defvar gnus-face-properties-alist))
2195
2196 (defun article-display-face ()
2197   "Display any Face headers in the header."
2198   (interactive)
2199   (let ((wash-face-p buffer-read-only))
2200     (gnus-with-article-headers
2201       ;; When displaying parts, this function can be called several times on
2202       ;; the same article, without any intended toggle semantic (as typing `W
2203       ;; D d' would have). So face deletion must occur only when we come from
2204       ;; an interactive command, that is when the *Article* buffer is
2205       ;; read-only.
2206       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2207           (gnus-delete-images 'face)
2208         (let (face faces from)
2209           (save-current-buffer
2210             (when (and wash-face-p
2211                        (gnus-buffer-live-p gnus-original-article-buffer)
2212                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2213               (set-buffer gnus-original-article-buffer))
2214             (save-restriction
2215               (mail-narrow-to-head)
2216               (while (gnus-article-goto-header "Face")
2217                 (push (mail-header-field-value) faces))))
2218           (when faces
2219             (goto-char (point-min))
2220             (let ((from (gnus-article-goto-header "from"))
2221                   png image)
2222               (unless from
2223                 (insert "From:")
2224                 (setq from (point))
2225                 (insert "[no `from' set]\n"))
2226               (while faces
2227                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2228                   (setq image
2229                         (apply 'gnus-create-image png 'png t
2230                                (cdr (assq 'png gnus-face-properties-alist))))
2231                   (goto-char from)
2232                   (gnus-add-wash-type 'face)
2233                   (gnus-add-image 'face image)
2234                   (gnus-put-image image nil 'face))))))))))
2235
2236 (defun article-display-x-face (&optional force)
2237   "Look for an X-Face header and display it if present."
2238   (interactive (list 'force))
2239   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2240     (gnus-with-article-headers
2241       ;; Delete the old process, if any.
2242       (when (process-status "article-x-face")
2243         (delete-process "article-x-face"))
2244       ;; See the comment in `article-display-face'.
2245       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2246           ;; We have already displayed X-Faces, so we remove them
2247           ;; instead.
2248           (gnus-delete-images 'xface)
2249         ;; Display X-Faces.
2250         (let (x-faces from face)
2251           (save-current-buffer
2252             (when (and wash-face-p
2253                        (gnus-buffer-live-p gnus-original-article-buffer)
2254                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2255               ;; If type `W f', use gnus-original-article-buffer,
2256               ;; otherwise use the current buffer because displaying
2257               ;; RFC822 parts calls this function too.
2258               (set-buffer gnus-original-article-buffer))
2259             (save-restriction
2260               (mail-narrow-to-head)
2261               (while (gnus-article-goto-header "X-Face")
2262                 (push (mail-header-field-value) x-faces))
2263               (setq from (message-fetch-field "from"))))
2264           ;; Sending multiple EOFs to xv doesn't work, so we only do a
2265           ;; single external face.
2266           (when (stringp gnus-article-x-face-command)
2267             (setq x-faces (list (car x-faces))))
2268           (when (and x-faces
2269                      gnus-article-x-face-command
2270                      (or force
2271                          ;; Check whether this face is censored.
2272                          (not gnus-article-x-face-too-ugly)
2273                          (and from
2274                               (not (string-match gnus-article-x-face-too-ugly
2275                                                  from)))))
2276             (while (setq face (pop x-faces))
2277               ;; We display the face.
2278               (cond ((stringp gnus-article-x-face-command)
2279                      ;; The command is a string, so we interpret the command
2280                      ;; as a, well, command, and fork it off.
2281                      (let ((process-connection-type nil))
2282                        (gnus-set-process-query-on-exit-flag
2283                         (start-process
2284                          "article-x-face" nil shell-file-name
2285                          shell-command-switch gnus-article-x-face-command)
2286                         nil)
2287                        (with-temp-buffer
2288                          (insert face)
2289                          (process-send-region "article-x-face"
2290                                               (point-min) (point-max)))
2291                        (process-send-eof "article-x-face")))
2292                     ((functionp gnus-article-x-face-command)
2293                      ;; The command is a lisp function, so we call it.
2294                      (funcall gnus-article-x-face-command face))
2295                     (t
2296                      (error "%s is not a function"
2297                             gnus-article-x-face-command))))))))))
2298
2299 (defun article-decode-mime-words ()
2300   "Decode all MIME-encoded words in the article."
2301   (interactive)
2302   (gnus-with-article-buffer
2303     (let ((inhibit-point-motion-hooks t)
2304           (mail-parse-charset gnus-newsgroup-charset)
2305           (mail-parse-ignored-charsets
2306            (with-current-buffer gnus-summary-buffer
2307              gnus-newsgroup-ignored-charsets)))
2308       (mail-decode-encoded-word-region (point-min) (point-max)))))
2309
2310 (defun article-decode-charset (&optional prompt)
2311   "Decode charset-encoded text in the article.
2312 If PROMPT (the prefix), prompt for a coding system to use."
2313   (interactive "P")
2314   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2315         (inhibit-read-only t)
2316         (mail-parse-charset gnus-newsgroup-charset)
2317         (mail-parse-ignored-charsets
2318          (save-excursion (condition-case nil
2319                              (set-buffer gnus-summary-buffer)
2320                            (error))
2321                          gnus-newsgroup-ignored-charsets))
2322         ct cte ctl charset format)
2323     (save-excursion
2324       (save-restriction
2325         (article-narrow-to-head)
2326         (setq ct (message-fetch-field "Content-Type" t)
2327               cte (message-fetch-field "Content-Transfer-Encoding" t)
2328               ctl (and ct (mail-header-parse-content-type ct))
2329               charset (cond
2330                        (prompt
2331                         (mm-read-coding-system "Charset to decode: "))
2332                        (ctl
2333                         (mail-content-type-get ctl 'charset)))
2334               format (and ctl (mail-content-type-get ctl 'format)))
2335         (when cte
2336           (setq cte (mail-header-strip cte)))
2337         (if (and ctl (not (string-match "/" (car ctl))))
2338             (setq ctl nil))
2339         (goto-char (point-max)))
2340       (forward-line 1)
2341       (save-restriction
2342         (narrow-to-region (point) (point-max))
2343         (when (and (eq mail-parse-charset 'gnus-decoded)
2344                    (eq (mm-body-7-or-8) '8bit))
2345           ;; The text code could have been decoded.
2346           (setq charset mail-parse-charset))
2347         (when (and (or (not ctl)
2348                        (equal (car ctl) "text/plain"))
2349                    (not format)) ;; article with format will decode later.
2350           (mm-decode-body
2351            charset (and cte (intern (downcase
2352                                      (gnus-strip-whitespace cte))))
2353            (car ctl)))))))
2354
2355 (defun article-decode-encoded-words ()
2356   "Remove encoded-word encoding from headers."
2357   (let ((inhibit-point-motion-hooks t)
2358         (mail-parse-charset gnus-newsgroup-charset)
2359         (mail-parse-ignored-charsets
2360          (save-excursion (condition-case nil
2361                              (set-buffer gnus-summary-buffer)
2362                            (error))
2363                          gnus-newsgroup-ignored-charsets))
2364         (inhibit-read-only t))
2365     (save-restriction
2366       (article-narrow-to-head)
2367       (funcall gnus-decode-header-function (point-min) (point-max)))))
2368
2369 (defun article-decode-group-name ()
2370   "Decode group names in `Newsgroups:'."
2371   (let ((inhibit-point-motion-hooks t)
2372         (inhibit-read-only t)
2373         (method (gnus-find-method-for-group gnus-newsgroup-name)))
2374     (when (and (or gnus-group-name-charset-method-alist
2375                    gnus-group-name-charset-group-alist)
2376                (gnus-buffer-live-p gnus-original-article-buffer))
2377       (save-restriction
2378         (article-narrow-to-head)
2379         (with-current-buffer gnus-original-article-buffer
2380           (goto-char (point-min)))
2381         (while (re-search-forward
2382                 "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2383           (replace-match (save-match-data
2384                            (gnus-decode-newsgroups
2385                             ;; XXX how to use data in article buffer?
2386                             (with-current-buffer gnus-original-article-buffer
2387                               (re-search-forward
2388                                "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2389                                nil t)
2390                               (match-string 1))
2391                             gnus-newsgroup-name method))
2392                          t t nil 1))
2393         (goto-char (point-min))
2394         (with-current-buffer gnus-original-article-buffer
2395           (goto-char (point-min)))
2396         (while (re-search-forward
2397                 "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2398           (replace-match (save-match-data
2399                            (gnus-decode-newsgroups
2400                             ;; XXX how to use data in article buffer?
2401                             (with-current-buffer gnus-original-article-buffer
2402                               (re-search-forward
2403                                "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2404                                nil t)
2405                               (match-string 1))
2406                             gnus-newsgroup-name method))
2407                          t t nil 1))))))
2408
2409 (autoload 'idna-to-unicode "idna")
2410
2411 (defun article-decode-idna-rhs ()
2412   "Decode IDNA strings in RHS in various headers in current buffer.
2413 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2414 Mail-Reply-To: and Mail-Followup-To:."
2415   (when gnus-use-idna
2416     (save-restriction
2417       (let ((inhibit-point-motion-hooks t)
2418             (inhibit-read-only t))
2419         (article-narrow-to-head)
2420         (goto-char (point-min))
2421         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2422           (let (ace unicode)
2423             (when (save-match-data
2424                     (and (setq ace (match-string 1))
2425                          (save-excursion
2426                            (and (re-search-backward "^[^ \t]" nil t)
2427                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2428                          (setq unicode (idna-to-unicode ace))))
2429               (unless (string= ace unicode)
2430                 (replace-match unicode nil nil nil 1)))))))))
2431
2432 (defun article-de-quoted-unreadable (&optional force read-charset)
2433   "Translate a quoted-printable-encoded article.
2434 If FORCE, decode the article whether it is marked as quoted-printable
2435 or not.
2436 If READ-CHARSET, ask for a coding system."
2437   (interactive (list 'force current-prefix-arg))
2438   (save-excursion
2439     (let ((inhibit-read-only t) type charset)
2440       (if (gnus-buffer-live-p gnus-original-article-buffer)
2441           (with-current-buffer gnus-original-article-buffer
2442             (setq type
2443                   (gnus-fetch-field "content-transfer-encoding"))
2444             (let* ((ct (gnus-fetch-field "content-type"))
2445                    (ctl (and ct (mail-header-parse-content-type ct))))
2446               (setq charset (and ctl
2447                                  (mail-content-type-get ctl 'charset)))
2448               (if (stringp charset)
2449                   (setq charset (intern (downcase charset)))))))
2450       (if read-charset
2451           (setq charset (mm-read-coding-system "Charset: " charset)))
2452       (unless charset
2453         (setq charset gnus-newsgroup-charset))
2454       (when (or force
2455                 (and type (let ((case-fold-search t))
2456                             (string-match "quoted-printable" type))))
2457         (article-goto-body)
2458         (quoted-printable-decode-region
2459          (point) (point-max) (mm-charset-to-coding-system charset))))))
2460
2461 (defun article-de-base64-unreadable (&optional force read-charset)
2462   "Translate a base64 article.
2463 If FORCE, decode the article whether it is marked as base64 not.
2464 If READ-CHARSET, ask for a coding system."
2465   (interactive (list 'force current-prefix-arg))
2466   (save-excursion
2467     (let ((inhibit-read-only t) type charset)
2468       (if (gnus-buffer-live-p gnus-original-article-buffer)
2469           (with-current-buffer gnus-original-article-buffer
2470             (setq type
2471                   (gnus-fetch-field "content-transfer-encoding"))
2472             (let* ((ct (gnus-fetch-field "content-type"))
2473                    (ctl (and ct (mail-header-parse-content-type ct))))
2474               (setq charset (and ctl
2475                                  (mail-content-type-get ctl 'charset)))
2476               (if (stringp charset)
2477                   (setq charset (intern (downcase charset)))))))
2478       (if read-charset
2479           (setq charset (mm-read-coding-system "Charset: " charset)))
2480       (unless charset
2481         (setq charset gnus-newsgroup-charset))
2482       (when (or force
2483                 (and type (let ((case-fold-search t))
2484                             (string-match "base64" type))))
2485         (article-goto-body)
2486         (save-restriction
2487           (narrow-to-region (point) (point-max))
2488           (base64-decode-region (point-min) (point-max))
2489           (mm-decode-coding-region
2490            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2491
2492 (eval-when-compile
2493   (require 'rfc1843))
2494
2495 (defun article-decode-HZ ()
2496   "Translate a HZ-encoded article."
2497   (interactive)
2498   (require 'rfc1843)
2499   (save-excursion
2500     (let ((inhibit-read-only t))
2501       (rfc1843-decode-region (point-min) (point-max)))))
2502
2503 (defun article-unsplit-urls ()
2504   "Remove the newlines that some other mailers insert into URLs."
2505   (interactive)
2506   (save-excursion
2507     (let ((inhibit-read-only t))
2508       (goto-char (point-min))
2509       (while (re-search-forward
2510               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2511         (replace-match "\\1\\3" t)))
2512     (when (interactive-p)
2513       (gnus-treat-article nil))))
2514
2515
2516 (defun article-wash-html (&optional read-charset)
2517   "Format an HTML article.
2518 If READ-CHARSET, ask for a coding system.  If it is a number, the
2519 charset defined in `gnus-summary-show-article-charset-alist' is used."
2520   (interactive "P")
2521   (save-excursion
2522     (let ((inhibit-read-only t)
2523           charset)
2524       (if read-charset
2525           (if (or (and (numberp read-charset)
2526                        (setq charset
2527                              (cdr
2528                               (assq read-charset
2529                                     gnus-summary-show-article-charset-alist))))
2530                   (setq charset (mm-read-coding-system "Charset: ")))
2531               (let ((gnus-summary-show-article-charset-alist
2532                      (list (cons 1 charset))))
2533                 (with-current-buffer gnus-summary-buffer
2534                   (gnus-summary-show-article 1)))
2535             (error "No charset is given"))
2536         (when (gnus-buffer-live-p gnus-original-article-buffer)
2537           (with-current-buffer gnus-original-article-buffer
2538             (let* ((ct (gnus-fetch-field "content-type"))
2539                    (ctl (and ct (mail-header-parse-content-type ct))))
2540               (setq charset (and ctl
2541                                  (mail-content-type-get ctl 'charset)))
2542               (when (stringp charset)
2543                 (setq charset (intern (downcase charset)))))))
2544         (unless charset
2545           (setq charset gnus-newsgroup-charset)))
2546       (article-goto-body)
2547       (save-window-excursion
2548         (save-restriction
2549           (narrow-to-region (point) (point-max))
2550           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2551                  (entry (assq func mm-text-html-washer-alist)))
2552             (when entry
2553               (setq func (cdr entry)))
2554             (cond
2555              ((functionp func)
2556               (funcall func))
2557              (t
2558               (apply (car func) (cdr func))))))))))
2559
2560 (defun gnus-article-wash-html-with-w3 ()
2561   "Wash the current buffer with w3."
2562   (mm-setup-w3)
2563   (let ((w3-strict-width (window-width))
2564         (url-standalone-mode t)
2565         (url-gateway-unplugged t)
2566         (w3-honor-stylesheets nil))
2567     (condition-case ()
2568         (w3-region (point-min) (point-max))
2569       (error))))
2570
2571 (defun gnus-article-wash-html-with-w3m ()
2572   "Wash the current buffer with emacs-w3m."
2573   (mm-setup-w3m)
2574   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2575         w3m-force-redisplay)
2576     (w3m-region (point-min) (point-max)))
2577   (when (and mm-inline-text-html-with-w3m-keymap
2578              (boundp 'w3m-minor-mode-map)
2579              w3m-minor-mode-map)
2580     (add-text-properties
2581      (point-min) (point-max)
2582      (list 'keymap w3m-minor-mode-map
2583            ;; Put the mark meaning this part was rendered by emacs-w3m.
2584            'mm-inline-text-html-with-w3m t))))
2585
2586 (eval-when-compile (defvar charset)) ;; Bound by `article-wash-html'.
2587
2588 (defun gnus-article-wash-html-with-w3m-standalone ()
2589   "Wash the current buffer with w3m."
2590   (if (mm-w3m-standalone-supports-m17n-p)
2591       (progn
2592         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2593           ;; The default.
2594           (setq charset 'iso-8859-1))
2595         (let ((coding-system-for-write charset)
2596               (coding-system-for-read charset))
2597           (call-process-region
2598            (point-min) (point-max)
2599            "w3m" t t nil "-dump" "-T" "text/html"
2600            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2601     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2602
2603 (defun article-hide-list-identifiers ()
2604   "Remove list identifies from the Subject header.
2605 The `gnus-list-identifiers' variable specifies what to do."
2606   (interactive)
2607   (let ((inhibit-point-motion-hooks t)
2608         (regexp (if (consp gnus-list-identifiers)
2609                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2610                   gnus-list-identifiers))
2611         (inhibit-read-only t))
2612     (when regexp
2613       (save-excursion
2614         (save-restriction
2615           (article-narrow-to-head)
2616           (goto-char (point-min))
2617           (while (re-search-forward
2618                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2619                   nil t)
2620             (delete-region (match-beginning 2) (match-end 0))
2621             (beginning-of-line))
2622           (when (re-search-forward
2623                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2624             (delete-region (match-beginning 1) (match-end 1))))))))
2625
2626 (defun article-hide-pem (&optional arg)
2627   "Toggle hiding of any PEM headers and signatures in the current article.
2628 If given a negative prefix, always show; if given a positive prefix,
2629 always hide."
2630   (interactive (gnus-article-hidden-arg))
2631   (unless (gnus-article-check-hidden-text 'pem arg)
2632     (save-excursion
2633       (let ((inhibit-read-only t) end)
2634         (goto-char (point-min))
2635         ;; Hide the horrendously ugly "header".
2636         (when (and (search-forward
2637                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2638                     nil t)
2639                    (setq end (1+ (match-beginning 0))))
2640           (gnus-add-wash-type 'pem)
2641           (gnus-article-hide-text-type
2642            end
2643            (if (search-forward "\n\n" nil t)
2644                (match-end 0)
2645              (point-max))
2646            'pem)
2647           ;; Hide the trailer as well
2648           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2649                                 nil t)
2650             (gnus-article-hide-text-type
2651              (match-beginning 0) (match-end 0) 'pem)))))))
2652
2653 (defun article-strip-banner ()
2654   "Strip the banners specified by the `banner' group parameter and by
2655 `gnus-article-address-banner-alist'."
2656   (interactive)
2657   (save-excursion
2658     (save-restriction
2659       (let ((inhibit-point-motion-hooks t))
2660         (when (gnus-parameter-banner gnus-newsgroup-name)
2661           (article-really-strip-banner
2662            (gnus-parameter-banner gnus-newsgroup-name)))
2663         (when gnus-article-address-banner-alist
2664           ;; It is necessary to encode from fields before checking,
2665           ;; because `mail-header-parse-addresses' does not work
2666           ;; (reliably) on decoded headers.  And more, it is
2667           ;; impossible to use `gnus-fetch-original-field' here,
2668           ;; because `article-strip-banner' may be called in draft
2669           ;; buffers to preview them.
2670           (let ((from (save-restriction
2671                         (widen)
2672                         (article-narrow-to-head)
2673                         (mail-fetch-field "from"))))
2674             (when (and from
2675                        (setq from
2676                              (caar (mail-header-parse-addresses
2677                                     (mail-encode-encoded-word-string from)))))
2678               (catch 'found
2679                 (dolist (pair gnus-article-address-banner-alist)
2680                   (when (string-match (car pair) from)
2681                     (throw 'found
2682                            (article-really-strip-banner (cdr pair)))))))))))))
2683
2684 (defun article-really-strip-banner (banner)
2685   "Strip the banner specified by the argument."
2686   (save-excursion
2687     (save-restriction
2688       (let ((inhibit-point-motion-hooks t)
2689             (gnus-signature-limit nil)
2690             (inhibit-read-only t))
2691         (article-goto-body)
2692         (cond
2693          ((eq banner 'signature)
2694           (when (gnus-article-narrow-to-signature)
2695             (widen)
2696             (forward-line -1)
2697             (delete-region (point) (point-max))))
2698          ((symbolp banner)
2699           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2700               (while (re-search-forward banner nil t)
2701                 (delete-region (match-beginning 0) (match-end 0)))))
2702          ((stringp banner)
2703           (while (re-search-forward banner nil t)
2704             (delete-region (match-beginning 0) (match-end 0)))))))))
2705
2706 (defun article-babel ()
2707   "Translate article using an online translation service."
2708   (interactive)
2709   (require 'babel)
2710   (gnus-with-article-buffer
2711     (when (article-goto-body)
2712       (let* ((start (point))
2713              (end (point-max))
2714              (orig (buffer-substring start end))
2715              (trans (babel-as-string orig)))
2716         (save-restriction
2717           (narrow-to-region start end)
2718           (delete-region start end)
2719           (insert trans))))))
2720
2721 (defun article-hide-signature (&optional arg)
2722   "Hide the signature in the current article.
2723 If given a negative prefix, always show; if given a positive prefix,
2724 always hide."
2725   (interactive (gnus-article-hidden-arg))
2726   (unless (gnus-article-check-hidden-text 'signature arg)
2727     (save-excursion
2728       (save-restriction
2729         (let ((inhibit-read-only t))
2730           (when (gnus-article-narrow-to-signature)
2731             (gnus-article-hide-text-type
2732              (point-min) (point-max) 'signature))))))
2733   (gnus-set-mode-line 'article))
2734
2735 (defun article-strip-headers-in-body ()
2736   "Strip offensive headers from bodies."
2737   (interactive)
2738   (save-excursion
2739     (article-goto-body)
2740     (let ((case-fold-search t))
2741       (when (looking-at "x-no-archive:")
2742         (gnus-delete-line)))))
2743
2744 (defun article-strip-leading-blank-lines ()
2745   "Remove all blank lines from the beginning of the article."
2746   (interactive)
2747   (save-excursion
2748     (let ((inhibit-point-motion-hooks t)
2749           (inhibit-read-only t))
2750       (when (article-goto-body)
2751         (while (and (not (eobp))
2752                     (looking-at "[ \t]*$"))
2753           (gnus-delete-line))))))
2754
2755 (defun article-narrow-to-head ()
2756   "Narrow the buffer to the head of the message.
2757 Point is left at the beginning of the narrowed-to region."
2758   (narrow-to-region
2759    (goto-char (point-min))
2760    (if (search-forward "\n\n" nil 1)
2761        (1- (point))
2762      (point-max)))
2763   (goto-char (point-min)))
2764
2765 (defun article-goto-body ()
2766   "Place point at the start of the body."
2767   (goto-char (point-min))
2768   (cond
2769    ;; This variable is only bound when dealing with separate
2770    ;; MIME body parts.
2771    (article-goto-body-goes-to-point-min-p
2772     t)
2773    ((search-forward "\n\n" nil t)
2774     t)
2775    (t
2776     (goto-char (point-max))
2777     nil)))
2778
2779 (defun article-strip-multiple-blank-lines ()
2780   "Replace consecutive blank lines with one empty line."
2781   (interactive)
2782   (save-excursion
2783     (let ((inhibit-point-motion-hooks t)
2784           (inhibit-read-only t))
2785       ;; First make all blank lines empty.
2786       (article-goto-body)
2787       (while (re-search-forward "^[ \t]+$" nil t)
2788         (unless (gnus-annotation-in-region-p
2789                  (match-beginning 0) (match-end 0))
2790           (replace-match "" nil t)))
2791       ;; Then replace multiple empty lines with a single empty line.
2792       (article-goto-body)
2793       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2794         (unless (gnus-annotation-in-region-p
2795                  (match-beginning 0) (match-end 0))
2796           (delete-region (match-beginning 1) (match-end 1)))))))
2797
2798 (defun article-strip-leading-space ()
2799   "Remove all white space from the beginning of the lines in the article."
2800   (interactive)
2801   (save-excursion
2802     (let ((inhibit-point-motion-hooks t)
2803           (inhibit-read-only t))
2804       (article-goto-body)
2805       (while (re-search-forward "^[ \t]+" nil t)
2806         (replace-match "" t t)))))
2807
2808 (defun article-strip-trailing-space ()
2809   "Remove all white space from the end of the lines in the article."
2810   (interactive)
2811   (save-excursion
2812     (let ((inhibit-point-motion-hooks t)
2813           (inhibit-read-only t))
2814       (article-goto-body)
2815       (while (re-search-forward "[ \t]+$" nil t)
2816         (replace-match "" t t)))))
2817
2818 (defun article-strip-blank-lines ()
2819   "Strip leading, trailing and multiple blank lines."
2820   (interactive)
2821   (article-strip-leading-blank-lines)
2822   (article-remove-trailing-blank-lines)
2823   (article-strip-multiple-blank-lines))
2824
2825 (defun article-strip-all-blank-lines ()
2826   "Strip all blank lines."
2827   (interactive)
2828   (save-excursion
2829     (let ((inhibit-point-motion-hooks t)
2830           (inhibit-read-only t))
2831       (article-goto-body)
2832       (while (re-search-forward "^[ \t]*\n" nil t)
2833         (replace-match "" t t)))))
2834
2835 (defun gnus-article-narrow-to-signature ()
2836   "Narrow to the signature; return t if a signature is found, else nil."
2837   (let ((inhibit-point-motion-hooks t))
2838     (when (gnus-article-search-signature)
2839       (forward-line 1)
2840       ;; Check whether we have some limits to what we consider
2841       ;; to be a signature.
2842       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2843                       (list gnus-signature-limit)))
2844             limit limited)
2845         (while (setq limit (pop limits))
2846           (if (or (and (integerp limit)
2847                        (< (- (point-max) (point)) limit))
2848                   (and (floatp limit)
2849                        (< (count-lines (point) (point-max)) limit))
2850                   (and (functionp limit)
2851                        (funcall limit))
2852                   (and (stringp limit)
2853                        (not (re-search-forward limit nil t))))
2854               ()                        ; This limit did not succeed.
2855             (setq limited t
2856                   limits nil)))
2857         (unless limited
2858           (narrow-to-region (point) (point-max))
2859           t)))))
2860
2861 (defun gnus-article-search-signature ()
2862   "Search the current buffer for the signature separator.
2863 Put point at the beginning of the signature separator."
2864   (let ((cur (point)))
2865     (goto-char (point-max))
2866     (if (if (stringp gnus-signature-separator)
2867             (re-search-backward gnus-signature-separator nil t)
2868           (let ((seps gnus-signature-separator))
2869             (while (and seps
2870                         (not (re-search-backward (car seps) nil t)))
2871               (pop seps))
2872             seps))
2873         t
2874       (goto-char cur)
2875       nil)))
2876
2877 (defun gnus-article-hidden-arg ()
2878   "Return the current prefix arg as a number, or 0 if no prefix."
2879   (list (if current-prefix-arg
2880             (prefix-numeric-value current-prefix-arg)
2881           0)))
2882
2883 (defun gnus-article-check-hidden-text (type arg)
2884   "Return nil if hiding is necessary.
2885 Arg can be nil or a number.  nil and positive means hide, negative
2886 means show, 0 means toggle."
2887   (save-excursion
2888     (save-restriction
2889       (let ((hide (gnus-article-hidden-text-p type)))
2890         (cond
2891          ((or (null arg)
2892               (> arg 0))
2893           nil)
2894          ((< arg 0)
2895           (gnus-article-show-hidden-text type)
2896           t)
2897          (t
2898           (if (eq hide 'hidden)
2899               (progn
2900                 (gnus-article-show-hidden-text type)
2901                 t)
2902             nil)))))))
2903
2904 (defun gnus-article-hidden-text-p (type)
2905   "Say whether the current buffer contains hidden text of type TYPE."
2906   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2907     (while (and pos
2908                 (not (get-text-property pos 'invisible))
2909                 (not (get-text-property pos 'dummy-invisible)))
2910       (setq pos
2911             (text-property-any (1+ pos) (point-max) 'article-type type)))
2912     (if pos
2913         'hidden
2914       nil)))
2915
2916 (defun gnus-article-show-hidden-text (type &optional dummy)
2917   "Show all hidden text of type TYPE.
2918 Originally it is hide instead of DUMMY."
2919   (let ((inhibit-read-only t)
2920         (inhibit-point-motion-hooks t))
2921     (gnus-remove-text-properties-when
2922      'article-type type
2923      (point-min) (point-max)
2924      (cons 'article-type (cons type
2925                                gnus-hidden-properties)))
2926     (gnus-delete-wash-type type)))
2927
2928 (defconst article-time-units
2929   `((year . ,(* 365.25 24 60 60))
2930     (week . ,(* 7 24 60 60))
2931     (day . ,(* 24 60 60))
2932     (hour . ,(* 60 60))
2933     (minute . 60)
2934     (second . 1))
2935   "Mapping from time units to seconds.")
2936
2937 (defun gnus-article-forward-header ()
2938   "Move point to the start of the next header.
2939 If the current header is a continuation header, this can be several
2940 lines forward."
2941   (let ((ended nil))
2942     (while (not ended)
2943       (forward-line 1)
2944       (if (looking-at "[ \t]+[^ \t]")
2945           (forward-line 1)
2946         (setq ended t)))))
2947
2948 (defun article-date-ut (&optional type highlight)
2949   "Convert DATE date to universal time in the current article.
2950 If TYPE is `local', convert to local time; if it is `lapsed', output
2951 how much time has lapsed since DATE.  For `lapsed', the value of
2952 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2953 should replace the \"Date:\" one, or should be added below it."
2954   (interactive (list 'ut t))
2955   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
2956          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
2957                              tdate-regexp)
2958                             ((eq type 'lapsed)
2959                              "^X-Sent:[ \t]")
2960                             (article-lapsed-timer
2961                              "^Date:[ \t]")
2962                             (t
2963                              tdate-regexp)))
2964          (case-fold-search t)
2965          (inhibit-read-only t)
2966          (inhibit-point-motion-hooks t)
2967          pos date bface eface)
2968     (save-excursion
2969       (save-restriction
2970         (widen)
2971         (goto-char (point-min))
2972         (while (or (setq date (get-text-property (setq pos (point))
2973                                                  'original-date))
2974                    (when (setq pos (next-single-property-change
2975                                     (point) 'original-date))
2976                      (setq date (get-text-property pos 'original-date))
2977                      t))
2978           (narrow-to-region pos (or (text-property-any pos (point-max)
2979                                                        'original-date nil)
2980                                     (point-max)))
2981           (goto-char (point-min))
2982           (when (re-search-forward tdate-regexp nil t)
2983             (setq bface (get-text-property (point-at-bol) 'face)
2984                   eface (get-text-property (1- (point-at-eol)) 'face)))
2985           (goto-char (point-min))
2986           (setq pos nil)
2987           ;; Delete any old Date headers.
2988           (while (re-search-forward date-regexp nil t)
2989             (if pos
2990                 (delete-region (point-at-bol) (progn
2991                                                 (gnus-article-forward-header)
2992                                                 (point)))
2993               (delete-region (point-at-bol) (progn
2994                                               (gnus-article-forward-header)
2995                                               (forward-char -1)
2996                                               (point)))
2997               (setq pos (point))))
2998           (when (and (not pos)
2999                      (re-search-forward tdate-regexp nil t))
3000             (forward-line 1))
3001           (gnus-goto-char pos)
3002           (insert (article-make-date-line date (or type 'ut)))
3003           (unless pos
3004             (insert "\n")
3005             (forward-line -1))
3006           ;; Do highlighting.
3007           (beginning-of-line)
3008           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3009             (put-text-property (match-beginning 1) (1+ (match-end 1))
3010                                'face bface)
3011             (put-text-property (match-beginning 2) (match-end 2)
3012                                'face eface))
3013           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3014           (goto-char (point-max))
3015           (widen))))))
3016
3017 (defun article-make-date-line (date type)
3018   "Return a DATE line of TYPE."
3019   (unless (memq type '(local ut original user iso8601 lapsed english))
3020     (error "Unknown conversion type: %s" type))
3021   (condition-case ()
3022       (let ((time (date-to-time date)))
3023         (cond
3024          ;; Convert to the local timezone.
3025          ((eq type 'local)
3026           (concat "Date: " (message-make-date time)))
3027          ;; Convert to Universal Time.
3028          ((eq type 'ut)
3029           (concat "Date: "
3030                   (substring
3031                    (message-make-date
3032                     (let* ((e (parse-time-string date))
3033                            (tm (apply 'encode-time e))
3034                            (ms (car tm))
3035                            (ls (- (cadr tm) (car (current-time-zone time)))))
3036                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3037                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3038                             (t (list ms ls)))))
3039                    0 -5)
3040                   "UT"))
3041          ;; Get the original date from the article.
3042          ((eq type 'original)
3043           (concat "Date: " (if (string-match "\n+$" date)
3044                                (substring date 0 (match-beginning 0))
3045                              date)))
3046          ;; Let the user define the format.
3047          ((eq type 'user)
3048           (let ((format (or (condition-case nil
3049                                 (with-current-buffer gnus-summary-buffer
3050                                   gnus-article-time-format)
3051                               (error nil))
3052                             gnus-article-time-format)))
3053             (if (functionp format)
3054                 (funcall format time)
3055               (concat "Date: " (format-time-string format time)))))
3056          ;; ISO 8601.
3057          ((eq type 'iso8601)
3058           (let ((tz (car (current-time-zone time))))
3059             (concat
3060              "Date: "
3061              (format-time-string "%Y%m%dT%H%M%S" time)
3062              (format "%s%02d%02d"
3063                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3064                      (/ (% (abs tz) 3600) 60)))))
3065          ;; Do an X-Sent lapsed format.
3066          ((eq type 'lapsed)
3067           ;; If the date is seriously mangled, the timezone functions are
3068           ;; liable to bug out, so we ignore all errors.
3069           (let* ((now (current-time))
3070                  (real-time (subtract-time now time))
3071                  (real-sec (and real-time
3072                                 (+ (* (float (car real-time)) 65536)
3073                                    (cadr real-time))))
3074                  (sec (and real-time (abs real-sec)))
3075                  num prev)
3076             (cond
3077              ((null real-time)
3078               "X-Sent: Unknown")
3079              ((zerop sec)
3080               "X-Sent: Now")
3081              (t
3082               (concat
3083                "X-Sent: "
3084                ;; This is a bit convoluted, but basically we go
3085                ;; through the time units for years, weeks, etc,
3086                ;; and divide things to see whether that results
3087                ;; in positive answers.
3088                (mapconcat
3089                 (lambda (unit)
3090                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3091                       ;; The (remaining) seconds are too few to
3092                       ;; be divided into this time unit.
3093                       ""
3094                     ;; It's big enough, so we output it.
3095                     (setq sec (- sec (* num (cdr unit))))
3096                     (prog1
3097                         (concat (if prev ", " "") (int-to-string
3098                                                    (floor num))
3099                                 " " (symbol-name (car unit))
3100                                 (if (> num 1) "s" ""))
3101                       (setq prev t))))
3102                 article-time-units "")
3103                ;; If dates are odd, then it might appear like the
3104                ;; article was sent in the future.
3105                (if (> real-sec 0)
3106                    " ago"
3107                  " in the future"))))))
3108          ;; Display the date in proper English
3109          ((eq type 'english)
3110           (let ((dtime (decode-time time)))
3111             (concat
3112              "Date: the "
3113              (number-to-string (nth 3 dtime))
3114              (let ((digit (% (nth 3 dtime) 10)))
3115                (cond
3116                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3117                 ((= digit 1) "st")
3118                 ((= digit 2) "nd")
3119                 ((= digit 3) "rd")
3120                 (t "th")))
3121              " of "
3122              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3123              " "
3124              (number-to-string (nth 5 dtime))
3125              " at "
3126              (format "%02d" (nth 2 dtime))
3127              ":"
3128              (format "%02d" (nth 1 dtime)))))))
3129     (error
3130      (format "Date: %s (from Gnus)" date))))
3131
3132 (defun article-date-local (&optional highlight)
3133   "Convert the current article date to the local timezone."
3134   (interactive (list t))
3135   (article-date-ut 'local highlight))
3136
3137 (defun article-date-english (&optional highlight)
3138   "Convert the current article date to something that is proper English."
3139   (interactive (list t))
3140   (article-date-ut 'english highlight))
3141
3142 (defun article-date-original (&optional highlight)
3143   "Convert the current article date to what it was originally.
3144 This is only useful if you have used some other date conversion
3145 function and want to see what the date was before converting."
3146   (interactive (list t))
3147   (article-date-ut 'original highlight))
3148
3149 (defun article-date-lapsed (&optional highlight)
3150   "Convert the current article date to time lapsed since it was sent."
3151   (interactive (list t))
3152   (article-date-ut 'lapsed highlight))
3153
3154 (defun article-update-date-lapsed ()
3155   "Function to be run from a timer to update the lapsed time line."
3156   (save-match-data
3157     (let (deactivate-mark)
3158       (save-excursion
3159         (ignore-errors
3160          (walk-windows
3161           (lambda (w)
3162             (set-buffer (window-buffer w))
3163             (when (eq major-mode 'gnus-article-mode)
3164               (let ((mark (point-marker)))
3165                 (goto-char (point-min))
3166                 (when (re-search-forward "^X-Sent:" nil t)
3167                   (article-date-lapsed t))
3168                 (goto-char (marker-position mark))
3169                 (move-marker mark nil))))
3170           nil 'visible))))))
3171
3172 (defun gnus-start-date-timer (&optional n)
3173   "Start a timer to update the X-Sent header in the article buffers.
3174 The numerical prefix says how frequently (in seconds) the function
3175 is to run."
3176   (interactive "p")
3177   (unless n
3178     (setq n 1))
3179   (gnus-stop-date-timer)
3180   (setq article-lapsed-timer
3181         (run-at-time 1 n 'article-update-date-lapsed)))
3182
3183 (defun gnus-stop-date-timer ()
3184   "Stop the X-Sent timer."
3185   (interactive)
3186   (when article-lapsed-timer
3187     (nnheader-cancel-timer article-lapsed-timer)
3188     (setq article-lapsed-timer nil)))
3189
3190 (defun article-date-user (&optional highlight)
3191   "Convert the current article date to the user-defined format.
3192 This format is defined by the `gnus-article-time-format' variable."
3193   (interactive (list t))
3194   (article-date-ut 'user highlight))
3195
3196 (defun article-date-iso8601 (&optional highlight)
3197   "Convert the current article date to ISO8601."
3198   (interactive (list t))
3199   (article-date-ut 'iso8601 highlight))
3200
3201 (defmacro gnus-article-save-original-date (&rest forms)
3202   "Save the original date as a text property and evaluate FORMS."
3203   `(let* ((case-fold-search t)
3204           (start (progn
3205                    (goto-char (point-min))
3206                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3207                               (not (bolp)))
3208                      (match-end 0))))
3209           (date (when (and start
3210                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3211                                               nil t))
3212                   (buffer-substring-no-properties start
3213                                                   (match-beginning 0)))))
3214      (goto-char (point-max))
3215      (skip-chars-backward "\n")
3216      (put-text-property (point-min) (point) 'original-date date)
3217      ,@forms
3218      (goto-char (point-max))
3219      (skip-chars-backward "\n")
3220      (put-text-property (point-min) (point) 'original-date date)))
3221
3222 ;; (defun article-show-all ()
3223 ;;   "Show all hidden text in the article buffer."
3224 ;;   (interactive)
3225 ;;   (save-excursion
3226 ;;     (let ((inhibit-read-only t))
3227 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3228
3229 (defun article-remove-leading-whitespace ()
3230   "Remove excessive whitespace from all headers."
3231   (interactive)
3232   (save-excursion
3233     (save-restriction
3234       (let ((inhibit-read-only t))
3235         (article-narrow-to-head)
3236         (goto-char (point-min))
3237         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3238           (delete-region (match-beginning 1) (match-end 1)))))))
3239
3240 (defun article-emphasize (&optional arg)
3241   "Emphasize text according to `gnus-emphasis-alist'."
3242   (interactive (gnus-article-hidden-arg))
3243   (unless (gnus-article-check-hidden-text 'emphasis arg)
3244     (save-excursion
3245       (let ((alist (or
3246                     (condition-case nil
3247                         (with-current-buffer gnus-summary-buffer
3248                           gnus-article-emphasis-alist)
3249                       (error))
3250                     gnus-emphasis-alist))
3251             (inhibit-read-only t)
3252             (props (append '(article-type emphasis)
3253                            gnus-hidden-properties))
3254             regexp elem beg invisible visible face)
3255         (article-goto-body)
3256         (setq beg (point))
3257         (while (setq elem (pop alist))
3258           (goto-char beg)
3259           (setq regexp (car elem)
3260                 invisible (nth 1 elem)
3261                 visible (nth 2 elem)
3262                 face (nth 3 elem))
3263           (while (re-search-forward regexp nil t)
3264             (when (and (match-beginning visible) (match-beginning invisible))
3265               (gnus-article-hide-text
3266                (match-beginning invisible) (match-end invisible) props)
3267               (gnus-article-unhide-text-type
3268                (match-beginning visible) (match-end visible) 'emphasis)
3269               (gnus-put-overlay-excluding-newlines
3270                (match-beginning visible) (match-end visible) 'face face)
3271               (gnus-add-wash-type 'emphasis)
3272               (goto-char (match-end invisible)))))))))
3273
3274 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3275   "Setup newsgroup emphasis alist."
3276   (unless gnus-article-emphasis-alist
3277     (let ((name (and gnus-newsgroup-name
3278                      (gnus-group-real-name gnus-newsgroup-name))))
3279       (make-local-variable 'gnus-article-emphasis-alist)
3280       (setq gnus-article-emphasis-alist
3281             (nconc
3282              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3283                (while (setq elem (pop alist))
3284                  (when (and name (string-match (car elem) name))
3285                    (setq alist nil
3286                          highlight (copy-sequence (cdr elem)))))
3287                highlight)
3288              (copy-sequence highlight-words)
3289              (if gnus-newsgroup-name
3290                  (copy-sequence (gnus-group-find-parameter
3291                                  gnus-newsgroup-name 'highlight-words t)))
3292              gnus-emphasis-alist)))))
3293
3294 (eval-when-compile
3295   (defvar gnus-summary-article-menu)
3296   (defvar gnus-summary-post-menu))
3297
3298 ;;; Saving functions.
3299
3300 (defun gnus-article-save (save-buffer file &optional num)
3301   "Save the currently selected article."
3302   (unless gnus-save-all-headers
3303     ;; Remove headers according to `gnus-saved-headers'.
3304     (let ((gnus-visible-headers
3305            (or gnus-saved-headers gnus-visible-headers))
3306           (gnus-article-buffer save-buffer))
3307       (save-excursion
3308         (set-buffer save-buffer)
3309         (article-hide-headers 1 t))))
3310   (save-window-excursion
3311     (if (not gnus-default-article-saver)
3312         (error "No default saver is defined")
3313       ;; !!! Magic!  The saving functions all save
3314       ;; `gnus-save-article-buffer' (or so they think), but we
3315       ;; bind that variable to our save-buffer.
3316       (set-buffer gnus-article-buffer)
3317       (let* ((gnus-save-article-buffer save-buffer)
3318              (filename
3319               (cond
3320                ((not gnus-prompt-before-saving) 'default)
3321                ((eq gnus-prompt-before-saving 'always) nil)
3322                (t file)))
3323              (gnus-number-of-articles-to-be-saved
3324               (when (eq gnus-prompt-before-saving t)
3325                 num)))                  ; Magic
3326         (set-buffer gnus-article-current-summary)
3327         (funcall gnus-default-article-saver filename)))))
3328
3329 (defun gnus-read-save-file-name (prompt &optional filename
3330                                         function group headers variable)
3331   (let ((default-name
3332           (funcall function group headers (symbol-value variable)))
3333         result)
3334     (setq result
3335           (expand-file-name
3336            (cond
3337             ((eq filename 'default)
3338              default-name)
3339             ((eq filename t)
3340              default-name)
3341             (filename filename)
3342             (t
3343              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3344                     (prompt
3345                      (format prompt
3346                              (if (and gnus-number-of-articles-to-be-saved
3347                                       (> gnus-number-of-articles-to-be-saved 1))
3348                                  (format "these %d articles"
3349                                          gnus-number-of-articles-to-be-saved)
3350                                "this article")))
3351                     (file
3352                      ;; Let the split methods have their say.
3353                      (cond
3354                       ;; No split name was found.
3355                       ((null split-name)
3356                        (read-file-name
3357                         (concat prompt " (default "
3358                                 (file-name-nondirectory default-name) "): ")
3359                         (file-name-directory default-name)
3360                         default-name))
3361                       ;; A single group name is returned.
3362                       ((stringp split-name)
3363                        (setq default-name
3364                              (funcall function split-name headers
3365                                       (symbol-value variable)))
3366                        (read-file-name
3367                         (concat prompt " (default "
3368                                 (file-name-nondirectory default-name) "): ")
3369                         (file-name-directory default-name)
3370                         default-name))
3371                       ;; A single split name was found
3372                       ((= 1 (length split-name))
3373                        (let* ((name (expand-file-name
3374                                      (car split-name)
3375                                      gnus-article-save-directory))
3376                               (dir (cond ((file-directory-p name)
3377                                           (file-name-as-directory name))
3378                                          ((file-exists-p name) name)
3379                                          (t gnus-article-save-directory))))
3380                          (read-file-name
3381                           (concat prompt " (default " name "): ")
3382                           dir name)))
3383                       ;; A list of splits was found.
3384                       (t
3385                        (setq split-name (nreverse split-name))
3386                        (let (result)
3387                          (let ((file-name-history
3388                                 (nconc split-name file-name-history)))
3389                            (setq result
3390                                  (expand-file-name
3391                                   (read-file-name
3392                                    (concat prompt " (`M-p' for defaults): ")
3393                                    gnus-article-save-directory
3394                                    (car split-name))
3395                                   gnus-article-save-directory)))
3396                          (car (push result file-name-history)))))))
3397                ;; Create the directory.
3398                (gnus-make-directory (file-name-directory file))
3399                ;; If we have read a directory, we append the default file name.
3400                (when (file-directory-p file)
3401                  (setq file (expand-file-name (file-name-nondirectory
3402                                                default-name)
3403                                               (file-name-as-directory file))))
3404                ;; Possibly translate some characters.
3405                (nnheader-translate-file-chars file))))))
3406     (gnus-make-directory (file-name-directory result))
3407     (set variable result)))
3408
3409 (defun gnus-article-archive-name (group)
3410   "Return the first instance of an \"Archive-name\" in the current buffer."
3411   (let ((case-fold-search t))
3412     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3413       (nnheader-concat gnus-article-save-directory
3414                        (match-string 1)))))
3415
3416 (defun gnus-article-nndoc-name (group)
3417   "If GROUP is an nndoc group, return the name of the parent group."
3418   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3419     (gnus-group-get-parameter group 'save-article-group)))
3420
3421 (defun gnus-summary-save-in-rmail (&optional filename)
3422   "Append this article to Rmail file.
3423 Optional argument FILENAME specifies file name.
3424 Directory to save to is default to `gnus-article-save-directory'."
3425   (setq filename (gnus-read-save-file-name
3426                   "Save %s in rmail file" filename
3427                   gnus-rmail-save-name gnus-newsgroup-name
3428                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3429   (gnus-eval-in-buffer-window gnus-save-article-buffer
3430     (save-excursion
3431       (save-restriction
3432         (widen)
3433         (gnus-output-to-rmail filename))))
3434   filename)
3435
3436 (defun gnus-summary-save-in-mail (&optional filename)
3437   "Append this article to Unix mail file.
3438 Optional argument FILENAME specifies file name.
3439 Directory to save to is default to `gnus-article-save-directory'."
3440   (setq filename (gnus-read-save-file-name
3441                   "Save %s in Unix mail file" filename
3442                   gnus-mail-save-name gnus-newsgroup-name
3443                   gnus-current-headers 'gnus-newsgroup-last-mail))
3444   (gnus-eval-in-buffer-window gnus-save-article-buffer
3445     (save-excursion
3446       (save-restriction
3447         (widen)
3448         (if (and (file-readable-p filename)
3449                  (file-regular-p filename)
3450                  (mail-file-babyl-p filename))
3451             (rmail-output-to-rmail-file filename t)
3452           (gnus-output-to-mail filename)))))
3453   filename)
3454
3455 (defun gnus-summary-save-in-file (&optional filename overwrite)
3456   "Append this article to file.
3457 Optional argument FILENAME specifies file name.
3458 Directory to save to is default to `gnus-article-save-directory'."
3459   (setq filename (gnus-read-save-file-name
3460                   "Save %s in file" filename
3461                   gnus-file-save-name gnus-newsgroup-name
3462                   gnus-current-headers 'gnus-newsgroup-last-file))
3463   (gnus-eval-in-buffer-window gnus-save-article-buffer
3464     (save-excursion
3465       (save-restriction
3466         (widen)
3467         (when (and overwrite
3468                    (file-exists-p filename))
3469           (delete-file filename))
3470         (gnus-output-to-file filename))))
3471   filename)
3472
3473 (defun gnus-summary-write-to-file (&optional filename)
3474   "Write this article to a file, overwriting it if the file exists.
3475 Optional argument FILENAME specifies file name.
3476 The directory to save in defaults to `gnus-article-save-directory'."
3477   (gnus-summary-save-in-file nil t))
3478
3479 (defun gnus-summary-save-body-in-file (&optional filename)
3480   "Append this article body to a file.
3481 Optional argument FILENAME specifies file name.
3482 The directory to save in defaults to `gnus-article-save-directory'."
3483   (setq filename (gnus-read-save-file-name
3484                   "Save %s body in file" filename
3485                   gnus-file-save-name gnus-newsgroup-name
3486                   gnus-current-headers 'gnus-newsgroup-last-file))
3487   (gnus-eval-in-buffer-window gnus-save-article-buffer
3488     (save-excursion
3489       (save-restriction
3490         (widen)
3491         (when (article-goto-body)
3492           (narrow-to-region (point) (point-max)))
3493         (gnus-output-to-file filename))))
3494   filename)
3495
3496 (defun gnus-summary-save-in-pipe (&optional command)
3497   "Pipe this article to subprocess."
3498   (setq command
3499         (cond ((and (eq command 'default)
3500                     gnus-last-shell-command)
3501                gnus-last-shell-command)
3502               ((stringp command)
3503                command)
3504               (t (read-string
3505                   (format
3506                    "Shell command on %s: "
3507                    (if (and gnus-number-of-articles-to-be-saved
3508                             (> gnus-number-of-articles-to-be-saved 1))
3509                        (format "these %d articles"
3510                                gnus-number-of-articles-to-be-saved)
3511                      "this article"))
3512                   gnus-last-shell-command))))
3513   (when (string-equal command "")
3514     (if gnus-last-shell-command
3515         (setq command gnus-last-shell-command)
3516       (error "A command is required")))
3517   (gnus-eval-in-buffer-window gnus-article-buffer
3518     (save-restriction
3519       (widen)
3520       (shell-command-on-region (point-min) (point-max) command nil)))
3521   (setq gnus-last-shell-command command))
3522
3523 (defun gnus-summary-pipe-to-muttprint (&optional command)
3524   "Pipe this article to muttprint."
3525   (setq command (read-string
3526                  "Print using command: " gnus-summary-muttprint-program
3527                  nil gnus-summary-muttprint-program))
3528   (gnus-summary-save-in-pipe command))
3529
3530 ;;; Article file names when saving.
3531
3532 (defun gnus-capitalize-newsgroup (newsgroup)
3533   "Capitalize NEWSGROUP name."
3534   (when (not (zerop (length newsgroup)))
3535     (concat (char-to-string (upcase (aref newsgroup 0)))
3536             (substring newsgroup 1))))
3537
3538 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3539   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3540 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3541 Otherwise, it is like ~/News/news/group/num."
3542   (let ((default
3543           (expand-file-name
3544            (concat (if (gnus-use-long-file-name 'not-save)
3545                        (gnus-capitalize-newsgroup newsgroup)
3546                      (gnus-newsgroup-directory-form newsgroup))
3547                    "/" (int-to-string (mail-header-number headers)))
3548            gnus-article-save-directory)))
3549     (if (and last-file
3550              (string-equal (file-name-directory default)
3551                            (file-name-directory last-file))
3552              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3553         default
3554       (or last-file default))))
3555
3556 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3557   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3558 If variable `gnus-use-long-file-name' is non-nil, it is
3559 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3560   (let ((default
3561           (expand-file-name
3562            (concat (if (gnus-use-long-file-name 'not-save)
3563                        newsgroup
3564                      (gnus-newsgroup-directory-form newsgroup))
3565                    "/" (int-to-string (mail-header-number headers)))
3566            gnus-article-save-directory)))
3567     (if (and last-file
3568              (string-equal (file-name-directory default)
3569                            (file-name-directory last-file))
3570              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3571         default
3572       (or last-file default))))
3573
3574 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3575   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3576 If variable `gnus-use-long-file-name' is non-nil, it is
3577 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3578   (or last-file
3579       (expand-file-name
3580        (if (gnus-use-long-file-name 'not-save)
3581            newsgroup
3582          (file-relative-name
3583           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3584           default-directory))
3585        gnus-article-save-directory)))
3586
3587 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3588   "Generate file name from sender."
3589   (let ((from (mail-header-from headers)))
3590     (expand-file-name
3591      (if (and from (string-match "\\([^ <]+\\)@" from))
3592          (match-string 1 from)
3593        "nobody")
3594      gnus-article-save-directory)))
3595
3596 (defun article-verify-x-pgp-sig ()
3597   "Verify X-PGP-Sig."
3598   (interactive)
3599   (if (gnus-buffer-live-p gnus-original-article-buffer)
3600       (let ((sig (with-current-buffer gnus-original-article-buffer
3601                    (gnus-fetch-field "X-PGP-Sig")))
3602             items info headers)
3603         (when (and sig
3604                    mml2015-use
3605                    (mml2015-clear-verify-function))
3606           (with-temp-buffer
3607             (insert-buffer-substring gnus-original-article-buffer)
3608             (setq items (split-string sig))
3609             (message-narrow-to-head)
3610             (let ((inhibit-point-motion-hooks t)
3611                   (case-fold-search t))
3612               ;; Don't verify multiple headers.
3613               (setq headers (mapconcat (lambda (header)
3614                                          (concat header ": "
3615                                                  (mail-fetch-field header)
3616                                                  "\n"))
3617                                        (split-string (nth 1 items) ",") "")))
3618             (delete-region (point-min) (point-max))
3619             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3620             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3621             (insert headers)
3622             (widen)
3623             (forward-line)
3624             (while (not (eobp))
3625               (if (looking-at "^-")
3626                   (insert "- "))
3627               (forward-line))
3628             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3629             (insert "Version: " (car items) "\n\n")
3630             (insert (mapconcat 'identity (cddr items) "\n"))
3631             (insert "\n-----END PGP SIGNATURE-----\n")
3632             (let ((mm-security-handle (list (format "multipart/signed"))))
3633               (mml2015-clean-buffer)
3634               (let ((coding-system-for-write (or gnus-newsgroup-charset
3635                                                  'iso-8859-1)))
3636                 (funcall (mml2015-clear-verify-function)))
3637               (setq info
3638                     (or (mm-handle-multipart-ctl-parameter
3639                          mm-security-handle 'gnus-details)
3640                         (mm-handle-multipart-ctl-parameter
3641                          mm-security-handle 'gnus-info)))))
3642           (when info
3643             (let ((inhibit-read-only t) bface eface)
3644               (save-restriction
3645                 (message-narrow-to-head)
3646                 (goto-char (point-max))
3647                 (forward-line -1)
3648                 (setq bface (get-text-property (point-at-bol) 'face)
3649                       eface (get-text-property (1- (point-at-eol)) 'face))
3650                 (message-remove-header "X-Gnus-PGP-Verify")
3651                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3652                     (forward-line)
3653                   (goto-char (point-max)))
3654                 (narrow-to-region (point) (point))
3655                 (insert "X-Gnus-PGP-Verify: " info "\n")
3656                 (goto-char (point-min))
3657                 (forward-line)
3658                 (while (not (eobp))
3659                   (if (not (looking-at "^[ \t]"))
3660                       (insert " "))
3661                   (forward-line))
3662                 ;; Do highlighting.
3663                 (goto-char (point-min))
3664                 (when (looking-at "\\([^:]+\\): *")
3665                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3666                                      'face bface)
3667                   (put-text-property (match-end 0) (point-max)
3668                                      'face eface)))))))))
3669
3670 (defun article-verify-cancel-lock ()
3671   "Verify Cancel-Lock header."
3672   (interactive)
3673   (if (gnus-buffer-live-p gnus-original-article-buffer)
3674       (canlock-verify gnus-original-article-buffer)))
3675
3676 (eval-and-compile
3677   (mapcar
3678    (lambda (func)
3679      (let (afunc gfunc)
3680        (if (consp func)
3681            (setq afunc (car func)
3682                  gfunc (cdr func))
3683          (setq afunc func
3684                gfunc (intern (format "gnus-%s" func))))
3685        (defalias gfunc
3686          (when (fboundp afunc)
3687            `(lambda (&optional interactive &rest args)
3688               ,(documentation afunc t)
3689               (interactive (list t))
3690               (save-excursion
3691                 (set-buffer gnus-article-buffer)
3692                 (if interactive
3693                     (call-interactively ',afunc)
3694                   (apply ',afunc args))))))))
3695    '(article-hide-headers
3696      article-verify-x-pgp-sig
3697      article-verify-cancel-lock
3698      article-hide-boring-headers
3699      article-treat-overstrike
3700      article-treat-ansi-sequences
3701      article-fill-long-lines
3702      article-capitalize-sentences
3703      article-remove-cr
3704      article-remove-leading-whitespace
3705      article-display-x-face
3706      article-display-face
3707      article-de-quoted-unreadable
3708      article-de-base64-unreadable
3709      article-decode-HZ
3710      article-wash-html
3711      article-unsplit-urls
3712      article-hide-list-identifiers
3713      article-strip-banner
3714      article-babel
3715      article-hide-pem
3716      article-hide-signature
3717      article-strip-headers-in-body
3718      article-remove-trailing-blank-lines
3719      article-strip-leading-blank-lines
3720      article-strip-multiple-blank-lines
3721      article-strip-leading-space
3722      article-strip-trailing-space
3723      article-strip-blank-lines
3724      article-strip-all-blank-lines
3725      article-date-local
3726      article-date-english
3727      article-date-iso8601
3728      article-date-original
3729      article-date-ut
3730      article-decode-mime-words
3731      article-decode-charset
3732      article-decode-encoded-words
3733      article-date-user
3734      article-date-lapsed
3735      article-emphasize
3736      article-treat-dumbquotes
3737      article-normalize-headers
3738 ;;     (article-show-all . gnus-article-show-all-headers)
3739      )))
3740 \f
3741 ;;;
3742 ;;; Gnus article mode
3743 ;;;
3744
3745 (put 'gnus-article-mode 'mode-class 'special)
3746
3747 (set-keymap-parent gnus-article-mode-map widget-keymap)
3748
3749 (gnus-define-keys gnus-article-mode-map
3750   " " gnus-article-goto-next-page
3751   "\177" gnus-article-goto-prev-page
3752   [delete] gnus-article-goto-prev-page
3753   [backspace] gnus-article-goto-prev-page
3754   "\C-c^" gnus-article-refer-article
3755   "h" gnus-article-show-summary
3756   "s" gnus-article-show-summary
3757   "\C-c\C-m" gnus-article-mail
3758   "?" gnus-article-describe-briefly
3759   "e" gnus-summary-edit-article
3760   "<" beginning-of-buffer
3761   ">" end-of-buffer
3762   "\C-c\C-i" gnus-info-find-node
3763   "\C-c\C-b" gnus-bug
3764   "R" gnus-article-reply-with-original
3765   "F" gnus-article-followup-with-original
3766   "\C-hk" gnus-article-describe-key
3767   "\C-hc" gnus-article-describe-key-briefly
3768
3769   "\C-d" gnus-article-read-summary-keys
3770   "\M-*" gnus-article-read-summary-keys
3771   "\M-#" gnus-article-read-summary-keys
3772   "\M-^" gnus-article-read-summary-keys
3773   "\M-g" gnus-article-read-summary-keys)
3774
3775 (substitute-key-definition
3776  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
3777
3778 (defun gnus-article-make-menu-bar ()
3779   (unless (boundp 'gnus-article-commands-menu)
3780     (gnus-summary-make-menu-bar))
3781   (gnus-turn-off-edit-menu 'article)
3782   (unless (boundp 'gnus-article-article-menu)
3783     (easy-menu-define
3784      gnus-article-article-menu gnus-article-mode-map ""
3785      '("Article"
3786        ["Scroll forwards" gnus-article-goto-next-page t]
3787        ["Scroll backwards" gnus-article-goto-prev-page t]
3788        ["Show summary" gnus-article-show-summary t]
3789        ["Fetch Message-ID at point" gnus-article-refer-article t]
3790        ["Mail to address at point" gnus-article-mail t]
3791        ["Send a bug report" gnus-bug t]))
3792
3793     (easy-menu-define
3794      gnus-article-treatment-menu gnus-article-mode-map ""
3795      ;; Fixme: this should use :active (and maybe :visible).
3796      '("Treatment"
3797        ["Hide headers" gnus-article-hide-headers t]
3798        ["Hide signature" gnus-article-hide-signature t]
3799        ["Hide citation" gnus-article-hide-citation t]
3800        ["Treat overstrike" gnus-article-treat-overstrike t]
3801        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
3802        ["Remove carriage return" gnus-article-remove-cr t]
3803        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3804        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
3805        ["Remove base64" gnus-article-de-base64-unreadable t]
3806        ["Treat html" gnus-article-wash-html t]
3807        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3808        ["Decode HZ" gnus-article-decode-HZ t]))
3809
3810     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3811
3812     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3813
3814     (gnus-run-hooks 'gnus-article-menu-hook)))
3815
3816 (defun gnus-article-mode ()
3817   "Major mode for displaying an article.
3818
3819 All normal editing commands are switched off.
3820
3821 The following commands are available in addition to all summary mode
3822 commands:
3823 \\<gnus-article-mode-map>
3824 \\[gnus-article-next-page]\t Scroll the article one page forwards
3825 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3826 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3827 \\[gnus-article-show-summary]\t Display the summary buffer
3828 \\[gnus-article-mail]\t Send a reply to the address near point
3829 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3830 \\[gnus-info-find-node]\t Go to the Gnus info node"
3831   (interactive)
3832   (kill-all-local-variables)
3833   (gnus-simplify-mode-line)
3834   (setq mode-name "Article")
3835   (setq major-mode 'gnus-article-mode)
3836   (make-local-variable 'minor-mode-alist)
3837   (use-local-map gnus-article-mode-map)
3838   (when (gnus-visual-p 'article-menu 'menu)
3839     (gnus-article-make-menu-bar)
3840     (when gnus-summary-tool-bar-map
3841       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
3842   (gnus-update-format-specifications nil 'article-mode)
3843   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3844   (set (make-local-variable 'gnus-page-broken) nil)
3845   (make-local-variable 'gnus-button-marker-list)
3846   (make-local-variable 'gnus-article-current-summary)
3847   (make-local-variable 'gnus-article-mime-handles)
3848   (make-local-variable 'gnus-article-decoded-p)
3849   (make-local-variable 'gnus-article-mime-handle-alist)
3850   (make-local-variable 'gnus-article-wash-types)
3851   (make-local-variable 'gnus-article-image-alist)
3852   (make-local-variable 'gnus-article-charset)
3853   (make-local-variable 'gnus-article-ignored-charsets)
3854   ;; Prevent recent Emacsen from displaying non-break space as "\ ".
3855   (set (make-local-variable 'nobreak-char-display) nil)
3856   (gnus-set-default-directory)
3857   (buffer-disable-undo)
3858   (setq buffer-read-only t
3859         show-trailing-whitespace nil)
3860   (set-syntax-table gnus-article-mode-syntax-table)
3861   (mm-enable-multibyte)
3862   (gnus-run-mode-hooks 'gnus-article-mode-hook))
3863
3864 (defun gnus-article-setup-buffer ()
3865   "Initialize the article buffer."
3866   (let* ((name (if gnus-single-article-buffer "*Article*"
3867                  (concat "*Article " gnus-newsgroup-name "*")))
3868          (original
3869           (progn (string-match "\\*Article" name)
3870                  (concat " *Original Article"
3871                          (substring name (match-end 0))))))
3872     (setq gnus-article-buffer name)
3873     (setq gnus-original-article-buffer original)
3874     (setq gnus-article-mime-handle-alist nil)
3875     ;; This might be a variable local to the summary buffer.
3876     (unless gnus-single-article-buffer
3877       (save-excursion
3878         (set-buffer gnus-summary-buffer)
3879         (setq gnus-article-buffer name)
3880         (setq gnus-original-article-buffer original)
3881         (gnus-set-global-variables)))
3882     (gnus-article-setup-highlight-words)
3883     ;; Init original article buffer.
3884     (save-excursion
3885       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3886       (mm-enable-multibyte)
3887       (setq major-mode 'gnus-original-article-mode)
3888       (make-local-variable 'gnus-original-article))
3889     (if (and (get-buffer name)
3890              (with-current-buffer name
3891                (if gnus-article-edit-mode
3892                    (if (y-or-n-p "Article mode edit in progress; discard? ")
3893                        (progn
3894                          (set-buffer-modified-p nil)
3895                          (gnus-kill-buffer name)
3896                          (message "")
3897                          nil)
3898                      (error "Action aborted"))
3899                  t)))
3900         (save-excursion
3901           (set-buffer name)
3902           (set (make-local-variable 'gnus-article-edit-mode) nil)
3903           (when gnus-article-mime-handles
3904             (mm-destroy-parts gnus-article-mime-handles)
3905             (setq gnus-article-mime-handles nil))
3906           ;; Set it to nil in article-buffer!
3907           (setq gnus-article-mime-handle-alist nil)
3908           (buffer-disable-undo)
3909           (setq buffer-read-only t)
3910           ;; This list just keeps growing if we don't reset it.
3911           (setq gnus-button-marker-list nil)
3912           (unless (eq major-mode 'gnus-article-mode)
3913             (gnus-article-mode))
3914           (current-buffer))
3915       (save-excursion
3916         (set-buffer (gnus-get-buffer-create name))
3917         (gnus-article-mode)
3918         (make-local-variable 'gnus-summary-buffer)
3919         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3920         (current-buffer)))))
3921
3922 ;; Set article window start at LINE, where LINE is the number of lines
3923 ;; from the head of the article.
3924 (defun gnus-article-set-window-start (&optional line)
3925   (set-window-start
3926    (gnus-get-buffer-window gnus-article-buffer t)
3927    (save-excursion
3928      (set-buffer gnus-article-buffer)
3929      (goto-char (point-min))
3930      (if (not line)
3931          (point-min)
3932        (gnus-message 6 "Moved to bookmark")
3933        (search-forward "\n\n" nil t)
3934        (forward-line line)
3935        (point)))))
3936
3937 (defun gnus-article-prepare (article &optional all-headers header)
3938   "Prepare ARTICLE in article mode buffer.
3939 ARTICLE should either be an article number or a Message-ID.
3940 If ARTICLE is an id, HEADER should be the article headers.
3941 If ALL-HEADERS is non-nil, no headers are hidden."
3942   (save-excursion
3943     ;; Make sure we start in a summary buffer.
3944     (unless (eq major-mode 'gnus-summary-mode)
3945       (set-buffer gnus-summary-buffer))
3946     (setq gnus-summary-buffer (current-buffer))
3947     (let* ((gnus-article (if header (mail-header-number header) article))
3948            (summary-buffer (current-buffer))
3949            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3950            (group gnus-newsgroup-name)
3951            result)
3952       (save-excursion
3953         (gnus-article-setup-buffer)
3954         (set-buffer gnus-article-buffer)
3955         ;; Deactivate active regions.
3956         (when (and (boundp 'transient-mark-mode)
3957                    transient-mark-mode)
3958           (setq mark-active nil))
3959         (if (not (setq result (let ((inhibit-read-only t))
3960                                 (gnus-request-article-this-buffer
3961                                  article group))))
3962             ;; There is no such article.
3963             (save-excursion
3964               (when (and (numberp article)
3965                          (not (memq article gnus-newsgroup-sparse)))
3966                 (setq gnus-article-current
3967                       (cons gnus-newsgroup-name article))
3968                 (set-buffer gnus-summary-buffer)
3969                 (setq gnus-current-article article)
3970                 (if (and (memq article gnus-newsgroup-undownloaded)
3971                          (not (gnus-online (gnus-find-method-for-group
3972                                             gnus-newsgroup-name))))
3973                     (progn
3974                       (gnus-summary-set-agent-mark article)
3975                       (message "Message marked for downloading"))
3976                   (gnus-summary-mark-article article gnus-canceled-mark)
3977                   (unless (memq article gnus-newsgroup-sparse)
3978                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3979           (if (or (eq result 'pseudo)
3980                   (eq result 'nneething))
3981               (progn
3982                 (save-excursion
3983                   (set-buffer summary-buffer)
3984                   (push article gnus-newsgroup-history)
3985                   (setq gnus-last-article gnus-current-article
3986                         gnus-current-article 0
3987                         gnus-current-headers nil
3988                         gnus-article-current nil)
3989                   (if (eq result 'nneething)
3990                       (gnus-configure-windows 'summary)
3991                     (gnus-configure-windows 'article))
3992                   (gnus-set-global-variables))
3993                 (let ((gnus-article-mime-handle-alist-1
3994                        gnus-article-mime-handle-alist))
3995                   (gnus-set-mode-line 'article)))
3996             ;; The result from the `request' was an actual article -
3997             ;; or at least some text that is now displayed in the
3998             ;; article buffer.
3999             (when (and (numberp article)
4000                        (not (eq article gnus-current-article)))
4001               ;; Seems like a new article has been selected.
4002               ;; `gnus-current-article' must be an article number.
4003               (save-excursion
4004                 (set-buffer summary-buffer)
4005                 (push article gnus-newsgroup-history)
4006                 (setq gnus-last-article gnus-current-article
4007                       gnus-current-article article
4008                       gnus-current-headers
4009                       (gnus-summary-article-header gnus-current-article)
4010                       gnus-article-current
4011                       (cons gnus-newsgroup-name gnus-current-article))
4012                 (unless (vectorp gnus-current-headers)
4013                   (setq gnus-current-headers nil))
4014                 (gnus-summary-goto-subject gnus-current-article)
4015                 (when (gnus-summary-show-thread)
4016                   ;; If the summary buffer really was folded, the
4017                   ;; previous goto may not actually have gone to
4018                   ;; the right article, but the thread root instead.
4019                   ;; So we go again.
4020                   (gnus-summary-goto-subject gnus-current-article))
4021                 (gnus-run-hooks 'gnus-mark-article-hook)
4022                 (gnus-set-mode-line 'summary)
4023                 (when (gnus-visual-p 'article-highlight 'highlight)
4024                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4025                 ;; Set the global newsgroup variables here.
4026                 (gnus-set-global-variables)
4027                 (setq gnus-have-all-headers
4028                       (or all-headers gnus-show-all-headers))))
4029             (save-excursion
4030               (gnus-configure-windows 'article))
4031             (when (or (numberp article)
4032                       (stringp article))
4033               (gnus-article-prepare-display)
4034               ;; Do page break.
4035               (goto-char (point-min))
4036               (when gnus-break-pages
4037                 (gnus-narrow-to-page)))
4038             (let ((gnus-article-mime-handle-alist-1
4039                    gnus-article-mime-handle-alist))
4040               (gnus-set-mode-line 'article))
4041             (article-goto-body)
4042             (unless (bobp)
4043               (forward-line -1))
4044             (set-window-point (get-buffer-window (current-buffer)) (point))
4045             (gnus-configure-windows 'article)
4046             t))))))
4047
4048 ;;;###autoload
4049 (defun gnus-article-prepare-display ()
4050   "Make the current buffer look like a nice article."
4051   ;; Hooks for getting information from the article.
4052   ;; This hook must be called before being narrowed.
4053   (let ((gnus-article-buffer (current-buffer))
4054         buffer-read-only
4055         (inhibit-read-only t))
4056     (unless (eq major-mode 'gnus-article-mode)
4057       (gnus-article-mode))
4058     (setq buffer-read-only nil
4059           gnus-article-wash-types nil
4060           gnus-article-image-alist nil)
4061     (gnus-run-hooks 'gnus-tmp-internal-hook)
4062     (when gnus-display-mime-function
4063       (funcall gnus-display-mime-function))
4064     (gnus-run-hooks 'gnus-article-prepare-hook)))
4065
4066 ;;;
4067 ;;; Gnus MIME viewing functions
4068 ;;;
4069
4070 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4071   "Format of the MIME buttons.
4072
4073 Valid specifiers include:
4074 %t  The MIME type
4075 %T  MIME type, along with additional info
4076 %n  The `name' parameter
4077 %d  The description, if any
4078 %l  The length of the encoded part
4079 %p  The part identifier number
4080 %e  Dots if the part isn't displayed
4081
4082 General format specifiers can also be used.  See Info node
4083 `(gnus)Formatting Variables'.")
4084
4085 (defvar gnus-mime-button-line-format-alist
4086   '((?t gnus-tmp-type ?s)
4087     (?T gnus-tmp-type-long ?s)
4088     (?n gnus-tmp-name ?s)
4089     (?d gnus-tmp-description ?s)
4090     (?p gnus-tmp-id ?s)
4091     (?l gnus-tmp-length ?d)
4092     (?e gnus-tmp-dots ?s)))
4093
4094 (defvar gnus-mime-button-commands
4095   '((gnus-article-press-button "\r" "Toggle Display")
4096     (gnus-mime-view-part "v" "View Interactively...")
4097     (gnus-mime-view-part-as-type "t" "View As Type...")
4098     (gnus-mime-view-part-as-charset "C" "View As charset...")
4099     (gnus-mime-save-part "o" "Save...")
4100     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4101     (gnus-mime-replace-part "r" "Replace part")
4102     (gnus-mime-delete-part "d" "Delete part")
4103     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4104     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4105     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4106     (gnus-mime-view-part-externally "e" "View Externally")
4107     (gnus-mime-print-part "p" "Print")
4108     (gnus-mime-pipe-part "|" "Pipe To Command...")
4109     (gnus-mime-action-on-part "." "Take action on the part...")))
4110
4111 (defun gnus-article-mime-part-status ()
4112   (if gnus-article-mime-handle-alist-1
4113       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4114           " (1 part)"
4115         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4116     ""))
4117
4118 (defvar gnus-mime-button-map
4119   (let ((map (make-sparse-keymap)))
4120     (define-key map gnus-mouse-2 'gnus-article-push-button)
4121     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4122     (dolist (c gnus-mime-button-commands)
4123       (define-key map (cadr c) (car c)))
4124     map))
4125
4126 (easy-menu-define
4127   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4128   `("MIME Part"
4129     ,@(mapcar (lambda (c)
4130                 (vector (caddr c) (car c) :enable t))
4131               gnus-mime-button-commands)))
4132
4133 (defun gnus-mime-button-menu (event prefix)
4134  "Construct a context-sensitive menu of MIME commands."
4135  (interactive "e\nP")
4136  (save-window-excursion
4137    (let ((pos (event-start event)))
4138      (select-window (posn-window pos))
4139      (goto-char (posn-point pos))
4140      (gnus-article-check-buffer)
4141      (popup-menu gnus-mime-button-menu nil prefix))))
4142
4143 (defun gnus-mime-view-all-parts (&optional handles)
4144   "View all the MIME parts."
4145   (interactive)
4146   (with-current-buffer gnus-article-buffer
4147     (let ((handles (or handles gnus-article-mime-handles))
4148           (mail-parse-charset gnus-newsgroup-charset)
4149           (mail-parse-ignored-charsets
4150            (with-current-buffer gnus-summary-buffer
4151              gnus-newsgroup-ignored-charsets)))
4152       (when handles
4153         (mm-remove-parts handles)
4154         (goto-char (point-min))
4155         (or (search-forward "\n\n") (goto-char (point-max)))
4156         (let ((inhibit-read-only t))
4157           (delete-region (point) (point-max))
4158           (mm-display-parts handles))))))
4159
4160 (defun gnus-article-jump-to-part (n)
4161   "Jump to MIME part N."
4162   (interactive "P")
4163   (pop-to-buffer gnus-article-buffer)
4164   ;; FIXME: why is it necessary?
4165   (sit-for 0)
4166   (let ((parts (length gnus-article-mime-handle-alist)))
4167     (or n (setq n
4168                 (string-to-number
4169                  (read-string ;; Emacs 21 doesn't have `read-number'.
4170                   (format "Jump to part (2..%s): " parts)))))
4171     (unless (and (integerp n) (<= n parts) (>= n 1))
4172       (setq n
4173             (progn
4174               (gnus-message 7 "Invalid part `%s', using %s instead."
4175                             n parts)
4176               parts)))
4177     (gnus-message 9 "Jumping to part %s." n)
4178     (cond ((>= gnus-auto-select-part 1)
4179            (while (and (<= n parts)
4180                        (not (gnus-article-goto-part n)))
4181              (setq n (1+ n))))
4182           ((< gnus-auto-select-part 0)
4183            (while (and (>= n 1)
4184                        (not (gnus-article-goto-part n)))
4185              (setq n (1- n))))
4186           (t
4187            (gnus-article-goto-part n)))))
4188
4189 (eval-when-compile
4190   (defsubst gnus-article-edit-part (handles &optional current-id)
4191     "Edit an article in order to delete a mime part.
4192 This function is exclusively used by `gnus-mime-save-part-and-strip'
4193 and `gnus-mime-delete-part', and not provided at run-time normally."
4194     (gnus-article-edit-article
4195      `(lambda ()
4196         (buffer-disable-undo)
4197         (erase-buffer)
4198         (let ((mail-parse-charset (or gnus-article-charset
4199                                       ',gnus-newsgroup-charset))
4200               (mail-parse-ignored-charsets
4201                (or gnus-article-ignored-charsets
4202                    ',gnus-newsgroup-ignored-charsets))
4203               (mbl mml-buffer-list))
4204           (setq mml-buffer-list nil)
4205           (insert-buffer-substring gnus-original-article-buffer)
4206           (mime-to-mml ',handles)
4207           (setq gnus-article-mime-handles nil)
4208           (let ((mbl1 mml-buffer-list))
4209             (setq mml-buffer-list mbl)
4210             (set (make-local-variable 'mml-buffer-list) mbl1))
4211           (gnus-make-local-hook 'kill-buffer-hook)
4212           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4213      `(lambda (no-highlight)
4214         (let ((mail-parse-charset (or gnus-article-charset
4215                                       ',gnus-newsgroup-charset))
4216               (message-options message-options)
4217               (message-options-set-recipient)
4218               (mail-parse-ignored-charsets
4219                (or gnus-article-ignored-charsets
4220                    ',gnus-newsgroup-ignored-charsets)))
4221           (mml-to-mime)
4222           (mml-destroy-buffers)
4223           (remove-hook 'kill-buffer-hook
4224                        'mml-destroy-buffers t)
4225           (kill-local-variable 'mml-buffer-list))
4226         (gnus-summary-edit-article-done
4227          ,(or (mail-header-references gnus-current-headers) "")
4228          ,(gnus-group-read-only-p)
4229          ,gnus-summary-buffer no-highlight))
4230      t)
4231     (gnus-article-edit-done)
4232     (gnus-summary-expand-window)
4233     (gnus-summary-show-article)
4234     (when (and current-id (integerp gnus-auto-select-part))
4235       (gnus-article-jump-to-part
4236        (+ current-id gnus-auto-select-part)))))
4237
4238 (defun gnus-mime-replace-part (file)
4239   "Replace MIME part under point with an external body."
4240   ;; Useful if file has already been saved to disk
4241   (interactive
4242    (list
4243     (mm-with-multibyte
4244       (read-file-name "Replace MIME part with file: "
4245                       (or mm-default-directory default-directory)
4246                       nil nil))))
4247   (gnus-mime-save-part-and-strip file))
4248
4249 (defun gnus-mime-save-part-and-strip (&optional file)
4250   "Save the MIME part under point then replace it with an external body.
4251 If FILE is given, use it for the external part."
4252   (interactive)
4253   (gnus-article-check-buffer)
4254   (when (gnus-group-read-only-p)
4255     (error "The current group does not support deleting of parts"))
4256   (when (mm-complicated-handles gnus-article-mime-handles)
4257     (error "\
4258 The current article has a complicated MIME structure, giving up..."))
4259   (let* ((data (get-text-property (point) 'gnus-data))
4260          (id (get-text-property (point) 'gnus-part))
4261          param
4262          (handles gnus-article-mime-handles))
4263     (unless file
4264       (setq file
4265             (and data (mm-save-part data "Delete MIME part and save to: "))))
4266     (when file
4267       (with-current-buffer (mm-handle-buffer data)
4268         (erase-buffer)
4269         (insert "Content-Type: " (mm-handle-media-type data))
4270         (mml-insert-parameter-string (cdr (mm-handle-type data))
4271                                      '(charset))
4272         (insert "\n")
4273         (insert "Content-ID: " (message-make-message-id) "\n")
4274         (insert "Content-Transfer-Encoding: binary\n")
4275         (insert "\n"))
4276       (setcdr data
4277               (cdr (mm-make-handle nil
4278                                    `("message/external-body"
4279                                      (access-type . "LOCAL-FILE")
4280                                      (name . ,file)))))
4281       ;; (set-buffer gnus-summary-buffer)
4282       (gnus-article-edit-part handles id))))
4283
4284 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4285 ;; parts...>') but with stripping would be nice.
4286
4287 (defun gnus-mime-delete-part ()
4288   "Delete the MIME part under point.
4289 Replace it with some information about the removed part."
4290   (interactive)
4291   (gnus-article-check-buffer)
4292   (when (gnus-group-read-only-p)
4293     (error "The current group does not support deleting of parts"))
4294   (when (mm-complicated-handles gnus-article-mime-handles)
4295     (error "\
4296 The current article has a complicated MIME structure, giving up..."))
4297   (when (or gnus-expert-user
4298             (gnus-yes-or-no-p "\
4299 Deleting parts may malfunction or destroy the article; continue? "))
4300     (let* ((data (get-text-property (point) 'gnus-data))
4301            (id (get-text-property (point) 'gnus-part))
4302            (handles gnus-article-mime-handles)
4303            (none "(none)")
4304            (description
4305             (or
4306              (mail-decode-encoded-word-string (or (mm-handle-description data)
4307                                                   none))))
4308            (filename
4309             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4310                 none))
4311            (type (mm-handle-media-type data)))
4312       (unless data
4313         (error "No MIME part under point"))
4314       (with-current-buffer (mm-handle-buffer data)
4315         (let ((bsize (format "%s" (buffer-size))))
4316           (erase-buffer)
4317           (insert
4318            (concat
4319             ",----\n"
4320             "| The following attachment has been deleted:\n"
4321             "|\n"
4322             "| Type:           " type "\n"
4323             "| Filename:       " filename "\n"
4324             "| Size (encoded): " bsize " Byte\n"
4325             "| Description:    " description "\n"
4326             "`----\n"))
4327           (setcdr data
4328                   (cdr (mm-make-handle
4329                         nil `("text/plain") nil nil
4330                         (list "attachment")
4331                         (format "Deleted attachment (%s bytes)" bsize))))))
4332       ;; (set-buffer gnus-summary-buffer)
4333       (gnus-article-edit-part handles id))))
4334
4335 (defun gnus-mime-save-part ()
4336   "Save the MIME part under point."
4337   (interactive)
4338   (gnus-article-check-buffer)
4339   (let ((data (get-text-property (point) 'gnus-data)))
4340     (when data
4341       (mm-save-part data))))
4342
4343 (defun gnus-mime-pipe-part ()
4344   "Pipe the MIME part under point to a process."
4345   (interactive)
4346   (gnus-article-check-buffer)
4347   (let ((data (get-text-property (point) 'gnus-data)))
4348     (when data
4349       (mm-pipe-part data))))
4350
4351 (defun gnus-mime-view-part ()
4352   "Interactively choose a viewing method for the MIME part under point."
4353   (interactive)
4354   (gnus-article-check-buffer)
4355   (let ((data (get-text-property (point) 'gnus-data)))
4356     (when data
4357       (setq gnus-article-mime-handles
4358             (mm-merge-handles
4359              gnus-article-mime-handles (setq data (copy-sequence data))))
4360       (mm-interactively-view-part data))))
4361
4362 (defun gnus-mime-view-part-as-type-internal ()
4363   (gnus-article-check-buffer)
4364   (let* ((handle (get-text-property (point) 'gnus-data))
4365          (name (or
4366                 ;; Content-Type: foo/bar; name=...
4367                 (mail-content-type-get (mm-handle-type handle) 'name)
4368                 ;; Content-Disposition: attachment; filename=...
4369                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
4370          (def-type (and name (mm-default-file-encoding name))))
4371     (and def-type (cons def-type 0))))
4372
4373 (defun gnus-mime-view-part-as-type (&optional mime-type)
4374   "Choose a MIME media type, and view the part as such."
4375   (interactive)
4376   (unless mime-type
4377     (setq mime-type
4378           (let ((default (gnus-mime-view-part-as-type-internal)))
4379             (completing-read
4380              (format "View as MIME type (default %s): "
4381                      (car default))
4382              (mapcar #'list (mailcap-mime-types))
4383              nil nil nil nil
4384              (car default)))))
4385   (gnus-article-check-buffer)
4386   (let ((handle (get-text-property (point) 'gnus-data)))
4387     (when handle
4388       (setq handle
4389             (mm-make-handle (mm-handle-buffer handle)
4390                             (cons mime-type (cdr (mm-handle-type handle)))
4391                             (mm-handle-encoding handle)
4392                             (mm-handle-undisplayer handle)
4393                             (mm-handle-disposition handle)
4394                             (mm-handle-description handle)
4395                             nil
4396                             (mm-handle-id handle)))
4397       (setq gnus-article-mime-handles
4398             (mm-merge-handles gnus-article-mime-handles handle))
4399       (gnus-mm-display-part handle))))
4400
4401 (defun gnus-mime-copy-part (&optional handle arg)
4402   "Put the MIME part under point into a new buffer.
4403 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4404 are decompressed."
4405   (interactive (list nil current-prefix-arg))
4406   (gnus-article-check-buffer)
4407   (unless handle
4408     (setq handle (get-text-property (point) 'gnus-data)))
4409   (when handle
4410     (let ((filename (or (mail-content-type-get (mm-handle-disposition handle)
4411                                                'name)
4412                         (mail-content-type-get (mm-handle-disposition handle)
4413                                                'filename)))
4414           contents dont-decode charset coding-system)
4415       (mm-with-unibyte-buffer
4416         (mm-insert-part handle)
4417         (setq contents (or (condition-case nil
4418                                (mm-decompress-buffer filename nil 'sig)
4419                              (error
4420                               (setq dont-decode t)
4421                               nil))
4422                            (buffer-string))))
4423       (setq filename (cond (filename (file-name-nondirectory filename))
4424                            (dont-decode "*raw data*")
4425                            (t "*decoded*")))
4426       (cond
4427        (dont-decode)
4428        ((not arg)
4429         (unless (setq charset (mail-content-type-get
4430                                (mm-handle-type handle) 'charset))
4431           (unless (setq coding-system (mm-with-unibyte-buffer
4432                                         (insert contents)
4433                                         (mm-find-buffer-file-coding-system)))
4434             (setq charset gnus-newsgroup-charset))))
4435        ((numberp arg)
4436         (setq charset (or (cdr (assq arg
4437                                      gnus-summary-show-article-charset-alist))
4438                           (mm-read-coding-system "Charset: ")))))
4439       (switch-to-buffer (generate-new-buffer filename))
4440       (if (or coding-system
4441               (and charset
4442                    (setq coding-system (mm-charset-to-coding-system charset))
4443                    (not (eq charset 'ascii))))
4444           (progn
4445             (mm-enable-multibyte)
4446             (insert (mm-decode-coding-string contents coding-system))
4447             (setq buffer-file-coding-system
4448                   (if (boundp 'last-coding-system-used)
4449                       (symbol-value 'last-coding-system-used)
4450                     coding-system)))
4451         (mm-disable-multibyte)
4452         (insert contents)
4453         (setq buffer-file-coding-system mm-binary-coding-system))
4454       ;; We do it this way to make `normal-mode' set the appropriate mode.
4455       (unwind-protect
4456           (progn
4457             (setq buffer-file-name (expand-file-name filename))
4458             (normal-mode))
4459         (setq buffer-file-name nil))
4460       (goto-char (point-min)))))
4461
4462 (defun gnus-mime-print-part (&optional handle filename)
4463   "Print the MIME part under point."
4464   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4465   (gnus-article-check-buffer)
4466   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4467          (contents (and handle (mm-get-part handle)))
4468          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4469          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
4470     (when contents
4471         (if printer
4472             (unwind-protect
4473                 (progn
4474                   (mm-save-part-to-file handle file)
4475                   (call-process shell-file-name nil
4476                                 (generate-new-buffer " *mm*")
4477                                 nil
4478                                 shell-command-switch
4479                                 (mm-mailcap-command
4480                                  printer file (mm-handle-type handle))))
4481               (delete-file file))
4482           (with-temp-buffer
4483             (insert contents)
4484             (gnus-print-buffer))
4485           (ps-despool filename)))))
4486
4487 (defun gnus-mime-inline-part (&optional handle arg)
4488   "Insert the MIME part under point into the current buffer.
4489 Compressed files like .gz and .bz2 are decompressed."
4490   (interactive (list nil current-prefix-arg))
4491   (gnus-article-check-buffer)
4492   (unless handle
4493     (setq handle (get-text-property (point) 'gnus-data)))
4494   (when handle
4495     (let ((b (point))
4496           (inhibit-read-only t)
4497           contents charset coding-system)
4498       (if (and (not arg) (mm-handle-undisplayer handle))
4499           (mm-remove-part handle)
4500         (mm-with-unibyte-buffer
4501           (mm-insert-part handle)
4502           (setq contents
4503                 (or (mm-decompress-buffer
4504                      (or (mail-content-type-get (mm-handle-disposition handle)
4505                                                 'name)
4506                          (mail-content-type-get (mm-handle-disposition handle)
4507                                                 'filename))
4508                      nil t)
4509                     (buffer-string))))
4510         (cond
4511          ((not arg)
4512           (unless (setq charset (mail-content-type-get
4513                                  (mm-handle-type handle) 'charset))
4514             (unless (setq coding-system
4515                           (mm-with-unibyte-buffer
4516                             (insert contents)
4517                             (mm-find-buffer-file-coding-system)))
4518               (setq charset gnus-newsgroup-charset))))
4519          ((numberp arg)
4520           (if (mm-handle-undisplayer handle)
4521               (mm-remove-part handle))
4522           (setq charset
4523                 (or (cdr (assq arg
4524                                gnus-summary-show-article-charset-alist))
4525                     (mm-read-coding-system "Charset: "))))
4526          (t
4527           (if (mm-handle-undisplayer handle)
4528               (mm-remove-part handle))))
4529         (forward-line 2)
4530         (mm-insert-inline
4531          handle
4532          (if (or coding-system
4533                  (and charset
4534                       (setq coding-system
4535                             (mm-charset-to-coding-system charset))
4536                       (not (eq coding-system 'ascii))))
4537              (mm-decode-coding-string contents coding-system)
4538            (mm-string-to-multibyte contents)))
4539         (goto-char b)))))
4540
4541 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4542   "Insert the MIME part under point into the current buffer using the
4543 specified charset."
4544   (interactive (list nil current-prefix-arg))
4545   (gnus-article-check-buffer)
4546   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4547          contents charset
4548          (b (point))
4549          (inhibit-read-only t))
4550     (when handle
4551       (if (mm-handle-undisplayer handle)
4552           (mm-remove-part handle))
4553       (let ((gnus-newsgroup-charset
4554              (or (cdr (assq arg
4555                             gnus-summary-show-article-charset-alist))
4556                  (mm-read-coding-system "Charset: ")))
4557           (gnus-newsgroup-ignored-charsets 'gnus-all))
4558         (gnus-article-press-button)))))
4559
4560 (defun gnus-mime-view-part-externally (&optional handle)
4561   "View the MIME part under point with an external viewer."
4562   (interactive)
4563   (gnus-article-check-buffer)
4564   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4565          (mm-user-display-methods nil)
4566          (mm-inlined-types nil)
4567          (mail-parse-charset gnus-newsgroup-charset)
4568          (mail-parse-ignored-charsets
4569           (with-current-buffer gnus-summary-buffer
4570             gnus-newsgroup-ignored-charsets)))
4571     (when handle
4572       (if (mm-handle-undisplayer handle)
4573           (mm-remove-part handle)
4574         (mm-display-part handle)))))
4575
4576 (defun gnus-mime-view-part-internally (&optional handle)
4577   "View the MIME part under point with an internal viewer.
4578 If no internal viewer is available, use an external viewer."
4579   (interactive)
4580   (gnus-article-check-buffer)
4581   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4582          (mm-inlined-types '(".*"))
4583          (mm-inline-large-images t)
4584          (mail-parse-charset gnus-newsgroup-charset)
4585          (mail-parse-ignored-charsets
4586           (with-current-buffer gnus-summary-buffer
4587             gnus-newsgroup-ignored-charsets))
4588          (inhibit-read-only t))
4589     (when handle
4590       (if (mm-handle-undisplayer handle)
4591           (mm-remove-part handle)
4592         (mm-display-part handle)))))
4593
4594 (defun gnus-mime-action-on-part (&optional action)
4595   "Do something with the MIME attachment at \(point\)."
4596   (interactive
4597    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
4598   (gnus-article-check-buffer)
4599   (let ((action-pair (assoc action gnus-mime-action-alist)))
4600     (if action-pair
4601         (funcall (cdr action-pair)))))
4602
4603 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
4604   "Call FUNCTION on MIME part N.
4605 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
4606 If INTERACTIVE, call FUNCTION interactivly."
4607   (let (window frame)
4608     ;; Check whether the article is displayed.
4609     (unless (and (gnus-buffer-live-p gnus-article-buffer)
4610                  (setq window (get-buffer-window gnus-article-buffer t))
4611                  (frame-visible-p (setq frame (window-frame window))))
4612       (error "No article is displayed"))
4613     (with-current-buffer gnus-article-buffer
4614       ;; Check whether the article displays the right contents.
4615       (unless (with-current-buffer gnus-summary-buffer
4616                 (eq gnus-current-article (gnus-summary-article-number)))
4617         (error "You should select the right article first"))
4618       ;; Check whether the specified part exists.
4619       (when (> n (length gnus-article-mime-handle-alist))
4620         (error "No such part")))
4621     (unless
4622         (progn
4623           ;; To select the window is needed so that the cursor
4624           ;; might be visible on the MIME button.
4625           (select-window (prog1
4626                              window
4627                            (setq window (selected-window))
4628                            ;; Article may be displayed in the other frame.
4629                            (gnus-select-frame-set-input-focus
4630                             (prog1
4631                                 frame
4632                               (setq frame (selected-frame))))))
4633           (when (gnus-article-goto-part n)
4634             ;; We point the cursor and the arrow at the MIME button
4635             ;; when the `function' prompt the user for something.
4636             (let ((cursor-in-non-selected-windows t)
4637                   (overlay-arrow-string "=>")
4638                   (overlay-arrow-position (point-marker)))
4639               (unwind-protect
4640                   (cond
4641                    ((and no-handle interactive)
4642                     (call-interactively function))
4643                    (no-handle
4644                     (funcall function))
4645                    (interactive
4646                     (call-interactively
4647                      function
4648                      (cdr (assq n gnus-article-mime-handle-alist))))
4649                    (t
4650                     (funcall function
4651                              (cdr (assq n gnus-article-mime-handle-alist)))))
4652                 (set-marker overlay-arrow-position nil)
4653                 (unless gnus-auto-select-part
4654                   (gnus-select-frame-set-input-focus frame)
4655                   (select-window window))))
4656             t))
4657       (if gnus-inhibit-mime-unbuttonizing
4658           ;; This is the default though the program shouldn't reach here.
4659           (error "No such part")
4660         ;; The part which doesn't have the MIME button is selected.
4661         ;; So, we display all the buttons and redo it.
4662         (let ((gnus-inhibit-mime-unbuttonizing t))
4663           (gnus-summary-show-article)
4664           (gnus-article-part-wrapper n function no-handle))))))
4665
4666 (defun gnus-article-pipe-part (n)
4667   "Pipe MIME part N, which is the numerical prefix."
4668   (interactive "p")
4669   (gnus-article-part-wrapper n 'mm-pipe-part))
4670
4671 (defun gnus-article-save-part (n)
4672   "Save MIME part N, which is the numerical prefix."
4673   (interactive "p")
4674   (gnus-article-part-wrapper n 'mm-save-part))
4675
4676 (defun gnus-article-interactively-view-part (n)
4677   "View MIME part N interactively, which is the numerical prefix."
4678   (interactive "p")
4679   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4680
4681 (defun gnus-article-copy-part (n)
4682   "Copy MIME part N, which is the numerical prefix."
4683   (interactive "p")
4684   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4685
4686 (defun gnus-article-view-part-as-charset (n)
4687   "View MIME part N using a specified charset.
4688 N is the numerical prefix."
4689   (interactive "p")
4690   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4691
4692 (defun gnus-article-view-part-externally (n)
4693   "View MIME part N externally, which is the numerical prefix."
4694   (interactive "p")
4695   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4696
4697 (defun gnus-article-inline-part (n)
4698   "Inline MIME part N, which is the numerical prefix."
4699   (interactive "p")
4700   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4701
4702 (defun gnus-article-save-part-and-strip (n)
4703   "Save MIME part N and replace it with an external body.
4704 N is the numerical prefix."
4705   (interactive "p")
4706   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
4707
4708 (defun gnus-article-replace-part (n)
4709   "Replace MIME part N with an external body.
4710 N is the numerical prefix."
4711   (interactive "p")
4712   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
4713
4714 (defun gnus-article-delete-part (n)
4715   "Delete MIME part N and add some information about the removed part.
4716 N is the numerical prefix."
4717   (interactive "p")
4718   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
4719
4720 (defun gnus-article-mime-match-handle-first (condition)
4721   (if condition
4722       (let (n)
4723         (dolist (ihandle gnus-article-mime-handle-alist)
4724           (if (and (cond
4725                     ((functionp condition)
4726                      (funcall condition (cdr ihandle)))
4727                     ((eq condition 'undisplayed)
4728                      (not (or (mm-handle-undisplayer (cdr ihandle))
4729                               (equal (mm-handle-media-type (cdr ihandle))
4730                                      "multipart/alternative"))))
4731                     ((eq condition 'undisplayed-alternative)
4732                      (not (mm-handle-undisplayer (cdr ihandle))))
4733                     (t t))
4734                    (gnus-article-goto-part (car ihandle))
4735                    (or (not n) (< (car ihandle) n)))
4736               (setq n (car ihandle))))
4737         (or n 1))
4738     1))
4739
4740 (defun gnus-article-view-part (&optional n)
4741   "View MIME part N, which is the numerical prefix."
4742   (interactive "P")
4743   (with-current-buffer gnus-article-buffer
4744     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4745                              gnus-article-mime-match-handle-function)))
4746     (when (> n (length gnus-article-mime-handle-alist))
4747       (error "No such part"))
4748     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4749       (when (gnus-article-goto-part n)
4750         (if (equal (car handle) "multipart/alternative")
4751             (gnus-article-press-button)
4752           (when (eq (gnus-mm-display-part handle) 'internal)
4753             (gnus-set-window-start)))))))
4754
4755 (defsubst gnus-article-mime-total-parts ()
4756   (if (bufferp (car gnus-article-mime-handles))
4757       1 ;; single part
4758     (1- (length gnus-article-mime-handles))))
4759
4760 (defun gnus-mm-display-part (handle)
4761   "Display HANDLE and fix MIME button."
4762   (let ((id (get-text-property (point) 'gnus-part))
4763         (point (point))
4764         (inhibit-read-only t))
4765     (forward-line 1)
4766     (prog1
4767         (let ((window (selected-window))
4768               (mail-parse-charset gnus-newsgroup-charset)
4769               (mail-parse-ignored-charsets
4770                (if (gnus-buffer-live-p gnus-summary-buffer)
4771                    (with-current-buffer gnus-summary-buffer
4772                      gnus-newsgroup-ignored-charsets)
4773                  nil)))
4774           (save-excursion
4775             (unwind-protect
4776                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4777                       (beg (point)))
4778                   (when win
4779                     (select-window win))
4780                   (goto-char point)
4781                   (forward-line)
4782                   (if (mm-handle-displayed-p handle)
4783                       ;; This will remove the part.
4784                       (mm-display-part handle)
4785                     (save-restriction
4786                       (narrow-to-region (point)
4787                                         (if (eobp) (point) (1+ (point))))
4788                       (mm-display-part handle)
4789                       ;; We narrow to the part itself and
4790                       ;; then call the treatment functions.
4791                       (goto-char (point-min))
4792                       (forward-line 1)
4793                       (narrow-to-region (point) (point-max))
4794                       (gnus-treat-article
4795                        nil id
4796                        (gnus-article-mime-total-parts)
4797                        (mm-handle-media-type handle)))))
4798               (if (window-live-p window)
4799                   (select-window window)))))
4800       (goto-char point)
4801       (gnus-delete-line)
4802       (gnus-insert-mime-button
4803        handle id (list (mm-handle-displayed-p handle)))
4804       (goto-char point))))
4805
4806 (defun gnus-article-goto-part (n)
4807   "Go to MIME part N."
4808   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
4809
4810 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4811   (let ((gnus-tmp-name
4812          (or (mail-content-type-get (mm-handle-type handle) 'name)
4813              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4814              (mail-content-type-get (mm-handle-type handle) 'url)
4815              ""))
4816         (gnus-tmp-type (mm-handle-media-type handle))
4817         (gnus-tmp-description
4818          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4819                                               "")))
4820         (gnus-tmp-dots
4821          (if (if displayed (car displayed)
4822                (mm-handle-displayed-p handle))
4823              "" "..."))
4824         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4825                            (buffer-size)))
4826         gnus-tmp-type-long b e)
4827     (when (string-match ".*/" gnus-tmp-name)
4828       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4829     (setq gnus-tmp-type-long (concat gnus-tmp-type
4830                                      (and (not (equal gnus-tmp-name ""))
4831                                           (concat "; " gnus-tmp-name))))
4832     (unless (equal gnus-tmp-description "")
4833       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4834     (unless (bolp)
4835       (insert "\n"))
4836     (setq b (point))
4837     (gnus-eval-format
4838      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4839      `(keymap ,gnus-mime-button-map
4840               gnus-callback gnus-mm-display-part
4841               gnus-part ,gnus-tmp-id
4842               article-type annotation
4843               gnus-data ,handle))
4844     (setq e (if (bolp)
4845                 ;; Exclude a newline.
4846                 (1- (point))
4847               (point)))
4848     (widget-convert-button
4849      'link b e
4850      :mime-handle handle
4851      :action 'gnus-widget-press-button
4852      :button-keymap gnus-mime-button-map
4853      :help-echo
4854      (lambda (widget/window &optional overlay pos)
4855        ;; Needed to properly clear the message due to a bug in
4856        ;; wid-edit (XEmacs only).
4857        (if (boundp 'help-echo-owns-message)
4858            (setq help-echo-owns-message t))
4859        (format
4860         "%S: %s the MIME part; %S: more options"
4861         (aref gnus-mouse-2 0)
4862         ;; XEmacs will get a single widget arg; Emacs 21 will get
4863         ;; window, overlay, position.
4864         (if (mm-handle-displayed-p
4865              (if overlay
4866                  (with-current-buffer (gnus-overlay-buffer overlay)
4867                    (widget-get (widget-at (gnus-overlay-start overlay))
4868                                :mime-handle))
4869                (widget-get widget/window :mime-handle)))
4870             "hide" "show")
4871         (aref gnus-down-mouse-3 0))))))
4872
4873 (defun gnus-widget-press-button (elems el)
4874   (goto-char (widget-get elems :from))
4875   (gnus-article-press-button))
4876
4877 (defvar gnus-displaying-mime nil)
4878
4879 (defun gnus-display-mime (&optional ihandles)
4880   "Display the MIME parts."
4881   (save-excursion
4882     (save-selected-window
4883       (let ((window (get-buffer-window gnus-article-buffer))
4884             (point (point)))
4885         (when window
4886           (select-window window)
4887           ;; We have to do this since selecting the window
4888           ;; may change the point.  So we set the window point.
4889           (set-window-point window point)))
4890       (let ((handles ihandles)
4891             (inhibit-read-only t)
4892             handle)
4893         (cond (handles)
4894               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
4895                (when gnus-article-emulate-mime
4896                  (mm-uu-dissect-text-parts handles)))
4897               (gnus-article-emulate-mime
4898                (setq handles (mm-uu-dissect))))
4899         (when (and (not ihandles)
4900                    (not gnus-displaying-mime))
4901           ;; Top-level call; we clean up.
4902           (when gnus-article-mime-handles
4903             (mm-destroy-parts gnus-article-mime-handles)
4904             (setq gnus-article-mime-handle-alist nil));; A trick.
4905           (setq gnus-article-mime-handles handles)
4906           ;; We allow users to glean info from the handles.
4907           (when gnus-article-mime-part-function
4908             (gnus-mime-part-function handles)))
4909         (if (and handles
4910                  (or (not (stringp (car handles)))
4911                      (cdr handles)))
4912             (progn
4913               (when (and (not ihandles)
4914                          (not gnus-displaying-mime))
4915                 ;; Clean up for mime parts.
4916                 (article-goto-body)
4917                 (delete-region (point) (point-max)))
4918               (let ((gnus-displaying-mime t))
4919                 (gnus-mime-display-part handles)))
4920           (save-restriction
4921             (article-goto-body)
4922             (narrow-to-region (point) (point-max))
4923             (gnus-treat-article nil 1 1)
4924             (widen)))
4925         (unless ihandles
4926           ;; Highlight the headers.
4927           (save-excursion
4928             (save-restriction
4929               (article-goto-body)
4930               (narrow-to-region (point-min) (point))
4931               (gnus-article-save-original-date
4932                (gnus-treat-article 'head)))))))))
4933
4934 (defcustom gnus-mime-display-multipart-as-mixed nil
4935   "Display \"multipart\" parts as  \"multipart/mixed\".
4936
4937 If t, it overrides nil values of
4938 `gnus-mime-display-multipart-alternative-as-mixed' and
4939 `gnus-mime-display-multipart-related-as-mixed'."
4940   :group 'gnus-article-mime
4941   :type 'boolean)
4942
4943 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
4944   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
4945   :version "22.1"
4946   :group 'gnus-article-mime
4947   :type 'boolean)
4948
4949 (defcustom gnus-mime-display-multipart-related-as-mixed nil
4950   "Display \"multipart/related\" parts as  \"multipart/mixed\".
4951
4952 If displaying \"text/html\" is discouraged \(see
4953 `mm-discouraged-alternatives'\) images or other material inside a
4954 \"multipart/related\" part might be overlooked when this variable is nil."
4955   :version "22.1"
4956   :group 'gnus-article-mime
4957   :type 'boolean)
4958
4959 (defun gnus-mime-display-part (handle)
4960   (cond
4961    ;; Maybe a broken MIME message.
4962    ((null handle))
4963    ;; Single part.
4964    ((not (stringp (car handle)))
4965     (gnus-mime-display-single handle))
4966    ;; User-defined multipart
4967    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4968     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4969              handle))
4970    ;; multipart/alternative
4971    ((and (equal (car handle) "multipart/alternative")
4972          (not (or gnus-mime-display-multipart-as-mixed
4973                   gnus-mime-display-multipart-alternative-as-mixed)))
4974     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4975       (push (cons id handle) gnus-article-mime-handle-alist)
4976       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4977    ;; multipart/related
4978    ((and (equal (car handle) "multipart/related")
4979          (not (or gnus-mime-display-multipart-as-mixed
4980                   gnus-mime-display-multipart-related-as-mixed)))
4981     ;;;!!!We should find the start part, but we just default
4982     ;;;!!!to the first part.
4983     ;;(gnus-mime-display-part (cadr handle))
4984     ;;;!!! Most multipart/related is an HTML message plus images.
4985     ;;;!!! Unfortunately we are unable to let W3 display those
4986     ;;;!!! included images, so we just display it as a mixed multipart.
4987     ;;(gnus-mime-display-mixed (cdr handle))
4988     ;;;!!! No, w3 can display everything just fine.
4989     (gnus-mime-display-part (cadr handle)))
4990    ((equal (car handle) "multipart/signed")
4991     (gnus-add-wash-type 'signed)
4992     (gnus-mime-display-security handle))
4993    ((equal (car handle) "multipart/encrypted")
4994     (gnus-add-wash-type 'encrypted)
4995     (gnus-mime-display-security handle))
4996    ;; Other multiparts are handled like multipart/mixed.
4997    (t
4998     (gnus-mime-display-mixed (cdr handle)))))
4999
5000 (defun gnus-mime-part-function (handles)
5001   (if (stringp (car handles))
5002       (mapcar 'gnus-mime-part-function (cdr handles))
5003     (funcall gnus-article-mime-part-function handles)))
5004
5005 (defun gnus-mime-display-mixed (handles)
5006   (mapcar 'gnus-mime-display-part handles))
5007
5008 (defun gnus-mime-display-single (handle)
5009   (let ((type (mm-handle-media-type handle))
5010         (ignored gnus-ignored-mime-types)
5011         (not-attachment t)
5012         (move nil)
5013         display text)
5014     (catch 'ignored
5015       (progn
5016         (while ignored
5017           (when (string-match (pop ignored) type)
5018             (throw 'ignored nil)))
5019         (if (and (setq not-attachment
5020                        (and (not (mm-inline-override-p handle))
5021                             (or (not (mm-handle-disposition handle))
5022                                 (equal (car (mm-handle-disposition handle))
5023                                        "inline")
5024                                 (mm-attachment-override-p handle))))
5025                  (mm-automatic-display-p handle)
5026                  (or (and
5027                       (mm-inlinable-p handle)
5028                       (mm-inlined-p handle))
5029                      (mm-automatic-external-display-p type)))
5030             (setq display t)
5031           (when (equal (mm-handle-media-supertype handle) "text")
5032             (setq text t)))
5033         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5034               beg)
5035           (push (cons id handle) gnus-article-mime-handle-alist)
5036           (when (or (not display)
5037                     (not (gnus-unbuttonized-mime-type-p type)))
5038             (gnus-insert-mime-button
5039              handle id (list (or display (and not-attachment text))))
5040             (gnus-article-insert-newline)
5041             ;; Remember modify the number of forward lines.
5042             (setq move t))
5043           (setq beg (point))
5044           (cond
5045            (display
5046             (when move
5047               (forward-line -1)
5048               (setq beg (point)))
5049             (let ((mail-parse-charset gnus-newsgroup-charset)
5050                   (mail-parse-ignored-charsets
5051                    (save-excursion (condition-case ()
5052                                        (set-buffer gnus-summary-buffer)
5053                                      (error))
5054                                    gnus-newsgroup-ignored-charsets)))
5055               (mm-display-part handle t))
5056             (goto-char (point-max)))
5057            ((and text not-attachment)
5058             (when move
5059               (forward-line -1)
5060               (setq beg (point)))
5061             (gnus-article-insert-newline)