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