gnus-art.el (org-entities): Bind it.
[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 (defvar org-entities)
2118
2119 (defun article-treat-non-ascii ()
2120   "Translate many Unicode characters into their ASCII equivalents."
2121   (interactive)
2122   (require 'org-entities)
2123   (let ((table (make-char-table nil)))
2124     (dolist (elem org-entities)
2125       (when (and (listp elem)
2126                  (= (length (nth 6 elem)) 1))
2127         (set-char-table-range table
2128                               (aref (nth 6 elem) 0)
2129                               (nth 4 elem))))
2130     (save-excursion
2131       (when (article-goto-body)
2132         (let ((inhibit-read-only t)
2133               replace)
2134           (while (not (eobp))
2135             (if (not (setq replace (aref table (following-char))))
2136                 (forward-char 1)
2137               (delete-char 1)
2138               (insert replace))))))))
2139
2140 (defun article-translate-characters (from to)
2141   "Translate all characters in the body of the article according to FROM and TO.
2142 FROM is a string of characters to translate from; to is a string of
2143 characters to translate to."
2144   (save-excursion
2145     (when (article-goto-body)
2146       (let ((inhibit-read-only t)
2147             (x (make-string 225 ?x))
2148             (i -1))
2149         (while (< (incf i) (length x))
2150           (aset x i i))
2151         (setq i 0)
2152         (while (< i (length from))
2153           (aset x (aref from i) (aref to i))
2154           (incf i))
2155         (translate-region (point) (point-max) x)))))
2156
2157 (defun article-translate-strings (map)
2158   "Translate all string in the body of the article according to MAP.
2159 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2160   (save-excursion
2161     (when (article-goto-body)
2162       (let ((inhibit-read-only t))
2163         (dolist (elem map)
2164           (let ((from (car elem))
2165                 (to (cadr elem)))
2166             (save-excursion
2167               (if (stringp from)
2168                   (while (search-forward from nil t)
2169                     (replace-match to))
2170                 (while (not (eobp))
2171                   (if (eq (following-char) from)
2172                       (progn
2173                         (delete-char 1)
2174                         (insert to))
2175                     (forward-char 1)))))))))))
2176
2177 (defun article-treat-overstrike ()
2178   "Translate overstrikes into bold text."
2179   (interactive)
2180   (save-excursion
2181     (when (article-goto-body)
2182       (let ((inhibit-read-only t))
2183         (while (search-forward "\b" nil t)
2184           (let ((next (char-after))
2185                 (previous (char-after (- (point) 2))))
2186             ;; We do the boldification/underlining by hiding the
2187             ;; overstrikes and putting the proper text property
2188             ;; on the letters.
2189             (cond
2190              ((eq next previous)
2191               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2192               (put-text-property (point) (1+ (point)) 'face 'bold))
2193              ((eq next ?_)
2194               (gnus-article-hide-text-type
2195                (1- (point)) (1+ (point)) 'overstrike)
2196               (put-text-property
2197                (- (point) 2) (1- (point)) 'face 'underline))
2198              ((eq previous ?_)
2199               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2200               (put-text-property
2201                (point) (1+ (point)) 'face 'underline)))))))))
2202
2203 (defun article-treat-ansi-sequences ()
2204   "Translate ANSI SGR control sequences into overlays or extents."
2205   (interactive)
2206   (save-excursion
2207     (when (article-goto-body)
2208       (let ((inhibit-read-only t))
2209         (ansi-color-apply-on-region (point) (point-max))))))
2210
2211 (defun gnus-article-treat-unfold-headers ()
2212   "Unfold folded message headers.
2213 Only the headers that fit into the current window width will be
2214 unfolded."
2215   (interactive)
2216   (gnus-with-article-headers
2217     (let (length)
2218       (while (not (eobp))
2219         (save-restriction
2220           (mail-header-narrow-to-field)
2221           (let* ((header (buffer-string))
2222                  (unfoldable
2223                   (or (equal gnus-article-unfold-long-headers t)
2224                       (and (stringp gnus-article-unfold-long-headers)
2225                            (string-match gnus-article-unfold-long-headers header)))))
2226             (with-temp-buffer
2227               (insert header)
2228               (goto-char (point-min))
2229               (while (re-search-forward "\n[\t ]" nil t)
2230                 (replace-match " " t t)))
2231             (setq length (- (point-max) (point-min) 1))
2232             (when (or unfoldable
2233                       (< length (window-width)))
2234               (while (re-search-forward "\n[\t ]" nil t)
2235                 (replace-match " " t t))))
2236           (goto-char (point-max)))))))
2237
2238 (defun gnus-article-treat-fold-headers ()
2239   "Fold message headers."
2240   (interactive)
2241   (gnus-with-article-headers
2242     (while (not (eobp))
2243       (save-restriction
2244         (mail-header-narrow-to-field)
2245         (mail-header-fold-field)
2246         (goto-char (point-max))))))
2247
2248 (defun gnus-treat-smiley ()
2249   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2250   (interactive)
2251   (gnus-with-article-buffer
2252     (if (memq 'smiley gnus-article-wash-types)
2253         (gnus-delete-images 'smiley)
2254       (article-goto-body)
2255       (let ((images (smiley-region (point) (point-max))))
2256         (when images
2257           (gnus-add-wash-type 'smiley)
2258           (dolist (image images)
2259             (gnus-add-image 'smiley image)))))))
2260
2261 (defun gnus-article-remove-images ()
2262   "Remove all images from the article buffer."
2263   (interactive)
2264   (gnus-with-article-buffer
2265     (dolist (elem gnus-article-image-alist)
2266       (gnus-delete-images (car elem)))))
2267
2268 (defun gnus-article-treat-fold-newsgroups ()
2269   "Unfold folded message headers.
2270 Only the headers that fit into the current window width will be
2271 unfolded."
2272   (interactive)
2273   (gnus-with-article-headers
2274     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2275       (save-restriction
2276         (mail-header-narrow-to-field)
2277         (while (re-search-forward ", *" nil t)
2278           (replace-match ", " t t))
2279         (mail-header-fold-field)
2280         (goto-char (point-max))))))
2281
2282 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2283   "Value of `truncate-lines' in Gnus Article buffer.
2284 Valid values are nil, t, `head', `first', `last', an integer or a
2285 predicate.  See Info node `(gnus)Customizing Articles'."
2286   :version "23.1" ;; No Gnus
2287   :group 'gnus-article
2288   ;; :link '(custom-manual "(gnus)Customizing Articles")
2289   :type 'boolean)
2290
2291 (defun gnus-article-toggle-truncate-lines (&optional arg)
2292   "Toggle whether to fold or truncate long lines in article the buffer.
2293 If ARG is non-nil and not a number, toggle
2294 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2295 long lines if and only if arg is positive."
2296   (interactive "P")
2297   (cond
2298    ((and (numberp arg) (> arg 0))
2299     (setq gnus-article-truncate-lines t))
2300    ((numberp arg)
2301     (setq gnus-article-truncate-lines nil))
2302    (arg
2303     (setq gnus-article-truncate-lines
2304           (not gnus-article-truncate-lines))))
2305   (gnus-with-article-buffer
2306     (cond
2307      ((and (numberp arg) (> arg 0))
2308       (setq truncate-lines nil))
2309      ((numberp arg)
2310       (setq truncate-lines t)))
2311     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2312     ;; `toggle-truncate-lines' needs an argument.
2313     (toggle-truncate-lines)))
2314
2315 (defun gnus-article-treat-body-boundary ()
2316   "Place a boundary line at the end of the headers."
2317   (interactive)
2318   (when (and gnus-body-boundary-delimiter
2319              (> (length gnus-body-boundary-delimiter) 0))
2320     (gnus-with-article-headers
2321       (goto-char (point-max))
2322       (let ((start (point)))
2323         (insert "X-Boundary: ")
2324         (gnus-add-text-properties start (point) '(invisible t intangible t))
2325         (insert (let (str)
2326                   (while (>= (window-width) (length str))
2327                     (setq str (concat str gnus-body-boundary-delimiter)))
2328                   (substring str 0 (window-width)))
2329                 "\n")
2330         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2331
2332 (defun article-fill-long-lines ()
2333   "Fill lines that are wider than the window width."
2334   (interactive)
2335   (save-excursion
2336     (let ((inhibit-read-only t)
2337           (width (window-width (get-buffer-window (current-buffer)))))
2338       (save-restriction
2339         (article-goto-body)
2340         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2341           (while (not (eobp))
2342             (end-of-line)
2343             (when (>= (current-column) (min fill-column width))
2344               (narrow-to-region (min (1+ (point)) (point-max))
2345                                 (point-at-bol))
2346               (let ((goback (point-marker)))
2347                 (fill-paragraph nil)
2348                 (goto-char (marker-position goback)))
2349               (widen))
2350             (forward-line 1)))))))
2351
2352 (defun article-capitalize-sentences ()
2353   "Capitalize the first word in each sentence."
2354   (interactive)
2355   (save-excursion
2356     (let ((inhibit-read-only t)
2357           (paragraph-start "^[\n\^L]"))
2358       (article-goto-body)
2359       (while (not (eobp))
2360         (capitalize-word 1)
2361         (forward-sentence)))))
2362
2363 (defun article-remove-cr ()
2364   "Remove trailing CRs and then translate remaining CRs into LFs."
2365   (interactive)
2366   (save-excursion
2367     (let ((inhibit-read-only t))
2368       (goto-char (point-min))
2369       (while (re-search-forward "\r+$" nil t)
2370         (replace-match "" t t))
2371       (goto-char (point-min))
2372       (while (search-forward "\r" nil t)
2373         (replace-match "\n" t t)))))
2374
2375 (defun article-remove-trailing-blank-lines ()
2376   "Remove all trailing blank lines from the article."
2377   (interactive)
2378   (save-excursion
2379     (let ((inhibit-read-only t))
2380       (goto-char (point-max))
2381       (delete-region
2382        (point)
2383        (progn
2384          (while (and (not (bobp))
2385                      (looking-at "^[ \t]*$")
2386                      (not (gnus-annotation-in-region-p
2387                            (point) (point-at-eol))))
2388            (forward-line -1))
2389          (forward-line 1)
2390          (point))))))
2391
2392 (defvar gnus-face-properties-alist)
2393
2394 (defun article-display-face (&optional force)
2395   "Display any Face headers in the header."
2396   (interactive (list 'force))
2397   (let ((wash-face-p buffer-read-only))
2398     (gnus-with-article-headers
2399       ;; When displaying parts, this function can be called several times on
2400       ;; the same article, without any intended toggle semantic (as typing `W
2401       ;; D d' would have). So face deletion must occur only when we come from
2402       ;; an interactive command, that is when the *Article* buffer is
2403       ;; read-only.
2404       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2405           (gnus-delete-images 'face)
2406         (let ((from (message-fetch-field "from"))
2407               face faces)
2408           (save-current-buffer
2409             (when (and wash-face-p
2410                        (gnus-buffer-live-p gnus-original-article-buffer)
2411                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2412               (set-buffer gnus-original-article-buffer))
2413             (save-restriction
2414               (mail-narrow-to-head)
2415               (when (or force
2416                         ;; Check whether this face is censored.
2417                         (not (and gnus-article-x-face-too-ugly
2418                                   (or from
2419                                       (setq from (message-fetch-field "from")))
2420                                   (string-match gnus-article-x-face-too-ugly
2421                                                 from))))
2422                 (while (gnus-article-goto-header "Face")
2423                   (push (mail-header-field-value) faces)))))
2424           (when faces
2425             (goto-char (point-min))
2426             (let (png image)
2427               (unless (setq from (gnus-article-goto-header "from"))
2428                 (insert "From:")
2429                 (setq from (point))
2430                 (insert " [no `from' set]\n"))
2431               (while faces
2432                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2433                   (setq image
2434                         (apply 'gnus-create-image png 'png t
2435                                (cdr (assq 'png gnus-face-properties-alist))))
2436                   (goto-char from)
2437                   (gnus-add-wash-type 'face)
2438                   (gnus-add-image 'face image)
2439                   (gnus-put-image image nil 'face))))))))))
2440
2441 (defun article-display-x-face (&optional force)
2442   "Look for an X-Face header and display it if present."
2443   (interactive (list 'force))
2444   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2445     (gnus-with-article-headers
2446       ;; Delete the old process, if any.
2447       (when (process-status "article-x-face")
2448         (delete-process "article-x-face"))
2449       ;; See the comment in `article-display-face'.
2450       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2451           ;; We have already displayed X-Faces, so we remove them
2452           ;; instead.
2453           (gnus-delete-images 'xface)
2454         ;; Display X-Faces.
2455         (let ((from (message-fetch-field "from"))
2456               x-faces face)
2457           (save-current-buffer
2458             (when (and wash-face-p
2459                        (gnus-buffer-live-p gnus-original-article-buffer)
2460                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2461               ;; If type `W f', use gnus-original-article-buffer,
2462               ;; otherwise use the current buffer because displaying
2463               ;; RFC822 parts calls this function too.
2464               (set-buffer gnus-original-article-buffer))
2465             (save-restriction
2466               (mail-narrow-to-head)
2467               (and gnus-article-x-face-command
2468                    (or force
2469                        ;; Check whether this face is censored.
2470                        (not (and gnus-article-x-face-too-ugly
2471                                  (or from
2472                                      (setq from (message-fetch-field "from")))
2473                                  (string-match gnus-article-x-face-too-ugly
2474                                                from))))
2475                    (while (gnus-article-goto-header "X-Face")
2476                      (push (mail-header-field-value) x-faces)))))
2477           (when x-faces
2478             ;; We display the face.
2479             (cond ((functionp gnus-article-x-face-command)
2480                    ;; The command is a lisp function, so we call it.
2481                    (mapc gnus-article-x-face-command x-faces))
2482                   ((stringp gnus-article-x-face-command)
2483                    ;; The command is a string, so we interpret the command
2484                    ;; as a, well, command, and fork it off.
2485                    (let ((process-connection-type nil))
2486                      (gnus-set-process-query-on-exit-flag
2487                       (start-process
2488                        "article-x-face" nil shell-file-name
2489                        shell-command-switch gnus-article-x-face-command)
2490                       nil)
2491                      ;; Sending multiple EOFs to xv doesn't work,
2492                      ;; so we only do a single external face.
2493                      (with-temp-buffer
2494                        (insert (car x-faces))
2495                        (process-send-region "article-x-face"
2496                                             (point-min) (point-max)))
2497                      (process-send-eof "article-x-face")))
2498                   (t
2499                    (error "`%s' set to `%s' is not a function"
2500                           gnus-article-x-face-command
2501                           'gnus-article-x-face-command)))))))))
2502
2503 (defun article-decode-mime-words ()
2504   "Decode all MIME-encoded words in the article."
2505   (interactive)
2506   (gnus-with-article-buffer
2507     (let ((inhibit-point-motion-hooks t)
2508           (mail-parse-charset gnus-newsgroup-charset)
2509           (mail-parse-ignored-charsets
2510            (with-current-buffer gnus-summary-buffer
2511              gnus-newsgroup-ignored-charsets)))
2512       (mail-decode-encoded-word-region (point-min) (point-max)))))
2513
2514 (defun article-decode-charset (&optional prompt)
2515   "Decode charset-encoded text in the article.
2516 If PROMPT (the prefix), prompt for a coding system to use."
2517   (interactive "P")
2518   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2519         (inhibit-read-only t)
2520         (mail-parse-charset gnus-newsgroup-charset)
2521         (mail-parse-ignored-charsets
2522          (save-excursion (condition-case nil
2523                              (set-buffer gnus-summary-buffer)
2524                            (error))
2525                          gnus-newsgroup-ignored-charsets))
2526         ct cte ctl charset format)
2527     (save-excursion
2528       (save-restriction
2529         (article-narrow-to-head)
2530         (setq ct (message-fetch-field "Content-Type" t)
2531               cte (message-fetch-field "Content-Transfer-Encoding" t)
2532               ctl (and ct (mail-header-parse-content-type ct))
2533               charset (cond
2534                        (prompt
2535                         (mm-read-coding-system "Charset to decode: "))
2536                        (ctl
2537                         (mail-content-type-get ctl 'charset)))
2538               format (and ctl (mail-content-type-get ctl 'format)))
2539         (when cte
2540           (setq cte (mail-header-strip cte)))
2541         (if (and ctl (not (string-match "/" (car ctl))))
2542             (setq ctl nil))
2543         (goto-char (point-max)))
2544       (forward-line 1)
2545       (save-restriction
2546         (narrow-to-region (point) (point-max))
2547         (when (and (eq mail-parse-charset 'gnus-decoded)
2548                    (eq (mm-body-7-or-8) '8bit))
2549           ;; The text code could have been decoded.
2550           (setq charset mail-parse-charset))
2551         (when (and (or (not ctl)
2552                        (equal (car ctl) "text/plain"))
2553                    (not format)) ;; article with format will decode later.
2554           (mm-decode-body
2555            charset (and cte (intern (downcase
2556                                      (gnus-strip-whitespace cte))))
2557            (car ctl)))))))
2558
2559 (defun article-decode-encoded-words ()
2560   "Remove encoded-word encoding from headers."
2561   (let ((inhibit-point-motion-hooks t)
2562         (mail-parse-charset gnus-newsgroup-charset)
2563         (mail-parse-ignored-charsets
2564          (save-excursion (condition-case nil
2565                              (set-buffer gnus-summary-buffer)
2566                            (error))
2567                          gnus-newsgroup-ignored-charsets))
2568         (inhibit-read-only t)
2569         end start)
2570     (goto-char (point-min))
2571     (when (search-forward "\n\n" nil 'move)
2572       (forward-line -1))
2573     (setq end (point))
2574     (while (not (bobp))
2575       (while (progn
2576                (forward-line -1)
2577                (and (not (bobp))
2578                     (memq (char-after) '(?\t ? )))))
2579       (setq start (point))
2580       (if (looking-at "\
2581 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2582 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2583           (funcall gnus-decode-address-function start end)
2584         (funcall gnus-decode-header-function start end))
2585       (goto-char (setq end start)))))
2586
2587 (defun article-decode-group-name ()
2588   "Decode group names in Newsgroups, Followup-To and Xref headers."
2589   (let ((inhibit-point-motion-hooks t)
2590         (inhibit-read-only t)
2591         (method (gnus-find-method-for-group gnus-newsgroup-name))
2592         regexp)
2593     (when (and (or gnus-group-name-charset-method-alist
2594                    gnus-group-name-charset-group-alist)
2595                (gnus-buffer-live-p gnus-original-article-buffer))
2596       (save-restriction
2597         (article-narrow-to-head)
2598         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2599           (with-current-buffer gnus-original-article-buffer
2600             (goto-char (point-min)))
2601           (setq regexp (concat "^" header
2602                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2603           (while (re-search-forward regexp nil t)
2604             (replace-match (save-match-data
2605                              (gnus-decode-newsgroups
2606                               ;; XXX how to use data in article buffer?
2607                               (with-current-buffer gnus-original-article-buffer
2608                                 (re-search-forward regexp nil t)
2609                                 (match-string 1))
2610                               gnus-newsgroup-name method))
2611                            t t nil 1))
2612           (goto-char (point-min)))))))
2613
2614 (autoload 'idna-to-unicode "idna")
2615
2616 (defun article-decode-idna-rhs ()
2617   "Decode IDNA strings in RHS in various headers in current buffer.
2618 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2619 Mail-Reply-To: and Mail-Followup-To:."
2620   (when gnus-use-idna
2621     (save-restriction
2622       (let ((inhibit-point-motion-hooks t)
2623             (inhibit-read-only t))
2624         (article-narrow-to-head)
2625         (goto-char (point-min))
2626         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2627           (let (ace unicode)
2628             (when (save-match-data
2629                     (and (setq ace (match-string 1))
2630                          (save-excursion
2631                            (and (re-search-backward "^[^ \t]" nil t)
2632                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2633                          (setq unicode (idna-to-unicode ace))))
2634               (unless (string= ace unicode)
2635                 (replace-match unicode nil nil nil 1)))))))))
2636
2637 (defun article-de-quoted-unreadable (&optional force read-charset)
2638   "Translate a quoted-printable-encoded article.
2639 If FORCE, decode the article whether it is marked as quoted-printable
2640 or not.
2641 If READ-CHARSET, ask for a coding system."
2642   (interactive (list 'force current-prefix-arg))
2643   (save-excursion
2644     (let ((inhibit-read-only t) type charset)
2645       (if (gnus-buffer-live-p gnus-original-article-buffer)
2646           (with-current-buffer gnus-original-article-buffer
2647             (setq type
2648                   (gnus-fetch-field "content-transfer-encoding"))
2649             (let* ((ct (gnus-fetch-field "content-type"))
2650                    (ctl (and ct (mail-header-parse-content-type ct))))
2651               (setq charset (and ctl
2652                                  (mail-content-type-get ctl 'charset)))
2653               (if (stringp charset)
2654                   (setq charset (intern (downcase charset)))))))
2655       (if read-charset
2656           (setq charset (mm-read-coding-system "Charset: " charset)))
2657       (unless charset
2658         (setq charset gnus-newsgroup-charset))
2659       (when (or force
2660                 (and type (let ((case-fold-search t))
2661                             (string-match "quoted-printable" type))))
2662         (article-goto-body)
2663         (quoted-printable-decode-region
2664          (point) (point-max) (mm-charset-to-coding-system charset))))))
2665
2666 (defun article-de-base64-unreadable (&optional force read-charset)
2667   "Translate a base64 article.
2668 If FORCE, decode the article whether it is marked as base64 not.
2669 If READ-CHARSET, ask for a coding system."
2670   (interactive (list 'force current-prefix-arg))
2671   (save-excursion
2672     (let ((inhibit-read-only t) type charset)
2673       (if (gnus-buffer-live-p gnus-original-article-buffer)
2674           (with-current-buffer gnus-original-article-buffer
2675             (setq type
2676                   (gnus-fetch-field "content-transfer-encoding"))
2677             (let* ((ct (gnus-fetch-field "content-type"))
2678                    (ctl (and ct (mail-header-parse-content-type ct))))
2679               (setq charset (and ctl
2680                                  (mail-content-type-get ctl 'charset)))
2681               (if (stringp charset)
2682                   (setq charset (intern (downcase charset)))))))
2683       (if read-charset
2684           (setq charset (mm-read-coding-system "Charset: " charset)))
2685       (unless charset
2686         (setq charset gnus-newsgroup-charset))
2687       (when (or force
2688                 (and type (let ((case-fold-search t))
2689                             (string-match "base64" type))))
2690         (article-goto-body)
2691         (save-restriction
2692           (narrow-to-region (point) (point-max))
2693           (base64-decode-region (point-min) (point-max))
2694           (mm-decode-coding-region
2695            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2696
2697 (eval-when-compile
2698   (require 'rfc1843))
2699
2700 (defun article-decode-HZ ()
2701   "Translate a HZ-encoded article."
2702   (interactive)
2703   (require 'rfc1843)
2704   (save-excursion
2705     (let ((inhibit-read-only t))
2706       (rfc1843-decode-region (point-min) (point-max)))))
2707
2708 (defun article-unsplit-urls ()
2709   "Remove the newlines that some other mailers insert into URLs."
2710   (interactive)
2711   (save-excursion
2712     (let ((inhibit-read-only t))
2713       (goto-char (point-min))
2714       (while (re-search-forward
2715               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2716         (replace-match "\\1\\3" t)))
2717     (when (interactive-p)
2718       (gnus-treat-article nil))))
2719
2720 (defun article-wash-html ()
2721   "Format an HTML article."
2722   (interactive)
2723   (let ((handles nil)
2724         (buffer-read-only nil))
2725     (when (gnus-buffer-live-p gnus-original-article-buffer)
2726       (setq handles (mm-dissect-buffer t t)))
2727     (article-goto-body)
2728     (delete-region (point) (point-max))
2729     (mm-inline-text-html handles)))
2730
2731 (defvar gnus-article-browse-html-temp-list nil
2732   "List of temporary files created by `gnus-article-browse-html-parts'.
2733 Internal variable.")
2734
2735 (defcustom gnus-article-browse-delete-temp 'ask
2736   "What to do with temporary files from `gnus-article-browse-html-parts'.
2737 If nil, don't delete temporary files.  If it is t, delete them on
2738 exit from the summary buffer.  If it is the symbol `file', query
2739 on each file, if it is `ask' ask once when exiting from the
2740 summary buffer."
2741   :group 'gnus-article
2742   :version "23.1" ;; No Gnus
2743   :type '(choice (const :tag "Don't delete" nil)
2744                  (const :tag "Don't ask" t)
2745                  (const :tag "Ask" ask)
2746                  (const :tag "Ask for each file" file)))
2747
2748 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2749
2750 (defun gnus-article-browse-delete-temp-files (&optional how)
2751   "Delete temp-files created by `gnus-article-browse-html-parts'."
2752   (when (and gnus-article-browse-html-temp-list
2753              (progn
2754                (or how (setq how gnus-article-browse-delete-temp))
2755                (if (eq how 'ask)
2756                    (let ((files (length gnus-article-browse-html-temp-list)))
2757                      (gnus-y-or-n-p (format
2758                                      "Delete all %s temporary HTML file%s? "
2759                                      files
2760                                      (if (> files 1) "s" ""))))
2761                  how)))
2762     (dolist (file gnus-article-browse-html-temp-list)
2763       (cond ((file-directory-p file)
2764              (when (or (not (eq how 'file))
2765                        (gnus-y-or-n-p
2766                         (format
2767                          "Delete temporary HTML file(s) in directory `%s'? "
2768                          (file-name-as-directory file))))
2769                (gnus-delete-directory file)))
2770             ((file-exists-p file)
2771              (when (or (not (eq how 'file))
2772                        (gnus-y-or-n-p
2773                         (format "Delete temporary HTML file `%s'? " file)))
2774                (delete-file file)))))
2775     ;; Also remove file from the list when not deleted or if file doesn't
2776     ;; exist anymore.
2777     (setq gnus-article-browse-html-temp-list nil))
2778   gnus-article-browse-html-temp-list)
2779
2780 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2781   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2782 Return file name."
2783   (save-match-data
2784     (let (file type)
2785       (catch 'found
2786         (dolist (handle handles)
2787           (cond
2788            ((not (listp handle)))
2789            ((equal (mm-handle-media-supertype handle) "multipart")
2790             (when (setq file (gnus-article-browse-html-save-cid-content
2791                               cid handle directory))
2792               (throw 'found file)))
2793            ((equal (concat "<" cid ">") (mm-handle-id handle))
2794             (setq file
2795                   (expand-file-name
2796                    (or (mail-content-type-get
2797                         (mm-handle-disposition handle) 'filename)
2798                        (mail-content-type-get
2799                         (setq type (mm-handle-type handle)) 'name)
2800                        (concat
2801                         (make-temp-name "cid")
2802                         (car (rassoc (car type) mailcap-mime-extensions))))
2803                    directory))
2804             (mm-save-part-to-file handle file)
2805             (throw 'found file))))))))
2806
2807 (defun gnus-article-browse-html-parts (list &optional header)
2808   "View all \"text/html\" parts from LIST.
2809 Recurse into multiparts.  The optional HEADER that should be a decoded
2810 message header will be added to the bodies of the \"text/html\" parts."
2811   ;; Internal function used by `gnus-article-browse-html-article'.
2812   (let (type file charset content cid-dir tmp-file showed)
2813     ;; Find and show the html-parts.
2814     (dolist (handle list)
2815       ;; If HTML, show it:
2816       (cond ((not (listp handle)))
2817             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2818                  (and (equal (car type) "message/external-body")
2819                       (or header
2820                           (setq file (or (mail-content-type-get type 'name)
2821                                          (mail-content-type-get
2822                                           (mm-handle-disposition handle)
2823                                           'filename))))
2824                       (or (mm-handle-cache handle)
2825                           (condition-case code
2826                               (progn (mm-extern-cache-contents handle) t)
2827                             (error
2828                              (gnus-message 3 "%s" (error-message-string code))
2829                              (when (>= gnus-verbose 3) (sit-for 2))
2830                              nil)))
2831                       (progn
2832                         (setq handle (mm-handle-cache handle)
2833                               type (mm-handle-type handle))
2834                         (equal (car type) "text/html"))))
2835              (setq charset (mail-content-type-get type 'charset)
2836                    content (mm-get-part handle))
2837              (with-temp-buffer
2838                (if (eq charset 'gnus-decoded)
2839                    (mm-enable-multibyte)
2840                  (mm-disable-multibyte))
2841                (insert content)
2842                ;; resolve cid contents
2843                (let ((case-fold-search t)
2844                      cid-file)
2845                  (goto-char (point-min))
2846                  (while (re-search-forward "\
2847 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2848                                            nil t)
2849                    (unless cid-dir
2850                      (setq cid-dir (mm-make-temp-file "cid" t))
2851                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2852                    (setq file nil
2853                          content nil)
2854                    (when (setq cid-file
2855                                (gnus-article-browse-html-save-cid-content
2856                                 (match-string 2)
2857                                 (with-current-buffer gnus-article-buffer
2858                                   gnus-article-mime-handles)
2859                                 cid-dir))
2860                      (replace-match (concat "file://" cid-file)
2861                                     nil nil nil 1))))
2862                (unless content (setq content (buffer-string))))
2863              (when (or charset header (not file))
2864                (setq tmp-file (mm-make-temp-file
2865                                ;; Do we need to care for 8.3 filenames?
2866                                "mm-" nil ".html")))
2867              ;; Add a meta html tag to specify charset and a header.
2868              (cond
2869               (header
2870                (let (title eheader body hcharset coding force-charset)
2871                  (with-temp-buffer
2872                    (mm-enable-multibyte)
2873                    (setq case-fold-search t)
2874                    (insert header "\n")
2875                    (setq title (message-fetch-field "subject"))
2876                    (goto-char (point-min))
2877                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2878                      (replace-match (cond ((match-beginning 1) "&lt;")
2879                                           ((match-beginning 2) "&gt;")
2880                                           (t "&amp;"))))
2881                    (goto-char (point-min))
2882                    (insert "<pre>\n")
2883                    (goto-char (point-max))
2884                    (insert "</pre>\n<hr>\n")
2885                    ;; We have to examine charset one by one since
2886                    ;; charset specified in parts might be different.
2887                    (if (eq charset 'gnus-decoded)
2888                        (setq charset 'utf-8
2889                              eheader (mm-encode-coding-string (buffer-string)
2890                                                               charset)
2891                              title (when title
2892                                      (mm-encode-coding-string title charset))
2893                              body (mm-encode-coding-string content charset)
2894                              force-charset t)
2895                      (setq hcharset (mm-find-mime-charset-region (point-min)
2896                                                                  (point-max)))
2897                      (cond ((= (length hcharset) 1)
2898                             (setq hcharset (car hcharset)
2899                                   coding (mm-charset-to-coding-system
2900                                           hcharset)))
2901                            ((> (length hcharset) 1)
2902                             (setq hcharset 'utf-8
2903                                   coding hcharset)))
2904                      (if coding
2905                          (if charset
2906                              (progn
2907                                (setq body
2908                                      (mm-charset-to-coding-system charset))
2909                                (if (eq coding body)
2910                                    (setq eheader (mm-encode-coding-string
2911                                                   (buffer-string) coding)
2912                                          title (when title
2913                                                  (mm-encode-coding-string
2914                                                   title coding))
2915                                          body content)
2916                                  (setq charset 'utf-8
2917                                        eheader (mm-encode-coding-string
2918                                                 (buffer-string) charset)
2919                                        title (when title
2920                                                (mm-encode-coding-string
2921                                                 title charset))
2922                                        body (mm-encode-coding-string
2923                                              (mm-decode-coding-string
2924                                               content body)
2925                                              charset)
2926                                        force-charset t)))
2927                            (setq charset hcharset
2928                                  eheader (mm-encode-coding-string
2929                                           (buffer-string) coding)
2930                                  title (when title
2931                                          (mm-encode-coding-string
2932                                           title coding))
2933                                  body content))
2934                        (setq eheader (mm-string-as-unibyte (buffer-string))
2935                              body content)))
2936                    (erase-buffer)
2937                    (mm-disable-multibyte)
2938                    (insert body)
2939                    (when charset
2940                      (mm-add-meta-html-tag handle charset force-charset))
2941                    (when title
2942                      (goto-char (point-min))
2943                      (unless (search-forward "<title>" nil t)
2944                        (re-search-forward "<head>\\s-*" nil t)
2945                        (insert "<title>" title "</title>\n")))
2946                    (goto-char (point-min))
2947                    (or (re-search-forward
2948                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2949                        (re-search-forward
2950                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2951                    (insert eheader)
2952                    (mm-write-region (point-min) (point-max)
2953                                     tmp-file nil nil nil 'binary t))))
2954               (charset
2955                (mm-with-unibyte-buffer
2956                  (insert (if (eq charset 'gnus-decoded)
2957                              (mm-encode-coding-string content
2958                                                       (setq charset 'utf-8))
2959                            content))
2960                  (if (or (mm-add-meta-html-tag handle charset)
2961                          (not file))
2962                      (mm-write-region (point-min) (point-max)
2963                                       tmp-file nil nil nil 'binary t)
2964                    (setq tmp-file nil))))
2965               (tmp-file
2966                (mm-save-part-to-file handle tmp-file)))
2967              (when tmp-file
2968                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2969              (add-hook 'gnus-summary-prepare-exit-hook
2970                        'gnus-article-browse-delete-temp-files)
2971              (add-hook 'gnus-exit-gnus-hook
2972                        (lambda  ()
2973                          (gnus-article-browse-delete-temp-files t)))
2974              ;; FIXME: Warn if there's an <img> tag?
2975              (browse-url-of-file (or tmp-file (expand-file-name file)))
2976              (setq showed t))
2977             ;; If multipart, recurse
2978             ((equal (mm-handle-media-supertype handle) "multipart")
2979              (when (gnus-article-browse-html-parts handle header)
2980                (setq showed t)))
2981             ((equal (mm-handle-media-type handle) "message/rfc822")
2982              (mm-with-multibyte-buffer
2983                (mm-insert-part handle)
2984                (setq handle (mm-dissect-buffer t t))
2985                (when (and (bufferp (car handle))
2986                           (stringp (car (mm-handle-type handle))))
2987                  (setq handle (list handle)))
2988                (when header
2989                  (article-decode-encoded-words)
2990                  (let ((gnus-visible-headers
2991                         (or (get 'gnus-visible-headers 'standard-value)
2992                             gnus-visible-headers)))
2993                    (article-hide-headers))
2994                  (goto-char (point-min))
2995                  (search-forward "\n\n" nil 'move)
2996                  (skip-chars-backward "\t\n ")
2997                  (setq header (buffer-substring (point-min) (point)))))
2998              (when (prog1
2999                        (gnus-article-browse-html-parts handle header)
3000                      (mm-destroy-parts handle))
3001                (setq showed t)))))
3002     showed))
3003
3004 (defun gnus-article-browse-html-article (&optional arg)
3005   "View \"text/html\" parts of the current article with a WWW browser.
3006 Inline images embedded in a message using the cid scheme, as they are
3007 generally considered to be safe, will be processed properly.
3008 The message header is added to the beginning of every html part unless
3009 the prefix argument ARG is given.
3010
3011 Warning: Spammers use links to images (using the http scheme) in HTML
3012 articles to verify whether you have read the message.  As
3013 `gnus-article-browse-html-article' passes the HTML content to the
3014 browser without eliminating these \"web bugs\" you should only
3015 use it for mails from trusted senders.
3016
3017 If you always want to display HTML parts in the browser, set
3018 `mm-text-html-renderer' to nil.
3019
3020 This command creates temporary files to pass HTML contents including
3021 images if any to the browser, and deletes them when exiting the group
3022 \(if you want)."
3023   ;; Cf. `mm-w3m-safe-url-regexp'
3024   (interactive "P")
3025   (if arg
3026       (gnus-summary-show-article)
3027     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3028                                     gnus-visible-headers))
3029           ;; As we insert a <hr>, there's no need for the body boundary.
3030           (gnus-treat-body-boundary nil))
3031       (gnus-summary-show-article)))
3032   (with-current-buffer gnus-article-buffer
3033     (let ((header (unless arg
3034                     (save-restriction
3035                       (widen)
3036                       (buffer-substring-no-properties
3037                        (goto-char (point-min))
3038                        (if (search-forward "\n\n" nil t)
3039                            (match-beginning 0)
3040                          (goto-char (point-max))
3041                          (skip-chars-backward "\t\n ")
3042                          (point))))))
3043           parts)
3044       (set-buffer gnus-original-article-buffer)
3045       (setq parts (mm-dissect-buffer t t))
3046       ;; If singlepart, enforce a list.
3047       (when (and (bufferp (car parts))
3048                  (stringp (car (mm-handle-type parts))))
3049         (setq parts (list parts)))
3050       ;; Process the list
3051       (unless (gnus-article-browse-html-parts parts header)
3052         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3053       (mm-destroy-parts parts)
3054       (unless arg
3055         (gnus-summary-show-article)))))
3056
3057 (defun article-hide-list-identifiers ()
3058   "Remove list identifies from the Subject header.
3059 The `gnus-list-identifiers' variable specifies what to do."
3060   (interactive)
3061   (let ((inhibit-point-motion-hooks t)
3062         (regexp (if (consp gnus-list-identifiers)
3063                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3064                   gnus-list-identifiers))
3065         (inhibit-read-only t))
3066     (when regexp
3067       (save-excursion
3068         (save-restriction
3069           (article-narrow-to-head)
3070           (goto-char (point-min))
3071           (while (re-search-forward
3072                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3073                   nil t)
3074             (delete-region (match-beginning 2) (match-end 0))
3075             (beginning-of-line))
3076           (when (re-search-forward
3077                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3078             (delete-region (match-beginning 1) (match-end 1))))))))
3079
3080 (defun article-hide-pem (&optional arg)
3081   "Toggle hiding of any PEM headers and signatures in the current article.
3082 If given a negative prefix, always show; if given a positive prefix,
3083 always hide."
3084   (interactive (gnus-article-hidden-arg))
3085   (unless (gnus-article-check-hidden-text 'pem arg)
3086     (save-excursion
3087       (let ((inhibit-read-only t) end)
3088         (goto-char (point-min))
3089         ;; Hide the horrendously ugly "header".
3090         (when (and (search-forward
3091                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3092                     nil t)
3093                    (setq end (1+ (match-beginning 0))))
3094           (gnus-add-wash-type 'pem)
3095           (gnus-article-hide-text-type
3096            end
3097            (if (search-forward "\n\n" nil t)
3098                (match-end 0)
3099              (point-max))
3100            'pem)
3101           ;; Hide the trailer as well
3102           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3103                                 nil t)
3104             (gnus-article-hide-text-type
3105              (match-beginning 0) (match-end 0) 'pem)))))))
3106
3107 (defun article-strip-banner ()
3108   "Strip the banners specified by the `banner' group parameter and by
3109 `gnus-article-address-banner-alist'."
3110   (interactive)
3111   (save-excursion
3112     (save-restriction
3113       (let ((inhibit-point-motion-hooks t))
3114         (when (gnus-parameter-banner gnus-newsgroup-name)
3115           (article-really-strip-banner
3116            (gnus-parameter-banner gnus-newsgroup-name)))
3117         (when gnus-article-address-banner-alist
3118           ;; Note that the From header is decoded here, so it is
3119           ;; required that the *-extract-address-components function
3120           ;; supports non-ASCII text.
3121           (let ((from (save-restriction
3122                         (widen)
3123                         (article-narrow-to-head)
3124                         (mail-fetch-field "from"))))
3125             (when (and from
3126                        (setq from
3127                              (cadr (funcall gnus-extract-address-components
3128                                             from))))
3129               (catch 'found
3130                 (dolist (pair gnus-article-address-banner-alist)
3131                   (when (string-match (car pair) from)
3132                     (throw 'found
3133                            (article-really-strip-banner (cdr pair)))))))))))))
3134
3135 (defun article-really-strip-banner (banner)
3136   "Strip the banner specified by the argument."
3137   (save-excursion
3138     (save-restriction
3139       (let ((inhibit-point-motion-hooks t)
3140             (gnus-signature-limit nil)
3141             (inhibit-read-only t))
3142         (article-goto-body)
3143         (cond
3144          ((eq banner 'signature)
3145           (when (gnus-article-narrow-to-signature)
3146             (widen)
3147             (forward-line -1)
3148             (delete-region (point) (point-max))))
3149          ((symbolp banner)
3150           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3151               (while (re-search-forward banner nil t)
3152                 (delete-region (match-beginning 0) (match-end 0)))))
3153          ((stringp banner)
3154           (while (re-search-forward banner nil t)
3155             (delete-region (match-beginning 0) (match-end 0)))))))))
3156
3157 (defun article-babel ()
3158   "Translate article using an online translation service."
3159   (interactive)
3160   (require 'babel)
3161   (gnus-with-article-buffer
3162     (when (article-goto-body)
3163       (let* ((start (point))
3164              (end (point-max))
3165              (orig (buffer-substring start end))
3166              (trans (babel-as-string orig)))
3167         (save-restriction
3168           (narrow-to-region start end)
3169           (delete-region start end)
3170           (insert trans))))))
3171
3172 (defun article-hide-signature (&optional arg)
3173   "Hide the signature in the current article.
3174 If given a negative prefix, always show; if given a positive prefix,
3175 always hide."
3176   (interactive (gnus-article-hidden-arg))
3177   (unless (gnus-article-check-hidden-text 'signature arg)
3178     (save-excursion
3179       (save-restriction
3180         (let ((inhibit-read-only t))
3181           (when (gnus-article-narrow-to-signature)
3182             (gnus-article-hide-text-type
3183              (point-min) (point-max) 'signature))))))
3184   (gnus-set-mode-line 'article))
3185
3186 (defun article-strip-headers-in-body ()
3187   "Strip offensive headers from bodies."
3188   (interactive)
3189   (save-excursion
3190     (article-goto-body)
3191     (let ((case-fold-search t))
3192       (when (looking-at "x-no-archive:")
3193         (gnus-delete-line)))))
3194
3195 (defun article-strip-leading-blank-lines ()
3196   "Remove all blank lines from the beginning of the article."
3197   (interactive)
3198   (save-excursion
3199     (let ((inhibit-point-motion-hooks t)
3200           (inhibit-read-only t))
3201       (when (article-goto-body)
3202         (while (and (not (eobp))
3203                     (looking-at "[ \t]*$"))
3204           (gnus-delete-line))))))
3205
3206 (defun article-narrow-to-head ()
3207   "Narrow the buffer to the head of the message.
3208 Point is left at the beginning of the narrowed-to region."
3209   (narrow-to-region
3210    (goto-char (point-min))
3211    (if (search-forward "\n\n" nil 1)
3212        (1- (point))
3213      (point-max)))
3214   (goto-char (point-min)))
3215
3216 (defun article-goto-body ()
3217   "Place point at the start of the body."
3218   (goto-char (point-min))
3219   (cond
3220    ;; This variable is only bound when dealing with separate
3221    ;; MIME body parts.
3222    (article-goto-body-goes-to-point-min-p
3223     t)
3224    ((search-forward "\n\n" nil t)
3225     t)
3226    (t
3227     (goto-char (point-max))
3228     nil)))
3229
3230 (defun article-strip-multiple-blank-lines ()
3231   "Replace consecutive blank lines with one empty line."
3232   (interactive)
3233   (save-excursion
3234     (let ((inhibit-point-motion-hooks t)
3235           (inhibit-read-only t))
3236       ;; First make all blank lines empty.
3237       (article-goto-body)
3238       (while (re-search-forward "^[ \t]+$" nil t)
3239         (unless (gnus-annotation-in-region-p
3240                  (match-beginning 0) (match-end 0))
3241           (replace-match "" nil t)))
3242       ;; Then replace multiple empty lines with a single empty line.
3243       (article-goto-body)
3244       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3245         (unless (gnus-annotation-in-region-p
3246                  (match-beginning 0) (match-end 0))
3247           (delete-region (match-beginning 1) (match-end 1)))))))
3248
3249 (defun article-strip-leading-space ()
3250   "Remove all white space from the beginning of the lines in the article."
3251   (interactive)
3252   (save-excursion
3253     (let ((inhibit-point-motion-hooks t)
3254           (inhibit-read-only t))
3255       (article-goto-body)
3256       (while (re-search-forward "^[ \t]+" nil t)
3257         (replace-match "" t t)))))
3258
3259 (defun article-strip-trailing-space ()
3260   "Remove all white space from the end of the lines in the article."
3261   (interactive)
3262   (save-excursion
3263     (let ((inhibit-point-motion-hooks t)
3264           (inhibit-read-only t))
3265       (article-goto-body)
3266       (while (re-search-forward "[ \t]+$" nil t)
3267         (replace-match "" t t)))))
3268
3269 (defun article-strip-blank-lines ()
3270   "Strip leading, trailing and multiple blank lines."
3271   (interactive)
3272   (article-strip-leading-blank-lines)
3273   (article-remove-trailing-blank-lines)
3274   (article-strip-multiple-blank-lines))
3275
3276 (defun article-strip-all-blank-lines ()
3277   "Strip all blank lines."
3278   (interactive)
3279   (save-excursion
3280     (let ((inhibit-point-motion-hooks t)
3281           (inhibit-read-only t))
3282       (article-goto-body)
3283       (while (re-search-forward "^[ \t]*\n" nil t)
3284         (replace-match "" t t)))))
3285
3286 (defun gnus-article-narrow-to-signature ()
3287   "Narrow to the signature; return t if a signature is found, else nil."
3288   (let ((inhibit-point-motion-hooks t))
3289     (when (gnus-article-search-signature)
3290       (forward-line 1)
3291       ;; Check whether we have some limits to what we consider
3292       ;; to be a signature.
3293       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3294                       (list gnus-signature-limit)))
3295             limit limited)
3296         (while (setq limit (pop limits))
3297           (if (or (and (integerp limit)
3298                        (< (- (point-max) (point)) limit))
3299                   (and (floatp limit)
3300                        (< (count-lines (point) (point-max)) limit))
3301                   (and (functionp limit)
3302                        (funcall limit))
3303                   (and (stringp limit)
3304                        (not (re-search-forward limit nil t))))
3305               ()                        ; This limit did not succeed.
3306             (setq limited t
3307                   limits nil)))
3308         (unless limited
3309           (narrow-to-region (point) (point-max))
3310           t)))))
3311
3312 (defun gnus-article-search-signature ()
3313   "Search the current buffer for the signature separator.
3314 Put point at the beginning of the signature separator."
3315   (let ((cur (point)))
3316     (goto-char (point-max))
3317     (if (if (stringp gnus-signature-separator)
3318             (re-search-backward gnus-signature-separator nil t)
3319           (let ((seps gnus-signature-separator))
3320             (while (and seps
3321                         (not (re-search-backward (car seps) nil t)))
3322               (pop seps))
3323             seps))
3324         t
3325       (goto-char cur)
3326       nil)))
3327
3328 (defun gnus-article-hidden-arg ()
3329   "Return the current prefix arg as a number, or 0 if no prefix."
3330   (list (if current-prefix-arg
3331             (prefix-numeric-value current-prefix-arg)
3332           0)))
3333
3334 (defun gnus-article-check-hidden-text (type arg)
3335   "Return nil if hiding is necessary.
3336 Arg can be nil or a number.  nil and positive means hide, negative
3337 means show, 0 means toggle."
3338   (save-excursion
3339     (save-restriction
3340       (let ((hide (gnus-article-hidden-text-p type)))
3341         (cond
3342          ((or (null arg)
3343               (> arg 0))
3344           nil)
3345          ((< arg 0)
3346           (gnus-article-show-hidden-text type)
3347           t)
3348          (t
3349           (if (eq hide 'hidden)
3350               (progn
3351                 (gnus-article-show-hidden-text type)
3352                 t)
3353             nil)))))))
3354
3355 (defun gnus-article-hidden-text-p (type)
3356   "Say whether the current buffer contains hidden text of type TYPE."
3357   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3358     (while (and pos
3359                 (not (get-text-property pos 'invisible))
3360                 (not (get-text-property pos 'dummy-invisible)))
3361       (setq pos
3362             (text-property-any (1+ pos) (point-max) 'article-type type)))
3363     (if pos
3364         'hidden
3365       nil)))
3366
3367 (defun gnus-article-show-hidden-text (type &optional dummy)
3368   "Show all hidden text of type TYPE.
3369 Originally it is hide instead of DUMMY."
3370   (let ((inhibit-read-only t)
3371         (inhibit-point-motion-hooks t))
3372     (gnus-remove-text-properties-when
3373      'article-type type
3374      (point-min) (point-max)
3375      (cons 'article-type (cons type
3376                                gnus-hidden-properties)))
3377     (gnus-delete-wash-type type)))
3378
3379 (defconst article-time-units
3380   `((year . ,(* 365.25 24 60 60))
3381     (week . ,(* 7 24 60 60))
3382     (day . ,(* 24 60 60))
3383     (hour . ,(* 60 60))
3384     (minute . 60)
3385     (second . 1))
3386   "Mapping from time units to seconds.")
3387
3388 (defun gnus-article-forward-header ()
3389   "Move point to the start of the next header.
3390 If the current header is a continuation header, this can be several
3391 lines forward."
3392   (let ((ended nil))
3393     (while (not ended)
3394       (forward-line 1)
3395       (if (looking-at "[ \t]+[^ \t]")
3396           (forward-line 1)
3397         (setq ended t)))))
3398
3399 (defun article-date-ut (&optional type highlight)
3400   "Convert DATE date to universal time in the current article.
3401 If TYPE is `local', convert to local time; if it is `lapsed', output
3402 how much time has lapsed since DATE.  For `lapsed', the value of
3403 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3404 should replace the \"Date:\" one, or should be added below it."
3405   (interactive (list 'ut t))
3406   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3407          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3408                              tdate-regexp)
3409                             ((eq type 'lapsed)
3410                              "^X-Sent:[ \t]")
3411                             (article-lapsed-timer
3412                              "^Date:[ \t]")
3413                             (t
3414                              tdate-regexp)))
3415          (case-fold-search t)
3416          (inhibit-read-only t)
3417          (inhibit-point-motion-hooks t)
3418          pos date bface eface)
3419     (save-excursion
3420       (save-restriction
3421         (widen)
3422         (goto-char (point-min))
3423         (while (or (setq date (get-text-property (setq pos (point))
3424                                                  'original-date))
3425                    (when (setq pos (next-single-property-change
3426                                     (point) 'original-date))
3427                      (setq date (get-text-property pos 'original-date))
3428                      t))
3429           (narrow-to-region
3430            pos (if (setq pos (text-property-any pos (point-max)
3431                                                 'original-date nil))
3432                    (progn
3433                      (goto-char pos)
3434                      (if (or (bolp) (eobp))
3435                          (point)
3436                        (1+ (point))))
3437                  (point-max)))
3438           (goto-char (point-min))
3439           (when (re-search-forward tdate-regexp nil t)
3440             (setq bface (get-text-property (point-at-bol) 'face)
3441                   eface (get-text-property (1- (point-at-eol)) 'face)))
3442           (goto-char (point-min))
3443           (setq pos nil)
3444           ;; Delete any old Date headers.
3445           (while (re-search-forward date-regexp nil t)
3446             (if pos
3447                 (delete-region (point-at-bol) (progn
3448                                                 (gnus-article-forward-header)
3449                                                 (point)))
3450               (delete-region (point-at-bol) (progn
3451                                               (gnus-article-forward-header)
3452                                               (forward-char -1)
3453                                               (point)))
3454               (setq pos (point))))
3455           (when (and (not pos)
3456                      (re-search-forward tdate-regexp nil t))
3457             (forward-line 1))
3458           (gnus-goto-char pos)
3459           (insert (article-make-date-line date (or type 'ut)))
3460           (unless pos
3461             (insert "\n")
3462             (forward-line -1))
3463           ;; Do highlighting.
3464           (beginning-of-line)
3465           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3466             (put-text-property (match-beginning 1) (1+ (match-end 1))
3467                                'face bface)
3468             (put-text-property (match-beginning 2) (match-end 2)
3469                                'face eface))
3470           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3471           (goto-char (point-max))
3472           (widen))))))
3473
3474 (defun article-make-date-line (date type)
3475   "Return a DATE line of TYPE."
3476   (unless (memq type '(local ut original user iso8601 lapsed english))
3477     (error "Unknown conversion type: %s" type))
3478   (condition-case ()
3479       (let ((time (date-to-time date)))
3480         (cond
3481          ;; Convert to the local timezone.
3482          ((eq type 'local)
3483           (concat "Date: " (message-make-date time)))
3484          ;; Convert to Universal Time.
3485          ((eq type 'ut)
3486           (concat "Date: "
3487                   (substring
3488                    (message-make-date
3489                     (let* ((e (parse-time-string date))
3490                            (tm (apply 'encode-time e))
3491                            (ms (car tm))
3492                            (ls (- (cadr tm) (car (current-time-zone time)))))
3493                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3494                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3495                             (t (list ms ls)))))
3496                    0 -5)
3497                   "UT"))
3498          ;; Get the original date from the article.
3499          ((eq type 'original)
3500           (concat "Date: " (if (string-match "\n+$" date)
3501                                (substring date 0 (match-beginning 0))
3502                              date)))
3503          ;; Let the user define the format.
3504          ((eq type 'user)
3505           (let ((format (or (condition-case nil
3506                                 (with-current-buffer gnus-summary-buffer
3507                                   gnus-article-time-format)
3508                               (error nil))
3509                             gnus-article-time-format)))
3510             (if (functionp format)
3511                 (funcall format time)
3512               (concat "Date: " (format-time-string format time)))))
3513          ;; ISO 8601.
3514          ((eq type 'iso8601)
3515           (let ((tz (car (current-time-zone time))))
3516             (concat
3517              "Date: "
3518              (format-time-string "%Y%m%dT%H%M%S" time)
3519              (format "%s%02d%02d"
3520                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3521                      (/ (% (abs tz) 3600) 60)))))
3522          ;; Do an X-Sent lapsed format.
3523          ((eq type 'lapsed)
3524           ;; If the date is seriously mangled, the timezone functions are
3525           ;; liable to bug out, so we ignore all errors.
3526           (let* ((now (current-time))
3527                  (real-time (subtract-time now time))
3528                  (real-sec (and real-time
3529                                 (+ (* (float (car real-time)) 65536)
3530                                    (cadr real-time))))
3531                  (sec (and real-time (abs real-sec)))
3532                  num prev)
3533             (cond
3534              ((null real-time)
3535               "X-Sent: Unknown")
3536              ((zerop sec)
3537               "X-Sent: Now")
3538              (t
3539               (concat
3540                "X-Sent: "
3541                ;; This is a bit convoluted, but basically we go
3542                ;; through the time units for years, weeks, etc,
3543                ;; and divide things to see whether that results
3544                ;; in positive answers.
3545                (mapconcat
3546                 (lambda (unit)
3547                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3548                       ;; The (remaining) seconds are too few to
3549                       ;; be divided into this time unit.
3550                       ""
3551                     ;; It's big enough, so we output it.
3552                     (setq sec (- sec (* num (cdr unit))))
3553                     (prog1
3554                         (concat (if prev ", " "") (int-to-string
3555                                                    (floor num))
3556                                 " " (symbol-name (car unit))
3557                                 (if (> num 1) "s" ""))
3558                       (setq prev t))))
3559                 article-time-units "")
3560                ;; If dates are odd, then it might appear like the
3561                ;; article was sent in the future.
3562                (if (> real-sec 0)
3563                    " ago"
3564                  " in the future"))))))
3565          ;; Display the date in proper English
3566          ((eq type 'english)
3567           (let ((dtime (decode-time time)))
3568             (concat
3569              "Date: the "
3570              (number-to-string (nth 3 dtime))
3571              (let ((digit (% (nth 3 dtime) 10)))
3572                (cond
3573                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3574                 ((= digit 1) "st")
3575                 ((= digit 2) "nd")
3576                 ((= digit 3) "rd")
3577                 (t "th")))
3578              " of "
3579              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3580              " "
3581              (number-to-string (nth 5 dtime))
3582              " at "
3583              (format "%02d" (nth 2 dtime))
3584              ":"
3585              (format "%02d" (nth 1 dtime)))))))
3586     (error
3587      (format "Date: %s (from Gnus)" date))))
3588
3589 (defun article-date-local (&optional highlight)
3590   "Convert the current article date to the local timezone."
3591   (interactive (list t))
3592   (article-date-ut 'local highlight))
3593
3594 (defun article-date-english (&optional highlight)
3595   "Convert the current article date to something that is proper English."
3596   (interactive (list t))
3597   (article-date-ut 'english highlight))
3598
3599 (defun article-date-original (&optional highlight)
3600   "Convert the current article date to what it was originally.
3601 This is only useful if you have used some other date conversion
3602 function and want to see what the date was before converting."
3603   (interactive (list t))
3604   (article-date-ut 'original highlight))
3605
3606 (defun article-date-lapsed (&optional highlight)
3607   "Convert the current article date to time lapsed since it was sent."
3608   (interactive (list t))
3609   (article-date-ut 'lapsed highlight))
3610
3611 (defun article-update-date-lapsed ()
3612   "Function to be run from a timer to update the lapsed time line."
3613   (save-match-data
3614     (let (deactivate-mark)
3615       (save-excursion
3616         (ignore-errors
3617          (walk-windows
3618           (lambda (w)
3619             (set-buffer (window-buffer w))
3620             (when (eq major-mode 'gnus-article-mode)
3621               (let ((mark (point-marker)))
3622                 (goto-char (point-min))
3623                 (when (re-search-forward "^X-Sent:" nil t)
3624                   (article-date-lapsed t))
3625                 (goto-char (marker-position mark))
3626                 (move-marker mark nil))))
3627           nil 'visible))))))
3628
3629 (defun gnus-start-date-timer (&optional n)
3630   "Start a timer to update the X-Sent header in the article buffers.
3631 The numerical prefix says how frequently (in seconds) the function
3632 is to run."
3633   (interactive "p")
3634   (unless n
3635     (setq n 1))
3636   (gnus-stop-date-timer)
3637   (setq article-lapsed-timer
3638         (run-at-time 1 n 'article-update-date-lapsed)))
3639
3640 (defun gnus-stop-date-timer ()
3641   "Stop the X-Sent timer."
3642   (interactive)
3643   (when article-lapsed-timer
3644     (nnheader-cancel-timer article-lapsed-timer)
3645     (setq article-lapsed-timer nil)))
3646
3647 (defun article-date-user (&optional highlight)
3648   "Convert the current article date to the user-defined format.
3649 This format is defined by the `gnus-article-time-format' variable."
3650   (interactive (list t))
3651   (article-date-ut 'user highlight))
3652
3653 (defun article-date-iso8601 (&optional highlight)
3654   "Convert the current article date to ISO8601."
3655   (interactive (list t))
3656   (article-date-ut 'iso8601 highlight))
3657
3658 (defmacro gnus-article-save-original-date (&rest forms)
3659   "Save the original date as a text property and evaluate FORMS."
3660   `(let* ((case-fold-search t)
3661           (start (progn
3662                    (goto-char (point-min))
3663                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3664                               (not (bolp)))
3665                      (match-end 0))))
3666           (date (when (and start
3667                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3668                                               nil t))
3669                   (buffer-substring-no-properties start
3670                                                   (match-beginning 0)))))
3671      (goto-char (point-max))
3672      (skip-chars-backward "\n")
3673      (put-text-property (point-min) (point) 'original-date date)
3674      ,@forms
3675      (goto-char (point-max))
3676      (skip-chars-backward "\n")
3677      (put-text-property (point-min) (point) 'original-date date)))
3678
3679 ;; (defun article-show-all ()
3680 ;;   "Show all hidden text in the article buffer."
3681 ;;   (interactive)
3682 ;;   (save-excursion
3683 ;;     (let ((inhibit-read-only t))
3684 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3685
3686 (defun article-remove-leading-whitespace ()
3687   "Remove excessive whitespace from all headers."
3688   (interactive)
3689   (save-excursion
3690     (save-restriction
3691       (let ((inhibit-read-only t))
3692         (article-narrow-to-head)
3693         (goto-char (point-min))
3694         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3695           (delete-region (match-beginning 1) (match-end 1)))))))
3696
3697 (defun article-emphasize (&optional arg)
3698   "Emphasize text according to `gnus-emphasis-alist'."
3699   (interactive (gnus-article-hidden-arg))
3700   (unless (gnus-article-check-hidden-text 'emphasis arg)
3701     (save-excursion
3702       (let ((alist (or
3703                     (condition-case nil
3704                         (with-current-buffer gnus-summary-buffer
3705                           gnus-article-emphasis-alist)
3706                       (error))
3707                     gnus-emphasis-alist))
3708             (inhibit-read-only t)
3709             (props (append '(article-type emphasis)
3710                            gnus-hidden-properties))
3711             regexp elem beg invisible visible face)
3712         (article-goto-body)
3713         (setq beg (point))
3714         (while (setq elem (pop alist))
3715           (goto-char beg)
3716           (setq regexp (car elem)
3717                 invisible (nth 1 elem)
3718                 visible (nth 2 elem)
3719                 face (nth 3 elem))
3720           (while (re-search-forward regexp nil t)
3721             (when (and (match-beginning visible) (match-beginning invisible))
3722               (gnus-article-hide-text
3723                (match-beginning invisible) (match-end invisible) props)
3724               (gnus-article-unhide-text-type
3725                (match-beginning visible) (match-end visible) 'emphasis)
3726               (gnus-put-overlay-excluding-newlines
3727                (match-beginning visible) (match-end visible) 'face face)
3728               (gnus-add-wash-type 'emphasis)
3729               (goto-char (match-end invisible)))))))))
3730
3731 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3732   "Setup newsgroup emphasis alist."
3733   (unless gnus-article-emphasis-alist
3734     (let ((name (and gnus-newsgroup-name
3735                      (gnus-group-real-name gnus-newsgroup-name))))
3736       (make-local-variable 'gnus-article-emphasis-alist)
3737       (setq gnus-article-emphasis-alist
3738             (nconc
3739              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3740                (while (setq elem (pop alist))
3741                  (when (and name (string-match (car elem) name))
3742                    (setq alist nil
3743                          highlight (copy-sequence (cdr elem)))))
3744                highlight)
3745              (copy-sequence highlight-words)
3746              (if gnus-newsgroup-name
3747                  (copy-sequence (gnus-group-find-parameter
3748                                  gnus-newsgroup-name 'highlight-words t)))
3749              gnus-emphasis-alist)))))
3750
3751 (defvar gnus-summary-article-menu)
3752 (defvar gnus-summary-post-menu)
3753
3754 ;;; Saving functions.
3755
3756 (defun gnus-article-save (save-buffer file &optional num)
3757   "Save the currently selected article."
3758   (when (or (get gnus-default-article-saver :headers)
3759             (not gnus-save-all-headers))
3760     ;; Remove headers according to `gnus-saved-headers' or the value
3761     ;; of the `:headers' property that the saver function might have.
3762     (let ((gnus-visible-headers
3763            (or (symbol-value (get gnus-default-article-saver :headers))
3764                gnus-saved-headers gnus-visible-headers))
3765           ;; Ignore group parameter.  See `article-hide-headers'.
3766           (gnus-summary-buffer nil))
3767       (with-current-buffer save-buffer
3768         (article-hide-headers 1 t))))
3769   (save-window-excursion
3770     (if (not gnus-default-article-saver)
3771         (error "No default saver is defined")
3772       ;; !!! Magic!  The saving functions all save
3773       ;; `gnus-save-article-buffer' (or so they think), but we
3774       ;; bind that variable to our save-buffer.
3775       (set-buffer gnus-article-buffer)
3776       (let* ((gnus-save-article-buffer save-buffer)
3777              (filename
3778               (cond
3779                ((not gnus-prompt-before-saving) 'default)
3780                ((eq gnus-prompt-before-saving 'always) nil)
3781                (t file)))
3782              (gnus-number-of-articles-to-be-saved
3783               (when (eq gnus-prompt-before-saving t)
3784                 num)))                  ; Magic
3785         (set-buffer gnus-article-current-summary)
3786         (funcall gnus-default-article-saver filename)))))
3787
3788 (defun gnus-read-save-file-name (prompt &optional filename
3789                                         function group headers variable
3790                                         dir-var)
3791   (let ((default-name
3792           (funcall function group headers (symbol-value variable)))
3793         result)
3794     (setq result
3795           (expand-file-name
3796            (cond
3797             ((eq filename 'default)
3798              default-name)
3799             ((eq filename t)
3800              default-name)
3801             (filename filename)
3802             (t
3803              (when (symbol-value dir-var)
3804                (setq default-name (expand-file-name
3805                                    (file-name-nondirectory default-name)
3806                                    (symbol-value dir-var))))
3807              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3808                     (prompt
3809                      (format prompt
3810                              (if (and gnus-number-of-articles-to-be-saved
3811                                       (> gnus-number-of-articles-to-be-saved 1))
3812                                  (format "these %d articles"
3813                                          gnus-number-of-articles-to-be-saved)
3814                                "this article")))
3815                     (file
3816                      ;; Let the split methods have their say.
3817                      (cond
3818                       ;; No split name was found.
3819                       ((null split-name)
3820                        (read-file-name
3821                         (concat prompt " (default "
3822                                 (file-name-nondirectory default-name) "): ")
3823                         (file-name-directory default-name)
3824                         default-name))
3825                       ;; A single group name is returned.
3826                       ((stringp split-name)
3827                        (setq default-name
3828                              (funcall function split-name headers
3829                                       (symbol-value variable)))
3830                        (read-file-name
3831                         (concat prompt " (default "
3832                                 (file-name-nondirectory default-name) "): ")
3833                         (file-name-directory default-name)
3834                         default-name))
3835                       ;; A single split name was found
3836                       ((= 1 (length split-name))
3837                        (let* ((name (expand-file-name
3838                                      (car split-name)
3839                                      gnus-article-save-directory))
3840                               (dir (cond ((file-directory-p name)
3841                                           (file-name-as-directory name))
3842                                          ((file-exists-p name) name)
3843                                          (t gnus-article-save-directory))))
3844                          (read-file-name
3845                           (concat prompt " (default " name "): ")
3846                           dir name)))
3847                       ;; A list of splits was found.
3848                       (t
3849                        (setq split-name (nreverse split-name))
3850                        (let (result)
3851                          (let ((file-name-history
3852                                 (nconc split-name file-name-history)))
3853                            (setq result
3854                                  (expand-file-name
3855                                   (read-file-name
3856                                    (concat prompt " (`M-p' for defaults): ")
3857                                    gnus-article-save-directory
3858                                    (car split-name))
3859                                   gnus-article-save-directory)))
3860                          (car (push result file-name-history)))))))
3861                ;; Create the directory.
3862                (gnus-make-directory (file-name-directory file))
3863                ;; If we have read a directory, we append the default file name.
3864                (when (file-directory-p file)
3865                  (setq file (expand-file-name (file-name-nondirectory
3866                                                default-name)
3867                                               (file-name-as-directory file))))
3868                ;; Possibly translate some characters.
3869                (nnheader-translate-file-chars file))))))
3870     (gnus-make-directory (file-name-directory result))
3871     (when variable
3872       (set variable result))
3873     (when dir-var
3874       (set dir-var (file-name-directory result)))
3875     result))
3876
3877 (defun gnus-article-archive-name (group)
3878   "Return the first instance of an \"Archive-name\" in the current buffer."
3879   (let ((case-fold-search t))
3880     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3881       (nnheader-concat gnus-article-save-directory
3882                        (match-string 1)))))
3883
3884 (defun gnus-article-nndoc-name (group)
3885   "If GROUP is an nndoc group, return the name of the parent group."
3886   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3887     (gnus-group-get-parameter group 'save-article-group)))
3888
3889 (defun gnus-summary-save-in-rmail (&optional filename)
3890   "Append this article to Rmail file.
3891 Optional argument FILENAME specifies file name.
3892 Directory to save to is default to `gnus-article-save-directory'."
3893   (setq filename (gnus-read-save-file-name
3894                   "Save %s in rmail file" filename
3895                   gnus-rmail-save-name gnus-newsgroup-name
3896                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3897   (with-current-buffer gnus-save-article-buffer
3898     (save-excursion
3899       (save-restriction
3900         (widen)
3901         ;; Note that unlike gnus-summary-save-in-mail, there is no
3902         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3903         ;; not use Babyl.
3904         (gnus-output-to-rmail filename))))
3905   filename)
3906
3907 (defun gnus-summary-save-in-mail (&optional filename)
3908   "Append this article to Unix mail file.
3909 Optional argument FILENAME specifies file name.
3910 Directory to save to is default to `gnus-article-save-directory'."
3911   (setq filename (gnus-read-save-file-name
3912                   "Save %s in Unix mail file" filename
3913                   gnus-mail-save-name gnus-newsgroup-name
3914                   gnus-current-headers 'gnus-newsgroup-last-mail))
3915   (with-current-buffer gnus-save-article-buffer
3916     (save-excursion
3917       (save-restriction
3918         (widen)
3919         (if (and (file-readable-p filename)
3920                  (file-regular-p filename)
3921                  (mail-file-babyl-p filename))
3922             (gnus-output-to-rmail filename)
3923           (gnus-output-to-mail filename)))))
3924   filename)
3925
3926 (put 'gnus-summary-save-in-file :decode t)
3927 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3928 (defun gnus-summary-save-in-file (&optional filename overwrite)
3929   "Append this article to file.
3930 Optional argument FILENAME specifies file name.
3931 Directory to save to is default to `gnus-article-save-directory'."
3932   (setq filename (gnus-read-save-file-name
3933                   "Save %s in file" filename
3934                   gnus-file-save-name gnus-newsgroup-name
3935                   gnus-current-headers 'gnus-newsgroup-last-file))
3936   (with-current-buffer gnus-save-article-buffer
3937     (save-excursion
3938       (save-restriction
3939         (widen)
3940         (when (and overwrite
3941                    (file-exists-p filename))
3942           (delete-file filename))
3943         (gnus-output-to-file filename))))
3944   filename)
3945
3946 (put 'gnus-summary-write-to-file :decode t)
3947 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
3948 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
3949 (defun gnus-summary-write-to-file (&optional filename)
3950   "Write this article to a file, overwriting it if the file exists.
3951 Optional argument FILENAME specifies file name.
3952 The directory to save in defaults to `gnus-article-save-directory'."
3953   (setq filename (gnus-read-save-file-name
3954                   "Save %s in file" filename
3955                   gnus-file-save-name gnus-newsgroup-name
3956                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3957   (gnus-summary-save-in-file filename t))
3958
3959 (put 'gnus-summary-save-body-in-file :decode t)
3960 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
3961   "Append this article body to a file.
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 'gnus-newsgroup-last-file))
3968   (with-current-buffer gnus-save-article-buffer
3969     (save-excursion
3970       (save-restriction
3971         (widen)
3972         (when (article-goto-body)
3973           (narrow-to-region (point) (point-max)))
3974         (when (and overwrite
3975                    (file-exists-p filename))
3976           (delete-file filename))
3977         (gnus-output-to-file filename))))
3978   filename)
3979
3980 (put 'gnus-summary-write-body-to-file :decode t)
3981 (put 'gnus-summary-write-body-to-file
3982      :function 'gnus-summary-save-body-in-file)
3983 (defun gnus-summary-write-body-to-file (&optional filename)
3984   "Write this article body to a file, overwriting it if the file exists.
3985 Optional argument FILENAME specifies file name.
3986 The directory to save in defaults to `gnus-article-save-directory'."
3987   (setq filename (gnus-read-save-file-name
3988                   "Save %s body in file" filename
3989                   gnus-file-save-name gnus-newsgroup-name
3990                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3991   (gnus-summary-save-body-in-file filename t))
3992
3993 (put 'gnus-summary-save-in-pipe :decode t)
3994 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
3995 (defun gnus-summary-save-in-pipe (&optional command raw)
3996   "Pipe this article to subprocess COMMAND.
3997 Valid values for COMMAND include:
3998   a string
3999     The executable command name and possibly arguments.
4000   nil
4001     You will be prompted for the command in the minibuffer.
4002   the symbol `default'
4003     It will be replaced with the command which the variable
4004     `gnus-summary-pipe-output-default-command' holds or the command
4005     last used for saving.
4006 Non-nil value for RAW overrides `:decode' and `:headers' properties
4007 and the raw article including all headers will be piped."
4008   (let ((article (gnus-summary-article-number))
4009         (decode (unless raw
4010                   (get 'gnus-summary-save-in-pipe :decode)))
4011         save-buffer default)
4012     (if article
4013         (if (vectorp (gnus-summary-article-header article))
4014             (save-current-buffer
4015               (gnus-summary-select-article decode decode nil article)
4016               (insert-buffer-substring
4017                (prog1
4018                    (if decode
4019                        gnus-article-buffer
4020                      gnus-original-article-buffer)
4021                  (setq save-buffer
4022                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4023               ;; Remove unwanted headers.
4024               (when (and (not raw)
4025                          (or (get 'gnus-summary-save-in-pipe :headers)
4026                              (not gnus-save-all-headers)))
4027                 (let ((gnus-visible-headers
4028                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4029                                               :headers))
4030                            gnus-saved-headers gnus-visible-headers))
4031                       (gnus-summary-buffer nil))
4032                   (article-hide-headers 1 t))))
4033           (error "%d is not a real article" article))
4034       (error "No article to pipe"))
4035     (setq default (or gnus-summary-pipe-output-default-command
4036                       gnus-last-shell-command))
4037     (unless (stringp command)
4038       (setq command
4039             (if (and (eq command 'default) default)
4040                 default
4041               (gnus-read-shell-command "Shell command on this article: "
4042                                        default))))
4043     (when (string-equal command "")
4044       (if default
4045           (setq command default)
4046         (error "A command is required")))
4047     (with-current-buffer save-buffer
4048       (save-restriction
4049         (widen)
4050         (shell-command-on-region (point-min) (point-max) command nil)))
4051     (gnus-kill-buffer save-buffer))
4052   (setq gnus-summary-pipe-output-default-command command))
4053
4054 (defun gnus-summary-pipe-to-muttprint (&optional command)
4055   "Pipe this article to muttprint."
4056   (unless (stringp command)
4057     (setq command (read-string
4058                    "Print using command: " gnus-summary-muttprint-program
4059                    nil gnus-summary-muttprint-program)))
4060   (let ((gnus-summary-pipe-output-default-command
4061          gnus-summary-pipe-output-default-command))
4062     (gnus-summary-save-in-pipe command))
4063   (setq gnus-summary-muttprint-program command))
4064
4065 ;;; Article file names when saving.
4066
4067 (defun gnus-capitalize-newsgroup (newsgroup)
4068   "Capitalize NEWSGROUP name."
4069   (when (not (zerop (length newsgroup)))
4070     (concat (char-to-string (upcase (aref newsgroup 0)))
4071             (substring newsgroup 1))))
4072
4073 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4074   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4075 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4076 Otherwise, it is like ~/News/news/group/num."
4077   (let ((default
4078           (expand-file-name
4079            (concat (if (gnus-use-long-file-name 'not-save)
4080                        (gnus-capitalize-newsgroup newsgroup)
4081                      (gnus-newsgroup-directory-form newsgroup))
4082                    "/" (int-to-string (mail-header-number headers)))
4083            gnus-article-save-directory)))
4084     (if (and last-file
4085              (string-equal (file-name-directory default)
4086                            (file-name-directory last-file))
4087              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4088         default
4089       (or last-file default))))
4090
4091 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4092   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4093 If variable `gnus-use-long-file-name' is non-nil, it is
4094 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4095   (let ((default
4096           (expand-file-name
4097            (concat (if (gnus-use-long-file-name 'not-save)
4098                        newsgroup
4099                      (gnus-newsgroup-directory-form newsgroup))
4100                    "/" (int-to-string (mail-header-number headers)))
4101            gnus-article-save-directory)))
4102     (if (and last-file
4103              (string-equal (file-name-directory default)
4104                            (file-name-directory last-file))
4105              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4106         default
4107       (or last-file default))))
4108
4109 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4110   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4111 If variable `gnus-use-long-file-name' is non-nil, it is
4112 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4113   (or last-file
4114       (expand-file-name
4115        (if (gnus-use-long-file-name 'not-save)
4116            newsgroup
4117          (file-relative-name
4118           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4119           default-directory))
4120        gnus-article-save-directory)))
4121
4122 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4123   "Generate file name from sender."
4124   (let ((from (mail-header-from headers)))
4125     (expand-file-name
4126      (if (and from (string-match "\\([^ <]+\\)@" from))
4127          (match-string 1 from)
4128        "nobody")
4129      gnus-article-save-directory)))
4130
4131 (defun article-verify-x-pgp-sig ()
4132   "Verify X-PGP-Sig."
4133   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4134   (interactive)
4135   (if (gnus-buffer-live-p gnus-original-article-buffer)
4136       (let ((sig (with-current-buffer gnus-original-article-buffer
4137                    (gnus-fetch-field "X-PGP-Sig")))
4138             items info headers)
4139         (when (and sig
4140                    mml2015-use
4141                    (mml2015-clear-verify-function))
4142           (with-temp-buffer
4143             (insert-buffer-substring gnus-original-article-buffer)
4144             (setq items (split-string sig))
4145             (message-narrow-to-head)
4146             (let ((inhibit-point-motion-hooks t)
4147                   (case-fold-search t))
4148               ;; Don't verify multiple headers.
4149               (setq headers (mapconcat (lambda (header)
4150                                          (concat header ": "
4151                                                  (mail-fetch-field header)
4152                                                  "\n"))
4153                                        (split-string (nth 1 items) ",") "")))
4154             (delete-region (point-min) (point-max))
4155             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4156             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4157             (insert headers)
4158             (widen)
4159             (forward-line)
4160             (while (not (eobp))
4161               (if (looking-at "^-")
4162                   (insert "- "))
4163               (forward-line))
4164             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4165             (insert "Version: " (car items) "\n\n")
4166             (insert (mapconcat 'identity (cddr items) "\n"))
4167             (insert "\n-----END PGP SIGNATURE-----\n")
4168             (let ((mm-security-handle (list (format "multipart/signed"))))
4169               (mml2015-clean-buffer)
4170               (let ((coding-system-for-write (or gnus-newsgroup-charset
4171                                                  'iso-8859-1)))
4172                 (funcall (mml2015-clear-verify-function)))
4173               (setq info
4174                     (or (mm-handle-multipart-ctl-parameter
4175                          mm-security-handle 'gnus-details)
4176                         (mm-handle-multipart-ctl-parameter
4177                          mm-security-handle 'gnus-info)))))
4178           (when info
4179             (let ((inhibit-read-only t) bface eface)
4180               (save-restriction
4181                 (message-narrow-to-head)
4182                 (goto-char (point-max))
4183                 (forward-line -1)
4184                 (setq bface (get-text-property (point-at-bol) 'face)
4185                       eface (get-text-property (1- (point-at-eol)) 'face))
4186                 (message-remove-header "X-Gnus-PGP-Verify")
4187                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4188                     (forward-line)
4189                   (goto-char (point-max)))
4190                 (narrow-to-region (point) (point))
4191                 (insert "X-Gnus-PGP-Verify: " info "\n")
4192                 (goto-char (point-min))
4193                 (forward-line)
4194                 (while (not (eobp))
4195                   (if (not (looking-at "^[ \t]"))
4196                       (insert " "))
4197                   (forward-line))
4198                 ;; Do highlighting.
4199                 (goto-char (point-min))
4200                 (when (looking-at "\\([^:]+\\): *")
4201                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4202                                      'face bface)
4203                   (put-text-property (match-end 0) (point-max)
4204                                      'face eface)))))))))
4205
4206 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4207
4208 (defun article-verify-cancel-lock ()
4209   "Verify Cancel-Lock header."
4210   (interactive)
4211   (if (gnus-buffer-live-p gnus-original-article-buffer)
4212       (canlock-verify gnus-original-article-buffer)))
4213
4214 (eval-and-compile
4215   (mapc
4216    (lambda (func)
4217      (let (afunc gfunc)
4218        (if (consp func)
4219            (setq afunc (car func)
4220                  gfunc (cdr func))
4221          (setq afunc func
4222                gfunc (intern (format "gnus-%s" func))))
4223        (defalias gfunc
4224          (when (fboundp afunc)
4225            `(lambda (&optional interactive &rest args)
4226               ,(documentation afunc t)
4227               (interactive (list t))
4228               (with-current-buffer gnus-article-buffer
4229                 (if interactive
4230                     (call-interactively ',afunc)
4231                   (apply ',afunc args))))))))
4232    '(article-hide-headers
4233      article-verify-x-pgp-sig
4234      article-verify-cancel-lock
4235      article-hide-boring-headers
4236      article-treat-overstrike
4237      article-treat-ansi-sequences
4238      article-fill-long-lines
4239      article-capitalize-sentences
4240      article-remove-cr
4241      article-remove-leading-whitespace
4242      article-display-x-face
4243      article-display-face
4244      article-de-quoted-unreadable
4245      article-de-base64-unreadable
4246      article-decode-HZ
4247      article-wash-html
4248      article-unsplit-urls
4249      article-hide-list-identifiers
4250      article-strip-banner
4251      article-babel
4252      article-hide-pem
4253      article-hide-signature
4254      article-strip-headers-in-body
4255      article-remove-trailing-blank-lines
4256      article-strip-leading-blank-lines
4257      article-strip-multiple-blank-lines
4258      article-strip-leading-space
4259      article-strip-trailing-space
4260      article-strip-blank-lines
4261      article-strip-all-blank-lines
4262      article-date-local
4263      article-date-english
4264      article-date-iso8601
4265      article-date-original
4266      article-date-ut
4267      article-decode-mime-words
4268      article-decode-charset
4269      article-decode-encoded-words
4270      article-date-user
4271      article-date-lapsed
4272      article-emphasize
4273      article-treat-dumbquotes
4274      article-treat-non-ascii
4275      article-normalize-headers
4276      ;;(article-show-all . gnus-article-show-all-headers)
4277      )))
4278 \f
4279 ;;;
4280 ;;; Gnus article mode
4281 ;;;
4282
4283 (put 'gnus-article-mode 'mode-class 'special)
4284
4285 (set-keymap-parent gnus-article-mode-map widget-keymap)
4286
4287 (gnus-define-keys gnus-article-mode-map
4288   " " gnus-article-goto-next-page
4289   "\177" gnus-article-goto-prev-page
4290   [delete] gnus-article-goto-prev-page
4291   [backspace] gnus-article-goto-prev-page
4292   "\C-c^" gnus-article-refer-article
4293   "h" gnus-article-show-summary
4294   "s" gnus-article-show-summary
4295   "\C-c\C-m" gnus-article-mail
4296   "?" gnus-article-describe-briefly
4297   "<" beginning-of-buffer
4298   ">" end-of-buffer
4299   "\C-c\C-i" gnus-info-find-node
4300   "\C-c\C-b" gnus-bug
4301   "R" gnus-article-reply-with-original
4302   "F" gnus-article-followup-with-original
4303   "\C-hk" gnus-article-describe-key
4304   "\C-hc" gnus-article-describe-key-briefly
4305   "\C-hb" gnus-article-describe-bindings
4306
4307   "e" gnus-article-read-summary-keys
4308   "\C-d" gnus-article-read-summary-keys
4309   "\M-*" gnus-article-read-summary-keys
4310   "\M-#" gnus-article-read-summary-keys
4311   "\M-^" gnus-article-read-summary-keys
4312   "\M-g" gnus-article-read-summary-keys)
4313
4314 (substitute-key-definition
4315  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4316
4317 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4318   "W" gnus-article-wide-reply-with-original)
4319 (if (featurep 'xemacs)
4320     (set-keymap-default-binding gnus-article-send-map
4321                                 'gnus-article-read-summary-send-keys)
4322   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4323
4324 (defun gnus-article-make-menu-bar ()
4325   (unless (boundp 'gnus-article-commands-menu)
4326     (gnus-summary-make-menu-bar))
4327   (unless (boundp 'gnus-article-article-menu)
4328     (easy-menu-define
4329      gnus-article-article-menu gnus-article-mode-map ""
4330      '("Article"
4331        ["Scroll forwards" gnus-article-goto-next-page t]
4332        ["Scroll backwards" gnus-article-goto-prev-page t]
4333        ["Show summary" gnus-article-show-summary t]
4334        ["Fetch Message-ID at point" gnus-article-refer-article t]
4335        ["Mail to address at point" gnus-article-mail t]
4336        ["Send a bug report" gnus-bug t]))
4337
4338     (easy-menu-define
4339      gnus-article-treatment-menu gnus-article-mode-map ""
4340      ;; Fixme: this should use :active (and maybe :visible).
4341      '("Treatment"
4342        ["Hide headers" gnus-article-hide-headers t]
4343        ["Hide signature" gnus-article-hide-signature t]
4344        ["Hide citation" gnus-article-hide-citation t]
4345        ["Treat overstrike" gnus-article-treat-overstrike t]
4346        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4347        ["Remove carriage return" gnus-article-remove-cr t]
4348        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4349        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4350        ["Remove base64" gnus-article-de-base64-unreadable t]
4351        ["Treat html" gnus-article-wash-html t]
4352        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4353        ["Decode HZ" gnus-article-decode-HZ t]))
4354
4355     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4356
4357     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4358
4359     (gnus-run-hooks 'gnus-article-menu-hook)))
4360
4361 (defvar bookmark-make-record-function)
4362
4363 (defun gnus-article-mode ()
4364   "Major mode for displaying an article.
4365
4366 All normal editing commands are switched off.
4367
4368 The following commands are available in addition to all summary mode
4369 commands:
4370 \\<gnus-article-mode-map>
4371 \\[gnus-article-next-page]\t Scroll the article one page forwards
4372 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4373 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4374 \\[gnus-article-show-summary]\t Display the summary buffer
4375 \\[gnus-article-mail]\t Send a reply to the address near point
4376 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4377 \\[gnus-info-find-node]\t Go to the Gnus info node"
4378   (interactive)
4379   (kill-all-local-variables)
4380   (gnus-simplify-mode-line)
4381   (setq mode-name "Article")
4382   (setq major-mode 'gnus-article-mode)
4383   (make-local-variable 'minor-mode-alist)
4384   (use-local-map gnus-article-mode-map)
4385   (when (gnus-visual-p 'article-menu 'menu)
4386     (gnus-article-make-menu-bar)
4387     (when gnus-summary-tool-bar-map
4388       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4389   (gnus-update-format-specifications nil 'article-mode)
4390   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4391   (set (make-local-variable 'gnus-page-broken) nil)
4392   (make-local-variable 'gnus-button-marker-list)
4393   (make-local-variable 'gnus-article-current-summary)
4394   (make-local-variable 'gnus-article-mime-handles)
4395   (make-local-variable 'gnus-article-decoded-p)
4396   (make-local-variable 'gnus-article-mime-handle-alist)
4397   (make-local-variable 'gnus-article-wash-types)
4398   (make-local-variable 'gnus-article-image-alist)
4399   (make-local-variable 'gnus-article-charset)
4400   (make-local-variable 'gnus-article-ignored-charsets)
4401   (set (make-local-variable 'bookmark-make-record-function)
4402        'gnus-summary-bookmark-make-record)
4403   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4404   ;; face.
4405   (set (make-local-variable 'nobreak-char-display) nil)
4406   (setq cursor-in-non-selected-windows nil)
4407   (gnus-set-default-directory)
4408   (buffer-disable-undo)
4409   (setq buffer-read-only t
4410         show-trailing-whitespace nil)
4411   (set-syntax-table gnus-article-mode-syntax-table)
4412   (mm-enable-multibyte)
4413   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4414
4415 (defvar gnus-button-marker-list nil
4416   "Regexp matching any of the regexps from `gnus-button-alist'.
4417 Internal variable.")
4418
4419 (defun gnus-article-setup-buffer ()
4420   "Initialize the article buffer."
4421   (let* ((name (if gnus-single-article-buffer "*Article*"
4422                  (concat "*Article " gnus-newsgroup-name "*")))
4423          (original
4424           (progn (string-match "\\*Article" name)
4425                  (concat " *Original Article"
4426                          (substring name (match-end 0))))))
4427     (setq gnus-article-buffer name)
4428     (setq gnus-original-article-buffer original)
4429     (setq gnus-article-mime-handle-alist nil)
4430     (with-current-buffer gnus-summary-buffer
4431       ;; This might be a variable local to the summary buffer.
4432       (unless gnus-single-article-buffer
4433         (setq gnus-article-buffer name)
4434         (setq gnus-original-article-buffer original)
4435         (gnus-set-global-variables)))
4436     (gnus-article-setup-highlight-words)
4437     ;; Init original article buffer.
4438     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4439       (mm-enable-multibyte)
4440       (setq major-mode 'gnus-original-article-mode)
4441       (make-local-variable 'gnus-original-article))
4442     (if (and (get-buffer name)
4443              (with-current-buffer name
4444                (if gnus-article-edit-mode
4445                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4446                        (progn
4447                          (set-buffer-modified-p nil)
4448                          (gnus-kill-buffer name)
4449                          (message "")
4450                          nil)
4451                      (error "Action aborted"))
4452                  t)))
4453         (with-current-buffer name
4454           (set (make-local-variable 'gnus-article-edit-mode) nil)
4455           (when gnus-article-mime-handles
4456             (mm-destroy-parts gnus-article-mime-handles)
4457             (setq gnus-article-mime-handles nil))
4458           ;; Set it to nil in article-buffer!
4459           (setq gnus-article-mime-handle-alist nil)
4460           (buffer-disable-undo)
4461           (setq buffer-read-only t)
4462           ;; This list just keeps growing if we don't reset it.
4463           (setq gnus-button-marker-list nil)
4464           (unless (eq major-mode 'gnus-article-mode)
4465             (gnus-article-mode))
4466           (setq truncate-lines gnus-article-truncate-lines)
4467           (current-buffer))
4468       (with-current-buffer (gnus-get-buffer-create name)
4469         (gnus-article-mode)
4470         (setq truncate-lines gnus-article-truncate-lines)
4471         (make-local-variable 'gnus-summary-buffer)
4472         (setq gnus-summary-buffer
4473               (gnus-summary-buffer-name gnus-newsgroup-name))
4474         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4475         (current-buffer)))))
4476
4477 ;; Set article window start at LINE, where LINE is the number of lines
4478 ;; from the head of the article.
4479 (defun gnus-article-set-window-start (&optional line)
4480   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4481     (when article-window
4482       (set-window-start
4483        article-window
4484        (with-current-buffer gnus-article-buffer
4485          (goto-char (point-min))
4486          (if (not line)
4487              (point-min)
4488            (gnus-message 6 "Moved to bookmark")
4489            (search-forward "\n\n" nil t)
4490            (forward-line line)
4491            (point)))))))
4492
4493 (defun gnus-article-prepare (article &optional all-headers header)
4494   "Prepare ARTICLE in article mode buffer.
4495 ARTICLE should either be an article number or a Message-ID.
4496 If ARTICLE is an id, HEADER should be the article headers.
4497 If ALL-HEADERS is non-nil, no headers are hidden."
4498   (save-excursion
4499     ;; Make sure we start in a summary buffer.
4500     (unless (eq major-mode 'gnus-summary-mode)
4501       (set-buffer gnus-summary-buffer))
4502     (setq gnus-summary-buffer (current-buffer))
4503     (let* ((gnus-article (if header (mail-header-number header) article))
4504            (summary-buffer (current-buffer))
4505            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4506            (group gnus-newsgroup-name)
4507            result)
4508       (save-excursion
4509         (gnus-article-setup-buffer)
4510         (set-buffer gnus-article-buffer)
4511         ;; Deactivate active regions.
4512         (when (and (boundp 'transient-mark-mode)
4513                    transient-mark-mode)
4514           (setq mark-active nil))
4515         (if (not (setq result (let ((inhibit-read-only t))
4516                                 (gnus-request-article-this-buffer
4517                                  article group))))
4518             ;; There is no such article.
4519             (save-excursion
4520               (when (and (numberp article)
4521                          (not (memq article gnus-newsgroup-sparse)))
4522                 (setq gnus-article-current
4523                       (cons gnus-newsgroup-name article))
4524                 (set-buffer gnus-summary-buffer)
4525                 (setq gnus-current-article article)
4526                 (if (and (memq article gnus-newsgroup-undownloaded)
4527                          (not (gnus-online (gnus-find-method-for-group
4528                                             gnus-newsgroup-name))))
4529                     (progn
4530                       (gnus-summary-set-agent-mark article)
4531                       (message "Message marked for downloading"))
4532                   (gnus-summary-mark-article article gnus-canceled-mark)
4533                   (unless (memq article gnus-newsgroup-sparse)
4534                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4535           (if (or (eq result 'pseudo)
4536                   (eq result 'nneething))
4537               (progn
4538                 (with-current-buffer summary-buffer
4539                   (push article gnus-newsgroup-history)
4540                   (setq gnus-last-article gnus-current-article
4541                         gnus-current-article 0
4542                         gnus-current-headers nil
4543                         gnus-article-current nil)
4544                   (if (eq result 'nneething)
4545                       (gnus-configure-windows 'summary)
4546                     (gnus-configure-windows 'article))
4547                   (gnus-set-global-variables))
4548                 (let ((gnus-article-mime-handle-alist-1
4549                        gnus-article-mime-handle-alist))
4550                   (gnus-set-mode-line 'article)))
4551             ;; The result from the `request' was an actual article -
4552             ;; or at least some text that is now displayed in the
4553             ;; article buffer.
4554             (when (and (numberp article)
4555                        (not (eq article gnus-current-article)))
4556               ;; Seems like a new article has been selected.
4557               ;; `gnus-current-article' must be an article number.
4558               (with-current-buffer summary-buffer
4559                 (push article gnus-newsgroup-history)
4560                 (setq gnus-last-article gnus-current-article
4561                       gnus-current-article article
4562                       gnus-current-headers
4563                       (gnus-summary-article-header gnus-current-article)
4564                       gnus-article-current
4565                       (cons gnus-newsgroup-name gnus-current-article))
4566                 (unless (vectorp gnus-current-headers)
4567                   (setq gnus-current-headers nil))
4568                 (gnus-summary-goto-subject gnus-current-article)
4569                 (when (gnus-summary-show-thread)
4570                   ;; If the summary buffer really was folded, the
4571                   ;; previous goto may not actually have gone to
4572                   ;; the right article, but the thread root instead.
4573                   ;; So we go again.
4574                   (gnus-summary-goto-subject gnus-current-article))
4575                 (gnus-run-hooks 'gnus-mark-article-hook)
4576                 (gnus-set-mode-line 'summary)
4577                 (when (gnus-visual-p 'article-highlight 'highlight)
4578                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4579                 ;; Set the global newsgroup variables here.
4580                 (gnus-set-global-variables)
4581                 (setq gnus-have-all-headers
4582                       (or all-headers gnus-show-all-headers))))
4583             (save-excursion
4584               (gnus-configure-windows 'article))
4585             (when (or (numberp article)
4586                       (stringp article))
4587               (gnus-article-prepare-display)
4588               ;; Do page break.
4589               (goto-char (point-min))
4590               (when gnus-break-pages
4591                 (gnus-narrow-to-page)))
4592             (let ((gnus-article-mime-handle-alist-1
4593                    gnus-article-mime-handle-alist))
4594               (gnus-set-mode-line 'article))
4595             (article-goto-body)
4596             (unless (bobp)
4597               (forward-line -1))
4598             (set-window-point (get-buffer-window (current-buffer)) (point))
4599             (gnus-configure-windows 'article)
4600             t))))))
4601
4602 ;;;###autoload
4603 (defun gnus-article-prepare-display ()
4604   "Make the current buffer look like a nice article."
4605   ;; Hooks for getting information from the article.
4606   ;; This hook must be called before being narrowed.
4607   (let ((gnus-article-buffer (current-buffer))
4608         buffer-read-only
4609         (inhibit-read-only t))
4610     (unless (eq major-mode 'gnus-article-mode)
4611       (gnus-article-mode))
4612     (setq buffer-read-only nil
4613           gnus-article-wash-types nil
4614           gnus-article-image-alist nil)
4615     (gnus-run-hooks 'gnus-tmp-internal-hook)
4616     (when gnus-display-mime-function
4617       (funcall gnus-display-mime-function))
4618     (gnus-run-hooks 'gnus-article-prepare-hook)))
4619
4620 ;;;
4621 ;;; Gnus Sticky Article Mode
4622 ;;;
4623
4624 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4625   "Mode for sticky articles."
4626   ;; Release bindings that won't work.
4627   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4628                              gnus-sticky-article-mode-map)
4629   (substitute-key-definition 'gnus-article-refer-article 'undefined
4630                              gnus-sticky-article-mode-map)
4631   (dolist (k '("e" "h" "s" "F" "R"))
4632     (define-key gnus-sticky-article-mode-map k nil))
4633   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4634   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4635   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4636   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4637
4638 (defun gnus-sticky-article (arg)
4639   "Make the current article sticky.
4640 If a prefix ARG is given, ask for a name for this sticky article buffer."
4641   (interactive "P")
4642   (gnus-summary-show-thread)
4643   (gnus-summary-select-article nil nil 'pseudo)
4644   (let (new-art-buf-name)
4645     (gnus-eval-in-buffer-window gnus-article-buffer
4646       (setq new-art-buf-name
4647             (concat
4648              "*Sticky Article: "
4649              (if arg
4650                  (read-from-minibuffer "Sticky article buffer name: ")
4651                (gnus-with-article-headers
4652                  (gnus-article-goto-header "subject")
4653                  (setq new-art-buf-name
4654                        (buffer-substring-no-properties
4655                         (line-beginning-position) (line-end-position)))
4656                  (goto-char (point-min))
4657                  (gnus-article-goto-header "from")
4658                  (setq new-art-buf-name
4659                        (concat
4660                         new-art-buf-name ", "
4661                         (buffer-substring-no-properties
4662                          (line-beginning-position) (line-end-position))))
4663                  (goto-char (point-min))
4664                  (gnus-article-goto-header "date")
4665                  (setq new-art-buf-name
4666                        (concat
4667                         new-art-buf-name ", "
4668                         (buffer-substring-no-properties
4669                          (line-beginning-position) (line-end-position))))))
4670              "*"))
4671       (if (and (gnus-buffer-live-p new-art-buf-name)
4672                (with-current-buffer new-art-buf-name
4673                  (eq major-mode 'gnus-sticky-article-mode)))
4674           (switch-to-buffer new-art-buf-name)
4675         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4676       (gnus-sticky-article-mode))
4677     (setq gnus-article-buffer new-art-buf-name))
4678   (gnus-summary-recenter)
4679   (gnus-summary-position-point))
4680
4681 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4682   "Kill the given sticky article BUFFER.
4683 If none is given, assume the current buffer and kill it if it has
4684 `gnus-sticky-article-mode'."
4685   (interactive)
4686   (unless buffer
4687     (setq buffer (current-buffer)))
4688   (with-current-buffer buffer
4689     (when (eq major-mode 'gnus-sticky-article-mode)
4690       (gnus-kill-buffer buffer))))
4691
4692 (defun gnus-kill-sticky-article-buffers (arg)
4693   "Kill all sticky article buffers.
4694 If a prefix ARG is given, ask for confirmation."
4695   (interactive "P")
4696   (dolist (buf (gnus-buffers))
4697     (with-current-buffer buf
4698       (when (eq major-mode 'gnus-sticky-article-mode)
4699         (if (not arg)
4700             (gnus-kill-buffer buf)
4701           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4702             (gnus-kill-buffer buf)))))))
4703
4704 ;;;
4705 ;;; Gnus MIME viewing functions
4706 ;;;
4707
4708 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4709   "Format of the MIME buttons.
4710
4711 Valid specifiers include:
4712 %t  The MIME type
4713 %T  MIME type, along with additional info
4714 %n  The `name' parameter
4715 %d  The description, if any
4716 %l  The length of the encoded part
4717 %p  The part identifier number
4718 %e  Dots if the part isn't displayed
4719
4720 General format specifiers can also be used.  See Info node
4721 `(gnus)Formatting Variables'.")
4722
4723 (defvar gnus-mime-button-line-format-alist
4724   '((?t gnus-tmp-type ?s)
4725     (?T gnus-tmp-type-long ?s)
4726     (?n gnus-tmp-name ?s)
4727     (?d gnus-tmp-description ?s)
4728     (?p gnus-tmp-id ?s)
4729     (?l gnus-tmp-length ?d)
4730     (?e gnus-tmp-dots ?s)))
4731
4732 (defvar gnus-mime-button-commands
4733   '((gnus-article-press-button "\r" "Toggle Display")
4734     (gnus-mime-view-part "v" "View Interactively...")
4735     (gnus-mime-view-part-as-type "t" "View As Type...")
4736     (gnus-mime-view-part-as-charset "C" "View As charset...")
4737     (gnus-mime-save-part "o" "Save...")
4738     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4739     (gnus-mime-replace-part "r" "Replace part")
4740     (gnus-mime-delete-part "d" "Delete part")
4741     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4742     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4743     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4744     (gnus-mime-view-part-externally "e" "View Externally")
4745     (gnus-mime-print-part "p" "Print")
4746     (gnus-mime-pipe-part "|" "Pipe To Command...")
4747     (gnus-mime-action-on-part "." "Take action on the part...")))
4748
4749 (defun gnus-article-mime-part-status ()
4750   (if gnus-article-mime-handle-alist-1
4751       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4752           " (1 part)"
4753         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4754     ""))
4755
4756 (defvar gnus-mime-button-map
4757   (let ((map (make-sparse-keymap)))
4758     (define-key map gnus-mouse-2 'gnus-article-push-button)
4759     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4760     (dolist (c gnus-mime-button-commands)
4761       (define-key map (cadr c) (car c)))
4762     map))
4763
4764 (easy-menu-define
4765   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4766   `("MIME Part"
4767     ,@(mapcar (lambda (c)
4768                 (vector (caddr c) (car c) :active t))
4769               gnus-mime-button-commands)))
4770
4771 (defvar gnus-url-button-commands
4772   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4773
4774 (defvar gnus-url-button-map
4775   (let ((map (make-sparse-keymap)))
4776     (dolist (c gnus-url-button-commands)
4777       (define-key map (cadr c) (car c)))
4778     map))
4779
4780 (easy-menu-define
4781   gnus-url-button-menu gnus-url-button-map "URL button menu."
4782   `("Url Button"
4783     ,@(mapcar (lambda (c)
4784                 (vector (caddr c) (car c) :active t))
4785               gnus-url-button-commands)))
4786
4787 (defmacro gnus-bind-safe-url-regexp (&rest body)
4788   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4789   `(let ((mm-w3m-safe-url-regexp
4790           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4791                                 (gnus-buffer-live-p
4792                                  gnus-article-current-summary))
4793                            (with-current-buffer gnus-article-current-summary
4794                              gnus-newsgroup-name)
4795                          gnus-newsgroup-name)))
4796             (if (cond ((not group)
4797                        ;; Maybe we're in a mml-preview buffer
4798                        ;; and no group is selected.
4799                        t)
4800                       ((stringp gnus-safe-html-newsgroups)
4801                        (string-match gnus-safe-html-newsgroups group))
4802                       ((consp gnus-safe-html-newsgroups)
4803                        (member group gnus-safe-html-newsgroups)))
4804                 nil
4805               mm-w3m-safe-url-regexp))))
4806      ,@body))
4807
4808 (defun gnus-mime-button-menu (event prefix)
4809  "Construct a context-sensitive menu of MIME commands."
4810  (interactive "e\nP")
4811  (save-window-excursion
4812    (let ((pos (event-start event)))
4813      (select-window (posn-window pos))
4814      (goto-char (posn-point pos))
4815      (gnus-article-check-buffer)
4816      (popup-menu gnus-mime-button-menu nil prefix))))
4817
4818 (defun gnus-mime-view-all-parts (&optional handles)
4819   "View all the MIME parts."
4820   (interactive)
4821   (with-current-buffer gnus-article-buffer
4822     (let ((handles (or handles gnus-article-mime-handles))
4823           (mail-parse-charset gnus-newsgroup-charset)
4824           (mail-parse-ignored-charsets
4825            (with-current-buffer gnus-summary-buffer
4826              gnus-newsgroup-ignored-charsets)))
4827       (when handles
4828         (mm-remove-parts handles)
4829         (goto-char (point-min))
4830         (or (search-forward "\n\n") (goto-char (point-max)))
4831         (let ((inhibit-read-only t))
4832           (delete-region (point) (point-max))
4833           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4834
4835 (defun gnus-article-jump-to-part (n)
4836   "Jump to MIME part N."
4837   (interactive "P")
4838   (let ((parts (with-current-buffer gnus-article-buffer
4839                  (length gnus-article-mime-handle-alist))))
4840     (when (zerop parts)
4841       (error "No such part"))
4842     (pop-to-buffer gnus-article-buffer)
4843     ;; FIXME: why is it necessary?
4844     (sit-for 0)
4845     (or n
4846         (setq n (if (= parts 1)
4847                     1
4848                   (read-number (format "Jump to part (1..%s): " parts)))))
4849     (unless (and (integerp n) (<= n parts) (>= n 1))
4850       (setq n
4851             (progn
4852               (gnus-message 7 "Invalid part `%s', using %s instead."
4853                             n parts)
4854               parts)))
4855     (gnus-message 9 "Jumping to part %s." n)
4856     (cond ((>= gnus-auto-select-part 1)
4857            (while (and (<= n parts)
4858                        (not (gnus-article-goto-part n)))
4859              (setq n (1+ n))))
4860           ((< gnus-auto-select-part 0)
4861            (while (and (>= n 1)
4862                        (not (gnus-article-goto-part n)))
4863              (setq n (1- n))))
4864           (t
4865            (gnus-article-goto-part n)))))
4866
4867 (defvar gnus-mime-buttonized-part-id nil
4868   "ID of a mime part that should be buttonized.
4869 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4870
4871 (eval-when-compile
4872   (defsubst gnus-article-edit-part (handles &optional current-id)
4873     "Edit an article in order to delete a mime part.
4874 This function is exclusively used by `gnus-mime-save-part-and-strip'
4875 and `gnus-mime-delete-part', and not provided at run-time normally."
4876     (gnus-article-edit-article
4877      `(lambda ()
4878         (buffer-disable-undo)
4879         (erase-buffer)
4880         (let ((mail-parse-charset (or gnus-article-charset
4881                                       ',gnus-newsgroup-charset))
4882               (mail-parse-ignored-charsets
4883                (or gnus-article-ignored-charsets
4884                    ',gnus-newsgroup-ignored-charsets))
4885               (mbl mml-buffer-list))
4886           (setq mml-buffer-list nil)
4887           (insert-buffer-substring gnus-original-article-buffer)
4888           (mime-to-mml ',handles)
4889           (setq gnus-article-mime-handles nil)
4890           (let ((mbl1 mml-buffer-list))
4891             (setq mml-buffer-list mbl)
4892             (set (make-local-variable 'mml-buffer-list) mbl1))
4893           (gnus-make-local-hook 'kill-buffer-hook)
4894           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4895      `(lambda (no-highlight)
4896         (let ((mail-parse-charset (or gnus-article-charset
4897                                       ',gnus-newsgroup-charset))
4898               (message-options message-options)
4899               (message-options-set-recipient)
4900               (mail-parse-ignored-charsets
4901                (or gnus-article-ignored-charsets
4902                    ',gnus-newsgroup-ignored-charsets)))
4903           (mml-to-mime)
4904           (mml-destroy-buffers)
4905           (remove-hook 'kill-buffer-hook
4906                        'mml-destroy-buffers t)
4907           (kill-local-variable 'mml-buffer-list))
4908         (gnus-summary-edit-article-done
4909          ,(or (mail-header-references gnus-current-headers) "")
4910          ,(gnus-group-read-only-p)
4911          ,gnus-summary-buffer no-highlight))
4912      t)
4913     ;; Force buttonizing this part.
4914     (let ((gnus-mime-buttonized-part-id current-id))
4915       (gnus-article-edit-done))
4916     (gnus-configure-windows 'article)
4917     (when (and current-id (integerp gnus-auto-select-part))
4918       (gnus-article-jump-to-part
4919        (min (max (+ current-id gnus-auto-select-part) 1)
4920             (with-current-buffer gnus-article-buffer
4921               (length gnus-article-mime-handle-alist)))))))
4922
4923 (defun gnus-mime-replace-part (file)
4924   "Replace MIME part under point with an external body."
4925   ;; Useful if file has already been saved to disk
4926   (interactive
4927    (list
4928     (read-file-name "Replace MIME part with file: "
4929                     (or mm-default-directory default-directory)
4930                     nil nil)))
4931   (gnus-mime-save-part-and-strip file))
4932
4933 (defun gnus-mime-save-part-and-strip (&optional file)
4934   "Save the MIME part under point then replace it with an external body.
4935 If FILE is given, use it for the external part."
4936   (interactive)
4937   (gnus-article-check-buffer)
4938   (when (gnus-group-read-only-p)
4939     (error "The current group does not support deleting of parts"))
4940   (when (mm-complicated-handles gnus-article-mime-handles)
4941     (error "\
4942 The current article has a complicated MIME structure, giving up..."))
4943   (let* ((data (get-text-property (point) 'gnus-data))
4944          (id (get-text-property (point) 'gnus-part))
4945          param
4946          (handles gnus-article-mime-handles))
4947     (unless file
4948       (setq file
4949             (and data (mm-save-part data "Delete MIME part and save to: "))))
4950     (when file
4951       (with-current-buffer (mm-handle-buffer data)
4952         (erase-buffer)
4953         (insert "Content-Type: " (mm-handle-media-type data))
4954         (mml-insert-parameter-string (cdr (mm-handle-type data))
4955                                      '(charset))
4956         ;; Add a filename for the sake of saving the part again.
4957         (mml-insert-parameter
4958          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
4959         (insert "\n")
4960         (insert "Content-ID: " (message-make-message-id) "\n")
4961         (insert "Content-Transfer-Encoding: binary\n")
4962         (insert "\n"))
4963       (setcdr data
4964               (cdr (mm-make-handle nil
4965                                    `("message/external-body"
4966                                      (access-type . "LOCAL-FILE")
4967                                      (name . ,file)))))
4968       ;; (set-buffer gnus-summary-buffer)
4969       (gnus-article-edit-part handles id))))
4970
4971 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4972 ;; parts...>') but with stripping would be nice.
4973
4974 (defun gnus-mime-delete-part ()
4975   "Delete the MIME part under point.
4976 Replace it with some information about the removed part."
4977   (interactive)
4978   (gnus-article-check-buffer)
4979   (when (gnus-group-read-only-p)
4980     (error "The current group does not support deleting of parts"))
4981   (when (mm-complicated-handles gnus-article-mime-handles)
4982     (error "\
4983 The current article has a complicated MIME structure, giving up..."))
4984   (when (or gnus-expert-user
4985             (gnus-yes-or-no-p "\
4986 Deleting parts may malfunction or destroy the article; continue? "))
4987     (let* ((data (get-text-property (point) 'gnus-data))
4988            (id (get-text-property (point) 'gnus-part))
4989            (handles gnus-article-mime-handles)
4990            (none "(none)")
4991            (description
4992             (let ((desc (mm-handle-description data)))
4993               (when desc
4994                 (mail-decode-encoded-word-string desc))))
4995            (filename
4996             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4997                 none))
4998            (type (mm-handle-media-type data)))
4999       (unless data
5000         (error "No MIME part under point"))
5001       (with-current-buffer (mm-handle-buffer data)
5002         (let ((bsize (buffer-size)))
5003           (erase-buffer)
5004           (insert
5005            (concat
5006             ",----\n"
5007             "| The following attachment has been deleted:\n"
5008             "|\n"
5009             "| Type:           " type "\n"
5010             "| Filename:       " filename "\n"
5011             "| Size (encoded): " (format "%s byte%s\n"
5012                                          bsize (if (= bsize 1)
5013                                                    ""
5014                                                  "s"))
5015             (when description
5016               (concat    "| Description:    " description "\n"))
5017             "`----\n"))
5018           (setcdr data
5019                   (cdr (mm-make-handle
5020                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5021                         (list "attachment")
5022                         (format "Deleted attachment (%s bytes)" bsize))))))
5023       ;; (set-buffer gnus-summary-buffer)
5024       (gnus-article-edit-part handles id))))
5025
5026 (defun gnus-mime-save-part ()
5027   "Save the MIME part under point."
5028   (interactive)
5029   (gnus-article-check-buffer)
5030   (let ((data (get-text-property (point) 'gnus-data)))
5031     (when data
5032       (mm-save-part data))))
5033
5034 (defun gnus-mime-pipe-part (&optional cmd)
5035   "Pipe the MIME part under point to a process.
5036 Use CMD as the process."
5037   (interactive)
5038   (gnus-article-check-buffer)
5039   (let ((data (get-text-property (point) 'gnus-data)))
5040     (when data
5041       (mm-pipe-part data cmd))))
5042
5043 (defun gnus-mime-view-part ()
5044   "Interactively choose a viewing method for the MIME part under point."
5045   (interactive)
5046   (gnus-article-check-buffer)
5047   (let ((data (get-text-property (point) 'gnus-data)))
5048     (when data
5049       (setq gnus-article-mime-handles
5050             (mm-merge-handles
5051              gnus-article-mime-handles (setq data (copy-sequence data))))
5052       (mm-interactively-view-part data))))
5053
5054 (defun gnus-mime-view-part-as-type-internal ()
5055   (gnus-article-check-buffer)
5056   (let* ((handle (get-text-property (point) 'gnus-data))
5057          (name (or
5058                 ;; Content-Type: foo/bar; name=...
5059                 (mail-content-type-get (mm-handle-type handle) 'name)
5060                 ;; Content-Disposition: attachment; filename=...
5061                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5062          (def-type (and name (mm-default-file-encoding name))))
5063     (or (and def-type (cons def-type 0))
5064         (and handle
5065              (equal (mm-handle-media-supertype handle) "text")
5066              '("text/plain" . 0))
5067         '("application/octet-stream" . 0))))
5068
5069 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5070   "Choose a MIME media type, and view the part as such.
5071 If non-nil, PRED is a predicate to use during completion to limit the
5072 available media-types."
5073   (interactive)
5074   (unless mime-type
5075     (setq mime-type
5076           (let ((default (gnus-mime-view-part-as-type-internal)))
5077             (gnus-completing-read
5078              "View as MIME type"
5079              (if pred
5080                  (gnus-remove-if-not pred (mailcap-mime-types))
5081                (mailcap-mime-types))
5082              nil nil nil
5083              (car default)))))
5084   (gnus-article-check-buffer)
5085   (let ((handle (get-text-property (point) 'gnus-data)))
5086     (when handle
5087       (when (equal (mm-handle-media-type handle) "message/external-body")
5088         (unless (mm-handle-cache handle)
5089           (mm-extern-cache-contents handle))
5090         (setq handle (mm-handle-cache handle)))
5091       (setq handle
5092             (mm-make-handle (mm-handle-buffer handle)
5093                             (cons mime-type (cdr (mm-handle-type handle)))
5094                             (mm-handle-encoding handle)
5095                             (mm-handle-undisplayer handle)
5096                             (mm-handle-disposition handle)
5097                             (mm-handle-description handle)
5098                             nil
5099                             (mm-handle-id handle)))
5100       (setq gnus-article-mime-handles
5101             (mm-merge-handles gnus-article-mime-handles handle))
5102       (when (mm-handle-displayed-p handle)
5103         (mm-remove-part handle))
5104       (gnus-mm-display-part handle))))
5105
5106 (defun gnus-mime-copy-part (&optional handle arg)
5107   "Put the MIME part under point into a new buffer.
5108 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5109 are decompressed."
5110   (interactive (list nil current-prefix-arg))
5111   (gnus-article-check-buffer)
5112   (unless handle
5113     (setq handle (get-text-property (point) 'gnus-data)))
5114   (when handle
5115     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5116                                                'name)
5117                         (mail-content-type-get (mm-handle-disposition handle)
5118                                                'filename)))
5119           contents dont-decode charset coding-system)
5120       (mm-with-unibyte-buffer
5121         (mm-insert-part handle)
5122         (setq contents (or (condition-case nil
5123                                (mm-decompress-buffer filename nil 'sig)
5124                              (error
5125                               (setq dont-decode t)
5126                               nil))
5127                            (buffer-string))))
5128       (setq filename (cond (filename (file-name-nondirectory filename))
5129                            (dont-decode "*raw data*")
5130                            (t "*decoded*")))
5131       (cond
5132        (dont-decode)
5133        ((not arg)
5134         (unless (setq charset (mail-content-type-get
5135                                (mm-handle-type handle) 'charset))
5136           (unless (setq coding-system (mm-with-unibyte-buffer
5137                                         (insert contents)
5138                                         (mm-find-buffer-file-coding-system)))
5139             (setq charset gnus-newsgroup-charset))))
5140        ((numberp arg)
5141         (setq charset (or (cdr (assq arg
5142                                      gnus-summary-show-article-charset-alist))
5143                           (mm-read-coding-system "Charset: ")))))
5144       (switch-to-buffer (generate-new-buffer filename))
5145       (if (or coding-system
5146               (and charset
5147                    (setq coding-system (mm-charset-to-coding-system charset))
5148                    (not (eq coding-system 'ascii))))
5149           (progn
5150             (mm-enable-multibyte)
5151             (insert (mm-decode-coding-string contents coding-system))
5152             (setq buffer-file-coding-system
5153                   (if (boundp 'last-coding-system-used)
5154                       (symbol-value 'last-coding-system-used)
5155                     coding-system)))
5156         (mm-disable-multibyte)
5157         (insert contents)
5158         (setq buffer-file-coding-system mm-binary-coding-system))
5159       ;; We do it this way to make `normal-mode' set the appropriate mode.
5160       (unwind-protect
5161           (progn
5162             (setq buffer-file-name (expand-file-name filename))
5163             (normal-mode))
5164         (setq buffer-file-name nil))
5165       (goto-char (point-min)))))
5166
5167 (defun gnus-mime-print-part (&optional handle filename)
5168   "Print the MIME part under point."
5169   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5170   (gnus-article-check-buffer)
5171   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5172          (contents (and handle (mm-get-part handle)))
5173          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5174          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5175     (when contents
5176         (if printer
5177             (unwind-protect
5178                 (progn
5179                   (mm-save-part-to-file handle file)
5180                   (call-process shell-file-name nil
5181                                 (generate-new-buffer " *mm*")
5182                                 nil
5183                                 shell-command-switch
5184                                 (mm-mailcap-command
5185                                  printer file (mm-handle-type handle))))
5186               (delete-file file))
5187           (with-temp-buffer
5188             (insert contents)
5189             (gnus-print-buffer))
5190           (ps-despool filename)))))
5191
5192 (defun gnus-mime-inline-part (&optional handle arg)
5193   "Insert the MIME part under point into the current buffer.
5194 Compressed files like .gz and .bz2 are decompressed."
5195   (interactive (list nil current-prefix-arg))
5196   (gnus-article-check-buffer)
5197   (unless handle
5198     (setq handle (get-text-property (point) 'gnus-data)))
5199   (when handle
5200     (let ((b (point))
5201           (inhibit-read-only t)
5202           contents charset coding-system)
5203       (if (and (not arg) (mm-handle-undisplayer handle))
5204           (mm-remove-part handle)
5205         (mm-with-unibyte-buffer
5206           (mm-insert-part handle)
5207           (setq contents
5208                 (or (mm-decompress-buffer
5209                      (or (mail-content-type-get (mm-handle-type handle)
5210                                                 'name)
5211                          (mail-content-type-get (mm-handle-disposition handle)
5212                                                 'filename))
5213                      nil t)
5214                     (buffer-string))))
5215         (cond
5216          ((not arg)
5217           (unless (setq charset (mail-content-type-get
5218                                  (mm-handle-type handle) 'charset))
5219             (unless (setq coding-system
5220                           (mm-with-unibyte-buffer
5221                             (insert contents)
5222                             (mm-find-buffer-file-coding-system)))
5223               (setq charset gnus-newsgroup-charset))))
5224          ((numberp arg)
5225           (if (mm-handle-undisplayer handle)
5226               (mm-remove-part handle))
5227           (setq charset
5228                 (or (cdr (assq arg
5229                                gnus-summary-show-article-charset-alist))
5230                     (mm-read-coding-system "Charset: "))))
5231          (t
5232           (if (mm-handle-undisplayer handle)
5233               (mm-remove-part handle))))
5234         (forward-line 2)
5235         (mm-insert-inline
5236          handle
5237          (if (or coding-system
5238                  (and charset
5239                       (setq coding-system
5240                             (mm-charset-to-coding-system charset))
5241                       (not (eq coding-system 'ascii))))
5242              (mm-decode-coding-string contents coding-system)
5243            (mm-string-to-multibyte contents)))
5244         (goto-char b)))))
5245
5246 (defun gnus-mime-set-charset-parameters (handle charset)
5247   "Set CHARSET to parameters in HANDLE.
5248 CHARSET may either be a string or a symbol."
5249   (unless (stringp charset)
5250     (setq charset (symbol-name charset)))
5251   (if (stringp (car handle))
5252       (dolist (h (cdr handle))
5253         (gnus-mime-set-charset-parameters h charset))
5254     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5255                                             "message/external-body")
5256                                      (progn
5257                                        (unless (mm-handle-cache handle)
5258                                          (mm-extern-cache-contents handle))
5259                                        (mm-handle-cache handle))
5260                                    handle)))
5261            (param (assq 'charset (cdr type))))
5262       (if param
5263           (setcdr param charset)
5264         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5265
5266 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5267   "Insert the MIME part under point into the current buffer using the
5268 specified charset."
5269   (interactive (list nil current-prefix-arg))
5270   (gnus-article-check-buffer)
5271   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5272         (fun (get-text-property (point) 'gnus-callback))
5273         (gnus-newsgroup-ignored-charsets 'gnus-all)
5274         charset form preferred parts)
5275     (when handle
5276       (when (prog1
5277                 (and fun
5278                      (setq charset
5279                            (or (cdr (assq
5280                                      arg
5281                                      gnus-summary-show-article-charset-alist))
5282                                (mm-read-coding-system "Charset: "))))
5283               (if (mm-handle-undisplayer handle)
5284                   (mm-remove-part handle)))
5285         (gnus-mime-set-charset-parameters handle charset)
5286         (when (and (consp (setq form (cdr-safe fun)))
5287                    (setq form (ignore-errors
5288                                 (assq 'gnus-mime-display-alternative form)))
5289                    (setq preferred (caddr form))
5290                    (progn
5291                      (when (eq (car preferred) 'quote)
5292                        (setq preferred (cadr preferred)))
5293                      (not (equal preferred
5294                                  (get-text-property (point) 'gnus-data))))
5295                    (setq parts (get-text-property (point) 'gnus-part))
5296                    (setq parts (cdr (assq parts
5297                                           gnus-article-mime-handle-alist)))
5298                    (equal (mm-handle-media-type parts) "multipart/alternative")
5299                    (setq parts (reverse (cdr parts))))
5300           (setcar (cddr form)
5301                   (list 'quote (or (cadr (member preferred parts))
5302                                    (car parts)))))
5303         (funcall fun handle)))))
5304
5305 (defun gnus-mime-view-part-externally (&optional handle)
5306   "View the MIME part under point with an external viewer."
5307   (interactive)
5308   (gnus-article-check-buffer)
5309   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5310          (mm-user-display-methods nil)
5311          (mm-inlined-types nil)
5312          (mail-parse-charset gnus-newsgroup-charset)
5313          (mail-parse-ignored-charsets
5314           (with-current-buffer gnus-summary-buffer
5315             gnus-newsgroup-ignored-charsets))
5316          (type (mm-handle-media-type handle))
5317          (method (mailcap-mime-info type))
5318          (mm-enable-external t))
5319     (if (not (stringp method))
5320         (gnus-mime-view-part-as-type
5321          nil (lambda (type) (stringp (mailcap-mime-info type))))
5322       (when handle
5323         (mm-display-part handle nil t)))))
5324
5325 (defun gnus-mime-view-part-internally (&optional handle)
5326   "View the MIME part under point with an internal viewer.
5327 If no internal viewer is available, use an external viewer."
5328   (interactive)
5329   (gnus-article-check-buffer)
5330   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5331          (mm-inlined-types '(".*"))
5332          (mm-inline-large-images t)
5333          (mail-parse-charset gnus-newsgroup-charset)
5334          (mail-parse-ignored-charsets
5335           (with-current-buffer gnus-summary-buffer
5336             gnus-newsgroup-ignored-charsets))
5337          (inhibit-read-only t))
5338     (if (not (mm-inlinable-p handle))
5339         (gnus-mime-view-part-as-type
5340          nil (lambda (type) (mm-inlinable-p handle type)))
5341       (when handle
5342         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5343
5344 (defun gnus-mime-action-on-part (&optional action)
5345   "Do something with the MIME attachment at \(point\)."
5346   (interactive
5347    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5348   (gnus-article-check-buffer)
5349   (let ((action-pair (assoc action gnus-mime-action-alist)))
5350     (if action-pair
5351         (funcall (cdr action-pair)))))
5352
5353 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5354   "Call FUNCTION on MIME part N.
5355 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5356 If INTERACTIVE, call FUNCTION interactivly."
5357   (let (window frame)
5358     ;; Check whether the article is displayed.
5359     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5360                  (setq window (get-buffer-window gnus-article-buffer t))
5361                  (frame-visible-p (setq frame (window-frame window))))
5362       (error "No article is displayed"))
5363     (with-current-buffer gnus-article-buffer
5364       ;; Check whether the article displays the right contents.
5365       (unless (with-current-buffer gnus-summary-buffer
5366                 (eq gnus-current-article (gnus-summary-article-number)))
5367         (error "You should select the right article first"))
5368       (if n
5369           (setq n (prefix-numeric-value n))
5370         (let ((pt (point)))
5371           (setq n (or (get-text-property pt 'gnus-part)
5372                       (and (not (bobp))
5373                            (get-text-property (1- pt) 'gnus-part))
5374                       (get-text-property (prog2
5375                                              (forward-line 1)
5376                                              (point)
5377                                            (goto-char pt))
5378                                          'gnus-part)
5379                       (get-text-property
5380                        (or (and (setq pt (previous-single-property-change
5381                                           pt 'gnus-part))
5382                                 (1- pt))
5383                            (next-single-property-change (point) 'gnus-part)
5384                            (point))
5385                        'gnus-part)
5386                       1))))
5387       ;; Check whether the specified part exists.
5388       (when (> n (length gnus-article-mime-handle-alist))
5389         (error "No such part")))
5390     (unless
5391         (progn
5392           ;; To select the window is needed so that the cursor
5393           ;; might be visible on the MIME button.
5394           (select-window (prog1
5395                              window
5396                            (setq window (selected-window))
5397                            ;; Article may be displayed in the other frame.
5398                            (gnus-select-frame-set-input-focus
5399                             (prog1
5400                                 frame
5401                               (setq frame (selected-frame))))))
5402           (when (gnus-article-goto-part n)
5403             ;; We point the cursor and the arrow at the MIME button
5404             ;; when the `function' prompt the user for something.
5405             (unless (and (pos-visible-in-window-p)
5406                          (> (count-lines (point) (window-end))
5407                             (/ (1- (window-height)) 3)))
5408               (recenter (/ (1- (window-height)) 3)))
5409             (let ((cursor-in-non-selected-windows t)
5410                   (overlay-arrow-string "=>")
5411                   (overlay-arrow-position (point-marker)))
5412               (unwind-protect
5413                   (cond
5414                    ((and no-handle interactive)
5415                     (call-interactively function))
5416                    (no-handle
5417                     (funcall function))
5418                    (interactive
5419                     (call-interactively
5420                      function (get-text-property (point) 'gnus-data)))
5421                    (t
5422                     (funcall function
5423                              (get-text-property (point) 'gnus-data))))
5424                 (set-marker overlay-arrow-position nil)
5425                 (unless gnus-auto-select-part
5426                   (gnus-select-frame-set-input-focus frame)
5427                   (select-window window))))
5428             t))
5429       (if gnus-inhibit-mime-unbuttonizing
5430           ;; This is the default though the program shouldn't reach here.
5431           (error "No such part")
5432         ;; The part which doesn't have the MIME button is selected.
5433         ;; So, we display all the buttons and redo it.
5434         (let ((gnus-inhibit-mime-unbuttonizing t))
5435           (gnus-summary-show-article)
5436           (gnus-article-part-wrapper n function no-handle))))))
5437
5438 (defun gnus-article-pipe-part (n)
5439   "Pipe MIME part N, which is the numerical prefix."
5440   (interactive "P")
5441   (gnus-article-part-wrapper n 'mm-pipe-part))
5442
5443 (defun gnus-article-save-part (n)
5444   "Save MIME part N, which is the numerical prefix."
5445   (interactive "P")
5446   (gnus-article-part-wrapper n 'mm-save-part))
5447
5448 (defun gnus-article-interactively-view-part (n)
5449   "View MIME part N interactively, which is the numerical prefix."
5450   (interactive "P")
5451   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5452
5453 (defun gnus-article-copy-part (n)
5454   "Copy MIME part N, which is the numerical prefix."
5455   (interactive "P")
5456   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5457
5458 (defun gnus-article-view-part-as-charset (n)
5459   "View MIME part N using a specified charset.
5460 N is the numerical prefix."
5461   (interactive "P")
5462   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5463
5464 (defun gnus-article-view-part-externally (n)
5465   "View MIME part N externally, which is the numerical prefix."
5466   (interactive "P")
5467   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5468
5469 (defun gnus-article-inline-part (n)
5470   "Inline MIME part N, which is the numerical prefix."
5471   (interactive "P")
5472   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5473
5474 (defun gnus-article-save-part-and-strip (n)
5475   "Save MIME part N and replace it with an external body.
5476 N is the numerical prefix."
5477   (interactive "P")
5478   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5479
5480 (defun gnus-article-replace-part (n)
5481   "Replace MIME part N with an external body.
5482 N is the numerical prefix."
5483   (interactive "P")
5484   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5485
5486 (defun gnus-article-delete-part (n)
5487   "Delete MIME part N and add some information about the removed part.
5488 N is the numerical prefix."
5489   (interactive "P")
5490   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5491
5492 (defun gnus-article-view-part-as-type (n)
5493   "Choose a MIME media type, and view part N as such.
5494 N is the numerical prefix."
5495   (interactive "P")
5496   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5497
5498 (defun gnus-article-mime-match-handle-first (condition)
5499   (if condition
5500       (let (n)
5501         (dolist (ihandle gnus-article-mime-handle-alist)
5502           (if (and (cond
5503                     ((functionp condition)
5504                      (funcall condition (cdr ihandle)))
5505                     ((eq condition 'undisplayed)
5506                      (not (or (mm-handle-undisplayer (cdr ihandle))
5507                               (equal (mm-handle-media-type (cdr ihandle))
5508                                      "multipart/alternative"))))
5509                     ((eq condition 'undisplayed-alternative)
5510                      (not (mm-handle-undisplayer (cdr ihandle))))
5511                     (t t))
5512                    (gnus-article-goto-part (car ihandle))
5513                    (or (not n) (< (car ihandle) n)))
5514               (setq n (car ihandle))))
5515         (or n 1))
5516     1))
5517
5518 (defun gnus-article-view-part (&optional n)
5519   "View MIME part N, which is the numerical prefix.
5520 If the part is already shown, hide the part.  If N is nil, view
5521 all parts."
5522   (interactive "P")
5523   (with-current-buffer gnus-article-buffer
5524     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5525                              gnus-article-mime-match-handle-function)))
5526     (when (> n (length gnus-article-mime-handle-alist))
5527       (error "No such part"))
5528     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5529       (when (gnus-article-goto-part n)
5530         (if (equal (car handle) "multipart/alternative")
5531             (gnus-article-press-button)
5532           (when (eq (gnus-mm-display-part handle) 'internal)
5533             (gnus-set-window-start)))))))
5534
5535 (defsubst gnus-article-mime-total-parts ()
5536   (if (bufferp (car gnus-article-mime-handles))
5537       1 ;; single part
5538     (1- (length gnus-article-mime-handles))))
5539
5540 (defun gnus-mm-display-part (handle)
5541   "Display HANDLE and fix MIME button."
5542   (let ((id (get-text-property (point) 'gnus-part))
5543         (point (point))
5544         (inhibit-read-only t))
5545     (forward-line 1)
5546     (prog1
5547         (let ((window (selected-window))
5548               (mail-parse-charset gnus-newsgroup-charset)
5549               (mail-parse-ignored-charsets
5550                (if (gnus-buffer-live-p gnus-summary-buffer)
5551                    (with-current-buffer gnus-summary-buffer
5552                      gnus-newsgroup-ignored-charsets)
5553                  nil)))
5554           (save-excursion
5555             (unwind-protect
5556                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5557                       (beg (point)))
5558                   (when win
5559                     (select-window win))
5560                   (goto-char point)
5561                   (forward-line)
5562                   (if (mm-handle-displayed-p handle)
5563                       ;; This will remove the part.
5564                       (mm-display-part handle)
5565                     (save-restriction
5566                       (narrow-to-region (point)
5567                                         (if (eobp) (point) (1+ (point))))
5568                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5569                       ;; We narrow to the part itself and
5570                       ;; then call the treatment functions.
5571                       (goto-char (point-min))
5572                       (forward-line 1)
5573                       (narrow-to-region (point) (point-max))
5574                       (gnus-treat-article
5575                        nil id
5576                        (gnus-article-mime-total-parts)
5577                        (mm-handle-media-type handle)))))
5578               (if (window-live-p window)
5579                   (select-window window)))))
5580       (goto-char point)
5581       (gnus-delete-line)
5582       (gnus-insert-mime-button
5583        handle id (list (mm-handle-displayed-p handle)))
5584       (goto-char point))))
5585
5586 (defun gnus-article-goto-part (n)
5587   "Go to MIME part N."
5588   (when gnus-break-pages
5589     (widen))
5590   (prog1
5591       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5592             part handle end next handles)
5593         (when start
5594           (goto-char start)
5595           (if (setq handle (get-text-property start 'gnus-data))
5596               start
5597             ;; Go to the displayed subpart, assuming this is
5598             ;; multipart/alternative.
5599             (setq part start
5600                   end (point-at-eol))
5601             (while (and (not handle)
5602                         part
5603                         (< part end)
5604                         (setq next (text-property-not-all part end
5605                                                           'gnus-data nil)))
5606               (setq part next
5607                     handle (get-text-property part 'gnus-data))
5608               (push (cons handle part) handles)
5609               (unless (mm-handle-displayed-p handle)
5610                 (setq handle nil
5611                       part (text-property-any part end 'gnus-data nil))))
5612             (unless handle
5613               ;; No subpart is displayed, so we find preferred one.
5614               (setq part
5615                     (cdr (assq (mm-preferred-alternative
5616                                 (nreverse (mapcar 'car handles)))
5617                                handles))))
5618             (if part
5619                 (goto-char (1+ part))
5620               start))))
5621     (when gnus-break-pages
5622       (gnus-narrow-to-page))))
5623
5624 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5625   (let ((gnus-tmp-name
5626          (or (mail-content-type-get (mm-handle-type handle) 'name)
5627              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5628              (mail-content-type-get (mm-handle-type handle) 'url)
5629              ""))
5630         (gnus-tmp-type (mm-handle-media-type handle))
5631         (gnus-tmp-description (or (mm-handle-description handle) ""))
5632         (gnus-tmp-dots
5633          (if (if displayed (car displayed)
5634                (mm-handle-displayed-p handle))
5635              "" "..."))
5636         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5637                            (buffer-size)))
5638         gnus-tmp-type-long b e)
5639     (when (string-match ".*/" gnus-tmp-name)
5640       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5641     (setq gnus-tmp-type-long (concat gnus-tmp-type
5642                                      (and (not (equal gnus-tmp-name ""))
5643                                           (concat "; " gnus-tmp-name))))
5644     (unless (equal gnus-tmp-description "")
5645       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5646     (unless (bolp)
5647       (insert "\n"))
5648     (setq b (point))
5649     (gnus-eval-format
5650      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5651      `(keymap ,gnus-mime-button-map
5652               gnus-callback gnus-mm-display-part
5653               gnus-part ,gnus-tmp-id
5654               article-type annotation
5655               gnus-data ,handle))
5656     (setq e (if (bolp)
5657                 ;; Exclude a newline.
5658                 (1- (point))
5659               (point)))
5660     (when gnus-article-button-face
5661       (gnus-overlay-put (gnus-make-overlay b e nil t)
5662                         'face gnus-article-button-face))
5663     (widget-convert-button
5664      'link b e
5665      :mime-handle handle
5666      :action 'gnus-widget-press-button
5667      :button-keymap gnus-mime-button-map
5668      :help-echo
5669      (lambda (widget)
5670        ;; Needed to properly clear the message due to a bug in
5671        ;; wid-edit (XEmacs only).
5672        (if (boundp 'help-echo-owns-message)
5673            (setq help-echo-owns-message t))
5674        (format
5675         "%S: %s the MIME part; %S: more options"
5676         (aref gnus-mouse-2 0)
5677         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5678             "hide" "show")
5679         (aref gnus-down-mouse-3 0))))))
5680
5681 (defun gnus-widget-press-button (elems el)
5682   (goto-char (widget-get elems :from))
5683   (gnus-article-press-button))
5684
5685 (defvar gnus-displaying-mime nil)
5686
5687 (defun gnus-display-mime (&optional ihandles)
5688   "Display the MIME parts."
5689   (save-excursion
5690     (save-selected-window
5691       (let ((window (get-buffer-window gnus-article-buffer))
5692             (point (point)))
5693         (when window
5694           (select-window window)
5695           ;; We have to do this since selecting the window
5696           ;; may change the point.  So we set the window point.
5697           (set-window-point window point)))
5698       (let ((handles ihandles)
5699             (inhibit-read-only t)
5700             handle)
5701         (cond (handles)
5702               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5703                (when gnus-article-emulate-mime
5704                  (mm-uu-dissect-text-parts handles)))
5705               (gnus-article-emulate-mime
5706                (setq handles (mm-uu-dissect))))
5707         (when (and (not ihandles)
5708                    (not gnus-displaying-mime))
5709           ;; Top-level call; we clean up.
5710           (when gnus-article-mime-handles
5711             (mm-destroy-parts gnus-article-mime-handles)
5712             (setq gnus-article-mime-handle-alist nil));; A trick.
5713           (setq gnus-article-mime-handles handles)
5714           ;; We allow users to glean info from the handles.
5715           (when gnus-article-mime-part-function
5716             (gnus-mime-part-function handles)))
5717         (if (and handles
5718                  (or (not (stringp (car handles)))
5719                      (cdr handles)))
5720             (progn
5721               (when (and (not ihandles)
5722                          (not gnus-displaying-mime))
5723                 ;; Clean up for mime parts.
5724                 (article-goto-body)
5725                 (delete-region (point) (point-max)))
5726               (let ((gnus-displaying-mime t))
5727                 (gnus-mime-display-part handles)))
5728           (save-restriction
5729             (article-goto-body)
5730             (narrow-to-region (point) (point-max))
5731             (gnus-treat-article nil 1 1 "text/plain")
5732             (widen)))
5733         (unless ihandles
5734           ;; Highlight the headers.
5735           (save-excursion
5736             (save-restriction
5737               (article-goto-body)
5738               (narrow-to-region (point-min) (point))
5739               (gnus-article-save-original-date
5740                (gnus-treat-article 'head)))))))
5741     ;; Cope with broken MIME messages.
5742     (goto-char (point-max))
5743     (unless (bolp)
5744       (insert "\n"))))
5745
5746 (defcustom gnus-mime-display-multipart-as-mixed nil
5747   "Display \"multipart\" parts as  \"multipart/mixed\".
5748
5749 If t, it overrides nil values of
5750 `gnus-mime-display-multipart-alternative-as-mixed' and
5751 `gnus-mime-display-multipart-related-as-mixed'."
5752   :group 'gnus-article-mime
5753   :type 'boolean)
5754
5755 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5756   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5757   :version "22.1"
5758   :group 'gnus-article-mime
5759   :type 'boolean)
5760
5761 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5762   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5763
5764 If displaying \"text/html\" is discouraged \(see
5765 `mm-discouraged-alternatives'\) images or other material inside a
5766 \"multipart/related\" part might be overlooked when this variable is nil."
5767   :version "22.1"
5768   :group 'gnus-article-mime
5769   :type 'boolean)
5770
5771 (defun gnus-mime-display-part (handle)
5772   (cond
5773    ;; Maybe a broken MIME message.
5774    ((null handle))
5775    ;; Single part.
5776    ((not (stringp (car handle)))
5777     (gnus-mime-display-single handle))
5778    ;; User-defined multipart
5779    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5780     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5781              handle))
5782    ;; multipart/alternative
5783    ((and (equal (car handle) "multipart/alternative")
5784          (not (or gnus-mime-display-multipart-as-mixed
5785                   gnus-mime-display-multipart-alternative-as-mixed)))
5786     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5787       (push (cons id handle) gnus-article-mime-handle-alist)
5788       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5789    ;; multipart/related
5790    ((and (equal (car handle) "multipart/related")
5791          (not (or gnus-mime-display-multipart-as-mixed
5792                   gnus-mime-display-multipart-related-as-mixed)))
5793     ;;;!!!We should find the start part, but we just default
5794     ;;;!!!to the first part.
5795     ;;(gnus-mime-display-part (cadr handle))
5796     ;;;!!! Most multipart/related is an HTML message plus images.
5797     ;;;!!! Unfortunately we are unable to let W3 display those
5798     ;;;!!! included images, so we just display it as a mixed multipart.
5799     ;;(gnus-mime-display-mixed (cdr handle))
5800     ;;;!!! No, w3 can display everything just fine.
5801     (gnus-mime-display-part (cadr handle)))
5802    ((equal (car handle) "multipart/signed")
5803     (gnus-add-wash-type 'signed)
5804     (gnus-mime-display-security handle))
5805    ((equal (car handle) "multipart/encrypted")
5806     (gnus-add-wash-type 'encrypted)
5807     (gnus-mime-display-security handle))
5808    ;; Other multiparts are handled like multipart/mixed.
5809    (t
5810     (gnus-mime-display-mixed (cdr handle)))))
5811
5812 (defun gnus-mime-part-function (handles)
5813   (if (stringp (car handles))
5814       (mapcar 'gnus-mime-part-function (cdr handles))
5815     (funcall gnus-article-mime-part-function handles)))
5816
5817 (defun gnus-mime-display-mixed (handles)
5818   (mapcar 'gnus-mime-display-part handles))
5819
5820 (defun gnus-mime-display-single (handle)
5821   (let ((type (mm-handle-media-type handle))
5822         (ignored gnus-ignored-mime-types)
5823         (not-attachment t)
5824         (move nil)
5825         display text)
5826     (catch 'ignored
5827       (progn
5828         (while ignored
5829           (when (string-match (pop ignored) type)
5830             (throw 'ignored nil)))
5831         (if (and (setq not-attachment
5832                        (and (not (mm-inline-override-p handle))
5833                             (or (not (mm-handle-disposition handle))
5834                                 (equal (car (mm-handle-disposition handle))
5835                                        "inline")
5836                                 (mm-attachment-override-p handle))))
5837                  (mm-automatic-display-p handle)
5838                  (or (and
5839                       (mm-inlinable-p handle)
5840                       (mm-inlined-p handle))
5841                      (mm-automatic-external-display-p type)))
5842             (setq display t)
5843           (when (equal (mm-handle-media-supertype handle) "text")
5844             (setq text t)))
5845         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5846               beg)
5847           (push (cons id handle) gnus-article-mime-handle-alist)
5848           (when (and display
5849                      (equal (mm-handle-media-supertype handle) "message"))
5850             (insert-char
5851              ?\n
5852              (cond ((not (bolp)) 2)
5853                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5854                    (t 1))))
5855           (when (or (not display)
5856                     (not (gnus-unbuttonized-mime-type-p type))
5857                     (eq id gnus-mime-buttonized-part-id))
5858             (gnus-insert-mime-button
5859              handle id (list (or display (and not-attachment text))))
5860             (gnus-article-insert-newline)
5861             ;; Remember modify the number of forward lines.
5862             (setq move t))
5863           (setq beg (point))
5864           (cond
5865            (display
5866             (when move
5867               (forward-line -1)
5868               (setq beg (point)))
5869             (let ((mail-parse-charset gnus-newsgroup-charset)
5870                   (mail-parse-ignored-charsets
5871                    (save-excursion (condition-case ()
5872                                        (set-buffer gnus-summary-buffer)
5873                                      (error))
5874                                    gnus-newsgroup-ignored-charsets)))
5875               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5876             (goto-char (point-max)))
5877            ((and text not-attachment)
5878             (when move
5879               (forward-line -1)
5880               (setq beg (point)))
5881             (gnus-article-insert-newline)
5882             (mm-insert-inline
5883              handle
5884              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5885                                                        'charset)
5886                                 (and (equal type "text/calendar") 'utf-8))))
5887                (cond ((not charset)
5888                       (mm-string-as-multibyte (mm-get-part handle)))
5889                      ((eq charset 'gnus-decoded)
5890                       (with-current-buffer (mm-handle-buffer handle)
5891                         (buffer-string)))
5892                      (t
5893                       (mm-decode-string (mm-get-part handle) charset)))))
5894             (goto-char (point-max))))
5895           ;; Do highlighting.
5896           (save-excursion
5897             (save-restriction
5898               (narrow-to-region beg (point))
5899               (if (eq handle gnus-article-mime-handles)
5900                   ;; The format=flowed case.
5901                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5902                 ;; Don't count signature parts that are never displayed.
5903                 ;; The part number should be re-calculated supposing this
5904                 ;; might be a message/rfc822 part.
5905                 (let (handles)
5906                   (dolist (part gnus-article-mime-handles)
5907                     (unless (or (stringp part)
5908                                 (equal (car (mm-handle-type part))
5909                                        "application/pgp-signature"))
5910                       (push part handles)))
5911                   (gnus-treat-article
5912                    nil (length (memq handle handles)) (length handles)
5913                    (mm-handle-media-type handle)))))))))))
5914
5915 (defun gnus-unbuttonized-mime-type-p (type)
5916   "Say whether TYPE is to be unbuttonized."
5917   (unless gnus-inhibit-mime-unbuttonizing
5918     (when (catch 'found
5919             (let ((types gnus-unbuttonized-mime-types))
5920               (while types
5921                 (when (string-match (pop types) type)
5922                   (throw 'found t)))))
5923       (not (catch 'found
5924              (let ((types gnus-buttonized-mime-types))
5925                (while types
5926                  (when (string-match (pop types) type)
5927                    (throw 'found t)))))))))
5928
5929 (defun gnus-article-insert-newline ()
5930   "Insert a newline, but mark it as undeletable."
5931   (gnus-put-text-property
5932    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5933
5934 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5935   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5936          (ihandles handles)
5937          (point (point))
5938          handle (inhibit-read-only t) from props begend not-pref)
5939     (save-window-excursion
5940       (save-restriction
5941         (when ibegend
5942           (narrow-to-region (car ibegend)
5943                             (or (cdr ibegend)
5944                                 (progn
5945                                   (goto-char (car ibegend))
5946                                   (forward-line 2)
5947                                   (point))))
5948           (delete-region (point-min) (point-max))
5949           (mm-remove-parts handles))
5950         (setq begend (list (point-marker)))
5951         ;; Do the toggle.
5952         (unless (setq not-pref (cadr (member preferred ihandles)))
5953           (setq not-pref (car ihandles)))
5954         (when (or ibegend
5955                   (not preferred)
5956                   (not (gnus-unbuttonized-mime-type-p
5957                         "multipart/alternative")))
5958           (gnus-add-text-properties
5959            (setq from (point))
5960            (progn
5961              (insert (format "%d.  " id))
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 ',not-pref ',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              article-type multipart))
5975           (widget-convert-button 'link from (point)
5976                                  :action 'gnus-widget-press-button
5977                                  :button-keymap gnus-widget-button-keymap)
5978           ;; Do the handles
5979           (while (setq handle (pop handles))
5980             (gnus-add-text-properties
5981              (setq from (point))
5982              (progn
5983                (insert (format "(%c) %-18s"
5984                                (if (equal handle preferred) ?* ? )
5985                                (mm-handle-media-type handle)))
5986                (point))
5987              `(gnus-callback
5988                (lambda (handles)
5989                  (unless ,(not ibegend)
5990                    (setq gnus-article-mime-handle-alist
5991                          ',gnus-article-mime-handle-alist))
5992                  (gnus-mime-display-alternative
5993                   ',ihandles ',handle ',begend ,id))
5994                keymap ,gnus-mime-button-map
5995                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5996                face ,gnus-article-button-face
5997                gnus-part ,id
5998                gnus-data ,handle))
5999             (widget-convert-button 'link from (point)
6000                                    :action 'gnus-widget-press-button
6001                                    :button-keymap gnus-widget-button-keymap)
6002             (insert "  "))
6003           (insert "\n\n"))
6004         (when preferred
6005           (if (stringp (car preferred))
6006               (gnus-display-mime preferred)
6007             (let ((mail-parse-charset gnus-newsgroup-charset)
6008                   (mail-parse-ignored-charsets
6009                    (with-current-buffer gnus-summary-buffer
6010                      gnus-newsgroup-ignored-charsets)))
6011               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6012               ;; Do highlighting.
6013               (save-excursion
6014                 (save-restriction
6015                   (narrow-to-region (car begend) (point-max))
6016                   (gnus-treat-article
6017                    nil (length gnus-article-mime-handle-alist)
6018                    (gnus-article-mime-total-parts)
6019                    (mm-handle-media-type preferred))))))
6020           (goto-char (point-max))
6021           (setcdr begend (point-marker)))))
6022     (when ibegend
6023       (goto-char point))))
6024
6025 (defconst gnus-article-wash-status-strings
6026   (let ((alist '((cite "c" "Possible hidden citation text"
6027                        " " "All citation text visible")
6028                  (headers "h" "Hidden headers"
6029                           " " "All headers visible.")
6030                  (pgp "p" "Encrypted or signed message status hidden"
6031                       " " "No hidden encryption nor digital signature status")
6032                  (signature "s" "Signature has been hidden"
6033                             " " "Signature is visible")
6034                  (overstrike "o" "Overstrike (^H) characters applied"
6035                              " " "No overstrike characters applied")
6036                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6037                            " " "No /*_emphasis_*/ characters applied")))
6038         result)
6039     (dolist (entry alist result)
6040       (let ((key (nth 0 entry))
6041             (on (copy-sequence (nth 1 entry)))
6042             (on-help (nth 2 entry))
6043             (off (copy-sequence (nth 3 entry)))
6044             (off-help (nth 4 entry)))
6045         (put-text-property 0 1 'help-echo on-help on)
6046         (put-text-property 0 1 'help-echo off-help off)
6047         (push (list key on off) result))))
6048   "Alist of strings describing wash status in the mode line.
6049 Each entry has the form (KEY ON OF), where the KEY is a symbol
6050 representing the particular washing function, ON is the string to use
6051 in the article mode line when the washing function is active, and OFF
6052 is the string to use when it is inactive.")
6053
6054 (defun gnus-article-wash-status-entry (key value)
6055   (let ((entry (assoc key gnus-article-wash-status-strings)))
6056     (if value (nth 1 entry) (nth 2 entry))))
6057
6058 (defun gnus-article-wash-status ()
6059   "Return a string which display status of article washing."
6060   (with-current-buffer gnus-article-buffer
6061     (let ((cite (memq 'cite gnus-article-wash-types))
6062           (headers (memq 'headers gnus-article-wash-types))
6063           (boring (memq 'boring-headers gnus-article-wash-types))
6064           (pgp (memq 'pgp gnus-article-wash-types))
6065           (pem (memq 'pem gnus-article-wash-types))
6066           (signed (memq 'signed gnus-article-wash-types))
6067           (encrypted (memq 'encrypted gnus-article-wash-types))
6068           (signature (memq 'signature gnus-article-wash-types))
6069           (overstrike (memq 'overstrike gnus-article-wash-types))
6070           (emphasis (memq 'emphasis gnus-article-wash-types)))
6071       (concat
6072        (gnus-article-wash-status-entry 'cite cite)
6073        (gnus-article-wash-status-entry 'headers (or headers boring))
6074        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6075        (gnus-article-wash-status-entry 'signature signature)
6076        (gnus-article-wash-status-entry 'overstrike overstrike)
6077        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6078
6079 (defun gnus-add-wash-type (type)
6080   "Add a washing of TYPE to the current status."
6081   (add-to-list 'gnus-article-wash-types type))
6082
6083 (defun gnus-delete-wash-type (type)
6084   "Add a washing of TYPE to the current status."
6085   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6086
6087 (defun gnus-add-image (category image)
6088   "Add IMAGE of CATEGORY to the list of displayed images."
6089   (let ((entry (assq category gnus-article-image-alist)))
6090     (unless entry
6091       (setq entry (list category))
6092       (push entry gnus-article-image-alist))
6093     (nconc entry (list image))))
6094
6095 (defun gnus-delete-images (category)
6096   "Delete all images in CATEGORY."
6097   (let ((entry (assq category gnus-article-image-alist)))
6098     (dolist (image (cdr entry))
6099       (gnus-remove-image image category))
6100     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6101     (gnus-delete-wash-type category)))
6102
6103 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6104
6105 (defun gnus-article-maybe-hide-headers ()
6106   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6107 Provided for backwards compatibility."
6108   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6109                  (not (with-current-buffer gnus-summary-buffer
6110                         gnus-have-all-headers)))
6111              (not gnus-inhibit-hiding))
6112     (gnus-article-hide-headers)))
6113
6114 ;;; Article savers.
6115
6116 (defun gnus-output-to-file (file-name)
6117   "Append the current article to a file named FILE-NAME.
6118 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6119 text and used as the value of the coding cookie which is added to the
6120 top of a file.  Otherwise, this function saves a raw article without
6121 the coding cookie."
6122   (let* ((artbuf (current-buffer))
6123          (file-name-coding-system nnmail-pathname-coding-system)
6124          (coding gnus-article-save-coding-system)
6125          (coding-system-for-read (if coding
6126                                      nil ;; Rely on the coding cookie.
6127                                    mm-text-coding-system))
6128          (coding-system-for-write (or coding
6129                                       mm-text-coding-system-for-write
6130                                       mm-text-coding-system))
6131          (exists (file-exists-p file-name)))
6132     (with-temp-buffer
6133       (when exists
6134         (insert-file-contents file-name)
6135         (goto-char (point-min))
6136         ;; Remove the existing coding cookie.
6137         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6138           (delete-region (match-beginning 0) (match-end 0))))
6139       (goto-char (point-max))
6140       (insert-buffer-substring artbuf)
6141       ;; Append newline at end of the buffer as separator, and then
6142       ;; save it to file.
6143       (goto-char (point-max))
6144       (insert "\n")
6145       (when coding
6146         ;; If the coding system is not suitable to encode the text,
6147         ;; ask a user for a proper one.
6148         (when (fboundp 'select-safe-coding-system)
6149           (setq coding (coding-system-base
6150                         (save-window-excursion
6151                           (select-safe-coding-system (point-min) (point-max)
6152                                                      coding))))
6153           (setq coding-system-for-write
6154                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6155                     coding)))
6156         (goto-char (point-min))
6157         ;; Add the coding cookie.
6158         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6159                         coding-system-for-write)))
6160       (if exists
6161           (progn
6162             (write-region (point-min) (point-max) file-name nil 'no-message)
6163             (message "Appended to %s" file-name))
6164         (write-region (point-min) (point-max) file-name))))
6165   t)
6166
6167 (defun gnus-narrow-to-page (&optional arg)
6168   "Narrow the article buffer to a page.
6169 If given a numerical ARG, move forward ARG pages."
6170   (interactive "P")
6171   (setq arg (if arg (prefix-numeric-value arg) 0))
6172   (with-current-buffer gnus-article-buffer
6173     (widen)
6174     ;; Remove any old next/prev buttons.
6175     (when (gnus-visual-p 'page-marker)
6176       (let ((inhibit-read-only t))
6177         (gnus-remove-text-with-property 'gnus-prev)
6178         (gnus-remove-text-with-property 'gnus-next)))
6179     (let (st nd pt)
6180       (when (save-excursion
6181               (cond ((< arg 0)
6182                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6183                          (prog1
6184                              (setq nd (match-beginning 0)
6185                                    pt nd)
6186                            (when (re-search-backward page-delimiter nil t)
6187                              (setq st (match-end 0))))
6188                        (when (re-search-forward page-delimiter nil t)
6189                          (setq nd (match-beginning 0)
6190                                pt (point-min)))))
6191                     ((> arg 0)
6192                      (if (re-search-forward page-delimiter nil 'move arg)
6193                          (prog1
6194                              (setq st (match-end 0)
6195                                    pt st)
6196                            (when (re-search-forward page-delimiter nil t)
6197                              (setq nd (match-beginning 0))))
6198                        (when (re-search-backward page-delimiter nil t)
6199                          (setq st (match-end 0)
6200                                pt (point-max)))))
6201                     (t
6202                      (when (re-search-backward page-delimiter nil t)
6203                        (goto-char (setq st (match-end 0))))
6204                      (when (re-search-forward page-delimiter nil t)
6205                        (setq nd (match-beginning 0)))
6206                      (or st nd))))
6207         (setq gnus-page-broken t)
6208         (when pt (goto-char pt))
6209         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6210         (when (gnus-visual-p 'page-marker)
6211           (save-excursion
6212             (when nd
6213               (goto-char nd)
6214               (gnus-insert-next-page-button))
6215             (when st
6216               (goto-char st)
6217               (gnus-insert-prev-page-button))))))))
6218
6219 ;; Article mode commands
6220
6221 (defun gnus-article-goto-next-page ()
6222   "Show the next page of the article."
6223   (interactive)
6224   (when (gnus-article-next-page)
6225     (goto-char (point-min))
6226     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6227
6228
6229 (defun gnus-article-goto-prev-page ()
6230   "Show the previous page of the article."
6231   (interactive)
6232   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6233       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6234     (gnus-article-prev-page nil)))
6235
6236 ;; This is cleaner but currently breaks `gnus-pick-mode':
6237 ;;
6238 ;; (defun gnus-article-goto-next-page ()
6239 ;;   "Show the next page of the article."
6240 ;;   (interactive)
6241 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6242 ;;     (gnus-summary-next-page)))
6243 ;;
6244 ;; (defun gnus-article-goto-prev-page ()
6245 ;;   "Show the next page of the article."
6246 ;;   (interactive)
6247 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6248 ;;     (gnus-summary-prev-page)))
6249
6250 (defun gnus-article-next-page (&optional lines)
6251   "Show the next page of the current article.
6252 If end of article, return non-nil.  Otherwise return nil.
6253 Argument LINES specifies lines to be scrolled up."
6254   (interactive "p")
6255   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6256   (if (and (not (and gnus-article-over-scroll
6257                      (> (count-lines (window-start) (point-max))
6258                         (if (featurep 'xemacs)
6259                             (or lines (1- (window-height)))
6260                           (+ (or lines (1- (window-height))) scroll-margin)))))
6261            (save-excursion
6262              (end-of-line)
6263              (and (pos-visible-in-window-p)     ;Not continuation line.
6264                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6265       ;; Nothing in this page.
6266       (if (or (not gnus-page-broken)
6267               (save-excursion
6268                 (save-restriction
6269                   (widen)
6270                   (forward-line)
6271                   (eobp)))) ;Real end-of-buffer?
6272           (progn
6273             (when gnus-article-over-scroll
6274               (gnus-article-next-page-1 lines))
6275             t)                  ;Nothing more.
6276         (gnus-narrow-to-page 1)         ;Go to next page.
6277         nil)
6278     ;; More in this page.
6279     (gnus-article-next-page-1 lines)
6280     nil))
6281
6282 (defun gnus-article-beginning-of-window ()
6283   "Move point to the beginning of the window.
6284 In Emacs, the point is placed at the line number which `scroll-margin'
6285 specifies."
6286   (if (featurep 'xemacs)
6287       (move-to-window-line 0)
6288     ;; There is an obscure bug in Emacs that makes it impossible to
6289     ;; scroll past big pictures in the article buffer.  Try to fix
6290     ;; this by adding a sanity check by counting the lines visible.
6291     (when (> (count-lines (window-start) (window-end)) 30)
6292       (move-to-window-line
6293        (min (max 0 scroll-margin)
6294             (max 1 (- (window-height)
6295                       (if mode-line-format 1 0)
6296                       (if header-line-format 1 0)
6297                       2)))))))
6298
6299 (defun gnus-article-next-page-1 (lines)
6300   (condition-case ()
6301       (let ((scroll-in-place nil))
6302         (scroll-up lines))
6303     (end-of-buffer
6304      ;; Long lines may cause an end-of-buffer error.
6305      (goto-char (point-max))))
6306   (gnus-article-beginning-of-window))
6307
6308 (defun gnus-article-prev-page (&optional lines)
6309   "Show previous page of current article.
6310 Argument LINES specifies lines to be scrolled down."
6311   (interactive "p")
6312   (move-to-window-line 0)
6313   (if (and gnus-page-broken
6314            (bobp)
6315            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6316       (progn
6317         (gnus-narrow-to-page -1)        ;Go to previous page.
6318         (goto-char (point-max))
6319         (recenter (if gnus-article-over-scroll
6320                       (if lines
6321                           (max (if (featurep 'xemacs)
6322                                    lines
6323                                  (+ lines scroll-margin))
6324                                3)
6325                         (- (window-height) 2))
6326                     -1)))
6327     (prog1
6328         (condition-case ()
6329             (let ((scroll-in-place nil))
6330               (scroll-down lines))
6331           (beginning-of-buffer
6332            (goto-char (point-min))))
6333       (gnus-article-beginning-of-window))))
6334
6335 (defun gnus-article-only-boring-p ()
6336   "Decide whether there is only boring text remaining in the article.
6337 Something \"interesting\" is a word of at least two letters that does
6338 not have a face in `gnus-article-boring-faces'."
6339   (when (and gnus-article-skip-boring
6340              (boundp 'gnus-article-boring-faces)
6341              (symbol-value 'gnus-article-boring-faces))
6342     (save-excursion
6343       (let ((inhibit-point-motion-hooks t))
6344         (catch 'only-boring
6345           (while (re-search-forward "\\b\\w\\w" nil t)
6346             (forward-char -1)
6347             (when (not (gnus-intersection
6348                         (gnus-faces-at (point))
6349                         (symbol-value 'gnus-article-boring-faces)))
6350               (throw 'only-boring nil)))
6351           (throw 'only-boring t))))))
6352
6353 (defun gnus-article-refer-article ()
6354   "Read article specified by message-id around point."
6355   (interactive)
6356   (save-excursion
6357     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6358     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6359     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6360         (let ((msg-id (concat "<" (match-string 0) ">")))
6361           (set-buffer gnus-summary-buffer)
6362           (gnus-summary-refer-article msg-id))
6363       (error "No references around point"))))
6364
6365 (defun gnus-article-show-summary ()
6366   "Reconfigure windows to show summary buffer."
6367   (interactive)
6368   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6369       (error "There is no summary buffer for this article buffer")
6370     (gnus-article-set-globals)
6371     (gnus-configure-windows 'article)
6372     (gnus-summary-goto-subject gnus-current-article)
6373     (gnus-summary-position-point)))
6374
6375 (defun gnus-article-describe-briefly ()
6376   "Describe article mode commands briefly."
6377   (interactive)
6378   (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")))
6379
6380 (defun gnus-article-check-buffer ()
6381   "Beep if not in an article buffer."
6382   (unless (equal major-mode 'gnus-article-mode)
6383     (error "Command invoked outside of a Gnus article buffer")))
6384
6385 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6386   "Read a summary buffer key sequence and execute it from the article buffer."
6387   (interactive "P")
6388   (gnus-article-check-buffer)
6389   (let ((nosaves
6390          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6391            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6392            "=" "^" "\M-^" "|"))
6393         (nosave-but-article
6394          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6395            "An" "Ap" [?A (meta return)] [?A delete]))
6396         (nosave-in-article
6397          '("AS" "\C-d"))
6398         (up-to-top
6399          '("n" "Gn" "p" "Gp"))
6400         keys new-sum-point)
6401     (with-current-buffer gnus-article-current-summary
6402       (let (gnus-pick-mode)
6403         (setq unread-command-events (nconc unread-command-events
6404                                            (list (or key last-command-event)))
6405               keys (if (featurep 'xemacs)
6406                        (events-to-keys (read-key-sequence nil t))
6407                      (read-key-sequence nil t)))))
6408
6409     (message "")
6410
6411     (cond
6412      ((eq (aref keys (1- (length keys))) ?\C-h)
6413       (gnus-article-describe-bindings (substring keys 0 -1)))
6414      ((or (member keys nosaves)
6415           (member keys nosave-but-article)
6416           (member keys nosave-in-article))
6417       (let (func)
6418         (save-window-excursion
6419           (pop-to-buffer gnus-article-current-summary)
6420           ;; We disable the pick minor mode commands.
6421           (let (gnus-pick-mode)
6422             (setq func (key-binding keys t))))
6423         (if (or (not func)
6424                 (numberp func))
6425             (ding)
6426           (unless (member keys nosave-in-article)
6427             (set-buffer gnus-article-current-summary))
6428           (call-interactively func)
6429           (setq new-sum-point (point)))
6430         (when (member keys nosave-but-article)
6431           (pop-to-buffer gnus-article-buffer))))
6432      (t
6433       ;; These commands should restore window configuration.
6434       (let ((obuf (current-buffer))
6435             (owin (current-window-configuration))
6436             win func in-buffer selected new-sum-start new-sum-hscroll err)
6437         (cond (not-restore-window
6438                (pop-to-buffer gnus-article-current-summary)
6439                (setq win (selected-window)))
6440               ((setq win (get-buffer-window gnus-article-current-summary))
6441                (select-window win))
6442               (t
6443                (let ((summary-buffer gnus-article-current-summary))
6444                  (gnus-configure-windows 'article)
6445                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6446                    (let ((gnus-buffer-configuration
6447                           '((article ((vertical 1.0
6448                                                 (summary 0.25 point)
6449                                                 (article 1.0)))))))
6450                      (gnus-configure-windows 'article))
6451                    (setq win (get-buffer-window summary-buffer 'visible)))
6452                  (gnus-select-frame-set-input-focus (window-frame win))
6453                  (select-window win))))
6454         (setq in-buffer (current-buffer))
6455         ;; We disable the pick minor mode commands.
6456         (if (and (setq func (let (gnus-pick-mode)
6457                               (key-binding keys t)))
6458                  (functionp func)
6459                  (condition-case code
6460                      (progn
6461                        (call-interactively func)
6462                        t)
6463                    (error
6464                     (setq err code)
6465                     nil)))
6466             (progn
6467               (when (eq win (selected-window))
6468                 (setq new-sum-point (point)
6469                       new-sum-start (window-start win)
6470                       new-sum-hscroll (window-hscroll win)))
6471               (when (or (eq in-buffer (current-buffer))
6472                         (when (eq obuf (current-buffer))
6473                           (set-buffer in-buffer)
6474                           t))
6475                 (setq selected (gnus-summary-select-article))
6476                 (set-buffer obuf)
6477                 (unless not-restore-window
6478                   (set-window-configuration owin))
6479                 (when (and (eq selected 'old)
6480                            new-sum-point)
6481                   (set-window-start (get-buffer-window (current-buffer))
6482                                     1)
6483                   (set-window-point (get-buffer-window (current-buffer))
6484                                     (if (article-goto-body)
6485                                         (1- (point))
6486                                       (point))))
6487                 (when (and (not not-restore-window)
6488                            new-sum-point
6489                            (window-live-p win)
6490                            (with-current-buffer (window-buffer win)
6491                              (eq major-mode 'gnus-summary-mode)))
6492                   (set-window-point win new-sum-point)
6493                   (set-window-start win new-sum-start)
6494                   (set-window-hscroll win new-sum-hscroll))))
6495           (set-window-configuration owin)
6496           (if err
6497               (signal (car err) (cdr err))
6498             (ding))))))))
6499
6500 (defun gnus-article-read-summary-send-keys ()
6501   (interactive)
6502   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6503     (gnus-article-read-summary-keys)))
6504
6505 (defun gnus-article-describe-key (key)
6506   "Display documentation of the function invoked by KEY.
6507 KEY is a string or a vector."
6508   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6509                        (read-key-sequence "Describe key: "))))
6510   (gnus-article-check-buffer)
6511   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6512                                   gnus-article-read-summary-send-keys))
6513       (with-current-buffer gnus-article-current-summary
6514         (setq unread-command-events
6515               (if (featurep 'xemacs)
6516                   (append key nil)
6517                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6518                                         (list 'meta (- x 128))
6519                                       x))
6520                         key)))
6521         (let ((cursor-in-echo-area t)
6522               gnus-pick-mode)
6523           (describe-key (read-key-sequence nil t))))
6524     (describe-key key)))
6525
6526 (defun gnus-article-describe-key-briefly (key &optional insert)
6527   "Display documentation of the function invoked by KEY.
6528 KEY is a string or a vector."
6529   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6530                        (read-key-sequence "Describe key: "))
6531                      current-prefix-arg))
6532   (gnus-article-check-buffer)
6533   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6534                                   gnus-article-read-summary-send-keys))
6535       (with-current-buffer gnus-article-current-summary
6536         (setq unread-command-events
6537               (if (featurep 'xemacs)
6538                   (append key nil)
6539                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6540                                         (list 'meta (- x 128))
6541                                       x))
6542                         key)))
6543         (let ((cursor-in-echo-area t)
6544               gnus-pick-mode)
6545           (describe-key-briefly (read-key-sequence nil t) insert)))
6546     (describe-key-briefly key insert)))
6547
6548 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6549 (defvar gnus-agent-summary-mode)
6550 (defvar gnus-draft-mode)
6551 ;; Calling help-buffer will autoload help-mode.
6552 (defvar help-xref-stack-item)
6553 ;; Emacs 22 doesn't load it in the batch mode.
6554 (eval-when-compile
6555   (autoload 'help-buffer "help-mode"))
6556
6557 (defun gnus-article-describe-bindings (&optional prefix)
6558   "Show a list of all defined keys, and their definitions.
6559 The optional argument PREFIX, if non-nil, should be a key sequence;
6560 then we display only bindings that start with that prefix."
6561   (interactive)
6562   (gnus-article-check-buffer)
6563   (let ((keymap (copy-keymap gnus-article-mode-map))
6564         (map (copy-keymap gnus-article-send-map))
6565         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6566         parent agent draft)
6567     (define-key keymap "S" map)
6568     (define-key map [t] nil)
6569     (with-current-buffer gnus-article-current-summary
6570       (set-keymap-parent
6571        keymap
6572        (if (setq parent (keymap-parent gnus-article-mode-map))
6573            (prog1
6574                (setq parent (copy-keymap parent))
6575              (set-keymap-parent parent (current-local-map)))
6576          (current-local-map)))
6577       (set-keymap-parent map (key-binding "S"))
6578       (let (key def gnus-pick-mode)
6579         (while sumkeys
6580           (setq key (pop sumkeys))
6581           (cond ((and (vectorp key) (= (length key) 1)
6582                       (consp (setq def (aref key 0)))
6583                       (numberp (car def)) (numberp (cdr def)))
6584                  (when (< (max (car def) (cdr def)) 128)
6585                    (setq sumkeys
6586                          (append (mapcar
6587                                   #'vector
6588                                   (nreverse (gnus-uncompress-range def)))
6589                                  sumkeys))))
6590                 ((setq def (key-binding key))
6591                  (unless (eq def 'undefined)
6592                    (define-key keymap key def))))))
6593       (when (boundp 'gnus-agent-summary-mode)
6594         (setq agent gnus-agent-summary-mode))
6595       (when (boundp 'gnus-draft-mode)
6596         (setq draft gnus-draft-mode)))
6597     (with-temp-buffer
6598       (use-local-map keymap)
6599       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6600       (set (make-local-variable 'gnus-draft-mode) draft)
6601       (describe-bindings prefix))
6602     (let ((item `((lambda (prefix)
6603                     (with-current-buffer ,(current-buffer)
6604                       (gnus-article-describe-bindings prefix)))
6605                   ,prefix)))
6606       (with-current-buffer (let (help-xref-following) (help-buffer))
6607         (setq help-xref-stack-item item)))))
6608
6609 (defun gnus-article-reply-with-original (&optional wide)
6610   "Start composing a reply mail to the current message.
6611 The text in the region will be yanked.  If the region isn't active,
6612 the entire article will be yanked."
6613   (interactive)
6614   (let ((article (cdr gnus-article-current))
6615         contents)
6616     (if (not (gnus-region-active-p))
6617         (with-current-buffer gnus-summary-buffer
6618           (gnus-summary-reply (list (list article)) wide))
6619       (setq contents (buffer-substring (point) (mark t)))
6620       ;; Deactivate active regions.
6621       (when (and (boundp 'transient-mark-mode)
6622                  transient-mark-mode)
6623         (setq mark-active nil))
6624       (with-current-buffer gnus-summary-buffer
6625         (gnus-summary-reply
6626          (list (list article contents)) wide)))))
6627
6628 (defun gnus-article-wide-reply-with-original ()
6629   "Start composing a wide reply mail to the current message.
6630 The text in the region will be yanked.  If the region isn't active,
6631 the entire article will be yanked."
6632   (interactive)
6633   (gnus-article-reply-with-original t))
6634
6635 (defun gnus-article-followup-with-original ()
6636   "Compose a followup to the current article.
6637 The text in the region will be yanked.  If the region isn't active,
6638 the entire article will be yanked."
6639   (interactive)
6640   (let ((article (cdr gnus-article-current))
6641         contents)
6642       (if (not (gnus-region-active-p))
6643           (with-current-buffer gnus-summary-buffer
6644             (gnus-summary-followup (list (list article))))
6645         (setq contents (buffer-substring (point) (mark t)))
6646         ;; Deactivate active regions.
6647         (when (and (boundp 'transient-mark-mode)
6648                    transient-mark-mode)
6649           (setq mark-active nil))
6650         (with-current-buffer gnus-summary-buffer
6651           (gnus-summary-followup
6652            (list (list article contents)))))))
6653
6654 (defun gnus-article-hide (&optional arg force)
6655   "Hide all the gruft in the current article.
6656 This means that signatures, cited text and (some) headers will be
6657 hidden.
6658 If given a prefix, show the hidden text instead."
6659   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6660   (gnus-article-hide-headers arg)
6661   (gnus-article-hide-list-identifiers arg)
6662   (gnus-article-hide-citation-maybe arg force)
6663   (gnus-article-hide-signature arg))
6664
6665 (defun gnus-article-maybe-highlight ()
6666   "Do some article highlighting if article highlighting is requested."
6667   (when (gnus-visual-p 'article-highlight 'highlight)
6668     (gnus-article-highlight-some)))
6669
6670 (defun gnus-check-group-server ()
6671   ;; Make sure the connection to the server is alive.
6672   (unless (gnus-server-opened
6673            (gnus-find-method-for-group gnus-newsgroup-name))
6674     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6675     (gnus-request-group gnus-newsgroup-name t)))
6676
6677 (eval-when-compile
6678   (autoload 'nneething-get-file-name "nneething"))
6679
6680 (defun gnus-request-article-this-buffer (article group)
6681   "Get an article and insert it into this buffer."
6682   (let (do-update-line sparse-header)
6683     (prog1
6684         (save-excursion
6685           (erase-buffer)
6686           (gnus-kill-all-overlays)
6687           (setq group (or group gnus-newsgroup-name))
6688
6689           ;; Using `gnus-request-article' directly will insert the article into
6690           ;; `nntp-server-buffer' - so we'll save some time by not having to
6691           ;; copy it from the server buffer into the article buffer.
6692
6693           ;; We only request an article by message-id when we do not have the
6694           ;; headers for it, so we'll have to get those.
6695           (when (stringp article)
6696             (gnus-read-header article))
6697
6698           ;; If the article number is negative, that means that this article
6699           ;; doesn't belong in this newsgroup (possibly), so we find its
6700           ;; message-id and request it by id instead of number.
6701           (when (and (numberp article)
6702                      gnus-summary-buffer
6703                      (get-buffer gnus-summary-buffer)
6704                      (gnus-buffer-exists-p gnus-summary-buffer))
6705             (with-current-buffer gnus-summary-buffer
6706               (let ((header (gnus-summary-article-header article)))
6707                 (when (< article 0)
6708                   (cond
6709                    ((memq article gnus-newsgroup-sparse)
6710                     ;; This is a sparse gap article.
6711                     (setq do-update-line article)
6712                     (setq article (mail-header-id header))
6713                     (setq sparse-header (gnus-read-header article))
6714                     (setq gnus-newsgroup-sparse
6715                           (delq article gnus-newsgroup-sparse)))
6716                    ((vectorp header)
6717                     ;; It's a real article.
6718                     (setq article (mail-header-id header)))
6719                    (t
6720                     ;; It is an extracted pseudo-article.
6721                     (setq article 'pseudo)
6722                     (gnus-request-pseudo-article header))))
6723
6724                 (let ((method (gnus-find-method-for-group
6725                                gnus-newsgroup-name)))
6726                   (when (and (eq (car method) 'nneething)
6727                              (vectorp header))
6728                     (let ((dir (nneething-get-file-name
6729                                 (mail-header-id header))))
6730                       (when (and (stringp dir)
6731                                  (file-directory-p dir))
6732                         (setq article 'nneething)
6733                         (gnus-group-enter-directory dir))))))))
6734
6735           (cond
6736            ;; Refuse to select canceled articles.
6737            ((and (numberp article)
6738                  gnus-summary-buffer
6739                  (get-buffer gnus-summary-buffer)
6740                  (gnus-buffer-exists-p gnus-summary-buffer)
6741                  (eq (cdr (with-current-buffer gnus-summary-buffer
6742                             (assq article gnus-newsgroup-reads)))
6743                      gnus-canceled-mark))
6744             nil)
6745            ;; We first check `gnus-original-article-buffer'.
6746            ((and (get-buffer gnus-original-article-buffer)
6747                  (numberp article)
6748                  (with-current-buffer gnus-original-article-buffer
6749                    (and (equal (car gnus-original-article) group)
6750                         (eq (cdr gnus-original-article) article))))
6751             ;; `insert-buffer-substring' would incorrectly use the
6752             ;; equivalent of string-make-multibyte which amount to decoding
6753             ;; with locale-coding-system, causing failure of
6754             ;; subsequent decoding.
6755             (insert (mm-string-to-multibyte
6756                      (with-current-buffer gnus-original-article-buffer
6757                        (buffer-substring (point-min) (point-max)))))
6758             'article)
6759            ;; Check the backlog.
6760            ((and gnus-keep-backlog
6761                  (gnus-backlog-request-article group article (current-buffer)))
6762             'article)
6763            ;; Check asynchronous pre-fetch.
6764            ((gnus-async-request-fetched-article group article (current-buffer))
6765             (gnus-async-prefetch-next group article gnus-summary-buffer)
6766             (when (and (numberp article) gnus-keep-backlog)
6767               (gnus-backlog-enter-article group article (current-buffer)))
6768             'article)
6769            ;; Check the cache.
6770            ((and gnus-use-cache
6771                  (numberp article)
6772                  (gnus-cache-request-article article group))
6773             'article)
6774            ;; Check the agent cache.
6775            ((gnus-agent-request-article article group)
6776             'article)
6777            ;; Get the article and put into the article buffer.
6778            ((or (stringp article)
6779                 (numberp article))
6780             (let ((gnus-override-method gnus-override-method)
6781                   (methods (and (stringp article)
6782                                 gnus-refer-article-method))
6783                   (backend (car (gnus-find-method-for-group
6784                                  gnus-newsgroup-name)))
6785                   result
6786                   (inhibit-read-only t))
6787               (if (or (not (listp methods))
6788                       (and (symbolp (car methods))
6789                            (assq (car methods) nnoo-definition-alist)))
6790                   (setq methods (list methods)))
6791               (when (and (null gnus-override-method)
6792                          methods)
6793                 (setq gnus-override-method (pop methods)))
6794               (while (not result)
6795                 (when (eq gnus-override-method 'current)
6796                   (setq gnus-override-method
6797                         (with-current-buffer gnus-summary-buffer
6798                           gnus-current-select-method)))
6799                 (erase-buffer)
6800                 (gnus-kill-all-overlays)
6801                 (let ((gnus-newsgroup-name group))
6802                   (gnus-check-group-server))
6803                 (cond
6804                  ((gnus-request-article article group (current-buffer))
6805                   (when (numberp article)
6806                     (gnus-async-prefetch-next group article
6807                                               gnus-summary-buffer)
6808                     (when gnus-keep-backlog
6809                       (gnus-backlog-enter-article
6810                        group article (current-buffer))))
6811                   (setq result 'article))
6812                  (methods
6813                   (setq gnus-override-method (pop methods)))
6814                  ((not (string-match "^400 "
6815                                      (nnheader-get-report backend)))
6816                   ;; If we get 400 server disconnect, reconnect and
6817                   ;; retry; otherwise, assume the article has expired.
6818                   (setq result 'done))))
6819               (and (eq result 'article) 'article)))
6820            ;; It was a pseudo.
6821            (t article)))
6822
6823       ;; Associate this article with the current summary buffer.
6824       (setq gnus-article-current-summary gnus-summary-buffer)
6825
6826       ;; Take the article from the original article buffer
6827       ;; and place it in the buffer it's supposed to be in.
6828       (when (and (get-buffer gnus-article-buffer)
6829                  (equal (buffer-name (current-buffer))
6830                         (buffer-name (get-buffer gnus-article-buffer))))
6831         (save-excursion
6832           (if (get-buffer gnus-original-article-buffer)
6833               (set-buffer gnus-original-article-buffer)
6834             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6835             (buffer-disable-undo)
6836             (setq major-mode 'gnus-original-article-mode)
6837             (setq buffer-read-only t))
6838           (let ((inhibit-read-only t))
6839             (erase-buffer)
6840             (insert-buffer-substring gnus-article-buffer))
6841           (setq gnus-original-article (cons group article)))
6842
6843         ;; Decode charsets.
6844         (run-hooks 'gnus-article-decode-hook)
6845         ;; Mark article as decoded or not.
6846         (setq gnus-article-decoded-p gnus-article-decode-hook))
6847
6848       ;; Update sparse articles.
6849       (when (and do-update-line
6850                  (or (numberp article)
6851                      (stringp article)))
6852         (let ((buf (current-buffer)))
6853           (set-buffer gnus-summary-buffer)
6854           (gnus-summary-update-article do-update-line sparse-header)
6855           (gnus-summary-goto-subject do-update-line nil t)
6856           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6857                             (point))
6858           (set-buffer buf))))))
6859
6860 (defun gnus-block-private-groups (group)
6861   (if (gnus-news-group-p group)
6862       ;; Block nothing in news groups.
6863       nil
6864     ;; Block everything anywhere else.
6865     "."))
6866
6867 (defun gnus-blocked-images ()
6868   (if (functionp gnus-blocked-images)
6869       (funcall gnus-blocked-images gnus-newsgroup-name)
6870     gnus-blocked-images))
6871
6872 ;;;
6873 ;;; Article editing
6874 ;;;
6875
6876 (defcustom gnus-article-edit-mode-hook nil
6877   "Hook run in article edit mode buffers."
6878   :group 'gnus-article-various
6879   :type 'hook)
6880
6881 (defvar gnus-article-edit-done-function nil)
6882
6883 (defvar gnus-article-edit-mode-map nil)
6884
6885 ;; Should we be using derived.el for this?
6886 (unless gnus-article-edit-mode-map
6887   (setq gnus-article-edit-mode-map (make-keymap))
6888   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6889
6890   (gnus-define-keys gnus-article-edit-mode-map
6891     "\C-c?"    describe-mode
6892     "\C-c\C-c" gnus-article-edit-done
6893     "\C-c\C-k" gnus-article-edit-exit
6894     "\C-c\C-f\C-t" message-goto-to
6895     "\C-c\C-f\C-o" message-goto-from
6896     "\C-c\C-f\C-b" message-goto-bcc
6897     ;;"\C-c\C-f\C-w" message-goto-fcc
6898     "\C-c\C-f\C-c" message-goto-cc
6899     "\C-c\C-f\C-s" message-goto-subject
6900     "\C-c\C-f\C-r" message-goto-reply-to
6901     "\C-c\C-f\C-n" message-goto-newsgroups
6902     "\C-c\C-f\C-d" message-goto-distribution
6903     "\C-c\C-f\C-f" message-goto-followup-to
6904     "\C-c\C-f\C-m" message-goto-mail-followup-to
6905     "\C-c\C-f\C-k" message-goto-keywords
6906     "\C-c\C-f\C-u" message-goto-summary
6907     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6908     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6909     "\C-c\C-b" message-goto-body
6910     "\C-c\C-i" message-goto-signature
6911
6912     "\C-c\C-t" message-insert-to
6913     "\C-c\C-n" message-insert-newsgroups
6914     "\C-c\C-o" message-sort-headers
6915     "\C-c\C-e" message-elide-region
6916     "\C-c\C-v" message-delete-not-region
6917     "\C-c\C-z" message-kill-to-signature
6918     "\M-\r" message-newline-and-reformat
6919     "\C-c\C-a" mml-attach-file
6920     "\C-a" message-beginning-of-line
6921     "\t" message-tab
6922     "\M-;" comment-region)
6923
6924   (gnus-define-keys (gnus-article-edit-wash-map
6925                      "\C-c\C-w" gnus-article-edit-mode-map)
6926     "f" gnus-article-edit-full-stops))
6927
6928 (easy-menu-define
6929   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6930   '("Field"
6931     ["Fetch To" message-insert-to t]
6932     ["Fetch Newsgroups" message-insert-newsgroups t]
6933     "----"
6934     ["To" message-goto-to t]
6935     ["From" message-goto-from t]
6936     ["Subject" message-goto-subject t]
6937     ["Cc" message-goto-cc t]
6938     ["Reply-To" message-goto-reply-to t]
6939     ["Summary" message-goto-summary t]
6940     ["Keywords" message-goto-keywords t]
6941     ["Newsgroups" message-goto-newsgroups t]
6942     ["Followup-To" message-goto-followup-to t]
6943     ["Mail-Followup-To" message-goto-mail-followup-to t]
6944     ["Distribution" message-goto-distribution t]
6945     ["Body" message-goto-body t]
6946     ["Signature" message-goto-signature t]))
6947
6948 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6949   "Major mode for editing articles.
6950 This is an extended text-mode.
6951
6952 \\{gnus-article-edit-mode-map}"
6953   (make-local-variable 'gnus-article-edit-done-function)
6954   (make-local-variable 'gnus-prev-winconf)
6955   (set (make-local-variable 'font-lock-defaults)
6956        '(message-font-lock-keywords t))
6957   (set (make-local-variable 'mail-header-separator) "")
6958   (set (make-local-variable 'gnus-article-edit-mode) t)
6959   (easy-menu-add message-mode-field-menu message-mode-map)
6960   (mml-mode)
6961   (setq buffer-read-only nil)
6962   (buffer-enable-undo)
6963   (widen))
6964
6965 (defun gnus-article-edit (&optional force)
6966   "Edit the current article.
6967 This will have permanent effect only in mail groups.
6968 If FORCE is non-nil, allow editing of articles even in read-only
6969 groups."
6970   (interactive "P")
6971   (when (and (not force)
6972              (gnus-group-read-only-p))
6973     (error "The current newsgroup does not support article editing"))
6974   (gnus-article-date-original)
6975   (gnus-article-edit-article
6976    'ignore
6977    `(lambda (no-highlight)
6978       'ignore
6979       (gnus-summary-edit-article-done
6980        ,(or (mail-header-references gnus-current-headers) "")
6981        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6982
6983 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6984   "Start editing the contents of the current article buffer."
6985   (let ((winconf (current-window-configuration)))
6986     (set-buffer gnus-article-buffer)
6987     (let ((message-auto-save-directory
6988            ;; Don't associate the article buffer with a draft file.
6989            nil))
6990       (gnus-article-edit-mode))
6991     (funcall start-func)
6992     (set-buffer-modified-p nil)
6993     (gnus-configure-windows 'edit-article)
6994     (setq gnus-article-edit-done-function exit-func)
6995     (setq gnus-prev-winconf winconf)
6996     (unless quiet
6997       (gnus-message 6 "C-c C-c to end edits"))))
6998
6999 (defun gnus-article-edit-done (&optional arg)
7000   "Update the article edits and exit."
7001   (interactive "P")
7002   (let ((func gnus-article-edit-done-function)
7003         (buf (current-buffer))
7004         (start (window-start))
7005         (p (point))
7006         (winconf gnus-prev-winconf))
7007     (widen) ;; Widen it in case that users narrowed the buffer.
7008     (funcall func arg)
7009     (set-buffer buf)
7010     ;; The cache and backlog have to be flushed somewhat.
7011     (when gnus-keep-backlog
7012       (gnus-backlog-remove-article
7013        (car gnus-article-current) (cdr gnus-article-current)))
7014     ;; Flush original article as well.
7015     (gnus-flush-original-article-buffer)
7016     (when gnus-use-cache
7017       (gnus-cache-update-article
7018        (car gnus-article-current) (cdr gnus-article-current)))
7019     ;; We remove all text props from the article buffer.
7020     (kill-all-local-variables)
7021     (set-text-properties (point-min) (point-max) nil)
7022     (gnus-article-mode)
7023     (set-window-configuration winconf)
7024     (set-buffer buf)
7025     (set-window-start (get-buffer-window buf) start)
7026     (set-window-point (get-buffer-window buf) (point)))
7027   (gnus-summary-show-article))
7028
7029 (defun gnus-flush-original-article-buffer ()
7030   (when (get-buffer gnus-original-article-buffer)
7031     (with-current-buffer gnus-original-article-buffer
7032       (setq gnus-original-article nil))))
7033
7034 (defun gnus-article-edit-exit ()
7035   "Exit the article editing without updating."
7036   (interactive)
7037   (when (or (not (buffer-modified-p))
7038             (yes-or-no-p "Article modified; kill anyway? "))
7039     (let ((curbuf (current-buffer))
7040           (p (point))
7041           (window-start (window-start)))
7042       (erase-buffer)
7043       (if (gnus-buffer-live-p gnus-original-article-buffer)
7044           (insert-buffer-substring gnus-original-article-buffer))
7045       (let ((winconf gnus-prev-winconf))
7046         (kill-all-local-variables)
7047         (gnus-article-mode)
7048         (set-window-configuration winconf)
7049         ;; Tippy-toe some to make sure that point remains where it was.
7050         (save-current-buffer
7051           (set-buffer curbuf)
7052           (set-window-start (get-buffer-window (current-buffer)) window-start)
7053           (goto-char p))))
7054     (gnus-summary-show-article)))
7055
7056 (defun gnus-article-edit-full-stops ()
7057   "Interactively repair spacing at end of sentences."
7058   (interactive)
7059   (save-excursion
7060     (goto-char (point-min))
7061     (search-forward-regexp "^$" nil t)
7062     (let ((case-fold-search nil))
7063       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7064
7065 ;;;
7066 ;;; Article highlights
7067 ;;;
7068
7069 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7070
7071 ;;; Internal Variables:
7072
7073 (defcustom gnus-button-url-regexp
7074   (concat
7075    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7076    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7077    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7078    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7079        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7080              (punct "!?:;.,"))
7081          (concat
7082           "\\(?:"
7083           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7084           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7085           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7086           "\\|"
7087           "[" chars punct     "]+" "[" chars "]"
7088           "\\)"))
7089      (concat ;; XEmacs 21.4 doesn't support POSIX.
7090       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7091       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7092    "\\)")
7093   "Regular expression that matches URLs."
7094   :group 'gnus-article-buttons
7095   :type 'regexp)
7096
7097 (defcustom gnus-button-valid-fqdn-regexp
7098   message-valid-fqdn-regexp
7099   "Regular expression that matches a valid FQDN."
7100   :version "22.1"
7101   :group 'gnus-article-buttons
7102   :type 'regexp)
7103
7104 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7105 (defcustom gnus-button-valid-localpart-regexp
7106   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7107   "Regular expression that matches a localpart of mail addresses or MIDs."
7108   :version "22.1"
7109   :group 'gnus-article-buttons
7110   :type 'regexp)
7111
7112 (defcustom gnus-button-man-handler 'manual-entry
7113   "Function to use for displaying man pages.
7114 The function must take at least one argument with a string naming the
7115 man page."
7116   :version "22.1"
7117   :type '(choice (function-item :tag "Man" manual-entry)
7118                  (function-item :tag "Woman" woman)
7119                  (function :tag "Other"))
7120   :group 'gnus-article-buttons)
7121
7122 (defcustom gnus-button-mid-or-mail-regexp
7123   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7124           gnus-button-valid-fqdn-regexp
7125           ">?\\)\\b")
7126   "Regular expression that matches a message ID or a mail address."
7127   :version "22.1"
7128   :group 'gnus-article-buttons
7129   :type 'regexp)
7130
7131 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7132   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7133 Strings like this can be either a message ID or a mail address.  If it is one
7134 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7135 message ID or a mail address, respectively.  If this variable is set to the
7136 symbol `ask', always query the user what do do.  If it is a function, this
7137 function will be called with the string as its only argument.  The function
7138 must return `mid', `mail', `invalid' or `ask'."
7139   :version "22.1"
7140   :group 'gnus-article-buttons
7141   :type '(choice (function-item :tag "Heuristic function"
7142                                 gnus-button-mid-or-mail-heuristic)
7143                  (const ask)
7144                  (const mid)
7145                  (const mail)))
7146
7147 (defcustom gnus-button-mid-or-mail-heuristic-alist
7148   '((-10.0 . ".+\\$.+@")
7149     (-10.0 . "#")
7150     (-10.0 . "\\*")
7151     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7152     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7153     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7154     (-1.0  . "^[^a-z]+@")
7155     ;;
7156     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7157     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7158     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7159     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7160     ;;
7161     (-2.0  . "^[0-9]")
7162     (-1.0  . "^[0-9][0-9]")
7163     ;;
7164     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7165     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7166     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7167     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7168     ;;
7169     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7170     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7171     ;;       "[0-9]{8,}.*\@"
7172     (-3.0
7173      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7174     ;; "[0-9]{12,}.*\@"
7175     ;; compensation for TDMA dated mail addresses:
7176     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7177     ;;
7178     (-20.0 . "\\.fsf@") ;; Gnus
7179     (-20.0 . "^slrn")
7180     (-20.0 . "^Pine")
7181     (-20.0 . "^alpine\\.")
7182     (-20.0 . "_-_") ;; Subject change in thread
7183     ;;
7184     (-20.0 . "\\.ln@") ;; leafnode
7185     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7186     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7187     ;;
7188     ;; (5.0 . "") ;; $local_part_len <= 7
7189     (10.0  . "^[^0-9]+@")
7190     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7191     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7192     (3.0   . "\@stud")
7193     ;;
7194     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7195     ;;
7196     (0.5   . "^[A-Z][a-z]")
7197     (0.5   . "^[A-Z][a-z][a-z]")
7198     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7199     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7200   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7201
7202 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7203 address.  The REGEXP is processed with `case-fold-search' set to nil."
7204   :version "22.1"
7205   :group 'gnus-article-buttons
7206   :type '(repeat (cons (number :tag "Rate")
7207                        (regexp :tag "Regexp"))))
7208
7209 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7210   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7211 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7212 address, `ask' if unsure and `invalid' if the string is invalid."
7213   (let ((case-fold-search nil)
7214         (list gnus-button-mid-or-mail-heuristic-alist)
7215         (result 0) rate regexp lpartlen elem)
7216     (setq lpartlen
7217           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7218     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7219     ;; Certain special cases...
7220     (when (string-match
7221            (concat
7222             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7223             "^[0-9]+\\.[0-9]+@compuserve\\|"
7224             "@public\\.gmane\\.org")
7225            mid-or-mail)
7226       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7227       (setq result 'mail))
7228     (when (string-match "@.*@\\| " mid-or-mail)
7229       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7230       (setq result 'invalid))
7231     ;; Nothing more to do, if result is not a number here...
7232     (when (numberp result)
7233       (while list
7234         (setq elem (car list)
7235               rate (car elem)
7236               regexp (cdr elem)
7237               list (cdr list))
7238         (when (string-match regexp mid-or-mail)
7239           (setq result (+ result rate))
7240           (gnus-message
7241            9 "`%s' matched `%s', rate `%s', result `%s'."
7242            mid-or-mail regexp rate result)))
7243       (when (<= lpartlen 7)
7244         (setq result (+ result 5.0))
7245         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7246                       mid-or-mail result))
7247       (when (>= lpartlen 12)
7248         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7249         (cond
7250          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7251           ;; Long local part should contain realname if e-mail address,
7252           ;; too many digits: message-id.
7253           ;; $score -= 5.0 + 0.1 * $local_part_len;
7254           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7255           (setq result (+ result rate))
7256           (gnus-message
7257            9 "Many digits in `%s', rate `%s', result `%s'."
7258            mid-or-mail rate result))
7259          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7260                         mid-or-mail)
7261           ;; Too few vowels [^aeiouy]{4,}.*\@
7262           (setq result (+ result -5.0))
7263           (gnus-message
7264            9 "Few vowels in `%s', rate `%s', result `%s'."
7265            mid-or-mail -5.0 result))
7266          (t
7267           (setq result (+ result 5.0))
7268           (gnus-message
7269            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7270     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7271     ;; Maybe we should make this a customizable alist: (condition . 'result)
7272     (cond
7273      ((symbolp result) result)
7274      ;; Now convert number into proper results:
7275      ((< result -10.0) 'mid)
7276      ((> result  10.0) 'mail)
7277      (t 'ask))))
7278
7279 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7280   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7281          (url-mid (concat "news" ":" mid-or-mail))
7282          (url-mailto (concat "mailto" ":" mid-or-mail)))
7283     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7284     (when (fboundp pref)
7285       (setq guessed
7286             ;; get rid of surrounding angles...
7287             (funcall pref
7288                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7289       (if (or (eq 'mid guessed) (eq 'mail guessed))
7290           (setq pref guessed)
7291         (setq pref 'ask)))
7292     (if (eq pref 'ask)
7293         (save-window-excursion
7294           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7295               (setq pref 'mail)
7296             (setq pref 'mid))))
7297     (cond ((eq pref 'mid)
7298            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7299            (gnus-button-handle-news url-mid))
7300           ((eq pref 'mail)
7301            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7302            (gnus-url-mailto url-mailto))
7303           (t (gnus-message 3 "Invalid string.")))))
7304
7305 (defun gnus-button-handle-custom (fun arg)
7306   "Call function FUN on argument ARG.
7307 Both FUN and ARG are supposed to be strings.  ARG will be passed
7308 as a symbol to FUN."
7309   (funcall (intern fun)
7310            (if (string-match "^customize-apropos" fun)
7311                arg
7312              (intern arg))))
7313
7314 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7315
7316 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7317 ;; stuff?
7318
7319 (defun gnus-button-handle-describe-function (url)
7320   "Call `describe-function' when pushing the corresponding URL button."
7321   (describe-function
7322    (intern
7323     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7324
7325 (defun gnus-button-handle-describe-variable (url)
7326   "Call `describe-variable' when pushing the corresponding URL button."
7327   (describe-variable
7328    (intern
7329     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7330
7331 (defun gnus-button-handle-symbol (url)
7332 "Display help on variable or function.
7333 Calls `describe-variable' or `describe-function'."
7334   (let ((sym (intern url)))
7335     (cond
7336      ((fboundp sym) (describe-function sym))
7337      ((boundp sym) (describe-variable sym))
7338      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7339
7340 (defun gnus-button-handle-describe-key (url)
7341   "Call `describe-key' when pushing the corresponding URL button."
7342   (let* ((key-string
7343           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7344          (keys (ignore-errors (eval `(kbd ,key-string)))))
7345     (if keys
7346         (describe-key keys)
7347       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7348
7349 (defun gnus-button-handle-apropos (url)
7350   "Call `apropos' when pushing the corresponding URL button."
7351   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7352
7353 (defun gnus-button-handle-apropos-command (url)
7354   "Call `apropos' when pushing the corresponding URL button."
7355   (apropos-command
7356    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7357
7358 (defun gnus-button-handle-apropos-variable (url)
7359   "Call `apropos' when pushing the corresponding URL button."
7360   (funcall
7361    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7362    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7363
7364 (defun gnus-button-handle-apropos-documentation (url)
7365   "Call `apropos' when pushing the corresponding URL button."
7366   (funcall
7367    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7368    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7369
7370 (defun gnus-button-handle-library (url)
7371   "Call `locate-library' when pushing the corresponding URL button."
7372   (gnus-message 9 "url=`%s'" url)
7373   (let* ((lib (locate-library url))
7374          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7375     (if (not lib)
7376         (gnus-message 1 "Cannot locale library `%s'." url)
7377       (find-file-read-only file))))
7378
7379 (defcustom gnus-button-man-level 5
7380   "*Integer that says how many man-related buttons Gnus will show.
7381 The higher the number, the more buttons will appear and the more false
7382 positives are possible.  Note that you can set this variable local to
7383 specific groups.  Setting it higher in Unix groups is probably a good idea.
7384 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7385 how to set variables in specific groups."
7386   :version "22.1"
7387   :group 'gnus-article-buttons
7388   :link '(custom-manual "(gnus)Group Parameters")
7389   :type 'integer)
7390
7391 (defcustom gnus-button-emacs-level 5
7392   "*Integer that says how many emacs-related buttons Gnus will show.
7393 The higher the number, the more buttons will appear and the more false
7394 positives are possible.  Note that you can set this variable local to
7395 specific groups.  Setting it higher in Emacs or Gnus related groups is
7396 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7397 `gnus-parameters' on how to set variables in specific groups."
7398   :version "22.1"
7399   :group 'gnus-article-buttons
7400   :link '(custom-manual "(gnus)Group Parameters")
7401   :type 'integer)
7402
7403 (defcustom gnus-button-message-level 5
7404   "*Integer that says how many buttons for news or mail messages will appear.
7405 The higher the number, the more buttons will appear and the more false
7406 positives are possible."
7407   ;; mail addresses, MIDs, URLs for news, ...
7408   :version "22.1"
7409   :group 'gnus-article-buttons
7410   :type 'integer)
7411
7412 (defcustom gnus-button-browse-level 5
7413   "*Integer that says how many buttons for browsing will appear.
7414 The higher the number, the more buttons will appear and the more false
7415 positives are possible."
7416   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7417   :version "22.1"
7418   :group 'gnus-article-buttons
7419   :type 'integer)
7420
7421 (defcustom gnus-button-alist
7422   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7423      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7424     ((concat "\\b\\(nntp\\|news\\):\\("
7425              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7426      0 t gnus-button-handle-news 2)
7427     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7428      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7429     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7430      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7431     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7432     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7433      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7434     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7435      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7436     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7437      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7438     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7439      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7440     ;; RFC 2368 (The mailto URL scheme)
7441     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7442      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7443     ("\\bmailto:\\([^ \n\t]+\\)"
7444      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7445     ;; Info Konqueror style <info:/foo/bar baz>.
7446     ;; Must come before " Gnus home-grown style".
7447     ("\\binfo://?\\([^'\">\n\t]+\\)"
7448      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7449    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7450     ("\\binfo://\\([^'\">\n\t ]+\\)"
7451      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7452     ;; Info GNOME style <info:foo#bar_baz>
7453     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7454      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7455     ;; Info KDE style <info:(foo)bar baz>
7456     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7457      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7458     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7459      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7460     ("\\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,]*\\)\\)?"
7461      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7462      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7463     ;; This is custom
7464     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7465      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7466     ;; Emacs help commands
7467     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7468      ;; regexp doesn't match arguments containing ` '.
7469      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7470     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7471      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7472     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7473      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7474     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7475      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7476     ;; The following entries may lead to many false positives so don't enable
7477     ;; them by default (use a high button level).
7478     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7479      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7480      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7481     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7482      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7483     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7484      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7485     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7486      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7487     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7488      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7489     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7490      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7491     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7492      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7493     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7494      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7495     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7496      ;; Unlike the other regexps we really have to require quoting
7497      ;; here to determine where it ends.
7498      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7499     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7500     ("<URL: *\\([^\n<>]*\\)>"
7501      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7502     ;; RFC 2396 (2.4.3., delims) ...
7503     ("\"URL: *\\([^\n\"]*\\)\""
7504      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7505     ;; Raw URLs.
7506     (gnus-button-url-regexp
7507      0 (>= gnus-button-browse-level 0) browse-url 0)
7508     ;; man pages
7509     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7510      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7511      gnus-button-handle-man 1)
7512     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7513     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7514      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7515      gnus-button-handle-man 1)
7516     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7517     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7518     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7519      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7520     ;; Recognizing patches to .el files.  This is somewhat obscure,
7521     ;; but considering the percentage of Gnus users who hack Emacs
7522     ;; Lisp files...
7523     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7524      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7525     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7526      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7527     ;; MID or mail: To avoid too many false positives we don't try to catch
7528     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7529     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7530     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7531     ;; so that non-ambiguous entries (see above) match first.
7532     (gnus-button-mid-or-mail-regexp
7533      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7534   "*Alist of regexps matching buttons in article bodies.
7535
7536 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7537 REGEXP: is the string (case insensitive) matching text around the button (can
7538 also be Lisp expression evaluating to a string),
7539 BUTTON: is the number of the regexp grouping actually matching the button,
7540 FORM: is a Lisp expression which must eval to true for the button to
7541 be added,
7542 CALLBACK: is the function to call when the user push this button, and each
7543 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7544
7545 CALLBACK can also be a variable, in that case the value of that
7546 variable it the real callback function."
7547   :group 'gnus-article-buttons
7548   :type '(repeat (list (choice regexp variable sexp)
7549                        (integer :tag "Button")
7550                        (sexp :tag "Form")
7551                        (function :tag "Callback")
7552                        (repeat :tag "Par"
7553                                :inline t
7554                                (integer :tag "Regexp group")))))
7555 (put 'gnus-button-alist 'risky-local-variable t)
7556
7557 (defcustom gnus-header-button-alist
7558   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7559      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7560     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7561      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7562     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7563      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7564     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7565      0 (>= gnus-button-browse-level 0) browse-url 0)
7566     ("^Subject:" gnus-button-url-regexp
7567      0 (>= gnus-button-browse-level 0) browse-url 0)
7568     ("^[^:]+:" gnus-button-url-regexp
7569      0 (>= gnus-button-browse-level 0) browse-url 0)
7570     ("^OpenPGP:.*url=" gnus-button-url-regexp
7571      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7572     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7573      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7574     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7575      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7576   "*Alist of headers and regexps to match buttons in article heads.
7577
7578 This alist is very similar to `gnus-button-alist', except that each
7579 alist has an additional HEADER element first in each entry:
7580
7581 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7582
7583 HEADER is a regexp to match a header.  For a fuller explanation, see
7584 `gnus-button-alist'."
7585   :group 'gnus-article-buttons
7586   :group 'gnus-article-headers
7587   :type '(repeat (list (regexp :tag "Header")
7588                        (choice regexp variable)
7589                        (integer :tag "Button")
7590                        (sexp :tag "Form")
7591                        (function :tag "Callback")
7592                        (repeat :tag "Par"
7593                                :inline t
7594                                (integer :tag "Regexp group")))))
7595 (put 'gnus-header-button-alist 'risky-local-variable t)
7596
7597 ;;; Commands:
7598
7599 (defun gnus-article-push-button (event)
7600   "Check text under the mouse pointer for a callback function.
7601 If the text under the mouse pointer has a `gnus-callback' property,
7602 call it with the value of the `gnus-data' text property."
7603   (interactive "e")
7604   (set-buffer (window-buffer (posn-window (event-start event))))
7605   (let* ((pos (posn-point (event-start event)))
7606          (data (get-text-property pos 'gnus-data))
7607          (fun (get-text-property pos 'gnus-callback)))
7608     (goto-char pos)
7609     (when fun
7610       (funcall fun data))))
7611
7612 (defun gnus-article-press-button ()
7613   "Check text at point for a callback function.
7614 If the text at point has a `gnus-callback' property,
7615 call it with the value of the `gnus-data' text property."
7616   (interactive)
7617   (let ((data (get-text-property (point) 'gnus-data))
7618         (fun (get-text-property (point) 'gnus-callback)))
7619     (when fun
7620       (funcall fun data))))
7621
7622 (defun gnus-article-highlight (&optional force)
7623   "Highlight current article.
7624 This function calls `gnus-article-highlight-headers',
7625 `gnus-article-highlight-citation',
7626 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7627 do the highlighting.  See the documentation for those functions."
7628   (interactive (list 'force))
7629   (gnus-article-highlight-headers)
7630   (gnus-article-highlight-citation force)
7631   (gnus-article-highlight-signature)
7632   (gnus-article-add-buttons force)
7633   (gnus-article-add-buttons-to-head))
7634
7635 (defun gnus-article-highlight-some (&optional force)
7636   "Highlight current article.
7637 This function calls `gnus-article-highlight-headers',
7638 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7639 do the highlighting.  See the documentation for those functions."
7640   (interactive (list 'force))
7641   (gnus-article-highlight-headers)
7642   (gnus-article-highlight-signature)
7643   (gnus-article-add-buttons))
7644
7645 (defun gnus-article-highlight-headers ()
7646   "Highlight article headers as specified by `gnus-header-face-alist'."
7647   (interactive)
7648   (gnus-with-article-headers
7649     (let (regexp header-face field-face from hpoints fpoints)
7650       (dolist (entry gnus-header-face-alist)
7651         (goto-char (point-min))
7652         (setq regexp (concat "^\\("
7653                              (if (string-equal "" (nth 0 entry))
7654                                  "[^\t ]"
7655                                (nth 0 entry))
7656                              "\\)")
7657               header-face (nth 1 entry)
7658               field-face (nth 2 entry))
7659         (while (and (re-search-forward regexp nil t)
7660                     (not (eobp)))
7661           (beginning-of-line)
7662           (setq from (point))
7663           (unless (search-forward ":" nil t)
7664             (forward-char 1))
7665           (when (and header-face
7666                      (not (memq (point) hpoints)))
7667             (push (point) hpoints)
7668             (gnus-put-text-property from (point) 'face header-face))
7669           (when (and field-face
7670                      (not (memq (setq from (point)) fpoints)))
7671             (push from fpoints)
7672             (if (re-search-forward "^[^ \t]" nil t)
7673                 (forward-char -2)
7674               (goto-char (point-max)))
7675             (gnus-put-text-property from (point) 'face field-face)))))))
7676
7677 (defun gnus-article-highlight-signature ()
7678   "Highlight the signature in an article.
7679 It does this by highlighting everything after
7680 `gnus-signature-separator' using the face `gnus-signature'."
7681   (interactive)
7682   (gnus-with-article-buffer
7683     (let ((inhibit-point-motion-hooks t))
7684       (save-restriction
7685         (when (and gnus-signature-face
7686                    (gnus-article-narrow-to-signature))
7687           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7688                             'face gnus-signature-face)
7689           (widen)
7690           (gnus-article-search-signature)
7691           (let ((start (match-beginning 0))
7692                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7693             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7694                                      end)))))))
7695
7696 (defun gnus-button-in-region-p (b e prop)
7697   "Say whether PROP exists in the region."
7698   (text-property-not-all b e prop nil))
7699
7700 (defun gnus-article-add-buttons (&optional force)
7701   "Find external references in the article and make buttons of them.
7702 \"External references\" are things like Message-IDs and URLs, as
7703 specified by `gnus-button-alist'."
7704   (interactive (list 'force))
7705   (gnus-with-article-buffer
7706     (let ((inhibit-point-motion-hooks t)
7707           (case-fold-search t)
7708           (alist gnus-button-alist)
7709           beg entry regexp)
7710       ;; Remove all old markers.
7711       (let (marker entry new-list)
7712         (while (setq marker (pop gnus-button-marker-list))
7713           (if (or (< marker (point-min)) (>= marker (point-max)))
7714               (push marker new-list)
7715             (goto-char marker)
7716             (when (setq entry (gnus-button-entry))
7717               (put-text-property (match-beginning (nth 1 entry))
7718                                  (match-end (nth 1 entry))
7719                                  'gnus-callback nil))
7720             (set-marker marker nil)))
7721         (setq gnus-button-marker-list new-list))
7722       ;; We skip the headers.
7723       (article-goto-body)
7724       (setq beg (point))
7725       (while (setq entry (pop alist))
7726         (setq regexp (eval (car entry)))
7727         (goto-char beg)
7728         (while (re-search-forward regexp nil t)
7729           (let ((start (match-beginning (nth 1 entry)))
7730                 (end (match-end (nth 1 entry)))
7731                 (from (match-beginning 0)))
7732             (when (and (or (eq t (nth 2 entry))
7733                            (eval (nth 2 entry)))
7734                        (not (gnus-button-in-region-p
7735                              start end 'gnus-callback)))
7736               ;; That optional form returned non-nil, so we add the
7737               ;; button.
7738               (setq from (set-marker (make-marker) from))
7739               (push from gnus-button-marker-list)
7740               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7741                            (gnus-article-extend-url-button from start end))
7742                 (gnus-article-add-button start end
7743                                          'gnus-button-push from)
7744                 (gnus-put-text-property
7745                  start end
7746                  'gnus-string (buffer-substring-no-properties
7747                                start end))))))))))
7748
7749 (defun gnus-article-extend-url-button (beg start end)
7750   "Extend url button if url is folded into two or more lines.
7751 Return non-nil if button is extended.  BEG is a marker that points to
7752 the beginning position of a text containing url.  START and END are
7753 the endpoints of a url button before it is extended.  The concatenated
7754 url is put as the `gnus-button-url' overlay property on the button."
7755   (let ((opoint (point))
7756         (points (list start end))
7757         url delim regexp)
7758     (prog1
7759         (when (and (progn
7760                      (goto-char end)
7761                      (not (looking-at "[\t ]*[\">]")))
7762                    (progn
7763                      (goto-char start)
7764                      (string-match
7765                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7766                       (buffer-substring (point-at-bol) start)))
7767                    (progn
7768                      (setq url (list (buffer-substring start end))
7769                            delim (if (match-beginning 1) ">" "\""))
7770                      (beginning-of-line)
7771                      (setq regexp (concat
7772                                    (when (and (looking-at
7773                                                message-cite-prefix-regexp)
7774                                               (< (match-end 0) start))
7775                                      (regexp-quote (match-string 0)))
7776                                    "\
7777 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7778                                    delim "\\)"))
7779                      (while (progn
7780                               (forward-line 1)
7781                               (and (looking-at regexp)
7782                                    (prog1
7783                                        (match-beginning 1)
7784                                      (push (or (match-string 2)
7785                                                (match-string 1))
7786                                            url)
7787                                      (push (setq end (or (match-end 2)
7788                                                          (match-end 1)))
7789                                            points)
7790                                      (push (or (match-beginning 2)
7791                                                (match-beginning 1))
7792                                            points)))))
7793                      (match-beginning 2)))
7794           (let (gnus-article-mouse-face widget-mouse-face)
7795             (while points
7796               (gnus-article-add-button (pop points) (pop points)
7797                                        'gnus-button-push beg)))
7798           (let ((overlay (gnus-make-overlay start end)))
7799             (gnus-overlay-put overlay 'evaporate t)
7800             (gnus-overlay-put overlay 'gnus-button-url
7801                               (list (mapconcat 'identity (nreverse url) "")))
7802             (when gnus-article-mouse-face
7803               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7804           t)
7805       (goto-char opoint))))
7806
7807 ;; Add buttons to the head of an article.
7808 (defun gnus-article-add-buttons-to-head ()
7809   "Add buttons to the head of the article."
7810   (interactive)
7811   (gnus-with-article-headers
7812     (let (beg end)
7813       (dolist (entry gnus-header-button-alist)
7814         ;; Each alist entry.
7815         (goto-char (point-min))
7816         (while (re-search-forward (car entry) nil t)
7817           ;; Each header matching the entry.
7818           (setq beg (match-beginning 0))
7819           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7820                              (match-beginning 0))
7821                         (point-max)))
7822           (goto-char beg)
7823           (while (re-search-forward (eval (nth 1 entry)) end t)
7824             ;; Each match within a header.
7825             (let* ((entry (cdr entry))
7826                    (start (match-beginning (nth 1 entry)))
7827                    (end (match-end (nth 1 entry)))
7828                    (form (nth 2 entry)))
7829               (goto-char (match-end 0))
7830               (when (eval form)
7831                 (gnus-article-add-button
7832                  start end (nth 3 entry)
7833                  (buffer-substring (match-beginning (nth 4 entry))
7834                                    (match-end (nth 4 entry)))))))
7835           (goto-char end))))))
7836
7837 ;;; External functions:
7838
7839 (defun gnus-article-add-button (from to fun &optional data text)
7840   "Create a button between FROM and TO with callback FUN and data DATA."
7841   (when gnus-article-button-face
7842     (gnus-overlay-put (gnus-make-overlay from to nil t)
7843                       'face gnus-article-button-face))
7844   (gnus-add-text-properties
7845    from to
7846    (nconc (and gnus-article-mouse-face
7847                (list gnus-mouse-face-prop gnus-article-mouse-face))
7848           (list 'gnus-callback fun)
7849           (and data (list 'gnus-data data))))
7850   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7851                          :help-echo (or text "Follow the link")
7852                          :keymap gnus-url-button-map
7853                          :button-keymap gnus-widget-button-keymap))
7854
7855 (defun gnus-article-copy-string ()
7856   "Copy the string in the button to the kill ring."
7857   (interactive)
7858   (gnus-article-check-buffer)
7859   (let ((data (get-text-property (point) 'gnus-string)))
7860     (when data
7861       (with-temp-buffer
7862         (insert data)
7863         (copy-region-as-kill (point-min) (point-max))
7864         (message "Copied %s" data)))))
7865
7866 ;;; Internal functions:
7867
7868 (defun gnus-article-set-globals ()
7869   (with-current-buffer gnus-summary-buffer
7870     (gnus-set-global-variables)))
7871
7872 (defun gnus-signature-toggle (end)
7873   (gnus-with-article-buffer
7874     (let ((inhibit-point-motion-hooks t))
7875       (if (text-property-any end (point-max) 'article-type 'signature)
7876           (progn
7877             (gnus-delete-wash-type 'signature)
7878             (gnus-remove-text-properties-when
7879              'article-type 'signature end (point-max)
7880              (cons 'article-type (cons 'signature
7881                                        gnus-hidden-properties))))
7882         (gnus-add-wash-type 'signature)
7883         (gnus-add-text-properties-when
7884          'article-type nil end (point-max)
7885          (cons 'article-type (cons 'signature
7886                                    gnus-hidden-properties)))))
7887     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7888       (gnus-set-mode-line 'article))))
7889
7890 (defun gnus-button-entry ()
7891   ;; Return the first entry in `gnus-button-alist' matching this place.
7892   (let ((alist gnus-button-alist)
7893         (entry nil))
7894     (while alist
7895       (setq entry (pop alist))
7896       (if (looking-at (eval (car entry)))
7897           (setq alist nil)
7898         (setq entry nil)))
7899     entry))
7900
7901 (defun gnus-button-push (marker)
7902   ;; Push button starting at MARKER.
7903   (save-excursion
7904     (goto-char marker)
7905     (let* ((entry (gnus-button-entry))
7906            (inhibit-point-motion-hooks t)
7907            (fun (nth 3 entry))
7908            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7909                           (get-char-property marker 'gnus-button-url))
7910                      (mapcar (lambda (group)
7911                                (let ((string (match-string group)))
7912                                  (set-text-properties
7913                                   0 (length string) nil string)
7914                                  string))
7915                              (nthcdr 4 entry)))))
7916       (cond
7917        ((fboundp fun)
7918         (apply fun args))
7919        ((and (boundp fun)
7920              (fboundp (symbol-value fun)))
7921         (apply (symbol-value fun) args))
7922        (t
7923         (gnus-message 1 "You must define `%S' to use this button"
7924                       (cons fun args)))))))
7925
7926 (defun gnus-parse-news-url (url)
7927   (let (scheme server port group message-id articles)
7928     (with-temp-buffer
7929       (insert url)
7930       (goto-char (point-min))
7931       (when (looking-at "\\([A-Za-z]+\\):")
7932         (setq scheme (match-string 1))
7933         (goto-char (match-end 0)))
7934       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7935         (setq server (match-string 1))
7936         (setq port (if (stringp (match-string 3))
7937                        (string-to-number (match-string 3))
7938                      (match-string 3)))
7939         (goto-char (match-end 0)))
7940
7941       (cond
7942        ((looking-at "\\(.*@.*\\)")
7943         (setq message-id (match-string 1)))
7944        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7945         (setq group (match-string 1)
7946               articles (split-string (match-string 2) "-")))
7947        ((looking-at "\\([^/]+\\)/?")
7948         (setq group (match-string 1)))
7949        (t
7950         (error "Unknown news URL syntax"))))
7951     (list scheme server port group message-id articles)))
7952
7953 (defun gnus-button-handle-news (url)
7954   "Fetch a news URL."
7955   (destructuring-bind (scheme server port group message-id articles)
7956       (gnus-parse-news-url url)
7957     (cond
7958      (message-id
7959       (with-current-buffer gnus-summary-buffer
7960         (if server
7961             (let ((gnus-refer-article-method
7962                    (nconc (list (list 'nntp server))
7963                           gnus-refer-article-method))
7964                   (nntp-port-number (or port "nntp")))
7965               (gnus-message 7 "Fetching %s with %s"
7966                             message-id gnus-refer-article-method)
7967               (gnus-summary-refer-article message-id))
7968           (gnus-summary-refer-article message-id))))
7969      (group
7970       (gnus-button-fetch-group url)))))
7971
7972 (defun gnus-button-patch (library line)
7973   "Visit an Emacs Lisp library LIBRARY on line LINE."
7974   (interactive)
7975   (let ((file (locate-library (file-name-nondirectory library))))
7976     (unless file
7977       (error "Couldn't find library %s" library))
7978     (find-file file)
7979     (goto-char (point-min))
7980     (forward-line (1- (string-to-number line)))))
7981
7982 (defun gnus-button-handle-man (url)
7983   "Fetch a man page."
7984   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7985   (when (eq gnus-button-man-handler 'woman)
7986     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7987   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7988   (funcall gnus-button-man-handler url))
7989
7990 (defun gnus-button-handle-info-url (url)
7991   "Fetch an info URL."
7992   (setq url (mm-subst-char-in-string ?+ ?\  url))
7993   (cond
7994    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7995     (gnus-info-find-node
7996      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7997                      "Gnus")
7998              ")" (gnus-url-unhex-string (match-string 2 url)))))
7999    ((string-match "([^)\"]+)[^\"]+" url)
8000     (setq url
8001           (gnus-replace-in-string
8002            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8003     (gnus-info-find-node url))
8004    (t (error "Can't parse %s" url))))
8005
8006 (defun gnus-button-handle-info-url-gnome (url)
8007   "Fetch GNOME style info URL."
8008   (setq url (mm-subst-char-in-string ?_ ?\  url))
8009   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8010       (gnus-info-find-node
8011        (concat "("
8012                (gnus-url-unhex-string
8013                  (match-string 1 url))
8014                ")"
8015                (or (gnus-url-unhex-string
8016                     (match-string 2 url))
8017                    "Top")))
8018     (error "Can't parse %s" url)))
8019
8020 (defun gnus-button-handle-info-url-kde (url)
8021   "Fetch KDE style info URL."
8022   (gnus-info-find-node (gnus-url-unhex-string url)))
8023
8024 ;; (info) will autoload info.el
8025 (declare-function Info-menu "info" (menu-item &optional fork))
8026 (declare-function Info-index-next "info" (num))
8027
8028 (defun gnus-button-handle-info-keystrokes (url)
8029   "Call `info' when pushing the corresponding URL button."
8030   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8031   (let (node indx comma)
8032     (if (string-match
8033          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8034                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8035                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
8036                  "\\(?:[ \t\n,]*\\)\\)?")
8037          url)
8038         (setq node (match-string 2 url)
8039               indx (match-string 3 url))
8040       (error "Can't parse %s" url))
8041     (info)
8042     (Info-directory)
8043     (Info-menu node)
8044     (when (> (length indx) 0)
8045       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8046                             "\\([ \t\n,]*\\)")
8047                     indx)
8048       (setq comma (match-string 2 indx))
8049       (setq indx  (match-string 1 indx))
8050       (Info-index indx)
8051       (when comma
8052         (dotimes (i (with-temp-buffer
8053                       (insert comma)
8054                       ;; Note: the XEmacs version of `how-many' takes
8055                       ;; no optional argument.
8056                       (goto-char (point-min))
8057                       (how-many ",")))
8058           (Info-index-next 1)))
8059       nil)))
8060
8061 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8062 (declare-function pgg-display-output-buffer "pgg" (start end status))
8063
8064 (defun gnus-button-openpgp (url)
8065   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8066   (with-temp-buffer
8067     (mm-url-insert-file-contents-external url)
8068     (pgg-snarf-keys-region (point-min) (point-max))
8069     (pgg-display-output-buffer nil nil nil)))
8070
8071 (defun gnus-button-message-id (message-id)
8072   "Fetch MESSAGE-ID."
8073   (with-current-buffer gnus-summary-buffer
8074     (gnus-summary-refer-article message-id)))
8075
8076 (defun gnus-button-fetch-group (address &rest ignore)
8077   "Fetch GROUP specified by ADDRESS."
8078   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8079                       address)
8080     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8081     ;; for nntp:// and news://
8082     (setq address (match-string 3 address)))
8083   (if (not (string-match "[:/]" address))
8084       ;; This is just a simple group url.
8085       (gnus-group-read-ephemeral-group address gnus-select-method)
8086     (if (not
8087          (string-match
8088           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8089           address))
8090         (error "Can't parse %s" address)
8091       (gnus-group-read-ephemeral-group
8092        (match-string 4 address)
8093        `(nntp ,(match-string 1 address)
8094               (nntp-address ,(match-string 1 address))
8095               (nntp-port-number ,(if (match-end 3)
8096                                      (match-string 3 address)
8097                                    "nntp")))
8098        nil nil nil
8099        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8100
8101 (defun gnus-url-parse-query-string (query &optional downcase)
8102   (let (retval pairs cur key val)
8103     (setq pairs (split-string query "&"))
8104     (while pairs
8105       (setq cur (car pairs)
8106             pairs (cdr pairs))
8107       (if (not (string-match "=" cur))
8108           nil                           ; Grace
8109         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8110               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8111         (if downcase
8112             (setq key (downcase key)))
8113         (setq cur (assoc key retval))
8114         (if cur
8115             (setcdr cur (cons val (cdr cur)))
8116           (setq retval (cons (list key val) retval)))))
8117     retval))
8118
8119 (defun gnus-url-mailto (url)
8120   ;; Send mail to someone
8121   (when (string-match "mailto:/*\\(.*\\)" url)
8122     (setq url (substring url (match-beginning 1) nil)))
8123   (let (to args subject func)
8124     (setq args (gnus-url-parse-query-string
8125                 (if (string-match "^\\?" url)
8126                     (substring url 1)
8127                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8128                       (concat "to=" (match-string 1 url) "&"
8129                               (match-string 2 url))
8130                     (concat "to=" url)))
8131                 t)
8132           subject (cdr-safe (assoc "subject" args)))
8133     (gnus-msg-mail)
8134     (while args
8135       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8136       (if (fboundp func)
8137           (funcall func)
8138         (message-position-on-field (caar args)))
8139       (insert (gnus-replace-in-string
8140                (mapconcat 'identity (reverse (cdar args)) ", ")
8141                "\r\n" "\n" t))
8142       (setq args (cdr args)))
8143     (if subject
8144         (message-goto-body)
8145       (message-goto-subject))))
8146
8147 (defun gnus-button-embedded-url (address)
8148   "Activate ADDRESS with `browse-url'."
8149   (browse-url (gnus-strip-whitespace address)))
8150
8151 ;;; Next/prev buttons in the article buffer.
8152
8153 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8154 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8155
8156 (defvar gnus-prev-page-map
8157   (let ((map (make-sparse-keymap)))
8158     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8159     (define-key map "\r" 'gnus-button-prev-page)
8160     map))
8161
8162 (defvar gnus-next-page-map
8163   (let ((map (make-sparse-keymap)))
8164     (define-key map gnus-mouse-2 'gnus-button-next-page)
8165     (define-key map "\r" 'gnus-button-next-page)
8166     map))
8167
8168 (defun gnus-insert-prev-page-button ()
8169   (let ((b (point)) e
8170         (inhibit-read-only t))
8171     (gnus-eval-format
8172      gnus-prev-page-line-format nil
8173      `(keymap ,gnus-prev-page-map
8174               gnus-prev t
8175               gnus-callback gnus-article-button-prev-page
8176               article-type annotation))
8177     (setq e (if (bolp)
8178                 ;; Exclude a newline.
8179                 (1- (point))
8180               (point)))
8181     (when gnus-article-button-face
8182       (gnus-overlay-put (gnus-make-overlay b e nil t)
8183                         'face gnus-article-button-face))
8184     (widget-convert-button
8185      'link b e
8186      :action 'gnus-button-prev-page
8187      :button-keymap gnus-prev-page-map)))
8188
8189 (defun gnus-button-next-page (&optional args more-args)
8190   "Go to the next page."
8191   (interactive)
8192   (let ((win (selected-window)))
8193     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8194     (gnus-article-next-page)
8195     (select-window win)))
8196
8197 (defun gnus-button-prev-page (&optional args more-args)
8198   "Go to the prev page."
8199   (interactive)
8200   (let ((win (selected-window)))
8201     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8202     (gnus-article-prev-page)
8203     (select-window win)))
8204
8205 (defun gnus-insert-next-page-button ()
8206   (let ((b (point)) e
8207         (inhibit-read-only t))
8208     (gnus-eval-format gnus-next-page-line-format nil
8209                       `(keymap ,gnus-next-page-map
8210                                gnus-next t
8211                                gnus-callback gnus-article-button-next-page
8212                                article-type annotation))
8213     (setq e (if (bolp)
8214                 ;; Exclude a newline.
8215                 (1- (point))
8216               (point)))
8217     (when gnus-article-button-face
8218       (gnus-overlay-put (gnus-make-overlay b e nil t)
8219                         'face gnus-article-button-face))
8220     (widget-convert-button
8221      'link b e
8222      :action 'gnus-button-next-page
8223      :button-keymap gnus-next-page-map)))
8224
8225 (defun gnus-article-button-next-page (arg)
8226   "Go to the next page."
8227   (interactive "P")
8228   (let ((win (selected-window)))
8229     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8230     (gnus-article-next-page)
8231     (select-window win)))
8232
8233 (defun gnus-article-button-prev-page (arg)
8234   "Go to the prev page."
8235   (interactive "P")
8236   (let ((win (selected-window)))
8237     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8238     (gnus-article-prev-page)
8239     (select-window win)))
8240
8241 (defvar gnus-decode-header-methods
8242   '(mail-decode-encoded-word-region)
8243   "List of methods used to decode headers.
8244
8245 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8246 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8247 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8248 whose names match REGEXP.
8249
8250 For example:
8251 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8252  mail-decode-encoded-word-region
8253  (\"chinese\" . rfc1843-decode-region))
8254 ")
8255
8256 (defvar gnus-decode-header-methods-cache nil)
8257
8258 (defun gnus-multi-decode-header (start end)
8259   "Apply the functions from `gnus-encoded-word-methods' that match."
8260   (unless (and gnus-decode-header-methods-cache
8261                (eq gnus-newsgroup-name
8262                    (car gnus-decode-header-methods-cache)))
8263     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8264     (dolist (x gnus-decode-header-methods)
8265       (if (symbolp x)
8266           (nconc gnus-decode-header-methods-cache (list x))
8267         (if (and gnus-newsgroup-name
8268                  (string-match (car x) gnus-newsgroup-name))
8269             (nconc gnus-decode-header-methods-cache
8270                    (list (cdr x)))))))
8271   (let ((xlist gnus-decode-header-methods-cache))
8272     (pop xlist)
8273     (save-restriction
8274       (narrow-to-region start end)
8275       (while xlist
8276         (funcall (pop xlist) (point-min) (point-max))))))
8277
8278 ;;;
8279 ;;; Treatment top-level handling.
8280 ;;;
8281
8282 (defvar gnus-inhibit-article-treatments nil)
8283
8284 (defun gnus-treat-article (gnus-treat-condition
8285                            &optional part-number total-parts gnus-treat-type)
8286   (let ((gnus-treat-length (- (point-max) (point-min)))
8287         (alist gnus-treatment-function-alist)
8288         (article-goto-body-goes-to-point-min-p t)
8289         (treated-type
8290          (or (not gnus-treat-type)
8291              (catch 'found
8292                (let ((list gnus-article-treat-types))
8293                  (while list
8294                    (when (string-match (pop list) gnus-treat-type)
8295                      (throw 'found t)))))))
8296         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8297         val elem)
8298     (gnus-run-hooks 'gnus-part-display-hook)
8299     (dolist (elem alist)
8300       (setq val
8301             (save-excursion
8302               (when (gnus-buffer-live-p gnus-summary-buffer)
8303                 (set-buffer gnus-summary-buffer))
8304               (symbol-value (car elem))))
8305       (when (and (or (consp val)
8306                      treated-type)
8307                  (or (not gnus-inhibit-article-treatments)
8308                      (eq gnus-treat-condition 'head))
8309                  (gnus-treat-predicate val)
8310                  (or (not (get (car elem) 'highlight))
8311                      highlightp))
8312         (save-restriction
8313           (funcall (cadr elem)))))))
8314
8315 ;; Dynamic variables.
8316 (defvar part-number)
8317 (defvar total-parts)
8318 (defvar gnus-treat-type)
8319 (defvar gnus-treat-condition)
8320 (defvar gnus-treat-length)
8321
8322 (defun gnus-treat-predicate (val)
8323   (cond
8324    ((null val)
8325     nil)
8326    (gnus-treat-condition
8327     (eq gnus-treat-condition val))
8328    ((and (listp val)
8329          (stringp (car val)))
8330     (apply 'gnus-or (mapcar `(lambda (s)
8331                                (string-match s ,(or gnus-newsgroup-name "")))
8332                             val)))
8333    ((listp val)
8334     (let ((pred (pop val)))
8335       (cond
8336        ((eq pred 'or)
8337         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8338        ((eq pred 'and)
8339         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8340        ((eq pred 'not)
8341         (not (gnus-treat-predicate (car val))))
8342        ((eq pred 'typep)
8343         (equal (car val) gnus-treat-type))
8344        (t
8345         (error "%S is not a valid predicate" pred)))))
8346    ((eq val t)
8347     t)
8348    ((eq val 'head)
8349     nil)
8350    ((eq val 'first)
8351     (eq part-number 1))
8352    ((eq val 'last)
8353     (eq part-number total-parts))
8354    ((numberp val)
8355     (< gnus-treat-length val))
8356    (t
8357     (error "%S is not a valid value" val))))
8358
8359 (defun gnus-article-encrypt-body (protocol &optional n)
8360   "Encrypt the article body."
8361   (interactive
8362    (list
8363     (or gnus-article-encrypt-protocol
8364         (gnus-completing-read "Encrypt protocol"
8365                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8366                               t))
8367     current-prefix-arg))
8368   ;; User might hit `K E' instead of `K e', so prompt once.
8369   (when (and gnus-article-encrypt-protocol
8370              gnus-novice-user)
8371     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8372       (error "Encrypt aborted")))
8373   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8374     (unless func
8375       (error "Can't find the encrypt protocol %s" protocol))
8376     (if (member gnus-newsgroup-name '("nndraft:delayed"
8377                                       "nndraft:drafts"
8378                                       "nndraft:queue"))
8379         (error "Can't encrypt the article in group %s"
8380                gnus-newsgroup-name))
8381     (gnus-summary-iterate n
8382       (with-current-buffer gnus-summary-buffer
8383         (let ((mail-parse-charset gnus-newsgroup-charset)
8384               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8385               (summary-buffer gnus-summary-buffer)
8386               references point)
8387           (gnus-set-global-variables)
8388           (when (gnus-group-read-only-p)
8389             (error "The current newsgroup does not support article encrypt"))
8390           (gnus-summary-show-article t)
8391           (setq references
8392               (or (mail-header-references gnus-current-headers) ""))
8393           (set-buffer gnus-article-buffer)
8394           (let* ((inhibit-read-only t)
8395                  (headers
8396                   (mapcar (lambda (field)
8397                             (and (save-restriction
8398                                    (message-narrow-to-head)
8399                                    (goto-char (point-min))
8400                                    (search-forward field nil t))
8401                                  (prog2
8402                                      (message-narrow-to-field)
8403                                      (buffer-string)
8404                                    (delete-region (point-min) (point-max))
8405                                    (widen))))
8406                           '("Content-Type:" "Content-Transfer-Encoding:"
8407                             "Content-Disposition:"))))
8408             (message-narrow-to-head)
8409             (message-remove-header "MIME-Version")
8410             (goto-char (point-max))
8411             (setq point (point))
8412             (insert (apply 'concat headers))
8413             (widen)
8414             (narrow-to-region point (point-max))
8415             (let ((message-options message-options))
8416               (message-options-set 'message-sender user-mail-address)
8417               (message-options-set 'message-recipients user-mail-address)
8418               (message-options-set 'message-sign-encrypt 'not)
8419               (funcall func))
8420             (goto-char (point-min))
8421             (insert "MIME-Version: 1.0\n")
8422             (widen)
8423             (gnus-summary-edit-article-done
8424              references nil summary-buffer t))
8425           (when gnus-keep-backlog
8426             (gnus-backlog-remove-article
8427              (car gnus-article-current) (cdr gnus-article-current)))
8428           (gnus-flush-original-article-buffer)
8429           (when gnus-use-cache
8430             (gnus-cache-update-article
8431              (car gnus-article-current) (cdr gnus-article-current))))))))
8432
8433 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8434   "The following specs can be used:
8435 %t  The security MIME type
8436 %i  Additional info
8437 %d  Details
8438 %D  Details if button is pressed")
8439
8440 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8441   "The following specs can be used:
8442 %t  The security MIME type
8443 %i  Additional info
8444 %d  Details
8445 %D  Details if button is pressed")
8446
8447 (defvar gnus-mime-security-button-line-format-alist
8448   '((?t gnus-tmp-type ?s)
8449     (?i gnus-tmp-info ?s)
8450     (?d gnus-tmp-details ?s)
8451     (?D gnus-tmp-pressed-details ?s)))
8452
8453 (defvar gnus-mime-security-button-commands
8454   '((gnus-article-press-button "\r" "Show Detail")
8455     (undefined "v")
8456     (undefined "t")
8457     (undefined "C")
8458     (gnus-mime-security-save-part "o" "Save...")
8459     (undefined "\C-o")
8460     (undefined "r")
8461     (undefined "d")
8462     (undefined "c")
8463     (undefined "i")
8464     (undefined "E")
8465     (undefined "e")
8466     (undefined "p")
8467     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8468     (undefined ".")))
8469
8470 (defvar gnus-mime-security-button-map
8471   (let ((map (make-sparse-keymap)))
8472     (define-key map gnus-mouse-2 'gnus-article-push-button)
8473     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8474     (dolist (c gnus-mime-security-button-commands)
8475       (define-key map (cadr c) (car c)))
8476     map))
8477
8478 (easy-menu-define
8479   gnus-mime-security-button-menu gnus-mime-security-button-map
8480   "Security button menu."
8481   `("Security Part"
8482     ,@(delq nil
8483             (mapcar (lambda (c)
8484                       (unless (eq (car c) 'undefined)
8485                         (vector (caddr c) (car c) :active t)))
8486                     gnus-mime-security-button-commands))))
8487
8488 (defun gnus-mime-security-button-menu (event prefix)
8489   "Construct a context-sensitive menu of security commands."
8490   (interactive "e\nP")
8491   (save-window-excursion
8492     (let ((pos (event-start event)))
8493       (select-window (posn-window pos))
8494       (goto-char (posn-point pos))
8495       (gnus-article-check-buffer)
8496       (popup-menu gnus-mime-security-button-menu nil prefix))))
8497
8498 (defvar gnus-mime-security-details-buffer nil)
8499
8500 (defvar gnus-mime-security-button-pressed nil)
8501
8502 (defvar gnus-mime-security-show-details-inline t
8503   "If non-nil, show details in the article buffer.")
8504
8505 (defun gnus-mime-security-verify-or-decrypt (handle)
8506   (mm-remove-parts (cdr handle))
8507   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8508         point (inhibit-read-only t))
8509     (if region
8510         (goto-char (car region)))
8511     (setq point (point))
8512     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8513       (let* ((mm-verify-option 'known)
8514              (mm-decrypt-option 'known)
8515              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8516         (unless (eq nparts (cdr handle))
8517           (mm-destroy-parts (cdr handle))
8518           (setcdr handle nparts))))
8519     (gnus-mime-display-security handle)
8520     (when region
8521       (delete-region (point) (cdr region))
8522       (set-marker (car region) nil)
8523       (set-marker (cdr region) nil))
8524     (goto-char point)))
8525
8526 (defun gnus-mime-security-show-details (handle)
8527   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8528     (if (not details)
8529         (gnus-message 5 "No details.")
8530       (if gnus-mime-security-show-details-inline
8531           (let ((gnus-mime-security-button-pressed
8532                  (not (get-text-property (point) 'gnus-mime-details)))
8533                 (gnus-mime-security-button-line-format
8534                  (get-text-property (point) 'gnus-line-format))
8535                 (inhibit-read-only t))
8536             (forward-char -1)
8537             (while (eq (get-text-property (point) 'gnus-line-format)
8538                        gnus-mime-security-button-line-format)
8539               (forward-char -1))
8540             (forward-char)
8541             (save-restriction
8542               (narrow-to-region (point) (point))
8543               (gnus-insert-mime-security-button handle))
8544             (delete-region (point)
8545                            (or (text-property-not-all
8546                                 (point) (point-max)
8547                                 'gnus-line-format
8548                                 gnus-mime-security-button-line-format)
8549                                (point-max))))
8550         ;; Not inlined.
8551         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8552             (with-current-buffer gnus-mime-security-details-buffer
8553               (erase-buffer)
8554               t)
8555           (setq gnus-mime-security-details-buffer
8556                 (gnus-get-buffer-create "*MIME Security Details*")))
8557         (with-current-buffer gnus-mime-security-details-buffer
8558           (insert details)
8559           (goto-char (point-min)))
8560         (pop-to-buffer gnus-mime-security-details-buffer)))))
8561
8562 (defun gnus-mime-security-press-button (handle)
8563   (save-excursion
8564     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8565         (gnus-mime-security-show-details handle)
8566       (gnus-mime-security-verify-or-decrypt handle))))
8567
8568 (defun gnus-insert-mime-security-button (handle &optional displayed)
8569   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8570          (gnus-tmp-type
8571           (concat
8572            (or (nth 2 (assoc protocol mm-verify-function-alist))
8573                (nth 2 (assoc protocol mm-decrypt-function-alist))
8574                "Unknown")
8575            (if (equal (car handle) "multipart/signed")
8576                " Signed" " Encrypted")
8577            " Part"))
8578          (gnus-tmp-info
8579           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8580               "Undecided"))
8581          (gnus-tmp-details
8582           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8583          gnus-tmp-pressed-details
8584          b e)
8585     (setq gnus-tmp-details
8586           (if gnus-tmp-details
8587               (concat "\n" gnus-tmp-details)
8588             ""))
8589     (setq gnus-tmp-pressed-details
8590           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8591     (unless (bolp)
8592       (insert "\n"))
8593     (setq b (point))
8594     (gnus-eval-format
8595      gnus-mime-security-button-line-format
8596      gnus-mime-security-button-line-format-alist
8597      `(keymap ,gnus-mime-security-button-map
8598          gnus-callback gnus-mime-security-press-button
8599          gnus-line-format ,gnus-mime-security-button-line-format
8600          gnus-mime-details ,gnus-mime-security-button-pressed
8601          article-type annotation
8602          gnus-data ,handle))
8603     (setq e (if (bolp)
8604                 ;; Exclude a newline.
8605                 (1- (point))
8606               (point)))
8607     (when gnus-article-button-face
8608       (gnus-overlay-put (gnus-make-overlay b e nil t)
8609                         'face gnus-article-button-face))
8610     (widget-convert-button
8611      'link b e
8612      :mime-handle handle
8613      :action 'gnus-widget-press-button
8614      :button-keymap gnus-mime-security-button-map
8615      :help-echo
8616      (lambda (widget)
8617        ;; Needed to properly clear the message due to a bug in
8618        ;; wid-edit (XEmacs only).
8619        (when (boundp 'help-echo-owns-message)
8620          (setq help-echo-owns-message t))
8621        (format
8622         "%S: show detail; %S: more options"
8623         (aref gnus-mouse-2 0)
8624         (aref gnus-down-mouse-3 0))))))
8625
8626 (defun gnus-mime-display-security (handle)
8627   (save-restriction
8628     (narrow-to-region (point) (point))
8629     (unless (gnus-unbuttonized-mime-type-p (car handle))
8630       (gnus-insert-mime-security-button handle))
8631     (gnus-mime-display-part (cadr handle))
8632     (unless (bolp)
8633       (insert "\n"))
8634     (unless (gnus-unbuttonized-mime-type-p (car handle))
8635       (let ((gnus-mime-security-button-line-format
8636              gnus-mime-security-button-end-line-format))
8637         (gnus-insert-mime-security-button handle)))
8638     (mm-set-handle-multipart-parameter
8639      handle 'gnus-region
8640      (cons (set-marker (make-marker) (point-min))
8641            (set-marker (make-marker) (point-max))))
8642     (goto-char (point-max))))
8643
8644 (defun gnus-mime-security-run-function (function)
8645   "Run FUNCTION with the security part under point."
8646   (gnus-article-check-buffer)
8647   (let ((data (get-text-property (point) 'gnus-data))
8648         buffer handle)
8649     (when (and (stringp (car-safe data))
8650                (setq buffer (mm-handle-multipart-original-buffer data))
8651                (setq handle (cadr data)))
8652       (if (bufferp (mm-handle-buffer handle))
8653           (progn
8654             (setq handle (cons buffer (copy-sequence (cdr handle))))
8655             (mm-handle-set-undisplayer handle nil))
8656         (setq handle (mm-make-handle
8657                       buffer
8658                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8659                       nil nil nil nil nil nil)))
8660       (funcall function handle))))
8661
8662 (defun gnus-mime-security-save-part ()
8663   "Save the security part under point."
8664   (interactive)
8665   (gnus-mime-security-run-function 'mm-save-part))
8666
8667 (defun gnus-mime-security-pipe-part ()
8668   "Pipe the security part under point to a process."
8669   (interactive)
8670   (gnus-mime-security-run-function 'mm-pipe-part))
8671
8672 (gnus-ems-redefine)
8673
8674 (provide 'gnus-art)
8675
8676 (run-hooks 'gnus-art-load-hook)
8677
8678 ;;; gnus-art.el ends here