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