0e354dc84f34e67634c6eb75861f3ebc48aeba5f
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs <22.2 and XEmacs.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (defvar tool-bar-map)
34 (defvar w3m-minor-mode-map)
35
36 (require 'gnus)
37 (require 'gnus-sum)
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54 (autoload 'mm-url-insert-file-contents-external "mm-url")
55 (autoload 'mm-extern-cache-contents "mm-extern")
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   (mapcar
120    (lambda (header)
121      (concat "^" header ":"))
122    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
123      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
124      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
125      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
126      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
127      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
128      "X-Attribution" "X-Originating-IP" "Delivered-To"
129      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
130      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
131      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
132      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
133      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
134      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
135      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
136      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
137      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
138      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
139      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
140      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
141      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
142      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
143      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
144      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
145      "List-[A-Za-z]+" "X-Listprocessor-Version"
146      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
147      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
148      "X-Received" "Content-length" "X-precedence"
149      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
150      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
151      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
152      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
153      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
154      "X-Content-length" "X-Posting-Agent" "Original-Received"
155      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
156      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
157      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
158      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
159      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
160      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
161      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
162      "Envelope-Sender" "Envelope-Recipients"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice :custom-show nil
167                  regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-hidden-properties '(invisible t intangible t)
261   "Property list to use for hiding text."
262   :type 'sexp
263   :group 'gnus-article-hiding)
264
265 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
266 ;; frames in a session.
267 (defcustom gnus-article-x-face-command
268   (if (featurep 'xemacs)
269       (if (or (gnus-image-type-available-p 'xface)
270               (gnus-image-type-available-p 'pbm))
271           'gnus-display-x-face-in-from
272         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
273     (if (gnus-image-type-available-p 'pbm)
274         'gnus-display-x-face-in-from
275       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
276 display -"))
277   "*String or function to be executed to display an X-Face header.
278 If it is a string, the command will be executed in a sub-shell
279 asynchronously.  The compressed face will be piped to this command."
280   :type `(choice string
281                  (function-item gnus-display-x-face-in-from)
282                  function)
283   :version "21.1"
284   :group 'gnus-picon
285   :group 'gnus-article-washing)
286
287 (defcustom gnus-article-x-face-too-ugly nil
288   "Regexp matching posters whose face shouldn't be shown automatically."
289   :type '(choice regexp (const nil))
290   :group 'gnus-article-washing)
291
292 (defcustom gnus-article-banner-alist nil
293   "Banner alist for stripping.
294 For example,
295      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
296   :version "21.1"
297   :type '(repeat (cons symbol regexp))
298   :group 'gnus-article-washing)
299
300 (gnus-define-group-parameter
301  banner
302  :variable-document
303  "Alist of regexps (to match group names) and banner."
304  :variable-group gnus-article-washing
305  :parameter-type
306  '(choice :tag "Banner"
307           :value nil
308           (const :tag "Remove signature" signature)
309           (symbol :tag "Item in `gnus-article-banner-alist'" none)
310           regexp
311           (const :tag "None" nil))
312  :parameter-document
313  "If non-nil, specify how to remove `banners' from articles.
314
315 Symbol `signature' means to remove signatures delimited by
316 `gnus-signature-separator'.  Any other symbol is used to look up a
317 regular expression to match the banner in `gnus-article-banner-alist'.
318 A string is used as a regular expression to match the banner
319 directly.")
320
321 (defcustom gnus-article-address-banner-alist nil
322   "Alist of mail addresses and banners.
323 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
324 to match a mail address in the From: header, BANNER is one of a symbol
325 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
326 If ADDRESS matches author's mail address, it will remove things like
327 advertisements.  For example:
328
329 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
330 "
331   :type '(repeat
332           (cons
333            (regexp :tag "Address")
334            (choice :tag "Banner" :value nil
335                    (const :tag "Remove signature" signature)
336                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
337                    regexp
338                    (const :tag "None" nil))))
339   :version "22.1"
340   :group 'gnus-article-washing)
341
342 (defmacro gnus-emphasis-custom-with-format (&rest body)
343   `(let ((format "\
344 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
345 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
346      ,@body))
347
348 (defun gnus-emphasis-custom-value-to-external (value)
349   (gnus-emphasis-custom-with-format
350    (if (consp (car value))
351        (list (format format (car (car value)) (cdr (car value)))
352              2
353              (if (nth 1 value) 2 3)
354              (nth 2 value))
355      value)))
356
357 (defun gnus-emphasis-custom-value-to-internal (value)
358   (gnus-emphasis-custom-with-format
359    (let ((regexp (concat "\\`"
360                          (format (regexp-quote format)
361                                  "\\([^()]+\\)" "\\([^()]+\\)")
362                          "\\'"))
363          pattern)
364      (if (string-match regexp (setq pattern (car value)))
365          (list (cons (match-string 1 pattern) (match-string 2 pattern))
366                (= (nth 2 value) 2)
367                (nth 3 value))
368        value))))
369
370 (defcustom gnus-emphasis-alist
371   (let ((types
372          '(("\\*" "\\*" bold nil 2)
373            ("_" "_" underline)
374            ("/" "/" italic)
375            ("_/" "/_" underline-italic)
376            ("_\\*" "\\*_" underline-bold)
377            ("\\*/" "/\\*" bold-italic)
378            ("_\\*/" "/\\*_" underline-bold-italic))))
379     (nconc
380      (gnus-emphasis-custom-with-format
381       (mapcar (lambda (spec)
382                 (list (format format (car spec) (cadr spec))
383                       (or (nth 3 spec) 2)
384                       (or (nth 4 spec) 3)
385                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
386               types))
387      '(;; I've never seen anyone use this strikethru convention whereas I've
388        ;; several times seen it triggered by normal text.  --Stef
389        ;; Miles suggests that this form is sometimes used but for italics,
390        ;; so maybe we should map it to `italic'.
391        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
392        ;; 2 3 gnus-emphasis-strikethru)
393        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
394         2 3 gnus-emphasis-underline))))
395   "*Alist that says how to fontify certain phrases.
396 Each item looks like this:
397
398   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
399
400 The first element is a regular expression to be matched.  The second
401 is a number that says what regular expression grouping used to find
402 the entire emphasized word.  The third is a number that says what
403 regexp grouping should be displayed and highlighted.  The fourth
404 is the face used for highlighting."
405   :type
406   '(repeat
407     (menu-choice
408      :format "%[Customizing Style%]\n%v"
409      :indent 2
410      (group :tag "Default"
411             :value ("" 0 0 default)
412             :value-create
413             (lambda (widget)
414               (let ((value (widget-get
415                             (cadr (widget-get (widget-get widget :parent)
416                                               :args))
417                             :value)))
418                 (if (not (eq (nth 2 value) 'default))
419                     (widget-put
420                      widget
421                      :value
422                      (gnus-emphasis-custom-value-to-external value))))
423               (widget-group-value-create widget))
424             regexp
425             (integer :format "Match group: %v")
426             (integer :format "Emphasize group: %v")
427             face)
428      (group :tag "Simple"
429             :value (("_" . "_") nil default)
430             (cons :format "%v"
431                   (regexp :format "Start regexp: %v")
432                   (regexp :format "End regexp: %v"))
433             (boolean :format "Show start and end patterns: %[%v%]\n"
434                      :on " On " :off " Off ")
435             face)))
436   :get (lambda (symbol)
437          (mapcar 'gnus-emphasis-custom-value-to-internal
438                  (default-value symbol)))
439   :set (lambda (symbol value)
440          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
441                                      value)))
442   :group 'gnus-article-emphasis)
443
444 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
445   "A regexp to describe whitespace which should not be emphasized.
446 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
447 The former avoids underlining of leading and trailing whitespace,
448 and the latter avoids underlining any whitespace at all."
449   :version "21.1"
450   :group 'gnus-article-emphasis
451   :type 'regexp)
452
453 (defface gnus-emphasis-bold '((t (:bold t)))
454   "Face used for displaying strong emphasized text (*word*)."
455   :group 'gnus-article-emphasis)
456
457 (defface gnus-emphasis-italic '((t (:italic t)))
458   "Face used for displaying italic emphasized text (/word/)."
459   :group 'gnus-article-emphasis)
460
461 (defface gnus-emphasis-underline '((t (:underline t)))
462   "Face used for displaying underlined emphasized text (_word_)."
463   :group 'gnus-article-emphasis)
464
465 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
466   "Face used for displaying underlined bold emphasized text (_*word*_)."
467   :group 'gnus-article-emphasis)
468
469 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
470   "Face used for displaying underlined italic emphasized text (_/word/_)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
474   "Face used for displaying bold italic emphasized text (/*word*/)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-underline-bold-italic
478   '((t (:bold t :italic t :underline t)))
479   "Face used for displaying underlined bold italic emphasized text.
480 Example: (_/*word*/_)."
481   :group 'gnus-article-emphasis)
482
483 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
484                                       '((t (:strikethru t)))
485                                     '((t (:strike-through t))))
486   "Face used for displaying strike-through text (-word-)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-highlight-words
490   '((t (:background "black" :foreground "yellow")))
491   "Face used for displaying highlighted words."
492   :group 'gnus-article-emphasis)
493
494 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
495   "Format for display of Date headers in article bodies.
496 See `format-time-string' for the possible values.
497
498 The variable can also be function, which should return a complete Date
499 header.  The function is called with one argument, the time, which can
500 be fed to `format-time-string'."
501   :type '(choice string function)
502   :link '(custom-manual "(gnus)Article Date")
503   :group 'gnus-article-washing)
504
505 (defcustom gnus-save-all-headers t
506   "*If non-nil, don't remove any headers before saving.
507 This will be overridden by the `:headers' property that the symbol of
508 the saver function, which is specified by `gnus-default-article-saver',
509 might have."
510   :group 'gnus-article-saving
511   :type 'boolean)
512
513 (defcustom gnus-prompt-before-saving 'always
514   "*This variable says how much prompting is to be done when saving articles.
515 If it is nil, no prompting will be done, and the articles will be
516 saved to the default files.  If this variable is `always', each and
517 every article that is saved will be preceded by a prompt, even when
518 saving large batches of articles.  If this variable is neither nil not
519 `always', there the user will be prompted once for a file name for
520 each invocation of the saving commands."
521   :group 'gnus-article-saving
522   :type '(choice (item always)
523                  (item :tag "never" nil)
524                  (sexp :tag "once" :format "%t\n" :value t)))
525
526 (defcustom gnus-saved-headers gnus-visible-headers
527   "Headers to keep if `gnus-save-all-headers' is nil.
528 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
529 If that variable is nil, however, all headers that match this regexp
530 will be kept while the rest will be deleted before saving.  This and
531 `gnus-save-all-headers' will be overridden by the `:headers' property
532 that the symbol of the saver function, which is specified by
533 `gnus-default-article-saver', might have."
534   :group 'gnus-article-saving
535   :type 'regexp)
536
537 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
538 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
539   "A function to save articles in your favourite format.
540 The function will be called by way of the `gnus-summary-save-article'
541 command, and friends such as `gnus-summary-save-article-rmail'.
542
543 Gnus provides the following functions:
544
545 * gnus-summary-save-in-rmail (Rmail format)
546 * gnus-summary-save-in-mail (Unix mail format)
547 * gnus-summary-save-in-folder (MH folder)
548 * gnus-summary-save-in-file (article format)
549 * gnus-summary-save-body-in-file (article body)
550 * gnus-summary-save-in-vm (use VM's folder format)
551 * gnus-summary-write-to-file (article format -- overwrite)
552 * gnus-summary-write-body-to-file (article body -- overwrite)
553 * gnus-summary-save-in-pipe (article format)
554
555 The symbol of each function may have the following properties:
556
557 * :decode
558 The value non-nil means save decoded articles.  This is meaningful
559 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
560 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
561 `gnus-summary-save-in-pipe'.
562
563 * :function
564 The value specifies an alternative function which appends, not
565 overwrites, articles to a file.  This implies that when saving many
566 articles at a time, `gnus-prompt-before-saving' is bound to t and all
567 articles are saved in a single file.  This is meaningful only with
568 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
569
570 * :headers
571 The value specifies the symbol of a variable of which the value
572 specifies headers to be saved.  If it is omitted,
573 `gnus-save-all-headers' and `gnus-saved-headers' control what
574 headers should be saved."
575   :group 'gnus-article-saving
576   :type '(radio (function-item gnus-summary-save-in-rmail)
577                 (function-item gnus-summary-save-in-mail)
578                 (function-item gnus-summary-save-in-folder)
579                 (function-item gnus-summary-save-in-file)
580                 (function-item gnus-summary-save-body-in-file)
581                 (function-item gnus-summary-save-in-vm)
582                 (function-item gnus-summary-write-to-file)
583                 (function-item gnus-summary-write-body-to-file)
584                 (function-item gnus-summary-save-in-pipe)
585                 (function)))
586
587 (defcustom gnus-article-save-coding-system
588   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
589       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
590       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
591       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
592   "Coding system used to save decoded articles to a file.
593
594 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
595 which can safely encode any characters in text.  This is used by the
596 commands including:
597
598 * gnus-summary-save-article-file
599 * gnus-summary-save-article-body-file
600 * gnus-summary-write-article-file
601 * gnus-summary-write-article-body-file
602
603 and the functions to which you may set `gnus-default-article-saver':
604
605 * gnus-summary-save-in-file
606 * gnus-summary-save-body-in-file
607 * gnus-summary-write-to-file
608 * gnus-summary-write-body-to-file
609
610 Those commands and functions save just text displayed in the article
611 buffer to a file if the value of this variable is non-nil.  Note that
612 buttonized MIME parts will be lost in a saved file in that case.
613 Otherwise, raw articles will be saved."
614   :group 'gnus-article-saving
615   :type `(choice
616           :format "%{%t%}:\n %[Value Menu%] %v"
617           (const :tag "Save raw articles" nil)
618           ,@(delq nil
619                   (mapcar
620                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
621                    '((const :tag "UTF-8" utf-8)
622                      (const :tag "iso-2022-7bit" iso-2022-7bit)
623                      (const :tag "Emacs internal" emacs-mule)
624                      (const :tag "escape-quoted" escape-quoted))))
625           (symbol :tag "Coding system")))
626
627 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
628   "A function generating a file name to save articles in Rmail format.
629 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
630   :group 'gnus-article-saving
631   :type 'function)
632
633 (defcustom gnus-mail-save-name 'gnus-plain-save-name
634   "A function generating a file name to save articles in Unix mail format.
635 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
636   :group 'gnus-article-saving
637   :type 'function)
638
639 (defcustom gnus-folder-save-name 'gnus-folder-save-name
640   "A function generating a file name to save articles in MH folder.
641 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
642   :group 'gnus-article-saving
643   :type 'function)
644
645 (defcustom gnus-file-save-name 'gnus-numeric-save-name
646   "A function generating a file name to save articles in article format.
647 The function is called with NEWSGROUP, HEADERS, and optional
648 LAST-FILE."
649   :group 'gnus-article-saving
650   :type 'function)
651
652 (defcustom gnus-split-methods
653   '((gnus-article-archive-name)
654     (gnus-article-nndoc-name))
655   "*Variable used to suggest where articles are to be saved.
656 For instance, if you would like to save articles related to Gnus in
657 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
658 you could set this variable to something like:
659
660  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
661    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
662
663 This variable is an alist where the key is the match and the
664 value is a list of possible files to save in if the match is
665 non-nil.
666
667 If the match is a string, it is used as a regexp match on the
668 article.  If the match is a symbol, that symbol will be funcalled
669 from the buffer of the article to be saved with the newsgroup as the
670 parameter.  If it is a list, it will be evaled in the same buffer.
671
672 If this form or function returns a string, this string will be used as a
673 possible file name; and if it returns a non-nil list, that list will be
674 used as possible file names."
675   :group 'gnus-article-saving
676   :type '(repeat (choice (list :value (fun) function)
677                          (cons :value ("" "") regexp (repeat string))
678                          (sexp :value nil))))
679
680 (defcustom gnus-page-delimiter "^\^L"
681   "*Regexp describing what to use as article page delimiters.
682 The default value is \"^\^L\", which is a form linefeed at the
683 beginning of a line."
684   :type 'regexp
685   :group 'gnus-article-various)
686
687 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
688   "*The format specification for the article mode line.
689 See `gnus-summary-mode-line-format' for a closer description.
690
691 The following additional specs are available:
692
693 %w  The article washing status.
694 %m  The number of MIME parts in the article."
695   :type 'string
696   :group 'gnus-article-various)
697
698 (defcustom gnus-article-mode-hook nil
699   "*A hook for Gnus article mode."
700   :type 'hook
701   :group 'gnus-article-various)
702
703 (when (featurep 'xemacs)
704   ;; Extracted from gnus-xmas-define in order to preserve user settings
705   (when (fboundp 'turn-off-scroll-in-place)
706     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
707   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
708   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
709
710 (defcustom gnus-article-menu-hook nil
711   "*Hook run after the creation of the article mode menu."
712   :type 'hook
713   :group 'gnus-article-various)
714
715 (defcustom gnus-article-prepare-hook nil
716   "*A hook called after an article has been prepared in the article buffer."
717   :type 'hook
718   :group 'gnus-article-various)
719
720 (defcustom gnus-copy-article-ignored-headers nil
721   "List of headers to be removed when copying an article.
722 Each element is a regular expression."
723   :version "23.1" ;; No Gnus
724   :type '(repeat regexp)
725   :group 'gnus-article-various)
726
727 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
728                         "Gnus 5.10 (Emacs 22.1)")
729
730 (defface gnus-button
731   '((t (:weight bold)))
732   "Face used for highlighting a button in the article buffer."
733   :group 'gnus-article-buttons)
734
735 (defcustom gnus-article-button-face 'gnus-button
736   "Face used for highlighting buttons in the article buffer.
737
738 An article button is a piece of text that you can activate by pressing
739 `RET' or `mouse-2' above it."
740   :type 'face
741   :group 'gnus-article-buttons)
742
743 (defcustom gnus-article-mouse-face 'highlight
744   "Face used for mouse highlighting in the article buffer.
745
746 Article buttons will be displayed in this face when the cursor is
747 above them."
748   :type 'face
749   :group 'gnus-article-buttons)
750
751 (defcustom gnus-signature-face 'gnus-signature
752   "Face used for highlighting a signature in the article buffer.
753 Obsolete; use the face `gnus-signature' for customizations instead."
754   :type 'face
755   :group 'gnus-article-highlight
756   :group 'gnus-article-signature)
757
758 (defface gnus-signature
759   '((t
760      (:italic t)))
761   "Face used for highlighting a signature in the article buffer."
762   :group 'gnus-article-highlight
763   :group 'gnus-article-signature)
764 ;; backward-compatibility alias
765 (put 'gnus-signature-face 'face-alias 'gnus-signature)
766 (put 'gnus-signature-face 'obsolete-face "22.1")
767
768 (defface gnus-header-from
769   '((((class color)
770       (background dark))
771      (:foreground "PaleGreen1"))
772     (((class color)
773       (background light))
774      (:foreground "red3"))
775     (t
776      (:italic t)))
777   "Face used for displaying from headers."
778   :group 'gnus-article-headers
779   :group 'gnus-article-highlight)
780 ;; backward-compatibility alias
781 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
782 (put 'gnus-header-from-face 'obsolete-face "22.1")
783
784 (defface gnus-header-subject
785   '((((class color)
786       (background dark))
787      (:foreground "SeaGreen1"))
788     (((class color)
789       (background light))
790      (:foreground "red4"))
791     (t
792      (:bold t :italic t)))
793   "Face used for displaying subject headers."
794   :group 'gnus-article-headers
795   :group 'gnus-article-highlight)
796 ;; backward-compatibility alias
797 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
798 (put 'gnus-header-subject-face 'obsolete-face "22.1")
799
800 (defface gnus-header-newsgroups
801   '((((class color)
802       (background dark))
803      (:foreground "yellow" :italic t))
804     (((class color)
805       (background light))
806      (:foreground "MidnightBlue" :italic t))
807     (t
808      (:italic t)))
809   "Face used for displaying newsgroups headers.
810 In the default setup this face is only used for crossposted
811 articles."
812   :group 'gnus-article-headers
813   :group 'gnus-article-highlight)
814 ;; backward-compatibility alias
815 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
816 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
817
818 (defface gnus-header-name
819   '((((class color)
820       (background dark))
821      (:foreground "SpringGreen2"))
822     (((class color)
823       (background light))
824      (:foreground "maroon"))
825     (t
826      (:bold t)))
827   "Face used for displaying header names."
828   :group 'gnus-article-headers
829   :group 'gnus-article-highlight)
830 ;; backward-compatibility alias
831 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
832 (put 'gnus-header-name-face 'obsolete-face "22.1")
833
834 (defface gnus-header-content
835   '((((class color)
836       (background dark))
837      (:foreground "SpringGreen1" :italic t))
838     (((class color)
839       (background light))
840      (:foreground "indianred4" :italic t))
841     (t
842      (:italic t)))  "Face used for displaying header content."
843   :group 'gnus-article-headers
844   :group 'gnus-article-highlight)
845 ;; backward-compatibility alias
846 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
847 (put 'gnus-header-content-face 'obsolete-face "22.1")
848
849 (defcustom gnus-header-face-alist
850   '(("From" nil gnus-header-from)
851     ("Subject" nil gnus-header-subject)
852     ("Newsgroups:.*," nil gnus-header-newsgroups)
853     ("" gnus-header-name gnus-header-content))
854   "*Controls highlighting of article headers.
855
856 An alist of the form (HEADER NAME CONTENT).
857
858 HEADER is a regular expression which should match the name of a
859 header and NAME and CONTENT are either face names or nil.
860
861 The name of each header field will be displayed using the face
862 specified by the first element in the list where HEADER matches
863 the header name and NAME is non-nil.  Similarly, the content will
864 be displayed by the first non-nil matching CONTENT face."
865   :group 'gnus-article-headers
866   :group 'gnus-article-highlight
867   :type '(repeat (list (regexp :tag "Header")
868                        (choice :tag "Name"
869                                (item :tag "skip" nil)
870                                (face :value default))
871                        (choice :tag "Content"
872                                (item :tag "skip" nil)
873                                (face :value default)))))
874
875 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
876                                           '((xface . (:face gnus-x-face)))
877                                         '((pbm . (:face gnus-x-face))
878                                           (png . nil)))
879   "Alist of image types and properties applied to Face and X-Face images.
880 Here are examples:
881
882 ;; Specify the altitude of Face images in the From header.
883 \(setq gnus-face-properties-alist
884       '((pbm . (:face gnus-x-face :ascent 80))
885         (png . (:ascent 80))))
886
887 ;; Show Face images as pressed buttons.
888 \(setq gnus-face-properties-alist
889       '((pbm . (:face gnus-x-face :relief -2))
890         (png . (:relief -2))))
891
892 See the manual for the valid properties for various image types.
893 Currently, `pbm' is used for X-Face images and `png' is used for Face
894 images in Emacs.  Only the `:face' property is effective on the `xface'
895 image type in XEmacs if it is built with the libcompface library."
896   :version "23.1" ;; No Gnus
897   :group 'gnus-article-headers
898   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
899
900 (defcustom gnus-article-decode-hook
901   '(article-decode-charset article-decode-encoded-words
902                            article-decode-group-name article-decode-idna-rhs)
903   "*Hook run to decode charsets in articles."
904   :group 'gnus-article-headers
905   :type 'hook)
906
907 (defcustom gnus-display-mime-function 'gnus-display-mime
908   "Function to display MIME articles."
909   :group 'gnus-article-mime
910   :type 'function)
911
912 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
913   "Function used to decode headers.")
914
915 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
916   "Function used to decode addresses.")
917
918 (defvar gnus-article-dumbquotes-map
919   '((?\200 "EUR")
920     (?\202 ",")
921     (?\203 "f")
922     (?\204 ",,")
923     (?\205 "...")
924     (?\213 "<")
925     (?\214 "OE")
926     (?\221 "`")
927     (?\222 "'")
928     (?\223 "``")
929     (?\224 "\"")
930     (?\225 "*")
931     (?\226 "-")
932     (?\227 "--")
933     (?\230 "~")
934     (?\231 "(TM)")
935     (?\233 ">")
936     (?\234 "oe")
937     (?\264 "'"))
938   "Table for MS-to-Latin1 translation.")
939
940 (defcustom gnus-ignored-mime-types nil
941   "List of MIME types that should be ignored by Gnus."
942   :version "21.1"
943   :group 'gnus-article-mime
944   :type '(repeat regexp))
945
946 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
947   "List of MIME types that should not be given buttons when rendered inline.
948 See also `gnus-buttonized-mime-types' which may override this variable.
949 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
950   :version "21.1"
951   :group 'gnus-article-mime
952   :type '(repeat regexp))
953
954 (defcustom gnus-buttonized-mime-types nil
955   "List of MIME types that should be given buttons when rendered inline.
956 If set, this variable overrides `gnus-unbuttonized-mime-types'.
957 To see e.g. security buttons you could set this to
958 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
959 this list to display radio buttons that allow you to choose one of two
960 media types those mails include.  See also `mm-discouraged-alternatives'.
961 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
962   :version "22.1"
963   :group 'gnus-article-mime
964   :type '(repeat regexp))
965
966 (defcustom gnus-inhibit-mime-unbuttonizing nil
967   "If non-nil, all MIME parts get buttons.
968 When nil (the default value), then some MIME parts do not get buttons,
969 as described by the variables `gnus-buttonized-mime-types' and
970 `gnus-unbuttonized-mime-types'."
971   :version "22.1"
972   :group 'gnus-article-mime
973   :type 'boolean)
974
975 (defcustom gnus-body-boundary-delimiter "_"
976   "String used to delimit header and body.
977 This variable is used by `gnus-article-treat-body-boundary' which can
978 be controlled by `gnus-treat-body-boundary'."
979   :version "22.1"
980   :group 'gnus-article-various
981   :type '(choice (item :tag "None" :value nil)
982                  string))
983
984 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
985                                   "/usr/share/picons")
986   "Defines the location of the faces database.
987 For information on obtaining this database of pretty pictures, please
988 see http://www.cs.indiana.edu/picons/ftp/index.html"
989   :version "22.1"
990   :type '(repeat directory)
991   :link '(url-link :tag "download"
992                    "http://www.cs.indiana.edu/picons/ftp/index.html")
993   :link '(custom-manual "(gnus)Picons")
994   :group 'gnus-picon)
995
996 (defun gnus-picons-installed-p ()
997   "Say whether picons are installed on your machine."
998   (let ((installed nil))
999     (dolist (database gnus-picon-databases)
1000       (when (file-exists-p database)
1001         (setq installed t)))
1002     installed))
1003
1004 (defcustom gnus-article-mime-part-function nil
1005   "Function called with a MIME handle as the argument.
1006 This is meant for people who want to do something automatic based
1007 on parts -- for instance, adding Vcard info to a database."
1008   :group 'gnus-article-mime
1009   :type '(choice (const nil)
1010                  function))
1011
1012 (defcustom gnus-mime-multipart-functions nil
1013   "An alist of MIME types to functions to display them."
1014   :version "21.1"
1015   :group 'gnus-article-mime
1016   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1017
1018 (defcustom gnus-article-date-lapsed-new-header nil
1019   "Whether the X-Sent and Date headers can coexist.
1020 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1021 either replace the old \"Date:\" header (if this variable is nil), or
1022 be added below it (otherwise)."
1023   :version "21.1"
1024   :group 'gnus-article-headers
1025   :type 'boolean)
1026
1027 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1028   "Function called with a MIME handle as the argument.
1029 This is meant for people who want to view first matched part.
1030 For `undisplayed-alternative' (default), the first undisplayed
1031 part or alternative part is used.  For `undisplayed', the first
1032 undisplayed part is used.  For a function, the first part which
1033 the function return t is used.  For nil, the first part is
1034 used."
1035   :version "21.1"
1036   :group 'gnus-article-mime
1037   :type '(choice
1038           (item :tag "first" :value nil)
1039           (item :tag "undisplayed" :value undisplayed)
1040           (item :tag "undisplayed or alternative"
1041                 :value undisplayed-alternative)
1042           (function)))
1043
1044 (defcustom gnus-mime-action-alist
1045   '(("save to file" . gnus-mime-save-part)
1046     ("save and strip" . gnus-mime-save-part-and-strip)
1047     ("replace with file" . gnus-mime-replace-part)
1048     ("delete part" . gnus-mime-delete-part)
1049     ("display as text" . gnus-mime-inline-part)
1050     ("view the part" . gnus-mime-view-part)
1051     ("pipe to command" . gnus-mime-pipe-part)
1052     ("toggle display" . gnus-article-press-button)
1053     ("toggle display" . gnus-article-view-part-as-charset)
1054     ("view as type" . gnus-mime-view-part-as-type)
1055     ("view internally" . gnus-mime-view-part-internally)
1056     ("view externally" . gnus-mime-view-part-externally))
1057   "An alist of actions that run on the MIME attachment."
1058   :group 'gnus-article-mime
1059   :type '(repeat (cons (string :tag "name")
1060                        (function))))
1061
1062 (defcustom gnus-auto-select-part 1
1063   "Advance to next MIME part when deleting or stripping parts.
1064
1065 When 0, point will be placed on the same part as before.  When
1066 positive (negative), move point forward (backwards) this many
1067 parts.  When nil, redisplay article."
1068   :version "23.1" ;; No Gnus
1069   :group 'gnus-article-mime
1070   :type '(choice (const nil :tag "Redisplay article.")
1071                  (const 1 :tag "Next part.")
1072                  (const 0 :tag "Current part.")
1073                  integer))
1074
1075 ;;;
1076 ;;; The treatment variables
1077 ;;;
1078
1079 (defvar gnus-part-display-hook nil
1080   "Hook called on parts that are to receive treatment.")
1081
1082 (defvar gnus-article-treat-custom
1083   '(choice (const :tag "Off" nil)
1084            (const :tag "On" t)
1085            (const :tag "Header" head)
1086            (const :tag "First" first)
1087            (const :tag "Last" last)
1088            (integer :tag "Less")
1089            (repeat :tag "Groups" regexp)
1090            (sexp :tag "Predicate")))
1091
1092 (defvar gnus-article-treat-head-custom
1093   '(choice (const :tag "Off" nil)
1094            (const :tag "Header" head)))
1095
1096 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1097                                    "text/x-patch")
1098   "Parts to treat.")
1099
1100 (defvar gnus-inhibit-treatment nil
1101   "Whether to inhibit treatment.")
1102
1103 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1104   "Highlight the signature.
1105 Valid values are nil, t, `head', `first', `last', an integer or a
1106 predicate.  See Info node `(gnus)Customizing Articles'."
1107   :group 'gnus-article-treat
1108   :link '(custom-manual "(gnus)Customizing Articles")
1109   :type gnus-article-treat-custom)
1110 (put 'gnus-treat-highlight-signature 'highlight t)
1111
1112 (defcustom gnus-treat-buttonize 100000
1113   "Add buttons.
1114 Valid values are nil, t, `head', `first', `last', an integer or a
1115 predicate.  See Info node `(gnus)Customizing Articles'."
1116   :group 'gnus-article-treat
1117   :link '(custom-manual "(gnus)Customizing Articles")
1118   :type gnus-article-treat-custom)
1119 (put 'gnus-treat-buttonize 'highlight t)
1120
1121 (defcustom gnus-treat-buttonize-head 'head
1122   "Add buttons to the head.
1123 Valid values are nil, t, `head', `first', `last', an integer or a
1124 predicate.  See Info node `(gnus)Customizing Articles'."
1125   :group 'gnus-article-treat
1126   :link '(custom-manual "(gnus)Customizing Articles")
1127   :type gnus-article-treat-head-custom)
1128 (put 'gnus-treat-buttonize-head 'highlight t)
1129
1130 (defcustom gnus-treat-emphasize 50000
1131   "Emphasize text.
1132 Valid values are nil, t, `head', `first', `last', an integer or a
1133 predicate.  See Info node `(gnus)Customizing Articles'."
1134   :group 'gnus-article-treat
1135   :link '(custom-manual "(gnus)Customizing Articles")
1136   :type gnus-article-treat-custom)
1137 (put 'gnus-treat-emphasize 'highlight t)
1138
1139 (defcustom gnus-treat-strip-cr nil
1140   "Remove carriage returns.
1141 Valid values are nil, t, `head', `first', `last', an integer or a
1142 predicate.  See Info node `(gnus)Customizing Articles'."
1143   :version "22.1"
1144   :group 'gnus-article-treat
1145   :link '(custom-manual "(gnus)Customizing Articles")
1146   :type gnus-article-treat-custom)
1147
1148 (defcustom gnus-treat-unsplit-urls nil
1149   "Remove newlines from within URLs.
1150 Valid values are nil, t, `head', `first', `last', an integer or a
1151 predicate.  See Info node `(gnus)Customizing Articles'."
1152   :version "22.1"
1153   :group 'gnus-article-treat
1154   :link '(custom-manual "(gnus)Customizing Articles")
1155   :type gnus-article-treat-custom)
1156
1157 (defcustom gnus-treat-leading-whitespace nil
1158   "Remove leading whitespace in headers.
1159 Valid values are nil, t, `head', `first', `last', an integer or a
1160 predicate.  See Info node `(gnus)Customizing Articles'."
1161   :version "22.1"
1162   :group 'gnus-article-treat
1163   :link '(custom-manual "(gnus)Customizing Articles")
1164   :type gnus-article-treat-custom)
1165
1166 (defcustom gnus-treat-hide-headers 'head
1167   "Hide headers.
1168 Valid values are nil, t, `head', `first', `last', an integer or a
1169 predicate.  See Info node `(gnus)Customizing Articles'."
1170   :group 'gnus-article-treat
1171   :link '(custom-manual "(gnus)Customizing Articles")
1172   :type gnus-article-treat-head-custom)
1173
1174 (defcustom gnus-treat-hide-boring-headers nil
1175   "Hide boring headers.
1176 Valid values are nil, t, `head', `first', `last', an integer or a
1177 predicate.  See Info node `(gnus)Customizing Articles'."
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-head-custom)
1181
1182 (defcustom gnus-treat-hide-signature nil
1183   "Hide the signature.
1184 Valid values are nil, t, `head', `first', `last', an integer or a
1185 predicate.  See Info node `(gnus)Customizing Articles'."
1186   :group 'gnus-article-treat
1187   :link '(custom-manual "(gnus)Customizing Articles")
1188   :type gnus-article-treat-custom)
1189
1190 (defcustom gnus-treat-fill-article nil
1191   "Fill the article.
1192 Valid values are nil, t, `head', `first', `last', an integer or a
1193 predicate.  See Info node `(gnus)Customizing Articles'."
1194   :group 'gnus-article-treat
1195   :link '(custom-manual "(gnus)Customizing Articles")
1196   :type gnus-article-treat-custom)
1197
1198 (defcustom gnus-treat-hide-citation nil
1199   "Hide cited text.
1200 Valid values are nil, t, `head', `first', `last', an integer or a
1201 predicate.  See Info node `(gnus)Customizing Articles'."
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-custom)
1205
1206 (defcustom gnus-treat-hide-citation-maybe nil
1207   "Hide cited text.
1208 Valid values are nil, t, `head', `first', `last', an integer or a
1209 predicate.  See Info node `(gnus)Customizing Articles'."
1210   :group 'gnus-article-treat
1211   :link '(custom-manual "(gnus)Customizing Articles")
1212   :type gnus-article-treat-custom)
1213
1214 (defcustom gnus-treat-strip-list-identifiers 'head
1215   "Strip list identifiers from `gnus-list-identifiers`.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :version "21.1"
1219   :group 'gnus-article-treat
1220   :link '(custom-manual "(gnus)Customizing Articles")
1221   :type gnus-article-treat-custom)
1222
1223 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1224                         "Gnus 5.10 (Emacs 22.1)")
1225
1226 (defcustom gnus-treat-strip-pem nil
1227   "Strip PEM signatures.
1228 Valid values are nil, t, `head', `first', `last', an integer or a
1229 predicate.  See Info node `(gnus)Customizing Articles'."
1230   :group 'gnus-article-treat
1231   :link '(custom-manual "(gnus)Customizing Articles")
1232   :type gnus-article-treat-custom)
1233
1234 (defcustom gnus-treat-strip-banner t
1235   "Strip banners from articles.
1236 The banner to be stripped is specified in the `banner' group parameter.
1237 Valid values are nil, t, `head', `first', `last', an integer or a
1238 predicate.  See Info node `(gnus)Customizing Articles'."
1239   :group 'gnus-article-treat
1240   :link '(custom-manual "(gnus)Customizing Articles")
1241   :type gnus-article-treat-custom)
1242
1243 (defcustom gnus-treat-highlight-headers 'head
1244   "Highlight the headers.
1245 Valid values are nil, t, `head', `first', `last', an integer or a
1246 predicate.  See Info node `(gnus)Customizing Articles'."
1247   :group 'gnus-article-treat
1248   :link '(custom-manual "(gnus)Customizing Articles")
1249   :type gnus-article-treat-head-custom)
1250 (put 'gnus-treat-highlight-headers 'highlight t)
1251
1252 (defcustom gnus-treat-highlight-citation t
1253   "Highlight cited text.
1254 Valid values are nil, t, `head', `first', `last', an integer or a
1255 predicate.  See Info node `(gnus)Customizing Articles'."
1256   :group 'gnus-article-treat
1257   :link '(custom-manual "(gnus)Customizing Articles")
1258   :type gnus-article-treat-custom)
1259 (put 'gnus-treat-highlight-citation 'highlight t)
1260
1261 (defcustom gnus-treat-date-ut nil
1262   "Display the Date in UT (GMT).
1263 Valid values are nil, t, `head', `first', `last', an integer or a
1264 predicate.  See Info node `(gnus)Customizing Articles'."
1265   :group 'gnus-article-treat
1266   :link '(custom-manual "(gnus)Customizing Articles")
1267   :type gnus-article-treat-head-custom)
1268
1269 (defcustom gnus-treat-date-local nil
1270   "Display the Date in the local timezone.
1271 Valid values are nil, t, `head', `first', `last', an integer or a
1272 predicate.  See Info node `(gnus)Customizing Articles'."
1273   :group 'gnus-article-treat
1274   :link '(custom-manual "(gnus)Customizing Articles")
1275   :type gnus-article-treat-head-custom)
1276
1277 (defcustom gnus-treat-date-english nil
1278   "Display the Date in a format that can be read aloud in English.
1279 Valid values are nil, t, `head', `first', `last', an integer or a
1280 predicate.  See Info node `(gnus)Customizing Articles'."
1281   :version "22.1"
1282   :group 'gnus-article-treat
1283   :link '(custom-manual "(gnus)Customizing Articles")
1284   :type gnus-article-treat-head-custom)
1285
1286 (defcustom gnus-treat-date-lapsed nil
1287   "Display the Date header in a way that says how much time has elapsed.
1288 Valid values are nil, t, `head', `first', `last', an integer or a
1289 predicate.  See Info node `(gnus)Customizing Articles'."
1290   :group 'gnus-article-treat
1291   :link '(custom-manual "(gnus)Customizing Articles")
1292   :type gnus-article-treat-head-custom)
1293
1294 (defcustom gnus-treat-date-original nil
1295   "Display the date in the original timezone.
1296 Valid values are nil, t, `head', `first', `last', an integer or a
1297 predicate.  See Info node `(gnus)Customizing Articles'."
1298   :group 'gnus-article-treat
1299   :link '(custom-manual "(gnus)Customizing Articles")
1300   :type gnus-article-treat-head-custom)
1301
1302 (defcustom gnus-treat-date-iso8601 nil
1303   "Display the date in the ISO8601 format.
1304 Valid values are nil, t, `head', `first', `last', an integer or a
1305 predicate.  See Info node `(gnus)Customizing Articles'."
1306   :version "21.1"
1307   :group 'gnus-article-treat
1308   :link '(custom-manual "(gnus)Customizing Articles")
1309   :type gnus-article-treat-head-custom)
1310
1311 (defcustom gnus-treat-date-user-defined nil
1312   "Display the date in a user-defined format.
1313 The format is defined by the `gnus-article-time-format' variable.
1314 Valid values are nil, t, `head', `first', `last', an integer or a
1315 predicate.  See Info node `(gnus)Customizing Articles'."
1316   :group 'gnus-article-treat
1317   :link '(custom-manual "(gnus)Customizing Articles")
1318   :type gnus-article-treat-head-custom)
1319
1320 (defcustom gnus-treat-strip-headers-in-body t
1321   "Strip the X-No-Archive header line from the beginning of the body.
1322 Valid values are nil, t, `head', `first', `last', an integer or a
1323 predicate.  See Info node `(gnus)Customizing Articles'."
1324   :version "21.1"
1325   :group 'gnus-article-treat
1326   :link '(custom-manual "(gnus)Customizing Articles")
1327   :type gnus-article-treat-custom)
1328
1329 (defcustom gnus-treat-strip-trailing-blank-lines nil
1330   "Strip trailing blank lines.
1331 Valid values are nil, t, `head', `first', `last', an integer or a
1332 predicate.  See Info node `(gnus)Customizing Articles'.
1333
1334 When set to t, it also strips trailing blanks in all MIME parts.
1335 Consider to use `last' instead."
1336   :group 'gnus-article-treat
1337   :link '(custom-manual "(gnus)Customizing Articles")
1338   :type gnus-article-treat-custom)
1339
1340 (defcustom gnus-treat-strip-leading-blank-lines nil
1341   "Strip leading blank lines.
1342 Valid values are nil, t, `head', `first', `last', an integer or a
1343 predicate.  See Info node `(gnus)Customizing Articles'.
1344
1345 When set to t, it also strips trailing blanks in all MIME parts."
1346   :group 'gnus-article-treat
1347   :link '(custom-manual "(gnus)Customizing Articles")
1348   :type gnus-article-treat-custom)
1349
1350 (defcustom gnus-treat-strip-multiple-blank-lines nil
1351   "Strip multiple blank lines.
1352 Valid values are nil, t, `head', `first', `last', an integer or a
1353 predicate.  See Info node `(gnus)Customizing Articles'."
1354   :group 'gnus-article-treat
1355   :link '(custom-manual "(gnus)Customizing Articles")
1356   :type gnus-article-treat-custom)
1357
1358 (defcustom gnus-treat-unfold-headers 'head
1359   "Unfold folded header lines.
1360 Valid values are nil, t, `head', `first', `last', an integer or a
1361 predicate.  See Info node `(gnus)Customizing Articles'."
1362   :version "22.1"
1363   :group 'gnus-article-treat
1364   :link '(custom-manual "(gnus)Customizing Articles")
1365   :type gnus-article-treat-custom)
1366
1367 (defcustom gnus-article-unfold-long-headers nil
1368   "If non-nil, allow unfolding headers even if the header is long.
1369 If it is a regexp, only long headers matching this regexp are unfolded.
1370 If it is t, all long headers are unfolded.
1371
1372 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1373   :version "23.1" ;; No Gnus
1374   :group 'gnus-article-treat
1375   :type '(choice (const nil)
1376                  (const :tag "all" t)
1377                  (regexp)))
1378
1379 (defcustom gnus-treat-fold-headers nil
1380   "Fold headers.
1381 Valid values are nil, t, `head', `first', `last', an integer or a
1382 predicate.  See Info node `(gnus)Customizing Articles'."
1383   :version "22.1"
1384   :group 'gnus-article-treat
1385   :link '(custom-manual "(gnus)Customizing Articles")
1386   :type gnus-article-treat-custom)
1387
1388 (defcustom gnus-treat-fold-newsgroups 'head
1389   "Fold the Newsgroups and Followup-To headers.
1390 Valid values are nil, t, `head', `first', `last', an integer or a
1391 predicate.  See Info node `(gnus)Customizing Articles'."
1392   :version "22.1"
1393   :group 'gnus-article-treat
1394   :link '(custom-manual "(gnus)Customizing Articles")
1395   :type gnus-article-treat-custom)
1396
1397 (defcustom gnus-treat-overstrike t
1398   "Treat overstrike highlighting.
1399 Valid values are nil, t, `head', `first', `last', an integer or a
1400 predicate.  See Info node `(gnus)Customizing Articles'."
1401   :group 'gnus-article-treat
1402   :link '(custom-manual "(gnus)Customizing Articles")
1403   :type gnus-article-treat-custom)
1404 (put 'gnus-treat-overstrike 'highlight t)
1405
1406 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1407   "Treat ANSI SGR control sequences.
1408 Valid values are nil, t, `head', `first', `last', an integer or a
1409 predicate.  See Info node `(gnus)Customizing Articles'."
1410   :group 'gnus-article-treat
1411   :link '(custom-manual "(gnus)Customizing Articles")
1412   :type gnus-article-treat-custom)
1413
1414 (make-obsolete-variable 'gnus-treat-display-xface
1415                         'gnus-treat-display-x-face "Emacs 22.1")
1416
1417 (defcustom gnus-treat-display-x-face
1418   (and (not noninteractive)
1419        (gnus-image-type-available-p 'xbm)
1420        (if (featurep 'xemacs)
1421            (featurep 'xface)
1422          (condition-case nil
1423              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1424                   (executable-find "icontopbm"))
1425            ;; shell-command-to-string may signal an error, e.g. if
1426            ;; shell-file-name is not found.
1427            (error nil)))
1428        'head)
1429   "Display X-Face headers.
1430 Valid values are nil and `head'.
1431 See Info node `(gnus)Customizing Articles' and Info node
1432 `(gnus)X-Face' for details."
1433   :group 'gnus-article-treat
1434   :version "21.1"
1435   :link '(custom-manual "(gnus)Customizing Articles")
1436   :link '(custom-manual "(gnus)X-Face")
1437   :type gnus-article-treat-head-custom
1438   :set (lambda (symbol value)
1439          (set-default
1440           symbol
1441           (cond ((or (boundp symbol) (get symbol 'saved-value))
1442                  value)
1443                 ((boundp 'gnus-treat-display-xface)
1444                  (message "\
1445 ** gnus-treat-display-xface is an obsolete variable;\
1446  use gnus-treat-display-x-face instead")
1447                  (default-value 'gnus-treat-display-xface))
1448                 ((get 'gnus-treat-display-xface 'saved-value)
1449                  (message "\
1450 ** gnus-treat-display-xface is an obsolete variable;\
1451  use gnus-treat-display-x-face instead")
1452                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1453                 (t
1454                  value)))))
1455 (put 'gnus-treat-display-x-face 'highlight t)
1456
1457 (defcustom gnus-treat-display-face
1458   (and (not noninteractive)
1459        (gnus-image-type-available-p 'png)
1460        'head)
1461   "Display Face headers.
1462 Valid values are nil, t, `head', `first', `last', an integer or a
1463 predicate.  See Info node `(gnus)Customizing Articles' and Info
1464 node `(gnus)Face' for details."
1465   :group 'gnus-article-treat
1466   :version "22.1"
1467   :link '(custom-manual "(gnus)Customizing Articles")
1468   :link '(custom-manual "(gnus)X-Face")
1469   :type gnus-article-treat-head-custom)
1470 (put 'gnus-treat-display-face 'highlight t)
1471
1472 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1473   "Display smileys.
1474 Valid values are nil, t, `head', `first', `last', an integer or a
1475 predicate.  See Info node `(gnus)Customizing Articles' and Info
1476 node `(gnus)Smileys' for details."
1477   :group 'gnus-article-treat
1478   :version "21.1"
1479   :link '(custom-manual "(gnus)Customizing Articles")
1480   :link '(custom-manual "(gnus)Smileys")
1481   :type gnus-article-treat-custom)
1482 (put 'gnus-treat-display-smileys 'highlight t)
1483
1484 (defcustom gnus-treat-from-picon
1485   (if (and (gnus-image-type-available-p 'xpm)
1486            (gnus-picons-installed-p))
1487       'head nil)
1488   "Display picons in the From header.
1489 Valid values are nil, t, `head', `first', `last', an integer or a
1490 predicate.  See Info node `(gnus)Customizing Articles' and Info
1491 node `(gnus)Picons' for details."
1492   :version "22.1"
1493   :group 'gnus-article-treat
1494   :group 'gnus-picon
1495   :link '(custom-manual "(gnus)Customizing Articles")
1496   :link '(custom-manual "(gnus)Picons")
1497   :type gnus-article-treat-head-custom)
1498 (put 'gnus-treat-from-picon 'highlight t)
1499
1500 (defcustom gnus-treat-mail-picon
1501   (if (and (gnus-image-type-available-p 'xpm)
1502            (gnus-picons-installed-p))
1503       'head nil)
1504   "Display picons in To and Cc headers.
1505 Valid values are nil, t, `head', `first', `last', an integer or a
1506 predicate.  See Info node `(gnus)Customizing Articles' and Info
1507 node `(gnus)Picons' for details."
1508   :version "22.1"
1509   :group 'gnus-article-treat
1510   :group 'gnus-picon
1511   :link '(custom-manual "(gnus)Customizing Articles")
1512   :link '(custom-manual "(gnus)Picons")
1513   :type gnus-article-treat-head-custom)
1514 (put 'gnus-treat-mail-picon 'highlight t)
1515
1516 (defcustom gnus-treat-newsgroups-picon
1517   (if (and (gnus-image-type-available-p 'xpm)
1518            (gnus-picons-installed-p))
1519       'head nil)
1520   "Display picons in the Newsgroups and Followup-To headers.
1521 Valid values are nil, t, `head', `first', `last', an integer or a
1522 predicate.  See Info node `(gnus)Customizing Articles' and Info
1523 node `(gnus)Picons' for details."
1524   :version "22.1"
1525   :group 'gnus-article-treat
1526   :group 'gnus-picon
1527   :link '(custom-manual "(gnus)Customizing Articles")
1528   :link '(custom-manual "(gnus)Picons")
1529   :type gnus-article-treat-head-custom)
1530 (put 'gnus-treat-newsgroups-picon 'highlight t)
1531
1532 (defcustom gnus-treat-from-gravatar nil
1533   "Display gravatars in the From header.
1534 Valid values are nil, t, `head', `first', `last', an integer or a
1535 predicate.  See Info node `(gnus)Customizing Articles' and Info
1536 node `(gnus)Gravatars' for details."
1537   :version "24.1"
1538   :group 'gnus-article-treat
1539   :group 'gnus-gravatar
1540   :link '(custom-manual "(gnus)Customizing Articles")
1541   :link '(custom-manual "(gnus)Gravatars")
1542   :type gnus-article-treat-head-custom)
1543 (put 'gnus-treat-from-gravatar 'highlight t)
1544
1545 (defcustom gnus-treat-mail-gravatar nil
1546   "Display gravatars in To and Cc headers.
1547 Valid values are nil, t, `head', `first', `last', an integer or a
1548 predicate.  See Info node `(gnus)Customizing Articles' and Info
1549 node `(gnus)Gravatars' for details."
1550   :version "24.1"
1551   :group 'gnus-article-treat
1552   :group 'gnus-gravatar
1553   :link '(custom-manual "(gnus)Customizing Articles")
1554   :link '(custom-manual "(gnus)Gravatars")
1555   :type gnus-article-treat-head-custom)
1556 (put 'gnus-treat-mail-gravatar 'highlight t)
1557
1558 (defcustom gnus-treat-body-boundary
1559   (if (or gnus-treat-newsgroups-picon
1560           gnus-treat-mail-picon
1561           gnus-treat-from-picon
1562           gnus-treat-from-gravatar
1563           gnus-treat-mail-gravatar)
1564       ;; If there's much decoration, the user might prefer a boundery.
1565       'head
1566     nil)
1567   "Draw a boundary at the end of the headers.
1568 Valid values are nil and `head'.
1569 See Info node `(gnus)Customizing Articles' for details."
1570   :version "22.1"
1571   :group 'gnus-article-treat
1572   :link '(custom-manual "(gnus)Customizing Articles")
1573   :type gnus-article-treat-head-custom)
1574
1575 (defcustom gnus-treat-capitalize-sentences nil
1576   "Capitalize sentence-starting words.
1577 Valid values are nil, t, `head', `first', `last', an integer or a
1578 predicate.  See Info node `(gnus)Customizing Articles'."
1579   :version "21.1"
1580   :group 'gnus-article-treat
1581   :link '(custom-manual "(gnus)Customizing Articles")
1582   :type gnus-article-treat-custom)
1583
1584 (defcustom gnus-treat-wash-html nil
1585   "Format as HTML.
1586 Valid values are nil, t, `head', `first', `last', an integer or a
1587 predicate.  See Info node `(gnus)Customizing Articles'."
1588   :version "22.1"
1589   :group 'gnus-article-treat
1590   :link '(custom-manual "(gnus)Customizing Articles")
1591   :type gnus-article-treat-custom)
1592
1593 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1594   "Fill long lines.
1595 Valid values are nil, t, `head', `first', `last', an integer or a
1596 predicate.  See Info node `(gnus)Customizing Articles'."
1597   :group 'gnus-article-treat
1598   :link '(custom-manual "(gnus)Customizing Articles")
1599   :type gnus-article-treat-custom)
1600
1601 (defcustom gnus-treat-x-pgp-sig nil
1602   "Verify X-PGP-Sig.
1603 To automatically treat X-PGP-Sig, set it to head.
1604 Valid values are nil, t, `head', `first', `last', an integer or a
1605 predicate.  See Info node `(gnus)Customizing Articles'."
1606   :version "22.1"
1607   :group 'gnus-article-treat
1608   :group 'mime-security
1609   :link '(custom-manual "(gnus)Customizing Articles")
1610   :type gnus-article-treat-custom)
1611
1612 (defvar gnus-article-encrypt-protocol-alist
1613   '(("PGP" . mml2015-self-encrypt)))
1614
1615 ;; Set to nil if more than one protocol added to
1616 ;; gnus-article-encrypt-protocol-alist.
1617 (defcustom gnus-article-encrypt-protocol "PGP"
1618   "The protocol used for encrypt articles.
1619 It is a string, such as \"PGP\". If nil, ask user."
1620   :version "22.1"
1621   :type 'string
1622   :group 'mime-security)
1623
1624 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1625                               (mm-coding-system-p 'utf-8)
1626                               (executable-find idna-program))
1627   "Whether IDNA decoding of headers is used when viewing messages.
1628 This requires GNU Libidn, and by default only enabled if it is found."
1629   :version "22.1"
1630   :group 'gnus-article-headers
1631   :type 'boolean)
1632
1633 (defcustom gnus-article-over-scroll nil
1634   "If non-nil, allow scrolling the article buffer even when there no more text."
1635   :version "22.1"
1636   :group 'gnus-article
1637   :type 'boolean)
1638
1639 (defcustom gnus-blocked-images 'gnus-block-private-groups
1640   "Images that have URLs matching this regexp will be blocked.
1641 This can also be a function to be evaluated.  If so, it will be
1642 called with the group name as the parameter, and should return a
1643 regexp."
1644   :version "24.1"
1645   :group 'gnus-art
1646   :type 'regexp)
1647
1648 ;;; Internal variables
1649
1650 (defvar gnus-english-month-names
1651   '("January" "February" "March" "April" "May" "June" "July" "August"
1652     "September" "October" "November" "December"))
1653
1654 (defvar article-goto-body-goes-to-point-min-p nil)
1655 (defvar gnus-article-wash-types nil)
1656 (defvar gnus-article-emphasis-alist nil)
1657 (defvar gnus-article-image-alist nil)
1658
1659 (defvar gnus-article-mime-handle-alist-1 nil)
1660 (defvar gnus-treatment-function-alist
1661   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1662     (gnus-treat-strip-banner gnus-article-strip-banner)
1663     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1664     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1665     (gnus-treat-buttonize gnus-article-add-buttons)
1666     (gnus-treat-fill-article gnus-article-fill-cited-article)
1667     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1668     (gnus-treat-strip-cr gnus-article-remove-cr)
1669     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1670     (gnus-treat-date-ut gnus-article-date-ut)
1671     (gnus-treat-date-local gnus-article-date-local)
1672     (gnus-treat-date-english gnus-article-date-english)
1673     (gnus-treat-date-original gnus-article-date-original)
1674     (gnus-treat-date-user-defined gnus-article-date-user)
1675     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1676     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1677     (gnus-treat-display-x-face gnus-article-display-x-face)
1678     (gnus-treat-display-face gnus-article-display-face)
1679     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1680     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1681     (gnus-treat-hide-signature gnus-article-hide-signature)
1682     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1683     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1684     (gnus-treat-from-picon gnus-treat-from-picon)
1685     (gnus-treat-mail-picon gnus-treat-mail-picon)
1686     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1687     (gnus-treat-strip-pem gnus-article-hide-pem)
1688     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1689     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1690     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1691     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1692     (gnus-treat-strip-trailing-blank-lines
1693      gnus-article-remove-trailing-blank-lines)
1694     (gnus-treat-strip-leading-blank-lines
1695      gnus-article-strip-leading-blank-lines)
1696     (gnus-treat-strip-multiple-blank-lines
1697      gnus-article-strip-multiple-blank-lines)
1698     (gnus-treat-overstrike gnus-article-treat-overstrike)
1699     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1700     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1701     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1702     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1703     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1704     (gnus-treat-display-smileys gnus-treat-smiley)
1705     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1706     (gnus-treat-wash-html gnus-article-wash-html)
1707     (gnus-treat-emphasize gnus-article-emphasize)
1708     (gnus-treat-hide-citation gnus-article-hide-citation)
1709     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1710     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1711     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1712
1713 (defvar gnus-article-mime-handle-alist nil)
1714 (defvar article-lapsed-timer nil)
1715 (defvar gnus-article-current-summary nil)
1716
1717 (defvar gnus-article-mode-syntax-table
1718   (let ((table (copy-syntax-table text-mode-syntax-table)))
1719     ;; This causes the citation match run O(2^n).
1720     ;; (modify-syntax-entry ?- "w" table)
1721     (modify-syntax-entry ?> ")<" table)
1722     (modify-syntax-entry ?< "(>" table)
1723     ;; make M-. in article buffers work for `foo' strings
1724     (modify-syntax-entry ?' " " table)
1725     (modify-syntax-entry ?` " " table)
1726     table)
1727   "Syntax table used in article mode buffers.
1728 Initialized from `text-mode-syntax-table.")
1729
1730 (defvar gnus-save-article-buffer nil)
1731
1732 (defvar gnus-number-of-articles-to-be-saved nil)
1733
1734 (defvar gnus-inhibit-hiding nil)
1735
1736 (defvar gnus-article-edit-mode nil)
1737
1738 ;;; Macros for dealing with the article buffer.
1739
1740 (defmacro gnus-with-article-headers (&rest forms)
1741   `(with-current-buffer gnus-article-buffer
1742      (save-restriction
1743        (let ((inhibit-read-only t)
1744              (inhibit-point-motion-hooks t)
1745              (case-fold-search t))
1746          (article-narrow-to-head)
1747          ,@forms))))
1748
1749 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1750 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1751
1752 (defmacro gnus-with-article-buffer (&rest forms)
1753   `(with-current-buffer gnus-article-buffer
1754      (let ((inhibit-read-only t))
1755        ,@forms)))
1756
1757 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1758 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1759
1760 (defun gnus-article-goto-header (header)
1761   "Go to HEADER, which is a regular expression."
1762   (re-search-forward (concat "^\\(" header "\\):") nil t))
1763
1764 (defsubst gnus-article-hide-text (b e props)
1765   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1766   (gnus-add-text-properties-when 'article-type nil b e props)
1767   (when (memq 'intangible props)
1768     (put-text-property
1769      (max (1- b) (point-min))
1770      b 'intangible (cddr (memq 'intangible props)))))
1771
1772 (defsubst gnus-article-unhide-text (b e)
1773   "Remove hidden text properties from region between B and E."
1774   (remove-text-properties b e gnus-hidden-properties)
1775   (when (memq 'intangible gnus-hidden-properties)
1776     (put-text-property (max (1- b) (point-min))
1777                        b 'intangible nil)))
1778
1779 (defun gnus-article-hide-text-type (b e type)
1780   "Hide text of TYPE between B and E."
1781   (gnus-add-wash-type type)
1782   (gnus-article-hide-text
1783    b e (cons 'article-type (cons type gnus-hidden-properties))))
1784
1785 (defun gnus-article-unhide-text-type (b e type)
1786   "Unhide text of TYPE between B and E."
1787   (gnus-delete-wash-type type)
1788   (remove-text-properties
1789    b e (cons 'article-type (cons type gnus-hidden-properties)))
1790   (when (memq 'intangible gnus-hidden-properties)
1791     (put-text-property (max (1- b) (point-min))
1792                        b 'intangible nil)))
1793
1794 (defun gnus-article-hide-text-of-type (type)
1795   "Hide text of TYPE in the current buffer."
1796   (save-excursion
1797     (let ((b (point-min))
1798           (e (point-max)))
1799       (while (setq b (text-property-any b e 'article-type type))
1800         (add-text-properties b (incf b) gnus-hidden-properties)))))
1801
1802 (defun gnus-article-delete-text-of-type (type)
1803   "Delete text of TYPE in the current buffer."
1804   (save-excursion
1805     (let ((b (point-min)))
1806       (if (eq type 'multipart)
1807           ;; Remove MIME buttons associated with multipart/alternative parts.
1808           (progn
1809             (goto-char b)
1810             (while (if (get-text-property (point) 'gnus-part)
1811                        (setq b (point))
1812                      (when (setq b (next-single-property-change (point)
1813                                                                 'gnus-part))
1814                        (goto-char b)
1815                        t))
1816               (end-of-line)
1817               (skip-chars-forward "\n")
1818               (when (eq (get-text-property b 'article-type) 'multipart)
1819                 (delete-region b (point)))))
1820         (while (setq b (text-property-any b (point-max) 'article-type type))
1821           (delete-region
1822            b (or (text-property-not-all b (point-max) 'article-type type)
1823                  (point-max))))))))
1824
1825 (defun gnus-article-delete-invisible-text ()
1826   "Delete all invisible text in the current buffer."
1827   (save-excursion
1828     (let ((b (point-min)))
1829       (while (setq b (text-property-any b (point-max) 'invisible t))
1830         (delete-region
1831          b (or (text-property-not-all b (point-max) 'invisible t)
1832                (point-max)))))))
1833
1834 (defun gnus-article-text-type-exists-p (type)
1835   "Say whether any text of type TYPE exists in the buffer."
1836   (text-property-any (point-min) (point-max) 'article-type type))
1837
1838 (defsubst gnus-article-header-rank ()
1839   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1840   (let ((list gnus-sorted-header-list)
1841         (i 1))
1842     (while list
1843       (if (looking-at (car list))
1844           (setq list nil)
1845         (setq list (cdr list))
1846         (incf i)))
1847       i))
1848
1849 (defun article-hide-headers (&optional arg delete)
1850   "Hide unwanted headers and possibly sort them as well."
1851   (interactive)
1852   ;; This function might be inhibited.
1853   (unless gnus-inhibit-hiding
1854     (let ((inhibit-read-only t)
1855           (case-fold-search t)
1856           (max (1+ (length gnus-sorted-header-list)))
1857           (inhibit-point-motion-hooks t)
1858           (cur (current-buffer))
1859           ignored visible beg)
1860       (save-excursion
1861         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1862         ;; group parameters, so we should go to the summary buffer.
1863         (when (prog1
1864                   (condition-case nil
1865                       (progn (set-buffer gnus-summary-buffer) t)
1866                     (error nil))
1867                 (setq ignored (when (not gnus-visible-headers)
1868                                 (cond ((stringp gnus-ignored-headers)
1869                                        gnus-ignored-headers)
1870                                       ((listp gnus-ignored-headers)
1871                                        (mapconcat 'identity
1872                                                   gnus-ignored-headers
1873                                                   "\\|"))))
1874                       visible (cond ((stringp gnus-visible-headers)
1875                                      gnus-visible-headers)
1876                                     ((and gnus-visible-headers
1877                                           (listp gnus-visible-headers))
1878                                      (mapconcat 'identity
1879                                                 gnus-visible-headers
1880                                                 "\\|")))))
1881           (set-buffer cur))
1882         (save-restriction
1883           ;; First we narrow to just the headers.
1884           (article-narrow-to-head)
1885           ;; Hide any "From " lines at the beginning of (mail) articles.
1886           (while (looking-at "From ")
1887             (forward-line 1))
1888           (unless (bobp)
1889             (delete-region (point-min) (point)))
1890           ;; Then treat the rest of the header lines.
1891           ;; Then we use the two regular expressions
1892           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1893           ;; select which header lines is to remain visible in the
1894           ;; article buffer.
1895           (while (re-search-forward "^[^ \t:]*:" nil t)
1896             (beginning-of-line)
1897             ;; Mark the rank of the header.
1898             (put-text-property
1899              (point) (1+ (point)) 'message-rank
1900              (if (or (and visible (looking-at visible))
1901                      (and ignored
1902                           (not (looking-at ignored))))
1903                  (gnus-article-header-rank)
1904                (+ 2 max)))
1905             (forward-line 1))
1906           (message-sort-headers-1)
1907           (when (setq beg (text-property-any
1908                            (point-min) (point-max) 'message-rank (+ 2 max)))
1909             ;; We delete the unwanted headers.
1910             (gnus-add-wash-type 'headers)
1911             (add-text-properties (point-min) (+ 5 (point-min))
1912                                  '(article-type headers dummy-invisible t))
1913             (delete-region beg (point-max))))))))
1914
1915 (defun article-hide-boring-headers (&optional arg)
1916   "Toggle hiding of headers that aren't very interesting.
1917 If given a negative prefix, always show; if given a positive prefix,
1918 always hide."
1919   (interactive (gnus-article-hidden-arg))
1920   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1921              (not gnus-show-all-headers))
1922     (save-excursion
1923       (save-restriction
1924         (let ((inhibit-read-only t)
1925               (inhibit-point-motion-hooks t))
1926           (article-narrow-to-head)
1927           (dolist (elem gnus-boring-article-headers)
1928             (goto-char (point-min))
1929             (cond
1930              ;; Hide empty headers.
1931              ((eq elem 'empty)
1932               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1933                 (forward-line -1)
1934                 (gnus-article-hide-text-type
1935                  (point-at-bol)
1936                  (progn
1937                    (end-of-line)
1938                    (if (re-search-forward "^[^ \t]" nil t)
1939                        (match-beginning 0)
1940                      (point-max)))
1941                  'boring-headers)))
1942              ;; Hide boring Newsgroups header.
1943              ((eq elem 'newsgroups)
1944               (when (gnus-string-equal
1945                      (gnus-fetch-field "newsgroups")
1946                      (gnus-group-real-name
1947                       (if (boundp 'gnus-newsgroup-name)
1948                           gnus-newsgroup-name
1949                         "")))
1950                 (gnus-article-hide-header "newsgroups")))
1951              ((eq elem 'to-address)
1952               (let ((to (message-fetch-field "to"))
1953                     (to-address
1954                      (gnus-parameter-to-address
1955                       (if (boundp 'gnus-newsgroup-name)
1956                           gnus-newsgroup-name ""))))
1957                 (when (and to to-address
1958                            (ignore-errors
1959                              (gnus-string-equal
1960                               ;; only one address in To
1961                               (nth 1 (mail-extract-address-components to))
1962                               to-address)))
1963                   (gnus-article-hide-header "to"))))
1964              ((eq elem 'to-list)
1965               (let ((to (message-fetch-field "to"))
1966                     (to-list
1967                      (gnus-parameter-to-list
1968                       (if (boundp 'gnus-newsgroup-name)
1969                           gnus-newsgroup-name ""))))
1970                 (when (and to to-list
1971                            (ignore-errors
1972                              (gnus-string-equal
1973                               ;; only one address in To
1974                               (nth 1 (mail-extract-address-components to))
1975                               to-list)))
1976                   (gnus-article-hide-header "to"))))
1977              ((eq elem 'cc-list)
1978               (let ((cc (message-fetch-field "cc"))
1979                     (to-list
1980                      (gnus-parameter-to-list
1981                       (if (boundp 'gnus-newsgroup-name)
1982                           gnus-newsgroup-name ""))))
1983                 (when (and cc to-list
1984                            (ignore-errors
1985                              (gnus-string-equal
1986                               ;; only one address in CC
1987                               (nth 1 (mail-extract-address-components cc))
1988                               to-list)))
1989                   (gnus-article-hide-header "cc"))))
1990              ((eq elem 'followup-to)
1991               (when (gnus-string-equal
1992                      (message-fetch-field "followup-to")
1993                      (message-fetch-field "newsgroups"))
1994                 (gnus-article-hide-header "followup-to")))
1995              ((eq elem 'reply-to)
1996               (if (gnus-group-find-parameter
1997                    gnus-newsgroup-name 'broken-reply-to)
1998                   (gnus-article-hide-header "reply-to")
1999                 (let ((from (message-fetch-field "from"))
2000                       (reply-to (message-fetch-field "reply-to")))
2001                   (when
2002                       (and
2003                        from reply-to
2004                        (ignore-errors
2005                          (equal
2006                           (sort (mapcar
2007                                  (lambda (x) (downcase (cadr x)))
2008                                  (mail-extract-address-components from t))
2009                                 'string<)
2010                           (sort (mapcar
2011                                  (lambda (x) (downcase (cadr x)))
2012                                  (mail-extract-address-components reply-to t))
2013                                 'string<))))
2014                     (gnus-article-hide-header "reply-to")))))
2015              ((eq elem 'date)
2016               (let ((date (with-current-buffer gnus-original-article-buffer
2017                             ;; If date in `gnus-article-buffer' is localized
2018                             ;; (`gnus-treat-date-user-defined'),
2019                             ;; `days-between' might fail.
2020                             (message-fetch-field "date"))))
2021                 (when (and date
2022                            (< (days-between (current-time-string) date)
2023                               4))
2024                   (gnus-article-hide-header "date"))))
2025              ((eq elem 'long-to)
2026               (let ((to (message-fetch-field "to"))
2027                     (cc (message-fetch-field "cc")))
2028                 (when (> (length to) 1024)
2029                   (gnus-article-hide-header "to"))
2030                 (when (> (length cc) 1024)
2031                   (gnus-article-hide-header "cc"))))
2032              ((eq elem 'many-to)
2033               (let ((to-count 0)
2034                     (cc-count 0))
2035                 (goto-char (point-min))
2036                 (while (re-search-forward "^to:" nil t)
2037                   (setq to-count (1+ to-count)))
2038                 (when (> to-count 1)
2039                   (while (> to-count 0)
2040                     (goto-char (point-min))
2041                     (save-restriction
2042                       (re-search-forward "^to:" nil nil to-count)
2043                       (forward-line -1)
2044                       (narrow-to-region (point) (point-max))
2045                       (gnus-article-hide-header "to"))
2046                     (setq to-count (1- to-count))))
2047                 (goto-char (point-min))
2048                 (while (re-search-forward "^cc:" nil t)
2049                   (setq cc-count (1+ cc-count)))
2050                 (when (> cc-count 1)
2051                   (while (> cc-count 0)
2052                     (goto-char (point-min))
2053                     (save-restriction
2054                       (re-search-forward "^cc:" nil nil cc-count)
2055                       (forward-line -1)
2056                       (narrow-to-region (point) (point-max))
2057                       (gnus-article-hide-header "cc"))
2058                     (setq cc-count (1- cc-count)))))))))))))
2059
2060 (defun gnus-article-hide-header (header)
2061   (save-excursion
2062     (goto-char (point-min))
2063     (when (re-search-forward (concat "^" header ":") nil t)
2064       (gnus-article-hide-text-type
2065        (point-at-bol)
2066        (progn
2067          (end-of-line)
2068          (if (re-search-forward "^[^ \t]" nil t)
2069              (match-beginning 0)
2070            (point-max)))
2071        'boring-headers))))
2072
2073 (defvar gnus-article-normalized-header-length 40
2074   "Length of normalized headers.")
2075
2076 (defun article-normalize-headers ()
2077   "Make all header lines 40 characters long."
2078   (interactive)
2079   (let ((inhibit-read-only t)
2080         column)
2081     (save-excursion
2082       (save-restriction
2083         (article-narrow-to-head)
2084         (while (not (eobp))
2085           (cond
2086            ((< (setq column (- (point-at-eol) (point)))
2087                gnus-article-normalized-header-length)
2088             (end-of-line)
2089             (insert (make-string
2090                      (- gnus-article-normalized-header-length column)
2091                      ? )))
2092            ((> column gnus-article-normalized-header-length)
2093             (gnus-put-text-property
2094              (progn
2095                (forward-char gnus-article-normalized-header-length)
2096                (point))
2097              (point-at-eol)
2098              'invisible t))
2099            (t
2100             ;; Do nothing.
2101             ))
2102           (forward-line 1))))))
2103
2104 (defun article-treat-dumbquotes ()
2105   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2106 Note that this function guesses whether a character is a sm*rtq**t* or
2107 not, so it should only be used interactively.
2108
2109 Sm*rtq**t*s are M****s***'s unilateral extension to the
2110 iso-8859-1 character map in an attempt to provide more quoting
2111 characters.  If you see something like \\222 or \\264 where
2112 you're expecting some kind of apostrophe or quotation mark, then
2113 try this wash."
2114   (interactive)
2115   (article-translate-strings gnus-article-dumbquotes-map))
2116
2117 (defun article-treat-non-ascii ()
2118   "Translate many Unicode characters into their ASCII equivalents."
2119   (interactive)
2120   (require 'org-entities)
2121   (let ((table (make-char-table nil)))
2122     (dolist (elem org-entities)
2123       (when (and (listp elem)
2124                  (= (length (nth 6 elem)) 1))
2125         (set-char-table-range table
2126                               (aref (nth 6 elem) 0)
2127                               (nth 4 elem))))
2128     (save-excursion
2129       (when (article-goto-body)
2130         (let ((inhibit-read-only t)
2131               replace)
2132           (while (not (eobp))
2133             (if (not (setq replace (aref table (following-char))))
2134                 (forward-char 1)
2135               (delete-char 1)
2136               (insert replace))))))))
2137
2138 (defun article-translate-characters (from to)
2139   "Translate all characters in the body of the article according to FROM and TO.
2140 FROM is a string of characters to translate from; to is a string of
2141 characters to translate to."
2142   (save-excursion
2143     (when (article-goto-body)
2144       (let ((inhibit-read-only t)
2145             (x (make-string 225 ?x))
2146             (i -1))
2147         (while (< (incf i) (length x))
2148           (aset x i i))
2149         (setq i 0)
2150         (while (< i (length from))
2151           (aset x (aref from i) (aref to i))
2152           (incf i))
2153         (translate-region (point) (point-max) x)))))
2154
2155 (defun article-translate-strings (map)
2156   "Translate all string in the body of the article according to MAP.
2157 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2158   (save-excursion
2159     (when (article-goto-body)
2160       (let ((inhibit-read-only t))
2161         (dolist (elem map)
2162           (let ((from (car elem))
2163                 (to (cadr elem)))
2164             (save-excursion
2165               (if (stringp from)
2166                   (while (search-forward from nil t)
2167                     (replace-match to))
2168                 (while (not (eobp))
2169                   (if (eq (following-char) from)
2170                       (progn
2171                         (delete-char 1)
2172                         (insert to))
2173                     (forward-char 1)))))))))))
2174
2175 (defun article-treat-overstrike ()
2176   "Translate overstrikes into bold text."
2177   (interactive)
2178   (save-excursion
2179     (when (article-goto-body)
2180       (let ((inhibit-read-only t))
2181         (while (search-forward "\b" nil t)
2182           (let ((next (char-after))
2183                 (previous (char-after (- (point) 2))))
2184             ;; We do the boldification/underlining by hiding the
2185             ;; overstrikes and putting the proper text property
2186             ;; on the letters.
2187             (cond
2188              ((eq next previous)
2189               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2190               (put-text-property (point) (1+ (point)) 'face 'bold))
2191              ((eq next ?_)
2192               (gnus-article-hide-text-type
2193                (1- (point)) (1+ (point)) 'overstrike)
2194               (put-text-property
2195                (- (point) 2) (1- (point)) 'face 'underline))
2196              ((eq previous ?_)
2197               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2198               (put-text-property
2199                (point) (1+ (point)) 'face 'underline)))))))))
2200
2201 (defun article-treat-ansi-sequences ()
2202   "Translate ANSI SGR control sequences into overlays or extents."
2203   (interactive)
2204   (save-excursion
2205     (when (article-goto-body)
2206       (let ((inhibit-read-only t))
2207         (ansi-color-apply-on-region (point) (point-max))))))
2208
2209 (defun gnus-article-treat-unfold-headers ()
2210   "Unfold folded message headers.
2211 Only the headers that fit into the current window width will be
2212 unfolded."
2213   (interactive)
2214   (gnus-with-article-headers
2215     (let (length)
2216       (while (not (eobp))
2217         (save-restriction
2218           (mail-header-narrow-to-field)
2219           (let* ((header (buffer-string))
2220                  (unfoldable
2221                   (or (equal gnus-article-unfold-long-headers t)
2222                       (and (stringp gnus-article-unfold-long-headers)
2223                            (string-match gnus-article-unfold-long-headers header)))))
2224             (with-temp-buffer
2225               (insert header)
2226               (goto-char (point-min))
2227               (while (re-search-forward "\n[\t ]" nil t)
2228                 (replace-match " " t t)))
2229             (setq length (- (point-max) (point-min) 1))
2230             (when (or unfoldable
2231                       (< length (window-width)))
2232               (while (re-search-forward "\n[\t ]" nil t)
2233                 (replace-match " " t t))))
2234           (goto-char (point-max)))))))
2235
2236 (defun gnus-article-treat-fold-headers ()
2237   "Fold message headers."
2238   (interactive)
2239   (gnus-with-article-headers
2240     (while (not (eobp))
2241       (save-restriction
2242         (mail-header-narrow-to-field)
2243         (mail-header-fold-field)
2244         (goto-char (point-max))))))
2245
2246 (defun gnus-treat-smiley ()
2247   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2248   (interactive)
2249   (gnus-with-article-buffer
2250     (if (memq 'smiley gnus-article-wash-types)
2251         (gnus-delete-images 'smiley)
2252       (article-goto-body)
2253       (let ((images (smiley-region (point) (point-max))))
2254         (when images
2255           (gnus-add-wash-type 'smiley)
2256           (dolist (image images)
2257             (gnus-add-image 'smiley image)))))))
2258
2259 (defun gnus-article-remove-images ()
2260   "Remove all images from the article buffer."
2261   (interactive)
2262   (gnus-with-article-buffer
2263     (dolist (elem gnus-article-image-alist)
2264       (gnus-delete-images (car elem)))))
2265
2266 (defun gnus-article-treat-fold-newsgroups ()
2267   "Unfold folded message headers.
2268 Only the headers that fit into the current window width will be
2269 unfolded."
2270   (interactive)
2271   (gnus-with-article-headers
2272     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2273       (save-restriction
2274         (mail-header-narrow-to-field)
2275         (while (re-search-forward ", *" nil t)
2276           (replace-match ", " t t))
2277         (mail-header-fold-field)
2278         (goto-char (point-max))))))
2279
2280 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2281   "Value of `truncate-lines' in Gnus Article buffer.
2282 Valid values are nil, t, `head', `first', `last', an integer or a
2283 predicate.  See Info node `(gnus)Customizing Articles'."
2284   :version "23.1" ;; No Gnus
2285   :group 'gnus-article
2286   ;; :link '(custom-manual "(gnus)Customizing Articles")
2287   :type 'boolean)
2288
2289 (defun gnus-article-toggle-truncate-lines (&optional arg)
2290   "Toggle whether to fold or truncate long lines in article the buffer.
2291 If ARG is non-nil and not a number, toggle
2292 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2293 long lines if and only if arg is positive."
2294   (interactive "P")
2295   (cond
2296    ((and (numberp arg) (> arg 0))
2297     (setq gnus-article-truncate-lines t))
2298    ((numberp arg)
2299     (setq gnus-article-truncate-lines nil))
2300    (arg
2301     (setq gnus-article-truncate-lines
2302           (not gnus-article-truncate-lines))))
2303   (gnus-with-article-buffer
2304     (cond
2305      ((and (numberp arg) (> arg 0))
2306       (setq truncate-lines nil))
2307      ((numberp arg)
2308       (setq truncate-lines t)))
2309     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2310     ;; `toggle-truncate-lines' needs an argument.
2311     (toggle-truncate-lines)))
2312
2313 (defun gnus-article-treat-body-boundary ()
2314   "Place a boundary line at the end of the headers."
2315   (interactive)
2316   (when (and gnus-body-boundary-delimiter
2317              (> (length gnus-body-boundary-delimiter) 0))
2318     (gnus-with-article-headers
2319       (goto-char (point-max))
2320       (let ((start (point)))
2321         (insert "X-Boundary: ")
2322         (gnus-add-text-properties start (point) '(invisible t intangible t))
2323         (insert (let (str)
2324                   (while (>= (window-width) (length str))
2325                     (setq str (concat str gnus-body-boundary-delimiter)))
2326                   (substring str 0 (window-width)))
2327                 "\n")
2328         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2329
2330 (defun article-fill-long-lines ()
2331   "Fill lines that are wider than the window width."
2332   (interactive)
2333   (save-excursion
2334     (let ((inhibit-read-only t)
2335           (width (window-width (get-buffer-window (current-buffer)))))
2336       (save-restriction
2337         (article-goto-body)
2338         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2339           (while (not (eobp))
2340             (end-of-line)
2341             (when (>= (current-column) (min fill-column width))
2342               (narrow-to-region (min (1+ (point)) (point-max))
2343                                 (point-at-bol))
2344               (let ((goback (point-marker)))
2345                 (fill-paragraph nil)
2346                 (goto-char (marker-position goback)))
2347               (widen))
2348             (forward-line 1)))))))
2349
2350 (defun article-capitalize-sentences ()
2351   "Capitalize the first word in each sentence."
2352   (interactive)
2353   (save-excursion
2354     (let ((inhibit-read-only t)
2355           (paragraph-start "^[\n\^L]"))
2356       (article-goto-body)
2357       (while (not (eobp))
2358         (capitalize-word 1)
2359         (forward-sentence)))))
2360
2361 (defun article-remove-cr ()
2362   "Remove trailing CRs and then translate remaining CRs into LFs."
2363   (interactive)
2364   (save-excursion
2365     (let ((inhibit-read-only t))
2366       (goto-char (point-min))
2367       (while (re-search-forward "\r+$" nil t)
2368         (replace-match "" t t))
2369       (goto-char (point-min))
2370       (while (search-forward "\r" nil t)
2371         (replace-match "\n" t t)))))
2372
2373 (defun article-remove-trailing-blank-lines ()
2374   "Remove all trailing blank lines from the article."
2375   (interactive)
2376   (save-excursion
2377     (let ((inhibit-read-only t))
2378       (goto-char (point-max))
2379       (delete-region
2380        (point)
2381        (progn
2382          (while (and (not (bobp))
2383                      (looking-at "^[ \t]*$")
2384                      (not (gnus-annotation-in-region-p
2385                            (point) (point-at-eol))))
2386            (forward-line -1))
2387          (forward-line 1)
2388          (point))))))
2389
2390 (defvar gnus-face-properties-alist)
2391
2392 (defun article-display-face (&optional force)
2393   "Display any Face headers in the header."
2394   (interactive (list 'force))
2395   (let ((wash-face-p buffer-read-only))
2396     (gnus-with-article-headers
2397       ;; When displaying parts, this function can be called several times on
2398       ;; the same article, without any intended toggle semantic (as typing `W
2399       ;; D d' would have). So face deletion must occur only when we come from
2400       ;; an interactive command, that is when the *Article* buffer is
2401       ;; read-only.
2402       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2403           (gnus-delete-images 'face)
2404         (let ((from (message-fetch-field "from"))
2405               face faces)
2406           (save-current-buffer
2407             (when (and wash-face-p
2408                        (gnus-buffer-live-p gnus-original-article-buffer)
2409                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2410               (set-buffer gnus-original-article-buffer))
2411             (save-restriction
2412               (mail-narrow-to-head)
2413               (when (or force
2414                         ;; Check whether this face is censored.
2415                         (not (and gnus-article-x-face-too-ugly
2416                                   (or from
2417                                       (setq from (message-fetch-field "from")))
2418                                   (string-match gnus-article-x-face-too-ugly
2419                                                 from))))
2420                 (while (gnus-article-goto-header "Face")
2421                   (push (mail-header-field-value) faces)))))
2422           (when faces
2423             (goto-char (point-min))
2424             (let (png image)
2425               (unless (setq from (gnus-article-goto-header "from"))
2426                 (insert "From:")
2427                 (setq from (point))
2428                 (insert " [no `from' set]\n"))
2429               (while faces
2430                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2431                   (setq image
2432                         (apply 'gnus-create-image png 'png t
2433                                (cdr (assq 'png gnus-face-properties-alist))))
2434                   (goto-char from)
2435                   (gnus-add-wash-type 'face)
2436                   (gnus-add-image 'face image)
2437                   (gnus-put-image image nil 'face))))))))))
2438
2439 (defun article-display-x-face (&optional force)
2440   "Look for an X-Face header and display it if present."
2441   (interactive (list 'force))
2442   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2443     (gnus-with-article-headers
2444       ;; Delete the old process, if any.
2445       (when (process-status "article-x-face")
2446         (delete-process "article-x-face"))
2447       ;; See the comment in `article-display-face'.
2448       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2449           ;; We have already displayed X-Faces, so we remove them
2450           ;; instead.
2451           (gnus-delete-images 'xface)
2452         ;; Display X-Faces.
2453         (let ((from (message-fetch-field "from"))
2454               x-faces face)
2455           (save-current-buffer
2456             (when (and wash-face-p
2457                        (gnus-buffer-live-p gnus-original-article-buffer)
2458                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2459               ;; If type `W f', use gnus-original-article-buffer,
2460               ;; otherwise use the current buffer because displaying
2461               ;; RFC822 parts calls this function too.
2462               (set-buffer gnus-original-article-buffer))
2463             (save-restriction
2464               (mail-narrow-to-head)
2465               (and gnus-article-x-face-command
2466                    (or force
2467                        ;; Check whether this face is censored.
2468                        (not (and gnus-article-x-face-too-ugly
2469                                  (or from
2470                                      (setq from (message-fetch-field "from")))
2471                                  (string-match gnus-article-x-face-too-ugly
2472                                                from))))
2473                    (while (gnus-article-goto-header "X-Face")
2474                      (push (mail-header-field-value) x-faces)))))
2475           (when x-faces
2476             ;; We display the face.
2477             (cond ((functionp gnus-article-x-face-command)
2478                    ;; The command is a lisp function, so we call it.
2479                    (mapc gnus-article-x-face-command x-faces))
2480                   ((stringp gnus-article-x-face-command)
2481                    ;; The command is a string, so we interpret the command
2482                    ;; as a, well, command, and fork it off.
2483                    (let ((process-connection-type nil))
2484                      (gnus-set-process-query-on-exit-flag
2485                       (start-process
2486                        "article-x-face" nil shell-file-name
2487                        shell-command-switch gnus-article-x-face-command)
2488                       nil)
2489                      ;; Sending multiple EOFs to xv doesn't work,
2490                      ;; so we only do a single external face.
2491                      (with-temp-buffer
2492                        (insert (car x-faces))
2493                        (process-send-region "article-x-face"
2494                                             (point-min) (point-max)))
2495                      (process-send-eof "article-x-face")))
2496                   (t
2497                    (error "`%s' set to `%s' is not a function"
2498                           gnus-article-x-face-command
2499                           'gnus-article-x-face-command)))))))))
2500
2501 (defun article-decode-mime-words ()
2502   "Decode all MIME-encoded words in the article."
2503   (interactive)
2504   (gnus-with-article-buffer
2505     (let ((inhibit-point-motion-hooks t)
2506           (mail-parse-charset gnus-newsgroup-charset)
2507           (mail-parse-ignored-charsets
2508            (with-current-buffer gnus-summary-buffer
2509              gnus-newsgroup-ignored-charsets)))
2510       (mail-decode-encoded-word-region (point-min) (point-max)))))
2511
2512 (defun article-decode-charset (&optional prompt)
2513   "Decode charset-encoded text in the article.
2514 If PROMPT (the prefix), prompt for a coding system to use."
2515   (interactive "P")
2516   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2517         (inhibit-read-only t)
2518         (mail-parse-charset gnus-newsgroup-charset)
2519         (mail-parse-ignored-charsets
2520          (save-excursion (condition-case nil
2521                              (set-buffer gnus-summary-buffer)
2522                            (error))
2523                          gnus-newsgroup-ignored-charsets))
2524         ct cte ctl charset format)
2525     (save-excursion
2526       (save-restriction
2527         (article-narrow-to-head)
2528         (setq ct (message-fetch-field "Content-Type" t)
2529               cte (message-fetch-field "Content-Transfer-Encoding" t)
2530               ctl (and ct (mail-header-parse-content-type ct))
2531               charset (cond
2532                        (prompt
2533                         (mm-read-coding-system "Charset to decode: "))
2534                        (ctl
2535                         (mail-content-type-get ctl 'charset)))
2536               format (and ctl (mail-content-type-get ctl 'format)))
2537         (when cte
2538           (setq cte (mail-header-strip cte)))
2539         (if (and ctl (not (string-match "/" (car ctl))))
2540             (setq ctl nil))
2541         (goto-char (point-max)))
2542       (forward-line 1)
2543       (save-restriction
2544         (narrow-to-region (point) (point-max))
2545         (when (and (eq mail-parse-charset 'gnus-decoded)
2546                    (eq (mm-body-7-or-8) '8bit))
2547           ;; The text code could have been decoded.
2548           (setq charset mail-parse-charset))
2549         (when (and (or (not ctl)
2550                        (equal (car ctl) "text/plain"))
2551                    (not format)) ;; article with format will decode later.
2552           (mm-decode-body
2553            charset (and cte (intern (downcase
2554                                      (gnus-strip-whitespace cte))))
2555            (car ctl)))))))
2556
2557 (defun article-decode-encoded-words ()
2558   "Remove encoded-word encoding from headers."
2559   (let ((inhibit-point-motion-hooks t)
2560         (mail-parse-charset gnus-newsgroup-charset)
2561         (mail-parse-ignored-charsets
2562          (save-excursion (condition-case nil
2563                              (set-buffer gnus-summary-buffer)
2564                            (error))
2565                          gnus-newsgroup-ignored-charsets))
2566         (inhibit-read-only t)
2567         end start)
2568     (goto-char (point-min))
2569     (when (search-forward "\n\n" nil 'move)
2570       (forward-line -1))
2571     (setq end (point))
2572     (while (not (bobp))
2573       (while (progn
2574                (forward-line -1)
2575                (and (not (bobp))
2576                     (memq (char-after) '(?\t ? )))))
2577       (setq start (point))
2578       (if (looking-at "\
2579 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2580 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2581           (funcall gnus-decode-address-function start end)
2582         (funcall gnus-decode-header-function start end))
2583       (goto-char (setq end start)))))
2584
2585 (defun article-decode-group-name ()
2586   "Decode group names in Newsgroups, Followup-To and Xref headers."
2587   (let ((inhibit-point-motion-hooks t)
2588         (inhibit-read-only t)
2589         (method (gnus-find-method-for-group gnus-newsgroup-name))
2590         regexp)
2591     (when (and (or gnus-group-name-charset-method-alist
2592                    gnus-group-name-charset-group-alist)
2593                (gnus-buffer-live-p gnus-original-article-buffer))
2594       (save-restriction
2595         (article-narrow-to-head)
2596         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2597           (with-current-buffer gnus-original-article-buffer
2598             (goto-char (point-min)))
2599           (setq regexp (concat "^" header
2600                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2601           (while (re-search-forward regexp nil t)
2602             (replace-match (save-match-data
2603                              (gnus-decode-newsgroups
2604                               ;; XXX how to use data in article buffer?
2605                               (with-current-buffer gnus-original-article-buffer
2606                                 (re-search-forward regexp nil t)
2607                                 (match-string 1))
2608                               gnus-newsgroup-name method))
2609                            t t nil 1))
2610           (goto-char (point-min)))))))
2611
2612 (autoload 'idna-to-unicode "idna")
2613
2614 (defun article-decode-idna-rhs ()
2615   "Decode IDNA strings in RHS in various headers in current buffer.
2616 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2617 Mail-Reply-To: and Mail-Followup-To:."
2618   (when gnus-use-idna
2619     (save-restriction
2620       (let ((inhibit-point-motion-hooks t)
2621             (inhibit-read-only t))
2622         (article-narrow-to-head)
2623         (goto-char (point-min))
2624         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2625           (let (ace unicode)
2626             (when (save-match-data
2627                     (and (setq ace (match-string 1))
2628                          (save-excursion
2629                            (and (re-search-backward "^[^ \t]" nil t)
2630                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2631                          (setq unicode (idna-to-unicode ace))))
2632               (unless (string= ace unicode)
2633                 (replace-match unicode nil nil nil 1)))))))))
2634
2635 (defun article-de-quoted-unreadable (&optional force read-charset)
2636   "Translate a quoted-printable-encoded article.
2637 If FORCE, decode the article whether it is marked as quoted-printable
2638 or not.
2639 If READ-CHARSET, ask for a coding system."
2640   (interactive (list 'force current-prefix-arg))
2641   (save-excursion
2642     (let ((inhibit-read-only t) type charset)
2643       (if (gnus-buffer-live-p gnus-original-article-buffer)
2644           (with-current-buffer gnus-original-article-buffer
2645             (setq type
2646                   (gnus-fetch-field "content-transfer-encoding"))
2647             (let* ((ct (gnus-fetch-field "content-type"))
2648                    (ctl (and ct (mail-header-parse-content-type ct))))
2649               (setq charset (and ctl
2650                                  (mail-content-type-get ctl 'charset)))
2651               (if (stringp charset)
2652                   (setq charset (intern (downcase charset)))))))
2653       (if read-charset
2654           (setq charset (mm-read-coding-system "Charset: " charset)))
2655       (unless charset
2656         (setq charset gnus-newsgroup-charset))
2657       (when (or force
2658                 (and type (let ((case-fold-search t))
2659                             (string-match "quoted-printable" type))))
2660         (article-goto-body)
2661         (quoted-printable-decode-region
2662          (point) (point-max) (mm-charset-to-coding-system charset))))))
2663
2664 (defun article-de-base64-unreadable (&optional force read-charset)
2665   "Translate a base64 article.
2666 If FORCE, decode the article whether it is marked as base64 not.
2667 If READ-CHARSET, ask for a coding system."
2668   (interactive (list 'force current-prefix-arg))
2669   (save-excursion
2670     (let ((inhibit-read-only t) type charset)
2671       (if (gnus-buffer-live-p gnus-original-article-buffer)
2672           (with-current-buffer gnus-original-article-buffer
2673             (setq type
2674                   (gnus-fetch-field "content-transfer-encoding"))
2675             (let* ((ct (gnus-fetch-field "content-type"))
2676                    (ctl (and ct (mail-header-parse-content-type ct))))
2677               (setq charset (and ctl
2678                                  (mail-content-type-get ctl 'charset)))
2679               (if (stringp charset)
2680                   (setq charset (intern (downcase charset)))))))
2681       (if read-charset
2682           (setq charset (mm-read-coding-system "Charset: " charset)))
2683       (unless charset
2684         (setq charset gnus-newsgroup-charset))
2685       (when (or force
2686                 (and type (let ((case-fold-search t))
2687                             (string-match "base64" type))))
2688         (article-goto-body)
2689         (save-restriction
2690           (narrow-to-region (point) (point-max))
2691           (base64-decode-region (point-min) (point-max))
2692           (mm-decode-coding-region
2693            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2694
2695 (eval-when-compile
2696   (require 'rfc1843))
2697
2698 (defun article-decode-HZ ()
2699   "Translate a HZ-encoded article."
2700   (interactive)
2701   (require 'rfc1843)
2702   (save-excursion
2703     (let ((inhibit-read-only t))
2704       (rfc1843-decode-region (point-min) (point-max)))))
2705
2706 (defun article-unsplit-urls ()
2707   "Remove the newlines that some other mailers insert into URLs."
2708   (interactive)
2709   (save-excursion
2710     (let ((inhibit-read-only t))
2711       (goto-char (point-min))
2712       (while (re-search-forward
2713               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2714         (replace-match "\\1\\3" t)))
2715     (when (interactive-p)
2716       (gnus-treat-article nil))))
2717
2718 (defun article-wash-html ()
2719   "Format an HTML article."
2720   (interactive)
2721   (let ((handles nil)
2722         (buffer-read-only nil))
2723     (when (gnus-buffer-live-p gnus-original-article-buffer)
2724       (setq handles (mm-dissect-buffer t t)))
2725     (article-goto-body)
2726     (delete-region (point) (point-max))
2727     (mm-inline-text-html handles)))
2728
2729 (defvar gnus-article-browse-html-temp-list nil
2730   "List of temporary files created by `gnus-article-browse-html-parts'.
2731 Internal variable.")
2732
2733 (defcustom gnus-article-browse-delete-temp 'ask
2734   "What to do with temporary files from `gnus-article-browse-html-parts'.
2735 If nil, don't delete temporary files.  If it is t, delete them on
2736 exit from the summary buffer.  If it is the symbol `file', query
2737 on each file, if it is `ask' ask once when exiting from the
2738 summary buffer."
2739   :group 'gnus-article
2740   :version "23.1" ;; No Gnus
2741   :type '(choice (const :tag "Don't delete" nil)
2742                  (const :tag "Don't ask" t)
2743                  (const :tag "Ask" ask)
2744                  (const :tag "Ask for each file" file)))
2745
2746 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2747
2748 (defun gnus-article-browse-delete-temp-files (&optional how)
2749   "Delete temp-files created by `gnus-article-browse-html-parts'."
2750   (when (and gnus-article-browse-html-temp-list
2751              (progn
2752                (or how (setq how gnus-article-browse-delete-temp))
2753                (if (eq how 'ask)
2754                    (let ((files (length gnus-article-browse-html-temp-list)))
2755                      (gnus-y-or-n-p (format
2756                                      "Delete all %s temporary HTML file%s? "
2757                                      files
2758                                      (if (> files 1) "s" ""))))
2759                  how)))
2760     (dolist (file gnus-article-browse-html-temp-list)
2761       (cond ((file-directory-p file)
2762              (when (or (not (eq how 'file))
2763                        (gnus-y-or-n-p
2764                         (format
2765                          "Delete temporary HTML file(s) in directory `%s'? "
2766                          (file-name-as-directory file))))
2767                (gnus-delete-directory file)))
2768             ((file-exists-p file)
2769              (when (or (not (eq how 'file))
2770                        (gnus-y-or-n-p
2771                         (format "Delete temporary HTML file `%s'? " file)))
2772                (delete-file file)))))
2773     ;; Also remove file from the list when not deleted or if file doesn't
2774     ;; exist anymore.
2775     (setq gnus-article-browse-html-temp-list nil))
2776   gnus-article-browse-html-temp-list)
2777
2778 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2779   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2780 Return file name."
2781   (save-match-data
2782     (let (file type)
2783       (catch 'found
2784         (dolist (handle handles)
2785           (cond
2786            ((not (listp handle)))
2787            ((equal (mm-handle-media-supertype handle) "multipart")
2788             (when (setq file (gnus-article-browse-html-save-cid-content
2789                               cid handle directory))
2790               (throw 'found file)))
2791            ((equal (concat "<" cid ">") (mm-handle-id handle))
2792             (setq file
2793                   (expand-file-name
2794                    (or (mail-content-type-get
2795                         (mm-handle-disposition handle) 'filename)
2796                        (mail-content-type-get
2797                         (setq type (mm-handle-type handle)) 'name)
2798                        (concat
2799                         (make-temp-name "cid")
2800                         (car (rassoc (car type) mailcap-mime-extensions))))
2801                    directory))
2802             (mm-save-part-to-file handle file)
2803             (throw 'found file))))))))
2804
2805 (defun gnus-article-browse-html-parts (list &optional header)
2806   "View all \"text/html\" parts from LIST.
2807 Recurse into multiparts.  The optional HEADER that should be a decoded
2808 message header will be added to the bodies of the \"text/html\" parts."
2809   ;; Internal function used by `gnus-article-browse-html-article'.
2810   (let (type file charset content cid-dir tmp-file showed)
2811     ;; Find and show the html-parts.
2812     (dolist (handle list)
2813       ;; If HTML, show it:
2814       (cond ((not (listp handle)))
2815             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2816                  (and (equal (car type) "message/external-body")
2817                       (or header
2818                           (setq file (or (mail-content-type-get type 'name)
2819                                          (mail-content-type-get
2820                                           (mm-handle-disposition handle)
2821                                           'filename))))
2822                       (or (mm-handle-cache handle)
2823                           (condition-case code
2824                               (progn (mm-extern-cache-contents handle) t)
2825                             (error
2826                              (gnus-message 3 "%s" (error-message-string code))
2827                              (when (>= gnus-verbose 3) (sit-for 2))
2828                              nil)))
2829                       (progn
2830                         (setq handle (mm-handle-cache handle)
2831                               type (mm-handle-type handle))
2832                         (equal (car type) "text/html"))))
2833              (setq charset (mail-content-type-get type 'charset)
2834                    content (mm-get-part handle))
2835              (with-temp-buffer
2836                (if (eq charset 'gnus-decoded)
2837                    (mm-enable-multibyte)
2838                  (mm-disable-multibyte))
2839                (insert content)
2840                ;; resolve cid contents
2841                (let ((case-fold-search t)
2842                      cid-file)
2843                  (goto-char (point-min))
2844                  (while (re-search-forward "\
2845 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2846                                            nil t)
2847                    (unless cid-dir
2848                      (setq cid-dir (mm-make-temp-file "cid" t))
2849                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2850                    (setq file nil
2851                          content nil)
2852                    (when (setq cid-file
2853                                (gnus-article-browse-html-save-cid-content
2854                                 (match-string 2)
2855                                 (with-current-buffer gnus-article-buffer
2856                                   gnus-article-mime-handles)
2857                                 cid-dir))
2858                      (replace-match (concat "file://" cid-file)
2859                                     nil nil nil 1))))
2860                (unless content (setq content (buffer-string))))
2861              (when (or charset header (not file))
2862                (setq tmp-file (mm-make-temp-file
2863                                ;; Do we need to care for 8.3 filenames?
2864                                "mm-" nil ".html")))
2865              ;; Add a meta html tag to specify charset and a header.
2866              (cond
2867               (header
2868                (let (title eheader body hcharset coding force-charset)
2869                  (with-temp-buffer
2870                    (mm-enable-multibyte)
2871                    (setq case-fold-search t)
2872                    (insert header "\n")
2873                    (setq title (message-fetch-field "subject"))
2874                    (goto-char (point-min))
2875                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2876                      (replace-match (cond ((match-beginning 1) "&lt;")
2877                                           ((match-beginning 2) "&gt;")
2878                                           (t "&amp;"))))
2879                    (goto-char (point-min))
2880                    (insert "<pre>\n")
2881                    (goto-char (point-max))
2882                    (insert "</pre>\n<hr>\n")
2883                    ;; We have to examine charset one by one since
2884                    ;; charset specified in parts might be different.
2885                    (if (eq charset 'gnus-decoded)
2886                        (setq charset 'utf-8
2887                              eheader (mm-encode-coding-string (buffer-string)
2888                                                               charset)
2889                              title (when title
2890                                      (mm-encode-coding-string title charset))
2891                              body (mm-encode-coding-string content charset)
2892                              force-charset t)
2893                      (setq hcharset (mm-find-mime-charset-region (point-min)
2894                                                                  (point-max)))
2895                      (cond ((= (length hcharset) 1)
2896                             (setq hcharset (car hcharset)
2897                                   coding (mm-charset-to-coding-system
2898                                           hcharset)))
2899                            ((> (length hcharset) 1)
2900                             (setq hcharset 'utf-8
2901                                   coding hcharset)))
2902                      (if coding
2903                          (if charset
2904                              (progn
2905                                (setq body
2906                                      (mm-charset-to-coding-system charset))
2907                                (if (eq coding body)
2908                                    (setq eheader (mm-encode-coding-string
2909                                                   (buffer-string) coding)
2910                                          title (when title
2911                                                  (mm-encode-coding-string
2912                                                   title coding))
2913                                          body content)
2914                                  (setq charset 'utf-8
2915                                        eheader (mm-encode-coding-string
2916                                                 (buffer-string) charset)
2917                                        title (when title
2918                                                (mm-encode-coding-string
2919                                                 title charset))
2920                                        body (mm-encode-coding-string
2921                                              (mm-decode-coding-string
2922                                               content body)
2923                                              charset)
2924                                        force-charset t)))
2925                            (setq charset hcharset
2926                                  eheader (mm-encode-coding-string
2927                                           (buffer-string) coding)
2928                                  title (when title
2929                                          (mm-encode-coding-string
2930                                           title coding))
2931                                  body content))
2932                        (setq eheader (mm-string-as-unibyte (buffer-string))
2933                              body content)))
2934                    (erase-buffer)
2935                    (mm-disable-multibyte)
2936                    (insert body)
2937                    (when charset
2938                      (mm-add-meta-html-tag handle charset force-charset))
2939                    (when title
2940                      (goto-char (point-min))
2941                      (unless (search-forward "<title>" nil t)
2942                        (re-search-forward "<head>\\s-*" nil t)
2943                        (insert "<title>" title "</title>\n")))
2944                    (goto-char (point-min))
2945                    (or (re-search-forward
2946                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2947                        (re-search-forward
2948                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2949                    (insert eheader)
2950                    (mm-write-region (point-min) (point-max)
2951                                     tmp-file nil nil nil 'binary t))))
2952               (charset
2953                (mm-with-unibyte-buffer
2954                  (insert (if (eq charset 'gnus-decoded)
2955                              (mm-encode-coding-string content
2956                                                       (setq charset 'utf-8))
2957                            content))
2958                  (if (or (mm-add-meta-html-tag handle charset)
2959                          (not file))
2960                      (mm-write-region (point-min) (point-max)
2961                                       tmp-file nil nil nil 'binary t)
2962                    (setq tmp-file nil))))
2963               (tmp-file
2964                (mm-save-part-to-file handle tmp-file)))
2965              (when tmp-file
2966                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2967              (add-hook 'gnus-summary-prepare-exit-hook
2968                        'gnus-article-browse-delete-temp-files)
2969              (add-hook 'gnus-exit-gnus-hook
2970                        (lambda  ()
2971                          (gnus-article-browse-delete-temp-files t)))
2972              ;; FIXME: Warn if there's an <img> tag?
2973              (browse-url-of-file (or tmp-file (expand-file-name file)))
2974              (setq showed t))
2975             ;; If multipart, recurse
2976             ((equal (mm-handle-media-supertype handle) "multipart")
2977              (when (gnus-article-browse-html-parts handle header)
2978                (setq showed t)))
2979             ((equal (mm-handle-media-type handle) "message/rfc822")
2980              (mm-with-multibyte-buffer
2981                (mm-insert-part handle)
2982                (setq handle (mm-dissect-buffer t t))
2983                (when (and (bufferp (car handle))
2984                           (stringp (car (mm-handle-type handle))))
2985                  (setq handle (list handle)))
2986                (when header
2987                  (article-decode-encoded-words)
2988                  (let ((gnus-visible-headers
2989                         (or (get 'gnus-visible-headers 'standard-value)
2990                             gnus-visible-headers)))
2991                    (article-hide-headers))
2992                  (goto-char (point-min))
2993                  (search-forward "\n\n" nil 'move)
2994                  (skip-chars-backward "\t\n ")
2995                  (setq header (buffer-substring (point-min) (point)))))
2996              (when (prog1
2997                        (gnus-article-browse-html-parts handle header)
2998                      (mm-destroy-parts handle))
2999                (setq showed t)))))
3000     showed))
3001
3002 (defun gnus-article-browse-html-article (&optional arg)
3003   "View \"text/html\" parts of the current article with a WWW browser.
3004 Inline images embedded in a message using the cid scheme, as they are
3005 generally considered to be safe, will be processed properly.
3006 The message header is added to the beginning of every html part unless
3007 the prefix argument ARG is given.
3008
3009 Warning: Spammers use links to images (using the http scheme) in HTML
3010 articles to verify whether you have read the message.  As
3011 `gnus-article-browse-html-article' passes the HTML content to the
3012 browser without eliminating these \"web bugs\" you should only
3013 use it for mails from trusted senders.
3014
3015 If you always want to display HTML parts in the browser, set
3016 `mm-text-html-renderer' to nil.
3017
3018 This command creates temporary files to pass HTML contents including
3019 images if any to the browser, and deletes them when exiting the group
3020 \(if you want)."
3021   ;; Cf. `mm-w3m-safe-url-regexp'
3022   (interactive "P")
3023   (if arg
3024       (gnus-summary-show-article)
3025     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3026                                     gnus-visible-headers))
3027           ;; As we insert a <hr>, there's no need for the body boundary.
3028           (gnus-treat-body-boundary nil))
3029       (gnus-summary-show-article)))
3030   (with-current-buffer gnus-article-buffer
3031     (let ((header (unless arg
3032                     (save-restriction
3033                       (widen)
3034                       (buffer-substring-no-properties
3035                        (goto-char (point-min))
3036                        (if (search-forward "\n\n" nil t)
3037                            (match-beginning 0)
3038                          (goto-char (point-max))
3039                          (skip-chars-backward "\t\n ")
3040                          (point))))))
3041           parts)
3042       (set-buffer gnus-original-article-buffer)
3043       (setq parts (mm-dissect-buffer t t))
3044       ;; If singlepart, enforce a list.
3045       (when (and (bufferp (car parts))
3046                  (stringp (car (mm-handle-type parts))))
3047         (setq parts (list parts)))
3048       ;; Process the list
3049       (unless (gnus-article-browse-html-parts parts header)
3050         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3051       (mm-destroy-parts parts)
3052       (unless arg
3053         (gnus-summary-show-article)))))
3054
3055 (defun article-hide-list-identifiers ()
3056   "Remove list identifies from the Subject header.
3057 The `gnus-list-identifiers' variable specifies what to do."
3058   (interactive)
3059   (let ((inhibit-point-motion-hooks t)
3060         (regexp (if (consp gnus-list-identifiers)
3061                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3062                   gnus-list-identifiers))
3063         (inhibit-read-only t))
3064     (when regexp
3065       (save-excursion
3066         (save-restriction
3067           (article-narrow-to-head)
3068           (goto-char (point-min))
3069           (while (re-search-forward
3070                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3071                   nil t)
3072             (delete-region (match-beginning 2) (match-end 0))
3073             (beginning-of-line))
3074           (when (re-search-forward
3075                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3076             (delete-region (match-beginning 1) (match-end 1))))))))
3077
3078 (defun article-hide-pem (&optional arg)
3079   "Toggle hiding of any PEM headers and signatures in the current article.
3080 If given a negative prefix, always show; if given a positive prefix,
3081 always hide."
3082   (interactive (gnus-article-hidden-arg))
3083   (unless (gnus-article-check-hidden-text 'pem arg)
3084     (save-excursion
3085       (let ((inhibit-read-only t) end)
3086         (goto-char (point-min))
3087         ;; Hide the horrendously ugly "header".
3088         (when (and (search-forward
3089                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3090                     nil t)
3091                    (setq end (1+ (match-beginning 0))))
3092           (gnus-add-wash-type 'pem)
3093           (gnus-article-hide-text-type
3094            end
3095            (if (search-forward "\n\n" nil t)
3096                (match-end 0)
3097              (point-max))
3098            'pem)
3099           ;; Hide the trailer as well
3100           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3101                                 nil t)
3102             (gnus-article-hide-text-type
3103              (match-beginning 0) (match-end 0) 'pem)))))))
3104
3105 (defun article-strip-banner ()
3106   "Strip the banners specified by the `banner' group parameter and by
3107 `gnus-article-address-banner-alist'."
3108   (interactive)
3109   (save-excursion
3110     (save-restriction
3111       (let ((inhibit-point-motion-hooks t))
3112         (when (gnus-parameter-banner gnus-newsgroup-name)
3113           (article-really-strip-banner
3114            (gnus-parameter-banner gnus-newsgroup-name)))
3115         (when gnus-article-address-banner-alist
3116           ;; Note that the From header is decoded here, so it is
3117           ;; required that the *-extract-address-components function
3118           ;; supports non-ASCII text.
3119           (let ((from (save-restriction
3120                         (widen)
3121                         (article-narrow-to-head)
3122                         (mail-fetch-field "from"))))
3123             (when (and from
3124                        (setq from
3125                              (cadr (funcall gnus-extract-address-components
3126                                             from))))
3127               (catch 'found
3128                 (dolist (pair gnus-article-address-banner-alist)
3129                   (when (string-match (car pair) from)
3130                     (throw 'found
3131                            (article-really-strip-banner (cdr pair)))))))))))))
3132
3133 (defun article-really-strip-banner (banner)
3134   "Strip the banner specified by the argument."
3135   (save-excursion
3136     (save-restriction
3137       (let ((inhibit-point-motion-hooks t)
3138             (gnus-signature-limit nil)
3139             (inhibit-read-only t))
3140         (article-goto-body)
3141         (cond
3142          ((eq banner 'signature)
3143           (when (gnus-article-narrow-to-signature)
3144             (widen)
3145             (forward-line -1)
3146             (delete-region (point) (point-max))))
3147          ((symbolp banner)
3148           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3149               (while (re-search-forward banner nil t)
3150                 (delete-region (match-beginning 0) (match-end 0)))))
3151          ((stringp banner)
3152           (while (re-search-forward banner nil t)
3153             (delete-region (match-beginning 0) (match-end 0)))))))))
3154
3155 (defun article-babel ()
3156   "Translate article using an online translation service."
3157   (interactive)
3158   (require 'babel)
3159   (gnus-with-article-buffer
3160     (when (article-goto-body)
3161       (let* ((start (point))
3162              (end (point-max))
3163              (orig (buffer-substring start end))
3164              (trans (babel-as-string orig)))
3165         (save-restriction
3166           (narrow-to-region start end)
3167           (delete-region start end)
3168           (insert trans))))))
3169
3170 (defun article-hide-signature (&optional arg)
3171   "Hide the signature in the current article.
3172 If given a negative prefix, always show; if given a positive prefix,
3173 always hide."
3174   (interactive (gnus-article-hidden-arg))
3175   (unless (gnus-article-check-hidden-text 'signature arg)
3176     (save-excursion
3177       (save-restriction
3178         (let ((inhibit-read-only t))
3179           (when (gnus-article-narrow-to-signature)
3180             (gnus-article-hide-text-type
3181              (point-min) (point-max) 'signature))))))
3182   (gnus-set-mode-line 'article))
3183
3184 (defun article-strip-headers-in-body ()
3185   "Strip offensive headers from bodies."
3186   (interactive)
3187   (save-excursion
3188     (article-goto-body)
3189     (let ((case-fold-search t))
3190       (when (looking-at "x-no-archive:")
3191         (gnus-delete-line)))))
3192
3193 (defun article-strip-leading-blank-lines ()
3194   "Remove all blank lines from the beginning of the article."
3195   (interactive)
3196   (save-excursion
3197     (let ((inhibit-point-motion-hooks t)
3198           (inhibit-read-only t))
3199       (when (article-goto-body)
3200         (while (and (not (eobp))
3201                     (looking-at "[ \t]*$"))
3202           (gnus-delete-line))))))
3203
3204 (defun article-narrow-to-head ()
3205   "Narrow the buffer to the head of the message.
3206 Point is left at the beginning of the narrowed-to region."
3207   (narrow-to-region
3208    (goto-char (point-min))
3209    (if (search-forward "\n\n" nil 1)
3210        (1- (point))
3211      (point-max)))
3212   (goto-char (point-min)))
3213
3214 (defun article-goto-body ()
3215   "Place point at the start of the body."
3216   (goto-char (point-min))
3217   (cond
3218    ;; This variable is only bound when dealing with separate
3219    ;; MIME body parts.
3220    (article-goto-body-goes-to-point-min-p
3221     t)
3222    ((search-forward "\n\n" nil t)
3223     t)
3224    (t
3225     (goto-char (point-max))
3226     nil)))
3227
3228 (defun article-strip-multiple-blank-lines ()
3229   "Replace consecutive blank lines with one empty line."
3230   (interactive)
3231   (save-excursion
3232     (let ((inhibit-point-motion-hooks t)
3233           (inhibit-read-only t))
3234       ;; First make all blank lines empty.
3235       (article-goto-body)
3236       (while (re-search-forward "^[ \t]+$" nil t)
3237         (unless (gnus-annotation-in-region-p
3238                  (match-beginning 0) (match-end 0))
3239           (replace-match "" nil t)))
3240       ;; Then replace multiple empty lines with a single empty line.
3241       (article-goto-body)
3242       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3243         (unless (gnus-annotation-in-region-p
3244                  (match-beginning 0) (match-end 0))
3245           (delete-region (match-beginning 1) (match-end 1)))))))
3246
3247 (defun article-strip-leading-space ()
3248   "Remove all white space from the beginning of the lines in the article."
3249   (interactive)
3250   (save-excursion
3251     (let ((inhibit-point-motion-hooks t)
3252           (inhibit-read-only t))
3253       (article-goto-body)
3254       (while (re-search-forward "^[ \t]+" nil t)
3255         (replace-match "" t t)))))
3256
3257 (defun article-strip-trailing-space ()
3258   "Remove all white space from the end of the lines in the article."
3259   (interactive)
3260   (save-excursion
3261     (let ((inhibit-point-motion-hooks t)
3262           (inhibit-read-only t))
3263       (article-goto-body)
3264       (while (re-search-forward "[ \t]+$" nil t)
3265         (replace-match "" t t)))))
3266
3267 (defun article-strip-blank-lines ()
3268   "Strip leading, trailing and multiple blank lines."
3269   (interactive)
3270   (article-strip-leading-blank-lines)
3271   (article-remove-trailing-blank-lines)
3272   (article-strip-multiple-blank-lines))
3273
3274 (defun article-strip-all-blank-lines ()
3275   "Strip all blank lines."
3276   (interactive)
3277   (save-excursion
3278     (let ((inhibit-point-motion-hooks t)
3279           (inhibit-read-only t))
3280       (article-goto-body)
3281       (while (re-search-forward "^[ \t]*\n" nil t)
3282         (replace-match "" t t)))))
3283
3284 (defun gnus-article-narrow-to-signature ()
3285   "Narrow to the signature; return t if a signature is found, else nil."
3286   (let ((inhibit-point-motion-hooks t))
3287     (when (gnus-article-search-signature)
3288       (forward-line 1)
3289       ;; Check whether we have some limits to what we consider
3290       ;; to be a signature.
3291       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3292                       (list gnus-signature-limit)))
3293             limit limited)
3294         (while (setq limit (pop limits))
3295           (if (or (and (integerp limit)
3296                        (< (- (point-max) (point)) limit))
3297                   (and (floatp limit)
3298                        (< (count-lines (point) (point-max)) limit))
3299                   (and (functionp limit)
3300                        (funcall limit))
3301                   (and (stringp limit)
3302                        (not (re-search-forward limit nil t))))
3303               ()                        ; This limit did not succeed.
3304             (setq limited t
3305                   limits nil)))
3306         (unless limited
3307           (narrow-to-region (point) (point-max))
3308           t)))))
3309
3310 (defun gnus-article-search-signature ()
3311   "Search the current buffer for the signature separator.
3312 Put point at the beginning of the signature separator."
3313   (let ((cur (point)))
3314     (goto-char (point-max))
3315     (if (if (stringp gnus-signature-separator)
3316             (re-search-backward gnus-signature-separator nil t)
3317           (let ((seps gnus-signature-separator))
3318             (while (and seps
3319                         (not (re-search-backward (car seps) nil t)))
3320               (pop seps))
3321             seps))
3322         t
3323       (goto-char cur)
3324       nil)))
3325
3326 (defun gnus-article-hidden-arg ()
3327   "Return the current prefix arg as a number, or 0 if no prefix."
3328   (list (if current-prefix-arg
3329             (prefix-numeric-value current-prefix-arg)
3330           0)))
3331
3332 (defun gnus-article-check-hidden-text (type arg)
3333   "Return nil if hiding is necessary.
3334 Arg can be nil or a number.  nil and positive means hide, negative
3335 means show, 0 means toggle."
3336   (save-excursion
3337     (save-restriction
3338       (let ((hide (gnus-article-hidden-text-p type)))
3339         (cond
3340          ((or (null arg)
3341               (> arg 0))
3342           nil)
3343          ((< arg 0)
3344           (gnus-article-show-hidden-text type)
3345           t)
3346          (t
3347           (if (eq hide 'hidden)
3348               (progn
3349                 (gnus-article-show-hidden-text type)
3350                 t)
3351             nil)))))))
3352
3353 (defun gnus-article-hidden-text-p (type)
3354   "Say whether the current buffer contains hidden text of type TYPE."
3355   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3356     (while (and pos
3357                 (not (get-text-property pos 'invisible))
3358                 (not (get-text-property pos 'dummy-invisible)))
3359       (setq pos
3360             (text-property-any (1+ pos) (point-max) 'article-type type)))
3361     (if pos
3362         'hidden
3363       nil)))
3364
3365 (defun gnus-article-show-hidden-text (type &optional dummy)
3366   "Show all hidden text of type TYPE.
3367 Originally it is hide instead of DUMMY."
3368   (let ((inhibit-read-only t)
3369         (inhibit-point-motion-hooks t))
3370     (gnus-remove-text-properties-when
3371      'article-type type
3372      (point-min) (point-max)
3373      (cons 'article-type (cons type
3374                                gnus-hidden-properties)))
3375     (gnus-delete-wash-type type)))
3376
3377 (defconst article-time-units
3378   `((year . ,(* 365.25 24 60 60))
3379     (week . ,(* 7 24 60 60))
3380     (day . ,(* 24 60 60))
3381     (hour . ,(* 60 60))
3382     (minute . 60)
3383     (second . 1))
3384   "Mapping from time units to seconds.")
3385
3386 (defun gnus-article-forward-header ()
3387   "Move point to the start of the next header.
3388 If the current header is a continuation header, this can be several
3389 lines forward."
3390   (let ((ended nil))
3391     (while (not ended)
3392       (forward-line 1)
3393       (if (looking-at "[ \t]+[^ \t]")
3394           (forward-line 1)
3395         (setq ended t)))))
3396
3397 (defun article-date-ut (&optional type highlight)
3398   "Convert DATE date to universal time in the current article.
3399 If TYPE is `local', convert to local time; if it is `lapsed', output
3400 how much time has lapsed since DATE.  For `lapsed', the value of
3401 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3402 should replace the \"Date:\" one, or should be added below it."
3403   (interactive (list 'ut t))
3404   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3405          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3406                              tdate-regexp)
3407                             ((eq type 'lapsed)
3408                              "^X-Sent:[ \t]")
3409                             (article-lapsed-timer
3410                              "^Date:[ \t]")
3411                             (t
3412                              tdate-regexp)))
3413          (case-fold-search t)
3414          (inhibit-read-only t)
3415          (inhibit-point-motion-hooks t)
3416          pos date bface eface)
3417     (save-excursion
3418       (save-restriction
3419         (widen)
3420         (goto-char (point-min))
3421         (while (or (setq date (get-text-property (setq pos (point))
3422                                                  'original-date))
3423                    (when (setq pos (next-single-property-change
3424                                     (point) 'original-date))
3425                      (setq date (get-text-property pos 'original-date))
3426                      t))
3427           (narrow-to-region
3428            pos (if (setq pos (text-property-any pos (point-max)
3429                                                 'original-date nil))
3430                    (progn
3431                      (goto-char pos)
3432                      (if (or (bolp) (eobp))
3433                          (point)
3434                        (1+ (point))))
3435                  (point-max)))
3436           (goto-char (point-min))
3437           (when (re-search-forward tdate-regexp nil t)
3438             (setq bface (get-text-property (point-at-bol) 'face)
3439                   eface (get-text-property (1- (point-at-eol)) 'face)))
3440           (goto-char (point-min))
3441           (setq pos nil)
3442           ;; Delete any old Date headers.
3443           (while (re-search-forward date-regexp nil t)
3444             (if pos
3445                 (delete-region (point-at-bol) (progn
3446                                                 (gnus-article-forward-header)
3447                                                 (point)))
3448               (delete-region (point-at-bol) (progn
3449                                               (gnus-article-forward-header)
3450                                               (forward-char -1)
3451                                               (point)))
3452               (setq pos (point))))
3453           (when (and (not pos)
3454                      (re-search-forward tdate-regexp nil t))
3455             (forward-line 1))
3456           (gnus-goto-char pos)
3457           (insert (article-make-date-line date (or type 'ut)))
3458           (unless pos
3459             (insert "\n")
3460             (forward-line -1))
3461           ;; Do highlighting.
3462           (beginning-of-line)
3463           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3464             (put-text-property (match-beginning 1) (1+ (match-end 1))
3465                                'face bface)
3466             (put-text-property (match-beginning 2) (match-end 2)
3467                                'face eface))
3468           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3469           (goto-char (point-max))
3470           (widen))))))
3471
3472 (defun article-make-date-line (date type)
3473   "Return a DATE line of TYPE."
3474   (unless (memq type '(local ut original user iso8601 lapsed english))
3475     (error "Unknown conversion type: %s" type))
3476   (condition-case ()
3477       (let ((time (date-to-time date)))
3478         (cond
3479          ;; Convert to the local timezone.
3480          ((eq type 'local)
3481           (concat "Date: " (message-make-date time)))
3482          ;; Convert to Universal Time.
3483          ((eq type 'ut)
3484           (concat "Date: "
3485                   (substring
3486                    (message-make-date
3487                     (let* ((e (parse-time-string date))
3488                            (tm (apply 'encode-time e))
3489                            (ms (car tm))
3490                            (ls (- (cadr tm) (car (current-time-zone time)))))
3491                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3492                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3493                             (t (list ms ls)))))
3494                    0 -5)
3495                   "UT"))
3496          ;; Get the original date from the article.
3497          ((eq type 'original)
3498           (concat "Date: " (if (string-match "\n+$" date)
3499                                (substring date 0 (match-beginning 0))
3500                              date)))
3501          ;; Let the user define the format.
3502          ((eq type 'user)
3503           (let ((format (or (condition-case nil
3504                                 (with-current-buffer gnus-summary-buffer
3505                                   gnus-article-time-format)
3506                               (error nil))
3507                             gnus-article-time-format)))
3508             (if (functionp format)
3509                 (funcall format time)
3510               (concat "Date: " (format-time-string format time)))))
3511          ;; ISO 8601.
3512          ((eq type 'iso8601)
3513           (let ((tz (car (current-time-zone time))))
3514             (concat
3515              "Date: "
3516              (format-time-string "%Y%m%dT%H%M%S" time)
3517              (format "%s%02d%02d"
3518                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3519                      (/ (% (abs tz) 3600) 60)))))
3520          ;; Do an X-Sent lapsed format.
3521          ((eq type 'lapsed)
3522           ;; If the date is seriously mangled, the timezone functions are
3523           ;; liable to bug out, so we ignore all errors.
3524           (let* ((now (current-time))
3525                  (real-time (subtract-time now time))
3526                  (real-sec (and real-time
3527                                 (+ (* (float (car real-time)) 65536)
3528                                    (cadr real-time))))
3529                  (sec (and real-time (abs real-sec)))
3530                  num prev)
3531             (cond
3532              ((null real-time)
3533               "X-Sent: Unknown")
3534              ((zerop sec)
3535               "X-Sent: Now")
3536              (t
3537               (concat
3538                "X-Sent: "
3539                ;; This is a bit convoluted, but basically we go
3540                ;; through the time units for years, weeks, etc,
3541                ;; and divide things to see whether that results
3542                ;; in positive answers.
3543                (mapconcat
3544                 (lambda (unit)
3545                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3546                       ;; The (remaining) seconds are too few to
3547                       ;; be divided into this time unit.
3548                       ""
3549                     ;; It's big enough, so we output it.
3550                     (setq sec (- sec (* num (cdr unit))))
3551                     (prog1
3552                         (concat (if prev ", " "") (int-to-string
3553                                                    (floor num))
3554                                 " " (symbol-name (car unit))
3555                                 (if (> num 1) "s" ""))
3556                       (setq prev t))))
3557                 article-time-units "")
3558                ;; If dates are odd, then it might appear like the
3559                ;; article was sent in the future.
3560                (if (> real-sec 0)
3561                    " ago"
3562                  " in the future"))))))
3563          ;; Display the date in proper English
3564          ((eq type 'english)
3565           (let ((dtime (decode-time time)))
3566             (concat
3567              "Date: the "
3568              (number-to-string (nth 3 dtime))
3569              (let ((digit (% (nth 3 dtime) 10)))
3570                (cond
3571                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3572                 ((= digit 1) "st")
3573                 ((= digit 2) "nd")
3574                 ((= digit 3) "rd")
3575                 (t "th")))
3576              " of "
3577              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3578              " "
3579              (number-to-string (nth 5 dtime))
3580              " at "
3581              (format "%02d" (nth 2 dtime))
3582              ":"
3583              (format "%02d" (nth 1 dtime)))))))
3584     (error
3585      (format "Date: %s (from Gnus)" date))))
3586
3587 (defun article-date-local (&optional highlight)
3588   "Convert the current article date to the local timezone."
3589   (interactive (list t))
3590   (article-date-ut 'local highlight))
3591
3592 (defun article-date-english (&optional highlight)
3593   "Convert the current article date to something that is proper English."
3594   (interactive (list t))
3595   (article-date-ut 'english highlight))
3596
3597 (defun article-date-original (&optional highlight)
3598   "Convert the current article date to what it was originally.
3599 This is only useful if you have used some other date conversion
3600 function and want to see what the date was before converting."
3601   (interactive (list t))
3602   (article-date-ut 'original highlight))
3603
3604 (defun article-date-lapsed (&optional highlight)
3605   "Convert the current article date to time lapsed since it was sent."
3606   (interactive (list t))
3607   (article-date-ut 'lapsed highlight))
3608
3609 (defun article-update-date-lapsed ()
3610   "Function to be run from a timer to update the lapsed time line."
3611   (save-match-data
3612     (let (deactivate-mark)
3613       (save-excursion
3614         (ignore-errors
3615          (walk-windows
3616           (lambda (w)
3617             (set-buffer (window-buffer w))
3618             (when (eq major-mode 'gnus-article-mode)
3619               (let ((mark (point-marker)))
3620                 (goto-char (point-min))
3621                 (when (re-search-forward "^X-Sent:" nil t)
3622                   (article-date-lapsed t))
3623                 (goto-char (marker-position mark))
3624                 (move-marker mark nil))))
3625           nil 'visible))))))
3626
3627 (defun gnus-start-date-timer (&optional n)
3628   "Start a timer to update the X-Sent header in the article buffers.
3629 The numerical prefix says how frequently (in seconds) the function
3630 is to run."
3631   (interactive "p")
3632   (unless n
3633     (setq n 1))
3634   (gnus-stop-date-timer)
3635   (setq article-lapsed-timer
3636         (run-at-time 1 n 'article-update-date-lapsed)))
3637
3638 (defun gnus-stop-date-timer ()
3639   "Stop the X-Sent timer."
3640   (interactive)
3641   (when article-lapsed-timer
3642     (nnheader-cancel-timer article-lapsed-timer)
3643     (setq article-lapsed-timer nil)))
3644
3645 (defun article-date-user (&optional highlight)
3646   "Convert the current article date to the user-defined format.
3647 This format is defined by the `gnus-article-time-format' variable."
3648   (interactive (list t))
3649   (article-date-ut 'user highlight))
3650
3651 (defun article-date-iso8601 (&optional highlight)
3652   "Convert the current article date to ISO8601."
3653   (interactive (list t))
3654   (article-date-ut 'iso8601 highlight))
3655
3656 (defmacro gnus-article-save-original-date (&rest forms)
3657   "Save the original date as a text property and evaluate FORMS."
3658   `(let* ((case-fold-search t)
3659           (start (progn
3660                    (goto-char (point-min))
3661                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3662                               (not (bolp)))
3663                      (match-end 0))))
3664           (date (when (and start
3665                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3666                                               nil t))
3667                   (buffer-substring-no-properties start
3668                                                   (match-beginning 0)))))
3669      (goto-char (point-max))
3670      (skip-chars-backward "\n")
3671      (put-text-property (point-min) (point) 'original-date date)
3672      ,@forms
3673      (goto-char (point-max))
3674      (skip-chars-backward "\n")
3675      (put-text-property (point-min) (point) 'original-date date)))
3676
3677 ;; (defun article-show-all ()
3678 ;;   "Show all hidden text in the article buffer."
3679 ;;   (interactive)
3680 ;;   (save-excursion
3681 ;;     (let ((inhibit-read-only t))
3682 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3683
3684 (defun article-remove-leading-whitespace ()
3685   "Remove excessive whitespace from all headers."
3686   (interactive)
3687   (save-excursion
3688     (save-restriction
3689       (let ((inhibit-read-only t))
3690         (article-narrow-to-head)
3691         (goto-char (point-min))
3692         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3693           (delete-region (match-beginning 1) (match-end 1)))))))
3694
3695 (defun article-emphasize (&optional arg)
3696   "Emphasize text according to `gnus-emphasis-alist'."
3697   (interactive (gnus-article-hidden-arg))
3698   (unless (gnus-article-check-hidden-text 'emphasis arg)
3699     (save-excursion
3700       (let ((alist (or
3701                     (condition-case nil
3702                         (with-current-buffer gnus-summary-buffer
3703                           gnus-article-emphasis-alist)
3704                       (error))
3705                     gnus-emphasis-alist))
3706             (inhibit-read-only t)
3707             (props (append '(article-type emphasis)
3708                            gnus-hidden-properties))
3709             regexp elem beg invisible visible face)
3710         (article-goto-body)
3711         (setq beg (point))
3712         (while (setq elem (pop alist))
3713           (goto-char beg)
3714           (setq regexp (car elem)
3715                 invisible (nth 1 elem)
3716                 visible (nth 2 elem)
3717                 face (nth 3 elem))
3718           (while (re-search-forward regexp nil t)
3719             (when (and (match-beginning visible) (match-beginning invisible))
3720               (gnus-article-hide-text
3721                (match-beginning invisible) (match-end invisible) props)
3722               (gnus-article-unhide-text-type
3723                (match-beginning visible) (match-end visible) 'emphasis)
3724               (gnus-put-overlay-excluding-newlines
3725                (match-beginning visible) (match-end visible) 'face face)
3726               (gnus-add-wash-type 'emphasis)
3727               (goto-char (match-end invisible)))))))))
3728
3729 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3730   "Setup newsgroup emphasis alist."
3731   (unless gnus-article-emphasis-alist
3732     (let ((name (and gnus-newsgroup-name
3733                      (gnus-group-real-name gnus-newsgroup-name))))
3734       (make-local-variable 'gnus-article-emphasis-alist)
3735       (setq gnus-article-emphasis-alist
3736             (nconc
3737              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3738                (while (setq elem (pop alist))
3739                  (when (and name (string-match (car elem) name))
3740                    (setq alist nil
3741                          highlight (copy-sequence (cdr elem)))))
3742                highlight)
3743              (copy-sequence highlight-words)
3744              (if gnus-newsgroup-name
3745                  (copy-sequence (gnus-group-find-parameter
3746                                  gnus-newsgroup-name 'highlight-words t)))
3747              gnus-emphasis-alist)))))
3748
3749 (defvar gnus-summary-article-menu)
3750 (defvar gnus-summary-post-menu)
3751
3752 ;;; Saving functions.
3753
3754 (defun gnus-article-save (save-buffer file &optional num)
3755   "Save the currently selected article."
3756   (when (or (get gnus-default-article-saver :headers)
3757             (not gnus-save-all-headers))
3758     ;; Remove headers according to `gnus-saved-headers' or the value
3759     ;; of the `:headers' property that the saver function might have.
3760     (let ((gnus-visible-headers
3761            (or (symbol-value (get gnus-default-article-saver :headers))
3762                gnus-saved-headers gnus-visible-headers))
3763           ;; Ignore group parameter.  See `article-hide-headers'.
3764           (gnus-summary-buffer nil))
3765       (with-current-buffer save-buffer
3766         (article-hide-headers 1 t))))
3767   (save-window-excursion
3768     (if (not gnus-default-article-saver)
3769         (error "No default saver is defined")
3770       ;; !!! Magic!  The saving functions all save
3771       ;; `gnus-save-article-buffer' (or so they think), but we
3772       ;; bind that variable to our save-buffer.
3773       (set-buffer gnus-article-buffer)
3774       (let* ((gnus-save-article-buffer save-buffer)
3775              (filename
3776               (cond
3777                ((not gnus-prompt-before-saving) 'default)
3778                ((eq gnus-prompt-before-saving 'always) nil)
3779                (t file)))
3780              (gnus-number-of-articles-to-be-saved
3781               (when (eq gnus-prompt-before-saving t)
3782                 num)))                  ; Magic
3783         (set-buffer gnus-article-current-summary)
3784         (funcall gnus-default-article-saver filename)))))
3785
3786 (defun gnus-read-save-file-name (prompt &optional filename
3787                                         function group headers variable
3788                                         dir-var)
3789   (let ((default-name
3790           (funcall function group headers (symbol-value variable)))
3791         result)
3792     (setq result
3793           (expand-file-name
3794            (cond
3795             ((eq filename 'default)
3796              default-name)
3797             ((eq filename t)
3798              default-name)
3799             (filename filename)
3800             (t
3801              (when (symbol-value dir-var)
3802                (setq default-name (expand-file-name
3803                                    (file-name-nondirectory default-name)
3804                                    (symbol-value dir-var))))
3805              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3806                     (prompt
3807                      (format prompt
3808                              (if (and gnus-number-of-articles-to-be-saved
3809                                       (> gnus-number-of-articles-to-be-saved 1))
3810                                  (format "these %d articles"
3811                                          gnus-number-of-articles-to-be-saved)
3812                                "this article")))
3813                     (file
3814                      ;; Let the split methods have their say.
3815                      (cond
3816                       ;; No split name was found.
3817                       ((null split-name)
3818                        (read-file-name
3819                         (concat prompt " (default "
3820                                 (file-name-nondirectory default-name) "): ")
3821                         (file-name-directory default-name)
3822                         default-name))
3823                       ;; A single group name is returned.
3824                       ((stringp split-name)
3825                        (setq default-name
3826                              (funcall function split-name headers
3827                                       (symbol-value variable)))
3828                        (read-file-name
3829                         (concat prompt " (default "
3830                                 (file-name-nondirectory default-name) "): ")
3831                         (file-name-directory default-name)
3832                         default-name))
3833                       ;; A single split name was found
3834                       ((= 1 (length split-name))
3835                        (let* ((name (expand-file-name
3836                                      (car split-name)
3837                                      gnus-article-save-directory))
3838                               (dir (cond ((file-directory-p name)
3839                                           (file-name-as-directory name))
3840                                          ((file-exists-p name) name)
3841                                          (t gnus-article-save-directory))))
3842                          (read-file-name
3843                           (concat prompt " (default " name "): ")
3844                           dir name)))
3845                       ;; A list of splits was found.
3846                       (t
3847                        (setq split-name (nreverse split-name))
3848                        (let (result)
3849                          (let ((file-name-history
3850                                 (nconc split-name file-name-history)))
3851                            (setq result
3852                                  (expand-file-name
3853                                   (read-file-name
3854                                    (concat prompt " (`M-p' for defaults): ")
3855                                    gnus-article-save-directory
3856                                    (car split-name))
3857                                   gnus-article-save-directory)))
3858                          (car (push result file-name-history)))))))
3859                ;; Create the directory.
3860                (gnus-make-directory (file-name-directory file))
3861                ;; If we have read a directory, we append the default file name.
3862                (when (file-directory-p file)
3863                  (setq file (expand-file-name (file-name-nondirectory
3864                                                default-name)
3865                                               (file-name-as-directory file))))
3866                ;; Possibly translate some characters.
3867                (nnheader-translate-file-chars file))))))
3868     (gnus-make-directory (file-name-directory result))
3869     (when variable
3870       (set variable result))
3871     (when dir-var
3872       (set dir-var (file-name-directory result)))
3873     result))
3874
3875 (defun gnus-article-archive-name (group)
3876   "Return the first instance of an \"Archive-name\" in the current buffer."
3877   (let ((case-fold-search t))
3878     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3879       (nnheader-concat gnus-article-save-directory
3880                        (match-string 1)))))
3881
3882 (defun gnus-article-nndoc-name (group)
3883   "If GROUP is an nndoc group, return the name of the parent group."
3884   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3885     (gnus-group-get-parameter group 'save-article-group)))
3886
3887 (defun gnus-summary-save-in-rmail (&optional filename)
3888   "Append this article to Rmail file.
3889 Optional argument FILENAME specifies file name.
3890 Directory to save to is default to `gnus-article-save-directory'."
3891   (setq filename (gnus-read-save-file-name
3892                   "Save %s in rmail file" filename
3893                   gnus-rmail-save-name gnus-newsgroup-name
3894                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3895   (with-current-buffer gnus-save-article-buffer
3896     (save-excursion
3897       (save-restriction
3898         (widen)
3899         ;; Note that unlike gnus-summary-save-in-mail, there is no
3900         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3901         ;; not use Babyl.
3902         (gnus-output-to-rmail filename))))
3903   filename)
3904
3905 (defun gnus-summary-save-in-mail (&optional filename)
3906   "Append this article to Unix mail file.
3907 Optional argument FILENAME specifies file name.
3908 Directory to save to is default to `gnus-article-save-directory'."
3909   (setq filename (gnus-read-save-file-name
3910                   "Save %s in Unix mail file" filename
3911                   gnus-mail-save-name gnus-newsgroup-name
3912                   gnus-current-headers 'gnus-newsgroup-last-mail))
3913   (with-current-buffer gnus-save-article-buffer
3914     (save-excursion
3915       (save-restriction
3916         (widen)
3917         (if (and (file-readable-p filename)
3918                  (file-regular-p filename)
3919                  (mail-file-babyl-p filename))
3920             (gnus-output-to-rmail filename)
3921           (gnus-output-to-mail filename)))))
3922   filename)
3923
3924 (put 'gnus-summary-save-in-file :decode t)
3925 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3926 (defun gnus-summary-save-in-file (&optional filename overwrite)
3927   "Append this article to file.
3928 Optional argument FILENAME specifies file name.
3929 Directory to save to is default to `gnus-article-save-directory'."
3930   (setq filename (gnus-read-save-file-name
3931                   "Save %s in file" filename
3932                   gnus-file-save-name gnus-newsgroup-name
3933                   gnus-current-headers 'gnus-newsgroup-last-file))
3934   (with-current-buffer gnus-save-article-buffer
3935     (save-excursion
3936       (save-restriction
3937         (widen)
3938         (when (and overwrite
3939                    (file-exists-p filename))
3940           (delete-file filename))
3941         (gnus-output-to-file filename))))
3942   filename)
3943
3944 (put 'gnus-summary-write-to-file :decode t)
3945 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
3946 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
3947 (defun gnus-summary-write-to-file (&optional filename)
3948   "Write this article to a file, overwriting it if the file exists.
3949 Optional argument FILENAME specifies file name.
3950 The directory to save in defaults to `gnus-article-save-directory'."
3951   (setq filename (gnus-read-save-file-name
3952                   "Save %s in file" filename
3953                   gnus-file-save-name gnus-newsgroup-name
3954                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3955   (gnus-summary-save-in-file filename t))
3956
3957 (put 'gnus-summary-save-body-in-file :decode t)
3958 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
3959   "Append this article body to a file.
3960 Optional argument FILENAME specifies file name.
3961 The directory to save in defaults to `gnus-article-save-directory'."
3962   (setq filename (gnus-read-save-file-name
3963                   "Save %s body in file" filename
3964                   gnus-file-save-name gnus-newsgroup-name
3965                   gnus-current-headers 'gnus-newsgroup-last-file))
3966   (with-current-buffer gnus-save-article-buffer
3967     (save-excursion
3968       (save-restriction
3969         (widen)
3970         (when (article-goto-body)
3971           (narrow-to-region (point) (point-max)))
3972         (when (and overwrite
3973                    (file-exists-p filename))
3974           (delete-file filename))
3975         (gnus-output-to-file filename))))
3976   filename)
3977
3978 (put 'gnus-summary-write-body-to-file :decode t)
3979 (put 'gnus-summary-write-body-to-file
3980      :function 'gnus-summary-save-body-in-file)
3981 (defun gnus-summary-write-body-to-file (&optional filename)
3982   "Write this article body to a file, overwriting it if the file exists.
3983 Optional argument FILENAME specifies file name.
3984 The directory to save in defaults to `gnus-article-save-directory'."
3985   (setq filename (gnus-read-save-file-name
3986                   "Save %s body in file" filename
3987                   gnus-file-save-name gnus-newsgroup-name
3988                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3989   (gnus-summary-save-body-in-file filename t))
3990
3991 (put 'gnus-summary-save-in-pipe :decode t)
3992 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
3993 (defun gnus-summary-save-in-pipe (&optional command raw)
3994   "Pipe this article to subprocess COMMAND.
3995 Valid values for COMMAND include:
3996   a string
3997     The executable command name and possibly arguments.
3998   nil
3999     You will be prompted for the command in the minibuffer.
4000   the symbol `default'
4001     It will be replaced with the command which the variable
4002     `gnus-summary-pipe-output-default-command' holds or the command
4003     last used for saving.
4004 Non-nil value for RAW overrides `:decode' and `:headers' properties
4005 and the raw article including all headers will be piped."
4006   (let ((article (gnus-summary-article-number))
4007         (decode (unless raw
4008                   (get 'gnus-summary-save-in-pipe :decode)))
4009         save-buffer default)
4010     (if article
4011         (if (vectorp (gnus-summary-article-header article))
4012             (save-current-buffer
4013               (gnus-summary-select-article decode decode nil article)
4014               (insert-buffer-substring
4015                (prog1
4016                    (if decode
4017                        gnus-article-buffer
4018                      gnus-original-article-buffer)
4019                  (setq save-buffer
4020                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4021               ;; Remove unwanted headers.
4022               (when (and (not raw)
4023                          (or (get 'gnus-summary-save-in-pipe :headers)
4024                              (not gnus-save-all-headers)))
4025                 (let ((gnus-visible-headers
4026                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4027                                               :headers))
4028                            gnus-saved-headers gnus-visible-headers))
4029                       (gnus-summary-buffer nil))
4030                   (article-hide-headers 1 t))))
4031           (error "%d is not a real article" article))
4032       (error "No article to pipe"))
4033     (setq default (or gnus-summary-pipe-output-default-command
4034                       gnus-last-shell-command))
4035     (unless (stringp command)
4036       (setq command
4037             (if (and (eq command 'default) default)
4038                 default
4039               (gnus-read-shell-command "Shell command on this article: "
4040                                        default))))
4041     (when (string-equal command "")
4042       (if default
4043           (setq command default)
4044         (error "A command is required")))
4045     (with-current-buffer save-buffer
4046       (save-restriction
4047         (widen)
4048         (shell-command-on-region (point-min) (point-max) command nil)))
4049     (gnus-kill-buffer save-buffer))
4050   (setq gnus-summary-pipe-output-default-command command))
4051
4052 (defun gnus-summary-pipe-to-muttprint (&optional command)
4053   "Pipe this article to muttprint."
4054   (unless (stringp command)
4055     (setq command (read-string
4056                    "Print using command: " gnus-summary-muttprint-program
4057                    nil gnus-summary-muttprint-program)))
4058   (let ((gnus-summary-pipe-output-default-command
4059          gnus-summary-pipe-output-default-command))
4060     (gnus-summary-save-in-pipe command))
4061   (setq gnus-summary-muttprint-program command))
4062
4063 ;;; Article file names when saving.
4064
4065 (defun gnus-capitalize-newsgroup (newsgroup)
4066   "Capitalize NEWSGROUP name."
4067   (when (not (zerop (length newsgroup)))
4068     (concat (char-to-string (upcase (aref newsgroup 0)))
4069             (substring newsgroup 1))))
4070
4071 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4072   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4073 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4074 Otherwise, it is like ~/News/news/group/num."
4075   (let ((default
4076           (expand-file-name
4077            (concat (if (gnus-use-long-file-name 'not-save)
4078                        (gnus-capitalize-newsgroup newsgroup)
4079                      (gnus-newsgroup-directory-form newsgroup))
4080                    "/" (int-to-string (mail-header-number headers)))
4081            gnus-article-save-directory)))
4082     (if (and last-file
4083              (string-equal (file-name-directory default)
4084                            (file-name-directory last-file))
4085              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4086         default
4087       (or last-file default))))
4088
4089 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4090   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4091 If variable `gnus-use-long-file-name' is non-nil, it is
4092 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4093   (let ((default
4094           (expand-file-name
4095            (concat (if (gnus-use-long-file-name 'not-save)
4096                        newsgroup
4097                      (gnus-newsgroup-directory-form newsgroup))
4098                    "/" (int-to-string (mail-header-number headers)))
4099            gnus-article-save-directory)))
4100     (if (and last-file
4101              (string-equal (file-name-directory default)
4102                            (file-name-directory last-file))
4103              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4104         default
4105       (or last-file default))))
4106
4107 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4108   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4109 If variable `gnus-use-long-file-name' is non-nil, it is
4110 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4111   (or last-file
4112       (expand-file-name
4113        (if (gnus-use-long-file-name 'not-save)
4114            newsgroup
4115          (file-relative-name
4116           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4117           default-directory))
4118        gnus-article-save-directory)))
4119
4120 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4121   "Generate file name from sender."
4122   (let ((from (mail-header-from headers)))
4123     (expand-file-name
4124      (if (and from (string-match "\\([^ <]+\\)@" from))
4125          (match-string 1 from)
4126        "nobody")
4127      gnus-article-save-directory)))
4128
4129 (defun article-verify-x-pgp-sig ()
4130   "Verify X-PGP-Sig."
4131   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4132   (interactive)
4133   (if (gnus-buffer-live-p gnus-original-article-buffer)
4134       (let ((sig (with-current-buffer gnus-original-article-buffer
4135                    (gnus-fetch-field "X-PGP-Sig")))
4136             items info headers)
4137         (when (and sig
4138                    mml2015-use
4139                    (mml2015-clear-verify-function))
4140           (with-temp-buffer
4141             (insert-buffer-substring gnus-original-article-buffer)
4142             (setq items (split-string sig))
4143             (message-narrow-to-head)
4144             (let ((inhibit-point-motion-hooks t)
4145                   (case-fold-search t))
4146               ;; Don't verify multiple headers.
4147               (setq headers (mapconcat (lambda (header)
4148                                          (concat header ": "
4149                                                  (mail-fetch-field header)
4150                                                  "\n"))
4151                                        (split-string (nth 1 items) ",") "")))
4152             (delete-region (point-min) (point-max))
4153             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4154             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4155             (insert headers)
4156             (widen)
4157             (forward-line)
4158             (while (not (eobp))
4159               (if (looking-at "^-")
4160                   (insert "- "))
4161               (forward-line))
4162             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4163             (insert "Version: " (car items) "\n\n")
4164             (insert (mapconcat 'identity (cddr items) "\n"))
4165             (insert "\n-----END PGP SIGNATURE-----\n")
4166             (let ((mm-security-handle (list (format "multipart/signed"))))
4167               (mml2015-clean-buffer)
4168               (let ((coding-system-for-write (or gnus-newsgroup-charset
4169                                                  'iso-8859-1)))
4170                 (funcall (mml2015-clear-verify-function)))
4171               (setq info
4172                     (or (mm-handle-multipart-ctl-parameter
4173                          mm-security-handle 'gnus-details)
4174                         (mm-handle-multipart-ctl-parameter
4175                          mm-security-handle 'gnus-info)))))
4176           (when info
4177             (let ((inhibit-read-only t) bface eface)
4178               (save-restriction
4179                 (message-narrow-to-head)
4180                 (goto-char (point-max))
4181                 (forward-line -1)
4182                 (setq bface (get-text-property (point-at-bol) 'face)
4183                       eface (get-text-property (1- (point-at-eol)) 'face))
4184                 (message-remove-header "X-Gnus-PGP-Verify")
4185                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4186                     (forward-line)
4187                   (goto-char (point-max)))
4188                 (narrow-to-region (point) (point))
4189                 (insert "X-Gnus-PGP-Verify: " info "\n")
4190                 (goto-char (point-min))
4191                 (forward-line)
4192                 (while (not (eobp))
4193                   (if (not (looking-at "^[ \t]"))
4194                       (insert " "))
4195                   (forward-line))
4196                 ;; Do highlighting.
4197                 (goto-char (point-min))
4198                 (when (looking-at "\\([^:]+\\): *")
4199                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4200                                      'face bface)
4201                   (put-text-property (match-end 0) (point-max)
4202                                      'face eface)))))))))
4203
4204 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4205
4206 (defun article-verify-cancel-lock ()
4207   "Verify Cancel-Lock header."
4208   (interactive)
4209   (if (gnus-buffer-live-p gnus-original-article-buffer)
4210       (canlock-verify gnus-original-article-buffer)))
4211
4212 (eval-and-compile
4213   (mapc
4214    (lambda (func)
4215      (let (afunc gfunc)
4216        (if (consp func)
4217            (setq afunc (car func)
4218                  gfunc (cdr func))
4219          (setq afunc func
4220                gfunc (intern (format "gnus-%s" func))))
4221        (defalias gfunc
4222          (when (fboundp afunc)
4223            `(lambda (&optional interactive &rest args)
4224               ,(documentation afunc t)
4225               (interactive (list t))
4226               (with-current-buffer gnus-article-buffer
4227                 (if interactive
4228                     (call-interactively ',afunc)
4229                   (apply ',afunc args))))))))
4230    '(article-hide-headers
4231      article-verify-x-pgp-sig
4232      article-verify-cancel-lock
4233      article-hide-boring-headers
4234      article-treat-overstrike
4235      article-treat-ansi-sequences
4236      article-fill-long-lines
4237      article-capitalize-sentences
4238      article-remove-cr
4239      article-remove-leading-whitespace
4240      article-display-x-face
4241      article-display-face
4242      article-de-quoted-unreadable
4243      article-de-base64-unreadable
4244      article-decode-HZ
4245      article-wash-html
4246      article-unsplit-urls
4247      article-hide-list-identifiers
4248      article-strip-banner
4249      article-babel
4250      article-hide-pem
4251      article-hide-signature
4252      article-strip-headers-in-body
4253      article-remove-trailing-blank-lines
4254      article-strip-leading-blank-lines
4255      article-strip-multiple-blank-lines
4256      article-strip-leading-space
4257      article-strip-trailing-space
4258      article-strip-blank-lines
4259      article-strip-all-blank-lines
4260      article-date-local
4261      article-date-english
4262      article-date-iso8601
4263      article-date-original
4264      article-date-ut
4265      article-decode-mime-words
4266      article-decode-charset
4267      article-decode-encoded-words
4268      article-date-user
4269      article-date-lapsed
4270      article-emphasize
4271      article-treat-dumbquotes
4272      article-treat-non-ascii
4273      article-normalize-headers
4274      ;;(article-show-all . gnus-article-show-all-headers)
4275      )))
4276 \f
4277 ;;;
4278 ;;; Gnus article mode
4279 ;;;
4280
4281 (put 'gnus-article-mode 'mode-class 'special)
4282
4283 (set-keymap-parent gnus-article-mode-map widget-keymap)
4284
4285 (gnus-define-keys gnus-article-mode-map
4286   " " gnus-article-goto-next-page
4287   "\177" gnus-article-goto-prev-page
4288   [delete] gnus-article-goto-prev-page
4289   [backspace] gnus-article-goto-prev-page
4290   "\C-c^" gnus-article-refer-article
4291   "h" gnus-article-show-summary
4292   "s" gnus-article-show-summary
4293   "\C-c\C-m" gnus-article-mail
4294   "?" gnus-article-describe-briefly
4295   "<" beginning-of-buffer
4296   ">" end-of-buffer
4297   "\C-c\C-i" gnus-info-find-node
4298   "\C-c\C-b" gnus-bug
4299   "R" gnus-article-reply-with-original
4300   "F" gnus-article-followup-with-original
4301   "\C-hk" gnus-article-describe-key
4302   "\C-hc" gnus-article-describe-key-briefly
4303   "\C-hb" gnus-article-describe-bindings
4304
4305   "e" gnus-article-read-summary-keys
4306   "\C-d" gnus-article-read-summary-keys
4307   "\M-*" gnus-article-read-summary-keys
4308   "\M-#" gnus-article-read-summary-keys
4309   "\M-^" gnus-article-read-summary-keys
4310   "\M-g" gnus-article-read-summary-keys)
4311
4312 (substitute-key-definition
4313  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4314
4315 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4316   "W" gnus-article-wide-reply-with-original)
4317 (if (featurep 'xemacs)
4318     (set-keymap-default-binding gnus-article-send-map
4319                                 'gnus-article-read-summary-send-keys)
4320   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4321
4322 (defun gnus-article-make-menu-bar ()
4323   (unless (boundp 'gnus-article-commands-menu)
4324     (gnus-summary-make-menu-bar))
4325   (unless (boundp 'gnus-article-article-menu)
4326     (easy-menu-define
4327      gnus-article-article-menu gnus-article-mode-map ""
4328      '("Article"
4329        ["Scroll forwards" gnus-article-goto-next-page t]
4330        ["Scroll backwards" gnus-article-goto-prev-page t]
4331        ["Show summary" gnus-article-show-summary t]
4332        ["Fetch Message-ID at point" gnus-article-refer-article t]
4333        ["Mail to address at point" gnus-article-mail t]
4334        ["Send a bug report" gnus-bug t]))
4335
4336     (easy-menu-define
4337      gnus-article-treatment-menu gnus-article-mode-map ""
4338      ;; Fixme: this should use :active (and maybe :visible).
4339      '("Treatment"
4340        ["Hide headers" gnus-article-hide-headers t]
4341        ["Hide signature" gnus-article-hide-signature t]
4342        ["Hide citation" gnus-article-hide-citation t]
4343        ["Treat overstrike" gnus-article-treat-overstrike t]
4344        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4345        ["Remove carriage return" gnus-article-remove-cr t]
4346        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4347        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4348        ["Remove base64" gnus-article-de-base64-unreadable t]
4349        ["Treat html" gnus-article-wash-html t]
4350        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4351        ["Decode HZ" gnus-article-decode-HZ t]))
4352
4353     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4354
4355     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4356
4357     (gnus-run-hooks 'gnus-article-menu-hook)))
4358
4359 (defvar bookmark-make-record-function)
4360
4361 (defun gnus-article-mode ()
4362   "Major mode for displaying an article.
4363
4364 All normal editing commands are switched off.
4365
4366 The following commands are available in addition to all summary mode
4367 commands:
4368 \\<gnus-article-mode-map>
4369 \\[gnus-article-next-page]\t Scroll the article one page forwards
4370 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4371 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4372 \\[gnus-article-show-summary]\t Display the summary buffer
4373 \\[gnus-article-mail]\t Send a reply to the address near point
4374 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4375 \\[gnus-info-find-node]\t Go to the Gnus info node"
4376   (interactive)
4377   (kill-all-local-variables)
4378   (gnus-simplify-mode-line)
4379   (setq mode-name "Article")
4380   (setq major-mode 'gnus-article-mode)
4381   (make-local-variable 'minor-mode-alist)
4382   (use-local-map gnus-article-mode-map)
4383   (when (gnus-visual-p 'article-menu 'menu)
4384     (gnus-article-make-menu-bar)
4385     (when gnus-summary-tool-bar-map
4386       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4387   (gnus-update-format-specifications nil 'article-mode)
4388   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4389   (set (make-local-variable 'gnus-page-broken) nil)
4390   (make-local-variable 'gnus-button-marker-list)
4391   (make-local-variable 'gnus-article-current-summary)
4392   (make-local-variable 'gnus-article-mime-handles)
4393   (make-local-variable 'gnus-article-decoded-p)
4394   (make-local-variable 'gnus-article-mime-handle-alist)
4395   (make-local-variable 'gnus-article-wash-types)
4396   (make-local-variable 'gnus-article-image-alist)
4397   (make-local-variable 'gnus-article-charset)
4398   (make-local-variable 'gnus-article-ignored-charsets)
4399   (set (make-local-variable 'bookmark-make-record-function)
4400        'gnus-summary-bookmark-make-record)
4401   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4402   ;; face.
4403   (set (make-local-variable 'nobreak-char-display) nil)
4404   (setq cursor-in-non-selected-windows nil)
4405   (gnus-set-default-directory)
4406   (buffer-disable-undo)
4407   (setq buffer-read-only t
4408         show-trailing-whitespace nil)
4409   (set-syntax-table gnus-article-mode-syntax-table)
4410   (mm-enable-multibyte)
4411   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4412
4413 (defvar gnus-button-marker-list nil
4414   "Regexp matching any of the regexps from `gnus-button-alist'.
4415 Internal variable.")
4416
4417 (defun gnus-article-setup-buffer ()
4418   "Initialize the article buffer."
4419   (let* ((name (if gnus-single-article-buffer "*Article*"
4420                  (concat "*Article " gnus-newsgroup-name "*")))
4421          (original
4422           (progn (string-match "\\*Article" name)
4423                  (concat " *Original Article"
4424                          (substring name (match-end 0))))))
4425     (setq gnus-article-buffer name)
4426     (setq gnus-original-article-buffer original)
4427     (setq gnus-article-mime-handle-alist nil)
4428     (with-current-buffer gnus-summary-buffer
4429       ;; This might be a variable local to the summary buffer.
4430       (unless gnus-single-article-buffer
4431         (setq gnus-article-buffer name)
4432         (setq gnus-original-article-buffer original)
4433         (gnus-set-global-variables)))
4434     (gnus-article-setup-highlight-words)
4435     ;; Init original article buffer.
4436     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4437       (mm-enable-multibyte)
4438       (setq major-mode 'gnus-original-article-mode)
4439       (make-local-variable 'gnus-original-article))
4440     (if (and (get-buffer name)
4441              (with-current-buffer name
4442                (if gnus-article-edit-mode
4443                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4444                        (progn
4445                          (set-buffer-modified-p nil)
4446                          (gnus-kill-buffer name)
4447                          (message "")
4448                          nil)
4449                      (error "Action aborted"))
4450                  t)))
4451         (with-current-buffer name
4452           (set (make-local-variable 'gnus-article-edit-mode) nil)
4453           (when gnus-article-mime-handles
4454             (mm-destroy-parts gnus-article-mime-handles)
4455             (setq gnus-article-mime-handles nil))
4456           ;; Set it to nil in article-buffer!
4457           (setq gnus-article-mime-handle-alist nil)
4458           (buffer-disable-undo)
4459           (setq buffer-read-only t)
4460           ;; This list just keeps growing if we don't reset it.
4461           (setq gnus-button-marker-list nil)
4462           (unless (eq major-mode 'gnus-article-mode)
4463             (gnus-article-mode))
4464           (setq truncate-lines gnus-article-truncate-lines)
4465           (current-buffer))
4466       (with-current-buffer (gnus-get-buffer-create name)
4467         (gnus-article-mode)
4468         (setq truncate-lines gnus-article-truncate-lines)
4469         (make-local-variable 'gnus-summary-buffer)
4470         (setq gnus-summary-buffer
4471               (gnus-summary-buffer-name gnus-newsgroup-name))
4472         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4473         (current-buffer)))))
4474
4475 ;; Set article window start at LINE, where LINE is the number of lines
4476 ;; from the head of the article.
4477 (defun gnus-article-set-window-start (&optional line)
4478   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4479     (when article-window
4480       (set-window-start
4481        article-window
4482        (with-current-buffer gnus-article-buffer
4483          (goto-char (point-min))
4484          (if (not line)
4485              (point-min)
4486            (gnus-message 6 "Moved to bookmark")
4487            (search-forward "\n\n" nil t)
4488            (forward-line line)
4489            (point)))))))
4490
4491 (defun gnus-article-prepare (article &optional all-headers header)
4492   "Prepare ARTICLE in article mode buffer.
4493 ARTICLE should either be an article number or a Message-ID.
4494 If ARTICLE is an id, HEADER should be the article headers.
4495 If ALL-HEADERS is non-nil, no headers are hidden."
4496   (save-excursion
4497     ;; Make sure we start in a summary buffer.
4498     (unless (eq major-mode 'gnus-summary-mode)
4499       (set-buffer gnus-summary-buffer))
4500     (setq gnus-summary-buffer (current-buffer))
4501     (let* ((gnus-article (if header (mail-header-number header) article))
4502            (summary-buffer (current-buffer))
4503            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4504            (group gnus-newsgroup-name)
4505            result)
4506       (save-excursion
4507         (gnus-article-setup-buffer)
4508         (set-buffer gnus-article-buffer)
4509         ;; Deactivate active regions.
4510         (when (and (boundp 'transient-mark-mode)
4511                    transient-mark-mode)
4512           (setq mark-active nil))
4513         (if (not (setq result (let ((inhibit-read-only t))
4514                                 (gnus-request-article-this-buffer
4515                                  article group))))
4516             ;; There is no such article.
4517             (save-excursion
4518               (when (and (numberp article)
4519                          (not (memq article gnus-newsgroup-sparse)))
4520                 (setq gnus-article-current
4521                       (cons gnus-newsgroup-name article))
4522                 (set-buffer gnus-summary-buffer)
4523                 (setq gnus-current-article article)
4524                 (if (and (memq article gnus-newsgroup-undownloaded)
4525                          (not (gnus-online (gnus-find-method-for-group
4526                                             gnus-newsgroup-name))))
4527                     (progn
4528                       (gnus-summary-set-agent-mark article)
4529                       (message "Message marked for downloading"))
4530                   (gnus-summary-mark-article article gnus-canceled-mark)
4531                   (unless (memq article gnus-newsgroup-sparse)
4532                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4533           (if (or (eq result 'pseudo)
4534                   (eq result 'nneething))
4535               (progn
4536                 (with-current-buffer summary-buffer
4537                   (push article gnus-newsgroup-history)
4538                   (setq gnus-last-article gnus-current-article
4539                         gnus-current-article 0
4540                         gnus-current-headers nil
4541                         gnus-article-current nil)
4542                   (if (eq result 'nneething)
4543                       (gnus-configure-windows 'summary)
4544                     (gnus-configure-windows 'article))
4545                   (gnus-set-global-variables))
4546                 (let ((gnus-article-mime-handle-alist-1
4547                        gnus-article-mime-handle-alist))
4548                   (gnus-set-mode-line 'article)))
4549             ;; The result from the `request' was an actual article -
4550             ;; or at least some text that is now displayed in the
4551             ;; article buffer.
4552             (when (and (numberp article)
4553                        (not (eq article gnus-current-article)))
4554               ;; Seems like a new article has been selected.
4555               ;; `gnus-current-article' must be an article number.
4556               (with-current-buffer summary-buffer
4557                 (push article gnus-newsgroup-history)
4558                 (setq gnus-last-article gnus-current-article
4559                       gnus-current-article article
4560                       gnus-current-headers
4561                       (gnus-summary-article-header gnus-current-article)
4562                       gnus-article-current
4563                       (cons gnus-newsgroup-name gnus-current-article))
4564                 (unless (vectorp gnus-current-headers)
4565                   (setq gnus-current-headers nil))
4566                 (gnus-summary-goto-subject gnus-current-article)
4567                 (when (gnus-summary-show-thread)
4568                   ;; If the summary buffer really was folded, the
4569                   ;; previous goto may not actually have gone to
4570                   ;; the right article, but the thread root instead.
4571                   ;; So we go again.
4572                   (gnus-summary-goto-subject gnus-current-article))
4573                 (gnus-run-hooks 'gnus-mark-article-hook)
4574                 (gnus-set-mode-line 'summary)
4575                 (when (gnus-visual-p 'article-highlight 'highlight)
4576                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4577                 ;; Set the global newsgroup variables here.
4578                 (gnus-set-global-variables)
4579                 (setq gnus-have-all-headers
4580                       (or all-headers gnus-show-all-headers))))
4581             (save-excursion
4582               (gnus-configure-windows 'article))
4583             (when (or (numberp article)
4584                       (stringp article))
4585               (gnus-article-prepare-display)
4586               ;; Do page break.
4587               (goto-char (point-min))
4588               (when gnus-break-pages
4589                 (gnus-narrow-to-page)))
4590             (let ((gnus-article-mime-handle-alist-1
4591                    gnus-article-mime-handle-alist))
4592               (gnus-set-mode-line 'article))
4593             (article-goto-body)
4594             (unless (bobp)
4595               (forward-line -1))
4596             (set-window-point (get-buffer-window (current-buffer)) (point))
4597             (gnus-configure-windows 'article)
4598             t))))))
4599
4600 ;;;###autoload
4601 (defun gnus-article-prepare-display ()
4602   "Make the current buffer look like a nice article."
4603   ;; Hooks for getting information from the article.
4604   ;; This hook must be called before being narrowed.
4605   (let ((gnus-article-buffer (current-buffer))
4606         buffer-read-only
4607         (inhibit-read-only t))
4608     (unless (eq major-mode 'gnus-article-mode)
4609       (gnus-article-mode))
4610     (setq buffer-read-only nil
4611           gnus-article-wash-types nil
4612           gnus-article-image-alist nil)
4613     (gnus-run-hooks 'gnus-tmp-internal-hook)
4614     (when gnus-display-mime-function
4615       (funcall gnus-display-mime-function))
4616     (gnus-run-hooks 'gnus-article-prepare-hook)))
4617
4618 ;;;
4619 ;;; Gnus Sticky Article Mode
4620 ;;;
4621
4622 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4623   "Mode for sticky articles."
4624   ;; Release bindings that won't work.
4625   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4626                              gnus-sticky-article-mode-map)
4627   (substitute-key-definition 'gnus-article-refer-article 'undefined
4628                              gnus-sticky-article-mode-map)
4629   (dolist (k '("e" "h" "s" "F" "R"))
4630     (define-key gnus-sticky-article-mode-map k nil))
4631   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4632   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4633   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4634   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4635
4636 (defun gnus-sticky-article (arg)
4637   "Make the current article sticky.
4638 If a prefix ARG is given, ask for a name for this sticky article buffer."
4639   (interactive "P")
4640   (gnus-summary-show-thread)
4641   (gnus-summary-select-article nil nil 'pseudo)
4642   (let (new-art-buf-name)
4643     (gnus-eval-in-buffer-window gnus-article-buffer
4644       (setq new-art-buf-name
4645             (concat
4646              "*Sticky Article: "
4647              (if arg
4648                  (read-from-minibuffer "Sticky article buffer name: ")
4649                (gnus-with-article-headers
4650                  (gnus-article-goto-header "subject")
4651                  (setq new-art-buf-name
4652                        (buffer-substring-no-properties
4653                         (line-beginning-position) (line-end-position)))
4654                  (goto-char (point-min))
4655                  (gnus-article-goto-header "from")
4656                  (setq new-art-buf-name
4657                        (concat
4658                         new-art-buf-name ", "
4659                         (buffer-substring-no-properties
4660                          (line-beginning-position) (line-end-position))))
4661                  (goto-char (point-min))
4662                  (gnus-article-goto-header "date")
4663                  (setq new-art-buf-name
4664                        (concat
4665                         new-art-buf-name ", "
4666                         (buffer-substring-no-properties
4667                          (line-beginning-position) (line-end-position))))))
4668              "*"))
4669       (if (and (gnus-buffer-live-p new-art-buf-name)
4670                (with-current-buffer new-art-buf-name
4671                  (eq major-mode 'gnus-sticky-article-mode)))
4672           (switch-to-buffer new-art-buf-name)
4673         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4674       (gnus-sticky-article-mode))
4675     (setq gnus-article-buffer new-art-buf-name))
4676   (gnus-summary-recenter)
4677   (gnus-summary-position-point))
4678
4679 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4680   "Kill the given sticky article BUFFER.
4681 If none is given, assume the current buffer and kill it if it has
4682 `gnus-sticky-article-mode'."
4683   (interactive)
4684   (unless buffer
4685     (setq buffer (current-buffer)))
4686   (with-current-buffer buffer
4687     (when (eq major-mode 'gnus-sticky-article-mode)
4688       (gnus-kill-buffer buffer))))
4689
4690 (defun gnus-kill-sticky-article-buffers (arg)
4691   "Kill all sticky article buffers.
4692 If a prefix ARG is given, ask for confirmation."
4693   (interactive "P")
4694   (dolist (buf (gnus-buffers))
4695     (with-current-buffer buf
4696       (when (eq major-mode 'gnus-sticky-article-mode)
4697         (if (not arg)
4698             (gnus-kill-buffer buf)
4699           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4700             (gnus-kill-buffer buf)))))))
4701
4702 ;;;
4703 ;;; Gnus MIME viewing functions
4704 ;;;
4705
4706 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4707   "Format of the MIME buttons.
4708
4709 Valid specifiers include:
4710 %t  The MIME type
4711 %T  MIME type, along with additional info
4712 %n  The `name' parameter
4713 %d  The description, if any
4714 %l  The length of the encoded part
4715 %p  The part identifier number
4716 %e  Dots if the part isn't displayed
4717
4718 General format specifiers can also be used.  See Info node
4719 `(gnus)Formatting Variables'.")
4720
4721 (defvar gnus-mime-button-line-format-alist
4722   '((?t gnus-tmp-type ?s)
4723     (?T gnus-tmp-type-long ?s)
4724     (?n gnus-tmp-name ?s)
4725     (?d gnus-tmp-description ?s)
4726     (?p gnus-tmp-id ?s)
4727     (?l gnus-tmp-length ?d)
4728     (?e gnus-tmp-dots ?s)))
4729
4730 (defvar gnus-mime-button-commands
4731   '((gnus-article-press-button "\r" "Toggle Display")
4732     (gnus-mime-view-part "v" "View Interactively...")
4733     (gnus-mime-view-part-as-type "t" "View As Type...")
4734     (gnus-mime-view-part-as-charset "C" "View As charset...")
4735     (gnus-mime-save-part "o" "Save...")
4736     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4737     (gnus-mime-replace-part "r" "Replace part")
4738     (gnus-mime-delete-part "d" "Delete part")
4739     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4740     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4741     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4742     (gnus-mime-view-part-externally "e" "View Externally")
4743     (gnus-mime-print-part "p" "Print")
4744     (gnus-mime-pipe-part "|" "Pipe To Command...")
4745     (gnus-mime-action-on-part "." "Take action on the part...")))
4746
4747 (defun gnus-article-mime-part-status ()
4748   (if gnus-article-mime-handle-alist-1
4749       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4750           " (1 part)"
4751         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4752     ""))
4753
4754 (defvar gnus-mime-button-map
4755   (let ((map (make-sparse-keymap)))
4756     (define-key map gnus-mouse-2 'gnus-article-push-button)
4757     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4758     (dolist (c gnus-mime-button-commands)
4759       (define-key map (cadr c) (car c)))
4760     map))
4761
4762 (easy-menu-define
4763   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4764   `("MIME Part"
4765     ,@(mapcar (lambda (c)
4766                 (vector (caddr c) (car c) :active t))
4767               gnus-mime-button-commands)))
4768
4769 (defvar gnus-url-button-commands
4770   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4771
4772 (defvar gnus-url-button-map
4773   (let ((map (make-sparse-keymap)))
4774     (dolist (c gnus-url-button-commands)
4775       (define-key map (cadr c) (car c)))
4776     map))
4777
4778 (easy-menu-define
4779   gnus-url-button-menu gnus-url-button-map "URL button menu."
4780   `("Url Button"
4781     ,@(mapcar (lambda (c)
4782                 (vector (caddr c) (car c) :active t))
4783               gnus-url-button-commands)))
4784
4785 (defmacro gnus-bind-safe-url-regexp (&rest body)
4786   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4787   `(let ((mm-w3m-safe-url-regexp
4788           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4789                                 (gnus-buffer-live-p
4790                                  gnus-article-current-summary))
4791                            (with-current-buffer gnus-article-current-summary
4792                              gnus-newsgroup-name)
4793                          gnus-newsgroup-name)))
4794             (if (cond ((not group)
4795                        ;; Maybe we're in a mml-preview buffer
4796                        ;; and no group is selected.
4797                        t)
4798                       ((stringp gnus-safe-html-newsgroups)
4799                        (string-match gnus-safe-html-newsgroups group))
4800                       ((consp gnus-safe-html-newsgroups)
4801                        (member group gnus-safe-html-newsgroups)))
4802                 nil
4803               mm-w3m-safe-url-regexp))))
4804      ,@body))
4805
4806 (defun gnus-mime-button-menu (event prefix)
4807  "Construct a context-sensitive menu of MIME commands."
4808  (interactive "e\nP")
4809  (save-window-excursion
4810    (let ((pos (event-start event)))
4811      (select-window (posn-window pos))
4812      (goto-char (posn-point pos))
4813      (gnus-article-check-buffer)
4814      (popup-menu gnus-mime-button-menu nil prefix))))
4815
4816 (defun gnus-mime-view-all-parts (&optional handles)
4817   "View all the MIME parts."
4818   (interactive)
4819   (with-current-buffer gnus-article-buffer
4820     (let ((handles (or handles gnus-article-mime-handles))
4821           (mail-parse-charset gnus-newsgroup-charset)
4822           (mail-parse-ignored-charsets
4823            (with-current-buffer gnus-summary-buffer
4824              gnus-newsgroup-ignored-charsets)))
4825       (when handles
4826         (mm-remove-parts handles)
4827         (goto-char (point-min))
4828         (or (search-forward "\n\n") (goto-char (point-max)))
4829         (let ((inhibit-read-only t))
4830           (delete-region (point) (point-max))
4831           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4832
4833 (defun gnus-article-jump-to-part (n)
4834   "Jump to MIME part N."
4835   (interactive "P")
4836   (let ((parts (with-current-buffer gnus-article-buffer
4837                  (length gnus-article-mime-handle-alist))))
4838     (when (zerop parts)
4839       (error "No such part"))
4840     (pop-to-buffer gnus-article-buffer)
4841     ;; FIXME: why is it necessary?
4842     (sit-for 0)
4843     (or n
4844         (setq n (if (= parts 1)
4845                     1
4846                   (read-number (format "Jump to part (1..%s): " parts)))))
4847     (unless (and (integerp n) (<= n parts) (>= n 1))
4848       (setq n
4849             (progn
4850               (gnus-message 7 "Invalid part `%s', using %s instead."
4851                             n parts)
4852               parts)))
4853     (gnus-message 9 "Jumping to part %s." n)
4854     (cond ((>= gnus-auto-select-part 1)
4855            (while (and (<= n parts)
4856                        (not (gnus-article-goto-part n)))
4857              (setq n (1+ n))))
4858           ((< gnus-auto-select-part 0)
4859            (while (and (>= n 1)
4860                        (not (gnus-article-goto-part n)))
4861              (setq n (1- n))))
4862           (t
4863            (gnus-article-goto-part n)))))
4864
4865 (defvar gnus-mime-buttonized-part-id nil
4866   "ID of a mime part that should be buttonized.
4867 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4868
4869 (eval-when-compile
4870   (defsubst gnus-article-edit-part (handles &optional current-id)
4871     "Edit an article in order to delete a mime part.
4872 This function is exclusively used by `gnus-mime-save-part-and-strip'
4873 and `gnus-mime-delete-part', and not provided at run-time normally."
4874     (gnus-article-edit-article
4875      `(lambda ()
4876         (buffer-disable-undo)
4877         (erase-buffer)
4878         (let ((mail-parse-charset (or gnus-article-charset
4879                                       ',gnus-newsgroup-charset))
4880               (mail-parse-ignored-charsets
4881                (or gnus-article-ignored-charsets
4882                    ',gnus-newsgroup-ignored-charsets))
4883               (mbl mml-buffer-list))
4884           (setq mml-buffer-list nil)
4885           (insert-buffer-substring gnus-original-article-buffer)
4886           (mime-to-mml ',handles)
4887           (setq gnus-article-mime-handles nil)
4888           (let ((mbl1 mml-buffer-list))
4889             (setq mml-buffer-list mbl)
4890             (set (make-local-variable 'mml-buffer-list) mbl1))
4891           (gnus-make-local-hook 'kill-buffer-hook)
4892           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4893      `(lambda (no-highlight)
4894         (let ((mail-parse-charset (or gnus-article-charset
4895                                       ',gnus-newsgroup-charset))
4896               (message-options message-options)
4897               (message-options-set-recipient)
4898               (mail-parse-ignored-charsets
4899                (or gnus-article-ignored-charsets
4900                    ',gnus-newsgroup-ignored-charsets)))
4901           (mml-to-mime)
4902           (mml-destroy-buffers)
4903           (remove-hook 'kill-buffer-hook
4904                        'mml-destroy-buffers t)
4905           (kill-local-variable 'mml-buffer-list))
4906         (gnus-summary-edit-article-done
4907          ,(or (mail-header-references gnus-current-headers) "")
4908          ,(gnus-group-read-only-p)
4909          ,gnus-summary-buffer no-highlight))
4910      t)
4911     ;; Force buttonizing this part.
4912     (let ((gnus-mime-buttonized-part-id current-id))
4913       (gnus-article-edit-done))
4914     (gnus-configure-windows 'article)
4915     (when (and current-id (integerp gnus-auto-select-part))
4916       (gnus-article-jump-to-part
4917        (min (max (+ current-id gnus-auto-select-part) 1)
4918             (with-current-buffer gnus-article-buffer
4919               (length gnus-article-mime-handle-alist)))))))
4920
4921 (defun gnus-mime-replace-part (file)
4922   "Replace MIME part under point with an external body."
4923   ;; Useful if file has already been saved to disk
4924   (interactive
4925    (list
4926     (read-file-name "Replace MIME part with file: "
4927                     (or mm-default-directory default-directory)
4928                     nil nil)))
4929   (gnus-mime-save-part-and-strip file))
4930
4931 (defun gnus-mime-save-part-and-strip (&optional file)
4932   "Save the MIME part under point then replace it with an external body.
4933 If FILE is given, use it for the external part."
4934   (interactive)
4935   (gnus-article-check-buffer)
4936   (when (gnus-group-read-only-p)
4937     (error "The current group does not support deleting of parts"))
4938   (when (mm-complicated-handles gnus-article-mime-handles)
4939     (error "\
4940 The current article has a complicated MIME structure, giving up..."))
4941   (let* ((data (get-text-property (point) 'gnus-data))
4942          (id (get-text-property (point) 'gnus-part))
4943          param
4944          (handles gnus-article-mime-handles))
4945     (unless file
4946       (setq file
4947             (and data (mm-save-part data "Delete MIME part and save to: "))))
4948     (when file
4949       (with-current-buffer (mm-handle-buffer data)
4950         (erase-buffer)
4951         (insert "Content-Type: " (mm-handle-media-type data))
4952         (mml-insert-parameter-string (cdr (mm-handle-type data))
4953                                      '(charset))
4954         ;; Add a filename for the sake of saving the part again.
4955         (mml-insert-parameter
4956          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
4957         (insert "\n")
4958         (insert "Content-ID: " (message-make-message-id) "\n")
4959         (insert "Content-Transfer-Encoding: binary\n")
4960         (insert "\n"))
4961       (setcdr data
4962               (cdr (mm-make-handle nil
4963                                    `("message/external-body"
4964                                      (access-type . "LOCAL-FILE")
4965                                      (name . ,file)))))
4966       ;; (set-buffer gnus-summary-buffer)
4967       (gnus-article-edit-part handles id))))
4968
4969 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4970 ;; parts...>') but with stripping would be nice.
4971
4972 (defun gnus-mime-delete-part ()
4973   "Delete the MIME part under point.
4974 Replace it with some information about the removed part."
4975   (interactive)
4976   (gnus-article-check-buffer)
4977   (when (gnus-group-read-only-p)
4978     (error "The current group does not support deleting of parts"))
4979   (when (mm-complicated-handles gnus-article-mime-handles)
4980     (error "\
4981 The current article has a complicated MIME structure, giving up..."))
4982   (when (or gnus-expert-user
4983             (gnus-yes-or-no-p "\
4984 Deleting parts may malfunction or destroy the article; continue? "))
4985     (let* ((data (get-text-property (point) 'gnus-data))
4986            (id (get-text-property (point) 'gnus-part))
4987            (handles gnus-article-mime-handles)
4988            (none "(none)")
4989            (description
4990             (let ((desc (mm-handle-description data)))
4991               (when desc
4992                 (mail-decode-encoded-word-string desc))))
4993            (filename
4994             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4995                 none))
4996            (type (mm-handle-media-type data)))
4997       (unless data
4998         (error "No MIME part under point"))
4999       (with-current-buffer (mm-handle-buffer data)
5000         (let ((bsize (buffer-size)))
5001           (erase-buffer)
5002           (insert
5003            (concat
5004             ",----\n"
5005             "| The following attachment has been deleted:\n"
5006             "|\n"
5007             "| Type:           " type "\n"
5008             "| Filename:       " filename "\n"
5009             "| Size (encoded): " (format "%s byte%s\n"
5010                                          bsize (if (= bsize 1)
5011                                                    ""
5012                                                  "s"))
5013             (when description
5014               (concat    "| Description:    " description "\n"))
5015             "`----\n"))
5016           (setcdr data
5017                   (cdr (mm-make-handle
5018                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5019                         (list "attachment")
5020                         (format "Deleted attachment (%s bytes)" bsize))))))
5021       ;; (set-buffer gnus-summary-buffer)
5022       (gnus-article-edit-part handles id))))
5023
5024 (defun gnus-mime-save-part ()
5025   "Save the MIME part under point."
5026   (interactive)
5027   (gnus-article-check-buffer)
5028   (let ((data (get-text-property (point) 'gnus-data)))
5029     (when data
5030       (mm-save-part data))))
5031
5032 (defun gnus-mime-pipe-part (&optional cmd)
5033   "Pipe the MIME part under point to a process.
5034 Use CMD as the process."
5035   (interactive)
5036   (gnus-article-check-buffer)
5037   (let ((data (get-text-property (point) 'gnus-data)))
5038     (when data
5039       (mm-pipe-part data cmd))))
5040
5041 (defun gnus-mime-view-part ()
5042   "Interactively choose a viewing method for the MIME part under point."
5043   (interactive)
5044   (gnus-article-check-buffer)
5045   (let ((data (get-text-property (point) 'gnus-data)))
5046     (when data
5047       (setq gnus-article-mime-handles
5048             (mm-merge-handles
5049              gnus-article-mime-handles (setq data (copy-sequence data))))
5050       (mm-interactively-view-part data))))
5051
5052 (defun gnus-mime-view-part-as-type-internal ()
5053   (gnus-article-check-buffer)
5054   (let* ((handle (get-text-property (point) 'gnus-data))
5055          (name (or
5056                 ;; Content-Type: foo/bar; name=...
5057                 (mail-content-type-get (mm-handle-type handle) 'name)
5058                 ;; Content-Disposition: attachment; filename=...
5059                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5060          (def-type (and name (mm-default-file-encoding name))))
5061     (or (and def-type (cons def-type 0))
5062         (and handle
5063              (equal (mm-handle-media-supertype handle) "text")
5064              '("text/plain" . 0))
5065         '("application/octet-stream" . 0))))
5066
5067 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5068   "Choose a MIME media type, and view the part as such.
5069 If non-nil, PRED is a predicate to use during completion to limit the
5070 available media-types."
5071   (interactive)
5072   (unless mime-type
5073     (setq mime-type
5074           (let ((default (gnus-mime-view-part-as-type-internal)))
5075             (gnus-completing-read
5076              "View as MIME type"
5077              (if pred
5078                  (gnus-remove-if-not pred (mailcap-mime-types))
5079                (mailcap-mime-types))
5080              nil nil nil
5081              (car default)))))
5082   (gnus-article-check-buffer)
5083   (let ((handle (get-text-property (point) 'gnus-data)))
5084     (when handle
5085       (when (equal (mm-handle-media-type handle) "message/external-body")
5086         (unless (mm-handle-cache handle)
5087           (mm-extern-cache-contents handle))
5088         (setq handle (mm-handle-cache handle)))
5089       (setq handle
5090             (mm-make-handle (mm-handle-buffer handle)
5091                             (cons mime-type (cdr (mm-handle-type handle)))
5092                             (mm-handle-encoding handle)
5093                             (mm-handle-undisplayer handle)
5094                             (mm-handle-disposition handle)
5095                             (mm-handle-description handle)
5096                             nil
5097                             (mm-handle-id handle)))
5098       (setq gnus-article-mime-handles
5099             (mm-merge-handles gnus-article-mime-handles handle))
5100       (when (mm-handle-displayed-p handle)
5101         (mm-remove-part handle))
5102       (gnus-mm-display-part handle))))
5103
5104 (defun gnus-mime-copy-part (&optional handle arg)
5105   "Put the MIME part under point into a new buffer.
5106 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5107 are decompressed."
5108   (interactive (list nil current-prefix-arg))
5109   (gnus-article-check-buffer)
5110   (unless handle
5111     (setq handle (get-text-property (point) 'gnus-data)))
5112   (when handle
5113     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5114                                                'name)
5115                         (mail-content-type-get (mm-handle-disposition handle)
5116                                                'filename)))
5117           contents dont-decode charset coding-system)
5118       (mm-with-unibyte-buffer
5119         (mm-insert-part handle)
5120         (setq contents (or (condition-case nil
5121                                (mm-decompress-buffer filename nil 'sig)
5122                              (error
5123                               (setq dont-decode t)
5124                               nil))
5125                            (buffer-string))))
5126       (setq filename (cond (filename (file-name-nondirectory filename))
5127                            (dont-decode "*raw data*")
5128                            (t "*decoded*")))
5129       (cond
5130        (dont-decode)
5131        ((not arg)
5132         (unless (setq charset (mail-content-type-get
5133                                (mm-handle-type handle) 'charset))
5134           (unless (setq coding-system (mm-with-unibyte-buffer
5135                                         (insert contents)
5136                                         (mm-find-buffer-file-coding-system)))
5137             (setq charset gnus-newsgroup-charset))))
5138        ((numberp arg)
5139         (setq charset (or (cdr (assq arg
5140                                      gnus-summary-show-article-charset-alist))
5141                           (mm-read-coding-system "Charset: ")))))
5142       (switch-to-buffer (generate-new-buffer filename))
5143       (if (or coding-system
5144               (and charset
5145                    (setq coding-system (mm-charset-to-coding-system charset))
5146                    (not (eq coding-system 'ascii))))
5147           (progn
5148             (mm-enable-multibyte)
5149             (insert (mm-decode-coding-string contents coding-system))
5150             (setq buffer-file-coding-system
5151                   (if (boundp 'last-coding-system-used)
5152                       (symbol-value 'last-coding-system-used)
5153                     coding-system)))
5154         (mm-disable-multibyte)
5155         (insert contents)
5156         (setq buffer-file-coding-system mm-binary-coding-system))
5157       ;; We do it this way to make `normal-mode' set the appropriate mode.
5158       (unwind-protect
5159           (progn
5160             (setq buffer-file-name (expand-file-name filename))
5161             (normal-mode))
5162         (setq buffer-file-name nil))
5163       (goto-char (point-min)))))
5164
5165 (defun gnus-mime-print-part (&optional handle filename)
5166   "Print the MIME part under point."
5167   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5168   (gnus-article-check-buffer)
5169   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5170          (contents (and handle (mm-get-part handle)))
5171          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5172          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5173     (when contents
5174         (if printer
5175             (unwind-protect
5176                 (progn
5177                   (mm-save-part-to-file handle file)
5178                   (call-process shell-file-name nil
5179                                 (generate-new-buffer " *mm*")
5180                                 nil
5181                                 shell-command-switch
5182                                 (mm-mailcap-command
5183                                  printer file (mm-handle-type handle))))
5184               (delete-file file))
5185           (with-temp-buffer
5186             (insert contents)
5187             (gnus-print-buffer))
5188           (ps-despool filename)))))
5189
5190 (defun gnus-mime-inline-part (&optional handle arg)
5191   "Insert the MIME part under point into the current buffer.
5192 Compressed files like .gz and .bz2 are decompressed."
5193   (interactive (list nil current-prefix-arg))
5194   (gnus-article-check-buffer)
5195   (unless handle
5196     (setq handle (get-text-property (point) 'gnus-data)))
5197   (when handle
5198     (let ((b (point))
5199           (inhibit-read-only t)
5200           contents charset coding-system)
5201       (if (and (not arg) (mm-handle-undisplayer handle))
5202           (mm-remove-part handle)
5203         (mm-with-unibyte-buffer
5204           (mm-insert-part handle)
5205           (setq contents
5206                 (or (mm-decompress-buffer
5207                      (or (mail-content-type-get (mm-handle-type handle)
5208                                                 'name)
5209                          (mail-content-type-get (mm-handle-disposition handle)
5210                                                 'filename))
5211                      nil t)
5212                     (buffer-string))))
5213         (cond
5214          ((not arg)
5215           (unless (setq charset (mail-content-type-get
5216                                  (mm-handle-type handle) 'charset))
5217             (unless (setq coding-system
5218                           (mm-with-unibyte-buffer
5219                             (insert contents)
5220                             (mm-find-buffer-file-coding-system)))
5221               (setq charset gnus-newsgroup-charset))))
5222          ((numberp arg)
5223           (if (mm-handle-undisplayer handle)
5224               (mm-remove-part handle))
5225           (setq charset
5226                 (or (cdr (assq arg
5227                                gnus-summary-show-article-charset-alist))
5228                     (mm-read-coding-system "Charset: "))))
5229          (t
5230           (if (mm-handle-undisplayer handle)
5231               (mm-remove-part handle))))
5232         (forward-line 2)
5233         (mm-insert-inline
5234          handle
5235          (if (or coding-system
5236                  (and charset
5237                       (setq coding-system
5238                             (mm-charset-to-coding-system charset))
5239                       (not (eq coding-system 'ascii))))
5240              (mm-decode-coding-string contents coding-system)
5241            (mm-string-to-multibyte contents)))
5242         (goto-char b)))))
5243
5244 (defun gnus-mime-set-charset-parameters (handle charset)
5245   "Set CHARSET to parameters in HANDLE.
5246 CHARSET may either be a string or a symbol."
5247   (unless (stringp charset)
5248     (setq charset (symbol-name charset)))
5249   (if (stringp (car handle))
5250       (dolist (h (cdr handle))
5251         (gnus-mime-set-charset-parameters h charset))
5252     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5253                                             "message/external-body")
5254                                      (progn
5255                                        (unless (mm-handle-cache handle)
5256                                          (mm-extern-cache-contents handle))
5257                                        (mm-handle-cache handle))
5258                                    handle)))
5259            (param (assq 'charset (cdr type))))
5260       (if param
5261           (setcdr param charset)
5262         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5263
5264 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5265   "Insert the MIME part under point into the current buffer using the
5266 specified charset."
5267   (interactive (list nil current-prefix-arg))
5268   (gnus-article-check-buffer)
5269   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5270         (fun (get-text-property (point) 'gnus-callback))
5271         (gnus-newsgroup-ignored-charsets 'gnus-all)
5272         charset form preferred parts)
5273     (when handle
5274       (when (prog1
5275                 (and fun
5276                      (setq charset
5277                            (or (cdr (assq
5278                                      arg
5279                                      gnus-summary-show-article-charset-alist))
5280                                (mm-read-coding-system "Charset: "))))
5281               (if (mm-handle-undisplayer handle)
5282                   (mm-remove-part handle)))
5283         (gnus-mime-set-charset-parameters handle charset)
5284         (when (and (consp (setq form (cdr-safe fun)))
5285                    (setq form (ignore-errors
5286                                 (assq 'gnus-mime-display-alternative form)))
5287                    (setq preferred (caddr form))
5288                    (progn
5289                      (when (eq (car preferred) 'quote)
5290                        (setq preferred (cadr preferred)))
5291                      (not (equal preferred
5292                                  (get-text-property (point) 'gnus-data))))
5293                    (setq parts (get-text-property (point) 'gnus-part))
5294                    (setq parts (cdr (assq parts
5295                                           gnus-article-mime-handle-alist)))
5296                    (equal (mm-handle-media-type parts) "multipart/alternative")
5297                    (setq parts (reverse (cdr parts))))
5298           (setcar (cddr form)
5299                   (list 'quote (or (cadr (member preferred parts))
5300                                    (car parts)))))
5301         (funcall fun handle)))))
5302
5303 (defun gnus-mime-view-part-externally (&optional handle)
5304   "View the MIME part under point with an external viewer."
5305   (interactive)
5306   (gnus-article-check-buffer)
5307   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5308          (mm-user-display-methods nil)
5309          (mm-inlined-types nil)
5310          (mail-parse-charset gnus-newsgroup-charset)
5311          (mail-parse-ignored-charsets
5312           (with-current-buffer gnus-summary-buffer
5313             gnus-newsgroup-ignored-charsets))
5314          (type (mm-handle-media-type handle))
5315          (method (mailcap-mime-info type))
5316          (mm-enable-external t))
5317     (if (not (stringp method))
5318         (gnus-mime-view-part-as-type
5319          nil (lambda (type) (stringp (mailcap-mime-info type))))
5320       (when handle
5321         (mm-display-part handle nil t)))))
5322
5323 (defun gnus-mime-view-part-internally (&optional handle)
5324   "View the MIME part under point with an internal viewer.
5325 If no internal viewer is available, use an external viewer."
5326   (interactive)
5327   (gnus-article-check-buffer)
5328   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5329          (mm-inlined-types '(".*"))
5330          (mm-inline-large-images t)
5331          (mail-parse-charset gnus-newsgroup-charset)
5332          (mail-parse-ignored-charsets
5333           (with-current-buffer gnus-summary-buffer
5334             gnus-newsgroup-ignored-charsets))
5335          (inhibit-read-only t))
5336     (if (not (mm-inlinable-p handle))
5337         (gnus-mime-view-part-as-type
5338          nil (lambda (type) (mm-inlinable-p handle type)))
5339       (when handle
5340         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5341
5342 (defun gnus-mime-action-on-part (&optional action)
5343   "Do something with the MIME attachment at \(point\)."
5344   (interactive
5345    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5346   (gnus-article-check-buffer)
5347   (let ((action-pair (assoc action gnus-mime-action-alist)))
5348     (if action-pair
5349         (funcall (cdr action-pair)))))
5350
5351 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5352   "Call FUNCTION on MIME part N.
5353 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5354 If INTERACTIVE, call FUNCTION interactivly."
5355   (let (window frame)
5356     ;; Check whether the article is displayed.
5357     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5358                  (setq window (get-buffer-window gnus-article-buffer t))
5359                  (frame-visible-p (setq frame (window-frame window))))
5360       (error "No article is displayed"))
5361     (with-current-buffer gnus-article-buffer
5362       ;; Check whether the article displays the right contents.
5363       (unless (with-current-buffer gnus-summary-buffer
5364                 (eq gnus-current-article (gnus-summary-article-number)))
5365         (error "You should select the right article first"))
5366       (if n
5367           (setq n (prefix-numeric-value n))
5368         (let ((pt (point)))
5369           (setq n (or (get-text-property pt 'gnus-part)
5370                       (and (not (bobp))
5371                            (get-text-property (1- pt) 'gnus-part))
5372                       (get-text-property (prog2
5373                                              (forward-line 1)
5374                                              (point)
5375                                            (goto-char pt))
5376                                          'gnus-part)
5377                       (get-text-property
5378                        (or (and (setq pt (previous-single-property-change
5379                                           pt 'gnus-part))
5380                                 (1- pt))
5381                            (next-single-property-change (point) 'gnus-part)
5382                            (point))
5383                        'gnus-part)
5384                       1))))
5385       ;; Check whether the specified part exists.
5386       (when (> n (length gnus-article-mime-handle-alist))
5387         (error "No such part")))
5388     (unless
5389         (progn
5390           ;; To select the window is needed so that the cursor
5391           ;; might be visible on the MIME button.
5392           (select-window (prog1
5393                              window
5394                            (setq window (selected-window))
5395                            ;; Article may be displayed in the other frame.
5396                            (gnus-select-frame-set-input-focus
5397                             (prog1
5398                                 frame
5399                               (setq frame (selected-frame))))))
5400           (when (gnus-article-goto-part n)
5401             ;; We point the cursor and the arrow at the MIME button
5402             ;; when the `function' prompt the user for something.
5403             (unless (and (pos-visible-in-window-p)
5404                          (> (count-lines (point) (window-end))
5405                             (/ (1- (window-height)) 3)))
5406               (recenter (/ (1- (window-height)) 3)))
5407             (let ((cursor-in-non-selected-windows t)
5408                   (overlay-arrow-string "=>")
5409                   (overlay-arrow-position (point-marker)))
5410               (unwind-protect
5411                   (cond
5412                    ((and no-handle interactive)
5413                     (call-interactively function))
5414                    (no-handle
5415                     (funcall function))
5416                    (interactive
5417                     (call-interactively
5418                      function (get-text-property (point) 'gnus-data)))
5419                    (t
5420                     (funcall function
5421                              (get-text-property (point) 'gnus-data))))
5422                 (set-marker overlay-arrow-position nil)
5423                 (unless gnus-auto-select-part
5424                   (gnus-select-frame-set-input-focus frame)
5425                   (select-window window))))
5426             t))
5427       (if gnus-inhibit-mime-unbuttonizing
5428           ;; This is the default though the program shouldn't reach here.
5429           (error "No such part")
5430         ;; The part which doesn't have the MIME button is selected.
5431         ;; So, we display all the buttons and redo it.
5432         (let ((gnus-inhibit-mime-unbuttonizing t))
5433           (gnus-summary-show-article)
5434           (gnus-article-part-wrapper n function no-handle))))))
5435
5436 (defun gnus-article-pipe-part (n)
5437   "Pipe MIME part N, which is the numerical prefix."
5438   (interactive "P")
5439   (gnus-article-part-wrapper n 'mm-pipe-part))
5440
5441 (defun gnus-article-save-part (n)
5442   "Save MIME part N, which is the numerical prefix."
5443   (interactive "P")
5444   (gnus-article-part-wrapper n 'mm-save-part))
5445
5446 (defun gnus-article-interactively-view-part (n)
5447   "View MIME part N interactively, which is the numerical prefix."
5448   (interactive "P")
5449   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5450
5451 (defun gnus-article-copy-part (n)
5452   "Copy MIME part N, which is the numerical prefix."
5453   (interactive "P")
5454   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5455
5456 (defun gnus-article-view-part-as-charset (n)
5457   "View MIME part N using a specified charset.
5458 N is the numerical prefix."
5459   (interactive "P")
5460   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5461
5462 (defun gnus-article-view-part-externally (n)
5463   "View MIME part N externally, which is the numerical prefix."
5464   (interactive "P")
5465   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5466
5467 (defun gnus-article-inline-part (n)
5468   "Inline MIME part N, which is the numerical prefix."
5469   (interactive "P")
5470   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5471
5472 (defun gnus-article-save-part-and-strip (n)
5473   "Save MIME part N and replace it with an external body.
5474 N is the numerical prefix."
5475   (interactive "P")
5476   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5477
5478 (defun gnus-article-replace-part (n)
5479   "Replace MIME part N with an external body.
5480 N is the numerical prefix."
5481   (interactive "P")
5482   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5483
5484 (defun gnus-article-delete-part (n)
5485   "Delete MIME part N and add some information about the removed part.
5486 N is the numerical prefix."
5487   (interactive "P")
5488   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5489
5490 (defun gnus-article-view-part-as-type (n)
5491   "Choose a MIME media type, and view part N as such.
5492 N is the numerical prefix."
5493   (interactive "P")
5494   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5495
5496 (defun gnus-article-mime-match-handle-first (condition)
5497   (if condition
5498       (let (n)
5499         (dolist (ihandle gnus-article-mime-handle-alist)
5500           (if (and (cond
5501                     ((functionp condition)
5502                      (funcall condition (cdr ihandle)))
5503                     ((eq condition 'undisplayed)
5504                      (not (or (mm-handle-undisplayer (cdr ihandle))
5505                               (equal (mm-handle-media-type (cdr ihandle))
5506                                      "multipart/alternative"))))
5507                     ((eq condition 'undisplayed-alternative)
5508                      (not (mm-handle-undisplayer (cdr ihandle))))
5509                     (t t))
5510                    (gnus-article-goto-part (car ihandle))
5511                    (or (not n) (< (car ihandle) n)))
5512               (setq n (car ihandle))))
5513         (or n 1))
5514     1))
5515
5516 (defun gnus-article-view-part (&optional n)
5517   "View MIME part N, which is the numerical prefix.
5518 If the part is already shown, hide the part.  If N is nil, view
5519 all parts."
5520   (interactive "P")
5521   (with-current-buffer gnus-article-buffer
5522     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5523                              gnus-article-mime-match-handle-function)))
5524     (when (> n (length gnus-article-mime-handle-alist))
5525       (error "No such part"))
5526     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5527       (when (gnus-article-goto-part n)
5528         (if (equal (car handle) "multipart/alternative")
5529             (gnus-article-press-button)
5530           (when (eq (gnus-mm-display-part handle) 'internal)
5531             (gnus-set-window-start)))))))
5532
5533 (defsubst gnus-article-mime-total-parts ()
5534   (if (bufferp (car gnus-article-mime-handles))
5535       1 ;; single part
5536     (1- (length gnus-article-mime-handles))))
5537
5538 (defun gnus-mm-display-part (handle)
5539   "Display HANDLE and fix MIME button."
5540   (let ((id (get-text-property (point) 'gnus-part))
5541         (point (point))
5542         (inhibit-read-only t))
5543     (forward-line 1)
5544     (prog1
5545         (let ((window (selected-window))
5546               (mail-parse-charset gnus-newsgroup-charset)
5547               (mail-parse-ignored-charsets
5548                (if (gnus-buffer-live-p gnus-summary-buffer)
5549                    (with-current-buffer gnus-summary-buffer
5550                      gnus-newsgroup-ignored-charsets)
5551                  nil)))
5552           (save-excursion
5553             (unwind-protect
5554                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5555                       (beg (point)))
5556                   (when win
5557                     (select-window win))
5558                   (goto-char point)
5559                   (forward-line)
5560                   (if (mm-handle-displayed-p handle)
5561                       ;; This will remove the part.
5562                       (mm-display-part handle)
5563                     (save-restriction
5564                       (narrow-to-region (point)
5565                                         (if (eobp) (point) (1+ (point))))
5566                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5567                       ;; We narrow to the part itself and
5568                       ;; then call the treatment functions.
5569                       (goto-char (point-min))
5570                       (forward-line 1)
5571                       (narrow-to-region (point) (point-max))
5572                       (gnus-treat-article
5573                        nil id
5574                        (gnus-article-mime-total-parts)
5575                        (mm-handle-media-type handle)))))
5576               (if (window-live-p window)
5577                   (select-window window)))))
5578       (goto-char point)
5579       (gnus-delete-line)
5580       (gnus-insert-mime-button
5581        handle id (list (mm-handle-displayed-p handle)))
5582       (goto-char point))))
5583
5584 (defun gnus-article-goto-part (n)
5585   "Go to MIME part N."
5586   (when gnus-break-pages
5587     (widen))
5588   (prog1
5589       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5590             part handle end next handles)
5591         (when start
5592           (goto-char start)
5593           (if (setq handle (get-text-property start 'gnus-data))
5594               start
5595             ;; Go to the displayed subpart, assuming this is
5596             ;; multipart/alternative.
5597             (setq part start
5598                   end (point-at-eol))
5599             (while (and (not handle)
5600                         part
5601                         (< part end)
5602                         (setq next (text-property-not-all part end
5603                                                           'gnus-data nil)))
5604               (setq part next
5605                     handle (get-text-property part 'gnus-data))
5606               (push (cons handle part) handles)
5607               (unless (mm-handle-displayed-p handle)
5608                 (setq handle nil
5609                       part (text-property-any part end 'gnus-data nil))))
5610             (unless handle
5611               ;; No subpart is displayed, so we find preferred one.
5612               (setq part
5613                     (cdr (assq (mm-preferred-alternative
5614                                 (nreverse (mapcar 'car handles)))
5615                                handles))))
5616             (if part
5617                 (goto-char (1+ part))
5618               start))))
5619     (when gnus-break-pages
5620       (gnus-narrow-to-page))))
5621
5622 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5623   (let ((gnus-tmp-name
5624          (or (mail-content-type-get (mm-handle-type handle) 'name)
5625              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5626              (mail-content-type-get (mm-handle-type handle) 'url)
5627              ""))
5628         (gnus-tmp-type (mm-handle-media-type handle))
5629         (gnus-tmp-description (or (mm-handle-description handle) ""))
5630         (gnus-tmp-dots
5631          (if (if displayed (car displayed)
5632                (mm-handle-displayed-p handle))
5633              "" "..."))
5634         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5635                            (buffer-size)))
5636         gnus-tmp-type-long b e)
5637     (when (string-match ".*/" gnus-tmp-name)
5638       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5639     (setq gnus-tmp-type-long (concat gnus-tmp-type
5640                                      (and (not (equal gnus-tmp-name ""))
5641                                           (concat "; " gnus-tmp-name))))
5642     (unless (equal gnus-tmp-description "")
5643       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5644     (unless (bolp)
5645       (insert "\n"))
5646     (setq b (point))
5647     (gnus-eval-format
5648      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5649      `(keymap ,gnus-mime-button-map
5650               gnus-callback gnus-mm-display-part
5651               gnus-part ,gnus-tmp-id
5652               article-type annotation
5653               gnus-data ,handle))
5654     (setq e (if (bolp)
5655                 ;; Exclude a newline.
5656                 (1- (point))
5657               (point)))
5658     (when gnus-article-button-face
5659       (gnus-overlay-put (gnus-make-overlay b e nil t)
5660                         'face gnus-article-button-face))
5661     (widget-convert-button
5662      'link b e
5663      :mime-handle handle
5664      :action 'gnus-widget-press-button
5665      :button-keymap gnus-mime-button-map
5666      :help-echo
5667      (lambda (widget)
5668        ;; Needed to properly clear the message due to a bug in
5669        ;; wid-edit (XEmacs only).
5670        (if (boundp 'help-echo-owns-message)
5671            (setq help-echo-owns-message t))
5672        (format
5673         "%S: %s the MIME part; %S: more options"
5674         (aref gnus-mouse-2 0)
5675         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5676             "hide" "show")
5677         (aref gnus-down-mouse-3 0))))))
5678
5679 (defun gnus-widget-press-button (elems el)
5680   (goto-char (widget-get elems :from))
5681   (gnus-article-press-button))
5682
5683 (defvar gnus-displaying-mime nil)
5684
5685 (defun gnus-display-mime (&optional ihandles)
5686   "Display the MIME parts."
5687   (save-excursion
5688     (save-selected-window
5689       (let ((window (get-buffer-window gnus-article-buffer))
5690             (point (point)))
5691         (when window
5692           (select-window window)
5693           ;; We have to do this since selecting the window
5694           ;; may change the point.  So we set the window point.
5695           (set-window-point window point)))
5696       (let ((handles ihandles)
5697             (inhibit-read-only t)
5698             handle)
5699         (cond (handles)
5700               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5701                (when gnus-article-emulate-mime
5702                  (mm-uu-dissect-text-parts handles)))
5703               (gnus-article-emulate-mime
5704                (setq handles (mm-uu-dissect))))
5705         (when (and (not ihandles)
5706                    (not gnus-displaying-mime))
5707           ;; Top-level call; we clean up.
5708           (when gnus-article-mime-handles
5709             (mm-destroy-parts gnus-article-mime-handles)
5710             (setq gnus-article-mime-handle-alist nil));; A trick.
5711           (setq gnus-article-mime-handles handles)
5712           ;; We allow users to glean info from the handles.
5713           (when gnus-article-mime-part-function
5714             (gnus-mime-part-function handles)))
5715         (if (and handles
5716                  (or (not (stringp (car handles)))
5717                      (cdr handles)))
5718             (progn
5719               (when (and (not ihandles)
5720                          (not gnus-displaying-mime))
5721                 ;; Clean up for mime parts.
5722                 (article-goto-body)
5723                 (delete-region (point) (point-max)))
5724               (let ((gnus-displaying-mime t))
5725                 (gnus-mime-display-part handles)))
5726           (save-restriction
5727             (article-goto-body)
5728             (narrow-to-region (point) (point-max))
5729             (gnus-treat-article nil 1 1 "text/plain")
5730             (widen)))
5731         (unless ihandles
5732           ;; Highlight the headers.
5733           (save-excursion
5734             (save-restriction
5735               (article-goto-body)
5736               (narrow-to-region (point-min) (point))
5737               (gnus-article-save-original-date
5738                (gnus-treat-article 'head)))))))
5739     ;; Cope with broken MIME messages.
5740     (goto-char (point-max))
5741     (unless (bolp)
5742       (insert "\n"))))
5743
5744 (defcustom gnus-mime-display-multipart-as-mixed nil
5745   "Display \"multipart\" parts as  \"multipart/mixed\".
5746
5747 If t, it overrides nil values of
5748 `gnus-mime-display-multipart-alternative-as-mixed' and
5749 `gnus-mime-display-multipart-related-as-mixed'."
5750   :group 'gnus-article-mime
5751   :type 'boolean)
5752
5753 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5754   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5755   :version "22.1"
5756   :group 'gnus-article-mime
5757   :type 'boolean)
5758
5759 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5760   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5761
5762 If displaying \"text/html\" is discouraged \(see
5763 `mm-discouraged-alternatives'\) images or other material inside a
5764 \"multipart/related\" part might be overlooked when this variable is nil."
5765   :version "22.1"
5766   :group 'gnus-article-mime
5767   :type 'boolean)
5768
5769 (defun gnus-mime-display-part (handle)
5770   (cond
5771    ;; Maybe a broken MIME message.
5772    ((null handle))
5773    ;; Single part.
5774    ((not (stringp (car handle)))
5775     (gnus-mime-display-single handle))
5776    ;; User-defined multipart
5777    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5778     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5779              handle))
5780    ;; multipart/alternative
5781    ((and (equal (car handle) "multipart/alternative")
5782          (not (or gnus-mime-display-multipart-as-mixed
5783                   gnus-mime-display-multipart-alternative-as-mixed)))
5784     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5785       (push (cons id handle) gnus-article-mime-handle-alist)
5786       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5787    ;; multipart/related
5788    ((and (equal (car handle) "multipart/related")
5789          (not (or gnus-mime-display-multipart-as-mixed
5790                   gnus-mime-display-multipart-related-as-mixed)))
5791     ;;;!!!We should find the start part, but we just default
5792     ;;;!!!to the first part.
5793     ;;(gnus-mime-display-part (cadr handle))
5794     ;;;!!! Most multipart/related is an HTML message plus images.
5795     ;;;!!! Unfortunately we are unable to let W3 display those
5796     ;;;!!! included images, so we just display it as a mixed multipart.
5797     ;;(gnus-mime-display-mixed (cdr handle))
5798     ;;;!!! No, w3 can display everything just fine.
5799     (gnus-mime-display-part (cadr handle)))
5800    ((equal (car handle) "multipart/signed")
5801     (gnus-add-wash-type 'signed)
5802     (gnus-mime-display-security handle))
5803    ((equal (car handle) "multipart/encrypted")
5804     (gnus-add-wash-type 'encrypted)
5805     (gnus-mime-display-security handle))
5806    ;; Other multiparts are handled like multipart/mixed.
5807    (t
5808     (gnus-mime-display-mixed (cdr handle)))))
5809
5810 (defun gnus-mime-part-function (handles)
5811   (if (stringp (car handles))
5812       (mapcar 'gnus-mime-part-function (cdr handles))
5813     (funcall gnus-article-mime-part-function handles)))
5814
5815 (defun gnus-mime-display-mixed (handles)
5816   (mapcar 'gnus-mime-display-part handles))
5817
5818 (defun gnus-mime-display-single (handle)
5819   (let ((type (mm-handle-media-type handle))
5820         (ignored gnus-ignored-mime-types)
5821         (not-attachment t)
5822         (move nil)
5823         display text)
5824     (catch 'ignored
5825       (progn
5826         (while ignored
5827           (when (string-match (pop ignored) type)
5828             (throw 'ignored nil)))
5829         (if (and (setq not-attachment
5830                        (and (not (mm-inline-override-p handle))
5831                             (or (not (mm-handle-disposition handle))
5832                                 (equal (car (mm-handle-disposition handle))
5833                                        "inline")
5834                                 (mm-attachment-override-p handle))))
5835                  (mm-automatic-display-p handle)
5836                  (or (and
5837                       (mm-inlinable-p handle)
5838                       (mm-inlined-p handle))
5839                      (mm-automatic-external-display-p type)))
5840             (setq display t)
5841           (when (equal (mm-handle-media-supertype handle) "text")
5842             (setq text t)))
5843         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5844               beg)
5845           (push (cons id handle) gnus-article-mime-handle-alist)
5846           (when (and display
5847                      (equal (mm-handle-media-supertype handle) "message"))
5848             (insert-char
5849              ?\n
5850              (cond ((not (bolp)) 2)
5851                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5852                    (t 1))))
5853           (when (or (not display)
5854                     (not (gnus-unbuttonized-mime-type-p type))
5855                     (eq id gnus-mime-buttonized-part-id))
5856             (gnus-insert-mime-button
5857              handle id (list (or display (and not-attachment text))))
5858             (gnus-article-insert-newline)
5859             ;; Remember modify the number of forward lines.
5860             (setq move t))
5861           (setq beg (point))
5862           (cond
5863            (display
5864             (when move
5865               (forward-line -1)
5866               (setq beg (point)))
5867             (let ((mail-parse-charset gnus-newsgroup-charset)
5868                   (mail-parse-ignored-charsets
5869                    (save-excursion (condition-case ()
5870                                        (set-buffer gnus-summary-buffer)
5871                                      (error))
5872                                    gnus-newsgroup-ignored-charsets)))
5873               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5874             (goto-char (point-max)))
5875            ((and text not-attachment)
5876             (when move
5877               (forward-line -1)
5878               (setq beg (point)))
5879             (gnus-article-insert-newline)
5880             (mm-insert-inline
5881              handle
5882              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5883                                                        'charset)
5884                                 (and (equal type "text/calendar") 'utf-8))))
5885                (cond ((not charset)
5886                       (mm-string-as-multibyte (mm-get-part handle)))
5887                      ((eq charset 'gnus-decoded)
5888                       (with-current-buffer (mm-handle-buffer handle)
5889                         (buffer-string)))
5890                      (t
5891                       (mm-decode-string (mm-get-part handle) charset)))))
5892             (goto-char (point-max))))
5893           ;; Do highlighting.
5894           (save-excursion
5895             (save-restriction
5896               (narrow-to-region beg (point))
5897               (if (eq handle gnus-article-mime-handles)
5898                   ;; The format=flowed case.
5899                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5900                 ;; Don't count signature parts that are never displayed.
5901                 ;; The part number should be re-calculated supposing this
5902                 ;; might be a message/rfc822 part.
5903                 (let (handles)
5904                   (dolist (part gnus-article-mime-handles)
5905                     (unless (or (stringp part)
5906                                 (equal (car (mm-handle-type part))
5907                                        "application/pgp-signature"))
5908                       (push part handles)))
5909                   (gnus-treat-article
5910                    nil (length (memq handle handles)) (length handles)
5911                    (mm-handle-media-type handle)))))))))))
5912
5913 (defun gnus-unbuttonized-mime-type-p (type)
5914   "Say whether TYPE is to be unbuttonized."
5915   (unless gnus-inhibit-mime-unbuttonizing
5916     (when (catch 'found
5917             (let ((types gnus-unbuttonized-mime-types))
5918               (while types
5919                 (when (string-match (pop types) type)
5920                   (throw 'found t)))))
5921       (not (catch 'found
5922              (let ((types gnus-buttonized-mime-types))
5923                (while types
5924                  (when (string-match (pop types) type)
5925                    (throw 'found t)))))))))
5926
5927 (defun gnus-article-insert-newline ()
5928   "Insert a newline, but mark it as undeletable."
5929   (gnus-put-text-property
5930    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5931
5932 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5933   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5934          (ihandles handles)
5935          (point (point))
5936          handle (inhibit-read-only t) from props begend not-pref)
5937     (save-window-excursion
5938       (save-restriction
5939         (when ibegend
5940           (narrow-to-region (car ibegend)
5941                             (or (cdr ibegend)
5942                                 (progn
5943                                   (goto-char (car ibegend))
5944                                   (forward-line 2)
5945                                   (point))))
5946           (delete-region (point-min) (point-max))
5947           (mm-remove-parts handles))
5948         (setq begend (list (point-marker)))
5949         ;; Do the toggle.
5950         (unless (setq not-pref (cadr (member preferred ihandles)))
5951           (setq not-pref (car ihandles)))
5952         (when (or ibegend
5953                   (not preferred)
5954                   (not (gnus-unbuttonized-mime-type-p
5955                         "multipart/alternative")))
5956           (gnus-add-text-properties
5957            (setq from (point))
5958            (progn
5959              (insert (format "%d.  " id))
5960              (point))
5961            `(gnus-callback
5962              (lambda (handles)
5963                (unless ,(not ibegend)
5964                  (setq gnus-article-mime-handle-alist
5965                        ',gnus-article-mime-handle-alist))
5966                (gnus-mime-display-alternative
5967                 ',ihandles ',not-pref ',begend ,id))
5968              keymap ,gnus-mime-button-map
5969              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5970              face ,gnus-article-button-face
5971              gnus-part ,id
5972              article-type multipart))
5973           (widget-convert-button 'link from (point)
5974                                  :action 'gnus-widget-press-button
5975                                  :button-keymap gnus-widget-button-keymap)
5976           ;; Do the handles
5977           (while (setq handle (pop handles))
5978             (gnus-add-text-properties
5979              (setq from (point))
5980              (progn
5981                (insert (format "(%c) %-18s"
5982                                (if (equal handle preferred) ?* ? )
5983                                (mm-handle-media-type handle)))
5984                (point))
5985              `(gnus-callback
5986                (lambda (handles)
5987                  (unless ,(not ibegend)
5988                    (setq gnus-article-mime-handle-alist
5989                          ',gnus-article-mime-handle-alist))
5990                  (gnus-mime-display-alternative
5991                   ',ihandles ',handle ',begend ,id))
5992                keymap ,gnus-mime-button-map
5993                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5994                face ,gnus-article-button-face
5995                gnus-part ,id
5996                gnus-data ,handle))
5997             (widget-convert-button 'link from (point)
5998                                    :action 'gnus-widget-press-button
5999                                    :button-keymap gnus-widget-button-keymap)
6000             (insert "  "))
6001           (insert "\n\n"))
6002         (when preferred
6003           (if (stringp (car preferred))
6004               (gnus-display-mime preferred)
6005             (let ((mail-parse-charset gnus-newsgroup-charset)
6006                   (mail-parse-ignored-charsets
6007                    (with-current-buffer gnus-summary-buffer
6008                      gnus-newsgroup-ignored-charsets)))
6009               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6010               ;; Do highlighting.
6011               (save-excursion
6012                 (save-restriction
6013                   (narrow-to-region (car begend) (point-max))
6014                   (gnus-treat-article
6015                    nil (length gnus-article-mime-handle-alist)
6016                    (gnus-article-mime-total-parts)
6017                    (mm-handle-media-type preferred))))))
6018           (goto-char (point-max))
6019           (setcdr begend (point-marker)))))
6020     (when ibegend
6021       (goto-char point))))
6022
6023 (defconst gnus-article-wash-status-strings
6024   (let ((alist '((cite "c" "Possible hidden citation text"
6025                        " " "All citation text visible")
6026                  (headers "h" "Hidden headers"
6027                           " " "All headers visible.")
6028                  (pgp "p" "Encrypted or signed message status hidden"
6029                       " " "No hidden encryption nor digital signature status")
6030                  (signature "s" "Signature has been hidden"
6031                             " " "Signature is visible")
6032                  (overstrike "o" "Overstrike (^H) characters applied"
6033                              " " "No overstrike characters applied")
6034                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6035                            " " "No /*_emphasis_*/ characters applied")))
6036         result)
6037     (dolist (entry alist result)
6038       (let ((key (nth 0 entry))
6039             (on (copy-sequence (nth 1 entry)))
6040             (on-help (nth 2 entry))
6041             (off (copy-sequence (nth 3 entry)))
6042             (off-help (nth 4 entry)))
6043         (put-text-property 0 1 'help-echo on-help on)
6044         (put-text-property 0 1 'help-echo off-help off)
6045         (push (list key on off) result))))
6046   "Alist of strings describing wash status in the mode line.
6047 Each entry has the form (KEY ON OF), where the KEY is a symbol
6048 representing the particular washing function, ON is the string to use
6049 in the article mode line when the washing function is active, and OFF
6050 is the string to use when it is inactive.")
6051
6052 (defun gnus-article-wash-status-entry (key value)
6053   (let ((entry (assoc key gnus-article-wash-status-strings)))
6054     (if value (nth 1 entry) (nth 2 entry))))
6055
6056 (defun gnus-article-wash-status ()
6057   "Return a string which display status of article washing."
6058   (with-current-buffer gnus-article-buffer
6059     (let ((cite (memq 'cite gnus-article-wash-types))
6060           (headers (memq 'headers gnus-article-wash-types))
6061           (boring (memq 'boring-headers gnus-article-wash-types))
6062           (pgp (memq 'pgp gnus-article-wash-types))
6063           (pem (memq 'pem gnus-article-wash-types))
6064           (signed (memq 'signed gnus-article-wash-types))
6065           (encrypted (memq 'encrypted gnus-article-wash-types))
6066           (signature (memq 'signature gnus-article-wash-types))
6067           (overstrike (memq 'overstrike gnus-article-wash-types))
6068           (emphasis (memq 'emphasis gnus-article-wash-types)))
6069       (concat
6070        (gnus-article-wash-status-entry 'cite cite)
6071        (gnus-article-wash-status-entry 'headers (or headers boring))
6072        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6073        (gnus-article-wash-status-entry 'signature signature)
6074        (gnus-article-wash-status-entry 'overstrike overstrike)
6075        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6076
6077 (defun gnus-add-wash-type (type)
6078   "Add a washing of TYPE to the current status."
6079   (add-to-list 'gnus-article-wash-types type))
6080
6081 (defun gnus-delete-wash-type (type)
6082   "Add a washing of TYPE to the current status."
6083   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6084
6085 (defun gnus-add-image (category image)
6086   "Add IMAGE of CATEGORY to the list of displayed images."
6087   (let ((entry (assq category gnus-article-image-alist)))
6088     (unless entry
6089       (setq entry (list category))
6090       (push entry gnus-article-image-alist))
6091     (nconc entry (list image))))
6092
6093 (defun gnus-delete-images (category)
6094   "Delete all images in CATEGORY."
6095   (let ((entry (assq category gnus-article-image-alist)))
6096     (dolist (image (cdr entry))
6097       (gnus-remove-image image category))
6098     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6099     (gnus-delete-wash-type category)))
6100
6101 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6102
6103 (defun gnus-article-maybe-hide-headers ()
6104   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6105 Provided for backwards compatibility."
6106   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6107                  (not (with-current-buffer gnus-summary-buffer
6108                         gnus-have-all-headers)))
6109              (not gnus-inhibit-hiding))
6110     (gnus-article-hide-headers)))
6111
6112 ;;; Article savers.
6113
6114 (defun gnus-output-to-file (file-name)
6115   "Append the current article to a file named FILE-NAME.
6116 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6117 text and used as the value of the coding cookie which is added to the
6118 top of a file.  Otherwise, this function saves a raw article without
6119 the coding cookie."
6120   (let* ((artbuf (current-buffer))
6121          (file-name-coding-system nnmail-pathname-coding-system)
6122          (coding gnus-article-save-coding-system)
6123          (coding-system-for-read (if coding
6124                                      nil ;; Rely on the coding cookie.
6125                                    mm-text-coding-system))
6126          (coding-system-for-write (or coding
6127                                       mm-text-coding-system-for-write
6128                                       mm-text-coding-system))
6129          (exists (file-exists-p file-name)))
6130     (with-temp-buffer
6131       (when exists
6132         (insert-file-contents file-name)
6133         (goto-char (point-min))
6134         ;; Remove the existing coding cookie.
6135         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6136           (delete-region (match-beginning 0) (match-end 0))))
6137       (goto-char (point-max))
6138       (insert-buffer-substring artbuf)
6139       ;; Append newline at end of the buffer as separator, and then
6140       ;; save it to file.
6141       (goto-char (point-max))
6142       (insert "\n")
6143       (when coding
6144         ;; If the coding system is not suitable to encode the text,
6145         ;; ask a user for a proper one.
6146         (when (fboundp 'select-safe-coding-system)
6147           (setq coding (coding-system-base
6148                         (save-window-excursion
6149                           (select-safe-coding-system (point-min) (point-max)
6150                                                      coding))))
6151           (setq coding-system-for-write
6152                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6153                     coding)))
6154         (goto-char (point-min))
6155         ;; Add the coding cookie.
6156         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6157                         coding-system-for-write)))
6158       (if exists
6159           (progn
6160             (write-region (point-min) (point-max) file-name nil 'no-message)
6161             (message "Appended to %s" file-name))
6162         (write-region (point-min) (point-max) file-name))))
6163   t)
6164
6165 (defun gnus-narrow-to-page (&optional arg)
6166   "Narrow the article buffer to a page.
6167 If given a numerical ARG, move forward ARG pages."
6168   (interactive "P")
6169   (setq arg (if arg (prefix-numeric-value arg) 0))
6170   (with-current-buffer gnus-article-buffer
6171     (widen)
6172     ;; Remove any old next/prev buttons.
6173     (when (gnus-visual-p 'page-marker)
6174       (let ((inhibit-read-only t))
6175         (gnus-remove-text-with-property 'gnus-prev)
6176         (gnus-remove-text-with-property 'gnus-next)))
6177     (let (st nd pt)
6178       (when (save-excursion
6179               (cond ((< arg 0)
6180                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6181                          (prog1
6182                              (setq nd (match-beginning 0)
6183                                    pt nd)
6184                            (when (re-search-backward page-delimiter nil t)
6185                              (setq st (match-end 0))))
6186                        (when (re-search-forward page-delimiter nil t)
6187                          (setq nd (match-beginning 0)
6188                                pt (point-min)))))
6189                     ((> arg 0)
6190                      (if (re-search-forward page-delimiter nil 'move arg)
6191                          (prog1
6192                              (setq st (match-end 0)
6193                                    pt st)
6194                            (when (re-search-forward page-delimiter nil t)
6195                              (setq nd (match-beginning 0))))
6196                        (when (re-search-backward page-delimiter nil t)
6197                          (setq st (match-end 0)
6198                                pt (point-max)))))
6199                     (t
6200                      (when (re-search-backward page-delimiter nil t)
6201                        (goto-char (setq st (match-end 0))))
6202                      (when (re-search-forward page-delimiter nil t)
6203                        (setq nd (match-beginning 0)))
6204                      (or st nd))))
6205         (setq gnus-page-broken t)
6206         (when pt (goto-char pt))
6207         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6208         (when (gnus-visual-p 'page-marker)
6209           (save-excursion
6210             (when nd
6211               (goto-char nd)
6212               (gnus-insert-next-page-button))
6213             (when st
6214               (goto-char st)
6215               (gnus-insert-prev-page-button))))))))
6216
6217 ;; Article mode commands
6218
6219 (defun gnus-article-goto-next-page ()
6220   "Show the next page of the article."
6221   (interactive)
6222   (when (gnus-article-next-page)
6223     (goto-char (point-min))
6224     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6225
6226
6227 (defun gnus-article-goto-prev-page ()
6228   "Show the previous page of the article."
6229   (interactive)
6230   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6231       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6232     (gnus-article-prev-page nil)))
6233
6234 ;; This is cleaner but currently breaks `gnus-pick-mode':
6235 ;;
6236 ;; (defun gnus-article-goto-next-page ()
6237 ;;   "Show the next page of the article."
6238 ;;   (interactive)
6239 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6240 ;;     (gnus-summary-next-page)))
6241 ;;
6242 ;; (defun gnus-article-goto-prev-page ()
6243 ;;   "Show the next page of the article."
6244 ;;   (interactive)
6245 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6246 ;;     (gnus-summary-prev-page)))
6247
6248 (defun gnus-article-next-page (&optional lines)
6249   "Show the next page of the current article.
6250 If end of article, return non-nil.  Otherwise return nil.
6251 Argument LINES specifies lines to be scrolled up."
6252   (interactive "p")
6253   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6254   (if (and (not (and gnus-article-over-scroll
6255                      (> (count-lines (window-start) (point-max))
6256                         (if (featurep 'xemacs)
6257                             (or lines (1- (window-height)))
6258                           (+ (or lines (1- (window-height))) scroll-margin)))))
6259            (save-excursion
6260              (end-of-line)
6261              (and (pos-visible-in-window-p)     ;Not continuation line.
6262                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6263       ;; Nothing in this page.
6264       (if (or (not gnus-page-broken)
6265               (save-excursion
6266                 (save-restriction
6267                   (widen)
6268                   (forward-line)
6269                   (eobp)))) ;Real end-of-buffer?
6270           (progn
6271             (when gnus-article-over-scroll
6272               (gnus-article-next-page-1 lines))
6273             t)                  ;Nothing more.
6274         (gnus-narrow-to-page 1)         ;Go to next page.
6275         nil)
6276     ;; More in this page.
6277     (gnus-article-next-page-1 lines)
6278     nil))
6279
6280 (defun gnus-article-beginning-of-window ()
6281   "Move point to the beginning of the window.
6282 In Emacs, the point is placed at the line number which `scroll-margin'
6283 specifies."
6284   (if (featurep 'xemacs)
6285       (move-to-window-line 0)
6286     ;; There is an obscure bug in Emacs that makes it impossible to
6287     ;; scroll past big pictures in the article buffer.  Try to fix
6288     ;; this by adding a sanity check by counting the lines visible.
6289     (when (> (count-lines (window-start) (window-end)) 30)
6290       (move-to-window-line
6291        (min (max 0 scroll-margin)
6292             (max 1 (- (window-height)
6293                       (if mode-line-format 1 0)
6294                       (if header-line-format 1 0)
6295                       2)))))))
6296
6297 (defun gnus-article-next-page-1 (lines)
6298   (condition-case ()
6299       (let ((scroll-in-place nil))
6300         (scroll-up lines))
6301     (end-of-buffer
6302      ;; Long lines may cause an end-of-buffer error.
6303      (goto-char (point-max))))
6304   (gnus-article-beginning-of-window))
6305
6306 (defun gnus-article-prev-page (&optional lines)
6307   "Show previous page of current article.
6308 Argument LINES specifies lines to be scrolled down."
6309   (interactive "p")
6310   (move-to-window-line 0)
6311   (if (and gnus-page-broken
6312            (bobp)
6313            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6314       (progn
6315         (gnus-narrow-to-page -1)        ;Go to previous page.
6316         (goto-char (point-max))
6317         (recenter (if gnus-article-over-scroll
6318                       (if lines
6319                           (max (if (featurep 'xemacs)
6320                                    lines
6321                                  (+ lines scroll-margin))
6322                                3)
6323                         (- (window-height) 2))
6324                     -1)))
6325     (prog1
6326         (condition-case ()
6327             (let ((scroll-in-place nil))
6328               (scroll-down lines))
6329           (beginning-of-buffer
6330            (goto-char (point-min))))
6331       (gnus-article-beginning-of-window))))
6332
6333 (defun gnus-article-only-boring-p ()
6334   "Decide whether there is only boring text remaining in the article.
6335 Something \"interesting\" is a word of at least two letters that does
6336 not have a face in `gnus-article-boring-faces'."
6337   (when (and gnus-article-skip-boring
6338              (boundp 'gnus-article-boring-faces)
6339              (symbol-value 'gnus-article-boring-faces))
6340     (save-excursion
6341       (let ((inhibit-point-motion-hooks t))
6342         (catch 'only-boring
6343           (while (re-search-forward "\\b\\w\\w" nil t)
6344             (forward-char -1)
6345             (when (not (gnus-intersection
6346                         (gnus-faces-at (point))
6347                         (symbol-value 'gnus-article-boring-faces)))
6348               (throw 'only-boring nil)))
6349           (throw 'only-boring t))))))
6350
6351 (defun gnus-article-refer-article ()
6352   "Read article specified by message-id around point."
6353   (interactive)
6354   (save-excursion
6355     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6356     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6357     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6358         (let ((msg-id (concat "<" (match-string 0) ">")))
6359           (set-buffer gnus-summary-buffer)
6360           (gnus-summary-refer-article msg-id))
6361       (error "No references around point"))))
6362
6363 (defun gnus-article-show-summary ()
6364   "Reconfigure windows to show summary buffer."
6365   (interactive)
6366   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6367       (error "There is no summary buffer for this article buffer")
6368     (gnus-article-set-globals)
6369     (gnus-configure-windows 'article)
6370     (gnus-summary-goto-subject gnus-current-article)
6371     (gnus-summary-position-point)))
6372
6373 (defun gnus-article-describe-briefly ()
6374   "Describe article mode commands briefly."
6375   (interactive)
6376   (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")))
6377
6378 (defun gnus-article-check-buffer ()
6379   "Beep if not in an article buffer."
6380   (unless (equal major-mode 'gnus-article-mode)
6381     (error "Command invoked outside of a Gnus article buffer")))
6382
6383 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6384   "Read a summary buffer key sequence and execute it from the article buffer."
6385   (interactive "P")
6386   (gnus-article-check-buffer)
6387   (let ((nosaves
6388          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6389            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6390            "=" "^" "\M-^" "|"))
6391         (nosave-but-article
6392          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6393            "An" "Ap" [?A (meta return)] [?A delete]))
6394         (nosave-in-article
6395          '("AS" "\C-d"))
6396         (up-to-top
6397          '("n" "Gn" "p" "Gp"))
6398         keys new-sum-point)
6399     (with-current-buffer gnus-article-current-summary
6400       (let (gnus-pick-mode)
6401         (setq unread-command-events (nconc unread-command-events
6402                                            (list (or key last-command-event)))
6403               keys (if (featurep 'xemacs)
6404                        (events-to-keys (read-key-sequence nil t))
6405                      (read-key-sequence nil t)))))
6406
6407     (message "")
6408
6409     (cond
6410      ((eq (aref keys (1- (length keys))) ?\C-h)
6411       (gnus-article-describe-bindings (substring keys 0 -1)))
6412      ((or (member keys nosaves)
6413           (member keys nosave-but-article)
6414           (member keys nosave-in-article))
6415       (let (func)
6416         (save-window-excursion
6417           (pop-to-buffer gnus-article-current-summary)
6418           ;; We disable the pick minor mode commands.
6419           (let (gnus-pick-mode)
6420             (setq func (key-binding keys t))))
6421         (if (or (not func)
6422                 (numberp func))
6423             (ding)
6424           (unless (member keys nosave-in-article)
6425             (set-buffer gnus-article-current-summary))
6426           (call-interactively func)
6427           (setq new-sum-point (point)))
6428         (when (member keys nosave-but-article)
6429           (pop-to-buffer gnus-article-buffer))))
6430      (t
6431       ;; These commands should restore window configuration.
6432       (let ((obuf (current-buffer))
6433             (owin (current-window-configuration))
6434             win func in-buffer selected new-sum-start new-sum-hscroll err)
6435         (cond (not-restore-window
6436                (pop-to-buffer gnus-article-current-summary)
6437                (setq win (selected-window)))
6438               ((setq win (get-buffer-window gnus-article-current-summary))
6439                (select-window win))
6440               (t
6441                (let ((summary-buffer gnus-article-current-summary))
6442                  (gnus-configure-windows 'article)
6443                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6444                    (let ((gnus-buffer-configuration
6445                           '((article ((vertical 1.0
6446                                                 (summary 0.25 point)
6447                                                 (article 1.0)))))))
6448                      (gnus-configure-windows 'article))
6449                    (setq win (get-buffer-window summary-buffer 'visible)))
6450                  (gnus-select-frame-set-input-focus (window-frame win))
6451                  (select-window win))))
6452         (setq in-buffer (current-buffer))
6453         ;; We disable the pick minor mode commands.
6454         (if (and (setq func (let (gnus-pick-mode)
6455                               (key-binding keys t)))
6456                  (functionp func)
6457                  (condition-case code
6458                      (progn
6459                        (call-interactively func)
6460                        t)
6461                    (error
6462                     (setq err code)
6463                     nil)))
6464             (progn
6465               (when (eq win (selected-window))
6466                 (setq new-sum-point (point)
6467                       new-sum-start (window-start win)
6468                       new-sum-hscroll (window-hscroll win)))
6469               (when (or (eq in-buffer (current-buffer))
6470                         (when (eq obuf (current-buffer))
6471                           (set-buffer in-buffer)
6472                           t))
6473                 (setq selected (gnus-summary-select-article))
6474                 (set-buffer obuf)
6475                 (unless not-restore-window
6476                   (set-window-configuration owin))
6477                 (when (and (eq selected 'old)
6478                            new-sum-point)
6479                   (set-window-start (get-buffer-window (current-buffer))
6480                                     1)
6481                   (set-window-point (get-buffer-window (current-buffer))
6482                                     (if (article-goto-body)
6483                                         (1- (point))
6484                                       (point))))
6485                 (when (and (not not-restore-window)
6486                            new-sum-point
6487                            (window-live-p win)
6488                            (with-current-buffer (window-buffer win)
6489                              (eq major-mode 'gnus-summary-mode)))
6490                   (set-window-point win new-sum-point)
6491                   (set-window-start win new-sum-start)
6492                   (set-window-hscroll win new-sum-hscroll))))
6493           (set-window-configuration owin)
6494           (if err
6495               (signal (car err) (cdr err))
6496             (ding))))))))
6497
6498 (defun gnus-article-read-summary-send-keys ()
6499   (interactive)
6500   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6501     (gnus-article-read-summary-keys)))
6502
6503 (defun gnus-article-describe-key (key)
6504   "Display documentation of the function invoked by KEY.
6505 KEY is a string or a vector."
6506   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6507                        (read-key-sequence "Describe key: "))))
6508   (gnus-article-check-buffer)
6509   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6510                                   gnus-article-read-summary-send-keys))
6511       (with-current-buffer gnus-article-current-summary
6512         (setq unread-command-events
6513               (if (featurep 'xemacs)
6514                   (append key nil)
6515                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6516                                         (list 'meta (- x 128))
6517                                       x))
6518                         key)))
6519         (let ((cursor-in-echo-area t)
6520               gnus-pick-mode)
6521           (describe-key (read-key-sequence nil t))))
6522     (describe-key key)))
6523
6524 (defun gnus-article-describe-key-briefly (key &optional insert)
6525   "Display documentation of the function invoked by KEY.
6526 KEY is a string or a vector."
6527   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6528                        (read-key-sequence "Describe key: "))
6529                      current-prefix-arg))
6530   (gnus-article-check-buffer)
6531   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6532                                   gnus-article-read-summary-send-keys))
6533       (with-current-buffer gnus-article-current-summary
6534         (setq unread-command-events
6535               (if (featurep 'xemacs)
6536                   (append key nil)
6537                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6538                                         (list 'meta (- x 128))
6539                                       x))
6540                         key)))
6541         (let ((cursor-in-echo-area t)
6542               gnus-pick-mode)
6543           (describe-key-briefly (read-key-sequence nil t) insert)))
6544     (describe-key-briefly key insert)))
6545
6546 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6547 (defvar gnus-agent-summary-mode)
6548 (defvar gnus-draft-mode)
6549 ;; Calling help-buffer will autoload help-mode.
6550 (defvar help-xref-stack-item)
6551 ;; Emacs 22 doesn't load it in the batch mode.
6552 (eval-when-compile
6553   (autoload 'help-buffer "help-mode"))
6554
6555 (defun gnus-article-describe-bindings (&optional prefix)
6556   "Show a list of all defined keys, and their definitions.
6557 The optional argument PREFIX, if non-nil, should be a key sequence;
6558 then we display only bindings that start with that prefix."
6559   (interactive)
6560   (gnus-article-check-buffer)
6561   (let ((keymap (copy-keymap gnus-article-mode-map))
6562         (map (copy-keymap gnus-article-send-map))
6563         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6564         parent agent draft)
6565     (define-key keymap "S" map)
6566     (define-key map [t] nil)
6567     (with-current-buffer gnus-article-current-summary
6568       (set-keymap-parent
6569        keymap
6570        (if (setq parent (keymap-parent gnus-article-mode-map))
6571            (prog1
6572                (setq parent (copy-keymap parent))
6573              (set-keymap-parent parent (current-local-map)))
6574          (current-local-map)))
6575       (set-keymap-parent map (key-binding "S"))
6576       (let (key def gnus-pick-mode)
6577         (while sumkeys
6578           (setq key (pop sumkeys))
6579           (cond ((and (vectorp key) (= (length key) 1)
6580                       (consp (setq def (aref key 0)))
6581                       (numberp (car def)) (numberp (cdr def)))
6582                  (when (< (max (car def) (cdr def)) 128)
6583                    (setq sumkeys
6584                          (append (mapcar
6585                                   #'vector
6586                                   (nreverse (gnus-uncompress-range def)))
6587                                  sumkeys))))
6588                 ((setq def (key-binding key))
6589                  (unless (eq def 'undefined)
6590                    (define-key keymap key def))))))
6591       (when (boundp 'gnus-agent-summary-mode)
6592         (setq agent gnus-agent-summary-mode))
6593       (when (boundp 'gnus-draft-mode)
6594         (setq draft gnus-draft-mode)))
6595     (with-temp-buffer
6596       (use-local-map keymap)
6597       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6598       (set (make-local-variable 'gnus-draft-mode) draft)
6599       (describe-bindings prefix))
6600     (let ((item `((lambda (prefix)
6601                     (with-current-buffer ,(current-buffer)
6602                       (gnus-article-describe-bindings prefix)))
6603                   ,prefix)))
6604       (with-current-buffer (let (help-xref-following) (help-buffer))
6605         (setq help-xref-stack-item item)))))
6606
6607 (defun gnus-article-reply-with-original (&optional wide)
6608   "Start composing a reply mail to the current message.
6609 The text in the region will be yanked.  If the region isn't active,
6610 the entire article will be yanked."
6611   (interactive)
6612   (let ((article (cdr gnus-article-current))
6613         contents)
6614     (if (not (gnus-region-active-p))
6615         (with-current-buffer gnus-summary-buffer
6616           (gnus-summary-reply (list (list article)) wide))
6617       (setq contents (buffer-substring (point) (mark t)))
6618       ;; Deactivate active regions.
6619       (when (and (boundp 'transient-mark-mode)
6620                  transient-mark-mode)
6621         (setq mark-active nil))
6622       (with-current-buffer gnus-summary-buffer
6623         (gnus-summary-reply
6624          (list (list article contents)) wide)))))
6625
6626 (defun gnus-article-wide-reply-with-original ()
6627   "Start composing a wide reply mail to the current message.
6628 The text in the region will be yanked.  If the region isn't active,
6629 the entire article will be yanked."
6630   (interactive)
6631   (gnus-article-reply-with-original t))
6632
6633 (defun gnus-article-followup-with-original ()
6634   "Compose a followup to the current article.
6635 The text in the region will be yanked.  If the region isn't active,
6636 the entire article will be yanked."
6637   (interactive)
6638   (let ((article (cdr gnus-article-current))
6639         contents)
6640       (if (not (gnus-region-active-p))
6641           (with-current-buffer gnus-summary-buffer
6642             (gnus-summary-followup (list (list article))))
6643         (setq contents (buffer-substring (point) (mark t)))
6644         ;; Deactivate active regions.
6645         (when (and (boundp 'transient-mark-mode)
6646                    transient-mark-mode)
6647           (setq mark-active nil))
6648         (with-current-buffer gnus-summary-buffer
6649           (gnus-summary-followup
6650            (list (list article contents)))))))
6651
6652 (defun gnus-article-hide (&optional arg force)
6653   "Hide all the gruft in the current article.
6654 This means that signatures, cited text and (some) headers will be
6655 hidden.
6656 If given a prefix, show the hidden text instead."
6657   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6658   (gnus-article-hide-headers arg)
6659   (gnus-article-hide-list-identifiers arg)
6660   (gnus-article-hide-citation-maybe arg force)
6661   (gnus-article-hide-signature arg))
6662
6663 (defun gnus-article-maybe-highlight ()
6664   "Do some article highlighting if article highlighting is requested."
6665   (when (gnus-visual-p 'article-highlight 'highlight)
6666     (gnus-article-highlight-some)))
6667
6668 (defun gnus-check-group-server ()
6669   ;; Make sure the connection to the server is alive.
6670   (unless (gnus-server-opened
6671            (gnus-find-method-for-group gnus-newsgroup-name))
6672     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6673     (gnus-request-group gnus-newsgroup-name t)))
6674
6675 (eval-when-compile
6676   (autoload 'nneething-get-file-name "nneething"))
6677
6678 (defun gnus-request-article-this-buffer (article group)
6679   "Get an article and insert it into this buffer."
6680   (let (do-update-line sparse-header)
6681     (prog1
6682         (save-excursion
6683           (erase-buffer)
6684           (gnus-kill-all-overlays)
6685           (setq group (or group gnus-newsgroup-name))
6686
6687           ;; Using `gnus-request-article' directly will insert the article into
6688           ;; `nntp-server-buffer' - so we'll save some time by not having to
6689           ;; copy it from the server buffer into the article buffer.
6690
6691           ;; We only request an article by message-id when we do not have the
6692           ;; headers for it, so we'll have to get those.
6693           (when (stringp article)
6694             (gnus-read-header article))
6695
6696           ;; If the article number is negative, that means that this article
6697           ;; doesn't belong in this newsgroup (possibly), so we find its
6698           ;; message-id and request it by id instead of number.
6699           (when (and (numberp article)
6700                      gnus-summary-buffer
6701                      (get-buffer gnus-summary-buffer)
6702                      (gnus-buffer-exists-p gnus-summary-buffer))
6703             (with-current-buffer gnus-summary-buffer
6704               (let ((header (gnus-summary-article-header article)))
6705                 (when (< article 0)
6706                   (cond
6707                    ((memq article gnus-newsgroup-sparse)
6708                     ;; This is a sparse gap article.
6709                     (setq do-update-line article)
6710                     (setq article (mail-header-id header))
6711                     (setq sparse-header (gnus-read-header article))
6712                     (setq gnus-newsgroup-sparse
6713                           (delq article gnus-newsgroup-sparse)))
6714                    ((vectorp header)
6715                     ;; It's a real article.
6716                     (setq article (mail-header-id header)))
6717                    (t
6718                     ;; It is an extracted pseudo-article.
6719                     (setq article 'pseudo)
6720                     (gnus-request-pseudo-article header))))
6721
6722                 (let ((method (gnus-find-method-for-group
6723                                gnus-newsgroup-name)))
6724                   (when (and (eq (car method) 'nneething)
6725                              (vectorp header))
6726                     (let ((dir (nneething-get-file-name
6727                                 (mail-header-id header))))
6728                       (when (and (stringp dir)
6729                                  (file-directory-p dir))
6730                         (setq article 'nneething)
6731                         (gnus-group-enter-directory dir))))))))
6732
6733           (cond
6734            ;; Refuse to select canceled articles.
6735            ((and (numberp article)
6736                  gnus-summary-buffer
6737                  (get-buffer gnus-summary-buffer)
6738                  (gnus-buffer-exists-p gnus-summary-buffer)
6739                  (eq (cdr (with-current-buffer gnus-summary-buffer
6740                             (assq article gnus-newsgroup-reads)))
6741                      gnus-canceled-mark))
6742             nil)
6743            ;; We first check `gnus-original-article-buffer'.
6744            ((and (get-buffer gnus-original-article-buffer)
6745                  (numberp article)
6746                  (with-current-buffer gnus-original-article-buffer
6747                    (and (equal (car gnus-original-article) group)
6748                         (eq (cdr gnus-original-article) article))))
6749             ;; `insert-buffer-substring' would incorrectly use the
6750             ;; equivalent of string-make-multibyte which amount to decoding
6751             ;; with locale-coding-system, causing failure of
6752             ;; subsequent decoding.
6753             (insert (mm-string-to-multibyte
6754                      (with-current-buffer gnus-original-article-buffer
6755                        (buffer-substring (point-min) (point-max)))))
6756             'article)
6757            ;; Check the backlog.
6758            ((and gnus-keep-backlog
6759                  (gnus-backlog-request-article group article (current-buffer)))
6760             'article)
6761            ;; Check asynchronous pre-fetch.
6762            ((gnus-async-request-fetched-article group article (current-buffer))
6763             (gnus-async-prefetch-next group article gnus-summary-buffer)
6764             (when (and (numberp article) gnus-keep-backlog)
6765               (gnus-backlog-enter-article group article (current-buffer)))
6766             'article)
6767            ;; Check the cache.
6768            ((and gnus-use-cache
6769                  (numberp article)
6770                  (gnus-cache-request-article article group))
6771             'article)
6772            ;; Check the agent cache.
6773            ((gnus-agent-request-article article group)
6774             'article)
6775            ;; Get the article and put into the article buffer.
6776            ((or (stringp article)
6777                 (numberp article))
6778             (let ((gnus-override-method gnus-override-method)
6779                   (methods (and (stringp article)
6780                                 gnus-refer-article-method))
6781                   (backend (car (gnus-find-method-for-group
6782                                  gnus-newsgroup-name)))
6783                   result
6784                   (inhibit-read-only t))
6785               (if (or (not (listp methods))
6786                       (and (symbolp (car methods))
6787                            (assq (car methods) nnoo-definition-alist)))
6788                   (setq methods (list methods)))
6789               (when (and (null gnus-override-method)
6790                          methods)
6791                 (setq gnus-override-method (pop methods)))
6792               (while (not result)
6793                 (when (eq gnus-override-method 'current)
6794                   (setq gnus-override-method
6795                         (with-current-buffer gnus-summary-buffer
6796                           gnus-current-select-method)))
6797                 (erase-buffer)
6798                 (gnus-kill-all-overlays)
6799                 (let ((gnus-newsgroup-name group))
6800                   (gnus-check-group-server))
6801                 (cond
6802                  ((gnus-request-article article group (current-buffer))
6803                   (when (numberp article)
6804                     (gnus-async-prefetch-next group article
6805                                               gnus-summary-buffer)
6806                     (when gnus-keep-backlog
6807                       (gnus-backlog-enter-article
6808                        group article (current-buffer))))
6809                   (setq result 'article))
6810                  (methods
6811                   (setq gnus-override-method (pop methods)))
6812                  ((not (string-match "^400 "
6813                                      (nnheader-get-report backend)))
6814                   ;; If we get 400 server disconnect, reconnect and
6815                   ;; retry; otherwise, assume the article has expired.
6816                   (setq result 'done))))
6817               (and (eq result 'article) 'article)))
6818            ;; It was a pseudo.
6819            (t article)))
6820
6821       ;; Associate this article with the current summary buffer.
6822       (setq gnus-article-current-summary gnus-summary-buffer)
6823
6824       ;; Take the article from the original article buffer
6825       ;; and place it in the buffer it's supposed to be in.
6826       (when (and (get-buffer gnus-article-buffer)
6827                  (equal (buffer-name (current-buffer))
6828                         (buffer-name (get-buffer gnus-article-buffer))))
6829         (save-excursion
6830           (if (get-buffer gnus-original-article-buffer)
6831               (set-buffer gnus-original-article-buffer)
6832             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6833             (buffer-disable-undo)
6834             (setq major-mode 'gnus-original-article-mode)
6835             (setq buffer-read-only t))
6836           (let ((inhibit-read-only t))
6837             (erase-buffer)
6838             (insert-buffer-substring gnus-article-buffer))
6839           (setq gnus-original-article (cons group article)))
6840
6841         ;; Decode charsets.
6842         (run-hooks 'gnus-article-decode-hook)
6843         ;; Mark article as decoded or not.
6844         (setq gnus-article-decoded-p gnus-article-decode-hook))
6845
6846       ;; Update sparse articles.
6847       (when (and do-update-line
6848                  (or (numberp article)
6849                      (stringp article)))
6850         (let ((buf (current-buffer)))
6851           (set-buffer gnus-summary-buffer)
6852           (gnus-summary-update-article do-update-line sparse-header)
6853           (gnus-summary-goto-subject do-update-line nil t)
6854           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6855                             (point))
6856           (set-buffer buf))))))
6857
6858 (defun gnus-block-private-groups (group)
6859   (if (gnus-news-group-p group)
6860       ;; Block nothing in news groups.
6861       nil
6862     ;; Block everything anywhere else.
6863     "."))
6864
6865 (defun gnus-blocked-images ()
6866   (if (functionp gnus-blocked-images)
6867       (funcall gnus-blocked-images gnus-newsgroup-name)
6868     gnus-blocked-images))
6869
6870 ;;;
6871 ;;; Article editing
6872 ;;;
6873
6874 (defcustom gnus-article-edit-mode-hook nil
6875   "Hook run in article edit mode buffers."
6876   :group 'gnus-article-various
6877   :type 'hook)
6878
6879 (defvar gnus-article-edit-done-function nil)
6880
6881 (defvar gnus-article-edit-mode-map nil)
6882
6883 ;; Should we be using derived.el for this?
6884 (unless gnus-article-edit-mode-map
6885   (setq gnus-article-edit-mode-map (make-keymap))
6886   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6887
6888   (gnus-define-keys gnus-article-edit-mode-map
6889     "\C-c?"    describe-mode
6890     "\C-c\C-c" gnus-article-edit-done
6891     "\C-c\C-k" gnus-article-edit-exit
6892     "\C-c\C-f\C-t" message-goto-to
6893     "\C-c\C-f\C-o" message-goto-from
6894     "\C-c\C-f\C-b" message-goto-bcc
6895     ;;"\C-c\C-f\C-w" message-goto-fcc
6896     "\C-c\C-f\C-c" message-goto-cc
6897     "\C-c\C-f\C-s" message-goto-subject
6898     "\C-c\C-f\C-r" message-goto-reply-to
6899     "\C-c\C-f\C-n" message-goto-newsgroups
6900     "\C-c\C-f\C-d" message-goto-distribution
6901     "\C-c\C-f\C-f" message-goto-followup-to
6902     "\C-c\C-f\C-m" message-goto-mail-followup-to
6903     "\C-c\C-f\C-k" message-goto-keywords
6904     "\C-c\C-f\C-u" message-goto-summary
6905     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6906     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6907     "\C-c\C-b" message-goto-body
6908     "\C-c\C-i" message-goto-signature
6909
6910     "\C-c\C-t" message-insert-to
6911     "\C-c\C-n" message-insert-newsgroups
6912     "\C-c\C-o" message-sort-headers
6913     "\C-c\C-e" message-elide-region
6914     "\C-c\C-v" message-delete-not-region
6915     "\C-c\C-z" message-kill-to-signature
6916     "\M-\r" message-newline-and-reformat
6917     "\C-c\C-a" mml-attach-file
6918     "\C-a" message-beginning-of-line
6919     "\t" message-tab
6920     "\M-;" comment-region)
6921
6922   (gnus-define-keys (gnus-article-edit-wash-map
6923                      "\C-c\C-w" gnus-article-edit-mode-map)
6924     "f" gnus-article-edit-full-stops))
6925
6926 (easy-menu-define
6927   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6928   '("Field"
6929     ["Fetch To" message-insert-to t]
6930     ["Fetch Newsgroups" message-insert-newsgroups t]
6931     "----"
6932     ["To" message-goto-to t]
6933     ["From" message-goto-from t]
6934     ["Subject" message-goto-subject t]
6935     ["Cc" message-goto-cc t]
6936     ["Reply-To" message-goto-reply-to t]
6937     ["Summary" message-goto-summary t]
6938     ["Keywords" message-goto-keywords t]
6939     ["Newsgroups" message-goto-newsgroups t]
6940     ["Followup-To" message-goto-followup-to t]
6941     ["Mail-Followup-To" message-goto-mail-followup-to t]
6942     ["Distribution" message-goto-distribution t]
6943     ["Body" message-goto-body t]
6944     ["Signature" message-goto-signature t]))
6945
6946 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6947   "Major mode for editing articles.
6948 This is an extended text-mode.
6949
6950 \\{gnus-article-edit-mode-map}"
6951   (make-local-variable 'gnus-article-edit-done-function)
6952   (make-local-variable 'gnus-prev-winconf)
6953   (set (make-local-variable 'font-lock-defaults)
6954        '(message-font-lock-keywords t))
6955   (set (make-local-variable 'mail-header-separator) "")
6956   (set (make-local-variable 'gnus-article-edit-mode) t)
6957   (easy-menu-add message-mode-field-menu message-mode-map)
6958   (mml-mode)
6959   (setq buffer-read-only nil)
6960   (buffer-enable-undo)
6961   (widen))
6962
6963 (defun gnus-article-edit (&optional force)
6964   "Edit the current article.
6965 This will have permanent effect only in mail groups.
6966 If FORCE is non-nil, allow editing of articles even in read-only
6967 groups."
6968   (interactive "P")
6969   (when (and (not force)
6970              (gnus-group-read-only-p))
6971     (error "The current newsgroup does not support article editing"))
6972   (gnus-article-date-original)
6973   (gnus-article-edit-article
6974    'ignore
6975    `(lambda (no-highlight)
6976       'ignore
6977       (gnus-summary-edit-article-done
6978        ,(or (mail-header-references gnus-current-headers) "")
6979        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6980
6981 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6982   "Start editing the contents of the current article buffer."
6983   (let ((winconf (current-window-configuration)))
6984     (set-buffer gnus-article-buffer)
6985     (let ((message-auto-save-directory
6986            ;; Don't associate the article buffer with a draft file.
6987            nil))
6988       (gnus-article-edit-mode))
6989     (funcall start-func)
6990     (set-buffer-modified-p nil)
6991     (gnus-configure-windows 'edit-article)
6992     (setq gnus-article-edit-done-function exit-func)
6993     (setq gnus-prev-winconf winconf)
6994     (unless quiet
6995       (gnus-message 6 "C-c C-c to end edits"))))
6996
6997 (defun gnus-article-edit-done (&optional arg)
6998   "Update the article edits and exit."
6999   (interactive "P")
7000   (let ((func gnus-article-edit-done-function)
7001         (buf (current-buffer))
7002         (start (window-start))
7003         (p (point))
7004         (winconf gnus-prev-winconf))
7005     (widen) ;; Widen it in case that users narrowed the buffer.
7006     (funcall func arg)
7007     (set-buffer buf)
7008     ;; The cache and backlog have to be flushed somewhat.
7009     (when gnus-keep-backlog
7010       (gnus-backlog-remove-article
7011        (car gnus-article-current) (cdr gnus-article-current)))
7012     ;; Flush original article as well.
7013     (gnus-flush-original-article-buffer)
7014     (when gnus-use-cache
7015       (gnus-cache-update-article
7016        (car gnus-article-current) (cdr gnus-article-current)))
7017     ;; We remove all text props from the article buffer.
7018     (kill-all-local-variables)
7019     (set-text-properties (point-min) (point-max) nil)
7020     (gnus-article-mode)
7021     (set-window-configuration winconf)
7022     (set-buffer buf)
7023     (set-window-start (get-buffer-window buf) start)
7024     (set-window-point (get-buffer-window buf) (point)))
7025   (gnus-summary-show-article))
7026
7027 (defun gnus-flush-original-article-buffer ()
7028   (when (get-buffer gnus-original-article-buffer)
7029     (with-current-buffer gnus-original-article-buffer
7030       (setq gnus-original-article nil))))
7031
7032 (defun gnus-article-edit-exit ()
7033   "Exit the article editing without updating."
7034   (interactive)
7035   (when (or (not (buffer-modified-p))
7036             (yes-or-no-p "Article modified; kill anyway? "))
7037     (let ((curbuf (current-buffer))
7038           (p (point))
7039           (window-start (window-start)))
7040       (erase-buffer)
7041       (if (gnus-buffer-live-p gnus-original-article-buffer)
7042           (insert-buffer-substring gnus-original-article-buffer))
7043       (let ((winconf gnus-prev-winconf))
7044         (kill-all-local-variables)
7045         (gnus-article-mode)
7046         (set-window-configuration winconf)
7047         ;; Tippy-toe some to make sure that point remains where it was.
7048         (save-current-buffer
7049           (set-buffer curbuf)
7050           (set-window-start (get-buffer-window (current-buffer)) window-start)
7051           (goto-char p))))
7052     (gnus-summary-show-article)))
7053
7054 (defun gnus-article-edit-full-stops ()
7055   "Interactively repair spacing at end of sentences."
7056   (interactive)
7057   (save-excursion
7058     (goto-char (point-min))
7059     (search-forward-regexp "^$" nil t)
7060     (let ((case-fold-search nil))
7061       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7062
7063 ;;;
7064 ;;; Article highlights
7065 ;;;
7066
7067 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7068
7069 ;;; Internal Variables:
7070
7071 (defcustom gnus-button-url-regexp
7072   (concat
7073    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7074    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7075    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7076    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7077        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7078              (punct "!?:;.,"))
7079          (concat
7080           "\\(?:"
7081           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7082           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7083           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7084           "\\|"
7085           "[" chars punct     "]+" "[" chars "]"
7086           "\\)"))
7087      (concat ;; XEmacs 21.4 doesn't support POSIX.
7088       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7089       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7090    "\\)")
7091   "Regular expression that matches URLs."
7092   :group 'gnus-article-buttons
7093   :type 'regexp)
7094
7095 (defcustom gnus-button-valid-fqdn-regexp
7096   message-valid-fqdn-regexp
7097   "Regular expression that matches a valid FQDN."
7098   :version "22.1"
7099   :group 'gnus-article-buttons
7100   :type 'regexp)
7101
7102 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7103 (defcustom gnus-button-valid-localpart-regexp
7104   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7105   "Regular expression that matches a localpart of mail addresses or MIDs."
7106   :version "22.1"
7107   :group 'gnus-article-buttons
7108   :type 'regexp)
7109
7110 (defcustom gnus-button-man-handler 'manual-entry
7111   "Function to use for displaying man pages.
7112 The function must take at least one argument with a string naming the
7113 man page."
7114   :version "22.1"
7115   :type '(choice (function-item :tag "Man" manual-entry)
7116                  (function-item :tag "Woman" woman)
7117                  (function :tag "Other"))
7118   :group 'gnus-article-buttons)
7119
7120 (defcustom gnus-button-mid-or-mail-regexp
7121   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7122           gnus-button-valid-fqdn-regexp
7123           ">?\\)\\b")
7124   "Regular expression that matches a message ID or a mail address."
7125   :version "22.1"
7126   :group 'gnus-article-buttons
7127   :type 'regexp)
7128
7129 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7130   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7131 Strings like this can be either a message ID or a mail address.  If it is one
7132 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7133 message ID or a mail address, respectively.  If this variable is set to the
7134 symbol `ask', always query the user what do do.  If it is a function, this
7135 function will be called with the string as its only argument.  The function
7136 must return `mid', `mail', `invalid' or `ask'."
7137   :version "22.1"
7138   :group 'gnus-article-buttons
7139   :type '(choice (function-item :tag "Heuristic function"
7140                                 gnus-button-mid-or-mail-heuristic)
7141                  (const ask)
7142                  (const mid)
7143                  (const mail)))
7144
7145 (defcustom gnus-button-mid-or-mail-heuristic-alist
7146   '((-10.0 . ".+\\$.+@")
7147     (-10.0 . "#")
7148     (-10.0 . "\\*")
7149     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7150     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7151     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7152     (-1.0  . "^[^a-z]+@")
7153     ;;
7154     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7155     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7156     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7157     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7158     ;;
7159     (-2.0  . "^[0-9]")
7160     (-1.0  . "^[0-9][0-9]")
7161     ;;
7162     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7163     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7164     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7165     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7166     ;;
7167     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7168     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7169     ;;       "[0-9]{8,}.*\@"
7170     (-3.0
7171      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7172     ;; "[0-9]{12,}.*\@"
7173     ;; compensation for TDMA dated mail addresses:
7174     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7175     ;;
7176     (-20.0 . "\\.fsf@") ;; Gnus
7177     (-20.0 . "^slrn")
7178     (-20.0 . "^Pine")
7179     (-20.0 . "^alpine\\.")
7180     (-20.0 . "_-_") ;; Subject change in thread
7181     ;;
7182     (-20.0 . "\\.ln@") ;; leafnode
7183     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7184     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7185     ;;
7186     ;; (5.0 . "") ;; $local_part_len <= 7
7187     (10.0  . "^[^0-9]+@")
7188     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7189     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7190     (3.0   . "\@stud")
7191     ;;
7192     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7193     ;;
7194     (0.5   . "^[A-Z][a-z]")
7195     (0.5   . "^[A-Z][a-z][a-z]")
7196     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7197     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7198   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7199
7200 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7201 address.  The REGEXP is processed with `case-fold-search' set to nil."
7202   :version "22.1"
7203   :group 'gnus-article-buttons
7204   :type '(repeat (cons (number :tag "Rate")
7205                        (regexp :tag "Regexp"))))
7206
7207 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7208   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7209 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7210 address, `ask' if unsure and `invalid' if the string is invalid."
7211   (let ((case-fold-search nil)
7212         (list gnus-button-mid-or-mail-heuristic-alist)
7213         (result 0) rate regexp lpartlen elem)
7214     (setq lpartlen
7215           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7216     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7217     ;; Certain special cases...
7218     (when (string-match
7219            (concat
7220             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7221             "^[0-9]+\\.[0-9]+@compuserve\\|"
7222             "@public\\.gmane\\.org")
7223            mid-or-mail)
7224       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7225       (setq result 'mail))
7226     (when (string-match "@.*@\\| " mid-or-mail)
7227       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7228       (setq result 'invalid))
7229     ;; Nothing more to do, if result is not a number here...
7230     (when (numberp result)
7231       (while list
7232         (setq elem (car list)
7233               rate (car elem)
7234               regexp (cdr elem)
7235               list (cdr list))
7236         (when (string-match regexp mid-or-mail)
7237           (setq result (+ result rate))
7238           (gnus-message
7239            9 "`%s' matched `%s', rate `%s', result `%s'."
7240            mid-or-mail regexp rate result)))
7241       (when (<= lpartlen 7)
7242         (setq result (+ result 5.0))
7243         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7244                       mid-or-mail result))
7245       (when (>= lpartlen 12)
7246         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7247         (cond
7248          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7249           ;; Long local part should contain realname if e-mail address,
7250           ;; too many digits: message-id.
7251           ;; $score -= 5.0 + 0.1 * $local_part_len;
7252           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7253           (setq result (+ result rate))
7254           (gnus-message
7255            9 "Many digits in `%s', rate `%s', result `%s'."
7256            mid-or-mail rate result))
7257          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7258                         mid-or-mail)
7259           ;; Too few vowels [^aeiouy]{4,}.*\@
7260           (setq result (+ result -5.0))
7261           (gnus-message
7262            9 "Few vowels in `%s', rate `%s', result `%s'."
7263            mid-or-mail -5.0 result))
7264          (t
7265           (setq result (+ result 5.0))
7266           (gnus-message
7267            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7268     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7269     ;; Maybe we should make this a customizable alist: (condition . 'result)
7270     (cond
7271      ((symbolp result) result)
7272      ;; Now convert number into proper results:
7273      ((< result -10.0) 'mid)
7274      ((> result  10.0) 'mail)
7275      (t 'ask))))
7276
7277 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7278   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7279          (url-mid (concat "news" ":" mid-or-mail))
7280          (url-mailto (concat "mailto" ":" mid-or-mail)))
7281     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7282     (when (fboundp pref)
7283       (setq guessed
7284             ;; get rid of surrounding angles...
7285             (funcall pref
7286                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7287       (if (or (eq 'mid guessed) (eq 'mail guessed))
7288           (setq pref guessed)
7289         (setq pref 'ask)))
7290     (if (eq pref 'ask)
7291         (save-window-excursion
7292           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7293               (setq pref 'mail)
7294             (setq pref 'mid))))
7295     (cond ((eq pref 'mid)
7296            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7297            (gnus-button-handle-news url-mid))
7298           ((eq pref 'mail)
7299            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7300            (gnus-url-mailto url-mailto))
7301           (t (gnus-message 3 "Invalid string.")))))
7302
7303 (defun gnus-button-handle-custom (fun arg)
7304   "Call function FUN on argument ARG.
7305 Both FUN and ARG are supposed to be strings.  ARG will be passed
7306 as a symbol to FUN."
7307   (funcall (intern fun)
7308            (if (string-match "^customize-apropos" fun)
7309                arg
7310              (intern arg))))
7311
7312 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7313
7314 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7315 ;; stuff?
7316
7317 (defun gnus-button-handle-describe-function (url)
7318   "Call `describe-function' when pushing the corresponding URL button."
7319   (describe-function
7320    (intern
7321     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7322
7323 (defun gnus-button-handle-describe-variable (url)
7324   "Call `describe-variable' when pushing the corresponding URL button."
7325   (describe-variable
7326    (intern
7327     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7328
7329 (defun gnus-button-handle-symbol (url)
7330 "Display help on variable or function.
7331 Calls `describe-variable' or `describe-function'."
7332   (let ((sym (intern url)))
7333     (cond
7334      ((fboundp sym) (describe-function sym))
7335      ((boundp sym) (describe-variable sym))
7336      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7337
7338 (defun gnus-button-handle-describe-key (url)
7339   "Call `describe-key' when pushing the corresponding URL button."
7340   (let* ((key-string
7341           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7342          (keys (ignore-errors (eval `(kbd ,key-string)))))
7343     (if keys
7344         (describe-key keys)
7345       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7346
7347 (defun gnus-button-handle-apropos (url)
7348   "Call `apropos' when pushing the corresponding URL button."
7349   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7350
7351 (defun gnus-button-handle-apropos-command (url)
7352   "Call `apropos' when pushing the corresponding URL button."
7353   (apropos-command
7354    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7355
7356 (defun gnus-button-handle-apropos-variable (url)
7357   "Call `apropos' when pushing the corresponding URL button."
7358   (funcall
7359    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7360    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7361
7362 (defun gnus-button-handle-apropos-documentation (url)
7363   "Call `apropos' when pushing the corresponding URL button."
7364   (funcall
7365    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7366    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7367
7368 (defun gnus-button-handle-library (url)
7369   "Call `locate-library' when pushing the corresponding URL button."
7370   (gnus-message 9 "url=`%s'" url)
7371   (let* ((lib (locate-library url))
7372          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7373     (if (not lib)
7374         (gnus-message 1 "Cannot locale library `%s'." url)
7375       (find-file-read-only file))))
7376
7377 (defcustom gnus-button-man-level 5
7378   "*Integer that says how many man-related buttons Gnus will show.
7379 The higher the number, the more buttons will appear and the more false
7380 positives are possible.  Note that you can set this variable local to
7381 specific groups.  Setting it higher in Unix groups is probably a good idea.
7382 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7383 how to set variables in specific groups."
7384   :version "22.1"
7385   :group 'gnus-article-buttons
7386   :link '(custom-manual "(gnus)Group Parameters")
7387   :type 'integer)
7388
7389 (defcustom gnus-button-emacs-level 5
7390   "*Integer that says how many emacs-related buttons Gnus will show.
7391 The higher the number, the more buttons will appear and the more false
7392 positives are possible.  Note that you can set this variable local to
7393 specific groups.  Setting it higher in Emacs or Gnus related groups is
7394 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7395 `gnus-parameters' on how to set variables in specific groups."
7396   :version "22.1"
7397   :group 'gnus-article-buttons
7398   :link '(custom-manual "(gnus)Group Parameters")
7399   :type 'integer)
7400
7401 (defcustom gnus-button-message-level 5
7402   "*Integer that says how many buttons for news or mail messages will appear.
7403 The higher the number, the more buttons will appear and the more false
7404 positives are possible."
7405   ;; mail addresses, MIDs, URLs for news, ...
7406   :version "22.1"
7407   :group 'gnus-article-buttons
7408   :type 'integer)
7409
7410 (defcustom gnus-button-browse-level 5
7411   "*Integer that says how many buttons for browsing will appear.
7412 The higher the number, the more buttons will appear and the more false
7413 positives are possible."
7414   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7415   :version "22.1"
7416   :group 'gnus-article-buttons
7417   :type 'integer)
7418
7419 (defcustom gnus-button-alist
7420   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7421      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7422     ((concat "\\b\\(nntp\\|news\\):\\("
7423              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7424      0 t gnus-button-handle-news 2)
7425     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7426      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7427     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7428      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7429     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7430     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7431      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7432     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7433      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7434     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7435      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7436     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7437      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7438     ;; RFC 2368 (The mailto URL scheme)
7439     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7440      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7441     ("\\bmailto:\\([^ \n\t]+\\)"
7442      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7443     ;; Info Konqueror style <info:/foo/bar baz>.
7444     ;; Must come before " Gnus home-grown style".
7445     ("\\binfo://?\\([^'\">\n\t]+\\)"
7446      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7447    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7448     ("\\binfo://\\([^'\">\n\t ]+\\)"
7449      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7450     ;; Info GNOME style <info:foo#bar_baz>
7451     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7452      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7453     ;; Info KDE style <info:(foo)bar baz>
7454     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7455      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7456     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7457      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7458     ("\\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,]*\\)\\)?"
7459      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7460      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7461     ;; This is custom
7462     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7463      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7464     ;; Emacs help commands
7465     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7466      ;; regexp doesn't match arguments containing ` '.
7467      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7468     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7469      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7470     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7471      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7472     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7473      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7474     ;; The following entries may lead to many false positives so don't enable
7475     ;; them by default (use a high button level).
7476     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7477      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7478      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7479     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7480      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7481     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7482      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7483     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7484      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7485     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7486      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7487     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7488      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7489     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7490      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7491     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7492      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7493     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7494      ;; Unlike the other regexps we really have to require quoting
7495      ;; here to determine where it ends.
7496      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7497     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7498     ("<URL: *\\([^\n<>]*\\)>"
7499      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7500     ;; RFC 2396 (2.4.3., delims) ...
7501     ("\"URL: *\\([^\n\"]*\\)\""
7502      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7503     ;; Raw URLs.
7504     (gnus-button-url-regexp
7505      0 (>= gnus-button-browse-level 0) browse-url 0)
7506     ;; man pages
7507     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7508      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7509      gnus-button-handle-man 1)
7510     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7511     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7512      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7513      gnus-button-handle-man 1)
7514     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7515     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7516     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7517      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7518     ;; Recognizing patches to .el files.  This is somewhat obscure,
7519     ;; but considering the percentage of Gnus users who hack Emacs
7520     ;; Lisp files...
7521     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7522      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7523     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7524      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7525     ;; MID or mail: To avoid too many false positives we don't try to catch
7526     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7527     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7528     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7529     ;; so that non-ambiguous entries (see above) match first.
7530     (gnus-button-mid-or-mail-regexp
7531      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7532   "*Alist of regexps matching buttons in article bodies.
7533
7534 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7535 REGEXP: is the string (case insensitive) matching text around the button (can
7536 also be Lisp expression evaluating to a string),
7537 BUTTON: is the number of the regexp grouping actually matching the button,
7538 FORM: is a Lisp expression which must eval to true for the button to
7539 be added,
7540 CALLBACK: is the function to call when the user push this button, and each
7541 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7542
7543 CALLBACK can also be a variable, in that case the value of that
7544 variable it the real callback function."
7545   :group 'gnus-article-buttons
7546   :type '(repeat (list (choice regexp variable sexp)
7547                        (integer :tag "Button")
7548                        (sexp :tag "Form")
7549                        (function :tag "Callback")
7550                        (repeat :tag "Par"
7551                                :inline t
7552                                (integer :tag "Regexp group")))))
7553 (put 'gnus-button-alist 'risky-local-variable t)
7554
7555 (defcustom gnus-header-button-alist
7556   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7557      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7558     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7559      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7560     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7561      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7562     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7563      0 (>= gnus-button-browse-level 0) browse-url 0)
7564     ("^Subject:" gnus-button-url-regexp
7565      0 (>= gnus-button-browse-level 0) browse-url 0)
7566     ("^[^:]+:" gnus-button-url-regexp
7567      0 (>= gnus-button-browse-level 0) browse-url 0)
7568     ("^OpenPGP:.*url=" gnus-button-url-regexp
7569      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7570     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7571      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7572     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7573      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7574   "*Alist of headers and regexps to match buttons in article heads.
7575
7576 This alist is very similar to `gnus-button-alist', except that each
7577 alist has an additional HEADER element first in each entry:
7578
7579 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7580
7581 HEADER is a regexp to match a header.  For a fuller explanation, see
7582 `gnus-button-alist'."
7583   :group 'gnus-article-buttons
7584   :group 'gnus-article-headers
7585   :type '(repeat (list (regexp :tag "Header")
7586                        (choice regexp variable)
7587                        (integer :tag "Button")
7588                        (sexp :tag "Form")
7589                        (function :tag "Callback")
7590                        (repeat :tag "Par"
7591                                :inline t
7592                                (integer :tag "Regexp group")))))
7593 (put 'gnus-header-button-alist 'risky-local-variable t)
7594
7595 ;;; Commands:
7596
7597 (defun gnus-article-push-button (event)
7598   "Check text under the mouse pointer for a callback function.
7599 If the text under the mouse pointer has a `gnus-callback' property,
7600 call it with the value of the `gnus-data' text property."
7601   (interactive "e")
7602   (set-buffer (window-buffer (posn-window (event-start event))))
7603   (let* ((pos (posn-point (event-start event)))
7604          (data (get-text-property pos 'gnus-data))
7605          (fun (get-text-property pos 'gnus-callback)))
7606     (goto-char pos)
7607     (when fun
7608       (funcall fun data))))
7609
7610 (defun gnus-article-press-button ()
7611   "Check text at point for a callback function.
7612 If the text at point has a `gnus-callback' property,
7613 call it with the value of the `gnus-data' text property."
7614   (interactive)
7615   (let ((data (get-text-property (point) 'gnus-data))
7616         (fun (get-text-property (point) 'gnus-callback)))
7617     (when fun
7618       (funcall fun data))))
7619
7620 (defun gnus-article-highlight (&optional force)
7621   "Highlight current article.
7622 This function calls `gnus-article-highlight-headers',
7623 `gnus-article-highlight-citation',
7624 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7625 do the highlighting.  See the documentation for those functions."
7626   (interactive (list 'force))
7627   (gnus-article-highlight-headers)
7628   (gnus-article-highlight-citation force)
7629   (gnus-article-highlight-signature)
7630   (gnus-article-add-buttons force)
7631   (gnus-article-add-buttons-to-head))
7632
7633 (defun gnus-article-highlight-some (&optional force)
7634   "Highlight current article.
7635 This function calls `gnus-article-highlight-headers',
7636 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7637 do the highlighting.  See the documentation for those functions."
7638   (interactive (list 'force))
7639   (gnus-article-highlight-headers)
7640   (gnus-article-highlight-signature)
7641   (gnus-article-add-buttons))
7642
7643 (defun gnus-article-highlight-headers ()
7644   "Highlight article headers as specified by `gnus-header-face-alist'."
7645   (interactive)
7646   (gnus-with-article-headers
7647     (let (regexp header-face field-face from hpoints fpoints)
7648       (dolist (entry gnus-header-face-alist)
7649         (goto-char (point-min))
7650         (setq regexp (concat "^\\("
7651                              (if (string-equal "" (nth 0 entry))
7652                                  "[^\t ]"
7653                                (nth 0 entry))
7654                              "\\)")
7655               header-face (nth 1 entry)
7656               field-face (nth 2 entry))
7657         (while (and (re-search-forward regexp nil t)
7658                     (not (eobp)))
7659           (beginning-of-line)
7660           (setq from (point))
7661           (unless (search-forward ":" nil t)
7662             (forward-char 1))
7663           (when (and header-face
7664                      (not (memq (point) hpoints)))
7665             (push (point) hpoints)
7666             (gnus-put-text-property from (point) 'face header-face))
7667           (when (and field-face
7668                      (not (memq (setq from (point)) fpoints)))
7669             (push from fpoints)
7670             (if (re-search-forward "^[^ \t]" nil t)
7671                 (forward-char -2)
7672               (goto-char (point-max)))
7673             (gnus-put-text-property from (point) 'face field-face)))))))
7674
7675 (defun gnus-article-highlight-signature ()
7676   "Highlight the signature in an article.
7677 It does this by highlighting everything after
7678 `gnus-signature-separator' using the face `gnus-signature'."
7679   (interactive)
7680   (gnus-with-article-buffer
7681     (let ((inhibit-point-motion-hooks t))
7682       (save-restriction
7683         (when (and gnus-signature-face
7684                    (gnus-article-narrow-to-signature))
7685           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7686                             'face gnus-signature-face)
7687           (widen)
7688           (gnus-article-search-signature)
7689           (let ((start (match-beginning 0))
7690                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7691             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7692                                      end)))))))
7693
7694 (defun gnus-button-in-region-p (b e prop)
7695   "Say whether PROP exists in the region."
7696   (text-property-not-all b e prop nil))
7697
7698 (defun gnus-article-add-buttons (&optional force)
7699   "Find external references in the article and make buttons of them.
7700 \"External references\" are things like Message-IDs and URLs, as
7701 specified by `gnus-button-alist'."
7702   (interactive (list 'force))
7703   (gnus-with-article-buffer
7704     (let ((inhibit-point-motion-hooks t)
7705           (case-fold-search t)
7706           (alist gnus-button-alist)
7707           beg entry regexp)
7708       ;; Remove all old markers.
7709       (let (marker entry new-list)
7710         (while (setq marker (pop gnus-button-marker-list))
7711           (if (or (< marker (point-min)) (>= marker (point-max)))
7712               (push marker new-list)
7713             (goto-char marker)
7714             (when (setq entry (gnus-button-entry))
7715               (put-text-property (match-beginning (nth 1 entry))
7716                                  (match-end (nth 1 entry))
7717                                  'gnus-callback nil))
7718             (set-marker marker nil)))
7719         (setq gnus-button-marker-list new-list))
7720       ;; We skip the headers.
7721       (article-goto-body)
7722       (setq beg (point))
7723       (while (setq entry (pop alist))
7724         (setq regexp (eval (car entry)))
7725         (goto-char beg)
7726         (while (re-search-forward regexp nil t)
7727           (let ((start (match-beginning (nth 1 entry)))
7728                 (end (match-end (nth 1 entry)))
7729                 (from (match-beginning 0)))
7730             (when (and (or (eq t (nth 2 entry))
7731                            (eval (nth 2 entry)))
7732                        (not (gnus-button-in-region-p
7733                              start end 'gnus-callback)))
7734               ;; That optional form returned non-nil, so we add the
7735               ;; button.
7736               (setq from (set-marker (make-marker) from))
7737               (push from gnus-button-marker-list)
7738               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7739                            (gnus-article-extend-url-button from start end))
7740                 (gnus-article-add-button start end
7741                                          'gnus-button-push from)
7742                 (gnus-put-text-property
7743                  start end
7744                  'gnus-string (buffer-substring-no-properties
7745                                start end))))))))))
7746
7747 (defun gnus-article-extend-url-button (beg start end)
7748   "Extend url button if url is folded into two or more lines.
7749 Return non-nil if button is extended.  BEG is a marker that points to
7750 the beginning position of a text containing url.  START and END are
7751 the endpoints of a url button before it is extended.  The concatenated
7752 url is put as the `gnus-button-url' overlay property on the button."
7753   (let ((opoint (point))
7754         (points (list start end))
7755         url delim regexp)
7756     (prog1
7757         (when (and (progn
7758                      (goto-char end)
7759                      (not (looking-at "[\t ]*[\">]")))
7760                    (progn
7761                      (goto-char start)
7762                      (string-match
7763                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7764                       (buffer-substring (point-at-bol) start)))
7765                    (progn
7766                      (setq url (list (buffer-substring start end))
7767                            delim (if (match-beginning 1) ">" "\""))
7768                      (beginning-of-line)
7769                      (setq regexp (concat
7770                                    (when (and (looking-at
7771                                                message-cite-prefix-regexp)
7772                                               (< (match-end 0) start))
7773                                      (regexp-quote (match-string 0)))
7774                                    "\
7775 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7776                                    delim "\\)"))
7777                      (while (progn
7778                               (forward-line 1)
7779                               (and (looking-at regexp)
7780                                    (prog1
7781                                        (match-beginning 1)
7782                                      (push (or (match-string 2)
7783                                                (match-string 1))
7784                                            url)
7785                                      (push (setq end (or (match-end 2)
7786                                                          (match-end 1)))
7787                                            points)
7788                                      (push (or (match-beginning 2)
7789                                                (match-beginning 1))
7790                                            points)))))
7791                      (match-beginning 2)))
7792           (let (gnus-article-mouse-face widget-mouse-face)
7793             (while points
7794               (gnus-article-add-button (pop points) (pop points)
7795                                        'gnus-button-push beg)))
7796           (let ((overlay (gnus-make-overlay start end)))
7797             (gnus-overlay-put overlay 'evaporate t)
7798             (gnus-overlay-put overlay 'gnus-button-url
7799                               (list (mapconcat 'identity (nreverse url) "")))
7800             (when gnus-article-mouse-face
7801               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7802           t)
7803       (goto-char opoint))))
7804
7805 ;; Add buttons to the head of an article.
7806 (defun gnus-article-add-buttons-to-head ()
7807   "Add buttons to the head of the article."
7808   (interactive)
7809   (gnus-with-article-headers
7810     (let (beg end)
7811       (dolist (entry gnus-header-button-alist)
7812         ;; Each alist entry.
7813         (goto-char (point-min))
7814         (while (re-search-forward (car entry) nil t)
7815           ;; Each header matching the entry.
7816           (setq beg (match-beginning 0))
7817           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7818                              (match-beginning 0))
7819                         (point-max)))
7820           (goto-char beg)
7821           (while (re-search-forward (eval (nth 1 entry)) end t)
7822             ;; Each match within a header.
7823             (let* ((entry (cdr entry))
7824                    (start (match-beginning (nth 1 entry)))
7825                    (end (match-end (nth 1 entry)))
7826                    (form (nth 2 entry)))
7827               (goto-char (match-end 0))
7828               (when (eval form)
7829                 (gnus-article-add-button
7830                  start end (nth 3 entry)
7831                  (buffer-substring (match-beginning (nth 4 entry))
7832                                    (match-end (nth 4 entry)))))))
7833           (goto-char end))))))
7834
7835 ;;; External functions:
7836
7837 (defun gnus-article-add-button (from to fun &optional data text)
7838   "Create a button between FROM and TO with callback FUN and data DATA."
7839   (when gnus-article-button-face
7840     (gnus-overlay-put (gnus-make-overlay from to nil t)
7841                       'face gnus-article-button-face))
7842   (gnus-add-text-properties
7843    from to
7844    (nconc (and gnus-article-mouse-face
7845                (list gnus-mouse-face-prop gnus-article-mouse-face))
7846           (list 'gnus-callback fun)
7847           (and data (list 'gnus-data data))))
7848   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7849                          :help-echo (or text "Follow the link")
7850                          :keymap gnus-url-button-map
7851                          :button-keymap gnus-widget-button-keymap))
7852
7853 (defun gnus-article-copy-string ()
7854   "Copy the string in the button to the kill ring."
7855   (interactive)
7856   (gnus-article-check-buffer)
7857   (let ((data (get-text-property (point) 'gnus-string)))
7858     (when data
7859       (with-temp-buffer
7860         (insert data)
7861         (copy-region-as-kill (point-min) (point-max))
7862         (message "Copied %s" data)))))
7863
7864 ;;; Internal functions:
7865
7866 (defun gnus-article-set-globals ()
7867   (with-current-buffer gnus-summary-buffer
7868     (gnus-set-global-variables)))
7869
7870 (defun gnus-signature-toggle (end)
7871   (gnus-with-article-buffer
7872     (let ((inhibit-point-motion-hooks t))
7873       (if (text-property-any end (point-max) 'article-type 'signature)
7874           (progn
7875             (gnus-delete-wash-type 'signature)
7876             (gnus-remove-text-properties-when
7877              'article-type 'signature end (point-max)
7878              (cons 'article-type (cons 'signature
7879                                        gnus-hidden-properties))))
7880         (gnus-add-wash-type 'signature)
7881         (gnus-add-text-properties-when
7882          'article-type nil end (point-max)
7883          (cons 'article-type (cons 'signature
7884                                    gnus-hidden-properties)))))
7885     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7886       (gnus-set-mode-line 'article))))
7887
7888 (defun gnus-button-entry ()
7889   ;; Return the first entry in `gnus-button-alist' matching this place.
7890   (let ((alist gnus-button-alist)
7891         (entry nil))
7892     (while alist
7893       (setq entry (pop alist))
7894       (if (looking-at (eval (car entry)))
7895           (setq alist nil)
7896         (setq entry nil)))
7897     entry))
7898
7899 (defun gnus-button-push (marker)
7900   ;; Push button starting at MARKER.
7901   (save-excursion
7902     (goto-char marker)
7903     (let* ((entry (gnus-button-entry))
7904            (inhibit-point-motion-hooks t)
7905            (fun (nth 3 entry))
7906            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7907                           (get-char-property marker 'gnus-button-url))
7908                      (mapcar (lambda (group)
7909                                (let ((string (match-string group)))
7910                                  (set-text-properties
7911                                   0 (length string) nil string)
7912                                  string))
7913                              (nthcdr 4 entry)))))
7914       (cond
7915        ((fboundp fun)
7916         (apply fun args))
7917        ((and (boundp fun)
7918              (fboundp (symbol-value fun)))
7919         (apply (symbol-value fun) args))
7920        (t
7921         (gnus-message 1 "You must define `%S' to use this button"
7922                       (cons fun args)))))))
7923
7924 (defun gnus-parse-news-url (url)
7925   (let (scheme server port group message-id articles)
7926     (with-temp-buffer
7927       (insert url)
7928       (goto-char (point-min))
7929       (when (looking-at "\\([A-Za-z]+\\):")
7930         (setq scheme (match-string 1))
7931         (goto-char (match-end 0)))
7932       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7933         (setq server (match-string 1))
7934         (setq port (if (stringp (match-string 3))
7935                        (string-to-number (match-string 3))
7936                      (match-string 3)))
7937         (goto-char (match-end 0)))
7938
7939       (cond
7940        ((looking-at "\\(.*@.*\\)")
7941         (setq message-id (match-string 1)))
7942        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7943         (setq group (match-string 1)
7944               articles (split-string (match-string 2) "-")))
7945        ((looking-at "\\([^/]+\\)/?")
7946         (setq group (match-string 1)))
7947        (t
7948         (error "Unknown news URL syntax"))))
7949     (list scheme server port group message-id articles)))
7950
7951 (defun gnus-button-handle-news (url)
7952   "Fetch a news URL."
7953   (destructuring-bind (scheme server port group message-id articles)
7954       (gnus-parse-news-url url)
7955     (cond
7956      (message-id
7957       (with-current-buffer gnus-summary-buffer
7958         (if server
7959             (let ((gnus-refer-article-method
7960                    (nconc (list (list 'nntp server))
7961                           gnus-refer-article-method))
7962                   (nntp-port-number (or port "nntp")))
7963               (gnus-message 7 "Fetching %s with %s"
7964                             message-id gnus-refer-article-method)
7965               (gnus-summary-refer-article message-id))
7966           (gnus-summary-refer-article message-id))))
7967      (group
7968       (gnus-button-fetch-group url)))))
7969
7970 (defun gnus-button-patch (library line)
7971   "Visit an Emacs Lisp library LIBRARY on line LINE."
7972   (interactive)
7973   (let ((file (locate-library (file-name-nondirectory library))))
7974     (unless file
7975       (error "Couldn't find library %s" library))
7976     (find-file file)
7977     (goto-char (point-min))
7978     (forward-line (1- (string-to-number line)))))
7979
7980 (defun gnus-button-handle-man (url)
7981   "Fetch a man page."
7982   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7983   (when (eq gnus-button-man-handler 'woman)
7984     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7985   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7986   (funcall gnus-button-man-handler url))
7987
7988 (defun gnus-button-handle-info-url (url)
7989   "Fetch an info URL."
7990   (setq url (mm-subst-char-in-string ?+ ?\  url))
7991   (cond
7992    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7993     (gnus-info-find-node
7994      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7995                      "Gnus")
7996              ")" (gnus-url-unhex-string (match-string 2 url)))))
7997    ((string-match "([^)\"]+)[^\"]+" url)
7998     (setq url
7999           (gnus-replace-in-string
8000            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8001     (gnus-info-find-node url))
8002    (t (error "Can't parse %s" url))))
8003
8004 (defun gnus-button-handle-info-url-gnome (url)
8005   "Fetch GNOME style info URL."
8006   (setq url (mm-subst-char-in-string ?_ ?\  url))
8007   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8008       (gnus-info-find-node
8009        (concat "("
8010                (gnus-url-unhex-string
8011                  (match-string 1 url))
8012                ")"
8013                (or (gnus-url-unhex-string
8014                     (match-string 2 url))
8015                    "Top")))
8016     (error "Can't parse %s" url)))
8017
8018 (defun gnus-button-handle-info-url-kde (url)
8019   "Fetch KDE style info URL."
8020   (gnus-info-find-node (gnus-url-unhex-string url)))
8021
8022 ;; (info) will autoload info.el
8023 (declare-function Info-menu "info" (menu-item &optional fork))
8024 (declare-function Info-index-next "info" (num))
8025
8026 (defun gnus-button-handle-info-keystrokes (url)
8027   "Call `info' when pushing the corresponding URL button."
8028   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8029   (let (node indx comma)
8030     (if (string-match
8031          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8032                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8033                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
8034                  "\\(?:[ \t\n,]*\\)\\)?")
8035          url)
8036         (setq node (match-string 2 url)
8037               indx (match-string 3 url))
8038       (error "Can't parse %s" url))
8039     (info)
8040     (Info-directory)
8041     (Info-menu node)
8042     (when (> (length indx) 0)
8043       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8044                             "\\([ \t\n,]*\\)")
8045                     indx)
8046       (setq comma (match-string 2 indx))
8047       (setq indx  (match-string 1 indx))
8048       (Info-index indx)
8049       (when comma
8050         (dotimes (i (with-temp-buffer
8051                       (insert comma)
8052                       ;; Note: the XEmacs version of `how-many' takes
8053                       ;; no optional argument.
8054                       (goto-char (point-min))
8055                       (how-many ",")))
8056           (Info-index-next 1)))
8057       nil)))
8058
8059 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8060 (declare-function pgg-display-output-buffer "pgg" (start end status))
8061
8062 (defun gnus-button-openpgp (url)
8063   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8064   (with-temp-buffer
8065     (mm-url-insert-file-contents-external url)
8066     (pgg-snarf-keys-region (point-min) (point-max))
8067     (pgg-display-output-buffer nil nil nil)))
8068
8069 (defun gnus-button-message-id (message-id)
8070   "Fetch MESSAGE-ID."
8071   (with-current-buffer gnus-summary-buffer
8072     (gnus-summary-refer-article message-id)))
8073
8074 (defun gnus-button-fetch-group (address &rest ignore)
8075   "Fetch GROUP specified by ADDRESS."
8076   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8077                       address)
8078     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8079     ;; for nntp:// and news://
8080     (setq address (match-string 3 address)))
8081   (if (not (string-match "[:/]" address))
8082       ;; This is just a simple group url.
8083       (gnus-group-read-ephemeral-group address gnus-select-method)
8084     (if (not
8085          (string-match
8086           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8087           address))
8088         (error "Can't parse %s" address)
8089       (gnus-group-read-ephemeral-group
8090        (match-string 4 address)
8091        `(nntp ,(match-string 1 address)
8092               (nntp-address ,(match-string 1 address))
8093               (nntp-port-number ,(if (match-end 3)
8094                                      (match-string 3 address)
8095                                    "nntp")))
8096        nil nil nil
8097        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8098
8099 (defun gnus-url-parse-query-string (query &optional downcase)
8100   (let (retval pairs cur key val)
8101     (setq pairs (split-string query "&"))
8102     (while pairs
8103       (setq cur (car pairs)
8104             pairs (cdr pairs))
8105       (if (not (string-match "=" cur))
8106           nil                           ; Grace
8107         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8108               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8109         (if downcase
8110             (setq key (downcase key)))
8111         (setq cur (assoc key retval))
8112         (if cur
8113             (setcdr cur (cons val (cdr cur)))
8114           (setq retval (cons (list key val) retval)))))
8115     retval))
8116
8117 (defun gnus-url-mailto (url)
8118   ;; Send mail to someone
8119   (when (string-match "mailto:/*\\(.*\\)" url)
8120     (setq url (substring url (match-beginning 1) nil)))
8121   (let (to args subject func)
8122     (setq args (gnus-url-parse-query-string
8123                 (if (string-match "^\\?" url)
8124                     (substring url 1)
8125                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8126                       (concat "to=" (match-string 1 url) "&"
8127                               (match-string 2 url))
8128                     (concat "to=" url)))
8129                 t)
8130           subject (cdr-safe (assoc "subject" args)))
8131     (gnus-msg-mail)
8132     (while args
8133       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8134       (if (fboundp func)
8135           (funcall func)
8136         (message-position-on-field (caar args)))
8137       (insert (gnus-replace-in-string
8138                (mapconcat 'identity (reverse (cdar args)) ", ")
8139                "\r\n" "\n" t))
8140       (setq args (cdr args)))
8141     (if subject
8142         (message-goto-body)
8143       (message-goto-subject))))
8144
8145 (defun gnus-button-embedded-url (address)
8146   "Activate ADDRESS with `browse-url'."
8147   (browse-url (gnus-strip-whitespace address)))
8148
8149 ;;; Next/prev buttons in the article buffer.
8150
8151 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8152 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8153
8154 (defvar gnus-prev-page-map
8155   (let ((map (make-sparse-keymap)))
8156     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8157     (define-key map "\r" 'gnus-button-prev-page)
8158     map))
8159
8160 (defvar gnus-next-page-map
8161   (let ((map (make-sparse-keymap)))
8162     (define-key map gnus-mouse-2 'gnus-button-next-page)
8163     (define-key map "\r" 'gnus-button-next-page)
8164     map))
8165
8166 (defun gnus-insert-prev-page-button ()
8167   (let ((b (point)) e
8168         (inhibit-read-only t))
8169     (gnus-eval-format
8170      gnus-prev-page-line-format nil
8171      `(keymap ,gnus-prev-page-map
8172               gnus-prev t
8173               gnus-callback gnus-article-button-prev-page
8174               article-type annotation))
8175     (setq e (if (bolp)
8176                 ;; Exclude a newline.
8177                 (1- (point))
8178               (point)))
8179     (when gnus-article-button-face
8180       (gnus-overlay-put (gnus-make-overlay b e nil t)
8181                         'face gnus-article-button-face))
8182     (widget-convert-button
8183      'link b e
8184      :action 'gnus-button-prev-page
8185      :button-keymap gnus-prev-page-map)))
8186
8187 (defun gnus-button-next-page (&optional args more-args)
8188   "Go to the next page."
8189   (interactive)
8190   (let ((win (selected-window)))
8191     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8192     (gnus-article-next-page)
8193     (select-window win)))
8194
8195 (defun gnus-button-prev-page (&optional args more-args)
8196   "Go to the prev page."
8197   (interactive)
8198   (let ((win (selected-window)))
8199     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8200     (gnus-article-prev-page)
8201     (select-window win)))
8202
8203 (defun gnus-insert-next-page-button ()
8204   (let ((b (point)) e
8205         (inhibit-read-only t))
8206     (gnus-eval-format gnus-next-page-line-format nil
8207                       `(keymap ,gnus-next-page-map
8208                                gnus-next t
8209                                gnus-callback gnus-article-button-next-page
8210                                article-type annotation))
8211     (setq e (if (bolp)
8212                 ;; Exclude a newline.
8213                 (1- (point))
8214               (point)))
8215     (when gnus-article-button-face
8216       (gnus-overlay-put (gnus-make-overlay b e nil t)
8217                         'face gnus-article-button-face))
8218     (widget-convert-button
8219      'link b e
8220      :action 'gnus-button-next-page
8221      :button-keymap gnus-next-page-map)))
8222
8223 (defun gnus-article-button-next-page (arg)
8224   "Go to the next page."
8225   (interactive "P")
8226   (let ((win (selected-window)))
8227     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8228     (gnus-article-next-page)
8229     (select-window win)))
8230
8231 (defun gnus-article-button-prev-page (arg)
8232   "Go to the prev page."
8233   (interactive "P")
8234   (let ((win (selected-window)))
8235     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8236     (gnus-article-prev-page)
8237     (select-window win)))
8238
8239 (defvar gnus-decode-header-methods
8240   '(mail-decode-encoded-word-region)
8241   "List of methods used to decode headers.
8242
8243 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8244 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8245 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8246 whose names match REGEXP.
8247
8248 For example:
8249 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8250  mail-decode-encoded-word-region
8251  (\"chinese\" . rfc1843-decode-region))
8252 ")
8253
8254 (defvar gnus-decode-header-methods-cache nil)
8255
8256 (defun gnus-multi-decode-header (start end)
8257   "Apply the functions from `gnus-encoded-word-methods' that match."
8258   (unless (and gnus-decode-header-methods-cache
8259                (eq gnus-newsgroup-name
8260                    (car gnus-decode-header-methods-cache)))
8261     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8262     (dolist (x gnus-decode-header-methods)
8263       (if (symbolp x)
8264           (nconc gnus-decode-header-methods-cache (list x))
8265         (if (and gnus-newsgroup-name
8266                  (string-match (car x) gnus-newsgroup-name))
8267             (nconc gnus-decode-header-methods-cache
8268                    (list (cdr x)))))))
8269   (let ((xlist gnus-decode-header-methods-cache))
8270     (pop xlist)
8271     (save-restriction
8272       (narrow-to-region start end)
8273       (while xlist
8274         (funcall (pop xlist) (point-min) (point-max))))))
8275
8276 ;;;
8277 ;;; Treatment top-level handling.
8278 ;;;
8279
8280 (defvar gnus-inhibit-article-treatments nil)
8281
8282 (defun gnus-treat-article (gnus-treat-condition
8283                            &optional part-number total-parts gnus-treat-type)
8284   (let ((gnus-treat-length (- (point-max) (point-min)))
8285         (alist gnus-treatment-function-alist)
8286         (article-goto-body-goes-to-point-min-p t)
8287         (treated-type
8288          (or (not gnus-treat-type)
8289              (catch 'found
8290                (let ((list gnus-article-treat-types))
8291                  (while list
8292                    (when (string-match (pop list) gnus-treat-type)
8293                      (throw 'found t)))))))
8294         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8295         val elem)
8296     (gnus-run-hooks 'gnus-part-display-hook)
8297     (dolist (elem alist)
8298       (setq val
8299             (save-excursion
8300               (when (gnus-buffer-live-p gnus-summary-buffer)
8301                 (set-buffer gnus-summary-buffer))
8302               (symbol-value (car elem))))
8303       (when (and (or (consp val)
8304                      treated-type)
8305                  (or (not gnus-inhibit-article-treatments)
8306                      (eq gnus-treat-condition 'head))
8307                  (gnus-treat-predicate val)
8308                  (or (not (get (car elem) 'highlight))
8309                      highlightp))
8310         (save-restriction
8311           (funcall (cadr elem)))))))
8312
8313 ;; Dynamic variables.
8314 (defvar part-number)
8315 (defvar total-parts)
8316 (defvar gnus-treat-type)
8317 (defvar gnus-treat-condition)
8318 (defvar gnus-treat-length)
8319
8320 (defun gnus-treat-predicate (val)
8321   (cond
8322    ((null val)
8323     nil)
8324    (gnus-treat-condition
8325     (eq gnus-treat-condition val))
8326    ((and (listp val)
8327          (stringp (car val)))
8328     (apply 'gnus-or (mapcar `(lambda (s)
8329                                (string-match s ,(or gnus-newsgroup-name "")))
8330                             val)))
8331    ((listp val)
8332     (let ((pred (pop val)))
8333       (cond
8334        ((eq pred 'or)
8335         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8336        ((eq pred 'and)
8337         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8338        ((eq pred 'not)
8339         (not (gnus-treat-predicate (car val))))
8340        ((eq pred 'typep)
8341         (equal (car val) gnus-treat-type))
8342        (t
8343         (error "%S is not a valid predicate" pred)))))
8344    ((eq val t)
8345     t)
8346    ((eq val 'head)
8347     nil)
8348    ((eq val 'first)
8349     (eq part-number 1))
8350    ((eq val 'last)
8351     (eq part-number total-parts))
8352    ((numberp val)
8353     (< gnus-treat-length val))
8354    (t
8355     (error "%S is not a valid value" val))))
8356
8357 (defun gnus-article-encrypt-body (protocol &optional n)
8358   "Encrypt the article body."
8359   (interactive
8360    (list
8361     (or gnus-article-encrypt-protocol
8362         (gnus-completing-read "Encrypt protocol"
8363                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8364                               t))
8365     current-prefix-arg))
8366   ;; User might hit `K E' instead of `K e', so prompt once.
8367   (when (and gnus-article-encrypt-protocol
8368              gnus-novice-user)
8369     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8370       (error "Encrypt aborted")))
8371   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8372     (unless func
8373       (error "Can't find the encrypt protocol %s" protocol))
8374     (if (member gnus-newsgroup-name '("nndraft:delayed"
8375                                       "nndraft:drafts"
8376                                       "nndraft:queue"))
8377         (error "Can't encrypt the article in group %s"
8378                gnus-newsgroup-name))
8379     (gnus-summary-iterate n
8380       (with-current-buffer gnus-summary-buffer
8381         (let ((mail-parse-charset gnus-newsgroup-charset)
8382               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8383               (summary-buffer gnus-summary-buffer)
8384               references point)
8385           (gnus-set-global-variables)
8386           (when (gnus-group-read-only-p)
8387             (error "The current newsgroup does not support article encrypt"))
8388           (gnus-summary-show-article t)
8389           (setq references
8390               (or (mail-header-references gnus-current-headers) ""))
8391           (set-buffer gnus-article-buffer)
8392           (let* ((inhibit-read-only t)
8393                  (headers
8394                   (mapcar (lambda (field)
8395                             (and (save-restriction
8396                                    (message-narrow-to-head)
8397                                    (goto-char (point-min))
8398                                    (search-forward field nil t))
8399                                  (prog2
8400                                      (message-narrow-to-field)
8401                                      (buffer-string)
8402                                    (delete-region (point-min) (point-max))
8403                                    (widen))))
8404                           '("Content-Type:" "Content-Transfer-Encoding:"
8405                             "Content-Disposition:"))))
8406             (message-narrow-to-head)
8407             (message-remove-header "MIME-Version")
8408             (goto-char (point-max))
8409             (setq point (point))
8410             (insert (apply 'concat headers))
8411             (widen)
8412             (narrow-to-region point (point-max))
8413             (let ((message-options message-options))
8414               (message-options-set 'message-sender user-mail-address)
8415               (message-options-set 'message-recipients user-mail-address)
8416               (message-options-set 'message-sign-encrypt 'not)
8417               (funcall func))
8418             (goto-char (point-min))
8419             (insert "MIME-Version: 1.0\n")
8420             (widen)
8421             (gnus-summary-edit-article-done
8422              references nil summary-buffer t))
8423           (when gnus-keep-backlog
8424             (gnus-backlog-remove-article
8425              (car gnus-article-current) (cdr gnus-article-current)))
8426           (gnus-flush-original-article-buffer)
8427           (when gnus-use-cache
8428             (gnus-cache-update-article
8429              (car gnus-article-current) (cdr gnus-article-current))))))))
8430
8431 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8432   "The following specs can be used:
8433 %t  The security MIME type
8434 %i  Additional info
8435 %d  Details
8436 %D  Details if button is pressed")
8437
8438 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8439   "The following specs can be used:
8440 %t  The security MIME type
8441 %i  Additional info
8442 %d  Details
8443 %D  Details if button is pressed")
8444
8445 (defvar gnus-mime-security-button-line-format-alist
8446   '((?t gnus-tmp-type ?s)
8447     (?i gnus-tmp-info ?s)
8448     (?d gnus-tmp-details ?s)
8449     (?D gnus-tmp-pressed-details ?s)))
8450
8451 (defvar gnus-mime-security-button-commands
8452   '((gnus-article-press-button "\r" "Show Detail")
8453     (undefined "v")
8454     (undefined "t")
8455     (undefined "C")
8456     (gnus-mime-security-save-part "o" "Save...")
8457     (undefined "\C-o")
8458     (undefined "r")
8459     (undefined "d")
8460     (undefined "c")
8461     (undefined "i")
8462     (undefined "E")
8463     (undefined "e")
8464     (undefined "p")
8465     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8466     (undefined ".")))
8467
8468 (defvar gnus-mime-security-button-map
8469   (let ((map (make-sparse-keymap)))
8470     (define-key map gnus-mouse-2 'gnus-article-push-button)
8471     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8472     (dolist (c gnus-mime-security-button-commands)
8473       (define-key map (cadr c) (car c)))
8474     map))
8475
8476 (easy-menu-define
8477   gnus-mime-security-button-menu gnus-mime-security-button-map
8478   "Security button menu."
8479   `("Security Part"
8480     ,@(delq nil
8481             (mapcar (lambda (c)
8482                       (unless (eq (car c) 'undefined)
8483                         (vector (caddr c) (car c) :active t)))
8484                     gnus-mime-security-button-commands))))
8485
8486 (defun gnus-mime-security-button-menu (event prefix)
8487   "Construct a context-sensitive menu of security commands."
8488   (interactive "e\nP")
8489   (save-window-excursion
8490     (let ((pos (event-start event)))
8491       (select-window (posn-window pos))
8492       (goto-char (posn-point pos))
8493       (gnus-article-check-buffer)
8494       (popup-menu gnus-mime-security-button-menu nil prefix))))
8495
8496 (defvar gnus-mime-security-details-buffer nil)
8497
8498 (defvar gnus-mime-security-button-pressed nil)
8499
8500 (defvar gnus-mime-security-show-details-inline t
8501   "If non-nil, show details in the article buffer.")
8502
8503 (defun gnus-mime-security-verify-or-decrypt (handle)
8504   (mm-remove-parts (cdr handle))
8505   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8506         point (inhibit-read-only t))
8507     (if region
8508         (goto-char (car region)))
8509     (setq point (point))
8510     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8511       (let* ((mm-verify-option 'known)
8512              (mm-decrypt-option 'known)
8513              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8514         (unless (eq nparts (cdr handle))
8515           (mm-destroy-parts (cdr handle))
8516           (setcdr handle nparts))))
8517     (gnus-mime-display-security handle)
8518     (when region
8519       (delete-region (point) (cdr region))
8520       (set-marker (car region) nil)
8521       (set-marker (cdr region) nil))
8522     (goto-char point)))
8523
8524 (defun gnus-mime-security-show-details (handle)
8525   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8526     (if (not details)
8527         (gnus-message 5 "No details.")
8528       (if gnus-mime-security-show-details-inline
8529           (let ((gnus-mime-security-button-pressed
8530                  (not (get-text-property (point) 'gnus-mime-details)))
8531                 (gnus-mime-security-button-line-format
8532                  (get-text-property (point) 'gnus-line-format))
8533                 (inhibit-read-only t))
8534             (forward-char -1)
8535             (while (eq (get-text-property (point) 'gnus-line-format)
8536                        gnus-mime-security-button-line-format)
8537               (forward-char -1))
8538             (forward-char)
8539             (save-restriction
8540               (narrow-to-region (point) (point))
8541               (gnus-insert-mime-security-button handle))
8542             (delete-region (point)
8543                            (or (text-property-not-all
8544                                 (point) (point-max)
8545                                 'gnus-line-format
8546                                 gnus-mime-security-button-line-format)
8547                                (point-max))))
8548         ;; Not inlined.
8549         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8550             (with-current-buffer gnus-mime-security-details-buffer
8551               (erase-buffer)
8552               t)
8553           (setq gnus-mime-security-details-buffer
8554                 (gnus-get-buffer-create "*MIME Security Details*")))
8555         (with-current-buffer gnus-mime-security-details-buffer
8556           (insert details)
8557           (goto-char (point-min)))
8558         (pop-to-buffer gnus-mime-security-details-buffer)))))
8559
8560 (defun gnus-mime-security-press-button (handle)
8561   (save-excursion
8562     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8563         (gnus-mime-security-show-details handle)
8564       (gnus-mime-security-verify-or-decrypt handle))))
8565
8566 (defun gnus-insert-mime-security-button (handle &optional displayed)
8567   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8568          (gnus-tmp-type
8569           (concat
8570            (or (nth 2 (assoc protocol mm-verify-function-alist))
8571                (nth 2 (assoc protocol mm-decrypt-function-alist))
8572                "Unknown")
8573            (if (equal (car handle) "multipart/signed")
8574                " Signed" " Encrypted")
8575            " Part"))
8576          (gnus-tmp-info
8577           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8578               "Undecided"))
8579          (gnus-tmp-details
8580           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8581          gnus-tmp-pressed-details
8582          b e)
8583     (setq gnus-tmp-details
8584           (if gnus-tmp-details
8585               (concat "\n" gnus-tmp-details)
8586             ""))
8587     (setq gnus-tmp-pressed-details
8588           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8589     (unless (bolp)
8590       (insert "\n"))
8591     (setq b (point))
8592     (gnus-eval-format
8593      gnus-mime-security-button-line-format
8594      gnus-mime-security-button-line-format-alist
8595      `(keymap ,gnus-mime-security-button-map
8596          gnus-callback gnus-mime-security-press-button
8597          gnus-line-format ,gnus-mime-security-button-line-format
8598          gnus-mime-details ,gnus-mime-security-button-pressed
8599          article-type annotation
8600          gnus-data ,handle))
8601     (setq e (if (bolp)
8602                 ;; Exclude a newline.
8603                 (1- (point))
8604               (point)))
8605     (when gnus-article-button-face
8606       (gnus-overlay-put (gnus-make-overlay b e nil t)
8607                         'face gnus-article-button-face))
8608     (widget-convert-button
8609      'link b e
8610      :mime-handle handle
8611      :action 'gnus-widget-press-button
8612      :button-keymap gnus-mime-security-button-map
8613      :help-echo
8614      (lambda (widget)
8615        ;; Needed to properly clear the message due to a bug in
8616        ;; wid-edit (XEmacs only).
8617        (when (boundp 'help-echo-owns-message)
8618          (setq help-echo-owns-message t))
8619        (format
8620         "%S: show detail; %S: more options"
8621         (aref gnus-mouse-2 0)
8622         (aref gnus-down-mouse-3 0))))))
8623
8624 (defun gnus-mime-display-security (handle)
8625   (save-restriction
8626     (narrow-to-region (point) (point))
8627     (unless (gnus-unbuttonized-mime-type-p (car handle))
8628       (gnus-insert-mime-security-button handle))
8629     (gnus-mime-display-part (cadr handle))
8630     (unless (bolp)
8631       (insert "\n"))
8632     (unless (gnus-unbuttonized-mime-type-p (car handle))
8633       (let ((gnus-mime-security-button-line-format
8634              gnus-mime-security-button-end-line-format))
8635         (gnus-insert-mime-security-button handle)))
8636     (mm-set-handle-multipart-parameter
8637      handle 'gnus-region
8638      (cons (set-marker (make-marker) (point-min))
8639            (set-marker (make-marker) (point-max))))
8640     (goto-char (point-max))))
8641
8642 (defun gnus-mime-security-run-function (function)
8643   "Run FUNCTION with the security part under point."
8644   (gnus-article-check-buffer)
8645   (let ((data (get-text-property (point) 'gnus-data))
8646         buffer handle)
8647     (when (and (stringp (car-safe data))
8648                (setq buffer (mm-handle-multipart-original-buffer data))
8649                (setq handle (cadr data)))
8650       (if (bufferp (mm-handle-buffer handle))
8651           (progn
8652             (setq handle (cons buffer (copy-sequence (cdr handle))))
8653             (mm-handle-set-undisplayer handle nil))
8654         (setq handle (mm-make-handle
8655                       buffer
8656                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8657                       nil nil nil nil nil nil)))
8658       (funcall function handle))))
8659
8660 (defun gnus-mime-security-save-part ()
8661   "Save the security part under point."
8662   (interactive)
8663   (gnus-mime-security-run-function 'mm-save-part))
8664
8665 (defun gnus-mime-security-pipe-part ()
8666   "Pipe the security part under point to a process."
8667   (interactive)
8668   (gnus-mime-security-run-function 'mm-pipe-part))
8669
8670 (gnus-ems-redefine)
8671
8672 (provide 'gnus-art)
8673
8674 (run-hooks 'gnus-art-load-hook)
8675
8676 ;;; gnus-art.el ends here